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.");
64 drvc->sr_ctx = sr_ctx;
70 static GSList *hw_scan(GSList *options)
73 GSList *devices = NULL;
74 const char *conn = NULL;
75 const char *serialcomm = NULL;
77 struct sr_config *src;
82 for (l = options; l; l = l->next) {
88 case SR_HWOPT_SERIALCOMM:
89 serialcomm = src->value;
95 if (serialcomm == NULL)
96 serialcomm = SERIALCOMM;
100 sr_err("Failed to initialize udev.");
103 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
104 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
105 udev_enumerate_scan_devices(enumerate);
106 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
107 struct udev_list_entry *dev_list_entry;
108 for (dev_list_entry = devs;
109 dev_list_entry != NULL;
110 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) {
111 const char *syspath = udev_list_entry_get_name(dev_list_entry);
112 struct udev_device *dev =
113 udev_device_new_from_syspath(udev, syspath);
114 const char *sysname = udev_device_get_sysname(dev);
115 struct udev_device *parent =
116 udev_device_get_parent_with_subsystem_devtype(dev, "usb",
120 sr_err("Unable to find parent usb device for %s",
125 const char *idVendor =
126 udev_device_get_sysattr_value(parent, "idVendor");
127 const char *idProduct =
128 udev_device_get_sysattr_value(parent, "idProduct");
129 if (strcmp(USB_VENDOR, idVendor)
130 || strcmp(USB_PRODUCT, idProduct))
133 const char *iSerial =
134 udev_device_get_sysattr_value(parent, "serial");
135 const char *iProduct =
136 udev_device_get_sysattr_value(parent, "product");
139 snprintf(path, sizeof(path), "/dev/%s", sysname);
142 size_t s = strcspn(iProduct, " ");
144 char manufacturer[32];
145 if (s > sizeof(product) ||
146 strlen(iProduct) - s > sizeof(manufacturer)) {
147 sr_err("Could not parse iProduct: %s.", iProduct);
150 strncpy(product, iProduct, s);
152 strcpy(manufacturer, iProduct + s + 1);
154 //Create the device context and set its params
155 struct dev_context *devc;
156 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
157 sr_err("Device context malloc failed.");
161 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
162 sr_err("Invalid iSerial: %s.", iSerial);
168 sprintf(hwrev, "r%d", devc->hwrev);
170 devc->protocol_trigger.spimode = 0;
171 for (i = 0; i < 4; i++) {
172 devc->protocol_trigger.word[i] = 0;
173 devc->protocol_trigger.mask[i] = 0xff;
176 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm))) {
181 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
182 manufacturer, product, hwrev);
185 sr_err("Unable to create device instance for %s",
187 sr_dev_inst_free(sdi);
195 for (i = 0; i < NUM_PROBES; i++) {
196 struct sr_probe *probe;
197 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
198 mso19_probe_names[i])))
200 sdi->probes = g_slist_append(sdi->probes, probe);
204 struct drv_context *drvc = di->priv;
205 drvc->instances = g_slist_append(drvc->instances, sdi);
206 devices = g_slist_append(devices, sdi);
212 static GSList *hw_dev_list(void)
214 struct drv_context *drvc;
218 return drvc->instances;
221 static int hw_dev_open(struct sr_dev_inst *sdi)
224 struct dev_context *devc;
228 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
231 sdi->status = SR_ST_ACTIVE;
233 /* FIXME: discard serial buffer */
234 mso_check_trigger(devc->serial, &devc->trigger_state);
235 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
237 ret = mso_reset_adc(sdi);
241 mso_check_trigger(devc->serial, &devc->trigger_state);
242 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
244 // ret = mso_reset_fsm(sdi);
252 static int hw_dev_close(struct sr_dev_inst *sdi)
254 struct dev_context *devc;
258 if (devc->serial && devc->serial->fd != -1) {
259 serial_close(devc->serial);
260 sdi->status = SR_ST_INACTIVE;
266 static int hw_cleanup(void)
269 struct sr_dev_inst *sdi;
270 struct drv_context *drvc;
271 struct dev_context *devc;
274 if (!(drvc = di->priv))
277 /* Properly close and free all devices. */
278 for (l = drvc->instances; l; l = l->next) {
279 if (!(sdi = l->data)) {
280 /* Log error, but continue cleaning up the rest. */
281 sr_err("%s: sdi was NULL, continuing", __func__);
285 if (!(devc = sdi->priv)) {
286 /* Log error, but continue cleaning up the rest. */
287 sr_err("%s: sdi->priv was NULL, continuing", __func__);
292 sr_serial_dev_inst_free(devc->serial);
293 sr_dev_inst_free(sdi);
295 g_slist_free(drvc->instances);
296 drvc->instances = NULL;
301 static int hw_info_get(int info_id, const void **data,
302 const struct sr_dev_inst *sdi)
304 struct dev_context *devc;
310 case SR_DI_SAMPLERATES:
311 *data = &samplerates;
313 case SR_DI_TRIGGER_TYPES:
314 *data = (char *)TRIGGER_TYPES;
316 case SR_DI_CUR_SAMPLERATE:
319 *data = &devc->cur_rate;
330 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
334 struct dev_context *devc;
335 uint64_t num_samples, slope;
341 if (sdi->status != SR_ST_ACTIVE)
345 case SR_HWCAP_SAMPLERATE:
347 return mso_configure_rate(sdi, *(const uint64_t *)value);
350 case SR_HWCAP_LIMIT_SAMPLES:
351 num_samples = *(uint64_t *)value;
352 if (num_samples < 1024) {
353 sr_err("minimum of 1024 samples required");
356 devc->limit_samples = num_samples;
357 sr_dbg("setting limit_samples to %i\n",
362 case SR_HWCAP_CAPTURE_RATIO:
365 case SR_HWCAP_TRIGGER_SLOPE:
366 slope = *(uint64_t *)value;
367 if (slope != SLOPE_NEGATIVE && slope != SLOPE_POSITIVE) {
368 sr_err("Invalid trigger slope");
371 devc->trigger_slope = slope;
375 case SR_HWCAP_HORIZ_TRIGGERPOS:
376 pos = *(float *)value;
377 if (pos < 0 || pos > 255) {
378 sr_err("Trigger position (%f) should be between 0 and 255.", pos);
381 trigger_pos = (int)pos;
382 devc->trigger_holdoff[0] = trigger_pos & 0xff;
397 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
400 struct sr_datafeed_packet *packet;
401 struct sr_datafeed_header *header;
402 struct dev_context *devc;
407 if (sdi->status != SR_ST_ACTIVE)
410 if (mso_configure_probes(sdi) != SR_OK) {
411 sr_err("Failed to configure probes.");
415 /* FIXME: No need to do full reconfigure every time */
416 // ret = mso_reset_fsm(sdi);
420 /* FIXME: ACDC Mode */
421 devc->ctlbase1 &= 0x7f;
422 // devc->ctlbase1 |= devc->acdcmode;
424 ret = mso_configure_rate(sdi, devc->cur_rate);
429 ret = mso_dac_out(sdi, devc->dac_offset);
433 ret = mso_configure_threshold_level(sdi);
437 ret = mso_configure_trigger(sdi);
441 /* END of config hardware part */
446 /* Start acquisition on the device. */
447 mso_check_trigger(devc->serial, &devc->trigger_state);
448 ret = mso_check_trigger(devc->serial, NULL);
452 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
454 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
455 sr_err("Datafeed packet malloc failed.");
456 return SR_ERR_MALLOC;
459 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
460 sr_err("Datafeed header malloc failed.");
462 return SR_ERR_MALLOC;
465 packet->type = SR_DF_HEADER;
466 packet->payload = (unsigned char *)header;
467 header->feed_version = 1;
468 gettimeofday(&header->starttime, NULL);
469 sr_session_send(cb_data, packet);
477 /* This stops acquisition on ALL devices, ignoring dev_index. */
478 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
482 stop_acquisition(sdi);
487 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
488 .name = "link-mso19",
489 .longname = "Link Instruments MSO-19",
492 .cleanup = hw_cleanup,
494 .dev_list = hw_dev_list,
495 .dev_clear = hw_cleanup,
496 .dev_open = hw_dev_open,
497 .dev_close = hw_dev_close,
498 .info_get = hw_info_get,
499 .dev_config_set = hw_dev_config_set,
500 .dev_acquisition_start = hw_dev_acquisition_start,
501 .dev_acquisition_stop = hw_dev_acquisition_stop,