2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 static const int hwcaps[] = {
23 SR_HWCAP_LOGIC_ANALYZER,
25 // SR_HWCAP_CAPTURE_RATIO,
26 SR_HWCAP_LIMIT_SAMPLES,
32 * Probes are numbered 0 to 7.
34 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
36 SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = {
37 "0", "1", "2", "3", "4", "5", "6", "7", NULL
40 /*supported samplerates */
41 static const struct sr_samplerates samplerates = {
68 SR_PRIV struct sr_dev_driver link_mso19_driver_info;
69 static struct sr_dev_driver *di = &link_mso19_driver_info;
71 static int hw_init(struct sr_context *sr_ctx)
73 printf("Init driver\n");
75 struct drv_context *drvc;
77 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
78 sr_err("Driver context malloc failed.");
81 drvc->sr_ctx = sr_ctx;
87 static GSList *hw_scan(GSList *options)
89 //struct sr_hwopt *opt;
90 //struct sr_probe *probe;
98 GSList *devices = NULL;
100 sr_info("Checking for link mso19\n");
102 const char* conn = NULL;
103 const char* serialcomm = NULL;
105 for (l = options; l; l = l->next) {
106 struct sr_hwopt* opt = l->data;
107 switch (opt->hwopt) {
111 case SR_HWOPT_SERIALCOMM:
112 serialcomm = opt->value;
118 if (serialcomm == NULL)
119 serialcomm = SERIALCOMM;
121 struct udev *udev = udev_new();
123 sr_err("Failed to initialize udev.");
125 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
126 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
127 udev_enumerate_scan_devices(enumerate);
128 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
129 struct udev_list_entry *dev_list_entry;
130 for (dev_list_entry = devs;
131 dev_list_entry != NULL;
132 dev_list_entry = udev_list_entry_get_next(dev_list_entry))
134 const char *syspath = udev_list_entry_get_name(dev_list_entry);
135 struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
136 const char *sysname = udev_device_get_sysname(dev);
137 struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(
138 dev, "usb", "usb_device");
141 sr_err("Unable to find parent usb device for %s",
146 const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor");
147 const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct");
148 if (strcmp(USB_VENDOR, idVendor)
149 || strcmp(USB_PRODUCT, idProduct))
152 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
153 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
156 snprintf(path, sizeof(path), "/dev/%s", sysname);
158 size_t s = strcspn(iProduct, " ");
160 char manufacturer[32];
161 if (s > sizeof(product) ||
162 strlen(iProduct) - s > sizeof(manufacturer)) {
163 sr_err("Could not parse iProduct: %s.", iProduct);
166 strncpy(product, iProduct, s);
168 strcpy(manufacturer, iProduct + s + 1);
170 //Create the device context and set its params
171 struct dev_context *devc;
172 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
173 sr_err("Device context malloc failed.");
177 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
178 sr_err("Invalid iSerial: %s.", iSerial);
184 sprintf(hwrev, "r%d", devc->hwrev);
186 devc->protocol_trigger.spimode = 0;
187 for (i = 0; i < 4; i++) {
188 devc->protocol_trigger.word[i] = 0;
189 devc->protocol_trigger.mask[i] = 0xff;
192 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
198 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
199 manufacturer, product, hwrev);
202 sr_err("Unable to create device instance for %s",
204 sr_dev_inst_free(sdi);
212 for (i = 0; i < NUM_PROBES; i++) {
213 struct sr_probe *probe;
214 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
215 mso19_probe_names[i])))
217 sdi->probes = g_slist_append(sdi->probes, probe);
221 printf("Add the context\n");
223 struct drv_context *drvc = di->priv;
224 drvc->instances = g_slist_append(drvc->instances, sdi);
225 devices = g_slist_append(devices, sdi);
228 printf("Return devices\n");
232 static GSList *hw_dev_list(void)
234 printf("Dev list\n");
235 struct drv_context *drvc;
239 return drvc->instances;
242 static int hw_dev_open(struct sr_dev_inst *sdi)
244 printf("Dev opewn\n");
245 struct dev_context *devc;
249 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
252 sdi->status = SR_ST_ACTIVE;
254 /* FIXME: discard serial buffer */
255 mso_check_trigger(devc->serial, &devc->trigger_state);
256 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
258 int ret = mso_reset_adc(sdi);
262 mso_check_trigger(devc->serial, &devc->trigger_state);
263 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
265 // ret = mso_reset_fsm(sdi);
273 static int hw_dev_close(struct sr_dev_inst *sdi)
275 printf("dev close\n");
276 struct dev_context *devc;
280 if (devc->serial && devc->serial->fd != -1) {
281 serial_close(devc->serial);
282 sdi->status = SR_ST_INACTIVE;
288 static int hw_cleanup(void)
290 printf("*Dev clearup\n");
292 struct sr_dev_inst *sdi;
293 struct drv_context *drvc;
294 struct dev_context *devc;
297 if (!(drvc = di->priv))
300 /* Properly close and free all devices. */
301 for (l = drvc->instances; l; l = l->next) {
302 if (!(sdi = l->data)) {
303 /* Log error, but continue cleaning up the rest. */
304 sr_err("%s: sdi was NULL, continuing", __func__);
308 if (!(devc = sdi->priv)) {
309 /* Log error, but continue cleaning up the rest. */
310 sr_err("%s: sdi->priv was NULL, continuing", __func__);
315 sr_serial_dev_inst_free(devc->serial);
316 sr_dev_inst_free(sdi);
318 g_slist_free(drvc->instances);
319 drvc->instances = NULL;
324 static int hw_info_get(int info_id, const void **data,
325 const struct sr_dev_inst *sdi)
327 struct dev_context *devc;
329 printf("Get info\n");
335 case SR_DI_NUM_PROBES:
336 *data = GINT_TO_POINTER(1);
338 case SR_DI_PROBE_NAMES:
339 *data = mso19_probe_names;
341 case SR_DI_SAMPLERATES:
342 *data = &samplerates;
344 case SR_DI_TRIGGER_TYPES:
345 *data = (char *)TRIGGER_TYPES;
347 case SR_DI_CUR_SAMPLERATE:
350 *data = &devc->cur_rate;
361 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
366 if (sdi->status != SR_ST_ACTIVE)
370 case SR_HWCAP_SAMPLERATE:
371 return mso_configure_rate(sdi, *(const uint64_t *) value);
374 case SR_HWCAP_LIMIT_SAMPLES:
377 case SR_HWCAP_CAPTURE_RATIO:
390 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
393 struct sr_datafeed_packet *packet;
394 struct sr_datafeed_header *header;
395 struct sr_datafeed_meta_logic meta;
396 struct dev_context *devc;
400 printf("Accquistion start\n");
403 if (sdi->status != SR_ST_ACTIVE)
406 //TODO if (ols_configure_probes(sdi) != SR_OK) {
407 //TODO sr_err("Failed to configure probes.");
408 //TODO return SR_ERR;
412 * Enable/disable channel groups in the flag register according to the
413 * probe mask. Calculate this here, because num_channels is needed
414 * to limit readcount.
418 //for (i = 0; i < 4; i++) {
419 // if (devc->probe_mask & (0xff << (i * 8))) {
420 // changrp_mask |= (1 << i);
425 /* FIXME: No need to do full reconfigure every time */
426 // ret = mso_reset_fsm(sdi);
430 /* FIXME: ACDC Mode */
431 devc->ctlbase1 &= 0x7f;
432 // devc->ctlbase1 |= devc->acdcmode;
434 printf("Configure rate\n");
435 ret = mso_configure_rate(sdi, devc->cur_rate);
440 printf("Configure dac\n");
441 ret = mso_dac_out(sdi, devc->dac_offset);
445 printf("Configure threshold level\n");
446 ret = mso_configure_threshold_level(sdi);
450 printf("Configure trigger\n");
451 ret = mso_configure_trigger(sdi);
455 /* FIXME: trigger_position */
458 /* END of config hardware part */
466 /* without trigger */
467 // ret = mso_force_capture(sdi);
471 /* Start acquisition on the device. */
472 printf("Check trigger\n");
473 mso_check_trigger(devc->serial, &devc->trigger_state);
474 ret = mso_check_trigger(devc->serial, NULL);
478 printf("Source add\n");
479 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
481 printf("Create packet\n");
482 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
483 sr_err("Datafeed packet malloc failed.");
484 return SR_ERR_MALLOC;
487 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
488 sr_err("Datafeed header malloc failed.");
490 return SR_ERR_MALLOC;
493 packet->type = SR_DF_HEADER;
494 packet->payload = (unsigned char *)header;
495 header->feed_version = 1;
496 gettimeofday(&header->starttime, NULL);
497 sr_session_send(cb_data, packet);
499 packet->type = SR_DF_META_LOGIC;
500 packet->payload = &meta;
501 meta.samplerate = devc->cur_rate;
502 meta.num_probes = NUM_PROBES;
503 sr_session_send(cb_data, packet);
511 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
512 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
514 printf("Accuqstion stop\n");
515 /* Avoid compiler warnings. */
518 stop_acquisition(sdi);
523 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
524 .name = "link-mso19",
525 .longname = "Link Instruments MSO-19",
528 .cleanup = hw_cleanup,
530 .dev_list = hw_dev_list,
531 .dev_clear = hw_cleanup,
532 .dev_open = hw_dev_open,
533 .dev_close = hw_dev_close,
534 .info_get = hw_info_get,
535 .dev_config_set = hw_dev_config_set,
536 .dev_acquisition_start = hw_dev_acquisition_start,
537 .dev_acquisition_stop = hw_dev_acquisition_stop,