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 = {
48 SR_PRIV struct sr_dev_driver link_mso19_driver_info;
49 static struct sr_dev_driver *di = &link_mso19_driver_info;
51 static int hw_init(struct sr_context *sr_ctx)
53 struct drv_context *drvc;
55 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
56 sr_err("Driver context malloc failed.");
59 drvc->sr_ctx = sr_ctx;
65 static GSList *hw_scan(GSList *options)
70 GSList *devices = NULL;
72 const char* conn = NULL;
73 const char* serialcomm = NULL;
75 for (l = options; l; l = l->next) {
76 struct sr_hwopt* opt = l->data;
81 case SR_HWOPT_SERIALCOMM:
82 serialcomm = opt->value;
88 if (serialcomm == NULL)
89 serialcomm = SERIALCOMM;
91 struct udev *udev = udev_new();
93 sr_err("Failed to initialize udev.");
95 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
96 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
97 udev_enumerate_scan_devices(enumerate);
98 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
99 struct udev_list_entry *dev_list_entry;
100 for (dev_list_entry = devs;
101 dev_list_entry != NULL;
102 dev_list_entry = udev_list_entry_get_next(dev_list_entry))
104 const char *syspath = udev_list_entry_get_name(dev_list_entry);
105 struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
106 const char *sysname = udev_device_get_sysname(dev);
107 struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(
108 dev, "usb", "usb_device");
111 sr_err("Unable to find parent usb device for %s",
116 const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor");
117 const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct");
118 if (strcmp(USB_VENDOR, idVendor)
119 || strcmp(USB_PRODUCT, idProduct))
122 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
123 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
126 snprintf(path, sizeof(path), "/dev/%s", sysname);
129 size_t s = strcspn(iProduct, " ");
131 char manufacturer[32];
132 if (s > sizeof(product) ||
133 strlen(iProduct) - s > sizeof(manufacturer)) {
134 sr_err("Could not parse iProduct: %s.", iProduct);
137 strncpy(product, iProduct, s);
139 strcpy(manufacturer, iProduct + s + 1);
141 //Create the device context and set its params
142 struct dev_context *devc;
143 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
144 sr_err("Device context malloc failed.");
148 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
149 sr_err("Invalid iSerial: %s.", iSerial);
155 sprintf(hwrev, "r%d", devc->hwrev);
157 devc->protocol_trigger.spimode = 0;
158 for (i = 0; i < 4; i++) {
159 devc->protocol_trigger.word[i] = 0;
160 devc->protocol_trigger.mask[i] = 0xff;
163 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
169 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
170 manufacturer, product, hwrev);
173 sr_err("Unable to create device instance for %s",
175 sr_dev_inst_free(sdi);
183 for (i = 0; i < NUM_PROBES; i++) {
184 struct sr_probe *probe;
185 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
186 mso19_probe_names[i])))
188 sdi->probes = g_slist_append(sdi->probes, probe);
192 struct drv_context *drvc = di->priv;
193 drvc->instances = g_slist_append(drvc->instances, sdi);
194 devices = g_slist_append(devices, sdi);
200 static GSList *hw_dev_list(void)
202 struct drv_context *drvc;
206 return drvc->instances;
209 static int hw_dev_open(struct sr_dev_inst *sdi)
211 struct dev_context *devc;
215 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
218 sdi->status = SR_ST_ACTIVE;
220 /* FIXME: discard serial buffer */
221 mso_check_trigger(devc->serial, &devc->trigger_state);
222 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
224 int ret = mso_reset_adc(sdi);
228 mso_check_trigger(devc->serial, &devc->trigger_state);
229 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
231 // ret = mso_reset_fsm(sdi);
239 static int hw_dev_close(struct sr_dev_inst *sdi)
241 printf("dev close\n");
242 struct dev_context *devc;
246 if (devc->serial && devc->serial->fd != -1) {
247 serial_close(devc->serial);
248 sdi->status = SR_ST_INACTIVE;
254 static int hw_cleanup(void)
257 struct sr_dev_inst *sdi;
258 struct drv_context *drvc;
259 struct dev_context *devc;
262 if (!(drvc = di->priv))
265 /* Properly close and free all devices. */
266 for (l = drvc->instances; l; l = l->next) {
267 if (!(sdi = l->data)) {
268 /* Log error, but continue cleaning up the rest. */
269 sr_err("%s: sdi was NULL, continuing", __func__);
273 if (!(devc = sdi->priv)) {
274 /* Log error, but continue cleaning up the rest. */
275 sr_err("%s: sdi->priv was NULL, continuing", __func__);
280 sr_serial_dev_inst_free(devc->serial);
281 sr_dev_inst_free(sdi);
283 g_slist_free(drvc->instances);
284 drvc->instances = NULL;
289 static int hw_info_get(int info_id, const void **data,
290 const struct sr_dev_inst *sdi)
292 struct dev_context *devc;
298 case SR_DI_NUM_PROBES:
299 *data = GINT_TO_POINTER(1);
301 case SR_DI_PROBE_NAMES:
302 *data = mso19_probe_names;
304 case SR_DI_SAMPLERATES:
305 *data = &samplerates;
307 case SR_DI_TRIGGER_TYPES:
308 *data = (char *)TRIGGER_TYPES;
310 case SR_DI_CUR_SAMPLERATE:
313 *data = &devc->cur_rate;
324 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
329 if (sdi->status != SR_ST_ACTIVE)
333 case SR_HWCAP_SAMPLERATE:
334 return mso_configure_rate(sdi, *(const uint64_t *) value);
337 case SR_HWCAP_LIMIT_SAMPLES:
340 case SR_HWCAP_CAPTURE_RATIO:
353 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
356 struct sr_datafeed_packet *packet;
357 struct sr_datafeed_header *header;
358 struct sr_datafeed_meta_logic meta;
359 struct dev_context *devc;
363 printf("Accquistion start\n");
366 if (sdi->status != SR_ST_ACTIVE)
369 if (mso_configure_probes(sdi) != SR_OK) {
370 sr_err("Failed to configure probes.");
375 * Enable/disable channel groups in the flag register according to the
376 * probe mask. Calculate this here, because num_channels is needed
377 * to limit readcount.
381 //for (i = 0; i < 4; i++) {
382 // if (devc->probe_mask & (0xff << (i * 8))) {
383 // changrp_mask |= (1 << i);
388 /* FIXME: No need to do full reconfigure every time */
389 // ret = mso_reset_fsm(sdi);
393 /* FIXME: ACDC Mode */
394 devc->ctlbase1 &= 0x7f;
395 // devc->ctlbase1 |= devc->acdcmode;
397 printf("Configure rate\n");
398 ret = mso_configure_rate(sdi, devc->cur_rate);
403 printf("Configure dac\n");
404 ret = mso_dac_out(sdi, devc->dac_offset);
408 printf("Configure threshold level\n");
409 ret = mso_configure_threshold_level(sdi);
413 printf("Configure trigger\n");
414 ret = mso_configure_trigger(sdi);
418 /* FIXME: trigger_position */
421 /* END of config hardware part */
429 /* without trigger */
430 // ret = mso_force_capture(sdi);
434 /* Start acquisition on the device. */
435 printf("Check trigger\n");
436 mso_check_trigger(devc->serial, &devc->trigger_state);
437 ret = mso_check_trigger(devc->serial, NULL);
441 printf("Source add\n");
442 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
444 printf("Create packet\n");
445 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
446 sr_err("Datafeed packet malloc failed.");
447 return SR_ERR_MALLOC;
450 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
451 sr_err("Datafeed header malloc failed.");
453 return SR_ERR_MALLOC;
456 packet->type = SR_DF_HEADER;
457 packet->payload = (unsigned char *)header;
458 header->feed_version = 1;
459 gettimeofday(&header->starttime, NULL);
460 sr_session_send(cb_data, packet);
462 packet->type = SR_DF_META_LOGIC;
463 packet->payload = &meta;
464 meta.samplerate = devc->cur_rate;
465 meta.num_probes = NUM_PROBES;
466 sr_session_send(cb_data, packet);
474 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
475 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
477 /* Avoid compiler warnings. */
480 stop_acquisition(sdi);
485 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
486 .name = "link-mso19",
487 .longname = "Link Instruments MSO-19",
490 .cleanup = hw_cleanup,
492 .dev_list = hw_dev_list,
493 .dev_clear = hw_cleanup,
494 .dev_open = hw_dev_open,
495 .dev_close = hw_dev_close,
496 .info_get = hw_info_get,
497 .dev_config_set = hw_dev_config_set,
498 .dev_acquisition_start = hw_dev_acquisition_start,
499 .dev_acquisition_stop = hw_dev_acquisition_stop,