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