1
0
Fork 0
mirror of https://github.com/linux-usb-gadgets/libusbgx.git synced 2025-07-13 00:30:06 +03:00

function: add support for usb9pfs

This commit is contained in:
Michael Grzeschik 2024-04-29 23:57:13 +02:00
parent a5bfa81017
commit 30784e3519
6 changed files with 183 additions and 2 deletions

View file

@ -14,7 +14,7 @@ EXTRA_DIST = doxygen.cfg
library_includedir=$(includedir)/usbg
library_include_HEADERS = include/usbg/usbg.h include/usbg/usbg_version.h
function_includedir=$(includedir)/usbg/function
function_include_HEADERS = include/usbg/function/ffs.h include/usbg/function/loopback.h include/usbg/function/midi.h include/usbg/function/ms.h include/usbg/function/net.h include/usbg/function/phonet.h include/usbg/function/serial.h include/usbg/function/hid.h include/usbg/function/uac2.h include/usbg/function/uvc.h include/usbg/function/printer.h
function_include_HEADERS = include/usbg/function/ffs.h include/usbg/function/loopback.h include/usbg/function/midi.h include/usbg/function/ms.h include/usbg/function/net.h include/usbg/function/phonet.h include/usbg/function/serial.h include/usbg/function/hid.h include/usbg/function/uac2.h include/usbg/function/uvc.h include/usbg/function/printer.h include/usbg/function/9pfs.h
pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libusbgx.pc
cmakeconfigdir = $(libdir)/cmake/LibUsbgx

View file

@ -0,0 +1,71 @@
/*
* Copyright (C) 2024 Pengutronix
*
* Michael Grzeschik <m.grzeschik@pengutronix.de>
*
* 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.
*/
#ifndef USBG_FUNCTION_FS__
#define USBG_FUNCTION_FS__
#include <usbg/usbg.h>
#ifdef __cplusplus
extern "C" {
#endif
struct usbg_f_9pfs;
typedef struct usbg_f_9pfs usbg_f_9pfs;
/**
* @brief Convert from generic function to 9pfs
* @param[in] f function to be converted to 9pfs.
* Function should be of type function fs.
* @return Converted function or NULL if function hasn't suitable type
*/
usbg_f_9pfs *usbg_to_9pfs(usbg_function *f);
/**
* @brief Convert form 9pfs to a generic one
* @param[in] 9pfs function to be converted to generic one
* @return Generic usbg function
*/
usbg_function *usbg_from_9pfs(usbg_f_9pfs *p9fs);
/**
* @brief Get the device name which should be used while mounting
* 9pfs instace into newly allocated storage
* @param[in] pf Pointer to net function
* @param[out] dev_name Newly allocated string containing device name
* @return 0 on success usbg_error if error occurred.
* @note returned dev_name should be free() by caller
*/
int usbg_f_9pfs_get_dev_name(usbg_f_9pfs *p9fs, char **dev_name);
/**
* @brief Get the device name which should be used while mounting
* 9pfs instace into user buffer
* @param[in] pf Pointer to fs function
* @param[out] buf Place where ifname should be stored
* @param[in] len Size of buffer
* @return Number of bytes placed into buf (excluding '\0') or the number of
* characters which would have been written to the buffer if enough
* space had been available. (just like snprintf() family). This may
* also return negative error code from usbg_error.
*/
int usbg_f_9pfs_get_dev_name_s(usbg_f_9pfs *p9fs, char *buf, int len);
#ifdef __cplusplus
}
#endif
#endif /* USBG_FUNCTION_FS__ */

View file

@ -221,6 +221,7 @@ typedef enum
USBG_F_UAC2,
USBG_F_UVC,
USBG_F_PRINTER,
USBG_F_9PFS,
USBG_FUNCTION_TYPE_MAX,
} usbg_function_type;

View file

@ -1,6 +1,6 @@
AUTOMAKE_OPTIONS = std-options subdir-objects
lib_LTLIBRARIES = libusbgx.la
libusbgx_la_SOURCES = usbg.c usbg_error.c usbg_common.c function/ether.c function/ffs.c function/midi.c function/ms.c function/phonet.c function/serial.c function/loopback.c function/hid.c function/uac2.c function/uvc.c function/printer.c
libusbgx_la_SOURCES = usbg.c usbg_error.c usbg_common.c function/ether.c function/ffs.c function/midi.c function/ms.c function/phonet.c function/serial.c function/loopback.c function/hid.c function/uac2.c function/uvc.c function/printer.c function/9pfs.c
if TEST_GADGET_SCHEMES
libusbgx_la_SOURCES += usbg_schemes_libconfig.c usbg_common_libconfig.c
else

107
src/function/9pfs.c Normal file
View file

@ -0,0 +1,107 @@
/*
* Copyright (C) 2024 Pengutronix
*
* Michael Grzeschik <m.grzeschik@pengutronix.de>
*
* 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.
*/
#include "usbg/usbg.h"
#include "usbg/usbg_internal.h"
#include "usbg/function/9pfs.h"
#include <malloc.h>
#ifdef HAS_GADGET_SCHEMES
#include <libconfig.h>
#endif
struct usbg_f_9pfs {
struct usbg_function func;
};
GENERIC_ALLOC_INST(p9fs, struct usbg_f_9pfs, func);
GENERIC_FREE_INST(p9fs, struct usbg_f_9pfs, func);
static int p9fs_set_attrs(struct usbg_function *f, void *f_attrs)
{
const char *dev_name = *(const char **)f_attrs;
return dev_name && dev_name[0] ? USBG_ERROR_INVALID_PARAM
: USBG_SUCCESS;
}
static int p9fs_get_attrs(struct usbg_function *f, void *f_attrs)
{
return usbg_f_9pfs_get_dev_name(usbg_to_9pfs(f), f_attrs);
}
static void p9fs_cleanup_attrs(struct usbg_function *f, void *f_attrs)
{
free(*(char **)f_attrs);
}
static int p9fs_libconfig_import(struct usbg_function *f,
config_setting_t *root)
{
return USBG_SUCCESS;
}
static int p9fs_libconfig_export(struct usbg_function *f,
config_setting_t *root)
{
return USBG_SUCCESS;
}
struct usbg_function_type usbg_f_type_9pfs = {
.name = "usb9pfs",
.alloc_inst = p9fs_alloc_inst,
.free_inst = p9fs_free_inst,
.set_attrs = p9fs_set_attrs,
.get_attrs = p9fs_get_attrs,
.cleanup_attrs = p9fs_cleanup_attrs,
.import = p9fs_libconfig_import,
.export = p9fs_libconfig_export,
};
/* API implementation */
usbg_f_9pfs *usbg_to_9pfs(usbg_function *f)
{
return f->ops == &usbg_f_type_9pfs ?
container_of(f, struct usbg_f_9pfs, func) : NULL;
}
usbg_function *usbg_from_9pfs(usbg_f_9pfs *p9fs)
{
return &p9fs->func;
}
int usbg_f_9pfs_get_dev_name(usbg_f_9pfs *p9fs, char **dev_name)
{
if (!p9fs || !dev_name)
return USBG_ERROR_INVALID_PARAM;
*dev_name = strdup(p9fs->func.instance);
if (!*dev_name)
return USBG_ERROR_NO_MEM;
return 0;
}
int usbg_f_9pfs_get_dev_name_s(usbg_f_9pfs *p9fs, char *buf, int len)
{
if (!p9fs || !buf)
return USBG_ERROR_INVALID_PARAM;
return snprintf(buf, len, "%s", p9fs->func.instance);
}

View file

@ -54,6 +54,7 @@ extern struct usbg_function_type usbg_f_type_hid;
extern struct usbg_function_type usbg_f_type_uac2;
extern struct usbg_function_type usbg_f_type_uvc;
extern struct usbg_function_type usbg_f_type_printer;
extern struct usbg_function_type usbg_f_type_9pfs;
/**
* @var function_types
@ -77,6 +78,7 @@ struct usbg_function_type* function_types[] = {
[USBG_F_UAC2] = &usbg_f_type_uac2,
[USBG_F_UVC] = &usbg_f_type_uvc,
[USBG_F_PRINTER] = &usbg_f_type_printer,
[USBG_F_9PFS] = &usbg_f_type_9pfs,
};
ARRAY_SIZE_SENTINEL(function_types, USBG_FUNCTION_TYPE_MAX);