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