]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/brymen-bm86x/api.c
Don't check sr_channel_new() return value (always succeeds).
[libsigrok.git] / src / hardware / brymen-bm86x / api.c
... / ...
CommitLineData
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
22#define BRYMEN_BC86X "0820.0001"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26};
27
28static const uint32_t devopts[] = {
29 SR_CONF_MULTIMETER,
30 SR_CONF_CONTINUOUS,
31 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
32 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
33};
34
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{
45 GSList *usb_devices, *devices, *l;
46 struct drv_context *drvc;
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_channel *ch;
52 const char *conn;
53
54 drvc = di->priv;
55 drvc->instances = NULL;
56
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 sdi = g_malloc0(sizeof(struct sr_dev_inst));
77 sdi->status = SR_ST_INACTIVE;
78 sdi->vendor = g_strdup("Brymen");
79 sdi->model = g_strdup("BM869");
80 devc = g_malloc0(sizeof(struct dev_context));
81 sdi->priv = devc;
82 sdi->driver = di;
83 ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1");
84 sdi->channels = g_slist_append(sdi->channels, ch);
85 ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P2");
86 sdi->channels = g_slist_append(sdi->channels, ch);
87
88 sdi->inst_type = SR_INST_USB;
89 sdi->conn = usb;
90
91 drvc->instances = g_slist_append(drvc->instances, sdi);
92 devices = g_slist_append(devices, sdi);
93 }
94
95 return devices;
96}
97
98static GSList *dev_list(void)
99{
100 return ((struct drv_context *)(di->priv))->instances;
101}
102
103static int dev_open(struct sr_dev_inst *sdi)
104{
105 struct drv_context *drvc = di->priv;
106 struct sr_usb_dev_inst *usb;
107 struct dev_context *devc;
108 int ret;
109
110 usb = sdi->conn;
111 devc = sdi->priv;
112
113 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
114 sdi->status = SR_ST_ACTIVE;
115
116 /* Detach kernel drivers which grabbed this device (if any). */
117 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
118 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
119 if (ret < 0) {
120 sr_err("Failed to detach kernel driver: %s.",
121 libusb_error_name(ret));
122 return SR_ERR;
123 }
124 devc->detached_kernel_driver = 1;
125 sr_dbg("Successfully detached kernel driver.");
126 } else {
127 sr_dbg("No need to detach a kernel driver.");
128 }
129
130 /* Claim interface 0. */
131 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
132 sr_err("Failed to claim interface 0: %s.",
133 libusb_error_name(ret));
134 return SR_ERR;
135 }
136 sr_dbg("Successfully claimed interface 0.");
137
138 return ret;
139}
140
141static int dev_close(struct sr_dev_inst *sdi)
142{
143 struct sr_usb_dev_inst *usb;
144 struct dev_context *devc;
145 int ret;
146
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 }
164
165 libusb_close(usb->devhdl);
166
167 sdi->status = SR_ST_INACTIVE;
168
169 return ret;
170}
171
172static int cleanup(void)
173{
174 return std_dev_clear(di, NULL);
175}
176
177static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
178 const struct sr_channel_group *cg)
179{
180 struct dev_context *devc = sdi->priv;
181
182 (void)cg;
183
184 switch (key) {
185 case SR_CONF_LIMIT_SAMPLES:
186 *data = g_variant_new_uint64(devc->limit_samples);
187 break;
188 case SR_CONF_LIMIT_MSEC:
189 *data = g_variant_new_uint64(devc->limit_msec);
190 break;
191 default:
192 return SR_ERR_NA;
193 }
194
195 return SR_OK;
196}
197
198static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
199 const struct sr_channel_group *cg)
200{
201 struct dev_context *devc;
202
203 (void)cg;
204
205 if (sdi->status != SR_ST_ACTIVE)
206 return SR_ERR_DEV_CLOSED;
207
208 if (!(devc = sdi->priv)) {
209 sr_err("sdi->priv was NULL.");
210 return SR_ERR_BUG;
211 }
212
213 switch (key) {
214 case SR_CONF_LIMIT_SAMPLES:
215 devc->limit_samples = g_variant_get_uint64(data);
216 sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples);
217 break;
218 case SR_CONF_LIMIT_MSEC:
219 devc->limit_msec = g_variant_get_uint64(data);
220 sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec);
221 break;
222 default:
223 return SR_ERR_NA;
224 }
225
226 return SR_OK;
227}
228
229static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
230 const struct sr_channel_group *cg)
231{
232 (void)sdi;
233 (void)cg;
234
235 switch (key) {
236 case SR_CONF_SCAN_OPTIONS:
237 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
238 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
239 break;
240 case SR_CONF_DEVICE_OPTIONS:
241 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
242 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
243 break;
244 default:
245 return SR_ERR_NA;
246 }
247
248 return SR_OK;
249}
250
251static int dev_acquisition_start(const struct sr_dev_inst *sdi,
252 void *cb_data)
253{
254 struct dev_context *devc;
255
256 (void)cb_data;
257
258 if (sdi->status != SR_ST_ACTIVE)
259 return SR_ERR_DEV_CLOSED;
260
261 devc = sdi->priv;
262 devc->start_time = g_get_monotonic_time();
263
264 /* Send header packet to the session bus. */
265 std_session_send_df_header(sdi, LOG_PREFIX);
266
267 sr_session_source_add(sdi->session, 0, 0, 10,
268 brymen_bm86x_receive_data, (void *)sdi);
269
270 return SR_OK;
271}
272
273static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
274{
275 struct sr_datafeed_packet packet;
276
277 (void)cb_data;
278
279 if (sdi->status != SR_ST_ACTIVE)
280 return SR_ERR_DEV_CLOSED;
281
282 /* Send end packet to the session bus. */
283 packet.type = SR_DF_END;
284 sr_session_send(sdi, &packet);
285
286 sr_session_source_remove(sdi->session, 0);
287
288 return SR_OK;
289}
290
291SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = {
292 .name = "brymen-bm86x",
293 .longname = "Brymen BM86X",
294 .api_version = 1,
295 .init = init,
296 .cleanup = cleanup,
297 .scan = scan,
298 .dev_list = dev_list,
299 .dev_clear = NULL,
300 .config_get = config_get,
301 .config_set = config_set,
302 .config_list = config_list,
303 .dev_open = dev_open,
304 .dev_close = dev_close,
305 .dev_acquisition_start = dev_acquisition_start,
306 .dev_acquisition_stop = dev_acquisition_stop,
307 .priv = NULL,
308};