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/device.h>
18 #include <linux/netdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_phonet.h>
21 #include <linux/if_arp.h>
23 #include <linux/usb/ch9.h>
24 #include <linux/usb/cdc.h>
25 #include <linux/usb/composite.h>
29 #define PN_MEDIA_USB 0x1B
31 #if (PAGE_SIZE % MAXPACKET)
32 #error MAXPACKET must divide PAGE_SIZE!
35 /*-------------------------------------------------------------------------*/
43 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
= 17;
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
= 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
= 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
= cpu_to_le16(MAXPACKET
),
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
= 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 netif_wake_queue(dev
);
195 static int pn_net_close(struct net_device
*dev
)
197 netif_stop_queue(dev
);
201 static void pn_tx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
203 struct f_phonet
*fp
= ep
->driver_data
;
204 struct net_device
*dev
= fp
->dev
;
205 struct sk_buff
*skb
= req
->context
;
207 switch (req
->status
) {
209 dev
->stats
.tx_packets
++;
210 dev
->stats
.tx_bytes
+= skb
->len
;
213 case -ESHUTDOWN
: /* disconnected */
214 case -ECONNRESET
: /* disabled */
215 dev
->stats
.tx_aborted_errors
++;
217 dev
->stats
.tx_errors
++;
220 dev_kfree_skb_any(skb
);
221 netif_wake_queue(dev
);
224 static int pn_net_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
226 struct phonet_port
*port
= netdev_priv(dev
);
228 struct usb_request
*req
;
231 if (skb
->protocol
!= htons(ETH_P_PHONET
))
234 spin_lock_irqsave(&port
->lock
, flags
);
236 if (unlikely(!fp
)) /* race with carrier loss */
240 req
->buf
= skb
->data
;
241 req
->length
= skb
->len
;
242 req
->complete
= pn_tx_complete
;
246 if (unlikely(usb_ep_queue(fp
->in_ep
, req
, GFP_ATOMIC
)))
249 netif_stop_queue(dev
);
253 spin_unlock_irqrestore(&port
->lock
, flags
);
257 dev
->stats
.tx_dropped
++;
262 static int pn_net_mtu(struct net_device
*dev
, int new_mtu
)
264 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
270 static const struct net_device_ops pn_netdev_ops
= {
271 .ndo_open
= pn_net_open
,
272 .ndo_stop
= pn_net_close
,
273 .ndo_start_xmit
= pn_net_xmit
,
274 .ndo_change_mtu
= pn_net_mtu
,
277 static void pn_net_setup(struct net_device
*dev
)
280 dev
->type
= ARPHRD_PHONET
;
281 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
282 dev
->mtu
= PHONET_DEV_MTU
;
283 dev
->hard_header_len
= 1;
284 dev
->dev_addr
[0] = PN_MEDIA_USB
;
286 dev
->tx_queue_len
= 1;
288 dev
->netdev_ops
= &pn_netdev_ops
;
289 dev
->destructor
= free_netdev
;
290 dev
->header_ops
= &phonet_header_ops
;
293 /*-------------------------------------------------------------------------*/
296 * Queue buffer for data from the host
299 pn_rx_submit(struct f_phonet
*fp
, struct usb_request
*req
, gfp_t gfp_flags
)
301 struct net_device
*dev
= fp
->dev
;
305 page
= __netdev_alloc_page(dev
, gfp_flags
);
309 req
->buf
= page_address(page
);
310 req
->length
= PAGE_SIZE
;
313 err
= usb_ep_queue(fp
->out_ep
, req
, gfp_flags
);
315 netdev_free_page(dev
, page
);
319 static void pn_rx_complete(struct usb_ep
*ep
, struct usb_request
*req
)
321 struct f_phonet
*fp
= ep
->driver_data
;
322 struct net_device
*dev
= fp
->dev
;
323 struct page
*page
= req
->context
;
326 int status
= req
->status
;
330 spin_lock_irqsave(&fp
->rx
.lock
, flags
);
333 skb
= fp
->rx
.skb
= netdev_alloc_skb(dev
, 12);
334 if (req
->actual
< req
->length
) /* Last fragment */
336 spin_unlock_irqrestore(&fp
->rx
.lock
, flags
);
341 if (skb
->len
== 0) { /* First fragment */
342 skb
->protocol
= htons(ETH_P_PHONET
);
343 skb_reset_mac_header(skb
);
344 /* Can't use pskb_pull() on page in IRQ */
345 memcpy(skb_put(skb
, 1), page_address(page
), 1);
348 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
, page
,
349 skb
->len
== 0, req
->actual
);
352 if (req
->actual
< req
->length
) { /* Last fragment */
354 dev
->stats
.rx_packets
++;
355 dev
->stats
.rx_bytes
+= skb
->len
;
361 /* Do not resubmit in these cases: */
362 case -ESHUTDOWN
: /* disconnect */
363 case -ECONNABORTED
: /* hw reset */
364 case -ECONNRESET
: /* dequeued (unlink or netif down) */
368 /* Do resubmit in these cases: */
369 case -EOVERFLOW
: /* request buffer overflow */
370 dev
->stats
.rx_over_errors
++;
372 dev
->stats
.rx_errors
++;
377 netdev_free_page(dev
, page
);
379 pn_rx_submit(fp
, req
, GFP_ATOMIC
);
382 /*-------------------------------------------------------------------------*/
384 static void __pn_reset(struct usb_function
*f
)
386 struct f_phonet
*fp
= func_to_pn(f
);
387 struct net_device
*dev
= fp
->dev
;
388 struct phonet_port
*port
= netdev_priv(dev
);
390 netif_carrier_off(dev
);
393 usb_ep_disable(fp
->out_ep
);
394 usb_ep_disable(fp
->in_ep
);
396 dev_kfree_skb_irq(fp
->rx
.skb
);
401 static int pn_set_alt(struct usb_function
*f
, unsigned intf
, unsigned alt
)
403 struct f_phonet
*fp
= func_to_pn(f
);
404 struct usb_gadget
*gadget
= fp
->function
.config
->cdev
->gadget
;
406 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
407 /* control interface, no altsetting */
408 return (alt
> 0) ? -EINVAL
: 0;
410 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
411 struct net_device
*dev
= fp
->dev
;
412 struct phonet_port
*port
= netdev_priv(dev
);
414 /* data intf (0: inactive, 1: active) */
418 spin_lock(&port
->lock
);
423 if (config_ep_by_speed(gadget
, f
, fp
->in_ep
) ||
424 config_ep_by_speed(gadget
, f
, fp
->out_ep
)) {
425 fp
->in_ep
->desc
= NULL
;
426 fp
->out_ep
->desc
= NULL
;
427 spin_unlock(&port
->lock
);
430 usb_ep_enable(fp
->out_ep
);
431 usb_ep_enable(fp
->in_ep
);
434 fp
->out_ep
->driver_data
= fp
;
435 fp
->in_ep
->driver_data
= fp
;
437 netif_carrier_on(dev
);
438 for (i
= 0; i
< phonet_rxq_size
; i
++)
439 pn_rx_submit(fp
, fp
->out_reqv
[i
], GFP_ATOMIC
);
441 spin_unlock(&port
->lock
);
448 static int pn_get_alt(struct usb_function
*f
, unsigned intf
)
450 struct f_phonet
*fp
= func_to_pn(f
);
452 if (intf
== pn_control_intf_desc
.bInterfaceNumber
)
455 if (intf
== pn_data_intf_desc
.bInterfaceNumber
) {
456 struct phonet_port
*port
= netdev_priv(fp
->dev
);
459 spin_lock(&port
->lock
);
460 alt
= port
->usb
!= NULL
;
461 spin_unlock(&port
->lock
);
468 static void pn_disconnect(struct usb_function
*f
)
470 struct f_phonet
*fp
= func_to_pn(f
);
471 struct phonet_port
*port
= netdev_priv(fp
->dev
);
474 /* remain disabled until set_alt */
475 spin_lock_irqsave(&port
->lock
, flags
);
477 spin_unlock_irqrestore(&port
->lock
, flags
);
480 /*-------------------------------------------------------------------------*/
483 int pn_bind(struct usb_configuration
*c
, struct usb_function
*f
)
485 struct usb_composite_dev
*cdev
= c
->cdev
;
486 struct usb_gadget
*gadget
= cdev
->gadget
;
487 struct f_phonet
*fp
= func_to_pn(f
);
491 /* Reserve interface IDs */
492 status
= usb_interface_id(c
, f
);
495 pn_control_intf_desc
.bInterfaceNumber
= status
;
496 pn_union_desc
.bMasterInterface0
= status
;
498 status
= usb_interface_id(c
, f
);
501 pn_data_nop_intf_desc
.bInterfaceNumber
= status
;
502 pn_data_intf_desc
.bInterfaceNumber
= status
;
503 pn_union_desc
.bSlaveInterface0
= status
;
505 /* Reserve endpoints */
507 ep
= usb_ep_autoconfig(gadget
, &pn_fs_sink_desc
);
511 ep
->driver_data
= fp
; /* Claim */
513 ep
= usb_ep_autoconfig(gadget
, &pn_fs_source_desc
);
517 ep
->driver_data
= fp
; /* Claim */
519 pn_hs_sink_desc
.bEndpointAddress
=
520 pn_fs_sink_desc
.bEndpointAddress
;
521 pn_hs_source_desc
.bEndpointAddress
=
522 pn_fs_source_desc
.bEndpointAddress
;
524 /* Do not try to bind Phonet twice... */
525 fp
->function
.descriptors
= fs_pn_function
;
526 fp
->function
.hs_descriptors
= hs_pn_function
;
528 /* Incoming USB requests */
530 for (i
= 0; i
< phonet_rxq_size
; i
++) {
531 struct usb_request
*req
;
533 req
= usb_ep_alloc_request(fp
->out_ep
, GFP_KERNEL
);
537 req
->complete
= pn_rx_complete
;
538 fp
->out_reqv
[i
] = req
;
541 /* Outgoing USB requests */
542 fp
->in_req
= usb_ep_alloc_request(fp
->in_ep
, GFP_KERNEL
);
546 INFO(cdev
, "USB CDC Phonet function\n");
547 INFO(cdev
, "using %s, OUT %s, IN %s\n", cdev
->gadget
->name
,
548 fp
->out_ep
->name
, fp
->in_ep
->name
);
553 fp
->out_ep
->driver_data
= NULL
;
555 fp
->in_ep
->driver_data
= NULL
;
556 ERROR(cdev
, "USB CDC Phonet: cannot autoconfigure\n");
561 pn_unbind(struct usb_configuration
*c
, struct usb_function
*f
)
563 struct f_phonet
*fp
= func_to_pn(f
);
566 /* We are already disconnected */
568 usb_ep_free_request(fp
->in_ep
, fp
->in_req
);
569 for (i
= 0; i
< phonet_rxq_size
; i
++)
571 usb_ep_free_request(fp
->out_ep
, fp
->out_reqv
[i
]);
576 /*-------------------------------------------------------------------------*/
578 static struct net_device
*dev
;
580 int __init
phonet_bind_config(struct usb_configuration
*c
)
585 size
= sizeof(*fp
) + (phonet_rxq_size
* sizeof(struct usb_request
*));
586 fp
= kzalloc(size
, GFP_KERNEL
);
591 fp
->function
.name
= "phonet";
592 fp
->function
.bind
= pn_bind
;
593 fp
->function
.unbind
= pn_unbind
;
594 fp
->function
.set_alt
= pn_set_alt
;
595 fp
->function
.get_alt
= pn_get_alt
;
596 fp
->function
.disable
= pn_disconnect
;
597 spin_lock_init(&fp
->rx
.lock
);
599 err
= usb_add_function(c
, &fp
->function
);
605 int __init
gphonet_setup(struct usb_gadget
*gadget
)
607 struct phonet_port
*port
;
610 /* Create net device */
612 dev
= alloc_netdev(sizeof(*port
), "upnlink%d", pn_net_setup
);
616 port
= netdev_priv(dev
);
617 spin_lock_init(&port
->lock
);
618 netif_carrier_off(dev
);
619 SET_NETDEV_DEV(dev
, &gadget
->dev
);
621 err
= register_netdev(dev
);
627 void gphonet_cleanup(void)
629 unregister_netdev(dev
);