]> sigrok.org Git - libsigrok.git/blob - hardware/link-mso19/link-mso19.c
83fb4db04233ca81c5010ff68338a11056ef4b25
[libsigrok.git] / hardware / link-mso19 / link-mso19.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/time.h>
26 #include <inttypes.h>
27 #include <glib.h>
28 #include <libudev.h>
29 #include <arpa/inet.h>
30 #include "sigrok.h"
31 #include "sigrok-internal.h"
32 #include "config.h"
33 #include "link-mso19.h"
34
35 #define USB_VENDOR "3195"
36 #define USB_PRODUCT "f190"
37
38 static int capabilities[] = {
39         SR_HWCAP_LOGIC_ANALYZER,
40 //      SR_HWCAP_OSCILLOSCOPE,
41 //      SR_HWCAP_PAT_GENERATOR,
42
43         SR_HWCAP_SAMPLERATE,
44 //      SR_HWCAP_CAPTURE_RATIO,
45         SR_HWCAP_LIMIT_SAMPLES,
46         0,
47 };
48
49 static uint64_t supported_samplerates[] = {
50         SR_HZ(100),
51         SR_HZ(200),
52         SR_HZ(500),
53         SR_KHZ(1),
54         SR_KHZ(2),
55         SR_KHZ(5),
56         SR_KHZ(10),
57         SR_KHZ(20),
58         SR_KHZ(50),
59         SR_KHZ(100),
60         SR_KHZ(200),
61         SR_KHZ(500),
62         SR_MHZ(1),
63         SR_MHZ(2),
64         SR_MHZ(5),
65         SR_MHZ(10),
66         SR_MHZ(20),
67         SR_MHZ(50),
68         SR_MHZ(100),
69         SR_MHZ(200),
70         0,
71 };
72
73 static struct sr_samplerates samplerates = {
74         SR_HZ(100),
75         SR_MHZ(200),
76         SR_HZ(0),
77         supported_samplerates,
78 };
79
80 static GSList *device_instances = NULL;
81
82 static int mso_send_control_message(struct sr_device_instance *sdi,
83                 uint16_t payload[], int n)
84 {
85         int fd = sdi->serial->fd;
86         int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot);
87         char *p, *buf;
88
89         ret = SR_ERR;
90
91         if (fd < 0)
92                 goto ret;
93
94         if (!(buf = g_try_malloc(s))) {
95                 sr_err("mso19: %s: buf malloc failed", __func__);
96                 ret = SR_ERR_MALLOC;
97                 goto ret;
98         }
99
100         p = buf;
101         memcpy(p, mso_head, sizeof(mso_head));
102         p += sizeof(mso_head);
103
104         for (i = 0; i < n; i++) {
105                 *(uint16_t *) p = htons(payload[i]);
106                 p += 2;
107         }
108         memcpy(p, mso_foot, sizeof(mso_foot));
109
110         w = 0;
111         while (w < s) {
112                 ret = serial_write(fd, buf + w, s - w);
113                 if (ret < 0) {
114                         ret = SR_ERR;
115                         goto free;
116                 }
117                 w += ret;
118         }
119         ret = SR_OK;
120 free:
121         g_free(buf);
122 ret:
123         return ret;
124 }
125
126 static int mso_reset_adc(struct sr_device_instance *sdi)
127 {
128         struct mso *mso = sdi->priv;
129         uint16_t ops[2];
130
131         ops[0] = mso_trans(REG_CTL, (mso->ctlbase | BIT_CTL_RESETADC));
132         ops[1] = mso_trans(REG_CTL, mso->ctlbase);
133         mso->ctlbase |= BIT_CTL_ADC_UNKNOWN4;
134
135         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
136 }
137
138 static int mso_reset_fsm(struct sr_device_instance *sdi)
139 {
140         struct mso *mso = sdi->priv;
141         uint16_t ops[1];
142
143         mso->ctlbase |= BIT_CTL_RESETFSM;
144         ops[0] = mso_trans(REG_CTL, mso->ctlbase);
145
146         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
147 }
148
149 static int mso_toggle_led(struct sr_device_instance *sdi, int state)
150 {
151         struct mso *mso = sdi->priv;
152         uint16_t ops[1];
153
154         mso->ctlbase &= BIT_CTL_LED;
155         if (state)
156                 mso->ctlbase |= BIT_CTL_LED;
157         ops[0] = mso_trans(REG_CTL, mso->ctlbase);
158
159         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
160 }
161
162 static int mso_check_trigger(struct sr_device_instance *sdi,
163                 uint8_t *info)
164 {
165         uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) };
166         char buf[1];
167         int ret;
168
169         ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
170         if (info == NULL || ret != SR_OK)
171                 return ret;
172
173         buf[0] = 0;
174         if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
175                 ret = SR_ERR;
176         *info = buf[0];
177
178         return ret;
179 }
180
181 static int mso_read_buffer(struct sr_device_instance *sdi)
182 {
183         uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
184
185         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
186 }
187
188 static int mso_arm(struct sr_device_instance *sdi)
189 {
190         struct mso *mso = sdi->priv;
191         uint16_t ops[] = {
192                 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETFSM),
193                 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_ARM),
194                 mso_trans(REG_CTL, mso->ctlbase),
195         };
196
197         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
198 }
199
200 static int mso_force_capture(struct sr_device_instance *sdi)
201 {
202         struct mso *mso = sdi->priv;
203         uint16_t ops[] = {
204                 mso_trans(REG_CTL, mso->ctlbase | 8),
205                 mso_trans(REG_CTL, mso->ctlbase),
206         };
207
208         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
209 }
210
211 static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
212 {
213         struct mso *mso = sdi->priv;
214         uint16_t ops[] = {
215                 mso_trans(REG_DAC1, (val >> 8) & 0xff),
216                 mso_trans(REG_DAC2, val & 0xff),
217                 mso_trans(REG_CTL, mso->ctlbase | BIT_CTL_RESETADC),
218         };
219
220         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
221 }
222
223 static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
224 {
225         uint16_t ops[] = {
226                 mso_trans(REG_CLKRATE1, (val >> 8) & 0xff),
227                 mso_trans(REG_CLKRATE2, val & 0xff),
228         };
229
230         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
231 }
232
233 static int mso_configure_rate(struct sr_device_instance *sdi,
234                 uint32_t rate)
235 {
236         struct mso *mso = sdi->priv;
237         unsigned int i;
238         int ret = SR_ERR;
239
240         for (i = 0; i < ARRAY_SIZE(rate_map); i++) {
241                 if (rate_map[i].rate == rate) {
242                         mso->slowmode = rate_map[i].slowmode;
243                         ret = mso_clkrate_out(sdi, rate_map[i].val);
244                         if (ret == SR_OK)
245                                 mso->cur_rate = rate;
246                         return ret;
247                 }
248         }
249         return ret;
250 }
251
252 static inline uint16_t mso_calc_raw_from_mv(struct mso *mso)
253 {
254         return (uint16_t) (0x200 -
255                         ((mso->dso_trigger_voltage / mso->dso_probe_attn) /
256                          mso->vbit));
257 }
258
259 static int mso_configure_trigger(struct sr_device_instance *sdi)
260 {
261         struct mso *mso = sdi->priv;
262         uint16_t ops[16];
263         uint16_t dso_trigger = mso_calc_raw_from_mv(mso);
264
265         dso_trigger &= 0x3ff;
266         if ((!mso->trigger_slope && mso->trigger_chan == 1) ||
267                         (mso->trigger_slope &&
268                          (mso->trigger_chan == 0 ||
269                           mso->trigger_chan == 2 ||
270                           mso->trigger_chan == 3)))
271                 dso_trigger |= 0x400;
272
273         switch (mso->trigger_chan) {
274         case 1:
275                 dso_trigger |= 0xe000;
276         case 2:
277                 dso_trigger |= 0x4000;
278                 break;
279         case 3:
280                 dso_trigger |= 0x2000;
281                 break;
282         case 4:
283                 dso_trigger |= 0xa000;
284                 break;
285         case 5:
286                 dso_trigger |= 0x8000;
287                 break;
288         default:
289         case 0:
290                 break;
291         }
292
293         switch (mso->trigger_outsrc) {
294         case 1:
295                 dso_trigger |= 0x800;
296                 break;
297         case 2:
298                 dso_trigger |= 0x1000;
299                 break;
300         case 3:
301                 dso_trigger |= 0x1800;
302                 break;
303
304         }
305
306         ops[0] = mso_trans(5, mso->la_trigger);
307         ops[1] = mso_trans(6, mso->la_trigger_mask);
308         ops[2] = mso_trans(3, dso_trigger & 0xff);
309         ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff);
310         ops[4] = mso_trans(11,
311                         mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
312         ops[5] = mso_trans(15, (2 | mso->slowmode));
313
314         /* FIXME SPI/I2C Triggers */
315         ops[6] = mso_trans(0, 0);
316         ops[7] = mso_trans(1, 0);
317         ops[8] = mso_trans(2, 0);
318         ops[9] = mso_trans(3, 0);
319         ops[10] = mso_trans(4, 0xff);
320         ops[11] = mso_trans(5, 0xff);
321         ops[12] = mso_trans(6, 0xff);
322         ops[13] = mso_trans(7, 0xff);
323         ops[14] = mso_trans(8, mso->trigger_spimode);
324         ops[15] = mso_trans(15, mso->slowmode);
325
326         return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
327 }
328
329 static int mso_configure_threshold_level(struct sr_device_instance *sdi)
330 {
331         struct mso *mso = sdi->priv;
332
333         return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
334 }
335
336 static int mso_parse_serial(const char *iSerial, const char *iProduct,
337                 struct mso *mso)
338 {
339         unsigned int u1, u2, u3, u4, u5, u6;
340
341         iProduct = iProduct;
342         /* FIXME: This code is in the original app, but I think its
343          * used only for the GUI */
344 /*      if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03"))
345                 mso->num_sample_rates = 0x16;
346         else
347                 mso->num_sample_rates = 0x10; */
348
349         /* parse iSerial */
350         if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u",
351                                 &u1, &u2, &u3, &u4, &u5, &u6) != 6)
352                 return SR_ERR;
353         mso->hwmodel = u4;
354         mso->hwrev = u5;
355         mso->serial = u6;
356         mso->vbit = u1 / 10000;
357         if (mso->vbit == 0)
358                 mso->vbit = 4.19195;
359         mso->dac_offset = u2;
360         if (mso->dac_offset == 0)
361                 mso->dac_offset = 0x1ff;
362         mso->offset_range = u3;
363         if (mso->offset_range == 0)
364                 mso->offset_range = 0x17d;
365
366         /*
367          * FIXME: There is more code on the original software to handle
368          * bigger iSerial strings, but as I can't test on my device
369          * I will not implement it yet
370          */
371
372         return SR_OK;
373 }
374
375 static int hw_init(const char *deviceinfo)
376 {
377         struct sr_device_instance *sdi;
378         int devcnt = 0;
379         struct udev *udev;
380         struct udev_enumerate *enumerate;
381         struct udev_list_entry *devices, *dev_list_entry;
382         struct mso *mso;
383
384         deviceinfo = deviceinfo;
385
386         /* It's easier to map usb<->serial using udev */
387         /*
388          * FIXME: On windows we can get the same information from the
389          * registry, add an #ifdef here later
390          */
391         udev = udev_new();
392         if (!udev) {
393                 sr_warn("Failed to initialize udev.");
394                 goto ret;
395         }
396         enumerate = udev_enumerate_new(udev);
397         udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
398         udev_enumerate_scan_devices(enumerate);
399         devices = udev_enumerate_get_list_entry(enumerate);
400         udev_list_entry_foreach(dev_list_entry, devices) {
401                 const char *syspath, *sysname, *idVendor, *idProduct,
402                         *iSerial, *iProduct;
403                 char path[32], manufacturer[32], product[32], hwrev[32];
404                 struct udev_device *dev, *parent;
405                 size_t s;
406
407                 syspath = udev_list_entry_get_name(dev_list_entry);
408                 dev = udev_device_new_from_syspath(udev, syspath);
409                 sysname = udev_device_get_sysname(dev);
410                 parent = udev_device_get_parent_with_subsystem_devtype(
411                                 dev, "usb", "usb_device");
412                 if (!parent) {
413                         sr_warn("Unable to find parent usb device for %s",
414                                 sysname);
415                         continue;
416                 }
417
418                 idVendor = udev_device_get_sysattr_value(parent, "idVendor");
419                 idProduct = udev_device_get_sysattr_value(parent, "idProduct");
420                 if (strcmp(USB_VENDOR, idVendor)
421                                 || strcmp(USB_PRODUCT, idProduct))
422                         continue;
423
424                 iSerial = udev_device_get_sysattr_value(parent, "serial");
425                 iProduct = udev_device_get_sysattr_value(parent, "product");
426
427                 snprintf(path, sizeof(path), "/dev/%s", sysname);
428
429                 s = strcspn(iProduct, " ");
430                 if (s > sizeof(product) ||
431                                 strlen(iProduct) - s > sizeof(manufacturer)) {
432                         sr_warn("Could not parse iProduct: %s", iProduct);
433                         continue;
434                 }
435                 strncpy(product, iProduct, s);
436                 product[s] = 0;
437                 strcpy(manufacturer, iProduct + s);
438                 sprintf(hwrev, "r%d", mso->hwrev);
439
440                 if (!(mso = g_try_malloc0(sizeof(struct mso)))) {
441                         sr_err("mso19: %s: mso malloc failed", __func__);
442                         continue; /* TODO: Errors handled correctly? */
443                 }
444
445                 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
446                         sr_warn("Invalid iSerial: %s", iSerial);
447                         goto err_free_mso;
448                 }
449                 /* hardware initial state */
450                 mso->ctlbase = 0;
451
452                 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
453                         manufacturer, product, hwrev);
454                 if (!sdi) {
455                         sr_warn("Unable to create device instance for %s",
456                                 sysname);
457                         goto err_free_mso;
458                 }
459
460                 /* save a pointer to our private instance data */
461                 sdi->priv = mso;
462
463                 sdi->serial = sr_serial_device_instance_new(path, -1);
464                 if (!sdi->serial)
465                         goto err_device_instance_free;
466
467                 device_instances = g_slist_append(device_instances, sdi);
468                 devcnt++;
469                 continue;
470
471 err_device_instance_free:
472                 sr_device_instance_free(sdi);
473 err_free_mso:
474                 free(mso);
475         }
476
477         udev_enumerate_unref(enumerate);
478         udev_unref(udev);
479
480 ret:
481         return devcnt;
482 }
483
484 static void hw_cleanup(void)
485 {
486         GSList *l;
487         struct sr_device_instance *sdi;
488
489         /* Properly close all devices. */
490         for (l = device_instances; l; l = l->next) {
491                 sdi = l->data;
492                 if (sdi->serial->fd != -1)
493                         serial_close(sdi->serial->fd);
494                 if (sdi->priv != NULL)
495                         free(sdi->priv);
496                 sr_device_instance_free(sdi);
497         }
498         g_slist_free(device_instances);
499         device_instances = NULL;
500 }
501
502 static int hw_opendev(int device_index)
503 {
504         struct sr_device_instance *sdi;
505         struct mso *mso;
506         int ret = SR_ERR;
507
508         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
509                 return ret;
510
511         mso = sdi->priv;
512         sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
513         if (sdi->serial->fd == -1)
514                 return ret;
515
516         ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2);
517         if (ret != SR_OK)
518                 return ret;
519
520         sdi->status = SR_ST_ACTIVE;
521
522         /* FIXME: discard serial buffer */
523
524         mso_check_trigger(sdi, &mso->trigger_state);
525 //      sr_warn("trigger state: %c", mso->trigger_state);
526
527         ret = mso_reset_adc(sdi);
528         if (ret != SR_OK)
529                 return ret;
530
531         mso_check_trigger(sdi, &mso->trigger_state);
532 //      sr_warn("trigger state: %c", mso->trigger_state);
533
534 //      ret = mso_reset_fsm(sdi);
535 //      if (ret != SR_OK)
536 //              return ret;
537
538 //      return SR_ERR;
539         return SR_OK;
540 }
541
542 static int hw_closedev(int device_index)
543 {
544         struct sr_device_instance *sdi;
545
546         if (!(sdi = sr_get_device_instance(device_instances, device_index))) {
547                 sr_err("mso19: %s: sdi was NULL", __func__);
548                 return SR_ERR; /* TODO: SR_ERR_ARG? */
549         }
550
551         /* TODO */
552         if (sdi->serial->fd != -1) {
553                 serial_close(sdi->serial->fd);
554                 sdi->serial->fd = -1;
555                 sdi->status = SR_ST_INACTIVE;
556         }
557
558         return SR_OK;
559 }
560
561 static void *hw_get_device_info(int device_index, int device_info_id)
562 {
563         struct sr_device_instance *sdi;
564         struct mso *mso;
565         void *info = NULL;
566
567         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
568                 return NULL;
569         mso = sdi->priv;
570
571         switch (device_info_id) {
572         case SR_DI_INSTANCE:
573                 info = sdi;
574                 break;
575         case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
576                 info = GINT_TO_POINTER(8);
577                 break;
578         case SR_DI_SAMPLERATES:
579                 info = &samplerates;
580                 break;
581         case SR_DI_TRIGGER_TYPES:
582                 info = "01"; /* FIXME */
583                 break;
584         case SR_DI_CUR_SAMPLERATE:
585                 info = &mso->cur_rate;
586                 break;
587         }
588         return info;
589 }
590
591 static int hw_get_status(int device_index)
592 {
593         struct sr_device_instance *sdi;
594
595         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
596                 return SR_ST_NOT_FOUND;
597
598         return sdi->status;
599 }
600
601 static int *hw_get_capabilities(void)
602 {
603         return capabilities;
604 }
605
606 static int hw_set_configuration(int device_index, int capability, void *value)
607 {
608         struct sr_device_instance *sdi;
609
610         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
611                 return SR_ERR;
612
613         switch (capability) {
614         case SR_HWCAP_SAMPLERATE:
615                 return mso_configure_rate(sdi, *(uint64_t *) value);
616         case SR_HWCAP_PROBECONFIG:
617         case SR_HWCAP_LIMIT_SAMPLES:
618         default:
619                 return SR_OK; /* FIXME */
620         }
621 }
622
623 #define MSO_TRIGGER_UNKNOWN     '!'
624 #define MSO_TRIGGER_UNKNOWN1    '1'
625 #define MSO_TRIGGER_UNKNOWN2    '2'
626 #define MSO_TRIGGER_UNKNOWN3    '3'
627 #define MSO_TRIGGER_WAIT        '4'
628 #define MSO_TRIGGER_FIRED       '5'
629 #define MSO_TRIGGER_DATAREADY   '6'
630
631 /* FIXME: Pass errors? */
632 static int receive_data(int fd, int revents, void *user_data)
633 {
634         struct sr_device_instance *sdi = user_data;
635         struct mso *mso = sdi->priv;
636         struct sr_datafeed_packet packet;
637         uint8_t in[1024], logic_out[1024];
638         double analog_out[1024];
639         size_t i, s;
640
641         revents = revents;
642
643         s = serial_read(fd, in, sizeof(in));
644         if (s <= 0)
645                 return FALSE;
646
647         /* No samples */
648         if (mso->trigger_state != MSO_TRIGGER_DATAREADY) {
649                 mso->trigger_state = in[0];
650                 if (mso->trigger_state == MSO_TRIGGER_DATAREADY) {
651                         mso_read_buffer(sdi);
652                         mso->buffer_n = 0;
653                 } else {
654                         mso_check_trigger(sdi, NULL);
655                 }
656                 return FALSE;
657         }
658
659         /* the hardware always dumps 1024 samples, 24bits each */
660         if (mso->buffer_n < 3072) {
661                 memcpy(mso->buffer + mso->buffer_n, in, s);
662                 mso->buffer_n += s;
663         }
664         if (mso->buffer_n < 3072)
665                 return FALSE;
666
667         /* do the conversion */
668         for (i = 0; i < 1024; i++) {
669                 /* FIXME: Need to do conversion to mV */
670                 analog_out[i] = (mso->buffer[i * 3] & 0x3f) |
671                         ((mso->buffer[i * 3 + 1] & 0xf) << 6);
672                 logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) |
673                         ((mso->buffer[i * 3 + 2] & 0x3f) << 2);
674         }
675
676         packet.type = SR_DF_LOGIC;
677         packet.length = 1024;
678         packet.unitsize = 1;
679         packet.payload = logic_out;
680         sr_session_bus(mso->session_id, &packet);
681
682
683         packet.type = SR_DF_ANALOG;
684         packet.length = 1024;
685         packet.unitsize = sizeof(double);
686         packet.payload = analog_out;
687         sr_session_bus(mso->session_id, &packet);
688
689         packet.type = SR_DF_END;
690         sr_session_bus(mso->session_id, &packet);
691
692         return TRUE;
693 }
694
695 static int hw_start_acquisition(int device_index, gpointer session_device_id)
696 {
697         struct sr_device_instance *sdi;
698         struct mso *mso;
699         struct sr_datafeed_packet packet;
700         struct sr_datafeed_header header;
701         int ret = SR_ERR;
702
703         if (!(sdi = sr_get_device_instance(device_instances, device_index)))
704                 return ret;
705         mso = sdi->priv;
706
707         /* FIXME: No need to do full reconfigure every time */
708 //      ret = mso_reset_fsm(sdi);
709 //      if (ret != SR_OK)
710 //              return ret;
711
712         /* FIXME: ACDC Mode */
713         mso->ctlbase &= 0x7f;
714 //      mso->ctlbase |= mso->acdcmode;
715
716         ret = mso_configure_rate(sdi, mso->cur_rate);
717         if (ret != SR_OK)
718                 return ret;
719
720         /* set dac offset */
721         ret = mso_dac_out(sdi, mso->dac_offset);
722         if (ret != SR_OK)
723                 return ret;
724
725         ret = mso_configure_threshold_level(sdi);
726         if (ret != SR_OK)
727                 return ret;
728
729         ret = mso_configure_trigger(sdi);
730         if (ret != SR_OK)
731                 return ret;
732
733         /* FIXME: trigger_position */
734
735
736         /* END of config hardware part */
737
738         /* with trigger */
739         ret = mso_arm(sdi);
740         if (ret != SR_OK)
741                 return ret;
742
743         /* without trigger */
744 //      ret = mso_force_capture(sdi);
745 //      if (ret != SR_OK)
746 //              return ret;
747
748         mso_check_trigger(sdi, &mso->trigger_state);
749         ret = mso_check_trigger(sdi, NULL);
750         if (ret != SR_OK)
751                 return ret;
752
753         mso->session_id = session_device_id;
754         sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
755
756         packet.type = SR_DF_HEADER;
757         packet.length = sizeof(struct sr_datafeed_header);
758         packet.payload = (unsigned char *) &header;
759         header.feed_version = 1;
760         gettimeofday(&header.starttime, NULL);
761         header.samplerate = mso->cur_rate;
762         header.num_analog_probes = 1;
763         header.num_logic_probes = 8;
764         header.protocol_id = SR_PROTO_RAW;
765         sr_session_bus(session_device_id, &packet);
766
767         return ret;
768 }
769
770 /* FIXME */
771 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
772 {
773         struct sr_datafeed_packet packet;
774
775         device_index = device_index;
776
777         packet.type = SR_DF_END;
778         sr_session_bus(session_device_id, &packet);
779 }
780
781 struct sr_device_plugin link_mso19_plugin_info = {
782         .name = "link-mso19",
783         .longname = "Link Instruments MSO-19",
784         .api_version = 1,
785         .init = hw_init,
786         .cleanup = hw_cleanup,
787         .opendev = hw_opendev,
788         .closedev = hw_closedev,
789         .get_device_info = hw_get_device_info,
790         .get_status = hw_get_status,
791         .get_capabilities = hw_get_capabilities,
792         .set_configuration = hw_set_configuration,
793         .start_acquisition = hw_start_acquisition,
794         .stop_acquisition = hw_stop_acquisition,
795 };