]>
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; | |
ecaa89af | 51 | const char *conn; |
8d9c8554 | 52 | |
8d9c8554 AJ |
53 | drvc = di->priv; |
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 | ||
95 | static GSList *dev_list(void) | |
96 | { | |
97 | return ((struct drv_context *)(di->priv))->instances; | |
98 | } | |
99 | ||
8d9c8554 AJ |
100 | static int dev_open(struct sr_dev_inst *sdi) |
101 | { | |
ecaa89af AJ |
102 | struct drv_context *drvc = di->priv; |
103 | struct sr_usb_dev_inst *usb; | |
104 | struct dev_context *devc; | |
105 | int ret; | |
8d9c8554 | 106 | |
ecaa89af AJ |
107 | usb = sdi->conn; |
108 | devc = sdi->priv; | |
109 | ||
110 | if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK) | |
111 | sdi->status = SR_ST_ACTIVE; | |
112 | ||
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); | |
116 | if (ret < 0) { | |
117 | sr_err("Failed to detach kernel driver: %s.", | |
118 | libusb_error_name(ret)); | |
119 | return SR_ERR; | |
120 | } | |
121 | devc->detached_kernel_driver = 1; | |
122 | sr_dbg("Successfully detached kernel driver."); | |
123 | } else { | |
124 | sr_dbg("No need to detach a kernel driver."); | |
125 | } | |
8d9c8554 | 126 | |
ecaa89af AJ |
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)); | |
131 | return SR_ERR; | |
132 | } | |
133 | sr_dbg("Successfully claimed interface 0."); | |
8d9c8554 | 134 | |
ecaa89af | 135 | return ret; |
8d9c8554 AJ |
136 | } |
137 | ||
138 | static int dev_close(struct sr_dev_inst *sdi) | |
139 | { | |
ecaa89af AJ |
140 | struct sr_usb_dev_inst *usb; |
141 | struct dev_context *devc; | |
142 | int ret; | |
143 | ||
144 | usb = sdi->conn; | |
145 | devc = sdi->priv; | |
146 | ||
147 | if ((ret = libusb_release_interface(usb->devhdl, 0))) | |
148 | sr_err("Failed to release interface 0: %s.\n", libusb_error_name(ret)); | |
149 | else | |
150 | sr_dbg("Successfully released interface 0.\n"); | |
151 | ||
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)); | |
156 | } else { | |
157 | devc->detached_kernel_driver = 0; | |
158 | sr_dbg("Successfully attached kernel driver.\n"); | |
159 | } | |
160 | } | |
8d9c8554 | 161 | |
ecaa89af | 162 | libusb_close(usb->devhdl); |
8d9c8554 AJ |
163 | |
164 | sdi->status = SR_ST_INACTIVE; | |
165 | ||
ecaa89af | 166 | return ret; |
8d9c8554 AJ |
167 | } |
168 | ||
169 | static int cleanup(void) | |
170 | { | |
0ab70260 | 171 | return std_dev_clear(di, NULL); |
8d9c8554 AJ |
172 | } |
173 | ||
584560f1 | 174 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 175 | const struct sr_channel_group *cg) |
8d9c8554 | 176 | { |
ecaa89af | 177 | struct dev_context *devc = sdi->priv; |
8d9c8554 | 178 | |
53b4680f | 179 | (void)cg; |
8d9c8554 | 180 | |
8d9c8554 | 181 | switch (key) { |
ecaa89af AJ |
182 | case SR_CONF_LIMIT_SAMPLES: |
183 | *data = g_variant_new_uint64(devc->limit_samples); | |
184 | break; | |
185 | case SR_CONF_LIMIT_MSEC: | |
186 | *data = g_variant_new_uint64(devc->limit_msec); | |
187 | break; | |
8d9c8554 AJ |
188 | default: |
189 | return SR_ERR_NA; | |
190 | } | |
191 | ||
ecaa89af | 192 | return SR_OK; |
8d9c8554 AJ |
193 | } |
194 | ||
584560f1 | 195 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 196 | const struct sr_channel_group *cg) |
8d9c8554 | 197 | { |
ecaa89af | 198 | struct dev_context *devc; |
8d9c8554 | 199 | |
53b4680f | 200 | (void)cg; |
8d9c8554 AJ |
201 | |
202 | if (sdi->status != SR_ST_ACTIVE) | |
203 | return SR_ERR_DEV_CLOSED; | |
204 | ||
ecaa89af AJ |
205 | if (!(devc = sdi->priv)) { |
206 | sr_err("sdi->priv was NULL."); | |
207 | return SR_ERR_BUG; | |
208 | } | |
209 | ||
8d9c8554 | 210 | switch (key) { |
ecaa89af AJ |
211 | case SR_CONF_LIMIT_SAMPLES: |
212 | devc->limit_samples = g_variant_get_uint64(data); | |
213 | sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples); | |
214 | break; | |
215 | case SR_CONF_LIMIT_MSEC: | |
216 | devc->limit_msec = g_variant_get_uint64(data); | |
217 | sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec); | |
218 | break; | |
8d9c8554 | 219 | default: |
ecaa89af | 220 | return SR_ERR_NA; |
8d9c8554 AJ |
221 | } |
222 | ||
ecaa89af | 223 | return SR_OK; |
8d9c8554 AJ |
224 | } |
225 | ||
584560f1 | 226 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 227 | const struct sr_channel_group *cg) |
8d9c8554 | 228 | { |
8d9c8554 | 229 | (void)sdi; |
53b4680f | 230 | (void)cg; |
8d9c8554 | 231 | |
8d9c8554 | 232 | switch (key) { |
ecaa89af | 233 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 234 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 235 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
ecaa89af AJ |
236 | break; |
237 | case SR_CONF_DEVICE_OPTIONS: | |
584560f1 | 238 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
f254bc4b | 239 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
ecaa89af | 240 | break; |
8d9c8554 AJ |
241 | default: |
242 | return SR_ERR_NA; | |
243 | } | |
244 | ||
ecaa89af | 245 | return SR_OK; |
8d9c8554 AJ |
246 | } |
247 | ||
248 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, | |
249 | void *cb_data) | |
250 | { | |
ecaa89af | 251 | struct dev_context *devc; |
8d9c8554 | 252 | |
102f1239 BV |
253 | (void)cb_data; |
254 | ||
8d9c8554 AJ |
255 | if (sdi->status != SR_ST_ACTIVE) |
256 | return SR_ERR_DEV_CLOSED; | |
257 | ||
ecaa89af | 258 | devc = sdi->priv; |
ecaa89af AJ |
259 | devc->start_time = g_get_monotonic_time(); |
260 | ||
261 | /* Send header packet to the session bus. */ | |
102f1239 | 262 | std_session_send_df_header(sdi, LOG_PREFIX); |
ecaa89af | 263 | |
102f1239 BV |
264 | sr_session_source_add(sdi->session, 0, 0, 10, |
265 | brymen_bm86x_receive_data, (void *)sdi); | |
8d9c8554 AJ |
266 | |
267 | return SR_OK; | |
268 | } | |
269 | ||
270 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
271 | { | |
ecaa89af | 272 | struct sr_datafeed_packet packet; |
8d9c8554 | 273 | |
102f1239 BV |
274 | (void)cb_data; |
275 | ||
8d9c8554 AJ |
276 | if (sdi->status != SR_ST_ACTIVE) |
277 | return SR_ERR_DEV_CLOSED; | |
278 | ||
ecaa89af AJ |
279 | /* Send end packet to the session bus. */ |
280 | packet.type = SR_DF_END; | |
102f1239 | 281 | sr_session_send(sdi, &packet); |
ecaa89af | 282 | |
102f1239 | 283 | sr_session_source_remove(sdi->session, 0); |
8d9c8554 AJ |
284 | |
285 | return SR_OK; | |
286 | } | |
287 | ||
288 | SR_PRIV struct sr_dev_driver brymen_bm86x_driver_info = { | |
289 | .name = "brymen-bm86x", | |
290 | .longname = "Brymen BM86X", | |
291 | .api_version = 1, | |
292 | .init = init, | |
293 | .cleanup = cleanup, | |
294 | .scan = scan, | |
295 | .dev_list = dev_list, | |
0ab70260 | 296 | .dev_clear = NULL, |
8d9c8554 AJ |
297 | .config_get = config_get, |
298 | .config_set = config_set, | |
299 | .config_list = config_list, | |
300 | .dev_open = dev_open, | |
301 | .dev_close = dev_close, | |
302 | .dev_acquisition_start = dev_acquisition_start, | |
303 | .dev_acquisition_stop = dev_acquisition_stop, | |
304 | .priv = NULL, | |
305 | }; |