您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Linux Device Driver ch14.pdf
ThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.362Chapter14CHAPTER14TheLinuxDeviceModelOneofthestatedgoalsforthe2.5developmentcyclewasthecreationofaunifieddevicemodelforthekernel.Previouskernelshadnosingledatastructuretowhichtheycouldturntoobtaininformationabouthowthesystemisputtogether.Despitethislackofinformation,thingsworkedwellforsometime.Thedemandsofnewersystems,withtheirmorecomplicatedtopologiesandneedtosupportfeaturessuchaspowermanagement,madeitclear,however,thatageneralabstractiondescribingthestructureofthesystemwasneeded.The2.6devicemodelprovidesthatabstraction.Itisnowusedwithinthekerneltosupportawidevarietyoftasks,including:PowermanagementandsystemshutdownTheserequireanunderstandingofthesystem’sstructure.Forexample,aUSBhostadaptorcannotbeshutdownbeforedealingwithallofthedevicescon-nectedtothatadaptor.Thedevicemodelenablesatraversalofthesystem’shardwareintherightorder.CommunicationswithuserspaceTheimplementationofthesysfsvirtualfilesystemistightlytiedintothedevicemodelandexposesthestructurerepresentedbyit.Theprovisionofinformationaboutthesystemtouserspaceandknobsforchangingoperatingparametersisincreasinglydonethroughsysfsand,therefore,throughthedevicemodel.HotpluggabledevicesComputerhardwareisincreasinglydynamic;peripheralscancomeandgoatthewhimoftheuser.Thehotplugmechanismusedwithinthekerneltohandleand(especially)communicatewithuserspaceaboutthepluggingandunpluggingofdevicesismanagedthroughthedevicemodel.DeviceclassesManypartsofthesystemhavelittleinterestinhowdevicesareconnected,buttheyneedtoknowwhatkindsofdevicesareavailable.Thedevicemodelincludesamechanismforassigningdevicestoclasses,whichdescribethose,ch14.12359Page362Tuesday,January25,20051:54PMThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.TheLinuxDeviceModel|363devicesatahigher,functionallevelandallowthemtobediscoveredfromuserspace.ObjectlifecyclesManyofthefunctionsdescribedabove,includinghotplugsupportandsysfs,complicatethecreationandmanipulationofobjectscreatedwithinthekernel.Theimplementationofthedevicemodelrequiredthecreationofasetofmecha-nismsfordealingwithobjectlifecycles,theirrelationshipstoeachother,andtheirrepresentationinuserspace.TheLinuxdevicemodelisacomplexdatastructure.Forexample,considerFigure14-1,whichshows(insimplifiedform)atinypieceofthedevicemodelstruc-tureassociatedwithaUSBmouse.Downthecenterofthediagram,weseethepartofthecore“devices”treethatshowshowthemouseisconnectedtothesystem.The“bus”treetrackswhatisconnectedtoeachbus,whilethesubtreeunder“classes”concernsitselfwiththefunctionsprovidedbythedevices,regardlessofhowtheyareconnected.Thedevicemodeltreeonevenasimplesystemcontainshundredsofnodeslikethoseshowninthediagram;itisadifficultdatastructuretovisualizeasawhole.Forthemostpart,theLinuxdevicemodelcodetakescareofalltheseconsiderationswithoutimposingitselfupondriverauthors.Itsitsmostlyinthebackground;directinteractionwiththedevicemodelisgenerallyhandledbybus-levellogicandvariousotherkernelsubsystems.Asaresult,manydriverauthorscanignorethedevicemodelentirely,andtrustittotakecareofitself.Therearetimes,however,whenanunderstandingofthedevicemodelisagoodthingtohave.Therearetimeswhenthedevicemodel“leaksout”frombehindtheotherlayers;forexample,thegenericDMAcode(whichweencounterinFigure14-1.AsmallpieceofthedevicemodelBusesDevicesClassesInputdevsMouse1pci()dev0:10usb2port1dev1:1-0usbdriversdevicesusb-hid,ch14.12359Page363Tuesday,January25,20051:54PMThisistheTitleoftheBook,eMatterEditionCopyright©2005O’Reilly&Associates,Inc.Allrightsreserved.364|Chapter14:TheLinuxDeviceModelChapter15)workswithstructdevice.Youmaywanttousesomeofthecapabili-tiesprovidedbythedevicemodel,suchasthereferencecountingandrelatedfea-turesprovidedbykobjects.Communicationwithuserspaceviasysfsisalsoadevicemodelfunction;thischapterexplainshowthatcommunicationworks.Westart,however,withabottom-uppresentationofthedevicemodel.Thecom-plexityofthedevicemodelmakesithardtounderstandbystartingwithahigh-levelview.Ourhopeisthat,byshowinghowthelow-leveldevicecomponentswork,wecanprepareyouforthechallengeofgraspinghowthosecomponentsareusedtobuildthelargerstructure.Formanyreaders,thischaptercanbetreatedasadvancedmaterialthatneednotbereadthefirsttimethrough.ThosewhoareinterestedinhowtheLinuxdevicemodelworksareencouragedtopressahead,however,aswegetintothelow-leveldetails.Kobjects,Ksets,andSubsystemsThekobjectisthefundamentalstructurethatholdsthedevicemodeltogether.Itwasinitiallyconceivedasasimplereferencecounter,butitsresponsibilitieshavegrownovertime,andsohaveitsfields.Thetaskshandledbystructkobjectanditssup-portingcodenowinclude:ReferencecountingofobjectsOften,whenakernelobjectiscreated,thereisnowaytoknowjusthowlongitwillexist.Onewayoftrackingthelifecycleofsuchobjectsisthroughreferencecounting.Whennocodeinthekernelholdsareferencetoagivenobject,thatobjecthasfinisheditsusefullifeandcanbedeleted.SysfsrepresentationEveryobjectthatshowsupinsysfshas,underneathit,akobjectthatinteractswiththekerne
本文标题:Linux Device Driver ch14.pdf
链接地址:https://www.777doc.com/doc-5453919 .html