File Index
Files and directories
Sign in
FILE

trans_usbg.c

File preview with syntax highlighting.

linux-6.19.14/net/9p/trans_usbg.c C
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * trans_usbg.c - USB peripheral usb9pfs configuration driver and transport.
  4 *
  5 * Copyright (C) 2024 Michael Grzeschik <m.grzeschik@pengutronix.de>
  6 */
  7
  8/* Gadget usb9pfs only needs two bulk endpoints, and will use the usb9pfs
  9 * transport to mount host exported filesystem via usb gadget.
 10 */
 11
 12/*     +--------------------------+    |    +--------------------------+
 13 *     |  9PFS mounting client    |    |    |  9PFS exporting server   |
 14 *  SW |                          |    |    |                          |
 15 *     |   (this:trans_usbg)      |    |    |(e.g. diod or nfs-ganesha)|
 16 *     +-------------^------------+    |    +-------------^------------+
 17 *                   |                 |                  |
 18 * ------------------|------------------------------------|-------------
 19 *                   |                 |                  |
 20 *     +-------------v------------+    |    +-------------v------------+
 21 *     |                          |    |    |                          |
 22 *  HW |   USB Device Controller  <--------->   USB Host Controller    |
 23 *     |                          |    |    |                          |
 24 *     +--------------------------+    |    +--------------------------+
 25 */
 26
 27#include <linux/cleanup.h>
 28#include <linux/kernel.h>
 29#include <linux/module.h>
 30#include <linux/fs_context.h>
 31#include <linux/usb/composite.h>
 32#include <linux/usb/func_utils.h>
 33
 34#include <net/9p/9p.h>
 35#include <net/9p/client.h>
 36#include <net/9p/transport.h>
 37
 38#define DEFAULT_BUFLEN        16384
 39
 40struct f_usb9pfs {
 41	struct p9_client *client;
 42
 43	/* 9p request lock for en/dequeue */
 44	spinlock_t lock;
 45
 46	struct usb_request *in_req;
 47	struct usb_request *out_req;
 48
 49	struct usb_ep *in_ep;
 50	struct usb_ep *out_ep;
 51
 52	struct completion send;
 53	struct completion received;
 54
 55	unsigned int buflen;
 56
 57	struct usb_function function;
 58};
 59
 60static inline struct f_usb9pfs *func_to_usb9pfs(struct usb_function *f)
 61{
 62	return container_of(f, struct f_usb9pfs, function);
 63}
 64
 65struct f_usb9pfs_opts {
 66	struct usb_function_instance func_inst;
 67	unsigned int buflen;
 68
 69	struct f_usb9pfs_dev *dev;
 70
 71	/* Read/write access to configfs attributes is handled by configfs.
 72	 *
 73	 * This is to protect the data from concurrent access by read/write
 74	 * and create symlink/remove symlink.
 75	 */
 76	struct mutex lock;
 77	int refcnt;
 78};
 79
 80struct f_usb9pfs_dev {
 81	struct f_usb9pfs *usb9pfs;
 82	struct f_usb9pfs_opts *opts;
 83	char tag[41];
 84	bool inuse;
 85
 86	struct list_head usb9pfs_instance;
 87};
 88
 89static DEFINE_MUTEX(usb9pfs_lock);
 90static struct list_head usbg_instance_list;
 91
 92static int usb9pfs_queue_tx(struct f_usb9pfs *usb9pfs, struct p9_req_t *p9_tx_req,
 93			    gfp_t gfp_flags)
 94{
 95	struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
 96	struct usb_request *req = usb9pfs->in_req;
 97	int ret;
 98
 99	if (!(p9_tx_req->tc.size % usb9pfs->in_ep->maxpacket))
100		req->zero = 1;
101
102	req->buf = p9_tx_req->tc.sdata;
103	req->length = p9_tx_req->tc.size;
104	req->context = p9_tx_req;
105
106	dev_dbg(&cdev->gadget->dev, "%s usb9pfs send --> %d/%d, zero: %d\n",
107		usb9pfs->in_ep->name, req->actual, req->length, req->zero);
108
109	ret = usb_ep_queue(usb9pfs->in_ep, req, gfp_flags);
110	if (ret)
111		req->context = NULL;
112
113	dev_dbg(&cdev->gadget->dev, "tx submit --> %d\n", ret);
114
115	return ret;
116}
117
118static int usb9pfs_queue_rx(struct f_usb9pfs *usb9pfs, struct usb_request *req,
119			    gfp_t gfp_flags)
120{
121	struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
122	int ret;
123
124	ret = usb_ep_queue(usb9pfs->out_ep, req, gfp_flags);
125
126	dev_dbg(&cdev->gadget->dev, "rx submit --> %d\n", ret);
127
128	return ret;
129}
130
131static int usb9pfs_transmit(struct f_usb9pfs *usb9pfs, struct p9_req_t *p9_req)
132{
133	int ret = 0;
134
135	guard(spinlock_irqsave)(&usb9pfs->lock);
136
137	ret = usb9pfs_queue_tx(usb9pfs, p9_req, GFP_ATOMIC);
138	if (ret)
139		return ret;
140
141	list_del(&p9_req->req_list);
142
143	p9_req_get(p9_req);
144
145	return ret;
146}
147
148static void usb9pfs_tx_complete(struct usb_ep *ep, struct usb_request *req)
149{
150	struct f_usb9pfs *usb9pfs = ep->driver_data;
151	struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
152	struct p9_req_t *p9_tx_req = req->context;
153	unsigned long flags;
154
155	/* reset zero packages */
156	req->zero = 0;
157
158	if (req->status) {
159		dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
160			ep->name, req->status, req->actual, req->length);
161		return;
162	}
163
164	dev_dbg(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
165		ep->name, req->status, req->actual, req->length);
166
167	spin_lock_irqsave(&usb9pfs->lock, flags);
168	WRITE_ONCE(p9_tx_req->status, REQ_STATUS_SENT);
169
170	p9_req_put(usb9pfs->client, p9_tx_req);
171
172	req->context = NULL;
173
174	spin_unlock_irqrestore(&usb9pfs->lock, flags);
175
176	complete(&usb9pfs->send);
177}
178
179static struct p9_req_t *usb9pfs_rx_header(struct f_usb9pfs *usb9pfs, void *buf)
180{
181	struct p9_req_t *p9_rx_req;
182	struct p9_fcall	rc;
183	int ret;
184
185	/* start by reading header */
186	rc.sdata = buf;
187	rc.offset = 0;
188	rc.capacity = P9_HDRSZ;
189	rc.size = P9_HDRSZ;
190
191	p9_debug(P9_DEBUG_TRANS, "mux %p got %zu bytes\n", usb9pfs,
192		 rc.capacity - rc.offset);
193
194	ret = p9_parse_header(&rc, &rc.size, NULL, NULL, 0);
195	if (ret) {
196		p9_debug(P9_DEBUG_ERROR,
197			 "error parsing header: %d\n", ret);
198		return NULL;
199	}
200
201	p9_debug(P9_DEBUG_TRANS,
202		 "mux %p pkt: size: %d bytes tag: %d\n",
203		 usb9pfs, rc.size, rc.tag);
204
205	p9_rx_req = p9_tag_lookup(usb9pfs->client, rc.tag);
206	if (!p9_rx_req || p9_rx_req->status != REQ_STATUS_SENT) {
207		p9_debug(P9_DEBUG_ERROR, "Unexpected packet tag %d\n", rc.tag);
208		return NULL;
209	}
210
211	if (rc.size > p9_rx_req->rc.capacity) {
212		p9_debug(P9_DEBUG_ERROR,
213			 "requested packet size too big: %d for tag %d with capacity %zd\n",
214			 rc.size, rc.tag, p9_rx_req->rc.capacity);
215		p9_req_put(usb9pfs->client, p9_rx_req);
216		return NULL;
217	}
218
219	if (!p9_rx_req->rc.sdata) {
220		p9_debug(P9_DEBUG_ERROR,
221			 "No recv fcall for tag %d (req %p), disconnecting!\n",
222			 rc.tag, p9_rx_req);
223		p9_req_put(usb9pfs->client, p9_rx_req);
224		return NULL;
225	}
226
227	return p9_rx_req;
228}
229
230static void usb9pfs_rx_complete(struct usb_ep *ep, struct usb_request *req)
231{
232	struct f_usb9pfs *usb9pfs = ep->driver_data;
233	struct usb_composite_dev *cdev = usb9pfs->function.config->cdev;
234	struct p9_req_t *p9_rx_req;
235	unsigned int req_size = req->actual;
236	int status = REQ_STATUS_RCVD;
237
238	if (req->status) {
239		dev_err(&cdev->gadget->dev, "%s usb9pfs complete --> %d, %d/%d\n",
240			ep->name, req->status, req->actual, req->length);
241		return;
242	}
243
244	p9_rx_req = usb9pfs_rx_header(usb9pfs, req->buf);
245	if (!p9_rx_req)
246		return;
247
248	if (req_size > p9_rx_req->rc.capacity) {
249		dev_err(&cdev->gadget->dev,
250			"%s received data size %u exceeds buffer capacity %zu\n",
251			ep->name, req_size, p9_rx_req->rc.capacity);
252		req_size = 0;
253		status = REQ_STATUS_ERROR;
254	}
255
256	memcpy(p9_rx_req->rc.sdata, req->buf, req_size);
257
258	p9_rx_req->rc.size = req_size;
259
260	p9_client_cb(usb9pfs->client, p9_rx_req, status);
261	p9_req_put(usb9pfs->client, p9_rx_req);
262
263	complete(&usb9pfs->received);
264}
265
266static void disable_ep(struct usb_composite_dev *cdev, struct usb_ep *ep)
267{
268	int value;
269
270	value = usb_ep_disable(ep);
271	if (value < 0)
272		dev_info(&cdev->gadget->dev,
273			 "disable %s --> %d\n", ep->name, value);
274}
275
276static void disable_usb9pfs(struct f_usb9pfs *usb9pfs)
277{
278	struct usb_composite_dev *cdev =
279		usb9pfs->function.config->cdev;
280
281	if (usb9pfs->in_req) {
282		usb_ep_free_request(usb9pfs->in_ep, usb9pfs->in_req);
283		usb9pfs->in_req = NULL;
284	}
285
286	if (usb9pfs->out_req) {
287		usb_ep_free_request(usb9pfs->out_ep, usb9pfs->out_req);
288		usb9pfs->out_req = NULL;
289	}
290
291	disable_ep(cdev, usb9pfs->in_ep);
292	disable_ep(cdev, usb9pfs->out_ep);
293	dev_dbg(&cdev->gadget->dev, "%s disabled\n",
294		usb9pfs->function.name);
295}
296
297static int alloc_requests(struct usb_composite_dev *cdev,
298			  struct f_usb9pfs *usb9pfs)
299{
300	int ret;
301
302	usb9pfs->in_req = usb_ep_alloc_request(usb9pfs->in_ep, GFP_ATOMIC);
303	if (!usb9pfs->in_req) {
304		ret = -ENOENT;
305		goto fail;
306	}
307
308	usb9pfs->out_req = alloc_ep_req(usb9pfs->out_ep, usb9pfs->buflen);
309	if (!usb9pfs->out_req) {
310		ret = -ENOENT;
311		goto fail_in;
312	}
313
314	usb9pfs->in_req->complete = usb9pfs_tx_complete;
315	usb9pfs->out_req->complete = usb9pfs_rx_complete;
316
317	/* length will be set in complete routine */
318	usb9pfs->in_req->context = usb9pfs;
319	usb9pfs->out_req->context = usb9pfs;
320
321	return 0;
322
323fail_in:
324	usb_ep_free_request(usb9pfs->in_ep, usb9pfs->in_req);
325fail:
326	return ret;
327}
328
329static int enable_endpoint(struct usb_composite_dev *cdev,
330			   struct f_usb9pfs *usb9pfs, struct usb_ep *ep)
331{
332	int ret;
333
334	ret = config_ep_by_speed(cdev->gadget, &usb9pfs->function, ep);
335	if (ret)
336		return ret;
337
338	ret = usb_ep_enable(ep);
339	if (ret < 0)
340		return ret;
341
342	ep->driver_data = usb9pfs;
343
344	return 0;
345}
346
347static int
348enable_usb9pfs(struct usb_composite_dev *cdev, struct f_usb9pfs *usb9pfs)
349{
350	struct p9_client *client;
351	int ret = 0;
352
353	ret = enable_endpoint(cdev, usb9pfs, usb9pfs->in_ep);
354	if (ret)
355		goto out;
356
357	ret = enable_endpoint(cdev, usb9pfs, usb9pfs->out_ep);
358	if (ret)
359		goto disable_in;
360
361	ret = alloc_requests(cdev, usb9pfs);
362	if (ret)
363		goto disable_out;
364
365	client = usb9pfs->client;
366	if (client)
367		client->status = Connected;
368
369	dev_dbg(&cdev->gadget->dev, "%s enabled\n", usb9pfs->function.name);
370	return 0;
371
372disable_out:
373	usb_ep_disable(usb9pfs->out_ep);
374disable_in:
375	usb_ep_disable(usb9pfs->in_ep);
376out:
377	return ret;
378}
379
380static int p9_usbg_create(struct p9_client *client, struct fs_context *fc)
381{
382	const char *devname = fc->source;
383	struct f_usb9pfs_dev *dev;
384	struct f_usb9pfs *usb9pfs;
385	int ret = -ENOENT;
386	int found = 0;
387
388	if (!devname)
389		return -EINVAL;
390
391	guard(mutex)(&usb9pfs_lock);
392
393	list_for_each_entry(dev, &usbg_instance_list, usb9pfs_instance) {
394		if (!strncmp(devname, dev->tag, strlen(devname))) {
395			if (!dev->inuse) {
396				dev->inuse = true;
397				found = 1;
398				break;
399			}
400			ret = -EBUSY;
401			break;
402		}
403	}
404
405	if (!found) {
406		pr_err("no channels available for device %s\n", devname);
407		return ret;
408	}
409
410	usb9pfs = dev->usb9pfs;
411	if (!usb9pfs)
412		return -EINVAL;
413
414	client->trans = (void *)usb9pfs;
415	if (!usb9pfs->in_req)
416		client->status = Disconnected;
417	else
418		client->status = Connected;
419	usb9pfs->client = client;
420
421	client->trans_mod->maxsize = usb9pfs->buflen;
422
423	complete(&usb9pfs->received);
424
425	return 0;
426}
427
428static void usb9pfs_clear_tx(struct f_usb9pfs *usb9pfs)
429{
430	struct p9_req_t *req;
431
432	guard(spinlock_irqsave)(&usb9pfs->lock);
433
434	req = usb9pfs->in_req->context;
435	if (!req)
436		return;
437
438	if (!req->t_err)
439		req->t_err = -ECONNRESET;
440
441	p9_client_cb(usb9pfs->client, req, REQ_STATUS_ERROR);
442}
443
444static void p9_usbg_close(struct p9_client *client)
445{
446	struct f_usb9pfs *usb9pfs;
447	struct f_usb9pfs_dev *dev;
448	struct f_usb9pfs_opts *opts;
449
450	if (!client)
451		return;
452
453	usb9pfs = client->trans;
454	if (!usb9pfs)
455		return;
456
457	client->status = Disconnected;
458
459	usb9pfs_clear_tx(usb9pfs);
460
461	opts = container_of(usb9pfs->function.fi,
462			    struct f_usb9pfs_opts, func_inst);
463
464	dev = opts->dev;
465
466	mutex_lock(&usb9pfs_lock);
467	dev->inuse = false;
468	mutex_unlock(&usb9pfs_lock);
469}
470
471static int p9_usbg_request(struct p9_client *client, struct p9_req_t *p9_req)
472{
473	struct f_usb9pfs *usb9pfs = client->trans;
474	int ret;
475
476	if (client->status != Connected)
477		return -EBUSY;
478
479	ret = wait_for_completion_killable(&usb9pfs->received);
480	if (ret)
481		return ret;
482
483	ret = usb9pfs_transmit(usb9pfs, p9_req);
484	if (ret)
485		return ret;
486
487	ret = wait_for_completion_killable(&usb9pfs->send);
488	if (ret)
489		return ret;
490
491	return usb9pfs_queue_rx(usb9pfs, usb9pfs->out_req, GFP_ATOMIC);
492}
493
494static int p9_usbg_cancel(struct p9_client *client, struct p9_req_t *req)
495{
496	struct f_usb9pfs *usb9pfs = client->trans;
497	int ret = 1;
498
499	p9_debug(P9_DEBUG_TRANS, "client %p req %p\n", client, req);
500
501	guard(spinlock_irqsave)(&usb9pfs->lock);
502
503	if (req->status == REQ_STATUS_UNSENT) {
504		list_del(&req->req_list);
505		WRITE_ONCE(req->status, REQ_STATUS_FLSHD);
506		p9_req_put(client, req);
507		ret = 0;
508	}
509
510	return ret;
511}
512
513static struct p9_trans_module p9_usbg_trans = {
514	.name = "usbg",
515	.create = p9_usbg_create,
516	.close = p9_usbg_close,
517	.request = p9_usbg_request,
518	.cancel = p9_usbg_cancel,
519	.supports_vmalloc = false,
520	.owner = THIS_MODULE,
521};
522
523/*-------------------------------------------------------------------------*/
524
525#define USB_PROTOCOL_9PFS	0x09
526
527static struct usb_interface_descriptor usb9pfs_intf = {
528	.bLength =		sizeof(usb9pfs_intf),
529	.bDescriptorType =	USB_DT_INTERFACE,
530
531	.bNumEndpoints =	2,
532	.bInterfaceClass =	USB_CLASS_VENDOR_SPEC,
533	.bInterfaceSubClass =	USB_SUBCLASS_VENDOR_SPEC,
534	.bInterfaceProtocol =   USB_PROTOCOL_9PFS,
535
536	/* .iInterface = DYNAMIC */
537};
538
539/* full speed support: */
540
541static struct usb_endpoint_descriptor fs_usb9pfs_source_desc = {
542	.bLength =		USB_DT_ENDPOINT_SIZE,
543	.bDescriptorType =	USB_DT_ENDPOINT,
544
545	.bEndpointAddress =	USB_DIR_IN,
546	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
547};
548
549static struct usb_endpoint_descriptor fs_usb9pfs_sink_desc = {
550	.bLength =		USB_DT_ENDPOINT_SIZE,
551	.bDescriptorType =	USB_DT_ENDPOINT,
552
553	.bEndpointAddress =	USB_DIR_OUT,
554	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
555};
556
557static struct usb_descriptor_header *fs_usb9pfs_descs[] = {
558	(struct usb_descriptor_header *)&usb9pfs_intf,
559	(struct usb_descriptor_header *)&fs_usb9pfs_sink_desc,
560	(struct usb_descriptor_header *)&fs_usb9pfs_source_desc,
561	NULL,
562};
563
564/* high speed support: */
565
566static struct usb_endpoint_descriptor hs_usb9pfs_source_desc = {
567	.bLength =		USB_DT_ENDPOINT_SIZE,
568	.bDescriptorType =	USB_DT_ENDPOINT,
569
570	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
571	.wMaxPacketSize =	cpu_to_le16(512),
572};
573
574static struct usb_endpoint_descriptor hs_usb9pfs_sink_desc = {
575	.bLength =		USB_DT_ENDPOINT_SIZE,
576	.bDescriptorType =	USB_DT_ENDPOINT,
577
578	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
579	.wMaxPacketSize =	cpu_to_le16(512),
580};
581
582static struct usb_descriptor_header *hs_usb9pfs_descs[] = {
583	(struct usb_descriptor_header *)&usb9pfs_intf,
584	(struct usb_descriptor_header *)&hs_usb9pfs_source_desc,
585	(struct usb_descriptor_header *)&hs_usb9pfs_sink_desc,
586	NULL,
587};
588
589/* super speed support: */
590
591static struct usb_endpoint_descriptor ss_usb9pfs_source_desc = {
592	.bLength =		USB_DT_ENDPOINT_SIZE,
593	.bDescriptorType =	USB_DT_ENDPOINT,
594
595	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
596	.wMaxPacketSize =	cpu_to_le16(1024),
597};
598
599static struct usb_ss_ep_comp_descriptor ss_usb9pfs_source_comp_desc = {
600	.bLength =		USB_DT_SS_EP_COMP_SIZE,
601	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
602	.bMaxBurst =		0,
603	.bmAttributes =		0,
604	.wBytesPerInterval =	0,
605};
606
607static struct usb_endpoint_descriptor ss_usb9pfs_sink_desc = {
608	.bLength =		USB_DT_ENDPOINT_SIZE,
609	.bDescriptorType =	USB_DT_ENDPOINT,
610
611	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
612	.wMaxPacketSize =	cpu_to_le16(1024),
613};
614
615static struct usb_ss_ep_comp_descriptor ss_usb9pfs_sink_comp_desc = {
616	.bLength =		USB_DT_SS_EP_COMP_SIZE,
617	.bDescriptorType =	USB_DT_SS_ENDPOINT_COMP,
618	.bMaxBurst =		0,
619	.bmAttributes =		0,
620	.wBytesPerInterval =	0,
621};
622
623static struct usb_descriptor_header *ss_usb9pfs_descs[] = {
624	(struct usb_descriptor_header *)&usb9pfs_intf,
625	(struct usb_descriptor_header *)&ss_usb9pfs_source_desc,
626	(struct usb_descriptor_header *)&ss_usb9pfs_source_comp_desc,
627	(struct usb_descriptor_header *)&ss_usb9pfs_sink_desc,
628	(struct usb_descriptor_header *)&ss_usb9pfs_sink_comp_desc,
629	NULL,
630};
631
632/* function-specific strings: */
633static struct usb_string strings_usb9pfs[] = {
634	[0].s = "usb9pfs input to output",
635	{  }			/* end of list */
636};
637
638static struct usb_gadget_strings stringtab_usb9pfs = {
639	.language	= 0x0409,	/* en-us */
640	.strings	= strings_usb9pfs,
641};
642
643static struct usb_gadget_strings *usb9pfs_strings[] = {
644	&stringtab_usb9pfs,
645	NULL,
646};
647
648/*-------------------------------------------------------------------------*/
649
650static int usb9pfs_func_bind(struct usb_configuration *c,
651			     struct usb_function *f)
652{
653	struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
654	struct f_usb9pfs_opts *opts;
655	struct usb_composite_dev *cdev = c->cdev;
656	int ret;
657	int id;
658
659	/* allocate interface ID(s) */
660	id = usb_interface_id(c, f);
661	if (id < 0)
662		return id;
663	usb9pfs_intf.bInterfaceNumber = id;
664
665	id = usb_string_id(cdev);
666	if (id < 0)
667		return id;
668	strings_usb9pfs[0].id = id;
669	usb9pfs_intf.iInterface = id;
670
671	/* allocate endpoints */
672	usb9pfs->in_ep = usb_ep_autoconfig(cdev->gadget,
673					   &fs_usb9pfs_source_desc);
674	if (!usb9pfs->in_ep)
675		goto autoconf_fail;
676
677	usb9pfs->out_ep = usb_ep_autoconfig(cdev->gadget,
678					    &fs_usb9pfs_sink_desc);
679	if (!usb9pfs->out_ep)
680		goto autoconf_fail;
681
682	/* support high speed hardware */
683	hs_usb9pfs_source_desc.bEndpointAddress =
684		fs_usb9pfs_source_desc.bEndpointAddress;
685	hs_usb9pfs_sink_desc.bEndpointAddress =
686		fs_usb9pfs_sink_desc.bEndpointAddress;
687
688	/* support super speed hardware */
689	ss_usb9pfs_source_desc.bEndpointAddress =
690		fs_usb9pfs_source_desc.bEndpointAddress;
691	ss_usb9pfs_sink_desc.bEndpointAddress =
692		fs_usb9pfs_sink_desc.bEndpointAddress;
693
694	ret = usb_assign_descriptors(f, fs_usb9pfs_descs, hs_usb9pfs_descs,
695				     ss_usb9pfs_descs, ss_usb9pfs_descs);
696	if (ret)
697		return ret;
698
699	opts = container_of(f->fi, struct f_usb9pfs_opts, func_inst);
700	opts->dev->usb9pfs = usb9pfs;
701
702	dev_dbg(&cdev->gadget->dev, "%s speed %s: IN/%s, OUT/%s\n",
703		(gadget_is_superspeed(c->cdev->gadget) ? "super" :
704		(gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full")),
705			f->name, usb9pfs->in_ep->name, usb9pfs->out_ep->name);
706
707	return 0;
708
709autoconf_fail:
710	ERROR(cdev, "%s: can't autoconfigure on %s\n",
711	      f->name, cdev->gadget->name);
712	return -ENODEV;
713}
714
715static void usb9pfs_func_unbind(struct usb_configuration *c,
716				struct usb_function *f)
717{
718	struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
719
720	disable_usb9pfs(usb9pfs);
721}
722
723static void usb9pfs_free_func(struct usb_function *f)
724{
725	struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
726	struct f_usb9pfs_opts *opts;
727
728	kfree(usb9pfs);
729
730	opts = container_of(f->fi, struct f_usb9pfs_opts, func_inst);
731
732	mutex_lock(&opts->lock);
733	opts->refcnt--;
734	mutex_unlock(&opts->lock);
735
736	usb_free_all_descriptors(f);
737}
738
739static int usb9pfs_set_alt(struct usb_function *f,
740			   unsigned int intf, unsigned int alt)
741{
742	struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
743	struct usb_composite_dev *cdev = f->config->cdev;
744
745	return enable_usb9pfs(cdev, usb9pfs);
746}
747
748static void usb9pfs_disable(struct usb_function *f)
749{
750	struct f_usb9pfs *usb9pfs = func_to_usb9pfs(f);
751
752	usb9pfs_clear_tx(usb9pfs);
753}
754
755static struct usb_function *usb9pfs_alloc(struct usb_function_instance *fi)
756{
757	struct f_usb9pfs_opts *usb9pfs_opts;
758	struct f_usb9pfs *usb9pfs;
759
760	usb9pfs = kzalloc(sizeof(*usb9pfs), GFP_KERNEL);
761	if (!usb9pfs)
762		return ERR_PTR(-ENOMEM);
763
764	spin_lock_init(&usb9pfs->lock);
765
766	init_completion(&usb9pfs->send);
767	init_completion(&usb9pfs->received);
768
769	usb9pfs_opts = container_of(fi, struct f_usb9pfs_opts, func_inst);
770
771	mutex_lock(&usb9pfs_opts->lock);
772	usb9pfs_opts->refcnt++;
773	mutex_unlock(&usb9pfs_opts->lock);
774
775	usb9pfs->buflen = usb9pfs_opts->buflen;
776
777	usb9pfs->function.name = "usb9pfs";
778	usb9pfs->function.bind = usb9pfs_func_bind;
779	usb9pfs->function.unbind = usb9pfs_func_unbind;
780	usb9pfs->function.set_alt = usb9pfs_set_alt;
781	usb9pfs->function.disable = usb9pfs_disable;
782	usb9pfs->function.strings = usb9pfs_strings;
783
784	usb9pfs->function.free_func = usb9pfs_free_func;
785
786	return &usb9pfs->function;
787}
788
789static inline struct f_usb9pfs_opts *to_f_usb9pfs_opts(struct config_item *item)
790{
791	return container_of(to_config_group(item), struct f_usb9pfs_opts,
792			    func_inst.group);
793}
794
795static inline struct f_usb9pfs_opts *fi_to_f_usb9pfs_opts(struct usb_function_instance *fi)
796{
797	return container_of(fi, struct f_usb9pfs_opts, func_inst);
798}
799
800static void usb9pfs_attr_release(struct config_item *item)
801{
802	struct f_usb9pfs_opts *usb9pfs_opts = to_f_usb9pfs_opts(item);
803
804	usb_put_function_instance(&usb9pfs_opts->func_inst);
805}
806
807static struct configfs_item_operations usb9pfs_item_ops = {
808	.release		= usb9pfs_attr_release,
809};
810
811static ssize_t f_usb9pfs_opts_buflen_show(struct config_item *item, char *page)
812{
813	struct f_usb9pfs_opts *opts = to_f_usb9pfs_opts(item);
814	int ret;
815
816	mutex_lock(&opts->lock);
817	ret = sysfs_emit(page, "%d\n", opts->buflen);
818	mutex_unlock(&opts->lock);
819
820	return ret;
821}
822
823static ssize_t f_usb9pfs_opts_buflen_store(struct config_item *item,
824					   const char *page, size_t len)
825{
826	struct f_usb9pfs_opts *opts = to_f_usb9pfs_opts(item);
827	int ret;
828	u32 num;
829
830	guard(mutex)(&opts->lock);
831
832	if (opts->refcnt)
833		return -EBUSY;
834
835	ret = kstrtou32(page, 0, &num);
836	if (ret)
837		return ret;
838
839	opts->buflen = num;
840
841	return len;
842}
843
844CONFIGFS_ATTR(f_usb9pfs_opts_, buflen);
845
846static struct configfs_attribute *usb9pfs_attrs[] = {
847	&f_usb9pfs_opts_attr_buflen,
848	NULL,
849};
850
851static const struct config_item_type usb9pfs_func_type = {
852	.ct_item_ops	= &usb9pfs_item_ops,
853	.ct_attrs	= usb9pfs_attrs,
854	.ct_owner	= THIS_MODULE,
855};
856
857static struct f_usb9pfs_dev *_usb9pfs_do_find_dev(const char *tag)
858{
859	struct f_usb9pfs_dev *usb9pfs_dev;
860
861	if (!tag)
862		return NULL;
863
864	list_for_each_entry(usb9pfs_dev, &usbg_instance_list, usb9pfs_instance) {
865		if (strcmp(usb9pfs_dev->tag, tag) == 0)
866			return usb9pfs_dev;
867	}
868
869	return NULL;
870}
871
872static int usb9pfs_tag_instance(struct f_usb9pfs_dev *dev, const char *tag)
873{
874	struct f_usb9pfs_dev *existing;
875	int ret = 0;
876
877	guard(mutex)(&usb9pfs_lock);
878
879	existing = _usb9pfs_do_find_dev(tag);
880	if (!existing)
881		strscpy(dev->tag, tag, ARRAY_SIZE(dev->tag));
882	else if (existing != dev)
883		ret = -EBUSY;
884
885	return ret;
886}
887
888static int usb9pfs_set_inst_tag(struct usb_function_instance *fi, const char *tag)
889{
890	if (strlen(tag) >= sizeof_field(struct f_usb9pfs_dev, tag))
891		return -ENAMETOOLONG;
892	return usb9pfs_tag_instance(fi_to_f_usb9pfs_opts(fi)->dev, tag);
893}
894
895static void usb9pfs_free_instance(struct usb_function_instance *fi)
896{
897	struct f_usb9pfs_opts *usb9pfs_opts =
898		container_of(fi, struct f_usb9pfs_opts, func_inst);
899	struct f_usb9pfs_dev *dev = usb9pfs_opts->dev;
900
901	mutex_lock(&usb9pfs_lock);
902	list_del(&dev->usb9pfs_instance);
903	mutex_unlock(&usb9pfs_lock);
904
905	kfree(usb9pfs_opts);
906}
907
908static struct usb_function_instance *usb9pfs_alloc_instance(void)
909{
910	struct f_usb9pfs_opts *usb9pfs_opts;
911	struct f_usb9pfs_dev *dev;
912
913	usb9pfs_opts = kzalloc(sizeof(*usb9pfs_opts), GFP_KERNEL);
914	if (!usb9pfs_opts)
915		return ERR_PTR(-ENOMEM);
916
917	mutex_init(&usb9pfs_opts->lock);
918
919	usb9pfs_opts->func_inst.set_inst_name = usb9pfs_set_inst_tag;
920	usb9pfs_opts->func_inst.free_func_inst = usb9pfs_free_instance;
921
922	usb9pfs_opts->buflen = DEFAULT_BUFLEN;
923
924	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
925	if (!dev) {
926		kfree(usb9pfs_opts);
927		return ERR_PTR(-ENOMEM);
928	}
929
930	usb9pfs_opts->dev = dev;
931	dev->opts = usb9pfs_opts;
932
933	config_group_init_type_name(&usb9pfs_opts->func_inst.group, "",
934				    &usb9pfs_func_type);
935
936	mutex_lock(&usb9pfs_lock);
937	list_add_tail(&dev->usb9pfs_instance, &usbg_instance_list);
938	mutex_unlock(&usb9pfs_lock);
939
940	return &usb9pfs_opts->func_inst;
941}
942DECLARE_USB_FUNCTION(usb9pfs, usb9pfs_alloc_instance, usb9pfs_alloc);
943
944static int __init usb9pfs_modinit(void)
945{
946	int ret;
947
948	INIT_LIST_HEAD(&usbg_instance_list);
949
950	ret = usb_function_register(&usb9pfsusb_func);
951	if (!ret)
952		v9fs_register_trans(&p9_usbg_trans);
953
954	return ret;
955}
956
957static void __exit usb9pfs_modexit(void)
958{
959	usb_function_unregister(&usb9pfsusb_func);
960	v9fs_unregister_trans(&p9_usbg_trans);
961}
962
963module_init(usb9pfs_modinit);
964module_exit(usb9pfs_modexit);
965
966MODULE_ALIAS_9P("usbg");
967MODULE_LICENSE("GPL");
968MODULE_DESCRIPTION("USB gadget 9pfs transport");
969MODULE_AUTHOR("Michael Grzeschik");
~/ Read-only file preview.