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