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