]> sigrok.org Git - libsigrok.git/blame - hardware/link-mso19/link-mso19.c
Fix outdated ezusb_install_firmware() prototype.
[libsigrok.git] / hardware / link-mso19 / link-mso19.c
CommitLineData
01cf8814
DR
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.
8a839354
UH
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/>.
01cf8814
DR
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>
01cf8814 29#include <arpa/inet.h>
b7f09cf8
UH
30#include "sigrok.h"
31#include "sigrok-internal.h"
01cf8814
DR
32#include "config.h"
33#include "link-mso19.h"
34
35#define USB_VENDOR "3195"
36#define USB_PRODUCT "f190"
37
38static int capabilities[] = {
5a2326a7
UH
39 SR_HWCAP_LOGIC_ANALYZER,
40// SR_HWCAP_OSCILLOSCOPE,
41// SR_HWCAP_PAT_GENERATOR,
01cf8814 42
5a2326a7
UH
43 SR_HWCAP_SAMPLERATE,
44// SR_HWCAP_CAPTURE_RATIO,
45 SR_HWCAP_LIMIT_SAMPLES,
01cf8814
DR
46 0,
47};
48
49static uint64_t supported_samplerates[] = {
c9140419
UH
50 SR_HZ(100),
51 SR_HZ(200),
52 SR_HZ(500),
59df0c77
UH
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,
01cf8814
DR
71};
72
60679b18 73static struct sr_samplerates samplerates = {
c9140419 74 SR_HZ(100),
59df0c77 75 SR_MHZ(200),
c9140419 76 SR_HZ(0),
59df0c77 77 supported_samplerates,
01cf8814
DR
78};
79
80static GSList *device_instances = NULL;
81
a00ba012 82static int mso_send_control_message(struct sr_device_instance *sdi,
01cf8814
DR
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
ecad043f
UH
89 ret = SR_ERR;
90
01cf8814
DR
91 if (fd < 0)
92 goto ret;
93
ecad043f
UH
94 if (!(buf = g_try_malloc(s))) {
95 sr_err("mso19: %s: buf malloc failed", __func__);
96 ret = SR_ERR_MALLOC;
01cf8814 97 goto ret;
ecad043f 98 }
01cf8814
DR
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) {
2119ab03 112 ret = serial_write(fd, buf + w, s - w);
01cf8814 113 if (ret < 0) {
e46b8fb1 114 ret = SR_ERR;
01cf8814
DR
115 goto free;
116 }
117 w += ret;
118 }
e46b8fb1 119 ret = SR_OK;
01cf8814 120free:
ecad043f 121 g_free(buf);
01cf8814
DR
122ret:
123 return ret;
124}
125
a00ba012 126static int mso_reset_adc(struct sr_device_instance *sdi)
01cf8814
DR
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
a00ba012 138static int mso_reset_fsm(struct sr_device_instance *sdi)
01cf8814
DR
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
a00ba012 149static int mso_toggle_led(struct sr_device_instance *sdi, int state)
01cf8814
DR
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
a00ba012 162static int mso_check_trigger(struct sr_device_instance *sdi,
01cf8814
DR
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));
e46b8fb1 170 if (info == NULL || ret != SR_OK)
01cf8814
DR
171 return ret;
172
173 buf[0] = 0;
2119ab03 174 if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */
e46b8fb1 175 ret = SR_ERR;
01cf8814
DR
176 *info = buf[0];
177
178 return ret;
179}
180
a00ba012 181static int mso_read_buffer(struct sr_device_instance *sdi)
01cf8814
DR
182{
183 uint16_t ops[] = { mso_trans(REG_BUFFER, 0) };
184
185 return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops));
186}
187
a00ba012 188static int mso_arm(struct sr_device_instance *sdi)
01cf8814
DR
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
a00ba012 200static int mso_force_capture(struct sr_device_instance *sdi)
01cf8814
DR
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
a00ba012 211static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val)
01cf8814
DR
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
a00ba012 223static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val)
01cf8814
DR
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
a00ba012 233static int mso_configure_rate(struct sr_device_instance *sdi,
01cf8814
DR
234 uint32_t rate)
235{
236 struct mso *mso = sdi->priv;
237 unsigned int i;
e46b8fb1 238 int ret = SR_ERR;
01cf8814
DR
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);
e46b8fb1 244 if (ret == SR_OK)
01cf8814
DR
245 mso->cur_rate = rate;
246 return ret;
247 }
248 }
249 return ret;
250}
251
01cf8814
DR
252static 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
a00ba012 259static int mso_configure_trigger(struct sr_device_instance *sdi)
01cf8814
DR
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,
59df0c77 311 mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate));
01cf8814
DR
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
a00ba012 329static int mso_configure_threshold_level(struct sr_device_instance *sdi)
01cf8814
DR
330{
331 struct mso *mso = sdi->priv;
332
333 return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]);
334}
335
336static 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)
e46b8fb1 352 return SR_ERR;
01cf8814
DR
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
e46b8fb1 372 return SR_OK;
01cf8814
DR
373}
374
54ac5277 375static int hw_init(const char *deviceinfo)
01cf8814 376{
a00ba012 377 struct sr_device_instance *sdi;
01cf8814
DR
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) {
b08024a8 393 sr_warn("Failed to initialize udev.");
01cf8814
DR
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) {
b08024a8
UH
413 sr_warn("Unable to find parent usb device for %s",
414 sysname);
01cf8814
DR
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)) {
b08024a8 432 sr_warn("Could not parse iProduct: %s", iProduct);
01cf8814
DR
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
ecad043f
UH
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 }
01cf8814 444
e46b8fb1 445 if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) {
b08024a8 446 sr_warn("Invalid iSerial: %s", iSerial);
01cf8814
DR
447 goto err_free_mso;
448 }
449 /* hardware initial state */
450 mso->ctlbase = 0;
451
5a2326a7 452 sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING,
01cf8814
DR
453 manufacturer, product, hwrev);
454 if (!sdi) {
b08024a8
UH
455 sr_warn("Unable to create device instance for %s",
456 sysname);
01cf8814
DR
457 goto err_free_mso;
458 }
459
460 /* save a pointer to our private instance data */
461 sdi->priv = mso;
462
6c290072 463 sdi->serial = sr_serial_device_instance_new(path, -1);
01cf8814
DR
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
471err_device_instance_free:
a00ba012 472 sr_device_instance_free(sdi);
01cf8814
DR
473err_free_mso:
474 free(mso);
475 }
476
477 udev_enumerate_unref(enumerate);
478 udev_unref(udev);
479
480ret:
481 return devcnt;
482}
483
484static void hw_cleanup(void)
485{
486 GSList *l;
a00ba012 487 struct sr_device_instance *sdi;
01cf8814
DR
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);
a00ba012 496 sr_device_instance_free(sdi);
01cf8814
DR
497 }
498 g_slist_free(device_instances);
499 device_instances = NULL;
500}
501
502static int hw_opendev(int device_index)
503{
a00ba012 504 struct sr_device_instance *sdi;
01cf8814 505 struct mso *mso;
e46b8fb1 506 int ret = SR_ERR;
01cf8814 507
d32d961d 508 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
01cf8814
DR
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);
e46b8fb1 517 if (ret != SR_OK)
01cf8814
DR
518 return ret;
519
5a2326a7 520 sdi->status = SR_ST_ACTIVE;
01cf8814
DR
521
522 /* FIXME: discard serial buffer */
523
524 mso_check_trigger(sdi, &mso->trigger_state);
b08024a8 525// sr_warn("trigger state: %c", mso->trigger_state);
01cf8814
DR
526
527 ret = mso_reset_adc(sdi);
e46b8fb1 528 if (ret != SR_OK)
01cf8814
DR
529 return ret;
530
531 mso_check_trigger(sdi, &mso->trigger_state);
b08024a8 532// sr_warn("trigger state: %c", mso->trigger_state);
01cf8814
DR
533
534// ret = mso_reset_fsm(sdi);
e46b8fb1 535// if (ret != SR_OK)
01cf8814
DR
536// return ret;
537
e46b8fb1
UH
538// return SR_ERR;
539 return SR_OK;
01cf8814
DR
540}
541
697785d1 542static int hw_closedev(int device_index)
01cf8814 543{
a00ba012 544 struct sr_device_instance *sdi;
01cf8814 545
697785d1
UH
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 }
01cf8814 550
697785d1 551 /* TODO */
01cf8814
DR
552 if (sdi->serial->fd != -1) {
553 serial_close(sdi->serial->fd);
554 sdi->serial->fd = -1;
5a2326a7 555 sdi->status = SR_ST_INACTIVE;
01cf8814 556 }
697785d1
UH
557
558 return SR_OK;
01cf8814
DR
559}
560
561static void *hw_get_device_info(int device_index, int device_info_id)
562{
a00ba012 563 struct sr_device_instance *sdi;
01cf8814
DR
564 struct mso *mso;
565 void *info = NULL;
566
d32d961d 567 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
01cf8814
DR
568 return NULL;
569 mso = sdi->priv;
570
571 switch (device_info_id) {
5a2326a7 572 case SR_DI_INSTANCE:
01cf8814
DR
573 info = sdi;
574 break;
5a2326a7 575 case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */
01cf8814
DR
576 info = GINT_TO_POINTER(8);
577 break;
5a2326a7 578 case SR_DI_SAMPLERATES:
01cf8814
DR
579 info = &samplerates;
580 break;
5a2326a7 581 case SR_DI_TRIGGER_TYPES:
01cf8814
DR
582 info = "01"; /* FIXME */
583 break;
5a2326a7 584 case SR_DI_CUR_SAMPLERATE:
01cf8814
DR
585 info = &mso->cur_rate;
586 break;
587 }
588 return info;
589}
590
591static int hw_get_status(int device_index)
592{
a00ba012 593 struct sr_device_instance *sdi;
01cf8814 594
d32d961d 595 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
5a2326a7 596 return SR_ST_NOT_FOUND;
01cf8814
DR
597
598 return sdi->status;
599}
600
601static int *hw_get_capabilities(void)
602{
603 return capabilities;
604}
605
606static int hw_set_configuration(int device_index, int capability, void *value)
607{
a00ba012 608 struct sr_device_instance *sdi;
01cf8814 609
d32d961d 610 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 611 return SR_ERR;
01cf8814
DR
612
613 switch (capability) {
5a2326a7 614 case SR_HWCAP_SAMPLERATE:
01cf8814 615 return mso_configure_rate(sdi, *(uint64_t *) value);
5a2326a7
UH
616 case SR_HWCAP_PROBECONFIG:
617 case SR_HWCAP_LIMIT_SAMPLES:
01cf8814 618 default:
e46b8fb1 619 return SR_OK; /* FIXME */
01cf8814 620 }
01cf8814
DR
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? */
632static int receive_data(int fd, int revents, void *user_data)
633{
a00ba012 634 struct sr_device_instance *sdi = user_data;
01cf8814 635 struct mso *mso = sdi->priv;
b9c735a2 636 struct sr_datafeed_packet packet;
01cf8814
DR
637 uint8_t in[1024], logic_out[1024];
638 double analog_out[1024];
639 size_t i, s;
640
641 revents = revents;
642
2119ab03 643 s = serial_read(fd, in, sizeof(in));
01cf8814
DR
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
5a2326a7 676 packet.type = SR_DF_LOGIC;
01cf8814
DR
677 packet.length = 1024;
678 packet.unitsize = 1;
679 packet.payload = logic_out;
8a2efef2 680 sr_session_bus(mso->session_id, &packet);
01cf8814
DR
681
682
5a2326a7 683 packet.type = SR_DF_ANALOG;
01cf8814
DR
684 packet.length = 1024;
685 packet.unitsize = sizeof(double);
686 packet.payload = analog_out;
8a2efef2 687 sr_session_bus(mso->session_id, &packet);
01cf8814 688
5a2326a7 689 packet.type = SR_DF_END;
8a2efef2 690 sr_session_bus(mso->session_id, &packet);
01cf8814
DR
691
692 return TRUE;
693}
694
695static int hw_start_acquisition(int device_index, gpointer session_device_id)
696{
a00ba012 697 struct sr_device_instance *sdi;
01cf8814 698 struct mso *mso;
b9c735a2
UH
699 struct sr_datafeed_packet packet;
700 struct sr_datafeed_header header;
e46b8fb1 701 int ret = SR_ERR;
01cf8814 702
d32d961d 703 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
01cf8814
DR
704 return ret;
705 mso = sdi->priv;
706
707 /* FIXME: No need to do full reconfigure every time */
708// ret = mso_reset_fsm(sdi);
e46b8fb1 709// if (ret != SR_OK)
01cf8814
DR
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);
e46b8fb1 717 if (ret != SR_OK)
01cf8814
DR
718 return ret;
719
720 /* set dac offset */
721 ret = mso_dac_out(sdi, mso->dac_offset);
e46b8fb1 722 if (ret != SR_OK)
01cf8814
DR
723 return ret;
724
725 ret = mso_configure_threshold_level(sdi);
e46b8fb1 726 if (ret != SR_OK)
01cf8814
DR
727 return ret;
728
729 ret = mso_configure_trigger(sdi);
e46b8fb1 730 if (ret != SR_OK)
01cf8814
DR
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);
e46b8fb1 740 if (ret != SR_OK)
01cf8814
DR
741 return ret;
742
743 /* without trigger */
744// ret = mso_force_capture(sdi);
e46b8fb1 745// if (ret != SR_OK)
01cf8814
DR
746// return ret;
747
748 mso_check_trigger(sdi, &mso->trigger_state);
749 ret = mso_check_trigger(sdi, NULL);
e46b8fb1 750 if (ret != SR_OK)
01cf8814
DR
751 return ret;
752
753 mso->session_id = session_device_id;
6f1be0a2 754 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi);
01cf8814 755
5a2326a7 756 packet.type = SR_DF_HEADER;
b9c735a2 757 packet.length = sizeof(struct sr_datafeed_header);
01cf8814
DR
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;
5a2326a7 764 header.protocol_id = SR_PROTO_RAW;
8a2efef2 765 sr_session_bus(session_device_id, &packet);
01cf8814
DR
766
767 return ret;
768}
769
770/* FIXME */
771static void hw_stop_acquisition(int device_index, gpointer session_device_id)
772{
b9c735a2 773 struct sr_datafeed_packet packet;
01cf8814
DR
774
775 device_index = device_index;
776
5a2326a7 777 packet.type = SR_DF_END;
8a2efef2 778 sr_session_bus(session_device_id, &packet);
01cf8814
DR
779}
780
5c2d46d1 781struct sr_device_plugin link_mso19_plugin_info = {
01cf8814 782 .name = "link-mso19",
9f8274a5 783 .longname = "Link Instruments MSO-19",
01cf8814
DR
784 .api_version = 1,
785 .init = hw_init,
786 .cleanup = hw_cleanup,
86f5e3d8
UH
787 .opendev = hw_opendev,
788 .closedev = hw_closedev,
01cf8814
DR
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};