12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950 |
- /*++
- Copyright (c) 2013 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- devrem.c
- Abstract:
- This module implements a test driver that handles device removal.
- Author:
- Chris Stevens 31-May-2013
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/driver.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // Device removal pool tag.
- //
- #define DEVR_ALLOCATION_TAG 0x52766544 // 'RveD'
- //
- // Device removal level and children constants.
- //
- #define DEVICE_REMOVAL_LEVEL_MAX 4
- #define DEVICE_REMOVAL_ROOT_LEVEL 0
- //
- // Device IDs and lengths.
- //
- #define DEVR_ROOT_ID "DEVREMROOT"
- #define DEVR_CHILD_ID "DEVREMCHLD"
- #define DEVR_DEVICE_ID_SIZE 11
- //
- // Class ID format and length.
- //
- #define DEVR_CLASS_ID_FORMAT "Level%04x"
- #define DEVR_CLASS_ID_FORMAT_SIZE 10
- #define DEVR_CLASS_ID_SIZE 10
- //
- // Removal test timer values.
- //
- #define DEVICE_REMOVAL_TEST_PERIOD (300 * MICROSECONDS_PER_MILLISECOND)
- #define DEVICE_REMOVAL_TEST_DUE_TIME (15000 * MICROSECONDS_PER_MILLISECOND)
- //
- // Defines the rate at which removal IRPs fail when removal IRP failure is
- // enabled.
- //
- #define REMOVAL_IRP_FAILURE_RATE 15
- //
- // Defines the rate at which the random test cleans up the test tracking tree.
- //
- #define RANDOM_TEST_CLEAN_TREE_RATE 5
- #define RANDOM_REMOVE_START_LEVEL (DEVICE_REMOVAL_ROOT_LEVEL + 1)
- #define RANDOM_REMOVE_END_LEVEL (DEVICE_REMOVAL_LEVEL_MAX - 1)
- #define RANDOM_ADD_START_LEVEL DEVICE_REMOVAL_ROOT_LEVEL
- #define RANDOM_ADD_END_LEVEL (DEVICE_REMOVAL_LEVEL_MAX - 2)
- #define RANDOM_TEST_MAX_COUNT 100
- //
- // Define the rate at which device queue failures should occur.
- //
- #define DEVICE_QUEUE_FAILURE_RATE 10
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- typedef struct _REMOVAL_DEVICE REMOVAL_DEVICE, *PREMOVAL_DEVICE;
- typedef enum _REMOVAL_DEVICE_TEST {
- RemovalDeviceTestAddChild,
- RemovalDeviceTestUnreportedChild,
- RemovalDeviceTestAddSibling,
- RemovalDeviceTestRemoveChild,
- RemovalDeviceTestRemoveSibling,
- RemovalDeviceTestAddRemoveChild,
- RemovalDeviceTestAddRemoveSibling,
- RemovalDeviceTestCascadeRemove,
- RemovalDeviceTestRandom,
- RemovalDeviceTestCleanup,
- RemovalDeviceTestMax
- } REMOVAL_DEVICE_TEST, *PREMOVAL_DEVICE_TEST;
- /*++
- Structure Definition:
- This structure defines an entry in the removal device tree.
- Members:
- DeviceToken - Stores an opaque token representing the device.
- BusContext - Stores the bus device's driver context for this device.
- FunctionContext - Stores the function device's driver context for this
- device.
- Attached - Stores a bool indicating whether or not the device is attached.
- RemovalIrp - Stores a bool indicating whether or not the device has
- received a removal IRP.
- SilbingEntry - Stores a list entry pointing to the entries sibling devices.
- ChildListHead - Stores a list entry pointing to the entries children.
- --*/
- typedef struct _REMOVAL_DEVICE_ENTRY {
- PVOID DeviceToken;
- PREMOVAL_DEVICE BusContext;
- PREMOVAL_DEVICE FunctionContext;
- BOOL Attached;
- BOOL RemovalIrp;
- LIST_ENTRY SiblingEntry;
- LIST_ENTRY ChildListHead;
- } REMOVAL_DEVICE_ENTRY, *PREMOVAL_DEVICE_ENTRY;
- /*++
- Enum Definition:
- This enumerates device removal types.
- Values:
- DeviceRemovalInvalid - Represents an invalid device.
- DeviceRemovalBus - Represents a bus device, implying that this driver will
- act as a functional driver for the bus.
- DeviceRemovalFunction - Represents a functional device, implying that this
- driver will act as a bus driver for the function.
- --*/
- typedef enum _REMOVAL_DEVICE_TYPE {
- DeviceRemovalInvalid,
- DeviceRemovalBus,
- DeviceRemovalFunction
- } REMOVAL_DEVICE_TYPE, *PREMOVAL_DEVICE_TYPE;
- /*++
- Structure Definition:
- This structure defines a removal device.
- Members:
- Type - Stores the type of removal device.
- Root - Stores whether or not the device is the root removal device.
- Level - Stores the heirarchy level of the device.
- Children - Stores an array of the device's children.
- ChildCount - Stores the number of children that belong to the device.
- TreeEntry - Stores the devices entry in the global tree.
- --*/
- struct _REMOVAL_DEVICE {
- REMOVAL_DEVICE_TYPE Type;
- BOOL Root;
- ULONG Level;
- PDEVICE *Children;
- ULONG ChildCount;
- PREMOVAL_DEVICE_ENTRY TreeEntry;
- };
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- KSTATUS
- DeviceRemovalAddDevice (
- PVOID Driver,
- PCSTR DeviceId,
- PCSTR ClassId,
- PCSTR CompatibleIds,
- PVOID DeviceToken
- );
- VOID
- DeviceRemovalDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- DeviceRemovalDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- );
- VOID
- DeviceRemovalReportChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- );
- VOID
- DeviceRemovalEnumerateChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- );
- VOID
- DeviceRemovalValidateChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- );
- KSTATUS
- DeviceRemovalRemoveDevice (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- );
- VOID
- DeviceRemovalRunTests (
- VOID
- );
- VOID
- DeviceRemovalDestroyTests (
- PVOID Parameter
- );
- VOID
- DeviceRemovalServiceRemovalDpc (
- PDPC Dpc
- );
- VOID
- DeviceRemovalTests (
- PVOID Parameter
- );
- VOID
- DeviceRemovalTestAddChild (
- VOID
- );
- VOID
- DeviceRemovalTestUnreportedChild (
- VOID
- );
- VOID
- DeviceRemovalTestAddSibling (
- VOID
- );
- VOID
- DeviceRemovalTestRemoveChild (
- VOID
- );
- VOID
- DeviceRemovalTestRemoveSibling (
- VOID
- );
- VOID
- DeviceRemovalTestAddRemoveChild (
- VOID
- );
- VOID
- DeviceRemovalTestAddRemoveSibling (
- VOID
- );
- VOID
- DeviceRemovalTestCascadeRemove (
- VOID
- );
- VOID
- DeviceRemovalCascadeRemoveHelper (
- PREMOVAL_DEVICE_ENTRY RootEntry,
- PVOID ParentDeviceToken
- );
- VOID
- DeviceRemovalTestRandom (
- VOID
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindDeviceAndDetach (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY *ParentEntry,
- ULONG Level
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindDeviceAndAddChild (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY *ParentEntry,
- ULONG Level
- );
- VOID
- DeviceRemovalDestroyTree (
- PREMOVAL_DEVICE_ENTRY RootEntry
- );
- VOID
- DeviceRemovalCleanTree (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY ParentEntry
- );
- VOID
- DeviceRemovalNukeTree (
- VOID
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalInitializeTreeForTest (
- VOID
- );
- VOID
- DeviceRemovalWaitForTreeCreation (
- PREMOVAL_DEVICE_ENTRY RootEntry
- );
- VOID
- DeviceRemovalDetachDevice (
- PREMOVAL_DEVICE_ENTRY DeviceEntry
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalAttachChildDevice (
- PREMOVAL_DEVICE Device
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalAttachChildDeviceHelper (
- PREMOVAL_DEVICE Device
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalCreateTreeEntry (
- PREMOVAL_DEVICE DeviceContext,
- PREMOVAL_DEVICE_ENTRY ParentEntry
- );
- VOID
- DeviceRemovalDeleteTreeEntry (
- PREMOVAL_DEVICE_ENTRY Entry
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindChildByToken (
- PREMOVAL_DEVICE_ENTRY Root,
- PVOID DeviceToken
- );
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindEntryByToken (
- PVOID DeviceToken
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Stores a boolean to toggle running the device removal tests during system
- // startup.
- //
- BOOL DeviceRemovalTestsEnabled = FALSE;
- //
- // Array of how many children each level should automaticlly create.
- //
- ULONG LevelChildCount[DEVICE_REMOVAL_LEVEL_MAX] = {0, 2, 4, 0};
- //
- // Reference to this device driver.
- //
- PDRIVER DeviceRemovalDriver = NULL;
- //
- // Tree of device removal devices.
- //
- PREMOVAL_DEVICE_ENTRY RemovalDeviceTree = NULL;
- KSPIN_LOCK DeviceTreeLock;
- volatile ULONG DeviceEntryCount = 0;
- //
- // Device removal initialization, timer and work queue variables.
- //
- ULONG RemovalTestsInitialized = FALSE;
- PKTIMER RemovalTestTimer = NULL;
- PWORK_QUEUE RemovalTestWorkQueue = NULL;
- PWORK_ITEM RemovalTestWorkItem = NULL;
- PDPC RemovalTestDpc = NULL;
- KSPIN_LOCK RemovalTestLock;
- REMOVAL_DEVICE_TEST RemovalTest;
- //
- // Random device removal test variables.
- //
- ULONG RandomTestCount = 0;
- BOOL RandomRemoveDevice = TRUE;
- ULONG RandomRemoveLevel = RANDOM_REMOVE_START_LEVEL;
- ULONG RandomAddLevel = RANDOM_ADD_START_LEVEL;
- //
- // IRP failure variables.
- //
- BOOL RemovalIrpFailEnabled = FALSE;
- volatile ULONG RemovalIrpFailureCount = 0;
- //
- // Device queue failure variables.
- //
- BOOL DeviceQueueFailEnabled = FALSE;
- volatile ULONG DeviceQueueFailureCount = 0;
- //
- // The root device context.
- //
- PREMOVAL_DEVICE RootDevice;
- PDEVICE RootDeviceToken;
- //
- // Allocation counters.
- //
- volatile ULONG BusDeviceCount = 0;
- volatile ULONG FunctionDeviceCount = 0;
- //
- // ------------------------------------------------------------------ Functions
- //
- __USED
- KSTATUS
- DriverEntry (
- PDRIVER Driver
- )
- /*++
- Routine Description:
- This routine is the entry point for the device removal driver. It registers
- its other dispatch functions, and performs driver-wide initialization.
- Arguments:
- Driver - Supplies a pointer to the driver object.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code on error.
- --*/
- {
- DRIVER_FUNCTION_TABLE FunctionTable;
- KSTATUS Status;
- DeviceRemovalDriver = Driver;
- KeInitializeSpinLock(&DeviceTreeLock);
- KeInitializeSpinLock(&RemovalTestLock);
- RtlZeroMemory(&FunctionTable, sizeof(DRIVER_FUNCTION_TABLE));
- FunctionTable.Version = DRIVER_FUNCTION_TABLE_VERSION;
- FunctionTable.AddDevice = DeviceRemovalAddDevice;
- FunctionTable.DispatchStateChange = DeviceRemovalDispatchStateChange;
- FunctionTable.DispatchSystemControl = DeviceRemovalDispatchSystemControl;
- Status = IoRegisterDriverFunctions(Driver, &FunctionTable);
- if (!KSUCCESS(Status)) {
- goto DriverEntryEnd;
- }
- //
- // Create the root device.
- //
- if (DeviceRemovalTestsEnabled != FALSE) {
- Status = IoCreateDevice(NULL,
- NULL,
- NULL,
- "DEVREMROOT",
- NULL,
- NULL,
- &RootDeviceToken);
- }
- DriverEntryEnd:
- return Status;
- }
- KSTATUS
- DeviceRemovalAddDevice (
- PVOID Driver,
- PCSTR DeviceId,
- PCSTR ClassId,
- PCSTR CompatibleIds,
- PVOID DeviceToken
- )
- /*++
- Routine Description:
- This routine is called when a device is detected for which the device
- removal driver acts as the function driver. The driver will attach itself
- to the stack.
- Arguments:
- Driver - Supplies a pointer to the driver being called.
- DeviceId - Supplies a pointer to a string with the device ID.
- ClassId - Supplies a pointer to a string containing the device's class ID.
- CompatibleIds - Supplies a pointer to a string containing device IDs
- that would be compatible with this device.
- DeviceToken - Supplies an opaque token that the driver can use to identify
- the device in the system. This token should be used when attaching to
- the stack.
- Return Value:
- STATUS_SUCCESS on success.
- Failure code if the driver was unsuccessful in attaching itself.
- --*/
- {
- PREMOVAL_DEVICE DeviceContext;
- ULONG ItemsScanned;
- ULONG Level;
- BOOL Root;
- KSTATUS Status;
- PREMOVAL_DEVICE_ENTRY TreeEntry;
- TreeEntry = NULL;
- DeviceContext = NULL;
- //
- // Determine if this is the device removal root or some child.
- //
- Root = IoAreDeviceIdsEqual(DeviceId, DEVR_ROOT_ID);
- if (Root != FALSE) {
- Level = DEVICE_REMOVAL_ROOT_LEVEL;
- } else {
- ASSERT(IoAreDeviceIdsEqual(DeviceId, DEVR_CHILD_ID) != FALSE);
- //
- // Look at the class ID to determine the level.
- //
- Status = RtlStringScan(ClassId,
- DEVR_CLASS_ID_SIZE,
- DEVR_CLASS_ID_FORMAT,
- DEVR_CLASS_ID_FORMAT_SIZE,
- CharacterEncodingDefault,
- &ItemsScanned,
- &Level);
- if (!KSUCCESS(Status)) {
- goto AddDeviceEnd;
- }
- if (ItemsScanned != 1) {
- Status = STATUS_UNSUCCESSFUL;
- goto AddDeviceEnd;
- }
- }
- //
- // Initialize the functional device context that treats this device as a
- // bus.
- //
- DeviceContext = MmAllocateNonPagedPool(sizeof(REMOVAL_DEVICE),
- DEVR_ALLOCATION_TAG);
- if (DeviceContext == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AddDeviceEnd;
- }
- RtlAtomicAdd32(&BusDeviceCount, 1);
- RtlZeroMemory(DeviceContext, sizeof(REMOVAL_DEVICE));
- DeviceContext->Type = DeviceRemovalBus;
- DeviceContext->Root = Root;
- DeviceContext->Level = Level;
- ASSERT(Level < DEVICE_REMOVAL_LEVEL_MAX);
- DeviceContext->ChildCount = LevelChildCount[Level];
- //
- // The root device needs to create a tree entry for itself.
- //
- if (Root != FALSE) {
- KeAcquireSpinLock(&DeviceTreeLock);
- TreeEntry = DeviceRemovalCreateTreeEntry(DeviceContext,
- NULL);
- KeReleaseSpinLock(&DeviceTreeLock);
- if (TreeEntry == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AddDeviceEnd;
- }
- TreeEntry->DeviceToken = DeviceToken;
- RemovalDeviceTree = TreeEntry;
- //
- // For other devices, the tree entry was created by the parent, find it and
- // attach it to this devie context.
- //
- } else {
- TreeEntry = DeviceRemovalFindEntryByToken(DeviceToken);
- ASSERT(TreeEntry != NULL);
- DeviceContext->TreeEntry = TreeEntry;
- TreeEntry->BusContext = DeviceContext;
- }
- //
- // Attach the bus driver context to the device.
- //
- Status = IoAttachDriverToDevice(Driver, DeviceToken, DeviceContext);
- if (!KSUCCESS(Status)) {
- goto AddDeviceEnd;
- }
- //
- // When adding the root device, begin the test sequence.
- //
- if (Root != FALSE) {
- RootDevice = DeviceContext;
- DeviceRemovalRunTests();
- }
- AddDeviceEnd:
- if (!KSUCCESS(Status)) {
- if (TreeEntry != NULL) {
- DeviceRemovalDeleteTreeEntry(TreeEntry);
- }
- if (DeviceContext != NULL) {
- MmFreeNonPagedPool(DeviceContext);
- RtlAtomicAdd32(&BusDeviceCount, (ULONG)-1);
- }
- }
- return Status;
- }
- VOID
- DeviceRemovalDispatchStateChange (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles State Change IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE Device;
- KSTATUS Status;
- ASSERT(Irp->MajorCode == IrpMajorStateChange);
- Device = (PREMOVAL_DEVICE)DeviceContext;
- //
- // Process the IRP based on the minor code and direction.
- //
- if (Irp->Direction == IrpDown) {
- switch (Irp->MinorCode) {
- case IrpMinorQueryResources:
- //
- // Act on this IRP if the driver is acting as the bus driver or
- // if it is the only driver for the device, which is the case for
- // the root.
- //
- if ((Device->Type == DeviceRemovalFunction) ||
- (Device->Root != FALSE)) {
- IoCompleteIrp(DeviceRemovalDriver, Irp, STATUS_SUCCESS);
- }
- break;
- case IrpMinorStartDevice:
- //
- // Act on this IRP fi the driver is acting as the bus driver or
- // if it is the only driver for the device, which is the case for
- // the root.
- //
- if ((Device->Type == DeviceRemovalFunction) ||
- (Device->Root != FALSE)) {
- IoCompleteIrp(DeviceRemovalDriver, Irp, STATUS_SUCCESS);
- }
- break;
- case IrpMinorQueryChildren:
- //
- // The device is a function and this is operating as a bus driver,
- // so just complete the IRP.
- //
- if (Device->Type == DeviceRemovalFunction) {
- IoCompleteIrp(DeviceRemovalDriver, Irp, STATUS_SUCCESS);
- //
- // The device is a bus and this driver is acting as the
- // functional driver, so report the children.
- //
- } else {
- ASSERT(Device->Type == DeviceRemovalBus);
- DeviceRemovalReportChildren(Irp, Device);
- //
- // The root device has no bus driver, so it has to complete
- // the IRP itself.
- //
- if (Device->Root != FALSE) {
- IoCompleteIrp(DeviceRemovalDriver, Irp, STATUS_SUCCESS);
- }
- }
- break;
- case IrpMinorRemoveDevice:
- if ((Device->Type == DeviceRemovalFunction) ||
- (Device->Root != FALSE)) {
- Status = DeviceRemovalRemoveDevice(Irp, Device);
- IoCompleteIrp(DeviceRemovalDriver, Irp, Status);
- }
- default:
- break;
- }
- } else {
- ASSERT(Irp->Direction == IrpUp);
- switch (Irp->MinorCode) {
- case IrpMinorRemoveDevice:
- DeviceRemovalRemoveDevice(Irp, Device);
- break;
- default:
- break;
- }
- }
- return;
- }
- VOID
- DeviceRemovalDispatchSystemControl (
- PIRP Irp,
- PVOID DeviceContext,
- PVOID IrpContext
- )
- /*++
- Routine Description:
- This routine handles System Control IRPs.
- Arguments:
- Irp - Supplies a pointer to the I/O request packet.
- DeviceContext - Supplies the context pointer supplied by the driver when it
- attached itself to the driver stack. Presumably this pointer contains
- driver-specific device context.
- IrpContext - Supplies the context pointer supplied by the driver when
- the IRP was created.
- Return Value:
- None.
- --*/
- {
- ASSERT(Irp->MajorCode == IrpMajorSystemControl);
- //
- // Complete the IRP.
- //
- if (Irp->Direction == IrpDown) {
- IoCompleteIrp(DeviceRemovalDriver, Irp, STATUS_NOT_HANDLED);
- } else {
- ASSERT(Irp->Direction == IrpUp);
- }
- return;
- }
- VOID
- DeviceRemovalReportChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine reports the number of children of the device. If the children
- have not yet been enumerated, it will enumerate them. If they have
- previously been enumerated, then it will validate them to make sure they
- all still exist.
- Arguments:
- Irp - Supplies a pointer to the IRP requestion the children.
- Device - Supplies a pointer to the current device context.
- Return Value:
- None.
- --*/
- {
- PDEVICE *Children;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- Irp->U.QueryChildren.Children = NULL;
- Irp->U.QueryChildren.ChildCount = 0;
- //
- // If the device has never reported children, enumerate them. Otherwise
- // validate that they all still exist.
- //
- if (Device->Children == NULL) {
- DeviceRemovalEnumerateChildren(Irp, Device);
- } else {
- DeviceRemovalValidateChildren(Irp, Device);
- }
- //
- // If the bus driver has no children, exit immediately.
- //
- if (Device->ChildCount == 0) {
- return;
- }
- //
- // Report the current children in the IRP.
- //
- ASSERT(Device->ChildCount != 0);
- Children = MmAllocatePagedPool(sizeof(PDEVICE) * Device->ChildCount,
- DEVR_ALLOCATION_TAG);
- if (Children == NULL) {
- goto ReportChildrenEnd;
- }
- RtlCopyMemory(Children,
- Device->Children,
- Device->ChildCount * sizeof(PDEVICE));
- Irp->U.QueryChildren.Children = Children;
- Irp->U.QueryChildren.ChildCount = Device->ChildCount;
- ReportChildrenEnd:
- return;
- }
- VOID
- DeviceRemovalEnumerateChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine enumerates the children of the supplied device.
- Arguments:
- Irp - Supplies a pointer to the IRP requesting the enumeration.
- Device - Supplies a pointer to the device removal context.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- ULONG ChildIndex;
- CHAR ClassId[DEVR_CLASS_ID_SIZE];
- PREMOVAL_DEVICE NewContext;
- KSTATUS Status;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- Status = STATUS_SUCCESS;
- //
- // Synchronize with device creation.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- ASSERT(Device->TreeEntry->RemovalIrp == FALSE);
- //
- // Only bus driver should report children.
- //
- ASSERT(Device->Type == DeviceRemovalBus);
- if (Device->ChildCount == 0) {
- goto EnumerateChildrenEnd;
- }
- //
- // Allocate an array for child device pointers.
- //
- Device->Children = MmAllocatePagedPool(sizeof(PDEVICE) * Device->ChildCount,
- DEVR_ALLOCATION_TAG);
- if (Device->Children == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto EnumerateChildrenEnd;
- }
- RtlZeroMemory(Device->Children, sizeof(PDEVICE) * Device->ChildCount);
- //
- // Create the class ID for the children.
- //
- RtlPrintToString(ClassId,
- DEVR_CLASS_ID_SIZE,
- CharacterEncodingDefault,
- DEVR_CLASS_ID_FORMAT,
- (Device->Level + 1));
- //
- // Create devices for the current device's children and track them in the
- // the global device removal tree.
- //
- for (ChildIndex = 0; ChildIndex < Device->ChildCount; ChildIndex += 1) {
- NewContext = MmAllocateNonPagedPool(sizeof(REMOVAL_DEVICE),
- DEVR_ALLOCATION_TAG);
- if (NewContext == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto EnumerateChildrenEnd;
- }
- RtlAtomicAdd32(&FunctionDeviceCount, 1);
- RtlZeroMemory(NewContext, sizeof(REMOVAL_DEVICE));
- NewContext->Type = DeviceRemovalFunction;
- NewContext->Root = FALSE;
- //
- // Create a tree entry for the child device.
- //
- ChildEntry = DeviceRemovalCreateTreeEntry(NewContext,
- Device->TreeEntry);
- if (ChildEntry == NULL) {
- MmFreeNonPagedPool(NewContext);
- RtlAtomicAdd32(&FunctionDeviceCount, (ULONG)-1);
- break;
- }
- //
- // Create the child device and fill out the accounting structures.
- //
- Status = IoCreateDevice(DeviceRemovalDriver,
- NewContext,
- Irp->Device,
- DEVR_CHILD_ID,
- ClassId,
- NULL,
- &(Device->Children[ChildIndex]));
- if (!KSUCCESS(Status)) {
- RtlDebugPrint("IoCreateDevice failed with status %d.\n", Status);
- DeviceRemovalDeleteTreeEntry(ChildEntry);
- MmFreeNonPagedPool(NewContext);
- RtlAtomicAdd32(&FunctionDeviceCount, (ULONG)-1);
- break;
- }
- ChildEntry->DeviceToken = Device->Children[ChildIndex];
- }
- //
- // If child creation ever failed, the current index is the count of how
- // many children were successfully created.
- //
- Device->ChildCount = ChildIndex;
- EnumerateChildrenEnd:
- KeReleaseSpinLock(&DeviceTreeLock);
- return;
- }
- VOID
- DeviceRemovalValidateChildren (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine validates that all of the devices children still exist. Where
- a physical device would query hardware, this driver searches through the
- global tree for missing children. If any of the children have been removed,
- it updates the device's child list.
- Arguments:
- Irp - Supplies a pointer to the IRP that requires child validation.
- Device - Supplies a pointer to the current device context.
- Return Value:
- None.
- --*/
- {
- ULONG ChildCount;
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- ULONG ChildIndex;
- PDEVICE *CurrentChildren;
- ULONG CurrentIndex;
- PDEVICE *OriginalChildren;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- //
- // Search through the children to determine how many devices are still
- // attached.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- ASSERT(Device->TreeEntry->RemovalIrp == FALSE);
- ASSERT(((Device->ChildCount == 0) && (Device->Children == NULL)) ||
- ((Device->ChildCount != 0) && (Device->Children != NULL)));
- ChildCount = 0;
- OriginalChildren = Device->Children;
- for (ChildIndex = 0; ChildIndex < Device->ChildCount; ChildIndex += 1) {
- ChildEntry = DeviceRemovalFindChildByToken(
- Device->TreeEntry,
- OriginalChildren[ChildIndex]);
- //
- // If the child has an entry and is attached, count is as validated.
- // Otherwise, remove it from the original array.
- //
- if ((ChildEntry != NULL) && (ChildEntry->Attached != FALSE)) {
- ChildCount += 1;
- }
- }
- //
- // If the count did not change, exit.
- //
- if (Device->ChildCount == ChildCount) {
- goto ValidateChildrenEnd;
- }
- //
- // If there are no children anymore, free the old list and do not create a
- // new list.
- //
- if (ChildCount == 0) {
- MmFreePagedPool(Device->Children);
- Device->Children = NULL;
- Device->ChildCount = 0;
- goto ValidateChildrenEnd;
- }
- //
- // Allocate an array for the new children.
- //
- CurrentChildren = MmAllocatePagedPool(sizeof(PDEVICE) * ChildCount,
- DEVR_ALLOCATION_TAG);
- if (CurrentChildren == NULL) {
- goto ValidateChildrenEnd;
- }
- CurrentIndex = 0;
- for (ChildIndex = 0; ChildIndex < Device->ChildCount; ChildIndex += 1) {
- ChildEntry = DeviceRemovalFindChildByToken(
- Device->TreeEntry,
- OriginalChildren[ChildIndex]);
- if ((ChildEntry != NULL) && (ChildEntry->Attached != FALSE)) {
- CurrentChildren[CurrentIndex] = OriginalChildren[ChildIndex];
- CurrentIndex += 1;
- }
- }
- ASSERT(CurrentIndex == ChildCount);
- Device->Children = CurrentChildren;
- Device->ChildCount = ChildCount;
- MmFreePagedPool(OriginalChildren);
- ValidateChildrenEnd:
- KeReleaseSpinLock(&DeviceTreeLock);
- return;
- }
- KSTATUS
- DeviceRemovalRemoveDevice (
- PIRP Irp,
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine prepares the device for removal from the system.
- Arguments:
- Irp - Supplies a pointer to the IRP that is requesting the device removal.
- Device - Supplies a pointer to the device context for this driver.
- Return Value:
- Status code.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- ULONG OldFailureCount;
- PREMOVAL_DEVICE_ENTRY TreeEntry;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- //
- // Handle two cases where the driver is acting as the functional driver.
- //
- if (Device->Type == DeviceRemovalBus) {
- //
- // If the bus driver failed the IRP on the way down, exit immediately
- // on the way back up.
- //
- if ((RemovalIrpFailEnabled != FALSE) &&
- (Irp->Status != STATUS_NOT_HANDLED) &&
- (!KSUCCESS(Irp->Status))) {
- return Irp->Status;
- }
- //
- // Otherwise free the device context and return successfully.
- //
- if (Device->Children != NULL) {
- MmFreePagedPool(Device->Children);
- }
- MmFreeNonPagedPool(Device);
- RtlAtomicAdd32(&BusDeviceCount, (ULONG)-1);
- return STATUS_SUCCESS;
- }
- //
- // Fail some removal IRPs once IRP failure is enabled.
- //
- if (RemovalIrpFailEnabled != FALSE) {
- OldFailureCount = RtlAtomicAdd32(&RemovalIrpFailureCount, 1);
- if ((OldFailureCount % REMOVAL_IRP_FAILURE_RATE) == 0) {
- return STATUS_UNSUCCESSFUL;
- }
- }
- //
- // Fail the next device queue action if enabled.
- //
- if (DeviceQueueFailEnabled != FALSE) {
- OldFailureCount = RtlAtomicAdd32(&DeviceQueueFailureCount, 1);
- if ((OldFailureCount % DEVICE_QUEUE_FAILURE_RATE) == 0) {
- IoSetTestHook(IO_FAIL_QUEUE_DEVICE_WORK);
- }
- }
- //
- // Mark the device as detached.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- TreeEntry = Device->TreeEntry;
- ASSERT(TreeEntry != NULL);
- TreeEntry->Attached = FALSE;
- //
- // Assert that the device's children have already been marked as detached
- // and that they have seen a removal IRP.
- //
- CurrentEntry = TreeEntry->ChildListHead.Next;
- while (CurrentEntry != &(TreeEntry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- CurrentEntry = CurrentEntry->Next;
- ASSERT(ChildEntry->Attached == FALSE);
- ASSERT(ChildEntry->RemovalIrp != FALSE);
- }
- TreeEntry->RemovalIrp = TRUE;
- KeReleaseSpinLock(&DeviceTreeLock);
- if (Device->Children != NULL) {
- MmFreePagedPool(Device->Children);
- }
- MmFreeNonPagedPool(Device);
- RtlAtomicAdd32(&FunctionDeviceCount, (ULONG)-1);
- return STATUS_SUCCESS;
- }
- //
- // -------------------------------------------------------- Test Infrastructure
- //
- VOID
- DeviceRemovalRunTests (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes the device removal test sequence.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- ULONGLONG DueTime;
- BOOL Initialized;
- ULONGLONG Period;
- KSTATUS Status;
- //
- // Test and set the initialization boolean. If the test sequence has
- // already begun, exit. This could be done with the timer pointer, but
- // there is no compare-exchange pointer routine yet.
- //
- Initialized = RtlAtomicCompareExchange32(&RemovalTestsInitialized,
- TRUE,
- FALSE);
- if (Initialized != FALSE) {
- return;
- }
- ASSERT(RemovalTestTimer == NULL);
- ASSERT(RemovalTestWorkQueue == NULL);
- ASSERT(RemovalTestWorkItem == NULL);
- //
- // Create and queue a timer that will kick off the test sequence.
- //
- ASSERT(KeGetRunLevel() <= RunLevelDispatch);
- RemovalTestTimer = KeCreateTimer(DEVR_ALLOCATION_TAG);
- if (RemovalTestTimer == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto RunTestsEnd;
- }
- //
- // Create a work queue that will be filled with a work item when the DPC
- // fires.
- //
- RemovalTestWorkQueue = KeCreateWorkQueue(
- WORK_QUEUE_FLAG_SUPPORT_DISPATCH_LEVEL,
- "DeviceRemovalTestQueue");
- if (RemovalTestWorkQueue == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto RunTestsEnd;
- }
- //
- // Create a work item to be added to the work queue by the DPC.
- //
- RemovalTestWorkItem = KeCreateWorkItem(RemovalTestWorkQueue,
- WorkPriorityNormal,
- DeviceRemovalTests,
- NULL,
- DEVR_ALLOCATION_TAG);
- if (RemovalTestWorkItem == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto RunTestsEnd;
- }
- //
- // Create a DPC to queue once the timer expires.
- //
- RemovalTestDpc = KeCreateDpc(DeviceRemovalServiceRemovalDpc, NULL);
- if (RemovalTestDpc == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto RunTestsEnd;
- }
- //
- // Now that the test sequence is almost good to go, set the first test.
- // This just needs to happen before the timer first expires.
- //
- RemovalTest = RemovalDeviceTestAddChild;
- //
- // Set the timer to go off at the test intervals.
- //
- DueTime = HlQueryTimeCounter();
- DueTime += KeConvertMicrosecondsToTimeTicks(DEVICE_REMOVAL_TEST_DUE_TIME);
- Period = KeConvertMicrosecondsToTimeTicks(DEVICE_REMOVAL_TEST_PERIOD);
- Status = KeQueueTimer(RemovalTestTimer,
- TimerQueueSoftWake,
- DueTime,
- Period,
- 0,
- RemovalTestDpc);
- if (!KSUCCESS(Status)) {
- goto RunTestsEnd;
- }
- RunTestsEnd:
- if (!KSUCCESS(Status)) {
- if (RemovalTestDpc != NULL) {
- KeDestroyDpc(RemovalTestDpc);
- }
- if (RemovalTestTimer != NULL) {
- KeDestroyTimer(RemovalTestTimer);
- }
- if (RemovalTestWorkQueue != NULL) {
- KeDestroyWorkQueue(RemovalTestWorkQueue);
- }
- if (RemovalTestWorkItem != NULL) {
- KeDestroyWorkItem(RemovalTestWorkItem);
- }
- }
- return;
- }
- VOID
- DeviceRemovalDestroyTests (
- PVOID Parameter
- )
- /*++
- Routine Description:
- This routine destroys the resources allocated to run the device removal
- tests.
- Arguments:
- Parameter - Supplies an optional parameter for the work item routine.
- Return Value:
- None.
- --*/
- {
- KeDestroyTimer(RemovalTestTimer);
- KeDestroyDpc(RemovalTestDpc);
- KeDestroyWorkQueue(RemovalTestWorkQueue);
- KeDestroyWorkItem(RemovalTestWorkItem);
- return;
- }
- VOID
- DeviceRemovalServiceRemovalDpc (
- PDPC Dpc
- )
- /*++
- Routine Description:
- This routine services the removal DPC that is queued by the test timer.
- Arguments:
- Dpc - Supplies a pointer to the DPC that is running.
- Return Value:
- None.
- --*/
- {
- //
- // Only queue more work if there isn't an entry already on the queue. This
- // needs a lock in case two DPCs are on top of each other and they both see
- // that the work item is not currently queued.
- //
- KeAcquireSpinLock(&RemovalTestLock);
- KeQueueWorkItem(RemovalTestWorkItem);
- KeReleaseSpinLock(&RemovalTestLock);
- return;
- }
- VOID
- DeviceRemovalTests (
- PVOID Parameter
- )
- /*++
- Routine Description:
- This routine runs through a sequence of device tests.
- Arguments:
- Parameter - Supplies an optional parameter for the work item routine.
- Return Value:
- None.
- --*/
- {
- switch (RemovalTest) {
- //
- // The child addition test stresses adding a child device "concurrently"
- // with removing the parent device. This will test both notifying the
- // system of the child's addition followed by the parent's removal and vice
- // versa.
- //
- case RemovalDeviceTestAddChild:
- DeviceRemovalTestAddChild();
- break;
- //
- // The unreported child test forces a case that is not guaranteed to be
- // tested by the child add test due to timing. It tests the scenario where
- // a device needs to remove a child device that is yet to be reported.
- //
- case RemovalDeviceTestUnreportedChild:
- DeviceRemovalTestUnreportedChild();
- break;
- //
- // The sibling addition test will stress adding two devices to a bus at the
- // same time. It will test adding with the same notification and in
- // subsequent notifications.
- //
- case RemovalDeviceTestAddSibling:
- DeviceRemovalTestAddSibling();
- break;
- //
- // The child remove test will stress removing a child while removing the
- // parent. This will test both notifying the system of the child's removal
- // followed by the parent's removal and vice versa.
- //
- case RemovalDeviceTestRemoveChild:
- DeviceRemovalTestRemoveChild();
- break;
- //
- // The sibling removal test will stress removing two devices from a bus
- // at the same time. It will test removing the devices in the same
- // notification and in two subsequent notifications.
- //
- case RemovalDeviceTestRemoveSibling:
- DeviceRemovalTestRemoveSibling();
- break;
- //
- // The child add/remove test stresses the scenario where one child gets
- // added and another removed just before a parent device gets removed.
- //
- case RemovalDeviceTestAddRemoveChild:
- DeviceRemovalTestAddRemoveChild();
- break;
- //
- // The sibling add/remove test stresses the scenario where one device gets
- // added while another gets removed.
- //
- case RemovalDeviceTestAddRemoveSibling:
- DeviceRemovalTestAddRemoveSibling();
- break;
- //
- // This test covers the case where non-parent ancestory devices get removed
- // while a device is getting removed.
- //
- case RemovalDeviceTestCascadeRemove:
- DeviceRemovalTestCascadeRemove();
- break;
- //
- // The random test creates and removes devices at various levels of the
- // tree every time it is called. This test is used to flush out any timing
- // related issues that cannot be simulated directly.
- //
- case RemovalDeviceTestRandom:
- if (RandomTestCount == 0) {
- RemovalIrpFailEnabled = TRUE;
- DeviceQueueFailEnabled = TRUE;
- }
- DeviceRemovalTestRandom();
- break;
- case RemovalDeviceTestCleanup:
- DeviceRemovalNukeTree();
- RtlDebugPrint("Device Removal Tests Complete.\n");
- if ((DeviceEntryCount != 1) ||
- (BusDeviceCount != 1) ||
- (FunctionDeviceCount != 0)) {
- RtlDebugPrint("Device Removal Cleanup Failed:\n");
- if (DeviceEntryCount != 1) {
- RtlDebugPrint("\tDeviceEntryCount: %d, expected 1\n",
- DeviceEntryCount);
- }
- if (BusDeviceCount != 1) {
- RtlDebugPrint("\tBusDeviceCount: %d, expected 1\n",
- BusDeviceCount);
- }
- if (FunctionDeviceCount != 0) {
- RtlDebugPrint("\tFunctionDeviceCount: %d, expected 1\n",
- FunctionDeviceCount);
- }
- } else {
- RtlDebugPrint("Device Removal Cleanup Succeeded.\n");
- }
- //
- // Fire off a work item to clean everything up.
- //
- KeCreateAndQueueWorkItem(NULL,
- WorkPriorityNormal,
- DeviceRemovalDestroyTests,
- NULL);
- break;
- default:
- break;
- }
- //
- // If the random test is currently not running or the random test has
- // completed its cycles, increment the test counter.
- //
- if ((RemovalTest != RemovalDeviceTestRandom) ||
- (RandomTestCount == RANDOM_TEST_MAX_COUNT)) {
- RemovalTest += 1;
- }
- return;
- }
- VOID
- DeviceRemovalTestAddChild (
- VOID
- )
- /*++
- Routine Description:
- This routine performs the child addition test. This stresses adding a child
- device "concurrently" with removing the parent device. This will test both
- notifying the system of the child's addition followed by the parent's
- removal and vice versa.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- BOOL Result;
- RtlDebugPrint("ChildAdd: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("ChildAdd 0: Failed to attach parent device.\n");
- goto TestAddChild;
- }
- //
- // Now that the tree has been fully initialized, add a child device to the
- // parent, notify the system, and then immediately remove the parent,
- // notifying the system again. This will cause the parent to make the start
- // device call on the child before it gets the removal call. This should
- // trigger some bugs if children cannot be removed mid-initialization.
- //
- ChildEntry = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (ChildEntry == NULL) {
- RtlDebugPrint("ChildAdd 0: Failed to attach child device.\n");
- }
- //
- // If the child failed to be attached, there should be nothing to notify,
- // but do it anyway to stress the system.
- //
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait until the parent receives its removal IRP.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // The child should have been removed even though it was in the middle of
- // being initialized. Validate this.
- //
- Result = TRUE;
- if ((ChildEntry != NULL) &&
- ((ChildEntry->Attached != FALSE) ||
- (ChildEntry->RemovalIrp == FALSE))) {
- RtlDebugPrint("ChildAdd 0: Failed to detach the child!\n");
- Result = FALSE;
- }
- if (ParentEntry->Attached != FALSE) {
- RtlDebugPrint("ChildAdd 0: Failed to detach the parent!\n");
- Result = FALSE;
- }
- //
- // The original device tree should have been destroyed, clean up the tree
- // tracking entries.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- if (Result != FALSE) {
- RtlDebugPrint("ChildAdd 0: Succeeded!\n");
- }
- //
- // Now perform the test again, but send the parent removal notification
- // first.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("ChildAdd 1: Failed to attach parent device.\n");
- goto TestAddChild;
- }
- //
- // Now that the tree has been fully initialized, remove the parent device,
- // add the child device, signal the system of the parent change and then
- // the child change. This should either test unreported device removal or
- // handling a query children work item between prepare remove and remove
- // work items.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- ChildEntry = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (ChildEntry == NULL) {
- RtlDebugPrint("ChildAdd 1: Failed to attach child device.\n");
- }
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- KeAcquireSpinLock(&DeviceTreeLock);
- //
- // Only notify the system about the child's creation if the parent is yet
- // to receive a removal IRP and the child was actually created.
- //
- if ((ParentEntry->RemovalIrp == FALSE) && (ChildEntry != NULL)) {
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Wait until the parent receives its removal IRP.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // The child should have been removed even though it was in the middle of
- // being initialized. Validate this.
- //
- Result = TRUE;
- if ((ChildEntry != NULL) &&
- ((ChildEntry->Attached != FALSE) ||
- (ChildEntry->RemovalIrp == FALSE))) {
- RtlDebugPrint("ChildAdd 1: Failed to detach the child!\n");
- Result = FALSE;
- }
- if (ParentEntry->Attached != FALSE) {
- RtlDebugPrint("ChildAdd 1: Failed to detach the parent!\n");
- Result = FALSE;
- }
- //
- // The original device tree should have been destroyed, clean up the tree
- // tracking entries.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- if (Result != FALSE) {
- RtlDebugPrint("ChildAdd 1: Succeeded!\n");
- }
- TestAddChild:
- return;
- }
- VOID
- DeviceRemovalTestUnreportedChild (
- VOID
- )
- /*++
- Routine Description:
- This routine performs the unreported child test. It causes a device with a
- child in the unreported state to be removed. This triggers some failure
- handling behavior in the IO subsystem.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- BOOL Result;
- RtlDebugPrint("UnreportedChild: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("UnreportedChild: Failed to attach parent device.\n");
- goto TestUnreportedChildEnd;
- }
- //
- // Now that the tree has been fully initialized, remove the parent device,
- // add the child device, and signal the system of the parent change. Do not
- // notify the system of the child's presence. This should test unreported
- // device removal.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- ChildEntry = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (ChildEntry == NULL) {
- RtlDebugPrint("UnreportedChild: Failed to attach child device.\n");
- }
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait until the parent receives its removal IRP.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // The child should have been removed even though it was in the middle of
- // being initialized. Validate this.
- //
- Result = TRUE;
- if ((ChildEntry != NULL) &&
- ((ChildEntry->Attached != FALSE) ||
- (ChildEntry->RemovalIrp == FALSE))) {
- RtlDebugPrint("UnreportedChild: Failed to detach the child!\n");
- Result = FALSE;
- }
- if (ParentEntry->Attached != FALSE) {
- RtlDebugPrint("UnreportedChild: Failed to detach the parent!\n");
- Result = FALSE;
- }
- //
- // The original device tree should have been destroyed, clean up the tree
- // tracking entries.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- if (Result != FALSE) {
- RtlDebugPrint("UnreportedChild: Succeeded!\n");
- }
- TestUnreportedChildEnd:
- return;
- }
- VOID
- DeviceRemovalTestAddSibling (
- VOID
- )
- /*++
- Routine Description:
- This routine tests adding two sibling devices. It first tests adding them
- within one system notification call and then from sequential calls.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY FirstSibling;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- BOOL Result;
- PREMOVAL_DEVICE_ENTRY SecondSibling;
- RtlDebugPrint("AddSibling: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("AddSibling Failed to attach parent device.\n");
- goto TestAddSiblingEnd;
- }
- //
- // Now attach two devices and notify the system.
- //
- FirstSibling = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (FirstSibling == NULL) {
- RtlDebugPrint("AddSibling 0: Failed to allocate first sibling.\n");
- }
- SecondSibling = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (SecondSibling == NULL) {
- RtlDebugPrint("AddSibling 0: Failed to allocate second sibling.\n");
- }
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- //
- // Wait for the tree creation to complete and then check to make sure the
- // devices are fully attached.
- //
- Result = TRUE;
- DeviceRemovalWaitForTreeCreation(ParentEntry);
- if ((FirstSibling != NULL) && (FirstSibling->BusContext == NULL)) {
- RtlDebugPrint("AddSibling 0: First sibling failed to enumerate.\n");
- Result = FALSE;
- }
- if ((SecondSibling != NULL) && (SecondSibling->BusContext == NULL)) {
- RtlDebugPrint("AddSibling 0: Second sibling failed to enumerate.\n");
- Result = FALSE;
- }
- if (Result != FALSE) {
- RtlDebugPrint("AddSibling 0: Succeeded!\n");
- }
- //
- // Now add two additional siblings and notify the system after each
- // addition.
- //
- FirstSibling = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (FirstSibling == NULL) {
- RtlDebugPrint("AddSibling 1: Failed to allocate first sibling.\n");
- }
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- SecondSibling = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (SecondSibling == NULL) {
- RtlDebugPrint("AddSibling 1: Failed to allocate second sibling.\n");
- }
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- //
- // Wait for the tree creation to complete and then check to make sure the
- // devices are fully attached.
- //
- Result = TRUE;
- DeviceRemovalWaitForTreeCreation(ParentEntry);
- if ((FirstSibling != NULL) && (FirstSibling->BusContext == NULL)) {
- RtlDebugPrint("AddSibling 1: First sibling failed to enumerate.\n");
- Result = FALSE;
- }
- if ((SecondSibling != NULL) && (SecondSibling->BusContext == NULL)) {
- RtlDebugPrint("AddSibling 1: Second sibling failed to enumerate.\n");
- Result = FALSE;
- }
- if (Result != FALSE) {
- RtlDebugPrint("AddSibling 1: Succeeded!\n");
- }
- //
- // Now detach the parent device and exit.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait until the parent receives its removal IRP.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // The original device tree should have been destroyed, clean up the tree
- // tracking entries.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestAddSiblingEnd:
- return;
- }
- VOID
- DeviceRemovalTestRemoveChild (
- VOID
- )
- /*++
- Routine Description:
- This routine tests removing a child device while removing the devices
- parent. This test should stress scenarios where a child device has already
- entered the removal process by the time the parent tries to remove it and
- where the parent has already pushed the child into the removal process by
- the time the child tries to remove itself.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- RtlDebugPrint("RemoveChild: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("RemoveChild: Failed to attach parent device.\n");
- goto TestRemoveChild;
- }
- //
- // Get one of the children and mark it for removal.
- //
- ASSERT(LIST_EMPTY(&(ParentEntry->ChildListHead)) == FALSE);
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Next,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- DeviceRemovalDetachDevice(ChildEntry);
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- //
- // Now remove the parent.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait for the removal process to complete and then validate the tree.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Make sure the child was detached.
- //
- if ((ChildEntry->RemovalIrp == FALSE) || (ChildEntry->Attached != FALSE)) {
- RtlDebugPrint("RemoveChild 0: Failed to properly remove child.\n");
- } else {
- RtlDebugPrint("RemoveChild 0: Succeeded!\n");
- }
- //
- // Clean up the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Now perform the test again but notify the system about the parent's
- // removal first.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("RemoveChild: Failed to attach parent device.\n");
- goto TestRemoveChild;
- }
- //
- // Get one of the children.
- //
- ASSERT(LIST_EMPTY(&(ParentEntry->ChildListHead)) == FALSE);
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Next,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- //
- // Mark the parent for removal, notify the system, and then try to remove
- // the child.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- DeviceRemovalDetachDevice(ChildEntry);
- KeAcquireSpinLock(&DeviceTreeLock);
- if (ParentEntry->RemovalIrp == FALSE) {
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Wait for the removal process to complete and then validate the tree.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Make sure the child was detached.
- //
- if ((ChildEntry->RemovalIrp == FALSE) || (ChildEntry->Attached != FALSE)) {
- RtlDebugPrint("RemoveChild 1: Failed to properly remove child.\n");
- } else {
- RtlDebugPrint("RemoveChild 1: Succeeded!\n");
- }
- //
- // Clean up the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestRemoveChild:
- return;
- }
- VOID
- DeviceRemovalTestRemoveSibling (
- VOID
- )
- /*++
- Routine Description:
- This routine implements the sibling removal test. This test stresses
- removing two devices from a bus at the same time. It tests removing the
- devices in the same system notification and in sequential notifications.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY FirstSibling;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- PREMOVAL_DEVICE_ENTRY SecondSibling;
- RtlDebugPrint("RemoveSibling: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("RemoveSibling: Failed to attach parent device.\n");
- goto TestRemoveSibling;
- }
- //
- // Make sure that the parent has at least two children.
- //
- ASSERT(ParentEntry->ChildListHead.Next !=
- ParentEntry->ChildListHead.Previous);
- FirstSibling = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Next,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- SecondSibling = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Previous,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- //
- // Detach the children and notify the system.
- //
- DeviceRemovalDetachDevice(FirstSibling);
- DeviceRemovalDetachDevice(SecondSibling);
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- //
- // Wait for their removal IRPs.
- //
- while (FirstSibling->RemovalIrp == FALSE) {
- KeYield();
- }
- while (SecondSibling->RemovalIrp == FALSE) {
- KeYield();
- }
- RtlDebugPrint("RemoveSibling: Successful!\n");
- //
- // Make sure removing the children did not remove the parent.
- //
- ASSERT(ParentEntry->RemovalIrp == FALSE);
- ASSERT(ParentEntry->Attached != FALSE);
- //
- // Now destroy the parent and exit.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Clean up the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestRemoveSibling:
- return;
- }
- VOID
- DeviceRemovalTestAddRemoveChild (
- VOID
- )
- /*++
- Routine Description:
- This routine implements the add/remove child test. This test adds a child
- and removes a different child while removing their parent device.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY AddChild;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- PREMOVAL_DEVICE_ENTRY RemoveChild;
- BOOL Result;
- RtlDebugPrint("AddRemoveChild: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("AddRemoveChild: Failed to attach parent device.\n");
- goto TestAddRemoveChildEnd;
- }
- //
- // Get a child entry to remove and mark it for removal.
- //
- ASSERT(LIST_EMPTY(&(ParentEntry->ChildListHead)) == FALSE);
- RemoveChild = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Next,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- DeviceRemovalDetachDevice(RemoveChild);
- //
- // Attach a new child to the parent device and notify the system that the
- // parent device's topology changed.
- //
- AddChild = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (AddChild == NULL) {
- RtlDebugPrint("AddRemoveChild 0: Failed to allocate child device.\n");
- }
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- //
- // Now remove the parent device.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait for the removal.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Check the state of the tree.
- //
- Result = TRUE;
- if ((AddChild->RemovalIrp == FALSE) || (AddChild->Attached != FALSE)) {
- RtlDebugPrint("AddRemoveChild 0: Failed to remove added child.\n");
- Result = FALSE;
- }
- if ((RemoveChild->RemovalIrp == FALSE) ||
- (RemoveChild->Attached != FALSE)) {
- RtlDebugPrint("AddRemoveChild 0: Failed to remove child marked "
- "removed.\n");
- Result = FALSE;
- }
- if (Result != FALSE) {
- RtlDebugPrint("AddRemoveChild 0: Successful!\n");
- }
- //
- // Destroy the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Now do it where the system gets notified of the parent's removal first.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry == NULL) {
- RtlDebugPrint("AddRemoveChild: Failed to attach parent device.\n");
- goto TestAddRemoveChildEnd;
- }
- //
- // Get a child entry to remove and mark it for removal.
- //
- ASSERT(LIST_EMPTY(&(ParentEntry->ChildListHead)) == FALSE);
- RemoveChild = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(
- ParentEntry->ChildListHead.Next,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- DeviceRemovalDetachDevice(RemoveChild);
- //
- // Attach a new child to the parent device and notify the system that the
- // parent device's topology changed.
- //
- AddChild = DeviceRemovalAttachChildDevice(ParentEntry->BusContext);
- if (AddChild == NULL) {
- RtlDebugPrint("AddRemoveChild 1: Failed to allocate child device.\n");
- }
- //
- // Mark the parent for removal and notify the system.
- //
- DeviceRemovalDetachDevice(ParentEntry);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // If the children haven't received removal IRPs yet, signal the system.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- if ((AddChild->RemovalIrp == FALSE) || (RemoveChild->RemovalIrp == FALSE)) {
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Wait for the removal.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Check the state of the tree.
- //
- Result = TRUE;
- if ((AddChild->RemovalIrp == FALSE) || (AddChild->Attached != FALSE)) {
- RtlDebugPrint("AddRemoveChild 1: Failed to remove added child.\n");
- Result = FALSE;
- }
- if ((RemoveChild->RemovalIrp == FALSE) ||
- (RemoveChild->Attached != FALSE)) {
- RtlDebugPrint("AddRemoveChild 1: Failed to remove child marked "
- "removed.\n");
- Result = FALSE;
- }
- if (Result != FALSE) {
- RtlDebugPrint("AddRemoveChild 1: Successful!\n");
- }
- //
- // Destroy the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestAddRemoveChildEnd:
- return;
- }
- VOID
- DeviceRemovalTestAddRemoveSibling (
- VOID
- )
- /*++
- Routine Description:
- This routine implements the add/remove sibling test. This test adds a
- device tree while removing another.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY FirstSibling;
- PREMOVAL_DEVICE_ENTRY SecondSibling;
- RtlDebugPrint("AddRemoveSibling: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- FirstSibling = DeviceRemovalInitializeTreeForTest();
- if (FirstSibling == NULL) {
- RtlDebugPrint("AddRemoveSibling: Failed to attach first sibling.\n");
- goto TestAddRemoveSiblingEnd;
- }
- //
- // Now attach another device to the root, remove the one that was just
- // created and then notify the system.
- //
- SecondSibling = DeviceRemovalAttachChildDevice(RootDevice);
- if (SecondSibling == NULL) {
- RtlDebugPrint("AddRemoveSibling: Failed to attach second sibling.\n");
- }
- DeviceRemovalDetachDevice(FirstSibling);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- //
- // Wait for the first sibling to be removed.
- //
- while (FirstSibling->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Wait for the second sibling to be created.
- //
- DeviceRemovalWaitForTreeCreation(SecondSibling);
- //
- // If it made it this far, it succeeeded.
- //
- RtlDebugPrint("AddRemoveSibling: Successful!\n");
- //
- // Remove the second sibling's tree.
- //
- DeviceRemovalDetachDevice(SecondSibling);
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- while (SecondSibling->RemovalIrp == FALSE) {
- KeYield();
- }
- //
- // Clean up the accounting structures.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(FirstSibling);
- DeviceRemovalDestroyTree(SecondSibling);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestAddRemoveSiblingEnd:
- return;
- }
- VOID
- DeviceRemovalTestCascadeRemove (
- VOID
- )
- /*++
- Routine Description:
- This routine implements the cascade removal test. This test sends removal
- notifications about multiple devices that are in a device hierarchy.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- RtlDebugPrint("CascadeRemove: Started.\n");
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- ParentEntry = DeviceRemovalInitializeTreeForTest();
- if (ParentEntry== NULL) {
- RtlDebugPrint("CascadeRemove: Failed to attach root entry.\n");
- goto TestCascadeRemoveEnd;
- }
- //
- // Mark each element in the tree for removal.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalCascadeRemoveHelper(ParentEntry,
- RootDevice->TreeEntry->DeviceToken);
- KeReleaseSpinLock(&DeviceTreeLock);
- //
- // Wait for the parent device to be removed. Its removal process will
- // assert if any other removal failed.
- //
- while (ParentEntry->RemovalIrp == FALSE) {
- KeYield();
- }
- RtlDebugPrint("CascadeRemove: Successful!\n");
- //
- // Clean up the accounting structures.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- DeviceRemovalDestroyTree(ParentEntry);
- KeReleaseSpinLock(&DeviceTreeLock);
- TestCascadeRemoveEnd:
- return;
- }
- VOID
- DeviceRemovalCascadeRemoveHelper (
- PREMOVAL_DEVICE_ENTRY RootEntry,
- PVOID ParentDeviceToken
- )
- /*++
- Routine Description:
- This routine recursively marks every device in a tree for removal,
- notifying the system along the way. It does a post-order traversal.
- Arguments:
- RootEntry - Supplies a pointer to the root of the device tree that needs
- to be removed.
- ParentDeviceToken - Supplies a pointer to the device token of the root's
- parent device.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- CurrentEntry = RootEntry->ChildListHead.Next;
- while (CurrentEntry != &(RootEntry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- CurrentEntry = CurrentEntry->Next;
- DeviceRemovalCascadeRemoveHelper(ChildEntry, RootEntry->DeviceToken);
- }
- DeviceRemovalDetachDevice(RootEntry);
- IoNotifyDeviceTopologyChange(ParentDeviceToken);
- return;
- }
- VOID
- DeviceRemovalTestRandom (
- VOID
- )
- /*++
- Routine Description:
- This routine walks the device tree and marks some devices as detached.
- Arguments:
- Parameter - Supplies an optional parameter for the work item routine.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY DetachEntry;
- PREMOVAL_DEVICE_ENTRY NewEntry;
- PREMOVAL_DEVICE_ENTRY ParentEntry;
- BOOL QueueFailure;
- BOOL TopologyChanged;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- ASSERT(RemovalDeviceTree != NULL);
- TopologyChanged = FALSE;
- //
- // Acquire the tree lock before operating on the tree and the random test's
- // variables.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- //
- // Clean up the device tree's lingering test structures based on the clean
- // up rate.
- //
- RandomTestCount += 1;
- if ((RandomTestCount % RANDOM_TEST_CLEAN_TREE_RATE) == 0) {
- DeviceRemovalCleanTree(RemovalDeviceTree, NULL);
- }
- //
- // Record that a device queue failure should be added after the system
- // is notified of the change.
- //
- if ((RandomTestCount % DEVICE_QUEUE_FAILURE_RATE) == 0) {
- QueueFailure = TRUE;
- } else {
- QueueFailure = FALSE;
- }
- //
- // This test alternates between adding and removing a device from the tree.
- //
- ParentEntry = NULL;
- if (RandomRemoveDevice != FALSE) {
- RandomRemoveDevice = FALSE;
- ASSERT((RandomRemoveLevel <= RANDOM_REMOVE_END_LEVEL) &&
- (RandomRemoveLevel >= RANDOM_REMOVE_START_LEVEL));
- //
- // Pick a device at the current removal level and detach it.
- //
- DetachEntry = DeviceRemovalFindDeviceAndDetach(
- RemovalDeviceTree,
- &ParentEntry,
- RANDOM_REMOVE_START_LEVEL);
- if (DetachEntry != NULL) {
- ASSERT(DetachEntry->Attached == FALSE);
- ASSERT(ParentEntry != NULL);
- TopologyChanged = TRUE;
- }
- if (RandomRemoveLevel == RANDOM_REMOVE_END_LEVEL) {
- RandomRemoveLevel = RANDOM_REMOVE_START_LEVEL;
- } else {
- RandomRemoveLevel += 1;
- }
- //
- // Add a new device tree at some layer within the existing tree.
- //
- } else {
- RandomRemoveDevice = TRUE;
- ASSERT(RandomAddLevel <= RANDOM_ADD_END_LEVEL);
- NewEntry = DeviceRemovalFindDeviceAndAddChild(RemovalDeviceTree,
- &ParentEntry,
- RANDOM_ADD_START_LEVEL);
- if (NewEntry != NULL) {
- ASSERT(ParentEntry != NULL);
- TopologyChanged = TRUE;
- }
- if (RandomAddLevel == RANDOM_ADD_END_LEVEL) {
- RandomAddLevel = RANDOM_ADD_START_LEVEL;
- } else {
- RandomAddLevel += 1;
- }
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- if (TopologyChanged != FALSE) {
- ASSERT(ParentEntry != NULL);
- //
- // Inform the system that it might want to check the device tree again.
- // This is simulating the actions a bus driver might take when it
- // notices that a child has been detached.
- //
- IoNotifyDeviceTopologyChange(ParentEntry->DeviceToken);
- if (QueueFailure != FALSE) {
- IoSetTestHook(IO_FAIL_QUEUE_DEVICE_WORK);
- }
- }
- return;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindDeviceAndDetach (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY *ParentEntry,
- ULONG Level
- )
- /*++
- Routine Description:
- This routine recurses over the device tree looking for a device to detach
- at the current removal level. It detaches the device if it finds one.
- Arguments:
- Entry - Supplies a pointer to the entry currently being evaluated for
- removal.
- ParentEntry - Supplies a pointer that receives the parent device of the
- device marked for removal.
- Level - Supplies the current tree level of the search.
- Return Value:
- Returns a tree entry if the level matches the removal level and the device
- is attached or if a child of the tree entry matches the requirements.
- Returns NULL if the above requirements are not met by the device or any of
- the devices in its tree.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- PREMOVAL_DEVICE_ENTRY DetachEntry;
- //
- // Recurse over each child looking for a device to remove from the
- // appropriate level.
- //
- DetachEntry = NULL;
- CurrentEntry = Entry->ChildListHead.Next;
- while (CurrentEntry != &(Entry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- //
- // If the current device fits the criteria, return it.
- //
- if ((Level == RandomRemoveLevel) && (ChildEntry->Attached != FALSE)) {
- DeviceRemovalDetachDevice(ChildEntry);
- *ParentEntry = Entry;
- return ChildEntry;
- }
- //
- // Otherwise recurse on the device.
- //
- DetachEntry = DeviceRemovalFindDeviceAndDetach(ChildEntry,
- ParentEntry,
- Level + 1);
- if (DetachEntry != NULL) {
- break;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- return DetachEntry;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindDeviceAndAddChild (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY *ParentEntry,
- ULONG Level
- )
- /*++
- Routine Description:
- This routine recurses over the device tree looking for a device to which it
- will add a child. The requirement is that the device is fully initialized
- and remains attached.
- Arguments:
- Entry - Supplies a pointer to the entry currently being evaluated for
- child addition.
- ParentEntry - Supplies a pointer that receives the parent entry of the new
- device tree entry.
- Level - Supplies the current tree level of the search.
- Return Value:
- Returns a new device that was attached to the current entry. Or NULL if no
- device could be found that are prepared for new children.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- PDEVICE Device;
- PREMOVAL_DEVICE_ENTRY NewEntry;
- //
- // If the current device fits the criteria, then add a child and return the
- // new child.
- //
- Device = (PDEVICE)Entry->DeviceToken;
- if ((Level == RandomAddLevel) &&
- (Entry->Attached != FALSE) &&
- (Entry->BusContext != NULL) &&
- (IoIsDeviceStarted(Device) != FALSE)) {
- ASSERT(Entry->RemovalIrp == FALSE);
- NewEntry = DeviceRemovalAttachChildDeviceHelper(Entry->BusContext);
- *ParentEntry = Entry;
- return NewEntry;
- }
- //
- // Recurse over each child looking for a device to which to add a child.
- //
- NewEntry = NULL;
- CurrentEntry = Entry->ChildListHead.Next;
- while (CurrentEntry != &(Entry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- NewEntry = DeviceRemovalFindDeviceAndAddChild(ChildEntry,
- ParentEntry,
- Level + 1);
- if (NewEntry != NULL) {
- break;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- return NewEntry;
- }
- VOID
- DeviceRemovalDestroyTree (
- PREMOVAL_DEVICE_ENTRY RootEntry
- )
- /*++
- Routine Description:
- This routine destroys the device removal tree tracking structures.
- Arguments:
- RootEntry - Supplies a pointer to the root of the tree that needs ot be
- destroyed.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- CurrentEntry = RootEntry->ChildListHead.Next;
- while (CurrentEntry != &(RootEntry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- CurrentEntry = CurrentEntry->Next;
- DeviceRemovalDestroyTree(ChildEntry);
- }
- ASSERT(RootEntry->Attached == FALSE);
- ASSERT(RootEntry->RemovalIrp != FALSE);
- DeviceRemovalDeleteTreeEntry(RootEntry);
- return;
- }
- VOID
- DeviceRemovalCleanTree (
- PREMOVAL_DEVICE_ENTRY Entry,
- PREMOVAL_DEVICE_ENTRY ParentEntry
- )
- /*++
- Routine Description:
- This routine destroys the device removal tree tracking structures if they
- have received the removal IRP and their parent has received the removal
- IRP.
- Arguments:
- Entry - Supplies a pointer to the local root of the tree that needs ot be
- destroyed.
- ParentEntry - Supplies a pointer to the parent of the local tree root that
- needs to be destroyed.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- CurrentEntry = Entry->ChildListHead.Next;
- while (CurrentEntry != &(Entry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- CurrentEntry = CurrentEntry->Next;
- DeviceRemovalCleanTree(ChildEntry, Entry);
- }
- //
- // If both the entry and the parent entry have received the removal IRP,
- // then this structure can be cleaned up. Or if the parent is the root
- // device.
- //
- if ((Entry->RemovalIrp != FALSE) &&
- (ParentEntry != NULL) &&
- ((ParentEntry->RemovalIrp != FALSE) ||
- (ParentEntry == RemovalDeviceTree))) {
- ASSERT(LIST_EMPTY(&(Entry->ChildListHead)) != FALSE);
- DeviceRemovalDeleteTreeEntry(Entry);
- }
- return;
- }
- VOID
- DeviceRemovalNukeTree (
- VOID
- )
- /*++
- Routine Description:
- This routine removes all the subtrees from the root device. It current
- cannot remove the root because it is attached to the root device which
- doesn't accept query children commands.
- Arguments:
- None.
- Return Value:
- None.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- //
- // Acquire the tree lock before traversing the tree.
- //
- KeAcquireSpinLock(&DeviceTreeLock);
- //
- // Disable IRP and queue failures before nuking the tree.
- //
- RemovalIrpFailEnabled = FALSE;
- DeviceQueueFailEnabled = FALSE;
- IoClearTestHook(IO_FAIL_QUEUE_DEVICE_WORK);
- //
- // Mark each one of the root device's children as detached.
- //
- CurrentEntry = RemovalDeviceTree->ChildListHead.Next;
- while (CurrentEntry != &(RemovalDeviceTree->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- DeviceRemovalDetachDevice(ChildEntry);
- CurrentEntry = CurrentEntry->Next;
- }
- //
- // Notify the root device that its device topology has changed.
- //
- IoNotifyDeviceTopologyChange((PDEVICE)RemovalDeviceTree->DeviceToken);
- //
- // Wait for the root's children to all receive removal IRPs. Destroy the
- // test tracking tree for each child.
- //
- CurrentEntry = RemovalDeviceTree->ChildListHead.Next;
- while (CurrentEntry != &(RemovalDeviceTree->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- while (ChildEntry->RemovalIrp == FALSE) {
- KeReleaseSpinLock(&DeviceTreeLock);
- KeYield();
- KeAcquireSpinLock(&DeviceTreeLock);
- }
- DeviceRemovalDestroyTree(ChildEntry);
- CurrentEntry = CurrentEntry->Next;
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- return;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalInitializeTreeForTest (
- VOID
- )
- /*++
- Routine Description:
- This routine initializes a tree for device removal testing. It attaches a
- device to the root node, notifies the system, and then waits for the
- children beneath the tree to be fully created.
- Arguments:
- None.
- Return Value:
- Returns the a pointer to the root device of the tree created, or NULL on
- failure.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY TreeEntry;
- //
- // Add a tree of 1->2->4 beneath the root, notify the system, and wait
- // until it is enumerated.
- //
- TreeEntry = DeviceRemovalAttachChildDevice(RootDevice);
- if (TreeEntry == NULL) {
- return NULL;
- }
- IoNotifyDeviceTopologyChange(RootDevice->TreeEntry->DeviceToken);
- DeviceRemovalWaitForTreeCreation(TreeEntry);
- return TreeEntry;
- }
- VOID
- DeviceRemovalWaitForTreeCreation (
- PREMOVAL_DEVICE_ENTRY RootEntry
- )
- /*++
- Routine Description:
- This routine waits for a device tree to be fully attached. It is used as a
- blocking mechanism to wait for device enumeration to finish before testing.
- It will yield the processor if the tree is not complete.
- Arguments:
- RootEntry - Supplies a pointer to the root of the device tree that needs
- to be evaluated.
- Return Value:
- None.
- --*/
- {
- ULONG ChildCount;
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- PDEVICE Device;
- //
- // Wait for the device to hit the started state. If it does not, then exit.
- //
- Device = RootEntry->DeviceToken;
- ObWaitOnObject(Device, 0, WAIT_TIME_INDEFINITE);
- if (IoIsDeviceStarted(Device) == FALSE) {
- return;
- }
- //
- // The bus context should be filled by now.
- //
- ASSERT(RootEntry->BusContext != NULL);
- //
- // Now the child count should be filled in, exit if there are no children.
- //
- if (RootEntry->BusContext->ChildCount == 0) {
- return;
- }
- //
- // Wait for the children tree entries to appear.
- //
- do {
- ChildCount = 0;
- KeAcquireSpinLock(&DeviceTreeLock);
- CurrentEntry = RootEntry->ChildListHead.Next;
- while (CurrentEntry != &(RootEntry->ChildListHead)) {
- ChildCount += 1;
- CurrentEntry = CurrentEntry->Next;
- }
- KeReleaseSpinLock(&DeviceTreeLock);
- KeYield();
- } while (ChildCount != RootEntry->BusContext->ChildCount);
- //
- // Recurse on each child of this device.
- //
- CurrentEntry = RootEntry->ChildListHead.Next;
- while (CurrentEntry != &(RootEntry->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- DeviceRemovalWaitForTreeCreation(ChildEntry);
- CurrentEntry = CurrentEntry->Next;
- }
- return;
- }
- VOID
- DeviceRemovalDetachDevice (
- PREMOVAL_DEVICE_ENTRY DeviceEntry
- )
- /*++
- Routine Description:
- This routine detachs a device from the system.
- Arguments:
- DeviceEntry - Supplies a pointer to the device that will be detached.
- Return Value:
- None.
- --*/
- {
- ASSERT(DeviceEntry != NULL);
- DeviceEntry->Attached = FALSE;
- return;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalAttachChildDevice (
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine attaches a child device to the given device.
- Arguments:
- Device - Supplies a pointer to the device to whom a child device will be
- attached.
- Return Value:
- Returns the newly created child device, or NULL on failure.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- KeAcquireSpinLock(&DeviceTreeLock);
- ChildEntry = DeviceRemovalAttachChildDeviceHelper(Device);
- KeReleaseSpinLock(&DeviceTreeLock);
- return ChildEntry;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalAttachChildDeviceHelper (
- PREMOVAL_DEVICE Device
- )
- /*++
- Routine Description:
- This routine attaches a child device to the given device.
- Arguments:
- Device - Supplies a pointer to the device to whom a child device will be
- attached.
- Return Value:
- Returns the newly created child device, or NULL on failure.
- --*/
- {
- ULONG ChildCount;
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- ULONG ChildIndex;
- CHAR ClassId[DEVR_CLASS_ID_SIZE];
- PDEVICE *NewChildren;
- PREMOVAL_DEVICE NewContext;
- ULONG OldChildCount;
- PDEVICE *OldChildren;
- KSTATUS Status;
- NewChildren = NULL;
- NewContext = NULL;
- ChildEntry = NULL;
- //
- // Allocate an array for child device pointers.
- //
- ChildCount = Device->ChildCount + 1;
- NewChildren = MmAllocatePagedPool(sizeof(PDEVICE) * ChildCount,
- DEVR_ALLOCATION_TAG);
- if (NewChildren == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AttachChildDeviceEnd;
- }
- //
- // Copy the current childen to the new array.
- //
- ASSERT(((Device->ChildCount == 0) && (Device->Children == NULL)) ||
- ((Device->ChildCount != 0) && (Device->Children != NULL)));
- OldChildCount = Device->ChildCount;
- RtlZeroMemory(NewChildren, sizeof(PDEVICE) * ChildCount);
- for (ChildIndex = 0; ChildIndex < OldChildCount; ChildIndex += 1) {
- NewChildren[ChildIndex] = Device->Children[ChildIndex];
- }
- OldChildren = Device->Children;
- //
- // Create the class ID for the children.
- //
- RtlPrintToString(ClassId,
- DEVR_CLASS_ID_SIZE,
- CharacterEncodingDefault,
- DEVR_CLASS_ID_FORMAT,
- (Device->Level + 1));
- //
- // Create a new device and track it in the global device tree.
- //
- NewContext = MmAllocateNonPagedPool(sizeof(REMOVAL_DEVICE),
- DEVR_ALLOCATION_TAG);
- if (NewContext == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AttachChildDeviceEnd;
- }
- RtlAtomicAdd32(&FunctionDeviceCount, 1);
- RtlZeroMemory(NewContext, sizeof(REMOVAL_DEVICE));
- NewContext->Type = DeviceRemovalFunction;
- NewContext->Root = FALSE;
- //
- // Create a tree entry for the child device and initialize it.
- //
- ChildEntry = DeviceRemovalCreateTreeEntry(NewContext,
- Device->TreeEntry);
- if (ChildEntry == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto AttachChildDeviceEnd;
- }
- //
- // Create the child device and fill out the accounting structures.
- //
- Status = IoCreateDevice(DeviceRemovalDriver,
- NewContext,
- Device->TreeEntry->DeviceToken,
- DEVR_CHILD_ID,
- ClassId,
- NULL,
- &(NewChildren[ChildCount - 1]));
- if (!KSUCCESS(Status)) {
- RtlDebugPrint("IoCreateDevice failed with status %d.\n", Status);
- goto AttachChildDeviceEnd;
- }
- ChildEntry->DeviceToken = NewChildren[ChildCount - 1];
- //
- // Update the devices children now that the routine will be successful.
- //
- Device->Children = NewChildren;
- Device->ChildCount = ChildCount;
- if (OldChildren != NULL) {
- MmFreePagedPool(OldChildren);
- }
- AttachChildDeviceEnd:
- if (!KSUCCESS(Status)) {
- if (NewChildren != NULL) {
- MmFreePagedPool(NewChildren);
- }
- if (NewContext != NULL) {
- MmFreeNonPagedPool(NewContext);
- RtlAtomicAdd32(&FunctionDeviceCount, (ULONG)-1);
- }
- if (ChildEntry != NULL) {
- DeviceRemovalDeleteTreeEntry(ChildEntry);
- }
- }
- return ChildEntry;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalCreateTreeEntry (
- PREMOVAL_DEVICE DeviceContext,
- PREMOVAL_DEVICE_ENTRY ParentEntry
- )
- /*++
- Routine Description:
- This routine allocates and initializes a removal device tree entry.
- Arguments:
- DeviceContext - Supplies a pointer to one of two device contexts associated
- with the tree entry.
- ParentEntry - Supplies a pointer to the parent device tree entry.
- Return Value:
- Returns a pointer to the tree entry on success, or NULL on failure.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY DeviceEntry;
- ASSERT(DeviceContext != NULL);
- DeviceEntry = MmAllocatePagedPool(sizeof(REMOVAL_DEVICE_ENTRY),
- DEVR_ALLOCATION_TAG);
- if (DeviceEntry == NULL) {
- return NULL;
- }
- RtlZeroMemory(DeviceEntry, sizeof(REMOVAL_DEVICE_ENTRY));
- if (DeviceContext->Type == DeviceRemovalFunction) {
- DeviceEntry->FunctionContext = DeviceContext;
- } else {
- DeviceEntry->BusContext = DeviceContext;
- }
- INITIALIZE_LIST_HEAD(&(DeviceEntry->ChildListHead));
- if (ParentEntry == NULL) {
- INITIALIZE_LIST_HEAD(&(DeviceEntry->SiblingEntry));
- } else {
- INSERT_AFTER(&(DeviceEntry->SiblingEntry),
- &(ParentEntry->ChildListHead));
- }
- DeviceContext->TreeEntry = DeviceEntry;
- DeviceEntry->Attached = TRUE;
- DeviceEntry->RemovalIrp = FALSE;
- RtlAtomicAdd32(&DeviceEntryCount, 1);
- return DeviceEntry;
- }
- VOID
- DeviceRemovalDeleteTreeEntry (
- PREMOVAL_DEVICE_ENTRY Entry
- )
- /*++
- Routine Description:
- This routine removes and deletes a device tree entry.
- Arguments:
- Entry - Supplies a pointer to the tree entry to be deleted.
- Return Value:
- None.
- --*/
- {
- LIST_REMOVE(&(Entry->SiblingEntry));
- MmFreePagedPool(Entry);
- RtlAtomicAdd32(&DeviceEntryCount, (ULONG)-1);
- return;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindChildByToken (
- PREMOVAL_DEVICE_ENTRY Root,
- PVOID DeviceToken
- )
- /*++
- Routine Description:
- This routine searches for a device underneath the supplied root.
- Arguments:
- Root - Supplies a pointer to the search's root device entry.
- DeviceToken - Supplies an opaque token used to identify the device.
- Return Value:
- Returns a device entry upon success, NULL on failure.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY ChildEntry;
- PLIST_ENTRY CurrentEntry;
- PREMOVAL_DEVICE_ENTRY ResultEntry;
- ASSERT(KeGetRunLevel() == RunLevelLow);
- //
- // In order to optimize the case where an immediate child is being sought,
- // perform a breadth first search. Look at the root's children first.
- //
- CurrentEntry = Root->ChildListHead.Next;
- while (CurrentEntry != &(Root->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- //
- // The token has to match and it cannot yet be removed. Device tokens
- // get reused.
- //
- if ((ChildEntry->DeviceToken == DeviceToken) &&
- (ChildEntry->RemovalIrp == FALSE)) {
- return ChildEntry;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- //
- // Now recurse over each of the children.
- //
- CurrentEntry = Root->ChildListHead.Next;
- while (CurrentEntry != &(Root->ChildListHead)) {
- ChildEntry = (PREMOVAL_DEVICE_ENTRY)LIST_VALUE(CurrentEntry,
- REMOVAL_DEVICE_ENTRY,
- SiblingEntry);
- ResultEntry = DeviceRemovalFindChildByToken(ChildEntry, DeviceToken);
- if (ResultEntry != NULL) {
- return ResultEntry;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- //
- // If nothing is found, then the device does not exist.
- //
- return NULL;
- }
- PREMOVAL_DEVICE_ENTRY
- DeviceRemovalFindEntryByToken (
- PVOID DeviceToken
- )
- /*++
- Routine Description:
- This routine searches for a device underneath the tree root.
- Arguments:
- Root - Supplies a pointer to the search's root device entry.
- DeviceToken - Supplies an opaque token used to identify the device.
- Return Value:
- Returns a device entry upon success, NULL on failure.
- --*/
- {
- PREMOVAL_DEVICE_ENTRY TreeEntry;
- KeAcquireSpinLock(&DeviceTreeLock);
- TreeEntry = DeviceRemovalFindChildByToken(RemovalDeviceTree, DeviceToken);
- KeReleaseSpinLock(&DeviceTreeLock);
- return TreeEntry;
- }
|