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 #define UNI_T_UT_D04_NEW "1a86.e008"
29 static const int hwopts[] = {
34 static const int hwcaps[] = {
36 SR_HWCAP_LIMIT_SAMPLES,
42 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
43 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
45 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
46 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
48 /* After hw_init() this will point to a device-specific entry (see above). */
49 static struct sr_dev_driver *di = NULL;
51 static int clear_instances(void)
53 /* TODO: Use common code later. */
58 static int hw_init(struct sr_context *sr_ctx, int dmm)
60 struct drv_context *drvc;
62 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
63 sr_err("Driver context malloc failed.");
67 if (dmm == UNI_T_UT61D)
69 else if (dmm == VOLTCRAFT_VC820)
71 sr_dbg("Selected '%s' subdriver.", di->name);
73 drvc->sr_ctx = sr_ctx;
79 static int hw_init_ut61d(struct sr_context *sr_ctx)
81 return hw_init(sr_ctx, UNI_T_UT61D);
84 static int hw_init_vc820(struct sr_context *sr_ctx)
86 return hw_init(sr_ctx, VOLTCRAFT_VC820);
89 static GSList *hw_scan(GSList *options)
91 GSList *usb_devices, *devices, *l;
92 struct sr_dev_inst *sdi;
93 struct dev_context *devc;
94 struct drv_context *drvc;
95 struct sr_usb_dev_inst *usb;
97 struct sr_probe *probe;
104 /* USB scan is always authoritative. */
108 for (l = options; l; l = l->next) {
110 switch (opt->hwopt) {
117 conn = UNI_T_UT_D04_NEW;
120 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
121 g_slist_free_full(usb_devices, g_free);
125 for (l = usb_devices; l; l = l->next) {
128 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
129 sr_err("Device context malloc failed.");
133 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
134 di->longname, NULL, NULL))) {
135 sr_err("sr_dev_inst_new returned NULL.");
140 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
142 sdi->probes = g_slist_append(sdi->probes, probe);
146 drvc->instances = g_slist_append(drvc->instances, sdi);
147 devices = g_slist_append(devices, sdi);
153 static GSList *hw_dev_list(void)
155 struct drv_context *drvc;
159 return drvc->instances;
162 static int hw_dev_open(struct sr_dev_inst *sdi)
164 struct drv_context *drvc;
165 struct dev_context *devc;
170 return sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb);
173 static int hw_dev_close(struct sr_dev_inst *sdi)
182 static int hw_cleanup(void)
189 static int hw_info_get(int info_id, const void **data,
190 const struct sr_dev_inst *sdi)
194 sr_spew("Backend requested info_id %d.", info_id);
202 sr_spew("%s: Returning hwcaps.", __func__);
204 case SR_DI_SAMPLERATES:
205 /* TODO: Get rid of this. */
207 sr_spew("%s: Returning samplerates.", __func__);
210 case SR_DI_CUR_SAMPLERATE:
211 /* TODO: Get rid of this. */
213 sr_spew("%s: Returning current samplerate.", __func__);
224 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
227 struct dev_context *devc;
232 case SR_HWCAP_LIMIT_MSEC:
233 /* TODO: Not yet implemented. */
234 if (*(const uint64_t *)value == 0) {
235 sr_err("Time limit cannot be 0.");
238 devc->limit_msec = *(const uint64_t *)value;
239 sr_dbg("Setting time limit to %" PRIu64 "ms.",
242 case SR_HWCAP_LIMIT_SAMPLES:
243 if (*(const uint64_t *)value == 0) {
244 sr_err("Sample limit cannot be 0.");
247 devc->limit_samples = *(const uint64_t *)value;
248 sr_dbg("Setting sample limit to %" PRIu64 ".",
249 devc->limit_samples);
252 sr_err("Unknown capability: %d.", hwcap);
260 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
263 struct sr_datafeed_packet packet;
264 struct sr_datafeed_header header;
265 struct sr_datafeed_meta_analog meta;
266 struct dev_context *devc;
270 sr_dbg("Starting acquisition.");
272 devc->cb_data = cb_data;
274 /* Send header packet to the session bus. */
275 sr_dbg("Sending SR_DF_HEADER.");
276 packet.type = SR_DF_HEADER;
277 packet.payload = (uint8_t *)&header;
278 header.feed_version = 1;
279 gettimeofday(&header.starttime, NULL);
280 sr_session_send(devc->cb_data, &packet);
282 /* Send metadata about the SR_DF_ANALOG packets to come. */
283 sr_dbg("Sending SR_DF_META_ANALOG.");
284 packet.type = SR_DF_META_ANALOG;
285 packet.payload = &meta;
287 sr_session_send(devc->cb_data, &packet);
289 if (!strcmp(di->name, "uni-t-ut61d")) {
290 sr_source_add(0, 0, 10 /* poll_timeout */,
291 uni_t_ut61d_receive_data, (void *)sdi);
292 } else if (!strcmp(di->name, "voltcraft-vc820")) {
293 sr_source_add(0, 0, 10 /* poll_timeout */,
294 voltcraft_vc820_receive_data, (void *)sdi);
300 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
302 struct sr_datafeed_packet packet;
306 sr_dbg("Stopping acquisition.");
308 /* Send end packet to the session bus. */
309 sr_dbg("Sending SR_DF_END.");
310 packet.type = SR_DF_END;
311 sr_session_send(cb_data, &packet);
319 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
320 .name = "uni-t-ut61d",
321 .longname = "UNI-T UT61D",
323 .init = hw_init_ut61d,
324 .cleanup = hw_cleanup,
326 .dev_list = hw_dev_list,
327 .dev_clear = clear_instances,
328 .dev_open = hw_dev_open,
329 .dev_close = hw_dev_close,
330 .info_get = hw_info_get,
331 .dev_config_set = hw_dev_config_set,
332 .dev_acquisition_start = hw_dev_acquisition_start,
333 .dev_acquisition_stop = hw_dev_acquisition_stop,
337 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
338 .name = "voltcraft-vc820",
339 .longname = "Voltcraft VC-820",
341 .init = hw_init_vc820,
342 .cleanup = hw_cleanup,
344 .dev_list = hw_dev_list,
345 .dev_clear = clear_instances,
346 .dev_open = hw_dev_open,
347 .dev_close = hw_dev_close,
348 .info_get = hw_info_get,
349 .dev_config_set = hw_dev_config_set,
350 .dev_acquisition_start = hw_dev_acquisition_start,
351 .dev_acquisition_stop = hw_dev_acquisition_stop,