1 /* 2 * Copyright (c) 2013 Gerhard Sittig <gsi@denx.de> 3 * based on the U-Boot Asix driver as well as information 4 * from the Linux Moschip driver 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 /* 10 * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices 11 */ 12 13 #include <common.h> 14 #include <errno.h> 15 #include <linux/mii.h> 16 #include <malloc.h> 17 #include <usb.h> 18 19 #include "usb_ether.h" 20 21 #define MCS7830_BASE_NAME "mcs" 22 23 #define USBCALL_TIMEOUT 1000 24 #define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */ 25 #define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */ 26 27 #define MCS7830_RX_URB_SIZE 2048 28 29 /* command opcodes */ 30 #define MCS7830_WR_BREQ 0x0d 31 #define MCS7830_RD_BREQ 0x0e 32 33 /* register layout, numerical offset specs for USB API calls */ 34 struct mcs7830_regs { 35 uint8_t multicast_hashes[8]; 36 uint8_t packet_gap[2]; 37 uint8_t phy_data[2]; 38 uint8_t phy_command[2]; 39 uint8_t configuration; 40 uint8_t ether_address[6]; 41 uint8_t frame_drop_count; 42 uint8_t pause_threshold; 43 }; 44 #define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes) 45 #define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data) 46 #define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command) 47 #define REG_CONFIG offsetof(struct mcs7830_regs, configuration) 48 #define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address) 49 #define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count) 50 #define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold) 51 52 /* bit masks and default values for the above registers */ 53 #define PHY_CMD1_READ 0x40 54 #define PHY_CMD1_WRITE 0x20 55 #define PHY_CMD1_PHYADDR 0x01 56 57 #define PHY_CMD2_PEND 0x80 58 #define PHY_CMD2_READY 0x40 59 60 #define CONF_CFG 0x80 61 #define CONF_SPEED100 0x40 62 #define CONF_FDX_ENABLE 0x20 63 #define CONF_RXENABLE 0x10 64 #define CONF_TXENABLE 0x08 65 #define CONF_SLEEPMODE 0x04 66 #define CONF_ALLMULTICAST 0x02 67 #define CONF_PROMISCUOUS 0x01 68 69 #define PAUSE_THRESHOLD_DEFAULT 0 70 71 /* bit masks for the status byte which follows received ethernet frames */ 72 #define STAT_RX_FRAME_CORRECT 0x20 73 #define STAT_RX_LARGE_FRAME 0x10 74 #define STAT_RX_CRC_ERROR 0x08 75 #define STAT_RX_ALIGNMENT_ERROR 0x04 76 #define STAT_RX_LENGTH_ERROR 0x02 77 #define STAT_RX_SHORT_FRAME 0x01 78 79 /* 80 * struct mcs7830_private - private driver data for an individual adapter 81 * @config: shadow for the network adapter's configuration register 82 * @mchash: shadow for the network adapter's multicast hash registers 83 */ 84 struct mcs7830_private { 85 uint8_t config; 86 uint8_t mchash[8]; 87 }; 88 89 /* 90 * mcs7830_read_reg() - read a register of the network adapter 91 * @dev: network device to read from 92 * @idx: index of the register to start reading from 93 * @size: number of bytes to read 94 * @data: buffer to read into 95 * Return: zero upon success, negative upon error 96 */ 97 static int mcs7830_read_reg(struct ueth_data *dev, uint8_t idx, 98 uint16_t size, void *data) 99 { 100 int len; 101 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); 102 103 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); 104 105 len = usb_control_msg(dev->pusb_dev, 106 usb_rcvctrlpipe(dev->pusb_dev, 0), 107 MCS7830_RD_BREQ, 108 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 109 0, idx, buf, size, 110 USBCALL_TIMEOUT); 111 if (len != size) { 112 debug("%s() len=%d != sz=%d\n", __func__, len, size); 113 return -EIO; 114 } 115 memcpy(data, buf, size); 116 return 0; 117 } 118 119 /* 120 * mcs7830_write_reg() - write a register of the network adapter 121 * @dev: network device to write to 122 * @idx: index of the register to start writing to 123 * @size: number of bytes to write 124 * @data: buffer holding the data to write 125 * Return: zero upon success, negative upon error 126 */ 127 static int mcs7830_write_reg(struct ueth_data *dev, uint8_t idx, 128 uint16_t size, void *data) 129 { 130 int len; 131 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); 132 133 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); 134 135 memcpy(buf, data, size); 136 len = usb_control_msg(dev->pusb_dev, 137 usb_sndctrlpipe(dev->pusb_dev, 0), 138 MCS7830_WR_BREQ, 139 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, 140 0, idx, buf, size, 141 USBCALL_TIMEOUT); 142 if (len != size) { 143 debug("%s() len=%d != sz=%d\n", __func__, len, size); 144 return -EIO; 145 } 146 return 0; 147 } 148 149 /* 150 * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution 151 * @dev: network device to talk to 152 * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode 153 * @index: number of the PHY register to read or write 154 * Return: zero upon success, negative upon error 155 */ 156 static int mcs7830_phy_emit_wait(struct ueth_data *dev, 157 uint8_t rwflag, uint8_t index) 158 { 159 int rc; 160 int retry; 161 uint8_t cmd[2]; 162 163 /* send the PHY read/write request */ 164 cmd[0] = rwflag | PHY_CMD1_PHYADDR; 165 cmd[1] = PHY_CMD2_PEND | (index & 0x1f); 166 rc = mcs7830_write_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd); 167 if (rc < 0) 168 return rc; 169 170 /* wait for the response to become available (usually < 1ms) */ 171 retry = 10; 172 do { 173 rc = mcs7830_read_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd); 174 if (rc < 0) 175 return rc; 176 if (cmd[1] & PHY_CMD2_READY) 177 return 0; 178 if (!retry--) 179 return -ETIMEDOUT; 180 mdelay(1); 181 } while (1); 182 /* UNREACH */ 183 } 184 185 /* 186 * mcs7830_read_phy() - read a PHY register of the network adapter 187 * @dev: network device to read from 188 * @index: index of the PHY register to read from 189 * Return: non-negative 16bit register content, negative upon error 190 */ 191 static int mcs7830_read_phy(struct ueth_data *dev, uint8_t index) 192 { 193 int rc; 194 uint16_t val; 195 196 /* issue the PHY read request and wait for its execution */ 197 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_READ, index); 198 if (rc < 0) 199 return rc; 200 201 /* fetch the PHY data which was read */ 202 rc = mcs7830_read_reg(dev, REG_PHY_DATA, sizeof(val), &val); 203 if (rc < 0) 204 return rc; 205 rc = le16_to_cpu(val); 206 debug("%s(%s, %d) => 0x%04X\n", __func__, dev->eth_dev.name, index, rc); 207 return rc; 208 } 209 210 /* 211 * mcs7830_write_phy() - write a PHY register of the network adapter 212 * @dev: network device to write to 213 * @index: index of the PHY register to write to 214 * @val: value to write to the PHY register 215 * Return: zero upon success, negative upon error 216 */ 217 static int mcs7830_write_phy(struct ueth_data *dev, uint8_t index, uint16_t val) 218 { 219 int rc; 220 221 debug("%s(%s, %d, 0x%04X)\n", __func__, dev->eth_dev.name, index, val); 222 223 /* setup the PHY data which is to get written */ 224 val = cpu_to_le16(val); 225 rc = mcs7830_write_reg(dev, REG_PHY_DATA, sizeof(val), &val); 226 if (rc < 0) 227 return rc; 228 229 /* issue the PHY write request and wait for its execution */ 230 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_WRITE, index); 231 if (rc < 0) 232 return rc; 233 234 return 0; 235 } 236 237 /* 238 * mcs7830_write_config() - write to the network adapter's config register 239 * @eth: network device to write to 240 * Return: zero upon success, negative upon error 241 * 242 * the data which gets written is taken from the shadow config register 243 * within the device driver's private data 244 */ 245 static int mcs7830_write_config(struct ueth_data *dev) 246 { 247 struct mcs7830_private *priv; 248 int rc; 249 250 debug("%s()\n", __func__); 251 priv = dev->dev_priv; 252 253 rc = mcs7830_write_reg(dev, REG_CONFIG, 254 sizeof(priv->config), &priv->config); 255 if (rc < 0) { 256 debug("writing config to adapter failed\n"); 257 return rc; 258 } 259 260 return 0; 261 } 262 263 /* 264 * mcs7830_write_mchash() - write the network adapter's multicast filter 265 * @eth: network device to write to 266 * Return: zero upon success, negative upon error 267 * 268 * the data which gets written is taken from the shadow multicast hashes 269 * within the device driver's private data 270 */ 271 static int mcs7830_write_mchash(struct ueth_data *dev) 272 { 273 struct mcs7830_private *priv; 274 int rc; 275 276 debug("%s()\n", __func__); 277 priv = dev->dev_priv; 278 279 rc = mcs7830_write_reg(dev, REG_MULTICAST_HASH, 280 sizeof(priv->mchash), &priv->mchash); 281 if (rc < 0) { 282 debug("writing multicast hash to adapter failed\n"); 283 return rc; 284 } 285 286 return 0; 287 } 288 289 /* 290 * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation 291 * @eth: network device to run link negotiation on 292 * Return: zero upon success, negative upon error 293 * 294 * the routine advertises available media and starts autonegotiation 295 */ 296 static int mcs7830_set_autoneg(struct ueth_data *dev) 297 { 298 int adv, flg; 299 int rc; 300 301 debug("%s()\n", __func__); 302 303 /* 304 * algorithm taken from the Linux driver, which took it from 305 * "the original mcs7830 version 1.4 driver": 306 * 307 * enable all media, reset BMCR, enable auto neg, restart 308 * auto neg while keeping the enable auto neg flag set 309 */ 310 311 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA; 312 rc = mcs7830_write_phy(dev, MII_ADVERTISE, adv); 313 314 flg = 0; 315 if (!rc) 316 rc = mcs7830_write_phy(dev, MII_BMCR, flg); 317 318 flg |= BMCR_ANENABLE; 319 if (!rc) 320 rc = mcs7830_write_phy(dev, MII_BMCR, flg); 321 322 flg |= BMCR_ANRESTART; 323 if (!rc) 324 rc = mcs7830_write_phy(dev, MII_BMCR, flg); 325 326 return rc; 327 } 328 329 /* 330 * mcs7830_get_rev() - identify a network adapter's chip revision 331 * @eth: network device to identify 332 * Return: non-negative number, reflecting the revision number 333 * 334 * currently, only "rev C and higher" and "below rev C" are needed, so 335 * the return value is #1 for "below rev C", and #2 for "rev C and above" 336 */ 337 static int mcs7830_get_rev(struct ueth_data *dev) 338 { 339 uint8_t buf[2]; 340 int rc; 341 int rev; 342 343 /* register 22 is readable in rev C and higher */ 344 rc = mcs7830_read_reg(dev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf); 345 if (rc < 0) 346 rev = 1; 347 else 348 rev = 2; 349 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev); 350 return rev; 351 } 352 353 /* 354 * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups 355 * @eth: network device to identify and apply fixups to 356 * Return: zero upon success (no errors emitted from here) 357 * 358 * this routine identifies the network adapter's chip revision, and applies 359 * fixups for known issues 360 */ 361 static int mcs7830_apply_fixup(struct ueth_data *dev) 362 { 363 int rev; 364 int i; 365 uint8_t thr; 366 367 rev = mcs7830_get_rev(dev); 368 debug("%s() rev=%d\n", __func__, rev); 369 370 /* 371 * rev C requires setting the pause threshold (the Linux driver 372 * is inconsistent, the implementation does it for "rev C 373 * exactly", the introductory comment says "rev C and above") 374 */ 375 if (rev == 2) { 376 debug("%s: applying rev C fixup\n", dev->eth_dev.name); 377 thr = PAUSE_THRESHOLD_DEFAULT; 378 for (i = 0; i < 2; i++) { 379 (void)mcs7830_write_reg(dev, REG_PAUSE_THRESHOLD, 380 sizeof(thr), &thr); 381 mdelay(1); 382 } 383 } 384 385 return 0; 386 } 387 388 /* 389 * mcs7830_basic_reset() - bring the network adapter into a known first state 390 * @eth: network device to act upon 391 * Return: zero upon success, negative upon error 392 * 393 * this routine initializes the network adapter such that subsequent invocations 394 * of the interface callbacks can exchange ethernet frames; link negotiation is 395 * triggered from here already and continues in background 396 */ 397 static int mcs7830_basic_reset(struct ueth_data *dev) 398 { 399 struct mcs7830_private *priv; 400 int rc; 401 402 debug("%s()\n", __func__); 403 priv = dev->dev_priv; 404 405 /* 406 * comment from the respective Linux driver, which 407 * unconditionally sets the ALLMULTICAST flag as well: 408 * should not be needed, but does not work otherwise 409 */ 410 priv->config = CONF_TXENABLE; 411 priv->config |= CONF_ALLMULTICAST; 412 413 rc = mcs7830_set_autoneg(dev); 414 if (rc < 0) { 415 error("setting autoneg failed\n"); 416 return rc; 417 } 418 419 rc = mcs7830_write_mchash(dev); 420 if (rc < 0) { 421 error("failed to set multicast hash\n"); 422 return rc; 423 } 424 425 rc = mcs7830_write_config(dev); 426 if (rc < 0) { 427 error("failed to set configuration\n"); 428 return rc; 429 } 430 431 rc = mcs7830_apply_fixup(dev); 432 if (rc < 0) { 433 error("fixup application failed\n"); 434 return rc; 435 } 436 437 return 0; 438 } 439 440 /* 441 * mcs7830_read_mac() - read an ethernet adapter's MAC address 442 * @eth: network device to read from 443 * Return: zero upon success, negative upon error 444 * 445 * this routine fetches the MAC address stored within the ethernet adapter, 446 * and stores it in the ethernet interface's data structure 447 */ 448 static int mcs7830_read_mac(struct eth_device *eth) 449 { 450 struct ueth_data *dev; 451 int rc; 452 uint8_t buf[ETH_ALEN]; 453 454 debug("%s()\n", __func__); 455 dev = eth->priv; 456 457 rc = mcs7830_read_reg(dev, REG_ETHER_ADDR, ETH_ALEN, buf); 458 if (rc < 0) { 459 debug("reading MAC from adapter failed\n"); 460 return rc; 461 } 462 463 memcpy(ð->enetaddr[0], buf, ETH_ALEN); 464 return 0; 465 } 466 467 /* 468 * mcs7830_write_mac() - write an ethernet adapter's MAC address 469 * @eth: network device to write to 470 * Return: zero upon success, negative upon error 471 * 472 * this routine takes the MAC address from the ethernet interface's data 473 * structure, and writes it into the ethernet adapter such that subsequent 474 * exchange of ethernet frames uses this address 475 */ 476 static int mcs7830_write_mac(struct eth_device *eth) 477 { 478 struct ueth_data *dev; 479 int rc; 480 481 debug("%s()\n", __func__); 482 dev = eth->priv; 483 484 if (sizeof(eth->enetaddr) != ETH_ALEN) 485 return -EINVAL; 486 rc = mcs7830_write_reg(dev, REG_ETHER_ADDR, ETH_ALEN, eth->enetaddr); 487 if (rc < 0) { 488 debug("writing MAC to adapter failed\n"); 489 return rc; 490 } 491 return 0; 492 } 493 494 /* 495 * mcs7830_init() - network interface's init callback 496 * @eth: network device to initialize 497 * @bd: board information 498 * Return: zero upon success, negative upon error 499 * 500 * after initial setup during probe() and get_info(), this init() callback 501 * ensures that the link is up and subsequent send() and recv() calls can 502 * exchange ethernet frames 503 */ 504 static int mcs7830_init(struct eth_device *eth, bd_t *bd) 505 { 506 struct ueth_data *dev; 507 int timeout; 508 int have_link; 509 510 debug("%s()\n", __func__); 511 dev = eth->priv; 512 513 timeout = 0; 514 do { 515 have_link = mcs7830_read_phy(dev, MII_BMSR) & BMSR_LSTATUS; 516 if (have_link) 517 break; 518 udelay(LINKSTATUS_TIMEOUT_RES * 1000); 519 timeout += LINKSTATUS_TIMEOUT_RES; 520 } while (timeout < LINKSTATUS_TIMEOUT); 521 if (!have_link) { 522 debug("ethernet link is down\n"); 523 return -ETIMEDOUT; 524 } 525 return 0; 526 } 527 528 /* 529 * mcs7830_send() - network interface's send callback 530 * @eth: network device to send the frame from 531 * @packet: ethernet frame content 532 * @length: ethernet frame length 533 * Return: zero upon success, negative upon error 534 * 535 * this routine send an ethernet frame out of the network interface 536 */ 537 static int mcs7830_send(struct eth_device *eth, void *packet, int length) 538 { 539 struct ueth_data *dev; 540 int rc; 541 int gotlen; 542 /* there is a status byte after the ethernet frame */ 543 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t)); 544 545 dev = eth->priv; 546 547 memcpy(buf, packet, length); 548 rc = usb_bulk_msg(dev->pusb_dev, 549 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out), 550 &buf[0], length, &gotlen, 551 USBCALL_TIMEOUT); 552 debug("%s() TX want len %d, got len %d, rc %d\n", 553 __func__, length, gotlen, rc); 554 return rc; 555 } 556 557 /* 558 * mcs7830_recv() - network interface's recv callback 559 * @eth: network device to receive frames from 560 * Return: zero upon success, negative upon error 561 * 562 * this routine checks for available ethernet frames that the network 563 * interface might have received, and notifies the network stack 564 */ 565 static int mcs7830_recv(struct eth_device *eth) 566 { 567 struct ueth_data *dev; 568 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE); 569 int rc, wantlen, gotlen; 570 uint8_t sts; 571 572 debug("%s()\n", __func__); 573 dev = eth->priv; 574 575 /* fetch input data from the adapter */ 576 wantlen = MCS7830_RX_URB_SIZE; 577 rc = usb_bulk_msg(dev->pusb_dev, 578 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in), 579 &buf[0], wantlen, &gotlen, 580 USBCALL_TIMEOUT); 581 debug("%s() RX want len %d, got len %d, rc %d\n", 582 __func__, wantlen, gotlen, rc); 583 if (rc != 0) { 584 error("RX: failed to receive\n"); 585 return rc; 586 } 587 if (gotlen > wantlen) { 588 error("RX: got too many bytes (%d)\n", gotlen); 589 return -EIO; 590 } 591 592 /* 593 * the bulk message that we received from USB contains exactly 594 * one ethernet frame and a trailing status byte 595 */ 596 if (gotlen < sizeof(sts)) 597 return -EIO; 598 gotlen -= sizeof(sts); 599 sts = buf[gotlen]; 600 601 if (sts == STAT_RX_FRAME_CORRECT) { 602 debug("%s() got a frame, len=%d\n", __func__, gotlen); 603 NetReceive(buf, gotlen); 604 return 0; 605 } 606 607 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n", 608 sts, 609 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-", 610 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-", 611 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-", 612 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-", 613 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-"); 614 return -EIO; 615 } 616 617 /* 618 * mcs7830_halt() - network interface's halt callback 619 * @eth: network device to cease operation of 620 * Return: none 621 * 622 * this routine is supposed to undo the effect of previous initialization and 623 * ethernet frames exchange; in this implementation it's a NOP 624 */ 625 static void mcs7830_halt(struct eth_device *eth) 626 { 627 debug("%s()\n", __func__); 628 } 629 630 /* 631 * mcs7830_iface_idx - index of detected network interfaces 632 * 633 * this counter keeps track of identified supported interfaces, 634 * to assign unique names as more interfaces are found 635 */ 636 static int mcs7830_iface_idx; 637 638 /* 639 * mcs7830_eth_before_probe() - network driver's before_probe callback 640 * Return: none 641 * 642 * this routine initializes driver's internal data in preparation of 643 * subsequent probe callbacks 644 */ 645 void mcs7830_eth_before_probe(void) 646 { 647 mcs7830_iface_idx = 0; 648 } 649 650 /* 651 * struct mcs7830_dongle - description of a supported Moschip ethernet dongle 652 * @vendor: 16bit USB vendor identification 653 * @product: 16bit USB product identification 654 * 655 * this structure describes a supported USB ethernet dongle by means of the 656 * vendor and product codes found during USB enumeration; no flags are held 657 * here since all supported dongles have identical behaviour, and required 658 * fixups get determined at runtime, such that no manual configuration is 659 * needed 660 */ 661 struct mcs7830_dongle { 662 uint16_t vendor; 663 uint16_t product; 664 }; 665 666 /* 667 * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles 668 */ 669 static const struct mcs7830_dongle const mcs7830_dongles[] = { 670 { 0x9710, 0x7832, }, /* Moschip 7832 */ 671 { 0x9710, 0x7830, }, /* Moschip 7830 */ 672 { 0x9710, 0x7730, }, /* Moschip 7730 */ 673 { 0x0df6, 0x0021, }, /* Sitecom LN 30 */ 674 }; 675 676 /* 677 * mcs7830_eth_probe() - network driver's probe callback 678 * @dev: detected USB device to check 679 * @ifnum: detected USB interface to check 680 * @ss: USB ethernet data structure to fill in upon match 681 * Return: #1 upon match, #0 upon mismatch or error 682 * 683 * this routine checks whether the found USB device is supported by 684 * this ethernet driver, and upon match fills in the USB ethernet 685 * data structure which later is passed to the get_info callback 686 */ 687 int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum, 688 struct ueth_data *ss) 689 { 690 struct usb_interface *iface; 691 struct usb_interface_descriptor *iface_desc; 692 int i; 693 struct mcs7830_private *priv; 694 int ep_in_found, ep_out_found, ep_intr_found; 695 696 debug("%s()\n", __func__); 697 698 /* iterate the list of supported dongles */ 699 iface = &dev->config.if_desc[ifnum]; 700 iface_desc = &iface->desc; 701 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) { 702 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor && 703 dev->descriptor.idProduct == mcs7830_dongles[i].product) 704 break; 705 } 706 if (i == ARRAY_SIZE(mcs7830_dongles)) 707 return 0; 708 debug("detected USB ethernet device: %04X:%04X\n", 709 dev->descriptor.idVendor, dev->descriptor.idProduct); 710 711 /* fill in driver private data */ 712 priv = calloc(1, sizeof(*priv)); 713 if (!priv) 714 return 0; 715 716 /* fill in the ueth_data structure, attach private data */ 717 memset(ss, 0, sizeof(*ss)); 718 ss->ifnum = ifnum; 719 ss->pusb_dev = dev; 720 ss->subclass = iface_desc->bInterfaceSubClass; 721 ss->protocol = iface_desc->bInterfaceProtocol; 722 ss->dev_priv = priv; 723 724 /* 725 * a minimum of three endpoints is expected: in (bulk), 726 * out (bulk), and interrupt; ignore all others 727 */ 728 ep_in_found = ep_out_found = ep_intr_found = 0; 729 for (i = 0; i < iface_desc->bNumEndpoints; i++) { 730 uint8_t eptype, epaddr; 731 bool is_input; 732 733 eptype = iface->ep_desc[i].bmAttributes; 734 eptype &= USB_ENDPOINT_XFERTYPE_MASK; 735 736 epaddr = iface->ep_desc[i].bEndpointAddress; 737 is_input = epaddr & USB_DIR_IN; 738 epaddr &= USB_ENDPOINT_NUMBER_MASK; 739 740 if (eptype == USB_ENDPOINT_XFER_BULK) { 741 if (is_input && !ep_in_found) { 742 ss->ep_in = epaddr; 743 ep_in_found++; 744 } 745 if (!is_input && !ep_out_found) { 746 ss->ep_out = epaddr; 747 ep_out_found++; 748 } 749 } 750 751 if (eptype == USB_ENDPOINT_XFER_INT) { 752 if (is_input && !ep_intr_found) { 753 ss->ep_int = epaddr; 754 ss->irqinterval = iface->ep_desc[i].bInterval; 755 ep_intr_found++; 756 } 757 } 758 } 759 debug("endpoints: in %d, out %d, intr %d\n", 760 ss->ep_in, ss->ep_out, ss->ep_int); 761 762 /* apply basic sanity checks */ 763 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) || 764 !ss->ep_in || !ss->ep_out || !ss->ep_int) { 765 debug("device probe incomplete\n"); 766 return 0; 767 } 768 769 dev->privptr = ss; 770 return 1; 771 } 772 773 /* 774 * mcs7830_eth_get_info() - network driver's get_info callback 775 * @dev: detected USB device 776 * @ss: USB ethernet data structure filled in at probe() 777 * @eth: ethernet interface data structure to fill in 778 * Return: #1 upon success, #0 upon error 779 * 780 * this routine registers the mandatory init(), send(), recv(), and 781 * halt() callbacks with the ethernet interface, can register the 782 * optional write_hwaddr() callback with the ethernet interface, 783 * and initiates configuration of the interface such that subsequent 784 * calls to those callbacks results in network communication 785 */ 786 int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss, 787 struct eth_device *eth) 788 { 789 debug("%s()\n", __func__); 790 if (!eth) { 791 debug("%s: missing parameter.\n", __func__); 792 return 0; 793 } 794 795 snprintf(eth->name, sizeof(eth->name), "%s%d", 796 MCS7830_BASE_NAME, mcs7830_iface_idx++); 797 eth->init = mcs7830_init; 798 eth->send = mcs7830_send; 799 eth->recv = mcs7830_recv; 800 eth->halt = mcs7830_halt; 801 eth->write_hwaddr = mcs7830_write_mac; 802 eth->priv = ss; 803 804 if (mcs7830_basic_reset(ss)) 805 return 0; 806 807 if (mcs7830_read_mac(eth)) 808 return 0; 809 debug("MAC %pM\n", eth->enetaddr); 810 811 return 1; 812 } 813