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