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