]>
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 | ||
26 | SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info; | |
27 | static struct sr_dev_driver *di = &lascar_el_usb_driver_info; | |
851d5b22 | 28 | |
7faf69da | 29 | static const int32_t hwopts[] = { |
1953564a | 30 | SR_CONF_CONN, |
851d5b22 BV |
31 | }; |
32 | ||
7faf69da | 33 | static const int32_t hwcaps[] = { |
1953564a BV |
34 | SR_CONF_THERMOMETER, |
35 | SR_CONF_HYGROMETER, | |
361d1511 | 36 | SR_CONF_DATALOG, |
1953564a | 37 | SR_CONF_LIMIT_SAMPLES, |
851d5b22 BV |
38 | }; |
39 | ||
3b412e3a | 40 | static int dev_clear(void) |
46697e38 | 41 | { |
c9d622a4 | 42 | return std_dev_clear(di, NULL); |
46697e38 BV |
43 | } |
44 | ||
6078d2c9 | 45 | static int init(struct sr_context *sr_ctx) |
46697e38 | 46 | { |
f6beaac5 | 47 | return std_init(sr_ctx, di, LOG_PREFIX); |
46697e38 BV |
48 | } |
49 | ||
6078d2c9 | 50 | static GSList *scan(GSList *options) |
46697e38 BV |
51 | { |
52 | struct drv_context *drvc; | |
851d5b22 BV |
53 | struct sr_dev_inst *sdi; |
54 | struct sr_usb_dev_inst *usb; | |
1987b8d6 | 55 | struct sr_config *src; |
851d5b22 BV |
56 | GSList *usb_devices, *devices, *l; |
57 | const char *conn; | |
46697e38 | 58 | |
4b97c74e | 59 | drvc = di->priv; |
46697e38 | 60 | |
851d5b22 BV |
61 | conn = NULL; |
62 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
63 | src = l->data; |
64 | switch (src->key) { | |
1953564a | 65 | case SR_CONF_CONN: |
7faf69da | 66 | conn = g_variant_get_string(src->data, NULL); |
851d5b22 BV |
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 | } | |
0f150649 BV |
84 | sdi->inst_type = SR_INST_USB; |
85 | sdi->conn = usb; | |
851d5b22 BV |
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); | |
46697e38 BV |
92 | |
93 | return devices; | |
94 | } | |
95 | ||
6078d2c9 | 96 | static GSList *dev_list(void) |
46697e38 | 97 | { |
0e94d524 | 98 | return ((struct drv_context *)(di->priv))->instances; |
46697e38 BV |
99 | } |
100 | ||
6078d2c9 | 101 | static int dev_open(struct sr_dev_inst *sdi) |
46697e38 | 102 | { |
6aa1eb4e | 103 | struct drv_context *drvc; |
0f150649 | 104 | struct sr_usb_dev_inst *usb; |
6aa1eb4e | 105 | int ret; |
46697e38 | 106 | |
6aa1eb4e BV |
107 | if (!(drvc = di->priv)) { |
108 | sr_err("Driver was not initialized."); | |
109 | return SR_ERR; | |
110 | } | |
111 | ||
0f150649 BV |
112 | usb = sdi->conn; |
113 | ||
114 | if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) | |
6aa1eb4e BV |
115 | return SR_ERR; |
116 | ||
0f150649 | 117 | if ((ret = libusb_claim_interface(usb->devhdl, LASCAR_INTERFACE))) { |
6aa1eb4e BV |
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; | |
46697e38 BV |
124 | } |
125 | ||
6078d2c9 | 126 | static int dev_close(struct sr_dev_inst *sdi) |
46697e38 | 127 | { |
0f150649 | 128 | struct sr_usb_dev_inst *usb; |
6aa1eb4e BV |
129 | |
130 | if (!di->priv) { | |
131 | sr_err("Driver was not initialized."); | |
132 | return SR_ERR; | |
133 | } | |
134 | ||
0f150649 BV |
135 | usb = sdi->conn; |
136 | ||
137 | if (!usb->devhdl) | |
6aa1eb4e BV |
138 | /* Nothing to do. */ |
139 | return SR_OK; | |
140 | ||
0f150649 BV |
141 | libusb_release_interface(usb->devhdl, LASCAR_INTERFACE); |
142 | libusb_close(usb->devhdl); | |
143 | usb->devhdl = NULL; | |
6aa1eb4e BV |
144 | sdi->status = SR_ST_INACTIVE; |
145 | ||
46697e38 BV |
146 | return SR_OK; |
147 | } | |
148 | ||
6078d2c9 | 149 | static int cleanup(void) |
46697e38 | 150 | { |
c9d622a4 | 151 | int ret; |
6aa1eb4e | 152 | struct drv_context *drvc; |
46697e38 | 153 | |
6aa1eb4e BV |
154 | if (!(drvc = di->priv)) |
155 | /* Can get called on an unused driver, doesn't matter. */ | |
156 | return SR_OK; | |
157 | ||
3b412e3a | 158 | ret = dev_clear(); |
6aa1eb4e BV |
159 | g_free(drvc); |
160 | di->priv = NULL; | |
46697e38 | 161 | |
c9d622a4 | 162 | return ret; |
46697e38 BV |
163 | } |
164 | ||
8f996b89 ML |
165 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, |
166 | const struct sr_probe_group *probe_group) | |
361d1511 BV |
167 | { |
168 | struct dev_context *devc; | |
0f150649 | 169 | struct sr_usb_dev_inst *usb; |
361d1511 | 170 | int ret; |
0f150649 | 171 | char str[128]; |
361d1511 | 172 | |
8f996b89 ML |
173 | (void)probe_group; |
174 | ||
361d1511 BV |
175 | devc = sdi->priv; |
176 | switch (id) { | |
0f150649 BV |
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; | |
361d1511 BV |
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; | |
7faf69da | 189 | *data = g_variant_new_boolean(ret ? TRUE : FALSE); |
361d1511 BV |
190 | break; |
191 | case SR_CONF_LIMIT_SAMPLES: | |
7faf69da | 192 | *data = g_variant_new_uint64(devc->limit_samples); |
361d1511 BV |
193 | break; |
194 | default: | |
bd6fbf62 | 195 | return SR_ERR_NA; |
361d1511 BV |
196 | } |
197 | ||
198 | return SR_OK; | |
199 | } | |
200 | ||
8f996b89 ML |
201 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
202 | const struct sr_probe_group *probe_group) | |
46697e38 | 203 | { |
6aa1eb4e | 204 | struct dev_context *devc; |
46697e38 BV |
205 | int ret; |
206 | ||
8f996b89 ML |
207 | (void)probe_group; |
208 | ||
e73ffd42 BV |
209 | if (sdi->status != SR_ST_ACTIVE) |
210 | return SR_ERR_DEV_CLOSED; | |
211 | ||
6aa1eb4e BV |
212 | if (!di->priv) { |
213 | sr_err("Driver was not initialized."); | |
214 | return SR_ERR; | |
215 | } | |
46697e38 | 216 | |
6aa1eb4e | 217 | devc = sdi->priv; |
46697e38 | 218 | ret = SR_OK; |
035a1078 | 219 | switch (id) { |
361d1511 | 220 | case SR_CONF_DATALOG: |
7faf69da | 221 | if (g_variant_get_boolean(data)) { |
361d1511 BV |
222 | /* Start logging. */ |
223 | ret = lascar_start_logging(sdi); | |
224 | } else { | |
225 | /* Stop logging. */ | |
226 | ret = lascar_stop_logging(sdi); | |
227 | } | |
228 | break; | |
1953564a | 229 | case SR_CONF_LIMIT_SAMPLES: |
7faf69da | 230 | devc->limit_samples = g_variant_get_uint64(data); |
6aa1eb4e BV |
231 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
232 | devc->limit_samples); | |
233 | break; | |
46697e38 | 234 | default: |
bd6fbf62 | 235 | ret = SR_ERR_NA; |
46697e38 BV |
236 | } |
237 | ||
238 | return ret; | |
239 | } | |
240 | ||
8f996b89 ML |
241 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
242 | const struct sr_probe_group *probe_group) | |
a1c743fc | 243 | { |
a1c743fc | 244 | (void)sdi; |
8f996b89 | 245 | (void)probe_group; |
a1c743fc BV |
246 | |
247 | switch (key) { | |
0d485e30 | 248 | case SR_CONF_SCAN_OPTIONS: |
7faf69da BV |
249 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
250 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 251 | break; |
9a6517d1 | 252 | case SR_CONF_DEVICE_OPTIONS: |
7faf69da BV |
253 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
254 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 255 | break; |
a1c743fc | 256 | default: |
bd6fbf62 | 257 | return SR_ERR_NA; |
a1c743fc BV |
258 | } |
259 | ||
260 | return SR_OK; | |
261 | } | |
262 | ||
6aa1eb4e BV |
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; | |
0f150649 | 295 | struct sr_usb_dev_inst *usb; |
361d1511 | 296 | int dummy, ret; |
6aa1eb4e BV |
297 | |
298 | devc = sdi->priv; | |
0f150649 BV |
299 | usb = sdi->conn; |
300 | ||
301 | if (lascar_get_config(usb->devhdl, devc->config, &dummy) != SR_OK) | |
6aa1eb4e BV |
302 | return SR_ERR; |
303 | ||
304 | ret = SR_OK; | |
305 | switch (devc->profile->logformat) { | |
306 | case LOG_TEMP_RH: | |
6787f404 BV |
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"); | |
6aa1eb4e BV |
316 | break; |
317 | case LOG_CO: | |
318 | devc->sample_size = 2; | |
6787f404 BV |
319 | devc->co_high = binary32_le_to_float(devc->config + 0x24); |
320 | devc->co_low = binary32_le_to_float(devc->config + 0x28); | |
6aa1eb4e BV |
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 | ||
6078d2c9 | 333 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
46697e38 | 334 | { |
6aa1eb4e | 335 | struct sr_datafeed_packet packet; |
8e77bc20 BV |
336 | struct sr_datafeed_meta meta; |
337 | struct sr_config *src; | |
6aa1eb4e | 338 | struct dev_context *devc; |
8e77bc20 | 339 | struct drv_context *drvc; |
0f150649 | 340 | struct sr_usb_dev_inst *usb; |
6aa1eb4e | 341 | struct libusb_transfer *xfer_in, *xfer_out; |
6aa1eb4e | 342 | struct timeval tv; |
8e77bc20 | 343 | uint64_t interval; |
6c60facc | 344 | int ret; |
6aa1eb4e BV |
345 | unsigned char cmd[3], resp[4], *buf; |
346 | ||
e73ffd42 BV |
347 | if (sdi->status != SR_ST_ACTIVE) |
348 | return SR_ERR_DEV_CLOSED; | |
349 | ||
6aa1eb4e BV |
350 | if (!di->priv) { |
351 | sr_err("Driver was not initialized."); | |
352 | return SR_ERR; | |
353 | } | |
354 | ||
8e77bc20 | 355 | drvc = di->priv; |
6aa1eb4e | 356 | devc = sdi->priv; |
0f150649 | 357 | usb = sdi->conn; |
6aa1eb4e BV |
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. */ | |
29a27196 | 366 | std_session_send_df_header(cb_data, LOG_PREFIX); |
6aa1eb4e | 367 | |
8e77bc20 BV |
368 | interval = (devc->config[0x1c] | (devc->config[0x1d] << 8)) * 1000; |
369 | packet.type = SR_DF_META; | |
370 | packet.payload = &meta; | |
7faf69da | 371 | src = sr_config_new(SR_CONF_SAMPLE_INTERVAL, g_variant_new_uint64(interval)); |
8e77bc20 BV |
372 | meta.config = g_slist_append(NULL, src); |
373 | sr_session_send(devc->cb_data, &packet); | |
374 | g_free(src); | |
375 | ||
6aa1eb4e BV |
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 | ||
0f150649 | 387 | libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, |
6aa1eb4e | 388 | 0x00, 0xffff, 0x00, NULL, 0, 50); |
0f150649 | 389 | libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, |
6aa1eb4e | 390 | 0x02, 0x0002, 0x00, NULL, 0, 50); |
0f150649 | 391 | libusb_control_transfer(usb->devhdl, LIBUSB_REQUEST_TYPE_VENDOR, |
6aa1eb4e BV |
392 | 0x02, 0x0001, 0x00, NULL, 0, 50); |
393 | ||
394 | ||
395 | /* Flush input. The F321 requires this. */ | |
0f150649 | 396 | while (libusb_bulk_transfer(usb->devhdl, LASCAR_EP_IN, resp, |
6aa1eb4e BV |
397 | 256, &ret, 5) == 0 && ret > 0) |
398 | ; | |
399 | ||
0f150649 | 400 | libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN, |
6aa1eb4e BV |
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; | |
0f150649 | 411 | libusb_fill_bulk_transfer(xfer_out, usb->devhdl, LASCAR_EP_OUT, |
6aa1eb4e BV |
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 | ||
6c60facc | 441 | usb_source_add(drvc->sr_ctx, 100, lascar_el_usb_handle_events, (void *)sdi); |
6aa1eb4e BV |
442 | |
443 | buf = g_try_malloc(4096); | |
0f150649 | 444 | libusb_fill_bulk_transfer(xfer_in, usb->devhdl, LASCAR_EP_IN, |
6aa1eb4e BV |
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 | } | |
46697e38 BV |
452 | |
453 | return SR_OK; | |
454 | } | |
455 | ||
6078d2c9 | 456 | SR_PRIV int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
46697e38 BV |
457 | { |
458 | (void)cb_data; | |
459 | ||
6aa1eb4e BV |
460 | if (!di->priv) { |
461 | sr_err("Driver was not initialized."); | |
462 | return SR_ERR; | |
463 | } | |
464 | ||
46697e38 BV |
465 | if (sdi->status != SR_ST_ACTIVE) { |
466 | sr_err("Device inactive, can't stop acquisition."); | |
467 | return SR_ERR; | |
468 | } | |
469 | ||
6aa1eb4e BV |
470 | sdi->status = SR_ST_STOPPING; |
471 | /* TODO: free ongoing transfers? */ | |
46697e38 BV |
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, | |
6078d2c9 UH |
480 | .init = init, |
481 | .cleanup = cleanup, | |
482 | .scan = scan, | |
483 | .dev_list = dev_list, | |
3b412e3a | 484 | .dev_clear = dev_clear, |
361d1511 | 485 | .config_get = config_get, |
035a1078 | 486 | .config_set = config_set, |
a1c743fc | 487 | .config_list = config_list, |
6078d2c9 UH |
488 | .dev_open = dev_open, |
489 | .dev_close = dev_close, | |
490 | .dev_acquisition_start = dev_acquisition_start, | |
491 | .dev_acquisition_stop = dev_acquisition_stop, | |
46697e38 BV |
492 | .priv = NULL, |
493 | }; |