]> sigrok.org Git - libsigrok.git/blame - src/hardware/brymen-bm86x/api.c
Match std_init() parameter order to the driver init() callback
[libsigrok.git] / src / hardware / brymen-bm86x / api.c
CommitLineData
8d9c8554
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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>
8d9c8554
AJ
21#include "protocol.h"
22
ecaa89af
AJ
23#define BRYMEN_BC86X "0820.0001"
24
a0e0bb41 25static const uint32_t scanopts[] = {
ecaa89af
AJ
26 SR_CONF_CONN,
27};
28
f254bc4b 29static const uint32_t devopts[] = {
ecaa89af 30 SR_CONF_MULTIMETER,
ecaa89af 31 SR_CONF_CONTINUOUS,
5827f61b
BV
32 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
33 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
ecaa89af
AJ
34};
35
8d9c8554 36SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info;
8d9c8554 37
4f840ce9 38static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
8d9c8554 39{
1f8f5bc0 40 return std_init(di, sr_ctx, LOG_PREFIX);
8d9c8554
AJ
41}
42
4f840ce9 43static GSList *scan(struct sr_dev_driver *di, GSList *options)
8d9c8554 44{
ecaa89af 45 GSList *usb_devices, *devices, *l;
8d9c8554 46 struct drv_context *drvc;
ecaa89af
AJ
47 struct dev_context *devc;
48 struct sr_dev_inst *sdi;
49 struct sr_usb_dev_inst *usb;
50 struct sr_config *src;
ecaa89af 51 const char *conn;
8d9c8554 52
41812aca 53 drvc = di->context;
8d9c8554
AJ
54 drvc->instances = NULL;
55
ecaa89af
AJ
56 conn = BRYMEN_BC86X;
57 for (l = options; l; l = l->next) {
58 src = l->data;
59 switch (src->key) {
60 case SR_CONF_CONN:
61 conn = g_variant_get_string(src->data, NULL);
62 break;
63 }
64 }
65
66 devices = NULL;
67 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
68 g_slist_free_full(usb_devices, g_free);
69 return NULL;
70 }
71
72 for (l = usb_devices; l; l = l->next) {
73 usb = l->data;
74
aac29cc1 75 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
76 sdi->status = SR_ST_INACTIVE;
77 sdi->vendor = g_strdup("Brymen");
78 sdi->model = g_strdup("BM869");
f57d8ffe 79 devc = g_malloc0(sizeof(struct dev_context));
ecaa89af
AJ
80 sdi->priv = devc;
81 sdi->driver = di;
5e23fcab
ML
82 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
83 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2");
ecaa89af
AJ
84
85 sdi->inst_type = SR_INST_USB;
86 sdi->conn = usb;
87
ab939ebb
LPC
88 sr_sw_limits_init(&devc->sw_limits);
89
ecaa89af
AJ
90 drvc->instances = g_slist_append(drvc->instances, sdi);
91 devices = g_slist_append(devices, sdi);
92 }
8d9c8554
AJ
93
94 return devices;
95}
96
8d9c8554
AJ
97static int dev_open(struct sr_dev_inst *sdi)
98{
4f840ce9 99 struct sr_dev_driver *di = sdi->driver;
41812aca 100 struct drv_context *drvc = di->context;
ecaa89af
AJ
101 struct sr_usb_dev_inst *usb;
102 struct dev_context *devc;
103 int ret;
8d9c8554 104
ecaa89af
AJ
105 usb = sdi->conn;
106 devc = sdi->priv;
107
108 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
109 sdi->status = SR_ST_ACTIVE;
6d930b41
AJ
110 else
111 return SR_ERR;
ecaa89af
AJ
112
113 /* Detach kernel drivers which grabbed this device (if any). */
114 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
115 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
116 if (ret < 0) {
117 sr_err("Failed to detach kernel driver: %s.",
118 libusb_error_name(ret));
119 return SR_ERR;
120 }
121 devc->detached_kernel_driver = 1;
122 sr_dbg("Successfully detached kernel driver.");
123 } else {
124 sr_dbg("No need to detach a kernel driver.");
125 }
8d9c8554 126
ecaa89af
AJ
127 /* Claim interface 0. */
128 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
129 sr_err("Failed to claim interface 0: %s.",
130 libusb_error_name(ret));
131 return SR_ERR;
132 }
133 sr_dbg("Successfully claimed interface 0.");
8d9c8554 134
ecaa89af 135 return ret;
8d9c8554
AJ
136}
137
138static int dev_close(struct sr_dev_inst *sdi)
139{
ecaa89af
AJ
140 struct sr_usb_dev_inst *usb;
141 struct dev_context *devc;
142 int ret;
143
6d930b41
AJ
144 if (sdi->status != SR_ST_ACTIVE)
145 return SR_ERR_DEV_CLOSED;
146
ecaa89af
AJ
147 usb = sdi->conn;
148 devc = sdi->priv;
149
150 if ((ret = libusb_release_interface(usb->devhdl, 0)))
151 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
152 else
153 sr_dbg("Successfully released interface 0.\n");
154
155 if (!ret && devc->detached_kernel_driver) {
156 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) {
157 sr_err("Failed to attach kernel driver: %s.\n",
158 libusb_error_name(ret));
159 } else {
160 devc->detached_kernel_driver = 0;
161 sr_dbg("Successfully attached kernel driver.\n");
162 }
163 }
8d9c8554 164
ecaa89af 165 libusb_close(usb->devhdl);
8d9c8554
AJ
166
167 sdi->status = SR_ST_INACTIVE;
168
ecaa89af 169 return ret;
8d9c8554
AJ
170}
171
584560f1 172static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 173 const struct sr_channel_group *cg)
8d9c8554 174{
ecaa89af 175 struct dev_context *devc = sdi->priv;
8d9c8554 176
53b4680f 177 (void)cg;
8d9c8554 178
ab939ebb 179 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
8d9c8554
AJ
180}
181
584560f1 182static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 183 const struct sr_channel_group *cg)
8d9c8554 184{
ecaa89af 185 struct dev_context *devc;
8d9c8554 186
53b4680f 187 (void)cg;
8d9c8554
AJ
188
189 if (sdi->status != SR_ST_ACTIVE)
190 return SR_ERR_DEV_CLOSED;
191
ecaa89af
AJ
192 if (!(devc = sdi->priv)) {
193 sr_err("sdi->priv was NULL.");
194 return SR_ERR_BUG;
195 }
196
ab939ebb 197 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
8d9c8554
AJ
198}
199
584560f1 200static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 201 const struct sr_channel_group *cg)
8d9c8554 202{
8d9c8554 203 (void)sdi;
53b4680f 204 (void)cg;
8d9c8554 205
8d9c8554 206 switch (key) {
ecaa89af 207 case SR_CONF_SCAN_OPTIONS:
584560f1 208 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
a0e0bb41 209 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
ecaa89af
AJ
210 break;
211 case SR_CONF_DEVICE_OPTIONS:
584560f1 212 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 213 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
ecaa89af 214 break;
8d9c8554
AJ
215 default:
216 return SR_ERR_NA;
217 }
218
ecaa89af 219 return SR_OK;
8d9c8554
AJ
220}
221
695dc859 222static int dev_acquisition_start(const struct sr_dev_inst *sdi)
8d9c8554 223{
ecaa89af 224 struct dev_context *devc;
8d9c8554
AJ
225
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
228
ecaa89af 229 devc = sdi->priv;
ab939ebb
LPC
230
231 sr_sw_limits_acquisition_start(&devc->sw_limits);
ecaa89af 232
102f1239 233 std_session_send_df_header(sdi, LOG_PREFIX);
ecaa89af 234
c650d3ec 235 sr_session_source_add(sdi->session, -1, 0, 10,
102f1239 236 brymen_bm86x_receive_data, (void *)sdi);
8d9c8554
AJ
237
238 return SR_OK;
239}
240
695dc859 241static int dev_acquisition_stop(struct sr_dev_inst *sdi)
8d9c8554 242{
8d9c8554
AJ
243 if (sdi->status != SR_ST_ACTIVE)
244 return SR_ERR_DEV_CLOSED;
245
3be42bc2 246 std_session_send_df_end(sdi, LOG_PREFIX);
ecaa89af 247
dd7a4a71 248 sr_session_source_remove(sdi->session, -1);
8d9c8554
AJ
249
250 return SR_OK;
251}
252
253SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = {
254 .name = "brymen-bm86x",
255 .longname = "Brymen BM86X",
256 .api_version = 1,
257 .init = init,
700d6b64 258 .cleanup = std_cleanup,
8d9c8554 259 .scan = scan,
c01bf34c 260 .dev_list = std_dev_list,
0ab70260 261 .dev_clear = NULL,
8d9c8554
AJ
262 .config_get = config_get,
263 .config_set = config_set,
264 .config_list = config_list,
265 .dev_open = dev_open,
266 .dev_close = dev_close,
267 .dev_acquisition_start = dev_acquisition_start,
268 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 269 .context = NULL,
8d9c8554 270};