writing-an-alsa-driver.tmpl 199 KB

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