2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
29 extern struct sr_dev_driver lascar_el_usb_driver_info;
30 static struct sr_dev_driver *di = &lascar_el_usb_driver_info;
32 static const struct elusb_profile profiles[] = {
33 { 1, "EL-USB-1", LOG_UNSUPPORTED },
34 { 2, "EL-USB-1", LOG_UNSUPPORTED },
35 { 3, "EL-USB-2", LOG_TEMP_RH },
36 { 4, "EL-USB-3", LOG_UNSUPPORTED },
37 { 5, "EL-USB-4", LOG_UNSUPPORTED },
38 { 6, "EL-USB-3", LOG_UNSUPPORTED },
39 { 7, "EL-USB-4", LOG_UNSUPPORTED },
40 { 8, "EL-USB-LITE", LOG_UNSUPPORTED },
41 { 9, "EL-USB-CO", LOG_CO },
42 { 10, "EL-USB-TC", LOG_UNSUPPORTED },
43 { 11, "EL-USB-CO300", LOG_CO },
44 { 12, "EL-USB-2-LCD", LOG_TEMP_RH },
45 { 13, "EL-USB-2+", LOG_TEMP_RH },
46 { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED },
47 { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED },
48 { 16, "EL-USB-2-LCD+", LOG_TEMP_RH },
49 { 17, "EL-USB-5", LOG_UNSUPPORTED },
50 { 18, "EL-USB-1-RCG", LOG_UNSUPPORTED },
51 { 19, "EL-USB-1-LCD", LOG_UNSUPPORTED },
52 { 20, "EL-OEM-3", LOG_UNSUPPORTED },
53 { 21, "EL-USB-1-LCD", LOG_UNSUPPORTED },
58 SR_PRIV libusb_device_handle *lascar_open(struct libusb_device *dev)
60 libusb_device_handle *dev_hdl;
63 if ((ret = libusb_open(dev, &dev_hdl)) != 0) {
64 sr_dbg("failed to open device for scan: %s",
65 libusb_error_name(ret));
69 /* Some of these fail, but it needs doing -- some sort of mode
70 * setup for the SILabs F32x. */
71 libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
72 0x00, 0xffff, 0x00, NULL, 0, 50);
73 libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
74 0x02, 0x0002, 0x00, NULL, 0, 50);
75 libusb_control_transfer(dev_hdl, LIBUSB_REQUEST_TYPE_VENDOR,
76 0x02, 0x0001, 0x00, NULL, 0, 50);
81 static void mark_xfer(struct libusb_transfer *xfer)
84 xfer->user_data = GINT_TO_POINTER(1);
88 SR_PRIV int lascar_get_config(libusb_device_handle *dev_hdl,
89 unsigned char *configblock, int *configlen)
91 struct drv_context *drvc;
92 struct libusb_transfer *xfer_in, *xfer_out;
96 unsigned char cmd[3], buf[MAX_CONFIGBLOCK_SIZE];
98 sr_spew("Reading config block.");
103 if (!(xfer_in = libusb_alloc_transfer(0)) ||
104 !(xfer_out = libusb_alloc_transfer(0)))
107 /* Flush anything the F321 still has queued. */
108 while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
109 5) == 0 && buflen > 0)
112 /* Keep a read request waiting in the wings, ready to pounce
113 * the moment the device sends something. */
114 libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
115 buf, 256, mark_xfer, 0, 10000);
116 if (libusb_submit_transfer(xfer_in) != 0)
119 /* Request device configuration structure. */
123 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
124 cmd, 3, mark_xfer, 0, 100);
125 if (libusb_submit_transfer(xfer_out) != 0)
130 start = g_get_monotonic_time();
131 while (!xfer_in->user_data || !xfer_out->user_data) {
132 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
137 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
140 sr_dbg("no response");
143 if (xfer_in->actual_length != 3) {
144 sr_dbg("expected 3-byte header, got %d bytes", xfer_in->actual_length);
148 /* Got configuration structure header. */
149 sr_spew("Response to config request: 0x%.2x 0x%.2x 0x%.2x ",
150 buf[0], buf[1], buf[2]);
151 buflen = buf[1] | (buf[2] << 8);
152 if (buf[0] != 0x02 || buflen > MAX_CONFIGBLOCK_SIZE) {
153 sr_dbg("Invalid response to config request: "
154 "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]);
155 libusb_close(dev_hdl);
159 /* Get configuration structure. */
160 xfer_in->length = buflen;
161 xfer_in->user_data = 0;
162 if (libusb_submit_transfer(xfer_in) != 0)
164 while (!xfer_in->user_data) {
165 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
170 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
173 sr_dbg("Timeout waiting for configuration structure.");
176 if (xfer_in->actual_length != buflen) {
177 sr_dbg("expected %d-byte structure, got %d bytes", buflen,
178 xfer_in->actual_length);
182 memcpy(configblock, buf, buflen);
186 if (!xfer_in->user_data || !xfer_in->user_data) {
187 if (!xfer_in->user_data)
188 libusb_cancel_transfer(xfer_in);
189 if (!xfer_out->user_data)
190 libusb_cancel_transfer(xfer_out);
191 start = g_get_monotonic_time();
192 while (!xfer_in->user_data || !xfer_out->user_data) {
193 if (g_get_monotonic_time() - start > 10000)
196 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
199 libusb_free_transfer(xfer_in);
200 libusb_free_transfer(xfer_out);
202 return *configlen ? SR_OK : SR_ERR;
205 SR_PRIV int lascar_save_config(libusb_device_handle *dev_hdl,
206 unsigned char *config, int configlen)
208 struct drv_context *drvc;
209 struct libusb_transfer *xfer_in, *xfer_out;
213 unsigned char cmd[3], buf[256];
215 sr_spew("Writing config block.");
219 if (!(xfer_in = libusb_alloc_transfer(0)) ||
220 !(xfer_out = libusb_alloc_transfer(0)))
223 /* Flush anything the F321 still has queued. */
224 while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
225 5) == 0 && buflen > 0)
229 /* Keep a read request waiting in the wings, ready to pounce
230 * the moment the device sends something. */
231 libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
232 buf, 256, mark_xfer, 0, 10000);
233 if (libusb_submit_transfer(xfer_in) != 0) {
238 /* Request device configuration structure. */
240 cmd[1] = configlen & 0xff;
241 cmd[2] = (configlen >> 8) & 0xff;
242 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
243 cmd, 3, mark_xfer, 0, 100);
244 if (libusb_submit_transfer(xfer_out) != 0) {
250 while (!xfer_out->user_data) {
252 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
255 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
256 config, configlen, mark_xfer, 0, 100);
257 if (libusb_submit_transfer(xfer_out) != 0) {
261 while (!xfer_in->user_data || !xfer_out->user_data) {
263 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
266 if (xfer_in->actual_length != 1 || buf[0] != 0xff) {
267 sr_dbg("unexpected response after transfer");
272 if (!xfer_in->user_data || !xfer_in->user_data) {
273 if (!xfer_in->user_data)
274 libusb_cancel_transfer(xfer_in);
275 if (!xfer_out->user_data)
276 libusb_cancel_transfer(xfer_out);
277 start = g_get_monotonic_time();
278 while (!xfer_in->user_data || !xfer_out->user_data) {
279 if (g_get_monotonic_time() - start > 10000)
282 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
285 libusb_free_transfer(xfer_in);
286 libusb_free_transfer(xfer_out);
291 static struct sr_dev_inst *lascar_identify(unsigned char *config)
293 struct dev_context *devc;
294 const struct elusb_profile *profile;
295 struct sr_dev_inst *sdi;
296 struct sr_probe *probe;
304 for (i = 0; profiles[i].modelid; i++) {
305 if (profiles[i].modelid == modelid) {
306 profile = &profiles[i];
311 sr_dbg("unknown EL-USB modelid %d", modelid);
315 i = config[52] | (config[53] << 8);
316 memcpy(firmware, config + 0x30, 4);
318 sr_dbg("found %s with firmware version %s serial %d",
319 profile->modelname, firmware, i);
321 if (profile->logformat == LOG_UNSUPPORTED) {
322 sr_dbg("unsupported EL-USB logformat for %s", profile->modelname);
326 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, LASCAR_VENDOR,
327 profile->modelname, firmware)))
331 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
333 sdi->probes = g_slist_append(NULL, probe);
335 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
338 devc->profile = profile;
344 SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
346 struct drv_context *drvc;
347 struct sr_dev_inst *sdi;
348 struct libusb_device **devlist;
349 struct libusb_device_descriptor des;
350 libusb_device_handle *dev_hdl;
352 unsigned char config[MAX_CONFIGBLOCK_SIZE];
357 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
358 for (i = 0; devlist[i]; i++) {
359 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
360 sr_err("Failed to get device descriptor: %d.", ret);
364 if (libusb_get_bus_number(devlist[i]) != bus ||
365 libusb_get_device_address(devlist[i]) != address)
368 if (!(dev_hdl = lascar_open(devlist[i])))
371 if (lascar_get_config(dev_hdl, config, &dummy) != SR_OK)
374 libusb_close(dev_hdl);
375 sdi = lascar_identify(config);
381 static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
384 struct dev_context *devc;
385 struct sr_datafeed_packet packet;
386 struct sr_datafeed_analog analog;
389 int samples, samples_left, i, j;
392 analog.probes = sdi->probes;
394 samples = buflen / devc->sample_size;
395 samples_left = devc->logged_samples - devc->rcvd_samples;
396 if (samples_left < samples)
397 samples = samples_left;
398 switch (devc->profile->logformat) {
400 packet.type = SR_DF_ANALOG;
401 packet.payload = &analog;
403 if (!(temp = g_try_malloc(sizeof(float) * samples)))
405 if (!(rh = g_try_malloc(sizeof(float) * samples)))
407 for (i = 0, j = 0; i < samples; i++) {
408 /* Both Celcius and Fahrenheit stored at base -40. */
409 if (devc->temp_unit == 0)
410 /* Celcius is stored in half-degree increments. */
411 temp[j] = buf[i * 2] / 2 - 40;
413 temp[j] = buf[i * 2] - 40;
415 rh[j] = buf[i * 2 + 1] / 2;
417 if (temp[j] == 0.0 && rh[j] == 0.0)
418 /* Skip invalid measurement. */
422 analog.num_samples = j;
424 analog.mq = SR_MQ_TEMPERATURE;
425 if (devc->temp_unit == 1)
426 analog.unit = SR_UNIT_FAHRENHEIT;
428 analog.unit = SR_UNIT_CELSIUS;
430 sr_session_send(devc->cb_data, &packet);
432 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
433 analog.unit = SR_UNIT_PERCENTAGE;
435 sr_session_send(devc->cb_data, &packet);
440 packet.type = SR_DF_ANALOG;
441 packet.payload = &analog;
442 analog.num_samples = samples;
443 analog.mq = SR_MQ_CARBON_MONOXIDE;
444 analog.unit = SR_UNIT_CONCENTRATION;
446 if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
448 for (i = 0; i < samples; i++) {
449 s = (buf[i * 2] << 8) | buf[i * 2 + 1];
450 analog.data[i] = (s * devc->co_high + devc->co_low) / 1000000;
451 if (analog.data[i] < 0.0)
452 analog.data[i] = 0.0;
454 sr_session_send(devc->cb_data, &packet);
458 /* How did we even get this far? */
461 devc->rcvd_samples += samples;
465 SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
467 struct dev_context *devc;
468 struct drv_context *drvc = di->priv;
469 struct sr_datafeed_packet packet;
470 struct sr_dev_inst *sdi;
480 if (sdi->status == SR_ST_STOPPING) {
481 for (i = 0; devc->usbfd[i] != -1; i++)
482 sr_source_remove(devc->usbfd[i]);
484 sdi->driver->dev_close(sdi);
486 packet.type = SR_DF_END;
487 sr_session_send(cb_data, &packet);
490 memset(&tv, 0, sizeof(struct timeval));
491 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
497 SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
499 struct dev_context *devc;
500 struct sr_dev_inst *sdi;
502 gboolean packet_has_error;
504 sdi = transfer->user_data;
507 packet_has_error = FALSE;
508 switch (transfer->status) {
509 case LIBUSB_TRANSFER_NO_DEVICE:
510 /* USB device was unplugged. */
511 hw_dev_acquisition_stop(sdi, sdi);
513 case LIBUSB_TRANSFER_COMPLETED:
514 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
517 packet_has_error = TRUE;
521 if (!packet_has_error) {
522 if (devc->rcvd_samples < devc->logged_samples)
523 lascar_el_usb_dispatch(sdi, transfer->buffer,
524 transfer->actual_length);
525 devc->rcvd_bytes += transfer->actual_length;
526 sr_spew("received %d/%d bytes (%d/%d samples)",
527 devc->rcvd_bytes, devc->log_size,
528 devc->rcvd_samples, devc->logged_samples);
529 if (devc->rcvd_bytes >= devc->log_size)
530 hw_dev_acquisition_stop(sdi, sdi);
533 if (sdi->status == SR_ST_ACTIVE) {
534 /* Send the same request again. */
535 if ((ret = libusb_submit_transfer(transfer) != 0)) {
536 sr_err("Unable to resubmit transfer: %s.",
537 libusb_error_name(ret));
538 g_free(transfer->buffer);
539 libusb_free_transfer(transfer);
540 hw_dev_acquisition_stop(sdi, sdi);
543 /* This was the last transfer we're going to receive, so
545 g_free(transfer->buffer);
546 libusb_free_transfer(transfer);
551 static int get_flags(unsigned char *configblock)
555 flags = (configblock[32] | (configblock[33] << 8)) & 0x1fff;
556 sr_spew("Read flags (0x%.4x).", flags);
561 static int set_flags(unsigned char *configblock, int flags)
564 sr_spew("Setting flags to 0x%.4x.", flags);
565 configblock[32] = flags & 0xff;
566 configblock[33] = (flags >> 8) & 0x1f;
571 SR_PRIV int lascar_is_logging(const struct sr_dev_inst *sdi)
573 struct dev_context *devc;
574 int dummy, flags, ret;
577 if (lascar_get_config(devc->usb->devhdl, devc->config, &dummy) != SR_OK)
580 flags = get_flags(devc->config);
589 SR_PRIV int lascar_start_logging(const struct sr_dev_inst *sdi)
591 struct dev_context *devc;
595 if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK)
598 /* Turn on logging. */
599 flags = get_flags(devc->config);
601 set_flags(devc->config, flags);
603 /* Start logging in 0 seconds. */
604 memset(devc->config + 24, 0, 4);
606 ret = lascar_save_config(devc->usb->devhdl, devc->config, len);
607 sr_info("Started internal logging.");
612 SR_PRIV int lascar_stop_logging(const struct sr_dev_inst *sdi)
614 struct dev_context *devc;
618 if (lascar_get_config(devc->usb->devhdl, devc->config, &len) != SR_OK)
621 flags = get_flags(devc->config);
623 set_flags(devc->config, flags);
625 ret = lascar_save_config(devc->usb->devhdl, devc->config, len);
626 sr_info("Stopped internal logging.");