]> sigrok.org Git - libsigrok.git/blob - hardware/lascar-el-usb/api.c
Use consistent API callback function names.
[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 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                 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 init(struct sr_context *sr_ctx)
71 {
72         return std_hw_init(sr_ctx, di, LOG_PREFIX);
73 }
74
75 static GSList *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         drvc = di->priv;
85
86         conn = NULL;
87         for (l = options; l; l = l->next) {
88                 src = l->data;
89                 switch (src->key) {
90                 case SR_CONF_CONN:
91                         conn = g_variant_get_string(src->data, NULL);
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                         }
109                         sdi->inst_type = SR_INST_USB;
110                         sdi->conn = usb;
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);
117
118         return devices;
119 }
120
121 static GSList *dev_list(void)
122 {
123         return ((struct drv_context *)(di->priv))->instances;
124 }
125
126 static int dev_open(struct sr_dev_inst *sdi)
127 {
128         struct drv_context *drvc;
129         struct sr_usb_dev_inst *usb;
130         int ret;
131
132         if (!(drvc = di->priv)) {
133                 sr_err("Driver was not initialized.");
134                 return SR_ERR;
135         }
136
137         usb = sdi->conn;
138
139         if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
140                 return SR_ERR;
141
142         if ((ret = libusb_claim_interface(usb->devhdl, LASCAR_INTERFACE))) {
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;
149 }
150
151 static int dev_close(struct sr_dev_inst *sdi)
152 {
153         struct sr_usb_dev_inst *usb;
154
155         if (!di->priv) {
156                 sr_err("Driver was not initialized.");
157                 return SR_ERR;
158         }
159
160         usb = sdi->conn;
161
162         if (!usb->devhdl)
163                 /*  Nothing to do. */
164                 return SR_OK;
165
166         libusb_release_interface(usb->devhdl, LASCAR_INTERFACE);
167         libusb_close(usb->devhdl);
168         usb->devhdl = NULL;
169         sdi->status = SR_ST_INACTIVE;
170
171         return SR_OK;
172 }
173
174 static int cleanup(void)
175 {
176         struct drv_context *drvc;
177
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;
185
186         return SR_OK;
187 }
188
189 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi)
190 {
191         struct dev_context *devc;
192         struct sr_usb_dev_inst *usb;
193         int ret;
194         char str[128];
195
196         devc = sdi->priv;
197         switch (id) {
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;
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;
210                 *data = g_variant_new_boolean(ret ? TRUE : FALSE);
211                 break;
212         case SR_CONF_LIMIT_SAMPLES:
213                 *data = g_variant_new_uint64(devc->limit_samples);
214                 break;
215         default:
216                 return SR_ERR_NA;
217         }
218
219         return SR_OK;
220 }
221
222 static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
223 {
224         struct dev_context *devc;
225         int ret;
226
227         if (sdi->status != SR_ST_ACTIVE)
228                 return SR_ERR_DEV_CLOSED;
229
230         if (!di->priv) {
231                 sr_err("Driver was not initialized.");
232                 return SR_ERR;
233         }
234
235         devc = sdi->priv;
236         ret = SR_OK;
237         switch (id) {
238         case SR_CONF_DATALOG:
239                 if (g_variant_get_boolean(data)) {
240                         /* Start logging. */
241                         ret = lascar_start_logging(sdi);
242                 } else {
243                         /* Stop logging. */
244                         ret = lascar_stop_logging(sdi);
245                 }
246                 break;
247         case SR_CONF_LIMIT_SAMPLES:
248                 devc->limit_samples = g_variant_get_uint64(data);
249                 sr_dbg("Setting sample limit to %" PRIu64 ".",
250                        devc->limit_samples);
251                 break;
252         default:
253                 ret = SR_ERR_NA;
254         }
255
256         return ret;
257 }
258
259 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
260 {
261
262         (void)sdi;
263
264         switch (key) {
265         case SR_CONF_SCAN_OPTIONS:
266                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
267                                 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
268                 break;
269         case SR_CONF_DEVICE_OPTIONS:
270                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
271                                 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
272                 break;
273         default:
274                 return SR_ERR_NA;
275         }
276
277         return SR_OK;
278 }
279
280 static 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. */
297 static 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
309 static int lascar_proc_config(const struct sr_dev_inst *sdi)
310 {
311         struct dev_context *devc;
312         struct sr_usb_dev_inst *usb;
313         int dummy, ret;
314
315         devc = sdi->priv;
316         usb = sdi->conn;
317
318         if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK)
319                 return SR_ERR;
320
321         ret = SR_OK;
322         switch (devc->profile->logformat) {
323         case LOG_TEMP_RH:
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");
333                 break;
334         case LOG_CO:
335                 devc->sample_size = 2;
336                 devc->co_high = binary32_le_to_float(devc->config + 0x24);
337                 devc->co_low = binary32_le_to_float(devc->config + 0x28);
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
350 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
351 {
352         struct sr_datafeed_packet packet;
353         struct sr_datafeed_meta meta;
354         struct sr_config *src;
355         struct dev_context *devc;
356         struct drv_context *drvc;
357         struct sr_usb_dev_inst *usb;
358         struct libusb_transfer *xfer_in, *xfer_out;
359         const struct libusb_pollfd **pfd;
360         struct timeval tv;
361         uint64_t interval;
362         int ret, i;
363         unsigned char cmd[3], resp[4], *buf;
364
365         if (sdi->status != SR_ST_ACTIVE)
366                 return SR_ERR_DEV_CLOSED;
367
368         if (!di->priv) {
369                 sr_err("Driver was not initialized.");
370                 return SR_ERR;
371         }
372
373         drvc = di->priv;
374         devc = sdi->priv;
375         usb = sdi->conn;
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. */
384         std_session_send_df_header(cb_data, LOG_PREFIX);
385
386         interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
387         packet.type = SR_DF_META;
388         packet.payload = &meta;
389         src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval));
390         meta.config = g_slist_append(NULL, src);
391         sr_session_send(devc->cb_data, &packet);
392         g_free(src);
393
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
405         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
406                         0x00, 0xffff, 0x00, NULL, 0, 50);
407         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
408                         0x02, 0x0002, 0x00, NULL, 0, 50);
409         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
410                         0x02, 0x0001, 0x00, NULL, 0, 50);
411
412
413         /* Flush input. The F321 requires this. */
414         while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp,
415                         256, &ret, 5) == 0 && ret > 0)
416                 ;
417
418         libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
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;
429         libusb_fill_bulk_transfer(xfer_out, usb->devhdl, LASCAR_EP_OUT,
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
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);
470         libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
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         }
478
479         return SR_OK;
480 }
481
482 SR_PRIV int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
483 {
484         (void)cb_data;
485
486         if (!di->priv) {
487                 sr_err("Driver was not initialized.");
488                 return SR_ERR;
489         }
490
491         if (sdi->status != SR_ST_ACTIVE) {
492                 sr_err("Device inactive, can't stop acquisition.");
493                 return SR_ERR;
494         }
495
496         sdi->status = SR_ST_STOPPING;
497         /* TODO: free ongoing transfers? */
498
499         return SR_OK;
500 }
501
502 SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
503         .name = "lascar-el-usb",
504         .longname = "Lascar EL-USB",
505         .api_version = 1,
506         .init = init,
507         .cleanup = cleanup,
508         .scan = scan,
509         .dev_list = dev_list,
510         .dev_clear = clear_instances,
511         .config_get = config_get,
512         .config_set = config_set,
513         .config_list = config_list,
514         .dev_open = dev_open,
515         .dev_close = dev_close,
516         .dev_acquisition_start = dev_acquisition_start,
517         .dev_acquisition_stop = dev_acquisition_stop,
518         .priv = NULL,
519 };