From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x227iKlq0b7+dn/6yzTTv/ZQbNjIEyPfoYK3pUmW3lqxyk7Fv+PfnCuTUJOOSMlUGcErXQoBq ARC-Seal: i=1; a=rsa-sha256; t=1517301448; cv=none; d=google.com; s=arc-20160816; b=FTvJR7iK3n/HPgUF+262iS0g1tNAJFiIAHQ8Ag+QFXl0mlWJ3s/zQ62mHS8vvy1zc0 65yFGbEOB4Nlh+0y+CUjzmz60cxzBMX0BVQnpn1M1PdoBsCOJgOeXlb0aNA96QZI/jms 3LeJGkRqBZdYSASdkFKaJ+dm6AVB0xFBnK4+9A+QOuknkGTJz2SGXuCO60ZzYVmNR33P ACzUj+ywka02bMvdt1LOyoBLhTSnOiowq7FP8ala4bx3yQVr2qToSgtL27AOBKrWSjjm 6uU4p0vA4EUsfOJcszwFh4MyDNIhml7mwlJAT841carnxsQyqU+mkmTqnXajG8zgifTg 3W8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=fK9smn6v1LxmbeneioGYtn5hY7sagpnyvSXBQkUJRJM=; b=UphIeBOgW67QDfq2hIX22T/665mVxf0C3eRsxGzap99DchvkoEJA/MKILRep3HcXSN g5ohe1iqH+diNl7d8CoTqETh1ogc8B5Mw8N9n1B4dSCz/X3Iri4HR1RK8rjg7AwX1Aes hsY/7cmx5uRny9WOMGW2AMZM2h84x9o9YIod+++gRo+tx3jmh6LnTw45wKhw2XVx0x95 3qCv8YAjwGvdUiPQLs59ZVNK2RNaOyNMlSLgRK1ccYA/8rSpPmoN/XmwFK3JjvIQ/QCQ tu62w6Xqi/YScQ53JfMY+AylUaz9TAZ9vA/ThD3PqZBis8lEorkPtWOvj24ffk6IxWHU PKvw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of salva@qindel.com designates 89.140.90.34 as permitted sender) smtp.mailfrom=salva@qindel.com Authentication-Results: mx.google.com; spf=pass (google.com: domain of salva@qindel.com designates 89.140.90.34 as permitted sender) smtp.mailfrom=salva@qindel.com From: Salvador Fandino To: linux-usb@vger.kernel.org Cc: gregkh@linuxfoundation.org, valentina.manea.m@gmail.com, shuah@kernel.org, linux-kernel@vger.kernel.org, =?UTF-8?q?Salvador=20Fandi=C3=B1o?= Subject: [PATCH 2/4] libusbip: use per vhci_hcd controller attributes Date: Tue, 30 Jan 2018 09:36:28 +0100 Message-Id: <20180130083630.26501-3-salva@qindel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180130083630.26501-1-salva@qindel.com> References: <20180130083630.26501-1-salva@qindel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: =?utf-8?q?1591005883836686075?= X-GMAIL-MSGID: =?utf-8?q?1591005883836686075?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: From: Salvador Fandi=C3=B1o Now every vhci_hcd device is controlled by its own sysfs attributes (before all of them were controlled by vhci_hcd.0 attributes). This patch addapts libusbip to this new interface. Before this patch the library did not provide any mean to access a specific vhci_hcd device. In order to keep backward compatibility while at the same time allowing to access vhci_hcd devices independently the following approach has been taken: New functions accepting a vhci_hcd path or an index have been added ("usbip_vhci_driver_open_path" and "usbip_vhci_driver_open_ix" respectively). The old function "usbip_vhci_driver_open" can only be used to open "vhci_hcd.0"). That will make old programs fail to use any other vhci_hcd device, but as that functionality was broken in the kernel anyway until very recently (see 1ac7c8a78be85f84b019d3d2742d1a9f07255cc5) we consider it a good compromise. Signed-off-by: Salvador Fandi=C3=B1o --- tools/usb/usbip/libsrc/vhci_driver.c | 105 +++++++++++++++++++++--------= ------ tools/usb/usbip/libsrc/vhci_driver.h | 12 +++- 2 files changed, 74 insertions(+), 43 deletions(-) diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsr= c/vhci_driver.c index c9c81614a66a..34d15167bab3 100644 --- a/tools/usb/usbip/libsrc/vhci_driver.c +++ b/tools/usb/usbip/libsrc/vhci_driver.c @@ -114,13 +114,16 @@ static int refresh_imported_device_list(void) char status[MAX_STATUS_NAME+1] =3D "status"; int i, ret; =20 - for (i =3D 0; i < vhci_driver->ncontrollers; i++) { + for (i =3D 0; ; i++) { if (i > 0) snprintf(status, sizeof(status), "status.%d", i); =20 attr_status =3D udev_device_get_sysattr_value(vhci_driver->hc_device, status); if (!attr_status) { + if (i > 0) + break; + err("udev_device_get_sysattr_value failed"); return -1; } @@ -148,33 +151,6 @@ static int get_nports(void) return (int)strtoul(attr_nports, NULL, 10); } =20 -static int vhci_hcd_filter(const struct dirent *dirent) -{ - return strcmp(dirent->d_name, "vhci_hcd") >=3D 0; -} - -static int get_ncontrollers(void) -{ - struct dirent **namelist; - struct udev_device *platform; - int n; - - platform =3D udev_device_get_parent(vhci_driver->hc_device); - if (platform =3D=3D NULL) - return -1; - - n =3D scandir(udev_device_get_syspath(platform), &namelist, vhci_hcd_fi= lter, NULL); - if (n < 0) - err("scandir failed"); - else { - for (int i =3D 0; i < n; i++) - free(namelist[i]); - free(namelist); - } - - return n; -} - /* * Read the given port's record. * @@ -199,7 +175,8 @@ static int read_record(int rhport, char *host, unsign= ed long host_len, if (!buffer) return -1; =20 - snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d", rhport); + snprintf(path, PATH_MAX, VHCI_STATE_PATH"/port%d-%d", + vhci_driver->ix, rhport); =20 file =3D fopen(path, "r"); if (!file) { @@ -238,9 +215,18 @@ static int read_record(int rhport, char *host, unsig= ned long host_len, return 0; } =20 +static int vhci_path_to_ix(const char *path) +{ + int i =3D strlen(path); + + while (--i >=3D 0 && isdigit(path[i])) + ; + return atoi(path + i + 1); +} + /* ---------------------------------------------------------------------= - */ =20 -int usbip_vhci_driver_open(void) +int usbip_vhci_driver_open_path(const char *path) { udev_context =3D udev_new(); if (!udev_context) { @@ -252,14 +238,15 @@ int usbip_vhci_driver_open(void) =20 /* will be freed in usbip_driver_close() */ vhci_driver->hc_device =3D - udev_device_new_from_subsystem_sysname(udev_context, - USBIP_VHCI_BUS_TYPE, - USBIP_VHCI_DEVICE_NAME); + udev_device_new_from_syspath(udev_context, path); + if (!vhci_driver->hc_device) { err("udev_device_new_from_subsystem_sysname failed"); goto err; } =20 + vhci_driver->ix =3D vhci_path_to_ix(path); + vhci_driver->nports =3D get_nports(); dbg("available ports: %d", vhci_driver->nports); =20 @@ -271,14 +258,6 @@ int usbip_vhci_driver_open(void) goto err; } =20 - vhci_driver->ncontrollers =3D get_ncontrollers(); - dbg("available controllers: %d", vhci_driver->ncontrollers); - - if (vhci_driver->ncontrollers <=3D0) { - err("no available usb controllers"); - goto err; - } - if (refresh_imported_device_list()) goto err; =20 @@ -297,6 +276,45 @@ int usbip_vhci_driver_open(void) return -1; } =20 +int usbip_vhci_driver_open(void) +{ + return usbip_vhci_driver_open_ix(0); +} + +int usbip_vhci_driver_open_ix(int vhci_ix) +{ + struct udev *udev_context; + struct udev_device *dev =3D NULL; + char vhci_name[PATH_MAX + 1]; + int len, rc =3D -1; + + len =3D snprintf(vhci_name, PATH_MAX, + "%s.%d", USBIP_VHCI_DEVICE_NAME_PREFIX, vhci_ix); + if (len >=3D PATH_MAX) { + err("vhci device name is too long"); + return -1; + } + + udev_context =3D udev_new(); + if (!udev_context) { + err("udev_new failed"); + return -1; + } + + dev =3D udev_device_new_from_subsystem_sysname(udev_context, + USBIP_VHCI_BUS_TYPE, + vhci_name); + if (!dev) { + err("udev_device_from_subsystem_sysname failed to open %s", + vhci_name); + } else { + rc =3D usbip_vhci_driver_open_path(udev_device_get_syspath(dev)); + udev_device_unref(dev); + } + + udev_unref(udev_context); + return rc; +} =20 void usbip_vhci_driver_close(void) { @@ -459,3 +477,8 @@ int usbip_vhci_imported_device_dump(struct usbip_impo= rted_device *idev) =20 return 0; } + +int usbip_vhci_driver_ix(void) +{ + return vhci_driver->ix; +} diff --git a/tools/usb/usbip/libsrc/vhci_driver.h b/tools/usb/usbip/libsr= c/vhci_driver.h index 418b404d5121..40537f4f8917 100644 --- a/tools/usb/usbip/libsrc/vhci_driver.h +++ b/tools/usb/usbip/libsrc/vhci_driver.h @@ -12,7 +12,9 @@ #include "usbip_common.h" =20 #define USBIP_VHCI_BUS_TYPE "platform" -#define USBIP_VHCI_DEVICE_NAME "vhci_hcd.0" +#define USBIP_VHCI_DEVICE_NAME_PREFIX "vhci_hcd" +#define USBIP_VHCI_DEVICE_NAME_0 (USBIP_VHCI_DEVICE_NAME_PREFIX ".0") +#define USBIP_VHCI_DEVICE_NAME_PATTERN (USBIP_VHCI_DEVICE_NAME_PREFIX ".= *") #define MAXNPORT 128 =20 enum hub_speed { @@ -39,7 +41,7 @@ struct usbip_vhci_driver { /* /sys/devices/platform/vhci_hcd */ struct udev_device *hc_device; =20 - int ncontrollers; + int ix; int nports; struct usbip_imported_device idev[MAXNPORT]; }; @@ -47,7 +49,11 @@ struct usbip_vhci_driver { =20 extern struct usbip_vhci_driver *vhci_driver; =20 +/* will be removed */ int usbip_vhci_driver_open(void); + +int usbip_vhci_driver_open_path(const char *); +int usbip_vhci_driver_open_ix(int vhci_ix); void usbip_vhci_driver_close(void); =20 int usbip_vhci_refresh_device_list(void); @@ -65,4 +71,6 @@ int usbip_vhci_detach_device(uint8_t port); =20 int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev); =20 +int usbip_vhci_driver_ix(void); + #endif /* __VHCI_DRIVER_H */ --=20 2.14.1