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_UNSUPPORTED },
45 { 13, "EL-USB-2+", LOG_UNSUPPORTED },
46 { 14, "EL-USB-1-PRO", LOG_UNSUPPORTED },
47 { 15, "EL-USB-TC-LCD", LOG_UNSUPPORTED },
48 { 16, "EL-USB-2-LCD+", LOG_UNSUPPORTED },
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 unsigned char *lascar_get_config(libusb_device_handle *dev_hdl)
90 struct drv_context *drvc;
91 struct libusb_transfer *xfer_in, *xfer_out;
95 unsigned char cmd[3], buf[256], *config;
100 if (!(xfer_in = libusb_alloc_transfer(0)) ||
101 !(xfer_out = libusb_alloc_transfer(0)))
104 /* Flush anything the F321 still has queued. */
105 while (libusb_bulk_transfer(dev_hdl, LASCAR_EP_IN, buf, 256, &buflen,
106 5) == 0 && buflen > 0)
109 /* Keep a read request waiting in the wings, ready to pounce
110 * the moment the device sends something. */
111 libusb_fill_bulk_transfer(xfer_in, dev_hdl, LASCAR_EP_IN,
112 buf, 256, mark_xfer, 0, 10000);
113 if (libusb_submit_transfer(xfer_in) != 0)
116 /* Request device configuration structure. */
120 libusb_fill_bulk_transfer(xfer_out, dev_hdl, LASCAR_EP_OUT,
121 cmd, 3, mark_xfer, 0, 100);
122 if (libusb_submit_transfer(xfer_out) != 0)
127 start = g_get_monotonic_time();
128 while (!xfer_in->user_data || !xfer_out->user_data) {
129 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
134 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
137 sr_dbg("no response");
140 if (xfer_in->actual_length != 3) {
141 sr_dbg("expected 3-byte header, got %d bytes", xfer_in->actual_length);
145 /* Got configuration structure header. */
146 sr_dbg("response to config request: 0x%.2x 0x%.2x 0x%.2x ",
147 buf[0], buf[1], buf[2]);
148 buflen = buf[1] | (buf[2] << 8);
149 if (buf[0] != 0x02 || buflen > 256) {
150 sr_dbg("Invalid response to config request: "
151 "0x%.2x 0x%.2x 0x%.2x ", buf[0], buf[1], buf[2]);
152 libusb_close(dev_hdl);
156 /* Get configuration structure. */
157 xfer_in->length = buflen;
158 xfer_in->user_data = 0;
159 if (libusb_submit_transfer(xfer_in) != 0)
161 while (!xfer_in->user_data) {
162 if (g_get_monotonic_time() - start > SCAN_TIMEOUT) {
167 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
170 sr_dbg("Timeout waiting for configuration structure.");
173 if (xfer_in->actual_length != buflen) {
174 sr_dbg("expected %d-byte structure, got %d bytes", buflen,
175 xfer_in->actual_length);
179 if (!(config = g_try_malloc(256)))
181 memcpy(config, buf, buflen);
184 if (!xfer_in->user_data || !xfer_in->user_data) {
185 if (!xfer_in->user_data)
186 libusb_cancel_transfer(xfer_in);
187 if (!xfer_out->user_data)
188 libusb_cancel_transfer(xfer_out);
189 start = g_get_monotonic_time();
190 while (!xfer_in->user_data || !xfer_out->user_data) {
191 if (g_get_monotonic_time() - start > 10000)
194 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
197 libusb_free_transfer(xfer_in);
198 libusb_free_transfer(xfer_out);
203 static struct sr_dev_inst *lascar_identify(unsigned char *config)
205 struct dev_context *devc;
206 const struct elusb_profile *profile;
207 struct sr_dev_inst *sdi;
208 struct sr_probe *probe;
216 for (i = 0; profiles[i].modelid; i++) {
217 if (profiles[i].modelid == modelid) {
218 profile = &profiles[i];
223 sr_dbg("unknown EL-USB modelid %d", modelid);
227 i = config[52] | (config[53] << 8);
228 memcpy(firmware, config + 0x30, 4);
230 sr_dbg("found %s with firmware version %s serial %d",
231 profile->modelname, firmware, i);
233 if (profile->logformat == LOG_UNSUPPORTED) {
234 sr_dbg("unsupported EL-USB logformat for %s", profile->modelname);
238 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, LASCAR_VENDOR,
239 profile->modelname, firmware)))
243 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
245 sdi->probes = g_slist_append(NULL, probe);
247 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
250 devc->profile = profile;
256 SR_PRIV struct sr_dev_inst *lascar_scan(int bus, int address)
258 struct drv_context *drvc;
259 struct sr_dev_inst *sdi;
260 struct libusb_device **devlist;
261 struct libusb_device_descriptor des;
262 libusb_device_handle *dev_hdl;
264 unsigned char *config;
269 libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
270 for (i = 0; devlist[i]; i++) {
271 if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
272 sr_err("Failed to get device descriptor: %d.", ret);
276 if (libusb_get_bus_number(devlist[i]) != bus ||
277 libusb_get_device_address(devlist[i]) != address)
280 if (!(dev_hdl = lascar_open(devlist[i])))
283 if (!(config = lascar_get_config(dev_hdl)))
286 libusb_close(dev_hdl);
287 sdi = lascar_identify(config);
294 static void lascar_el_usb_dispatch(struct sr_dev_inst *sdi, unsigned char *buf,
297 struct dev_context *devc;
298 struct sr_datafeed_packet packet;
299 struct sr_datafeed_analog analog;
301 int samples, samples_left, i;
304 samples = buflen / devc->sample_size;
305 samples_left = devc->logged_samples - devc->rcvd_samples;
306 if (samples_left < samples)
307 samples = samples_left;
308 switch (devc->profile->logformat) {
313 packet.type = SR_DF_ANALOG;
314 packet.payload = &analog;
315 analog.num_samples = samples;
316 analog.mq = SR_MQ_CARBON_MONOXIDE;
317 analog.unit = SR_UNIT_CONCENTRATION;
319 if (!(analog.data = g_try_malloc(sizeof(float) * samples)))
321 for (i = 0; i < samples; i++) {
322 s = (buf[i * 2] << 8) | buf[i * 2 + 1];
323 analog.data[i] = s * devc->co_high + devc->co_low;
324 if (analog.data[i] < 0.0)
325 analog.data[i] = 0.0;
327 sr_session_send(devc->cb_data, &packet);
331 /* How did we even get this far? */
334 devc->rcvd_samples += samples;
338 SR_PRIV int lascar_el_usb_handle_events(int fd, int revents, void *cb_data)
340 struct dev_context *devc;
341 struct drv_context *drvc = di->priv;
342 struct sr_datafeed_packet packet;
343 struct sr_dev_inst *sdi;
353 if (sdi->status == SR_ST_STOPPING) {
354 for (i = 0; devc->usbfd[i] != -1; i++)
355 sr_source_remove(devc->usbfd[i]);
357 sdi->driver->dev_close(sdi);
359 packet.type = SR_DF_END;
360 sr_session_send(cb_data, &packet);
363 memset(&tv, 0, sizeof(struct timeval));
364 libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
370 SR_PRIV void lascar_el_usb_receive_transfer(struct libusb_transfer *transfer)
372 struct dev_context *devc;
373 struct sr_dev_inst *sdi;
375 gboolean packet_has_error;
377 sdi = transfer->user_data;
380 packet_has_error = FALSE;
381 switch (transfer->status) {
382 case LIBUSB_TRANSFER_NO_DEVICE:
383 /* USB device was unplugged. */
384 hw_dev_acquisition_stop(sdi, sdi);
386 case LIBUSB_TRANSFER_COMPLETED:
387 case LIBUSB_TRANSFER_TIMED_OUT: /* We may have received some data though */
390 packet_has_error = TRUE;
394 if (!packet_has_error) {
395 if (devc->rcvd_samples < devc->logged_samples)
396 lascar_el_usb_dispatch(sdi, transfer->buffer,
397 transfer->actual_length);
398 devc->rcvd_bytes += transfer->actual_length;
399 sr_spew("received %d/%d bytes (%d/%d samples)",
400 devc->rcvd_bytes, devc->log_size,
401 devc->rcvd_samples, devc->logged_samples);
402 if (devc->rcvd_bytes >= devc->log_size)
403 hw_dev_acquisition_stop(sdi, sdi);
406 if (sdi->status == SR_ST_ACTIVE) {
407 /* Send the same request again. */
408 if ((ret = libusb_submit_transfer(transfer) != 0)) {
409 sr_err("Unable to resubmit transfer: %s.",
410 libusb_error_name(ret));
411 g_free(transfer->buffer);
412 libusb_free_transfer(transfer);
413 hw_dev_acquisition_stop(sdi, sdi);
416 /* This was the last transfer we're going to receive, so
418 g_free(transfer->buffer);
419 libusb_free_transfer(transfer);