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