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/>.
23 #define BRYMEN_BC86X "0820.0001"
25 static const uint32_t scanopts[] = {
29 static const uint32_t devopts[] = {
32 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
33 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
36 static GSList *scan(struct sr_dev_driver *di, GSList *options)
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;
49 for (l = options; l; l = l->next) {
53 conn = g_variant_get_string(src->data, NULL);
59 if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
60 g_slist_free_full(usb_devices, g_free);
64 for (l = usb_devices; l; l = l->next) {
67 sdi = g_malloc0(sizeof(struct sr_dev_inst));
68 sdi->status = SR_ST_INACTIVE;
69 sdi->vendor = g_strdup("Brymen");
70 sdi->model = g_strdup("BM869");
71 devc = g_malloc0(sizeof(struct dev_context));
74 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
75 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2");
77 sdi->inst_type = SR_INST_USB;
80 sr_sw_limits_init(&devc->sw_limits);
82 drvc->instances = g_slist_append(drvc->instances, sdi);
83 devices = g_slist_append(devices, sdi);
89 static int dev_open(struct sr_dev_inst *sdi)
91 struct sr_dev_driver *di = sdi->driver;
92 struct drv_context *drvc = di->context;
93 struct sr_usb_dev_inst *usb;
94 struct dev_context *devc;
100 if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
101 sdi->status = SR_ST_ACTIVE;
105 /* Detach kernel drivers which grabbed this device (if any). */
106 if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
107 ret = libusb_detach_kernel_driver(usb->devhdl, 0);
109 sr_err("Failed to detach kernel driver: %s.",
110 libusb_error_name(ret));
113 devc->detached_kernel_driver = 1;
114 sr_dbg("Successfully detached kernel driver.");
116 sr_dbg("No need to detach a kernel driver.");
119 /* Claim interface 0. */
120 if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) {
121 sr_err("Failed to claim interface 0: %s.",
122 libusb_error_name(ret));
125 sr_dbg("Successfully claimed interface 0.");
130 static int dev_close(struct sr_dev_inst *sdi)
132 struct sr_usb_dev_inst *usb;
133 struct dev_context *devc;
136 if (sdi->status != SR_ST_ACTIVE)
137 return SR_ERR_DEV_CLOSED;
142 if ((ret = libusb_release_interface(usb->devhdl, 0)))
143 sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret));
145 sr_dbg("Successfully released interface 0.\n");
147 if (!ret && devc->detached_kernel_driver) {
148 if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) {
149 sr_err("Failed to attach kernel driver: %s.\n",
150 libusb_error_name(ret));
152 devc->detached_kernel_driver = 0;
153 sr_dbg("Successfully attached kernel driver.\n");
157 libusb_close(usb->devhdl);
159 sdi->status = SR_ST_INACTIVE;
164 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
165 const struct sr_channel_group *cg)
167 struct dev_context *devc = sdi->priv;
171 return sr_sw_limits_config_get(&devc->sw_limits, key, data);
174 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
175 const struct sr_channel_group *cg)
177 struct dev_context *devc;
181 if (sdi->status != SR_ST_ACTIVE)
182 return SR_ERR_DEV_CLOSED;
186 return sr_sw_limits_config_set(&devc->sw_limits, key, data);
189 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
190 const struct sr_channel_group *cg)
196 case SR_CONF_SCAN_OPTIONS:
197 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
198 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
200 case SR_CONF_DEVICE_OPTIONS:
201 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
202 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
211 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
213 struct dev_context *devc;
215 if (sdi->status != SR_ST_ACTIVE)
216 return SR_ERR_DEV_CLOSED;
220 sr_sw_limits_acquisition_start(&devc->sw_limits);
222 std_session_send_df_header(sdi, LOG_PREFIX);
224 sr_session_source_add(sdi->session, -1, 0, 10,
225 brymen_bm86x_receive_data, (void *)sdi);
230 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
232 if (sdi->status != SR_ST_ACTIVE)
233 return SR_ERR_DEV_CLOSED;
235 std_session_send_df_end(sdi, LOG_PREFIX);
237 sr_session_source_remove(sdi->session, -1);
242 static struct sr_dev_driver brymen_bm86x_driver_info = {
243 .name = "brymen-bm86x",
244 .longname = "Brymen BM86X",
247 .cleanup = std_cleanup,
249 .dev_list = std_dev_list,
251 .config_get = config_get,
252 .config_set = config_set,
253 .config_list = config_list,
254 .dev_open = dev_open,
255 .dev_close = dev_close,
256 .dev_acquisition_start = dev_acquisition_start,
257 .dev_acquisition_stop = dev_acquisition_stop,
260 SR_REGISTER_DEV_DRIVER(brymen_bm86x_driver_info);