]> sigrok.org Git - libsigrok.git/blame - src/hardware/victor-dmm/api.c
Remove unnecessary std_init() wrapper functions
[libsigrok.git] / src / hardware / victor-dmm / api.c
CommitLineData
ac3898d2
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
6ec6c43b 20#include <config.h>
ac3898d2 21#include <glib.h>
7a360375
BV
22#include <libusb.h>
23#include <stdlib.h>
24#include <string.h>
c1aae900 25#include <libsigrok/libsigrok.h>
fa773062
UH
26#include "libsigrok-internal.h"
27#include "protocol.h"
7a360375
BV
28
29#define VICTOR_VID 0x1244
30#define VICTOR_PID 0xd237
31#define VICTOR_VENDOR "Victor"
32#define VICTOR_INTERFACE 0
1a46cc62 33#define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
ac3898d2
BV
34
35SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
695dc859 36static int dev_acquisition_stop(struct sr_dev_inst *sdi);
7a360375 37
55333928
BV
38static const uint32_t drvopts[] = {
39 SR_CONF_MULTIMETER,
40};
1beccaed 41
a0e0bb41 42static const uint32_t scanopts[] = {
ac046ef8
BV
43 SR_CONF_CONN,
44};
45
f254bc4b 46static const uint32_t devopts[] = {
1953564a 47 SR_CONF_CONTINUOUS,
5827f61b
BV
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
49 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
55333928 50 SR_CONF_CONN | SR_CONF_GET,
7a360375
BV
51};
52
4f840ce9 53static GSList *scan(struct sr_dev_driver *di, GSList *options)
ac3898d2
BV
54{
55 struct drv_context *drvc;
7a360375
BV
56 struct dev_context *devc;
57 struct sr_dev_inst *sdi;
7a360375
BV
58 struct libusb_device_descriptor des;
59 libusb_device **devlist;
ac3898d2 60 GSList *devices;
2a8f2d41 61 int i;
66c91e25 62 char connection_id[64];
ac3898d2
BV
63
64 (void)options;
65
41812aca 66 drvc = di->context;
7a360375 67
ac3898d2 68 devices = NULL;
d4abb463 69 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
7a360375 70 for (i = 0; devlist[i]; i++) {
2a8f2d41 71 libusb_get_device_descriptor(devlist[i], &des);
ac3898d2 72
7a360375
BV
73 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
74 continue;
75
66c91e25
SA
76 usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
77
aac29cc1 78 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
79 sdi->status = SR_ST_INACTIVE;
80 sdi->vendor = g_strdup(VICTOR_VENDOR);
7a360375 81 sdi->driver = di;
66c91e25 82 sdi->connection_id = g_strdup(connection_id);
f57d8ffe 83 devc = g_malloc0(sizeof(struct dev_context));
cf9e86bc 84 sr_sw_limits_init(&devc->limits);
7a360375
BV
85 sdi->priv = devc;
86
5e23fcab 87 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
91219afc
UH
88 sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
89 libusb_get_device_address(devlist[i]), NULL);
ac046ef8 90 sdi->inst_type = SR_INST_USB;
7a360375
BV
91
92 drvc->instances = g_slist_append(drvc->instances, sdi);
93 devices = g_slist_append(devices, sdi);
94 }
95 libusb_free_device_list(devlist, 1);
ac3898d2
BV
96
97 return devices;
98}
99
6078d2c9 100static int dev_open(struct sr_dev_inst *sdi)
ac3898d2 101{
4f840ce9 102 struct sr_dev_driver *di = sdi->driver;
41812aca 103 struct drv_context *drvc = di->context;
ac046ef8 104 struct sr_usb_dev_inst *usb;
4af1f68f 105 int ret;
7a360375 106
ac046ef8
BV
107 usb = sdi->conn;
108
4af1f68f
LPC
109 ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
110 if (ret != SR_OK)
111 return ret;
7a360375
BV
112
113 /* The device reports as HID class, so the kernel would have
114 * claimed it. */
ac046ef8
BV
115 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
116 if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
d4928d71
PS
117 sr_err("Failed to detach kernel driver: %s.",
118 libusb_error_name(ret));
7a360375
BV
119 return SR_ERR;
120 }
121 }
122
ac046ef8 123 if ((ret = libusb_claim_interface(usb->devhdl,
7a360375 124 VICTOR_INTERFACE))) {
fa773062 125 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
7a360375
BV
126 return SR_ERR;
127 }
128 sdi->status = SR_ST_ACTIVE;
ac3898d2
BV
129
130 return SR_OK;
131}
132
6078d2c9 133static int dev_close(struct sr_dev_inst *sdi)
ac3898d2 134{
ac046ef8 135 struct sr_usb_dev_inst *usb;
7a360375 136
ac046ef8
BV
137 usb = sdi->conn;
138
139 if (!usb->devhdl)
7a360375
BV
140 /* Nothing to do. */
141 return SR_OK;
142
ac046ef8
BV
143 libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
144 libusb_close(usb->devhdl);
145 usb->devhdl = NULL;
7a360375 146 sdi->status = SR_ST_INACTIVE;
ac3898d2
BV
147
148 return SR_OK;
149}
150
584560f1 151static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 152 const struct sr_channel_group *cg)
ac046ef8 153{
cf9e86bc 154 struct dev_context *devc = sdi->priv;
ac046ef8
BV
155 struct sr_usb_dev_inst *usb;
156 char str[128];
157
53b4680f 158 (void)cg;
8f996b89 159
584560f1 160 switch (key) {
ac046ef8
BV
161 case SR_CONF_CONN:
162 if (!sdi || !sdi->conn)
163 return SR_ERR_ARG;
164 usb = sdi->conn;
165 snprintf(str, 128, "%d.%d", usb->bus, usb->address);
166 *data = g_variant_new_string(str);
167 break;
cf9e86bc
LPC
168 case SR_CONF_LIMIT_SAMPLES:
169 case SR_CONF_LIMIT_MSEC:
170 return sr_sw_limits_config_get(&devc->limits, key, data);
ac046ef8
BV
171 default:
172 return SR_ERR_NA;
173 }
174
175 return SR_OK;
176}
177
584560f1 178static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 179 const struct sr_channel_group *cg)
ac3898d2 180{
7a360375 181 struct dev_context *devc;
ac3898d2 182
53b4680f 183 (void)cg;
8f996b89 184
e73ffd42
BV
185 if (sdi->status != SR_ST_ACTIVE)
186 return SR_ERR_DEV_CLOSED;
187
7a360375 188 devc = sdi->priv;
dcd438ee 189
cf9e86bc 190 return sr_sw_limits_config_set(&devc->limits, key, data);
ac3898d2
BV
191}
192
584560f1 193static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 194 const struct sr_channel_group *cg)
a1c743fc 195{
a1c743fc 196 (void)sdi;
53b4680f 197 (void)cg;
a1c743fc
BV
198
199 switch (key) {
ac046ef8 200 case SR_CONF_SCAN_OPTIONS:
584560f1 201 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 202 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
ac046ef8 203 break;
9a6517d1 204 case SR_CONF_DEVICE_OPTIONS:
55333928
BV
205 if (!sdi)
206 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
207 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
208 else
209 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
210 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
9a6517d1 211 break;
a1c743fc 212 default:
bd6fbf62 213 return SR_ERR_NA;
a1c743fc
BV
214 }
215
216 return SR_OK;
217}
218
55462b8b 219static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
7a360375
BV
220{
221 struct dev_context *devc;
222 struct sr_dev_inst *sdi;
223 int ret;
224
225 sdi = transfer->user_data;
226 devc = sdi->priv;
227 if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
228 /* USB device was unplugged. */
695dc859 229 dev_acquisition_stop(sdi);
7a360375 230 } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
fa773062 231 sr_dbg("Got %d-byte packet.", transfer->actual_length);
ff945683
BV
232 if (transfer->actual_length == DMM_DATA_SIZE) {
233 victor_dmm_receive_data(sdi, transfer->buffer);
cf9e86bc
LPC
234 if (sr_sw_limits_check(&devc->limits))
235 dev_acquisition_stop(sdi);
7a360375
BV
236 }
237 }
238 /* Anything else is either an error or a timeout, which is fine:
239 * we were just going to send another transfer request anyway. */
240
241 if (sdi->status == SR_ST_ACTIVE) {
242 /* Send the same request again. */
243 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062
UH
244 sr_err("Unable to resubmit transfer: %s.",
245 libusb_error_name(ret));
7a360375 246 g_free(transfer->buffer);
9ec7ff94 247 libusb_free_transfer(transfer);
695dc859 248 dev_acquisition_stop(sdi);
7a360375
BV
249 }
250 } else {
251 /* This was the last transfer we're going to receive, so
252 * clean up now. */
7a360375 253 g_free(transfer->buffer);
9ec7ff94 254 libusb_free_transfer(transfer);
7a360375 255 }
7a360375
BV
256}
257
258static int handle_events(int fd, int revents, void *cb_data)
259{
260 struct dev_context *devc;
4f840ce9 261 struct drv_context *drvc;
7a360375 262 struct sr_dev_inst *sdi;
4f840ce9 263 struct sr_dev_driver *di;
7a360375 264 struct timeval tv;
7a360375
BV
265
266 (void)fd;
267 (void)revents;
268
269 sdi = cb_data;
270 devc = sdi->priv;
4f840ce9 271 di = sdi->driver;
41812aca 272 drvc = di->context;
7a360375 273
cf9e86bc
LPC
274 if (sr_sw_limits_check(&devc->limits))
275 dev_acquisition_stop(sdi);
7a360375
BV
276
277 if (sdi->status == SR_ST_STOPPING) {
102f1239 278 usb_source_remove(sdi->session, drvc->sr_ctx);
6078d2c9 279 dev_close(sdi);
3be42bc2 280 std_session_send_df_end(sdi, LOG_PREFIX);
7a360375
BV
281 }
282
283 memset(&tv, 0, sizeof(struct timeval));
d4abb463
PS
284 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
285 NULL);
7a360375
BV
286
287 return TRUE;
288}
289
695dc859 290static int dev_acquisition_start(const struct sr_dev_inst *sdi)
ac3898d2 291{
4f840ce9 292 struct sr_dev_driver *di = sdi->driver;
41812aca 293 struct drv_context *drvc = di->context;
ac046ef8 294 struct sr_usb_dev_inst *usb;
7a360375 295 struct libusb_transfer *transfer;
6c60facc 296 int ret;
7a360375
BV
297 unsigned char *buf;
298
e73ffd42
BV
299 if (sdi->status != SR_ST_ACTIVE)
300 return SR_ERR_DEV_CLOSED;
301
ac046ef8 302 usb = sdi->conn;
7a360375 303
695dc859 304 std_session_send_df_header(sdi, LOG_PREFIX);
7a360375 305
102f1239
BV
306 usb_source_add(sdi->session, drvc->sr_ctx, 100,
307 handle_events, (void *)sdi);
7a360375 308
a95f142e 309 buf = g_malloc(DMM_DATA_SIZE);
7a360375
BV
310 transfer = libusb_alloc_transfer(0);
311 /* Each transfer request gets 100ms to arrive before it's restarted.
312 * The device only sends 1 transfer/second no matter how many
313 * times you ask, but we want to keep step with the USB events
314 * handling above. */
ac046ef8 315 libusb_fill_interrupt_transfer(transfer, usb->devhdl,
fa773062 316 VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
695dc859 317 (struct sr_dev_inst *)sdi, 100);
7a360375 318 if ((ret = libusb_submit_transfer(transfer) != 0)) {
fa773062 319 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
7a360375
BV
320 libusb_free_transfer(transfer);
321 g_free(buf);
322 return SR_ERR;
323 }
ac3898d2
BV
324
325 return SR_OK;
326}
327
695dc859 328static int dev_acquisition_stop(struct sr_dev_inst *sdi)
ac3898d2 329{
ac3898d2 330 if (sdi->status != SR_ST_ACTIVE) {
7a360375 331 sr_err("Device not active, can't stop acquisition.");
ac3898d2
BV
332 return SR_ERR;
333 }
334
7a360375 335 sdi->status = SR_ST_STOPPING;
ac3898d2
BV
336
337 return SR_OK;
338}
339
340SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
341 .name = "victor-dmm",
7a360375 342 .longname = "Victor DMMs",
ac3898d2 343 .api_version = 1,
c2fdcc25 344 .init = std_init,
700d6b64 345 .cleanup = std_cleanup,
6078d2c9 346 .scan = scan,
c01bf34c 347 .dev_list = std_dev_list,
a6630742 348 .dev_clear = NULL,
ac046ef8 349 .config_get = config_get,
035a1078 350 .config_set = config_set,
a1c743fc 351 .config_list = config_list,
6078d2c9
UH
352 .dev_open = dev_open,
353 .dev_close = dev_close,
354 .dev_acquisition_start = dev_acquisition_start,
355 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 356 .context = NULL,
ac3898d2 357};