// SPDX-License-Identifier: GPL-2.0+
/*
* trans_usbg.c - USB peripheral usb9pfs configuration driver and transport.
*
* Copyright (C) 2024 Michael Grzeschik <m.grzeschik@pengutronix.de>
*/
/* Gadget usb9pfs only needs two bulk endpoints, and will use the usb9pfs
* transport to mount host exported filesystem via usb gadget.
*/
/* +--------------------------+ | +--------------------------+
* | 9PFS mounting client | | | 9PFS exporting server |
* SW | | | | |
* | (this:trans_usbg) | | |(e.g. diod or nfs-ganesha)|
* +-------------^------------+ | +-------------^------------+
* | | |
* ------------------|------------------------------------|-------------
* | | |
* +-------------v------------+ | +-------------v------------+
* | | | | |
* HW | USB Device Controller <---------> USB Host Controller |
* | | | | |
* +--------------------------+ | +--------------------------+
*/
#include <linux/cleanup.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs_context.h>
#include <linux/usb/composite.h>
#include <linux/usb/func_utils.h>
#include <net/9p/9p.h>
#include <net/9p/client.h>
#include <net/9p/transport.h>
#define DEFAULT_BUFLEN 16384
struct f_usb9pfs {
struct p9_client *client;
/* 9p request lock for en/dequeue */
spinlock_t lock;
struct usb_request *in_req;
struct usb_request *out_req;
struct usb_ep *in_ep;
struct usb_ep *out_ep;
struct completion send;
struct completion received;
unsigned int buflen;
struct usb_function function;
};
static inline struct f_usb9pfs *func_to_usb9pfs(struct usb_function *f)
{
return container_of(f, struct f_usb9pfs, function);
}
struct f_usb9pfs_opts {
struct usb_function_instance func_inst;
unsigned int buflen;
struct f_usb9pfs_dev *dev;
/* Read/write access to configfs attributes is handled by configfs.
*
* This is to protect the data from concurrent access by read/write
* and create symlink/remove symlink.
*/
struct mutex lock;
int refcnt;
};
struct f_usb9pfs_dev {
struct f_usb9pfs *usb9pfs;
struct f_usb9pfs_opts *opts;
char tag[41];
bool inuse;
struct list_head usb9pfs_instance;
};
static DEFINE_MUTEX(usb9pfs_lock);
static struct list_head usbg_instance_list;
static int usb9pfs_queue_tx(struct f_usb9pfs *usb9pfs, struct p9_req_t *p9_tx_req,
gfp_t gfp_flags)
{
struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
struct usb_request *req = usb9pfs->in_req;
int ret;
if (!(p9_tx_req->tc.size % usb9pfs->in_ep->maxpacket))
req->zero = 1;
req->buf = p9_tx_req->tc.sdata;
req->length = p9_tx_req->tc.size;
req->context = p9_tx_req;
dev_dbg(&cdev->gadget->dev, "%s usb9pfs send --> %d/%d, zero: %d\n",
usb9pfs->in_ep->name, req->actual, req->length, req->zero);
ret = usb_ep_queue(usb9pfs->in_ep, req, gfp_flags);
if (ret)
req->context = NULL;
dev_dbg(&cdev->gadget->dev, "tx submit --> %d\n", ret);
return ret;
}
static int usb9pfs_queue_rx(struct f_usb9pfs *usb9pfs, struct usb_request *req,
gfp_t gfp_flags)
{
struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
int ret;
ret = usb_ep_queue(usb9pfs->out_ep, req, gfp_flags);
dev_dbg(&cdev->gadget->dev, "rx submit --> %d\n", ret);
return ret;
}
static int usb9pfs_transmit(struct f_usb9pfs *usb9pfs, struct p9_req_t *p9_req)
{
int ret = 0;
guard(spinlock_irqsave)(&usb9pfs->lock);
ret = usb9pfs_queue_tx(usb9pfs, p9_req, GFP_ATOMIC);
if (ret)
return ret;
list_del(&p9_req->req_list);
p9_req_get(p9_req);
return ret;
}
static void usb9pfs_tx_complete(struct usb_ep *ep, struct usb_request *req)
{
struct f_usb9pfs *usb9pfs = ep->driver_data;
struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
struct p9_req_t *p9_tx_req = req->context;
unsigned long flags;
/* reset zero packages */
req->zero = 0;
if (req->status) {
dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
ep->name, req->status, req->actual, req->length);
return;
}
dev_dbg(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n