]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-4032l/protocol.c
hantek-4032l: Drop some unneeded malloc checks.
[libsigrok.git] / src / hardware / hantek-4032l / protocol.c
CommitLineData
6a25fa42
AZ
1/*
2 * This file is part of the libsigrok project.
3 *
5089a143
AZ
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>
6a25fa42
AZ
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
5089a143
AZ
25#define H4032L_USB_TIMEOUT 500
26
27enum h4032l_cmd {
74c4c174
AV
28 CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */
29 CMD_CONFIGURE = 0x2b1a,
5089a143
AZ
30 CMD_STATUS = 0x4b3a,
31 CMD_GET = 0x6b5a
32};
33
e80e1858 34struct h4032l_status_packet {
5089a143
AZ
35 uint32_t magic;
36 uint32_t values;
37 uint32_t status;
2bd5d17c
UH
38 uint32_t usbxi_data;
39 uint32_t fpga_version;
5089a143
AZ
40};
41
2958315d
AV
42static 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
a5b9880e
AV
56static void finish_acquisition(struct sr_dev_inst *sdi)
57{
2958315d 58 struct dev_context *devc = sdi->priv;
a5b9880e
AV
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);
2958315d
AV
63
64 devc->num_transfers = 0;
65 g_free(devc->transfers);
a5b9880e
AV
66}
67
68static void free_transfer(struct libusb_transfer *transfer)
69{
70 struct sr_dev_inst *sdi = transfer->user_data;
71 struct dev_context *devc = sdi->priv;
2958315d
AV
72 unsigned int i;
73
74 if ((transfer->buffer != (unsigned char *)&devc->cmd_pkt) &&
75 (transfer->buffer != devc->buffer)) {
76 g_free(transfer->buffer);
77 }
a5b9880e
AV
78
79 transfer->buffer = NULL;
80 libusb_free_transfer(transfer);
a5b9880e 81
2958315d
AV
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);
a5b9880e
AV
91}
92
43d86035
AV
93static 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
3dc976fe
AV
104static 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
5089a143 146SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
6a25fa42 147{
5089a143
AZ
148 struct timeval tv;
149 struct drv_context *drvc;
6a25fa42
AZ
150
151 (void)fd;
5089a143
AZ
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);
6a25fa42 158
5089a143
AZ
159 return TRUE;
160}
6a25fa42 161
43d86035 162void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
5089a143 163{
3dc976fe
AV
164 struct sr_dev_inst *const sdi = transfer->user_data;
165 struct dev_context *const devc = sdi->priv;
43d86035 166 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
43d86035
AV
167 uint32_t *buffer;
168 uint32_t number_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 buffer = (uint32_t *)transfer->buffer;
189
190 number_samples = (devc->remaining_samples < max_samples) ?
191 devc->remaining_samples : max_samples;
192 devc->remaining_samples -= number_samples;
3dc976fe 193 send_data(sdi, buffer, number_samples);
43d86035
AV
194 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
195 buffer[0], buffer[1]);
196
197 /* Close data receiving. */
198 if (devc->remaining_samples == 0) {
43d86035
AV
199 if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
200 sr_err("Mismatch magic number of end poll.");
2958315d
AV
201
202 abort_acquisition(devc);
203 free_transfer(transfer);
43d86035 204 } else {
2958315d
AV
205 if (((devc->submitted_transfers - 1) * H4032L_DATA_BUFFER_SIZE) <
206 (int32_t)(devc->remaining_samples * sizeof(uint32_t)))
207 resubmit_transfer(transfer);
208 else
209 free_transfer(transfer);
43d86035
AV
210 }
211}
212
213void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
214{
3dc976fe
AV
215 struct sr_dev_inst *const sdi = transfer->user_data;
216 struct dev_context *const devc = sdi->priv;
5089a143
AZ
217 struct sr_usb_dev_inst *usb = sdi->conn;
218 gboolean cmd = FALSE;
43d86035 219 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
5089a143
AZ
220 uint32_t *buffer;
221 struct h4032l_status_packet *status;
5089a143
AZ
222 uint32_t number_samples;
223 int ret;
6a25fa42 224
a5b9880e
AV
225 /*
226 * If acquisition has already ended, just free any queued up
227 * transfers that come in.
228 */
229 if (devc->acq_aborted) {
230 free_transfer(transfer);
231 return;
232 }
233
74c4c174
AV
234 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
235 sr_dbg("%s error: %d.", __func__, transfer->status);
6a25fa42 236
5089a143
AZ
237 buffer = (uint32_t *)transfer->buffer;
238
239 switch (devc->status) {
240 case H4032L_STATUS_IDLE:
241 sr_err("USB callback called in idle.");
242 break;
243 case H4032L_STATUS_CMD_CONFIGURE:
244 /* Select status request as next. */
245 cmd = TRUE;
246 devc->cmd_pkt.cmd = CMD_STATUS;
247 devc->status = H4032L_STATUS_CMD_STATUS;
248 break;
249 case H4032L_STATUS_CMD_STATUS:
250 /* Select status request as next. */
251 devc->status = H4032L_STATUS_RESPONSE_STATUS;
252 break;
253 case H4032L_STATUS_RESPONSE_STATUS:
254 /*
255 * Check magic and if status is complete, then select
256 * First Transfer as next.
257 */
258 status = (struct h4032l_status_packet *)transfer->buffer;
28f2d07f 259 if (status->magic != H4032L_STATUS_PACKET_MAGIC)
74c4c174 260 devc->status = H4032L_STATUS_RESPONSE_STATUS;
28f2d07f 261 else if (status->status == 2)
5089a143 262 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
28f2d07f 263 else
5089a143 264 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
5089a143
AZ
265 break;
266 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
267 devc->status = H4032L_STATUS_CMD_STATUS;
268 devc->cmd_pkt.cmd = CMD_STATUS;
269 cmd = TRUE;
270 break;
271 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
272 devc->status = H4032L_STATUS_CMD_GET;
273 devc->cmd_pkt.cmd = CMD_GET;
274 cmd = TRUE;
275 break;
276 case H4032L_STATUS_CMD_GET:
277 devc->status = H4032L_STATUS_FIRST_TRANSFER;
43d86035
AV
278 /* Trigger has been captured. */
279 std_session_send_df_header(sdi);
5089a143
AZ
280 break;
281 case H4032L_STATUS_FIRST_TRANSFER:
74c4c174 282 /* Drop packets until H4032L_START_PACKET_MAGIC. */
5089a143 283 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
74c4c174 284 sr_dbg("Mismatch magic number of start poll.");
5089a143
AZ
285 break;
286 }
287 devc->status = H4032L_STATUS_TRANSFER;
288 max_samples--;
289 buffer++;
97200156 290 /* Fallthrough. */
5089a143 291 case H4032L_STATUS_TRANSFER:
28f2d07f 292 number_samples = (devc->remaining_samples < max_samples) ?
43d86035 293 devc->remaining_samples : max_samples;
5089a143 294 devc->remaining_samples -= number_samples;
3dc976fe 295 send_data(sdi, buffer, number_samples);
5089a143 296 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
28f2d07f 297 buffer[0], buffer[1]);
5089a143
AZ
298 break;
299 }
300
43d86035
AV
301 /* Start data receiving. */
302 if (devc->status == H4032L_STATUS_TRANSFER) {
303 if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) {
304 sr_err("Can not start data transfers: %d", ret);
305 devc->status = H4032L_STATUS_IDLE;
306 }
307 } else if (devc->status != H4032L_STATUS_IDLE) {
5089a143
AZ
308 if (cmd) {
309 /* Setup new USB cmd packet, reuse transfer object. */
310 sr_dbg("New command: %d.", devc->status);
311 libusb_fill_bulk_transfer(transfer, usb->devhdl,
312 2 | LIBUSB_ENDPOINT_OUT,
313 (unsigned char *)&devc->cmd_pkt,
314 sizeof(struct h4032l_cmd_pkt),
28f2d07f
AV
315 h4032l_usb_callback,
316 (void *)sdi, H4032L_USB_TIMEOUT);
5089a143
AZ
317 } else {
318 /* Setup new USB poll packet, reuse transfer object. */
319 sr_dbg("Poll: %d.", devc->status);
320 libusb_fill_bulk_transfer(transfer, usb->devhdl,
321 6 | LIBUSB_ENDPOINT_IN,
322 devc->buffer, ARRAY_SIZE(devc->buffer),
323 h4032l_usb_callback,
324 (void *)sdi, H4032L_USB_TIMEOUT);
325 }
4868f15a 326 /* Send prepared USB packet. */
5089a143
AZ
327 if ((ret = libusb_submit_transfer(transfer)) != 0) {
328 sr_err("Failed to submit transfer: %s.",
329 libusb_error_name(ret));
330 devc->status = H4032L_STATUS_IDLE;
331 }
332 } else {
333 sr_dbg("Now idle.");
334 }
335
336 if (devc->status == H4032L_STATUS_IDLE)
a5b9880e 337 free_transfer(transfer);
5089a143
AZ
338}
339
340uint16_t h4032l_voltage2pwm(double voltage)
341{
342 /*
343 * word PwmA - channel A Vref PWM value, pseudocode:
344 * -6V < ThresholdVoltage < +6V
345 * Vref = 1.8 - ThresholdVoltage
346 * if Vref > 10.0
347 * Vref = 10.0
348 * if Vref < -5.0
349 * Vref = -5.0
350 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
351 * if pwm > 4095
352 * pwm = 4095
353 */
354 voltage = 1.8 - voltage;
355 if (voltage > 10.0)
356 voltage = 10.0;
357 else if (voltage < -5.0)
358 voltage = -5.0;
359
360 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
361}
362
43d86035
AV
363SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
364{
365 struct dev_context *devc = sdi->priv;
366 struct sr_usb_dev_inst *usb = sdi->conn;
367 struct libusb_transfer *transfer;
2958315d
AV
368 uint8_t *buffer;
369 unsigned int num_transfers;
370 unsigned int i;
43d86035
AV
371 int ret;
372
2958315d 373 devc->submitted_transfers = 0;
43d86035 374
2958315d
AV
375 /*
376 * Set number of data transfers regarding to size of buffer.
377 * FPGA version 0 can't transfer multiple transfers at once.
378 */
379 if ((num_transfers = MIN(devc->remaining_samples * sizeof(uint32_t) /
380 H4032L_DATA_BUFFER_SIZE, devc->fpga_version ?
381 H4032L_DATA_TRANSFER_MAX_NUM : 1)) == 0)
382 num_transfers = 1;
383
384 g_free(devc->transfers);
2dcd904c 385 devc->transfers = g_malloc(sizeof(*devc->transfers) * num_transfers);
2958315d 386 devc->num_transfers = num_transfers;
2dcd904c 387
2958315d 388 for (i = 0; i < num_transfers; i++) {
2dcd904c 389 buffer = g_malloc(H4032L_DATA_BUFFER_SIZE);
2958315d
AV
390 transfer = libusb_alloc_transfer(0);
391
392 libusb_fill_bulk_transfer(transfer, usb->devhdl,
393 6 | LIBUSB_ENDPOINT_IN,
394 buffer, H4032L_DATA_BUFFER_SIZE,
395 h4032l_data_transfer_callback,
396 (void *)sdi, H4032L_USB_TIMEOUT);
397
398 /* Send prepared usb packet. */
399 if ((ret = libusb_submit_transfer(transfer)) != 0) {
400 sr_err("Failed to submit transfer: %s.",
401 libusb_error_name(ret));
402 libusb_free_transfer(transfer);
403 g_free(buffer);
404 abort_acquisition(devc);
405 return SR_ERR;
406 }
407 devc->transfers[i] = transfer;
408 devc->submitted_transfers++;
409 }
43d86035 410
2958315d 411 return SR_OK;
43d86035
AV
412}
413
5089a143
AZ
414SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
415{
416 struct dev_context *devc = sdi->priv;
417 struct sr_usb_dev_inst *usb = sdi->conn;
418 struct libusb_transfer *transfer;
74c4c174 419 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5089a143
AZ
420 int ret;
421
74c4c174
AV
422 /* Send reset command to arm the logic analyzer. */
423 if ((ret = libusb_control_transfer(usb->devhdl,
424 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
425 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) {
28f2d07f
AV
426 sr_err("Failed to send vendor request %s.",
427 libusb_error_name(ret));
74c4c174
AV
428 return SR_ERR;
429 }
430
431 /* Wait for reset vendor request. */
432 g_usleep(20 * 1000);
433
434 /* Send configure command. */
5089a143
AZ
435 devc->cmd_pkt.cmd = CMD_CONFIGURE;
436 devc->status = H4032L_STATUS_CMD_CONFIGURE;
437 devc->remaining_samples = devc->cmd_pkt.sample_size;
438
439 transfer = libusb_alloc_transfer(0);
440
441 libusb_fill_bulk_transfer(transfer, usb->devhdl,
442 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
443 sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
444 (void *)sdi, H4032L_USB_TIMEOUT);
445
446 if ((ret = libusb_submit_transfer(transfer)) != 0) {
28f2d07f
AV
447 sr_err("Failed to submit transfer: %s.",
448 libusb_error_name(ret));
5089a143
AZ
449 libusb_free_transfer(transfer);
450 return SR_ERR;
451 }
452
2958315d 453 devc->transfers = g_malloc0(sizeof(*devc->transfers));
2958315d
AV
454 devc->submitted_transfers++;
455 devc->num_transfers = 1;
456 devc->transfers[0] = transfer;
457
458 return SR_OK;
459}
460
461SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi)
462{
463 abort_acquisition(sdi->priv);
464
5089a143
AZ
465 return SR_OK;
466}
467
468SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
469{
470 struct drv_context *drvc = sdi->driver->context;
471 struct sr_usb_dev_inst *usb = sdi->conn;
472 struct libusb_device_descriptor des;
473 libusb_device **devlist;
474 int ret = SR_ERR, i, device_count;
475 char connection_id[64];
476
477 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
478 if (device_count < 0) {
479 sr_err("Failed to get device list: %s.",
480 libusb_error_name(device_count));
481 return SR_ERR;
482 }
483
484 for (i = 0; i < device_count; i++) {
485 libusb_get_device_descriptor(devlist[i], &des);
486
487 if (des.idVendor != H4032L_USB_VENDOR ||
488 des.idProduct != H4032L_USB_PRODUCT)
489 continue;
490
491 if ((sdi->status == SR_ST_INITIALIZING) ||
492 (sdi->status == SR_ST_INACTIVE)) {
493 /* Check device by its physical USB bus/port address. */
28f2d07f
AV
494 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
495 continue;
496
5089a143
AZ
497 if (strcmp(sdi->connection_id, connection_id))
498 /* This is not the one. */
499 continue;
500 }
501
502 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
503 if (usb->address == 0xff)
504 /*
505 * First time we touch this device after FW
506 * upload, so we don't know the address yet.
507 */
508 usb->address =
28f2d07f 509 libusb_get_device_address(devlist[i]);
5089a143
AZ
510 } else {
511 sr_err("Failed to open device: %s.",
512 libusb_error_name(ret));
513 ret = SR_ERR;
514 break;
515 }
516
517 ret = SR_OK;
518 break;
519 }
520
521 libusb_free_device_list(devlist, 1);
522 return ret;
6a25fa42 523}
7a7afc00
AV
524
525SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
526{
527 struct dev_context *devc = sdi->priv;
528 struct sr_usb_dev_inst *usb = sdi->conn;
529 struct h4032l_status_packet *status;
530 int transferred;
531 int i, ret;
532
533 /* Set command to status. */
534 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
535 devc->cmd_pkt.cmd = CMD_STATUS;
536
537 /* Send status request. */
538 if ((ret = libusb_bulk_transfer(usb->devhdl,
539 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
540 sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) {
541 sr_err("Unable to send FPGA version request: %s.",
542 libusb_error_name(ret));
543 return SR_ERR;
544 }
545
546 /* Attempt to get FGPA version. */
547 for (i = 0; i < 10; i++) {
548 if ((ret = libusb_bulk_transfer(usb->devhdl,
549 6 | LIBUSB_ENDPOINT_IN, devc->buffer,
550 ARRAY_SIZE(devc->buffer), &transferred, H4032L_USB_TIMEOUT)) < 0) {
551 sr_err("Unable to receive FPGA version: %s.",
552 libusb_error_name(ret));
553 return SR_ERR;
554 }
555 status = (struct h4032l_status_packet *)devc->buffer;
556 if (status->magic == H4032L_STATUS_PACKET_MAGIC) {
557 sr_dbg("FPGA version: 0x%x.", status->fpga_version);
558 devc->fpga_version = status->fpga_version;
559 return SR_OK;
560 }
561 }
562
563 sr_err("Unable to get FPGA version.");
564
565 return SR_ERR;
566}