]> sigrok.org Git - libsigrok.git/blame - src/hardware/brymen-bm86x/api.c
Change type of SR_CONF keys to uint32_t.
[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
20#include "protocol.h"
21
ecaa89af
AJ
22#define BRYMEN_BC86X "0820.0001"
23
584560f1 24static const uint32_t hwopts[] = {
ecaa89af
AJ
25 SR_CONF_CONN,
26};
27
584560f1 28static const uint32_t hwcaps[] = {
ecaa89af
AJ
29 SR_CONF_MULTIMETER,
30 SR_CONF_LIMIT_SAMPLES,
31 SR_CONF_LIMIT_MSEC,
32 SR_CONF_CONTINUOUS,
33};
34
8d9c8554
AJ
35SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info;
36static struct sr_dev_driver *di = &brymen_bm86x_driver_info;
37
38static int init(struct sr_context *sr_ctx)
39{
40 return std_init(sr_ctx, di, LOG_PREFIX);
41}
42
43static GSList *scan(GSList *options)
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;
ba7dd8bb 51 struct sr_channel *ch;
ecaa89af 52 const char *conn;
8d9c8554 53
8d9c8554
AJ
54 drvc = di->priv;
55 drvc->instances = NULL;
56
ecaa89af
AJ
57 conn = BRYMEN_BC86X;
58 for (l = options; l; l = l->next) {
59 src = l->data;
60 switch (src->key) {
61 case SR_CONF_CONN:
62 conn = g_variant_get_string(src->data, NULL);
63 break;
64 }
65 }
66
67 devices = NULL;
68 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
69 g_slist_free_full(usb_devices, g_free);
70 return NULL;
71 }
72
73 for (l = usb_devices; l; l = l->next) {
74 usb = l->data;
75
76 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
77 "Brymen", "BM869", NULL))) {
78 sr_err("sr_dev_inst_new returned NULL.");
79 return NULL;
80 }
81
82 if (!(devc = g_try_malloc0(sizeof(*devc)))) {
83 sr_err("Device context malloc failed.");
84 return NULL;
85 }
86
87 sdi->priv = devc;
88 sdi->driver = di;
3f239f08 89 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
ecaa89af 90 return NULL;
ba7dd8bb 91 sdi->channels = g_slist_append(sdi->channels, ch);
3f239f08 92 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P2")))
ecaa89af 93 return NULL;
ba7dd8bb 94 sdi->channels = g_slist_append(sdi->channels, ch);
ecaa89af
AJ
95
96 sdi->inst_type = SR_INST_USB;
97 sdi->conn = usb;
98
99 drvc->instances = g_slist_append(drvc->instances, sdi);
100 devices = g_slist_append(devices, sdi);
101 }
8d9c8554
AJ
102
103 return devices;
104}
105
106static GSList *dev_list(void)
107{
108 return ((struct drv_context *)(di->priv))->instances;
109}
110
8d9c8554
AJ
111static int dev_open(struct sr_dev_inst *sdi)
112{
ecaa89af
AJ
113 struct drv_context *drvc = di->priv;
114 struct sr_usb_dev_inst *usb;
115 struct dev_context *devc;
116 int ret;
8d9c8554 117
ecaa89af
AJ
118 usb = sdi->conn;
119 devc = sdi->priv;
120
121 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
122 sdi->status = SR_ST_ACTIVE;
123
124 /* Detach kernel drivers which grabbed this device (if any). */
125 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
126 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
127 if (ret < 0) {
128 sr_err("Failed to detach kernel driver: %s.",
129 libusb_error_name(ret));
130 return SR_ERR;
131 }
132 devc->detached_kernel_driver = 1;
133 sr_dbg("Successfully detached kernel driver.");
134 } else {
135 sr_dbg("No need to detach a kernel driver.");
136 }
8d9c8554 137
ecaa89af
AJ
138 /* Claim interface 0. */
139 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
140 sr_err("Failed to claim interface 0: %s.",
141 libusb_error_name(ret));
142 return SR_ERR;
143 }
144 sr_dbg("Successfully claimed interface 0.");
8d9c8554 145
ecaa89af 146 return ret;
8d9c8554
AJ
147}
148
149static int dev_close(struct sr_dev_inst *sdi)
150{
ecaa89af
AJ
151 struct sr_usb_dev_inst *usb;
152 struct dev_context *devc;
153 int ret;
154
155 usb = sdi->conn;
156 devc = sdi->priv;
157
158 if ((ret = libusb_release_interface(usb->devhdl, 0)))
159 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
160 else
161 sr_dbg("Successfully released interface 0.\n");
162
163 if (!ret && devc->detached_kernel_driver) {
164 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) {
165 sr_err("Failed to attach kernel driver: %s.\n",
166 libusb_error_name(ret));
167 } else {
168 devc->detached_kernel_driver = 0;
169 sr_dbg("Successfully attached kernel driver.\n");
170 }
171 }
8d9c8554 172
ecaa89af 173 libusb_close(usb->devhdl);
8d9c8554
AJ
174
175 sdi->status = SR_ST_INACTIVE;
176
ecaa89af 177 return ret;
8d9c8554
AJ
178}
179
180static int cleanup(void)
181{
0ab70260 182 return std_dev_clear(di, NULL);
8d9c8554
AJ
183}
184
584560f1 185static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 186 const struct sr_channel_group *cg)
8d9c8554 187{
ecaa89af 188 struct dev_context *devc = sdi->priv;
8d9c8554 189
53b4680f 190 (void)cg;
8d9c8554 191
8d9c8554 192 switch (key) {
ecaa89af
AJ
193 case SR_CONF_LIMIT_SAMPLES:
194 *data = g_variant_new_uint64(devc->limit_samples);
195 break;
196 case SR_CONF_LIMIT_MSEC:
197 *data = g_variant_new_uint64(devc->limit_msec);
198 break;
8d9c8554
AJ
199 default:
200 return SR_ERR_NA;
201 }
202
ecaa89af 203 return SR_OK;
8d9c8554
AJ
204}
205
584560f1 206static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 207 const struct sr_channel_group *cg)
8d9c8554 208{
ecaa89af 209 struct dev_context *devc;
8d9c8554 210
53b4680f 211 (void)cg;
8d9c8554
AJ
212
213 if (sdi->status != SR_ST_ACTIVE)
214 return SR_ERR_DEV_CLOSED;
215
ecaa89af
AJ
216 if (!(devc = sdi->priv)) {
217 sr_err("sdi->priv was NULL.");
218 return SR_ERR_BUG;
219 }
220
8d9c8554 221 switch (key) {
ecaa89af
AJ
222 case SR_CONF_LIMIT_SAMPLES:
223 devc->limit_samples = g_variant_get_uint64(data);
224 sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples);
225 break;
226 case SR_CONF_LIMIT_MSEC:
227 devc->limit_msec = g_variant_get_uint64(data);
228 sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec);
229 break;
8d9c8554 230 default:
ecaa89af 231 return SR_ERR_NA;
8d9c8554
AJ
232 }
233
ecaa89af 234 return SR_OK;
8d9c8554
AJ
235}
236
584560f1 237static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 238 const struct sr_channel_group *cg)
8d9c8554 239{
8d9c8554 240 (void)sdi;
53b4680f 241 (void)cg;
8d9c8554 242
8d9c8554 243 switch (key) {
ecaa89af 244 case SR_CONF_SCAN_OPTIONS:
584560f1
BV
245 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
246 hwopts, ARRAY_SIZE(hwopts), sizeof(uint32_t));
ecaa89af
AJ
247 break;
248 case SR_CONF_DEVICE_OPTIONS:
584560f1
BV
249 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
250 hwcaps, ARRAY_SIZE(hwcaps), sizeof(uint32_t));
ecaa89af 251 break;
8d9c8554
AJ
252 default:
253 return SR_ERR_NA;
254 }
255
ecaa89af 256 return SR_OK;
8d9c8554
AJ
257}
258
259static int dev_acquisition_start(const struct sr_dev_inst *sdi,
260 void *cb_data)
261{
ecaa89af 262 struct dev_context *devc;
8d9c8554 263
102f1239
BV
264 (void)cb_data;
265
8d9c8554
AJ
266 if (sdi->status != SR_ST_ACTIVE)
267 return SR_ERR_DEV_CLOSED;
268
ecaa89af 269 devc = sdi->priv;
ecaa89af
AJ
270 devc->start_time = g_get_monotonic_time();
271
272 /* Send header packet to the session bus. */
102f1239 273 std_session_send_df_header(sdi, LOG_PREFIX);
ecaa89af 274
102f1239
BV
275 sr_session_source_add(sdi->session, 0, 0, 10,
276 brymen_bm86x_receive_data, (void *)sdi);
8d9c8554
AJ
277
278 return SR_OK;
279}
280
281static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
282{
ecaa89af 283 struct sr_datafeed_packet packet;
8d9c8554 284
102f1239
BV
285 (void)cb_data;
286
8d9c8554
AJ
287 if (sdi->status != SR_ST_ACTIVE)
288 return SR_ERR_DEV_CLOSED;
289
ecaa89af
AJ
290 /* Send end packet to the session bus. */
291 packet.type = SR_DF_END;
102f1239 292 sr_session_send(sdi, &packet);
ecaa89af 293
102f1239 294 sr_session_source_remove(sdi->session, 0);
8d9c8554
AJ
295
296 return SR_OK;
297}
298
299SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = {
300 .name = "brymen-bm86x",
301 .longname = "Brymen BM86X",
302 .api_version = 1,
303 .init = init,
304 .cleanup = cleanup,
305 .scan = scan,
306 .dev_list = dev_list,
0ab70260 307 .dev_clear = NULL,
8d9c8554
AJ
308 .config_get = config_get,
309 .config_set = config_set,
310 .config_list = config_list,
311 .dev_open = dev_open,
312 .dev_close = dev_close,
313 .dev_acquisition_start = dev_acquisition_start,
314 .dev_acquisition_stop = dev_acquisition_stop,
315 .priv = NULL,
316};