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_TRIGGER_SLOPE,
26 SR_HWCAP_HORIZ_TRIGGERPOS,
27 // SR_HWCAP_CAPTURE_RATIO,
28 SR_HWCAP_LIMIT_SAMPLES,
34 * Probes are numbered 0 to 7.
36 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
38 SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = {
39 "0", "1", "2", "3", "4", "5", "6", "7", NULL
42 /*supported samplerates */
43 static const struct sr_samplerates samplerates = {
50 SR_PRIV struct sr_dev_driver link_mso19_driver_info;
51 static struct sr_dev_driver *di = &link_mso19_driver_info;
53 static int hw_init(struct sr_context *sr_ctx)
55 struct drv_context *drvc;
57 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
58 sr_err("Driver context malloc failed.");
61 drvc->sr_ctx = sr_ctx;
67 static GSList *hw_scan(GSList *options)
72 GSList *devices = NULL;
74 const char* conn = NULL;
75 const char* serialcomm = NULL;
77 for (l = options; l; l = l->next) {
78 struct sr_hwopt* opt = l->data;
83 case SR_HWOPT_SERIALCOMM:
84 serialcomm = opt->value;
90 if (serialcomm == NULL)
91 serialcomm = SERIALCOMM;
93 struct udev *udev = udev_new();
95 sr_err("Failed to initialize udev.");
97 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
98 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
99 udev_enumerate_scan_devices(enumerate);
100 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
101 struct udev_list_entry *dev_list_entry;
102 for (dev_list_entry = devs;
103 dev_list_entry != NULL;
104 dev_list_entry = udev_list_entry_get_next(dev_list_entry))
106 const char *syspath = udev_list_entry_get_name(dev_list_entry);
107 struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
108 const char *sysname = udev_device_get_sysname(dev);
109 struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(
110 dev, "usb", "usb_device");
113 sr_err("Unable to find parent usb device for %s",
118 const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor");
119 const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct");
120 if (strcmp(USB_VENDOR, idVendor)
121 || strcmp(USB_PRODUCT, idProduct))
124 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
125 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
128 snprintf(path, sizeof(path), "/dev/%s", sysname);
131 size_t s = strcspn(iProduct, " ");
133 char manufacturer[32];
134 if (s > sizeof(product) ||
135 strlen(iProduct) - s > sizeof(manufacturer)) {
136 sr_err("Could not parse iProduct: %s.", iProduct);
139 strncpy(product, iProduct, s);
141 strcpy(manufacturer, iProduct + s + 1);
143 //Create the device context and set its params
144 struct dev_context *devc;
145 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
146 sr_err("Device context malloc failed.");
150 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
151 sr_err("Invalid iSerial: %s.", iSerial);
157 sprintf(hwrev, "r%d", devc->hwrev);
159 devc->protocol_trigger.spimode = 0;
160 for (i = 0; i < 4; i++) {
161 devc->protocol_trigger.word[i] = 0;
162 devc->protocol_trigger.mask[i] = 0xff;
165 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
171 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
172 manufacturer, product, hwrev);
175 sr_err("Unable to create device instance for %s",
177 sr_dev_inst_free(sdi);
185 for (i = 0; i < NUM_PROBES; i++) {
186 struct sr_probe *probe;
187 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
188 mso19_probe_names[i])))
190 sdi->probes = g_slist_append(sdi->probes, probe);
194 struct drv_context *drvc = di->priv;
195 drvc->instances = g_slist_append(drvc->instances, sdi);
196 devices = g_slist_append(devices, sdi);
202 static GSList *hw_dev_list(void)
204 struct drv_context *drvc;
208 return drvc->instances;
211 static int hw_dev_open(struct sr_dev_inst *sdi)
213 struct dev_context *devc;
217 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
220 sdi->status = SR_ST_ACTIVE;
222 /* FIXME: discard serial buffer */
223 mso_check_trigger(devc->serial, &devc->trigger_state);
224 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
226 int ret = mso_reset_adc(sdi);
230 mso_check_trigger(devc->serial, &devc->trigger_state);
231 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
233 // ret = mso_reset_fsm(sdi);
241 static int hw_dev_close(struct sr_dev_inst *sdi)
243 struct dev_context *devc;
247 if (devc->serial && devc->serial->fd != -1) {
248 serial_close(devc->serial);
249 sdi->status = SR_ST_INACTIVE;
255 static int hw_cleanup(void)
258 struct sr_dev_inst *sdi;
259 struct drv_context *drvc;
260 struct dev_context *devc;
263 if (!(drvc = di->priv))
266 /* Properly close and free all devices. */
267 for (l = drvc->instances; l; l = l->next) {
268 if (!(sdi = l->data)) {
269 /* Log error, but continue cleaning up the rest. */
270 sr_err("%s: sdi was NULL, continuing", __func__);
274 if (!(devc = sdi->priv)) {
275 /* Log error, but continue cleaning up the rest. */
276 sr_err("%s: sdi->priv was NULL, continuing", __func__);
281 sr_serial_dev_inst_free(devc->serial);
282 sr_dev_inst_free(sdi);
284 g_slist_free(drvc->instances);
285 drvc->instances = NULL;
290 static int hw_info_get(int info_id, const void **data,
291 const struct sr_dev_inst *sdi)
293 struct dev_context *devc;
299 case SR_DI_NUM_PROBES:
300 *data = GINT_TO_POINTER(1);
302 case SR_DI_PROBE_NAMES:
303 *data = mso19_probe_names;
305 case SR_DI_SAMPLERATES:
306 *data = &samplerates;
308 case SR_DI_TRIGGER_TYPES:
309 *data = (char *)TRIGGER_TYPES;
311 case SR_DI_CUR_SAMPLERATE:
314 *data = &devc->cur_rate;
325 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
330 struct dev_context *devc;
333 if (sdi->status != SR_ST_ACTIVE)
337 case SR_HWCAP_SAMPLERATE:
338 return mso_configure_rate(sdi, *(const uint64_t *) value);
341 case SR_HWCAP_LIMIT_SAMPLES:
343 const uint64_t num_samples = *(const uint64_t *)value;
344 if (num_samples < 1024)
346 sr_err("minimum of 1024 samples required");
350 devc->limit_samples = num_samples;
351 sr_dbg("setting limit_samples to %i\n", num_samples);
356 case SR_HWCAP_CAPTURE_RATIO:
359 case SR_HWCAP_TRIGGER_SLOPE:
361 const uint64_t slope = *(const uint64_t *)value;
362 if (slope != SLOPE_NEGATIVE && slope != SLOPE_POSITIVE)
364 sr_err("Invalid trigger slope");
367 devc->trigger_slope = slope;
372 case SR_HWCAP_HORIZ_TRIGGERPOS:
374 const float pos = *(const float *)value;
375 if (pos < 0 || pos > 255) {
376 sr_err("Trigger position (%f) should be between 0 and 255.", pos);
379 int trigger_pos = (int)pos;
380 devc->trigger_holdoff[0] = trigger_pos&0xff;
396 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
399 struct sr_datafeed_packet *packet;
400 struct sr_datafeed_header *header;
401 struct sr_datafeed_meta_logic meta;
402 struct dev_context *devc;
408 if (sdi->status != SR_ST_ACTIVE)
411 if (mso_configure_probes(sdi) != SR_OK) {
412 sr_err("Failed to configure probes.");
416 /* FIXME: No need to do full reconfigure every time */
417 // ret = mso_reset_fsm(sdi);
421 /* FIXME: ACDC Mode */
422 devc->ctlbase1 &= 0x7f;
423 // devc->ctlbase1 |= devc->acdcmode;
425 ret = mso_configure_rate(sdi, devc->cur_rate);
430 ret = mso_dac_out(sdi, devc->dac_offset);
434 ret = mso_configure_threshold_level(sdi);
439 ret = mso_configure_trigger(sdi);
444 /* END of config hardware part */
449 /* Start acquisition on the device. */
450 mso_check_trigger(devc->serial, &devc->trigger_state);
451 ret = mso_check_trigger(devc->serial, NULL);
455 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
457 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
458 sr_err("Datafeed packet malloc failed.");
459 return SR_ERR_MALLOC;
462 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
463 sr_err("Datafeed header malloc failed.");
465 return SR_ERR_MALLOC;
469 packet->type = SR_DF_HEADER;
470 packet->payload = (unsigned char *)header;
471 header->feed_version = 1;
472 gettimeofday(&header->starttime, NULL);
473 sr_session_send(cb_data, packet);
475 packet->type = SR_DF_META_LOGIC;
476 packet->payload = &meta;
477 meta.samplerate = devc->cur_rate;
478 meta.num_probes = NUM_PROBES;
479 sr_session_send(cb_data, packet);
487 /* This stops acquisition on ALL devices, ignoring dev_index. */
488 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
490 /* Avoid compiler warnings. */
493 stop_acquisition(sdi);
498 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
499 .name = "link-mso19",
500 .longname = "Link Instruments MSO-19",
503 .cleanup = hw_cleanup,
505 .dev_list = hw_dev_list,
506 .dev_clear = hw_cleanup,
507 .dev_open = hw_dev_open,
508 .dev_close = hw_dev_close,
509 .info_get = hw_info_get,
510 .dev_config_set = hw_dev_config_set,
511 .dev_acquisition_start = hw_dev_acquisition_start,
512 .dev_acquisition_stop = hw_dev_acquisition_stop,