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