]> sigrok.org Git - libsigrok.git/blob - hardware/asix-sigma/asix-sigma.c
e6a109c638b3faa81f7fe5d8b0ead3025fe94318
[libsigrok.git] / hardware / asix-sigma / asix-sigma.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010 Håvard Espeland <gus@ping.uio.no>,
5  * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no>
6  * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no>
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 /*
23  * ASIX SIGMA Logic Analyzer Driver
24  */
25
26 #include "config.h"
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <ftdi.h>
30 #include <string.h>
31 #include <zlib.h>
32 #include <sigrok.h>
33 #include <sigrok-internal.h>
34 #include "asix-sigma.h"
35
36 #define USB_VENDOR                      0xa600
37 #define USB_PRODUCT                     0xa000
38 #define USB_DESCRIPTION                 "ASIX SIGMA"
39 #define USB_VENDOR_NAME                 "ASIX"
40 #define USB_MODEL_NAME                  "SIGMA"
41 #define USB_MODEL_VERSION               ""
42 #define TRIGGER_TYPES                   "rf10"
43
44 static GSList *device_instances = NULL;
45
46 static uint64_t supported_samplerates[] = {
47         SR_KHZ(200),
48         SR_KHZ(250),
49         SR_KHZ(500),
50         SR_MHZ(1),
51         SR_MHZ(5),
52         SR_MHZ(10),
53         SR_MHZ(25),
54         SR_MHZ(50),
55         SR_MHZ(100),
56         SR_MHZ(200),
57         0,
58 };
59
60 static struct sr_samplerates samplerates = {
61         SR_KHZ(200),
62         SR_MHZ(200),
63         SR_HZ(0),
64         supported_samplerates,
65 };
66
67 static int capabilities[] = {
68         SR_HWCAP_LOGIC_ANALYZER,
69         SR_HWCAP_SAMPLERATE,
70         SR_HWCAP_CAPTURE_RATIO,
71         SR_HWCAP_PROBECONFIG,
72
73         SR_HWCAP_LIMIT_MSEC,
74         0,
75 };
76
77 /* Force the FPGA to reboot. */
78 static uint8_t suicide[] = {
79         0x84, 0x84, 0x88, 0x84, 0x88, 0x84, 0x88, 0x84,
80 };
81
82 /* Prepare to upload firmware (FPGA specific). */
83 static uint8_t init[] = {
84         0x03, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
85 };
86
87 /* Initialize the logic analyzer mode. */
88 static uint8_t logic_mode_start[] = {
89         0x00, 0x40, 0x0f, 0x25, 0x35, 0x40,
90         0x2a, 0x3a, 0x40, 0x03, 0x20, 0x38,
91 };
92
93 static const char *firmware_files[] = {
94         "asix-sigma-50.fw",     /* 50 MHz, supports 8 bit fractions */
95         "asix-sigma-100.fw",    /* 100 MHz */
96         "asix-sigma-200.fw",    /* 200 MHz */
97         "asix-sigma-50sync.fw", /* Synchronous clock from pin */
98         "asix-sigma-phasor.fw", /* Frequency counter */
99 };
100
101 static void hw_stop_acquisition(int device_index, gpointer session_data);
102
103 static int sigma_read(void *buf, size_t size, struct sigma *sigma)
104 {
105         int ret;
106
107         ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size);
108         if (ret < 0) {
109                 sr_warn("ftdi_read_data failed: %s",
110                         ftdi_get_error_string(&sigma->ftdic));
111         }
112
113         return ret;
114 }
115
116 static int sigma_write(void *buf, size_t size, struct sigma *sigma)
117 {
118         int ret;
119
120         ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size);
121         if (ret < 0) {
122                 sr_warn("ftdi_write_data failed: %s",
123                         ftdi_get_error_string(&sigma->ftdic));
124         } else if ((size_t) ret != size) {
125                 sr_warn("ftdi_write_data did not complete write\n");
126         }
127
128         return ret;
129 }
130
131 static int sigma_write_register(uint8_t reg, uint8_t *data, size_t len,
132                 struct sigma *sigma)
133 {
134         size_t i;
135         uint8_t buf[len + 2];
136         int idx = 0;
137
138         buf[idx++] = REG_ADDR_LOW | (reg & 0xf);
139         buf[idx++] = REG_ADDR_HIGH | (reg >> 4);
140
141         for (i = 0; i < len; ++i) {
142                 buf[idx++] = REG_DATA_LOW | (data[i] & 0xf);
143                 buf[idx++] = REG_DATA_HIGH_WRITE | (data[i] >> 4);
144         }
145
146         return sigma_write(buf, idx, sigma);
147 }
148
149 static int sigma_set_register(uint8_t reg, uint8_t value, struct sigma *sigma)
150 {
151         return sigma_write_register(reg, &value, 1, sigma);
152 }
153
154 static int sigma_read_register(uint8_t reg, uint8_t *data, size_t len,
155                 struct sigma *sigma)
156 {
157         uint8_t buf[3];
158
159         buf[0] = REG_ADDR_LOW | (reg & 0xf);
160         buf[1] = REG_ADDR_HIGH | (reg >> 4);
161         buf[2] = REG_READ_ADDR;
162
163         sigma_write(buf, sizeof(buf), sigma);
164
165         return sigma_read(data, len, sigma);
166 }
167
168 static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma)
169 {
170         uint8_t value;
171
172         if (1 != sigma_read_register(reg, &value, 1, sigma)) {
173                 sr_warn("sigma_get_register: 1 byte expected");
174                 return 0;
175         }
176
177         return value;
178 }
179
180 static int sigma_read_pos(uint32_t *stoppos, uint32_t *triggerpos,
181                 struct sigma *sigma)
182 {
183         uint8_t buf[] = {
184                 REG_ADDR_LOW | READ_TRIGGER_POS_LOW,
185
186                 REG_READ_ADDR | NEXT_REG,
187                 REG_READ_ADDR | NEXT_REG,
188                 REG_READ_ADDR | NEXT_REG,
189                 REG_READ_ADDR | NEXT_REG,
190                 REG_READ_ADDR | NEXT_REG,
191                 REG_READ_ADDR | NEXT_REG,
192         };
193         uint8_t result[6];
194
195         sigma_write(buf, sizeof(buf), sigma);
196
197         sigma_read(result, sizeof(result), sigma);
198
199         *triggerpos = result[0] | (result[1] << 8) | (result[2] << 16);
200         *stoppos = result[3] | (result[4] << 8) | (result[5] << 16);
201
202         /* Not really sure why this must be done, but according to spec. */
203         if ((--*stoppos & 0x1ff) == 0x1ff)
204                 stoppos -= 64;
205
206         if ((*--triggerpos & 0x1ff) == 0x1ff)
207                 triggerpos -= 64;
208
209         return 1;
210 }
211
212 static int sigma_read_dram(uint16_t startchunk, size_t numchunks,
213                 uint8_t *data, struct sigma *sigma)
214 {
215         size_t i;
216         uint8_t buf[4096];
217         int idx = 0;
218
219         /* Send the startchunk. Index start with 1. */
220         buf[0] = startchunk >> 8;
221         buf[1] = startchunk & 0xff;
222         sigma_write_register(WRITE_MEMROW, buf, 2, sigma);
223
224         /* Read the DRAM. */
225         buf[idx++] = REG_DRAM_BLOCK;
226         buf[idx++] = REG_DRAM_WAIT_ACK;
227
228         for (i = 0; i < numchunks; ++i) {
229                 /* Alternate bit to copy from DRAM to cache. */
230                 if (i != (numchunks - 1))
231                         buf[idx++] = REG_DRAM_BLOCK | (((i + 1) % 2) << 4);
232
233                 buf[idx++] = REG_DRAM_BLOCK_DATA | ((i % 2) << 4);
234
235                 if (i != (numchunks - 1))
236                         buf[idx++] = REG_DRAM_WAIT_ACK;
237         }
238
239         sigma_write(buf, idx, sigma);
240
241         return sigma_read(data, numchunks * CHUNK_SIZE, sigma);
242 }
243
244 /* Upload trigger look-up tables to Sigma. */
245 static int sigma_write_trigger_lut(struct triggerlut *lut, struct sigma *sigma)
246 {
247         int i;
248         uint8_t tmp[2];
249         uint16_t bit;
250
251         /* Transpose the table and send to Sigma. */
252         for (i = 0; i < 16; ++i) {
253                 bit = 1 << i;
254
255                 tmp[0] = tmp[1] = 0;
256
257                 if (lut->m2d[0] & bit)
258                         tmp[0] |= 0x01;
259                 if (lut->m2d[1] & bit)
260                         tmp[0] |= 0x02;
261                 if (lut->m2d[2] & bit)
262                         tmp[0] |= 0x04;
263                 if (lut->m2d[3] & bit)
264                         tmp[0] |= 0x08;
265
266                 if (lut->m3 & bit)
267                         tmp[0] |= 0x10;
268                 if (lut->m3s & bit)
269                         tmp[0] |= 0x20;
270                 if (lut->m4 & bit)
271                         tmp[0] |= 0x40;
272
273                 if (lut->m0d[0] & bit)
274                         tmp[1] |= 0x01;
275                 if (lut->m0d[1] & bit)
276                         tmp[1] |= 0x02;
277                 if (lut->m0d[2] & bit)
278                         tmp[1] |= 0x04;
279                 if (lut->m0d[3] & bit)
280                         tmp[1] |= 0x08;
281
282                 if (lut->m1d[0] & bit)
283                         tmp[1] |= 0x10;
284                 if (lut->m1d[1] & bit)
285                         tmp[1] |= 0x20;
286                 if (lut->m1d[2] & bit)
287                         tmp[1] |= 0x40;
288                 if (lut->m1d[3] & bit)
289                         tmp[1] |= 0x80;
290
291                 sigma_write_register(WRITE_TRIGGER_SELECT0, tmp, sizeof(tmp),
292                                 sigma);
293                 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x30 | i, sigma);
294         }
295
296         /* Send the parameters */
297         sigma_write_register(WRITE_TRIGGER_SELECT0, (uint8_t *) &lut->params,
298                              sizeof(lut->params), sigma);
299
300         return SR_OK;
301 }
302
303 /* Generate the bitbang stream for programming the FPGA. */
304 static int bin2bitbang(const char *filename,
305                        unsigned char **buf, size_t *buf_size)
306 {
307         FILE *f;
308         long file_size;
309         unsigned long offset = 0;
310         unsigned char *p;
311         uint8_t *compressed_buf, *firmware;
312         uLongf csize, fwsize;
313         const int buffer_size = 65536;
314         size_t i;
315         int c, ret, bit, v;
316         uint32_t imm = 0x3f6df2ab;
317
318         f = g_fopen(filename, "rb");
319         if (!f) {
320                 sr_warn("g_fopen(\"%s\", \"rb\")", filename);
321                 return SR_ERR;
322         }
323
324         if (-1 == fseek(f, 0, SEEK_END)) {
325                 sr_warn("fseek on %s failed", filename);
326                 fclose(f);
327                 return SR_ERR;
328         }
329
330         file_size = ftell(f);
331
332         fseek(f, 0, SEEK_SET);
333
334         if (!(compressed_buf = g_try_malloc(file_size))) {
335                 sr_err("sigma: %s: compressed_buf malloc failed", __func__);
336                 fclose(f);
337                 return SR_ERR_MALLOC;
338         }
339
340         if (!(firmware = g_try_malloc(buffer_size))) {
341                 sr_err("sigma: %s: firmware malloc failed", __func__);
342                 fclose(f);
343                 g_free(compressed_buf);
344                 return SR_ERR_MALLOC;
345         }
346
347         csize = 0;
348         while ((c = getc(f)) != EOF) {
349                 imm = (imm + 0xa853753) % 177 + (imm * 0x8034052);
350                 compressed_buf[csize++] = c ^ imm;
351         }
352         fclose(f);
353
354         fwsize = buffer_size;
355         ret = uncompress(firmware, &fwsize, compressed_buf, csize);
356         if (ret < 0) {
357                 g_free(compressed_buf);
358                 g_free(firmware);
359                 sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret);
360                 return SR_ERR;
361         }
362
363         g_free(compressed_buf);
364
365         *buf_size = fwsize * 2 * 8;
366
367         *buf = p = (unsigned char *)g_try_malloc(*buf_size);
368         if (!p) {
369                 sr_err("sigma: %s: buf/p malloc failed", __func__);
370                 g_free(compressed_buf);
371                 g_free(firmware);
372                 return SR_ERR_MALLOC;
373         }
374
375         for (i = 0; i < fwsize; ++i) {
376                 for (bit = 7; bit >= 0; --bit) {
377                         v = firmware[i] & 1 << bit ? 0x40 : 0x00;
378                         p[offset++] = v | 0x01;
379                         p[offset++] = v;
380                 }
381         }
382
383         g_free(firmware);
384
385         if (offset != *buf_size) {
386                 g_free(*buf);
387                 sr_warn("Error reading firmware %s "
388                         "offset=%ld, file_size=%ld, buf_size=%zd\n",
389                         filename, offset, file_size, *buf_size);
390
391                 return SR_ERR;
392         }
393
394         return SR_OK;
395 }
396
397 static int hw_init(const char *deviceinfo)
398 {
399         struct sr_device_instance *sdi;
400         struct sigma *sigma;
401
402         /* Avoid compiler warnings. */
403         (void)deviceinfo;
404
405         if (!(sigma = g_try_malloc(sizeof(struct sigma)))) {
406                 sr_err("sigma: %s: sigma malloc failed", __func__);
407                 return 0; /* FIXME: Should be SR_ERR_MALLOC. */
408         }
409
410         ftdi_init(&sigma->ftdic);
411
412         /* Look for SIGMAs. */
413         if (ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT,
414                                USB_DESCRIPTION, NULL) < 0)
415                 goto free;
416
417         sigma->cur_samplerate = 0;
418         sigma->period_ps = 0;
419         sigma->limit_msec = 0;
420         sigma->cur_firmware = -1;
421         sigma->num_probes = 0;
422         sigma->samples_per_event = 0;
423         sigma->capture_ratio = 50;
424         sigma->use_triggers = 0;
425
426         /* Register SIGMA device. */
427         sdi = sr_device_instance_new(0, SR_ST_INITIALIZING,
428                         USB_VENDOR_NAME, USB_MODEL_NAME, USB_MODEL_VERSION);
429         if (!sdi)
430                 goto free;
431
432         sdi->priv = sigma;
433
434         device_instances = g_slist_append(device_instances, sdi);
435
436         /* We will open the device again when we need it. */
437         ftdi_usb_close(&sigma->ftdic);
438
439         return 1;
440 free:
441         g_free(sigma);
442         return 0;
443 }
444
445 static int upload_firmware(int firmware_idx, struct sigma *sigma)
446 {
447         int ret;
448         unsigned char *buf;
449         unsigned char pins;
450         size_t buf_size;
451         unsigned char result[32];
452         char firmware_path[128];
453
454         /* Make sure it's an ASIX SIGMA. */
455         if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
456                 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
457                 sr_warn("ftdi_usb_open failed: %s",
458                         ftdi_get_error_string(&sigma->ftdic));
459                 return 0;
460         }
461
462         if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) {
463                 sr_warn("ftdi_set_bitmode failed: %s",
464                         ftdi_get_error_string(&sigma->ftdic));
465                 return 0;
466         }
467
468         /* Four times the speed of sigmalogan - Works well. */
469         if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) {
470                 sr_warn("ftdi_set_baudrate failed: %s",
471                         ftdi_get_error_string(&sigma->ftdic));
472                 return 0;
473         }
474
475         /* Force the FPGA to reboot. */
476         sigma_write(suicide, sizeof(suicide), sigma);
477         sigma_write(suicide, sizeof(suicide), sigma);
478         sigma_write(suicide, sizeof(suicide), sigma);
479         sigma_write(suicide, sizeof(suicide), sigma);
480
481         /* Prepare to upload firmware (FPGA specific). */
482         sigma_write(init, sizeof(init), sigma);
483
484         ftdi_usb_purge_buffers(&sigma->ftdic);
485
486         /* Wait until the FPGA asserts INIT_B. */
487         while (1) {
488                 ret = sigma_read(result, 1, sigma);
489                 if (result[0] & 0x20)
490                         break;
491         }
492
493         /* Prepare firmware. */
494         snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR,
495                  firmware_files[firmware_idx]);
496
497         if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) {
498                 sr_warn("An error occured while reading the firmware: %s",
499                         firmware_path);
500                 return ret;
501         }
502
503         /* Upload firmare. */
504         sigma_write(buf, buf_size, sigma);
505
506         g_free(buf);
507
508         if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) {
509                 sr_warn("ftdi_set_bitmode failed: %s",
510                         ftdi_get_error_string(&sigma->ftdic));
511                 return SR_ERR;
512         }
513
514         ftdi_usb_purge_buffers(&sigma->ftdic);
515
516         /* Discard garbage. */
517         while (1 == sigma_read(&pins, 1, sigma))
518                 ;
519
520         /* Initialize the logic analyzer mode. */
521         sigma_write(logic_mode_start, sizeof(logic_mode_start), sigma);
522
523         /* Expect a 3 byte reply. */
524         ret = sigma_read(result, 3, sigma);
525         if (ret != 3 ||
526             result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) {
527                 sr_warn("Configuration failed. Invalid reply received.");
528                 return SR_ERR;
529         }
530
531         sigma->cur_firmware = firmware_idx;
532
533         return SR_OK;
534 }
535
536 static int hw_opendev(int device_index)
537 {
538         struct sr_device_instance *sdi;
539         struct sigma *sigma;
540         int ret;
541
542         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
543                 return SR_ERR;
544
545         sigma = sdi->priv;
546
547         /* Make sure it's an ASIX SIGMA. */
548         if ((ret = ftdi_usb_open_desc(&sigma->ftdic,
549                 USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
550
551                 sr_warn("ftdi_usb_open failed: %s",
552                         ftdi_get_error_string(&sigma->ftdic));
553
554                 return 0;
555         }
556
557         sdi->status = SR_ST_ACTIVE;
558
559         return SR_OK;
560 }
561
562 static int set_samplerate(struct sr_device_instance *sdi,
563                           uint64_t samplerate)
564 {
565         int i, ret;
566         struct sigma *sigma = sdi->priv;
567
568         for (i = 0; supported_samplerates[i]; i++) {
569                 if (supported_samplerates[i] == samplerate)
570                         break;
571         }
572         if (supported_samplerates[i] == 0)
573                 return SR_ERR_SAMPLERATE;
574
575         if (samplerate <= SR_MHZ(50)) {
576                 ret = upload_firmware(0, sigma);
577                 sigma->num_probes = 16;
578         }
579         if (samplerate == SR_MHZ(100)) {
580                 ret = upload_firmware(1, sigma);
581                 sigma->num_probes = 8;
582         }
583         else if (samplerate == SR_MHZ(200)) {
584                 ret = upload_firmware(2, sigma);
585                 sigma->num_probes = 4;
586         }
587
588         sigma->cur_samplerate = samplerate;
589         sigma->period_ps = 1000000000000 / samplerate;
590         sigma->samples_per_event = 16 / sigma->num_probes;
591         sigma->state.state = SIGMA_IDLE;
592
593         sr_info("Firmware uploaded");
594
595         return ret;
596 }
597
598 /*
599  * In 100 and 200 MHz mode, only a single pin rising/falling can be
600  * set as trigger. In other modes, two rising/falling triggers can be set,
601  * in addition to value/mask trigger for any number of probes.
602  *
603  * The Sigma supports complex triggers using boolean expressions, but this
604  * has not been implemented yet.
605  */
606 static int configure_probes(struct sr_device_instance *sdi, GSList *probes)
607 {
608         struct sigma *sigma = sdi->priv;
609         struct sr_probe *probe;
610         GSList *l;
611         int trigger_set = 0;
612         int probebit;
613
614         memset(&sigma->trigger, 0, sizeof(struct sigma_trigger));
615
616         for (l = probes; l; l = l->next) {
617                 probe = (struct sr_probe *)l->data;
618                 probebit = 1 << (probe->index - 1);
619
620                 if (!probe->enabled || !probe->trigger)
621                         continue;
622
623                 if (sigma->cur_samplerate >= SR_MHZ(100)) {
624                         /* Fast trigger support. */
625                         if (trigger_set) {
626                                 sr_warn("ASIX SIGMA only supports a single "
627                                         "pin trigger in 100 and 200MHz mode.");
628                                 return SR_ERR;
629                         }
630                         if (probe->trigger[0] == 'f')
631                                 sigma->trigger.fallingmask |= probebit;
632                         else if (probe->trigger[0] == 'r')
633                                 sigma->trigger.risingmask |= probebit;
634                         else {
635                                 sr_warn("ASIX SIGMA only supports "
636                                         "rising/falling trigger in 100 "
637                                         "and 200MHz mode.");
638                                 return SR_ERR;
639                         }
640
641                         ++trigger_set;
642                 } else {
643                         /* Simple trigger support (event). */
644                         if (probe->trigger[0] == '1') {
645                                 sigma->trigger.simplevalue |= probebit;
646                                 sigma->trigger.simplemask |= probebit;
647                         }
648                         else if (probe->trigger[0] == '0') {
649                                 sigma->trigger.simplevalue &= ~probebit;
650                                 sigma->trigger.simplemask |= probebit;
651                         }
652                         else if (probe->trigger[0] == 'f') {
653                                 sigma->trigger.fallingmask |= probebit;
654                                 ++trigger_set;
655                         }
656                         else if (probe->trigger[0] == 'r') {
657                                 sigma->trigger.risingmask |= probebit;
658                                 ++trigger_set;
659                         }
660
661                         /*
662                          * Actually, Sigma supports 2 rising/falling triggers,
663                          * but they are ORed and the current trigger syntax
664                          * does not permit ORed triggers.
665                          */
666                         if (trigger_set > 1) {
667                                 sr_warn("ASIX SIGMA only supports 1 rising/"
668                                         "falling triggers.");
669                                 return SR_ERR;
670                         }
671                 }
672
673                 if (trigger_set)
674                         sigma->use_triggers = 1;
675         }
676
677         return SR_OK;
678 }
679
680 static int hw_closedev(int device_index)
681 {
682         struct sr_device_instance *sdi;
683         struct sigma *sigma;
684
685         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
686                 sr_err("sigma: %s: sdi was NULL", __func__);
687                 return SR_ERR; /* TODO: SR_ERR_ARG? */
688         }
689
690         if (!(sigma = sdi->priv)) {
691                 sr_err("sigma: %s: sdi->priv was NULL", __func__);
692                 return SR_ERR; /* TODO: SR_ERR_ARG? */
693         }
694
695         /* TODO */
696         if (sdi->status == SR_ST_ACTIVE)
697                 ftdi_usb_close(&sigma->ftdic);
698
699         sdi->status = SR_ST_INACTIVE;
700
701         return SR_OK;
702 }
703
704 static void hw_cleanup(void)
705 {
706         GSList *l;
707         struct sr_device_instance *sdi;
708
709         /* Properly close all devices. */
710         for (l = device_instances; l; l = l->next) {
711                 sdi = l->data;
712                 if (sdi->priv != NULL)
713                         free(sdi->priv);
714                 sr_device_instance_free(sdi);
715         }
716         g_slist_free(device_instances);
717         device_instances = NULL;
718 }
719
720 static void *hw_get_device_info(int device_index, int device_info_id)
721 {
722         struct sr_device_instance *sdi;
723         struct sigma *sigma;
724         void *info = NULL;
725
726         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
727                 sr_err("It's NULL.\n");
728                 return NULL;
729         }
730
731         sigma = sdi->priv;
732
733         switch (device_info_id) {
734         case SR_DI_INSTANCE:
735                 info = sdi;
736                 break;
737         case SR_DI_NUM_PROBES:
738                 info = GINT_TO_POINTER(16);
739                 break;
740         case SR_DI_SAMPLERATES:
741                 info = &samplerates;
742                 break;
743         case SR_DI_TRIGGER_TYPES:
744                 info = (char *)TRIGGER_TYPES;
745                 break;
746         case SR_DI_CUR_SAMPLERATE:
747                 info = &sigma->cur_samplerate;
748                 break;
749         }
750
751         return info;
752 }
753
754 static int hw_get_status(int device_index)
755 {
756         struct sr_device_instance *sdi;
757
758         sdi = sr_get_device_instance(device_instances, device_index);
759         if (sdi)
760                 return sdi->status;
761         else
762                 return SR_ST_NOT_FOUND;
763 }
764
765 static int *hw_get_capabilities(void)
766 {
767         return capabilities;
768 }
769
770 static int hw_set_configuration(int device_index, int capability, void *value)
771 {
772         struct sr_device_instance *sdi;
773         struct sigma *sigma;
774         int ret;
775
776         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
777                 return SR_ERR;
778
779         sigma = sdi->priv;
780
781         if (capability == SR_HWCAP_SAMPLERATE) {
782                 ret = set_samplerate(sdi, *(uint64_t*) value);
783         } else if (capability == SR_HWCAP_PROBECONFIG) {
784                 ret = configure_probes(sdi, value);
785         } else if (capability == SR_HWCAP_LIMIT_MSEC) {
786                 sigma->limit_msec = *(uint64_t*) value;
787                 if (sigma->limit_msec > 0)
788                         ret = SR_OK;
789                 else
790                         ret = SR_ERR;
791         } else if (capability == SR_HWCAP_CAPTURE_RATIO) {
792                 sigma->capture_ratio = *(uint64_t*) value;
793                 if (sigma->capture_ratio < 0 || sigma->capture_ratio > 100)
794                         ret = SR_ERR;
795                 else
796                         ret = SR_OK;
797         } else {
798                 ret = SR_ERR;
799         }
800
801         return ret;
802 }
803
804 /* Software trigger to determine exact trigger position. */
805 static int get_trigger_offset(uint16_t *samples, uint16_t last_sample,
806                               struct sigma_trigger *t)
807 {
808         int i;
809
810         for (i = 0; i < 8; ++i) {
811                 if (i > 0)
812                         last_sample = samples[i-1];
813
814                 /* Simple triggers. */
815                 if ((samples[i] & t->simplemask) != t->simplevalue)
816                         continue;
817
818                 /* Rising edge. */
819                 if ((last_sample & t->risingmask) != 0 || (samples[i] &
820                     t->risingmask) != t->risingmask)
821                         continue;
822
823                 /* Falling edge. */
824                 if ((last_sample & t->fallingmask) != t->fallingmask ||
825                     (samples[i] & t->fallingmask) != 0)
826                         continue;
827
828                 break;
829         }
830
831         /* If we did not match, return original trigger pos. */
832         return i & 0x7;
833 }
834
835 /*
836  * Decode chunk of 1024 bytes, 64 clusters, 7 events per cluster.
837  * Each event is 20ns apart, and can contain multiple samples.
838  *
839  * For 200 MHz, events contain 4 samples for each channel, spread 5 ns apart.
840  * For 100 MHz, events contain 2 samples for each channel, spread 10 ns apart.
841  * For 50 MHz and below, events contain one sample for each channel,
842  * spread 20 ns apart.
843  */
844 static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts,
845                            uint16_t *lastsample, int triggerpos,
846                            uint16_t limit_chunk, void *session_data)
847 {
848         struct sr_device_instance *sdi = session_data;
849         struct sigma *sigma = sdi->priv;
850         uint16_t tsdiff, ts;
851         uint16_t samples[65536 * sigma->samples_per_event];
852         struct sr_datafeed_packet packet;
853         struct sr_datafeed_logic logic;
854         int i, j, k, l, numpad, tosend;
855         size_t n = 0, sent = 0;
856         int clustersize = EVENTS_PER_CLUSTER * sigma->samples_per_event;
857         uint16_t *event;
858         uint16_t cur_sample;
859         int triggerts = -1;
860
861         /* Check if trigger is in this chunk. */
862         if (triggerpos != -1) {
863                 if (sigma->cur_samplerate <= SR_MHZ(50))
864                         triggerpos -= EVENTS_PER_CLUSTER - 1;
865
866                 if (triggerpos < 0)
867                         triggerpos = 0;
868
869                 /* Find in which cluster the trigger occured. */
870                 triggerts = triggerpos / 7;
871         }
872
873         /* For each ts. */
874         for (i = 0; i < 64; ++i) {
875                 ts = *(uint16_t *) &buf[i * 16];
876                 tsdiff = ts - *lastts;
877                 *lastts = ts;
878
879                 /* Decode partial chunk. */
880                 if (limit_chunk && ts > limit_chunk)
881                         return SR_OK;
882
883                 /* Pad last sample up to current point. */
884                 numpad = tsdiff * sigma->samples_per_event - clustersize;
885                 if (numpad > 0) {
886                         for (j = 0; j < numpad; ++j)
887                                 samples[j] = *lastsample;
888
889                         n = numpad;
890                 }
891
892                 /* Send samples between previous and this timestamp to sigrok. */
893                 sent = 0;
894                 while (sent < n) {
895                         tosend = MIN(2048, n - sent);
896
897                         packet.type = SR_DF_LOGIC;
898                         /* TODO: fill in timeoffset and duration */
899                         packet.timeoffset = 0;
900                         packet.duration = 0;
901                         packet.payload = &logic;
902                         logic.length = tosend * sizeof(uint16_t);
903                         logic.unitsize = 2;
904                         logic.data = samples + sent;
905                         sr_session_bus(sigma->session_id, &packet);
906
907                         sent += tosend;
908                 }
909                 n = 0;
910
911                 event = (uint16_t *) &buf[i * 16 + 2];
912                 cur_sample = 0;
913
914                 /* For each event in cluster. */
915                 for (j = 0; j < 7; ++j) {
916
917                         /* For each sample in event. */
918                         for (k = 0; k < sigma->samples_per_event; ++k) {
919                                 cur_sample = 0;
920
921                                 /* For each probe. */
922                                 for (l = 0; l < sigma->num_probes; ++l)
923                                         cur_sample |= (!!(event[j] & (1 << (l *
924                                                       sigma->samples_per_event
925                                                       + k))))
926                                                       << l;
927
928                                 samples[n++] = cur_sample;
929                         }
930                 }
931
932                 /* Send data up to trigger point (if triggered). */
933                 sent = 0;
934                 if (i == triggerts) {
935                         /*
936                          * Trigger is not always accurate to sample because of
937                          * pipeline delay. However, it always triggers before
938                          * the actual event. We therefore look at the next
939                          * samples to pinpoint the exact position of the trigger.
940                          */
941                         tosend = get_trigger_offset(samples, *lastsample,
942                                                     &sigma->trigger);
943
944                         if (tosend > 0) {
945                                 packet.type = SR_DF_LOGIC;
946                                 /* TODO: fill in timeoffset and duration */
947                                 packet.timeoffset = 0;
948                                 packet.duration = 0;
949                                 packet.payload = &logic;
950                                 logic.length = tosend * sizeof(uint16_t);
951                                 logic.unitsize = 2;
952                                 logic.data = samples;
953                                 sr_session_bus(sigma->session_id, &packet);
954
955                                 sent += tosend;
956                         }
957
958                         /* Only send trigger if explicitly enabled. */
959                         if (sigma->use_triggers) {
960                                 packet.type = SR_DF_TRIGGER;
961                                 /* TODO: fill in timeoffset only */
962                                 packet.timeoffset = 0;
963                                 packet.duration = 0;
964                                 sr_session_bus(sigma->session_id, &packet);
965                         }
966                 }
967
968                 /* Send rest of the chunk to sigrok. */
969                 tosend = n - sent;
970
971                 if (tosend > 0) {
972                         packet.type = SR_DF_LOGIC;
973                         /* TODO: fill in timeoffset and duration */
974                         packet.timeoffset = 0;
975                         packet.duration = 0;
976                         packet.payload = &logic;
977                         logic.length = tosend * sizeof(uint16_t);
978                         logic.unitsize = 2;
979                         logic.data = samples + sent;
980                         sr_session_bus(sigma->session_id, &packet);
981                 }
982
983                 *lastsample = samples[n - 1];
984         }
985
986         return SR_OK;
987 }
988
989 static int receive_data(int fd, int revents, void *session_data)
990 {
991         struct sr_device_instance *sdi = session_data;
992         struct sigma *sigma = sdi->priv;
993         struct sr_datafeed_packet packet;
994         const int chunks_per_read = 32;
995         unsigned char buf[chunks_per_read * CHUNK_SIZE];
996         int bufsz, numchunks, i, newchunks;
997         uint64_t running_msec;
998         struct timeval tv;
999
1000         /* Avoid compiler warnings. */
1001         (void)fd;
1002         (void)revents;
1003
1004         numchunks = (sigma->state.stoppos + 511) / 512;
1005
1006         if (sigma->state.state == SIGMA_IDLE)
1007                 return FALSE;
1008
1009         if (sigma->state.state == SIGMA_CAPTURE) {
1010
1011                 /* Check if the timer has expired, or memory is full. */
1012                 gettimeofday(&tv, 0);
1013                 running_msec = (tv.tv_sec - sigma->start_tv.tv_sec) * 1000 +
1014                         (tv.tv_usec - sigma->start_tv.tv_usec) / 1000;
1015
1016                 if (running_msec < sigma->limit_msec && numchunks < 32767)
1017                         return FALSE;
1018
1019                 hw_stop_acquisition(sdi->index, session_data);
1020
1021                 return FALSE;
1022
1023         } else if (sigma->state.state == SIGMA_DOWNLOAD) {
1024                 if (sigma->state.chunks_downloaded >= numchunks) {
1025                         /* End of samples. */
1026                         packet.type = SR_DF_END;
1027                         sr_session_bus(sigma->session_id, &packet);
1028
1029                         sigma->state.state = SIGMA_IDLE;
1030
1031                         return TRUE;
1032                 }
1033
1034                 newchunks = MIN(chunks_per_read,
1035                                 numchunks - sigma->state.chunks_downloaded);
1036
1037                 sr_info("Downloading sample data: %.0f %%",
1038                         100.0 * sigma->state.chunks_downloaded / numchunks);
1039
1040                 bufsz = sigma_read_dram(sigma->state.chunks_downloaded,
1041                                         newchunks, buf, sigma);
1042                 /* TODO: Check bufsz. For now, just avoid compiler warnings. */
1043                 (void)bufsz;
1044
1045                 /* Find first ts. */
1046                 if (sigma->state.chunks_downloaded == 0) {
1047                         sigma->state.lastts = *(uint16_t *) buf - 1;
1048                         sigma->state.lastsample = 0;
1049                 }
1050
1051                 /* Decode chunks and send them to sigrok. */
1052                 for (i = 0; i < newchunks; ++i) {
1053                         int limit_chunk = 0;
1054
1055                         /* The last chunk may potentially be only in part. */
1056                         if (sigma->state.chunks_downloaded == numchunks - 1)
1057                         {
1058                                 /* Find the last valid timestamp */
1059                                 limit_chunk = sigma->state.stoppos % 512 + sigma->state.lastts;
1060                         }
1061
1062                         if (sigma->state.chunks_downloaded + i == sigma->state.triggerchunk)
1063                                 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1064                                                 &sigma->state.lastts,
1065                                                 &sigma->state.lastsample,
1066                                                 sigma->state.triggerpos & 0x1ff,
1067                                                 limit_chunk, session_data);
1068                         else
1069                                 decode_chunk_ts(buf + (i * CHUNK_SIZE),
1070                                                 &sigma->state.lastts,
1071                                                 &sigma->state.lastsample,
1072                                                 -1, limit_chunk, session_data);
1073
1074                         ++sigma->state.chunks_downloaded;
1075                 }
1076         }
1077
1078         return TRUE;
1079 }
1080
1081 /* Build a LUT entry used by the trigger functions. */
1082 static void build_lut_entry(uint16_t value, uint16_t mask, uint16_t *entry)
1083 {
1084         int i, j, k, bit;
1085
1086         /* For each quad probe. */
1087         for (i = 0; i < 4; ++i) {
1088                 entry[i] = 0xffff;
1089
1090                 /* For each bit in LUT. */
1091                 for (j = 0; j < 16; ++j)
1092
1093                         /* For each probe in quad. */
1094                         for (k = 0; k < 4; ++k) {
1095                                 bit = 1 << (i * 4 + k);
1096
1097                                 /* Set bit in entry */
1098                                 if ((mask & bit) &&
1099                                     ((!(value & bit)) !=
1100                                     (!(j & (1 << k)))))
1101                                         entry[i] &= ~(1 << j);
1102                         }
1103         }
1104 }
1105
1106 /* Add a logical function to LUT mask. */
1107 static void add_trigger_function(enum triggerop oper, enum triggerfunc func,
1108                                  int index, int neg, uint16_t *mask)
1109 {
1110         int i, j;
1111         int x[2][2], tmp, a, b, aset, bset, rset;
1112
1113         memset(x, 0, 4 * sizeof(int));
1114
1115         /* Trigger detect condition. */
1116         switch (oper) {
1117         case OP_LEVEL:
1118                 x[0][1] = 1;
1119                 x[1][1] = 1;
1120                 break;
1121         case OP_NOT:
1122                 x[0][0] = 1;
1123                 x[1][0] = 1;
1124                 break;
1125         case OP_RISE:
1126                 x[0][1] = 1;
1127                 break;
1128         case OP_FALL:
1129                 x[1][0] = 1;
1130                 break;
1131         case OP_RISEFALL:
1132                 x[0][1] = 1;
1133                 x[1][0] = 1;
1134                 break;
1135         case OP_NOTRISE:
1136                 x[1][1] = 1;
1137                 x[0][0] = 1;
1138                 x[1][0] = 1;
1139                 break;
1140         case OP_NOTFALL:
1141                 x[1][1] = 1;
1142                 x[0][0] = 1;
1143                 x[0][1] = 1;
1144                 break;
1145         case OP_NOTRISEFALL:
1146                 x[1][1] = 1;
1147                 x[0][0] = 1;
1148                 break;
1149         }
1150
1151         /* Transpose if neg is set. */
1152         if (neg) {
1153                 for (i = 0; i < 2; ++i)
1154                         for (j = 0; j < 2; ++j) {
1155                                 tmp = x[i][j];
1156                                 x[i][j] = x[1-i][1-j];
1157                                 x[1-i][1-j] = tmp;
1158                         }
1159         }
1160
1161         /* Update mask with function. */
1162         for (i = 0; i < 16; ++i) {
1163                 a = (i >> (2 * index + 0)) & 1;
1164                 b = (i >> (2 * index + 1)) & 1;
1165
1166                 aset = (*mask >> i) & 1;
1167                 bset = x[b][a];
1168
1169                 if (func == FUNC_AND || func == FUNC_NAND)
1170                         rset = aset & bset;
1171                 else if (func == FUNC_OR || func == FUNC_NOR)
1172                         rset = aset | bset;
1173                 else if (func == FUNC_XOR || func == FUNC_NXOR)
1174                         rset = aset ^ bset;
1175
1176                 if (func == FUNC_NAND || func == FUNC_NOR || func == FUNC_NXOR)
1177                         rset = !rset;
1178
1179                 *mask &= ~(1 << i);
1180
1181                 if (rset)
1182                         *mask |= 1 << i;
1183         }
1184 }
1185
1186 /*
1187  * Build trigger LUTs used by 50 MHz and lower sample rates for supporting
1188  * simple pin change and state triggers. Only two transitions (rise/fall) can be
1189  * set at any time, but a full mask and value can be set (0/1).
1190  */
1191 static int build_basic_trigger(struct triggerlut *lut, struct sigma *sigma)
1192 {
1193         int i,j;
1194         uint16_t masks[2] = { 0, 0 };
1195
1196         memset(lut, 0, sizeof(struct triggerlut));
1197
1198         /* Contant for simple triggers. */
1199         lut->m4 = 0xa000;
1200
1201         /* Value/mask trigger support. */
1202         build_lut_entry(sigma->trigger.simplevalue, sigma->trigger.simplemask,
1203                         lut->m2d);
1204
1205         /* Rise/fall trigger support. */
1206         for (i = 0, j = 0; i < 16; ++i) {
1207                 if (sigma->trigger.risingmask & (1 << i) ||
1208                     sigma->trigger.fallingmask & (1 << i))
1209                         masks[j++] = 1 << i;
1210         }
1211
1212         build_lut_entry(masks[0], masks[0], lut->m0d);
1213         build_lut_entry(masks[1], masks[1], lut->m1d);
1214
1215         /* Add glue logic */
1216         if (masks[0] || masks[1]) {
1217                 /* Transition trigger. */
1218                 if (masks[0] & sigma->trigger.risingmask)
1219                         add_trigger_function(OP_RISE, FUNC_OR, 0, 0, &lut->m3);
1220                 if (masks[0] & sigma->trigger.fallingmask)
1221                         add_trigger_function(OP_FALL, FUNC_OR, 0, 0, &lut->m3);
1222                 if (masks[1] & sigma->trigger.risingmask)
1223                         add_trigger_function(OP_RISE, FUNC_OR, 1, 0, &lut->m3);
1224                 if (masks[1] & sigma->trigger.fallingmask)
1225                         add_trigger_function(OP_FALL, FUNC_OR, 1, 0, &lut->m3);
1226         } else {
1227                 /* Only value/mask trigger. */
1228                 lut->m3 = 0xffff;
1229         }
1230
1231         /* Triggertype: event. */
1232         lut->params.selres = 3;
1233
1234         return SR_OK;
1235 }
1236
1237 static int hw_start_acquisition(int device_index, gpointer session_data)
1238 {
1239         struct sr_device_instance *sdi;
1240         struct sigma *sigma;
1241         struct sr_datafeed_packet packet;
1242         struct sr_datafeed_header header;
1243         struct clockselect_50 clockselect;
1244         int frac, triggerpin, ret;
1245         uint8_t triggerselect;
1246         struct triggerinout triggerinout_conf;
1247         struct triggerlut lut;
1248
1249         /* Avoid compiler warnings. */
1250         (void)session_data;
1251
1252         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
1253                 return SR_ERR;
1254
1255         sigma = sdi->priv;
1256
1257         /* If the samplerate has not been set, default to 200 KHz. */
1258         if (sigma->cur_firmware == -1) {
1259                 if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK)
1260                         return ret;
1261         }
1262
1263         /* Enter trigger programming mode. */
1264         sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma);
1265
1266         /* 100 and 200 MHz mode. */
1267         if (sigma->cur_samplerate >= SR_MHZ(100)) {
1268                 sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma);
1269
1270                 /* Find which pin to trigger on from mask. */
1271                 for (triggerpin = 0; triggerpin < 8; ++triggerpin)
1272                         if ((sigma->trigger.risingmask | sigma->trigger.fallingmask) &
1273                             (1 << triggerpin))
1274                                 break;
1275
1276                 /* Set trigger pin and light LED on trigger. */
1277                 triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7);
1278
1279                 /* Default rising edge. */
1280                 if (sigma->trigger.fallingmask)
1281                         triggerselect |= 1 << 3;
1282
1283         /* All other modes. */
1284         } else if (sigma->cur_samplerate <= SR_MHZ(50)) {
1285                 build_basic_trigger(&lut, sigma);
1286
1287                 sigma_write_trigger_lut(&lut, sigma);
1288
1289                 triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0);
1290         }
1291
1292         /* Setup trigger in and out pins to default values. */
1293         memset(&triggerinout_conf, 0, sizeof(struct triggerinout));
1294         triggerinout_conf.trgout_bytrigger = 1;
1295         triggerinout_conf.trgout_enable = 1;
1296
1297         sigma_write_register(WRITE_TRIGGER_OPTION,
1298                              (uint8_t *) &triggerinout_conf,
1299                              sizeof(struct triggerinout), sigma);
1300
1301         /* Go back to normal mode. */
1302         sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma);
1303
1304         /* Set clock select register. */
1305         if (sigma->cur_samplerate == SR_MHZ(200))
1306                 /* Enable 4 probes. */
1307                 sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma);
1308         else if (sigma->cur_samplerate == SR_MHZ(100))
1309                 /* Enable 8 probes. */
1310                 sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma);
1311         else {
1312                 /*
1313                  * 50 MHz mode (or fraction thereof). Any fraction down to
1314                  * 50 MHz / 256 can be used, but is not supported by sigrok API.
1315                  */
1316                 frac = SR_MHZ(50) / sigma->cur_samplerate - 1;
1317
1318                 clockselect.async = 0;
1319                 clockselect.fraction = frac;
1320                 clockselect.disabled_probes = 0;
1321
1322                 sigma_write_register(WRITE_CLOCK_SELECT,
1323                                      (uint8_t *) &clockselect,
1324                                      sizeof(clockselect), sigma);
1325         }
1326
1327         /* Setup maximum post trigger time. */
1328         sigma_set_register(WRITE_POST_TRIGGER,
1329                         (sigma->capture_ratio * 255) / 100, sigma);
1330
1331         /* Start acqusition. */
1332         gettimeofday(&sigma->start_tv, 0);
1333         sigma_set_register(WRITE_MODE, 0x0d, sigma);
1334
1335         sigma->session_id = session_data;
1336
1337         /* Send header packet to the session bus. */
1338         packet.type = SR_DF_HEADER;
1339         packet.payload = &header;
1340         header.feed_version = 1;
1341         gettimeofday(&header.starttime, NULL);
1342         header.samplerate = sigma->cur_samplerate;
1343         header.num_logic_probes = sigma->num_probes;
1344         header.num_analog_probes = 0;
1345         sr_session_bus(session_data, &packet);
1346
1347         /* Add capture source. */
1348         sr_source_add(0, G_IO_IN, 10, receive_data, sdi);
1349
1350         sigma->state.state = SIGMA_CAPTURE;
1351
1352         return SR_OK;
1353 }
1354
1355 static void hw_stop_acquisition(int device_index, gpointer session_data)
1356 {
1357         struct sr_device_instance *sdi;
1358         struct sigma *sigma;
1359         uint8_t modestatus;
1360
1361         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
1362                 return;
1363
1364         sigma = sdi->priv;
1365
1366         /* Avoid compiler warnings. */
1367         (void)session_data;
1368
1369         /* Stop acquisition. */
1370         sigma_set_register(WRITE_MODE, 0x11, sigma);
1371
1372         /* Set SDRAM Read Enable. */
1373         sigma_set_register(WRITE_MODE, 0x02, sigma);
1374
1375         /* Get the current position. */
1376         sigma_read_pos(&sigma->state.stoppos, &sigma->state.triggerpos, sigma);
1377
1378         /* Check if trigger has fired. */
1379         modestatus = sigma_get_register(READ_MODE, sigma);
1380         if (modestatus & 0x20) {
1381                 sigma->state.triggerchunk = sigma->state.triggerpos / 512;
1382
1383         } else
1384                 sigma->state.triggerchunk = -1;
1385
1386         sigma->state.chunks_downloaded = 0;
1387
1388         sigma->state.state = SIGMA_DOWNLOAD;
1389 }
1390
1391 struct sr_device_plugin asix_sigma_plugin_info = {
1392         .name = "asix-sigma",
1393         .longname = "ASIX SIGMA",
1394         .api_version = 1,
1395         .init = hw_init,
1396         .cleanup = hw_cleanup,
1397         .opendev = hw_opendev,
1398         .closedev = hw_closedev,
1399         .get_device_info = hw_get_device_info,
1400         .get_status = hw_get_status,
1401         .get_capabilities = hw_get_capabilities,
1402         .set_configuration = hw_set_configuration,
1403         .start_acquisition = hw_start_acquisition,
1404         .stop_acquisition = hw_stop_acquisition,
1405 };