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