api.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <stdlib.h>
  22. #include <string.h>
  23. #include "libsigrok.h"
  24. #include "libsigrok-internal.h"
  25. #include "protocol.h"
  26. #define VICTOR_VID 0x1244
  27. #define VICTOR_PID 0xd237
  28. #define VICTOR_VENDOR "Victor"
  29. #define VICTOR_INTERFACE 0
  30. #define VICTOR_ENDPOINT LIBUSB_ENDPOINT_IN | 1
  31. SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
  32. static struct sr_dev_driver *di = &victor_dmm_driver_info;
  33. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
  34. static const int32_t hwopts[] = {
  35. SR_CONF_CONN,
  36. };
  37. static const int32_t hwcaps[] = {
  38. SR_CONF_MULTIMETER,
  39. SR_CONF_LIMIT_MSEC,
  40. SR_CONF_LIMIT_SAMPLES,
  41. SR_CONF_CONTINUOUS,
  42. };
  43. static int init(struct sr_context *sr_ctx)
  44. {
  45. return std_init(sr_ctx, di, LOG_PREFIX);
  46. }
  47. static GSList *scan(GSList *options)
  48. {
  49. struct drv_context *drvc;
  50. struct dev_context *devc;
  51. struct sr_dev_inst *sdi;
  52. struct sr_channel *ch;
  53. struct libusb_device_descriptor des;
  54. libusb_device **devlist;
  55. GSList *devices;
  56. int ret, devcnt, i;
  57. (void)options;
  58. drvc = di->priv;
  59. devices = NULL;
  60. libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
  61. for (i = 0; devlist[i]; i++) {
  62. if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
  63. sr_warn("Failed to get device descriptor: %s",
  64. libusb_error_name(ret));
  65. continue;
  66. }
  67. if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
  68. continue;
  69. devcnt = g_slist_length(drvc->instances);
  70. if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
  71. VICTOR_VENDOR, NULL, NULL)))
  72. return NULL;
  73. sdi->driver = di;
  74. if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
  75. return NULL;
  76. sdi->priv = devc;
  77. if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
  78. return NULL;
  79. sdi->channels = g_slist_append(NULL, ch);
  80. if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
  81. libusb_get_device_address(devlist[i]), NULL)))
  82. return NULL;
  83. sdi->inst_type = SR_INST_USB;
  84. drvc->instances = g_slist_append(drvc->instances, sdi);
  85. devices = g_slist_append(devices, sdi);
  86. }
  87. libusb_free_device_list(devlist, 1);
  88. return devices;
  89. }
  90. static GSList *dev_list(void)
  91. {
  92. return ((struct drv_context *)(di->priv))->instances;
  93. }
  94. static int dev_open(struct sr_dev_inst *sdi)
  95. {
  96. struct drv_context *drvc = di->priv;
  97. struct sr_usb_dev_inst *usb;
  98. libusb_device **devlist;
  99. int ret, i;
  100. if (!di->priv) {
  101. sr_err("Driver was not initialized.");
  102. return SR_ERR;
  103. }
  104. usb = sdi->conn;
  105. libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
  106. for (i = 0; devlist[i]; i++) {
  107. if (libusb_get_bus_number(devlist[i]) != usb->bus
  108. || libusb_get_device_address(devlist[i]) != usb->address)
  109. continue;
  110. if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
  111. sr_err("Failed to open device: %s.", libusb_error_name(ret));
  112. return SR_ERR;
  113. }
  114. break;
  115. }
  116. libusb_free_device_list(devlist, 1);
  117. if (!devlist[i]) {
  118. sr_err("Device not found.");
  119. return SR_ERR;
  120. }
  121. /* The device reports as HID class, so the kernel would have
  122. * claimed it. */
  123. if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
  124. if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
  125. sr_err("Failed to detach kernel driver: %s.",
  126. libusb_error_name(ret));
  127. return SR_ERR;
  128. }
  129. }
  130. if ((ret = libusb_claim_interface(usb->devhdl,
  131. VICTOR_INTERFACE))) {
  132. sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
  133. return SR_ERR;
  134. }
  135. sdi->status = SR_ST_ACTIVE;
  136. return SR_OK;
  137. }
  138. static int dev_close(struct sr_dev_inst *sdi)
  139. {
  140. struct sr_usb_dev_inst *usb;
  141. if (!di->priv) {
  142. sr_err("Driver was not initialized.");
  143. return SR_ERR;
  144. }
  145. usb = sdi->conn;
  146. if (!usb->devhdl)
  147. /* Nothing to do. */
  148. return SR_OK;
  149. libusb_release_interface(usb->devhdl, VICTOR_INTERFACE);
  150. libusb_close(usb->devhdl);
  151. usb->devhdl = NULL;
  152. sdi->status = SR_ST_INACTIVE;
  153. return SR_OK;
  154. }
  155. static int cleanup(void)
  156. {
  157. int ret;
  158. struct drv_context *drvc;
  159. if (!(drvc = di->priv))
  160. /* Can get called on an unused driver, doesn't matter. */
  161. return SR_OK;
  162. ret = std_dev_clear(di, NULL);
  163. g_free(drvc);
  164. di->priv = NULL;
  165. return ret;
  166. }
  167. static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
  168. const struct sr_channel_group *cg)
  169. {
  170. struct sr_usb_dev_inst *usb;
  171. char str[128];
  172. (void)cg;
  173. switch (id) {
  174. case SR_CONF_CONN:
  175. if (!sdi || !sdi->conn)
  176. return SR_ERR_ARG;
  177. usb = sdi->conn;
  178. snprintf(str, 128, "%d.%d", usb->bus, usb->address);
  179. *data = g_variant_new_string(str);
  180. break;
  181. default:
  182. return SR_ERR_NA;
  183. }
  184. return SR_OK;
  185. }
  186. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  187. const struct sr_channel_group *cg)
  188. {
  189. struct dev_context *devc;
  190. gint64 now;
  191. int ret;
  192. (void)cg;
  193. if (sdi->status != SR_ST_ACTIVE)
  194. return SR_ERR_DEV_CLOSED;
  195. if (!di->priv) {
  196. sr_err("Driver was not initialized.");
  197. return SR_ERR;
  198. }
  199. devc = sdi->priv;
  200. ret = SR_OK;
  201. switch (id) {
  202. case SR_CONF_LIMIT_MSEC:
  203. devc->limit_msec = g_variant_get_uint64(data);
  204. now = g_get_monotonic_time() / 1000;
  205. devc->end_time = now + devc->limit_msec;
  206. sr_dbg("Setting time limit to %" PRIu64 "ms.",
  207. devc->limit_msec);
  208. break;
  209. case SR_CONF_LIMIT_SAMPLES:
  210. devc->limit_samples = g_variant_get_uint64(data);
  211. sr_dbg("Setting sample limit to %" PRIu64 ".",
  212. devc->limit_samples);
  213. break;
  214. default:
  215. ret = SR_ERR_NA;
  216. }
  217. return ret;
  218. }
  219. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  220. const struct sr_channel_group *cg)
  221. {
  222. (void)sdi;
  223. (void)cg;
  224. switch (key) {
  225. case SR_CONF_SCAN_OPTIONS:
  226. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  227. hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
  228. break;
  229. case SR_CONF_DEVICE_OPTIONS:
  230. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  231. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  232. break;
  233. default:
  234. return SR_ERR_NA;
  235. }
  236. return SR_OK;
  237. }
  238. static void receive_transfer(struct libusb_transfer *transfer)
  239. {
  240. struct dev_context *devc;
  241. struct sr_dev_inst *sdi;
  242. int ret;
  243. sdi = transfer->user_data;
  244. devc = sdi->priv;
  245. if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
  246. /* USB device was unplugged. */
  247. dev_acquisition_stop(sdi, sdi);
  248. } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
  249. sr_dbg("Got %d-byte packet.", transfer->actual_length);
  250. if (transfer->actual_length == DMM_DATA_SIZE) {
  251. victor_dmm_receive_data(sdi, transfer->buffer);
  252. if (devc->limit_samples) {
  253. if (devc->num_samples >= devc->limit_samples)
  254. dev_acquisition_stop(sdi, sdi);
  255. }
  256. }
  257. }
  258. /* Anything else is either an error or a timeout, which is fine:
  259. * we were just going to send another transfer request anyway. */
  260. if (sdi->status == SR_ST_ACTIVE) {
  261. /* Send the same request again. */
  262. if ((ret = libusb_submit_transfer(transfer) != 0)) {
  263. sr_err("Unable to resubmit transfer: %s.",
  264. libusb_error_name(ret));
  265. g_free(transfer->buffer);
  266. libusb_free_transfer(transfer);
  267. dev_acquisition_stop(sdi, sdi);
  268. }
  269. } else {
  270. /* This was the last transfer we're going to receive, so
  271. * clean up now. */
  272. g_free(transfer->buffer);
  273. libusb_free_transfer(transfer);
  274. }
  275. }
  276. static int handle_events(int fd, int revents, void *cb_data)
  277. {
  278. struct dev_context *devc;
  279. struct drv_context *drvc = di->priv;
  280. struct sr_datafeed_packet packet;
  281. struct sr_dev_inst *sdi;
  282. struct timeval tv;
  283. gint64 now;
  284. (void)fd;
  285. (void)revents;
  286. sdi = cb_data;
  287. devc = sdi->priv;
  288. if (devc->limit_msec) {
  289. now = g_get_monotonic_time() / 1000;
  290. if (now > devc->end_time)
  291. dev_acquisition_stop(sdi, sdi);
  292. }
  293. if (sdi->status == SR_ST_STOPPING) {
  294. usb_source_remove(drvc->sr_ctx);
  295. dev_close(sdi);
  296. packet.type = SR_DF_END;
  297. sr_session_send(cb_data, &packet);
  298. }
  299. memset(&tv, 0, sizeof(struct timeval));
  300. libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
  301. NULL);
  302. return TRUE;
  303. }
  304. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  305. {
  306. struct dev_context *devc;
  307. struct drv_context *drvc = di->priv;
  308. struct sr_usb_dev_inst *usb;
  309. struct libusb_transfer *transfer;
  310. int ret;
  311. unsigned char *buf;
  312. if (sdi->status != SR_ST_ACTIVE)
  313. return SR_ERR_DEV_CLOSED;
  314. if (!di->priv) {
  315. sr_err("Driver was not initialized.");
  316. return SR_ERR;
  317. }
  318. devc = sdi->priv;
  319. usb = sdi->conn;
  320. devc->cb_data = cb_data;
  321. /* Send header packet to the session bus. */
  322. std_session_send_df_header(cb_data, LOG_PREFIX);
  323. usb_source_add(drvc->sr_ctx, 100, handle_events, (void *)sdi);
  324. buf = g_try_malloc(DMM_DATA_SIZE);
  325. transfer = libusb_alloc_transfer(0);
  326. /* Each transfer request gets 100ms to arrive before it's restarted.
  327. * The device only sends 1 transfer/second no matter how many
  328. * times you ask, but we want to keep step with the USB events
  329. * handling above. */
  330. libusb_fill_interrupt_transfer(transfer, usb->devhdl,
  331. VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
  332. cb_data, 100);
  333. if ((ret = libusb_submit_transfer(transfer) != 0)) {
  334. sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
  335. libusb_free_transfer(transfer);
  336. g_free(buf);
  337. return SR_ERR;
  338. }
  339. return SR_OK;
  340. }
  341. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  342. {
  343. (void)cb_data;
  344. if (!di->priv) {
  345. sr_err("Driver was not initialized.");
  346. return SR_ERR;
  347. }
  348. if (sdi->status != SR_ST_ACTIVE) {
  349. sr_err("Device not active, can't stop acquisition.");
  350. return SR_ERR;
  351. }
  352. sdi->status = SR_ST_STOPPING;
  353. return SR_OK;
  354. }
  355. SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
  356. .name = "victor-dmm",
  357. .longname = "Victor DMMs",
  358. .api_version = 1,
  359. .init = init,
  360. .cleanup = cleanup,
  361. .scan = scan,
  362. .dev_list = dev_list,
  363. .dev_clear = NULL,
  364. .config_get = config_get,
  365. .config_set = config_set,
  366. .config_list = config_list,
  367. .dev_open = dev_open,
  368. .dev_close = dev_close,
  369. .dev_acquisition_start = dev_acquisition_start,
  370. .dev_acquisition_stop = dev_acquisition_stop,
  371. .priv = NULL,
  372. };