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/kernel.h>
24 #include <linux/device.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/if_phonet.h>
29 #include <linux/if_arp.h>
31 #include <linux/usb/ch9.h>
32 #include <linux/usb/cdc.h>
33 #include <linux/usb/composite.h>
37 #define PN_MEDIA_USB 0x1B
39 #if (PAGE_SIZE % MAXPACKET)
40 #error MAXPACKET must divide PAGE_SIZE!
43 /*-------------------------------------------------------------------------*/
51 struct usb_function function
;
56 struct net_device
*dev
;
57 struct usb_ep
*in_ep
, *out_ep
;
59 struct usb_request
*in_req
;
60 struct usb_request
*out_reqv
[0];
63 static int phonet_rxq_size
= 17;
65 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
67 return container_of(f
, struct f_phonet
, function
);
70 /*-------------------------------------------------------------------------*/
72 #define USB_CDC_SUBCLASS_PHONET 0xfe
73 #define USB_CDC_PHONET_TYPE 0xab
75 static struct usb_interface_descriptor
76 pn_control_intf_desc
= {
77 .bLength
= sizeof pn_control_intf_desc
,
78 .bDescriptorType
= USB_DT_INTERFACE
,
80 /* .bInterfaceNumber = DYNAMIC, */
81 .bInterfaceClass
= USB_CLASS_COMM
,
82 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
85 static const struct usb_cdc_header_desc
87 .bLength
= sizeof pn_header_desc
,
88 .bDescriptorType
= USB_DT_CS_INTERFACE
,
89 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
90 .bcdCDC
= cpu_to_le16(0x0110),
93 static const struct usb_cdc_header_desc
95 .bLength
= sizeof pn_phonet_desc
,
96 .bDescriptorType
= USB_DT_CS_INTERFACE
,
97 .bDescriptorSubType
= USB_CDC_PHONET_TYPE
,
98 .bcdCDC
= cpu_to_le16(0x1505), /* ??? */
101 static struct usb_cdc_union_desc
103 .bLength
= sizeof pn_union_desc
,
104 .bDescriptorType
= USB_DT_CS_INTERFACE
,
105 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
107 /* .bMasterInterface0 = DYNAMIC, */
108 /* .bSlaveInterface0 = DYNAMIC, */
111 static struct usb_interface_descriptor
112 pn_data_nop_intf_desc
= {
113 .bLength
= sizeof pn_data_nop_intf_desc
,
114 .bDescriptorType
= USB_DT_INTERFACE
,
116 /* .bInterfaceNumber = DYNAMIC, */
117 .bAlternateSetting
= 0,
119 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
122 static struct usb_interface_descriptor
123 pn_data_intf_desc
= {
124 .bLength
= sizeof pn_data_intf_desc
,
125 .bDescriptorType
= USB_DT_INTERFACE
,
127 /* .bInterfaceNumber = DYNAMIC, */
128 .bAlternateSetting
= 1,
130 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
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
,
142 static struct usb_endpoint_descriptor
144 .bLength
= USB_DT_ENDPOINT_SIZE
,
145 .bDescriptorType
= USB_DT_ENDPOINT
,
147 .bEndpointAddress
= USB_DIR_OUT
,
148 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
149 .wMaxPacketSize
= cpu_to_le16(MAXPACKET
),
152 static struct usb_endpoint_descriptor
153 pn_fs_source_desc
= {
154 .bLength
= USB_DT_ENDPOINT_SIZE
,
155 .bDescriptorType
= USB_DT_ENDPOINT
,
157 .bEndpointAddress
= USB_DIR_IN
,
158 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
161 static struct usb_endpoint_descriptor
162 pn_hs_source_desc
= {
163 .bLength
= USB_DT_ENDPOINT_SIZE
,
164 .bDescriptorType
= USB_DT_ENDPOINT
,
166 .bEndpointAddress
= USB_DIR_IN
,
167 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
168 .wMaxPacketSize
= cpu_to_le16(512),
171 static struct usb_descriptor_header
*fs_pn_function
[] = {
172 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
173 (struct usb_descriptor_header
*) &pn_header_desc
,
174 (struct usb_descriptor_header
*) &pn_phonet_desc
,
175 (struct usb_descriptor_header
*) &pn_union_desc
,
176 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
177 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
178 (struct usb_descriptor_header
*) &pn_fs_sink_desc
,
179 (struct usb_descriptor_header
*) &pn_fs_source_desc
,
183 static struct usb_descriptor_header
*hs_pn_function
[] = {
184 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
185 (struct usb_descriptor_header
*) &pn_header_desc
,
186 (struct usb_descriptor_header
*) &pn_phonet_desc
,
187 (struct usb_descriptor_header
*) &pn_union_desc
,
188 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
189 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
190 (struct usb_descriptor_header
*) &pn_hs_sink_desc
,
191 (struct usb_descriptor_header
*) &pn_hs_source_desc
,
195 /*-------------------------------------------------------------------------*/
197 static int pn_net_open(struct net_device
*dev
)
199 netif_wake_queue(dev
);
203 static int pn_net_close(struct net_device
*dev
)
205 netif_stop_queue(dev
);
209 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
211 struct f_phonet
*fp
= ep
->driver_data
;
212 struct net_device
*dev
= fp
->dev
;
213 struct sk_buff
*skb
= req
->context
;
215 switch (req
->status
) {
217 dev
->stats
.tx_packets
++;
218 dev
->stats
.tx_bytes
+= skb
->len
;
221 case -ESHUTDOWN
: /* disconnected */
222 case -ECONNRESET
: /* disabled */
223 dev
->stats
.tx_aborted_errors
++;
225 dev
->stats
.tx_errors
++;
228 dev_kfree_skb_any(skb
);
229 netif_wake_queue(dev
);
232 static int pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
234 struct phonet_port
*port
= netdev_priv(dev
);
236 struct usb_request
*req
;
239 if (skb
->protocol
!= htons(ETH_P_PHONET
))
242 spin_lock_irqsave(&port
->lock
, flags
);
244 if (unlikely(!fp
)) /* race with carrier loss */
248 req
->buf
= skb
->data
;
249 req
->length
= skb
->len
;
250 req
->complete
= pn_tx_complete
;
254 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
257 netif_stop_queue(dev
);
261 spin_unlock_irqrestore(&port
->lock
, flags
);
265 dev
->stats
.tx_dropped
++;
270 static int pn_net_mtu(struct net_device
*dev
, int new_mtu
)
272 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
278 static const struct net_device_ops pn_netdev_ops
= {
279 .ndo_open
= pn_net_open
,
280 .ndo_stop
= pn_net_close
,
281 .ndo_start_xmit
= pn_net_xmit
,
282 .ndo_change_mtu
= pn_net_mtu
,
285 static void pn_net_setup(struct net_device
*dev
)
288 dev
->type
= ARPHRD_PHONET
;
289 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
290 dev
->mtu
= PHONET_DEV_MTU
;
291 dev
->hard_header_len
= 1;
292 dev
->dev_addr
[0] = PN_MEDIA_USB
;
294 dev
->tx_queue_len
= 1;
296 dev
->netdev_ops
= &pn_netdev_ops
;
297 dev
->destructor
= free_netdev
;
298 dev
->header_ops
= &phonet_header_ops
;
301 /*-------------------------------------------------------------------------*/
304 * Queue buffer for data from the host
307 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
309 struct net_device
*dev
= fp
->dev
;
313 page
= __netdev_alloc_page(dev
, gfp_flags
);
317 req
->buf
= page_address(page
);
318 req
->length
= PAGE_SIZE
;
321 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
323 netdev_free_page(dev
, page
);
327 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
329 struct f_phonet
*fp
= ep
->driver_data
;
330 struct net_device
*dev
= fp
->dev
;
331 struct page
*page
= req
->context
;
334 int status
= req
->status
;
338 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
341 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
342 if (req
->actual
< req
->length
) /* Last fragment */
344 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
348 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
, 0,
352 if (req
->actual
< req
->length
) { /* Last fragment */
353 skb
->protocol
= htons(ETH_P_PHONET
);
354 skb_reset_mac_header(skb
);
357 dev
->stats
.rx_packets
++;
358 dev
->stats
.rx_bytes
+= skb
->len
;
364 /* Do not resubmit in these cases: */
365 case -ESHUTDOWN
: /* disconnect */
366 case -ECONNABORTED
: /* hw reset */
367 case -ECONNRESET
: /* dequeued (unlink or netif down) */
371 /* Do resubmit in these cases: */
372 case -EOVERFLOW
: /* request buffer overflow */
373 dev
->stats
.rx_over_errors
++;
375 dev
->stats
.rx_errors
++;
380 netdev_free_page(dev
, page
);
382 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
385 /*-------------------------------------------------------------------------*/
387 static void __pn_reset(struct usb_function
*f
)
389 struct f_phonet
*fp
= func_to_pn(f
);
390 struct net_device
*dev
= fp
->dev
;
391 struct phonet_port
*port
= netdev_priv(dev
);
393 netif_carrier_off(dev
);
396 usb_ep_disable(fp
->out_ep
);
397 usb_ep_disable(fp
->in_ep
);
399 dev_kfree_skb_irq(fp
->rx
.skb
);
404 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
406 struct f_phonet
*fp
= func_to_pn(f
);
407 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
409 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
410 /* control interface, no altsetting */
411 return (alt
> 0) ? -EINVAL
: 0;
413 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
414 struct net_device
*dev
= fp
->dev
;
415 struct phonet_port
*port
= netdev_priv(dev
);
417 /* data intf (0: inactive, 1: active) */
421 spin_lock(&port
->lock
);
424 struct usb_endpoint_descriptor
*out
, *in
;
427 out
= ep_choose(gadget
,
430 in
= ep_choose(gadget
,
433 usb_ep_enable(fp
->out_ep
, out
);
434 usb_ep_enable(fp
->in_ep
, in
);
437 fp
->out_ep
->driver_data
= fp
;
438 fp
->in_ep
->driver_data
= fp
;
440 netif_carrier_on(dev
);
441 for (i
= 0; i
< phonet_rxq_size
; i
++)
442 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
444 spin_unlock(&port
->lock
);
451 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
453 struct f_phonet
*fp
= func_to_pn(f
);
455 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
458 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
459 struct phonet_port
*port
= netdev_priv(fp
->dev
);
462 spin_lock(&port
->lock
);
463 alt
= port
->usb
!= NULL
;
464 spin_unlock(&port
->lock
);
471 static void pn_disconnect(struct usb_function
*f
)
473 struct f_phonet
*fp
= func_to_pn(f
);
474 struct phonet_port
*port
= netdev_priv(fp
->dev
);
477 /* remain disabled until set_alt */
478 spin_lock_irqsave(&port
->lock
, flags
);
480 spin_unlock_irqrestore(&port
->lock
, flags
);
483 /*-------------------------------------------------------------------------*/
486 int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
488 struct usb_composite_dev
*cdev
= c
->cdev
;
489 struct usb_gadget
*gadget
= cdev
->gadget
;
490 struct f_phonet
*fp
= func_to_pn(f
);
494 /* Reserve interface IDs */
495 status
= usb_interface_id(c
, f
);
498 pn_control_intf_desc
.bInterfaceNumber
= status
;
499 pn_union_desc
.bMasterInterface0
= status
;
501 status
= usb_interface_id(c
, f
);
504 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
505 pn_data_intf_desc
.bInterfaceNumber
= status
;
506 pn_union_desc
.bSlaveInterface0
= status
;
508 /* Reserve endpoints */
510 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
514 ep
->driver_data
= fp
; /* Claim */
516 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
520 ep
->driver_data
= fp
; /* Claim */
522 pn_hs_sink_desc
.bEndpointAddress
=
523 pn_fs_sink_desc
.bEndpointAddress
;
524 pn_hs_source_desc
.bEndpointAddress
=
525 pn_fs_source_desc
.bEndpointAddress
;
527 /* Do not try to bind Phonet twice... */
528 fp
->function
.descriptors
= fs_pn_function
;
529 fp
->function
.hs_descriptors
= hs_pn_function
;
531 /* Incoming USB requests */
533 for (i
= 0; i
< phonet_rxq_size
; i
++) {
534 struct usb_request
*req
;
536 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
540 req
->complete
= pn_rx_complete
;
541 fp
->out_reqv
[i
] = req
;
544 /* Outgoing USB requests */
545 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
549 INFO(cdev
, "USB CDC Phonet function\n");
550 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
551 fp
->out_ep
->name
, fp
->in_ep
->name
);
556 fp
->out_ep
->driver_data
= NULL
;
558 fp
->in_ep
->driver_data
= NULL
;
559 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
564 pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
566 struct f_phonet
*fp
= func_to_pn(f
);
569 /* We are already disconnected */
571 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
572 for (i
= 0; i
< phonet_rxq_size
; i
++)
574 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
579 /*-------------------------------------------------------------------------*/
581 static struct net_device
*dev
;
583 int __init
phonet_bind_config(struct usb_configuration
*c
)
588 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
589 fp
= kzalloc(size
, GFP_KERNEL
);
594 fp
->function
.name
= "phonet";
595 fp
->function
.bind
= pn_bind
;
596 fp
->function
.unbind
= pn_unbind
;
597 fp
->function
.set_alt
= pn_set_alt
;
598 fp
->function
.get_alt
= pn_get_alt
;
599 fp
->function
.disable
= pn_disconnect
;
600 spin_lock_init(&fp
->rx
.lock
);
602 err
= usb_add_function(c
, &fp
->function
);
608 int __init
gphonet_setup(struct usb_gadget
*gadget
)
610 struct phonet_port
*port
;
613 /* Create net device */
615 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", pn_net_setup
);
619 port
= netdev_priv(dev
);
620 spin_lock_init(&port
->lock
);
621 netif_carrier_off(dev
);
622 SET_NETDEV_DEV(dev
, &gadget
->dev
);
624 err
= register_netdev(dev
);
630 void gphonet_cleanup(void)
632 unregister_netdev(dev
);