1 // SPDX-License-Identifier: GPL-2.0
3 * f_phonet.c -- USB CDC Phonet function
5 * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved.
7 * Author: Rémi Denis-Courmont
11 #include <linux/slab.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
16 #include <linux/netdevice.h>
17 #include <linux/if_ether.h>
18 #include <linux/if_phonet.h>
19 #include <linux/if_arp.h>
21 #include <linux/usb/ch9.h>
22 #include <linux/usb/cdc.h>
23 #include <linux/usb/composite.h>
28 #define PN_MEDIA_USB 0x1B
30 #if (PAGE_SIZE % MAXPACKET)
31 #error MAXPACKET must divide PAGE_SIZE!
34 /*-------------------------------------------------------------------------*/
42 struct usb_function function
;
47 struct net_device
*dev
;
48 struct usb_ep
*in_ep
, *out_ep
;
50 struct usb_request
*in_req
;
51 struct usb_request
*out_reqv
[0];
54 static int phonet_rxq_size
= 17;
56 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
58 return container_of(f
, struct f_phonet
, function
);
61 /*-------------------------------------------------------------------------*/
63 #define USB_CDC_SUBCLASS_PHONET 0xfe
64 #define USB_CDC_PHONET_TYPE 0xab
66 static struct usb_interface_descriptor
67 pn_control_intf_desc
= {
68 .bLength
= sizeof pn_control_intf_desc
,
69 .bDescriptorType
= USB_DT_INTERFACE
,
71 /* .bInterfaceNumber = DYNAMIC, */
72 .bInterfaceClass
= USB_CLASS_COMM
,
73 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
76 static const struct usb_cdc_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),
84 static const struct usb_cdc_header_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), /* ??? */
92 static struct usb_cdc_union_desc
94 .bLength
= sizeof pn_union_desc
,
95 .bDescriptorType
= USB_DT_CS_INTERFACE
,
96 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
98 /* .bMasterInterface0 = DYNAMIC, */
99 /* .bSlaveInterface0 = DYNAMIC, */
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
,
107 /* .bInterfaceNumber = DYNAMIC, */
108 .bAlternateSetting
= 0,
110 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
113 static struct usb_interface_descriptor
114 pn_data_intf_desc
= {
115 .bLength
= sizeof pn_data_intf_desc
,
116 .bDescriptorType
= USB_DT_INTERFACE
,
118 /* .bInterfaceNumber = DYNAMIC, */
119 .bAlternateSetting
= 1,
121 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
124 static struct usb_endpoint_descriptor
126 .bLength
= USB_DT_ENDPOINT_SIZE
,
127 .bDescriptorType
= USB_DT_ENDPOINT
,
129 .bEndpointAddress
= USB_DIR_OUT
,
130 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
133 static struct usb_endpoint_descriptor
135 .bLength
= USB_DT_ENDPOINT_SIZE
,
136 .bDescriptorType
= USB_DT_ENDPOINT
,
138 .bEndpointAddress
= USB_DIR_OUT
,
139 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
140 .wMaxPacketSize
= cpu_to_le16(MAXPACKET
),
143 static struct usb_endpoint_descriptor
144 pn_fs_source_desc
= {
145 .bLength
= USB_DT_ENDPOINT_SIZE
,
146 .bDescriptorType
= USB_DT_ENDPOINT
,
148 .bEndpointAddress
= USB_DIR_IN
,
149 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
152 static struct usb_endpoint_descriptor
153 pn_hs_source_desc
= {
154 .bLength
= USB_DT_ENDPOINT_SIZE
,
155 .bDescriptorType
= USB_DT_ENDPOINT
,
157 .bEndpointAddress
= USB_DIR_IN
,
158 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
159 .wMaxPacketSize
= cpu_to_le16(512),
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
,
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
,
186 /*-------------------------------------------------------------------------*/
188 static int pn_net_open(struct net_device
*dev
)
190 netif_wake_queue(dev
);
194 static int pn_net_close(struct net_device
*dev
)
196 netif_stop_queue(dev
);
200 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
202 struct f_phonet
*fp
= ep
->driver_data
;
203 struct net_device
*dev
= fp
->dev
;
204 struct sk_buff
*skb
= req
->context
;
206 switch (req
->status
) {
208 dev
->stats
.tx_packets
++;
209 dev
->stats
.tx_bytes
+= skb
->len
;
212 case -ESHUTDOWN
: /* disconnected */
213 case -ECONNRESET
: /* disabled */
214 dev
->stats
.tx_aborted_errors
++;
217 dev
->stats
.tx_errors
++;
220 dev_kfree_skb_any(skb
);
221 netif_wake_queue(dev
);
224 static netdev_tx_t
pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
226 struct phonet_port
*port
= netdev_priv(dev
);
228 struct usb_request
*req
;
231 if (skb
->protocol
!= htons(ETH_P_PHONET
))
234 spin_lock_irqsave(&port
->lock
, flags
);
236 if (unlikely(!fp
)) /* race with carrier loss */
240 req
->buf
= skb
->data
;
241 req
->length
= skb
->len
;
242 req
->complete
= pn_tx_complete
;
246 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
249 netif_stop_queue(dev
);
253 spin_unlock_irqrestore(&port
->lock
, flags
);
257 dev
->stats
.tx_dropped
++;
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
,
268 static void pn_net_setup(struct net_device
*dev
)
271 dev
->type
= ARPHRD_PHONET
;
272 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
273 dev
->mtu
= PHONET_DEV_MTU
;
274 dev
->min_mtu
= PHONET_MIN_MTU
;
275 dev
->max_mtu
= PHONET_MAX_MTU
;
276 dev
->hard_header_len
= 1;
277 dev
->dev_addr
[0] = PN_MEDIA_USB
;
279 dev
->tx_queue_len
= 1;
281 dev
->netdev_ops
= &pn_netdev_ops
;
282 dev
->needs_free_netdev
= true;
283 dev
->header_ops
= &phonet_header_ops
;
286 /*-------------------------------------------------------------------------*/
289 * Queue buffer for data from the host
292 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
297 page
= __dev_alloc_page(gfp_flags
| __GFP_NOMEMALLOC
);
301 req
->buf
= page_address(page
);
302 req
->length
= PAGE_SIZE
;
305 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
311 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
313 struct f_phonet
*fp
= ep
->driver_data
;
314 struct net_device
*dev
= fp
->dev
;
315 struct page
*page
= req
->context
;
318 int status
= req
->status
;
322 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
325 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
326 if (req
->actual
< req
->length
) /* Last fragment */
328 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
333 if (skb
->len
== 0) { /* First fragment */
334 skb
->protocol
= htons(ETH_P_PHONET
);
335 skb_reset_mac_header(skb
);
336 /* Can't use pskb_pull() on page in IRQ */
337 skb_put_data(skb
, page_address(page
), 1);
340 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
,
341 skb
->len
<= 1, req
->actual
, PAGE_SIZE
);
344 if (req
->actual
< req
->length
) { /* Last fragment */
346 dev
->stats
.rx_packets
++;
347 dev
->stats
.rx_bytes
+= skb
->len
;
353 /* Do not resubmit in these cases: */
354 case -ESHUTDOWN
: /* disconnect */
355 case -ECONNABORTED
: /* hw reset */
356 case -ECONNRESET
: /* dequeued (unlink or netif down) */
360 /* Do resubmit in these cases: */
361 case -EOVERFLOW
: /* request buffer overflow */
362 dev
->stats
.rx_over_errors
++;
365 dev
->stats
.rx_errors
++;
372 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
375 /*-------------------------------------------------------------------------*/
377 static void __pn_reset(struct usb_function
*f
)
379 struct f_phonet
*fp
= func_to_pn(f
);
380 struct net_device
*dev
= fp
->dev
;
381 struct phonet_port
*port
= netdev_priv(dev
);
383 netif_carrier_off(dev
);
386 usb_ep_disable(fp
->out_ep
);
387 usb_ep_disable(fp
->in_ep
);
389 dev_kfree_skb_irq(fp
->rx
.skb
);
394 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
396 struct f_phonet
*fp
= func_to_pn(f
);
397 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
399 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
400 /* control interface, no altsetting */
401 return (alt
> 0) ? -EINVAL
: 0;
403 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
404 struct net_device
*dev
= fp
->dev
;
405 struct phonet_port
*port
= netdev_priv(dev
);
407 /* data intf (0: inactive, 1: active) */
411 spin_lock(&port
->lock
);
413 if (fp
->in_ep
->enabled
)
419 if (config_ep_by_speed(gadget
, f
, fp
->in_ep
) ||
420 config_ep_by_speed(gadget
, f
, fp
->out_ep
)) {
421 fp
->in_ep
->desc
= NULL
;
422 fp
->out_ep
->desc
= NULL
;
423 spin_unlock(&port
->lock
);
426 usb_ep_enable(fp
->out_ep
);
427 usb_ep_enable(fp
->in_ep
);
430 fp
->out_ep
->driver_data
= fp
;
431 fp
->in_ep
->driver_data
= fp
;
433 netif_carrier_on(dev
);
434 for (i
= 0; i
< phonet_rxq_size
; i
++)
435 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
437 spin_unlock(&port
->lock
);
444 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
446 struct f_phonet
*fp
= func_to_pn(f
);
448 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
451 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
452 struct phonet_port
*port
= netdev_priv(fp
->dev
);
455 spin_lock(&port
->lock
);
456 alt
= port
->usb
!= NULL
;
457 spin_unlock(&port
->lock
);
464 static void pn_disconnect(struct usb_function
*f
)
466 struct f_phonet
*fp
= func_to_pn(f
);
467 struct phonet_port
*port
= netdev_priv(fp
->dev
);
470 /* remain disabled until set_alt */
471 spin_lock_irqsave(&port
->lock
, flags
);
473 spin_unlock_irqrestore(&port
->lock
, flags
);
476 /*-------------------------------------------------------------------------*/
478 static int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
480 struct usb_composite_dev
*cdev
= c
->cdev
;
481 struct usb_gadget
*gadget
= cdev
->gadget
;
482 struct f_phonet
*fp
= func_to_pn(f
);
486 struct f_phonet_opts
*phonet_opts
;
488 phonet_opts
= container_of(f
->fi
, struct f_phonet_opts
, func_inst
);
491 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
492 * configurations are bound in sequence with list_for_each_entry,
493 * in each configuration its functions are bound in sequence
494 * with list_for_each_entry, so we assume no race condition
495 * with regard to phonet_opts->bound access
497 if (!phonet_opts
->bound
) {
498 gphonet_set_gadget(phonet_opts
->net
, gadget
);
499 status
= gphonet_register_netdev(phonet_opts
->net
);
502 phonet_opts
->bound
= true;
505 /* Reserve interface IDs */
506 status
= usb_interface_id(c
, f
);
509 pn_control_intf_desc
.bInterfaceNumber
= status
;
510 pn_union_desc
.bMasterInterface0
= status
;
512 status
= usb_interface_id(c
, f
);
515 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
516 pn_data_intf_desc
.bInterfaceNumber
= status
;
517 pn_union_desc
.bSlaveInterface0
= status
;
519 /* Reserve endpoints */
521 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
526 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
531 pn_hs_sink_desc
.bEndpointAddress
= pn_fs_sink_desc
.bEndpointAddress
;
532 pn_hs_source_desc
.bEndpointAddress
= pn_fs_source_desc
.bEndpointAddress
;
534 /* Do not try to bind Phonet twice... */
535 status
= usb_assign_descriptors(f
, fs_pn_function
, hs_pn_function
,
540 /* Incoming USB requests */
542 for (i
= 0; i
< phonet_rxq_size
; i
++) {
543 struct usb_request
*req
;
545 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
549 req
->complete
= pn_rx_complete
;
550 fp
->out_reqv
[i
] = req
;
553 /* Outgoing USB requests */
554 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
558 INFO(cdev
, "USB CDC Phonet function\n");
559 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
560 fp
->out_ep
->name
, fp
->in_ep
->name
);
564 for (i
= 0; i
< phonet_rxq_size
&& fp
->out_reqv
[i
]; i
++)
565 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
566 usb_free_all_descriptors(f
);
568 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
572 static inline struct f_phonet_opts
*to_f_phonet_opts(struct config_item
*item
)
574 return container_of(to_config_group(item
), struct f_phonet_opts
,
578 static void phonet_attr_release(struct config_item
*item
)
580 struct f_phonet_opts
*opts
= to_f_phonet_opts(item
);
582 usb_put_function_instance(&opts
->func_inst
);
585 static struct configfs_item_operations phonet_item_ops
= {
586 .release
= phonet_attr_release
,
589 static ssize_t
f_phonet_ifname_show(struct config_item
*item
, char *page
)
591 return gether_get_ifname(to_f_phonet_opts(item
)->net
, page
, PAGE_SIZE
);
594 CONFIGFS_ATTR_RO(f_phonet_
, ifname
);
596 static struct configfs_attribute
*phonet_attrs
[] = {
597 &f_phonet_attr_ifname
,
601 static const struct config_item_type phonet_func_type
= {
602 .ct_item_ops
= &phonet_item_ops
,
603 .ct_attrs
= phonet_attrs
,
604 .ct_owner
= THIS_MODULE
,
607 static void phonet_free_inst(struct usb_function_instance
*f
)
609 struct f_phonet_opts
*opts
;
611 opts
= container_of(f
, struct f_phonet_opts
, func_inst
);
613 gphonet_cleanup(opts
->net
);
615 free_netdev(opts
->net
);
619 static struct usb_function_instance
*phonet_alloc_inst(void)
621 struct f_phonet_opts
*opts
;
623 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
625 return ERR_PTR(-ENOMEM
);
627 opts
->func_inst
.free_func_inst
= phonet_free_inst
;
628 opts
->net
= gphonet_setup_default();
629 if (IS_ERR(opts
->net
)) {
630 struct net_device
*net
= opts
->net
;
632 return ERR_CAST(net
);
635 config_group_init_type_name(&opts
->func_inst
.group
, "",
638 return &opts
->func_inst
;
641 static void phonet_free(struct usb_function
*f
)
643 struct f_phonet
*phonet
;
645 phonet
= func_to_pn(f
);
649 static void pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
651 struct f_phonet
*fp
= func_to_pn(f
);
654 /* We are already disconnected */
656 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
657 for (i
= 0; i
< phonet_rxq_size
; i
++)
659 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
661 usb_free_all_descriptors(f
);
664 static struct usb_function
*phonet_alloc(struct usb_function_instance
*fi
)
667 struct f_phonet_opts
*opts
;
670 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
671 fp
= kzalloc(size
, GFP_KERNEL
);
673 return ERR_PTR(-ENOMEM
);
675 opts
= container_of(fi
, struct f_phonet_opts
, func_inst
);
678 fp
->function
.name
= "phonet";
679 fp
->function
.bind
= pn_bind
;
680 fp
->function
.unbind
= pn_unbind
;
681 fp
->function
.set_alt
= pn_set_alt
;
682 fp
->function
.get_alt
= pn_get_alt
;
683 fp
->function
.disable
= pn_disconnect
;
684 fp
->function
.free_func
= phonet_free
;
685 spin_lock_init(&fp
->rx
.lock
);
687 return &fp
->function
;
690 struct net_device
*gphonet_setup_default(void)
692 struct net_device
*dev
;
693 struct phonet_port
*port
;
695 /* Create net device */
696 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", NET_NAME_UNKNOWN
,
699 return ERR_PTR(-ENOMEM
);
701 port
= netdev_priv(dev
);
702 spin_lock_init(&port
->lock
);
703 netif_carrier_off(dev
);
708 void gphonet_set_gadget(struct net_device
*net
, struct usb_gadget
*g
)
710 SET_NETDEV_DEV(net
, &g
->dev
);
713 int gphonet_register_netdev(struct net_device
*net
)
717 status
= register_netdev(net
);
724 void gphonet_cleanup(struct net_device
*dev
)
726 unregister_netdev(dev
);
729 DECLARE_USB_FUNCTION_INIT(phonet
, phonet_alloc_inst
, phonet_alloc
);
730 MODULE_AUTHOR("Rémi Denis-Courmont");
731 MODULE_LICENSE("GPL");