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
24 #include <linux/slab.h>
25 #include <linux/kernel.h>
26 #include <linux/device.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/if_phonet.h>
31 #include <linux/if_arp.h>
33 #include <linux/usb/ch9.h>
34 #include <linux/usb/cdc.h>
35 #include <linux/usb/composite.h>
39 #define PN_MEDIA_USB 0x1B
41 #if (PAGE_SIZE % MAXPACKET)
42 #error MAXPACKET must divide PAGE_SIZE!
45 /*-------------------------------------------------------------------------*/
53 struct usb_function function
;
58 struct net_device
*dev
;
59 struct usb_ep
*in_ep
, *out_ep
;
61 struct usb_request
*in_req
;
62 struct usb_request
*out_reqv
[0];
65 static int phonet_rxq_size
= 17;
67 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
69 return container_of(f
, struct f_phonet
, function
);
72 /*-------------------------------------------------------------------------*/
74 #define USB_CDC_SUBCLASS_PHONET 0xfe
75 #define USB_CDC_PHONET_TYPE 0xab
77 static struct usb_interface_descriptor
78 pn_control_intf_desc
= {
79 .bLength
= sizeof pn_control_intf_desc
,
80 .bDescriptorType
= USB_DT_INTERFACE
,
82 /* .bInterfaceNumber = DYNAMIC, */
83 .bInterfaceClass
= USB_CLASS_COMM
,
84 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
87 static const struct usb_cdc_header_desc
89 .bLength
= sizeof pn_header_desc
,
90 .bDescriptorType
= USB_DT_CS_INTERFACE
,
91 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
92 .bcdCDC
= cpu_to_le16(0x0110),
95 static const struct usb_cdc_header_desc
97 .bLength
= sizeof pn_phonet_desc
,
98 .bDescriptorType
= USB_DT_CS_INTERFACE
,
99 .bDescriptorSubType
= USB_CDC_PHONET_TYPE
,
100 .bcdCDC
= cpu_to_le16(0x1505), /* ??? */
103 static struct usb_cdc_union_desc
105 .bLength
= sizeof pn_union_desc
,
106 .bDescriptorType
= USB_DT_CS_INTERFACE
,
107 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
109 /* .bMasterInterface0 = DYNAMIC, */
110 /* .bSlaveInterface0 = DYNAMIC, */
113 static struct usb_interface_descriptor
114 pn_data_nop_intf_desc
= {
115 .bLength
= sizeof pn_data_nop_intf_desc
,
116 .bDescriptorType
= USB_DT_INTERFACE
,
118 /* .bInterfaceNumber = DYNAMIC, */
119 .bAlternateSetting
= 0,
121 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
124 static struct usb_interface_descriptor
125 pn_data_intf_desc
= {
126 .bLength
= sizeof pn_data_intf_desc
,
127 .bDescriptorType
= USB_DT_INTERFACE
,
129 /* .bInterfaceNumber = DYNAMIC, */
130 .bAlternateSetting
= 1,
132 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
135 static struct usb_endpoint_descriptor
137 .bLength
= USB_DT_ENDPOINT_SIZE
,
138 .bDescriptorType
= USB_DT_ENDPOINT
,
140 .bEndpointAddress
= USB_DIR_OUT
,
141 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
144 static struct usb_endpoint_descriptor
146 .bLength
= USB_DT_ENDPOINT_SIZE
,
147 .bDescriptorType
= USB_DT_ENDPOINT
,
149 .bEndpointAddress
= USB_DIR_OUT
,
150 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
151 .wMaxPacketSize
= cpu_to_le16(MAXPACKET
),
154 static struct usb_endpoint_descriptor
155 pn_fs_source_desc
= {
156 .bLength
= USB_DT_ENDPOINT_SIZE
,
157 .bDescriptorType
= USB_DT_ENDPOINT
,
159 .bEndpointAddress
= USB_DIR_IN
,
160 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
163 static struct usb_endpoint_descriptor
164 pn_hs_source_desc
= {
165 .bLength
= USB_DT_ENDPOINT_SIZE
,
166 .bDescriptorType
= USB_DT_ENDPOINT
,
168 .bEndpointAddress
= USB_DIR_IN
,
169 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
170 .wMaxPacketSize
= cpu_to_le16(512),
173 static struct usb_descriptor_header
*fs_pn_function
[] = {
174 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
175 (struct usb_descriptor_header
*) &pn_header_desc
,
176 (struct usb_descriptor_header
*) &pn_phonet_desc
,
177 (struct usb_descriptor_header
*) &pn_union_desc
,
178 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
179 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
180 (struct usb_descriptor_header
*) &pn_fs_sink_desc
,
181 (struct usb_descriptor_header
*) &pn_fs_source_desc
,
185 static struct usb_descriptor_header
*hs_pn_function
[] = {
186 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
187 (struct usb_descriptor_header
*) &pn_header_desc
,
188 (struct usb_descriptor_header
*) &pn_phonet_desc
,
189 (struct usb_descriptor_header
*) &pn_union_desc
,
190 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
191 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
192 (struct usb_descriptor_header
*) &pn_hs_sink_desc
,
193 (struct usb_descriptor_header
*) &pn_hs_source_desc
,
197 /*-------------------------------------------------------------------------*/
199 static int pn_net_open(struct net_device
*dev
)
201 netif_wake_queue(dev
);
205 static int pn_net_close(struct net_device
*dev
)
207 netif_stop_queue(dev
);
211 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
213 struct f_phonet
*fp
= ep
->driver_data
;
214 struct net_device
*dev
= fp
->dev
;
215 struct sk_buff
*skb
= req
->context
;
217 switch (req
->status
) {
219 dev
->stats
.tx_packets
++;
220 dev
->stats
.tx_bytes
+= skb
->len
;
223 case -ESHUTDOWN
: /* disconnected */
224 case -ECONNRESET
: /* disabled */
225 dev
->stats
.tx_aborted_errors
++;
227 dev
->stats
.tx_errors
++;
230 dev_kfree_skb_any(skb
);
231 netif_wake_queue(dev
);
234 static int pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
236 struct phonet_port
*port
= netdev_priv(dev
);
238 struct usb_request
*req
;
241 if (skb
->protocol
!= htons(ETH_P_PHONET
))
244 spin_lock_irqsave(&port
->lock
, flags
);
246 if (unlikely(!fp
)) /* race with carrier loss */
250 req
->buf
= skb
->data
;
251 req
->length
= skb
->len
;
252 req
->complete
= pn_tx_complete
;
256 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
259 netif_stop_queue(dev
);
263 spin_unlock_irqrestore(&port
->lock
, flags
);
267 dev
->stats
.tx_dropped
++;
272 static int pn_net_mtu(struct net_device
*dev
, int new_mtu
)
274 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
280 static const struct net_device_ops pn_netdev_ops
= {
281 .ndo_open
= pn_net_open
,
282 .ndo_stop
= pn_net_close
,
283 .ndo_start_xmit
= pn_net_xmit
,
284 .ndo_change_mtu
= pn_net_mtu
,
287 static void pn_net_setup(struct net_device
*dev
)
290 dev
->type
= ARPHRD_PHONET
;
291 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
292 dev
->mtu
= PHONET_DEV_MTU
;
293 dev
->hard_header_len
= 1;
294 dev
->dev_addr
[0] = PN_MEDIA_USB
;
296 dev
->tx_queue_len
= 1;
298 dev
->netdev_ops
= &pn_netdev_ops
;
299 dev
->destructor
= free_netdev
;
300 dev
->header_ops
= &phonet_header_ops
;
303 /*-------------------------------------------------------------------------*/
306 * Queue buffer for data from the host
309 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
311 struct net_device
*dev
= fp
->dev
;
315 page
= __netdev_alloc_page(dev
, gfp_flags
);
319 req
->buf
= page_address(page
);
320 req
->length
= PAGE_SIZE
;
323 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
325 netdev_free_page(dev
, page
);
329 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
331 struct f_phonet
*fp
= ep
->driver_data
;
332 struct net_device
*dev
= fp
->dev
;
333 struct page
*page
= req
->context
;
336 int status
= req
->status
;
340 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
343 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
344 if (req
->actual
< req
->length
) /* Last fragment */
346 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
351 if (skb
->len
== 0) { /* First fragment */
352 skb
->protocol
= htons(ETH_P_PHONET
);
353 skb_reset_mac_header(skb
);
354 /* Can't use pskb_pull() on page in IRQ */
355 memcpy(skb_put(skb
, 1), page_address(page
), 1);
358 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
,
359 skb
->len
== 0, req
->actual
);
362 if (req
->actual
< req
->length
) { /* Last fragment */
364 dev
->stats
.rx_packets
++;
365 dev
->stats
.rx_bytes
+= skb
->len
;
371 /* Do not resubmit in these cases: */
372 case -ESHUTDOWN
: /* disconnect */
373 case -ECONNABORTED
: /* hw reset */
374 case -ECONNRESET
: /* dequeued (unlink or netif down) */
378 /* Do resubmit in these cases: */
379 case -EOVERFLOW
: /* request buffer overflow */
380 dev
->stats
.rx_over_errors
++;
382 dev
->stats
.rx_errors
++;
387 netdev_free_page(dev
, page
);
389 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
392 /*-------------------------------------------------------------------------*/
394 static void __pn_reset(struct usb_function
*f
)
396 struct f_phonet
*fp
= func_to_pn(f
);
397 struct net_device
*dev
= fp
->dev
;
398 struct phonet_port
*port
= netdev_priv(dev
);
400 netif_carrier_off(dev
);
403 usb_ep_disable(fp
->out_ep
);
404 usb_ep_disable(fp
->in_ep
);
406 dev_kfree_skb_irq(fp
->rx
.skb
);
411 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
413 struct f_phonet
*fp
= func_to_pn(f
);
414 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
416 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
417 /* control interface, no altsetting */
418 return (alt
> 0) ? -EINVAL
: 0;
420 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
421 struct net_device
*dev
= fp
->dev
;
422 struct phonet_port
*port
= netdev_priv(dev
);
424 /* data intf (0: inactive, 1: active) */
428 spin_lock(&port
->lock
);
433 if (config_ep_by_speed(gadget
, f
, fp
->in_ep
) ||
434 config_ep_by_speed(gadget
, f
, fp
->out_ep
)) {
435 fp
->in_ep
->desc
= NULL
;
436 fp
->out_ep
->desc
= NULL
;
437 spin_unlock(&port
->lock
);
440 usb_ep_enable(fp
->out_ep
);
441 usb_ep_enable(fp
->in_ep
);
444 fp
->out_ep
->driver_data
= fp
;
445 fp
->in_ep
->driver_data
= fp
;
447 netif_carrier_on(dev
);
448 for (i
= 0; i
< phonet_rxq_size
; i
++)
449 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
451 spin_unlock(&port
->lock
);
458 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
460 struct f_phonet
*fp
= func_to_pn(f
);
462 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
465 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
466 struct phonet_port
*port
= netdev_priv(fp
->dev
);
469 spin_lock(&port
->lock
);
470 alt
= port
->usb
!= NULL
;
471 spin_unlock(&port
->lock
);
478 static void pn_disconnect(struct usb_function
*f
)
480 struct f_phonet
*fp
= func_to_pn(f
);
481 struct phonet_port
*port
= netdev_priv(fp
->dev
);
484 /* remain disabled until set_alt */
485 spin_lock_irqsave(&port
->lock
, flags
);
487 spin_unlock_irqrestore(&port
->lock
, flags
);
490 /*-------------------------------------------------------------------------*/
493 int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
495 struct usb_composite_dev
*cdev
= c
->cdev
;
496 struct usb_gadget
*gadget
= cdev
->gadget
;
497 struct f_phonet
*fp
= func_to_pn(f
);
501 /* Reserve interface IDs */
502 status
= usb_interface_id(c
, f
);
505 pn_control_intf_desc
.bInterfaceNumber
= status
;
506 pn_union_desc
.bMasterInterface0
= status
;
508 status
= usb_interface_id(c
, f
);
511 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
512 pn_data_intf_desc
.bInterfaceNumber
= status
;
513 pn_union_desc
.bSlaveInterface0
= status
;
515 /* Reserve endpoints */
517 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
521 ep
->driver_data
= fp
; /* Claim */
523 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
527 ep
->driver_data
= fp
; /* Claim */
529 pn_hs_sink_desc
.bEndpointAddress
=
530 pn_fs_sink_desc
.bEndpointAddress
;
531 pn_hs_source_desc
.bEndpointAddress
=
532 pn_fs_source_desc
.bEndpointAddress
;
534 /* Do not try to bind Phonet twice... */
535 fp
->function
.descriptors
= fs_pn_function
;
536 fp
->function
.hs_descriptors
= hs_pn_function
;
538 /* Incoming USB requests */
540 for (i
= 0; i
< phonet_rxq_size
; i
++) {
541 struct usb_request
*req
;
543 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
547 req
->complete
= pn_rx_complete
;
548 fp
->out_reqv
[i
] = req
;
551 /* Outgoing USB requests */
552 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
556 INFO(cdev
, "USB CDC Phonet function\n");
557 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
558 fp
->out_ep
->name
, fp
->in_ep
->name
);
563 fp
->out_ep
->driver_data
= NULL
;
565 fp
->in_ep
->driver_data
= NULL
;
566 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
571 pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
573 struct f_phonet
*fp
= func_to_pn(f
);
576 /* We are already disconnected */
578 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
579 for (i
= 0; i
< phonet_rxq_size
; i
++)
581 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
586 /*-------------------------------------------------------------------------*/
588 static struct net_device
*dev
;
590 int __init
phonet_bind_config(struct usb_configuration
*c
)
595 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
596 fp
= kzalloc(size
, GFP_KERNEL
);
601 fp
->function
.name
= "phonet";
602 fp
->function
.bind
= pn_bind
;
603 fp
->function
.unbind
= pn_unbind
;
604 fp
->function
.set_alt
= pn_set_alt
;
605 fp
->function
.get_alt
= pn_get_alt
;
606 fp
->function
.disable
= pn_disconnect
;
607 spin_lock_init(&fp
->rx
.lock
);
609 err
= usb_add_function(c
, &fp
->function
);
615 int __init
gphonet_setup(struct usb_gadget
*gadget
)
617 struct phonet_port
*port
;
620 /* Create net device */
622 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", pn_net_setup
);
626 port
= netdev_priv(dev
);
627 spin_lock_init(&port
->lock
);
628 netif_carrier_off(dev
);
629 SET_NETDEV_DEV(dev
, &gadget
->dev
);
631 err
= register_netdev(dev
);
637 void gphonet_cleanup(void)
639 unregister_netdev(dev
);