1 /* 2 * phonet.c -- USB CDC Phonet host driver 3 * 4 * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved. 5 * 6 * Author: Rémi Denis-Courmont 7 * 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. 11 * 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. 16 * 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 20 * 02110-1301 USA 21 */ 22 23 #include <linux/kernel.h> 24 #include <linux/mm.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> 33 34 #define PN_MEDIA_USB 0x1B 35 36 static const unsigned rxq_size = 17; 37 38 struct usbpn_dev { 39 struct net_device *dev; 40 41 struct usb_interface *intf, *data_intf; 42 struct usb_device *usb; 43 unsigned int tx_pipe, rx_pipe; 44 u8 active_setting; 45 u8 disconnected; 46 47 unsigned tx_queue; 48 spinlock_t tx_lock; 49 50 spinlock_t rx_lock; 51 struct sk_buff *rx_skb; 52 struct urb *urbs[0]; 53 }; 54 55 static void tx_complete(struct urb *req); 56 static void rx_complete(struct urb *req); 57 58 /* 59 * Network device callbacks 60 */ 61 static netdev_tx_t usbpn_xmit(struct sk_buff *skb, struct net_device *dev) 62 { 63 struct usbpn_dev *pnd = netdev_priv(dev); 64 struct urb *req = NULL; 65 unsigned long flags; 66 int err; 67 68 if (skb->protocol != htons(ETH_P_PHONET)) 69 goto drop; 70 71 req = usb_alloc_urb(0, GFP_ATOMIC); 72 if (!req) 73 goto drop; 74 usb_fill_bulk_urb(req, pnd->usb, pnd->tx_pipe, skb->data, skb->len, 75 tx_complete, skb); 76 req->transfer_flags = URB_ZERO_PACKET; 77 err = usb_submit_urb(req, GFP_ATOMIC); 78 if (err) { 79 usb_free_urb(req); 80 goto drop; 81 } 82 83 spin_lock_irqsave(&pnd->tx_lock, flags); 84 pnd->tx_queue++; 85 if (pnd->tx_queue >= dev->tx_queue_len) 86 netif_stop_queue(dev); 87 spin_unlock_irqrestore(&pnd->tx_lock, flags); 88 return NETDEV_TX_OK; 89 90 drop: 91 dev_kfree_skb(skb); 92 dev->stats.tx_dropped++; 93 return NETDEV_TX_OK; 94 } 95 96 static void tx_complete(struct urb *req) 97 { 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; 102 103 switch (status) { 104 case 0: 105 dev->stats.tx_bytes += skb->len; 106 break; 107 108 case -ENOENT: 109 case -ECONNRESET: 110 case -ESHUTDOWN: 111 dev->stats.tx_aborted_errors++; 112 default: 113 dev->stats.tx_errors++; 114 dev_dbg(&dev->dev, "TX error (%d)\n", status); 115 } 116 dev->stats.tx_packets++; 117 118 spin_lock(&pnd->tx_lock); 119 pnd->tx_queue--; 120 netif_wake_queue(dev); 121 spin_unlock(&pnd->tx_lock); 122 123 dev_kfree_skb_any(skb); 124 usb_free_urb(req); 125 } 126 127 static int rx_submit(struct usbpn_dev *pnd, struct urb *req, gfp_t gfp_flags) 128 { 129 struct net_device *dev = pnd->dev; 130 struct page *page; 131 int err; 132 133 page = __dev_alloc_page(gfp_flags | __GFP_NOMEMALLOC); 134 if (!page) 135 return -ENOMEM; 136 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); 141 if (unlikely(err)) { 142 dev_dbg(&dev->dev, "RX submit error (%d)\n", err); 143 put_page(page); 144 } 145 return err; 146 } 147 148 static void rx_complete(struct urb *req) 149 { 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); 153 struct sk_buff *skb; 154 unsigned long flags; 155 int status = req->status; 156 157 switch (status) { 158 case 0: 159 spin_lock_irqsave(&pnd->rx_lock, flags); 160 skb = pnd->rx_skb; 161 if (!skb) { 162 skb = pnd->rx_skb = netdev_alloc_skb(dev, 12); 163 if (likely(skb)) { 164 /* Can't use pskb_pull() on page in IRQ */ 165 skb_put_data(skb, page_address(page), 1); 166 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 167 page, 1, req->actual_length, 168 PAGE_SIZE); 169 page = NULL; 170 } 171 } else { 172 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 173 page, 0, req->actual_length, 174 PAGE_SIZE); 175 page = NULL; 176 } 177 if (req->actual_length < PAGE_SIZE) 178 pnd->rx_skb = NULL; /* Last fragment */ 179 else 180 skb = NULL; 181 spin_unlock_irqrestore(&pnd->rx_lock, flags); 182 if (skb) { 183 skb->protocol = htons(ETH_P_PHONET); 184 skb_reset_mac_header(skb); 185 __skb_pull(skb, 1); 186 skb->dev = dev; 187 dev->stats.rx_packets++; 188 dev->stats.rx_bytes += skb->len; 189 190 netif_rx(skb); 191 } 192 goto resubmit; 193 194 case -ENOENT: 195 case -ECONNRESET: 196 case -ESHUTDOWN: 197 req = NULL; 198 break; 199 200 case -EOVERFLOW: 201 dev->stats.rx_over_errors++; 202 dev_dbg(&dev->dev, "RX overflow\n"); 203 break; 204 205 case -EILSEQ: 206 dev->stats.rx_crc_errors++; 207 break; 208 } 209 210 dev->stats.rx_errors++; 211 resubmit: 212 if (page) 213 put_page(page); 214 if (req) 215 rx_submit(pnd, req, GFP_ATOMIC); 216 } 217 218 static int usbpn_close(struct net_device *dev); 219 220 static int usbpn_open(struct net_device *dev) 221 { 222 struct usbpn_dev *pnd = netdev_priv(dev); 223 int err; 224 unsigned i; 225 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber; 226 227 err = usb_set_interface(pnd->usb, num, pnd->active_setting); 228 if (err) 229 return err; 230 231 for (i = 0; i < rxq_size; i++) { 232 struct urb *req = usb_alloc_urb(0, GFP_KERNEL); 233 234 if (!req || rx_submit(pnd, req, GFP_KERNEL)) { 235 usb_free_urb(req); 236 usbpn_close(dev); 237 return -ENOMEM; 238 } 239 pnd->urbs[i] = req; 240 } 241 242 netif_wake_queue(dev); 243 return 0; 244 } 245 246 static int usbpn_close(struct net_device *dev) 247 { 248 struct usbpn_dev *pnd = netdev_priv(dev); 249 unsigned i; 250 unsigned num = pnd->data_intf->cur_altsetting->desc.bInterfaceNumber; 251 252 netif_stop_queue(dev); 253 254 for (i = 0; i < rxq_size; i++) { 255 struct urb *req = pnd->urbs[i]; 256 257 if (!req) 258 continue; 259 usb_kill_urb(req); 260 usb_free_urb(req); 261 pnd->urbs[i] = NULL; 262 } 263 264 return usb_set_interface(pnd->usb, num, !pnd->active_setting); 265 } 266 267 static int usbpn_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 268 { 269 struct if_phonet_req *req = (struct if_phonet_req *)ifr; 270 271 switch (cmd) { 272 case SIOCPNGAUTOCONF: 273 req->ifr_phonet_autoconf.device = PN_DEV_PC; 274 return 0; 275 } 276 return -ENOIOCTLCMD; 277 } 278 279 static const struct net_device_ops usbpn_ops = { 280 .ndo_open = usbpn_open, 281 .ndo_stop = usbpn_close, 282 .ndo_start_xmit = usbpn_xmit, 283 .ndo_do_ioctl = usbpn_ioctl, 284 }; 285 286 static void usbpn_setup(struct net_device *dev) 287 { 288 dev->features = 0; 289 dev->netdev_ops = &usbpn_ops, 290 dev->header_ops = &phonet_header_ops; 291 dev->type = ARPHRD_PHONET; 292 dev->flags = IFF_POINTOPOINT | IFF_NOARP; 293 dev->mtu = PHONET_MAX_MTU; 294 dev->min_mtu = PHONET_MIN_MTU; 295 dev->max_mtu = PHONET_MAX_MTU; 296 dev->hard_header_len = 1; 297 dev->dev_addr[0] = PN_MEDIA_USB; 298 dev->addr_len = 1; 299 dev->tx_queue_len = 3; 300 301 dev->needs_free_netdev = true; 302 } 303 304 /* 305 * USB driver callbacks 306 */ 307 static struct usb_device_id usbpn_ids[] = { 308 { 309 .match_flags = USB_DEVICE_ID_MATCH_VENDOR 310 | USB_DEVICE_ID_MATCH_INT_CLASS 311 | USB_DEVICE_ID_MATCH_INT_SUBCLASS, 312 .idVendor = 0x0421, /* Nokia */ 313 .bInterfaceClass = USB_CLASS_COMM, 314 .bInterfaceSubClass = 0xFE, 315 }, 316 { }, 317 }; 318 319 MODULE_DEVICE_TABLE(usb, usbpn_ids); 320 321 static struct usb_driver usbpn_driver; 322 323 static int usbpn_probe(struct usb_interface *intf, const struct usb_device_id *id) 324 { 325 static const char ifname[] = "usbpn%d"; 326 const struct usb_cdc_union_desc *union_header = NULL; 327 const struct usb_host_interface *data_desc; 328 struct usb_interface *data_intf; 329 struct usb_device *usbdev = interface_to_usbdev(intf); 330 struct net_device *dev; 331 struct usbpn_dev *pnd; 332 u8 *data; 333 int phonet = 0; 334 int len, err; 335 struct usb_cdc_parsed_header hdr; 336 337 data = intf->altsetting->extra; 338 len = intf->altsetting->extralen; 339 cdc_parse_cdc_header(&hdr, intf, data, len); 340 union_header = hdr.usb_cdc_union_desc; 341 phonet = hdr.phonet_magic_present; 342 343 if (!union_header || !phonet) 344 return -EINVAL; 345 346 data_intf = usb_ifnum_to_if(usbdev, union_header->bSlaveInterface0); 347 if (data_intf == NULL) 348 return -ENODEV; 349 /* Data interface has one inactive and one active setting */ 350 if (data_intf->num_altsetting != 2) 351 return -EINVAL; 352 if (data_intf->altsetting[0].desc.bNumEndpoints == 0 && 353 data_intf->altsetting[1].desc.bNumEndpoints == 2) 354 data_desc = data_intf->altsetting + 1; 355 else 356 if (data_intf->altsetting[0].desc.bNumEndpoints == 2 && 357 data_intf->altsetting[1].desc.bNumEndpoints == 0) 358 data_desc = data_intf->altsetting; 359 else 360 return -EINVAL; 361 362 dev = alloc_netdev(sizeof(*pnd) + sizeof(pnd->urbs[0]) * rxq_size, 363 ifname, NET_NAME_UNKNOWN, usbpn_setup); 364 if (!dev) 365 return -ENOMEM; 366 367 pnd = netdev_priv(dev); 368 SET_NETDEV_DEV(dev, &intf->dev); 369 370 pnd->dev = dev; 371 pnd->usb = usbdev; 372 pnd->intf = intf; 373 pnd->data_intf = data_intf; 374 spin_lock_init(&pnd->tx_lock); 375 spin_lock_init(&pnd->rx_lock); 376 /* Endpoints */ 377 if (usb_pipein(data_desc->endpoint[0].desc.bEndpointAddress)) { 378 pnd->rx_pipe = usb_rcvbulkpipe(usbdev, 379 data_desc->endpoint[0].desc.bEndpointAddress); 380 pnd->tx_pipe = usb_sndbulkpipe(usbdev, 381 data_desc->endpoint[1].desc.bEndpointAddress); 382 } else { 383 pnd->rx_pipe = usb_rcvbulkpipe(usbdev, 384 data_desc->endpoint[1].desc.bEndpointAddress); 385 pnd->tx_pipe = usb_sndbulkpipe(usbdev, 386 data_desc->endpoint[0].desc.bEndpointAddress); 387 } 388 pnd->active_setting = data_desc - data_intf->altsetting; 389 390 err = usb_driver_claim_interface(&usbpn_driver, data_intf, pnd); 391 if (err) 392 goto out; 393 394 /* Force inactive mode until the network device is brought UP */ 395 usb_set_interface(usbdev, union_header->bSlaveInterface0, 396 !pnd->active_setting); 397 usb_set_intfdata(intf, pnd); 398 399 err = register_netdev(dev); 400 if (err) { 401 usb_driver_release_interface(&usbpn_driver, data_intf); 402 goto out; 403 } 404 405 dev_dbg(&dev->dev, "USB CDC Phonet device found\n"); 406 return 0; 407 408 out: 409 usb_set_intfdata(intf, NULL); 410 free_netdev(dev); 411 return err; 412 } 413 414 static void usbpn_disconnect(struct usb_interface *intf) 415 { 416 struct usbpn_dev *pnd = usb_get_intfdata(intf); 417 418 if (pnd->disconnected) 419 return; 420 421 pnd->disconnected = 1; 422 usb_driver_release_interface(&usbpn_driver, 423 (pnd->intf == intf) ? pnd->data_intf : pnd->intf); 424 unregister_netdev(pnd->dev); 425 } 426 427 static struct usb_driver usbpn_driver = { 428 .name = "cdc_phonet", 429 .probe = usbpn_probe, 430 .disconnect = usbpn_disconnect, 431 .id_table = usbpn_ids, 432 .disable_hub_initiated_lpm = 1, 433 }; 434 435 module_usb_driver(usbpn_driver); 436 437 MODULE_AUTHOR("Remi Denis-Courmont"); 438 MODULE_DESCRIPTION("USB CDC Phonet host interface"); 439 MODULE_LICENSE("GPL"); 440