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