2 * phonet.c -- USB CDC Phonet host driver
4 * Copyright (C) 2008-2009 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/module.h>
25 #include <linux/gfp.h>
26 #include <linux/usb.h>
27 #include <linux/usb/cdc.h>
28 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/if_phonet.h>
31 #include <linux/phonet.h>
33 #define PN_MEDIA_USB 0x1B
35 static const unsigned rxq_size
= 17;
38 struct net_device
*dev
;
40 struct usb_interface
*intf
, *data_intf
;
41 struct usb_device
*usb
;
42 unsigned int tx_pipe
, rx_pipe
;
50 struct sk_buff
*rx_skb
;
54 static void tx_complete(struct urb
*req
);
55 static void rx_complete(struct urb
*req
);
58 * Network device callbacks
60 static netdev_tx_t
usbpn_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
62 struct usbpn_dev
*pnd
= netdev_priv(dev
);
63 struct urb
*req
= NULL
;
67 if (skb
->protocol
!= htons(ETH_P_PHONET
))
70 req
= usb_alloc_urb(0, GFP_ATOMIC
);
73 usb_fill_bulk_urb(req
, pnd
->usb
, pnd
->tx_pipe
, skb
->data
, skb
->len
,
75 req
->transfer_flags
= URB_ZERO_PACKET
;
76 err
= usb_submit_urb(req
, GFP_ATOMIC
);
82 spin_lock_irqsave(&pnd
->tx_lock
, flags
);
84 if (pnd
->tx_queue
>= dev
->tx_queue_len
)
85 netif_stop_queue(dev
);
86 spin_unlock_irqrestore(&pnd
->tx_lock
, flags
);
91 dev
->stats
.tx_dropped
++;
95 static void tx_complete(struct urb
*req
)
97 struct sk_buff
*skb
= req
->context
;
98 struct net_device
*dev
= skb
->dev
;
99 struct usbpn_dev
*pnd
= netdev_priv(dev
);
100 int status
= req
->status
;
104 dev
->stats
.tx_bytes
+= skb
->len
;
110 dev
->stats
.tx_aborted_errors
++;
112 dev
->stats
.tx_errors
++;
113 dev_dbg(&dev
->dev
, "TX error (%d)\n", status
);
115 dev
->stats
.tx_packets
++;
117 spin_lock(&pnd
->tx_lock
);
119 netif_wake_queue(dev
);
120 spin_unlock(&pnd
->tx_lock
);
122 dev_kfree_skb_any(skb
);
126 static int rx_submit(struct usbpn_dev
*pnd
, struct urb
*req
, gfp_t gfp_flags
)
128 struct net_device
*dev
= pnd
->dev
;
132 page
= __netdev_alloc_page(dev
, gfp_flags
);
136 usb_fill_bulk_urb(req
, pnd
->usb
, pnd
->rx_pipe
, page_address(page
),
137 PAGE_SIZE
, rx_complete
, dev
);
138 req
->transfer_flags
= 0;
139 err
= usb_submit_urb(req
, gfp_flags
);
141 dev_dbg(&dev
->dev
, "RX submit error (%d)\n", err
);
142 netdev_free_page(dev
, page
);
147 static void rx_complete(struct urb
*req
)
149 struct net_device
*dev
= req
->context
;
150 struct usbpn_dev
*pnd
= netdev_priv(dev
);
151 struct page
*page
= virt_to_page(req
->transfer_buffer
);
154 int status
= req
->status
;
158 spin_lock_irqsave(&pnd
->rx_lock
, flags
);
161 skb
= pnd
->rx_skb
= netdev_alloc_skb(dev
, 12);
163 /* Can't use pskb_pull() on page in IRQ */
164 memcpy(skb_put(skb
, 1), page_address(page
), 1);
165 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
166 page
, 1, req
->actual_length
);
170 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
171 page
, 0, req
->actual_length
);
174 if (req
->actual_length
< PAGE_SIZE
)
175 pnd
->rx_skb
= NULL
; /* Last fragment */
178 spin_unlock_irqrestore(&pnd
->rx_lock
, flags
);
180 skb
->protocol
= htons(ETH_P_PHONET
);
181 skb_reset_mac_header(skb
);
184 dev
->stats
.rx_packets
++;
185 dev
->stats
.rx_bytes
+= skb
->len
;
198 dev
->stats
.rx_over_errors
++;
199 dev_dbg(&dev
->dev
, "RX overflow\n");
203 dev
->stats
.rx_crc_errors
++;
207 dev
->stats
.rx_errors
++;
210 netdev_free_page(dev
, page
);
212 rx_submit(pnd
, req
, GFP_ATOMIC
);
215 static int usbpn_close(struct net_device
*dev
);
217 static int usbpn_open(struct net_device
*dev
)
219 struct usbpn_dev
*pnd
= netdev_priv(dev
);
222 unsigned num
= pnd
->data_intf
->cur_altsetting
->desc
.bInterfaceNumber
;
224 err
= usb_set_interface(pnd
->usb
, num
, pnd
->active_setting
);
228 for (i
= 0; i
< rxq_size
; i
++) {
229 struct urb
*req
= usb_alloc_urb(0, GFP_KERNEL
);
231 if (!req
|| rx_submit(pnd
, req
, GFP_KERNEL
)) {
238 netif_wake_queue(dev
);
242 static int usbpn_close(struct net_device
*dev
)
244 struct usbpn_dev
*pnd
= netdev_priv(dev
);
246 unsigned num
= pnd
->data_intf
->cur_altsetting
->desc
.bInterfaceNumber
;
248 netif_stop_queue(dev
);
250 for (i
= 0; i
< rxq_size
; i
++) {
251 struct urb
*req
= pnd
->urbs
[i
];
260 return usb_set_interface(pnd
->usb
, num
, !pnd
->active_setting
);
263 static int usbpn_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
265 struct if_phonet_req
*req
= (struct if_phonet_req
*)ifr
;
268 case SIOCPNGAUTOCONF
:
269 req
->ifr_phonet_autoconf
.device
= PN_DEV_PC
;
275 static int usbpn_set_mtu(struct net_device
*dev
, int new_mtu
)
277 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
284 static const struct net_device_ops usbpn_ops
= {
285 .ndo_open
= usbpn_open
,
286 .ndo_stop
= usbpn_close
,
287 .ndo_start_xmit
= usbpn_xmit
,
288 .ndo_do_ioctl
= usbpn_ioctl
,
289 .ndo_change_mtu
= usbpn_set_mtu
,
292 static void usbpn_setup(struct net_device
*dev
)
295 dev
->netdev_ops
= &usbpn_ops
,
296 dev
->header_ops
= &phonet_header_ops
;
297 dev
->type
= ARPHRD_PHONET
;
298 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
299 dev
->mtu
= PHONET_MAX_MTU
;
300 dev
->hard_header_len
= 1;
301 dev
->dev_addr
[0] = PN_MEDIA_USB
;
303 dev
->tx_queue_len
= 3;
305 dev
->destructor
= free_netdev
;
309 * USB driver callbacks
311 static struct usb_device_id usbpn_ids
[] = {
313 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
314 | USB_DEVICE_ID_MATCH_INT_CLASS
315 | USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
316 .idVendor
= 0x0421, /* Nokia */
317 .bInterfaceClass
= USB_CLASS_COMM
,
318 .bInterfaceSubClass
= 0xFE,
323 MODULE_DEVICE_TABLE(usb
, usbpn_ids
);
325 static struct usb_driver usbpn_driver
;
327 int usbpn_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
329 static const char ifname
[] = "usbpn%d";
330 const struct usb_cdc_union_desc
*union_header
= NULL
;
331 const struct usb_host_interface
*data_desc
;
332 struct usb_interface
*data_intf
;
333 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
334 struct net_device
*dev
;
335 struct usbpn_dev
*pnd
;
340 data
= intf
->altsetting
->extra
;
341 len
= intf
->altsetting
->extralen
;
347 /* bDescriptorType */
348 if (data
[1] == USB_DT_CS_INTERFACE
) {
349 /* bDescriptorSubType */
351 case USB_CDC_UNION_TYPE
:
352 if (union_header
|| dlen
< 5)
355 (struct usb_cdc_union_desc
*)data
;
366 if (!union_header
|| !phonet
)
369 data_intf
= usb_ifnum_to_if(usbdev
, union_header
->bSlaveInterface0
);
370 if (data_intf
== NULL
)
372 /* Data interface has one inactive and one active setting */
373 if (data_intf
->num_altsetting
!= 2)
375 if (data_intf
->altsetting
[0].desc
.bNumEndpoints
== 0 &&
376 data_intf
->altsetting
[1].desc
.bNumEndpoints
== 2)
377 data_desc
= data_intf
->altsetting
+ 1;
379 if (data_intf
->altsetting
[0].desc
.bNumEndpoints
== 2 &&
380 data_intf
->altsetting
[1].desc
.bNumEndpoints
== 0)
381 data_desc
= data_intf
->altsetting
;
385 dev
= alloc_netdev(sizeof(*pnd
) + sizeof(pnd
->urbs
[0]) * rxq_size
,
386 ifname
, usbpn_setup
);
390 pnd
= netdev_priv(dev
);
391 SET_NETDEV_DEV(dev
, &intf
->dev
);
394 pnd
->usb
= usb_get_dev(usbdev
);
396 pnd
->data_intf
= data_intf
;
397 spin_lock_init(&pnd
->tx_lock
);
398 spin_lock_init(&pnd
->rx_lock
);
400 if (usb_pipein(data_desc
->endpoint
[0].desc
.bEndpointAddress
)) {
401 pnd
->rx_pipe
= usb_rcvbulkpipe(usbdev
,
402 data_desc
->endpoint
[0].desc
.bEndpointAddress
);
403 pnd
->tx_pipe
= usb_sndbulkpipe(usbdev
,
404 data_desc
->endpoint
[1].desc
.bEndpointAddress
);
406 pnd
->rx_pipe
= usb_rcvbulkpipe(usbdev
,
407 data_desc
->endpoint
[1].desc
.bEndpointAddress
);
408 pnd
->tx_pipe
= usb_sndbulkpipe(usbdev
,
409 data_desc
->endpoint
[0].desc
.bEndpointAddress
);
411 pnd
->active_setting
= data_desc
- data_intf
->altsetting
;
413 err
= usb_driver_claim_interface(&usbpn_driver
, data_intf
, pnd
);
417 /* Force inactive mode until the network device is brought UP */
418 usb_set_interface(usbdev
, union_header
->bSlaveInterface0
,
419 !pnd
->active_setting
);
420 usb_set_intfdata(intf
, pnd
);
422 err
= register_netdev(dev
);
424 usb_driver_release_interface(&usbpn_driver
, data_intf
);
428 dev_dbg(&dev
->dev
, "USB CDC Phonet device found\n");
432 usb_set_intfdata(intf
, NULL
);
437 static void usbpn_disconnect(struct usb_interface
*intf
)
439 struct usbpn_dev
*pnd
= usb_get_intfdata(intf
);
440 struct usb_device
*usb
= pnd
->usb
;
442 if (pnd
->disconnected
)
445 pnd
->disconnected
= 1;
446 usb_driver_release_interface(&usbpn_driver
,
447 (pnd
->intf
== intf
) ? pnd
->data_intf
: pnd
->intf
);
448 unregister_netdev(pnd
->dev
);
452 static struct usb_driver usbpn_driver
= {
453 .name
= "cdc_phonet",
454 .probe
= usbpn_probe
,
455 .disconnect
= usbpn_disconnect
,
456 .id_table
= usbpn_ids
,
459 static int __init
usbpn_init(void)
461 return usb_register(&usbpn_driver
);
464 static void __exit
usbpn_exit(void)
466 usb_deregister(&usbpn_driver
);
469 module_init(usbpn_init
);
470 module_exit(usbpn_exit
);
472 MODULE_AUTHOR("Remi Denis-Courmont");
473 MODULE_DESCRIPTION("USB CDC Phonet host interface");
474 MODULE_LICENSE("GPL");