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