Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.1-rc3 639 lines 15 kB view raw
1/* 2 * f_phonet.c -- USB CDC Phonet function 3 * 4 * Copyright (C) 2007-2008 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/mm.h> 24#include <linux/slab.h> 25#include <linux/kernel.h> 26#include <linux/device.h> 27 28#include <linux/netdevice.h> 29#include <linux/if_ether.h> 30#include <linux/if_phonet.h> 31#include <linux/if_arp.h> 32 33#include <linux/usb/ch9.h> 34#include <linux/usb/cdc.h> 35#include <linux/usb/composite.h> 36 37#include "u_phonet.h" 38 39#define PN_MEDIA_USB 0x1B 40#define MAXPACKET 512 41#if (PAGE_SIZE % MAXPACKET) 42#error MAXPACKET must divide PAGE_SIZE! 43#endif 44 45/*-------------------------------------------------------------------------*/ 46 47struct phonet_port { 48 struct f_phonet *usb; 49 spinlock_t lock; 50}; 51 52struct f_phonet { 53 struct usb_function function; 54 struct { 55 struct sk_buff *skb; 56 spinlock_t lock; 57 } rx; 58 struct net_device *dev; 59 struct usb_ep *in_ep, *out_ep; 60 61 struct usb_request *in_req; 62 struct usb_request *out_reqv[0]; 63}; 64 65static int phonet_rxq_size = 17; 66 67static inline struct f_phonet *func_to_pn(struct usb_function *f) 68{ 69 return container_of(f, struct f_phonet, function); 70} 71 72/*-------------------------------------------------------------------------*/ 73 74#define USB_CDC_SUBCLASS_PHONET 0xfe 75#define USB_CDC_PHONET_TYPE 0xab 76 77static struct usb_interface_descriptor 78pn_control_intf_desc = { 79 .bLength = sizeof pn_control_intf_desc, 80 .bDescriptorType = USB_DT_INTERFACE, 81 82 /* .bInterfaceNumber = DYNAMIC, */ 83 .bInterfaceClass = USB_CLASS_COMM, 84 .bInterfaceSubClass = USB_CDC_SUBCLASS_PHONET, 85}; 86 87static const struct usb_cdc_header_desc 88pn_header_desc = { 89 .bLength = sizeof pn_header_desc, 90 .bDescriptorType = USB_DT_CS_INTERFACE, 91 .bDescriptorSubType = USB_CDC_HEADER_TYPE, 92 .bcdCDC = cpu_to_le16(0x0110), 93}; 94 95static const struct usb_cdc_header_desc 96pn_phonet_desc = { 97 .bLength = sizeof pn_phonet_desc, 98 .bDescriptorType = USB_DT_CS_INTERFACE, 99 .bDescriptorSubType = USB_CDC_PHONET_TYPE, 100 .bcdCDC = cpu_to_le16(0x1505), /* ??? */ 101}; 102 103static struct usb_cdc_union_desc 104pn_union_desc = { 105 .bLength = sizeof pn_union_desc, 106 .bDescriptorType = USB_DT_CS_INTERFACE, 107 .bDescriptorSubType = USB_CDC_UNION_TYPE, 108 109 /* .bMasterInterface0 = DYNAMIC, */ 110 /* .bSlaveInterface0 = DYNAMIC, */ 111}; 112 113static struct usb_interface_descriptor 114pn_data_nop_intf_desc = { 115 .bLength = sizeof pn_data_nop_intf_desc, 116 .bDescriptorType = USB_DT_INTERFACE, 117 118 /* .bInterfaceNumber = DYNAMIC, */ 119 .bAlternateSetting = 0, 120 .bNumEndpoints = 0, 121 .bInterfaceClass = USB_CLASS_CDC_DATA, 122}; 123 124static struct usb_interface_descriptor 125pn_data_intf_desc = { 126 .bLength = sizeof pn_data_intf_desc, 127 .bDescriptorType = USB_DT_INTERFACE, 128 129 /* .bInterfaceNumber = DYNAMIC, */ 130 .bAlternateSetting = 1, 131 .bNumEndpoints = 2, 132 .bInterfaceClass = USB_CLASS_CDC_DATA, 133}; 134 135static struct usb_endpoint_descriptor 136pn_fs_sink_desc = { 137 .bLength = USB_DT_ENDPOINT_SIZE, 138 .bDescriptorType = USB_DT_ENDPOINT, 139 140 .bEndpointAddress = USB_DIR_OUT, 141 .bmAttributes = USB_ENDPOINT_XFER_BULK, 142}; 143 144static struct usb_endpoint_descriptor 145pn_hs_sink_desc = { 146 .bLength = USB_DT_ENDPOINT_SIZE, 147 .bDescriptorType = USB_DT_ENDPOINT, 148 149 .bEndpointAddress = USB_DIR_OUT, 150 .bmAttributes = USB_ENDPOINT_XFER_BULK, 151 .wMaxPacketSize = cpu_to_le16(MAXPACKET), 152}; 153 154static struct usb_endpoint_descriptor 155pn_fs_source_desc = { 156 .bLength = USB_DT_ENDPOINT_SIZE, 157 .bDescriptorType = USB_DT_ENDPOINT, 158 159 .bEndpointAddress = USB_DIR_IN, 160 .bmAttributes = USB_ENDPOINT_XFER_BULK, 161}; 162 163static struct usb_endpoint_descriptor 164pn_hs_source_desc = { 165 .bLength = USB_DT_ENDPOINT_SIZE, 166 .bDescriptorType = USB_DT_ENDPOINT, 167 168 .bEndpointAddress = USB_DIR_IN, 169 .bmAttributes = USB_ENDPOINT_XFER_BULK, 170 .wMaxPacketSize = cpu_to_le16(512), 171}; 172 173static struct usb_descriptor_header *fs_pn_function[] = { 174 (struct usb_descriptor_header *) &pn_control_intf_desc, 175 (struct usb_descriptor_header *) &pn_header_desc, 176 (struct usb_descriptor_header *) &pn_phonet_desc, 177 (struct usb_descriptor_header *) &pn_union_desc, 178 (struct usb_descriptor_header *) &pn_data_nop_intf_desc, 179 (struct usb_descriptor_header *) &pn_data_intf_desc, 180 (struct usb_descriptor_header *) &pn_fs_sink_desc, 181 (struct usb_descriptor_header *) &pn_fs_source_desc, 182 NULL, 183}; 184 185static struct usb_descriptor_header *hs_pn_function[] = { 186 (struct usb_descriptor_header *) &pn_control_intf_desc, 187 (struct usb_descriptor_header *) &pn_header_desc, 188 (struct usb_descriptor_header *) &pn_phonet_desc, 189 (struct usb_descriptor_header *) &pn_union_desc, 190 (struct usb_descriptor_header *) &pn_data_nop_intf_desc, 191 (struct usb_descriptor_header *) &pn_data_intf_desc, 192 (struct usb_descriptor_header *) &pn_hs_sink_desc, 193 (struct usb_descriptor_header *) &pn_hs_source_desc, 194 NULL, 195}; 196 197/*-------------------------------------------------------------------------*/ 198 199static int pn_net_open(struct net_device *dev) 200{ 201 netif_wake_queue(dev); 202 return 0; 203} 204 205static int pn_net_close(struct net_device *dev) 206{ 207 netif_stop_queue(dev); 208 return 0; 209} 210 211static void pn_tx_complete(struct usb_ep *ep, struct usb_request *req) 212{ 213 struct f_phonet *fp = ep->driver_data; 214 struct net_device *dev = fp->dev; 215 struct sk_buff *skb = req->context; 216 217 switch (req->status) { 218 case 0: 219 dev->stats.tx_packets++; 220 dev->stats.tx_bytes += skb->len; 221 break; 222 223 case -ESHUTDOWN: /* disconnected */ 224 case -ECONNRESET: /* disabled */ 225 dev->stats.tx_aborted_errors++; 226 default: 227 dev->stats.tx_errors++; 228 } 229 230 dev_kfree_skb_any(skb); 231 netif_wake_queue(dev); 232} 233 234static int pn_net_xmit(struct sk_buff *skb, struct net_device *dev) 235{ 236 struct phonet_port *port = netdev_priv(dev); 237 struct f_phonet *fp; 238 struct usb_request *req; 239 unsigned long flags; 240 241 if (skb->protocol != htons(ETH_P_PHONET)) 242 goto out; 243 244 spin_lock_irqsave(&port->lock, flags); 245 fp = port->usb; 246 if (unlikely(!fp)) /* race with carrier loss */ 247 goto out_unlock; 248 249 req = fp->in_req; 250 req->buf = skb->data; 251 req->length = skb->len; 252 req->complete = pn_tx_complete; 253 req->zero = 1; 254 req->context = skb; 255 256 if (unlikely(usb_ep_queue(fp->in_ep, req, GFP_ATOMIC))) 257 goto out_unlock; 258 259 netif_stop_queue(dev); 260 skb = NULL; 261 262out_unlock: 263 spin_unlock_irqrestore(&port->lock, flags); 264out: 265 if (unlikely(skb)) { 266 dev_kfree_skb(skb); 267 dev->stats.tx_dropped++; 268 } 269 return NETDEV_TX_OK; 270} 271 272static int pn_net_mtu(struct net_device *dev, int new_mtu) 273{ 274 if ((new_mtu < PHONET_MIN_MTU) || (new_mtu > PHONET_MAX_MTU)) 275 return -EINVAL; 276 dev->mtu = new_mtu; 277 return 0; 278} 279 280static const struct net_device_ops pn_netdev_ops = { 281 .ndo_open = pn_net_open, 282 .ndo_stop = pn_net_close, 283 .ndo_start_xmit = pn_net_xmit, 284 .ndo_change_mtu = pn_net_mtu, 285}; 286 287static void pn_net_setup(struct net_device *dev) 288{ 289 dev->features = 0; 290 dev->type = ARPHRD_PHONET; 291 dev->flags = IFF_POINTOPOINT | IFF_NOARP; 292 dev->mtu = PHONET_DEV_MTU; 293 dev->hard_header_len = 1; 294 dev->dev_addr[0] = PN_MEDIA_USB; 295 dev->addr_len = 1; 296 dev->tx_queue_len = 1; 297 298 dev->netdev_ops = &pn_netdev_ops; 299 dev->destructor = free_netdev; 300 dev->header_ops = &phonet_header_ops; 301} 302 303/*-------------------------------------------------------------------------*/ 304 305/* 306 * Queue buffer for data from the host 307 */ 308static int 309pn_rx_submit(struct f_phonet *fp, struct usb_request *req, gfp_t gfp_flags) 310{ 311 struct net_device *dev = fp->dev; 312 struct page *page; 313 int err; 314 315 page = __netdev_alloc_page(dev, gfp_flags); 316 if (!page) 317 return -ENOMEM; 318 319 req->buf = page_address(page); 320 req->length = PAGE_SIZE; 321 req->context = page; 322 323 err = usb_ep_queue(fp->out_ep, req, gfp_flags); 324 if (unlikely(err)) 325 netdev_free_page(dev, page); 326 return err; 327} 328 329static void pn_rx_complete(struct usb_ep *ep, struct usb_request *req) 330{ 331 struct f_phonet *fp = ep->driver_data; 332 struct net_device *dev = fp->dev; 333 struct page *page = req->context; 334 struct sk_buff *skb; 335 unsigned long flags; 336 int status = req->status; 337 338 switch (status) { 339 case 0: 340 spin_lock_irqsave(&fp->rx.lock, flags); 341 skb = fp->rx.skb; 342 if (!skb) 343 skb = fp->rx.skb = netdev_alloc_skb(dev, 12); 344 if (req->actual < req->length) /* Last fragment */ 345 fp->rx.skb = NULL; 346 spin_unlock_irqrestore(&fp->rx.lock, flags); 347 348 if (unlikely(!skb)) 349 break; 350 351 if (skb->len == 0) { /* First fragment */ 352 skb->protocol = htons(ETH_P_PHONET); 353 skb_reset_mac_header(skb); 354 /* Can't use pskb_pull() on page in IRQ */ 355 memcpy(skb_put(skb, 1), page_address(page), 1); 356 } 357 358 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, 359 skb->len == 0, req->actual); 360 page = NULL; 361 362 if (req->actual < req->length) { /* Last fragment */ 363 skb->dev = dev; 364 dev->stats.rx_packets++; 365 dev->stats.rx_bytes += skb->len; 366 367 netif_rx(skb); 368 } 369 break; 370 371 /* Do not resubmit in these cases: */ 372 case -ESHUTDOWN: /* disconnect */ 373 case -ECONNABORTED: /* hw reset */ 374 case -ECONNRESET: /* dequeued (unlink or netif down) */ 375 req = NULL; 376 break; 377 378 /* Do resubmit in these cases: */ 379 case -EOVERFLOW: /* request buffer overflow */ 380 dev->stats.rx_over_errors++; 381 default: 382 dev->stats.rx_errors++; 383 break; 384 } 385 386 if (page) 387 netdev_free_page(dev, page); 388 if (req) 389 pn_rx_submit(fp, req, GFP_ATOMIC); 390} 391 392/*-------------------------------------------------------------------------*/ 393 394static void __pn_reset(struct usb_function *f) 395{ 396 struct f_phonet *fp = func_to_pn(f); 397 struct net_device *dev = fp->dev; 398 struct phonet_port *port = netdev_priv(dev); 399 400 netif_carrier_off(dev); 401 port->usb = NULL; 402 403 usb_ep_disable(fp->out_ep); 404 usb_ep_disable(fp->in_ep); 405 if (fp->rx.skb) { 406 dev_kfree_skb_irq(fp->rx.skb); 407 fp->rx.skb = NULL; 408 } 409} 410 411static int pn_set_alt(struct usb_function *f, unsigned intf, unsigned alt) 412{ 413 struct f_phonet *fp = func_to_pn(f); 414 struct usb_gadget *gadget = fp->function.config->cdev->gadget; 415 416 if (intf == pn_control_intf_desc.bInterfaceNumber) 417 /* control interface, no altsetting */ 418 return (alt > 0) ? -EINVAL : 0; 419 420 if (intf == pn_data_intf_desc.bInterfaceNumber) { 421 struct net_device *dev = fp->dev; 422 struct phonet_port *port = netdev_priv(dev); 423 424 /* data intf (0: inactive, 1: active) */ 425 if (alt > 1) 426 return -EINVAL; 427 428 spin_lock(&port->lock); 429 __pn_reset(f); 430 if (alt == 1) { 431 int i; 432 433 if (config_ep_by_speed(gadget, f, fp->in_ep) || 434 config_ep_by_speed(gadget, f, fp->out_ep)) { 435 fp->in_ep->desc = NULL; 436 fp->out_ep->desc = NULL; 437 return -EINVAL; 438 } 439 usb_ep_enable(fp->out_ep); 440 usb_ep_enable(fp->in_ep); 441 442 port->usb = fp; 443 fp->out_ep->driver_data = fp; 444 fp->in_ep->driver_data = fp; 445 446 netif_carrier_on(dev); 447 for (i = 0; i < phonet_rxq_size; i++) 448 pn_rx_submit(fp, fp->out_reqv[i], GFP_ATOMIC); 449 } 450 spin_unlock(&port->lock); 451 return 0; 452 } 453 454 return -EINVAL; 455} 456 457static int pn_get_alt(struct usb_function *f, unsigned intf) 458{ 459 struct f_phonet *fp = func_to_pn(f); 460 461 if (intf == pn_control_intf_desc.bInterfaceNumber) 462 return 0; 463 464 if (intf == pn_data_intf_desc.bInterfaceNumber) { 465 struct phonet_port *port = netdev_priv(fp->dev); 466 u8 alt; 467 468 spin_lock(&port->lock); 469 alt = port->usb != NULL; 470 spin_unlock(&port->lock); 471 return alt; 472 } 473 474 return -EINVAL; 475} 476 477static void pn_disconnect(struct usb_function *f) 478{ 479 struct f_phonet *fp = func_to_pn(f); 480 struct phonet_port *port = netdev_priv(fp->dev); 481 unsigned long flags; 482 483 /* remain disabled until set_alt */ 484 spin_lock_irqsave(&port->lock, flags); 485 __pn_reset(f); 486 spin_unlock_irqrestore(&port->lock, flags); 487} 488 489/*-------------------------------------------------------------------------*/ 490 491static __init 492int pn_bind(struct usb_configuration *c, struct usb_function *f) 493{ 494 struct usb_composite_dev *cdev = c->cdev; 495 struct usb_gadget *gadget = cdev->gadget; 496 struct f_phonet *fp = func_to_pn(f); 497 struct usb_ep *ep; 498 int status, i; 499 500 /* Reserve interface IDs */ 501 status = usb_interface_id(c, f); 502 if (status < 0) 503 goto err; 504 pn_control_intf_desc.bInterfaceNumber = status; 505 pn_union_desc.bMasterInterface0 = status; 506 507 status = usb_interface_id(c, f); 508 if (status < 0) 509 goto err; 510 pn_data_nop_intf_desc.bInterfaceNumber = status; 511 pn_data_intf_desc.bInterfaceNumber = status; 512 pn_union_desc.bSlaveInterface0 = status; 513 514 /* Reserve endpoints */ 515 status = -ENODEV; 516 ep = usb_ep_autoconfig(gadget, &pn_fs_sink_desc); 517 if (!ep) 518 goto err; 519 fp->out_ep = ep; 520 ep->driver_data = fp; /* Claim */ 521 522 ep = usb_ep_autoconfig(gadget, &pn_fs_source_desc); 523 if (!ep) 524 goto err; 525 fp->in_ep = ep; 526 ep->driver_data = fp; /* Claim */ 527 528 pn_hs_sink_desc.bEndpointAddress = 529 pn_fs_sink_desc.bEndpointAddress; 530 pn_hs_source_desc.bEndpointAddress = 531 pn_fs_source_desc.bEndpointAddress; 532 533 /* Do not try to bind Phonet twice... */ 534 fp->function.descriptors = fs_pn_function; 535 fp->function.hs_descriptors = hs_pn_function; 536 537 /* Incoming USB requests */ 538 status = -ENOMEM; 539 for (i = 0; i < phonet_rxq_size; i++) { 540 struct usb_request *req; 541 542 req = usb_ep_alloc_request(fp->out_ep, GFP_KERNEL); 543 if (!req) 544 goto err; 545 546 req->complete = pn_rx_complete; 547 fp->out_reqv[i] = req; 548 } 549 550 /* Outgoing USB requests */ 551 fp->in_req = usb_ep_alloc_request(fp->in_ep, GFP_KERNEL); 552 if (!fp->in_req) 553 goto err; 554 555 INFO(cdev, "USB CDC Phonet function\n"); 556 INFO(cdev, "using %s, OUT %s, IN %s\n", cdev->gadget->name, 557 fp->out_ep->name, fp->in_ep->name); 558 return 0; 559 560err: 561 if (fp->out_ep) 562 fp->out_ep->driver_data = NULL; 563 if (fp->in_ep) 564 fp->in_ep->driver_data = NULL; 565 ERROR(cdev, "USB CDC Phonet: cannot autoconfigure\n"); 566 return status; 567} 568 569static void 570pn_unbind(struct usb_configuration *c, struct usb_function *f) 571{ 572 struct f_phonet *fp = func_to_pn(f); 573 int i; 574 575 /* We are already disconnected */ 576 if (fp->in_req) 577 usb_ep_free_request(fp->in_ep, fp->in_req); 578 for (i = 0; i < phonet_rxq_size; i++) 579 if (fp->out_reqv[i]) 580 usb_ep_free_request(fp->out_ep, fp->out_reqv[i]); 581 582 kfree(fp); 583} 584 585/*-------------------------------------------------------------------------*/ 586 587static struct net_device *dev; 588 589int __init phonet_bind_config(struct usb_configuration *c) 590{ 591 struct f_phonet *fp; 592 int err, size; 593 594 size = sizeof(*fp) + (phonet_rxq_size * sizeof(struct usb_request *)); 595 fp = kzalloc(size, GFP_KERNEL); 596 if (!fp) 597 return -ENOMEM; 598 599 fp->dev = dev; 600 fp->function.name = "phonet"; 601 fp->function.bind = pn_bind; 602 fp->function.unbind = pn_unbind; 603 fp->function.set_alt = pn_set_alt; 604 fp->function.get_alt = pn_get_alt; 605 fp->function.disable = pn_disconnect; 606 spin_lock_init(&fp->rx.lock); 607 608 err = usb_add_function(c, &fp->function); 609 if (err) 610 kfree(fp); 611 return err; 612} 613 614int __init gphonet_setup(struct usb_gadget *gadget) 615{ 616 struct phonet_port *port; 617 int err; 618 619 /* Create net device */ 620 BUG_ON(dev); 621 dev = alloc_netdev(sizeof(*port), "upnlink%d", pn_net_setup); 622 if (!dev) 623 return -ENOMEM; 624 625 port = netdev_priv(dev); 626 spin_lock_init(&port->lock); 627 netif_carrier_off(dev); 628 SET_NETDEV_DEV(dev, &gadget->dev); 629 630 err = register_netdev(dev); 631 if (err) 632 free_netdev(dev); 633 return err; 634} 635 636void gphonet_cleanup(void) 637{ 638 unregister_netdev(dev); 639}