xref: /linux/drivers/usb/gadget/function/f_phonet.c (revision c088d5dd2fffb4de1fb8e7f57751c8b82942180a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * f_phonet.c -- USB CDC Phonet function
4  *
5  * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved.
6  *
7  * Author: Rémi Denis-Courmont
8  */
9 
10 #include <linux/mm.h>
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 
16 #include <linux/netdevice.h>
17 #include <linux/if_ether.h>
18 #include <linux/if_phonet.h>
19 #include <linux/if_arp.h>
20 
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/cdc.h>
23 #include <linux/usb/composite.h>
24 
25 #include "u_phonet.h"
26 #include "u_ether.h"
27 
28 #define PN_MEDIA_USB	0x1B
29 #define MAXPACKET	512
30 #if (PAGE_SIZE % MAXPACKET)
31 #error MAXPACKET must divide PAGE_SIZE!
32 #endif
33 
34 /*-------------------------------------------------------------------------*/
35 
36 struct phonet_port {
37 	struct f_phonet			*usb;
38 	spinlock_t			lock;
39 };
40 
41 struct f_phonet {
42 	struct usb_function		function;
43 	struct {
44 		struct sk_buff		*skb;
45 		spinlock_t		lock;
46 	} rx;
47 	struct net_device		*dev;
48 	struct usb_ep			*in_ep, *out_ep;
49 
50 	struct usb_request		*in_req;
51 	struct usb_request		*out_reqv[];
52 };
53 
54 static int phonet_rxq_size = 17;
55 
56 static inline struct f_phonet *func_to_pn(struct usb_function *f)
57 {
58 	return container_of(f, struct f_phonet, function);
59 }
60 
61 /*-------------------------------------------------------------------------*/
62 
63 #define USB_CDC_SUBCLASS_PHONET	0xfe
64 #define USB_CDC_PHONET_TYPE	0xab
65 
66 static struct usb_interface_descriptor
67 pn_control_intf_desc = {
68 	.bLength =		sizeof pn_control_intf_desc,
69 	.bDescriptorType =	USB_DT_INTERFACE,
70 
71 	/* .bInterfaceNumber =	DYNAMIC, */
72 	.bInterfaceClass =	USB_CLASS_COMM,
73 	.bInterfaceSubClass =	USB_CDC_SUBCLASS_PHONET,
74 };
75 
76 static const struct usb_cdc_header_desc
77 pn_header_desc = {
78 	.bLength =		sizeof pn_header_desc,
79 	.bDescriptorType =	USB_DT_CS_INTERFACE,
80 	.bDescriptorSubType =	USB_CDC_HEADER_TYPE,
81 	.bcdCDC =		cpu_to_le16(0x0110),
82 };
83 
84 static const struct usb_cdc_header_desc
85 pn_phonet_desc = {
86 	.bLength =		sizeof pn_phonet_desc,
87 	.bDescriptorType =	USB_DT_CS_INTERFACE,
88 	.bDescriptorSubType =	USB_CDC_PHONET_TYPE,
89 	.bcdCDC =		cpu_to_le16(0x1505), /* ??? */
90 };
91 
92 static struct usb_cdc_union_desc
93 pn_union_desc = {
94 	.bLength =		sizeof pn_union_desc,
95 	.bDescriptorType =	USB_DT_CS_INTERFACE,
96 	.bDescriptorSubType =	USB_CDC_UNION_TYPE,
97 
98 	/* .bMasterInterface0 =	DYNAMIC, */
99 	/* .bSlaveInterface0 =	DYNAMIC, */
100 };
101 
102 static struct usb_interface_descriptor
103 pn_data_nop_intf_desc = {
104 	.bLength =		sizeof pn_data_nop_intf_desc,
105 	.bDescriptorType =	USB_DT_INTERFACE,
106 
107 	/* .bInterfaceNumber =	DYNAMIC, */
108 	.bAlternateSetting =	0,
109 	.bNumEndpoints =	0,
110 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
111 };
112 
113 static struct usb_interface_descriptor
114 pn_data_intf_desc = {
115 	.bLength =		sizeof pn_data_intf_desc,
116 	.bDescriptorType =	USB_DT_INTERFACE,
117 
118 	/* .bInterfaceNumber =	DYNAMIC, */
119 	.bAlternateSetting =	1,
120 	.bNumEndpoints =	2,
121 	.bInterfaceClass =	USB_CLASS_CDC_DATA,
122 };
123 
124 static struct usb_endpoint_descriptor
125 pn_fs_sink_desc = {
126 	.bLength =		USB_DT_ENDPOINT_SIZE,
127 	.bDescriptorType =	USB_DT_ENDPOINT,
128 
129 	.bEndpointAddress =	USB_DIR_OUT,
130 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
131 };
132 
133 static struct usb_endpoint_descriptor
134 pn_hs_sink_desc = {
135 	.bLength =		USB_DT_ENDPOINT_SIZE,
136 	.bDescriptorType =	USB_DT_ENDPOINT,
137 
138 	.bEndpointAddress =	USB_DIR_OUT,
139 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
140 	.wMaxPacketSize =	cpu_to_le16(MAXPACKET),
141 };
142 
143 static struct usb_endpoint_descriptor
144 pn_fs_source_desc = {
145 	.bLength =		USB_DT_ENDPOINT_SIZE,
146 	.bDescriptorType =	USB_DT_ENDPOINT,
147 
148 	.bEndpointAddress =	USB_DIR_IN,
149 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
150 };
151 
152 static struct usb_endpoint_descriptor
153 pn_hs_source_desc = {
154 	.bLength =		USB_DT_ENDPOINT_SIZE,
155 	.bDescriptorType =	USB_DT_ENDPOINT,
156 
157 	.bEndpointAddress =	USB_DIR_IN,
158 	.bmAttributes =		USB_ENDPOINT_XFER_BULK,
159 	.wMaxPacketSize =	cpu_to_le16(512),
160 };
161 
162 static struct usb_descriptor_header *fs_pn_function[] = {
163 	(struct usb_descriptor_header *) &pn_control_intf_desc,
164 	(struct usb_descriptor_header *) &pn_header_desc,
165 	(struct usb_descriptor_header *) &pn_phonet_desc,
166 	(struct usb_descriptor_header *) &pn_union_desc,
167 	(struct usb_descriptor_header *) &pn_data_nop_intf_desc,
168 	(struct usb_descriptor_header *) &pn_data_intf_desc,
169 	(struct usb_descriptor_header *) &pn_fs_sink_desc,
170 	(struct usb_descriptor_header *) &pn_fs_source_desc,
171 	NULL,
172 };
173 
174 static struct usb_descriptor_header *hs_pn_function[] = {
175 	(struct usb_descriptor_header *) &pn_control_intf_desc,
176 	(struct usb_descriptor_header *) &pn_header_desc,
177 	(struct usb_descriptor_header *) &pn_phonet_desc,
178 	(struct usb_descriptor_header *) &pn_union_desc,
179 	(struct usb_descriptor_header *) &pn_data_nop_intf_desc,
180 	(struct usb_descriptor_header *) &pn_data_intf_desc,
181 	(struct usb_descriptor_header *) &pn_hs_sink_desc,
182 	(struct usb_descriptor_header *) &pn_hs_source_desc,
183 	NULL,
184 };
185 
186 /*-------------------------------------------------------------------------*/
187 
188 static int pn_net_open(struct net_device *dev)
189 {
190 	netif_wake_queue(dev);
191 	return 0;
192 }
193 
194 static int pn_net_close(struct net_device *dev)
195 {
196 	netif_stop_queue(dev);
197 	return 0;
198 }
199 
200 static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req)
201 {
202 	struct f_phonet *fp = ep->driver_data;
203 	struct net_device *dev = fp->dev;
204 	struct sk_buff *skb = req->context;
205 
206 	switch (req->status) {
207 	case 0:
208 		dev->stats.tx_packets++;
209 		dev->stats.tx_bytes += skb->len;
210 		break;
211 
212 	case -ESHUTDOWN: /* disconnected */
213 	case -ECONNRESET: /* disabled */
214 		dev->stats.tx_aborted_errors++;
215 		fallthrough;
216 	default:
217 		dev->stats.tx_errors++;
218 	}
219 
220 	dev_kfree_skb_any(skb);
221 	netif_wake_queue(dev);
222 }
223 
224 static netdev_tx_t pn_net_xmit(struct sk_buff *skb, struct net_device *dev)
225 {
226 	struct phonet_port *port = netdev_priv(dev);
227 	struct f_phonet *fp;
228 	struct usb_request *req;
229 	unsigned long flags;
230 
231 	if (skb->protocol != htons(ETH_P_PHONET))
232 		goto out;
233 
234 	spin_lock_irqsave(&port->lock, flags);
235 	fp = port->usb;
236 	if (unlikely(!fp)) /* race with carrier loss */
237 		goto out_unlock;
238 
239 	req = fp->in_req;
240 	req->buf = skb->data;
241 	req->length = skb->len;
242 	req->complete = pn_tx_complete;
243 	req->zero = 1;
244 	req->context = skb;
245 
246 	if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC)))
247 		goto out_unlock;
248 
249 	netif_stop_queue(dev);
250 	skb = NULL;
251 
252 out_unlock:
253 	spin_unlock_irqrestore(&port->lock, flags);
254 out:
255 	if (unlikely(skb)) {
256 		dev_kfree_skb(skb);
257 		dev->stats.tx_dropped++;
258 	}
259 	return NETDEV_TX_OK;
260 }
261 
262 static const struct net_device_ops pn_netdev_ops = {
263 	.ndo_open	= pn_net_open,
264 	.ndo_stop	= pn_net_close,
265 	.ndo_start_xmit	= pn_net_xmit,
266 };
267 
268 static void pn_net_setup(struct net_device *dev)
269 {
270 	const u8 addr = PN_MEDIA_USB;
271 
272 	dev->features		= 0;
273 	dev->type		= ARPHRD_PHONET;
274 	dev->flags		= IFF_POINTOPOINT | IFF_NOARP;
275 	dev->mtu		= PHONET_DEV_MTU;
276 	dev->min_mtu		= PHONET_MIN_MTU;
277 	dev->max_mtu		= PHONET_MAX_MTU;
278 	dev->hard_header_len	= 1;
279 	dev->addr_len		= 1;
280 	dev_addr_set(dev, &addr);
281 
282 	dev->tx_queue_len	= 1;
283 
284 	dev->netdev_ops		= &pn_netdev_ops;
285 	dev->needs_free_netdev	= true;
286 	dev->header_ops		= &phonet_header_ops;
287 }
288 
289 /*-------------------------------------------------------------------------*/
290 
291 /*
292  * Queue buffer for data from the host
293  */
294 static int
295 pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags)
296 {
297 	struct page *page;
298 	int err;
299 
300 	page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
301 	if (!page)
302 		return -ENOMEM;
303 
304 	req->buf = page_address(page);
305 	req->length = PAGE_SIZE;
306 	req->context = page;
307 
308 	err = usb_ep_queue(fp->out_ep, req, gfp_flags);
309 	if (unlikely(err))
310 		put_page(page);
311 	return err;
312 }
313 
314 static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req)
315 {
316 	struct f_phonet *fp = ep->driver_data;
317 	struct net_device *dev = fp->dev;
318 	struct page *page = req->context;
319 	struct sk_buff *skb;
320 	unsigned long flags;
321 	int status = req->status;
322 
323 	switch (status) {
324 	case 0:
325 		spin_lock_irqsave(&fp->rx.lock, flags);
326 		skb = fp->rx.skb;
327 		if (!skb)
328 			skb = fp->rx.skb = netdev_alloc_skb(dev, 12);
329 		if (req->actual < req->length) /* Last fragment */
330 			fp->rx.skb = NULL;
331 		spin_unlock_irqrestore(&fp->rx.lock, flags);
332 
333 		if (unlikely(!skb))
334 			break;
335 
336 		if (unlikely(skb_shinfo(skb)->nr_frags >= MAX_SKB_FRAGS)) {
337 			/* Frame count from host exceeds frags[] capacity */
338 			dev_kfree_skb_any(skb);
339 			if (fp->rx.skb == skb)
340 				fp->rx.skb = NULL;
341 			dev->stats.rx_length_errors++;
342 			break;
343 		}
344 
345 		if (skb->len == 0) { /* First fragment */
346 			skb->protocol = htons(ETH_P_PHONET);
347 			skb_reset_mac_header(skb);
348 			/* Can't use pskb_pull() on page in IRQ */
349 			skb_put_data(skb, page_address(page), 1);
350 		}
351 
352 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page,
353 				skb->len <= 1, req->actual, PAGE_SIZE);
354 		page = NULL;
355 
356 		if (req->actual < req->length) { /* Last fragment */
357 			skb->dev = dev;
358 			dev->stats.rx_packets++;
359 			dev->stats.rx_bytes += skb->len;
360 
361 			netif_rx(skb);
362 		}
363 		break;
364 
365 	/* Do not resubmit in these cases: */
366 	case -ESHUTDOWN: /* disconnect */
367 	case -ECONNABORTED: /* hw reset */
368 	case -ECONNRESET: /* dequeued (unlink or netif down) */
369 		req = NULL;
370 		break;
371 
372 	/* Do resubmit in these cases: */
373 	case -EOVERFLOW: /* request buffer overflow */
374 		dev->stats.rx_over_errors++;
375 		fallthrough;
376 	default:
377 		dev->stats.rx_errors++;
378 		break;
379 	}
380 
381 	if (page)
382 		put_page(page);
383 	if (req)
384 		pn_rx_submit(fp, req, GFP_ATOMIC);
385 }
386 
387 /*-------------------------------------------------------------------------*/
388 
389 static void __pn_reset(struct usb_function *f)
390 {
391 	struct f_phonet *fp = func_to_pn(f);
392 	struct net_device *dev = fp->dev;
393 	struct phonet_port *port = netdev_priv(dev);
394 
395 	netif_carrier_off(dev);
396 	port->usb = NULL;
397 
398 	usb_ep_disable(fp->out_ep);
399 	usb_ep_disable(fp->in_ep);
400 	if (fp->rx.skb) {
401 		dev_kfree_skb_irq(fp->rx.skb);
402 		fp->rx.skb = NULL;
403 	}
404 }
405 
406 static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
407 {
408 	struct f_phonet *fp = func_to_pn(f);
409 	struct usb_gadget *gadget = fp->function.config->cdev->gadget;
410 
411 	if (intf == pn_control_intf_desc.bInterfaceNumber)
412 		/* control interface, no altsetting */
413 		return (alt > 0) ? -EINVAL : 0;
414 
415 	if (intf == pn_data_intf_desc.bInterfaceNumber) {
416 		struct net_device *dev = fp->dev;
417 		struct phonet_port *port = netdev_priv(dev);
418 
419 		/* data intf (0: inactive, 1: active) */
420 		if (alt > 1)
421 			return -EINVAL;
422 
423 		spin_lock(&port->lock);
424 
425 		if (fp->in_ep->enabled)
426 			__pn_reset(f);
427 
428 		if (alt == 1) {
429 			int i;
430 
431 			if (config_ep_by_speed(gadget, f, fp->in_ep) ||
432 			    config_ep_by_speed(gadget, f, fp->out_ep)) {
433 				fp->in_ep->desc = NULL;
434 				fp->out_ep->desc = NULL;
435 				spin_unlock(&port->lock);
436 				return -EINVAL;
437 			}
438 			usb_ep_enable(fp->out_ep);
439 			usb_ep_enable(fp->in_ep);
440 
441 			port->usb = fp;
442 			fp->out_ep->driver_data = fp;
443 			fp->in_ep->driver_data = fp;
444 
445 			netif_carrier_on(dev);
446 			for (i = 0; i < phonet_rxq_size; i++)
447 				pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC);
448 		}
449 		spin_unlock(&port->lock);
450 		return 0;
451 	}
452 
453 	return -EINVAL;
454 }
455 
456 static int pn_get_alt(struct usb_function *f, unsigned intf)
457 {
458 	struct f_phonet *fp = func_to_pn(f);
459 
460 	if (intf == pn_control_intf_desc.bInterfaceNumber)
461 		return 0;
462 
463 	if (intf == pn_data_intf_desc.bInterfaceNumber) {
464 		struct phonet_port *port = netdev_priv(fp->dev);
465 		u8 alt;
466 
467 		spin_lock(&port->lock);
468 		alt = port->usb != NULL;
469 		spin_unlock(&port->lock);
470 		return alt;
471 	}
472 
473 	return -EINVAL;
474 }
475 
476 static void pn_disconnect(struct usb_function *f)
477 {
478 	struct f_phonet *fp = func_to_pn(f);
479 	struct phonet_port *port = netdev_priv(fp->dev);
480 	unsigned long flags;
481 
482 	/* remain disabled until set_alt */
483 	spin_lock_irqsave(&port->lock, flags);
484 	__pn_reset(f);
485 	spin_unlock_irqrestore(&port->lock, flags);
486 }
487 
488 /*-------------------------------------------------------------------------*/
489 
490 static int pn_bind(struct usb_configuration *c, struct usb_function *f)
491 {
492 	struct usb_composite_dev *cdev = c->cdev;
493 	struct usb_gadget *gadget = cdev->gadget;
494 	struct f_phonet *fp = func_to_pn(f);
495 	struct usb_ep *ep;
496 	int status, i;
497 
498 	struct f_phonet_opts *phonet_opts;
499 
500 	phonet_opts = container_of(f->fi, struct f_phonet_opts, func_inst);
501 
502 	/*
503 	 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
504 	 * configurations are bound in sequence with list_for_each_entry,
505 	 * in each configuration its functions are bound in sequence
506 	 * with list_for_each_entry, so we assume no race condition
507 	 * with regard to phonet_opts->bound access
508 	 */
509 	if (!phonet_opts->bound) {
510 		gphonet_set_gadget(phonet_opts->net, gadget);
511 		status = gphonet_register_netdev(phonet_opts->net);
512 		if (status)
513 			return status;
514 		phonet_opts->bound = true;
515 	}
516 
517 	/* Reserve interface IDs */
518 	status = usb_interface_id(c, f);
519 	if (status < 0)
520 		goto err;
521 	pn_control_intf_desc.bInterfaceNumber = status;
522 	pn_union_desc.bMasterInterface0 = status;
523 
524 	status = usb_interface_id(c, f);
525 	if (status < 0)
526 		goto err;
527 	pn_data_nop_intf_desc.bInterfaceNumber = status;
528 	pn_data_intf_desc.bInterfaceNumber = status;
529 	pn_union_desc.bSlaveInterface0 = status;
530 
531 	/* Reserve endpoints */
532 	status = -ENODEV;
533 	ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc);
534 	if (!ep)
535 		goto err;
536 	fp->out_ep = ep;
537 
538 	ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc);
539 	if (!ep)
540 		goto err;
541 	fp->in_ep = ep;
542 
543 	pn_hs_sink_desc.bEndpointAddress = pn_fs_sink_desc.bEndpointAddress;
544 	pn_hs_source_desc.bEndpointAddress = pn_fs_source_desc.bEndpointAddress;
545 
546 	/* Do not try to bind Phonet twice... */
547 	status = usb_assign_descriptors(f, fs_pn_function, hs_pn_function,
548 			NULL, NULL);
549 	if (status)
550 		goto err;
551 
552 	/* Incoming USB requests */
553 	status = -ENOMEM;
554 	for (i = 0; i < phonet_rxq_size; i++) {
555 		struct usb_request *req;
556 
557 		req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL);
558 		if (!req)
559 			goto err_req;
560 
561 		req->complete = pn_rx_complete;
562 		fp->out_reqv[i] = req;
563 	}
564 
565 	/* Outgoing USB requests */
566 	fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL);
567 	if (!fp->in_req)
568 		goto err_req;
569 
570 	INFO(cdev, "USB CDC Phonet function\n");
571 	INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name,
572 		fp->out_ep->name, fp->in_ep->name);
573 	return 0;
574 
575 err_req:
576 	for (i = 0; i < phonet_rxq_size && fp->out_reqv[i]; i++)
577 		usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
578 	usb_free_all_descriptors(f);
579 err:
580 	ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n");
581 	return status;
582 }
583 
584 static inline struct f_phonet_opts *to_f_phonet_opts(struct config_item *item)
585 {
586 	return container_of(to_config_group(item), struct f_phonet_opts,
587 			func_inst.group);
588 }
589 
590 static void phonet_attr_release(struct config_item *item)
591 {
592 	struct f_phonet_opts *opts = to_f_phonet_opts(item);
593 
594 	usb_put_function_instance(&opts->func_inst);
595 }
596 
597 static const struct configfs_item_operations phonet_item_ops = {
598 	.release		= phonet_attr_release,
599 };
600 
601 static ssize_t f_phonet_ifname_show(struct config_item *item, char *page)
602 {
603 	return gether_get_ifname(to_f_phonet_opts(item)->net, page, PAGE_SIZE);
604 }
605 
606 CONFIGFS_ATTR_RO(f_phonet_, ifname);
607 
608 static struct configfs_attribute *phonet_attrs[] = {
609 	&f_phonet_attr_ifname,
610 	NULL,
611 };
612 
613 static const struct config_item_type phonet_func_type = {
614 	.ct_item_ops	= &phonet_item_ops,
615 	.ct_attrs	= phonet_attrs,
616 	.ct_owner	= THIS_MODULE,
617 };
618 
619 static void phonet_free_inst(struct usb_function_instance *f)
620 {
621 	struct f_phonet_opts *opts;
622 
623 	opts = container_of(f, struct f_phonet_opts, func_inst);
624 	if (opts->bound)
625 		gphonet_cleanup(opts->net);
626 	else
627 		free_netdev(opts->net);
628 	kfree(opts);
629 }
630 
631 static struct usb_function_instance *phonet_alloc_inst(void)
632 {
633 	struct f_phonet_opts *opts;
634 
635 	opts = kzalloc_obj(*opts);
636 	if (!opts)
637 		return ERR_PTR(-ENOMEM);
638 
639 	opts->func_inst.free_func_inst = phonet_free_inst;
640 	opts->net = gphonet_setup_default();
641 	if (IS_ERR(opts->net)) {
642 		struct net_device *net = opts->net;
643 		kfree(opts);
644 		return ERR_CAST(net);
645 	}
646 
647 	config_group_init_type_name(&opts->func_inst.group, "",
648 			&phonet_func_type);
649 
650 	return &opts->func_inst;
651 }
652 
653 static void phonet_free(struct usb_function *f)
654 {
655 	struct f_phonet *phonet;
656 
657 	phonet = func_to_pn(f);
658 	kfree(phonet);
659 }
660 
661 static void pn_unbind(struct usb_configuration *c, struct usb_function *f)
662 {
663 	struct f_phonet *fp = func_to_pn(f);
664 	int i;
665 
666 	/* We are already disconnected */
667 	if (fp->in_req)
668 		usb_ep_free_request(fp->in_ep, fp->in_req);
669 	for (i = 0; i < phonet_rxq_size; i++)
670 		if (fp->out_reqv[i])
671 			usb_ep_free_request(fp->out_ep, fp->out_reqv[i]);
672 
673 	usb_free_all_descriptors(f);
674 }
675 
676 static struct usb_function *phonet_alloc(struct usb_function_instance *fi)
677 {
678 	struct f_phonet *fp;
679 	struct f_phonet_opts *opts;
680 
681 	fp = kzalloc_flex(*fp, out_reqv, phonet_rxq_size);
682 	if (!fp)
683 		return ERR_PTR(-ENOMEM);
684 
685 	opts = container_of(fi, struct f_phonet_opts, func_inst);
686 
687 	fp->dev = opts->net;
688 	fp->function.name = "phonet";
689 	fp->function.bind = pn_bind;
690 	fp->function.unbind = pn_unbind;
691 	fp->function.set_alt = pn_set_alt;
692 	fp->function.get_alt = pn_get_alt;
693 	fp->function.disable = pn_disconnect;
694 	fp->function.free_func = phonet_free;
695 	spin_lock_init(&fp->rx.lock);
696 
697 	return &fp->function;
698 }
699 
700 struct net_device *gphonet_setup_default(void)
701 {
702 	struct net_device *dev;
703 	struct phonet_port *port;
704 
705 	/* Create net device */
706 	dev = alloc_netdev(sizeof(*port), "upnlink%d", NET_NAME_UNKNOWN,
707 			   pn_net_setup);
708 	if (!dev)
709 		return ERR_PTR(-ENOMEM);
710 
711 	port = netdev_priv(dev);
712 	spin_lock_init(&port->lock);
713 	netif_carrier_off(dev);
714 
715 	return dev;
716 }
717 
718 void gphonet_set_gadget(struct net_device *net, struct usb_gadget *g)
719 {
720 	SET_NETDEV_DEV(net, &g->dev);
721 }
722 
723 int gphonet_register_netdev(struct net_device *net)
724 {
725 	int status;
726 
727 	status = register_netdev(net);
728 	if (status)
729 		free_netdev(net);
730 
731 	return status;
732 }
733 
734 void gphonet_cleanup(struct net_device *dev)
735 {
736 	unregister_netdev(dev);
737 }
738 
739 DECLARE_USB_FUNCTION_INIT(phonet, phonet_alloc_inst, phonet_alloc);
740 MODULE_AUTHOR("Rémi Denis-Courmont");
741 MODULE_DESCRIPTION("USB CDC Phonet function");
742 MODULE_LICENSE("GPL");
743