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