All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony PERARD <anthony.perard@citrix.com>
To: <xen-devel@lists.xenproject.org>
Cc: Anthony PERARD <anthony.perard@citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>, Wei Liu <wl@xen.org>
Subject: [Xen-devel] [PATCH 14/15] libxl_usb: usbctrl, make use of generic device handling functions
Date: Fri, 14 Jun 2019 12:24:43 +0100	[thread overview]
Message-ID: <20190614112444.29980-15-anthony.perard@citrix.com> (raw)
In-Reply-To: <20190614112444.29980-1-anthony.perard@citrix.com>

Two functions in generate `libxl_device_usbctrl' can be replaced by
generic macro:
- libxl_device_usbctrl_list -> LIBXL_DEFINE_DEVICE_LIST
- libxl_devid_to_device_usbctrl -> LIBXL_DEFINE_DEVID_TO_DEVICE

This patch only needs to define `libxl__usbctrl_devtype.from_xenstore'
to makes use of them.

Small change, libxl_devid_to_device_usbctrl doesn't list all usbctrl
anymore before finding the right one.

Signed-off-by: Anthony PERARD <anthony.perard@citrix.com>
---
 tools/libxl/libxl_usb.c | 122 +++++++++++-----------------------------
 1 file changed, 32 insertions(+), 90 deletions(-)

diff --git a/tools/libxl/libxl_usb.c b/tools/libxl/libxl_usb.c
index 9851fe8468..de49f8620c 100644
--- a/tools/libxl/libxl_usb.c
+++ b/tools/libxl/libxl_usb.c
@@ -563,81 +563,53 @@ void libxl__initiate_device_usbctrl_remove(libxl__egc *egc,
     return;
 }
 
-libxl_device_usbctrl *
-libxl_device_usbctrl_list(libxl_ctx *ctx, uint32_t domid, int *num)
+static int libxl__usbctrl_from_xenstore(libxl__gc *gc,
+                                        const char *libxl_path,
+                                        libxl_devid devid,
+                                        libxl_device_usbctrl *usbctrl_r)
 {
-    GC_INIT(ctx);
-    libxl_device_usbctrl *usbctrls = NULL;
-    char *libxl_vusbs_path = NULL;
-    char **entry = NULL;
-    unsigned int nentries = 0;
-
-    *num = 0;
-
-    libxl_vusbs_path = GCSPRINTF("%s/device/%s",
-                     libxl__xs_libxl_path(gc, domid),
-                     libxl__device_kind_to_string(LIBXL__DEVICE_KIND_VUSB));
-    entry = libxl__xs_directory(gc, XBT_NULL, libxl_vusbs_path, &nentries);
-
-    if (entry && nentries) {
-        usbctrls = libxl__zalloc(NOGC, sizeof(*usbctrls) * nentries);
-        libxl_device_usbctrl *usbctrl;
-        libxl_device_usbctrl *end = usbctrls + nentries;
-        for (usbctrl = usbctrls;
-             usbctrl < end;
-             usbctrl++, entry++, (*num)++) {
-            const char *tmp, *be_path, *libxl_path;
-            int ret;
-
-            libxl_device_usbctrl_init(usbctrl);
-            usbctrl->devid = atoi(*entry);
+    int rc;
+    const char *tmp;
+    const char *be_path;
 
 #define READ_SUBPATH(path, subpath) ({                                  \
-        ret = libxl__xs_read_checked(gc, XBT_NULL,                      \
+        rc = libxl__xs_read_checked(gc, XBT_NULL,                      \
                                      GCSPRINTF("%s/" subpath, path),    \
                                      &tmp);                             \
-        if (ret) goto out;                                              \
+        if (rc) goto out;                                              \
         (char *)tmp;                                                    \
     })
 
 #define READ_SUBPATH_INT(path, subpath) ({                              \
-        ret = libxl__xs_read_checked(gc, XBT_NULL,                      \
+        rc = libxl__xs_read_checked(gc, XBT_NULL,                      \
                                      GCSPRINTF("%s/" subpath, path),    \
                                      &tmp);                             \
-        if (ret) goto out;                                              \
+        if (rc) goto out;                                              \
         tmp ? atoi(tmp) : -1;                                           \
     })
 
-            libxl_path = GCSPRINTF("%s/%s", libxl_vusbs_path, *entry);
-            libxl_usbctrl_type_from_string(READ_SUBPATH(libxl_path, "type"),
-                                           &usbctrl->type);
-            if (usbctrl->type == LIBXL_USBCTRL_TYPE_DEVICEMODEL) {
-                be_path = libxl_path;
-                ret = libxl__get_domid(gc, &usbctrl->backend_domid);
-            } else {
-                be_path = READ_SUBPATH(libxl_path, "backend");
-                if (!be_path) goto out;
-                ret = libxl__backendpath_parse_domid(gc, be_path,
-                                                     &usbctrl->backend_domid);
-            }
-            if (ret) goto out;
-            usbctrl->version = READ_SUBPATH_INT(be_path, "usb-ver");
-            usbctrl->ports = READ_SUBPATH_INT(be_path, "num-ports");
+    usbctrl_r->devid = devid;
+    libxl_usbctrl_type_from_string(READ_SUBPATH(libxl_path, "type"),
+                                   &usbctrl_r->type);
+    if (usbctrl_r->type == LIBXL_USBCTRL_TYPE_DEVICEMODEL) {
+        be_path = libxl_path;
+        rc = libxl__get_domid(gc, &usbctrl_r->backend_domid);
+    } else {
+        be_path = READ_SUBPATH(libxl_path, "backend");
+        if (!be_path) goto out;
+        rc = libxl__backendpath_parse_domid(gc, be_path,
+                                             &usbctrl_r->backend_domid);
+    }
+    if (rc) goto out;
+    usbctrl_r->version = READ_SUBPATH_INT(be_path, "usb-ver");
+    usbctrl_r->ports = READ_SUBPATH_INT(be_path, "num-ports");
 
 #undef READ_SUBPATH
 #undef READ_SUBPATH_INT
-       }
-    }
-
-    GC_FREE;
-    return usbctrls;
-
 out:
-    LOGD(ERROR, domid, "Unable to list USB Controllers");
-    libxl_device_usbctrl_list_free(usbctrls, *num);
-    GC_FREE;
-    *num = 0;
-    return NULL;
+    if (rc)
+        libxl_device_usbctrl_dispose(usbctrl_r);
+    return rc;
 }
 
 int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid,
@@ -705,30 +677,6 @@ int libxl_device_usbctrl_getinfo(libxl_ctx *ctx, uint32_t domid,
     return rc;
 }
 
-int libxl_devid_to_device_usbctrl(libxl_ctx *ctx,
-                                  uint32_t domid,
-                                  int devid,
-                                  libxl_device_usbctrl *usbctrl)
-{
-    libxl_device_usbctrl *usbctrls;
-    int nb = 0;
-    int i, rc;
-
-    usbctrls = libxl_device_usbctrl_list(ctx, domid, &nb);
-    if (!usbctrls) return ERROR_FAIL;
-
-    rc = ERROR_FAIL;
-    for (i = 0; i < nb; i++) {
-        if (devid == usbctrls[i].devid) {
-            libxl_device_usbctrl_copy(ctx, usbctrl, &usbctrls[i]);
-            rc = 0;
-            break;
-        }
-    }
-
-    libxl_device_usbctrl_list_free(usbctrls, nb);
-    return rc;
-}
 
 static char *usbdev_busaddr_to_busid(libxl__gc *gc, int bus, int addr)
 {
@@ -1945,15 +1893,6 @@ static int libxl_device_usbdev_compare(const libxl_device_usbdev *d1,
     return COMPARE_USB(d1, d2);
 }
 
-void libxl_device_usbctrl_list_free(libxl_device_usbctrl *list, int nr)
-{
-   int i;
-
-   for (i = 0; i < nr; i++)
-       libxl_device_usbctrl_dispose(&list[i]);
-   free(list);
-}
-
 void libxl_device_usbdev_list_free(libxl_device_usbdev *list, int nr)
 {
    int i;
@@ -1965,7 +1904,10 @@ void libxl_device_usbdev_list_free(libxl_device_usbdev *list, int nr)
 
 #define libxl__device_usbctrl_update_devid NULL
 
+LIBXL_DEFINE_DEVID_TO_DEVICE(usbctrl)
+LIBXL_DEFINE_DEVICE_LIST(usbctrl)
 DEFINE_DEVICE_TYPE_STRUCT(usbctrl, VUSB,
+    .from_xenstore = (device_from_xenstore_fn_t)libxl__usbctrl_from_xenstore,
     .dm_needed = libxl_device_usbctrl_dm_needed
 );
 
-- 
Anthony PERARD


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  parent reply	other threads:[~2019-06-14 11:28 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-14 11:24 [Xen-devel] [PATCH 00/15] Some cleanup of libxl Anthony PERARD
2019-06-14 11:24 ` [Xen-devel] [PATCH 01/15] libxl: Rename struct libxl_device_type to libxl__device_type Anthony PERARD
2019-09-17 16:28   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 02/15] libxl: Remove unused variable in libxl__device_pci_add_xenstore Anthony PERARD
2019-09-17 16:31   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 03/15] libxl_pci: Make libxl__create_pci_backend static Anthony PERARD
2019-09-17 16:40   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 04/15] libxl_pci: Constify arg `pcidev' of libxl__device_pci_add_xenstore Anthony PERARD
2019-09-17 16:41   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 05/15] libxl_pci: `starting' is a bool Anthony PERARD
2019-09-17 16:41   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 06/15] libxl_dom_save: Reorder functions for switch_qemu_logdirty Anthony PERARD
2019-09-17 16:41   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 07/15] libxl_dm: Fix initialisation of libxl__stub_dm_spawn_state Anthony PERARD
2019-09-17 16:42   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 08/15] libxl: Comment libxl__dm_spawn_state aboud init and dispose Anthony PERARD
2019-09-17 16:42   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 09/15] libxl_domain: Cleanup libxl__destroy_domid Anthony PERARD
2019-09-17 16:42   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 10/15] libxl_usb: Use proper domid value, from libxl__device Anthony PERARD
2019-09-17 16:43   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 11/15] libxl_usb: Fix wrong usage of asserts Anthony PERARD
2019-09-17 16:44   ` Ian Jackson
2019-09-18 10:17     ` Anthony PERARD
2019-09-18 10:39       ` Ian Jackson
2019-09-19 10:03         ` [Xen-devel] [PATCH v2 " Anthony PERARD
2019-09-19 11:21           ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 12/15] libxl_usb: Fix libxl_device_usbctrl_getinfo Anthony PERARD
2019-09-17 16:44   ` Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 13/15] libxl: Constify libxl_device_* param of *_getinfo Anthony PERARD
2019-09-17 16:45   ` Ian Jackson
2019-06-14 11:24 ` Anthony PERARD [this message]
2019-09-17 16:46   ` [Xen-devel] [PATCH 14/15] libxl_usb: usbctrl, make use of generic device handling functions Ian Jackson
2019-06-14 11:24 ` [Xen-devel] [PATCH 15/15] libxl_usb: Use usbctrl instead of usbctrlinfo Anthony PERARD
2019-09-17 16:46   ` Ian Jackson
2019-09-19 17:07 ` [Xen-devel] [PATCH 00/15] Some cleanup of libxl Anthony PERARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190614112444.29980-15-anthony.perard@citrix.com \
    --to=anthony.perard@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.