]>
Commit | Line | Data |
---|---|---|
79081ec8 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include "libsigrok.h" | |
24 | #include "libsigrok-internal.h" | |
25 | #include "protocol.h" | |
26 | ||
388f9d3e UH |
27 | #define UNI_T_UT_D04_NEW "1a86.e008" |
28 | ||
73365eae UH |
29 | static const int hwopts[] = { |
30 | SR_HWOPT_CONN, | |
31 | 0, | |
32 | }; | |
33 | ||
79081ec8 UH |
34 | static const int hwcaps[] = { |
35 | SR_HWCAP_MULTIMETER, | |
36 | SR_HWCAP_LIMIT_SAMPLES, | |
37 | SR_HWCAP_LIMIT_MSEC, | |
38 | SR_HWCAP_CONTINUOUS, | |
39 | 0, | |
40 | }; | |
41 | ||
fdbcb86d | 42 | SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info; |
fdbcb86d | 43 | SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info; |
6ac5f892 UH |
44 | |
45 | static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info; | |
fdbcb86d | 46 | static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info; |
79081ec8 | 47 | |
6ac5f892 UH |
48 | /* After hw_init() this will point to a device-specific entry (see above). */ |
49 | static struct sr_dev_driver *di = NULL; | |
50 | ||
79081ec8 UH |
51 | static int clear_instances(void) |
52 | { | |
53 | /* TODO: Use common code later. */ | |
54 | ||
55 | return SR_OK; | |
56 | } | |
57 | ||
34f06b90 | 58 | static int hw_init(struct sr_context *sr_ctx, int dmm) |
79081ec8 | 59 | { |
79081ec8 UH |
60 | struct drv_context *drvc; |
61 | ||
62 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
63 | sr_err("Driver context malloc failed."); | |
64 | return SR_ERR_MALLOC; | |
65 | } | |
66 | ||
fdbcb86d | 67 | if (dmm == UNI_T_UT61D) |
6ac5f892 | 68 | di = di_ut61d; |
fdbcb86d | 69 | else if (dmm == VOLTCRAFT_VC820) |
6ac5f892 | 70 | di = di_vc820; |
8c1adf37 | 71 | sr_dbg("Selected '%s' subdriver.", di->name); |
6ac5f892 | 72 | |
1ebe4b4e | 73 | drvc->sr_ctx = sr_ctx; |
6ac5f892 | 74 | di->priv = drvc; |
79081ec8 UH |
75 | |
76 | return SR_OK; | |
77 | } | |
78 | ||
34f06b90 | 79 | static int hw_init_ut61d(struct sr_context *sr_ctx) |
fdbcb86d | 80 | { |
34f06b90 | 81 | return hw_init(sr_ctx, UNI_T_UT61D); |
fdbcb86d UH |
82 | } |
83 | ||
34f06b90 | 84 | static int hw_init_vc820(struct sr_context *sr_ctx) |
fdbcb86d | 85 | { |
34f06b90 | 86 | return hw_init(sr_ctx, VOLTCRAFT_VC820); |
fdbcb86d UH |
87 | } |
88 | ||
6ac5f892 | 89 | static GSList *hw_scan(GSList *options) |
79081ec8 | 90 | { |
388f9d3e | 91 | GSList *usb_devices, *devices, *l; |
79081ec8 | 92 | struct sr_dev_inst *sdi; |
41d9427f | 93 | struct dev_context *devc; |
79081ec8 | 94 | struct drv_context *drvc; |
388f9d3e UH |
95 | struct sr_usb_dev_inst *usb; |
96 | struct sr_hwopt *opt; | |
41d9427f | 97 | struct sr_probe *probe; |
388f9d3e | 98 | const char *conn; |
79081ec8 UH |
99 | |
100 | (void)options; | |
101 | ||
6ac5f892 | 102 | drvc = di->priv; |
79081ec8 | 103 | |
388f9d3e UH |
104 | /* USB scan is always authoritative. */ |
105 | clear_instances(); | |
106 | ||
107 | conn = NULL; | |
108 | for (l = options; l; l = l->next) { | |
109 | opt = l->data; | |
110 | switch (opt->hwopt) { | |
111 | case SR_HWOPT_CONN: | |
112 | conn = opt->value; | |
113 | break; | |
114 | } | |
115 | } | |
116 | if (!conn) | |
117 | conn = UNI_T_UT_D04_NEW; | |
41d9427f | 118 | |
388f9d3e UH |
119 | devices = NULL; |
120 | if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) { | |
121 | g_slist_free_full(usb_devices, g_free); | |
79081ec8 | 122 | return NULL; |
388f9d3e | 123 | } |
79081ec8 | 124 | |
388f9d3e UH |
125 | for (l = usb_devices; l; l = l->next) { |
126 | usb = l->data; | |
41d9427f UH |
127 | |
128 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
129 | sr_err("Device context malloc failed."); | |
130 | return NULL; | |
131 | } | |
132 | ||
388f9d3e | 133 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, |
41d9427f UH |
134 | di->longname, NULL, NULL))) { |
135 | sr_err("sr_dev_inst_new returned NULL."); | |
136 | return NULL; | |
137 | } | |
138 | sdi->priv = devc; | |
6ac5f892 | 139 | sdi->driver = di; |
41d9427f UH |
140 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) |
141 | return NULL; | |
142 | sdi->probes = g_slist_append(sdi->probes, probe); | |
388f9d3e UH |
143 | |
144 | devc->usb = usb; | |
145 | ||
146 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
41d9427f | 147 | devices = g_slist_append(devices, sdi); |
79081ec8 UH |
148 | } |
149 | ||
150 | return devices; | |
151 | } | |
152 | ||
6ac5f892 | 153 | static GSList *hw_dev_list(void) |
79081ec8 UH |
154 | { |
155 | struct drv_context *drvc; | |
156 | ||
6ac5f892 | 157 | drvc = di->priv; |
79081ec8 UH |
158 | |
159 | return drvc->instances; | |
160 | } | |
161 | ||
162 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
163 | { | |
388f9d3e | 164 | struct drv_context *drvc; |
41d9427f UH |
165 | struct dev_context *devc; |
166 | ||
388f9d3e | 167 | drvc = di->priv; |
41d9427f UH |
168 | devc = sdi->priv; |
169 | ||
388f9d3e | 170 | return sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb); |
79081ec8 UH |
171 | } |
172 | ||
173 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
174 | { | |
175 | (void)sdi; | |
176 | ||
177 | /* TODO */ | |
178 | ||
179 | return SR_OK; | |
180 | } | |
181 | ||
182 | static int hw_cleanup(void) | |
183 | { | |
184 | clear_instances(); | |
185 | ||
79081ec8 UH |
186 | return SR_OK; |
187 | } | |
188 | ||
189 | static int hw_info_get(int info_id, const void **data, | |
190 | const struct sr_dev_inst *sdi) | |
191 | { | |
192 | (void)sdi; | |
193 | ||
194 | sr_spew("Backend requested info_id %d.", info_id); | |
195 | ||
196 | switch (info_id) { | |
73365eae UH |
197 | case SR_DI_HWOPTS: |
198 | *data = hwopts; | |
199 | break; | |
79081ec8 UH |
200 | case SR_DI_HWCAPS: |
201 | *data = hwcaps; | |
202 | sr_spew("%s: Returning hwcaps.", __func__); | |
203 | break; | |
79081ec8 UH |
204 | case SR_DI_SAMPLERATES: |
205 | /* TODO: Get rid of this. */ | |
206 | *data = NULL; | |
207 | sr_spew("%s: Returning samplerates.", __func__); | |
208 | return SR_ERR_ARG; | |
209 | break; | |
210 | case SR_DI_CUR_SAMPLERATE: | |
211 | /* TODO: Get rid of this. */ | |
212 | *data = NULL; | |
213 | sr_spew("%s: Returning current samplerate.", __func__); | |
214 | return SR_ERR_ARG; | |
215 | break; | |
216 | default: | |
79081ec8 UH |
217 | return SR_ERR_ARG; |
218 | break; | |
219 | } | |
220 | ||
221 | return SR_OK; | |
222 | } | |
223 | ||
224 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
225 | const void *value) | |
226 | { | |
227 | struct dev_context *devc; | |
228 | ||
229 | devc = sdi->priv; | |
230 | ||
231 | switch (hwcap) { | |
232 | case SR_HWCAP_LIMIT_MSEC: | |
233 | /* TODO: Not yet implemented. */ | |
234 | if (*(const uint64_t *)value == 0) { | |
235 | sr_err("Time limit cannot be 0."); | |
236 | return SR_ERR; | |
237 | } | |
238 | devc->limit_msec = *(const uint64_t *)value; | |
239 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
240 | devc->limit_msec); | |
241 | break; | |
242 | case SR_HWCAP_LIMIT_SAMPLES: | |
243 | if (*(const uint64_t *)value == 0) { | |
244 | sr_err("Sample limit cannot be 0."); | |
245 | return SR_ERR; | |
246 | } | |
247 | devc->limit_samples = *(const uint64_t *)value; | |
248 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
249 | devc->limit_samples); | |
250 | break; | |
251 | default: | |
252 | sr_err("Unknown capability: %d.", hwcap); | |
253 | return SR_ERR; | |
254 | break; | |
255 | } | |
256 | ||
257 | return SR_OK; | |
258 | } | |
259 | ||
260 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
6ac5f892 | 261 | void *cb_data) |
79081ec8 UH |
262 | { |
263 | struct sr_datafeed_packet packet; | |
264 | struct sr_datafeed_header header; | |
79081ec8 UH |
265 | struct dev_context *devc; |
266 | ||
267 | devc = sdi->priv; | |
268 | ||
269 | sr_dbg("Starting acquisition."); | |
270 | ||
271 | devc->cb_data = cb_data; | |
272 | ||
273 | /* Send header packet to the session bus. */ | |
274 | sr_dbg("Sending SR_DF_HEADER."); | |
275 | packet.type = SR_DF_HEADER; | |
276 | packet.payload = (uint8_t *)&header; | |
277 | header.feed_version = 1; | |
278 | gettimeofday(&header.starttime, NULL); | |
279 | sr_session_send(devc->cb_data, &packet); | |
280 | ||
6ac5f892 | 281 | if (!strcmp(di->name, "uni-t-ut61d")) { |
fdbcb86d UH |
282 | sr_source_add(0, 0, 10 /* poll_timeout */, |
283 | uni_t_ut61d_receive_data, (void *)sdi); | |
6ac5f892 | 284 | } else if (!strcmp(di->name, "voltcraft-vc820")) { |
fdbcb86d UH |
285 | sr_source_add(0, 0, 10 /* poll_timeout */, |
286 | voltcraft_vc820_receive_data, (void *)sdi); | |
287 | } | |
79081ec8 UH |
288 | |
289 | return SR_OK; | |
290 | } | |
291 | ||
69b07d14 | 292 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
79081ec8 UH |
293 | { |
294 | struct sr_datafeed_packet packet; | |
295 | ||
296 | (void)sdi; | |
297 | ||
298 | sr_dbg("Stopping acquisition."); | |
299 | ||
300 | /* Send end packet to the session bus. */ | |
301 | sr_dbg("Sending SR_DF_END."); | |
302 | packet.type = SR_DF_END; | |
303 | sr_session_send(cb_data, &packet); | |
304 | ||
305 | /* TODO? */ | |
306 | sr_source_remove(0); | |
307 | ||
308 | return SR_OK; | |
309 | } | |
310 | ||
fdbcb86d UH |
311 | SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = { |
312 | .name = "uni-t-ut61d", | |
313 | .longname = "UNI-T UT61D", | |
314 | .api_version = 1, | |
315 | .init = hw_init_ut61d, | |
316 | .cleanup = hw_cleanup, | |
6ac5f892 UH |
317 | .scan = hw_scan, |
318 | .dev_list = hw_dev_list, | |
fdbcb86d UH |
319 | .dev_clear = clear_instances, |
320 | .dev_open = hw_dev_open, | |
321 | .dev_close = hw_dev_close, | |
322 | .info_get = hw_info_get, | |
323 | .dev_config_set = hw_dev_config_set, | |
6ac5f892 | 324 | .dev_acquisition_start = hw_dev_acquisition_start, |
fdbcb86d UH |
325 | .dev_acquisition_stop = hw_dev_acquisition_stop, |
326 | .priv = NULL, | |
327 | }; | |
328 | ||
329 | SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = { | |
330 | .name = "voltcraft-vc820", | |
331 | .longname = "Voltcraft VC-820", | |
79081ec8 | 332 | .api_version = 1, |
fdbcb86d | 333 | .init = hw_init_vc820, |
79081ec8 | 334 | .cleanup = hw_cleanup, |
6ac5f892 UH |
335 | .scan = hw_scan, |
336 | .dev_list = hw_dev_list, | |
79081ec8 UH |
337 | .dev_clear = clear_instances, |
338 | .dev_open = hw_dev_open, | |
339 | .dev_close = hw_dev_close, | |
340 | .info_get = hw_info_get, | |
341 | .dev_config_set = hw_dev_config_set, | |
6ac5f892 | 342 | .dev_acquisition_start = hw_dev_acquisition_start, |
79081ec8 UH |
343 | .dev_acquisition_stop = hw_dev_acquisition_stop, |
344 | .priv = NULL, | |
345 | }; |