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