Linux USB驱动程序基础.docx

上传人:b****6 文档编号:12278117 上传时间:2023-06-05 格式:DOCX 页数:17 大小:23.27KB
下载 相关 举报
Linux USB驱动程序基础.docx_第1页
第1页 / 共17页
Linux USB驱动程序基础.docx_第2页
第2页 / 共17页
Linux USB驱动程序基础.docx_第3页
第3页 / 共17页
Linux USB驱动程序基础.docx_第4页
第4页 / 共17页
Linux USB驱动程序基础.docx_第5页
第5页 / 共17页
Linux USB驱动程序基础.docx_第6页
第6页 / 共17页
Linux USB驱动程序基础.docx_第7页
第7页 / 共17页
Linux USB驱动程序基础.docx_第8页
第8页 / 共17页
Linux USB驱动程序基础.docx_第9页
第9页 / 共17页
Linux USB驱动程序基础.docx_第10页
第10页 / 共17页
Linux USB驱动程序基础.docx_第11页
第11页 / 共17页
Linux USB驱动程序基础.docx_第12页
第12页 / 共17页
Linux USB驱动程序基础.docx_第13页
第13页 / 共17页
Linux USB驱动程序基础.docx_第14页
第14页 / 共17页
Linux USB驱动程序基础.docx_第15页
第15页 / 共17页
Linux USB驱动程序基础.docx_第16页
第16页 / 共17页
Linux USB驱动程序基础.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

Linux USB驱动程序基础.docx

《Linux USB驱动程序基础.docx》由会员分享,可在线阅读,更多相关《Linux USB驱动程序基础.docx(17页珍藏版)》请在冰点文库上搜索。

Linux USB驱动程序基础.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2