]> sigrok.org Git - libsigrok.git/blame - hardware/lascar-el-usb/api.c
Use consistent API callback function names.
[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;
6078d2c9 29static int 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
6078d2c9 59 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
6078d2c9 70static int init(struct sr_context *sr_ctx)
46697e38 71{
29a27196 72 return std_hw_init(sr_ctx, di, LOG_PREFIX);
46697e38
BV
73}
74
6078d2c9 75static GSList *scan(GSList *options)
46697e38
BV
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
6078d2c9 121static GSList *dev_list(void)
46697e38 122{
0e94d524 123 return ((struct drv_context *)(di->priv))->instances;
46697e38
BV
124}
125
6078d2c9 126static int dev_open(struct sr_dev_inst *sdi)
46697e38 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
6078d2c9 151static int dev_close(struct sr_dev_inst *sdi)
46697e38 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
6078d2c9 174static int cleanup(void)
46697e38 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
6078d2c9 350static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
46697e38 351{
6aa1eb4e 352 struct sr_datafeed_packet packet;
8e77bc20
BV
353 struct sr_datafeed_meta meta;
354 struct sr_config *src;
6aa1eb4e 355 struct dev_context *devc;
8e77bc20 356 struct drv_context *drvc;
0f150649 357 struct sr_usb_dev_inst *usb;
6aa1eb4e
BV
358 struct libusb_transfer *xfer_in, *xfer_out;
359 const struct libusb_pollfd **pfd;
360 struct timeval tv;
8e77bc20 361 uint64_t interval;
6aa1eb4e
BV
362 int ret, i;
363 unsigned char cmd[3], resp[4], *buf;
364
e73ffd42
BV
365 if (sdi->status != SR_ST_ACTIVE)
366 return SR_ERR_DEV_CLOSED;
367
6aa1eb4e
BV
368 if (!di->priv) {
369 sr_err("Driver was not initialized.");
370 return SR_ERR;
371 }
372
8e77bc20 373 drvc = di->priv;
6aa1eb4e 374 devc = sdi->priv;
0f150649 375 usb = sdi->conn;
6aa1eb4e
BV
376 devc->cb_data = cb_data;
377
378 if (lascar_proc_config(sdi) != SR_OK)
379 return SR_ERR;
380
381 sr_dbg("Starting log retrieval.");
382
383 /* Send header packet to the session bus. */
29a27196 384 std_session_send_df_header(cb_data, LOG_PREFIX);
6aa1eb4e 385
8e77bc20
BV
386 interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
387 packet.type = SR_DF_META;
388 packet.payload = &meta;
7faf69da 389 src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval));
8e77bc20
BV
390 meta.config = g_slist_append(NULL, src);
391 sr_session_send(devc->cb_data, &packet);
392 g_free(src);
393
6aa1eb4e
BV
394 if (devc->logged_samples == 0) {
395 /* This ensures the frontend knows the session is done. */
396 packet.type = SR_DF_END;
397 sr_session_send(devc->cb_data, &packet);
398 return SR_OK;
399 }
400
401 if (!(xfer_in = libusb_alloc_transfer(0)) ||
402 !(xfer_out = libusb_alloc_transfer(0)))
403 return SR_ERR;
404
0f150649 405 libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
6aa1eb4e 406 0x00, 0xffff, 0x00, NULL, 0, 50);
0f150649 407 libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
6aa1eb4e 408 0x02, 0x0002, 0x00, NULL, 0, 50);
0f150649 409 libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
6aa1eb4e
BV
410 0x02, 0x0001, 0x00, NULL, 0, 50);
411
412
413 /* Flush input. The F321 requires this. */
0f150649 414 while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp,
6aa1eb4e
BV
415 256, &ret, 5) == 0 && ret > 0)
416 ;
417
0f150649 418 libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
6aa1eb4e
BV
419 resp, sizeof(resp), mark_xfer, 0, 10000);
420 if (libusb_submit_transfer(xfer_in) != 0) {
421 libusb_free_transfer(xfer_in);
422 libusb_free_transfer(xfer_out);
423 return SR_ERR;
424 }
425
426 cmd[0] = 0x03;
427 cmd[1] = 0xff;
428 cmd[2] = 0xff;
0f150649 429 libusb_fill_bulk_transfer(xfer_out, usb->devhdl, LASCAR_EP_OUT,
6aa1eb4e
BV
430 cmd, 3, mark_xfer, 0, 100);
431 if (libusb_submit_transfer(xfer_out) != 0) {
432 libusb_free_transfer(xfer_in);
433 libusb_free_transfer(xfer_out);
434 return SR_ERR;
435 }
436
437 tv.tv_sec = 0;
438 tv.tv_usec = 0;
439 while (!xfer_in->user_data || !xfer_out->user_data) {
440 g_usleep(5000);
441 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
442 }
443 if (xfer_in->user_data != GINT_TO_POINTER(1) ||
444 xfer_in->user_data != GINT_TO_POINTER(1)) {
445 sr_dbg("no response to log transfer request");
446 libusb_free_transfer(xfer_in);
447 libusb_free_transfer(xfer_out);
448 return SR_ERR;
449 }
450 if (xfer_in->actual_length != 3 || xfer_in->buffer[0] != 2) {
451 sr_dbg("invalid response to log transfer request");
452 libusb_free_transfer(xfer_in);
453 libusb_free_transfer(xfer_out);
454 return SR_ERR;
455 }
456 devc->log_size = xfer_in->buffer[1] + (xfer_in->buffer[2] << 8);
457 libusb_free_transfer(xfer_out);
458
6aa1eb4e
BV
459 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
460 for (i = 0; pfd[i]; i++) {
461 /* Handle USB events every 100ms, for decent latency. */
462 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
463 lascar_el_usb_handle_events, (void *)sdi);
464 /* We'll need to remove this fd later. */
465 devc->usbfd[i] = pfd[i]->fd;
466 }
467 devc->usbfd[i] = -1;
468
469 buf = g_try_malloc(4096);
0f150649 470 libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
6aa1eb4e
BV
471 buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100);
472 if ((ret = libusb_submit_transfer(xfer_in) != 0)) {
473 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
474 libusb_free_transfer(xfer_in);
475 g_free(buf);
476 return SR_ERR;
477 }
46697e38
BV
478
479 return SR_OK;
480}
481
6078d2c9 482SR_PRIV int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
46697e38
BV
483{
484 (void)cb_data;
485
6aa1eb4e
BV
486 if (!di->priv) {
487 sr_err("Driver was not initialized.");
488 return SR_ERR;
489 }
490
46697e38
BV
491 if (sdi->status != SR_ST_ACTIVE) {
492 sr_err("Device inactive, can't stop acquisition.");
493 return SR_ERR;
494 }
495
6aa1eb4e
BV
496 sdi->status = SR_ST_STOPPING;
497 /* TODO: free ongoing transfers? */
46697e38
BV
498
499 return SR_OK;
500}
501
502SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
503 .name = "lascar-el-usb",
504 .longname = "Lascar EL-USB",
505 .api_version = 1,
6078d2c9
UH
506 .init = init,
507 .cleanup = cleanup,
508 .scan = scan,
509 .dev_list = dev_list,
46697e38 510 .dev_clear = clear_instances,
361d1511 511 .config_get = config_get,
035a1078 512 .config_set = config_set,
a1c743fc 513 .config_list = config_list,
6078d2c9
UH
514 .dev_open = dev_open,
515 .dev_close = dev_close,
516 .dev_acquisition_start = dev_acquisition_start,
517 .dev_acquisition_stop = dev_acquisition_stop,
46697e38
BV
518 .priv = NULL,
519};