2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
27 static const int hwcaps[] = {
29 SR_HWCAP_LIMIT_SAMPLES,
35 static const char *probe_names[] = {
40 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
41 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
43 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
44 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
46 /* After hw_init() this will point to a device-specific entry (see above). */
47 static struct sr_dev_driver *di = NULL;
49 static int clear_instances(void)
51 /* TODO: Use common code later. */
56 static int hw_init(struct sr_context *sr_ctx, int dmm)
59 struct drv_context *drvc;
61 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
62 sr_err("Driver context malloc failed.");
66 if ((ret = libusb_init(NULL)) < 0) {
67 sr_err("Failed to initialize libusb: %s.",
68 libusb_error_name(ret));
72 if (dmm == UNI_T_UT61D)
74 else if (dmm == VOLTCRAFT_VC820)
76 sr_dbg("Selected '%s' subdriver.", di->name);
78 drvc->sr_ctx = sr_ctx;
84 static int hw_init_ut61d(struct sr_context *sr_ctx)
86 return hw_init(sr_ctx, UNI_T_UT61D);
89 static int hw_init_vc820(struct sr_context *sr_ctx)
91 return hw_init(sr_ctx, VOLTCRAFT_VC820);
94 static GSList *hw_scan(GSList *options)
97 struct sr_dev_inst *sdi;
98 struct dev_context *devc;
99 struct drv_context *drvc;
100 struct sr_probe *probe;
101 struct libusb_device *ldev;
110 if (!(l = sr_usb_connect(NULL, "1a86.e008")))
113 for (i = 0; i < (int)g_slist_length(l); i++) {
114 ldev = (struct libusb_device *)l->data;
116 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
117 sr_err("Device context malloc failed.");
121 devcnt = g_slist_length(drvc->instances);
122 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
123 di->longname, NULL, NULL))) {
124 sr_err("sr_dev_inst_new returned NULL.");
129 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
131 sdi->probes = g_slist_append(sdi->probes, probe);
132 devc->usb = sr_usb_dev_inst_new(
133 libusb_get_bus_number(ldev),
134 libusb_get_device_address(ldev), NULL);
135 drvc->instances = g_slist_append(drvc->instances, l->data);
136 devices = g_slist_append(devices, sdi);
142 static GSList *hw_dev_list(void)
144 struct drv_context *drvc;
148 return drvc->instances;
151 static int hw_dev_open(struct sr_dev_inst *sdi)
153 struct dev_context *devc;
157 return sr_usb_open(NULL, devc->usb);
160 static int hw_dev_close(struct sr_dev_inst *sdi)
169 static int hw_cleanup(void)
173 // libusb_exit(NULL);
178 static int hw_info_get(int info_id, const void **data,
179 const struct sr_dev_inst *sdi)
183 sr_spew("Backend requested info_id %d.", info_id);
188 sr_spew("%s: Returning hwcaps.", __func__);
190 case SR_DI_NUM_PROBES:
191 *data = GINT_TO_POINTER(1);
192 sr_spew("%s: Returning number of probes.", __func__);
194 case SR_DI_PROBE_NAMES:
196 sr_spew("%s: Returning probe names.", __func__);
198 case SR_DI_SAMPLERATES:
199 /* TODO: Get rid of this. */
201 sr_spew("%s: Returning samplerates.", __func__);
204 case SR_DI_CUR_SAMPLERATE:
205 /* TODO: Get rid of this. */
207 sr_spew("%s: Returning current samplerate.", __func__);
211 sr_err("%s: Unknown info_id %d.", __func__, info_id);
219 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
222 struct dev_context *devc;
227 case SR_HWCAP_LIMIT_MSEC:
228 /* TODO: Not yet implemented. */
229 if (*(const uint64_t *)value == 0) {
230 sr_err("Time limit cannot be 0.");
233 devc->limit_msec = *(const uint64_t *)value;
234 sr_dbg("Setting time limit to %" PRIu64 "ms.",
237 case SR_HWCAP_LIMIT_SAMPLES:
238 if (*(const uint64_t *)value == 0) {
239 sr_err("Sample limit cannot be 0.");
242 devc->limit_samples = *(const uint64_t *)value;
243 sr_dbg("Setting sample limit to %" PRIu64 ".",
244 devc->limit_samples);
247 sr_err("Unknown capability: %d.", hwcap);
255 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
258 struct sr_datafeed_packet packet;
259 struct sr_datafeed_header header;
260 struct sr_datafeed_meta_analog meta;
261 struct dev_context *devc;
265 sr_dbg("Starting acquisition.");
267 devc->cb_data = cb_data;
269 /* Send header packet to the session bus. */
270 sr_dbg("Sending SR_DF_HEADER.");
271 packet.type = SR_DF_HEADER;
272 packet.payload = (uint8_t *)&header;
273 header.feed_version = 1;
274 gettimeofday(&header.starttime, NULL);
275 sr_session_send(devc->cb_data, &packet);
277 /* Send metadata about the SR_DF_ANALOG packets to come. */
278 sr_dbg("Sending SR_DF_META_ANALOG.");
279 packet.type = SR_DF_META_ANALOG;
280 packet.payload = &meta;
282 sr_session_send(devc->cb_data, &packet);
284 if (!strcmp(di->name, "uni-t-ut61d")) {
285 sr_source_add(0, 0, 10 /* poll_timeout */,
286 uni_t_ut61d_receive_data, (void *)sdi);
287 } else if (!strcmp(di->name, "voltcraft-vc820")) {
288 sr_source_add(0, 0, 10 /* poll_timeout */,
289 voltcraft_vc820_receive_data, (void *)sdi);
295 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
297 struct sr_datafeed_packet packet;
301 sr_dbg("Stopping acquisition.");
303 /* Send end packet to the session bus. */
304 sr_dbg("Sending SR_DF_END.");
305 packet.type = SR_DF_END;
306 sr_session_send(cb_data, &packet);
314 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
315 .name = "uni-t-ut61d",
316 .longname = "UNI-T UT61D",
318 .init = hw_init_ut61d,
319 .cleanup = hw_cleanup,
321 .dev_list = hw_dev_list,
322 .dev_clear = clear_instances,
323 .dev_open = hw_dev_open,
324 .dev_close = hw_dev_close,
325 .info_get = hw_info_get,
326 .dev_config_set = hw_dev_config_set,
327 .dev_acquisition_start = hw_dev_acquisition_start,
328 .dev_acquisition_stop = hw_dev_acquisition_stop,
332 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
333 .name = "voltcraft-vc820",
334 .longname = "Voltcraft VC-820",
336 .init = hw_init_vc820,
337 .cleanup = hw_cleanup,
339 .dev_list = hw_dev_list,
340 .dev_clear = clear_instances,
341 .dev_open = hw_dev_open,
342 .dev_close = hw_dev_close,
343 .info_get = hw_info_get,
344 .dev_config_set = hw_dev_config_set,
345 .dev_acquisition_start = hw_dev_acquisition_start,
346 .dev_acquisition_stop = hw_dev_acquisition_stop,