]> sigrok.org Git - libsigrok.git/blob - src/hardware/brymen-bm86x/api.c
Consistently use the "Sysclk" spelling everywhere.
[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 drvopts[] = {
30         SR_CONF_MULTIMETER,
31 };
32
33 static const uint32_t devopts[] = {
34         SR_CONF_CONTINUOUS,
35         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 };
38
39 static GSList *scan(struct sr_dev_driver *di, GSList *options)
40 {
41         GSList *usb_devices, *devices, *l;
42         struct drv_context *drvc;
43         struct dev_context *devc;
44         struct sr_dev_inst *sdi;
45         struct sr_usb_dev_inst *usb;
46         struct sr_config *src;
47         const char *conn;
48
49         drvc = di->context;
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                 return NULL;
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                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
75                 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "P2");
76
77                 sdi->inst_type = SR_INST_USB;
78                 sdi->conn = usb;
79
80                 sr_sw_limits_init(&devc->sw_limits);
81
82                 devices = g_slist_append(devices, sdi);
83         }
84
85         return std_scan_complete(di, devices);
86 }
87
88 static int dev_open(struct sr_dev_inst *sdi)
89 {
90         struct sr_dev_driver *di = sdi->driver;
91         struct drv_context *drvc = di->context;
92         struct sr_usb_dev_inst *usb;
93         struct dev_context *devc;
94         int ret;
95
96         usb = sdi->conn;
97         devc = sdi->priv;
98
99         if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) < 0)
100                 return SR_ERR;
101
102         if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
103                 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
104                 if (ret < 0) {
105                         sr_err("Failed to detach kernel driver: %s.",
106                                libusb_error_name(ret));
107                         return SR_ERR;
108                 }
109                 devc->detached_kernel_driver = 1;
110         }
111
112         if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
113                 sr_err("Failed to claim interface 0: %s.",
114                        libusb_error_name(ret));
115                 return SR_ERR;
116         }
117
118         return SR_OK;
119 }
120
121 static int dev_close(struct sr_dev_inst *sdi)
122 {
123         struct sr_usb_dev_inst *usb;
124         struct dev_context *devc;
125         int ret;
126
127         usb = sdi->conn;
128         devc = sdi->priv;
129
130         if (!usb->devhdl)
131                 return SR_OK;
132
133         if ((ret = libusb_release_interface(usb->devhdl, 0)))
134                 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
135
136         if (!ret && devc->detached_kernel_driver) {
137                 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0)))
138                         sr_err("Failed to attach kernel driver: %s.\n",
139                                libusb_error_name(ret));
140                 else
141                         devc->detached_kernel_driver = 0;
142         }
143
144         libusb_close(usb->devhdl);
145
146         return (ret == 0) ? SR_OK : SR_ERR;
147 }
148
149 static int config_get(uint32_t key, GVariant **data,
150         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
151 {
152         struct dev_context *devc = sdi->priv;
153
154         (void)cg;
155
156         return sr_sw_limits_config_get(&devc->sw_limits, key, data);
157 }
158
159 static int config_set(uint32_t key, GVariant *data,
160         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
161 {
162         struct dev_context *devc;
163
164         (void)cg;
165
166         devc = sdi->priv;
167
168         return sr_sw_limits_config_set(&devc->sw_limits, key, data);
169 }
170
171 static int config_list(uint32_t key, GVariant **data,
172         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
173 {
174         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
175 }
176
177 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
178 {
179         struct dev_context *devc;
180
181         devc = sdi->priv;
182
183         sr_sw_limits_acquisition_start(&devc->sw_limits);
184
185         std_session_send_df_header(sdi);
186
187         sr_session_source_add(sdi->session, -1, 0, 10,
188                         brymen_bm86x_receive_data, (void *)sdi);
189
190         return SR_OK;
191 }
192
193 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
194 {
195         std_session_send_df_end(sdi);
196
197         sr_session_source_remove(sdi->session, -1);
198
199         return SR_OK;
200 }
201
202 static struct sr_dev_driver brymen_bm86x_driver_info = {
203         .name = "brymen-bm86x",
204         .longname = "Brymen BM86X",
205         .api_version = 1,
206         .init = std_init,
207         .cleanup = std_cleanup,
208         .scan = scan,
209         .dev_list = std_dev_list,
210         .dev_clear = std_dev_clear,
211         .config_get = config_get,
212         .config_set = config_set,
213         .config_list = config_list,
214         .dev_open = dev_open,
215         .dev_close = dev_close,
216         .dev_acquisition_start = dev_acquisition_start,
217         .dev_acquisition_stop = dev_acquisition_stop,
218         .context = NULL,
219 };
220 SR_REGISTER_DEV_DRIVER(brymen_bm86x_driver_info);