writing-an-alsa-driver.tmpl 193 KB

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