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