2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 static const int32_t hwcaps[] = {
26 SR_CONF_LIMIT_SAMPLES,
31 static char *probes[] = {
37 static const char *data_sources[] = {
42 SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info;
43 static struct sr_dev_driver *di = &uni_t_ut32x_driver_info;
46 static int init(struct sr_context *sr_ctx)
48 return std_init(sr_ctx, di, LOG_PREFIX);
51 static GSList *scan(GSList *options)
53 struct drv_context *drvc;
54 struct dev_context *devc;
55 struct sr_dev_inst *sdi;
56 struct sr_probe *probe;
57 struct sr_config *src;
58 GSList *usb_devices, *devices, *l;
63 drvc->instances = NULL;
66 for (l = options; l; l = l->next) {
70 conn = g_variant_get_string(src->data, NULL);
78 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_CONN))) {
79 /* We have a list of sr_usb_dev_inst matching the connection
80 * string. Wrap them in sr_dev_inst and we're done. */
81 for (l = usb_devices; l; l = l->next) {
82 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR,
86 sdi->inst_type = SR_INST_USB;
88 for (i = 0; i < 3; i++) {
89 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
91 sr_dbg("Probe malloc failed.");
94 sdi->probes = g_slist_append(sdi->probes, probe);
97 if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
98 sr_dbg("Device context malloc failed.");
102 devc->limit_samples = 0;
103 devc->data_source = DEFAULT_DATA_SOURCE;
104 drvc->instances = g_slist_append(drvc->instances, sdi);
105 devices = g_slist_append(devices, sdi);
107 g_slist_free(usb_devices);
109 g_slist_free_full(usb_devices, g_free);
114 static GSList *dev_list(void)
116 return ((struct drv_context *)(di->priv))->instances;
119 static int dev_clear(void)
121 return std_dev_clear(di, NULL);
124 static int dev_open(struct sr_dev_inst *sdi)
126 struct drv_context *drvc;
127 struct sr_usb_dev_inst *usb;
130 if (!(drvc = di->priv)) {
131 sr_err("Driver was not initialized.");
137 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
141 * The libusbx 1.0.9 darwin backend is broken: it can report a kernel
142 * driver being active, but detaching it always returns an error.
144 #if !defined(__APPLE__)
145 if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
146 if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
147 sr_err("failed to detach kernel driver: %s",
148 libusb_error_name(ret));
154 if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) {
155 sr_err("Failed to set configuration: %s.", libusb_error_name(ret));
159 if ((ret = libusb_claim_interface(usb->devhdl, USB_INTERFACE))) {
160 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
163 sdi->status = SR_ST_ACTIVE;
168 static int dev_close(struct sr_dev_inst *sdi)
170 struct sr_usb_dev_inst *usb;
173 sr_err("Driver was not initialized.");
182 libusb_release_interface(usb->devhdl, USB_INTERFACE);
183 libusb_close(usb->devhdl);
185 sdi->status = SR_ST_INACTIVE;
190 static int cleanup(void)
193 struct drv_context *drvc;
195 if (!(drvc = di->priv))
196 /* Can get called on an unused driver, doesn't matter. */
206 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
208 struct dev_context *devc;
212 case SR_CONF_LIMIT_SAMPLES:
213 *data = g_variant_new_uint64(devc->limit_samples);
215 case SR_CONF_DATA_SOURCE:
216 if (devc->data_source == DATA_SOURCE_LIVE)
217 *data = g_variant_new_string("Live");
219 *data = g_variant_new_string("Memory");
228 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
230 struct dev_context *devc;
234 if (sdi->status != SR_ST_ACTIVE)
235 return SR_ERR_DEV_CLOSED;
238 sr_err("Driver was not initialized.");
245 case SR_CONF_LIMIT_SAMPLES:
246 devc->limit_samples = g_variant_get_uint64(data);
247 sr_dbg("Setting sample limit to %" PRIu64 ".",
248 devc->limit_samples);
250 case SR_CONF_DATA_SOURCE:
251 tmp_str = g_variant_get_string(data, NULL);
252 if (!strcmp(tmp_str, "Live"))
253 devc->data_source = DATA_SOURCE_LIVE;
254 else if (!strcmp(tmp_str, "Memory"))
255 devc->data_source = DATA_SOURCE_MEMORY;
266 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
272 case SR_CONF_DEVICE_OPTIONS:
273 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
274 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
276 case SR_CONF_DATA_SOURCE:
277 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
286 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
289 struct drv_context *drvc;
290 struct dev_context *devc;
291 struct sr_usb_dev_inst *usb;
292 const struct libusb_pollfd **pfd;
294 unsigned char cmd[2];
296 if (sdi->status != SR_ST_ACTIVE)
297 return SR_ERR_DEV_CLOSED;
303 devc->cb_data = cb_data;
304 devc->num_samples = 0;
305 devc->packet_len = 0;
307 /* Configure serial port parameters on USB-UART interface
308 * chip inside the device (just baudrate 2400 actually). */
311 ret = libusb_control_transfer(usb->devhdl, 0x21, 0x09, 0x0300, 0x00,
314 sr_dbg("Failed to configure CH9325: %s", libusb_error_name(ret));
318 /* Send header packet to the session bus. */
319 std_session_send_df_header(cb_data, LOG_PREFIX);
321 if (!(devc->xfer = libusb_alloc_transfer(0)))
324 /* Length of payload to follow. */
326 if (devc->data_source == DATA_SOURCE_LIVE)
327 cmd[1] = CMD_GET_LIVE;
329 cmd[1] = CMD_GET_STORED;
331 ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, cmd, 2, &len, 5);
332 if (ret != 0 || len != 2) {
333 sr_dbg("Failed to start acquisition: %s", libusb_error_name(ret));
334 libusb_free_transfer(devc->xfer);
338 libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf,
339 8, uni_t_ut32x_receive_transfer, (void *)sdi, 15);
340 if (libusb_submit_transfer(devc->xfer) != 0) {
341 libusb_free_transfer(devc->xfer);
345 pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
346 for (i = 0; pfd[i]; i++) {
347 /* Handle USB events every 10ms. */
348 sr_source_add(pfd[i]->fd, pfd[i]->events, 10,
349 uni_t_ut32x_handle_events, (void *)sdi);
350 /* We'll need to remove this fd later. */
351 devc->usbfd[i] = pfd[i]->fd;
358 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
363 if (sdi->status != SR_ST_ACTIVE)
364 return SR_ERR_DEV_CLOSED;
366 /* Signal USB transfer handler to clean up and stop. */
367 sdi->status = SR_ST_STOPPING;
372 SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info = {
373 .name = "uni-t-ut32x",
374 .longname = "UNI-T UT32x",
379 .dev_list = dev_list,
380 .dev_clear = dev_clear,
381 .config_get = config_get,
382 .config_set = config_set,
383 .config_list = config_list,
384 .dev_open = dev_open,
385 .dev_close = dev_close,
386 .dev_acquisition_start = dev_acquisition_start,
387 .dev_acquisition_stop = dev_acquisition_stop,