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/usb.h>
26 #include <linux/usb/cdc.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_phonet.h>
30 #include <linux/phonet.h>
32 #define PN_MEDIA_USB 0x1B
34 static const unsigned rxq_size
= 17;
37 struct net_device
*dev
;
39 struct usb_interface
*intf
, *data_intf
;
40 struct usb_device
*usb
;
41 unsigned int tx_pipe
, rx_pipe
;
49 struct sk_buff
*rx_skb
;
53 static void tx_complete(struct urb
*req
);
54 static void rx_complete(struct urb
*req
);
57 * Network device callbacks
59 static netdev_tx_t
usbpn_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
61 struct usbpn_dev
*pnd
= netdev_priv(dev
);
62 struct urb
*req
= NULL
;
66 if (skb
->protocol
!= htons(ETH_P_PHONET
))
69 req
= usb_alloc_urb(0, GFP_ATOMIC
);
72 usb_fill_bulk_urb(req
, pnd
->usb
, pnd
->tx_pipe
, skb
->data
, skb
->len
,
74 req
->transfer_flags
= URB_ZERO_PACKET
;
75 err
= usb_submit_urb(req
, GFP_ATOMIC
);
81 spin_lock_irqsave(&pnd
->tx_lock
, flags
);
83 if (pnd
->tx_queue
>= dev
->tx_queue_len
)
84 netif_stop_queue(dev
);
85 spin_unlock_irqrestore(&pnd
->tx_lock
, flags
);
90 dev
->stats
.tx_dropped
++;
94 static void tx_complete(struct urb
*req
)
96 struct sk_buff
*skb
= req
->context
;
97 struct net_device
*dev
= skb
->dev
;
98 struct usbpn_dev
*pnd
= netdev_priv(dev
);
100 switch (req
->status
) {
102 dev
->stats
.tx_bytes
+= skb
->len
;
108 dev
->stats
.tx_aborted_errors
++;
110 dev
->stats
.tx_errors
++;
111 dev_dbg(&dev
->dev
, "TX error (%d)\n", req
->status
);
113 dev
->stats
.tx_packets
++;
115 spin_lock(&pnd
->tx_lock
);
117 netif_wake_queue(dev
);
118 spin_unlock(&pnd
->tx_lock
);
120 dev_kfree_skb_any(skb
);
124 static int rx_submit(struct usbpn_dev
*pnd
, struct urb
*req
, gfp_t gfp_flags
)
126 struct net_device
*dev
= pnd
->dev
;
130 page
= __netdev_alloc_page(dev
, gfp_flags
);
134 usb_fill_bulk_urb(req
, pnd
->usb
, pnd
->rx_pipe
, page_address(page
),
135 PAGE_SIZE
, rx_complete
, dev
);
136 req
->transfer_flags
= 0;
137 err
= usb_submit_urb(req
, gfp_flags
);
139 dev_dbg(&dev
->dev
, "RX submit error (%d)\n", err
);
140 netdev_free_page(dev
, page
);
145 static void rx_complete(struct urb
*req
)
147 struct net_device
*dev
= req
->context
;
148 struct usbpn_dev
*pnd
= netdev_priv(dev
);
149 struct page
*page
= virt_to_page(req
->transfer_buffer
);
153 switch (req
->status
) {
155 spin_lock_irqsave(&pnd
->rx_lock
, flags
);
158 skb
= pnd
->rx_skb
= netdev_alloc_skb(dev
, 12);
160 /* Can't use pskb_pull() on page in IRQ */
161 memcpy(skb_put(skb
, 1), page_address(page
), 1);
162 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
163 page
, 1, req
->actual_length
);
167 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
168 page
, 0, req
->actual_length
);
171 if (req
->actual_length
< PAGE_SIZE
)
172 pnd
->rx_skb
= NULL
; /* Last fragment */
175 spin_unlock_irqrestore(&pnd
->rx_lock
, flags
);
177 skb
->protocol
= htons(ETH_P_PHONET
);
178 skb_reset_mac_header(skb
);
181 dev
->stats
.rx_packets
++;
182 dev
->stats
.rx_bytes
+= skb
->len
;
195 dev
->stats
.rx_over_errors
++;
196 dev_dbg(&dev
->dev
, "RX overflow\n");
200 dev
->stats
.rx_crc_errors
++;
204 dev
->stats
.rx_errors
++;
207 netdev_free_page(dev
, page
);
209 rx_submit(pnd
, req
, GFP_ATOMIC
);
212 static int usbpn_close(struct net_device
*dev
);
214 static int usbpn_open(struct net_device
*dev
)
216 struct usbpn_dev
*pnd
= netdev_priv(dev
);
219 unsigned num
= pnd
->data_intf
->cur_altsetting
->desc
.bInterfaceNumber
;
221 err
= usb_set_interface(pnd
->usb
, num
, pnd
->active_setting
);
225 for (i
= 0; i
< rxq_size
; i
++) {
226 struct urb
*req
= usb_alloc_urb(0, GFP_KERNEL
);
228 if (!req
|| rx_submit(pnd
, req
, GFP_KERNEL
)) {
235 netif_wake_queue(dev
);
239 static int usbpn_close(struct net_device
*dev
)
241 struct usbpn_dev
*pnd
= netdev_priv(dev
);
243 unsigned num
= pnd
->data_intf
->cur_altsetting
->desc
.bInterfaceNumber
;
245 netif_stop_queue(dev
);
247 for (i
= 0; i
< rxq_size
; i
++) {
248 struct urb
*req
= pnd
->urbs
[i
];
257 return usb_set_interface(pnd
->usb
, num
, !pnd
->active_setting
);
260 static int usbpn_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
262 struct if_phonet_req
*req
= (struct if_phonet_req
*)ifr
;
265 case SIOCPNGAUTOCONF
:
266 req
->ifr_phonet_autoconf
.device
= PN_DEV_PC
;
272 static int usbpn_set_mtu(struct net_device
*dev
, int new_mtu
)
274 if ((new_mtu
< PHONET_MIN_MTU
) || (new_mtu
> PHONET_MAX_MTU
))
281 static const struct net_device_ops usbpn_ops
= {
282 .ndo_open
= usbpn_open
,
283 .ndo_stop
= usbpn_close
,
284 .ndo_start_xmit
= usbpn_xmit
,
285 .ndo_do_ioctl
= usbpn_ioctl
,
286 .ndo_change_mtu
= usbpn_set_mtu
,
289 static void usbpn_setup(struct net_device
*dev
)
292 dev
->netdev_ops
= &usbpn_ops
,
293 dev
->header_ops
= &phonet_header_ops
;
294 dev
->type
= ARPHRD_PHONET
;
295 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
296 dev
->mtu
= PHONET_MAX_MTU
;
297 dev
->hard_header_len
= 1;
298 dev
->dev_addr
[0] = PN_MEDIA_USB
;
300 dev
->tx_queue_len
= 3;
302 dev
->destructor
= free_netdev
;
306 * USB driver callbacks
308 static struct usb_device_id usbpn_ids
[] = {
310 .match_flags
= USB_DEVICE_ID_MATCH_VENDOR
311 | USB_DEVICE_ID_MATCH_INT_CLASS
312 | USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
313 .idVendor
= 0x0421, /* Nokia */
314 .bInterfaceClass
= USB_CLASS_COMM
,
315 .bInterfaceSubClass
= 0xFE,
320 MODULE_DEVICE_TABLE(usb
, usbpn_ids
);
322 static struct usb_driver usbpn_driver
;
324 int usbpn_probe(struct usb_interface
*intf
, const struct usb_device_id
*id
)
326 static const char ifname
[] = "usbpn%d";
327 const struct usb_cdc_union_desc
*union_header
= NULL
;
328 const struct usb_cdc_header_desc
*phonet_header
= NULL
;
329 const struct usb_host_interface
*data_desc
;
330 struct usb_interface
*data_intf
;
331 struct usb_device
*usbdev
= interface_to_usbdev(intf
);
332 struct net_device
*dev
;
333 struct usbpn_dev
*pnd
;
337 data
= intf
->altsetting
->extra
;
338 len
= intf
->altsetting
->extralen
;
344 /* bDescriptorType */
345 if (data
[1] == USB_DT_CS_INTERFACE
) {
346 /* bDescriptorSubType */
348 case USB_CDC_UNION_TYPE
:
349 if (union_header
|| dlen
< 5)
352 (struct usb_cdc_union_desc
*)data
;
355 if (phonet_header
|| dlen
< 5)
358 (struct usb_cdc_header_desc
*)data
;
366 if (!union_header
|| !phonet_header
)
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
);
392 netif_stop_queue(dev
);
395 pnd
->usb
= usb_get_dev(usbdev
);
397 pnd
->data_intf
= data_intf
;
398 spin_lock_init(&pnd
->tx_lock
);
399 spin_lock_init(&pnd
->rx_lock
);
401 if (usb_pipein(data_desc
->endpoint
[0].desc
.bEndpointAddress
)) {
402 pnd
->rx_pipe
= usb_rcvbulkpipe(usbdev
,
403 data_desc
->endpoint
[0].desc
.bEndpointAddress
);
404 pnd
->tx_pipe
= usb_sndbulkpipe(usbdev
,
405 data_desc
->endpoint
[1].desc
.bEndpointAddress
);
407 pnd
->rx_pipe
= usb_rcvbulkpipe(usbdev
,
408 data_desc
->endpoint
[1].desc
.bEndpointAddress
);
409 pnd
->tx_pipe
= usb_sndbulkpipe(usbdev
,
410 data_desc
->endpoint
[0].desc
.bEndpointAddress
);
412 pnd
->active_setting
= data_desc
- data_intf
->altsetting
;
414 err
= usb_driver_claim_interface(&usbpn_driver
, data_intf
, pnd
);
418 /* Force inactive mode until the network device is brought UP */
419 usb_set_interface(usbdev
, union_header
->bSlaveInterface0
,
420 !pnd
->active_setting
);
421 usb_set_intfdata(intf
, pnd
);
423 err
= register_netdev(dev
);
425 usb_driver_release_interface(&usbpn_driver
, data_intf
);
429 dev_dbg(&dev
->dev
, "USB CDC Phonet device found\n");
433 usb_set_intfdata(intf
, NULL
);
438 static void usbpn_disconnect(struct usb_interface
*intf
)
440 struct usbpn_dev
*pnd
= usb_get_intfdata(intf
);
441 struct usb_device
*usb
= pnd
->usb
;
443 if (pnd
->disconnected
)
446 pnd
->disconnected
= 1;
447 usb_driver_release_interface(&usbpn_driver
,
448 (pnd
->intf
== intf
) ? pnd
->data_intf
: pnd
->intf
);
449 unregister_netdev(pnd
->dev
);
453 static struct usb_driver usbpn_driver
= {
454 .name
= "cdc_phonet",
455 .probe
= usbpn_probe
,
456 .disconnect
= usbpn_disconnect
,
457 .id_table
= usbpn_ids
,
460 static int __init
usbpn_init(void)
462 return usb_register(&usbpn_driver
);
465 static void __exit
usbpn_exit(void)
467 usb_deregister(&usbpn_driver
);
470 module_init(usbpn_init
);
471 module_exit(usbpn_exit
);
473 MODULE_AUTHOR("Remi Denis-Courmont");
474 MODULE_DESCRIPTION("USB CDC Phonet host interface");
475 MODULE_LICENSE("GPL");