]> sigrok.org Git - libsigrok.git/blob - hardware/lascar-el-usb/api.c
Drop some lines that are no longer needed.
[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, LOG_PREFIX);
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         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 *hw_dev_list(void)
122 {
123         return ((struct drv_context *)(di->priv))->instances;
124 }
125
126 static int hw_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 hw_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 hw_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 hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
351                 void *cb_data)
352 {
353         struct sr_datafeed_packet packet;
354         struct sr_datafeed_meta meta;
355         struct sr_config *src;
356         struct dev_context *devc;
357         struct drv_context *drvc;
358         struct sr_usb_dev_inst *usb;
359         struct libusb_transfer *xfer_in, *xfer_out;
360         const struct libusb_pollfd **pfd;
361         struct timeval tv;
362         uint64_t interval;
363         int ret, i;
364         unsigned char cmd[3], resp[4], *buf;
365
366         if (sdi->status != SR_ST_ACTIVE)
367                 return SR_ERR_DEV_CLOSED;
368
369         if (!di->priv) {
370                 sr_err("Driver was not initialized.");
371                 return SR_ERR;
372         }
373
374         drvc = di->priv;
375         devc = sdi->priv;
376         usb = sdi->conn;
377         devc->cb_data = cb_data;
378
379         if (lascar_proc_config(sdi) != SR_OK)
380                 return SR_ERR;
381
382         sr_dbg("Starting log retrieval.");
383
384         /* Send header packet to the session bus. */
385         std_session_send_df_header(cb_data, LOG_PREFIX);
386
387         interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000;
388         packet.type = SR_DF_META;
389         packet.payload = &meta;
390         src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval));
391         meta.config = g_slist_append(NULL, src);
392         sr_session_send(devc->cb_data, &packet);
393         g_free(src);
394
395         if (devc->logged_samples == 0) {
396                 /* This ensures the frontend knows the session is done. */
397                 packet.type = SR_DF_END;
398                 sr_session_send(devc->cb_data, &packet);
399                 return SR_OK;
400         }
401
402         if (!(xfer_in = libusb_alloc_transfer(0)) ||
403                         !(xfer_out = libusb_alloc_transfer(0)))
404                 return SR_ERR;
405
406         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
407                         0x00, 0xffff, 0x00, NULL, 0, 50);
408         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
409                         0x02, 0x0002, 0x00, NULL, 0, 50);
410         libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR,
411                         0x02, 0x0001, 0x00, NULL, 0, 50);
412
413
414         /* Flush input. The F321 requires this. */
415         while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp,
416                         256, &ret, 5) == 0 && ret > 0)
417                 ;
418
419         libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
420                         resp, sizeof(resp), mark_xfer, 0, 10000);
421         if (libusb_submit_transfer(xfer_in) != 0) {
422                 libusb_free_transfer(xfer_in);
423                 libusb_free_transfer(xfer_out);
424                 return SR_ERR;
425         }
426
427         cmd[0] = 0x03;
428         cmd[1] = 0xff;
429         cmd[2] = 0xff;
430         libusb_fill_bulk_transfer(xfer_out, usb->devhdl, LASCAR_EP_OUT,
431                         cmd, 3, mark_xfer, 0, 100);
432         if (libusb_submit_transfer(xfer_out) != 0) {
433                 libusb_free_transfer(xfer_in);
434                 libusb_free_transfer(xfer_out);
435                 return SR_ERR;
436         }
437
438         tv.tv_sec = 0;
439         tv.tv_usec = 0;
440         while (!xfer_in->user_data || !xfer_out->user_data) {
441                 g_usleep(5000);
442                 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
443         }
444         if (xfer_in->user_data != GINT_TO_POINTER(1) ||
445                         xfer_in->user_data != GINT_TO_POINTER(1)) {
446                 sr_dbg("no response to log transfer request");
447                 libusb_free_transfer(xfer_in);
448                 libusb_free_transfer(xfer_out);
449                 return SR_ERR;
450         }
451         if (xfer_in->actual_length != 3 || xfer_in->buffer[0] != 2) {
452                 sr_dbg("invalid response to log transfer request");
453                 libusb_free_transfer(xfer_in);
454                 libusb_free_transfer(xfer_out);
455                 return SR_ERR;
456         }
457         devc->log_size = xfer_in->buffer[1] + (xfer_in->buffer[2] << 8);
458         libusb_free_transfer(xfer_out);
459
460         pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
461         for (i = 0; pfd[i]; i++) {
462                 /* Handle USB events every 100ms, for decent latency. */
463                 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
464                                 lascar_el_usb_handle_events, (void *)sdi);
465                 /* We'll need to remove this fd later. */
466                 devc->usbfd[i] = pfd[i]->fd;
467         }
468         devc->usbfd[i] = -1;
469
470         buf = g_try_malloc(4096);
471         libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN,
472                         buf, 4096, lascar_el_usb_receive_transfer, cb_data, 100);
473         if ((ret = libusb_submit_transfer(xfer_in) != 0)) {
474                 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
475                 libusb_free_transfer(xfer_in);
476                 g_free(buf);
477                 return SR_ERR;
478         }
479
480         return SR_OK;
481 }
482
483 SR_PRIV int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
484 {
485         (void)cb_data;
486
487         if (!di->priv) {
488                 sr_err("Driver was not initialized.");
489                 return SR_ERR;
490         }
491
492         if (sdi->status != SR_ST_ACTIVE) {
493                 sr_err("Device inactive, can't stop acquisition.");
494                 return SR_ERR;
495         }
496
497         sdi->status = SR_ST_STOPPING;
498         /* TODO: free ongoing transfers? */
499
500         return SR_OK;
501 }
502
503 SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info = {
504         .name = "lascar-el-usb",
505         .longname = "Lascar EL-USB",
506         .api_version = 1,
507         .init = hw_init,
508         .cleanup = hw_cleanup,
509         .scan = hw_scan,
510         .dev_list = hw_dev_list,
511         .dev_clear = clear_instances,
512         .config_get = config_get,
513         .config_set = config_set,
514         .config_list = config_list,
515         .dev_open = hw_dev_open,
516         .dev_close = hw_dev_close,
517         .dev_acquisition_start = hw_dev_acquisition_start,
518         .dev_acquisition_stop = hw_dev_acquisition_stop,
519         .priv = NULL,
520 };