2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
22 #define BRYMEN_BC86X "0820.0001"
24 static const uint32_t scanopts[] = {
28 static const uint32_t devopts[] = {
31 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
32 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
35 SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info;
37 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
39 return std_init(sr_ctx, di, LOG_PREFIX);
42 static GSList *scan(struct sr_dev_driver *di, GSList *options)
44 GSList *usb_devices, *devices, *l;
45 struct drv_context *drvc;
46 struct dev_context *devc;
47 struct sr_dev_inst *sdi;
48 struct sr_usb_dev_inst *usb;
49 struct sr_config *src;
53 drvc->instances = NULL;
56 for (l = options; l; l = l->next) {
60 conn = g_variant_get_string(src->data, NULL);
66 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
67 g_slist_free_full(usb_devices, g_free);
71 for (l = usb_devices; l; l = l->next) {
74 sdi = g_malloc0(sizeof(struct sr_dev_inst));
75 sdi->status = SR_ST_INACTIVE;
76 sdi->vendor = g_strdup("Brymen");
77 sdi->model = g_strdup("BM869");
78 devc = g_malloc0(sizeof(struct dev_context));
81 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
82 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2");
84 sdi->inst_type = SR_INST_USB;
87 drvc->instances = g_slist_append(drvc->instances, sdi);
88 devices = g_slist_append(devices, sdi);
94 static GSList *dev_list(const struct sr_dev_driver *di)
96 return ((struct drv_context *)(di->context))->instances;
99 static int dev_open(struct sr_dev_inst *sdi)
101 struct sr_dev_driver *di = sdi->driver;
102 struct drv_context *drvc = di->context;
103 struct sr_usb_dev_inst *usb;
104 struct dev_context *devc;
110 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
111 sdi->status = SR_ST_ACTIVE;
113 /* Detach kernel drivers which grabbed this device (if any). */
114 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
115 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
117 sr_err("Failed to detach kernel driver: %s.",
118 libusb_error_name(ret));
121 devc->detached_kernel_driver = 1;
122 sr_dbg("Successfully detached kernel driver.");
124 sr_dbg("No need to detach a kernel driver.");
127 /* Claim interface 0. */
128 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
129 sr_err("Failed to claim interface 0: %s.",
130 libusb_error_name(ret));
133 sr_dbg("Successfully claimed interface 0.");
138 static int dev_close(struct sr_dev_inst *sdi)
140 struct sr_usb_dev_inst *usb;
141 struct dev_context *devc;
147 if ((ret = libusb_release_interface(usb->devhdl, 0)))
148 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
150 sr_dbg("Successfully released interface 0.\n");
152 if (!ret && devc->detached_kernel_driver) {
153 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) {
154 sr_err("Failed to attach kernel driver: %s.\n",
155 libusb_error_name(ret));
157 devc->detached_kernel_driver = 0;
158 sr_dbg("Successfully attached kernel driver.\n");
162 libusb_close(usb->devhdl);
164 sdi->status = SR_ST_INACTIVE;
169 static int cleanup(const struct sr_dev_driver *di)
171 return std_dev_clear(di, NULL);
174 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
175 const struct sr_channel_group *cg)
177 struct dev_context *devc = sdi->priv;
182 case SR_CONF_LIMIT_SAMPLES:
183 *data = g_variant_new_uint64(devc->limit_samples);
185 case SR_CONF_LIMIT_MSEC:
186 *data = g_variant_new_uint64(devc->limit_msec);
195 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
196 const struct sr_channel_group *cg)
198 struct dev_context *devc;
202 if (sdi->status != SR_ST_ACTIVE)
203 return SR_ERR_DEV_CLOSED;
205 if (!(devc = sdi->priv)) {
206 sr_err("sdi->priv was NULL.");
211 case SR_CONF_LIMIT_SAMPLES:
212 devc->limit_samples = g_variant_get_uint64(data);
214 case SR_CONF_LIMIT_MSEC:
215 devc->limit_msec = g_variant_get_uint64(data);
224 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
225 const struct sr_channel_group *cg)
231 case SR_CONF_SCAN_OPTIONS:
232 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
233 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
235 case SR_CONF_DEVICE_OPTIONS:
236 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
237 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
246 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
249 struct dev_context *devc;
253 if (sdi->status != SR_ST_ACTIVE)
254 return SR_ERR_DEV_CLOSED;
257 devc->start_time = g_get_monotonic_time();
259 /* Send header packet to the session bus. */
260 std_session_send_df_header(sdi, LOG_PREFIX);
262 sr_session_source_add(sdi->session, 0, 0, 10,
263 brymen_bm86x_receive_data, (void *)sdi);
268 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
270 struct sr_datafeed_packet packet;
274 if (sdi->status != SR_ST_ACTIVE)
275 return SR_ERR_DEV_CLOSED;
277 /* Send end packet to the session bus. */
278 packet.type = SR_DF_END;
279 sr_session_send(sdi, &packet);
281 sr_session_source_remove(sdi->session, 0);
286 SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = {
287 .name = "brymen-bm86x",
288 .longname = "Brymen BM86X",
293 .dev_list = dev_list,
295 .config_get = config_get,
296 .config_set = config_set,
297 .config_list = config_list,
298 .dev_open = dev_open,
299 .dev_close = dev_close,
300 .dev_acquisition_start = dev_acquisition_start,
301 .dev_acquisition_stop = dev_acquisition_stop,