]>
Commit | Line | Data |
---|---|---|
8d9c8554 AJ |
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 | ||
6ec6c43b | 20 | #include <config.h> |
8d9c8554 AJ |
21 | #include "protocol.h" |
22 | ||
ecaa89af AJ |
23 | #define BRYMEN_BC86X "0820.0001" |
24 | ||
a0e0bb41 | 25 | static const uint32_t scanopts[] = { |
ecaa89af AJ |
26 | SR_CONF_CONN, |
27 | }; | |
28 | ||
f254bc4b | 29 | static const uint32_t devopts[] = { |
ecaa89af | 30 | SR_CONF_MULTIMETER, |
ecaa89af | 31 | SR_CONF_CONTINUOUS, |
5827f61b BV |
32 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
33 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
ecaa89af AJ |
34 | }; |
35 | ||
8d9c8554 | 36 | SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info; |
8d9c8554 | 37 | |
4f840ce9 | 38 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
8d9c8554 AJ |
39 | { |
40 | return std_init(sr_ctx, di, LOG_PREFIX); | |
41 | } | |
42 | ||
4f840ce9 | 43 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
8d9c8554 | 44 | { |
ecaa89af | 45 | GSList *usb_devices, *devices, *l; |
8d9c8554 | 46 | struct drv_context *drvc; |
ecaa89af AJ |
47 | struct dev_context *devc; |
48 | struct sr_dev_inst *sdi; | |
49 | struct sr_usb_dev_inst *usb; | |
50 | struct sr_config *src; | |
ecaa89af | 51 | const char *conn; |
8d9c8554 | 52 | |
41812aca | 53 | drvc = di->context; |
8d9c8554 AJ |
54 | drvc->instances = NULL; |
55 | ||
ecaa89af AJ |
56 | conn = BRYMEN_BC86X; |
57 | for (l = options; l; l = l->next) { | |
58 | src = l->data; | |
59 | switch (src->key) { | |
60 | case SR_CONF_CONN: | |
61 | conn = g_variant_get_string(src->data, NULL); | |
62 | break; | |
63 | } | |
64 | } | |
65 | ||
66 | devices = NULL; | |
67 | if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { | |
68 | g_slist_free_full(usb_devices, g_free); | |
69 | return NULL; | |
70 | } | |
71 | ||
72 | for (l = usb_devices; l; l = l->next) { | |
73 | usb = l->data; | |
74 | ||
aac29cc1 | 75 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
76 | sdi->status = SR_ST_INACTIVE; |
77 | sdi->vendor = g_strdup("Brymen"); | |
78 | sdi->model = g_strdup("BM869"); | |
f57d8ffe | 79 | devc = g_malloc0(sizeof(struct dev_context)); |
ecaa89af AJ |
80 | sdi->priv = devc; |
81 | sdi->driver = di; | |
5e23fcab ML |
82 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
83 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
ecaa89af AJ |
84 | |
85 | sdi->inst_type = SR_INST_USB; | |
86 | sdi->conn = usb; | |
87 | ||
88 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
89 | devices = g_slist_append(devices, sdi); | |
90 | } | |
8d9c8554 AJ |
91 | |
92 | return devices; | |
93 | } | |
94 | ||
4f840ce9 | 95 | static GSList *dev_list(const struct sr_dev_driver *di) |
8d9c8554 | 96 | { |
41812aca | 97 | return ((struct drv_context *)(di->context))->instances; |
8d9c8554 AJ |
98 | } |
99 | ||
8d9c8554 AJ |
100 | static int dev_open(struct sr_dev_inst *sdi) |
101 | { | |
4f840ce9 | 102 | struct sr_dev_driver *di = sdi->driver; |
41812aca | 103 | struct drv_context *drvc = di->context; |
ecaa89af AJ |
104 | struct sr_usb_dev_inst *usb; |
105 | struct dev_context *devc; | |
106 | int ret; | |
8d9c8554 | 107 | |
ecaa89af AJ |
108 | usb = sdi->conn; |
109 | devc = sdi->priv; | |
110 | ||
111 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) | |
112 | sdi->status = SR_ST_ACTIVE; | |
6d930b41 AJ |
113 | else |
114 | return SR_ERR; | |
ecaa89af AJ |
115 | |
116 | /* Detach kernel drivers which grabbed this device (if any). */ | |
117 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { | |
118 | ret = libusb_detach_kernel_driver(usb->devhdl, 0); | |
119 | if (ret < 0) { | |
120 | sr_err("Failed to detach kernel driver: %s.", | |
121 | libusb_error_name(ret)); | |
122 | return SR_ERR; | |
123 | } | |
124 | devc->detached_kernel_driver = 1; | |
125 | sr_dbg("Successfully detached kernel driver."); | |
126 | } else { | |
127 | sr_dbg("No need to detach a kernel driver."); | |
128 | } | |
8d9c8554 | 129 | |
ecaa89af AJ |
130 | /* Claim interface 0. */ |
131 | if ((ret = libusb_claim_interface(usb->devhdl, 0)) < 0) { | |
132 | sr_err("Failed to claim interface 0: %s.", | |
133 | libusb_error_name(ret)); | |
134 | return SR_ERR; | |
135 | } | |
136 | sr_dbg("Successfully claimed interface 0."); | |
8d9c8554 | 137 | |
ecaa89af | 138 | return ret; |
8d9c8554 AJ |
139 | } |
140 | ||
141 | static int dev_close(struct sr_dev_inst *sdi) | |
142 | { | |
ecaa89af AJ |
143 | struct sr_usb_dev_inst *usb; |
144 | struct dev_context *devc; | |
145 | int ret; | |
146 | ||
6d930b41 AJ |
147 | if (sdi->status != SR_ST_ACTIVE) |
148 | return SR_ERR_DEV_CLOSED; | |
149 | ||
ecaa89af AJ |
150 | usb = sdi->conn; |
151 | devc = sdi->priv; | |
152 | ||
153 | if ((ret = libusb_release_interface(usb->devhdl, 0))) | |
154 | sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret)); | |
155 | else | |
156 | sr_dbg("Successfully released interface 0.\n"); | |
157 | ||
158 | if (!ret && devc->detached_kernel_driver) { | |
159 | if ((ret = libusb_attach_kernel_driver(usb->devhdl, 0))) { | |
160 | sr_err("Failed to attach kernel driver: %s.\n", | |
161 | libusb_error_name(ret)); | |
162 | } else { | |
163 | devc->detached_kernel_driver = 0; | |
164 | sr_dbg("Successfully attached kernel driver.\n"); | |
165 | } | |
166 | } | |
8d9c8554 | 167 | |
ecaa89af | 168 | libusb_close(usb->devhdl); |
8d9c8554 AJ |
169 | |
170 | sdi->status = SR_ST_INACTIVE; | |
171 | ||
ecaa89af | 172 | return ret; |
8d9c8554 AJ |
173 | } |
174 | ||
4f840ce9 | 175 | static int cleanup(const struct sr_dev_driver *di) |
8d9c8554 | 176 | { |
0ab70260 | 177 | return std_dev_clear(di, NULL); |
8d9c8554 AJ |
178 | } |
179 | ||
584560f1 | 180 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 181 | const struct sr_channel_group *cg) |
8d9c8554 | 182 | { |
ecaa89af | 183 | struct dev_context *devc = sdi->priv; |
8d9c8554 | 184 | |
53b4680f | 185 | (void)cg; |
8d9c8554 | 186 | |
8d9c8554 | 187 | switch (key) { |
ecaa89af AJ |
188 | case SR_CONF_LIMIT_SAMPLES: |
189 | *data = g_variant_new_uint64(devc->limit_samples); | |
190 | break; | |
191 | case SR_CONF_LIMIT_MSEC: | |
192 | *data = g_variant_new_uint64(devc->limit_msec); | |
193 | break; | |
8d9c8554 AJ |
194 | default: |
195 | return SR_ERR_NA; | |
196 | } | |
197 | ||
ecaa89af | 198 | return SR_OK; |
8d9c8554 AJ |
199 | } |
200 | ||
584560f1 | 201 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 202 | const struct sr_channel_group *cg) |
8d9c8554 | 203 | { |
ecaa89af | 204 | struct dev_context *devc; |
8d9c8554 | 205 | |
53b4680f | 206 | (void)cg; |
8d9c8554 AJ |
207 | |
208 | if (sdi->status != SR_ST_ACTIVE) | |
209 | return SR_ERR_DEV_CLOSED; | |
210 | ||
ecaa89af AJ |
211 | if (!(devc = sdi->priv)) { |
212 | sr_err("sdi->priv was NULL."); | |
213 | return SR_ERR_BUG; | |
214 | } | |
215 | ||
8d9c8554 | 216 | switch (key) { |
ecaa89af AJ |
217 | case SR_CONF_LIMIT_SAMPLES: |
218 | devc->limit_samples = g_variant_get_uint64(data); | |
ecaa89af AJ |
219 | break; |
220 | case SR_CONF_LIMIT_MSEC: | |
221 | devc->limit_msec = g_variant_get_uint64(data); | |
ecaa89af | 222 | break; |
8d9c8554 | 223 | default: |
ecaa89af | 224 | return SR_ERR_NA; |
8d9c8554 AJ |
225 | } |
226 | ||
ecaa89af | 227 | return SR_OK; |
8d9c8554 AJ |
228 | } |
229 | ||
584560f1 | 230 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 231 | const struct sr_channel_group *cg) |
8d9c8554 | 232 | { |
8d9c8554 | 233 | (void)sdi; |
53b4680f | 234 | (void)cg; |
8d9c8554 | 235 | |
8d9c8554 | 236 | switch (key) { |
ecaa89af | 237 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 238 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 239 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
ecaa89af AJ |
240 | break; |
241 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 242 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 243 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
ecaa89af | 244 | break; |
8d9c8554 AJ |
245 | default: |
246 | return SR_ERR_NA; | |
247 | } | |
248 | ||
ecaa89af | 249 | return SR_OK; |
8d9c8554 AJ |
250 | } |
251 | ||
252 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
253 | void *cb_data) | |
254 | { | |
ecaa89af | 255 | struct dev_context *devc; |
8d9c8554 | 256 | |
102f1239 BV |
257 | (void)cb_data; |
258 | ||
8d9c8554 AJ |
259 | if (sdi->status != SR_ST_ACTIVE) |
260 | return SR_ERR_DEV_CLOSED; | |
261 | ||
ecaa89af | 262 | devc = sdi->priv; |
ecaa89af AJ |
263 | devc->start_time = g_get_monotonic_time(); |
264 | ||
265 | /* Send header packet to the session bus. */ | |
102f1239 | 266 | std_session_send_df_header(sdi, LOG_PREFIX); |
ecaa89af | 267 | |
c650d3ec | 268 | sr_session_source_add(sdi->session, -1, 0, 10, |
102f1239 | 269 | brymen_bm86x_receive_data, (void *)sdi); |
8d9c8554 AJ |
270 | |
271 | return SR_OK; | |
272 | } | |
273 | ||
274 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
275 | { | |
ecaa89af | 276 | struct sr_datafeed_packet packet; |
8d9c8554 | 277 | |
102f1239 BV |
278 | (void)cb_data; |
279 | ||
8d9c8554 AJ |
280 | if (sdi->status != SR_ST_ACTIVE) |
281 | return SR_ERR_DEV_CLOSED; | |
282 | ||
ecaa89af AJ |
283 | /* Send end packet to the session bus. */ |
284 | packet.type = SR_DF_END; | |
102f1239 | 285 | sr_session_send(sdi, &packet); |
ecaa89af | 286 | |
dd7a4a71 | 287 | sr_session_source_remove(sdi->session, -1); |
8d9c8554 AJ |
288 | |
289 | return SR_OK; | |
290 | } | |
291 | ||
292 | SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = { | |
293 | .name = "brymen-bm86x", | |
294 | .longname = "Brymen BM86X", | |
295 | .api_version = 1, | |
296 | .init = init, | |
297 | .cleanup = cleanup, | |
298 | .scan = scan, | |
299 | .dev_list = dev_list, | |
0ab70260 | 300 | .dev_clear = NULL, |
8d9c8554 AJ |
301 | .config_get = config_get, |
302 | .config_set = config_set, | |
303 | .config_list = config_list, | |
304 | .dev_open = dev_open, | |
305 | .dev_close = dev_close, | |
306 | .dev_acquisition_start = dev_acquisition_start, | |
307 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 308 | .context = NULL, |
8d9c8554 | 309 | }; |