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