2 * f_phonet.c -- USB CDC Phonet function
4 * Copyright (C) 2007-2008 Nokia Corporation. All rights reserved.
6 * Author: Rémi Denis-Courmont
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.
14 #include <linux/slab.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
19 #include <linux/netdevice.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_phonet.h>
22 #include <linux/if_arp.h>
24 #include <linux/usb/ch9.h>
25 #include <linux/usb/cdc.h>
26 #include <linux/usb/composite.h>
31 #define PN_MEDIA_USB 0x1B
33 #if (PAGE_SIZE % MAXPACKET)
34 #error MAXPACKET must divide PAGE_SIZE!
37 /*-------------------------------------------------------------------------*/
45 struct usb_function function
;
50 struct net_device
*dev
;
51 struct usb_ep
*in_ep
, *out_ep
;
53 struct usb_request
*in_req
;
54 struct usb_request
*out_reqv
[0];
57 static int phonet_rxq_size
= 17;
59 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
61 return container_of(f
, struct f_phonet
, function
);
64 /*-------------------------------------------------------------------------*/
66 #define USB_CDC_SUBCLASS_PHONET 0xfe
67 #define USB_CDC_PHONET_TYPE 0xab
69 static struct usb_interface_descriptor
70 pn_control_intf_desc
= {
71 .bLength
= sizeof pn_control_intf_desc
,
72 .bDescriptorType
= USB_DT_INTERFACE
,
74 /* .bInterfaceNumber = DYNAMIC, */
75 .bInterfaceClass
= USB_CLASS_COMM
,
76 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
79 static const struct usb_cdc_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),
87 static const struct usb_cdc_header_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), /* ??? */
95 static struct usb_cdc_union_desc
97 .bLength
= sizeof pn_union_desc
,
98 .bDescriptorType
= USB_DT_CS_INTERFACE
,
99 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
101 /* .bMasterInterface0 = DYNAMIC, */
102 /* .bSlaveInterface0 = DYNAMIC, */
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
,
110 /* .bInterfaceNumber = DYNAMIC, */
111 .bAlternateSetting
= 0,
113 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
116 static struct usb_interface_descriptor
117 pn_data_intf_desc
= {
118 .bLength
= sizeof pn_data_intf_desc
,
119 .bDescriptorType
= USB_DT_INTERFACE
,
121 /* .bInterfaceNumber = DYNAMIC, */
122 .bAlternateSetting
= 1,
124 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
127 static struct usb_endpoint_descriptor
129 .bLength
= USB_DT_ENDPOINT_SIZE
,
130 .bDescriptorType
= USB_DT_ENDPOINT
,
132 .bEndpointAddress
= USB_DIR_OUT
,
133 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
136 static struct usb_endpoint_descriptor
138 .bLength
= USB_DT_ENDPOINT_SIZE
,
139 .bDescriptorType
= USB_DT_ENDPOINT
,
141 .bEndpointAddress
= USB_DIR_OUT
,
142 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 .wMaxPacketSize
= cpu_to_le16(MAXPACKET
),
146 static struct usb_endpoint_descriptor
147 pn_fs_source_desc
= {
148 .bLength
= USB_DT_ENDPOINT_SIZE
,
149 .bDescriptorType
= USB_DT_ENDPOINT
,
151 .bEndpointAddress
= USB_DIR_IN
,
152 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
155 static struct usb_endpoint_descriptor
156 pn_hs_source_desc
= {
157 .bLength
= USB_DT_ENDPOINT_SIZE
,
158 .bDescriptorType
= USB_DT_ENDPOINT
,
160 .bEndpointAddress
= USB_DIR_IN
,
161 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
162 .wMaxPacketSize
= cpu_to_le16(512),
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
,
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
,
189 /*-------------------------------------------------------------------------*/
191 static int pn_net_open(struct net_device
*dev
)
193 netif_wake_queue(dev
);
197 static int pn_net_close(struct net_device
*dev
)
199 netif_stop_queue(dev
);
203 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
205 struct f_phonet
*fp
= ep
->driver_data
;
206 struct net_device
*dev
= fp
->dev
;
207 struct sk_buff
*skb
= req
->context
;
209 switch (req
->status
) {
211 dev
->stats
.tx_packets
++;
212 dev
->stats
.tx_bytes
+= skb
->len
;
215 case -ESHUTDOWN
: /* disconnected */
216 case -ECONNRESET
: /* disabled */
217 dev
->stats
.tx_aborted_errors
++;
219 dev
->stats
.tx_errors
++;
222 dev_kfree_skb_any(skb
);
223 netif_wake_queue(dev
);
226 static int pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
228 struct phonet_port
*port
= netdev_priv(dev
);
230 struct usb_request
*req
;
233 if (skb
->protocol
!= htons(ETH_P_PHONET
))
236 spin_lock_irqsave(&port
->lock
, flags
);
238 if (unlikely(!fp
)) /* race with carrier loss */
242 req
->buf
= skb
->data
;
243 req
->length
= skb
->len
;
244 req
->complete
= pn_tx_complete
;
248 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
251 netif_stop_queue(dev
);
255 spin_unlock_irqrestore(&port
->lock
, flags
);
259 dev
->stats
.tx_dropped
++;
264 static const struct net_device_ops pn_netdev_ops
= {
265 .ndo_open
= pn_net_open
,
266 .ndo_stop
= pn_net_close
,
267 .ndo_start_xmit
= pn_net_xmit
,
270 static void pn_net_setup(struct net_device
*dev
)
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
->dev_addr
[0] = PN_MEDIA_USB
;
281 dev
->tx_queue_len
= 1;
283 dev
->netdev_ops
= &pn_netdev_ops
;
284 dev
->destructor
= free_netdev
;
285 dev
->header_ops
= &phonet_header_ops
;
288 /*-------------------------------------------------------------------------*/
291 * Queue buffer for data from the host
294 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
299 page
= __dev_alloc_page(gfp_flags
| __GFP_NOMEMALLOC
);
303 req
->buf
= page_address(page
);
304 req
->length
= PAGE_SIZE
;
307 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
313 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
315 struct f_phonet
*fp
= ep
->driver_data
;
316 struct net_device
*dev
= fp
->dev
;
317 struct page
*page
= req
->context
;
320 int status
= req
->status
;
324 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
327 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
328 if (req
->actual
< req
->length
) /* Last fragment */
330 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
335 if (skb
->len
== 0) { /* First fragment */
336 skb
->protocol
= htons(ETH_P_PHONET
);
337 skb_reset_mac_header(skb
);
338 /* Can't use pskb_pull() on page in IRQ */
339 memcpy(skb_put(skb
, 1), page_address(page
), 1);
342 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
,
343 skb
->len
<= 1, req
->actual
, PAGE_SIZE
);
346 if (req
->actual
< req
->length
) { /* Last fragment */
348 dev
->stats
.rx_packets
++;
349 dev
->stats
.rx_bytes
+= skb
->len
;
355 /* Do not resubmit in these cases: */
356 case -ESHUTDOWN
: /* disconnect */
357 case -ECONNABORTED
: /* hw reset */
358 case -ECONNRESET
: /* dequeued (unlink or netif down) */
362 /* Do resubmit in these cases: */
363 case -EOVERFLOW
: /* request buffer overflow */
364 dev
->stats
.rx_over_errors
++;
366 dev
->stats
.rx_errors
++;
373 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
376 /*-------------------------------------------------------------------------*/
378 static void __pn_reset(struct usb_function
*f
)
380 struct f_phonet
*fp
= func_to_pn(f
);
381 struct net_device
*dev
= fp
->dev
;
382 struct phonet_port
*port
= netdev_priv(dev
);
384 netif_carrier_off(dev
);
387 usb_ep_disable(fp
->out_ep
);
388 usb_ep_disable(fp
->in_ep
);
390 dev_kfree_skb_irq(fp
->rx
.skb
);
395 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
397 struct f_phonet
*fp
= func_to_pn(f
);
398 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
400 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
401 /* control interface, no altsetting */
402 return (alt
> 0) ? -EINVAL
: 0;
404 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
405 struct net_device
*dev
= fp
->dev
;
406 struct phonet_port
*port
= netdev_priv(dev
);
408 /* data intf (0: inactive, 1: active) */
412 spin_lock(&port
->lock
);
414 if (fp
->in_ep
->enabled
)
420 if (config_ep_by_speed(gadget
, f
, fp
->in_ep
) ||
421 config_ep_by_speed(gadget
, f
, fp
->out_ep
)) {
422 fp
->in_ep
->desc
= NULL
;
423 fp
->out_ep
->desc
= NULL
;
424 spin_unlock(&port
->lock
);
427 usb_ep_enable(fp
->out_ep
);
428 usb_ep_enable(fp
->in_ep
);
431 fp
->out_ep
->driver_data
= fp
;
432 fp
->in_ep
->driver_data
= fp
;
434 netif_carrier_on(dev
);
435 for (i
= 0; i
< phonet_rxq_size
; i
++)
436 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
438 spin_unlock(&port
->lock
);
445 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
447 struct f_phonet
*fp
= func_to_pn(f
);
449 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
452 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
453 struct phonet_port
*port
= netdev_priv(fp
->dev
);
456 spin_lock(&port
->lock
);
457 alt
= port
->usb
!= NULL
;
458 spin_unlock(&port
->lock
);
465 static void pn_disconnect(struct usb_function
*f
)
467 struct f_phonet
*fp
= func_to_pn(f
);
468 struct phonet_port
*port
= netdev_priv(fp
->dev
);
471 /* remain disabled until set_alt */
472 spin_lock_irqsave(&port
->lock
, flags
);
474 spin_unlock_irqrestore(&port
->lock
, flags
);
477 /*-------------------------------------------------------------------------*/
479 static int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
481 struct usb_composite_dev
*cdev
= c
->cdev
;
482 struct usb_gadget
*gadget
= cdev
->gadget
;
483 struct f_phonet
*fp
= func_to_pn(f
);
487 struct f_phonet_opts
*phonet_opts
;
489 phonet_opts
= container_of(f
->fi
, struct f_phonet_opts
, func_inst
);
492 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
493 * configurations are bound in sequence with list_for_each_entry,
494 * in each configuration its functions are bound in sequence
495 * with list_for_each_entry, so we assume no race condition
496 * with regard to phonet_opts->bound access
498 if (!phonet_opts
->bound
) {
499 gphonet_set_gadget(phonet_opts
->net
, gadget
);
500 status
= gphonet_register_netdev(phonet_opts
->net
);
503 phonet_opts
->bound
= true;
506 /* Reserve interface IDs */
507 status
= usb_interface_id(c
, f
);
510 pn_control_intf_desc
.bInterfaceNumber
= status
;
511 pn_union_desc
.bMasterInterface0
= status
;
513 status
= usb_interface_id(c
, f
);
516 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
517 pn_data_intf_desc
.bInterfaceNumber
= status
;
518 pn_union_desc
.bSlaveInterface0
= status
;
520 /* Reserve endpoints */
522 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
527 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
532 pn_hs_sink_desc
.bEndpointAddress
= pn_fs_sink_desc
.bEndpointAddress
;
533 pn_hs_source_desc
.bEndpointAddress
= pn_fs_source_desc
.bEndpointAddress
;
535 /* Do not try to bind Phonet twice... */
536 status
= usb_assign_descriptors(f
, fs_pn_function
, hs_pn_function
,
541 /* Incoming USB requests */
543 for (i
= 0; i
< phonet_rxq_size
; i
++) {
544 struct usb_request
*req
;
546 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
550 req
->complete
= pn_rx_complete
;
551 fp
->out_reqv
[i
] = req
;
554 /* Outgoing USB requests */
555 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
559 INFO(cdev
, "USB CDC Phonet function\n");
560 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
561 fp
->out_ep
->name
, fp
->in_ep
->name
);
565 for (i
= 0; i
< phonet_rxq_size
&& fp
->out_reqv
[i
]; i
++)
566 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
567 usb_free_all_descriptors(f
);
569 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
573 static inline struct f_phonet_opts
*to_f_phonet_opts(struct config_item
*item
)
575 return container_of(to_config_group(item
), struct f_phonet_opts
,
579 static void phonet_attr_release(struct config_item
*item
)
581 struct f_phonet_opts
*opts
= to_f_phonet_opts(item
);
583 usb_put_function_instance(&opts
->func_inst
);
586 static struct configfs_item_operations phonet_item_ops
= {
587 .release
= phonet_attr_release
,
590 static ssize_t
f_phonet_ifname_show(struct config_item
*item
, char *page
)
592 return gether_get_ifname(to_f_phonet_opts(item
)->net
, page
, PAGE_SIZE
);
595 CONFIGFS_ATTR_RO(f_phonet_
, ifname
);
597 static struct configfs_attribute
*phonet_attrs
[] = {
598 &f_phonet_attr_ifname
,
602 static struct config_item_type phonet_func_type
= {
603 .ct_item_ops
= &phonet_item_ops
,
604 .ct_attrs
= phonet_attrs
,
605 .ct_owner
= THIS_MODULE
,
608 static void phonet_free_inst(struct usb_function_instance
*f
)
610 struct f_phonet_opts
*opts
;
612 opts
= container_of(f
, struct f_phonet_opts
, func_inst
);
614 gphonet_cleanup(opts
->net
);
616 free_netdev(opts
->net
);
620 static struct usb_function_instance
*phonet_alloc_inst(void)
622 struct f_phonet_opts
*opts
;
624 opts
= kzalloc(sizeof(*opts
), GFP_KERNEL
);
626 return ERR_PTR(-ENOMEM
);
628 opts
->func_inst
.free_func_inst
= phonet_free_inst
;
629 opts
->net
= gphonet_setup_default();
630 if (IS_ERR(opts
->net
)) {
631 struct net_device
*net
= opts
->net
;
633 return ERR_CAST(net
);
636 config_group_init_type_name(&opts
->func_inst
.group
, "",
639 return &opts
->func_inst
;
642 static void phonet_free(struct usb_function
*f
)
644 struct f_phonet
*phonet
;
646 phonet
= func_to_pn(f
);
650 static void pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
652 struct f_phonet
*fp
= func_to_pn(f
);
655 /* We are already disconnected */
657 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
658 for (i
= 0; i
< phonet_rxq_size
; i
++)
660 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
662 usb_free_all_descriptors(f
);
665 static struct usb_function
*phonet_alloc(struct usb_function_instance
*fi
)
668 struct f_phonet_opts
*opts
;
671 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
672 fp
= kzalloc(size
, GFP_KERNEL
);
674 return ERR_PTR(-ENOMEM
);
676 opts
= container_of(fi
, struct f_phonet_opts
, func_inst
);
679 fp
->function
.name
= "phonet";
680 fp
->function
.bind
= pn_bind
;
681 fp
->function
.unbind
= pn_unbind
;
682 fp
->function
.set_alt
= pn_set_alt
;
683 fp
->function
.get_alt
= pn_get_alt
;
684 fp
->function
.disable
= pn_disconnect
;
685 fp
->function
.free_func
= phonet_free
;
686 spin_lock_init(&fp
->rx
.lock
);
688 return &fp
->function
;
691 struct net_device
*gphonet_setup_default(void)
693 struct net_device
*dev
;
694 struct phonet_port
*port
;
696 /* Create net device */
697 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", NET_NAME_UNKNOWN
,
700 return ERR_PTR(-ENOMEM
);
702 port
= netdev_priv(dev
);
703 spin_lock_init(&port
->lock
);
704 netif_carrier_off(dev
);
709 void gphonet_set_gadget(struct net_device
*net
, struct usb_gadget
*g
)
711 SET_NETDEV_DEV(net
, &g
->dev
);
714 int gphonet_register_netdev(struct net_device
*net
)
718 status
= register_netdev(net
);
725 void gphonet_cleanup(struct net_device
*dev
)
727 unregister_netdev(dev
);
730 DECLARE_USB_FUNCTION_INIT(phonet
, phonet_alloc_inst
, phonet_alloc
);
731 MODULE_AUTHOR("Rémi Denis-Courmont");
732 MODULE_LICENSE("GPL");