]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/lascar-el-usb/api.c
hantek-dso: Consistently return SR_ERR_ARG if sdi is needed
[libsigrok.git] / hardware / lascar-el-usb / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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 <glib.h>
21#include <libusb.h>
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
24#include "protocol.h"
25
26SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address);
27SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
28static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
29static int hw_dev_close(struct sr_dev_inst *sdi);
30
31static const int32_t hwopts[] = {
32 SR_CONF_CONN,
33};
34
35static const int32_t hwcaps[] = {
36 SR_CONF_THERMOMETER,
37 SR_CONF_HYGROMETER,
38 SR_CONF_DATALOG,
39 SR_CONF_LIMIT_SAMPLES,
40};
41
42/* Properly close and free all devices. */
43static int clear_instances(void)
44{
45 struct sr_dev_inst *sdi;
46 struct drv_context *drvc;
47 struct dev_context *devc;
48 GSList *l;
49
50 if (!(drvc = di->priv))
51 return SR_OK;
52
53 for (l = drvc->instances; l; l = l->next) {
54 if (!(sdi = l->data))
55 continue;
56 if (!(devc = sdi->priv))
57 continue;
58
59 hw_dev_close(sdi);
60 sr_usb_dev_inst_free(devc->usb);
61 sr_dev_inst_free(sdi);
62 }
63
64 g_slist_free(drvc->instances);
65 drvc->instances = NULL;
66
67 return SR_OK;
68}
69
70static int hw_init(struct sr_context *sr_ctx)
71{
72 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
73}
74
75static GSList *hw_scan(GSList *options)
76{
77 struct drv_context *drvc;
78 struct dev_context *devc;
79 struct sr_dev_inst *sdi;
80 struct sr_usb_dev_inst *usb;
81 struct sr_config *src;
82 GSList *usb_devices, *devices, *l;
83 const char *conn;
84
85 (void)options;
86
87 drvc = di->priv;
88
89 /* USB scan is always authoritative. */
90 clear_instances();
91
92 conn = NULL;
93 for (l = options; l; l = l->next) {
94 src = l->data;
95 switch (src->key) {
96 case SR_CONF_CONN:
97 conn = g_variant_get_string(src->data, NULL);
98 break;
99 }
100 }
101 if (!conn)
102 return NULL;
103
104 devices = NULL;
105 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
106 /* We have a list of sr_usb_dev_inst matching the connection
107 * string. Wrap them in sr_dev_inst and we're done. */
108 for (l = usb_devices; l; l = l->next) {
109 usb = l->data;
110 if (!(sdi = lascar_scan(usb->bus, usb->address))) {
111 /* Not a Lascar EL-USB. */
112 g_free(usb);
113 continue;
114 }
115 devc = sdi->priv;
116 devc->usb = usb;
117 drvc->instances = g_slist_append(drvc->instances, sdi);
118 devices = g_slist_append(devices, sdi);
119 }
120 g_slist_free(usb_devices);
121 } else
122 g_slist_free_full(usb_devices, g_free);
123
124 return devices;
125}
126
127static GSList *hw_dev_list(void)
128{
129 return ((struct drv_context *)(di->priv))->instances;
130}
131
132static int hw_dev_open(struct sr_dev_inst *sdi)
133{
134 struct drv_context *drvc;
135 struct dev_context *devc;
136 int ret;
137
138 if (!(drvc = di->priv)) {
139 sr_err("Driver was not initialized.");
140 return SR_ERR;
141 }
142
143 devc = sdi->priv;
144 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, devc->usb) != SR_OK)
145 return SR_ERR;
146
147 if ((ret = libusb_claim_interface(devc->usb->devhdl, LASCAR_INTERFACE))) {
148 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
149 return SR_ERR;
150 }
151 sdi->status = SR_ST_ACTIVE;
152
153 return ret;
154}
155
156static int hw_dev_close(struct sr_dev_inst *sdi)
157{
158 struct dev_context *devc;
159
160 if (!di->priv) {
161 sr_err("Driver was not initialized.");
162 return SR_ERR;
163 }
164
165 devc = sdi->priv;
166 if (!devc->usb->devhdl)
167 /* Nothing to do. */
168 return SR_OK;
169
170 libusb_release_interface(devc->usb->devhdl, LASCAR_INTERFACE);
171 libusb_close(devc->usb->devhdl);
172 devc->usb->devhdl = NULL;
173 sdi->status = SR_ST_INACTIVE;
174
175
176 return SR_OK;
177}
178
179static int hw_cleanup(void)
180{
181 struct drv_context *drvc;
182
183 if (!(drvc = di->priv))
184 /* Can get called on an unused driver, doesn't matter. */
185 return SR_OK;
186
187 clear_instances();
188 g_free(drvc);
189 di->priv = NULL;
190
191 return SR_OK;
192}
193
194static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
195{
196 struct dev_context *devc;
197 int ret;
198
199 devc = sdi->priv;
200 switch (id) {
201 case SR_CONF_DATALOG:
202 if (!sdi)
203 return SR_ERR_ARG;
204 if ((ret = lascar_is_logging(sdi)) == -1)
205 return SR_ERR;
206 *data = g_variant_new_boolean(ret ? TRUE : FALSE);
207 break;
208 case SR_CONF_LIMIT_SAMPLES:
209 *data = g_variant_new_uint64(devc->limit_samples);
210 break;
211 default:
212 return SR_ERR_NA;
213 }
214
215 return SR_OK;
216}
217
218static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
219{
220 struct dev_context *devc;
221 int ret;
222
223 if (!di->priv) {
224 sr_err("Driver was not initialized.");
225 return SR_ERR;
226 }
227 if (sdi->status != SR_ST_ACTIVE) {
228 sr_err("Device inactive, can't set config options.");
229 return SR_ERR;
230 }
231
232 devc = sdi->priv;
233 ret = SR_OK;
234 switch (id) {
235 case SR_CONF_DATALOG:
236 if (g_variant_get_boolean(data)) {
237 /* Start logging. */
238 ret = lascar_start_logging(sdi);
239 } else {
240 /* Stop logging. */
241 ret = lascar_stop_logging(sdi);
242 }
243 break;
244 case SR_CONF_LIMIT_SAMPLES:
245 devc->limit_samples = g_variant_get_uint64(data);
246 sr_dbg("Setting sample limit to %" PRIu64 ".",
247 devc->limit_samples);
248 break;
249 default:
250 ret = SR_ERR_NA;
251 }
252
253 return ret;
254}
255
256static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
257{
258
259 (void)sdi;
260
261 switch (key) {
262 case SR_CONF_SCAN_OPTIONS:
263 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
264 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
265 break;
266 case SR_CONF_DEVICE_OPTIONS:
267 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
268 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
269 break;
270 default:
271 return SR_ERR_NA;
272 }
273
274 return SR_OK;
275}
276
277static void mark_xfer(struct libusb_transfer *xfer)
278{
279
280 if (xfer->status == LIBUSB_TRANSFER_COMPLETED)
281 xfer->user_data = GINT_TO_POINTER(1);
282 else
283 xfer->user_data = GINT_TO_POINTER(-1);
284
285}
286
287/* The Lascar software, in its infinite ignorance, reads a set of four
288 * bytes from the device config struct and interprets it as a float.
289 * That only works because they only use windows, and only on x86. However
290 * we may be running on any architecture, any operating system. So we have
291 * to convert these four bytes as the Lascar software would on windows/x86,
292 * to the local representation of a float.
293 * The source format is little-endian, with IEEE 754-2008 BINARY32 encoding. */
294static float binary32_le_to_float(unsigned char *buf)
295{
296 GFloatIEEE754 f;
297
298 f.v_float = 0;
299 f.mpn.sign = (buf[3] & 0x80) ? 1 : 0;
300 f.mpn.biased_exponent = (buf[3] << 1) | (buf[2] >> 7);
301 f.mpn.mantissa = buf[0] | (buf[1] << 8) | ((buf[2] & 0x7f) << 16);
302
303 return f.v_float;
304}
305
306static int lascar_proc_config(const struct sr_dev_inst *sdi)
307{
308 struct dev_context *devc;
309 int dummy, ret;
310
311 devc = sdi->priv;
312 if (lascar_get_config(devc->usb->devhdl, devc->config, &dummy) != SR_OK)
313 return SR_ERR;
314
315 ret = SR_OK;
316 switch (devc->profile->logformat) {
317 case LOG_TEMP_RH:
318 devc->sample_size = 2;
319 devc->temp_unit = devc->config[0x2e] | (devc->config[0x2f] << 8);
320 if (devc->temp_unit != 0 && devc->temp_unit != 1) {
321 sr_dbg("invalid temperature unit %d", devc->temp_unit);
322 /* Default to Celcius, we're all adults here. */
323 devc->temp_unit = 0;
324 } else
325 sr_dbg("temperature unit is %s", devc->temp_unit
326 ? "Fahrenheit" : "Celcius");
327 break;
328 case LOG_CO:
329 devc->sample_size = 2;
330 devc->co_high = binary32_le_to_float(devc->config + 0x24);
331 devc->co_low = binary32_le_to_float(devc->config + 0x28);
332 sr_dbg("EL-USB-CO calibration high %f low %f", devc->co_high,
333 devc->co_low);
334 break;
335 default:
336 ret = SR_ERR_ARG;
337 }
338 devc->logged_samples = devc->config[0x1e] | (devc->config[0x1f] << 8);
339 sr_dbg("device log contains %d samples.", devc->logged_samples);
340
341 return ret;
342}
343
344static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
345 void *cb_data)
346{
347 struct sr_datafeed_packet packet;
348 struct sr_datafeed_meta meta;
349 struct sr_config *src;
350 struct dev_context *devc;
351 struct drv_context *drvc;
352 struct libusb_transfer *xfer_in, *xfer_out;
353 const struct libusb_pollfd **pfd;
354 struct timeval tv;
355 uint64_t interval;
356 int ret, i;
357 unsigned char cmd[3], resp[4], *buf;
358
359 if (!di->priv) {
360 sr_err("Driver was not initialized.");
361 return SR_ERR;
362 }
363
364 drvc = di->priv;
365 devc = sdi->priv;
366 devc->cb_data = cb_data;
367
368 if (lascar_proc_config(sdi) != SR_OK)
369 return SR_ERR;
370
371 sr_dbg("Starting log retrieval.");
372
373 /* Send header packet to the session bus. */
374 std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
375
376 interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
377 packet.type = SR_DF_META;
378 packet.payload = &meta;
379 src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval));
380 meta.config = g_slist_append(NULL, src);
381 sr_session_send(devc->cb_data, &packet);
382 g_free(src);
383
384 if (devc->logged_samples == 0) {
385 /* This ensures the frontend knows the session is done. */
386 packet.type = SR_DF_END;
387 sr_session_send(devc->cb_data, &packet);
388 return SR_OK;
389 }
390
391 if (!(xfer_in = libusb_alloc_transfer(0)) ||
392 !(xfer_out = libusb_alloc_transfer(0)))
393 return SR_ERR;
394
395 libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
396 0x00, 0xffff, 0x00, NULL, 0, 50);
397 libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
398 0x02, 0x0002, 0x00, NULL, 0, 50);
399 libusb_control_transfer(devc->usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
400 0x02, 0x0001, 0x00, NULL, 0, 50);
401
402
403 /* Flush input. The F321 requires this. */
404 while (libusb_bulk_transfer(devc->usb->devhdl, LASCAR_EP_IN, resp,
405 256, &ret, 5) == 0 && ret > 0)
406 ;
407
408 libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN,
409 resp, sizeof(resp), mark_xfer, 0, 10000);
410 if (libusb_submit_transfer(xfer_in) != 0) {
411 libusb_free_transfer(xfer_in);
412 libusb_free_transfer(xfer_out);
413 return SR_ERR;
414 }
415
416 cmd[0] = 0x03;
417 cmd[1] = 0xff;
418 cmd[2] = 0xff;
419 libusb_fill_bulk_transfer(xfer_out, devc->usb->devhdl, LASCAR_EP_OUT,
420 cmd, 3, mark_xfer, 0, 100);
421 if (libusb_submit_transfer(xfer_out) != 0) {
422 libusb_free_transfer(xfer_in);
423 libusb_free_transfer(xfer_out);
424 return SR_ERR;
425 }
426
427 tv.tv_sec = 0;
428 tv.tv_usec = 0;
429 while (!xfer_in->user_data || !xfer_out->user_data) {
430 g_usleep(5000);
431 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
432 }
433 if (xfer_in->user_data != GINT_TO_POINTER(1) ||
434 xfer_in->user_data != GINT_TO_POINTER(1)) {
435 sr_dbg("no response to log transfer request");
436 libusb_free_transfer(xfer_in);
437 libusb_free_transfer(xfer_out);
438 return SR_ERR;
439 }
440 if (xfer_in->actual_length != 3 || xfer_in->buffer[0] != 2) {
441 sr_dbg("invalid response to log transfer request");
442 libusb_free_transfer(xfer_in);
443 libusb_free_transfer(xfer_out);
444 return SR_ERR;
445 }
446 devc->log_size = xfer_in->buffer[1] + (xfer_in->buffer[2] << 8);
447 libusb_free_transfer(xfer_out);
448
449 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
450 for (i = 0; pfd[i]; i++) {
451 /* Handle USB events every 100ms, for decent latency. */
452 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
453 lascar_el_usb_handle_events, (void *)sdi);
454 /* We'll need to remove this fd later. */
455 devc->usbfd[i] = pfd[i]->fd;
456 }
457 devc->usbfd[i] = -1;
458
459 buf = g_try_malloc(4096);
460 libusb_fill_bulk_transfer(xfer_in, devc->usb->devhdl, LASCAR_EP_IN,
461 buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100);
462 if ((ret = libusb_submit_transfer(xfer_in) != 0)) {
463 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
464 libusb_free_transfer(xfer_in);
465 g_free(buf);
466 return SR_ERR;
467 }
468
469 return SR_OK;
470}
471
472SR_PRIV int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
473{
474 (void)cb_data;
475
476 if (!di->priv) {
477 sr_err("Driver was not initialized.");
478 return SR_ERR;
479 }
480
481 if (sdi->status != SR_ST_ACTIVE) {
482 sr_err("Device inactive, can't stop acquisition.");
483 return SR_ERR;
484 }
485
486 sdi->status = SR_ST_STOPPING;
487 /* TODO: free ongoing transfers? */
488
489 return SR_OK;
490}
491
492SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
493 .name = "lascar-el-usb",
494 .longname = "Lascar EL-USB",
495 .api_version = 1,
496 .init = hw_init,
497 .cleanup = hw_cleanup,
498 .scan = hw_scan,
499 .dev_list = hw_dev_list,
500 .dev_clear = clear_instances,
501 .config_get = config_get,
502 .config_set = config_set,
503 .config_list = config_list,
504 .dev_open = hw_dev_open,
505 .dev_close = hw_dev_close,
506 .dev_acquisition_start = hw_dev_acquisition_start,
507 .dev_acquisition_stop = hw_dev_acquisition_stop,
508 .priv = NULL,
509};