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