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