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

1 / 50
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功