[Spice-devel] [PATCH 4/5] usbdk: Add UsbDk hider interface wrapper

Christophe Fergeau cfergeau at redhat.com
Thu May 28 08:37:10 PDT 2015


On Thu, May 28, 2015 at 01:24:03PM +0300, Kirill Moizik wrote:
> From: Dmitry Fleytman <dmitry at daynix.com>
> 
> add UsbDkDataHider.h UsbDkHelperHider.h usbdk_api.h usbdk_api.c files

s/add/Add

> to allow hide devices according  auto redirect rule functionality

to allow to hide USB devices so that they can be auto-redirected ?

> 
> Signed-off-by: Kirill Moizik <kirill at daynix.com>
> Signed-off-by: Dmitry Fleytman <dmitry at daynix.com>
> ---
>  gtk/Makefile.am              |   2 +
>  gtk/UsbDk/UsbDkDataHider.h   |  35 +++++++++++++++
>  gtk/UsbDk/UsbDkHelperHider.h | 105 +++++++++++++++++++++++++++++++++++++++++++
>  gtk/usbdk_api.c              |  77 +++++++++++++++++++++++++++++++
>  gtk/usbdk_api.h              |  42 +++++++++++++++++
>  5 files changed, 261 insertions(+)
>  create mode 100644 gtk/UsbDk/UsbDkDataHider.h
>  create mode 100644 gtk/UsbDk/UsbDkHelperHider.h
>  create mode 100644 gtk/usbdk_api.c
>  create mode 100644 gtk/usbdk_api.h
> 
> diff --git a/gtk/Makefile.am b/gtk/Makefile.am
> index ed35f47..83473ac 100644
> --- a/gtk/Makefile.am
> +++ b/gtk/Makefile.am
> @@ -369,6 +369,8 @@ WIN_USB_FILES= \
>  	win-usb-clerk.h			\
>  	win-usb-driver-install.h	\
>  	win-usb-driver-install.c	\
> +	usbdk_api.h			\
> +	usbdk_api.c			\
>  	$(NULL)
>  
>  if OS_WIN32
> diff --git a/gtk/UsbDk/UsbDkDataHider.h b/gtk/UsbDk/UsbDkDataHider.h
> new file mode 100644
> index 0000000..b2a7e55
> --- /dev/null
> +++ b/gtk/UsbDk/UsbDkDataHider.h
> @@ -0,0 +1,35 @@
> +/**********************************************************************
> +* Copyright (c) 2013-2014  Red Hat, Inc.
> +*
> +* Developed by Daynix Computing LTD.
> +*
> +* Authors:
> +*     Dmitry Fleytman <dmitry at daynix.com>
> +*     Kirill Moizik <kirill at daynix.com>
> +*
> +* Licensed under the Apache License, Version 2.0 (the "License");
> +* you may not use this file except in compliance with the License.
> +* You may obtain a copy of the License at
> +*
> +* http://www.apache.org/licenses/LICENSE-2.0
> +*
> +* Unless required by applicable law or agreed to in writing, software
> +* distributed under the License is distributed on an "AS IS" BASIS,
> +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +* See the License for the specific language governing permissions and
> +* limitations under the License.
> +*
> +**********************************************************************/
> +
> +#pragma once
> +
> +#define USB_DK_HIDE_RULE_MATCH_ALL ((ULONG64)(-1))
> +
> +typedef struct tag_USB_DK_HIDE_RULE
> +{
> +    ULONG64 Hide;
> +    ULONG64 Class;
> +    ULONG64 VID;
> +    ULONG64 PID;
> +    ULONG64 BCD;
> +} USB_DK_HIDE_RULE, *PUSB_DK_HIDE_RULE;
> diff --git a/gtk/UsbDk/UsbDkHelperHider.h b/gtk/UsbDk/UsbDkHelperHider.h
> new file mode 100644
> index 0000000..309943f
> --- /dev/null
> +++ b/gtk/UsbDk/UsbDkHelperHider.h
> @@ -0,0 +1,105 @@
> +/**********************************************************************
> +* Copyright (c) 2013-2014  Red Hat, Inc.
> +*
> +* Developed by Daynix Computing LTD.
> +*
> +* Authors:
> +*     Dmitry Fleytman <dmitry at daynix.com>
> +*     Kirill Moizik <kirill at daynix.com>
> +*
> +* Licensed under the Apache License, Version 2.0 (the "License");
> +* you may not use this file except in compliance with the License.
> +* You may obtain a copy of the License at
> +*
> +* http://www.apache.org/licenses/LICENSE-2.0
> +*
> +* Unless required by applicable law or agreed to in writing, software
> +* distributed under the License is distributed on an "AS IS" BASIS,
> +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
> +* See the License for the specific language governing permissions and
> +* limitations under the License.
> +*
> +**********************************************************************/
> +
> +#pragma once
> +
> +// UsbDkHelper C-interface
> +
> +#ifdef BUILD_DLL
> +#define DLL __declspec(dllexport)
> +#else
> +#ifdef _MSC_VER
> +#define DLL __declspec(dllimport)
> +#else
> +#define DLL
> +#endif
> +#endif
> +
> +#include "UsbDkDataHider.h"
> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +// UsbDk hider API provides hide device feature that allows to detach USB device from the hosting system.

« provides a 'hide device' feature ... to detach a USB device/USB devices
from the OS? »

> +// It can be useful in various scenarios, for example when there is no drivers for USB device on hosting machine
> +// and user would like to avoid dealing with "New Hardware" wizard.

« when the OS has no drivers for a USB device and the user would like to
avoid dealing with the "New Hardware" wizard. »

> +
> +    /* Create handle to hider interface of UsbDk driver

Create a handle to the hider interface of the UsbDk driver

> +    *
> +    * @params
> +    *    IN  - None
> +    *    OUT - None
> +    *
> +    * @return
> +    *  Handle to hider interface of UsbDk driver

of the UsbDk driver

> +    *
> +    * @note
> +    *  When this handle closes UsbDk clears all rules set by UsbDk_AddHideRule()
> +    *

When this handle is closed, UsbDk...


> +    */
> +    DLL HANDLE           UsbDk_CreateHiderHandle(void);
> +
> +   /* Add a rule to determine which USB devices
> +    * to be hidden when plugged in, a rule contains:

should be hidden?

> +    *
> +    * class, vendor, product, version, allow
> +    *
> +    * Use -1 for @class/@vendor/@product/@version to accept any value.
> +    *
> +    * @params
> +    *    IN  - HiderHandle  Handle to UsbDk driver

to the UsbDk driver ?

> +             - rule - pointer to hiding rule

I'd say 'to a hiding rule'

> +    *    OUT - None
> +    *
> +    * @return
> +    *  TRUE if function succeeds

if the call succeeds ?

> +    *
> +    */
> +    DLL BOOL             UsbDk_AddHideRule(HANDLE HiderHandle, PUSB_DK_HIDE_RULE Rule);
> +
> +    /* Clear all hider rules
> +    *
> +    * @params
> +    *    IN  -  HiderHandle  Handle to UsbDk driver

to the UsbDk driver ?

> +    *    OUT - None
> +    *
> +    * @return
> +    *  TRUE if function succeeds

if the call succeeds ?

> +    *
> +    */
> +    DLL BOOL             UsbDk_ClearHideRules(HANDLE HiderHandle);
> +
> +    /* Close Handle to UsbDk hider interface

Close a UsbDk handle?

> +    *
> +    * @params
> +    *    IN  -  HiderHandle  Handle to UsbDk driver

to the UsbDk driver ?

> +    *    OUT - None
> +    *
> +    * @return
> +    * None
> +    *
> +    */
> +    DLL void             UsbDk_CloseHiderHandle(HANDLE HiderHandle);
> +#ifdef __cplusplus
> +}
> +#endif
> diff --git a/gtk/usbdk_api.c b/gtk/usbdk_api.c
> new file mode 100644
> index 0000000..c8cd9d4
> --- /dev/null
> +++ b/gtk/usbdk_api.c
> @@ -0,0 +1,77 @@
> +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> +/*
> +Copyright (C) 2011, 2012 Red Hat, Inc.
> +
> +* Authors:
> +* Dmitry Fleytman <dmitry at daynix.com>
> +* Kirill Moizik <kirill at daynix.com>
> +
> +This library is free software; you can redistribute it and/or
> +modify it under the terms of the GNU Lesser General Public
> +License as published by the Free Software Foundation; either
> +version 2.1 of the License, or (at your option) any later version.
> +
> +This library is distributed in the hope that it will be useful,
> +but WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +Lesser General Public License for more details.
> +
> +You should have received a copy of the GNU Lesser General Public
> +License along with this library; if not, see <http://www.gnu.org/licenses/>.
> +*/
> +
> +#include <windows.h>
> +#include "usbdk_api.h"
> +#include "spice-util.h"
> +
> +void usbdk_api_unload(usbdk_api_wrapper *usbdk_api)
> +{
> +    SPICE_DEBUG("Unloading UsbDk API DLL");
> +    FreeLibrary(usbdk_api->module);
> +}
> +
> +int usbdk_api_load(usbdk_api_wrapper *usbdk_api)
> +{
> +    SPICE_DEBUG("Loading UsbDk API DLL");
> +
> +    usbdk_api->module = LoadLibraryA("UsbDkHelper");
> +    if (usbdk_api->module == NULL) {
> +        DWORD err = GetLastError();
> +        SPICE_DEBUG("Failed to load UsbDkHelper.dll, error %lu", err);
> +        return -1 ;
> +    }
> +
> +    usbdk_api->CreateHandle = (USBDK_CREATEHIDERHANDLE)
> +        GetProcAddress(usbdk_api->module, "UsbDk_CreateHiderHandle");
> +    if (usbdk_api->CreateHandle == NULL) {
> +        SPICE_DEBUG("CreateHandle");
> +        goto error_unload;
> +    }
> +
> +    usbdk_api->AddRule = (USBDK_ADDHIDERULE)
> +        GetProcAddress(usbdk_api->module, "UsbDk_AddHideRule");
> +    if (usbdk_api->AddRule == NULL) {
> +        SPICE_DEBUG("AddRule");
> +        goto error_unload;
> +
> +    }
> +
> +    usbdk_api->ClearRules = (USBDK_CLEARHIDERULES)
> +        GetProcAddress(usbdk_api->module, "UsbDk_ClearHideRules");
> +    if (usbdk_api->ClearRules == NULL) {
> +        SPICE_DEBUG("ClearRules");
> +        goto error_unload;
> +    }
> +
> +    usbdk_api->CloseHiderHandle = (USBDK_CLOSEHIDERHANDLE)
> +        GetProcAddress(usbdk_api->module, "UsbDk_CloseHiderHandle");
> +    if (usbdk_api->CloseHiderHandle == NULL) {
> +        SPICE_DEBUG("CloseHiderHandle");
> +        goto error_unload;
> +    }
> +    return 0;
> +
> +error_unload:
> +    FreeLibrary(usbdk_api->module);

Feels more robust to set all function pointers to NULL so that they are
not used by mistake. I'd do that in usbdk_api_unload, and call that from
error_unload:

> +    return -1;
> +}
> diff --git a/gtk/usbdk_api.h b/gtk/usbdk_api.h
> new file mode 100644
> index 0000000..d86782a
> --- /dev/null
> +++ b/gtk/usbdk_api.h
> @@ -0,0 +1,42 @@
> +/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
> +/*
> +Copyright (C) 2011, 2012 Red Hat, Inc.
> +
> +* Authors:
> +* Dmitry Fleytman <dmitry at daynix.com>
> +* Kirill Moizik <kirill at daynix.com>
> +
> +This library is free software; you can redistribute it and/or
> +modify it under the terms of the GNU Lesser General Public
> +License as published by the Free Software Foundation; either
> +version 2.1 of the License, or (at your option) any later version.
> +
> +This library is distributed in the hope that it will be useful,
> +but WITHOUT ANY WARRANTY; without even the implied warranty of
> +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +Lesser General Public License for more details.
> +
> +You should have received a copy of the GNU Lesser General Public
> +License along with this library; if not, see <http://www.gnu.org/licenses/>.
> +*/
> +
> +#pragma once
> +
> +#include "UsbDk/UsbDkDataHider.h"
> +
> +typedef HANDLE(__cdecl *USBDK_CREATEHIDERHANDLE)(void);
> +typedef BOOL(__cdecl * USBDK_ADDHIDERULE)(HANDLE HiderHandle, PUSB_DK_HIDE_RULE rule);
> +typedef BOOL(__cdecl *USBDK_CLEARHIDERULES)(HANDLE HiderHandle);
> +typedef void(__cdecl *USBDK_CLOSEHIDERHANDLE)(HANDLE HiderHandle);
> +
> +
> +typedef struct tag_usbdk_api_wrapper {
> +    HMODULE module;
> +    USBDK_CREATEHIDERHANDLE                 CreateHandle;
> +    USBDK_ADDHIDERULE                       AddRule;
> +    USBDK_CLEARHIDERULES                    ClearRules;
> +    USBDK_CLOSEHIDERHANDLE                  CloseHiderHandle;
> +} usbdk_api_wrapper;
> +
> +int usbdk_api_load(usbdk_api_wrapper *usbdk_api);
> +void usbdk_api_unload(usbdk_api_wrapper *usbdk_api);

I'd tend to make usbdk_api_wrapper opaque, and have
HANDLE usbdk_api_create_handle(void);
usbdk_api_wrapper_add_hide_rule();
and so on.

Christophe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20150528/cf4bd675/attachment.sig>


More information about the Spice-devel mailing list