home *** CD-ROM | disk | FTP | other *** search
/ ftp.sustworks.com 2018 / ftp.sustworks.com.zip / ftp.sustworks.com / USBAx8817x_101.dmg / src / Source / USBAx8817x.h < prev    next >
C/C++ Source or Header  |  2005-10-08  |  14KB  |  385 lines

  1. /*
  2.     File:         USBAx8817x.h
  3.  
  4.     Description:  Driver for USBAx8817 USB-To-Ethernet devices
  5.  
  6.     Copyright:    Copyright 2005, Daniel Sumorok and Peter Sichel
  7.         
  8.     Disclaimer:   This program is free software; you can redistribute it
  9.                   and/or modify it under the terms of the GNU General
  10.                   Public License as published by the Free Software
  11.                   Foundation; either version 2, or (at your option)
  12.                   any later version.
  13.  
  14.                   This program is distributed in the hope that it will
  15.                   be useful, but WITHOUT ANY WARRANTY; without even
  16.                   the implied warranty of MERCHANTABILITY or FITNESS
  17.                   FOR A PARTICULAR PURPOSE.  See the GNU General
  18.                   Public License for more details.
  19.  
  20.                   You should have received a copy of the GNU General
  21.                   along with this program; if not, write to the Free
  22.                   Software Foundation, 59 Temple Place - Suite 330,
  23.                   Boston, MA 02111-1307, USA.  Change History (least
  24.                   recent first):
  25.  
  26.         Change History (most recent first):
  27.         
  28.             <1>         03/25/05        USBPegasusEthernet source
  29. */
  30.  
  31. /*
  32.  * Copyright (c) 1997, 1998, 1999, 2000-2003
  33.  *      Bill Paul <wpaul@windriver.com>.  All rights reserved.
  34.  *
  35.  * Redistribution and use in source and binary forms, with or without
  36.  * modification, are permitted provided that the following conditions
  37.  * are met:
  38.  * 1. Redistributions of source code must retain the above copyright
  39.  *    notice, this list of conditions and the following disclaimer.
  40.  * 2. Redistributions in binary form must reproduce the above copyright
  41.  *    notice, this list of conditions and the following disclaimer in the
  42.  *    documentation and/or other materials provided with the distribution.
  43.  * 3. All advertising materials mentioning features or use of this software
  44.  *    must display the following acknowledgement:
  45.  *      This product includes software developed by Bill Paul.
  46.  * 4. Neither the name of the author nor the names of any co-contributors
  47.  *    may be used to endorse or promote products derived from this software
  48.  *    without specific prior written permission.
  49.  *
  50.  */
  51.  
  52. #include <libkern/OSByteOrder.h>
  53.  
  54. #include <IOKit/network/IOEthernetController.h>
  55. #include <IOKit/network/IOEthernetInterface.h>
  56. #include <IOKit/network/IONetworkInterface.h>
  57. //#include <IOKit/network/IOBasicOutputQueue.h>
  58. #include <IOKit/network/IOOutputQueue.h>
  59. #include "AxOutputQueue.h"
  60. #include <IOKit/network/IOBasicOutputQueue.h>
  61.  
  62. #include <IOKit/IOTimerEventSource.h>
  63. #include <IOKit/assert.h>
  64. #include <IOKit/IOLib.h>
  65. #include <IOKit/IOService.h>
  66. #include <IOKit/IOBufferMemoryDescriptor.h>
  67. #include <IOKit/IOMessage.h>
  68.  
  69. #include <IOKit/pwr_mgt/RootDomain.h>
  70.  
  71. #include <IOKit/usb/IOUSBBus.h>
  72. #include <IOKit/usb/IOUSBNub.h>
  73. #include <IOKit/usb/IOUSBDevice.h>
  74. #include <IOKit/usb/IOUSBPipe.h>
  75. #include <IOKit/usb/USB.h>
  76. #include <IOKit/usb/IOUSBInterface.h>
  77.  
  78. #include <UserNotification/KUNCUserNotifications.h>
  79.  
  80. extern "C" {
  81. #include <sys/param.h>
  82. #if TIGER
  83.         #include <sys/kpi_mbuf.h>
  84. #else
  85.         //#include <sys/mbuf.h>
  86.         #include "mbufPanther.h"
  87. #endif
  88. }
  89.  
  90. /* The following code definitions were taken from the bsd source file:
  91.    if_axereg.h 
  92. */
  93. //#define BUFSIZE 1536
  94. #define BUFSIZE 2048
  95.  
  96. #define AXE_CMD_DIR(x)  (((x) & 0x0F00) >> 8)
  97. #define AXE_CMD_LEN(x)  (((x) & 0xF000) >> 12)
  98. #define AXE_CMD_CMD(x)  ((x) & 0x00FF)
  99.  
  100. #define ONE_TX_AT_A_TIME 0
  101. #define TRANSMIT_QUEUE_SIZE 256
  102. #define TX_RING_SIZE 24
  103. #define TX_QUEUE_RESTART 16
  104. #define NUM_RX_BUFFERS 24
  105.  
  106.  
  107. #define RXTHREAD_STOP           (1 << 0)
  108. #define RXTHREAD_STOPPED        (1 << 1)
  109. #define RXTHREAD_RESET          (1 << 2)
  110. #define RXTHREAD_CLOSE          (1 << 3)
  111. #define RXTHREAD_OPEN           (1 << 4)
  112. #define RXTHREAD_READSTALLED    (1 << 5)
  113. #define RXTHREAD_WRITESTALLED   (1 << 6)
  114. #define RXTHREAD_INTRSTALLED    (1 << 7)
  115. #define RXTHREAD_CTRLSTALLED    (1 << 8)
  116.  
  117. enum {
  118.   kUSBEthernetOffState = 0,
  119.   kUSBEthernetOnState = 1,
  120.   kNumPowerStates = 2
  121. };
  122.  
  123. typedef enum ctrlEventTag {
  124.   ctrlEventServiceQueue,
  125.   ctrlEventStart,
  126.   ctrlEventTimer,
  127.   ctrlEventChekcLink,
  128.   ctrlEventCallback,
  129.   ctrlEventEnable,
  130.   ctrlEventDisable,
  131.   ctrlEventTxDone,
  132.   ctrlEventTransmit,
  133.   ctrlEventNudge,
  134.   ctrlEventSetMulticast,
  135.   ctrlEventSuspend,
  136.   ctrlEventResume,
  137.   ctrlEventTerminate,
  138.   ctrlEventLinkUp,
  139.   ctrlEventLinkDown,
  140.   ctrlEventWriteStalled,
  141.   ctrlEventClearedWriteStall
  142. } ctrlEvent_t;
  143.  
  144. typedef enum miiStateTag {
  145.   miiStateIdle,
  146.   miiStateRead,
  147.   miiStateFinish1,
  148.   miiStateFinish2,
  149.   miiStateWrite,
  150. } miiState_t;
  151.  
  152. typedef enum ctrlStateTag {
  153.   ctrlStateIdle,
  154.   ctrlStateLinkStatus,
  155.   ctrlStateSetMedium,
  156.   ctrlStateCheckGiga,
  157.   ctrlStateAutoDone1,
  158.   ctrlStateAutoDone2,
  159.   ctrlStateDisable,
  160.   ctrlStateWaitEth,
  161.   ctrlStateWaitMii,
  162.   ctrlStateSuspend,
  163.   ctrlStateClearWriteStall
  164. } ctrlState_t;
  165.  
  166. typedef struct ax8817xCompletion {
  167.   IOUSBCompletion *completion;
  168.   IOBufferMemoryDescriptor *mdp;
  169.   struct ax8817xCompletion *next;
  170.   int flags;
  171.   UInt32 packetLength;
  172. } ax8817xCompletion_t;
  173.  
  174. typedef struct ax8817xTxRing {
  175.   IOBufferMemoryDescriptor *mdp;
  176.   UInt8 *buffer;
  177.   IOUSBCompletion writeCompletionInfo;
  178.   IOUSBCompletion writeZeroCompletionInfo;
  179.   mbuf_t packet;
  180.   struct ax8817xTxRing *next;
  181.   int flags;
  182. } ax8817xTxRing_t;
  183.  
  184. class USBAx8817x : public IOEthernetController {
  185.   // Constructor & Destructor stuff
  186.     OSDeclareDefaultStructors(USBAx8817x); 
  187.  
  188. private:
  189.     IOUSBDevice                 *fpDevice;
  190.     mbuf_t                      txPkt;
  191.     //IOBasicOutputQueue          *fTransmitQueue;
  192.  
  193.     IOWorkLoop                  *fWorkLoop;
  194.     IOTimerEventSource          *fTimerSource;
  195.  
  196.     bool                        fMulticastEnable;
  197.  
  198.     char                        vendorStr[32];
  199.     char                        modelStr[32];
  200.     char                        revisionStr[32];
  201.      
  202.     IOUSBInterface              *fDataInterface;
  203.     
  204.     IOUSBPipe                   *fCntlPipe;
  205.     IOUSBPipe                   *fInterruptPipe;
  206.  
  207.     IOBufferMemoryDescriptor    *fPipeInterruptMDP;
  208.     IOBufferMemoryDescriptor    *fPipeCtrlMDP;
  209.     IOBufferMemoryDescriptor    *rxMDPList[NUM_RX_BUFFERS];
  210.     ax8817xTxRing_t             txRing[TX_RING_SIZE];
  211.     ax8817xCompletion_t         *rxRing;
  212.     ax8817xTxRing_t             *txProducePtr;
  213.     ax8817xCompletion_t         readComp[NUM_RX_BUFFERS];
  214.     UInt8                       *fInterruptPipeBuffer;
  215.  
  216.     IOUSBCompletion             fCtrlCompletionInfo;
  217.     IOUSBCompletion             fReadCompletionInfo[NUM_RX_BUFFERS];
  218.     IOUSBCompletion             fInterruptCompletionInfo;
  219.  
  220.     IOUSBDevRequestDesc         fCtrlRead;
  221.     IOUSBDevRequestDesc         fCtrlWrite;
  222.  
  223.     IOEthernetStats             *fpEtherStats;
  224.  
  225.     bool                        txQueueStopped;
  226.     bool                        readActive;
  227.     ctrlState_t                 cState;
  228.     bool                        rcvdLinkStatus;
  229.     bool                        interruptPending;
  230.     bool                        readingInterrupt;
  231.     UInt8                       ethCtrl0Val;
  232.     UInt8                       ethCtrl1Val;
  233.     UInt8                       ethCtrl2Val;
  234.     int                         txDone;
  235.     UInt32                      linkSpeed;
  236.     UInt32                      gpioBits;
  237.     IOLock                      *txQueueLock;
  238.     IOLock                      *txLock;
  239.     IOThread                    rxThread;
  240.     int                         txSpots;
  241.     thread_call_t               powerDownThread, powerUpThread;
  242.     unsigned long               currentPowerState;
  243.     int                         debugFlags;
  244.  
  245.     int               txInProgress;
  246.     bool              fNetifRequestEnabled;
  247.     bool              timerPending;
  248.     int               mcBytesToWrite;
  249.     int               setMc;
  250.     int               pktQueueLen;
  251.     bool              suspendPending;
  252.     bool              resumePending;
  253.     bool              ackPowerPending;
  254.     UInt32            selectedMedium;
  255.     UInt32            requestedMedium;
  256.     bool              awake;
  257.     bool              fLinkUp;
  258.     bool              callbackPending;
  259.     bool              checkLinkPending;
  260.     bool              writePipeStalled;
  261.     bool              writeStallCleared;
  262.     int               recursionLevel;
  263.     miiState_t        miiState;
  264.     UInt8             miiIndx;
  265.     UInt16            miiData;
  266.     ax8817xTxRing_t   *txConsumePtr;
  267.     bool              fTerminate;
  268.     AxOutputQueue     *fTransmitQueue;
  269.     UInt8             *fCtrlPipeBuffer;
  270.     bool              miiPending;
  271.     UInt8             mHashTable[8];
  272.     IOService         *ourPolicyMaker;
  273.     bool              started;
  274.     int               promiscMode;
  275.     int               promiscRequest;
  276.     OSDictionary      *fMediumDict;
  277.     bool              fNetifEnabled;
  278.     IOUSBPipe         *fOutPipe;
  279.     // Static Methods
  280.     static void dataReadComplete(void *obj, void *param, IOReturn ior, 
  281.                                  UInt32 remaining);
  282.     static void dataWriteComplete(void *obj, void *param, IOReturn ior, 
  283.                                   UInt32 remaining);
  284.     static void dataWriteZeroComplete(void *obj, void *param, IOReturn ior, 
  285.                                       UInt32 remaining);
  286.     static void ctrlComplete(void *obj, void *param, IOReturn rc, 
  287.                              UInt32 remaining);
  288.     static void interruptComplete(void *obj, void *param, IOReturn rc, 
  289.                                   UInt32 remaining);
  290.     static IOReturn postEvent(OSObject *target, void *evt, 
  291.                               void *result = (void *)kIOReturnSuccess, 
  292.                               void *gotLock = (void *)false, 
  293.                               void *arg3 = NULL);
  294.     static void handleSetPowerStateOff( thread_call_param_t param0,
  295.                                         thread_call_param_t param1 );
  296.     
  297.     static void handleSetPowerStateOn( thread_call_param_t param0,
  298.                                        thread_call_param_t param1 );
  299.  
  300.     static void timerFired(OSObject *owner, IOTimerEventSource *sender);
  301.  
  302.     static void rxThreadEntry(void *ob);
  303.  
  304.     // Driver instance Methods
  305.     
  306.     // Misc. Driver methods
  307.     UInt32            outputPacket(mbuf_t pkt, void *param);
  308.     void              rxLoop();
  309.     bool              handleRxThreadCmd();
  310.  
  311.     void              cleanUp();
  312.  
  313. protected:
  314.     IOEthernetInterface *fNetworkInterface;
  315.     IONetworkStats    *fpNetStats;
  316.     IOUSBPipe         *fInPipe;
  317.     UInt16            mediumStatus;
  318.  
  319.     IOLock            *rxQueueLock;
  320.     bool              supports1000BaseT;
  321.  
  322.     UInt8             fEaddr[6];
  323.     int               phy;
  324.     UInt16            deviceFlag;
  325.     UInt16            maxPacketSize;
  326.     UInt32            readThreadCmd;
  327.     UInt16            rxCtrlVal;
  328.  
  329.     virtual void      setState(ctrlEvent_t event);
  330.     virtual int       copyPktToBuffer(mbuf_t mbuf, UInt8 *buf, int len);
  331.     virtual void      receivePacket(UInt32 remaining,
  332.                                     IOBufferMemoryDescriptor *inMDP,
  333.                                     IOUSBCompletion *completion);
  334.  
  335.     // Device Control
  336.     bool              axCmd(UInt16 cmd, UInt16 index, UInt16 val, void *buf);
  337.     IOReturn          axCmdAsync(UInt16 cmd, UInt16 index, UInt16 val);
  338.     virtual bool      readMiiWord(UInt8 phyIn, UInt8 indx, UInt16 *regd);
  339.     virtual bool      writeMiiWord(UInt8 phyIn, UInt8 indx, UInt16 regd);
  340.     virtual IOReturn  readMiiWordAsync(UInt8 indx);
  341.     virtual IOReturn  writeMiiWordAsync(UInt8 indx, UInt16 data);
  342.     virtual void      setMediumStatus(UInt32 selectedMedium);
  343.  
  344.     // Initialization
  345.     virtual bool      openUSB(IOService *provider);
  346.     virtual bool      allocateBuffers(void);
  347.     virtual bool      resetMAC(void);
  348.     virtual bool      createNetworkInterface(void);
  349.     virtual bool      createMediumTables(void);
  350.     virtual bool      startIO(void);
  351.  
  352.     virtual IONetworkInterface  *createInterface(void);
  353.     virtual IOReturn           registerWithPolicyMaker(IOService *policyMaker);
  354.  
  355. public:
  356.     // IOKit methods
  357.     virtual bool                init(OSDictionary *properties = 0);
  358.     virtual bool                start(IOService *provider);
  359.     virtual void                stop(IOService *provider);
  360.     virtual bool                willTerminate(IOService *provider, 
  361.                                               IOOptionBits options );
  362.  
  363.     virtual bool                configureInterface(IONetworkInterface 
  364.                                                    *netif);
  365.     virtual IOService *         probe(IOService *provider, SInt32 *score );
  366.                                       
  367.     virtual IOReturn            setPowerState(unsigned long powerStateOrdinal, 
  368.                                               IOService *whatDevice);
  369.     // IOEthernetController methods
  370.     virtual IOReturn            enable(IONetworkInterface *netif);
  371.     virtual IOReturn            disable(IONetworkInterface *netif);
  372.     virtual IOReturn            getPacketFilters(const OSSymbol *group, 
  373.                                                  UInt32 *filters ) const;
  374.     virtual IOReturn            selectMedium(const IONetworkMedium *medium);
  375.     virtual IOReturn            getHardwareAddress(IOEthernetAddress *addr);
  376.     virtual IOReturn            setMulticastMode(IOEnetMulticastMode mode);
  377.     virtual IOReturn            setMulticastList(IOEthernetAddress *addrs, 
  378.                                                  UInt32 count);
  379.     virtual IOReturn            setPromiscuousMode(IOEnetPromiscuousMode mode);
  380.     virtual IOOutputQueue       *createOutputQueue(void);
  381.     virtual const OSString      *newVendorString(void) const;
  382.     virtual const OSString      *newModelString(void) const;
  383.     virtual const OSString      *newRevisionString(void) const;
  384. }; /* end class USBAx8817x */
  385.