1 /* $NetBSD: if_cdce.c,v 1.33 2011/06/07 05:46:00 msaitoh Exp $ */ 2 3 /* 4 * Copyright (c) 1997, 1998, 1999, 2000-2003 Bill Paul <wpaul@windriver.com> 5 * Copyright (c) 2003 Craig Boston 6 * Copyright (c) 2004 Daniel Hartmeier 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by Bill Paul. 20 * 4. Neither the name of the author nor the names of any co-contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul, THE VOICES IN HIS HEAD OR 28 * THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 /* 38 * USB Communication Device Class (Ethernet Networking Control Model) 39 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf 40 * 41 */ 42 43 #include <sys/cdefs.h> 44 __KERNEL_RCSID(0, "$NetBSD: if_cdce.c,v 1.33 2011/06/07 05:46:00 msaitoh Exp $"); 45 #ifdef __NetBSD__ 46 #include "opt_inet.h" 47 #endif 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/sockio.h> 52 #include <sys/mbuf.h> 53 #include <sys/kernel.h> 54 #include <sys/socket.h> 55 #include <sys/device.h> 56 57 #if NRND > 0 58 #include <sys/rnd.h> 59 #endif 60 61 #include <net/if.h> 62 #include <net/if_arp.h> 63 #include <net/if_dl.h> 64 #include <net/if_media.h> 65 66 #include <net/bpf.h> 67 68 #include <net/if_ether.h> 69 #ifdef INET 70 #include <netinet/in.h> 71 #include <netinet/if_inarp.h> 72 #endif 73 74 75 76 #include <dev/usb/usb.h> 77 #include <dev/usb/usbdi.h> 78 #include <dev/usb/usbdi_util.h> 79 #include <dev/usb/usbdevs.h> 80 #include <dev/usb/usbcdc.h> 81 82 #include <dev/usb/if_cdcereg.h> 83 84 Static int cdce_tx_list_init(struct cdce_softc *); 85 Static int cdce_rx_list_init(struct cdce_softc *); 86 Static int cdce_newbuf(struct cdce_softc *, struct cdce_chain *, 87 struct mbuf *); 88 Static int cdce_encap(struct cdce_softc *, struct mbuf *, int); 89 Static void cdce_rxeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 90 Static void cdce_txeof(usbd_xfer_handle, usbd_private_handle, usbd_status); 91 Static void cdce_start(struct ifnet *); 92 Static int cdce_ioctl(struct ifnet *, u_long, void *); 93 Static void cdce_init(void *); 94 Static void cdce_watchdog(struct ifnet *); 95 Static void cdce_stop(struct cdce_softc *); 96 97 Static const struct cdce_type cdce_devs[] = { 98 {{ USB_VENDOR_ACERLABS, USB_PRODUCT_ACERLABS_M5632 }, CDCE_NO_UNION }, 99 {{ USB_VENDOR_COMPAQ, USB_PRODUCT_COMPAQ_IPAQLINUX }, CDCE_NO_UNION }, 100 {{ USB_VENDOR_GMATE, USB_PRODUCT_GMATE_YP3X00 }, CDCE_NO_UNION }, 101 {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN }, CDCE_ZAURUS | CDCE_NO_UNION }, 102 {{ USB_VENDOR_MOTOROLA2, USB_PRODUCT_MOTOROLA2_USBLAN2 }, CDCE_ZAURUS | CDCE_NO_UNION }, 103 {{ USB_VENDOR_NETCHIP, USB_PRODUCT_NETCHIP_ETHERNETGADGET }, CDCE_NO_UNION }, 104 {{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION }, 105 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS }, 106 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION }, 107 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION }, 108 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION }, 109 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION }, 110 }; 111 #define cdce_lookup(v, p) \ 112 ((const struct cdce_type *)usb_lookup(cdce_devs, v, p)) 113 114 int cdce_match(device_t, cfdata_t, void *); 115 void cdce_attach(device_t, device_t, void *); 116 int cdce_detach(device_t, int); 117 int cdce_activate(device_t, enum devact); 118 extern struct cfdriver cdce_cd; 119 CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach, 120 cdce_detach, cdce_activate); 121 122 int 123 cdce_match(device_t parent, cfdata_t match, void *aux) 124 { 125 struct usbif_attach_arg *uaa = aux; 126 127 if (cdce_lookup(uaa->vendor, uaa->product) != NULL) 128 return (UMATCH_VENDOR_PRODUCT); 129 130 if (uaa->class == UICLASS_CDC && uaa->subclass == 131 UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL) 132 return (UMATCH_IFACECLASS_GENERIC); 133 134 return (UMATCH_NONE); 135 } 136 137 void 138 cdce_attach(device_t parent, device_t self, void *aux) 139 { 140 struct cdce_softc *sc = device_private(self); 141 struct usbif_attach_arg *uaa = aux; 142 char *devinfop; 143 int s; 144 struct ifnet *ifp; 145 usbd_device_handle dev = uaa->device; 146 const struct cdce_type *t; 147 usb_interface_descriptor_t *id; 148 usb_endpoint_descriptor_t *ed; 149 const usb_cdc_union_descriptor_t *ud; 150 usb_config_descriptor_t *cd; 151 int data_ifcno; 152 int i, j, numalts; 153 u_char eaddr[ETHER_ADDR_LEN]; 154 const usb_cdc_ethernet_descriptor_t *ue; 155 char eaddr_str[USB_MAX_ENCODED_STRING_LEN]; 156 157 sc->cdce_dev = self; 158 159 aprint_naive("\n"); 160 aprint_normal("\n"); 161 162 devinfop = usbd_devinfo_alloc(dev, 0); 163 aprint_normal_dev(self, "%s\n", devinfop); 164 usbd_devinfo_free(devinfop); 165 166 sc->cdce_udev = uaa->device; 167 sc->cdce_ctl_iface = uaa->iface; 168 169 t = cdce_lookup(uaa->vendor, uaa->product); 170 if (t) 171 sc->cdce_flags = t->cdce_flags; 172 173 if (sc->cdce_flags & CDCE_NO_UNION) 174 sc->cdce_data_iface = sc->cdce_ctl_iface; 175 else { 176 ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev, 177 UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION); 178 if (ud == NULL) { 179 aprint_error_dev(self, "no union descriptor\n"); 180 return; 181 } 182 data_ifcno = ud->bSlaveInterface[0]; 183 184 for (i = 0; i < uaa->nifaces; i++) { 185 if (uaa->ifaces[i] != NULL) { 186 id = usbd_get_interface_descriptor( 187 uaa->ifaces[i]); 188 if (id != NULL && id->bInterfaceNumber == 189 data_ifcno) { 190 sc->cdce_data_iface = uaa->ifaces[i]; 191 uaa->ifaces[i] = NULL; 192 } 193 } 194 } 195 } 196 197 if (sc->cdce_data_iface == NULL) { 198 aprint_error_dev(self, "no data interface\n"); 199 return; 200 } 201 202 /* 203 * <quote> 204 * The Data Class interface of a networking device shall have a minimum 205 * of two interface settings. The first setting (the default interface 206 * setting) includes no endpoints and therefore no networking traffic is 207 * exchanged whenever the default interface setting is selected. One or 208 * more additional interface settings are used for normal operation, and 209 * therefore each includes a pair of endpoints (one IN, and one OUT) to 210 * exchange network traffic. Select an alternate interface setting to 211 * initialize the network aspects of the device and to enable the 212 * exchange of network traffic. 213 * </quote> 214 * 215 * Some devices, most notably cable modems, include interface settings 216 * that have no IN or OUT endpoint, therefore loop through the list of all 217 * available interface settings looking for one with both IN and OUT 218 * endpoints. 219 */ 220 id = usbd_get_interface_descriptor(sc->cdce_data_iface); 221 cd = usbd_get_config_descriptor(sc->cdce_udev); 222 numalts = usbd_get_no_alts(cd, id->bInterfaceNumber); 223 224 for (j = 0; j < numalts; j++) { 225 if (usbd_set_interface(sc->cdce_data_iface, j)) { 226 aprint_error_dev(sc->cdce_dev, 227 "setting alternate interface failed\n"); 228 return; 229 } 230 /* Find endpoints. */ 231 id = usbd_get_interface_descriptor(sc->cdce_data_iface); 232 sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1; 233 for (i = 0; i < id->bNumEndpoints; i++) { 234 ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i); 235 if (!ed) { 236 aprint_error_dev(self, 237 "could not read endpoint descriptor\n"); 238 return; 239 } 240 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 241 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 242 sc->cdce_bulkin_no = ed->bEndpointAddress; 243 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 244 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 245 sc->cdce_bulkout_no = ed->bEndpointAddress; 246 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 247 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 248 /* XXX: CDC spec defines an interrupt pipe, but it is not 249 * needed for simple host-to-host applications. */ 250 } else { 251 aprint_error_dev(self, "unexpected endpoint\n"); 252 } 253 } 254 /* If we found something, try and use it... */ 255 if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1)) 256 break; 257 } 258 259 if (sc->cdce_bulkin_no == -1) { 260 aprint_error_dev(self, "could not find data bulk in\n"); 261 return; 262 } 263 if (sc->cdce_bulkout_no == -1 ) { 264 aprint_error_dev(self, "could not find data bulk out\n"); 265 return; 266 } 267 268 ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev, 269 UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF); 270 if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) || 271 ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) { 272 aprint_normal_dev(self, "faking address\n"); 273 eaddr[0]= 0x2a; 274 memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t)); 275 eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev)); 276 } 277 278 s = splnet(); 279 280 aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr)); 281 282 ifp = GET_IFP(sc); 283 ifp->if_softc = sc; 284 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 285 ifp->if_ioctl = cdce_ioctl; 286 ifp->if_start = cdce_start; 287 ifp->if_watchdog = cdce_watchdog; 288 strncpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ); 289 290 IFQ_SET_READY(&ifp->if_snd); 291 292 if_attach(ifp); 293 ether_ifattach(ifp, eaddr); 294 295 sc->cdce_attached = 1; 296 splx(s); 297 298 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev, 299 sc->cdce_dev); 300 301 if (!pmf_device_register(self, NULL, NULL)) 302 aprint_error_dev(self, "couldn't establish power handler\n"); 303 304 return; 305 } 306 307 int 308 cdce_detach(device_t self, int flags) 309 { 310 struct cdce_softc *sc = device_private(self); 311 struct ifnet *ifp = GET_IFP(sc); 312 int s; 313 314 if (device_pmf_is_registered(self)) 315 pmf_device_deregister(self); 316 317 s = splusb(); 318 319 if (!sc->cdce_attached) { 320 splx(s); 321 return (0); 322 } 323 324 if (ifp->if_flags & IFF_RUNNING) 325 cdce_stop(sc); 326 327 ether_ifdetach(ifp); 328 329 if_detach(ifp); 330 331 sc->cdce_attached = 0; 332 splx(s); 333 334 return (0); 335 } 336 337 Static void 338 cdce_start(struct ifnet *ifp) 339 { 340 struct cdce_softc *sc = ifp->if_softc; 341 struct mbuf *m_head = NULL; 342 343 if (sc->cdce_dying || (ifp->if_flags & IFF_OACTIVE)) 344 return; 345 346 IFQ_POLL(&ifp->if_snd, m_head); 347 if (m_head == NULL) 348 return; 349 350 if (cdce_encap(sc, m_head, 0)) { 351 ifp->if_flags |= IFF_OACTIVE; 352 return; 353 } 354 355 IFQ_DEQUEUE(&ifp->if_snd, m_head); 356 357 bpf_mtap(ifp, m_head); 358 359 ifp->if_flags |= IFF_OACTIVE; 360 361 ifp->if_timer = 6; 362 } 363 364 Static int 365 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx) 366 { 367 struct cdce_chain *c; 368 usbd_status err; 369 int extra = 0; 370 371 c = &sc->cdce_cdata.cdce_tx_chain[idx]; 372 373 m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf); 374 if (sc->cdce_flags & CDCE_ZAURUS) { 375 /* Zaurus wants a 32-bit CRC appended to every frame */ 376 uint32_t crc; 377 378 crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len)); 379 memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc)); 380 extra = sizeof(crc); 381 } 382 c->cdce_mbuf = m; 383 384 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkout_pipe, c, c->cdce_buf, 385 m->m_pkthdr.len + extra, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 386 10000, cdce_txeof); 387 err = usbd_transfer(c->cdce_xfer); 388 if (err != USBD_IN_PROGRESS) { 389 cdce_stop(sc); 390 return (EIO); 391 } 392 393 sc->cdce_cdata.cdce_tx_cnt++; 394 395 return (0); 396 } 397 398 Static void 399 cdce_stop(struct cdce_softc *sc) 400 { 401 usbd_status err; 402 struct ifnet *ifp = GET_IFP(sc); 403 int i; 404 405 ifp->if_timer = 0; 406 407 if (sc->cdce_bulkin_pipe != NULL) { 408 err = usbd_abort_pipe(sc->cdce_bulkin_pipe); 409 if (err) 410 printf("%s: abort rx pipe failed: %s\n", 411 device_xname(sc->cdce_dev), usbd_errstr(err)); 412 err = usbd_close_pipe(sc->cdce_bulkin_pipe); 413 if (err) 414 printf("%s: close rx pipe failed: %s\n", 415 device_xname(sc->cdce_dev), usbd_errstr(err)); 416 sc->cdce_bulkin_pipe = NULL; 417 } 418 419 if (sc->cdce_bulkout_pipe != NULL) { 420 err = usbd_abort_pipe(sc->cdce_bulkout_pipe); 421 if (err) 422 printf("%s: abort tx pipe failed: %s\n", 423 device_xname(sc->cdce_dev), usbd_errstr(err)); 424 err = usbd_close_pipe(sc->cdce_bulkout_pipe); 425 if (err) 426 printf("%s: close tx pipe failed: %s\n", 427 device_xname(sc->cdce_dev), usbd_errstr(err)); 428 sc->cdce_bulkout_pipe = NULL; 429 } 430 431 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 432 if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) { 433 m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf); 434 sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL; 435 } 436 if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) { 437 usbd_free_xfer 438 (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer); 439 sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL; 440 } 441 } 442 443 for (i = 0; i < CDCE_TX_LIST_CNT; i++) { 444 if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) { 445 m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf); 446 sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL; 447 } 448 if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) { 449 usbd_free_xfer( 450 sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer); 451 sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL; 452 } 453 } 454 455 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 456 } 457 458 Static int 459 cdce_ioctl(struct ifnet *ifp, u_long command, void *data) 460 { 461 struct cdce_softc *sc = ifp->if_softc; 462 struct ifaddr *ifa = (struct ifaddr *)data; 463 struct ifreq *ifr = (struct ifreq *)data; 464 int s, error = 0; 465 466 if (sc->cdce_dying) 467 return (EIO); 468 469 s = splnet(); 470 471 switch(command) { 472 case SIOCINITIFADDR: 473 ifp->if_flags |= IFF_UP; 474 cdce_init(sc); 475 switch (ifa->ifa_addr->sa_family) { 476 #ifdef INET 477 case AF_INET: 478 arp_ifinit(ifp, ifa); 479 break; 480 #endif /* INET */ 481 } 482 break; 483 484 case SIOCSIFMTU: 485 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) 486 error = EINVAL; 487 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) 488 error = 0; 489 break; 490 491 case SIOCSIFFLAGS: 492 if ((error = ifioctl_common(ifp, command, data)) != 0) 493 break; 494 /* XXX re-use ether_ioctl() */ 495 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 496 case IFF_UP: 497 cdce_init(sc); 498 break; 499 case IFF_RUNNING: 500 cdce_stop(sc); 501 break; 502 default: 503 break; 504 } 505 break; 506 507 default: 508 error = ether_ioctl(ifp, command, data); 509 break; 510 } 511 512 splx(s); 513 514 if (error == ENETRESET) 515 error = 0; 516 517 return (error); 518 } 519 520 Static void 521 cdce_watchdog(struct ifnet *ifp) 522 { 523 struct cdce_softc *sc = ifp->if_softc; 524 525 if (sc->cdce_dying) 526 return; 527 528 ifp->if_oerrors++; 529 printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev)); 530 } 531 532 Static void 533 cdce_init(void *xsc) 534 { 535 struct cdce_softc *sc = xsc; 536 struct ifnet *ifp = GET_IFP(sc); 537 struct cdce_chain *c; 538 usbd_status err; 539 int s, i; 540 541 if (ifp->if_flags & IFF_RUNNING) 542 return; 543 544 s = splnet(); 545 546 if (cdce_tx_list_init(sc) == ENOBUFS) { 547 printf("%s: tx list init failed\n", device_xname(sc->cdce_dev)); 548 splx(s); 549 return; 550 } 551 552 if (cdce_rx_list_init(sc) == ENOBUFS) { 553 printf("%s: rx list init failed\n", device_xname(sc->cdce_dev)); 554 splx(s); 555 return; 556 } 557 558 /* Maybe set multicast / broadcast here??? */ 559 560 err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no, 561 USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe); 562 if (err) { 563 printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev), 564 usbd_errstr(err)); 565 splx(s); 566 return; 567 } 568 569 err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no, 570 USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe); 571 if (err) { 572 printf("%s: open tx pipe failed: %s\n", 573 device_xname(sc->cdce_dev), usbd_errstr(err)); 574 splx(s); 575 return; 576 } 577 578 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 579 c = &sc->cdce_cdata.cdce_rx_chain[i]; 580 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, 581 c->cdce_buf, CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 582 USBD_NO_TIMEOUT, cdce_rxeof); 583 usbd_transfer(c->cdce_xfer); 584 } 585 586 ifp->if_flags |= IFF_RUNNING; 587 ifp->if_flags &= ~IFF_OACTIVE; 588 589 splx(s); 590 } 591 592 Static int 593 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m) 594 { 595 struct mbuf *m_new = NULL; 596 597 if (m == NULL) { 598 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 599 if (m_new == NULL) { 600 printf("%s: no memory for rx list " 601 "-- packet dropped!\n", device_xname(sc->cdce_dev)); 602 return (ENOBUFS); 603 } 604 MCLGET(m_new, M_DONTWAIT); 605 if (!(m_new->m_flags & M_EXT)) { 606 printf("%s: no memory for rx list " 607 "-- packet dropped!\n", device_xname(sc->cdce_dev)); 608 m_freem(m_new); 609 return (ENOBUFS); 610 } 611 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 612 } else { 613 m_new = m; 614 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 615 m_new->m_data = m_new->m_ext.ext_buf; 616 } 617 c->cdce_mbuf = m_new; 618 return (0); 619 } 620 621 Static int 622 cdce_rx_list_init(struct cdce_softc *sc) 623 { 624 struct cdce_cdata *cd; 625 struct cdce_chain *c; 626 int i; 627 628 cd = &sc->cdce_cdata; 629 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 630 c = &cd->cdce_rx_chain[i]; 631 c->cdce_sc = sc; 632 c->cdce_idx = i; 633 if (cdce_newbuf(sc, c, NULL) == ENOBUFS) 634 return (ENOBUFS); 635 if (c->cdce_xfer == NULL) { 636 c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev); 637 if (c->cdce_xfer == NULL) 638 return (ENOBUFS); 639 c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, 640 CDCE_BUFSZ); 641 if (c->cdce_buf == NULL) 642 return (ENOBUFS); 643 } 644 } 645 646 return (0); 647 } 648 649 Static int 650 cdce_tx_list_init(struct cdce_softc *sc) 651 { 652 struct cdce_cdata *cd; 653 struct cdce_chain *c; 654 int i; 655 656 cd = &sc->cdce_cdata; 657 for (i = 0; i < CDCE_TX_LIST_CNT; i++) { 658 c = &cd->cdce_tx_chain[i]; 659 c->cdce_sc = sc; 660 c->cdce_idx = i; 661 c->cdce_mbuf = NULL; 662 if (c->cdce_xfer == NULL) { 663 c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev); 664 if (c->cdce_xfer == NULL) 665 return (ENOBUFS); 666 c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, CDCE_BUFSZ); 667 if (c->cdce_buf == NULL) 668 return (ENOBUFS); 669 } 670 } 671 672 return (0); 673 } 674 675 Static void 676 cdce_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 677 { 678 struct cdce_chain *c = priv; 679 struct cdce_softc *sc = c->cdce_sc; 680 struct ifnet *ifp = GET_IFP(sc); 681 struct mbuf *m; 682 int total_len = 0; 683 int s; 684 685 if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING)) 686 return; 687 688 if (status != USBD_NORMAL_COMPLETION) { 689 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 690 return; 691 if (sc->cdce_rxeof_errors == 0) 692 printf("%s: usb error on rx: %s\n", 693 device_xname(sc->cdce_dev), usbd_errstr(status)); 694 if (status == USBD_STALLED) 695 usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe); 696 DELAY(sc->cdce_rxeof_errors * 10000); 697 sc->cdce_rxeof_errors++; 698 goto done; 699 } 700 701 sc->cdce_rxeof_errors = 0; 702 703 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 704 if (sc->cdce_flags & CDCE_ZAURUS) 705 total_len -= 4; /* Strip off CRC added by Zaurus */ 706 if (total_len <= 1) 707 goto done; 708 709 m = c->cdce_mbuf; 710 memcpy(mtod(m, char *), c->cdce_buf, total_len); 711 712 if (total_len < sizeof(struct ether_header)) { 713 ifp->if_ierrors++; 714 goto done; 715 } 716 717 ifp->if_ipackets++; 718 m->m_pkthdr.len = m->m_len = total_len; 719 m->m_pkthdr.rcvif = ifp; 720 721 s = splnet(); 722 723 if (cdce_newbuf(sc, c, NULL) == ENOBUFS) { 724 ifp->if_ierrors++; 725 goto done1; 726 } 727 728 bpf_mtap(ifp, m); 729 730 (*(ifp)->if_input)((ifp), (m)); 731 732 done1: 733 splx(s); 734 735 done: 736 /* Setup new transfer. */ 737 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, c->cdce_buf, 738 CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 739 cdce_rxeof); 740 usbd_transfer(c->cdce_xfer); 741 } 742 743 Static void 744 cdce_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 745 usbd_status status) 746 { 747 struct cdce_chain *c = priv; 748 struct cdce_softc *sc = c->cdce_sc; 749 struct ifnet *ifp = GET_IFP(sc); 750 usbd_status err; 751 int s; 752 753 if (sc->cdce_dying) 754 return; 755 756 s = splnet(); 757 758 ifp->if_timer = 0; 759 ifp->if_flags &= ~IFF_OACTIVE; 760 761 if (status != USBD_NORMAL_COMPLETION) { 762 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 763 splx(s); 764 return; 765 } 766 ifp->if_oerrors++; 767 printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev), 768 usbd_errstr(status)); 769 if (status == USBD_STALLED) 770 usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe); 771 splx(s); 772 return; 773 } 774 775 usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err); 776 777 if (c->cdce_mbuf != NULL) { 778 m_freem(c->cdce_mbuf); 779 c->cdce_mbuf = NULL; 780 } 781 782 if (err) 783 ifp->if_oerrors++; 784 else 785 ifp->if_opackets++; 786 787 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 788 cdce_start(ifp); 789 790 splx(s); 791 } 792 793 int 794 cdce_activate(device_t self, enum devact act) 795 { 796 struct cdce_softc *sc = device_private(self); 797 798 switch (act) { 799 case DVACT_DEACTIVATE: 800 if_deactivate(GET_IFP(sc)); 801 sc->cdce_dying = 1; 802 return 0; 803 default: 804 return EOPNOTSUPP; 805 } 806 } 807