]> sigrok.org Git - libsigrok.git/blame - hardware/brymen-bm86x/api.c
brymen-bm86x: actual driver implementation
[libsigrok.git] / 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
24static const int32_t hwopts[] = {
25 SR_CONF_CONN,
26};
27
28static const int32_t hwcaps[] = {
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;
51 struct sr_probe *probe;
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;
89 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
90 return NULL;
91 sdi->probes = g_slist_append(sdi->probes, probe);
92 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P2")))
93 return NULL;
94 sdi->probes = g_slist_append(sdi->probes, probe);
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
111static int dev_clear(void)
112{
113 return std_dev_clear(di, NULL);
114}
115
116static int dev_open(struct sr_dev_inst *sdi)
117{
ecaa89af
AJ
118 struct drv_context *drvc = di->priv;
119 struct sr_usb_dev_inst *usb;
120 struct dev_context *devc;
121 int ret;
8d9c8554 122
ecaa89af
AJ
123 usb = sdi->conn;
124 devc = sdi->priv;
125
126 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
127 sdi->status = SR_ST_ACTIVE;
128
129 /* Detach kernel drivers which grabbed this device (if any). */
130 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
131 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
132 if (ret < 0) {
133 sr_err("Failed to detach kernel driver: %s.",
134 libusb_error_name(ret));
135 return SR_ERR;
136 }
137 devc->detached_kernel_driver = 1;
138 sr_dbg("Successfully detached kernel driver.");
139 } else {
140 sr_dbg("No need to detach a kernel driver.");
141 }
8d9c8554 142
ecaa89af
AJ
143 /* Claim interface 0. */
144 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
145 sr_err("Failed to claim interface 0: %s.",
146 libusb_error_name(ret));
147 return SR_ERR;
148 }
149 sr_dbg("Successfully claimed interface 0.");
8d9c8554 150
ecaa89af 151 return ret;
8d9c8554
AJ
152}
153
154static int dev_close(struct sr_dev_inst *sdi)
155{
ecaa89af
AJ
156 struct sr_usb_dev_inst *usb;
157 struct dev_context *devc;
158 int ret;
159
160 usb = sdi->conn;
161 devc = sdi->priv;
162
163 if ((ret = libusb_release_interface(usb->devhdl, 0)))
164 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
165 else
166 sr_dbg("Successfully released interface 0.\n");
167
168 if (!ret && devc->detached_kernel_driver) {
169 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) {
170 sr_err("Failed to attach kernel driver: %s.\n",
171 libusb_error_name(ret));
172 } else {
173 devc->detached_kernel_driver = 0;
174 sr_dbg("Successfully attached kernel driver.\n");
175 }
176 }
8d9c8554 177
ecaa89af 178 libusb_close(usb->devhdl);
8d9c8554
AJ
179
180 sdi->status = SR_ST_INACTIVE;
181
ecaa89af 182 return ret;
8d9c8554
AJ
183}
184
185static int cleanup(void)
186{
ecaa89af 187 return dev_clear();
8d9c8554
AJ
188}
189
190static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
191 const struct sr_probe_group *probe_group)
192{
ecaa89af 193 struct dev_context *devc = sdi->priv;
8d9c8554 194
8d9c8554
AJ
195 (void)probe_group;
196
8d9c8554 197 switch (key) {
ecaa89af
AJ
198 case SR_CONF_LIMIT_SAMPLES:
199 *data = g_variant_new_uint64(devc->limit_samples);
200 break;
201 case SR_CONF_LIMIT_MSEC:
202 *data = g_variant_new_uint64(devc->limit_msec);
203 break;
8d9c8554
AJ
204 default:
205 return SR_ERR_NA;
206 }
207
ecaa89af 208 return SR_OK;
8d9c8554
AJ
209}
210
211static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
212 const struct sr_probe_group *probe_group)
213{
ecaa89af 214 struct dev_context *devc;
8d9c8554 215
8d9c8554
AJ
216 (void)probe_group;
217
218 if (sdi->status != SR_ST_ACTIVE)
219 return SR_ERR_DEV_CLOSED;
220
ecaa89af
AJ
221 if (!(devc = sdi->priv)) {
222 sr_err("sdi->priv was NULL.");
223 return SR_ERR_BUG;
224 }
225
8d9c8554 226 switch (key) {
ecaa89af
AJ
227 case SR_CONF_LIMIT_SAMPLES:
228 devc->limit_samples = g_variant_get_uint64(data);
229 sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples);
230 break;
231 case SR_CONF_LIMIT_MSEC:
232 devc->limit_msec = g_variant_get_uint64(data);
233 sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec);
234 break;
8d9c8554 235 default:
ecaa89af 236 return SR_ERR_NA;
8d9c8554
AJ
237 }
238
ecaa89af 239 return SR_OK;
8d9c8554
AJ
240}
241
242static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
243 const struct sr_probe_group *probe_group)
244{
8d9c8554 245 (void)sdi;
8d9c8554
AJ
246 (void)probe_group;
247
8d9c8554 248 switch (key) {
ecaa89af
AJ
249 case SR_CONF_SCAN_OPTIONS:
250 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
251 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
252 break;
253 case SR_CONF_DEVICE_OPTIONS:
254 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
255 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
256 break;
8d9c8554
AJ
257 default:
258 return SR_ERR_NA;
259 }
260
ecaa89af 261 return SR_OK;
8d9c8554
AJ
262}
263
264static int dev_acquisition_start(const struct sr_dev_inst *sdi,
265 void *cb_data)
266{
ecaa89af 267 struct dev_context *devc;
8d9c8554
AJ
268
269 if (sdi->status != SR_ST_ACTIVE)
270 return SR_ERR_DEV_CLOSED;
271
ecaa89af
AJ
272 devc = sdi->priv;
273 devc->session_cb_data = cb_data;
274 devc->start_time = g_get_monotonic_time();
275
276 /* Send header packet to the session bus. */
277 std_session_send_df_header(cb_data, LOG_PREFIX);
278
279 sr_source_add(0, 0, 10, brymen_bm86x_receive_data, (void *)sdi);
8d9c8554
AJ
280
281 return SR_OK;
282}
283
284static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
285{
ecaa89af 286 struct sr_datafeed_packet packet;
8d9c8554
AJ
287
288 if (sdi->status != SR_ST_ACTIVE)
289 return SR_ERR_DEV_CLOSED;
290
ecaa89af
AJ
291 /* Send end packet to the session bus. */
292 packet.type = SR_DF_END;
293 sr_session_send(cb_data, &packet);
294
295 sr_source_remove(0);
8d9c8554
AJ
296
297 return SR_OK;
298}
299
300SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = {
301 .name = "brymen-bm86x",
302 .longname = "Brymen BM86X",
303 .api_version = 1,
304 .init = init,
305 .cleanup = cleanup,
306 .scan = scan,
307 .dev_list = dev_list,
308 .dev_clear = dev_clear,
309 .config_get = config_get,
310 .config_set = config_set,
311 .config_list = config_list,
312 .dev_open = dev_open,
313 .dev_close = dev_close,
314 .dev_acquisition_start = dev_acquisition_start,
315 .dev_acquisition_stop = dev_acquisition_stop,
316 .priv = NULL,
317};