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.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #include <linux/slab.h>
24 #include <linux/kernel.h>
25 #include <linux/device.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_phonet.h>
30 #include <linux/if_arp.h>
32 #include <linux/usb/ch9.h>
33 #include <linux/usb/cdc.h>
34 #include <linux/usb/composite.h>
38 #define PN_MEDIA_USB 0x1B
40 #if (PAGE_SIZE % MAXPACKET)
41 #error MAXPACKET must divide PAGE_SIZE!
44 /*-------------------------------------------------------------------------*/
52 struct usb_function function
;
57 struct net_device
*dev
;
58 struct usb_ep
*in_ep
, *out_ep
;
60 struct usb_request
*in_req
;
61 struct usb_request
*out_reqv
[0];
64 static int phonet_rxq_size
= 17;
66 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
68 return container_of(f
, struct f_phonet
, function
);
71 /*-------------------------------------------------------------------------*/
73 #define USB_CDC_SUBCLASS_PHONET 0xfe
74 #define USB_CDC_PHONET_TYPE 0xab
76 static struct usb_interface_descriptor
77 pn_control_intf_desc
= {
78 .bLength
= sizeof pn_control_intf_desc
,
79 .bDescriptorType
= USB_DT_INTERFACE
,
81 /* .bInterfaceNumber = DYNAMIC, */
82 .bInterfaceClass
= USB_CLASS_COMM
,
83 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
86 static const struct usb_cdc_header_desc
88 .bLength
= sizeof pn_header_desc
,
89 .bDescriptorType
= USB_DT_CS_INTERFACE
,
90 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
91 .bcdCDC
= cpu_to_le16(0x0110),
94 static const struct usb_cdc_header_desc
96 .bLength
= sizeof pn_phonet_desc
,
97 .bDescriptorType
= USB_DT_CS_INTERFACE
,
98 .bDescriptorSubType
= USB_CDC_PHONET_TYPE
,
99 .bcdCDC
= cpu_to_le16(0x1505), /* ??? */
102 static struct usb_cdc_union_desc
104 .bLength
= sizeof pn_union_desc
,
105 .bDescriptorType
= USB_DT_CS_INTERFACE
,
106 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
108 /* .bMasterInterface0 = DYNAMIC, */
109 /* .bSlaveInterface0 = DYNAMIC, */
112 static struct usb_interface_descriptor
113 pn_data_nop_intf_desc
= {
114 .bLength
= sizeof pn_data_nop_intf_desc
,
115 .bDescriptorType
= USB_DT_INTERFACE
,
117 /* .bInterfaceNumber = DYNAMIC, */
118 .bAlternateSetting
= 0,
120 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
123 static struct usb_interface_descriptor
124 pn_data_intf_desc
= {
125 .bLength
= sizeof pn_data_intf_desc
,
126 .bDescriptorType
= USB_DT_INTERFACE
,
128 /* .bInterfaceNumber = DYNAMIC, */
129 .bAlternateSetting
= 1,
131 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
134 static struct usb_endpoint_descriptor
136 .bLength
= USB_DT_ENDPOINT_SIZE
,
137 .bDescriptorType
= USB_DT_ENDPOINT
,
139 .bEndpointAddress
= USB_DIR_OUT
,
140 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
143 static struct usb_endpoint_descriptor
145 .bLength
= USB_DT_ENDPOINT_SIZE
,
146 .bDescriptorType
= USB_DT_ENDPOINT
,
148 .bEndpointAddress
= USB_DIR_OUT
,
149 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
150 .wMaxPacketSize
= cpu_to_le16(MAXPACKET
),
153 static struct usb_endpoint_descriptor
154 pn_fs_source_desc
= {
155 .bLength
= USB_DT_ENDPOINT_SIZE
,
156 .bDescriptorType
= USB_DT_ENDPOINT
,
158 .bEndpointAddress
= USB_DIR_IN
,
159 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
162 static struct usb_endpoint_descriptor
163 pn_hs_source_desc
= {
164 .bLength
= USB_DT_ENDPOINT_SIZE
,
165 .bDescriptorType
= USB_DT_ENDPOINT
,
167 .bEndpointAddress
= USB_DIR_IN
,
168 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
169 .wMaxPacketSize
= cpu_to_le16(512),
172 static struct usb_descriptor_header
*fs_pn_function
[] = {
173 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
174 (struct usb_descriptor_header
*) &pn_header_desc
,
175 (struct usb_descriptor_header
*) &pn_phonet_desc
,
176 (struct usb_descriptor_header
*) &pn_union_desc
,
177 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
178 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
179 (struct usb_descriptor_header
*) &pn_fs_sink_desc
,
180 (struct usb_descriptor_header
*) &pn_fs_source_desc
,
184 static struct usb_descriptor_header
*hs_pn_function
[] = {
185 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
186 (struct usb_descriptor_header
*) &pn_header_desc
,
187 (struct usb_descriptor_header
*) &pn_phonet_desc
,
188 (struct usb_descriptor_header
*) &pn_union_desc
,
189 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
190 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
191 (struct usb_descriptor_header
*) &pn_hs_sink_desc
,
192 (struct usb_descriptor_header
*) &pn_hs_source_desc
,
196 /*-------------------------------------------------------------------------*/
198 static int pn_net_open(struct net_device
*dev
)
200 netif_wake_queue(dev
);
204 static int pn_net_close(struct net_device
*dev
)
206 netif_stop_queue(dev
);
210 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
212 struct f_phonet
*fp
= ep
->driver_data
;
213 struct net_device
*dev
= fp
->dev
;
214 struct sk_buff
*skb
= req
->context
;
216 switch (req
->status
) {
218 dev
->stats
.tx_packets
++;
219 dev
->stats
.tx_bytes
+= skb
->len
;
222 case -ESHUTDOWN
: /* disconnected */
223 case -ECONNRESET
: /* disabled */
224 dev
->stats
.tx_aborted_errors
++;
226 dev
->stats
.tx_errors
++;
229 dev_kfree_skb_any(skb
);
230 netif_wake_queue(dev
);
233 static int pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
235 struct phonet_port
*port
= netdev_priv(dev
);
237 struct usb_request
*req
;
240 if (skb
->protocol
!= htons(ETH_P_PHONET
))
243 spin_lock_irqsave(&port
->lock
, flags
);
245 if (unlikely(!fp
)) /* race with carrier loss */
249 req
->buf
= skb
->data
;
250 req
->length
= skb
->len
;
251 req
->complete
= pn_tx_complete
;
255 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
258 netif_stop_queue(dev
);
262 spin_unlock_irqrestore(&port
->lock
, flags
);
266 dev
->stats
.tx_dropped
++;
271 static int pn_net_mtu(struct net_device
*dev
, int new_mtu
)
273 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
279 static const struct net_device_ops pn_netdev_ops
= {
280 .ndo_open
= pn_net_open
,
281 .ndo_stop
= pn_net_close
,
282 .ndo_start_xmit
= pn_net_xmit
,
283 .ndo_change_mtu
= pn_net_mtu
,
286 static void pn_net_setup(struct net_device
*dev
)
289 dev
->type
= ARPHRD_PHONET
;
290 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
291 dev
->mtu
= PHONET_DEV_MTU
;
292 dev
->hard_header_len
= 1;
293 dev
->dev_addr
[0] = PN_MEDIA_USB
;
295 dev
->tx_queue_len
= 1;
297 dev
->netdev_ops
= &pn_netdev_ops
;
298 dev
->destructor
= free_netdev
;
299 dev
->header_ops
= &phonet_header_ops
;
302 /*-------------------------------------------------------------------------*/
305 * Queue buffer for data from the host
308 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
310 struct net_device
*dev
= fp
->dev
;
314 page
= __netdev_alloc_page(dev
, gfp_flags
);
318 req
->buf
= page_address(page
);
319 req
->length
= PAGE_SIZE
;
322 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
324 netdev_free_page(dev
, page
);
328 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
330 struct f_phonet
*fp
= ep
->driver_data
;
331 struct net_device
*dev
= fp
->dev
;
332 struct page
*page
= req
->context
;
335 int status
= req
->status
;
339 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
342 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
343 if (req
->actual
< req
->length
) /* Last fragment */
345 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
349 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
, 0,
353 if (req
->actual
< req
->length
) { /* Last fragment */
354 skb
->protocol
= htons(ETH_P_PHONET
);
355 skb_reset_mac_header(skb
);
358 dev
->stats
.rx_packets
++;
359 dev
->stats
.rx_bytes
+= skb
->len
;
365 /* Do not resubmit in these cases: */
366 case -ESHUTDOWN
: /* disconnect */
367 case -ECONNABORTED
: /* hw reset */
368 case -ECONNRESET
: /* dequeued (unlink or netif down) */
372 /* Do resubmit in these cases: */
373 case -EOVERFLOW
: /* request buffer overflow */
374 dev
->stats
.rx_over_errors
++;
376 dev
->stats
.rx_errors
++;
381 netdev_free_page(dev
, page
);
383 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
386 /*-------------------------------------------------------------------------*/
388 static void __pn_reset(struct usb_function
*f
)
390 struct f_phonet
*fp
= func_to_pn(f
);
391 struct net_device
*dev
= fp
->dev
;
392 struct phonet_port
*port
= netdev_priv(dev
);
394 netif_carrier_off(dev
);
397 usb_ep_disable(fp
->out_ep
);
398 usb_ep_disable(fp
->in_ep
);
400 dev_kfree_skb_irq(fp
->rx
.skb
);
405 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
407 struct f_phonet
*fp
= func_to_pn(f
);
408 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
410 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
411 /* control interface, no altsetting */
412 return (alt
> 0) ? -EINVAL
: 0;
414 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
415 struct net_device
*dev
= fp
->dev
;
416 struct phonet_port
*port
= netdev_priv(dev
);
418 /* data intf (0: inactive, 1: active) */
422 spin_lock(&port
->lock
);
425 struct usb_endpoint_descriptor
*out
, *in
;
428 out
= ep_choose(gadget
,
431 in
= ep_choose(gadget
,
434 usb_ep_enable(fp
->out_ep
, out
);
435 usb_ep_enable(fp
->in_ep
, in
);
438 fp
->out_ep
->driver_data
= fp
;
439 fp
->in_ep
->driver_data
= fp
;
441 netif_carrier_on(dev
);
442 for (i
= 0; i
< phonet_rxq_size
; i
++)
443 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
445 spin_unlock(&port
->lock
);
452 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
454 struct f_phonet
*fp
= func_to_pn(f
);
456 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
459 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
460 struct phonet_port
*port
= netdev_priv(fp
->dev
);
463 spin_lock(&port
->lock
);
464 alt
= port
->usb
!= NULL
;
465 spin_unlock(&port
->lock
);
472 static void pn_disconnect(struct usb_function
*f
)
474 struct f_phonet
*fp
= func_to_pn(f
);
475 struct phonet_port
*port
= netdev_priv(fp
->dev
);
478 /* remain disabled until set_alt */
479 spin_lock_irqsave(&port
->lock
, flags
);
481 spin_unlock_irqrestore(&port
->lock
, flags
);
484 /*-------------------------------------------------------------------------*/
487 int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
489 struct usb_composite_dev
*cdev
= c
->cdev
;
490 struct usb_gadget
*gadget
= cdev
->gadget
;
491 struct f_phonet
*fp
= func_to_pn(f
);
495 /* Reserve interface IDs */
496 status
= usb_interface_id(c
, f
);
499 pn_control_intf_desc
.bInterfaceNumber
= status
;
500 pn_union_desc
.bMasterInterface0
= status
;
502 status
= usb_interface_id(c
, f
);
505 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
506 pn_data_intf_desc
.bInterfaceNumber
= status
;
507 pn_union_desc
.bSlaveInterface0
= status
;
509 /* Reserve endpoints */
511 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
515 ep
->driver_data
= fp
; /* Claim */
517 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
521 ep
->driver_data
= fp
; /* Claim */
523 pn_hs_sink_desc
.bEndpointAddress
=
524 pn_fs_sink_desc
.bEndpointAddress
;
525 pn_hs_source_desc
.bEndpointAddress
=
526 pn_fs_source_desc
.bEndpointAddress
;
528 /* Do not try to bind Phonet twice... */
529 fp
->function
.descriptors
= fs_pn_function
;
530 fp
->function
.hs_descriptors
= hs_pn_function
;
532 /* Incoming USB requests */
534 for (i
= 0; i
< phonet_rxq_size
; i
++) {
535 struct usb_request
*req
;
537 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
541 req
->complete
= pn_rx_complete
;
542 fp
->out_reqv
[i
] = req
;
545 /* Outgoing USB requests */
546 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
550 INFO(cdev
, "USB CDC Phonet function\n");
551 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
552 fp
->out_ep
->name
, fp
->in_ep
->name
);
557 fp
->out_ep
->driver_data
= NULL
;
559 fp
->in_ep
->driver_data
= NULL
;
560 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
565 pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
567 struct f_phonet
*fp
= func_to_pn(f
);
570 /* We are already disconnected */
572 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
573 for (i
= 0; i
< phonet_rxq_size
; i
++)
575 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
580 /*-------------------------------------------------------------------------*/
582 static struct net_device
*dev
;
584 int __init
phonet_bind_config(struct usb_configuration
*c
)
589 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
590 fp
= kzalloc(size
, GFP_KERNEL
);
595 fp
->function
.name
= "phonet";
596 fp
->function
.bind
= pn_bind
;
597 fp
->function
.unbind
= pn_unbind
;
598 fp
->function
.set_alt
= pn_set_alt
;
599 fp
->function
.get_alt
= pn_get_alt
;
600 fp
->function
.disable
= pn_disconnect
;
601 spin_lock_init(&fp
->rx
.lock
);
603 err
= usb_add_function(c
, &fp
->function
);
609 int __init
gphonet_setup(struct usb_gadget
*gadget
)
611 struct phonet_port
*port
;
614 /* Create net device */
616 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", pn_net_setup
);
620 port
= netdev_priv(dev
);
621 spin_lock_init(&port
->lock
);
622 netif_carrier_off(dev
);
623 SET_NETDEV_DEV(dev
, &gadget
->dev
);
625 err
= register_netdev(dev
);
631 void gphonet_cleanup(void)
633 unregister_netdev(dev
);