api.c 12 KB

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