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