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