]> sigrok.org Git - libsigrok.git/blame - hardware/uni-t-dmm/api.c
genericdmm: Factor out USB functions.
[libsigrok.git] / hardware / uni-t-dmm / api.c
CommitLineData
79081ec8
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25#include "protocol.h"
26
27static const int hwcaps[] = {
28 SR_HWCAP_MULTIMETER,
29 SR_HWCAP_LIMIT_SAMPLES,
30 SR_HWCAP_LIMIT_MSEC,
31 SR_HWCAP_CONTINUOUS,
32 0,
33};
34
35static const char *probe_names[] = {
36 "Probe",
37 NULL,
38};
39
fdbcb86d 40SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
fdbcb86d 41SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
6ac5f892
UH
42
43static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
fdbcb86d 44static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
79081ec8 45
6ac5f892
UH
46/* After hw_init() this will point to a device-specific entry (see above). */
47static struct sr_dev_driver *di = NULL;
48
79081ec8
UH
49static int open_usb(struct sr_dev_inst *sdi)
50{
51 libusb_device **devlist;
52 struct libusb_device_descriptor des;
53 struct dev_context *devc;
54 int ret, tmp, cnt, i;
55
56 /* TODO: Use common code later, refactor. */
57
58 devc = sdi->priv;
59
60 if ((cnt = libusb_get_device_list(NULL, &devlist)) < 0) {
61 sr_err("Error getting USB device list: %d.", cnt);
62 return SR_ERR;
63 }
64
65 ret = SR_ERR;
66 for (i = 0; i < cnt; i++) {
67 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
68 sr_err("Failed to get device descriptor: %d.", tmp);
69 continue;
70 }
71
72 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
73 || libusb_get_device_address(devlist[i]) != devc->usb->address)
74 continue;
75
76 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
77 sr_err("Failed to open device: %d.", tmp);
78 break;
79 }
80
81 sr_info("Opened USB device on %d.%d.",
82 devc->usb->bus, devc->usb->address);
83 ret = SR_OK;
84 break;
85 }
86 libusb_free_device_list(devlist, 1);
87
88 return ret;
89}
90
6ac5f892 91static GSList *connect_usb(const char *conn)
79081ec8
UH
92{
93 struct sr_dev_inst *sdi;
94 struct drv_context *drvc;
95 struct dev_context *devc;
96 struct sr_probe *probe;
97 libusb_device **devlist;
98 struct libusb_device_descriptor des;
99 GSList *devices;
100 int vid, pid, devcnt, err, i;
101
102 (void)conn;
103
104 /* TODO: Use common code later, refactor. */
105
6ac5f892 106 drvc = di->priv;
79081ec8
UH
107
108 /* Hardcoded for now. */
109 vid = UT_D04_CABLE_USB_VID;
110 pid = UT_D04_CABLE_USB_DID;
111
112 devices = NULL;
113 libusb_get_device_list(NULL, &devlist);
114 for (i = 0; devlist[i]; i++) {
115 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
116 sr_err("Failed to get device descriptor: %d", err);
117 continue;
118 }
119
120 if (des.idVendor != vid || des.idProduct != pid)
121 continue;
122
123 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
124 sr_err("Device context malloc failed.");
125 return NULL;
126 }
127
128 devcnt = g_slist_length(drvc->instances);
129 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
6ac5f892 130 di->longname, NULL, NULL))) {
79081ec8
UH
131 sr_err("sr_dev_inst_new returned NULL.");
132 return NULL;
133 }
134 sdi->priv = devc;
135 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
136 return NULL;
137 sdi->probes = g_slist_append(sdi->probes, probe);
138 devc->usb = sr_usb_dev_inst_new(
139 libusb_get_bus_number(devlist[i]),
140 libusb_get_device_address(devlist[i]), NULL);
141 devices = g_slist_append(devices, sdi);
142 }
143 libusb_free_device_list(devlist, 1);
144
145 return devices;
146}
147
148static int clear_instances(void)
149{
150 /* TODO: Use common code later. */
151
152 return SR_OK;
153}
154
fdbcb86d 155static int hw_init(int dmm)
79081ec8
UH
156{
157 int ret;
158 struct drv_context *drvc;
159
160 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
161 sr_err("Driver context malloc failed.");
162 return SR_ERR_MALLOC;
163 }
164
165 if ((ret = libusb_init(NULL)) < 0) {
166 sr_err("Failed to initialize libusb: %s.",
167 libusb_error_name(ret));
168 return SR_ERR;
169 }
170
fdbcb86d 171 if (dmm == UNI_T_UT61D)
6ac5f892 172 di = di_ut61d;
fdbcb86d 173 else if (dmm == VOLTCRAFT_VC820)
6ac5f892 174 di = di_vc820;
8c1adf37 175 sr_dbg("Selected '%s' subdriver.", di->name);
6ac5f892
UH
176
177 di->priv = drvc;
79081ec8
UH
178
179 return SR_OK;
180}
181
fdbcb86d
UH
182static int hw_init_ut61d(void)
183{
184 return hw_init(UNI_T_UT61D);
185}
186
187static int hw_init_vc820(void)
188{
189 return hw_init(VOLTCRAFT_VC820);
190}
191
6ac5f892 192static GSList *hw_scan(GSList *options)
79081ec8
UH
193{
194 GSList *l, *devices;
195 struct sr_dev_inst *sdi;
196 struct drv_context *drvc;
197
198 (void)options;
199
6ac5f892 200 drvc = di->priv;
79081ec8 201
6ac5f892 202 if (!(devices = connect_usb(NULL)))
79081ec8
UH
203 return NULL;
204
205 for (l = devices; l; l = l->next) {
206 sdi = l->data;
6ac5f892 207 sdi->driver = di;
79081ec8
UH
208 drvc->instances = g_slist_append(drvc->instances, l->data);
209 }
210
211 return devices;
212}
213
6ac5f892 214static GSList *hw_dev_list(void)
79081ec8
UH
215{
216 struct drv_context *drvc;
217
6ac5f892 218 drvc = di->priv;
79081ec8
UH
219
220 return drvc->instances;
221}
222
223static int hw_dev_open(struct sr_dev_inst *sdi)
224{
225 return open_usb(sdi);
226}
227
228static int hw_dev_close(struct sr_dev_inst *sdi)
229{
230 (void)sdi;
231
232 /* TODO */
233
234 return SR_OK;
235}
236
237static int hw_cleanup(void)
238{
239 clear_instances();
240
241 // libusb_exit(NULL);
242
243 return SR_OK;
244}
245
246static int hw_info_get(int info_id, const void **data,
247 const struct sr_dev_inst *sdi)
248{
249 (void)sdi;
250
251 sr_spew("Backend requested info_id %d.", info_id);
252
253 switch (info_id) {
254 case SR_DI_HWCAPS:
255 *data = hwcaps;
256 sr_spew("%s: Returning hwcaps.", __func__);
257 break;
258 case SR_DI_NUM_PROBES:
259 *data = GINT_TO_POINTER(1);
260 sr_spew("%s: Returning number of probes.", __func__);
261 break;
262 case SR_DI_PROBE_NAMES:
263 *data = probe_names;
264 sr_spew("%s: Returning probe names.", __func__);
265 break;
266 case SR_DI_SAMPLERATES:
267 /* TODO: Get rid of this. */
268 *data = NULL;
269 sr_spew("%s: Returning samplerates.", __func__);
270 return SR_ERR_ARG;
271 break;
272 case SR_DI_CUR_SAMPLERATE:
273 /* TODO: Get rid of this. */
274 *data = NULL;
275 sr_spew("%s: Returning current samplerate.", __func__);
276 return SR_ERR_ARG;
277 break;
278 default:
279 sr_err("%s: Unknown info_id %d.", __func__, info_id);
280 return SR_ERR_ARG;
281 break;
282 }
283
284 return SR_OK;
285}
286
287static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
288 const void *value)
289{
290 struct dev_context *devc;
291
292 devc = sdi->priv;
293
294 switch (hwcap) {
295 case SR_HWCAP_LIMIT_MSEC:
296 /* TODO: Not yet implemented. */
297 if (*(const uint64_t *)value == 0) {
298 sr_err("Time limit cannot be 0.");
299 return SR_ERR;
300 }
301 devc->limit_msec = *(const uint64_t *)value;
302 sr_dbg("Setting time limit to %" PRIu64 "ms.",
303 devc->limit_msec);
304 break;
305 case SR_HWCAP_LIMIT_SAMPLES:
306 if (*(const uint64_t *)value == 0) {
307 sr_err("Sample limit cannot be 0.");
308 return SR_ERR;
309 }
310 devc->limit_samples = *(const uint64_t *)value;
311 sr_dbg("Setting sample limit to %" PRIu64 ".",
312 devc->limit_samples);
313 break;
314 default:
315 sr_err("Unknown capability: %d.", hwcap);
316 return SR_ERR;
317 break;
318 }
319
320 return SR_OK;
321}
322
323static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
6ac5f892 324 void *cb_data)
79081ec8
UH
325{
326 struct sr_datafeed_packet packet;
327 struct sr_datafeed_header header;
328 struct sr_datafeed_meta_analog meta;
329 struct dev_context *devc;
330
331 devc = sdi->priv;
332
333 sr_dbg("Starting acquisition.");
334
335 devc->cb_data = cb_data;
336
337 /* Send header packet to the session bus. */
338 sr_dbg("Sending SR_DF_HEADER.");
339 packet.type = SR_DF_HEADER;
340 packet.payload = (uint8_t *)&header;
341 header.feed_version = 1;
342 gettimeofday(&header.starttime, NULL);
343 sr_session_send(devc->cb_data, &packet);
344
345 /* Send metadata about the SR_DF_ANALOG packets to come. */
346 sr_dbg("Sending SR_DF_META_ANALOG.");
347 packet.type = SR_DF_META_ANALOG;
348 packet.payload = &meta;
349 meta.num_probes = 1;
350 sr_session_send(devc->cb_data, &packet);
351
6ac5f892 352 if (!strcmp(di->name, "uni-t-ut61d")) {
fdbcb86d
UH
353 sr_source_add(0, 0, 10 /* poll_timeout */,
354 uni_t_ut61d_receive_data, (void *)sdi);
6ac5f892 355 } else if (!strcmp(di->name, "voltcraft-vc820")) {
fdbcb86d
UH
356 sr_source_add(0, 0, 10 /* poll_timeout */,
357 voltcraft_vc820_receive_data, (void *)sdi);
358 }
79081ec8
UH
359
360 return SR_OK;
361}
362
363static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
364 void *cb_data)
365{
366 struct sr_datafeed_packet packet;
367
368 (void)sdi;
369
370 sr_dbg("Stopping acquisition.");
371
372 /* Send end packet to the session bus. */
373 sr_dbg("Sending SR_DF_END.");
374 packet.type = SR_DF_END;
375 sr_session_send(cb_data, &packet);
376
377 /* TODO? */
378 sr_source_remove(0);
379
380 return SR_OK;
381}
382
fdbcb86d
UH
383SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
384 .name = "uni-t-ut61d",
385 .longname = "UNI-T UT61D",
386 .api_version = 1,
387 .init = hw_init_ut61d,
388 .cleanup = hw_cleanup,
6ac5f892
UH
389 .scan = hw_scan,
390 .dev_list = hw_dev_list,
fdbcb86d
UH
391 .dev_clear = clear_instances,
392 .dev_open = hw_dev_open,
393 .dev_close = hw_dev_close,
394 .info_get = hw_info_get,
395 .dev_config_set = hw_dev_config_set,
6ac5f892 396 .dev_acquisition_start = hw_dev_acquisition_start,
fdbcb86d
UH
397 .dev_acquisition_stop = hw_dev_acquisition_stop,
398 .priv = NULL,
399};
400
401SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
402 .name = "voltcraft-vc820",
403 .longname = "Voltcraft VC-820",
79081ec8 404 .api_version = 1,
fdbcb86d 405 .init = hw_init_vc820,
79081ec8 406 .cleanup = hw_cleanup,
6ac5f892
UH
407 .scan = hw_scan,
408 .dev_list = hw_dev_list,
79081ec8
UH
409 .dev_clear = clear_instances,
410 .dev_open = hw_dev_open,
411 .dev_close = hw_dev_close,
412 .info_get = hw_info_get,
413 .dev_config_set = hw_dev_config_set,
6ac5f892 414 .dev_acquisition_start = hw_dev_acquisition_start,
79081ec8
UH
415 .dev_acquisition_stop = hw_dev_acquisition_stop,
416 .priv = NULL,
417};