2 * This file is part of the sigrok project.
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt>
6 * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 static const int hwcaps[] = {
25 SR_HWCAP_LOGIC_ANALYZER,
27 SR_HWCAP_TRIGGER_SLOPE,
28 SR_HWCAP_HORIZ_TRIGGERPOS,
29 // SR_HWCAP_CAPTURE_RATIO,
30 SR_HWCAP_LIMIT_SAMPLES,
36 * Probes are numbered 0 to 7.
38 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
40 SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = {
41 "0", "1", "2", "3", "4", "5", "6", "7", NULL
44 /*supported samplerates */
45 static const struct sr_samplerates samplerates = {
52 SR_PRIV struct sr_dev_driver link_mso19_driver_info;
53 static struct sr_dev_driver *di = &link_mso19_driver_info;
55 static int hw_init(struct sr_context *sr_ctx)
57 struct drv_context *drvc;
59 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
60 sr_err("Driver context malloc failed.");
63 drvc->sr_ctx = sr_ctx;
69 static GSList *hw_scan(GSList *options)
74 GSList *devices = NULL;
76 const char* conn = NULL;
77 const char* serialcomm = NULL;
79 for (l = options; l; l = l->next) {
80 struct sr_hwopt* opt = l->data;
85 case SR_HWOPT_SERIALCOMM:
86 serialcomm = opt->value;
92 if (serialcomm == NULL)
93 serialcomm = SERIALCOMM;
95 struct udev *udev = udev_new();
97 sr_err("Failed to initialize udev.");
99 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
100 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
101 udev_enumerate_scan_devices(enumerate);
102 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
103 struct udev_list_entry *dev_list_entry;
104 for (dev_list_entry = devs;
105 dev_list_entry != NULL;
106 dev_list_entry = udev_list_entry_get_next(dev_list_entry))
108 const char *syspath = udev_list_entry_get_name(dev_list_entry);
109 struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
110 const char *sysname = udev_device_get_sysname(dev);
111 struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(
112 dev, "usb", "usb_device");
115 sr_err("Unable to find parent usb device for %s",
120 const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor");
121 const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct");
122 if (strcmp(USB_VENDOR, idVendor)
123 || strcmp(USB_PRODUCT, idProduct))
126 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
127 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
130 snprintf(path, sizeof(path), "/dev/%s", sysname);
133 size_t s = strcspn(iProduct, " ");
135 char manufacturer[32];
136 if (s > sizeof(product) ||
137 strlen(iProduct) - s > sizeof(manufacturer)) {
138 sr_err("Could not parse iProduct: %s.", iProduct);
141 strncpy(product, iProduct, s);
143 strcpy(manufacturer, iProduct + s + 1);
145 //Create the device context and set its params
146 struct dev_context *devc;
147 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
148 sr_err("Device context malloc failed.");
152 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
153 sr_err("Invalid iSerial: %s.", iSerial);
159 sprintf(hwrev, "r%d", devc->hwrev);
161 devc->protocol_trigger.spimode = 0;
162 for (i = 0; i < 4; i++) {
163 devc->protocol_trigger.word[i] = 0;
164 devc->protocol_trigger.mask[i] = 0xff;
167 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
173 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
174 manufacturer, product, hwrev);
177 sr_err("Unable to create device instance for %s",
179 sr_dev_inst_free(sdi);
187 for (i = 0; i < NUM_PROBES; i++) {
188 struct sr_probe *probe;
189 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
190 mso19_probe_names[i])))
192 sdi->probes = g_slist_append(sdi->probes, probe);
196 struct drv_context *drvc = di->priv;
197 drvc->instances = g_slist_append(drvc->instances, sdi);
198 devices = g_slist_append(devices, sdi);
204 static GSList *hw_dev_list(void)
206 struct drv_context *drvc;
210 return drvc->instances;
213 static int hw_dev_open(struct sr_dev_inst *sdi)
215 struct dev_context *devc;
219 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
222 sdi->status = SR_ST_ACTIVE;
224 /* FIXME: discard serial buffer */
225 mso_check_trigger(devc->serial, &devc->trigger_state);
226 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
228 int ret = mso_reset_adc(sdi);
232 mso_check_trigger(devc->serial, &devc->trigger_state);
233 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
235 // ret = mso_reset_fsm(sdi);
243 static int hw_dev_close(struct sr_dev_inst *sdi)
245 struct dev_context *devc;
249 if (devc->serial && devc->serial->fd != -1) {
250 serial_close(devc->serial);
251 sdi->status = SR_ST_INACTIVE;
257 static int hw_cleanup(void)
260 struct sr_dev_inst *sdi;
261 struct drv_context *drvc;
262 struct dev_context *devc;
265 if (!(drvc = di->priv))
268 /* Properly close and free all devices. */
269 for (l = drvc->instances; l; l = l->next) {
270 if (!(sdi = l->data)) {
271 /* Log error, but continue cleaning up the rest. */
272 sr_err("%s: sdi was NULL, continuing", __func__);
276 if (!(devc = sdi->priv)) {
277 /* Log error, but continue cleaning up the rest. */
278 sr_err("%s: sdi->priv was NULL, continuing", __func__);
283 sr_serial_dev_inst_free(devc->serial);
284 sr_dev_inst_free(sdi);
286 g_slist_free(drvc->instances);
287 drvc->instances = NULL;
292 static int hw_info_get(int info_id, const void **data,
293 const struct sr_dev_inst *sdi)
295 struct dev_context *devc;
301 case SR_DI_NUM_PROBES:
302 *data = GINT_TO_POINTER(1);
304 case SR_DI_PROBE_NAMES:
305 *data = mso19_probe_names;
307 case SR_DI_SAMPLERATES:
308 *data = &samplerates;
310 case SR_DI_TRIGGER_TYPES:
311 *data = (char *)TRIGGER_TYPES;
313 case SR_DI_CUR_SAMPLERATE:
316 *data = &devc->cur_rate;
327 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
332 struct dev_context *devc;
335 if (sdi->status != SR_ST_ACTIVE)
339 case SR_HWCAP_SAMPLERATE:
340 return mso_configure_rate(sdi, *(const uint64_t *) value);
343 case SR_HWCAP_LIMIT_SAMPLES:
345 const uint64_t num_samples = *(const uint64_t *)value;
346 if (num_samples < 1024)
348 sr_err("minimum of 1024 samples required");
352 devc->limit_samples = num_samples;
353 sr_dbg("setting limit_samples to %i\n", num_samples);
358 case SR_HWCAP_CAPTURE_RATIO:
361 case SR_HWCAP_TRIGGER_SLOPE:
363 const uint64_t slope = *(const uint64_t *)value;
364 if (slope != SLOPE_NEGATIVE && slope != SLOPE_POSITIVE)
366 sr_err("Invalid trigger slope");
369 devc->trigger_slope = slope;
374 case SR_HWCAP_HORIZ_TRIGGERPOS:
376 const float pos = *(const float *)value;
377 if (pos < 0 || pos > 255) {
378 sr_err("Trigger position (%f) should be between 0 and 255.", pos);
381 int trigger_pos = (int)pos;
382 devc->trigger_holdoff[0] = trigger_pos&0xff;
398 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
401 struct sr_datafeed_packet *packet;
402 struct sr_datafeed_header *header;
403 struct sr_datafeed_meta_logic meta;
404 struct dev_context *devc;
410 if (sdi->status != SR_ST_ACTIVE)
413 if (mso_configure_probes(sdi) != SR_OK) {
414 sr_err("Failed to configure probes.");
418 /* FIXME: No need to do full reconfigure every time */
419 // ret = mso_reset_fsm(sdi);
423 /* FIXME: ACDC Mode */
424 devc->ctlbase1 &= 0x7f;
425 // devc->ctlbase1 |= devc->acdcmode;
427 ret = mso_configure_rate(sdi, devc->cur_rate);
432 ret = mso_dac_out(sdi, devc->dac_offset);
436 ret = mso_configure_threshold_level(sdi);
441 ret = mso_configure_trigger(sdi);
446 /* END of config hardware part */
451 /* Start acquisition on the device. */
452 mso_check_trigger(devc->serial, &devc->trigger_state);
453 ret = mso_check_trigger(devc->serial, NULL);
457 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
459 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
460 sr_err("Datafeed packet malloc failed.");
461 return SR_ERR_MALLOC;
464 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
465 sr_err("Datafeed header malloc failed.");
467 return SR_ERR_MALLOC;
471 packet->type = SR_DF_HEADER;
472 packet->payload = (unsigned char *)header;
473 header->feed_version = 1;
474 gettimeofday(&header->starttime, NULL);
475 sr_session_send(cb_data, packet);
477 packet->type = SR_DF_META_LOGIC;
478 packet->payload = &meta;
479 meta.samplerate = devc->cur_rate;
480 meta.num_probes = NUM_PROBES;
481 sr_session_send(cb_data, packet);
489 /* This stops acquisition on ALL devices, ignoring dev_index. */
490 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
492 /* Avoid compiler warnings. */
495 stop_acquisition(sdi);
500 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
501 .name = "link-mso19",
502 .longname = "Link Instruments MSO-19",
505 .cleanup = hw_cleanup,
507 .dev_list = hw_dev_list,
508 .dev_clear = hw_cleanup,
509 .dev_open = hw_dev_open,
510 .dev_close = hw_dev_close,
511 .info_get = hw_info_get,
512 .dev_config_set = hw_dev_config_set,
513 .dev_acquisition_start = hw_dev_acquisition_start,
514 .dev_acquisition_stop = hw_dev_acquisition_stop,