writing-an-alsa-driver.tmpl 200 KB

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