]> sigrok.org Git - libsigrok.git/blobdiff - hardware/uni-t-ut32x/api.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / uni-t-ut32x / api.c
index f18435245c31f338c519e615765fc270578a63ba..352853d8d34cf6efc71c69a70149b1b0eb68bff2 100644 (file)
 
 #include <string.h>
 
-#define USB_CONN "1a86.e008"
-#define VENDOR "UNI-T"
-#define MODEL "UT32x"
-#define USB_INTERFACE 0
-#define EP_IN 0x80 | 2
-#define EP_OUT 2
-
 static const int32_t hwcaps[] = {
        SR_CONF_THERMOMETER,
        SR_CONF_LIMIT_SAMPLES,
@@ -35,7 +28,7 @@ static const int32_t hwcaps[] = {
        SR_CONF_DATA_SOURCE,
 };
 
-static char *probes[] = {
+static char *channels[] = {
        "T1",
        "T2",
        "T1-T2",
@@ -60,7 +53,7 @@ static GSList *scan(GSList *options)
        struct drv_context *drvc;
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        struct sr_config *src;
        GSList *usb_devices, *devices, *l;
        int i;
@@ -82,7 +75,7 @@ static GSList *scan(GSList *options)
                return NULL;
 
        devices = NULL;
-       if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, USB_CONN))) {
+       if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
                /* We have a list of sr_usb_dev_inst matching the connection
                 * string. Wrap them in sr_dev_inst and we're done. */
                for (l = usb_devices; l; l = l->next) {
@@ -93,12 +86,12 @@ static GSList *scan(GSList *options)
                        sdi->inst_type = SR_INST_USB;
                        sdi->conn = l->data;
                        for (i = 0; i < 3; i++) {
-                               if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
-                                               probes[i]))) {
-                                       sr_dbg("Probe malloc failed.");
+                               if (!(ch = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
+                                               channels[i]))) {
+                                       sr_dbg("Channel malloc failed.");
                                        return NULL;
                                }
-                               sdi->probes = g_slist_append(sdi->probes, probe);
+                               sdi->channels = g_slist_append(sdi->channels, ch);
                        }
 
                        if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
@@ -120,16 +113,7 @@ static GSList *scan(GSList *options)
 
 static GSList *dev_list(void)
 {
-       struct drv_context *drvc;
-
-       drvc = di->priv;
-
-       return drvc->instances;
-}
-
-static int dev_clear(void)
-{
-       return std_dev_clear(di, NULL);
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
@@ -153,8 +137,8 @@ static int dev_open(struct sr_dev_inst *sdi)
  * driver being active, but detaching it always returns an error.
  */
 #if !defined(__APPLE__)
-       if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) {
-               if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) {
+       if (libusb_kernel_driver_active(usb->devhdl, USB_INTERFACE) == 1) {
+               if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
                        sr_err("failed to detach kernel driver: %s",
                                        libusb_error_name(ret));
                        return SR_ERR;
@@ -162,7 +146,7 @@ static int dev_open(struct sr_dev_inst *sdi)
        }
 #endif
 
-       if ((ret = libusb_set_configuration(usb->devhdl, 1))) {
+       if ((ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION))) {
                sr_err("Failed to set configuration: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
@@ -207,17 +191,21 @@ static int cleanup(void)
                /* Can get called on an unused driver, doesn't matter. */
                return SR_OK;
 
-       ret = dev_clear();
+
+       ret = std_dev_clear(di, NULL);
        g_free(drvc);
        di->priv = NULL;
 
        return ret;
 }
 
-static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
 
+       (void)cg;
+
        devc = sdi->priv;
        switch (key) {
        case SR_CONF_LIMIT_SAMPLES:
@@ -236,12 +224,15 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
+static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        int ret;
        const char *tmp_str;
 
+       (void)cg;
+
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
@@ -274,10 +265,12 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
        return ret;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
 
        (void)sdi;
+       (void)cg;
 
        switch (key) {
        case SR_CONF_DEVICE_OPTIONS:
@@ -300,8 +293,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        struct drv_context *drvc;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
-       const struct libusb_pollfd **pfd;
-       int len, ret, i;
+       int len, ret;
        unsigned char cmd[2];
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -313,6 +305,18 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
 
        devc->cb_data = cb_data;
        devc->num_samples = 0;
+       devc->packet_len = 0;
+
+       /* Configure serial port parameters on USB-UART interface
+        * chip inside the device (just baudrate 2400 actually). */
+       cmd[0] = 0x09;
+       cmd[1] = 0x60;
+       ret = libusb_control_transfer(usb->devhdl, 0x21, 0x09, 0x0300, 0x00,
+                       cmd, 2, 5);
+       if (ret != 2) {
+               sr_dbg("Failed to configure CH9325: %s", libusb_error_name(ret));
+               return SR_ERR;
+       }
 
        /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
@@ -320,16 +324,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        if (!(devc->xfer = libusb_alloc_transfer(0)))
                return SR_ERR;
 
-       pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
-       for (i = 0; pfd[i]; i++) {
-               /* Handle USB events every 10ms. */
-               sr_source_add(pfd[i]->fd, pfd[i]->events, 10,
-                               uni_t_ut32x_handle_events, (void *)sdi);
-               /* We'll need to remove this fd later. */
-               devc->usbfd[i] = pfd[i]->fd;
-       }
-       devc->usbfd[i] = -1;
-
        /* Length of payload to follow. */
        cmd[0] = 0x01;
        if (devc->data_source == DATA_SOURCE_LIVE)
@@ -345,23 +339,27 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        }
 
        libusb_fill_bulk_transfer(devc->xfer, usb->devhdl, EP_IN, devc->buf,
-                       128, uni_t_ut32x_receive_transfer, (void *)sdi, 15);
+                       8, uni_t_ut32x_receive_transfer, (void *)sdi, 15);
        if (libusb_submit_transfer(devc->xfer) != 0) {
                libusb_free_transfer(devc->xfer);
                return SR_ERR;
        }
 
+       usb_source_add(drvc->sr_ctx, 10, uni_t_ut32x_handle_events, (void *)sdi);
+
        return SR_OK;
 }
 
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
+
        (void)cb_data;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: stop acquisition. */
+       /* Signal USB transfer handler to clean up and stop. */
+       sdi->status = SR_ST_STOPPING;
 
        return SR_OK;
 }
@@ -374,7 +372,7 @@ SR_PRIV struct sr_dev_driver uni_t_ut32x_driver_info = {
        .cleanup = cleanup,
        .scan = scan,
        .dev_list = dev_list,
-       .dev_clear = dev_clear,
+       .dev_clear = NULL,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,