writing-an-alsa-driver.tmpl 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216
  1. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
  2. <book>
  3. <?dbhtml filename="index.html">
  4. <!-- ****************************************************** -->
  5. <!-- Header -->
  6. <!-- ****************************************************** -->
  7. <bookinfo>
  8. <title>Writing an ALSA Driver</title>
  9. <author>
  10. <firstname>Takashi</firstname>
  11. <surname>Iwai</surname>
  12. <affiliation>
  13. <address>
  14. <email>tiwai@suse.de</email>
  15. </address>
  16. </affiliation>
  17. </author>
  18. <date>Oct 15, 2007</date>
  19. <edition>0.3.7</edition>
  20. <abstract>
  21. <para>
  22. This document describes how to write an ALSA (Advanced Linux
  23. Sound Architecture) driver.
  24. </para>
  25. </abstract>
  26. <legalnotice>
  27. <para>
  28. Copyright (c) 2002-2005 Takashi Iwai <email>tiwai@suse.de</email>
  29. </para>
  30. <para>
  31. This document is free; you can redistribute it and/or modify it
  32. under the terms of the GNU General Public License as published by
  33. the Free Software Foundation; either version 2 of the License, or
  34. (at your option) any later version.
  35. </para>
  36. <para>
  37. This document is distributed in the hope that it will be useful,
  38. but <emphasis>WITHOUT ANY WARRANTY</emphasis>; without even the
  39. implied warranty of <emphasis>MERCHANTABILITY or FITNESS FOR A
  40. PARTICULAR PURPOSE</emphasis>. See the GNU General Public License
  41. for more details.
  42. </para>
  43. <para>
  44. You should have received a copy of the GNU General Public
  45. License along with this program; if not, write to the Free
  46. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  47. MA 02111-1307 USA
  48. </para>
  49. </legalnotice>
  50. </bookinfo>
  51. <!-- ****************************************************** -->
  52. <!-- Preface -->
  53. <!-- ****************************************************** -->
  54. <preface id="preface">
  55. <title>Preface</title>
  56. <para>
  57. This document describes how to write an
  58. <ulink url="http://www.alsa-project.org/"><citetitle>
  59. ALSA (Advanced Linux Sound Architecture)</citetitle></ulink>
  60. driver. The document focuses mainly on PCI soundcards.
  61. In the case of other device types, the API might
  62. be different, too. However, at least the ALSA kernel API is
  63. consistent, and therefore it would be still a bit help for
  64. writing them.
  65. </para>
  66. <para>
  67. This document targets people who already have enough
  68. C language skills and have basic linux kernel programming
  69. knowledge. This document doesn't explain the general
  70. topic of linux kernel coding and doesn't cover low-level
  71. driver implementation details. It only describes
  72. the standard way to write a PCI sound driver on ALSA.
  73. </para>
  74. <para>
  75. If you are already familiar with the older ALSA ver.0.5.x API, you
  76. can check the drivers such as <filename>sound/pci/es1938.c</filename> or
  77. <filename>sound/pci/maestro3.c</filename> which have also almost the same
  78. code-base in the ALSA 0.5.x tree, so you can compare the differences.
  79. </para>
  80. <para>
  81. This document is still a draft version. Any feedback and
  82. corrections, please!!
  83. </para>
  84. </preface>
  85. <!-- ****************************************************** -->
  86. <!-- File Tree Structure -->
  87. <!-- ****************************************************** -->
  88. <chapter id="file-tree">
  89. <title>File Tree Structure</title>
  90. <section id="file-tree-general">
  91. <title>General</title>
  92. <para>
  93. The ALSA drivers are provided in two ways.
  94. </para>
  95. <para>
  96. One is the trees provided as a tarball or via cvs from the
  97. ALSA's ftp site, and another is the 2.6 (or later) Linux kernel
  98. tree. To synchronize both, the ALSA driver tree is split into
  99. two different trees: alsa-kernel and alsa-driver. The former
  100. contains purely the source code for the Linux 2.6 (or later)
  101. tree. This tree is designed only for compilation on 2.6 or
  102. later environment. The latter, alsa-driver, contains many subtle
  103. files for compiling ALSA drivers outside of the Linux kernel tree,
  104. wrapper functions for older 2.2 and 2.4 kernels, to adapt the latest kernel API,
  105. and additional drivers which are still in development or in
  106. tests. The drivers in alsa-driver tree will be moved to
  107. alsa-kernel (and eventually to the 2.6 kernel tree) when they are
  108. finished and confirmed to work fine.
  109. </para>
  110. <para>
  111. The file tree structure of ALSA driver is depicted below. Both
  112. alsa-kernel and alsa-driver have almost the same file
  113. structure, except for <quote>core</quote> directory. It's
  114. named as <quote>acore</quote> in alsa-driver tree.
  115. <example>
  116. <title>ALSA File Tree Structure</title>
  117. <literallayout>
  118. sound
  119. /core
  120. /oss
  121. /seq
  122. /oss
  123. /instr
  124. /ioctl32
  125. /include
  126. /drivers
  127. /mpu401
  128. /opl3
  129. /i2c
  130. /l3
  131. /synth
  132. /emux
  133. /pci
  134. /(cards)
  135. /isa
  136. /(cards)
  137. /arm
  138. /ppc
  139. /sparc
  140. /usb
  141. /pcmcia /(cards)
  142. /oss
  143. </literallayout>
  144. </example>
  145. </para>
  146. </section>
  147. <section id="file-tree-core-directory">
  148. <title>core directory</title>
  149. <para>
  150. This directory contains the middle layer which is the heart
  151. of ALSA drivers. In this directory, the native ALSA modules are
  152. stored. The sub-directories contain different modules and are
  153. dependent upon the kernel config.
  154. </para>
  155. <section id="file-tree-core-directory-oss">
  156. <title>core/oss</title>
  157. <para>
  158. The codes for PCM and mixer OSS emulation modules are stored
  159. in this directory. The rawmidi OSS emulation is included in
  160. the ALSA rawmidi code since it's quite small. The sequencer
  161. code is stored in <filename>core/seq/oss</filename> directory (see
  162. <link linkend="file-tree-core-directory-seq-oss"><citetitle>
  163. below</citetitle></link>).
  164. </para>
  165. </section>
  166. <section id="file-tree-core-directory-ioctl32">
  167. <title>core/ioctl32</title>
  168. <para>
  169. This directory contains the 32bit-ioctl wrappers for 64bit
  170. architectures such like x86-64, ppc64 and sparc64. For 32bit
  171. and alpha architectures, these are not compiled.
  172. </para>
  173. </section>
  174. <section id="file-tree-core-directory-seq">
  175. <title>core/seq</title>
  176. <para>
  177. This directory and its sub-directories are for the ALSA
  178. sequencer. This directory contains the sequencer core and
  179. primary sequencer modules such like snd-seq-midi,
  180. snd-seq-virmidi, etc. They are compiled only when
  181. <constant>CONFIG_SND_SEQUENCER</constant> is set in the kernel
  182. config.
  183. </para>
  184. </section>
  185. <section id="file-tree-core-directory-seq-oss">
  186. <title>core/seq/oss</title>
  187. <para>
  188. This contains the OSS sequencer emulation codes.
  189. </para>
  190. </section>
  191. <section id="file-tree-core-directory-deq-instr">
  192. <title>core/seq/instr</title>
  193. <para>
  194. This directory contains the modules for the sequencer
  195. instrument layer.
  196. </para>
  197. </section>
  198. </section>
  199. <section id="file-tree-include-directory">
  200. <title>include directory</title>
  201. <para>
  202. This is the place for the public header files of ALSA drivers,
  203. which are to be exported to user-space, or included by
  204. several files at different directories. Basically, the private
  205. header files should not be placed in this directory, but you may
  206. still find files there, due to historical reasons :)
  207. </para>
  208. </section>
  209. <section id="file-tree-drivers-directory">
  210. <title>drivers directory</title>
  211. <para>
  212. This directory contains code shared among different drivers
  213. on different architectures. They are hence supposed not to be
  214. architecture-specific.
  215. For example, the dummy pcm driver and the serial MIDI
  216. driver are found in this directory. In the sub-directories,
  217. there is code for components which are independent from
  218. bus and cpu architectures.
  219. </para>
  220. <section id="file-tree-drivers-directory-mpu401">
  221. <title>drivers/mpu401</title>
  222. <para>
  223. The MPU401 and MPU401-UART modules are stored here.
  224. </para>
  225. </section>
  226. <section id="file-tree-drivers-directory-opl3">
  227. <title>drivers/opl3 and opl4</title>
  228. <para>
  229. The OPL3 and OPL4 FM-synth stuff is found here.
  230. </para>
  231. </section>
  232. </section>
  233. <section id="file-tree-i2c-directory">
  234. <title>i2c directory</title>
  235. <para>
  236. This contains the ALSA i2c components.
  237. </para>
  238. <para>
  239. Although there is a standard i2c layer on Linux, ALSA has its
  240. own i2c code for some cards, because the soundcard needs only a
  241. simple operation and the standard i2c API is too complicated for
  242. such a purpose.
  243. </para>
  244. <section id="file-tree-i2c-directory-l3">
  245. <title>i2c/l3</title>
  246. <para>
  247. This is a sub-directory for ARM L3 i2c.
  248. </para>
  249. </section>
  250. </section>
  251. <section id="file-tree-synth-directory">
  252. <title>synth directory</title>
  253. <para>
  254. This contains the synth middle-level modules.
  255. </para>
  256. <para>
  257. So far, there is only Emu8000/Emu10k1 synth driver under
  258. the <filename>synth/emux</filename> sub-directory.
  259. </para>
  260. </section>
  261. <section id="file-tree-pci-directory">
  262. <title>pci directory</title>
  263. <para>
  264. This directory and its sub-directories hold the top-level card modules
  265. for PCI soundcards and the code specific to the PCI BUS.
  266. </para>
  267. <para>
  268. The drivers compiled from a single file are stored directly
  269. in the pci directory, while the drivers with several source files are
  270. stored on their own sub-directory (e.g. emu10k1, ice1712).
  271. </para>
  272. </section>
  273. <section id="file-tree-isa-directory">
  274. <title>isa directory</title>
  275. <para>
  276. This directory and its sub-directories hold the top-level card modules
  277. for ISA soundcards.
  278. </para>
  279. </section>
  280. <section id="file-tree-arm-ppc-sparc-directories">
  281. <title>arm, ppc, and sparc directories</title>
  282. <para>
  283. They are used for top-level card modules which are
  284. specific to one of these architectures.
  285. </para>
  286. </section>
  287. <section id="file-tree-usb-directory">
  288. <title>usb directory</title>
  289. <para>
  290. This directory contains the USB-audio driver. In the latest version, the
  291. USB MIDI driver is integrated in the usb-audio driver.
  292. </para>
  293. </section>
  294. <section id="file-tree-pcmcia-directory">
  295. <title>pcmcia directory</title>
  296. <para>
  297. The PCMCIA, especially PCCard drivers will go here. CardBus
  298. drivers will be in the pci directory, because their API is identical
  299. to that of standard PCI cards.
  300. </para>
  301. </section>
  302. <section id="file-tree-oss-directory">
  303. <title>oss directory</title>
  304. <para>
  305. The OSS/Lite source files are stored here in Linux 2.6 (or
  306. later) tree. In the ALSA driver tarball, this directory is empty,
  307. of course :)
  308. </para>
  309. </section>
  310. </chapter>
  311. <!-- ****************************************************** -->
  312. <!-- Basic Flow for PCI Drivers -->
  313. <!-- ****************************************************** -->
  314. <chapter id="basic-flow">
  315. <title>Basic Flow for PCI Drivers</title>
  316. <section id="basic-flow-outline">
  317. <title>Outline</title>
  318. <para>
  319. The minimum flow for PCI soundcards is as follows:
  320. <itemizedlist>
  321. <listitem><para>define the PCI ID table (see the section
  322. <link linkend="pci-resource-entries"><citetitle>PCI Entries
  323. </citetitle></link>).</para></listitem>
  324. <listitem><para>create <function>probe()</function> callback.</para></listitem>
  325. <listitem><para>create <function>remove()</function> callback.</para></listitem>
  326. <listitem><para>create a <structname>pci_driver</structname> structure
  327. containing the three pointers above.</para></listitem>
  328. <listitem><para>create an <function>init()</function> function just calling
  329. the <function>pci_register_driver()</function> to register the pci_driver table
  330. defined above.</para></listitem>
  331. <listitem><para>create an <function>exit()</function> function to call
  332. the <function>pci_unregister_driver()</function> function.</para></listitem>
  333. </itemizedlist>
  334. </para>
  335. </section>
  336. <section id="basic-flow-example">
  337. <title>Full Code Example</title>
  338. <para>
  339. The code example is shown below. Some parts are kept
  340. unimplemented at this moment but will be filled in the
  341. next sections. The numbers in the comment lines of the
  342. <function>snd_mychip_probe()</function> function
  343. refer to details explained in the following section.
  344. <example>
  345. <title>Basic Flow for PCI Drivers - Example</title>
  346. <programlisting>
  347. <![CDATA[
  348. #include <linux/init.h>
  349. #include <linux/pci.h>
  350. #include <linux/slab.h>
  351. #include <sound/core.h>
  352. #include <sound/initval.h>
  353. /* module parameters (see "Module Parameters") */
  354. /* SNDRV_CARDS: maximum number of cards supported by this module */
  355. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  356. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  357. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  358. /* definition of the chip-specific record */
  359. struct mychip {
  360. struct snd_card *card;
  361. /* the rest of the implementation will be in section
  362. * "PCI Resource Management"
  363. */
  364. };
  365. /* chip-specific destructor
  366. * (see "PCI Resource Management")
  367. */
  368. static int snd_mychip_free(struct mychip *chip)
  369. {
  370. .... /* will be implemented later... */
  371. }
  372. /* component-destructor
  373. * (see "Management of Cards and Components")
  374. */
  375. static int snd_mychip_dev_free(struct snd_device *device)
  376. {
  377. return snd_mychip_free(device->device_data);
  378. }
  379. /* chip-specific constructor
  380. * (see "Management of Cards and Components")
  381. */
  382. static int __devinit snd_mychip_create(struct snd_card *card,
  383. struct pci_dev *pci,
  384. struct mychip **rchip)
  385. {
  386. struct mychip *chip;
  387. int err;
  388. static struct snd_device_ops ops = {
  389. .dev_free = snd_mychip_dev_free,
  390. };
  391. *rchip = NULL;
  392. /* check PCI availability here
  393. * (see "PCI Resource Management")
  394. */
  395. ....
  396. /* allocate a chip-specific data with zero filled */
  397. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  398. if (chip == NULL)
  399. return -ENOMEM;
  400. chip->card = card;
  401. /* rest of initialization here; will be implemented
  402. * later, see "PCI Resource Management"
  403. */
  404. ....
  405. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  406. if (err < 0) {
  407. snd_mychip_free(chip);
  408. return err;
  409. }
  410. snd_card_set_dev(card, &pci->dev);
  411. *rchip = chip;
  412. return 0;
  413. }
  414. /* constructor -- see "Constructor" sub-section */
  415. static int __devinit snd_mychip_probe(struct pci_dev *pci,
  416. const struct pci_device_id *pci_id)
  417. {
  418. static int dev;
  419. struct snd_card *card;
  420. struct mychip *chip;
  421. int err;
  422. /* (1) */
  423. if (dev >= SNDRV_CARDS)
  424. return -ENODEV;
  425. if (!enable[dev]) {
  426. dev++;
  427. return -ENOENT;
  428. }
  429. /* (2) */
  430. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  431. if (err < 0)
  432. return err;
  433. /* (3) */
  434. err = snd_mychip_create(card, pci, &chip);
  435. if (err < 0) {
  436. snd_card_free(card);
  437. return err;
  438. }
  439. /* (4) */
  440. strcpy(card->driver, "My Chip");
  441. strcpy(card->shortname, "My Own Chip 123");
  442. sprintf(card->longname, "%s at 0x%lx irq %i",
  443. card->shortname, chip->ioport, chip->irq);
  444. /* (5) */
  445. .... /* implemented later */
  446. /* (6) */
  447. err = snd_card_register(card);
  448. if (err < 0) {
  449. snd_card_free(card);
  450. return err;
  451. }
  452. /* (7) */
  453. pci_set_drvdata(pci, card);
  454. dev++;
  455. return 0;
  456. }
  457. /* destructor -- see the "Destructor" sub-section */
  458. static void __devexit snd_mychip_remove(struct pci_dev *pci)
  459. {
  460. snd_card_free(pci_get_drvdata(pci));
  461. pci_set_drvdata(pci, NULL);
  462. }
  463. ]]>
  464. </programlisting>
  465. </example>
  466. </para>
  467. </section>
  468. <section id="basic-flow-constructor">
  469. <title>Constructor</title>
  470. <para>
  471. The real constructor of PCI drivers is the <function>probe</function> callback.
  472. The <function>probe</function> callback and other component-constructors which are called
  473. from the <function>probe</function> callback should be defined with
  474. the <parameter>__devinit</parameter> prefix. You
  475. cannot use the <parameter>__init</parameter> prefix for them,
  476. because any PCI device could be a hotplug device.
  477. </para>
  478. <para>
  479. In the <function>probe</function> callback, the following scheme is often used.
  480. </para>
  481. <section id="basic-flow-constructor-device-index">
  482. <title>1) Check and increment the device index.</title>
  483. <para>
  484. <informalexample>
  485. <programlisting>
  486. <![CDATA[
  487. static int dev;
  488. ....
  489. if (dev >= SNDRV_CARDS)
  490. return -ENODEV;
  491. if (!enable[dev]) {
  492. dev++;
  493. return -ENOENT;
  494. }
  495. ]]>
  496. </programlisting>
  497. </informalexample>
  498. where enable[dev] is the module option.
  499. </para>
  500. <para>
  501. Each time the <function>probe</function> callback is called, check the
  502. availability of the device. If not available, simply increment
  503. the device index and returns. dev will be incremented also
  504. later (<link
  505. linkend="basic-flow-constructor-set-pci"><citetitle>step
  506. 7</citetitle></link>).
  507. </para>
  508. </section>
  509. <section id="basic-flow-constructor-create-card">
  510. <title>2) Create a card instance</title>
  511. <para>
  512. <informalexample>
  513. <programlisting>
  514. <![CDATA[
  515. struct snd_card *card;
  516. int err;
  517. ....
  518. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  519. ]]>
  520. </programlisting>
  521. </informalexample>
  522. </para>
  523. <para>
  524. The details will be explained in the section
  525. <link linkend="card-management-card-instance"><citetitle>
  526. Management of Cards and Components</citetitle></link>.
  527. </para>
  528. </section>
  529. <section id="basic-flow-constructor-create-main">
  530. <title>3) Create a main component</title>
  531. <para>
  532. In this part, the PCI resources are allocated.
  533. <informalexample>
  534. <programlisting>
  535. <![CDATA[
  536. struct mychip *chip;
  537. ....
  538. err = snd_mychip_create(card, pci, &chip);
  539. if (err < 0) {
  540. snd_card_free(card);
  541. return err;
  542. }
  543. ]]>
  544. </programlisting>
  545. </informalexample>
  546. The details will be explained in the section <link
  547. linkend="pci-resource"><citetitle>PCI Resource
  548. Management</citetitle></link>.
  549. </para>
  550. </section>
  551. <section id="basic-flow-constructor-main-component">
  552. <title>4) Set the driver ID and name strings.</title>
  553. <para>
  554. <informalexample>
  555. <programlisting>
  556. <![CDATA[
  557. strcpy(card->driver, "My Chip");
  558. strcpy(card->shortname, "My Own Chip 123");
  559. sprintf(card->longname, "%s at 0x%lx irq %i",
  560. card->shortname, chip->ioport, chip->irq);
  561. ]]>
  562. </programlisting>
  563. </informalexample>
  564. The driver field holds the minimal ID string of the
  565. chip. This is used by alsa-lib's configurator, so keep it
  566. simple but unique.
  567. Even the same driver can have different driver IDs to
  568. distinguish the functionality of each chip type.
  569. </para>
  570. <para>
  571. The shortname field is a string shown as more verbose
  572. name. The longname field contains the information
  573. shown in <filename>/proc/asound/cards</filename>.
  574. </para>
  575. </section>
  576. <section id="basic-flow-constructor-create-other">
  577. <title>5) Create other components, such as mixer, MIDI, etc.</title>
  578. <para>
  579. Here you define the basic components such as
  580. <link linkend="pcm-interface"><citetitle>PCM</citetitle></link>,
  581. mixer (e.g. <link linkend="api-ac97"><citetitle>AC97</citetitle></link>),
  582. MIDI (e.g. <link linkend="midi-interface"><citetitle>MPU-401</citetitle></link>),
  583. and other interfaces.
  584. Also, if you want a <link linkend="proc-interface"><citetitle>proc
  585. file</citetitle></link>, define it here, too.
  586. </para>
  587. </section>
  588. <section id="basic-flow-constructor-register-card">
  589. <title>6) Register the card instance.</title>
  590. <para>
  591. <informalexample>
  592. <programlisting>
  593. <![CDATA[
  594. err = snd_card_register(card);
  595. if (err < 0) {
  596. snd_card_free(card);
  597. return err;
  598. }
  599. ]]>
  600. </programlisting>
  601. </informalexample>
  602. </para>
  603. <para>
  604. Will be explained in the section <link
  605. linkend="card-management-registration"><citetitle>Management
  606. of Cards and Components</citetitle></link>, too.
  607. </para>
  608. </section>
  609. <section id="basic-flow-constructor-set-pci">
  610. <title>7) Set the PCI driver data and return zero.</title>
  611. <para>
  612. <informalexample>
  613. <programlisting>
  614. <![CDATA[
  615. pci_set_drvdata(pci, card);
  616. dev++;
  617. return 0;
  618. ]]>
  619. </programlisting>
  620. </informalexample>
  621. In the above, the card record is stored. This pointer is
  622. used in the remove callback and power-management
  623. callbacks, too.
  624. </para>
  625. </section>
  626. </section>
  627. <section id="basic-flow-destructor">
  628. <title>Destructor</title>
  629. <para>
  630. The destructor, remove callback, simply releases the card
  631. instance. Then the ALSA middle layer will release all the
  632. attached components automatically.
  633. </para>
  634. <para>
  635. It would be typically like the following:
  636. <informalexample>
  637. <programlisting>
  638. <![CDATA[
  639. static void __devexit snd_mychip_remove(struct pci_dev *pci)
  640. {
  641. snd_card_free(pci_get_drvdata(pci));
  642. pci_set_drvdata(pci, NULL);
  643. }
  644. ]]>
  645. </programlisting>
  646. </informalexample>
  647. The above code assumes that the card pointer is set to the PCI
  648. driver data.
  649. </para>
  650. </section>
  651. <section id="basic-flow-header-files">
  652. <title>Header Files</title>
  653. <para>
  654. For the above example, at least the following include files
  655. are necessary.
  656. <informalexample>
  657. <programlisting>
  658. <![CDATA[
  659. #include <linux/init.h>
  660. #include <linux/pci.h>
  661. #include <linux/slab.h>
  662. #include <sound/core.h>
  663. #include <sound/initval.h>
  664. ]]>
  665. </programlisting>
  666. </informalexample>
  667. where the last one is necessary only when module options are
  668. defined in the source file. If the code is split into several
  669. files, the files without module options don't need them.
  670. </para>
  671. <para>
  672. In addition to these headers, you'll need
  673. <filename>&lt;linux/interrupt.h&gt;</filename> for interrupt
  674. handling, and <filename>&lt;asm/io.h&gt;</filename> for I/O
  675. access. If you use the <function>mdelay()</function> or
  676. <function>udelay()</function> functions, you'll need to include
  677. <filename>&lt;linux/delay.h&gt;</filename> too.
  678. </para>
  679. <para>
  680. The ALSA interfaces like the PCM and control APIs are defined in other
  681. <filename>&lt;sound/xxx.h&gt;</filename> header files.
  682. They have to be included after
  683. <filename>&lt;sound/core.h&gt;</filename>.
  684. </para>
  685. </section>
  686. </chapter>
  687. <!-- ****************************************************** -->
  688. <!-- Management of Cards and Components -->
  689. <!-- ****************************************************** -->
  690. <chapter id="card-management">
  691. <title>Management of Cards and Components</title>
  692. <section id="card-management-card-instance">
  693. <title>Card Instance</title>
  694. <para>
  695. For each soundcard, a <quote>card</quote> record must be allocated.
  696. </para>
  697. <para>
  698. A card record is the headquarters of the soundcard. It manages
  699. the whole list of devices (components) on the soundcard, such as
  700. PCM, mixers, MIDI, synthesizer, and so on. Also, the card
  701. record holds the ID and the name strings of the card, manages
  702. the root of proc files, and controls the power-management states
  703. and hotplug disconnections. The component list on the card
  704. record is used to manage the correct release of resources at
  705. destruction.
  706. </para>
  707. <para>
  708. As mentioned above, to create a card instance, call
  709. <function>snd_card_create()</function>.
  710. <informalexample>
  711. <programlisting>
  712. <![CDATA[
  713. struct snd_card *card;
  714. int err;
  715. err = snd_card_create(index, id, module, extra_size, &card);
  716. ]]>
  717. </programlisting>
  718. </informalexample>
  719. </para>
  720. <para>
  721. The function takes five arguments, the card-index number, the
  722. id string, the module pointer (usually
  723. <constant>THIS_MODULE</constant>),
  724. the size of extra-data space, and the pointer to return the
  725. card instance. The extra_size argument is used to
  726. allocate card-&gt;private_data for the
  727. chip-specific data. Note that these data
  728. are allocated by <function>snd_card_create()</function>.
  729. </para>
  730. </section>
  731. <section id="card-management-component">
  732. <title>Components</title>
  733. <para>
  734. After the card is created, you can attach the components
  735. (devices) to the card instance. In an ALSA driver, a component is
  736. represented as a struct <structname>snd_device</structname> object.
  737. A component can be a PCM instance, a control interface, a raw
  738. MIDI interface, etc. Each such instance has one component
  739. entry.
  740. </para>
  741. <para>
  742. A component can be created via
  743. <function>snd_device_new()</function> function.
  744. <informalexample>
  745. <programlisting>
  746. <![CDATA[
  747. snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);
  748. ]]>
  749. </programlisting>
  750. </informalexample>
  751. </para>
  752. <para>
  753. This takes the card pointer, the device-level
  754. (<constant>SNDRV_DEV_XXX</constant>), the data pointer, and the
  755. callback pointers (<parameter>&amp;ops</parameter>). The
  756. device-level defines the type of components and the order of
  757. registration and de-registration. For most components, the
  758. device-level is already defined. For a user-defined component,
  759. you can use <constant>SNDRV_DEV_LOWLEVEL</constant>.
  760. </para>
  761. <para>
  762. This function itself doesn't allocate the data space. The data
  763. must be allocated manually beforehand, and its pointer is passed
  764. as the argument. This pointer is used as the
  765. (<parameter>chip</parameter> identifier in the above example)
  766. for the instance.
  767. </para>
  768. <para>
  769. Each pre-defined ALSA component such as ac97 and pcm calls
  770. <function>snd_device_new()</function> inside its
  771. constructor. The destructor for each component is defined in the
  772. callback pointers. Hence, you don't need to take care of
  773. calling a destructor for such a component.
  774. </para>
  775. <para>
  776. If you wish to create your own component, you need to
  777. set the destructor function to the dev_free callback in
  778. the <parameter>ops</parameter>, so that it can be released
  779. automatically via <function>snd_card_free()</function>.
  780. The next example will show an implementation of chip-specific
  781. data.
  782. </para>
  783. </section>
  784. <section id="card-management-chip-specific">
  785. <title>Chip-Specific Data</title>
  786. <para>
  787. Chip-specific information, e.g. the I/O port address, its
  788. resource pointer, or the irq number, is stored in the
  789. chip-specific record.
  790. <informalexample>
  791. <programlisting>
  792. <![CDATA[
  793. struct mychip {
  794. ....
  795. };
  796. ]]>
  797. </programlisting>
  798. </informalexample>
  799. </para>
  800. <para>
  801. In general, there are two ways of allocating the chip record.
  802. </para>
  803. <section id="card-management-chip-specific-snd-card-new">
  804. <title>1. Allocating via <function>snd_card_create()</function>.</title>
  805. <para>
  806. As mentioned above, you can pass the extra-data-length
  807. to the 4th argument of <function>snd_card_create()</function>, i.e.
  808. <informalexample>
  809. <programlisting>
  810. <![CDATA[
  811. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  812. sizeof(struct mychip), &card);
  813. ]]>
  814. </programlisting>
  815. </informalexample>
  816. struct <structname>mychip</structname> is the type of the chip record.
  817. </para>
  818. <para>
  819. In return, the allocated record can be accessed as
  820. <informalexample>
  821. <programlisting>
  822. <![CDATA[
  823. struct mychip *chip = card->private_data;
  824. ]]>
  825. </programlisting>
  826. </informalexample>
  827. With this method, you don't have to allocate twice.
  828. The record is released together with the card instance.
  829. </para>
  830. </section>
  831. <section id="card-management-chip-specific-allocate-extra">
  832. <title>2. Allocating an extra device.</title>
  833. <para>
  834. After allocating a card instance via
  835. <function>snd_card_create()</function> (with
  836. <constant>0</constant> on the 4th arg), call
  837. <function>kzalloc()</function>.
  838. <informalexample>
  839. <programlisting>
  840. <![CDATA[
  841. struct snd_card *card;
  842. struct mychip *chip;
  843. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  844. .....
  845. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  846. ]]>
  847. </programlisting>
  848. </informalexample>
  849. </para>
  850. <para>
  851. The chip record should have the field to hold the card
  852. pointer at least,
  853. <informalexample>
  854. <programlisting>
  855. <![CDATA[
  856. struct mychip {
  857. struct snd_card *card;
  858. ....
  859. };
  860. ]]>
  861. </programlisting>
  862. </informalexample>
  863. </para>
  864. <para>
  865. Then, set the card pointer in the returned chip instance.
  866. <informalexample>
  867. <programlisting>
  868. <![CDATA[
  869. chip->card = card;
  870. ]]>
  871. </programlisting>
  872. </informalexample>
  873. </para>
  874. <para>
  875. Next, initialize the fields, and register this chip
  876. record as a low-level device with a specified
  877. <parameter>ops</parameter>,
  878. <informalexample>
  879. <programlisting>
  880. <![CDATA[
  881. static struct snd_device_ops ops = {
  882. .dev_free = snd_mychip_dev_free,
  883. };
  884. ....
  885. snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  886. ]]>
  887. </programlisting>
  888. </informalexample>
  889. <function>snd_mychip_dev_free()</function> is the
  890. device-destructor function, which will call the real
  891. destructor.
  892. </para>
  893. <para>
  894. <informalexample>
  895. <programlisting>
  896. <![CDATA[
  897. static int snd_mychip_dev_free(struct snd_device *device)
  898. {
  899. return snd_mychip_free(device->device_data);
  900. }
  901. ]]>
  902. </programlisting>
  903. </informalexample>
  904. where <function>snd_mychip_free()</function> is the real destructor.
  905. </para>
  906. </section>
  907. </section>
  908. <section id="card-management-registration">
  909. <title>Registration and Release</title>
  910. <para>
  911. After all components are assigned, register the card instance
  912. by calling <function>snd_card_register()</function>. Access
  913. to the device files is enabled at this point. That is, before
  914. <function>snd_card_register()</function> is called, the
  915. components are safely inaccessible from external side. If this
  916. call fails, exit the probe function after releasing the card via
  917. <function>snd_card_free()</function>.
  918. </para>
  919. <para>
  920. For releasing the card instance, you can call simply
  921. <function>snd_card_free()</function>. As mentioned earlier, all
  922. components are released automatically by this call.
  923. </para>
  924. <para>
  925. As further notes, the destructors (both
  926. <function>snd_mychip_dev_free</function> and
  927. <function>snd_mychip_free</function>) cannot be defined with
  928. the <parameter>__devexit</parameter> prefix, because they may be
  929. called from the constructor, too, at the false path.
  930. </para>
  931. <para>
  932. For a device which allows hotplugging, you can use
  933. <function>snd_card_free_when_closed</function>. This one will
  934. postpone the destruction until all devices are closed.
  935. </para>
  936. </section>
  937. </chapter>
  938. <!-- ****************************************************** -->
  939. <!-- PCI Resource Management -->
  940. <!-- ****************************************************** -->
  941. <chapter id="pci-resource">
  942. <title>PCI Resource Management</title>
  943. <section id="pci-resource-example">
  944. <title>Full Code Example</title>
  945. <para>
  946. In this section, we'll complete the chip-specific constructor,
  947. destructor and PCI entries. Example code is shown first,
  948. below.
  949. <example>
  950. <title>PCI Resource Management Example</title>
  951. <programlisting>
  952. <![CDATA[
  953. struct mychip {
  954. struct snd_card *card;
  955. struct pci_dev *pci;
  956. unsigned long port;
  957. int irq;
  958. };
  959. static int snd_mychip_free(struct mychip *chip)
  960. {
  961. /* disable hardware here if any */
  962. .... /* (not implemented in this document) */
  963. /* release the irq */
  964. if (chip->irq >= 0)
  965. free_irq(chip->irq, chip);
  966. /* release the I/O ports & memory */
  967. pci_release_regions(chip->pci);
  968. /* disable the PCI entry */
  969. pci_disable_device(chip->pci);
  970. /* release the data */
  971. kfree(chip);
  972. return 0;
  973. }
  974. /* chip-specific constructor */
  975. static int __devinit snd_mychip_create(struct snd_card *card,
  976. struct pci_dev *pci,
  977. struct mychip **rchip)
  978. {
  979. struct mychip *chip;
  980. int err;
  981. static struct snd_device_ops ops = {
  982. .dev_free = snd_mychip_dev_free,
  983. };
  984. *rchip = NULL;
  985. /* initialize the PCI entry */
  986. err = pci_enable_device(pci);
  987. if (err < 0)
  988. return err;
  989. /* check PCI availability (28bit DMA) */
  990. if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
  991. pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
  992. printk(KERN_ERR "error to set 28bit mask DMA\n");
  993. pci_disable_device(pci);
  994. return -ENXIO;
  995. }
  996. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  997. if (chip == NULL) {
  998. pci_disable_device(pci);
  999. return -ENOMEM;
  1000. }
  1001. /* initialize the stuff */
  1002. chip->card = card;
  1003. chip->pci = pci;
  1004. chip->irq = -1;
  1005. /* (1) PCI resource allocation */
  1006. err = pci_request_regions(pci, "My Chip");
  1007. if (err < 0) {
  1008. kfree(chip);
  1009. pci_disable_device(pci);
  1010. return err;
  1011. }
  1012. chip->port = pci_resource_start(pci, 0);
  1013. if (request_irq(pci->irq, snd_mychip_interrupt,
  1014. IRQF_SHARED, "My Chip", chip)) {
  1015. printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
  1016. snd_mychip_free(chip);
  1017. return -EBUSY;
  1018. }
  1019. chip->irq = pci->irq;
  1020. /* (2) initialization of the chip hardware */
  1021. .... /* (not implemented in this document) */
  1022. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  1023. if (err < 0) {
  1024. snd_mychip_free(chip);
  1025. return err;
  1026. }
  1027. snd_card_set_dev(card, &pci->dev);
  1028. *rchip = chip;
  1029. return 0;
  1030. }
  1031. /* PCI IDs */
  1032. static struct pci_device_id snd_mychip_ids[] = {
  1033. { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
  1034. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  1035. ....
  1036. { 0, }
  1037. };
  1038. MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
  1039. /* pci_driver definition */
  1040. static struct pci_driver driver = {
  1041. .name = "My Own Chip",
  1042. .id_table = snd_mychip_ids,
  1043. .probe = snd_mychip_probe,
  1044. .remove = __devexit_p(snd_mychip_remove),
  1045. };
  1046. /* module initialization */
  1047. static int __init alsa_card_mychip_init(void)
  1048. {
  1049. return pci_register_driver(&driver);
  1050. }
  1051. /* module clean up */
  1052. static void __exit alsa_card_mychip_exit(void)
  1053. {
  1054. pci_unregister_driver(&driver);
  1055. }
  1056. module_init(alsa_card_mychip_init)
  1057. module_exit(alsa_card_mychip_exit)
  1058. EXPORT_NO_SYMBOLS; /* for old kernels only */
  1059. ]]>
  1060. </programlisting>
  1061. </example>
  1062. </para>
  1063. </section>
  1064. <section id="pci-resource-some-haftas">
  1065. <title>Some Hafta's</title>
  1066. <para>
  1067. The allocation of PCI resources is done in the
  1068. <function>probe()</function> function, and usually an extra
  1069. <function>xxx_create()</function> function is written for this
  1070. purpose.
  1071. </para>
  1072. <para>
  1073. In the case of PCI devices, you first have to call
  1074. the <function>pci_enable_device()</function> function before
  1075. allocating resources. Also, you need to set the proper PCI DMA
  1076. mask to limit the accessed I/O range. In some cases, you might
  1077. need to call <function>pci_set_master()</function> function,
  1078. too.
  1079. </para>
  1080. <para>
  1081. Suppose the 28bit mask, and the code to be added would be like:
  1082. <informalexample>
  1083. <programlisting>
  1084. <![CDATA[
  1085. err = pci_enable_device(pci);
  1086. if (err < 0)
  1087. return err;
  1088. if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
  1089. pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
  1090. printk(KERN_ERR "error to set 28bit mask DMA\n");
  1091. pci_disable_device(pci);
  1092. return -ENXIO;
  1093. }
  1094. ]]>
  1095. </programlisting>
  1096. </informalexample>
  1097. </para>
  1098. </section>
  1099. <section id="pci-resource-resource-allocation">
  1100. <title>Resource Allocation</title>
  1101. <para>
  1102. The allocation of I/O ports and irqs is done via standard kernel
  1103. functions. Unlike ALSA ver.0.5.x., there are no helpers for
  1104. that. And these resources must be released in the destructor
  1105. function (see below). Also, on ALSA 0.9.x, you don't need to
  1106. allocate (pseudo-)DMA for PCI like in ALSA 0.5.x.
  1107. </para>
  1108. <para>
  1109. Now assume that the PCI device has an I/O port with 8 bytes
  1110. and an interrupt. Then struct <structname>mychip</structname> will have the
  1111. following fields:
  1112. <informalexample>
  1113. <programlisting>
  1114. <![CDATA[
  1115. struct mychip {
  1116. struct snd_card *card;
  1117. unsigned long port;
  1118. int irq;
  1119. };
  1120. ]]>
  1121. </programlisting>
  1122. </informalexample>
  1123. </para>
  1124. <para>
  1125. For an I/O port (and also a memory region), you need to have
  1126. the resource pointer for the standard resource management. For
  1127. an irq, you have to keep only the irq number (integer). But you
  1128. need to initialize this number as -1 before actual allocation,
  1129. since irq 0 is valid. The port address and its resource pointer
  1130. can be initialized as null by
  1131. <function>kzalloc()</function> automatically, so you
  1132. don't have to take care of resetting them.
  1133. </para>
  1134. <para>
  1135. The allocation of an I/O port is done like this:
  1136. <informalexample>
  1137. <programlisting>
  1138. <![CDATA[
  1139. err = pci_request_regions(pci, "My Chip");
  1140. if (err < 0) {
  1141. kfree(chip);
  1142. pci_disable_device(pci);
  1143. return err;
  1144. }
  1145. chip->port = pci_resource_start(pci, 0);
  1146. ]]>
  1147. </programlisting>
  1148. </informalexample>
  1149. </para>
  1150. <para>
  1151. <!-- obsolete -->
  1152. It will reserve the I/O port region of 8 bytes of the given
  1153. PCI device. The returned value, chip-&gt;res_port, is allocated
  1154. via <function>kmalloc()</function> by
  1155. <function>request_region()</function>. The pointer must be
  1156. released via <function>kfree()</function>, but there is a
  1157. problem with this. This issue will be explained later.
  1158. </para>
  1159. <para>
  1160. The allocation of an interrupt source is done like this:
  1161. <informalexample>
  1162. <programlisting>
  1163. <![CDATA[
  1164. if (request_irq(pci->irq, snd_mychip_interrupt,
  1165. IRQF_SHARED, "My Chip", chip)) {
  1166. printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
  1167. snd_mychip_free(chip);
  1168. return -EBUSY;
  1169. }
  1170. chip->irq = pci->irq;
  1171. ]]>
  1172. </programlisting>
  1173. </informalexample>
  1174. where <function>snd_mychip_interrupt()</function> is the
  1175. interrupt handler defined <link
  1176. linkend="pcm-interface-interrupt-handler"><citetitle>later</citetitle></link>.
  1177. Note that chip-&gt;irq should be defined
  1178. only when <function>request_irq()</function> succeeded.
  1179. </para>
  1180. <para>
  1181. On the PCI bus, interrupts can be shared. Thus,
  1182. <constant>IRQF_SHARED</constant> is used as the interrupt flag of
  1183. <function>request_irq()</function>.
  1184. </para>
  1185. <para>
  1186. The last argument of <function>request_irq()</function> is the
  1187. data pointer passed to the interrupt handler. Usually, the
  1188. chip-specific record is used for that, but you can use what you
  1189. like, too.
  1190. </para>
  1191. <para>
  1192. I won't give details about the interrupt handler at this
  1193. point, but at least its appearance can be explained now. The
  1194. interrupt handler looks usually like the following:
  1195. <informalexample>
  1196. <programlisting>
  1197. <![CDATA[
  1198. static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
  1199. {
  1200. struct mychip *chip = dev_id;
  1201. ....
  1202. return IRQ_HANDLED;
  1203. }
  1204. ]]>
  1205. </programlisting>
  1206. </informalexample>
  1207. </para>
  1208. <para>
  1209. Now let's write the corresponding destructor for the resources
  1210. above. The role of destructor is simple: disable the hardware
  1211. (if already activated) and release the resources. So far, we
  1212. have no hardware part, so the disabling code is not written here.
  1213. </para>
  1214. <para>
  1215. To release the resources, the <quote>check-and-release</quote>
  1216. method is a safer way. For the interrupt, do like this:
  1217. <informalexample>
  1218. <programlisting>
  1219. <![CDATA[
  1220. if (chip->irq >= 0)
  1221. free_irq(chip->irq, chip);
  1222. ]]>
  1223. </programlisting>
  1224. </informalexample>
  1225. Since the irq number can start from 0, you should initialize
  1226. chip-&gt;irq with a negative value (e.g. -1), so that you can
  1227. check the validity of the irq number as above.
  1228. </para>
  1229. <para>
  1230. When you requested I/O ports or memory regions via
  1231. <function>pci_request_region()</function> or
  1232. <function>pci_request_regions()</function> like in this example,
  1233. release the resource(s) using the corresponding function,
  1234. <function>pci_release_region()</function> or
  1235. <function>pci_release_regions()</function>.
  1236. <informalexample>
  1237. <programlisting>
  1238. <![CDATA[
  1239. pci_release_regions(chip->pci);
  1240. ]]>
  1241. </programlisting>
  1242. </informalexample>
  1243. </para>
  1244. <para>
  1245. When you requested manually via <function>request_region()</function>
  1246. or <function>request_mem_region</function>, you can release it via
  1247. <function>release_resource()</function>. Suppose that you keep
  1248. the resource pointer returned from <function>request_region()</function>
  1249. in chip-&gt;res_port, the release procedure looks like:
  1250. <informalexample>
  1251. <programlisting>
  1252. <![CDATA[
  1253. release_and_free_resource(chip->res_port);
  1254. ]]>
  1255. </programlisting>
  1256. </informalexample>
  1257. </para>
  1258. <para>
  1259. Don't forget to call <function>pci_disable_device()</function>
  1260. before the end.
  1261. </para>
  1262. <para>
  1263. And finally, release the chip-specific record.
  1264. <informalexample>
  1265. <programlisting>
  1266. <![CDATA[
  1267. kfree(chip);
  1268. ]]>
  1269. </programlisting>
  1270. </informalexample>
  1271. </para>
  1272. <para>
  1273. Again, remember that you cannot
  1274. use the <parameter>__devexit</parameter> prefix for this destructor.
  1275. </para>
  1276. <para>
  1277. We didn't implement the hardware disabling part in the above.
  1278. If you need to do this, please note that the destructor may be
  1279. called even before the initialization of the chip is completed.
  1280. It would be better to have a flag to skip hardware disabling
  1281. if the hardware was not initialized yet.
  1282. </para>
  1283. <para>
  1284. When the chip-data is assigned to the card using
  1285. <function>snd_device_new()</function> with
  1286. <constant>SNDRV_DEV_LOWLELVEL</constant> , its destructor is
  1287. called at the last. That is, it is assured that all other
  1288. components like PCMs and controls have already been released.
  1289. You don't have to stop PCMs, etc. explicitly, but just
  1290. call low-level hardware stopping.
  1291. </para>
  1292. <para>
  1293. The management of a memory-mapped region is almost as same as
  1294. the management of an I/O port. You'll need three fields like
  1295. the following:
  1296. <informalexample>
  1297. <programlisting>
  1298. <![CDATA[
  1299. struct mychip {
  1300. ....
  1301. unsigned long iobase_phys;
  1302. void __iomem *iobase_virt;
  1303. };
  1304. ]]>
  1305. </programlisting>
  1306. </informalexample>
  1307. and the allocation would be like below:
  1308. <informalexample>
  1309. <programlisting>
  1310. <![CDATA[
  1311. if ((err = pci_request_regions(pci, "My Chip")) < 0) {
  1312. kfree(chip);
  1313. return err;
  1314. }
  1315. chip->iobase_phys = pci_resource_start(pci, 0);
  1316. chip->iobase_virt = ioremap_nocache(chip->iobase_phys,
  1317. pci_resource_len(pci, 0));
  1318. ]]>
  1319. </programlisting>
  1320. </informalexample>
  1321. and the corresponding destructor would be:
  1322. <informalexample>
  1323. <programlisting>
  1324. <![CDATA[
  1325. static int snd_mychip_free(struct mychip *chip)
  1326. {
  1327. ....
  1328. if (chip->iobase_virt)
  1329. iounmap(chip->iobase_virt);
  1330. ....
  1331. pci_release_regions(chip->pci);
  1332. ....
  1333. }
  1334. ]]>
  1335. </programlisting>
  1336. </informalexample>
  1337. </para>
  1338. </section>
  1339. <section id="pci-resource-device-struct">
  1340. <title>Registration of Device Struct</title>
  1341. <para>
  1342. At some point, typically after calling <function>snd_device_new()</function>,
  1343. you need to register the struct <structname>device</structname> of the chip
  1344. you're handling for udev and co. ALSA provides a macro for compatibility with
  1345. older kernels. Simply call like the following:
  1346. <informalexample>
  1347. <programlisting>
  1348. <![CDATA[
  1349. snd_card_set_dev(card, &pci->dev);
  1350. ]]>
  1351. </programlisting>
  1352. </informalexample>
  1353. so that it stores the PCI's device pointer to the card. This will be
  1354. referred by ALSA core functions later when the devices are registered.
  1355. </para>
  1356. <para>
  1357. In the case of non-PCI, pass the proper device struct pointer of the BUS
  1358. instead. (In the case of legacy ISA without PnP, you don't have to do
  1359. anything.)
  1360. </para>
  1361. </section>
  1362. <section id="pci-resource-entries">
  1363. <title>PCI Entries</title>
  1364. <para>
  1365. So far, so good. Let's finish the missing PCI
  1366. stuff. At first, we need a
  1367. <structname>pci_device_id</structname> table for this
  1368. chipset. It's a table of PCI vendor/device ID number, and some
  1369. masks.
  1370. </para>
  1371. <para>
  1372. For example,
  1373. <informalexample>
  1374. <programlisting>
  1375. <![CDATA[
  1376. static struct pci_device_id snd_mychip_ids[] = {
  1377. { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
  1378. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
  1379. ....
  1380. { 0, }
  1381. };
  1382. MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
  1383. ]]>
  1384. </programlisting>
  1385. </informalexample>
  1386. </para>
  1387. <para>
  1388. The first and second fields of
  1389. the <structname>pci_device_id</structname> structure are the vendor and
  1390. device IDs. If you have no reason to filter the matching
  1391. devices, you can leave the remaining fields as above. The last
  1392. field of the <structname>pci_device_id</structname> struct contains
  1393. private data for this entry. You can specify any value here, for
  1394. example, to define specific operations for supported device IDs.
  1395. Such an example is found in the intel8x0 driver.
  1396. </para>
  1397. <para>
  1398. The last entry of this list is the terminator. You must
  1399. specify this all-zero entry.
  1400. </para>
  1401. <para>
  1402. Then, prepare the <structname>pci_driver</structname> record:
  1403. <informalexample>
  1404. <programlisting>
  1405. <![CDATA[
  1406. static struct pci_driver driver = {
  1407. .name = "My Own Chip",
  1408. .id_table = snd_mychip_ids,
  1409. .probe = snd_mychip_probe,
  1410. .remove = __devexit_p(snd_mychip_remove),
  1411. };
  1412. ]]>
  1413. </programlisting>
  1414. </informalexample>
  1415. </para>
  1416. <para>
  1417. The <structfield>probe</structfield> and
  1418. <structfield>remove</structfield> functions have already
  1419. been defined in the previous sections.
  1420. The <structfield>remove</structfield> function should
  1421. be defined with the
  1422. <function>__devexit_p()</function> macro, so that it's not
  1423. defined for built-in (and non-hot-pluggable) case. The
  1424. <structfield>name</structfield>
  1425. field is the name string of this device. Note that you must not
  1426. use a slash <quote>/</quote> in this string.
  1427. </para>
  1428. <para>
  1429. And at last, the module entries:
  1430. <informalexample>
  1431. <programlisting>
  1432. <![CDATA[
  1433. static int __init alsa_card_mychip_init(void)
  1434. {
  1435. return pci_register_driver(&driver);
  1436. }
  1437. static void __exit alsa_card_mychip_exit(void)
  1438. {
  1439. pci_unregister_driver(&driver);
  1440. }
  1441. module_init(alsa_card_mychip_init)
  1442. module_exit(alsa_card_mychip_exit)
  1443. ]]>
  1444. </programlisting>
  1445. </informalexample>
  1446. </para>
  1447. <para>
  1448. Note that these module entries are tagged with
  1449. <parameter>__init</parameter> and
  1450. <parameter>__exit</parameter> prefixes, not
  1451. <parameter>__devinit</parameter> nor
  1452. <parameter>__devexit</parameter>.
  1453. </para>
  1454. <para>
  1455. Oh, one thing was forgotten. If you have no exported symbols,
  1456. you need to declare it in 2.2 or 2.4 kernels (it's not necessary in 2.6 kernels).
  1457. <informalexample>
  1458. <programlisting>
  1459. <![CDATA[
  1460. EXPORT_NO_SYMBOLS;
  1461. ]]>
  1462. </programlisting>
  1463. </informalexample>
  1464. That's all!
  1465. </para>
  1466. </section>
  1467. </chapter>
  1468. <!-- ****************************************************** -->
  1469. <!-- PCM Interface -->
  1470. <!-- ****************************************************** -->
  1471. <chapter id="pcm-interface">
  1472. <title>PCM Interface</title>
  1473. <section id="pcm-interface-general">
  1474. <title>General</title>
  1475. <para>
  1476. The PCM middle layer of ALSA is quite powerful and it is only
  1477. necessary for each driver to implement the low-level functions
  1478. to access its hardware.
  1479. </para>
  1480. <para>
  1481. For accessing to the PCM layer, you need to include
  1482. <filename>&lt;sound/pcm.h&gt;</filename> first. In addition,
  1483. <filename>&lt;sound/pcm_params.h&gt;</filename> might be needed
  1484. if you access to some functions related with hw_param.
  1485. </para>
  1486. <para>
  1487. Each card device can have up to four pcm instances. A pcm
  1488. instance corresponds to a pcm device file. The limitation of
  1489. number of instances comes only from the available bit size of
  1490. the Linux's device numbers. Once when 64bit device number is
  1491. used, we'll have more pcm instances available.
  1492. </para>
  1493. <para>
  1494. A pcm instance consists of pcm playback and capture streams,
  1495. and each pcm stream consists of one or more pcm substreams. Some
  1496. soundcards support multiple playback functions. For example,
  1497. emu10k1 has a PCM playback of 32 stereo substreams. In this case, at
  1498. each open, a free substream is (usually) automatically chosen
  1499. and opened. Meanwhile, when only one substream exists and it was
  1500. already opened, the successful open will either block
  1501. or error with <constant>EAGAIN</constant> according to the
  1502. file open mode. But you don't have to care about such details in your
  1503. driver. The PCM middle layer will take care of such work.
  1504. </para>
  1505. </section>
  1506. <section id="pcm-interface-example">
  1507. <title>Full Code Example</title>
  1508. <para>
  1509. The example code below does not include any hardware access
  1510. routines but shows only the skeleton, how to build up the PCM
  1511. interfaces.
  1512. <example>
  1513. <title>PCM Example Code</title>
  1514. <programlisting>
  1515. <![CDATA[
  1516. #include <sound/pcm.h>
  1517. ....
  1518. /* hardware definition */
  1519. static struct snd_pcm_hardware snd_mychip_playback_hw = {
  1520. .info = (SNDRV_PCM_INFO_MMAP |
  1521. SNDRV_PCM_INFO_INTERLEAVED |
  1522. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1523. SNDRV_PCM_INFO_MMAP_VALID),
  1524. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1525. .rates = SNDRV_PCM_RATE_8000_48000,
  1526. .rate_min = 8000,
  1527. .rate_max = 48000,
  1528. .channels_min = 2,
  1529. .channels_max = 2,
  1530. .buffer_bytes_max = 32768,
  1531. .period_bytes_min = 4096,
  1532. .period_bytes_max = 32768,
  1533. .periods_min = 1,
  1534. .periods_max = 1024,
  1535. };
  1536. /* hardware definition */
  1537. static struct snd_pcm_hardware snd_mychip_capture_hw = {
  1538. .info = (SNDRV_PCM_INFO_MMAP |
  1539. SNDRV_PCM_INFO_INTERLEAVED |
  1540. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1541. SNDRV_PCM_INFO_MMAP_VALID),
  1542. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1543. .rates = SNDRV_PCM_RATE_8000_48000,
  1544. .rate_min = 8000,
  1545. .rate_max = 48000,
  1546. .channels_min = 2,
  1547. .channels_max = 2,
  1548. .buffer_bytes_max = 32768,
  1549. .period_bytes_min = 4096,
  1550. .period_bytes_max = 32768,
  1551. .periods_min = 1,
  1552. .periods_max = 1024,
  1553. };
  1554. /* open callback */
  1555. static int snd_mychip_playback_open(struct snd_pcm_substream *substream)
  1556. {
  1557. struct mychip *chip = snd_pcm_substream_chip(substream);
  1558. struct snd_pcm_runtime *runtime = substream->runtime;
  1559. runtime->hw = snd_mychip_playback_hw;
  1560. /* more hardware-initialization will be done here */
  1561. ....
  1562. return 0;
  1563. }
  1564. /* close callback */
  1565. static int snd_mychip_playback_close(struct snd_pcm_substream *substream)
  1566. {
  1567. struct mychip *chip = snd_pcm_substream_chip(substream);
  1568. /* the hardware-specific codes will be here */
  1569. ....
  1570. return 0;
  1571. }
  1572. /* open callback */
  1573. static int snd_mychip_capture_open(struct snd_pcm_substream *substream)
  1574. {
  1575. struct mychip *chip = snd_pcm_substream_chip(substream);
  1576. struct snd_pcm_runtime *runtime = substream->runtime;
  1577. runtime->hw = snd_mychip_capture_hw;
  1578. /* more hardware-initialization will be done here */
  1579. ....
  1580. return 0;
  1581. }
  1582. /* close callback */
  1583. static int snd_mychip_capture_close(struct snd_pcm_substream *substream)
  1584. {
  1585. struct mychip *chip = snd_pcm_substream_chip(substream);
  1586. /* the hardware-specific codes will be here */
  1587. ....
  1588. return 0;
  1589. }
  1590. /* hw_params callback */
  1591. static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
  1592. struct snd_pcm_hw_params *hw_params)
  1593. {
  1594. return snd_pcm_lib_malloc_pages(substream,
  1595. params_buffer_bytes(hw_params));
  1596. }
  1597. /* hw_free callback */
  1598. static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
  1599. {
  1600. return snd_pcm_lib_free_pages(substream);
  1601. }
  1602. /* prepare callback */
  1603. static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream)
  1604. {
  1605. struct mychip *chip = snd_pcm_substream_chip(substream);
  1606. struct snd_pcm_runtime *runtime = substream->runtime;
  1607. /* set up the hardware with the current configuration
  1608. * for example...
  1609. */
  1610. mychip_set_sample_format(chip, runtime->format);
  1611. mychip_set_sample_rate(chip, runtime->rate);
  1612. mychip_set_channels(chip, runtime->channels);
  1613. mychip_set_dma_setup(chip, runtime->dma_addr,
  1614. chip->buffer_size,
  1615. chip->period_size);
  1616. return 0;
  1617. }
  1618. /* trigger callback */
  1619. static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream,
  1620. int cmd)
  1621. {
  1622. switch (cmd) {
  1623. case SNDRV_PCM_TRIGGER_START:
  1624. /* do something to start the PCM engine */
  1625. ....
  1626. break;
  1627. case SNDRV_PCM_TRIGGER_STOP:
  1628. /* do something to stop the PCM engine */
  1629. ....
  1630. break;
  1631. default:
  1632. return -EINVAL;
  1633. }
  1634. }
  1635. /* pointer callback */
  1636. static snd_pcm_uframes_t
  1637. snd_mychip_pcm_pointer(struct snd_pcm_substream *substream)
  1638. {
  1639. struct mychip *chip = snd_pcm_substream_chip(substream);
  1640. unsigned int current_ptr;
  1641. /* get the current hardware pointer */
  1642. current_ptr = mychip_get_hw_pointer(chip);
  1643. return current_ptr;
  1644. }
  1645. /* operators */
  1646. static struct snd_pcm_ops snd_mychip_playback_ops = {
  1647. .open = snd_mychip_playback_open,
  1648. .close = snd_mychip_playback_close,
  1649. .ioctl = snd_pcm_lib_ioctl,
  1650. .hw_params = snd_mychip_pcm_hw_params,
  1651. .hw_free = snd_mychip_pcm_hw_free,
  1652. .prepare = snd_mychip_pcm_prepare,
  1653. .trigger = snd_mychip_pcm_trigger,
  1654. .pointer = snd_mychip_pcm_pointer,
  1655. };
  1656. /* operators */
  1657. static struct snd_pcm_ops snd_mychip_capture_ops = {
  1658. .open = snd_mychip_capture_open,
  1659. .close = snd_mychip_capture_close,
  1660. .ioctl = snd_pcm_lib_ioctl,
  1661. .hw_params = snd_mychip_pcm_hw_params,
  1662. .hw_free = snd_mychip_pcm_hw_free,
  1663. .prepare = snd_mychip_pcm_prepare,
  1664. .trigger = snd_mychip_pcm_trigger,
  1665. .pointer = snd_mychip_pcm_pointer,
  1666. };
  1667. /*
  1668. * definitions of capture are omitted here...
  1669. */
  1670. /* create a pcm device */
  1671. static int __devinit snd_mychip_new_pcm(struct mychip *chip)
  1672. {
  1673. struct snd_pcm *pcm;
  1674. int err;
  1675. err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
  1676. if (err < 0)
  1677. return err;
  1678. pcm->private_data = chip;
  1679. strcpy(pcm->name, "My Chip");
  1680. chip->pcm = pcm;
  1681. /* set operators */
  1682. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1683. &snd_mychip_playback_ops);
  1684. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1685. &snd_mychip_capture_ops);
  1686. /* pre-allocation of buffers */
  1687. /* NOTE: this may fail */
  1688. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1689. snd_dma_pci_data(chip->pci),
  1690. 64*1024, 64*1024);
  1691. return 0;
  1692. }
  1693. ]]>
  1694. </programlisting>
  1695. </example>
  1696. </para>
  1697. </section>
  1698. <section id="pcm-interface-constructor">
  1699. <title>Constructor</title>
  1700. <para>
  1701. A pcm instance is allocated by the <function>snd_pcm_new()</function>
  1702. function. It would be better to create a constructor for pcm,
  1703. namely,
  1704. <informalexample>
  1705. <programlisting>
  1706. <![CDATA[
  1707. static int __devinit snd_mychip_new_pcm(struct mychip *chip)
  1708. {
  1709. struct snd_pcm *pcm;
  1710. int err;
  1711. err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
  1712. if (err < 0)
  1713. return err;
  1714. pcm->private_data = chip;
  1715. strcpy(pcm->name, "My Chip");
  1716. chip->pcm = pcm;
  1717. ....
  1718. return 0;
  1719. }
  1720. ]]>
  1721. </programlisting>
  1722. </informalexample>
  1723. </para>
  1724. <para>
  1725. The <function>snd_pcm_new()</function> function takes four
  1726. arguments. The first argument is the card pointer to which this
  1727. pcm is assigned, and the second is the ID string.
  1728. </para>
  1729. <para>
  1730. The third argument (<parameter>index</parameter>, 0 in the
  1731. above) is the index of this new pcm. It begins from zero. If
  1732. you create more than one pcm instances, specify the
  1733. different numbers in this argument. For example,
  1734. <parameter>index</parameter> = 1 for the second PCM device.
  1735. </para>
  1736. <para>
  1737. The fourth and fifth arguments are the number of substreams
  1738. for playback and capture, respectively. Here 1 is used for
  1739. both arguments. When no playback or capture substreams are available,
  1740. pass 0 to the corresponding argument.
  1741. </para>
  1742. <para>
  1743. If a chip supports multiple playbacks or captures, you can
  1744. specify more numbers, but they must be handled properly in
  1745. open/close, etc. callbacks. When you need to know which
  1746. substream you are referring to, then it can be obtained from
  1747. struct <structname>snd_pcm_substream</structname> data passed to each callback
  1748. as follows:
  1749. <informalexample>
  1750. <programlisting>
  1751. <![CDATA[
  1752. struct snd_pcm_substream *substream;
  1753. int index = substream->number;
  1754. ]]>
  1755. </programlisting>
  1756. </informalexample>
  1757. </para>
  1758. <para>
  1759. After the pcm is created, you need to set operators for each
  1760. pcm stream.
  1761. <informalexample>
  1762. <programlisting>
  1763. <![CDATA[
  1764. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1765. &snd_mychip_playback_ops);
  1766. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1767. &snd_mychip_capture_ops);
  1768. ]]>
  1769. </programlisting>
  1770. </informalexample>
  1771. </para>
  1772. <para>
  1773. The operators are defined typically like this:
  1774. <informalexample>
  1775. <programlisting>
  1776. <![CDATA[
  1777. static struct snd_pcm_ops snd_mychip_playback_ops = {
  1778. .open = snd_mychip_pcm_open,
  1779. .close = snd_mychip_pcm_close,
  1780. .ioctl = snd_pcm_lib_ioctl,
  1781. .hw_params = snd_mychip_pcm_hw_params,
  1782. .hw_free = snd_mychip_pcm_hw_free,
  1783. .prepare = snd_mychip_pcm_prepare,
  1784. .trigger = snd_mychip_pcm_trigger,
  1785. .pointer = snd_mychip_pcm_pointer,
  1786. };
  1787. ]]>
  1788. </programlisting>
  1789. </informalexample>
  1790. All the callbacks are described in the
  1791. <link linkend="pcm-interface-operators"><citetitle>
  1792. Operators</citetitle></link> subsection.
  1793. </para>
  1794. <para>
  1795. After setting the operators, you probably will want to
  1796. pre-allocate the buffer. For the pre-allocation, simply call
  1797. the following:
  1798. <informalexample>
  1799. <programlisting>
  1800. <![CDATA[
  1801. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1802. snd_dma_pci_data(chip->pci),
  1803. 64*1024, 64*1024);
  1804. ]]>
  1805. </programlisting>
  1806. </informalexample>
  1807. It will allocate a buffer up to 64kB as default.
  1808. Buffer management details will be described in the later section <link
  1809. linkend="buffer-and-memory"><citetitle>Buffer and Memory
  1810. Management</citetitle></link>.
  1811. </para>
  1812. <para>
  1813. Additionally, you can set some extra information for this pcm
  1814. in pcm-&gt;info_flags.
  1815. The available values are defined as
  1816. <constant>SNDRV_PCM_INFO_XXX</constant> in
  1817. <filename>&lt;sound/asound.h&gt;</filename>, which is used for
  1818. the hardware definition (described later). When your soundchip
  1819. supports only half-duplex, specify like this:
  1820. <informalexample>
  1821. <programlisting>
  1822. <![CDATA[
  1823. pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
  1824. ]]>
  1825. </programlisting>
  1826. </informalexample>
  1827. </para>
  1828. </section>
  1829. <section id="pcm-interface-destructor">
  1830. <title>... And the Destructor?</title>
  1831. <para>
  1832. The destructor for a pcm instance is not always
  1833. necessary. Since the pcm device will be released by the middle
  1834. layer code automatically, you don't have to call the destructor
  1835. explicitly.
  1836. </para>
  1837. <para>
  1838. The destructor would be necessary if you created
  1839. special records internally and needed to release them. In such a
  1840. case, set the destructor function to
  1841. pcm-&gt;private_free:
  1842. <example>
  1843. <title>PCM Instance with a Destructor</title>
  1844. <programlisting>
  1845. <![CDATA[
  1846. static void mychip_pcm_free(struct snd_pcm *pcm)
  1847. {
  1848. struct mychip *chip = snd_pcm_chip(pcm);
  1849. /* free your own data */
  1850. kfree(chip->my_private_pcm_data);
  1851. /* do what you like else */
  1852. ....
  1853. }
  1854. static int __devinit snd_mychip_new_pcm(struct mychip *chip)
  1855. {
  1856. struct snd_pcm *pcm;
  1857. ....
  1858. /* allocate your own data */
  1859. chip->my_private_pcm_data = kmalloc(...);
  1860. /* set the destructor */
  1861. pcm->private_data = chip;
  1862. pcm->private_free = mychip_pcm_free;
  1863. ....
  1864. }
  1865. ]]>
  1866. </programlisting>
  1867. </example>
  1868. </para>
  1869. </section>
  1870. <section id="pcm-interface-runtime">
  1871. <title>Runtime Pointer - The Chest of PCM Information</title>
  1872. <para>
  1873. When the PCM substream is opened, a PCM runtime instance is
  1874. allocated and assigned to the substream. This pointer is
  1875. accessible via <constant>substream-&gt;runtime</constant>.
  1876. This runtime pointer holds most information you need
  1877. to control the PCM: the copy of hw_params and sw_params configurations, the buffer
  1878. pointers, mmap records, spinlocks, etc.
  1879. </para>
  1880. <para>
  1881. The definition of runtime instance is found in
  1882. <filename>&lt;sound/pcm.h&gt;</filename>. Here are
  1883. the contents of this file:
  1884. <informalexample>
  1885. <programlisting>
  1886. <![CDATA[
  1887. struct _snd_pcm_runtime {
  1888. /* -- Status -- */
  1889. struct snd_pcm_substream *trigger_master;
  1890. snd_timestamp_t trigger_tstamp; /* trigger timestamp */
  1891. int overrange;
  1892. snd_pcm_uframes_t avail_max;
  1893. snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
  1894. snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/
  1895. /* -- HW params -- */
  1896. snd_pcm_access_t access; /* access mode */
  1897. snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
  1898. snd_pcm_subformat_t subformat; /* subformat */
  1899. unsigned int rate; /* rate in Hz */
  1900. unsigned int channels; /* channels */
  1901. snd_pcm_uframes_t period_size; /* period size */
  1902. unsigned int periods; /* periods */
  1903. snd_pcm_uframes_t buffer_size; /* buffer size */
  1904. unsigned int tick_time; /* tick time */
  1905. snd_pcm_uframes_t min_align; /* Min alignment for the format */
  1906. size_t byte_align;
  1907. unsigned int frame_bits;
  1908. unsigned int sample_bits;
  1909. unsigned int info;
  1910. unsigned int rate_num;
  1911. unsigned int rate_den;
  1912. /* -- SW params -- */
  1913. struct timespec tstamp_mode; /* mmap timestamp is updated */
  1914. unsigned int period_step;
  1915. unsigned int sleep_min; /* min ticks to sleep */
  1916. snd_pcm_uframes_t start_threshold;
  1917. snd_pcm_uframes_t stop_threshold;
  1918. snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
  1919. noise is nearest than this */
  1920. snd_pcm_uframes_t silence_size; /* Silence filling size */
  1921. snd_pcm_uframes_t boundary; /* pointers wrap point */
  1922. snd_pcm_uframes_t silenced_start;
  1923. snd_pcm_uframes_t silenced_size;
  1924. snd_pcm_sync_id_t sync; /* hardware synchronization ID */
  1925. /* -- mmap -- */
  1926. volatile struct snd_pcm_mmap_status *status;
  1927. volatile struct snd_pcm_mmap_control *control;
  1928. atomic_t mmap_count;
  1929. /* -- locking / scheduling -- */
  1930. spinlock_t lock;
  1931. wait_queue_head_t sleep;
  1932. struct timer_list tick_timer;
  1933. struct fasync_struct *fasync;
  1934. /* -- private section -- */
  1935. void *private_data;
  1936. void (*private_free)(struct snd_pcm_runtime *runtime);
  1937. /* -- hardware description -- */
  1938. struct snd_pcm_hardware hw;
  1939. struct snd_pcm_hw_constraints hw_constraints;
  1940. /* -- interrupt callbacks -- */
  1941. void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
  1942. void (*transfer_ack_end)(struct snd_pcm_substream *substream);
  1943. /* -- timer -- */
  1944. unsigned int timer_resolution; /* timer resolution */
  1945. /* -- DMA -- */
  1946. unsigned char *dma_area; /* DMA area */
  1947. dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
  1948. size_t dma_bytes; /* size of DMA area */
  1949. struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
  1950. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  1951. /* -- OSS things -- */
  1952. struct snd_pcm_oss_runtime oss;
  1953. #endif
  1954. };
  1955. ]]>
  1956. </programlisting>
  1957. </informalexample>
  1958. </para>
  1959. <para>
  1960. For the operators (callbacks) of each sound driver, most of
  1961. these records are supposed to be read-only. Only the PCM
  1962. middle-layer changes / updates them. The exceptions are
  1963. the hardware description (hw), interrupt callbacks
  1964. (transfer_ack_xxx), DMA buffer information, and the private
  1965. data. Besides, if you use the standard buffer allocation
  1966. method via <function>snd_pcm_lib_malloc_pages()</function>,
  1967. you don't need to set the DMA buffer information by yourself.
  1968. </para>
  1969. <para>
  1970. In the sections below, important records are explained.
  1971. </para>
  1972. <section id="pcm-interface-runtime-hw">
  1973. <title>Hardware Description</title>
  1974. <para>
  1975. The hardware descriptor (struct <structname>snd_pcm_hardware</structname>)
  1976. contains the definitions of the fundamental hardware
  1977. configuration. Above all, you'll need to define this in
  1978. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  1979. the open callback</citetitle></link>.
  1980. Note that the runtime instance holds the copy of the
  1981. descriptor, not the pointer to the existing descriptor. That
  1982. is, in the open callback, you can modify the copied descriptor
  1983. (<constant>runtime-&gt;hw</constant>) as you need. For example, if the maximum
  1984. number of channels is 1 only on some chip models, you can
  1985. still use the same hardware descriptor and change the
  1986. channels_max later:
  1987. <informalexample>
  1988. <programlisting>
  1989. <![CDATA[
  1990. struct snd_pcm_runtime *runtime = substream->runtime;
  1991. ...
  1992. runtime->hw = snd_mychip_playback_hw; /* common definition */
  1993. if (chip->model == VERY_OLD_ONE)
  1994. runtime->hw.channels_max = 1;
  1995. ]]>
  1996. </programlisting>
  1997. </informalexample>
  1998. </para>
  1999. <para>
  2000. Typically, you'll have a hardware descriptor as below:
  2001. <informalexample>
  2002. <programlisting>
  2003. <![CDATA[
  2004. static struct snd_pcm_hardware snd_mychip_playback_hw = {
  2005. .info = (SNDRV_PCM_INFO_MMAP |
  2006. SNDRV_PCM_INFO_INTERLEAVED |
  2007. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  2008. SNDRV_PCM_INFO_MMAP_VALID),
  2009. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  2010. .rates = SNDRV_PCM_RATE_8000_48000,
  2011. .rate_min = 8000,
  2012. .rate_max = 48000,
  2013. .channels_min = 2,
  2014. .channels_max = 2,
  2015. .buffer_bytes_max = 32768,
  2016. .period_bytes_min = 4096,
  2017. .period_bytes_max = 32768,
  2018. .periods_min = 1,
  2019. .periods_max = 1024,
  2020. };
  2021. ]]>
  2022. </programlisting>
  2023. </informalexample>
  2024. </para>
  2025. <para>
  2026. <itemizedlist>
  2027. <listitem><para>
  2028. The <structfield>info</structfield> field contains the type and
  2029. capabilities of this pcm. The bit flags are defined in
  2030. <filename>&lt;sound/asound.h&gt;</filename> as
  2031. <constant>SNDRV_PCM_INFO_XXX</constant>. Here, at least, you
  2032. have to specify whether the mmap is supported and which
  2033. interleaved format is supported.
  2034. When the is supported, add the
  2035. <constant>SNDRV_PCM_INFO_MMAP</constant> flag here. When the
  2036. hardware supports the interleaved or the non-interleaved
  2037. formats, <constant>SNDRV_PCM_INFO_INTERLEAVED</constant> or
  2038. <constant>SNDRV_PCM_INFO_NONINTERLEAVED</constant> flag must
  2039. be set, respectively. If both are supported, you can set both,
  2040. too.
  2041. </para>
  2042. <para>
  2043. In the above example, <constant>MMAP_VALID</constant> and
  2044. <constant>BLOCK_TRANSFER</constant> are specified for the OSS mmap
  2045. mode. Usually both are set. Of course,
  2046. <constant>MMAP_VALID</constant> is set only if the mmap is
  2047. really supported.
  2048. </para>
  2049. <para>
  2050. The other possible flags are
  2051. <constant>SNDRV_PCM_INFO_PAUSE</constant> and
  2052. <constant>SNDRV_PCM_INFO_RESUME</constant>. The
  2053. <constant>PAUSE</constant> bit means that the pcm supports the
  2054. <quote>pause</quote> operation, while the
  2055. <constant>RESUME</constant> bit means that the pcm supports
  2056. the full <quote>suspend/resume</quote> operation.
  2057. If the <constant>PAUSE</constant> flag is set,
  2058. the <structfield>trigger</structfield> callback below
  2059. must handle the corresponding (pause push/release) commands.
  2060. The suspend/resume trigger commands can be defined even without
  2061. the <constant>RESUME</constant> flag. See <link
  2062. linkend="power-management"><citetitle>
  2063. Power Management</citetitle></link> section for details.
  2064. </para>
  2065. <para>
  2066. When the PCM substreams can be synchronized (typically,
  2067. synchronized start/stop of a playback and a capture streams),
  2068. you can give <constant>SNDRV_PCM_INFO_SYNC_START</constant>,
  2069. too. In this case, you'll need to check the linked-list of
  2070. PCM substreams in the trigger callback. This will be
  2071. described in the later section.
  2072. </para>
  2073. </listitem>
  2074. <listitem>
  2075. <para>
  2076. <structfield>formats</structfield> field contains the bit-flags
  2077. of supported formats (<constant>SNDRV_PCM_FMTBIT_XXX</constant>).
  2078. If the hardware supports more than one format, give all or'ed
  2079. bits. In the example above, the signed 16bit little-endian
  2080. format is specified.
  2081. </para>
  2082. </listitem>
  2083. <listitem>
  2084. <para>
  2085. <structfield>rates</structfield> field contains the bit-flags of
  2086. supported rates (<constant>SNDRV_PCM_RATE_XXX</constant>).
  2087. When the chip supports continuous rates, pass
  2088. <constant>CONTINUOUS</constant> bit additionally.
  2089. The pre-defined rate bits are provided only for typical
  2090. rates. If your chip supports unconventional rates, you need to add
  2091. the <constant>KNOT</constant> bit and set up the hardware
  2092. constraint manually (explained later).
  2093. </para>
  2094. </listitem>
  2095. <listitem>
  2096. <para>
  2097. <structfield>rate_min</structfield> and
  2098. <structfield>rate_max</structfield> define the minimum and
  2099. maximum sample rate. This should correspond somehow to
  2100. <structfield>rates</structfield> bits.
  2101. </para>
  2102. </listitem>
  2103. <listitem>
  2104. <para>
  2105. <structfield>channel_min</structfield> and
  2106. <structfield>channel_max</structfield>
  2107. define, as you might already expected, the minimum and maximum
  2108. number of channels.
  2109. </para>
  2110. </listitem>
  2111. <listitem>
  2112. <para>
  2113. <structfield>buffer_bytes_max</structfield> defines the
  2114. maximum buffer size in bytes. There is no
  2115. <structfield>buffer_bytes_min</structfield> field, since
  2116. it can be calculated from the minimum period size and the
  2117. minimum number of periods.
  2118. Meanwhile, <structfield>period_bytes_min</structfield> and
  2119. define the minimum and maximum size of the period in bytes.
  2120. <structfield>periods_max</structfield> and
  2121. <structfield>periods_min</structfield> define the maximum and
  2122. minimum number of periods in the buffer.
  2123. </para>
  2124. <para>
  2125. The <quote>period</quote> is a term that corresponds to
  2126. a fragment in the OSS world. The period defines the size at
  2127. which a PCM interrupt is generated. This size strongly
  2128. depends on the hardware.
  2129. Generally, the smaller period size will give you more
  2130. interrupts, that is, more controls.
  2131. In the case of capture, this size defines the input latency.
  2132. On the other hand, the whole buffer size defines the
  2133. output latency for the playback direction.
  2134. </para>
  2135. </listitem>
  2136. <listitem>
  2137. <para>
  2138. There is also a field <structfield>fifo_size</structfield>.
  2139. This specifies the size of the hardware FIFO, but currently it
  2140. is neither used in the driver nor in the alsa-lib. So, you
  2141. can ignore this field.
  2142. </para>
  2143. </listitem>
  2144. </itemizedlist>
  2145. </para>
  2146. </section>
  2147. <section id="pcm-interface-runtime-config">
  2148. <title>PCM Configurations</title>
  2149. <para>
  2150. Ok, let's go back again to the PCM runtime records.
  2151. The most frequently referred records in the runtime instance are
  2152. the PCM configurations.
  2153. The PCM configurations are stored in the runtime instance
  2154. after the application sends <type>hw_params</type> data via
  2155. alsa-lib. There are many fields copied from hw_params and
  2156. sw_params structs. For example,
  2157. <structfield>format</structfield> holds the format type
  2158. chosen by the application. This field contains the enum value
  2159. <constant>SNDRV_PCM_FORMAT_XXX</constant>.
  2160. </para>
  2161. <para>
  2162. One thing to be noted is that the configured buffer and period
  2163. sizes are stored in <quote>frames</quote> in the runtime.
  2164. In the ALSA world, 1 frame = channels * samples-size.
  2165. For conversion between frames and bytes, you can use the
  2166. <function>frames_to_bytes()</function> and
  2167. <function>bytes_to_frames()</function> helper functions.
  2168. <informalexample>
  2169. <programlisting>
  2170. <![CDATA[
  2171. period_bytes = frames_to_bytes(runtime, runtime->period_size);
  2172. ]]>
  2173. </programlisting>
  2174. </informalexample>
  2175. </para>
  2176. <para>
  2177. Also, many software parameters (sw_params) are
  2178. stored in frames, too. Please check the type of the field.
  2179. <type>snd_pcm_uframes_t</type> is for the frames as unsigned
  2180. integer while <type>snd_pcm_sframes_t</type> is for the frames
  2181. as signed integer.
  2182. </para>
  2183. </section>
  2184. <section id="pcm-interface-runtime-dma">
  2185. <title>DMA Buffer Information</title>
  2186. <para>
  2187. The DMA buffer is defined by the following four fields,
  2188. <structfield>dma_area</structfield>,
  2189. <structfield>dma_addr</structfield>,
  2190. <structfield>dma_bytes</structfield> and
  2191. <structfield>dma_private</structfield>.
  2192. The <structfield>dma_area</structfield> holds the buffer
  2193. pointer (the logical address). You can call
  2194. <function>memcpy</function> from/to
  2195. this pointer. Meanwhile, <structfield>dma_addr</structfield>
  2196. holds the physical address of the buffer. This field is
  2197. specified only when the buffer is a linear buffer.
  2198. <structfield>dma_bytes</structfield> holds the size of buffer
  2199. in bytes. <structfield>dma_private</structfield> is used for
  2200. the ALSA DMA allocator.
  2201. </para>
  2202. <para>
  2203. If you use a standard ALSA function,
  2204. <function>snd_pcm_lib_malloc_pages()</function>, for
  2205. allocating the buffer, these fields are set by the ALSA middle
  2206. layer, and you should <emphasis>not</emphasis> change them by
  2207. yourself. You can read them but not write them.
  2208. On the other hand, if you want to allocate the buffer by
  2209. yourself, you'll need to manage it in hw_params callback.
  2210. At least, <structfield>dma_bytes</structfield> is mandatory.
  2211. <structfield>dma_area</structfield> is necessary when the
  2212. buffer is mmapped. If your driver doesn't support mmap, this
  2213. field is not necessary. <structfield>dma_addr</structfield>
  2214. is also optional. You can use
  2215. <structfield>dma_private</structfield> as you like, too.
  2216. </para>
  2217. </section>
  2218. <section id="pcm-interface-runtime-status">
  2219. <title>Running Status</title>
  2220. <para>
  2221. The running status can be referred via <constant>runtime-&gt;status</constant>.
  2222. This is the pointer to the struct <structname>snd_pcm_mmap_status</structname>
  2223. record. For example, you can get the current DMA hardware
  2224. pointer via <constant>runtime-&gt;status-&gt;hw_ptr</constant>.
  2225. </para>
  2226. <para>
  2227. The DMA application pointer can be referred via
  2228. <constant>runtime-&gt;control</constant>, which points to the
  2229. struct <structname>snd_pcm_mmap_control</structname> record.
  2230. However, accessing directly to this value is not recommended.
  2231. </para>
  2232. </section>
  2233. <section id="pcm-interface-runtime-private">
  2234. <title>Private Data</title>
  2235. <para>
  2236. You can allocate a record for the substream and store it in
  2237. <constant>runtime-&gt;private_data</constant>. Usually, this
  2238. is done in
  2239. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  2240. the open callback</citetitle></link>.
  2241. Don't mix this with <constant>pcm-&gt;private_data</constant>.
  2242. The <constant>pcm-&gt;private_data</constant> usually points to the
  2243. chip instance assigned statically at the creation of PCM, while the
  2244. <constant>runtime-&gt;private_data</constant> points to a dynamic
  2245. data structure created at the PCM open callback.
  2246. <informalexample>
  2247. <programlisting>
  2248. <![CDATA[
  2249. static int snd_xxx_open(struct snd_pcm_substream *substream)
  2250. {
  2251. struct my_pcm_data *data;
  2252. ....
  2253. data = kmalloc(sizeof(*data), GFP_KERNEL);
  2254. substream->runtime->private_data = data;
  2255. ....
  2256. }
  2257. ]]>
  2258. </programlisting>
  2259. </informalexample>
  2260. </para>
  2261. <para>
  2262. The allocated object must be released in
  2263. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  2264. the close callback</citetitle></link>.
  2265. </para>
  2266. </section>
  2267. <section id="pcm-interface-runtime-intr">
  2268. <title>Interrupt Callbacks</title>
  2269. <para>
  2270. The field <structfield>transfer_ack_begin</structfield> and
  2271. <structfield>transfer_ack_end</structfield> are called at
  2272. the beginning and at the end of
  2273. <function>snd_pcm_period_elapsed()</function>, respectively.
  2274. </para>
  2275. </section>
  2276. </section>
  2277. <section id="pcm-interface-operators">
  2278. <title>Operators</title>
  2279. <para>
  2280. OK, now let me give details about each pcm callback
  2281. (<parameter>ops</parameter>). In general, every callback must
  2282. return 0 if successful, or a negative error number
  2283. such as <constant>-EINVAL</constant>. To choose an appropriate
  2284. error number, it is advised to check what value other parts of
  2285. the kernel return when the same kind of request fails.
  2286. </para>
  2287. <para>
  2288. The callback function takes at least the argument with
  2289. <structname>snd_pcm_substream</structname> pointer. To retrieve
  2290. the chip record from the given substream instance, you can use the
  2291. following macro.
  2292. <informalexample>
  2293. <programlisting>
  2294. <![CDATA[
  2295. int xxx() {
  2296. struct mychip *chip = snd_pcm_substream_chip(substream);
  2297. ....
  2298. }
  2299. ]]>
  2300. </programlisting>
  2301. </informalexample>
  2302. The macro reads <constant>substream-&gt;private_data</constant>,
  2303. which is a copy of <constant>pcm-&gt;private_data</constant>.
  2304. You can override the former if you need to assign different data
  2305. records per PCM substream. For example, the cmi8330 driver assigns
  2306. different private_data for playback and capture directions,
  2307. because it uses two different codecs (SB- and AD-compatible) for
  2308. different directions.
  2309. </para>
  2310. <section id="pcm-interface-operators-open-callback">
  2311. <title>open callback</title>
  2312. <para>
  2313. <informalexample>
  2314. <programlisting>
  2315. <![CDATA[
  2316. static int snd_xxx_open(struct snd_pcm_substream *substream);
  2317. ]]>
  2318. </programlisting>
  2319. </informalexample>
  2320. This is called when a pcm substream is opened.
  2321. </para>
  2322. <para>
  2323. At least, here you have to initialize the runtime-&gt;hw
  2324. record. Typically, this is done by like this:
  2325. <informalexample>
  2326. <programlisting>
  2327. <![CDATA[
  2328. static int snd_xxx_open(struct snd_pcm_substream *substream)
  2329. {
  2330. struct mychip *chip = snd_pcm_substream_chip(substream);
  2331. struct snd_pcm_runtime *runtime = substream->runtime;
  2332. runtime->hw = snd_mychip_playback_hw;
  2333. return 0;
  2334. }
  2335. ]]>
  2336. </programlisting>
  2337. </informalexample>
  2338. where <parameter>snd_mychip_playback_hw</parameter> is the
  2339. pre-defined hardware description.
  2340. </para>
  2341. <para>
  2342. You can allocate a private data in this callback, as described
  2343. in <link linkend="pcm-interface-runtime-private"><citetitle>
  2344. Private Data</citetitle></link> section.
  2345. </para>
  2346. <para>
  2347. If the hardware configuration needs more constraints, set the
  2348. hardware constraints here, too.
  2349. See <link linkend="pcm-interface-constraints"><citetitle>
  2350. Constraints</citetitle></link> for more details.
  2351. </para>
  2352. </section>
  2353. <section id="pcm-interface-operators-close-callback">
  2354. <title>close callback</title>
  2355. <para>
  2356. <informalexample>
  2357. <programlisting>
  2358. <![CDATA[
  2359. static int snd_xxx_close(struct snd_pcm_substream *substream);
  2360. ]]>
  2361. </programlisting>
  2362. </informalexample>
  2363. Obviously, this is called when a pcm substream is closed.
  2364. </para>
  2365. <para>
  2366. Any private instance for a pcm substream allocated in the
  2367. open callback will be released here.
  2368. <informalexample>
  2369. <programlisting>
  2370. <![CDATA[
  2371. static int snd_xxx_close(struct snd_pcm_substream *substream)
  2372. {
  2373. ....
  2374. kfree(substream->runtime->private_data);
  2375. ....
  2376. }
  2377. ]]>
  2378. </programlisting>
  2379. </informalexample>
  2380. </para>
  2381. </section>
  2382. <section id="pcm-interface-operators-ioctl-callback">
  2383. <title>ioctl callback</title>
  2384. <para>
  2385. This is used for any special call to pcm ioctls. But
  2386. usually you can pass a generic ioctl callback,
  2387. <function>snd_pcm_lib_ioctl</function>.
  2388. </para>
  2389. </section>
  2390. <section id="pcm-interface-operators-hw-params-callback">
  2391. <title>hw_params callback</title>
  2392. <para>
  2393. <informalexample>
  2394. <programlisting>
  2395. <![CDATA[
  2396. static int snd_xxx_hw_params(struct snd_pcm_substream *substream,
  2397. struct snd_pcm_hw_params *hw_params);
  2398. ]]>
  2399. </programlisting>
  2400. </informalexample>
  2401. </para>
  2402. <para>
  2403. This is called when the hardware parameter
  2404. (<structfield>hw_params</structfield>) is set
  2405. up by the application,
  2406. that is, once when the buffer size, the period size, the
  2407. format, etc. are defined for the pcm substream.
  2408. </para>
  2409. <para>
  2410. Many hardware setups should be done in this callback,
  2411. including the allocation of buffers.
  2412. </para>
  2413. <para>
  2414. Parameters to be initialized are retrieved by
  2415. <function>params_xxx()</function> macros. To allocate
  2416. buffer, you can call a helper function,
  2417. <informalexample>
  2418. <programlisting>
  2419. <![CDATA[
  2420. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  2421. ]]>
  2422. </programlisting>
  2423. </informalexample>
  2424. <function>snd_pcm_lib_malloc_pages()</function> is available
  2425. only when the DMA buffers have been pre-allocated.
  2426. See the section <link
  2427. linkend="buffer-and-memory-buffer-types"><citetitle>
  2428. Buffer Types</citetitle></link> for more details.
  2429. </para>
  2430. <para>
  2431. Note that this and <structfield>prepare</structfield> callbacks
  2432. may be called multiple times per initialization.
  2433. For example, the OSS emulation may
  2434. call these callbacks at each change via its ioctl.
  2435. </para>
  2436. <para>
  2437. Thus, you need to be careful not to allocate the same buffers
  2438. many times, which will lead to memory leaks! Calling the
  2439. helper function above many times is OK. It will release the
  2440. previous buffer automatically when it was already allocated.
  2441. </para>
  2442. <para>
  2443. Another note is that this callback is non-atomic
  2444. (schedulable). This is important, because the
  2445. <structfield>trigger</structfield> callback
  2446. is atomic (non-schedulable). That is, mutexes or any
  2447. schedule-related functions are not available in
  2448. <structfield>trigger</structfield> callback.
  2449. Please see the subsection
  2450. <link linkend="pcm-interface-atomicity"><citetitle>
  2451. Atomicity</citetitle></link> for details.
  2452. </para>
  2453. </section>
  2454. <section id="pcm-interface-operators-hw-free-callback">
  2455. <title>hw_free callback</title>
  2456. <para>
  2457. <informalexample>
  2458. <programlisting>
  2459. <![CDATA[
  2460. static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
  2461. ]]>
  2462. </programlisting>
  2463. </informalexample>
  2464. </para>
  2465. <para>
  2466. This is called to release the resources allocated via
  2467. <structfield>hw_params</structfield>. For example, releasing the
  2468. buffer via
  2469. <function>snd_pcm_lib_malloc_pages()</function> is done by
  2470. calling the following:
  2471. <informalexample>
  2472. <programlisting>
  2473. <![CDATA[
  2474. snd_pcm_lib_free_pages(substream);
  2475. ]]>
  2476. </programlisting>
  2477. </informalexample>
  2478. </para>
  2479. <para>
  2480. This function is always called before the close callback is called.
  2481. Also, the callback may be called multiple times, too.
  2482. Keep track whether the resource was already released.
  2483. </para>
  2484. </section>
  2485. <section id="pcm-interface-operators-prepare-callback">
  2486. <title>prepare callback</title>
  2487. <para>
  2488. <informalexample>
  2489. <programlisting>
  2490. <![CDATA[
  2491. static int snd_xxx_prepare(struct snd_pcm_substream *substream);
  2492. ]]>
  2493. </programlisting>
  2494. </informalexample>
  2495. </para>
  2496. <para>
  2497. This callback is called when the pcm is
  2498. <quote>prepared</quote>. You can set the format type, sample
  2499. rate, etc. here. The difference from
  2500. <structfield>hw_params</structfield> is that the
  2501. <structfield>prepare</structfield> callback will be called each
  2502. time
  2503. <function>snd_pcm_prepare()</function> is called, i.e. when
  2504. recovering after underruns, etc.
  2505. </para>
  2506. <para>
  2507. Note that this callback is now non-atomic.
  2508. You can use schedule-related functions safely in this callback.
  2509. </para>
  2510. <para>
  2511. In this and the following callbacks, you can refer to the
  2512. values via the runtime record,
  2513. substream-&gt;runtime.
  2514. For example, to get the current
  2515. rate, format or channels, access to
  2516. runtime-&gt;rate,
  2517. runtime-&gt;format or
  2518. runtime-&gt;channels, respectively.
  2519. The physical address of the allocated buffer is set to
  2520. runtime-&gt;dma_area. The buffer and period sizes are
  2521. in runtime-&gt;buffer_size and runtime-&gt;period_size,
  2522. respectively.
  2523. </para>
  2524. <para>
  2525. Be careful that this callback will be called many times at
  2526. each setup, too.
  2527. </para>
  2528. </section>
  2529. <section id="pcm-interface-operators-trigger-callback">
  2530. <title>trigger callback</title>
  2531. <para>
  2532. <informalexample>
  2533. <programlisting>
  2534. <![CDATA[
  2535. static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);
  2536. ]]>
  2537. </programlisting>
  2538. </informalexample>
  2539. This is called when the pcm is started, stopped or paused.
  2540. </para>
  2541. <para>
  2542. Which action is specified in the second argument,
  2543. <constant>SNDRV_PCM_TRIGGER_XXX</constant> in
  2544. <filename>&lt;sound/pcm.h&gt;</filename>. At least,
  2545. the <constant>START</constant> and <constant>STOP</constant>
  2546. commands must be defined in this callback.
  2547. <informalexample>
  2548. <programlisting>
  2549. <![CDATA[
  2550. switch (cmd) {
  2551. case SNDRV_PCM_TRIGGER_START:
  2552. /* do something to start the PCM engine */
  2553. break;
  2554. case SNDRV_PCM_TRIGGER_STOP:
  2555. /* do something to stop the PCM engine */
  2556. break;
  2557. default:
  2558. return -EINVAL;
  2559. }
  2560. ]]>
  2561. </programlisting>
  2562. </informalexample>
  2563. </para>
  2564. <para>
  2565. When the pcm supports the pause operation (given in the info
  2566. field of the hardware table), the <constant>PAUSE_PUSE</constant>
  2567. and <constant>PAUSE_RELEASE</constant> commands must be
  2568. handled here, too. The former is the command to pause the pcm,
  2569. and the latter to restart the pcm again.
  2570. </para>
  2571. <para>
  2572. When the pcm supports the suspend/resume operation,
  2573. regardless of full or partial suspend/resume support,
  2574. the <constant>SUSPEND</constant> and <constant>RESUME</constant>
  2575. commands must be handled, too.
  2576. These commands are issued when the power-management status is
  2577. changed. Obviously, the <constant>SUSPEND</constant> and
  2578. <constant>RESUME</constant> commands
  2579. suspend and resume the pcm substream, and usually, they
  2580. are identical to the <constant>STOP</constant> and
  2581. <constant>START</constant> commands, respectively.
  2582. See the <link linkend="power-management"><citetitle>
  2583. Power Management</citetitle></link> section for details.
  2584. </para>
  2585. <para>
  2586. As mentioned, this callback is atomic. You cannot call
  2587. functions which may sleep.
  2588. The trigger callback should be as minimal as possible,
  2589. just really triggering the DMA. The other stuff should be
  2590. initialized hw_params and prepare callbacks properly
  2591. beforehand.
  2592. </para>
  2593. </section>
  2594. <section id="pcm-interface-operators-pointer-callback">
  2595. <title>pointer callback</title>
  2596. <para>
  2597. <informalexample>
  2598. <programlisting>
  2599. <![CDATA[
  2600. static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)
  2601. ]]>
  2602. </programlisting>
  2603. </informalexample>
  2604. This callback is called when the PCM middle layer inquires
  2605. the current hardware position on the buffer. The position must
  2606. be returned in frames,
  2607. ranging from 0 to buffer_size - 1.
  2608. </para>
  2609. <para>
  2610. This is called usually from the buffer-update routine in the
  2611. pcm middle layer, which is invoked when
  2612. <function>snd_pcm_period_elapsed()</function> is called in the
  2613. interrupt routine. Then the pcm middle layer updates the
  2614. position and calculates the available space, and wakes up the
  2615. sleeping poll threads, etc.
  2616. </para>
  2617. <para>
  2618. This callback is also atomic.
  2619. </para>
  2620. </section>
  2621. <section id="pcm-interface-operators-copy-silence">
  2622. <title>copy and silence callbacks</title>
  2623. <para>
  2624. These callbacks are not mandatory, and can be omitted in
  2625. most cases. These callbacks are used when the hardware buffer
  2626. cannot be in the normal memory space. Some chips have their
  2627. own buffer on the hardware which is not mappable. In such a
  2628. case, you have to transfer the data manually from the memory
  2629. buffer to the hardware buffer. Or, if the buffer is
  2630. non-contiguous on both physical and virtual memory spaces,
  2631. these callbacks must be defined, too.
  2632. </para>
  2633. <para>
  2634. If these two callbacks are defined, copy and set-silence
  2635. operations are done by them. The detailed will be described in
  2636. the later section <link
  2637. linkend="buffer-and-memory"><citetitle>Buffer and Memory
  2638. Management</citetitle></link>.
  2639. </para>
  2640. </section>
  2641. <section id="pcm-interface-operators-ack">
  2642. <title>ack callback</title>
  2643. <para>
  2644. This callback is also not mandatory. This callback is called
  2645. when the appl_ptr is updated in read or write operations.
  2646. Some drivers like emu10k1-fx and cs46xx need to track the
  2647. current appl_ptr for the internal buffer, and this callback
  2648. is useful only for such a purpose.
  2649. </para>
  2650. <para>
  2651. This callback is atomic.
  2652. </para>
  2653. </section>
  2654. <section id="pcm-interface-operators-page-callback">
  2655. <title>page callback</title>
  2656. <para>
  2657. This callback is optional too. This callback is used
  2658. mainly for non-contiguous buffers. The mmap calls this
  2659. callback to get the page address. Some examples will be
  2660. explained in the later section <link
  2661. linkend="buffer-and-memory"><citetitle>Buffer and Memory
  2662. Management</citetitle></link>, too.
  2663. </para>
  2664. </section>
  2665. </section>
  2666. <section id="pcm-interface-interrupt-handler">
  2667. <title>Interrupt Handler</title>
  2668. <para>
  2669. The rest of pcm stuff is the PCM interrupt handler. The
  2670. role of PCM interrupt handler in the sound driver is to update
  2671. the buffer position and to tell the PCM middle layer when the
  2672. buffer position goes across the prescribed period size. To
  2673. inform this, call the <function>snd_pcm_period_elapsed()</function>
  2674. function.
  2675. </para>
  2676. <para>
  2677. There are several types of sound chips to generate the interrupts.
  2678. </para>
  2679. <section id="pcm-interface-interrupt-handler-boundary">
  2680. <title>Interrupts at the period (fragment) boundary</title>
  2681. <para>
  2682. This is the most frequently found type: the hardware
  2683. generates an interrupt at each period boundary.
  2684. In this case, you can call
  2685. <function>snd_pcm_period_elapsed()</function> at each
  2686. interrupt.
  2687. </para>
  2688. <para>
  2689. <function>snd_pcm_period_elapsed()</function> takes the
  2690. substream pointer as its argument. Thus, you need to keep the
  2691. substream pointer accessible from the chip instance. For
  2692. example, define substream field in the chip record to hold the
  2693. current running substream pointer, and set the pointer value
  2694. at open callback (and reset at close callback).
  2695. </para>
  2696. <para>
  2697. If you acquire a spinlock in the interrupt handler, and the
  2698. lock is used in other pcm callbacks, too, then you have to
  2699. release the lock before calling
  2700. <function>snd_pcm_period_elapsed()</function>, because
  2701. <function>snd_pcm_period_elapsed()</function> calls other pcm
  2702. callbacks inside.
  2703. </para>
  2704. <para>
  2705. Typical code would be like:
  2706. <example>
  2707. <title>Interrupt Handler Case #1</title>
  2708. <programlisting>
  2709. <![CDATA[
  2710. static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
  2711. {
  2712. struct mychip *chip = dev_id;
  2713. spin_lock(&chip->lock);
  2714. ....
  2715. if (pcm_irq_invoked(chip)) {
  2716. /* call updater, unlock before it */
  2717. spin_unlock(&chip->lock);
  2718. snd_pcm_period_elapsed(chip->substream);
  2719. spin_lock(&chip->lock);
  2720. /* acknowledge the interrupt if necessary */
  2721. }
  2722. ....
  2723. spin_unlock(&chip->lock);
  2724. return IRQ_HANDLED;
  2725. }
  2726. ]]>
  2727. </programlisting>
  2728. </example>
  2729. </para>
  2730. </section>
  2731. <section id="pcm-interface-interrupt-handler-timer">
  2732. <title>High frequency timer interrupts</title>
  2733. <para>
  2734. This happense when the hardware doesn't generate interrupts
  2735. at the period boundary but issues timer interrupts at a fixed
  2736. timer rate (e.g. es1968 or ymfpci drivers).
  2737. In this case, you need to check the current hardware
  2738. position and accumulate the processed sample length at each
  2739. interrupt. When the accumulated size exceeds the period
  2740. size, call
  2741. <function>snd_pcm_period_elapsed()</function> and reset the
  2742. accumulator.
  2743. </para>
  2744. <para>
  2745. Typical code would be like the following.
  2746. <example>
  2747. <title>Interrupt Handler Case #2</title>
  2748. <programlisting>
  2749. <![CDATA[
  2750. static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
  2751. {
  2752. struct mychip *chip = dev_id;
  2753. spin_lock(&chip->lock);
  2754. ....
  2755. if (pcm_irq_invoked(chip)) {
  2756. unsigned int last_ptr, size;
  2757. /* get the current hardware pointer (in frames) */
  2758. last_ptr = get_hw_ptr(chip);
  2759. /* calculate the processed frames since the
  2760. * last update
  2761. */
  2762. if (last_ptr < chip->last_ptr)
  2763. size = runtime->buffer_size + last_ptr
  2764. - chip->last_ptr;
  2765. else
  2766. size = last_ptr - chip->last_ptr;
  2767. /* remember the last updated point */
  2768. chip->last_ptr = last_ptr;
  2769. /* accumulate the size */
  2770. chip->size += size;
  2771. /* over the period boundary? */
  2772. if (chip->size >= runtime->period_size) {
  2773. /* reset the accumulator */
  2774. chip->size %= runtime->period_size;
  2775. /* call updater */
  2776. spin_unlock(&chip->lock);
  2777. snd_pcm_period_elapsed(substream);
  2778. spin_lock(&chip->lock);
  2779. }
  2780. /* acknowledge the interrupt if necessary */
  2781. }
  2782. ....
  2783. spin_unlock(&chip->lock);
  2784. return IRQ_HANDLED;
  2785. }
  2786. ]]>
  2787. </programlisting>
  2788. </example>
  2789. </para>
  2790. </section>
  2791. <section id="pcm-interface-interrupt-handler-both">
  2792. <title>On calling <function>snd_pcm_period_elapsed()</function></title>
  2793. <para>
  2794. In both cases, even if more than one period are elapsed, you
  2795. don't have to call
  2796. <function>snd_pcm_period_elapsed()</function> many times. Call
  2797. only once. And the pcm layer will check the current hardware
  2798. pointer and update to the latest status.
  2799. </para>
  2800. </section>
  2801. </section>
  2802. <section id="pcm-interface-atomicity">
  2803. <title>Atomicity</title>
  2804. <para>
  2805. One of the most important (and thus difficult to debug) problems
  2806. in kernel programming are race conditions.
  2807. In the Linux kernel, they are usually avoided via spin-locks, mutexes
  2808. or semaphores. In general, if a race condition can happen
  2809. in an interrupt handler, it has to be managed atomically, and you
  2810. have to use a spinlock to protect the critical session. If the
  2811. critical section is not in interrupt handler code and
  2812. if taking a relatively long time to execute is acceptable, you
  2813. should use mutexes or semaphores instead.
  2814. </para>
  2815. <para>
  2816. As already seen, some pcm callbacks are atomic and some are
  2817. not. For example, the <parameter>hw_params</parameter> callback is
  2818. non-atomic, while <parameter>trigger</parameter> callback is
  2819. atomic. This means, the latter is called already in a spinlock
  2820. held by the PCM middle layer. Please take this atomicity into
  2821. account when you choose a locking scheme in the callbacks.
  2822. </para>
  2823. <para>
  2824. In the atomic callbacks, you cannot use functions which may call
  2825. <function>schedule</function> or go to
  2826. <function>sleep</function>. Semaphores and mutexes can sleep,
  2827. and hence they cannot be used inside the atomic callbacks
  2828. (e.g. <parameter>trigger</parameter> callback).
  2829. To implement some delay in such a callback, please use
  2830. <function>udelay()</function> or <function>mdelay()</function>.
  2831. </para>
  2832. <para>
  2833. All three atomic callbacks (trigger, pointer, and ack) are
  2834. called with local interrupts disabled.
  2835. </para>
  2836. </section>
  2837. <section id="pcm-interface-constraints">
  2838. <title>Constraints</title>
  2839. <para>
  2840. If your chip supports unconventional sample rates, or only the
  2841. limited samples, you need to set a constraint for the
  2842. condition.
  2843. </para>
  2844. <para>
  2845. For example, in order to restrict the sample rates in the some
  2846. supported values, use
  2847. <function>snd_pcm_hw_constraint_list()</function>.
  2848. You need to call this function in the open callback.
  2849. <example>
  2850. <title>Example of Hardware Constraints</title>
  2851. <programlisting>
  2852. <![CDATA[
  2853. static unsigned int rates[] =
  2854. {4000, 10000, 22050, 44100};
  2855. static struct snd_pcm_hw_constraint_list constraints_rates = {
  2856. .count = ARRAY_SIZE(rates),
  2857. .list = rates,
  2858. .mask = 0,
  2859. };
  2860. static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
  2861. {
  2862. int err;
  2863. ....
  2864. err = snd_pcm_hw_constraint_list(substream->runtime, 0,
  2865. SNDRV_PCM_HW_PARAM_RATE,
  2866. &constraints_rates);
  2867. if (err < 0)
  2868. return err;
  2869. ....
  2870. }
  2871. ]]>
  2872. </programlisting>
  2873. </example>
  2874. </para>
  2875. <para>
  2876. There are many different constraints.
  2877. Look at <filename>sound/pcm.h</filename> for a complete list.
  2878. You can even define your own constraint rules.
  2879. For example, let's suppose my_chip can manage a substream of 1 channel
  2880. if and only if the format is S16_LE, otherwise it supports any format
  2881. specified in the <structname>snd_pcm_hardware</structname> structure (or in any
  2882. other constraint_list). You can build a rule like this:
  2883. <example>
  2884. <title>Example of Hardware Constraints for Channels</title>
  2885. <programlisting>
  2886. <![CDATA[
  2887. static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
  2888. struct snd_pcm_hw_rule *rule)
  2889. {
  2890. struct snd_interval *c = hw_param_interval(params,
  2891. SNDRV_PCM_HW_PARAM_CHANNELS);
  2892. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  2893. struct snd_mask fmt;
  2894. snd_mask_any(&fmt); /* Init the struct */
  2895. if (c->min < 2) {
  2896. fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE;
  2897. return snd_mask_refine(f, &fmt);
  2898. }
  2899. return 0;
  2900. }
  2901. ]]>
  2902. </programlisting>
  2903. </example>
  2904. </para>
  2905. <para>
  2906. Then you need to call this function to add your rule:
  2907. <informalexample>
  2908. <programlisting>
  2909. <![CDATA[
  2910. snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  2911. hw_rule_channels_by_format, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  2912. -1);
  2913. ]]>
  2914. </programlisting>
  2915. </informalexample>
  2916. </para>
  2917. <para>
  2918. The rule function is called when an application sets the number of
  2919. channels. But an application can set the format before the number of
  2920. channels. Thus you also need to define the inverse rule:
  2921. <example>
  2922. <title>Example of Hardware Constraints for Channels</title>
  2923. <programlisting>
  2924. <![CDATA[
  2925. static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
  2926. struct snd_pcm_hw_rule *rule)
  2927. {
  2928. struct snd_interval *c = hw_param_interval(params,
  2929. SNDRV_PCM_HW_PARAM_CHANNELS);
  2930. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  2931. struct snd_interval ch;
  2932. snd_interval_any(&ch);
  2933. if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
  2934. ch.min = ch.max = 1;
  2935. ch.integer = 1;
  2936. return snd_interval_refine(c, &ch);
  2937. }
  2938. return 0;
  2939. }
  2940. ]]>
  2941. </programlisting>
  2942. </example>
  2943. </para>
  2944. <para>
  2945. ...and in the open callback:
  2946. <informalexample>
  2947. <programlisting>
  2948. <![CDATA[
  2949. snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  2950. hw_rule_format_by_channels, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  2951. -1);
  2952. ]]>
  2953. </programlisting>
  2954. </informalexample>
  2955. </para>
  2956. <para>
  2957. I won't give more details here, rather I
  2958. would like to say, <quote>Luke, use the source.</quote>
  2959. </para>
  2960. </section>
  2961. </chapter>
  2962. <!-- ****************************************************** -->
  2963. <!-- Control Interface -->
  2964. <!-- ****************************************************** -->
  2965. <chapter id="control-interface">
  2966. <title>Control Interface</title>
  2967. <section id="control-interface-general">
  2968. <title>General</title>
  2969. <para>
  2970. The control interface is used widely for many switches,
  2971. sliders, etc. which are accessed from user-space. Its most
  2972. important use is the mixer interface. In other words, since ALSA
  2973. 0.9.x, all the mixer stuff is implemented on the control kernel API.
  2974. </para>
  2975. <para>
  2976. ALSA has a well-defined AC97 control module. If your chip
  2977. supports only the AC97 and nothing else, you can skip this
  2978. section.
  2979. </para>
  2980. <para>
  2981. The control API is defined in
  2982. <filename>&lt;sound/control.h&gt;</filename>.
  2983. Include this file if you want to add your own controls.
  2984. </para>
  2985. </section>
  2986. <section id="control-interface-definition">
  2987. <title>Definition of Controls</title>
  2988. <para>
  2989. To create a new control, you need to define the
  2990. following three
  2991. callbacks: <structfield>info</structfield>,
  2992. <structfield>get</structfield> and
  2993. <structfield>put</structfield>. Then, define a
  2994. struct <structname>snd_kcontrol_new</structname> record, such as:
  2995. <example>
  2996. <title>Definition of a Control</title>
  2997. <programlisting>
  2998. <![CDATA[
  2999. static struct snd_kcontrol_new my_control __devinitdata = {
  3000. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3001. .name = "PCM Playback Switch",
  3002. .index = 0,
  3003. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  3004. .private_value = 0xffff,
  3005. .info = my_control_info,
  3006. .get = my_control_get,
  3007. .put = my_control_put
  3008. };
  3009. ]]>
  3010. </programlisting>
  3011. </example>
  3012. </para>
  3013. <para>
  3014. Most likely the control is created via
  3015. <function>snd_ctl_new1()</function>, and in such a case, you can
  3016. add the <parameter>__devinitdata</parameter> prefix to the
  3017. definition as above.
  3018. </para>
  3019. <para>
  3020. The <structfield>iface</structfield> field specifies the control
  3021. type, <constant>SNDRV_CTL_ELEM_IFACE_XXX</constant>, which
  3022. is usually <constant>MIXER</constant>.
  3023. Use <constant>CARD</constant> for global controls that are not
  3024. logically part of the mixer.
  3025. If the control is closely associated with some specific device on
  3026. the sound card, use <constant>HWDEP</constant>,
  3027. <constant>PCM</constant>, <constant>RAWMIDI</constant>,
  3028. <constant>TIMER</constant>, or <constant>SEQUENCER</constant>, and
  3029. specify the device number with the
  3030. <structfield>device</structfield> and
  3031. <structfield>subdevice</structfield> fields.
  3032. </para>
  3033. <para>
  3034. The <structfield>name</structfield> is the name identifier
  3035. string. Since ALSA 0.9.x, the control name is very important,
  3036. because its role is classified from its name. There are
  3037. pre-defined standard control names. The details are described in
  3038. the <link linkend="control-interface-control-names"><citetitle>
  3039. Control Names</citetitle></link> subsection.
  3040. </para>
  3041. <para>
  3042. The <structfield>index</structfield> field holds the index number
  3043. of this control. If there are several different controls with
  3044. the same name, they can be distinguished by the index
  3045. number. This is the case when
  3046. several codecs exist on the card. If the index is zero, you can
  3047. omit the definition above.
  3048. </para>
  3049. <para>
  3050. The <structfield>access</structfield> field contains the access
  3051. type of this control. Give the combination of bit masks,
  3052. <constant>SNDRV_CTL_ELEM_ACCESS_XXX</constant>, there.
  3053. The details will be explained in
  3054. the <link linkend="control-interface-access-flags"><citetitle>
  3055. Access Flags</citetitle></link> subsection.
  3056. </para>
  3057. <para>
  3058. The <structfield>private_value</structfield> field contains
  3059. an arbitrary long integer value for this record. When using
  3060. the generic <structfield>info</structfield>,
  3061. <structfield>get</structfield> and
  3062. <structfield>put</structfield> callbacks, you can pass a value
  3063. through this field. If several small numbers are necessary, you can
  3064. combine them in bitwise. Or, it's possible to give a pointer
  3065. (casted to unsigned long) of some record to this field, too.
  3066. </para>
  3067. <para>
  3068. The <structfield>tlv</structfield> field can be used to provide
  3069. metadata about the control; see the
  3070. <link linkend="control-interface-tlv">
  3071. <citetitle>Metadata</citetitle></link> subsection.
  3072. </para>
  3073. <para>
  3074. The other three are
  3075. <link linkend="control-interface-callbacks"><citetitle>
  3076. callback functions</citetitle></link>.
  3077. </para>
  3078. </section>
  3079. <section id="control-interface-control-names">
  3080. <title>Control Names</title>
  3081. <para>
  3082. There are some standards to define the control names. A
  3083. control is usually defined from the three parts as
  3084. <quote>SOURCE DIRECTION FUNCTION</quote>.
  3085. </para>
  3086. <para>
  3087. The first, <constant>SOURCE</constant>, specifies the source
  3088. of the control, and is a string such as <quote>Master</quote>,
  3089. <quote>PCM</quote>, <quote>CD</quote> and
  3090. <quote>Line</quote>. There are many pre-defined sources.
  3091. </para>
  3092. <para>
  3093. The second, <constant>DIRECTION</constant>, is one of the
  3094. following strings according to the direction of the control:
  3095. <quote>Playback</quote>, <quote>Capture</quote>, <quote>Bypass
  3096. Playback</quote> and <quote>Bypass Capture</quote>. Or, it can
  3097. be omitted, meaning both playback and capture directions.
  3098. </para>
  3099. <para>
  3100. The third, <constant>FUNCTION</constant>, is one of the
  3101. following strings according to the function of the control:
  3102. <quote>Switch</quote>, <quote>Volume</quote> and
  3103. <quote>Route</quote>.
  3104. </para>
  3105. <para>
  3106. The example of control names are, thus, <quote>Master Capture
  3107. Switch</quote> or <quote>PCM Playback Volume</quote>.
  3108. </para>
  3109. <para>
  3110. There are some exceptions:
  3111. </para>
  3112. <section id="control-interface-control-names-global">
  3113. <title>Global capture and playback</title>
  3114. <para>
  3115. <quote>Capture Source</quote>, <quote>Capture Switch</quote>
  3116. and <quote>Capture Volume</quote> are used for the global
  3117. capture (input) source, switch and volume. Similarly,
  3118. <quote>Playback Switch</quote> and <quote>Playback
  3119. Volume</quote> are used for the global output gain switch and
  3120. volume.
  3121. </para>
  3122. </section>
  3123. <section id="control-interface-control-names-tone">
  3124. <title>Tone-controls</title>
  3125. <para>
  3126. tone-control switch and volumes are specified like
  3127. <quote>Tone Control - XXX</quote>, e.g. <quote>Tone Control -
  3128. Switch</quote>, <quote>Tone Control - Bass</quote>,
  3129. <quote>Tone Control - Center</quote>.
  3130. </para>
  3131. </section>
  3132. <section id="control-interface-control-names-3d">
  3133. <title>3D controls</title>
  3134. <para>
  3135. 3D-control switches and volumes are specified like <quote>3D
  3136. Control - XXX</quote>, e.g. <quote>3D Control -
  3137. Switch</quote>, <quote>3D Control - Center</quote>, <quote>3D
  3138. Control - Space</quote>.
  3139. </para>
  3140. </section>
  3141. <section id="control-interface-control-names-mic">
  3142. <title>Mic boost</title>
  3143. <para>
  3144. Mic-boost switch is set as <quote>Mic Boost</quote> or
  3145. <quote>Mic Boost (6dB)</quote>.
  3146. </para>
  3147. <para>
  3148. More precise information can be found in
  3149. <filename>Documentation/sound/alsa/ControlNames.txt</filename>.
  3150. </para>
  3151. </section>
  3152. </section>
  3153. <section id="control-interface-access-flags">
  3154. <title>Access Flags</title>
  3155. <para>
  3156. The access flag is the bitmask which specifies the access type
  3157. of the given control. The default access type is
  3158. <constant>SNDRV_CTL_ELEM_ACCESS_READWRITE</constant>,
  3159. which means both read and write are allowed to this control.
  3160. When the access flag is omitted (i.e. = 0), it is
  3161. considered as <constant>READWRITE</constant> access as default.
  3162. </para>
  3163. <para>
  3164. When the control is read-only, pass
  3165. <constant>SNDRV_CTL_ELEM_ACCESS_READ</constant> instead.
  3166. In this case, you don't have to define
  3167. the <structfield>put</structfield> callback.
  3168. Similarly, when the control is write-only (although it's a rare
  3169. case), you can use the <constant>WRITE</constant> flag instead, and
  3170. you don't need the <structfield>get</structfield> callback.
  3171. </para>
  3172. <para>
  3173. If the control value changes frequently (e.g. the VU meter),
  3174. <constant>VOLATILE</constant> flag should be given. This means
  3175. that the control may be changed without
  3176. <link linkend="control-interface-change-notification"><citetitle>
  3177. notification</citetitle></link>. Applications should poll such
  3178. a control constantly.
  3179. </para>
  3180. <para>
  3181. When the control is inactive, set
  3182. the <constant>INACTIVE</constant> flag, too.
  3183. There are <constant>LOCK</constant> and
  3184. <constant>OWNER</constant> flags to change the write
  3185. permissions.
  3186. </para>
  3187. </section>
  3188. <section id="control-interface-callbacks">
  3189. <title>Callbacks</title>
  3190. <section id="control-interface-callbacks-info">
  3191. <title>info callback</title>
  3192. <para>
  3193. The <structfield>info</structfield> callback is used to get
  3194. detailed information on this control. This must store the
  3195. values of the given struct <structname>snd_ctl_elem_info</structname>
  3196. object. For example, for a boolean control with a single
  3197. element:
  3198. <example>
  3199. <title>Example of info callback</title>
  3200. <programlisting>
  3201. <![CDATA[
  3202. static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol,
  3203. struct snd_ctl_elem_info *uinfo)
  3204. {
  3205. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  3206. uinfo->count = 1;
  3207. uinfo->value.integer.min = 0;
  3208. uinfo->value.integer.max = 1;
  3209. return 0;
  3210. }
  3211. ]]>
  3212. </programlisting>
  3213. </example>
  3214. </para>
  3215. <para>
  3216. The <structfield>type</structfield> field specifies the type
  3217. of the control. There are <constant>BOOLEAN</constant>,
  3218. <constant>INTEGER</constant>, <constant>ENUMERATED</constant>,
  3219. <constant>BYTES</constant>, <constant>IEC958</constant> and
  3220. <constant>INTEGER64</constant>. The
  3221. <structfield>count</structfield> field specifies the
  3222. number of elements in this control. For example, a stereo
  3223. volume would have count = 2. The
  3224. <structfield>value</structfield> field is a union, and
  3225. the values stored are depending on the type. The boolean and
  3226. integer types are identical.
  3227. </para>
  3228. <para>
  3229. The enumerated type is a bit different from others. You'll
  3230. need to set the string for the currently given item index.
  3231. <informalexample>
  3232. <programlisting>
  3233. <![CDATA[
  3234. static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
  3235. struct snd_ctl_elem_info *uinfo)
  3236. {
  3237. static char *texts[4] = {
  3238. "First", "Second", "Third", "Fourth"
  3239. };
  3240. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3241. uinfo->count = 1;
  3242. uinfo->value.enumerated.items = 4;
  3243. if (uinfo->value.enumerated.item > 3)
  3244. uinfo->value.enumerated.item = 3;
  3245. strcpy(uinfo->value.enumerated.name,
  3246. texts[uinfo->value.enumerated.item]);
  3247. return 0;
  3248. }
  3249. ]]>
  3250. </programlisting>
  3251. </informalexample>
  3252. </para>
  3253. <para>
  3254. Some common info callbacks are available for your convenience:
  3255. <function>snd_ctl_boolean_mono_info()</function> and
  3256. <function>snd_ctl_boolean_stereo_info()</function>.
  3257. Obviously, the former is an info callback for a mono channel
  3258. boolean item, just like <function>snd_myctl_mono_info</function>
  3259. above, and the latter is for a stereo channel boolean item.
  3260. </para>
  3261. </section>
  3262. <section id="control-interface-callbacks-get">
  3263. <title>get callback</title>
  3264. <para>
  3265. This callback is used to read the current value of the
  3266. control and to return to user-space.
  3267. </para>
  3268. <para>
  3269. For example,
  3270. <example>
  3271. <title>Example of get callback</title>
  3272. <programlisting>
  3273. <![CDATA[
  3274. static int snd_myctl_get(struct snd_kcontrol *kcontrol,
  3275. struct snd_ctl_elem_value *ucontrol)
  3276. {
  3277. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  3278. ucontrol->value.integer.value[0] = get_some_value(chip);
  3279. return 0;
  3280. }
  3281. ]]>
  3282. </programlisting>
  3283. </example>
  3284. </para>
  3285. <para>
  3286. The <structfield>value</structfield> field depends on
  3287. the type of control as well as on the info callback. For example,
  3288. the sb driver uses this field to store the register offset,
  3289. the bit-shift and the bit-mask. The
  3290. <structfield>private_value</structfield> field is set as follows:
  3291. <informalexample>
  3292. <programlisting>
  3293. <![CDATA[
  3294. .private_value = reg | (shift << 16) | (mask << 24)
  3295. ]]>
  3296. </programlisting>
  3297. </informalexample>
  3298. and is retrieved in callbacks like
  3299. <informalexample>
  3300. <programlisting>
  3301. <![CDATA[
  3302. static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol,
  3303. struct snd_ctl_elem_value *ucontrol)
  3304. {
  3305. int reg = kcontrol->private_value & 0xff;
  3306. int shift = (kcontrol->private_value >> 16) & 0xff;
  3307. int mask = (kcontrol->private_value >> 24) & 0xff;
  3308. ....
  3309. }
  3310. ]]>
  3311. </programlisting>
  3312. </informalexample>
  3313. </para>
  3314. <para>
  3315. In the <structfield>get</structfield> callback,
  3316. you have to fill all the elements if the
  3317. control has more than one elements,
  3318. i.e. <structfield>count</structfield> &gt; 1.
  3319. In the example above, we filled only one element
  3320. (<structfield>value.integer.value[0]</structfield>) since it's
  3321. assumed as <structfield>count</structfield> = 1.
  3322. </para>
  3323. </section>
  3324. <section id="control-interface-callbacks-put">
  3325. <title>put callback</title>
  3326. <para>
  3327. This callback is used to write a value from user-space.
  3328. </para>
  3329. <para>
  3330. For example,
  3331. <example>
  3332. <title>Example of put callback</title>
  3333. <programlisting>
  3334. <![CDATA[
  3335. static int snd_myctl_put(struct snd_kcontrol *kcontrol,
  3336. struct snd_ctl_elem_value *ucontrol)
  3337. {
  3338. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  3339. int changed = 0;
  3340. if (chip->current_value !=
  3341. ucontrol->value.integer.value[0]) {
  3342. change_current_value(chip,
  3343. ucontrol->value.integer.value[0]);
  3344. changed = 1;
  3345. }
  3346. return changed;
  3347. }
  3348. ]]>
  3349. </programlisting>
  3350. </example>
  3351. As seen above, you have to return 1 if the value is
  3352. changed. If the value is not changed, return 0 instead.
  3353. If any fatal error happens, return a negative error code as
  3354. usual.
  3355. </para>
  3356. <para>
  3357. As in the <structfield>get</structfield> callback,
  3358. when the control has more than one elements,
  3359. all elements must be evaluated in this callback, too.
  3360. </para>
  3361. </section>
  3362. <section id="control-interface-callbacks-all">
  3363. <title>Callbacks are not atomic</title>
  3364. <para>
  3365. All these three callbacks are basically not atomic.
  3366. </para>
  3367. </section>
  3368. </section>
  3369. <section id="control-interface-constructor">
  3370. <title>Constructor</title>
  3371. <para>
  3372. When everything is ready, finally we can create a new
  3373. control. To create a control, there are two functions to be
  3374. called, <function>snd_ctl_new1()</function> and
  3375. <function>snd_ctl_add()</function>.
  3376. </para>
  3377. <para>
  3378. In the simplest way, you can do like this:
  3379. <informalexample>
  3380. <programlisting>
  3381. <![CDATA[
  3382. err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
  3383. if (err < 0)
  3384. return err;
  3385. ]]>
  3386. </programlisting>
  3387. </informalexample>
  3388. where <parameter>my_control</parameter> is the
  3389. struct <structname>snd_kcontrol_new</structname> object defined above, and chip
  3390. is the object pointer to be passed to
  3391. kcontrol-&gt;private_data
  3392. which can be referred to in callbacks.
  3393. </para>
  3394. <para>
  3395. <function>snd_ctl_new1()</function> allocates a new
  3396. <structname>snd_kcontrol</structname> instance (that's why the definition
  3397. of <parameter>my_control</parameter> can be with
  3398. the <parameter>__devinitdata</parameter>
  3399. prefix), and <function>snd_ctl_add</function> assigns the given
  3400. control component to the card.
  3401. </para>
  3402. </section>
  3403. <section id="control-interface-change-notification">
  3404. <title>Change Notification</title>
  3405. <para>
  3406. If you need to change and update a control in the interrupt
  3407. routine, you can call <function>snd_ctl_notify()</function>. For
  3408. example,
  3409. <informalexample>
  3410. <programlisting>
  3411. <![CDATA[
  3412. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);
  3413. ]]>
  3414. </programlisting>
  3415. </informalexample>
  3416. This function takes the card pointer, the event-mask, and the
  3417. control id pointer for the notification. The event-mask
  3418. specifies the types of notification, for example, in the above
  3419. example, the change of control values is notified.
  3420. The id pointer is the pointer of struct <structname>snd_ctl_elem_id</structname>
  3421. to be notified.
  3422. You can find some examples in <filename>es1938.c</filename> or
  3423. <filename>es1968.c</filename> for hardware volume interrupts.
  3424. </para>
  3425. </section>
  3426. <section id="control-interface-tlv">
  3427. <title>Metadata</title>
  3428. <para>
  3429. To provide information about the dB values of a mixer control, use
  3430. on of the <constant>DECLARE_TLV_xxx</constant> macros from
  3431. <filename>&lt;sound/tlv.h&gt;</filename> to define a variable
  3432. containing this information, set the<structfield>tlv.p
  3433. </structfield> field to point to this variable, and include the
  3434. <constant>SNDRV_CTL_ELEM_ACCESS_TLV_READ</constant> flag in the
  3435. <structfield>access</structfield> field; like this:
  3436. <informalexample>
  3437. <programlisting>
  3438. <![CDATA[
  3439. static DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0);
  3440. static struct snd_kcontrol_new my_control __devinitdata = {
  3441. ...
  3442. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3443. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3444. ...
  3445. .tlv.p = db_scale_my_control,
  3446. };
  3447. ]]>
  3448. </programlisting>
  3449. </informalexample>
  3450. </para>
  3451. <para>
  3452. The <function>DECLARE_TLV_DB_SCALE</function> macro defines
  3453. information about a mixer control where each step in the control's
  3454. value changes the dB value by a constant dB amount.
  3455. The first parameter is the name of the variable to be defined.
  3456. The second parameter is the minimum value, in units of 0.01 dB.
  3457. The third parameter is the step size, in units of 0.01 dB.
  3458. Set the fourth parameter to 1 if the minimum value actually mutes
  3459. the control.
  3460. </para>
  3461. <para>
  3462. The <function>DECLARE_TLV_DB_LINEAR</function> macro defines
  3463. information about a mixer control where the control's value affects
  3464. the output linearly.
  3465. The first parameter is the name of the variable to be defined.
  3466. The second parameter is the minimum value, in units of 0.01 dB.
  3467. The third parameter is the maximum value, in units of 0.01 dB.
  3468. If the minimum value mutes the control, set the second parameter to
  3469. <constant>TLV_DB_GAIN_MUTE</constant>.
  3470. </para>
  3471. </section>
  3472. </chapter>
  3473. <!-- ****************************************************** -->
  3474. <!-- API for AC97 Codec -->
  3475. <!-- ****************************************************** -->
  3476. <chapter id="api-ac97">
  3477. <title>API for AC97 Codec</title>
  3478. <section>
  3479. <title>General</title>
  3480. <para>
  3481. The ALSA AC97 codec layer is a well-defined one, and you don't
  3482. have to write much code to control it. Only low-level control
  3483. routines are necessary. The AC97 codec API is defined in
  3484. <filename>&lt;sound/ac97_codec.h&gt;</filename>.
  3485. </para>
  3486. </section>
  3487. <section id="api-ac97-example">
  3488. <title>Full Code Example</title>
  3489. <para>
  3490. <example>
  3491. <title>Example of AC97 Interface</title>
  3492. <programlisting>
  3493. <![CDATA[
  3494. struct mychip {
  3495. ....
  3496. struct snd_ac97 *ac97;
  3497. ....
  3498. };
  3499. static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
  3500. unsigned short reg)
  3501. {
  3502. struct mychip *chip = ac97->private_data;
  3503. ....
  3504. /* read a register value here from the codec */
  3505. return the_register_value;
  3506. }
  3507. static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
  3508. unsigned short reg, unsigned short val)
  3509. {
  3510. struct mychip *chip = ac97->private_data;
  3511. ....
  3512. /* write the given register value to the codec */
  3513. }
  3514. static int snd_mychip_ac97(struct mychip *chip)
  3515. {
  3516. struct snd_ac97_bus *bus;
  3517. struct snd_ac97_template ac97;
  3518. int err;
  3519. static struct snd_ac97_bus_ops ops = {
  3520. .write = snd_mychip_ac97_write,
  3521. .read = snd_mychip_ac97_read,
  3522. };
  3523. err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
  3524. if (err < 0)
  3525. return err;
  3526. memset(&ac97, 0, sizeof(ac97));
  3527. ac97.private_data = chip;
  3528. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  3529. }
  3530. ]]>
  3531. </programlisting>
  3532. </example>
  3533. </para>
  3534. </section>
  3535. <section id="api-ac97-constructor">
  3536. <title>Constructor</title>
  3537. <para>
  3538. To create an ac97 instance, first call <function>snd_ac97_bus</function>
  3539. with an <type>ac97_bus_ops_t</type> record with callback functions.
  3540. <informalexample>
  3541. <programlisting>
  3542. <![CDATA[
  3543. struct snd_ac97_bus *bus;
  3544. static struct snd_ac97_bus_ops ops = {
  3545. .write = snd_mychip_ac97_write,
  3546. .read = snd_mychip_ac97_read,
  3547. };
  3548. snd_ac97_bus(card, 0, &ops, NULL, &pbus);
  3549. ]]>
  3550. </programlisting>
  3551. </informalexample>
  3552. The bus record is shared among all belonging ac97 instances.
  3553. </para>
  3554. <para>
  3555. And then call <function>snd_ac97_mixer()</function> with an
  3556. struct <structname>snd_ac97_template</structname>
  3557. record together with the bus pointer created above.
  3558. <informalexample>
  3559. <programlisting>
  3560. <![CDATA[
  3561. struct snd_ac97_template ac97;
  3562. int err;
  3563. memset(&ac97, 0, sizeof(ac97));
  3564. ac97.private_data = chip;
  3565. snd_ac97_mixer(bus, &ac97, &chip->ac97);
  3566. ]]>
  3567. </programlisting>
  3568. </informalexample>
  3569. where chip-&gt;ac97 is a pointer to a newly created
  3570. <type>ac97_t</type> instance.
  3571. In this case, the chip pointer is set as the private data, so that
  3572. the read/write callback functions can refer to this chip instance.
  3573. This instance is not necessarily stored in the chip
  3574. record. If you need to change the register values from the
  3575. driver, or need the suspend/resume of ac97 codecs, keep this
  3576. pointer to pass to the corresponding functions.
  3577. </para>
  3578. </section>
  3579. <section id="api-ac97-callbacks">
  3580. <title>Callbacks</title>
  3581. <para>
  3582. The standard callbacks are <structfield>read</structfield> and
  3583. <structfield>write</structfield>. Obviously they
  3584. correspond to the functions for read and write accesses to the
  3585. hardware low-level codes.
  3586. </para>
  3587. <para>
  3588. The <structfield>read</structfield> callback returns the
  3589. register value specified in the argument.
  3590. <informalexample>
  3591. <programlisting>
  3592. <![CDATA[
  3593. static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
  3594. unsigned short reg)
  3595. {
  3596. struct mychip *chip = ac97->private_data;
  3597. ....
  3598. return the_register_value;
  3599. }
  3600. ]]>
  3601. </programlisting>
  3602. </informalexample>
  3603. Here, the chip can be cast from ac97-&gt;private_data.
  3604. </para>
  3605. <para>
  3606. Meanwhile, the <structfield>write</structfield> callback is
  3607. used to set the register value.
  3608. <informalexample>
  3609. <programlisting>
  3610. <![CDATA[
  3611. static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
  3612. unsigned short reg, unsigned short val)
  3613. ]]>
  3614. </programlisting>
  3615. </informalexample>
  3616. </para>
  3617. <para>
  3618. These callbacks are non-atomic like the control API callbacks.
  3619. </para>
  3620. <para>
  3621. There are also other callbacks:
  3622. <structfield>reset</structfield>,
  3623. <structfield>wait</structfield> and
  3624. <structfield>init</structfield>.
  3625. </para>
  3626. <para>
  3627. The <structfield>reset</structfield> callback is used to reset
  3628. the codec. If the chip requires a special kind of reset, you can
  3629. define this callback.
  3630. </para>
  3631. <para>
  3632. The <structfield>wait</structfield> callback is used to
  3633. add some waiting time in the standard initialization of the codec. If the
  3634. chip requires the extra waiting time, define this callback.
  3635. </para>
  3636. <para>
  3637. The <structfield>init</structfield> callback is used for
  3638. additional initialization of the codec.
  3639. </para>
  3640. </section>
  3641. <section id="api-ac97-updating-registers">
  3642. <title>Updating Registers in The Driver</title>
  3643. <para>
  3644. If you need to access to the codec from the driver, you can
  3645. call the following functions:
  3646. <function>snd_ac97_write()</function>,
  3647. <function>snd_ac97_read()</function>,
  3648. <function>snd_ac97_update()</function> and
  3649. <function>snd_ac97_update_bits()</function>.
  3650. </para>
  3651. <para>
  3652. Both <function>snd_ac97_write()</function> and
  3653. <function>snd_ac97_update()</function> functions are used to
  3654. set a value to the given register
  3655. (<constant>AC97_XXX</constant>). The difference between them is
  3656. that <function>snd_ac97_update()</function> doesn't write a
  3657. value if the given value has been already set, while
  3658. <function>snd_ac97_write()</function> always rewrites the
  3659. value.
  3660. <informalexample>
  3661. <programlisting>
  3662. <![CDATA[
  3663. snd_ac97_write(ac97, AC97_MASTER, 0x8080);
  3664. snd_ac97_update(ac97, AC97_MASTER, 0x8080);
  3665. ]]>
  3666. </programlisting>
  3667. </informalexample>
  3668. </para>
  3669. <para>
  3670. <function>snd_ac97_read()</function> is used to read the value
  3671. of the given register. For example,
  3672. <informalexample>
  3673. <programlisting>
  3674. <![CDATA[
  3675. value = snd_ac97_read(ac97, AC97_MASTER);
  3676. ]]>
  3677. </programlisting>
  3678. </informalexample>
  3679. </para>
  3680. <para>
  3681. <function>snd_ac97_update_bits()</function> is used to update
  3682. some bits in the given register.
  3683. <informalexample>
  3684. <programlisting>
  3685. <![CDATA[
  3686. snd_ac97_update_bits(ac97, reg, mask, value);
  3687. ]]>
  3688. </programlisting>
  3689. </informalexample>
  3690. </para>
  3691. <para>
  3692. Also, there is a function to change the sample rate (of a
  3693. given register such as
  3694. <constant>AC97_PCM_FRONT_DAC_RATE</constant>) when VRA or
  3695. DRA is supported by the codec:
  3696. <function>snd_ac97_set_rate()</function>.
  3697. <informalexample>
  3698. <programlisting>
  3699. <![CDATA[
  3700. snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);
  3701. ]]>
  3702. </programlisting>
  3703. </informalexample>
  3704. </para>
  3705. <para>
  3706. The following registers are available to set the rate:
  3707. <constant>AC97_PCM_MIC_ADC_RATE</constant>,
  3708. <constant>AC97_PCM_FRONT_DAC_RATE</constant>,
  3709. <constant>AC97_PCM_LR_ADC_RATE</constant>,
  3710. <constant>AC97_SPDIF</constant>. When
  3711. <constant>AC97_SPDIF</constant> is specified, the register is
  3712. not really changed but the corresponding IEC958 status bits will
  3713. be updated.
  3714. </para>
  3715. </section>
  3716. <section id="api-ac97-clock-adjustment">
  3717. <title>Clock Adjustment</title>
  3718. <para>
  3719. In some chips, the clock of the codec isn't 48000 but using a
  3720. PCI clock (to save a quartz!). In this case, change the field
  3721. bus-&gt;clock to the corresponding
  3722. value. For example, intel8x0
  3723. and es1968 drivers have their own function to read from the clock.
  3724. </para>
  3725. </section>
  3726. <section id="api-ac97-proc-files">
  3727. <title>Proc Files</title>
  3728. <para>
  3729. The ALSA AC97 interface will create a proc file such as
  3730. <filename>/proc/asound/card0/codec97#0/ac97#0-0</filename> and
  3731. <filename>ac97#0-0+regs</filename>. You can refer to these files to
  3732. see the current status and registers of the codec.
  3733. </para>
  3734. </section>
  3735. <section id="api-ac97-multiple-codecs">
  3736. <title>Multiple Codecs</title>
  3737. <para>
  3738. When there are several codecs on the same card, you need to
  3739. call <function>snd_ac97_mixer()</function> multiple times with
  3740. ac97.num=1 or greater. The <structfield>num</structfield> field
  3741. specifies the codec number.
  3742. </para>
  3743. <para>
  3744. If you set up multiple codecs, you either need to write
  3745. different callbacks for each codec or check
  3746. ac97-&gt;num in the callback routines.
  3747. </para>
  3748. </section>
  3749. </chapter>
  3750. <!-- ****************************************************** -->
  3751. <!-- MIDI (MPU401-UART) Interface -->
  3752. <!-- ****************************************************** -->
  3753. <chapter id="midi-interface">
  3754. <title>MIDI (MPU401-UART) Interface</title>
  3755. <section id="midi-interface-general">
  3756. <title>General</title>
  3757. <para>
  3758. Many soundcards have built-in MIDI (MPU401-UART)
  3759. interfaces. When the soundcard supports the standard MPU401-UART
  3760. interface, most likely you can use the ALSA MPU401-UART API. The
  3761. MPU401-UART API is defined in
  3762. <filename>&lt;sound/mpu401.h&gt;</filename>.
  3763. </para>
  3764. <para>
  3765. Some soundchips have a similar but slightly different
  3766. implementation of mpu401 stuff. For example, emu10k1 has its own
  3767. mpu401 routines.
  3768. </para>
  3769. </section>
  3770. <section id="midi-interface-constructor">
  3771. <title>Constructor</title>
  3772. <para>
  3773. To create a rawmidi object, call
  3774. <function>snd_mpu401_uart_new()</function>.
  3775. <informalexample>
  3776. <programlisting>
  3777. <![CDATA[
  3778. struct snd_rawmidi *rmidi;
  3779. snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags,
  3780. irq, irq_flags, &rmidi);
  3781. ]]>
  3782. </programlisting>
  3783. </informalexample>
  3784. </para>
  3785. <para>
  3786. The first argument is the card pointer, and the second is the
  3787. index of this component. You can create up to 8 rawmidi
  3788. devices.
  3789. </para>
  3790. <para>
  3791. The third argument is the type of the hardware,
  3792. <constant>MPU401_HW_XXX</constant>. If it's not a special one,
  3793. you can use <constant>MPU401_HW_MPU401</constant>.
  3794. </para>
  3795. <para>
  3796. The 4th argument is the I/O port address. Many
  3797. backward-compatible MPU401 have an I/O port such as 0x330. Or, it
  3798. might be a part of its own PCI I/O region. It depends on the
  3799. chip design.
  3800. </para>
  3801. <para>
  3802. The 5th argument is a bitflag for additional information.
  3803. When the I/O port address above is part of the PCI I/O
  3804. region, the MPU401 I/O port might have been already allocated
  3805. (reserved) by the driver itself. In such a case, pass a bit flag
  3806. <constant>MPU401_INFO_INTEGRATED</constant>,
  3807. and the mpu401-uart layer will allocate the I/O ports by itself.
  3808. </para>
  3809. <para>
  3810. When the controller supports only the input or output MIDI stream,
  3811. pass the <constant>MPU401_INFO_INPUT</constant> or
  3812. <constant>MPU401_INFO_OUTPUT</constant> bitflag, respectively.
  3813. Then the rawmidi instance is created as a single stream.
  3814. </para>
  3815. <para>
  3816. <constant>MPU401_INFO_MMIO</constant> bitflag is used to change
  3817. the access method to MMIO (via readb and writeb) instead of
  3818. iob and outb. In this case, you have to pass the iomapped address
  3819. to <function>snd_mpu401_uart_new()</function>.
  3820. </para>
  3821. <para>
  3822. When <constant>MPU401_INFO_TX_IRQ</constant> is set, the output
  3823. stream isn't checked in the default interrupt handler. The driver
  3824. needs to call <function>snd_mpu401_uart_interrupt_tx()</function>
  3825. by itself to start processing the output stream in the irq handler.
  3826. </para>
  3827. <para>
  3828. Usually, the port address corresponds to the command port and
  3829. port + 1 corresponds to the data port. If not, you may change
  3830. the <structfield>cport</structfield> field of
  3831. struct <structname>snd_mpu401</structname> manually
  3832. afterward. However, <structname>snd_mpu401</structname> pointer is not
  3833. returned explicitly by
  3834. <function>snd_mpu401_uart_new()</function>. You need to cast
  3835. rmidi-&gt;private_data to
  3836. <structname>snd_mpu401</structname> explicitly,
  3837. <informalexample>
  3838. <programlisting>
  3839. <![CDATA[
  3840. struct snd_mpu401 *mpu;
  3841. mpu = rmidi->private_data;
  3842. ]]>
  3843. </programlisting>
  3844. </informalexample>
  3845. and reset the cport as you like:
  3846. <informalexample>
  3847. <programlisting>
  3848. <![CDATA[
  3849. mpu->cport = my_own_control_port;
  3850. ]]>
  3851. </programlisting>
  3852. </informalexample>
  3853. </para>
  3854. <para>
  3855. The 6th argument specifies the irq number for UART. If the irq
  3856. is already allocated, pass 0 to the 7th argument
  3857. (<parameter>irq_flags</parameter>). Otherwise, pass the flags
  3858. for irq allocation
  3859. (<constant>SA_XXX</constant> bits) to it, and the irq will be
  3860. reserved by the mpu401-uart layer. If the card doesn't generate
  3861. UART interrupts, pass -1 as the irq number. Then a timer
  3862. interrupt will be invoked for polling.
  3863. </para>
  3864. </section>
  3865. <section id="midi-interface-interrupt-handler">
  3866. <title>Interrupt Handler</title>
  3867. <para>
  3868. When the interrupt is allocated in
  3869. <function>snd_mpu401_uart_new()</function>, the private
  3870. interrupt handler is used, hence you don't have anything else to do
  3871. than creating the mpu401 stuff. Otherwise, you have to call
  3872. <function>snd_mpu401_uart_interrupt()</function> explicitly when
  3873. a UART interrupt is invoked and checked in your own interrupt
  3874. handler.
  3875. </para>
  3876. <para>
  3877. In this case, you need to pass the private_data of the
  3878. returned rawmidi object from
  3879. <function>snd_mpu401_uart_new()</function> as the second
  3880. argument of <function>snd_mpu401_uart_interrupt()</function>.
  3881. <informalexample>
  3882. <programlisting>
  3883. <![CDATA[
  3884. snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);
  3885. ]]>
  3886. </programlisting>
  3887. </informalexample>
  3888. </para>
  3889. </section>
  3890. </chapter>
  3891. <!-- ****************************************************** -->
  3892. <!-- RawMIDI Interface -->
  3893. <!-- ****************************************************** -->
  3894. <chapter id="rawmidi-interface">
  3895. <title>RawMIDI Interface</title>
  3896. <section id="rawmidi-interface-overview">
  3897. <title>Overview</title>
  3898. <para>
  3899. The raw MIDI interface is used for hardware MIDI ports that can
  3900. be accessed as a byte stream. It is not used for synthesizer
  3901. chips that do not directly understand MIDI.
  3902. </para>
  3903. <para>
  3904. ALSA handles file and buffer management. All you have to do is
  3905. to write some code to move data between the buffer and the
  3906. hardware.
  3907. </para>
  3908. <para>
  3909. The rawmidi API is defined in
  3910. <filename>&lt;sound/rawmidi.h&gt;</filename>.
  3911. </para>
  3912. </section>
  3913. <section id="rawmidi-interface-constructor">
  3914. <title>Constructor</title>
  3915. <para>
  3916. To create a rawmidi device, call the
  3917. <function>snd_rawmidi_new</function> function:
  3918. <informalexample>
  3919. <programlisting>
  3920. <![CDATA[
  3921. struct snd_rawmidi *rmidi;
  3922. err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi);
  3923. if (err < 0)
  3924. return err;
  3925. rmidi->private_data = chip;
  3926. strcpy(rmidi->name, "My MIDI");
  3927. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  3928. SNDRV_RAWMIDI_INFO_INPUT |
  3929. SNDRV_RAWMIDI_INFO_DUPLEX;
  3930. ]]>
  3931. </programlisting>
  3932. </informalexample>
  3933. </para>
  3934. <para>
  3935. The first argument is the card pointer, the second argument is
  3936. the ID string.
  3937. </para>
  3938. <para>
  3939. The third argument is the index of this component. You can
  3940. create up to 8 rawmidi devices.
  3941. </para>
  3942. <para>
  3943. The fourth and fifth arguments are the number of output and
  3944. input substreams, respectively, of this device (a substream is
  3945. the equivalent of a MIDI port).
  3946. </para>
  3947. <para>
  3948. Set the <structfield>info_flags</structfield> field to specify
  3949. the capabilities of the device.
  3950. Set <constant>SNDRV_RAWMIDI_INFO_OUTPUT</constant> if there is
  3951. at least one output port,
  3952. <constant>SNDRV_RAWMIDI_INFO_INPUT</constant> if there is at
  3953. least one input port,
  3954. and <constant>SNDRV_RAWMIDI_INFO_DUPLEX</constant> if the device
  3955. can handle output and input at the same time.
  3956. </para>
  3957. <para>
  3958. After the rawmidi device is created, you need to set the
  3959. operators (callbacks) for each substream. There are helper
  3960. functions to set the operators for all the substreams of a device:
  3961. <informalexample>
  3962. <programlisting>
  3963. <![CDATA[
  3964. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops);
  3965. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);
  3966. ]]>
  3967. </programlisting>
  3968. </informalexample>
  3969. </para>
  3970. <para>
  3971. The operators are usually defined like this:
  3972. <informalexample>
  3973. <programlisting>
  3974. <![CDATA[
  3975. static struct snd_rawmidi_ops snd_mymidi_output_ops = {
  3976. .open = snd_mymidi_output_open,
  3977. .close = snd_mymidi_output_close,
  3978. .trigger = snd_mymidi_output_trigger,
  3979. };
  3980. ]]>
  3981. </programlisting>
  3982. </informalexample>
  3983. These callbacks are explained in the <link
  3984. linkend="rawmidi-interface-callbacks"><citetitle>Callbacks</citetitle></link>
  3985. section.
  3986. </para>
  3987. <para>
  3988. If there are more than one substream, you should give a
  3989. unique name to each of them:
  3990. <informalexample>
  3991. <programlisting>
  3992. <![CDATA[
  3993. struct snd_rawmidi_substream *substream;
  3994. list_for_each_entry(substream,
  3995. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  3996. list {
  3997. sprintf(substream->name, "My MIDI Port %d", substream->number + 1);
  3998. }
  3999. /* same for SNDRV_RAWMIDI_STREAM_INPUT */
  4000. ]]>
  4001. </programlisting>
  4002. </informalexample>
  4003. </para>
  4004. </section>
  4005. <section id="rawmidi-interface-callbacks">
  4006. <title>Callbacks</title>
  4007. <para>
  4008. In all the callbacks, the private data that you've set for the
  4009. rawmidi device can be accessed as
  4010. substream-&gt;rmidi-&gt;private_data.
  4011. <!-- <code> isn't available before DocBook 4.3 -->
  4012. </para>
  4013. <para>
  4014. If there is more than one port, your callbacks can determine the
  4015. port index from the struct snd_rawmidi_substream data passed to each
  4016. callback:
  4017. <informalexample>
  4018. <programlisting>
  4019. <![CDATA[
  4020. struct snd_rawmidi_substream *substream;
  4021. int index = substream->number;
  4022. ]]>
  4023. </programlisting>
  4024. </informalexample>
  4025. </para>
  4026. <section id="rawmidi-interface-op-open">
  4027. <title><function>open</function> callback</title>
  4028. <informalexample>
  4029. <programlisting>
  4030. <![CDATA[
  4031. static int snd_xxx_open(struct snd_rawmidi_substream *substream);
  4032. ]]>
  4033. </programlisting>
  4034. </informalexample>
  4035. <para>
  4036. This is called when a substream is opened.
  4037. You can initialize the hardware here, but you shouldn't
  4038. start transmitting/receiving data yet.
  4039. </para>
  4040. </section>
  4041. <section id="rawmidi-interface-op-close">
  4042. <title><function>close</function> callback</title>
  4043. <informalexample>
  4044. <programlisting>
  4045. <![CDATA[
  4046. static int snd_xxx_close(struct snd_rawmidi_substream *substream);
  4047. ]]>
  4048. </programlisting>
  4049. </informalexample>
  4050. <para>
  4051. Guess what.
  4052. </para>
  4053. <para>
  4054. The <function>open</function> and <function>close</function>
  4055. callbacks of a rawmidi device are serialized with a mutex,
  4056. and can sleep.
  4057. </para>
  4058. </section>
  4059. <section id="rawmidi-interface-op-trigger-out">
  4060. <title><function>trigger</function> callback for output
  4061. substreams</title>
  4062. <informalexample>
  4063. <programlisting>
  4064. <![CDATA[
  4065. static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);
  4066. ]]>
  4067. </programlisting>
  4068. </informalexample>
  4069. <para>
  4070. This is called with a nonzero <parameter>up</parameter>
  4071. parameter when there is some data in the substream buffer that
  4072. must be transmitted.
  4073. </para>
  4074. <para>
  4075. To read data from the buffer, call
  4076. <function>snd_rawmidi_transmit_peek</function>. It will
  4077. return the number of bytes that have been read; this will be
  4078. less than the number of bytes requested when there are no more
  4079. data in the buffer.
  4080. After the data have been transmitted successfully, call
  4081. <function>snd_rawmidi_transmit_ack</function> to remove the
  4082. data from the substream buffer:
  4083. <informalexample>
  4084. <programlisting>
  4085. <![CDATA[
  4086. unsigned char data;
  4087. while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
  4088. if (snd_mychip_try_to_transmit(data))
  4089. snd_rawmidi_transmit_ack(substream, 1);
  4090. else
  4091. break; /* hardware FIFO full */
  4092. }
  4093. ]]>
  4094. </programlisting>
  4095. </informalexample>
  4096. </para>
  4097. <para>
  4098. If you know beforehand that the hardware will accept data, you
  4099. can use the <function>snd_rawmidi_transmit</function> function
  4100. which reads some data and removes them from the buffer at once:
  4101. <informalexample>
  4102. <programlisting>
  4103. <![CDATA[
  4104. while (snd_mychip_transmit_possible()) {
  4105. unsigned char data;
  4106. if (snd_rawmidi_transmit(substream, &data, 1) != 1)
  4107. break; /* no more data */
  4108. snd_mychip_transmit(data);
  4109. }
  4110. ]]>
  4111. </programlisting>
  4112. </informalexample>
  4113. </para>
  4114. <para>
  4115. If you know beforehand how many bytes you can accept, you can
  4116. use a buffer size greater than one with the
  4117. <function>snd_rawmidi_transmit*</function> functions.
  4118. </para>
  4119. <para>
  4120. The <function>trigger</function> callback must not sleep. If
  4121. the hardware FIFO is full before the substream buffer has been
  4122. emptied, you have to continue transmitting data later, either
  4123. in an interrupt handler, or with a timer if the hardware
  4124. doesn't have a MIDI transmit interrupt.
  4125. </para>
  4126. <para>
  4127. The <function>trigger</function> callback is called with a
  4128. zero <parameter>up</parameter> parameter when the transmission
  4129. of data should be aborted.
  4130. </para>
  4131. </section>
  4132. <section id="rawmidi-interface-op-trigger-in">
  4133. <title><function>trigger</function> callback for input
  4134. substreams</title>
  4135. <informalexample>
  4136. <programlisting>
  4137. <![CDATA[
  4138. static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);
  4139. ]]>
  4140. </programlisting>
  4141. </informalexample>
  4142. <para>
  4143. This is called with a nonzero <parameter>up</parameter>
  4144. parameter to enable receiving data, or with a zero
  4145. <parameter>up</parameter> parameter do disable receiving data.
  4146. </para>
  4147. <para>
  4148. The <function>trigger</function> callback must not sleep; the
  4149. actual reading of data from the device is usually done in an
  4150. interrupt handler.
  4151. </para>
  4152. <para>
  4153. When data reception is enabled, your interrupt handler should
  4154. call <function>snd_rawmidi_receive</function> for all received
  4155. data:
  4156. <informalexample>
  4157. <programlisting>
  4158. <![CDATA[
  4159. void snd_mychip_midi_interrupt(...)
  4160. {
  4161. while (mychip_midi_available()) {
  4162. unsigned char data;
  4163. data = mychip_midi_read();
  4164. snd_rawmidi_receive(substream, &data, 1);
  4165. }
  4166. }
  4167. ]]>
  4168. </programlisting>
  4169. </informalexample>
  4170. </para>
  4171. </section>
  4172. <section id="rawmidi-interface-op-drain">
  4173. <title><function>drain</function> callback</title>
  4174. <informalexample>
  4175. <programlisting>
  4176. <![CDATA[
  4177. static void snd_xxx_drain(struct snd_rawmidi_substream *substream);
  4178. ]]>
  4179. </programlisting>
  4180. </informalexample>
  4181. <para>
  4182. This is only used with output substreams. This function should wait
  4183. until all data read from the substream buffer have been transmitted.
  4184. This ensures that the device can be closed and the driver unloaded
  4185. without losing data.
  4186. </para>
  4187. <para>
  4188. This callback is optional. If you do not set
  4189. <structfield>drain</structfield> in the struct snd_rawmidi_ops
  4190. structure, ALSA will simply wait for 50&nbsp;milliseconds
  4191. instead.
  4192. </para>
  4193. </section>
  4194. </section>
  4195. </chapter>
  4196. <!-- ****************************************************** -->
  4197. <!-- Miscellaneous Devices -->
  4198. <!-- ****************************************************** -->
  4199. <chapter id="misc-devices">
  4200. <title>Miscellaneous Devices</title>
  4201. <section id="misc-devices-opl3">
  4202. <title>FM OPL3</title>
  4203. <para>
  4204. The FM OPL3 is still used in many chips (mainly for backward
  4205. compatibility). ALSA has a nice OPL3 FM control layer, too. The
  4206. OPL3 API is defined in
  4207. <filename>&lt;sound/opl3.h&gt;</filename>.
  4208. </para>
  4209. <para>
  4210. FM registers can be directly accessed through the direct-FM API,
  4211. defined in <filename>&lt;sound/asound_fm.h&gt;</filename>. In
  4212. ALSA native mode, FM registers are accessed through
  4213. the Hardware-Dependant Device direct-FM extension API, whereas in
  4214. OSS compatible mode, FM registers can be accessed with the OSS
  4215. direct-FM compatible API in <filename>/dev/dmfmX</filename> device.
  4216. </para>
  4217. <para>
  4218. To create the OPL3 component, you have two functions to
  4219. call. The first one is a constructor for the <type>opl3_t</type>
  4220. instance.
  4221. <informalexample>
  4222. <programlisting>
  4223. <![CDATA[
  4224. struct snd_opl3 *opl3;
  4225. snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX,
  4226. integrated, &opl3);
  4227. ]]>
  4228. </programlisting>
  4229. </informalexample>
  4230. </para>
  4231. <para>
  4232. The first argument is the card pointer, the second one is the
  4233. left port address, and the third is the right port address. In
  4234. most cases, the right port is placed at the left port + 2.
  4235. </para>
  4236. <para>
  4237. The fourth argument is the hardware type.
  4238. </para>
  4239. <para>
  4240. When the left and right ports have been already allocated by
  4241. the card driver, pass non-zero to the fifth argument
  4242. (<parameter>integrated</parameter>). Otherwise, the opl3 module will
  4243. allocate the specified ports by itself.
  4244. </para>
  4245. <para>
  4246. When the accessing the hardware requires special method
  4247. instead of the standard I/O access, you can create opl3 instance
  4248. separately with <function>snd_opl3_new()</function>.
  4249. <informalexample>
  4250. <programlisting>
  4251. <![CDATA[
  4252. struct snd_opl3 *opl3;
  4253. snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);
  4254. ]]>
  4255. </programlisting>
  4256. </informalexample>
  4257. </para>
  4258. <para>
  4259. Then set <structfield>command</structfield>,
  4260. <structfield>private_data</structfield> and
  4261. <structfield>private_free</structfield> for the private
  4262. access function, the private data and the destructor.
  4263. The l_port and r_port are not necessarily set. Only the
  4264. command must be set properly. You can retrieve the data
  4265. from the opl3-&gt;private_data field.
  4266. </para>
  4267. <para>
  4268. After creating the opl3 instance via <function>snd_opl3_new()</function>,
  4269. call <function>snd_opl3_init()</function> to initialize the chip to the
  4270. proper state. Note that <function>snd_opl3_create()</function> always
  4271. calls it internally.
  4272. </para>
  4273. <para>
  4274. If the opl3 instance is created successfully, then create a
  4275. hwdep device for this opl3.
  4276. <informalexample>
  4277. <programlisting>
  4278. <![CDATA[
  4279. struct snd_hwdep *opl3hwdep;
  4280. snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);
  4281. ]]>
  4282. </programlisting>
  4283. </informalexample>
  4284. </para>
  4285. <para>
  4286. The first argument is the <type>opl3_t</type> instance you
  4287. created, and the second is the index number, usually 0.
  4288. </para>
  4289. <para>
  4290. The third argument is the index-offset for the sequencer
  4291. client assigned to the OPL3 port. When there is an MPU401-UART,
  4292. give 1 for here (UART always takes 0).
  4293. </para>
  4294. </section>
  4295. <section id="misc-devices-hardware-dependent">
  4296. <title>Hardware-Dependent Devices</title>
  4297. <para>
  4298. Some chips need user-space access for special
  4299. controls or for loading the micro code. In such a case, you can
  4300. create a hwdep (hardware-dependent) device. The hwdep API is
  4301. defined in <filename>&lt;sound/hwdep.h&gt;</filename>. You can
  4302. find examples in opl3 driver or
  4303. <filename>isa/sb/sb16_csp.c</filename>.
  4304. </para>
  4305. <para>
  4306. The creation of the <type>hwdep</type> instance is done via
  4307. <function>snd_hwdep_new()</function>.
  4308. <informalexample>
  4309. <programlisting>
  4310. <![CDATA[
  4311. struct snd_hwdep *hw;
  4312. snd_hwdep_new(card, "My HWDEP", 0, &hw);
  4313. ]]>
  4314. </programlisting>
  4315. </informalexample>
  4316. where the third argument is the index number.
  4317. </para>
  4318. <para>
  4319. You can then pass any pointer value to the
  4320. <parameter>private_data</parameter>.
  4321. If you assign a private data, you should define the
  4322. destructor, too. The destructor function is set in
  4323. the <structfield>private_free</structfield> field.
  4324. <informalexample>
  4325. <programlisting>
  4326. <![CDATA[
  4327. struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL);
  4328. hw->private_data = p;
  4329. hw->private_free = mydata_free;
  4330. ]]>
  4331. </programlisting>
  4332. </informalexample>
  4333. and the implementation of the destructor would be:
  4334. <informalexample>
  4335. <programlisting>
  4336. <![CDATA[
  4337. static void mydata_free(struct snd_hwdep *hw)
  4338. {
  4339. struct mydata *p = hw->private_data;
  4340. kfree(p);
  4341. }
  4342. ]]>
  4343. </programlisting>
  4344. </informalexample>
  4345. </para>
  4346. <para>
  4347. The arbitrary file operations can be defined for this
  4348. instance. The file operators are defined in
  4349. the <parameter>ops</parameter> table. For example, assume that
  4350. this chip needs an ioctl.
  4351. <informalexample>
  4352. <programlisting>
  4353. <![CDATA[
  4354. hw->ops.open = mydata_open;
  4355. hw->ops.ioctl = mydata_ioctl;
  4356. hw->ops.release = mydata_release;
  4357. ]]>
  4358. </programlisting>
  4359. </informalexample>
  4360. And implement the callback functions as you like.
  4361. </para>
  4362. </section>
  4363. <section id="misc-devices-IEC958">
  4364. <title>IEC958 (S/PDIF)</title>
  4365. <para>
  4366. Usually the controls for IEC958 devices are implemented via
  4367. the control interface. There is a macro to compose a name string for
  4368. IEC958 controls, <function>SNDRV_CTL_NAME_IEC958()</function>
  4369. defined in <filename>&lt;include/asound.h&gt;</filename>.
  4370. </para>
  4371. <para>
  4372. There are some standard controls for IEC958 status bits. These
  4373. controls use the type <type>SNDRV_CTL_ELEM_TYPE_IEC958</type>,
  4374. and the size of element is fixed as 4 bytes array
  4375. (value.iec958.status[x]). For the <structfield>info</structfield>
  4376. callback, you don't specify
  4377. the value field for this type (the count field must be set,
  4378. though).
  4379. </para>
  4380. <para>
  4381. <quote>IEC958 Playback Con Mask</quote> is used to return the
  4382. bit-mask for the IEC958 status bits of consumer mode. Similarly,
  4383. <quote>IEC958 Playback Pro Mask</quote> returns the bitmask for
  4384. professional mode. They are read-only controls, and are defined
  4385. as MIXER controls (iface =
  4386. <constant>SNDRV_CTL_ELEM_IFACE_MIXER</constant>).
  4387. </para>
  4388. <para>
  4389. Meanwhile, <quote>IEC958 Playback Default</quote> control is
  4390. defined for getting and setting the current default IEC958
  4391. bits. Note that this one is usually defined as a PCM control
  4392. (iface = <constant>SNDRV_CTL_ELEM_IFACE_PCM</constant>),
  4393. although in some places it's defined as a MIXER control.
  4394. </para>
  4395. <para>
  4396. In addition, you can define the control switches to
  4397. enable/disable or to set the raw bit mode. The implementation
  4398. will depend on the chip, but the control should be named as
  4399. <quote>IEC958 xxx</quote>, preferably using
  4400. the <function>SNDRV_CTL_NAME_IEC958()</function> macro.
  4401. </para>
  4402. <para>
  4403. You can find several cases, for example,
  4404. <filename>pci/emu10k1</filename>,
  4405. <filename>pci/ice1712</filename>, or
  4406. <filename>pci/cmipci.c</filename>.
  4407. </para>
  4408. </section>
  4409. </chapter>
  4410. <!-- ****************************************************** -->
  4411. <!-- Buffer and Memory Management -->
  4412. <!-- ****************************************************** -->
  4413. <chapter id="buffer-and-memory">
  4414. <title>Buffer and Memory Management</title>
  4415. <section id="buffer-and-memory-buffer-types">
  4416. <title>Buffer Types</title>
  4417. <para>
  4418. ALSA provides several different buffer allocation functions
  4419. depending on the bus and the architecture. All these have a
  4420. consistent API. The allocation of physically-contiguous pages is
  4421. done via
  4422. <function>snd_malloc_xxx_pages()</function> function, where xxx
  4423. is the bus type.
  4424. </para>
  4425. <para>
  4426. The allocation of pages with fallback is
  4427. <function>snd_malloc_xxx_pages_fallback()</function>. This
  4428. function tries to allocate the specified pages but if the pages
  4429. are not available, it tries to reduce the page sizes until
  4430. enough space is found.
  4431. </para>
  4432. <para>
  4433. The release the pages, call
  4434. <function>snd_free_xxx_pages()</function> function.
  4435. </para>
  4436. <para>
  4437. Usually, ALSA drivers try to allocate and reserve
  4438. a large contiguous physical space
  4439. at the time the module is loaded for the later use.
  4440. This is called <quote>pre-allocation</quote>.
  4441. As already written, you can call the following function at
  4442. pcm instance construction time (in the case of PCI bus).
  4443. <informalexample>
  4444. <programlisting>
  4445. <![CDATA[
  4446. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  4447. snd_dma_pci_data(pci), size, max);
  4448. ]]>
  4449. </programlisting>
  4450. </informalexample>
  4451. where <parameter>size</parameter> is the byte size to be
  4452. pre-allocated and the <parameter>max</parameter> is the maximum
  4453. size to be changed via the <filename>prealloc</filename> proc file.
  4454. The allocator will try to get an area as large as possible
  4455. within the given size.
  4456. </para>
  4457. <para>
  4458. The second argument (type) and the third argument (device pointer)
  4459. are dependent on the bus.
  4460. In the case of the ISA bus, pass <function>snd_dma_isa_data()</function>
  4461. as the third argument with <constant>SNDRV_DMA_TYPE_DEV</constant> type.
  4462. For the continuous buffer unrelated to the bus can be pre-allocated
  4463. with <constant>SNDRV_DMA_TYPE_CONTINUOUS</constant> type and the
  4464. <function>snd_dma_continuous_data(GFP_KERNEL)</function> device pointer,
  4465. where <constant>GFP_KERNEL</constant> is the kernel allocation flag to
  4466. use.
  4467. For the PCI scatter-gather buffers, use
  4468. <constant>SNDRV_DMA_TYPE_DEV_SG</constant> with
  4469. <function>snd_dma_pci_data(pci)</function>
  4470. (see the
  4471. <link linkend="buffer-and-memory-non-contiguous"><citetitle>Non-Contiguous Buffers
  4472. </citetitle></link> section).
  4473. </para>
  4474. <para>
  4475. Once the buffer is pre-allocated, you can use the
  4476. allocator in the <structfield>hw_params</structfield> callback:
  4477. <informalexample>
  4478. <programlisting>
  4479. <![CDATA[
  4480. snd_pcm_lib_malloc_pages(substream, size);
  4481. ]]>
  4482. </programlisting>
  4483. </informalexample>
  4484. Note that you have to pre-allocate to use this function.
  4485. </para>
  4486. </section>
  4487. <section id="buffer-and-memory-external-hardware">
  4488. <title>External Hardware Buffers</title>
  4489. <para>
  4490. Some chips have their own hardware buffers and the DMA
  4491. transfer from the host memory is not available. In such a case,
  4492. you need to either 1) copy/set the audio data directly to the
  4493. external hardware buffer, or 2) make an intermediate buffer and
  4494. copy/set the data from it to the external hardware buffer in
  4495. interrupts (or in tasklets, preferably).
  4496. </para>
  4497. <para>
  4498. The first case works fine if the external hardware buffer is large
  4499. enough. This method doesn't need any extra buffers and thus is
  4500. more effective. You need to define the
  4501. <structfield>copy</structfield> and
  4502. <structfield>silence</structfield> callbacks for
  4503. the data transfer. However, there is a drawback: it cannot
  4504. be mmapped. The examples are GUS's GF1 PCM or emu8000's
  4505. wavetable PCM.
  4506. </para>
  4507. <para>
  4508. The second case allows for mmap on the buffer, although you have
  4509. to handle an interrupt or a tasklet to transfer the data
  4510. from the intermediate buffer to the hardware buffer. You can find an
  4511. example in the vxpocket driver.
  4512. </para>
  4513. <para>
  4514. Another case is when the chip uses a PCI memory-map
  4515. region for the buffer instead of the host memory. In this case,
  4516. mmap is available only on certain architectures like the Intel one.
  4517. In non-mmap mode, the data cannot be transferred as in the normal
  4518. way. Thus you need to define the <structfield>copy</structfield> and
  4519. <structfield>silence</structfield> callbacks as well,
  4520. as in the cases above. The examples are found in
  4521. <filename>rme32.c</filename> and <filename>rme96.c</filename>.
  4522. </para>
  4523. <para>
  4524. The implementation of the <structfield>copy</structfield> and
  4525. <structfield>silence</structfield> callbacks depends upon
  4526. whether the hardware supports interleaved or non-interleaved
  4527. samples. The <structfield>copy</structfield> callback is
  4528. defined like below, a bit
  4529. differently depending whether the direction is playback or
  4530. capture:
  4531. <informalexample>
  4532. <programlisting>
  4533. <![CDATA[
  4534. static int playback_copy(struct snd_pcm_substream *substream, int channel,
  4535. snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count);
  4536. static int capture_copy(struct snd_pcm_substream *substream, int channel,
  4537. snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count);
  4538. ]]>
  4539. </programlisting>
  4540. </informalexample>
  4541. </para>
  4542. <para>
  4543. In the case of interleaved samples, the second argument
  4544. (<parameter>channel</parameter>) is not used. The third argument
  4545. (<parameter>pos</parameter>) points the
  4546. current position offset in frames.
  4547. </para>
  4548. <para>
  4549. The meaning of the fourth argument is different between
  4550. playback and capture. For playback, it holds the source data
  4551. pointer, and for capture, it's the destination data pointer.
  4552. </para>
  4553. <para>
  4554. The last argument is the number of frames to be copied.
  4555. </para>
  4556. <para>
  4557. What you have to do in this callback is again different
  4558. between playback and capture directions. In the
  4559. playback case, you copy the given amount of data
  4560. (<parameter>count</parameter>) at the specified pointer
  4561. (<parameter>src</parameter>) to the specified offset
  4562. (<parameter>pos</parameter>) on the hardware buffer. When
  4563. coded like memcpy-like way, the copy would be like:
  4564. <informalexample>
  4565. <programlisting>
  4566. <![CDATA[
  4567. my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src,
  4568. frames_to_bytes(runtime, count));
  4569. ]]>
  4570. </programlisting>
  4571. </informalexample>
  4572. </para>
  4573. <para>
  4574. For the capture direction, you copy the given amount of
  4575. data (<parameter>count</parameter>) at the specified offset
  4576. (<parameter>pos</parameter>) on the hardware buffer to the
  4577. specified pointer (<parameter>dst</parameter>).
  4578. <informalexample>
  4579. <programlisting>
  4580. <![CDATA[
  4581. my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos),
  4582. frames_to_bytes(runtime, count));
  4583. ]]>
  4584. </programlisting>
  4585. </informalexample>
  4586. Note that both the position and the amount of data are given
  4587. in frames.
  4588. </para>
  4589. <para>
  4590. In the case of non-interleaved samples, the implementation
  4591. will be a bit more complicated.
  4592. </para>
  4593. <para>
  4594. You need to check the channel argument, and if it's -1, copy
  4595. the whole channels. Otherwise, you have to copy only the
  4596. specified channel. Please check
  4597. <filename>isa/gus/gus_pcm.c</filename> as an example.
  4598. </para>
  4599. <para>
  4600. The <structfield>silence</structfield> callback is also
  4601. implemented in a similar way.
  4602. <informalexample>
  4603. <programlisting>
  4604. <![CDATA[
  4605. static int silence(struct snd_pcm_substream *substream, int channel,
  4606. snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
  4607. ]]>
  4608. </programlisting>
  4609. </informalexample>
  4610. </para>
  4611. <para>
  4612. The meanings of arguments are the same as in the
  4613. <structfield>copy</structfield>
  4614. callback, although there is no <parameter>src/dst</parameter>
  4615. argument. In the case of interleaved samples, the channel
  4616. argument has no meaning, as well as on
  4617. <structfield>copy</structfield> callback.
  4618. </para>
  4619. <para>
  4620. The role of <structfield>silence</structfield> callback is to
  4621. set the given amount
  4622. (<parameter>count</parameter>) of silence data at the
  4623. specified offset (<parameter>pos</parameter>) on the hardware
  4624. buffer. Suppose that the data format is signed (that is, the
  4625. silent-data is 0), and the implementation using a memset-like
  4626. function would be like:
  4627. <informalexample>
  4628. <programlisting>
  4629. <![CDATA[
  4630. my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0,
  4631. frames_to_bytes(runtime, count));
  4632. ]]>
  4633. </programlisting>
  4634. </informalexample>
  4635. </para>
  4636. <para>
  4637. In the case of non-interleaved samples, again, the
  4638. implementation becomes a bit more complicated. See, for example,
  4639. <filename>isa/gus/gus_pcm.c</filename>.
  4640. </para>
  4641. </section>
  4642. <section id="buffer-and-memory-non-contiguous">
  4643. <title>Non-Contiguous Buffers</title>
  4644. <para>
  4645. If your hardware supports the page table as in emu10k1 or the
  4646. buffer descriptors as in via82xx, you can use the scatter-gather
  4647. (SG) DMA. ALSA provides an interface for handling SG-buffers.
  4648. The API is provided in <filename>&lt;sound/pcm.h&gt;</filename>.
  4649. </para>
  4650. <para>
  4651. For creating the SG-buffer handler, call
  4652. <function>snd_pcm_lib_preallocate_pages()</function> or
  4653. <function>snd_pcm_lib_preallocate_pages_for_all()</function>
  4654. with <constant>SNDRV_DMA_TYPE_DEV_SG</constant>
  4655. in the PCM constructor like other PCI pre-allocator.
  4656. You need to pass <function>snd_dma_pci_data(pci)</function>,
  4657. where pci is the struct <structname>pci_dev</structname> pointer
  4658. of the chip as well.
  4659. The <type>struct snd_sg_buf</type> instance is created as
  4660. substream-&gt;dma_private. You can cast
  4661. the pointer like:
  4662. <informalexample>
  4663. <programlisting>
  4664. <![CDATA[
  4665. struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
  4666. ]]>
  4667. </programlisting>
  4668. </informalexample>
  4669. </para>
  4670. <para>
  4671. Then call <function>snd_pcm_lib_malloc_pages()</function>
  4672. in the <structfield>hw_params</structfield> callback
  4673. as well as in the case of normal PCI buffer.
  4674. The SG-buffer handler will allocate the non-contiguous kernel
  4675. pages of the given size and map them onto the virtually contiguous
  4676. memory. The virtual pointer is addressed in runtime-&gt;dma_area.
  4677. The physical address (runtime-&gt;dma_addr) is set to zero,
  4678. because the buffer is physically non-contigous.
  4679. The physical address table is set up in sgbuf-&gt;table.
  4680. You can get the physical address at a certain offset via
  4681. <function>snd_pcm_sgbuf_get_addr()</function>.
  4682. </para>
  4683. <para>
  4684. When a SG-handler is used, you need to set
  4685. <function>snd_pcm_sgbuf_ops_page</function> as
  4686. the <structfield>page</structfield> callback.
  4687. (See <link linkend="pcm-interface-operators-page-callback">
  4688. <citetitle>page callback section</citetitle></link>.)
  4689. </para>
  4690. <para>
  4691. To release the data, call
  4692. <function>snd_pcm_lib_free_pages()</function> in the
  4693. <structfield>hw_free</structfield> callback as usual.
  4694. </para>
  4695. </section>
  4696. <section id="buffer-and-memory-vmalloced">
  4697. <title>Vmalloc'ed Buffers</title>
  4698. <para>
  4699. It's possible to use a buffer allocated via
  4700. <function>vmalloc</function>, for example, for an intermediate
  4701. buffer. Since the allocated pages are not contiguous, you need
  4702. to set the <structfield>page</structfield> callback to obtain
  4703. the physical address at every offset.
  4704. </para>
  4705. <para>
  4706. The implementation of <structfield>page</structfield> callback
  4707. would be like this:
  4708. <informalexample>
  4709. <programlisting>
  4710. <![CDATA[
  4711. #include <linux/vmalloc.h>
  4712. /* get the physical page pointer on the given offset */
  4713. static struct page *mychip_page(struct snd_pcm_substream *substream,
  4714. unsigned long offset)
  4715. {
  4716. void *pageptr = substream->runtime->dma_area + offset;
  4717. return vmalloc_to_page(pageptr);
  4718. }
  4719. ]]>
  4720. </programlisting>
  4721. </informalexample>
  4722. </para>
  4723. </section>
  4724. </chapter>
  4725. <!-- ****************************************************** -->
  4726. <!-- Proc Interface -->
  4727. <!-- ****************************************************** -->
  4728. <chapter id="proc-interface">
  4729. <title>Proc Interface</title>
  4730. <para>
  4731. ALSA provides an easy interface for procfs. The proc files are
  4732. very useful for debugging. I recommend you set up proc files if
  4733. you write a driver and want to get a running status or register
  4734. dumps. The API is found in
  4735. <filename>&lt;sound/info.h&gt;</filename>.
  4736. </para>
  4737. <para>
  4738. To create a proc file, call
  4739. <function>snd_card_proc_new()</function>.
  4740. <informalexample>
  4741. <programlisting>
  4742. <![CDATA[
  4743. struct snd_info_entry *entry;
  4744. int err = snd_card_proc_new(card, "my-file", &entry);
  4745. ]]>
  4746. </programlisting>
  4747. </informalexample>
  4748. where the second argument specifies the name of the proc file to be
  4749. created. The above example will create a file
  4750. <filename>my-file</filename> under the card directory,
  4751. e.g. <filename>/proc/asound/card0/my-file</filename>.
  4752. </para>
  4753. <para>
  4754. Like other components, the proc entry created via
  4755. <function>snd_card_proc_new()</function> will be registered and
  4756. released automatically in the card registration and release
  4757. functions.
  4758. </para>
  4759. <para>
  4760. When the creation is successful, the function stores a new
  4761. instance in the pointer given in the third argument.
  4762. It is initialized as a text proc file for read only. To use
  4763. this proc file as a read-only text file as it is, set the read
  4764. callback with a private data via
  4765. <function>snd_info_set_text_ops()</function>.
  4766. <informalexample>
  4767. <programlisting>
  4768. <![CDATA[
  4769. snd_info_set_text_ops(entry, chip, my_proc_read);
  4770. ]]>
  4771. </programlisting>
  4772. </informalexample>
  4773. where the second argument (<parameter>chip</parameter>) is the
  4774. private data to be used in the callbacks. The third parameter
  4775. specifies the read buffer size and the fourth
  4776. (<parameter>my_proc_read</parameter>) is the callback function, which
  4777. is defined like
  4778. <informalexample>
  4779. <programlisting>
  4780. <![CDATA[
  4781. static void my_proc_read(struct snd_info_entry *entry,
  4782. struct snd_info_buffer *buffer);
  4783. ]]>
  4784. </programlisting>
  4785. </informalexample>
  4786. </para>
  4787. <para>
  4788. In the read callback, use <function>snd_iprintf()</function> for
  4789. output strings, which works just like normal
  4790. <function>printf()</function>. For example,
  4791. <informalexample>
  4792. <programlisting>
  4793. <![CDATA[
  4794. static void my_proc_read(struct snd_info_entry *entry,
  4795. struct snd_info_buffer *buffer)
  4796. {
  4797. struct my_chip *chip = entry->private_data;
  4798. snd_iprintf(buffer, "This is my chip!\n");
  4799. snd_iprintf(buffer, "Port = %ld\n", chip->port);
  4800. }
  4801. ]]>
  4802. </programlisting>
  4803. </informalexample>
  4804. </para>
  4805. <para>
  4806. The file permissions can be changed afterwards. As default, it's
  4807. set as read only for all users. If you want to add write
  4808. permission for the user (root as default), do as follows:
  4809. <informalexample>
  4810. <programlisting>
  4811. <![CDATA[
  4812. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  4813. ]]>
  4814. </programlisting>
  4815. </informalexample>
  4816. and set the write buffer size and the callback
  4817. <informalexample>
  4818. <programlisting>
  4819. <![CDATA[
  4820. entry->c.text.write = my_proc_write;
  4821. ]]>
  4822. </programlisting>
  4823. </informalexample>
  4824. </para>
  4825. <para>
  4826. For the write callback, you can use
  4827. <function>snd_info_get_line()</function> to get a text line, and
  4828. <function>snd_info_get_str()</function> to retrieve a string from
  4829. the line. Some examples are found in
  4830. <filename>core/oss/mixer_oss.c</filename>, core/oss/and
  4831. <filename>pcm_oss.c</filename>.
  4832. </para>
  4833. <para>
  4834. For a raw-data proc-file, set the attributes as follows:
  4835. <informalexample>
  4836. <programlisting>
  4837. <![CDATA[
  4838. static struct snd_info_entry_ops my_file_io_ops = {
  4839. .read = my_file_io_read,
  4840. };
  4841. entry->content = SNDRV_INFO_CONTENT_DATA;
  4842. entry->private_data = chip;
  4843. entry->c.ops = &my_file_io_ops;
  4844. entry->size = 4096;
  4845. entry->mode = S_IFREG | S_IRUGO;
  4846. ]]>
  4847. </programlisting>
  4848. </informalexample>
  4849. </para>
  4850. <para>
  4851. The callback is much more complicated than the text-file
  4852. version. You need to use a low-level I/O functions such as
  4853. <function>copy_from/to_user()</function> to transfer the
  4854. data.
  4855. <informalexample>
  4856. <programlisting>
  4857. <![CDATA[
  4858. static long my_file_io_read(struct snd_info_entry *entry,
  4859. void *file_private_data,
  4860. struct file *file,
  4861. char *buf,
  4862. unsigned long count,
  4863. unsigned long pos)
  4864. {
  4865. long size = count;
  4866. if (pos + size > local_max_size)
  4867. size = local_max_size - pos;
  4868. if (copy_to_user(buf, local_data + pos, size))
  4869. return -EFAULT;
  4870. return size;
  4871. }
  4872. ]]>
  4873. </programlisting>
  4874. </informalexample>
  4875. </para>
  4876. </chapter>
  4877. <!-- ****************************************************** -->
  4878. <!-- Power Management -->
  4879. <!-- ****************************************************** -->
  4880. <chapter id="power-management">
  4881. <title>Power Management</title>
  4882. <para>
  4883. If the chip is supposed to work with suspend/resume
  4884. functions, you need to add power-management code to the
  4885. driver. The additional code for power-management should be
  4886. <function>ifdef</function>'ed with
  4887. <constant>CONFIG_PM</constant>.
  4888. </para>
  4889. <para>
  4890. If the driver <emphasis>fully</emphasis> supports suspend/resume
  4891. that is, the device can be
  4892. properly resumed to its state when suspend was called,
  4893. you can set the <constant>SNDRV_PCM_INFO_RESUME</constant> flag
  4894. in the pcm info field. Usually, this is possible when the
  4895. registers of the chip can be safely saved and restored to
  4896. RAM. If this is set, the trigger callback is called with
  4897. <constant>SNDRV_PCM_TRIGGER_RESUME</constant> after the resume
  4898. callback completes.
  4899. </para>
  4900. <para>
  4901. Even if the driver doesn't support PM fully but
  4902. partial suspend/resume is still possible, it's still worthy to
  4903. implement suspend/resume callbacks. In such a case, applications
  4904. would reset the status by calling
  4905. <function>snd_pcm_prepare()</function> and restart the stream
  4906. appropriately. Hence, you can define suspend/resume callbacks
  4907. below but don't set <constant>SNDRV_PCM_INFO_RESUME</constant>
  4908. info flag to the PCM.
  4909. </para>
  4910. <para>
  4911. Note that the trigger with SUSPEND can always be called when
  4912. <function>snd_pcm_suspend_all</function> is called,
  4913. regardless of the <constant>SNDRV_PCM_INFO_RESUME</constant> flag.
  4914. The <constant>RESUME</constant> flag affects only the behavior
  4915. of <function>snd_pcm_resume()</function>.
  4916. (Thus, in theory,
  4917. <constant>SNDRV_PCM_TRIGGER_RESUME</constant> isn't needed
  4918. to be handled in the trigger callback when no
  4919. <constant>SNDRV_PCM_INFO_RESUME</constant> flag is set. But,
  4920. it's better to keep it for compatibility reasons.)
  4921. </para>
  4922. <para>
  4923. In the earlier version of ALSA drivers, a common
  4924. power-management layer was provided, but it has been removed.
  4925. The driver needs to define the suspend/resume hooks according to
  4926. the bus the device is connected to. In the case of PCI drivers, the
  4927. callbacks look like below:
  4928. <informalexample>
  4929. <programlisting>
  4930. <![CDATA[
  4931. #ifdef CONFIG_PM
  4932. static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
  4933. {
  4934. .... /* do things for suspend */
  4935. return 0;
  4936. }
  4937. static int snd_my_resume(struct pci_dev *pci)
  4938. {
  4939. .... /* do things for suspend */
  4940. return 0;
  4941. }
  4942. #endif
  4943. ]]>
  4944. </programlisting>
  4945. </informalexample>
  4946. </para>
  4947. <para>
  4948. The scheme of the real suspend job is as follows.
  4949. <orderedlist>
  4950. <listitem><para>Retrieve the card and the chip data.</para></listitem>
  4951. <listitem><para>Call <function>snd_power_change_state()</function> with
  4952. <constant>SNDRV_CTL_POWER_D3hot</constant> to change the
  4953. power status.</para></listitem>
  4954. <listitem><para>Call <function>snd_pcm_suspend_all()</function> to suspend the running PCM streams.</para></listitem>
  4955. <listitem><para>If AC97 codecs are used, call
  4956. <function>snd_ac97_suspend()</function> for each codec.</para></listitem>
  4957. <listitem><para>Save the register values if necessary.</para></listitem>
  4958. <listitem><para>Stop the hardware if necessary.</para></listitem>
  4959. <listitem><para>Disable the PCI device by calling
  4960. <function>pci_disable_device()</function>. Then, call
  4961. <function>pci_save_state()</function> at last.</para></listitem>
  4962. </orderedlist>
  4963. </para>
  4964. <para>
  4965. A typical code would be like:
  4966. <informalexample>
  4967. <programlisting>
  4968. <![CDATA[
  4969. static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
  4970. {
  4971. /* (1) */
  4972. struct snd_card *card = pci_get_drvdata(pci);
  4973. struct mychip *chip = card->private_data;
  4974. /* (2) */
  4975. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  4976. /* (3) */
  4977. snd_pcm_suspend_all(chip->pcm);
  4978. /* (4) */
  4979. snd_ac97_suspend(chip->ac97);
  4980. /* (5) */
  4981. snd_mychip_save_registers(chip);
  4982. /* (6) */
  4983. snd_mychip_stop_hardware(chip);
  4984. /* (7) */
  4985. pci_disable_device(pci);
  4986. pci_save_state(pci);
  4987. return 0;
  4988. }
  4989. ]]>
  4990. </programlisting>
  4991. </informalexample>
  4992. </para>
  4993. <para>
  4994. The scheme of the real resume job is as follows.
  4995. <orderedlist>
  4996. <listitem><para>Retrieve the card and the chip data.</para></listitem>
  4997. <listitem><para>Set up PCI. First, call <function>pci_restore_state()</function>.
  4998. Then enable the pci device again by calling <function>pci_enable_device()</function>.
  4999. Call <function>pci_set_master()</function> if necessary, too.</para></listitem>
  5000. <listitem><para>Re-initialize the chip.</para></listitem>
  5001. <listitem><para>Restore the saved registers if necessary.</para></listitem>
  5002. <listitem><para>Resume the mixer, e.g. calling
  5003. <function>snd_ac97_resume()</function>.</para></listitem>
  5004. <listitem><para>Restart the hardware (if any).</para></listitem>
  5005. <listitem><para>Call <function>snd_power_change_state()</function> with
  5006. <constant>SNDRV_CTL_POWER_D0</constant> to notify the processes.</para></listitem>
  5007. </orderedlist>
  5008. </para>
  5009. <para>
  5010. A typical code would be like:
  5011. <informalexample>
  5012. <programlisting>
  5013. <![CDATA[
  5014. static int mychip_resume(struct pci_dev *pci)
  5015. {
  5016. /* (1) */
  5017. struct snd_card *card = pci_get_drvdata(pci);
  5018. struct mychip *chip = card->private_data;
  5019. /* (2) */
  5020. pci_restore_state(pci);
  5021. pci_enable_device(pci);
  5022. pci_set_master(pci);
  5023. /* (3) */
  5024. snd_mychip_reinit_chip(chip);
  5025. /* (4) */
  5026. snd_mychip_restore_registers(chip);
  5027. /* (5) */
  5028. snd_ac97_resume(chip->ac97);
  5029. /* (6) */
  5030. snd_mychip_restart_chip(chip);
  5031. /* (7) */
  5032. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  5033. return 0;
  5034. }
  5035. ]]>
  5036. </programlisting>
  5037. </informalexample>
  5038. </para>
  5039. <para>
  5040. As shown in the above, it's better to save registers after
  5041. suspending the PCM operations via
  5042. <function>snd_pcm_suspend_all()</function> or
  5043. <function>snd_pcm_suspend()</function>. It means that the PCM
  5044. streams are already stoppped when the register snapshot is
  5045. taken. But, remember that you don't have to restart the PCM
  5046. stream in the resume callback. It'll be restarted via
  5047. trigger call with <constant>SNDRV_PCM_TRIGGER_RESUME</constant>
  5048. when necessary.
  5049. </para>
  5050. <para>
  5051. OK, we have all callbacks now. Let's set them up. In the
  5052. initialization of the card, make sure that you can get the chip
  5053. data from the card instance, typically via
  5054. <structfield>private_data</structfield> field, in case you
  5055. created the chip data individually.
  5056. <informalexample>
  5057. <programlisting>
  5058. <![CDATA[
  5059. static int __devinit snd_mychip_probe(struct pci_dev *pci,
  5060. const struct pci_device_id *pci_id)
  5061. {
  5062. ....
  5063. struct snd_card *card;
  5064. struct mychip *chip;
  5065. int err;
  5066. ....
  5067. err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
  5068. ....
  5069. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  5070. ....
  5071. card->private_data = chip;
  5072. ....
  5073. }
  5074. ]]>
  5075. </programlisting>
  5076. </informalexample>
  5077. When you created the chip data with
  5078. <function>snd_card_create()</function>, it's anyway accessible
  5079. via <structfield>private_data</structfield> field.
  5080. <informalexample>
  5081. <programlisting>
  5082. <![CDATA[
  5083. static int __devinit snd_mychip_probe(struct pci_dev *pci,
  5084. const struct pci_device_id *pci_id)
  5085. {
  5086. ....
  5087. struct snd_card *card;
  5088. struct mychip *chip;
  5089. int err;
  5090. ....
  5091. err = snd_card_create(index[dev], id[dev], THIS_MODULE,
  5092. sizeof(struct mychip), &card);
  5093. ....
  5094. chip = card->private_data;
  5095. ....
  5096. }
  5097. ]]>
  5098. </programlisting>
  5099. </informalexample>
  5100. </para>
  5101. <para>
  5102. If you need a space to save the registers, allocate the
  5103. buffer for it here, too, since it would be fatal
  5104. if you cannot allocate a memory in the suspend phase.
  5105. The allocated buffer should be released in the corresponding
  5106. destructor.
  5107. </para>
  5108. <para>
  5109. And next, set suspend/resume callbacks to the pci_driver.
  5110. <informalexample>
  5111. <programlisting>
  5112. <![CDATA[
  5113. static struct pci_driver driver = {
  5114. .name = "My Chip",
  5115. .id_table = snd_my_ids,
  5116. .probe = snd_my_probe,
  5117. .remove = __devexit_p(snd_my_remove),
  5118. #ifdef CONFIG_PM
  5119. .suspend = snd_my_suspend,
  5120. .resume = snd_my_resume,
  5121. #endif
  5122. };
  5123. ]]>
  5124. </programlisting>
  5125. </informalexample>
  5126. </para>
  5127. </chapter>
  5128. <!-- ****************************************************** -->
  5129. <!-- Module Parameters -->
  5130. <!-- ****************************************************** -->
  5131. <chapter id="module-parameters">
  5132. <title>Module Parameters</title>
  5133. <para>
  5134. There are standard module options for ALSA. At least, each
  5135. module should have the <parameter>index</parameter>,
  5136. <parameter>id</parameter> and <parameter>enable</parameter>
  5137. options.
  5138. </para>
  5139. <para>
  5140. If the module supports multiple cards (usually up to
  5141. 8 = <constant>SNDRV_CARDS</constant> cards), they should be
  5142. arrays. The default initial values are defined already as
  5143. constants for easier programming:
  5144. <informalexample>
  5145. <programlisting>
  5146. <![CDATA[
  5147. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  5148. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  5149. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  5150. ]]>
  5151. </programlisting>
  5152. </informalexample>
  5153. </para>
  5154. <para>
  5155. If the module supports only a single card, they could be single
  5156. variables, instead. <parameter>enable</parameter> option is not
  5157. always necessary in this case, but it would be better to have a
  5158. dummy option for compatibility.
  5159. </para>
  5160. <para>
  5161. The module parameters must be declared with the standard
  5162. <function>module_param()()</function>,
  5163. <function>module_param_array()()</function> and
  5164. <function>MODULE_PARM_DESC()</function> macros.
  5165. </para>
  5166. <para>
  5167. The typical coding would be like below:
  5168. <informalexample>
  5169. <programlisting>
  5170. <![CDATA[
  5171. #define CARD_NAME "My Chip"
  5172. module_param_array(index, int, NULL, 0444);
  5173. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  5174. module_param_array(id, charp, NULL, 0444);
  5175. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  5176. module_param_array(enable, bool, NULL, 0444);
  5177. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  5178. ]]>
  5179. </programlisting>
  5180. </informalexample>
  5181. </para>
  5182. <para>
  5183. Also, don't forget to define the module description, classes,
  5184. license and devices. Especially, the recent modprobe requires to
  5185. define the module license as GPL, etc., otherwise the system is
  5186. shown as <quote>tainted</quote>.
  5187. <informalexample>
  5188. <programlisting>
  5189. <![CDATA[
  5190. MODULE_DESCRIPTION("My Chip");
  5191. MODULE_LICENSE("GPL");
  5192. MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
  5193. ]]>
  5194. </programlisting>
  5195. </informalexample>
  5196. </para>
  5197. </chapter>
  5198. <!-- ****************************************************** -->
  5199. <!-- How To Put Your Driver -->
  5200. <!-- ****************************************************** -->
  5201. <chapter id="how-to-put-your-driver">
  5202. <title>How To Put Your Driver Into ALSA Tree</title>
  5203. <section>
  5204. <title>General</title>
  5205. <para>
  5206. So far, you've learned how to write the driver codes.
  5207. And you might have a question now: how to put my own
  5208. driver into the ALSA driver tree?
  5209. Here (finally :) the standard procedure is described briefly.
  5210. </para>
  5211. <para>
  5212. Suppose that you create a new PCI driver for the card
  5213. <quote>xyz</quote>. The card module name would be
  5214. snd-xyz. The new driver is usually put into the alsa-driver
  5215. tree, <filename>alsa-driver/pci</filename> directory in
  5216. the case of PCI cards.
  5217. Then the driver is evaluated, audited and tested
  5218. by developers and users. After a certain time, the driver
  5219. will go to the alsa-kernel tree (to the corresponding directory,
  5220. such as <filename>alsa-kernel/pci</filename>) and eventually
  5221. will be integrated into the Linux 2.6 tree (the directory would be
  5222. <filename>linux/sound/pci</filename>).
  5223. </para>
  5224. <para>
  5225. In the following sections, the driver code is supposed
  5226. to be put into alsa-driver tree. The two cases are covered:
  5227. a driver consisting of a single source file and one consisting
  5228. of several source files.
  5229. </para>
  5230. </section>
  5231. <section>
  5232. <title>Driver with A Single Source File</title>
  5233. <para>
  5234. <orderedlist>
  5235. <listitem>
  5236. <para>
  5237. Modify alsa-driver/pci/Makefile
  5238. </para>
  5239. <para>
  5240. Suppose you have a file xyz.c. Add the following
  5241. two lines
  5242. <informalexample>
  5243. <programlisting>
  5244. <![CDATA[
  5245. snd-xyz-objs := xyz.o
  5246. obj-$(CONFIG_SND_XYZ) += snd-xyz.o
  5247. ]]>
  5248. </programlisting>
  5249. </informalexample>
  5250. </para>
  5251. </listitem>
  5252. <listitem>
  5253. <para>
  5254. Create the Kconfig entry
  5255. </para>
  5256. <para>
  5257. Add the new entry of Kconfig for your xyz driver.
  5258. <informalexample>
  5259. <programlisting>
  5260. <![CDATA[
  5261. config SND_XYZ
  5262. tristate "Foobar XYZ"
  5263. depends on SND
  5264. select SND_PCM
  5265. help
  5266. Say Y here to include support for Foobar XYZ soundcard.
  5267. To compile this driver as a module, choose M here: the module
  5268. will be called snd-xyz.
  5269. ]]>
  5270. </programlisting>
  5271. </informalexample>
  5272. the line, select SND_PCM, specifies that the driver xyz supports
  5273. PCM. In addition to SND_PCM, the following components are
  5274. supported for select command:
  5275. SND_RAWMIDI, SND_TIMER, SND_HWDEP, SND_MPU401_UART,
  5276. SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, SND_AC97_CODEC.
  5277. Add the select command for each supported component.
  5278. </para>
  5279. <para>
  5280. Note that some selections imply the lowlevel selections.
  5281. For example, PCM includes TIMER, MPU401_UART includes RAWMIDI,
  5282. AC97_CODEC includes PCM, and OPL3_LIB includes HWDEP.
  5283. You don't need to give the lowlevel selections again.
  5284. </para>
  5285. <para>
  5286. For the details of Kconfig script, refer to the kbuild
  5287. documentation.
  5288. </para>
  5289. </listitem>
  5290. <listitem>
  5291. <para>
  5292. Run cvscompile script to re-generate the configure script and
  5293. build the whole stuff again.
  5294. </para>
  5295. </listitem>
  5296. </orderedlist>
  5297. </para>
  5298. </section>
  5299. <section>
  5300. <title>Drivers with Several Source Files</title>
  5301. <para>
  5302. Suppose that the driver snd-xyz have several source files.
  5303. They are located in the new subdirectory,
  5304. pci/xyz.
  5305. <orderedlist>
  5306. <listitem>
  5307. <para>
  5308. Add a new directory (<filename>xyz</filename>) in
  5309. <filename>alsa-driver/pci/Makefile</filename> as below
  5310. <informalexample>
  5311. <programlisting>
  5312. <![CDATA[
  5313. obj-$(CONFIG_SND) += xyz/
  5314. ]]>
  5315. </programlisting>
  5316. </informalexample>
  5317. </para>
  5318. </listitem>
  5319. <listitem>
  5320. <para>
  5321. Under the directory <filename>xyz</filename>, create a Makefile
  5322. <example>
  5323. <title>Sample Makefile for a driver xyz</title>
  5324. <programlisting>
  5325. <![CDATA[
  5326. ifndef SND_TOPDIR
  5327. SND_TOPDIR=../..
  5328. endif
  5329. include $(SND_TOPDIR)/toplevel.config
  5330. include $(SND_TOPDIR)/Makefile.conf
  5331. snd-xyz-objs := xyz.o abc.o def.o
  5332. obj-$(CONFIG_SND_XYZ) += snd-xyz.o
  5333. include $(SND_TOPDIR)/Rules.make
  5334. ]]>
  5335. </programlisting>
  5336. </example>
  5337. </para>
  5338. </listitem>
  5339. <listitem>
  5340. <para>
  5341. Create the Kconfig entry
  5342. </para>
  5343. <para>
  5344. This procedure is as same as in the last section.
  5345. </para>
  5346. </listitem>
  5347. <listitem>
  5348. <para>
  5349. Run cvscompile script to re-generate the configure script and
  5350. build the whole stuff again.
  5351. </para>
  5352. </listitem>
  5353. </orderedlist>
  5354. </para>
  5355. </section>
  5356. </chapter>
  5357. <!-- ****************************************************** -->
  5358. <!-- Useful Functions -->
  5359. <!-- ****************************************************** -->
  5360. <chapter id="useful-functions">
  5361. <title>Useful Functions</title>
  5362. <section id="useful-functions-snd-printk">
  5363. <title><function>snd_printk()</function> and friends</title>
  5364. <para>
  5365. ALSA provides a verbose version of the
  5366. <function>printk()</function> function. If a kernel config
  5367. <constant>CONFIG_SND_VERBOSE_PRINTK</constant> is set, this
  5368. function prints the given message together with the file name
  5369. and the line of the caller. The <constant>KERN_XXX</constant>
  5370. prefix is processed as
  5371. well as the original <function>printk()</function> does, so it's
  5372. recommended to add this prefix, e.g.
  5373. <informalexample>
  5374. <programlisting>
  5375. <![CDATA[
  5376. snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\n");
  5377. ]]>
  5378. </programlisting>
  5379. </informalexample>
  5380. </para>
  5381. <para>
  5382. There are also <function>printk()</function>'s for
  5383. debugging. <function>snd_printd()</function> can be used for
  5384. general debugging purposes. If
  5385. <constant>CONFIG_SND_DEBUG</constant> is set, this function is
  5386. compiled, and works just like
  5387. <function>snd_printk()</function>. If the ALSA is compiled
  5388. without the debugging flag, it's ignored.
  5389. </para>
  5390. <para>
  5391. <function>snd_printdd()</function> is compiled in only when
  5392. <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is set. Please note
  5393. that <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is not set as default
  5394. even if you configure the alsa-driver with
  5395. <option>--with-debug=full</option> option. You need to give
  5396. explicitly <option>--with-debug=detect</option> option instead.
  5397. </para>
  5398. </section>
  5399. <section id="useful-functions-snd-bug">
  5400. <title><function>snd_BUG()</function></title>
  5401. <para>
  5402. It shows the <computeroutput>BUG?</computeroutput> message and
  5403. stack trace as well as <function>snd_BUG_ON</function> at the point.
  5404. It's useful to show that a fatal error happens there.
  5405. </para>
  5406. <para>
  5407. When no debug flag is set, this macro is ignored.
  5408. </para>
  5409. </section>
  5410. <section id="useful-functions-snd-bug-on">
  5411. <title><function>snd_BUG_ON()</function></title>
  5412. <para>
  5413. <function>snd_BUG_ON()</function> macro is similar with
  5414. <function>WARN_ON()</function> macro. For example,
  5415. <informalexample>
  5416. <programlisting>
  5417. <![CDATA[
  5418. snd_BUG_ON(!pointer);
  5419. ]]>
  5420. </programlisting>
  5421. </informalexample>
  5422. or it can be used as the condition,
  5423. <informalexample>
  5424. <programlisting>
  5425. <![CDATA[
  5426. if (snd_BUG_ON(non_zero_is_bug))
  5427. return -EINVAL;
  5428. ]]>
  5429. </programlisting>
  5430. </informalexample>
  5431. </para>
  5432. <para>
  5433. The macro takes an conditional expression to evaluate.
  5434. When <constant>CONFIG_SND_DEBUG</constant>, is set, the
  5435. expression is actually evaluated. If it's non-zero, it shows
  5436. the warning message such as
  5437. <computeroutput>BUG? (xxx)</computeroutput>
  5438. normally followed by stack trace. It returns the evaluated
  5439. value.
  5440. When no <constant>CONFIG_SND_DEBUG</constant> is set, this
  5441. macro always returns zero.
  5442. </para>
  5443. </section>
  5444. </chapter>
  5445. <!-- ****************************************************** -->
  5446. <!-- Acknowledgments -->
  5447. <!-- ****************************************************** -->
  5448. <chapter id="acknowledgments">
  5449. <title>Acknowledgments</title>
  5450. <para>
  5451. I would like to thank Phil Kerr for his help for improvement and
  5452. corrections of this document.
  5453. </para>
  5454. <para>
  5455. Kevin Conder reformatted the original plain-text to the
  5456. DocBook format.
  5457. </para>
  5458. <para>
  5459. Giuliano Pochini corrected typos and contributed the example codes
  5460. in the hardware constraints section.
  5461. </para>
  5462. </chapter>
  5463. </book>