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