Linux USB驱动程序基础.docx
- 文档编号:12278117
- 上传时间:2023-06-05
- 格式:DOCX
- 页数:17
- 大小:23.27KB
Linux USB驱动程序基础.docx
《Linux USB驱动程序基础.docx》由会员分享,可在线阅读,更多相关《Linux USB驱动程序基础.docx(17页珍藏版)》请在冰点文库上搜索。
LinuxUSB驱动程序基础
LinuxUSB驱动程序基础
来源:
ChinaUnix博客 日期:
2008.04.1023:
55 (共有条评论)我要评论
∙
(
LinuxUSBDriverBasics
Introduction
Driversaresoftwarecomponentsthatoperatingsystemsusetoprovide
hardwarespecificservicestoapplications.Thiswebpageattemptstodocument
thebasicsofUSBdriversonLinux.Thegoalhereistoprovideyouwithabasic
understandingofhowUSBdevicedriversonLinuxwork.
TheFileAbstraction
Linux,likeotherUnixderivedoperatingsystems,triestomakeapplications
simplerbyprovidingacommonhardwareabstraction,theFile.Essentially,
interactionswithalmostallhardwarecanbeabstractedintothesameinterface
thattheoperatingsystemprovidesformanipulatingfiles.Hence,youcan"Open"
adriver,"Read"adriver,"Write"toadriverand"Close"adriver.This
abstractionextendsallthewayintotheapplication,whocanusethesame
systemcallsthatitusestoopenandmanipulatefilestotalktohardware.
Thewaythisworksisthatthekernelcreatesnodesinthefilesystem,
typicallylocatedin/devthatrepresentaparticularinterfacetoadriver.To
talktoaparticulardriveranapplicationwillopenthe/deventryassociated
withthatdriver.Thefiledescriptorreturnedbythatopenispassedtoall
futuresystemcalls(read,write,select),andiseventuallypassedto
close.
Besidesdrivers,Unixalsousesthisfileparadigmforvariousdifferent
kindsofIPC,andevenforsocketcommunicationsoveranetwork.
Itisquitesurprisinghowmanycompletelydifferentkindsofhardwarecanbe
modeledwithjust4operations(open,close,readandwrite).Thatsaid,their
isanotherveryimportantsystemcallthatunixapplicationsdeveloperscommonly
use,select().Theselect()systemcallallowsapplicationstopolland
determinewhetherdatacouldbereadfrom,orwrittentoafiledescriptor
withoutblocking.
Sometimesapieceofhardwareprovidessomefunctionalitythatdoesn'tfit
wellintothisfilecentricparadigm.Toallowforthis,unixapplications
typicallymakeuseoftheioctl()systemcall.Thiscalltakesanumericvalue
thatisessentiallyanidentifierforaspecificpieceoffunctionalityinthe
driver.
TheJoboftheDeviceDriver
Simplystated,itisthejobofthedrivertoprovidefunctionsthatthe
kernelcanusetoimplementthisfileprogrammingparadigm.Applicationsdonot
directlycallfunctionsinthedriver,insteadtheycallfunctionsinlibcthat
eventuallycallintothekernel(viathesystemcallinterface).Thekernel
implementationsofthesesystemcallscallintoyourdriver.
Asyoumightexpect,thismeansthatyou'regoingtobeimplementatingopen,
close,readandwriteinsideyourdriver.Toimplementselect(),thekernel
needsyoutoimplementamethodinyourdrivercalledpoll().Otherthanthese,
theirareonlyahandfullofotherfunctionsthatneedtobeimplemented,and
youhaveadriver!
LoadingaDriver
Linuxsupportsessentiallytwokindsofdriverdevelopmentmodels:
1)
Compiledintothekernel,2)Dynamicallyloadabledrivermodules.Ifconfigured
properly,option2hastheadvantagethatdriverscanbeloadedautomaticallyby
thekernelonlywhentheyareactuallyneededtoserviceanapplicationrequest.
Option1presentsalogicallycleaner,andslightlyfasterscenario.
Thechoiceisuptoyou.Thatsaid,wewillnowcoverthebasicsofloading
andunloadingakernelmoduleintoarunningkernel.
First,understandthataLinuxkerneldrivercannotbedynamicallylinked
againstanylibrarys(notevenlibc,whichmanyprogrammersdontevenknowthey
arelinkingagainst).Theonlycodeexternaltoyourdriverthatyourallowedto
makeuseofarefunctionsthatareimplementedwithinthekernel.Infact,
kernelmodulesarenever"Linked",insteadtheyarerelocatableobjectfiles,
.o'sorsometimes.ko's.
Thefollowingcommandisusedtoinstalladriverintoarunningkernel:
[td@objective~]$insmoddriver.ko
Thefollowingcommandisusedtoshowwhatdriversarecurrentlyloadedinto
thekernel:
[td@objective~]$lsmod
ModuleSizeUsedby
md540331
ipv626310522
ipt_REJECT54411
ipt_state18575
ip_conntrack413691ipt_state
iptable_filter28811
ip_tables195213ipt_REJECT,ipt_state,iptable_filter
video156850
button40330
battery92850
ac48050
uhci_hcd350250
ehci_hcd400130
hw_random59730
i2c_i80186530
i2c_core213131i2c_i801
snd_intel8x0341770
snd_ac97_codec749371snd_intel8x0
snd_pcm_oss506730
snd_mixer_oss177291snd_pcm_oss
snd_pcm988893snd_intel8x0,snd_ac97_codec
snd_timer328371snd_pcm
snd572856snd_intel8x0,snd_ac97_codec
soundcore107851snd
snd_page_alloc96692snd_intel8x0,snd_pcm
e10001025730
dm_snapshot174130
dm_zero21130
dm_mirror256450
ext31306332
jbd831611ext3
dm_mod573336dm_snapshot,dm_zero,dm_mirror
ata_piix94130
libata469171ata_piix
sd_mod202890
scsi_mod1463132libata,sd_mod
Thefollowingcommandisusedtoremoveadriverfromarunningkernel:
[td@objective~]$rmmoddriver.ko
Sinceyouneverlinkyourdriver,youwillnotknowaboutunresolvedsymbols
untilyouactuallyloadyourdriverintothekernel.Itisduringthe"insmod"
processthatthekernelbindsallofthecallstokernelfunctionsinyourcode
totheiractualcurrentlocationsinmemory.Thisprocessiscalled"resolving
symboldependancies".
UnderstandingtheUniversalSerialBus
Fromthehighestlevel
AsI'msureyouknowfromyourownuseofUSB,theUSBbus,besidesoffering
communicationsalsoofferspowertodevicesconnectedtoit.TheUSBcanoffer
upto500mAforthedevicesconnectedtoit.Devicesthatneedmorethanthis
canbeselfpowered.
Besidesthis,yourprobablyawareofthefactthattheUSBcanbeexpanded
withhubs.
Howhardwareisabouttomakeyourlifeeasier
Thankfully,yourdriverwillnothavetocommunicatedirectlyonthe
UniversalSerialBus.Instead,yourdriverwillcommunicatetotheUSBhost
controller,whichwillcommunicateonthebusonyourbehalf.TheUSBhost
controllerisapieceofhardwarethatactsasafocalpointforallofthe
CPU'sinteractionwiththeUSBhardware.Ithidesmostofthecomplexityof
dealingwithUSBandalsoprotectstheUSBhardwarefrompotentiallybadly
writtendriversoftwarethatmightotherwiseaffectthewholebus.
Theiraretwokindsofhostcontrollerhardwarecommonlyinuse,UHCI
(UniversalHostControllerInterface)andOHCI(OpenHostControllerInterface).
Thegoodnewsisthatbothofthesehostcontrollerspresentthesameinterface
toyourdriver,soyoureallydonthavetocareaboutwhatkindofhost
controllerhardwareispresent.Asyoumightexpect,UHCI&OHCIhardware
hasitsowndriver,thatthankfullyyouwillnotneedtotouch.Itwillbethe
jobofyourdrivertocommunicate(indirectly)withthehostcontrollerdriver,
toconfigure,readfromorwritetodevicesontheUSB.
TypesofCommunicationsontheUSB
USBdeviceshavethefullrangeofdifferentspeed,latencyandreliability
requirements.Thingslikemiceandkeyboardtransferonlysmallamountsofdata,
relativelyrarely,buttheycarealotaboutlatency.Noonelikeaslow
keyboard.
Ontheotherhand,USBwebcamsoftentransfercompressedMPEGvideo,whichis
extremelyhighbandwidth,butbecauseMPEGwasdesignedtobetransmittedover
lossycommunicationschannels,itisOKiftheoccasionalpacketislost.
Tosupportthesedifferingrequirements,USBsupportsanumberof
communicationstypes.
ControlTransfers-Controltransfersareusedwhenyouneed
reliable(thedataMUSTgettheir)communicationsandyouaresendingavery
smallamountofdata.Essentially,thesetransfersarecommands,andtheirarea
fewcommandsthateverydeviceisrequiredtosupport(GET_STATUS,
CLEAR_FEATURE,SET_FEATURE,SET_ADDRESS,GET_DESCRIPTOR,SET_DESCRIPTOR,
GET_CONFIGURATION,SET_CONFIGURATION,GET_INTERFACE,SET_INTERFACE,
SYNCH_FRAME).
∙
BulkTransfers-Bulktransfersareusedwhenyouneed
reliable(thedataMUSTgettheir)communicationsandyouaresendinglarge
amountsofdata.
∙
InterruptTransfers-Interrupttransfersaresimmilarto
bulktransfersexcepttheyareconfiguredtooccurautomaticallyatsome
interval.Thesetypesoftransfersareusefulfordevicesthatstreamconstant
dataoftheUSB.
∙
IsochronousTransfers-Isochronoustransfersareveryfast,
andhaveguaranteedbusbandwidth,buttheyhavenoreliability.Thistransfer
typeseemstohavebeendesignedwithMPEGinmind.
Thecodethatyouwriteisgoingtohavetospecifytothehostcontroller
whattypesofcommuncationsyouwanttouse.Thankfully,anextremelywiderange
ofdevicetypesfitnicelyintotheabovetypesofdatatransfers.
TheheirarchicalviewoftheUSB
IfyouthinkaboutwhatIhavedescribedsofar,itshouldbefairlyapparent
thatsinceyouwillbedealingonlyindirectlywithactualUSBdevicesand
insteadbecommunicatingthroughahostcontroller,thathostcontrollermust
havesomekindofdatamodelforthedevicesthatareconnectedtoitontheUSB
bus.Itmustpresentsomedescriptionofthedevicesconnected,andtheir
capabilities.
ThecapabilitesofaUSBdevicearemodeledheirarchicallybythehost
controller.YourdriverwillcommunicatetoaUSBdevicebymakingrequeststo
nodesofthisheirarchy.Tofullyunderstandthereasoningforallofthelayers
oftheheirarchyIamabouttotellyouaboutpleaseconsiderhowgenericahost
controllermustbe,givenhowwiderangingthefunctionalityofUSBhardwarecan
be.
WhenaUSBisfirstpluggedintothebusitisdetectedbythehost
controller.ThehostcontrollersendsittheGET_DESCRIPTORcommandand
retrieveswhatiscalledthedevicedescr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Linux USB驱动程序基础 USB 驱动程序 基础