1 /* $NetBSD: if_cdce.c,v 1.34 2012/01/10 11:32:25 ws 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.34 2012/01/10 11:32:25 ws 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_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2501 }, CDCE_NO_UNION }, 104 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5500 }, CDCE_ZAURUS }, 105 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_A300 }, CDCE_ZAURUS | CDCE_NO_UNION }, 106 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_SL5600 }, CDCE_ZAURUS | CDCE_NO_UNION }, 107 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C700 }, CDCE_ZAURUS | CDCE_NO_UNION }, 108 {{ USB_VENDOR_SHARP, USB_PRODUCT_SHARP_C750 }, CDCE_ZAURUS | CDCE_NO_UNION }, 109 }; 110 #define cdce_lookup(v, p) \ 111 ((const struct cdce_type *)usb_lookup(cdce_devs, v, p)) 112 113 int cdce_match(device_t, cfdata_t, void *); 114 void cdce_attach(device_t, device_t, void *); 115 int cdce_detach(device_t, int); 116 int cdce_activate(device_t, enum devact); 117 extern struct cfdriver cdce_cd; 118 CFATTACH_DECL_NEW(cdce, sizeof(struct cdce_softc), cdce_match, cdce_attach, 119 cdce_detach, cdce_activate); 120 121 int 122 cdce_match(device_t parent, cfdata_t match, void *aux) 123 { 124 struct usbif_attach_arg *uaa = aux; 125 126 if (cdce_lookup(uaa->vendor, uaa->product) != NULL) 127 return (UMATCH_VENDOR_PRODUCT); 128 129 if (uaa->class == UICLASS_CDC && uaa->subclass == 130 UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL) 131 return (UMATCH_IFACECLASS_GENERIC); 132 133 return (UMATCH_NONE); 134 } 135 136 void 137 cdce_attach(device_t parent, device_t self, void *aux) 138 { 139 struct cdce_softc *sc = device_private(self); 140 struct usbif_attach_arg *uaa = aux; 141 char *devinfop; 142 int s; 143 struct ifnet *ifp; 144 usbd_device_handle dev = uaa->device; 145 const struct cdce_type *t; 146 usb_interface_descriptor_t *id; 147 usb_endpoint_descriptor_t *ed; 148 const usb_cdc_union_descriptor_t *ud; 149 usb_config_descriptor_t *cd; 150 int data_ifcno; 151 int i, j, numalts; 152 u_char eaddr[ETHER_ADDR_LEN]; 153 const usb_cdc_ethernet_descriptor_t *ue; 154 char eaddr_str[USB_MAX_ENCODED_STRING_LEN]; 155 156 sc->cdce_dev = self; 157 158 aprint_naive("\n"); 159 aprint_normal("\n"); 160 161 devinfop = usbd_devinfo_alloc(dev, 0); 162 aprint_normal_dev(self, "%s\n", devinfop); 163 usbd_devinfo_free(devinfop); 164 165 sc->cdce_udev = uaa->device; 166 sc->cdce_ctl_iface = uaa->iface; 167 168 t = cdce_lookup(uaa->vendor, uaa->product); 169 if (t) 170 sc->cdce_flags = t->cdce_flags; 171 172 if (sc->cdce_flags & CDCE_NO_UNION) 173 sc->cdce_data_iface = sc->cdce_ctl_iface; 174 else { 175 ud = (const usb_cdc_union_descriptor_t *)usb_find_desc(sc->cdce_udev, 176 UDESC_CS_INTERFACE, UDESCSUB_CDC_UNION); 177 if (ud == NULL) { 178 aprint_error_dev(self, "no union descriptor\n"); 179 return; 180 } 181 data_ifcno = ud->bSlaveInterface[0]; 182 183 for (i = 0; i < uaa->nifaces; i++) { 184 if (uaa->ifaces[i] != NULL) { 185 id = usbd_get_interface_descriptor( 186 uaa->ifaces[i]); 187 if (id != NULL && id->bInterfaceNumber == 188 data_ifcno) { 189 sc->cdce_data_iface = uaa->ifaces[i]; 190 uaa->ifaces[i] = NULL; 191 } 192 } 193 } 194 } 195 196 if (sc->cdce_data_iface == NULL) { 197 aprint_error_dev(self, "no data interface\n"); 198 return; 199 } 200 201 /* 202 * <quote> 203 * The Data Class interface of a networking device shall have a minimum 204 * of two interface settings. The first setting (the default interface 205 * setting) includes no endpoints and therefore no networking traffic is 206 * exchanged whenever the default interface setting is selected. One or 207 * more additional interface settings are used for normal operation, and 208 * therefore each includes a pair of endpoints (one IN, and one OUT) to 209 * exchange network traffic. Select an alternate interface setting to 210 * initialize the network aspects of the device and to enable the 211 * exchange of network traffic. 212 * </quote> 213 * 214 * Some devices, most notably cable modems, include interface settings 215 * that have no IN or OUT endpoint, therefore loop through the list of all 216 * available interface settings looking for one with both IN and OUT 217 * endpoints. 218 */ 219 id = usbd_get_interface_descriptor(sc->cdce_data_iface); 220 cd = usbd_get_config_descriptor(sc->cdce_udev); 221 numalts = usbd_get_no_alts(cd, id->bInterfaceNumber); 222 223 for (j = 0; j < numalts; j++) { 224 if (usbd_set_interface(sc->cdce_data_iface, j)) { 225 aprint_error_dev(sc->cdce_dev, 226 "setting alternate interface failed\n"); 227 return; 228 } 229 /* Find endpoints. */ 230 id = usbd_get_interface_descriptor(sc->cdce_data_iface); 231 sc->cdce_bulkin_no = sc->cdce_bulkout_no = -1; 232 for (i = 0; i < id->bNumEndpoints; i++) { 233 ed = usbd_interface2endpoint_descriptor(sc->cdce_data_iface, i); 234 if (!ed) { 235 aprint_error_dev(self, 236 "could not read endpoint descriptor\n"); 237 return; 238 } 239 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 240 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 241 sc->cdce_bulkin_no = ed->bEndpointAddress; 242 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT && 243 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) { 244 sc->cdce_bulkout_no = ed->bEndpointAddress; 245 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN && 246 UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) { 247 /* XXX: CDC spec defines an interrupt pipe, but it is not 248 * needed for simple host-to-host applications. */ 249 } else { 250 aprint_error_dev(self, "unexpected endpoint\n"); 251 } 252 } 253 /* If we found something, try and use it... */ 254 if ((sc->cdce_bulkin_no != -1) && (sc->cdce_bulkout_no != -1)) 255 break; 256 } 257 258 if (sc->cdce_bulkin_no == -1) { 259 aprint_error_dev(self, "could not find data bulk in\n"); 260 return; 261 } 262 if (sc->cdce_bulkout_no == -1 ) { 263 aprint_error_dev(self, "could not find data bulk out\n"); 264 return; 265 } 266 267 ue = (const usb_cdc_ethernet_descriptor_t *)usb_find_desc(dev, 268 UDESC_CS_INTERFACE, UDESCSUB_CDC_ENF); 269 if (!ue || usbd_get_string(dev, ue->iMacAddress, eaddr_str) || 270 ether_aton_r(eaddr, sizeof(eaddr), eaddr_str)) { 271 aprint_normal_dev(self, "faking address\n"); 272 eaddr[0]= 0x2a; 273 memcpy(&eaddr[1], &hardclock_ticks, sizeof(uint32_t)); 274 eaddr[5] = (uint8_t)(device_unit(sc->cdce_dev)); 275 } 276 277 s = splnet(); 278 279 aprint_normal_dev(self, "address %s\n", ether_sprintf(eaddr)); 280 281 ifp = GET_IFP(sc); 282 ifp->if_softc = sc; 283 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 284 ifp->if_ioctl = cdce_ioctl; 285 ifp->if_start = cdce_start; 286 ifp->if_watchdog = cdce_watchdog; 287 strncpy(ifp->if_xname, device_xname(sc->cdce_dev), IFNAMSIZ); 288 289 IFQ_SET_READY(&ifp->if_snd); 290 291 if_attach(ifp); 292 ether_ifattach(ifp, eaddr); 293 294 sc->cdce_attached = 1; 295 splx(s); 296 297 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->cdce_udev, 298 sc->cdce_dev); 299 300 if (!pmf_device_register(self, NULL, NULL)) 301 aprint_error_dev(self, "couldn't establish power handler\n"); 302 303 return; 304 } 305 306 int 307 cdce_detach(device_t self, int flags) 308 { 309 struct cdce_softc *sc = device_private(self); 310 struct ifnet *ifp = GET_IFP(sc); 311 int s; 312 313 if (device_pmf_is_registered(self)) 314 pmf_device_deregister(self); 315 316 s = splusb(); 317 318 if (!sc->cdce_attached) { 319 splx(s); 320 return (0); 321 } 322 323 if (ifp->if_flags & IFF_RUNNING) 324 cdce_stop(sc); 325 326 ether_ifdetach(ifp); 327 328 if_detach(ifp); 329 330 sc->cdce_attached = 0; 331 splx(s); 332 333 return (0); 334 } 335 336 Static void 337 cdce_start(struct ifnet *ifp) 338 { 339 struct cdce_softc *sc = ifp->if_softc; 340 struct mbuf *m_head = NULL; 341 342 if (sc->cdce_dying || (ifp->if_flags & IFF_OACTIVE)) 343 return; 344 345 IFQ_POLL(&ifp->if_snd, m_head); 346 if (m_head == NULL) 347 return; 348 349 if (cdce_encap(sc, m_head, 0)) { 350 ifp->if_flags |= IFF_OACTIVE; 351 return; 352 } 353 354 IFQ_DEQUEUE(&ifp->if_snd, m_head); 355 356 bpf_mtap(ifp, m_head); 357 358 ifp->if_flags |= IFF_OACTIVE; 359 360 ifp->if_timer = 6; 361 } 362 363 Static int 364 cdce_encap(struct cdce_softc *sc, struct mbuf *m, int idx) 365 { 366 struct cdce_chain *c; 367 usbd_status err; 368 int extra = 0; 369 370 c = &sc->cdce_cdata.cdce_tx_chain[idx]; 371 372 m_copydata(m, 0, m->m_pkthdr.len, c->cdce_buf); 373 if (sc->cdce_flags & CDCE_ZAURUS) { 374 /* Zaurus wants a 32-bit CRC appended to every frame */ 375 uint32_t crc; 376 377 crc = htole32(~ether_crc32_le(c->cdce_buf, m->m_pkthdr.len)); 378 memcpy(c->cdce_buf + m->m_pkthdr.len, &crc, sizeof(crc)); 379 extra = sizeof(crc); 380 } 381 c->cdce_mbuf = m; 382 383 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkout_pipe, c, c->cdce_buf, 384 m->m_pkthdr.len + extra, USBD_FORCE_SHORT_XFER | USBD_NO_COPY, 385 10000, cdce_txeof); 386 err = usbd_transfer(c->cdce_xfer); 387 if (err != USBD_IN_PROGRESS) { 388 cdce_stop(sc); 389 return (EIO); 390 } 391 392 sc->cdce_cdata.cdce_tx_cnt++; 393 394 return (0); 395 } 396 397 Static void 398 cdce_stop(struct cdce_softc *sc) 399 { 400 usbd_status err; 401 struct ifnet *ifp = GET_IFP(sc); 402 int i; 403 404 ifp->if_timer = 0; 405 406 if (sc->cdce_bulkin_pipe != NULL) { 407 err = usbd_abort_pipe(sc->cdce_bulkin_pipe); 408 if (err) 409 printf("%s: abort rx pipe failed: %s\n", 410 device_xname(sc->cdce_dev), usbd_errstr(err)); 411 err = usbd_close_pipe(sc->cdce_bulkin_pipe); 412 if (err) 413 printf("%s: close rx pipe failed: %s\n", 414 device_xname(sc->cdce_dev), usbd_errstr(err)); 415 sc->cdce_bulkin_pipe = NULL; 416 } 417 418 if (sc->cdce_bulkout_pipe != NULL) { 419 err = usbd_abort_pipe(sc->cdce_bulkout_pipe); 420 if (err) 421 printf("%s: abort tx pipe failed: %s\n", 422 device_xname(sc->cdce_dev), usbd_errstr(err)); 423 err = usbd_close_pipe(sc->cdce_bulkout_pipe); 424 if (err) 425 printf("%s: close tx pipe failed: %s\n", 426 device_xname(sc->cdce_dev), usbd_errstr(err)); 427 sc->cdce_bulkout_pipe = NULL; 428 } 429 430 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 431 if (sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf != NULL) { 432 m_freem(sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf); 433 sc->cdce_cdata.cdce_rx_chain[i].cdce_mbuf = NULL; 434 } 435 if (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer != NULL) { 436 usbd_free_xfer 437 (sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer); 438 sc->cdce_cdata.cdce_rx_chain[i].cdce_xfer = NULL; 439 } 440 } 441 442 for (i = 0; i < CDCE_TX_LIST_CNT; i++) { 443 if (sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf != NULL) { 444 m_freem(sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf); 445 sc->cdce_cdata.cdce_tx_chain[i].cdce_mbuf = NULL; 446 } 447 if (sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer != NULL) { 448 usbd_free_xfer( 449 sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer); 450 sc->cdce_cdata.cdce_tx_chain[i].cdce_xfer = NULL; 451 } 452 } 453 454 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 455 } 456 457 Static int 458 cdce_ioctl(struct ifnet *ifp, u_long command, void *data) 459 { 460 struct cdce_softc *sc = ifp->if_softc; 461 struct ifaddr *ifa = (struct ifaddr *)data; 462 struct ifreq *ifr = (struct ifreq *)data; 463 int s, error = 0; 464 465 if (sc->cdce_dying) 466 return (EIO); 467 468 s = splnet(); 469 470 switch(command) { 471 case SIOCINITIFADDR: 472 ifp->if_flags |= IFF_UP; 473 cdce_init(sc); 474 switch (ifa->ifa_addr->sa_family) { 475 #ifdef INET 476 case AF_INET: 477 arp_ifinit(ifp, ifa); 478 break; 479 #endif /* INET */ 480 } 481 break; 482 483 case SIOCSIFMTU: 484 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > ETHERMTU) 485 error = EINVAL; 486 else if ((error = ifioctl_common(ifp, command, data)) == ENETRESET) 487 error = 0; 488 break; 489 490 case SIOCSIFFLAGS: 491 if ((error = ifioctl_common(ifp, command, data)) != 0) 492 break; 493 /* XXX re-use ether_ioctl() */ 494 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) { 495 case IFF_UP: 496 cdce_init(sc); 497 break; 498 case IFF_RUNNING: 499 cdce_stop(sc); 500 break; 501 default: 502 break; 503 } 504 break; 505 506 default: 507 error = ether_ioctl(ifp, command, data); 508 break; 509 } 510 511 splx(s); 512 513 if (error == ENETRESET) 514 error = 0; 515 516 return (error); 517 } 518 519 Static void 520 cdce_watchdog(struct ifnet *ifp) 521 { 522 struct cdce_softc *sc = ifp->if_softc; 523 524 if (sc->cdce_dying) 525 return; 526 527 ifp->if_oerrors++; 528 printf("%s: watchdog timeout\n", device_xname(sc->cdce_dev)); 529 } 530 531 Static void 532 cdce_init(void *xsc) 533 { 534 struct cdce_softc *sc = xsc; 535 struct ifnet *ifp = GET_IFP(sc); 536 struct cdce_chain *c; 537 usbd_status err; 538 int s, i; 539 540 if (ifp->if_flags & IFF_RUNNING) 541 return; 542 543 s = splnet(); 544 545 if (cdce_tx_list_init(sc) == ENOBUFS) { 546 printf("%s: tx list init failed\n", device_xname(sc->cdce_dev)); 547 splx(s); 548 return; 549 } 550 551 if (cdce_rx_list_init(sc) == ENOBUFS) { 552 printf("%s: rx list init failed\n", device_xname(sc->cdce_dev)); 553 splx(s); 554 return; 555 } 556 557 /* Maybe set multicast / broadcast here??? */ 558 559 err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkin_no, 560 USBD_EXCLUSIVE_USE, &sc->cdce_bulkin_pipe); 561 if (err) { 562 printf("%s: open rx pipe failed: %s\n", device_xname(sc->cdce_dev), 563 usbd_errstr(err)); 564 splx(s); 565 return; 566 } 567 568 err = usbd_open_pipe(sc->cdce_data_iface, sc->cdce_bulkout_no, 569 USBD_EXCLUSIVE_USE, &sc->cdce_bulkout_pipe); 570 if (err) { 571 printf("%s: open tx pipe failed: %s\n", 572 device_xname(sc->cdce_dev), usbd_errstr(err)); 573 splx(s); 574 return; 575 } 576 577 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 578 c = &sc->cdce_cdata.cdce_rx_chain[i]; 579 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, 580 c->cdce_buf, CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, 581 USBD_NO_TIMEOUT, cdce_rxeof); 582 usbd_transfer(c->cdce_xfer); 583 } 584 585 ifp->if_flags |= IFF_RUNNING; 586 ifp->if_flags &= ~IFF_OACTIVE; 587 588 splx(s); 589 } 590 591 Static int 592 cdce_newbuf(struct cdce_softc *sc, struct cdce_chain *c, struct mbuf *m) 593 { 594 struct mbuf *m_new = NULL; 595 596 if (m == NULL) { 597 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 598 if (m_new == NULL) { 599 printf("%s: no memory for rx list " 600 "-- packet dropped!\n", device_xname(sc->cdce_dev)); 601 return (ENOBUFS); 602 } 603 MCLGET(m_new, M_DONTWAIT); 604 if (!(m_new->m_flags & M_EXT)) { 605 printf("%s: no memory for rx list " 606 "-- packet dropped!\n", device_xname(sc->cdce_dev)); 607 m_freem(m_new); 608 return (ENOBUFS); 609 } 610 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 611 } else { 612 m_new = m; 613 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 614 m_new->m_data = m_new->m_ext.ext_buf; 615 } 616 c->cdce_mbuf = m_new; 617 return (0); 618 } 619 620 Static int 621 cdce_rx_list_init(struct cdce_softc *sc) 622 { 623 struct cdce_cdata *cd; 624 struct cdce_chain *c; 625 int i; 626 627 cd = &sc->cdce_cdata; 628 for (i = 0; i < CDCE_RX_LIST_CNT; i++) { 629 c = &cd->cdce_rx_chain[i]; 630 c->cdce_sc = sc; 631 c->cdce_idx = i; 632 if (cdce_newbuf(sc, c, NULL) == ENOBUFS) 633 return (ENOBUFS); 634 if (c->cdce_xfer == NULL) { 635 c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev); 636 if (c->cdce_xfer == NULL) 637 return (ENOBUFS); 638 c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, 639 CDCE_BUFSZ); 640 if (c->cdce_buf == NULL) 641 return (ENOBUFS); 642 } 643 } 644 645 return (0); 646 } 647 648 Static int 649 cdce_tx_list_init(struct cdce_softc *sc) 650 { 651 struct cdce_cdata *cd; 652 struct cdce_chain *c; 653 int i; 654 655 cd = &sc->cdce_cdata; 656 for (i = 0; i < CDCE_TX_LIST_CNT; i++) { 657 c = &cd->cdce_tx_chain[i]; 658 c->cdce_sc = sc; 659 c->cdce_idx = i; 660 c->cdce_mbuf = NULL; 661 if (c->cdce_xfer == NULL) { 662 c->cdce_xfer = usbd_alloc_xfer(sc->cdce_udev); 663 if (c->cdce_xfer == NULL) 664 return (ENOBUFS); 665 c->cdce_buf = usbd_alloc_buffer(c->cdce_xfer, CDCE_BUFSZ); 666 if (c->cdce_buf == NULL) 667 return (ENOBUFS); 668 } 669 } 670 671 return (0); 672 } 673 674 Static void 675 cdce_rxeof(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status) 676 { 677 struct cdce_chain *c = priv; 678 struct cdce_softc *sc = c->cdce_sc; 679 struct ifnet *ifp = GET_IFP(sc); 680 struct mbuf *m; 681 int total_len = 0; 682 int s; 683 684 if (sc->cdce_dying || !(ifp->if_flags & IFF_RUNNING)) 685 return; 686 687 if (status != USBD_NORMAL_COMPLETION) { 688 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) 689 return; 690 if (sc->cdce_rxeof_errors == 0) 691 printf("%s: usb error on rx: %s\n", 692 device_xname(sc->cdce_dev), usbd_errstr(status)); 693 if (status == USBD_STALLED) 694 usbd_clear_endpoint_stall_async(sc->cdce_bulkin_pipe); 695 DELAY(sc->cdce_rxeof_errors * 10000); 696 sc->cdce_rxeof_errors++; 697 goto done; 698 } 699 700 sc->cdce_rxeof_errors = 0; 701 702 usbd_get_xfer_status(xfer, NULL, NULL, &total_len, NULL); 703 if (sc->cdce_flags & CDCE_ZAURUS) 704 total_len -= 4; /* Strip off CRC added by Zaurus */ 705 if (total_len <= 1) 706 goto done; 707 708 m = c->cdce_mbuf; 709 memcpy(mtod(m, char *), c->cdce_buf, total_len); 710 711 if (total_len < sizeof(struct ether_header)) { 712 ifp->if_ierrors++; 713 goto done; 714 } 715 716 ifp->if_ipackets++; 717 m->m_pkthdr.len = m->m_len = total_len; 718 m->m_pkthdr.rcvif = ifp; 719 720 s = splnet(); 721 722 if (cdce_newbuf(sc, c, NULL) == ENOBUFS) { 723 ifp->if_ierrors++; 724 goto done1; 725 } 726 727 bpf_mtap(ifp, m); 728 729 (*(ifp)->if_input)((ifp), (m)); 730 731 done1: 732 splx(s); 733 734 done: 735 /* Setup new transfer. */ 736 usbd_setup_xfer(c->cdce_xfer, sc->cdce_bulkin_pipe, c, c->cdce_buf, 737 CDCE_BUFSZ, USBD_SHORT_XFER_OK | USBD_NO_COPY, USBD_NO_TIMEOUT, 738 cdce_rxeof); 739 usbd_transfer(c->cdce_xfer); 740 } 741 742 Static void 743 cdce_txeof(usbd_xfer_handle xfer, usbd_private_handle priv, 744 usbd_status status) 745 { 746 struct cdce_chain *c = priv; 747 struct cdce_softc *sc = c->cdce_sc; 748 struct ifnet *ifp = GET_IFP(sc); 749 usbd_status err; 750 int s; 751 752 if (sc->cdce_dying) 753 return; 754 755 s = splnet(); 756 757 ifp->if_timer = 0; 758 ifp->if_flags &= ~IFF_OACTIVE; 759 760 if (status != USBD_NORMAL_COMPLETION) { 761 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED) { 762 splx(s); 763 return; 764 } 765 ifp->if_oerrors++; 766 printf("%s: usb error on tx: %s\n", device_xname(sc->cdce_dev), 767 usbd_errstr(status)); 768 if (status == USBD_STALLED) 769 usbd_clear_endpoint_stall_async(sc->cdce_bulkout_pipe); 770 splx(s); 771 return; 772 } 773 774 usbd_get_xfer_status(c->cdce_xfer, NULL, NULL, NULL, &err); 775 776 if (c->cdce_mbuf != NULL) { 777 m_freem(c->cdce_mbuf); 778 c->cdce_mbuf = NULL; 779 } 780 781 if (err) 782 ifp->if_oerrors++; 783 else 784 ifp->if_opackets++; 785 786 if (IFQ_IS_EMPTY(&ifp->if_snd) == 0) 787 cdce_start(ifp); 788 789 splx(s); 790 } 791 792 int 793 cdce_activate(device_t self, enum devact act) 794 { 795 struct cdce_softc *sc = device_private(self); 796 797 switch (act) { 798 case DVACT_DEACTIVATE: 799 if_deactivate(GET_IFP(sc)); 800 sc->cdce_dying = 1; 801 return 0; 802 default: 803 return EOPNOTSUPP; 804 } 805 } 806