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 /*-------------------------------------------------------------------------*/
47 struct usb_function function
;
48 struct net_device
*dev
;
49 struct usb_ep
*in_ep
, *out_ep
;
51 struct usb_request
*in_req
;
52 struct usb_request
*out_reqv
[0];
55 static int phonet_rxq_size
= 2;
57 static inline struct f_phonet
*func_to_pn(struct usb_function
*f
)
59 return container_of(f
, struct f_phonet
, function
);
62 /*-------------------------------------------------------------------------*/
64 #define USB_CDC_SUBCLASS_PHONET 0xfe
65 #define USB_CDC_PHONET_TYPE 0xab
67 static struct usb_interface_descriptor
68 pn_control_intf_desc
= {
69 .bLength
= sizeof pn_control_intf_desc
,
70 .bDescriptorType
= USB_DT_INTERFACE
,
72 /* .bInterfaceNumber = DYNAMIC, */
73 .bInterfaceClass
= USB_CLASS_COMM
,
74 .bInterfaceSubClass
= USB_CDC_SUBCLASS_PHONET
,
77 static const struct usb_cdc_header_desc
79 .bLength
= sizeof pn_header_desc
,
80 .bDescriptorType
= USB_DT_CS_INTERFACE
,
81 .bDescriptorSubType
= USB_CDC_HEADER_TYPE
,
82 .bcdCDC
= __constant_cpu_to_le16(0x0110),
85 static const struct usb_cdc_header_desc
87 .bLength
= sizeof pn_phonet_desc
,
88 .bDescriptorType
= USB_DT_CS_INTERFACE
,
89 .bDescriptorSubType
= USB_CDC_PHONET_TYPE
,
90 .bcdCDC
= __constant_cpu_to_le16(0x1505), /* ??? */
93 static struct usb_cdc_union_desc
95 .bLength
= sizeof pn_union_desc
,
96 .bDescriptorType
= USB_DT_CS_INTERFACE
,
97 .bDescriptorSubType
= USB_CDC_UNION_TYPE
,
99 /* .bMasterInterface0 = DYNAMIC, */
100 /* .bSlaveInterface0 = DYNAMIC, */
103 static struct usb_interface_descriptor
104 pn_data_nop_intf_desc
= {
105 .bLength
= sizeof pn_data_nop_intf_desc
,
106 .bDescriptorType
= USB_DT_INTERFACE
,
108 /* .bInterfaceNumber = DYNAMIC, */
109 .bAlternateSetting
= 0,
111 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
114 static struct usb_interface_descriptor
115 pn_data_intf_desc
= {
116 .bLength
= sizeof pn_data_intf_desc
,
117 .bDescriptorType
= USB_DT_INTERFACE
,
119 /* .bInterfaceNumber = DYNAMIC, */
120 .bAlternateSetting
= 1,
122 .bInterfaceClass
= USB_CLASS_CDC_DATA
,
125 static struct usb_endpoint_descriptor
127 .bLength
= USB_DT_ENDPOINT_SIZE
,
128 .bDescriptorType
= USB_DT_ENDPOINT
,
130 .bEndpointAddress
= USB_DIR_OUT
,
131 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
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
,
141 .wMaxPacketSize
= __constant_cpu_to_le16(512),
144 static struct usb_endpoint_descriptor
145 pn_fs_source_desc
= {
146 .bLength
= USB_DT_ENDPOINT_SIZE
,
147 .bDescriptorType
= USB_DT_ENDPOINT
,
149 .bEndpointAddress
= USB_DIR_IN
,
150 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
153 static struct usb_endpoint_descriptor
154 pn_hs_source_desc
= {
155 .bLength
= USB_DT_ENDPOINT_SIZE
,
156 .bDescriptorType
= USB_DT_ENDPOINT
,
158 .bEndpointAddress
= USB_DIR_IN
,
159 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
160 .wMaxPacketSize
= __constant_cpu_to_le16(512),
163 static struct usb_descriptor_header
*fs_pn_function
[] = {
164 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
165 (struct usb_descriptor_header
*) &pn_header_desc
,
166 (struct usb_descriptor_header
*) &pn_phonet_desc
,
167 (struct usb_descriptor_header
*) &pn_union_desc
,
168 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
169 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
170 (struct usb_descriptor_header
*) &pn_fs_sink_desc
,
171 (struct usb_descriptor_header
*) &pn_fs_source_desc
,
175 static struct usb_descriptor_header
*hs_pn_function
[] = {
176 (struct usb_descriptor_header
*) &pn_control_intf_desc
,
177 (struct usb_descriptor_header
*) &pn_header_desc
,
178 (struct usb_descriptor_header
*) &pn_phonet_desc
,
179 (struct usb_descriptor_header
*) &pn_union_desc
,
180 (struct usb_descriptor_header
*) &pn_data_nop_intf_desc
,
181 (struct usb_descriptor_header
*) &pn_data_intf_desc
,
182 (struct usb_descriptor_header
*) &pn_hs_sink_desc
,
183 (struct usb_descriptor_header
*) &pn_hs_source_desc
,
187 /*-------------------------------------------------------------------------*/
189 static int pn_net_open(struct net_device
*dev
)
191 if (netif_carrier_ok(dev
))
192 netif_wake_queue(dev
);
196 static int pn_net_close(struct net_device
*dev
)
198 netif_stop_queue(dev
);
202 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
204 struct f_phonet
*fp
= ep
->driver_data
;
205 struct net_device
*dev
= fp
->dev
;
206 struct sk_buff
*skb
= req
->context
;
208 switch (req
->status
) {
210 dev
->stats
.tx_packets
++;
211 dev
->stats
.tx_bytes
+= skb
->len
;
214 case -ESHUTDOWN
: /* disconnected */
215 case -ECONNRESET
: /* disabled */
216 dev
->stats
.tx_aborted_errors
++;
218 dev
->stats
.tx_errors
++;
221 dev_kfree_skb_any(skb
);
222 if (netif_carrier_ok(dev
))
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
);
258 dev_kfree_skb_any(skb
);
259 dev
->stats
.tx_dropped
++;
264 static int pn_net_mtu(struct net_device
*dev
, int new_mtu
)
266 struct phonet_port
*port
= netdev_priv(dev
);
270 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
273 spin_lock_irqsave(&port
->lock
, flags
);
274 if (!netif_carrier_ok(dev
)) {
278 spin_unlock_irqrestore(&port
->lock
, flags
);
282 static const struct net_device_ops pn_netdev_ops
= {
283 .ndo_open
= pn_net_open
,
284 .ndo_stop
= pn_net_close
,
285 .ndo_start_xmit
= pn_net_xmit
,
286 .ndo_change_mtu
= pn_net_mtu
,
289 static void pn_net_setup(struct net_device
*dev
)
292 dev
->type
= ARPHRD_PHONET
;
293 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
294 dev
->mtu
= PHONET_DEV_MTU
;
295 dev
->hard_header_len
= 1;
296 dev
->dev_addr
[0] = PN_MEDIA_USB
;
298 dev
->tx_queue_len
= 1;
300 dev
->netdev_ops
= &pn_netdev_ops
;
301 dev
->destructor
= free_netdev
;
302 dev
->header_ops
= &phonet_header_ops
;
305 /*-------------------------------------------------------------------------*/
308 * Queue buffer for data from the host
311 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
314 const size_t size
= fp
->dev
->mtu
;
317 skb
= alloc_skb(size
, gfp_flags
);
321 req
->buf
= skb
->data
;
325 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
327 dev_kfree_skb_any(skb
);
331 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
333 struct f_phonet
*fp
= ep
->driver_data
;
334 struct net_device
*dev
= fp
->dev
;
335 struct sk_buff
*skb
= req
->context
;
336 int status
= req
->status
;
340 if (unlikely(!netif_running(dev
)))
342 if (unlikely(req
->actual
< 1))
344 skb_put(skb
, req
->actual
);
345 skb
->protocol
= htons(ETH_P_PHONET
);
346 skb_reset_mac_header(skb
);
349 dev
->stats
.rx_packets
++;
350 dev
->stats
.rx_bytes
+= skb
->len
;
356 /* Do not resubmit in these cases: */
357 case -ESHUTDOWN
: /* disconnect */
358 case -ECONNABORTED
: /* hw reset */
359 case -ECONNRESET
: /* dequeued (unlink or netif down) */
363 /* Do resubmit in these cases: */
364 case -EOVERFLOW
: /* request buffer overflow */
365 dev
->stats
.rx_over_errors
++;
367 dev
->stats
.rx_errors
++;
372 dev_kfree_skb_any(skb
);
374 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
377 /*-------------------------------------------------------------------------*/
379 static void __pn_reset(struct usb_function
*f
)
381 struct f_phonet
*fp
= func_to_pn(f
);
382 struct net_device
*dev
= fp
->dev
;
383 struct phonet_port
*port
= netdev_priv(dev
);
385 netif_carrier_off(dev
);
386 netif_stop_queue(dev
);
389 usb_ep_disable(fp
->out_ep
);
390 usb_ep_disable(fp
->in_ep
);
393 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
395 struct f_phonet
*fp
= func_to_pn(f
);
396 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
398 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
399 /* control interface, no altsetting */
400 return (alt
> 0) ? -EINVAL
: 0;
402 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
403 struct net_device
*dev
= fp
->dev
;
404 struct phonet_port
*port
= netdev_priv(dev
);
406 /* data intf (0: inactive, 1: active) */
410 spin_lock(&port
->lock
);
413 struct usb_endpoint_descriptor
*out
, *in
;
416 out
= ep_choose(gadget
,
419 in
= ep_choose(gadget
,
422 usb_ep_enable(fp
->out_ep
, out
);
423 usb_ep_enable(fp
->in_ep
, in
);
426 fp
->out_ep
->driver_data
= fp
;
427 fp
->in_ep
->driver_data
= fp
;
429 netif_carrier_on(dev
);
430 if (netif_running(dev
))
431 netif_wake_queue(dev
);
432 for (i
= 0; i
< phonet_rxq_size
; i
++)
433 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
435 spin_unlock(&port
->lock
);
442 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
444 struct f_phonet
*fp
= func_to_pn(f
);
446 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
449 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
450 struct phonet_port
*port
= netdev_priv(fp
->dev
);
453 spin_lock(&port
->lock
);
454 alt
= port
->usb
!= NULL
;
455 spin_unlock(&port
->lock
);
462 static void pn_disconnect(struct usb_function
*f
)
464 struct f_phonet
*fp
= func_to_pn(f
);
465 struct phonet_port
*port
= netdev_priv(fp
->dev
);
468 /* remain disabled until set_alt */
469 spin_lock_irqsave(&port
->lock
, flags
);
471 spin_unlock_irqrestore(&port
->lock
, flags
);
474 /*-------------------------------------------------------------------------*/
477 int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
479 struct usb_composite_dev
*cdev
= c
->cdev
;
480 struct usb_gadget
*gadget
= cdev
->gadget
;
481 struct f_phonet
*fp
= func_to_pn(f
);
485 /* Reserve interface IDs */
486 status
= usb_interface_id(c
, f
);
489 pn_control_intf_desc
.bInterfaceNumber
= status
;
490 pn_union_desc
.bMasterInterface0
= status
;
492 status
= usb_interface_id(c
, f
);
495 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
496 pn_data_intf_desc
.bInterfaceNumber
= status
;
497 pn_union_desc
.bSlaveInterface0
= status
;
499 /* Reserve endpoints */
501 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
505 ep
->driver_data
= fp
; /* Claim */
507 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
511 ep
->driver_data
= fp
; /* Claim */
513 pn_hs_sink_desc
.bEndpointAddress
=
514 pn_fs_sink_desc
.bEndpointAddress
;
515 pn_hs_source_desc
.bEndpointAddress
=
516 pn_fs_source_desc
.bEndpointAddress
;
518 /* Do not try to bind Phonet twice... */
519 fp
->function
.descriptors
= fs_pn_function
;
520 fp
->function
.hs_descriptors
= hs_pn_function
;
522 /* Incoming USB requests */
524 for (i
= 0; i
< phonet_rxq_size
; i
++) {
525 struct usb_request
*req
;
527 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
531 req
->complete
= pn_rx_complete
;
532 fp
->out_reqv
[i
] = req
;
535 /* Outgoing USB requests */
536 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
540 INFO(cdev
, "USB CDC Phonet function\n");
541 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
542 fp
->out_ep
->name
, fp
->in_ep
->name
);
547 fp
->out_ep
->driver_data
= NULL
;
549 fp
->in_ep
->driver_data
= NULL
;
550 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
555 pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
557 struct f_phonet
*fp
= func_to_pn(f
);
560 /* We are already disconnected */
562 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
563 for (i
= 0; i
< phonet_rxq_size
; i
++)
565 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
570 /*-------------------------------------------------------------------------*/
572 static struct net_device
*dev
;
574 int __init
phonet_bind_config(struct usb_configuration
*c
)
579 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
584 fp
->function
.name
= "phonet";
585 fp
->function
.bind
= pn_bind
;
586 fp
->function
.unbind
= pn_unbind
;
587 fp
->function
.set_alt
= pn_set_alt
;
588 fp
->function
.get_alt
= pn_get_alt
;
589 fp
->function
.disable
= pn_disconnect
;
591 err
= usb_add_function(c
, &fp
->function
);
597 int __init
gphonet_setup(struct usb_gadget
*gadget
)
599 struct phonet_port
*port
;
602 /* Create net device */
604 dev
= alloc_netdev(sizeof(*port
)
605 + (phonet_rxq_size
* sizeof(struct usb_request
*)),
606 "upnlink%d", pn_net_setup
);
610 port
= netdev_priv(dev
);
611 spin_lock_init(&port
->lock
);
612 netif_carrier_off(dev
);
613 netif_stop_queue(dev
);
614 SET_NETDEV_DEV(dev
, &gadget
->dev
);
616 err
= register_netdev(dev
);
622 void gphonet_cleanup(void)
624 unregister_netdev(dev
);