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>
25 #include <linux/module.h>
26 #include <linux/gfp.h>
27 #include <linux/usb.h>
28 #include <linux/usb/cdc.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_phonet.h>
32 #include <linux/phonet.h>
34 #define PN_MEDIA_USB 0x1B
36 static const unsigned rxq_size = 17;
39 struct net_device *dev;
41 struct usb_interface *intf, *data_intf;
42 struct usb_device *usb;
43 unsigned int tx_pipe, rx_pipe;
51 struct sk_buff *rx_skb;
55 static void tx_complete(struct urb *req);
56 static void rx_complete(struct urb *req);
59 * Network device callbacks
61 static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev)
63 struct usbpn_dev *pnd = netdev_priv(dev);
64 struct urb *req = NULL;
68 if (skb->protocol != htons(ETH_P_PHONET))
71 req = usb_alloc_urb(0, GFP_ATOMIC);
74 usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len,
76 req->transfer_flags = URB_ZERO_PACKET;
77 err = usb_submit_urb(req, GFP_ATOMIC);
83 spin_lock_irqsave(&pnd->tx_lock, flags);
85 if (pnd->tx_queue >= dev->tx_queue_len)
86 netif_stop_queue(dev);
87 spin_unlock_irqrestore(&pnd->tx_lock, flags);
92 dev->stats.tx_dropped++;
96 static void tx_complete(struct urb *req)
98 struct sk_buff *skb = req->context;
99 struct net_device *dev = skb->dev;
100 struct usbpn_dev *pnd = netdev_priv(dev);
101 int status = req->status;
105 dev->stats.tx_bytes += skb->len;
111 dev->stats.tx_aborted_errors++;
113 dev->stats.tx_errors++;
114 dev_dbg(&dev->dev, "TX error (%d)\n", status);
116 dev->stats.tx_packets++;
118 spin_lock(&pnd->tx_lock);
120 netif_wake_queue(dev);
121 spin_unlock(&pnd->tx_lock);
123 dev_kfree_skb_any(skb);
127 static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags)
129 struct net_device *dev = pnd->dev;
133 page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC);
137 usb_fill_bulk_urb(req, pnd->usb, pnd->rx_pipe, page_address(page),
138 PAGE_SIZE, rx_complete, dev);
139 req->transfer_flags = 0;
140 err = usb_submit_urb(req, gfp_flags);
142 dev_dbg(&dev->dev, "RX submit error (%d)\n", err);
148 static void rx_complete(struct urb *req)
150 struct net_device *dev = req->context;
151 struct usbpn_dev *pnd = netdev_priv(dev);
152 struct page *page = virt_to_page(req->transfer_buffer);
155 int status = req->status;
159 spin_lock_irqsave(&pnd->rx_lock, flags);
162 skb = pnd->rx_skb = netdev_alloc_skb(dev, 12);
164 /* Can't use pskb_pull() on page in IRQ */
165 memcpy(skb_put(skb, 1), page_address(page), 1);
166 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
167 page, 1, req->actual_length,
172 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
173 page, 0, req->actual_length,
177 if (req->actual_length < PAGE_SIZE)
178 pnd->rx_skb = NULL; /* Last fragment */
181 spin_unlock_irqrestore(&pnd->rx_lock, flags);
183 skb->protocol = htons(ETH_P_PHONET);
184 skb_reset_mac_header(skb);
187 dev->stats.rx_packets++;
188 dev->stats.rx_bytes += skb->len;
201 dev->stats.rx_over_errors++;
202 dev_dbg(&dev->dev, "RX overflow\n");
206 dev->stats.rx_crc_errors++;
210 dev->stats.rx_errors++;
215 rx_submit(pnd, req, GFP_ATOMIC);
218 static int usbpn_close(struct net_device *dev);
220 static int usbpn_open(struct net_device *dev)
222 struct usbpn_dev *pnd = netdev_priv(dev);
225 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
227 err = usb_set_interface(pnd->usb, num, pnd->active_setting);
231 for (i = 0; i < rxq_size; i++) {
232 struct urb *req = usb_alloc_urb(0, GFP_KERNEL);
234 if (!req || rx_submit(pnd, req, GFP_KERNEL)) {
242 netif_wake_queue(dev);
246 static int usbpn_close(struct net_device *dev)
248 struct usbpn_dev *pnd = netdev_priv(dev);
250 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber;
252 netif_stop_queue(dev);
254 for (i = 0; i < rxq_size; i++) {
255 struct urb *req = pnd->urbs[i];
264 return usb_set_interface(pnd->usb, num, !pnd->active_setting);
267 static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
269 struct if_phonet_req *req = (struct if_phonet_req *)ifr;
272 case SIOCPNGAUTOCONF:
273 req->ifr_phonet_autoconf.device = PN_DEV_PC;
279 static int usbpn_set_mtu(struct net_device *dev, int new_mtu)
281 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU))
288 static const struct net_device_ops usbpn_ops = {
289 .ndo_open = usbpn_open,
290 .ndo_stop = usbpn_close,
291 .ndo_start_xmit = usbpn_xmit,
292 .ndo_do_ioctl = usbpn_ioctl,
293 .ndo_change_mtu = usbpn_set_mtu,
296 static void usbpn_setup(struct net_device *dev)
299 dev->netdev_ops = &usbpn_ops,
300 dev->header_ops = &phonet_header_ops;
301 dev->type = ARPHRD_PHONET;
302 dev->flags = IFF_POINTOPOINT | IFF_NOARP;
303 dev->mtu = PHONET_MAX_MTU;
304 dev->hard_header_len = 1;
305 dev->dev_addr[0] = PN_MEDIA_USB;
307 dev->tx_queue_len = 3;
309 dev->destructor = free_netdev;
313 * USB driver callbacks
315 static struct usb_device_id usbpn_ids[] = {
317 .match_flags = USB_DEVICE_ID_MATCH_VENDOR
318 | USB_DEVICE_ID_MATCH_INT_CLASS
319 | USB_DEVICE_ID_MATCH_INT_SUBCLASS,
320 .idVendor = 0x0421, /* Nokia */
321 .bInterfaceClass = USB_CLASS_COMM,
322 .bInterfaceSubClass = 0xFE,
327 MODULE_DEVICE_TABLE(usb, usbpn_ids);
329 static struct usb_driver usbpn_driver;
331 static int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id)
333 static const char ifname[] = "usbpn%d";
334 const struct usb_cdc_union_desc *union_header = NULL;
335 const struct usb_host_interface *data_desc;
336 struct usb_interface *data_intf;
337 struct usb_device *usbdev = interface_to_usbdev(intf);
338 struct net_device *dev;
339 struct usbpn_dev *pnd;
343 struct usb_cdc_parsed_header hdr;
345 data = intf->altsetting->extra;
346 len = intf->altsetting->extralen;
347 cdc_parse_cdc_header(&hdr, intf, data, len);
348 union_header = hdr.usb_cdc_union_desc;
349 phonet = hdr.phonet_magic_present;
351 if (!union_header || !phonet)
354 data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0);
355 if (data_intf == NULL)
357 /* Data interface has one inactive and one active setting */
358 if (data_intf->num_altsetting != 2)
360 if (data_intf->altsetting[0].desc.bNumEndpoints == 0 &&
361 data_intf->altsetting[1].desc.bNumEndpoints == 2)
362 data_desc = data_intf->altsetting + 1;
364 if (data_intf->altsetting[0].desc.bNumEndpoints == 2 &&
365 data_intf->altsetting[1].desc.bNumEndpoints == 0)
366 data_desc = data_intf->altsetting;
370 dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size,
371 ifname, NET_NAME_UNKNOWN, usbpn_setup);
375 pnd = netdev_priv(dev);
376 SET_NETDEV_DEV(dev, &intf->dev);
381 pnd->data_intf = data_intf;
382 spin_lock_init(&pnd->tx_lock);
383 spin_lock_init(&pnd->rx_lock);
385 if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) {
386 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
387 data_desc->endpoint[0].desc.bEndpointAddress);
388 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
389 data_desc->endpoint[1].desc.bEndpointAddress);
391 pnd->rx_pipe = usb_rcvbulkpipe(usbdev,
392 data_desc->endpoint[1].desc.bEndpointAddress);
393 pnd->tx_pipe = usb_sndbulkpipe(usbdev,
394 data_desc->endpoint[0].desc.bEndpointAddress);
396 pnd->active_setting = data_desc - data_intf->altsetting;
398 err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd);
402 /* Force inactive mode until the network device is brought UP */
403 usb_set_interface(usbdev, union_header->bSlaveInterface0,
404 !pnd->active_setting);
405 usb_set_intfdata(intf, pnd);
407 err = register_netdev(dev);
409 /* Set disconnected flag so that disconnect() returns early. */
410 pnd->disconnected = 1;
411 usb_driver_release_interface(&usbpn_driver, data_intf);
415 dev_dbg(&dev->dev, "USB CDC Phonet device found\n");
419 usb_set_intfdata(intf, NULL);
424 static void usbpn_disconnect(struct usb_interface *intf)
426 struct usbpn_dev *pnd = usb_get_intfdata(intf);
428 if (pnd->disconnected)
431 pnd->disconnected = 1;
432 usb_driver_release_interface(&usbpn_driver,
433 (pnd->intf == intf) ? pnd->data_intf : pnd->intf);
434 unregister_netdev(pnd->dev);
437 static struct usb_driver usbpn_driver = {
438 .name = "cdc_phonet",
439 .probe = usbpn_probe,
440 .disconnect = usbpn_disconnect,
441 .id_table = usbpn_ids,
442 .disable_hub_initiated_lpm = 1,
445 module_usb_driver(usbpn_driver);
447 MODULE_AUTHOR("Remi Denis-Courmont");
448 MODULE_DESCRIPTION("USB CDC Phonet host interface");
449 MODULE_LICENSE("GPL");