]> sigrok.org Git - libsigrok.git/blob - src/hardware/fx2lafw/protocol.c
fx2lafw: scan/firmware support for DSLogic.
[libsigrok.git] / src / hardware / fx2lafw / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <glib.h>
22 #include <glib/gstdio.h>
23 #include "protocol.h"
24
25 #pragma pack(push, 1)
26
27 struct version_info {
28         uint8_t major;
29         uint8_t minor;
30 };
31
32 struct cmd_start_acquisition {
33         uint8_t flags;
34         uint8_t sample_delay_h;
35         uint8_t sample_delay_l;
36 };
37
38 #pragma pack(pop)
39
40 static int command_get_fw_version(libusb_device_handle *devhdl,
41                                   struct version_info *vi)
42 {
43         int ret;
44
45         ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
46                 LIBUSB_ENDPOINT_IN, CMD_GET_FW_VERSION, 0x0000, 0x0000,
47                 (unsigned char *)vi, sizeof(struct version_info), 100);
48
49         if (ret < 0) {
50                 sr_err("Unable to get version info: %s.",
51                        libusb_error_name(ret));
52                 return SR_ERR;
53         }
54
55         return SR_OK;
56 }
57
58 static int command_get_revid_version(struct sr_dev_inst *sdi, uint8_t *revid)
59 {
60         struct dev_context *devc = sdi->priv;
61         struct sr_usb_dev_inst *usb = sdi->conn;
62         libusb_device_handle *devhdl = usb->devhdl;
63         int cmd, ret;
64
65         cmd = devc->dslogic ? CMD_DSLOGIC_GET_REVID_VERSION : CMD_GET_REVID_VERSION;
66         ret = libusb_control_transfer(devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
67                 LIBUSB_ENDPOINT_IN, cmd, 0x0000, 0x0000, revid, 1, 100);
68
69         if (ret < 0) {
70                 sr_err("Unable to get REVID: %s.", libusb_error_name(ret));
71                 return SR_ERR;
72         }
73
74         return SR_OK;
75 }
76
77 SR_PRIV int fx2lafw_command_start_acquisition(const struct sr_dev_inst *sdi)
78 {
79         struct dev_context *devc;
80         struct sr_usb_dev_inst *usb;
81         uint64_t samplerate;
82         struct cmd_start_acquisition cmd;
83         int delay, ret;
84
85         devc = sdi->priv;
86         usb = sdi->conn;
87         samplerate = devc->cur_samplerate;
88
89         /* Compute the sample rate. */
90         if (devc->sample_wide && samplerate > MAX_16BIT_SAMPLE_RATE) {
91                 sr_err("Unable to sample at %" PRIu64 "Hz "
92                        "when collecting 16-bit samples.", samplerate);
93                 return SR_ERR;
94         }
95
96         delay = 0;
97         cmd.flags = cmd.sample_delay_h = cmd.sample_delay_l = 0;
98         if ((SR_MHZ(48) % samplerate) == 0) {
99                 cmd.flags = CMD_START_FLAGS_CLK_48MHZ;
100                 delay = SR_MHZ(48) / samplerate - 1;
101                 if (delay > MAX_SAMPLE_DELAY)
102                         delay = 0;
103         }
104
105         if (delay == 0 && (SR_MHZ(30) % samplerate) == 0) {
106                 cmd.flags = CMD_START_FLAGS_CLK_30MHZ;
107                 delay = SR_MHZ(30) / samplerate - 1;
108         }
109
110         sr_info("GPIF delay = %d, clocksource = %sMHz.", delay,
111                 (cmd.flags & CMD_START_FLAGS_CLK_48MHZ) ? "48" : "30");
112
113         if (delay <= 0 || delay > MAX_SAMPLE_DELAY) {
114                 sr_err("Unable to sample at %" PRIu64 "Hz.", samplerate);
115                 return SR_ERR;
116         }
117
118         cmd.sample_delay_h = (delay >> 8) & 0xff;
119         cmd.sample_delay_l = delay & 0xff;
120
121         /* Select the sampling width. */
122         cmd.flags |= devc->sample_wide ? CMD_START_FLAGS_SAMPLE_16BIT :
123                 CMD_START_FLAGS_SAMPLE_8BIT;
124
125         /* Send the control message. */
126         ret = libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR |
127                         LIBUSB_ENDPOINT_OUT, CMD_START, 0x0000, 0x0000,
128                         (unsigned char *)&cmd, sizeof(cmd), 100);
129         if (ret < 0) {
130                 sr_err("Unable to send start command: %s.",
131                        libusb_error_name(ret));
132                 return SR_ERR;
133         }
134
135         return SR_OK;
136 }
137
138 /**
139  * Check the USB configuration to determine if this is an fx2lafw device.
140  *
141  * @return TRUE if the device's configuration profile matches fx2lafw
142  * configuration, FALSE otherwise.
143  */
144 SR_PRIV gboolean match_manuf_prod(libusb_device *dev, const char *manufacturer,
145                 const char *product)
146 {
147         struct libusb_device_descriptor des;
148         struct libusb_device_handle *hdl;
149         gboolean ret;
150         unsigned char strdesc[64];
151
152         hdl = NULL;
153         ret = FALSE;
154         while (!ret) {
155                 /* Assume the FW has not been loaded, unless proven wrong. */
156                 if (libusb_get_device_descriptor(dev, &des) != 0)
157                         break;
158
159                 if (libusb_open(dev, &hdl) != 0)
160                         break;
161
162                 if (libusb_get_string_descriptor_ascii(hdl,
163                                 des.iManufacturer, strdesc, sizeof(strdesc)) < 0)
164                         break;
165                 if (strncmp((const char *)strdesc, manufacturer, 6))
166                         break;
167
168                 if (libusb_get_string_descriptor_ascii(hdl,
169                                 des.iProduct, strdesc, sizeof(strdesc)) < 0)
170                         break;
171                 if (strncmp((const char *)strdesc, product, 7))
172                         break;
173
174                 ret = TRUE;
175         }
176         if (hdl)
177                 libusb_close(hdl);
178
179         return ret;
180 }
181
182 SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
183 {
184         libusb_device **devlist;
185         struct sr_usb_dev_inst *usb;
186         struct libusb_device_descriptor des;
187         struct dev_context *devc;
188         struct drv_context *drvc;
189         struct version_info vi;
190         int ret, i, device_count;
191         uint8_t revid;
192         char connection_id[64];
193
194         drvc = di->priv;
195         devc = sdi->priv;
196         usb = sdi->conn;
197
198         if (sdi->status == SR_ST_ACTIVE)
199                 /* Device is already in use. */
200                 return SR_ERR;
201
202         device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
203         if (device_count < 0) {
204                 sr_err("Failed to get device list: %s.",
205                        libusb_error_name(device_count));
206                 return SR_ERR;
207         }
208
209         for (i = 0; i < device_count; i++) {
210                 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
211                         sr_err("Failed to get device descriptor: %s.",
212                                libusb_error_name(ret));
213                         continue;
214                 }
215
216                 if (des.idVendor != devc->profile->vid
217                     || des.idProduct != devc->profile->pid)
218                         continue;
219
220                 if ((sdi->status == SR_ST_INITIALIZING) ||
221                                 (sdi->status == SR_ST_INACTIVE)) {
222                         /*
223                          * Check device by its physical USB bus/port address.
224                          */
225                         usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
226                         if (strcmp(sdi->connection_id, connection_id))
227                                 /* This is not the one. */
228                                 continue;
229                 }
230
231                 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
232                         if (usb->address == 0xff)
233                                 /*
234                                  * First time we touch this device after FW
235                                  * upload, so we don't know the address yet.
236                                  */
237                                 usb->address = libusb_get_device_address(devlist[i]);
238                 } else {
239                         sr_err("Failed to open device: %s.",
240                                libusb_error_name(ret));
241                         break;
242                 }
243
244                 ret = command_get_fw_version(usb->devhdl, &vi);
245                 if (ret != SR_OK) {
246                         sr_err("Failed to get firmware version.");
247                         break;
248                 }
249
250                 ret = command_get_revid_version(sdi, &revid);
251                 if (ret != SR_OK) {
252                         sr_err("Failed to get REVID.");
253                         break;
254                 }
255
256                 /*
257                  * Changes in major version mean incompatible/API changes, so
258                  * bail out if we encounter an incompatible version.
259                  * Different minor versions are OK, they should be compatible.
260                  */
261                 if (vi.major != FX2LAFW_REQUIRED_VERSION_MAJOR) {
262                         sr_err("Expected firmware version %d.x, "
263                                "got %d.%d.", FX2LAFW_REQUIRED_VERSION_MAJOR,
264                                vi.major, vi.minor);
265                         break;
266                 }
267
268                 sdi->status = SR_ST_ACTIVE;
269                 sr_info("Opened device on %d.%d (logical) / %s (physical), "
270                         "interface %d, firmware %d.%d.",
271                         usb->bus, usb->address, connection_id,
272                         USB_INTERFACE, vi.major, vi.minor);
273
274                 sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
275                         revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
276
277                 break;
278         }
279         libusb_free_device_list(devlist, 1);
280
281         if (sdi->status != SR_ST_ACTIVE)
282                 return SR_ERR;
283
284         return SR_OK;
285 }
286
287 SR_PRIV struct dev_context *fx2lafw_dev_new(void)
288 {
289         struct dev_context *devc;
290
291         devc = g_malloc0(sizeof(struct dev_context));
292         devc->profile = NULL;
293         devc->fw_updated = 0;
294         devc->cur_samplerate = 0;
295         devc->limit_samples = 0;
296         devc->capture_ratio = 0;
297         devc->sample_wide = FALSE;
298         devc->stl = NULL;
299
300         return devc;
301 }
302
303 SR_PRIV void fx2lafw_abort_acquisition(struct dev_context *devc)
304 {
305         int i;
306
307         devc->acq_aborted = TRUE;
308
309         for (i = devc->num_transfers - 1; i >= 0; i--) {
310                 if (devc->transfers[i])
311                         libusb_cancel_transfer(devc->transfers[i]);
312         }
313 }
314
315 static void finish_acquisition(struct sr_dev_inst *sdi)
316 {
317         struct sr_datafeed_packet packet;
318         struct dev_context *devc;
319
320         devc = sdi->priv;
321
322         /* Terminate session. */
323         packet.type = SR_DF_END;
324         sr_session_send(sdi, &packet);
325
326         /* Remove fds from polling. */
327         usb_source_remove(sdi->session, devc->ctx);
328
329         devc->num_transfers = 0;
330         g_free(devc->transfers);
331
332         if (devc->stl) {
333                 soft_trigger_logic_free(devc->stl);
334                 devc->stl = NULL;
335         }
336 }
337
338 static void free_transfer(struct libusb_transfer *transfer)
339 {
340         struct sr_dev_inst *sdi;
341         struct dev_context *devc;
342         unsigned int i;
343
344         sdi = transfer->user_data;
345         devc = sdi->priv;
346
347         g_free(transfer->buffer);
348         transfer->buffer = NULL;
349         libusb_free_transfer(transfer);
350
351         for (i = 0; i < devc->num_transfers; i++) {
352                 if (devc->transfers[i] == transfer) {
353                         devc->transfers[i] = NULL;
354                         break;
355                 }
356         }
357
358         devc->submitted_transfers--;
359         if (devc->submitted_transfers == 0)
360                 finish_acquisition(sdi);
361 }
362
363 static void resubmit_transfer(struct libusb_transfer *transfer)
364 {
365         int ret;
366
367         if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
368                 return;
369
370         sr_err("%s: %s", __func__, libusb_error_name(ret));
371         free_transfer(transfer);
372
373 }
374
375 SR_PRIV void fx2lafw_receive_transfer(struct libusb_transfer *transfer)
376 {
377         struct sr_dev_inst *sdi;
378         struct dev_context *devc;
379         gboolean packet_has_error = FALSE;
380         struct sr_datafeed_packet packet;
381         struct sr_datafeed_logic logic;
382         unsigned int num_samples;
383         int trigger_offset, cur_sample_count, unitsize;
384         int pre_trigger_samples;
385
386         sdi = transfer->user_data;
387         devc = sdi->priv;
388
389         /*
390          * If acquisition has already ended, just free any queued up
391          * transfer that come in.
392          */
393         if (devc->acq_aborted) {
394                 free_transfer(transfer);
395                 return;
396         }
397
398         sr_info("receive_transfer(): status %d received %d bytes.",
399                 transfer->status, transfer->actual_length);
400
401         /* Save incoming transfer before reusing the transfer struct. */
402         unitsize = devc->sample_wide ? 2 : 1;
403         cur_sample_count = transfer->actual_length / unitsize;
404
405         switch (transfer->status) {
406         case LIBUSB_TRANSFER_NO_DEVICE:
407                 fx2lafw_abort_acquisition(devc);
408                 free_transfer(transfer);
409                 return;
410         case LIBUSB_TRANSFER_COMPLETED:
411         case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though. */
412                 break;
413         default:
414                 packet_has_error = TRUE;
415                 break;
416         }
417
418         if (transfer->actual_length == 0 || packet_has_error) {
419                 devc->empty_transfer_count++;
420                 if (devc->empty_transfer_count > MAX_EMPTY_TRANSFERS) {
421                         /*
422                          * The FX2 gave up. End the acquisition, the frontend
423                          * will work out that the samplecount is short.
424                          */
425                         fx2lafw_abort_acquisition(devc);
426                         free_transfer(transfer);
427                 } else {
428                         resubmit_transfer(transfer);
429                 }
430                 return;
431         } else {
432                 devc->empty_transfer_count = 0;
433         }
434
435         if (devc->trigger_fired) {
436                 if (!devc->limit_samples || devc->sent_samples < devc->limit_samples) {
437                         /* Send the incoming transfer to the session bus. */
438                         packet.type = SR_DF_LOGIC;
439                         packet.payload = &logic;
440                         if (devc->limit_samples && devc->sent_samples + cur_sample_count > devc->limit_samples)
441                                 num_samples = devc->limit_samples - devc->sent_samples;
442                         else
443                                 num_samples = cur_sample_count;
444                         logic.length = num_samples * unitsize;
445                         logic.unitsize = unitsize;
446                         logic.data = transfer->buffer;
447                         sr_session_send(devc->cb_data, &packet);
448                         devc->sent_samples += num_samples;
449                 }
450         } else {
451                 trigger_offset = soft_trigger_logic_check(devc->stl,
452                         transfer->buffer, transfer->actual_length, &pre_trigger_samples);
453                 if (trigger_offset > -1) {
454                         devc->sent_samples += pre_trigger_samples;
455                         packet.type = SR_DF_LOGIC;
456                         packet.payload = &logic;
457                         num_samples = cur_sample_count - trigger_offset;
458                         if (devc->limit_samples &&
459                                         num_samples > devc->limit_samples - devc->sent_samples)
460                                 num_samples = devc->limit_samples - devc->sent_samples;
461                         logic.length = num_samples * unitsize;
462                         logic.unitsize = unitsize;
463                         logic.data = transfer->buffer + trigger_offset * unitsize;
464                         sr_session_send(devc->cb_data, &packet);
465                         devc->sent_samples += num_samples;
466
467                         devc->trigger_fired = TRUE;
468                 }
469         }
470
471         if (devc->limit_samples && devc->sent_samples >= devc->limit_samples) {
472                 fx2lafw_abort_acquisition(devc);
473                 free_transfer(transfer);
474         } else
475                 resubmit_transfer(transfer);
476 }
477
478 static unsigned int to_bytes_per_ms(unsigned int samplerate)
479 {
480         return samplerate / 1000;
481 }
482
483 SR_PRIV size_t fx2lafw_get_buffer_size(struct dev_context *devc)
484 {
485         size_t s;
486
487         /*
488          * The buffer should be large enough to hold 10ms of data and
489          * a multiple of 512.
490          */
491         s = 10 * to_bytes_per_ms(devc->cur_samplerate);
492         return (s + 511) & ~511;
493 }
494
495 SR_PRIV unsigned int fx2lafw_get_number_of_transfers(struct dev_context *devc)
496 {
497         unsigned int n;
498
499         /* Total buffer size should be able to hold about 500ms of data. */
500         n = (500 * to_bytes_per_ms(devc->cur_samplerate) /
501                 fx2lafw_get_buffer_size(devc));
502
503         if (n > NUM_SIMUL_TRANSFERS)
504                 return NUM_SIMUL_TRANSFERS;
505
506         return n;
507 }
508
509 SR_PRIV unsigned int fx2lafw_get_timeout(struct dev_context *devc)
510 {
511         size_t total_size;
512         unsigned int timeout;
513
514         total_size = fx2lafw_get_buffer_size(devc) *
515                         fx2lafw_get_number_of_transfers(devc);
516         timeout = total_size / to_bytes_per_ms(devc->cur_samplerate);
517         return timeout + timeout / 4; /* Leave a headroom of 25% percent. */
518 }