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