writing-an-alsa-driver.tmpl 200 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207
  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. /* -- timer -- */
  1906. unsigned int timer_resolution; /* timer resolution */
  1907. /* -- DMA -- */
  1908. unsigned char *dma_area; /* DMA area */
  1909. dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
  1910. size_t dma_bytes; /* size of DMA area */
  1911. struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
  1912. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  1913. /* -- OSS things -- */
  1914. struct snd_pcm_oss_runtime oss;
  1915. #endif
  1916. };
  1917. ]]>
  1918. </programlisting>
  1919. </informalexample>
  1920. </para>
  1921. <para>
  1922. For the operators (callbacks) of each sound driver, most of
  1923. these records are supposed to be read-only. Only the PCM
  1924. middle-layer changes / updates them. The exceptions are
  1925. the hardware description (hw) DMA buffer information and the
  1926. private data. Besides, if you use the standard buffer allocation
  1927. method via <function>snd_pcm_lib_malloc_pages()</function>,
  1928. you don't need to set the DMA buffer information by yourself.
  1929. </para>
  1930. <para>
  1931. In the sections below, important records are explained.
  1932. </para>
  1933. <section id="pcm-interface-runtime-hw">
  1934. <title>Hardware Description</title>
  1935. <para>
  1936. The hardware descriptor (struct <structname>snd_pcm_hardware</structname>)
  1937. contains the definitions of the fundamental hardware
  1938. configuration. Above all, you'll need to define this in
  1939. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  1940. the open callback</citetitle></link>.
  1941. Note that the runtime instance holds the copy of the
  1942. descriptor, not the pointer to the existing descriptor. That
  1943. is, in the open callback, you can modify the copied descriptor
  1944. (<constant>runtime-&gt;hw</constant>) as you need. For example, if the maximum
  1945. number of channels is 1 only on some chip models, you can
  1946. still use the same hardware descriptor and change the
  1947. channels_max later:
  1948. <informalexample>
  1949. <programlisting>
  1950. <![CDATA[
  1951. struct snd_pcm_runtime *runtime = substream->runtime;
  1952. ...
  1953. runtime->hw = snd_mychip_playback_hw; /* common definition */
  1954. if (chip->model == VERY_OLD_ONE)
  1955. runtime->hw.channels_max = 1;
  1956. ]]>
  1957. </programlisting>
  1958. </informalexample>
  1959. </para>
  1960. <para>
  1961. Typically, you'll have a hardware descriptor as below:
  1962. <informalexample>
  1963. <programlisting>
  1964. <![CDATA[
  1965. static struct snd_pcm_hardware snd_mychip_playback_hw = {
  1966. .info = (SNDRV_PCM_INFO_MMAP |
  1967. SNDRV_PCM_INFO_INTERLEAVED |
  1968. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1969. SNDRV_PCM_INFO_MMAP_VALID),
  1970. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1971. .rates = SNDRV_PCM_RATE_8000_48000,
  1972. .rate_min = 8000,
  1973. .rate_max = 48000,
  1974. .channels_min = 2,
  1975. .channels_max = 2,
  1976. .buffer_bytes_max = 32768,
  1977. .period_bytes_min = 4096,
  1978. .period_bytes_max = 32768,
  1979. .periods_min = 1,
  1980. .periods_max = 1024,
  1981. };
  1982. ]]>
  1983. </programlisting>
  1984. </informalexample>
  1985. </para>
  1986. <para>
  1987. <itemizedlist>
  1988. <listitem><para>
  1989. The <structfield>info</structfield> field contains the type and
  1990. capabilities of this pcm. The bit flags are defined in
  1991. <filename>&lt;sound/asound.h&gt;</filename> as
  1992. <constant>SNDRV_PCM_INFO_XXX</constant>. Here, at least, you
  1993. have to specify whether the mmap is supported and which
  1994. interleaved format is supported.
  1995. When the hardware supports mmap, add the
  1996. <constant>SNDRV_PCM_INFO_MMAP</constant> flag here. When the
  1997. hardware supports the interleaved or the non-interleaved
  1998. formats, <constant>SNDRV_PCM_INFO_INTERLEAVED</constant> or
  1999. <constant>SNDRV_PCM_INFO_NONINTERLEAVED</constant> flag must
  2000. be set, respectively. If both are supported, you can set both,
  2001. too.
  2002. </para>
  2003. <para>
  2004. In the above example, <constant>MMAP_VALID</constant> and
  2005. <constant>BLOCK_TRANSFER</constant> are specified for the OSS mmap
  2006. mode. Usually both are set. Of course,
  2007. <constant>MMAP_VALID</constant> is set only if the mmap is
  2008. really supported.
  2009. </para>
  2010. <para>
  2011. The other possible flags are
  2012. <constant>SNDRV_PCM_INFO_PAUSE</constant> and
  2013. <constant>SNDRV_PCM_INFO_RESUME</constant>. The
  2014. <constant>PAUSE</constant> bit means that the pcm supports the
  2015. <quote>pause</quote> operation, while the
  2016. <constant>RESUME</constant> bit means that the pcm supports
  2017. the full <quote>suspend/resume</quote> operation.
  2018. If the <constant>PAUSE</constant> flag is set,
  2019. the <structfield>trigger</structfield> callback below
  2020. must handle the corresponding (pause push/release) commands.
  2021. The suspend/resume trigger commands can be defined even without
  2022. the <constant>RESUME</constant> flag. See <link
  2023. linkend="power-management"><citetitle>
  2024. Power Management</citetitle></link> section for details.
  2025. </para>
  2026. <para>
  2027. When the PCM substreams can be synchronized (typically,
  2028. synchronized start/stop of a playback and a capture streams),
  2029. you can give <constant>SNDRV_PCM_INFO_SYNC_START</constant>,
  2030. too. In this case, you'll need to check the linked-list of
  2031. PCM substreams in the trigger callback. This will be
  2032. described in the later section.
  2033. </para>
  2034. </listitem>
  2035. <listitem>
  2036. <para>
  2037. <structfield>formats</structfield> field contains the bit-flags
  2038. of supported formats (<constant>SNDRV_PCM_FMTBIT_XXX</constant>).
  2039. If the hardware supports more than one format, give all or'ed
  2040. bits. In the example above, the signed 16bit little-endian
  2041. format is specified.
  2042. </para>
  2043. </listitem>
  2044. <listitem>
  2045. <para>
  2046. <structfield>rates</structfield> field contains the bit-flags of
  2047. supported rates (<constant>SNDRV_PCM_RATE_XXX</constant>).
  2048. When the chip supports continuous rates, pass
  2049. <constant>CONTINUOUS</constant> bit additionally.
  2050. The pre-defined rate bits are provided only for typical
  2051. rates. If your chip supports unconventional rates, you need to add
  2052. the <constant>KNOT</constant> bit and set up the hardware
  2053. constraint manually (explained later).
  2054. </para>
  2055. </listitem>
  2056. <listitem>
  2057. <para>
  2058. <structfield>rate_min</structfield> and
  2059. <structfield>rate_max</structfield> define the minimum and
  2060. maximum sample rate. This should correspond somehow to
  2061. <structfield>rates</structfield> bits.
  2062. </para>
  2063. </listitem>
  2064. <listitem>
  2065. <para>
  2066. <structfield>channel_min</structfield> and
  2067. <structfield>channel_max</structfield>
  2068. define, as you might already expected, the minimum and maximum
  2069. number of channels.
  2070. </para>
  2071. </listitem>
  2072. <listitem>
  2073. <para>
  2074. <structfield>buffer_bytes_max</structfield> defines the
  2075. maximum buffer size in bytes. There is no
  2076. <structfield>buffer_bytes_min</structfield> field, since
  2077. it can be calculated from the minimum period size and the
  2078. minimum number of periods.
  2079. Meanwhile, <structfield>period_bytes_min</structfield> and
  2080. define the minimum and maximum size of the period in bytes.
  2081. <structfield>periods_max</structfield> and
  2082. <structfield>periods_min</structfield> define the maximum and
  2083. minimum number of periods in the buffer.
  2084. </para>
  2085. <para>
  2086. The <quote>period</quote> is a term that corresponds to
  2087. a fragment in the OSS world. The period defines the size at
  2088. which a PCM interrupt is generated. This size strongly
  2089. depends on the hardware.
  2090. Generally, the smaller period size will give you more
  2091. interrupts, that is, more controls.
  2092. In the case of capture, this size defines the input latency.
  2093. On the other hand, the whole buffer size defines the
  2094. output latency for the playback direction.
  2095. </para>
  2096. </listitem>
  2097. <listitem>
  2098. <para>
  2099. There is also a field <structfield>fifo_size</structfield>.
  2100. This specifies the size of the hardware FIFO, but currently it
  2101. is neither used in the driver nor in the alsa-lib. So, you
  2102. can ignore this field.
  2103. </para>
  2104. </listitem>
  2105. </itemizedlist>
  2106. </para>
  2107. </section>
  2108. <section id="pcm-interface-runtime-config">
  2109. <title>PCM Configurations</title>
  2110. <para>
  2111. Ok, let's go back again to the PCM runtime records.
  2112. The most frequently referred records in the runtime instance are
  2113. the PCM configurations.
  2114. The PCM configurations are stored in the runtime instance
  2115. after the application sends <type>hw_params</type> data via
  2116. alsa-lib. There are many fields copied from hw_params and
  2117. sw_params structs. For example,
  2118. <structfield>format</structfield> holds the format type
  2119. chosen by the application. This field contains the enum value
  2120. <constant>SNDRV_PCM_FORMAT_XXX</constant>.
  2121. </para>
  2122. <para>
  2123. One thing to be noted is that the configured buffer and period
  2124. sizes are stored in <quote>frames</quote> in the runtime.
  2125. In the ALSA world, 1 frame = channels * samples-size.
  2126. For conversion between frames and bytes, you can use the
  2127. <function>frames_to_bytes()</function> and
  2128. <function>bytes_to_frames()</function> helper functions.
  2129. <informalexample>
  2130. <programlisting>
  2131. <![CDATA[
  2132. period_bytes = frames_to_bytes(runtime, runtime->period_size);
  2133. ]]>
  2134. </programlisting>
  2135. </informalexample>
  2136. </para>
  2137. <para>
  2138. Also, many software parameters (sw_params) are
  2139. stored in frames, too. Please check the type of the field.
  2140. <type>snd_pcm_uframes_t</type> is for the frames as unsigned
  2141. integer while <type>snd_pcm_sframes_t</type> is for the frames
  2142. as signed integer.
  2143. </para>
  2144. </section>
  2145. <section id="pcm-interface-runtime-dma">
  2146. <title>DMA Buffer Information</title>
  2147. <para>
  2148. The DMA buffer is defined by the following four fields,
  2149. <structfield>dma_area</structfield>,
  2150. <structfield>dma_addr</structfield>,
  2151. <structfield>dma_bytes</structfield> and
  2152. <structfield>dma_private</structfield>.
  2153. The <structfield>dma_area</structfield> holds the buffer
  2154. pointer (the logical address). You can call
  2155. <function>memcpy</function> from/to
  2156. this pointer. Meanwhile, <structfield>dma_addr</structfield>
  2157. holds the physical address of the buffer. This field is
  2158. specified only when the buffer is a linear buffer.
  2159. <structfield>dma_bytes</structfield> holds the size of buffer
  2160. in bytes. <structfield>dma_private</structfield> is used for
  2161. the ALSA DMA allocator.
  2162. </para>
  2163. <para>
  2164. If you use a standard ALSA function,
  2165. <function>snd_pcm_lib_malloc_pages()</function>, for
  2166. allocating the buffer, these fields are set by the ALSA middle
  2167. layer, and you should <emphasis>not</emphasis> change them by
  2168. yourself. You can read them but not write them.
  2169. On the other hand, if you want to allocate the buffer by
  2170. yourself, you'll need to manage it in hw_params callback.
  2171. At least, <structfield>dma_bytes</structfield> is mandatory.
  2172. <structfield>dma_area</structfield> is necessary when the
  2173. buffer is mmapped. If your driver doesn't support mmap, this
  2174. field is not necessary. <structfield>dma_addr</structfield>
  2175. is also optional. You can use
  2176. <structfield>dma_private</structfield> as you like, too.
  2177. </para>
  2178. </section>
  2179. <section id="pcm-interface-runtime-status">
  2180. <title>Running Status</title>
  2181. <para>
  2182. The running status can be referred via <constant>runtime-&gt;status</constant>.
  2183. This is the pointer to the struct <structname>snd_pcm_mmap_status</structname>
  2184. record. For example, you can get the current DMA hardware
  2185. pointer via <constant>runtime-&gt;status-&gt;hw_ptr</constant>.
  2186. </para>
  2187. <para>
  2188. The DMA application pointer can be referred via
  2189. <constant>runtime-&gt;control</constant>, which points to the
  2190. struct <structname>snd_pcm_mmap_control</structname> record.
  2191. However, accessing directly to this value is not recommended.
  2192. </para>
  2193. </section>
  2194. <section id="pcm-interface-runtime-private">
  2195. <title>Private Data</title>
  2196. <para>
  2197. You can allocate a record for the substream and store it in
  2198. <constant>runtime-&gt;private_data</constant>. Usually, this
  2199. is done in
  2200. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  2201. the open callback</citetitle></link>.
  2202. Don't mix this with <constant>pcm-&gt;private_data</constant>.
  2203. The <constant>pcm-&gt;private_data</constant> usually points to the
  2204. chip instance assigned statically at the creation of PCM, while the
  2205. <constant>runtime-&gt;private_data</constant> points to a dynamic
  2206. data structure created at the PCM open callback.
  2207. <informalexample>
  2208. <programlisting>
  2209. <![CDATA[
  2210. static int snd_xxx_open(struct snd_pcm_substream *substream)
  2211. {
  2212. struct my_pcm_data *data;
  2213. ....
  2214. data = kmalloc(sizeof(*data), GFP_KERNEL);
  2215. substream->runtime->private_data = data;
  2216. ....
  2217. }
  2218. ]]>
  2219. </programlisting>
  2220. </informalexample>
  2221. </para>
  2222. <para>
  2223. The allocated object must be released in
  2224. <link linkend="pcm-interface-operators-open-callback"><citetitle>
  2225. the close callback</citetitle></link>.
  2226. </para>
  2227. </section>
  2228. </section>
  2229. <section id="pcm-interface-operators">
  2230. <title>Operators</title>
  2231. <para>
  2232. OK, now let me give details about each pcm callback
  2233. (<parameter>ops</parameter>). In general, every callback must
  2234. return 0 if successful, or a negative error number
  2235. such as <constant>-EINVAL</constant>. To choose an appropriate
  2236. error number, it is advised to check what value other parts of
  2237. the kernel return when the same kind of request fails.
  2238. </para>
  2239. <para>
  2240. The callback function takes at least the argument with
  2241. <structname>snd_pcm_substream</structname> pointer. To retrieve
  2242. the chip record from the given substream instance, you can use the
  2243. following macro.
  2244. <informalexample>
  2245. <programlisting>
  2246. <![CDATA[
  2247. int xxx() {
  2248. struct mychip *chip = snd_pcm_substream_chip(substream);
  2249. ....
  2250. }
  2251. ]]>
  2252. </programlisting>
  2253. </informalexample>
  2254. The macro reads <constant>substream-&gt;private_data</constant>,
  2255. which is a copy of <constant>pcm-&gt;private_data</constant>.
  2256. You can override the former if you need to assign different data
  2257. records per PCM substream. For example, the cmi8330 driver assigns
  2258. different private_data for playback and capture directions,
  2259. because it uses two different codecs (SB- and AD-compatible) for
  2260. different directions.
  2261. </para>
  2262. <section id="pcm-interface-operators-open-callback">
  2263. <title>open callback</title>
  2264. <para>
  2265. <informalexample>
  2266. <programlisting>
  2267. <![CDATA[
  2268. static int snd_xxx_open(struct snd_pcm_substream *substream);
  2269. ]]>
  2270. </programlisting>
  2271. </informalexample>
  2272. This is called when a pcm substream is opened.
  2273. </para>
  2274. <para>
  2275. At least, here you have to initialize the runtime-&gt;hw
  2276. record. Typically, this is done by like this:
  2277. <informalexample>
  2278. <programlisting>
  2279. <![CDATA[
  2280. static int snd_xxx_open(struct snd_pcm_substream *substream)
  2281. {
  2282. struct mychip *chip = snd_pcm_substream_chip(substream);
  2283. struct snd_pcm_runtime *runtime = substream->runtime;
  2284. runtime->hw = snd_mychip_playback_hw;
  2285. return 0;
  2286. }
  2287. ]]>
  2288. </programlisting>
  2289. </informalexample>
  2290. where <parameter>snd_mychip_playback_hw</parameter> is the
  2291. pre-defined hardware description.
  2292. </para>
  2293. <para>
  2294. You can allocate a private data in this callback, as described
  2295. in <link linkend="pcm-interface-runtime-private"><citetitle>
  2296. Private Data</citetitle></link> section.
  2297. </para>
  2298. <para>
  2299. If the hardware configuration needs more constraints, set the
  2300. hardware constraints here, too.
  2301. See <link linkend="pcm-interface-constraints"><citetitle>
  2302. Constraints</citetitle></link> for more details.
  2303. </para>
  2304. </section>
  2305. <section id="pcm-interface-operators-close-callback">
  2306. <title>close callback</title>
  2307. <para>
  2308. <informalexample>
  2309. <programlisting>
  2310. <![CDATA[
  2311. static int snd_xxx_close(struct snd_pcm_substream *substream);
  2312. ]]>
  2313. </programlisting>
  2314. </informalexample>
  2315. Obviously, this is called when a pcm substream is closed.
  2316. </para>
  2317. <para>
  2318. Any private instance for a pcm substream allocated in the
  2319. open callback will be released here.
  2320. <informalexample>
  2321. <programlisting>
  2322. <![CDATA[
  2323. static int snd_xxx_close(struct snd_pcm_substream *substream)
  2324. {
  2325. ....
  2326. kfree(substream->runtime->private_data);
  2327. ....
  2328. }
  2329. ]]>
  2330. </programlisting>
  2331. </informalexample>
  2332. </para>
  2333. </section>
  2334. <section id="pcm-interface-operators-ioctl-callback">
  2335. <title>ioctl callback</title>
  2336. <para>
  2337. This is used for any special call to pcm ioctls. But
  2338. usually you can pass a generic ioctl callback,
  2339. <function>snd_pcm_lib_ioctl</function>.
  2340. </para>
  2341. </section>
  2342. <section id="pcm-interface-operators-hw-params-callback">
  2343. <title>hw_params callback</title>
  2344. <para>
  2345. <informalexample>
  2346. <programlisting>
  2347. <![CDATA[
  2348. static int snd_xxx_hw_params(struct snd_pcm_substream *substream,
  2349. struct snd_pcm_hw_params *hw_params);
  2350. ]]>
  2351. </programlisting>
  2352. </informalexample>
  2353. </para>
  2354. <para>
  2355. This is called when the hardware parameter
  2356. (<structfield>hw_params</structfield>) is set
  2357. up by the application,
  2358. that is, once when the buffer size, the period size, the
  2359. format, etc. are defined for the pcm substream.
  2360. </para>
  2361. <para>
  2362. Many hardware setups should be done in this callback,
  2363. including the allocation of buffers.
  2364. </para>
  2365. <para>
  2366. Parameters to be initialized are retrieved by
  2367. <function>params_xxx()</function> macros. To allocate
  2368. buffer, you can call a helper function,
  2369. <informalexample>
  2370. <programlisting>
  2371. <![CDATA[
  2372. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  2373. ]]>
  2374. </programlisting>
  2375. </informalexample>
  2376. <function>snd_pcm_lib_malloc_pages()</function> is available
  2377. only when the DMA buffers have been pre-allocated.
  2378. See the section <link
  2379. linkend="buffer-and-memory-buffer-types"><citetitle>
  2380. Buffer Types</citetitle></link> for more details.
  2381. </para>
  2382. <para>
  2383. Note that this and <structfield>prepare</structfield> callbacks
  2384. may be called multiple times per initialization.
  2385. For example, the OSS emulation may
  2386. call these callbacks at each change via its ioctl.
  2387. </para>
  2388. <para>
  2389. Thus, you need to be careful not to allocate the same buffers
  2390. many times, which will lead to memory leaks! Calling the
  2391. helper function above many times is OK. It will release the
  2392. previous buffer automatically when it was already allocated.
  2393. </para>
  2394. <para>
  2395. Another note is that this callback is non-atomic
  2396. (schedulable) as default, i.e. when no
  2397. <structfield>nonatomic</structfield> flag set.
  2398. This is important, because the
  2399. <structfield>trigger</structfield> callback
  2400. is atomic (non-schedulable). That is, mutexes or any
  2401. schedule-related functions are not available in
  2402. <structfield>trigger</structfield> callback.
  2403. Please see the subsection
  2404. <link linkend="pcm-interface-atomicity"><citetitle>
  2405. Atomicity</citetitle></link> for details.
  2406. </para>
  2407. </section>
  2408. <section id="pcm-interface-operators-hw-free-callback">
  2409. <title>hw_free callback</title>
  2410. <para>
  2411. <informalexample>
  2412. <programlisting>
  2413. <![CDATA[
  2414. static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
  2415. ]]>
  2416. </programlisting>
  2417. </informalexample>
  2418. </para>
  2419. <para>
  2420. This is called to release the resources allocated via
  2421. <structfield>hw_params</structfield>. For example, releasing the
  2422. buffer via
  2423. <function>snd_pcm_lib_malloc_pages()</function> is done by
  2424. calling the following:
  2425. <informalexample>
  2426. <programlisting>
  2427. <![CDATA[
  2428. snd_pcm_lib_free_pages(substream);
  2429. ]]>
  2430. </programlisting>
  2431. </informalexample>
  2432. </para>
  2433. <para>
  2434. This function is always called before the close callback is called.
  2435. Also, the callback may be called multiple times, too.
  2436. Keep track whether the resource was already released.
  2437. </para>
  2438. </section>
  2439. <section id="pcm-interface-operators-prepare-callback">
  2440. <title>prepare callback</title>
  2441. <para>
  2442. <informalexample>
  2443. <programlisting>
  2444. <![CDATA[
  2445. static int snd_xxx_prepare(struct snd_pcm_substream *substream);
  2446. ]]>
  2447. </programlisting>
  2448. </informalexample>
  2449. </para>
  2450. <para>
  2451. This callback is called when the pcm is
  2452. <quote>prepared</quote>. You can set the format type, sample
  2453. rate, etc. here. The difference from
  2454. <structfield>hw_params</structfield> is that the
  2455. <structfield>prepare</structfield> callback will be called each
  2456. time
  2457. <function>snd_pcm_prepare()</function> is called, i.e. when
  2458. recovering after underruns, etc.
  2459. </para>
  2460. <para>
  2461. Note that this callback is now non-atomic.
  2462. You can use schedule-related functions safely in this callback.
  2463. </para>
  2464. <para>
  2465. In this and the following callbacks, you can refer to the
  2466. values via the runtime record,
  2467. substream-&gt;runtime.
  2468. For example, to get the current
  2469. rate, format or channels, access to
  2470. runtime-&gt;rate,
  2471. runtime-&gt;format or
  2472. runtime-&gt;channels, respectively.
  2473. The physical address of the allocated buffer is set to
  2474. runtime-&gt;dma_area. The buffer and period sizes are
  2475. in runtime-&gt;buffer_size and runtime-&gt;period_size,
  2476. respectively.
  2477. </para>
  2478. <para>
  2479. Be careful that this callback will be called many times at
  2480. each setup, too.
  2481. </para>
  2482. </section>
  2483. <section id="pcm-interface-operators-trigger-callback">
  2484. <title>trigger callback</title>
  2485. <para>
  2486. <informalexample>
  2487. <programlisting>
  2488. <![CDATA[
  2489. static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);
  2490. ]]>
  2491. </programlisting>
  2492. </informalexample>
  2493. This is called when the pcm is started, stopped or paused.
  2494. </para>
  2495. <para>
  2496. Which action is specified in the second argument,
  2497. <constant>SNDRV_PCM_TRIGGER_XXX</constant> in
  2498. <filename>&lt;sound/pcm.h&gt;</filename>. At least,
  2499. the <constant>START</constant> and <constant>STOP</constant>
  2500. commands must be defined in this callback.
  2501. <informalexample>
  2502. <programlisting>
  2503. <![CDATA[
  2504. switch (cmd) {
  2505. case SNDRV_PCM_TRIGGER_START:
  2506. /* do something to start the PCM engine */
  2507. break;
  2508. case SNDRV_PCM_TRIGGER_STOP:
  2509. /* do something to stop the PCM engine */
  2510. break;
  2511. default:
  2512. return -EINVAL;
  2513. }
  2514. ]]>
  2515. </programlisting>
  2516. </informalexample>
  2517. </para>
  2518. <para>
  2519. When the pcm supports the pause operation (given in the info
  2520. field of the hardware table), the <constant>PAUSE_PUSH</constant>
  2521. and <constant>PAUSE_RELEASE</constant> commands must be
  2522. handled here, too. The former is the command to pause the pcm,
  2523. and the latter to restart the pcm again.
  2524. </para>
  2525. <para>
  2526. When the pcm supports the suspend/resume operation,
  2527. regardless of full or partial suspend/resume support,
  2528. the <constant>SUSPEND</constant> and <constant>RESUME</constant>
  2529. commands must be handled, too.
  2530. These commands are issued when the power-management status is
  2531. changed. Obviously, the <constant>SUSPEND</constant> and
  2532. <constant>RESUME</constant> commands
  2533. suspend and resume the pcm substream, and usually, they
  2534. are identical to the <constant>STOP</constant> and
  2535. <constant>START</constant> commands, respectively.
  2536. See the <link linkend="power-management"><citetitle>
  2537. Power Management</citetitle></link> section for details.
  2538. </para>
  2539. <para>
  2540. As mentioned, this callback is atomic as default unless
  2541. <structfield>nonatomic</structfield> flag set, and
  2542. you cannot call functions which may sleep.
  2543. The trigger callback should be as minimal as possible,
  2544. just really triggering the DMA. The other stuff should be
  2545. initialized hw_params and prepare callbacks properly
  2546. beforehand.
  2547. </para>
  2548. </section>
  2549. <section id="pcm-interface-operators-pointer-callback">
  2550. <title>pointer callback</title>
  2551. <para>
  2552. <informalexample>
  2553. <programlisting>
  2554. <![CDATA[
  2555. static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)
  2556. ]]>
  2557. </programlisting>
  2558. </informalexample>
  2559. This callback is called when the PCM middle layer inquires
  2560. the current hardware position on the buffer. The position must
  2561. be returned in frames,
  2562. ranging from 0 to buffer_size - 1.
  2563. </para>
  2564. <para>
  2565. This is called usually from the buffer-update routine in the
  2566. pcm middle layer, which is invoked when
  2567. <function>snd_pcm_period_elapsed()</function> is called in the
  2568. interrupt routine. Then the pcm middle layer updates the
  2569. position and calculates the available space, and wakes up the
  2570. sleeping poll threads, etc.
  2571. </para>
  2572. <para>
  2573. This callback is also atomic as default.
  2574. </para>
  2575. </section>
  2576. <section id="pcm-interface-operators-copy-silence">
  2577. <title>copy and silence callbacks</title>
  2578. <para>
  2579. These callbacks are not mandatory, and can be omitted in
  2580. most cases. These callbacks are used when the hardware buffer
  2581. cannot be in the normal memory space. Some chips have their
  2582. own buffer on the hardware which is not mappable. In such a
  2583. case, you have to transfer the data manually from the memory
  2584. buffer to the hardware buffer. Or, if the buffer is
  2585. non-contiguous on both physical and virtual memory spaces,
  2586. these callbacks must be defined, too.
  2587. </para>
  2588. <para>
  2589. If these two callbacks are defined, copy and set-silence
  2590. operations are done by them. The detailed will be described in
  2591. the later section <link
  2592. linkend="buffer-and-memory"><citetitle>Buffer and Memory
  2593. Management</citetitle></link>.
  2594. </para>
  2595. </section>
  2596. <section id="pcm-interface-operators-ack">
  2597. <title>ack callback</title>
  2598. <para>
  2599. This callback is also not mandatory. This callback is called
  2600. when the appl_ptr is updated in read or write operations.
  2601. Some drivers like emu10k1-fx and cs46xx need to track the
  2602. current appl_ptr for the internal buffer, and this callback
  2603. is useful only for such a purpose.
  2604. </para>
  2605. <para>
  2606. This callback is atomic as default.
  2607. </para>
  2608. </section>
  2609. <section id="pcm-interface-operators-page-callback">
  2610. <title>page callback</title>
  2611. <para>
  2612. This callback is optional too. This callback is used
  2613. mainly for non-contiguous buffers. The mmap calls this
  2614. callback to get the page address. Some examples will be
  2615. explained in the later section <link
  2616. linkend="buffer-and-memory"><citetitle>Buffer and Memory
  2617. Management</citetitle></link>, too.
  2618. </para>
  2619. </section>
  2620. </section>
  2621. <section id="pcm-interface-interrupt-handler">
  2622. <title>Interrupt Handler</title>
  2623. <para>
  2624. The rest of pcm stuff is the PCM interrupt handler. The
  2625. role of PCM interrupt handler in the sound driver is to update
  2626. the buffer position and to tell the PCM middle layer when the
  2627. buffer position goes across the prescribed period size. To
  2628. inform this, call the <function>snd_pcm_period_elapsed()</function>
  2629. function.
  2630. </para>
  2631. <para>
  2632. There are several types of sound chips to generate the interrupts.
  2633. </para>
  2634. <section id="pcm-interface-interrupt-handler-boundary">
  2635. <title>Interrupts at the period (fragment) boundary</title>
  2636. <para>
  2637. This is the most frequently found type: the hardware
  2638. generates an interrupt at each period boundary.
  2639. In this case, you can call
  2640. <function>snd_pcm_period_elapsed()</function> at each
  2641. interrupt.
  2642. </para>
  2643. <para>
  2644. <function>snd_pcm_period_elapsed()</function> takes the
  2645. substream pointer as its argument. Thus, you need to keep the
  2646. substream pointer accessible from the chip instance. For
  2647. example, define substream field in the chip record to hold the
  2648. current running substream pointer, and set the pointer value
  2649. at open callback (and reset at close callback).
  2650. </para>
  2651. <para>
  2652. If you acquire a spinlock in the interrupt handler, and the
  2653. lock is used in other pcm callbacks, too, then you have to
  2654. release the lock before calling
  2655. <function>snd_pcm_period_elapsed()</function>, because
  2656. <function>snd_pcm_period_elapsed()</function> calls other pcm
  2657. callbacks inside.
  2658. </para>
  2659. <para>
  2660. Typical code would be like:
  2661. <example>
  2662. <title>Interrupt Handler Case #1</title>
  2663. <programlisting>
  2664. <![CDATA[
  2665. static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
  2666. {
  2667. struct mychip *chip = dev_id;
  2668. spin_lock(&chip->lock);
  2669. ....
  2670. if (pcm_irq_invoked(chip)) {
  2671. /* call updater, unlock before it */
  2672. spin_unlock(&chip->lock);
  2673. snd_pcm_period_elapsed(chip->substream);
  2674. spin_lock(&chip->lock);
  2675. /* acknowledge the interrupt if necessary */
  2676. }
  2677. ....
  2678. spin_unlock(&chip->lock);
  2679. return IRQ_HANDLED;
  2680. }
  2681. ]]>
  2682. </programlisting>
  2683. </example>
  2684. </para>
  2685. </section>
  2686. <section id="pcm-interface-interrupt-handler-timer">
  2687. <title>High frequency timer interrupts</title>
  2688. <para>
  2689. This happens when the hardware doesn't generate interrupts
  2690. at the period boundary but issues timer interrupts at a fixed
  2691. timer rate (e.g. es1968 or ymfpci drivers).
  2692. In this case, you need to check the current hardware
  2693. position and accumulate the processed sample length at each
  2694. interrupt. When the accumulated size exceeds the period
  2695. size, call
  2696. <function>snd_pcm_period_elapsed()</function> and reset the
  2697. accumulator.
  2698. </para>
  2699. <para>
  2700. Typical code would be like the following.
  2701. <example>
  2702. <title>Interrupt Handler Case #2</title>
  2703. <programlisting>
  2704. <![CDATA[
  2705. static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
  2706. {
  2707. struct mychip *chip = dev_id;
  2708. spin_lock(&chip->lock);
  2709. ....
  2710. if (pcm_irq_invoked(chip)) {
  2711. unsigned int last_ptr, size;
  2712. /* get the current hardware pointer (in frames) */
  2713. last_ptr = get_hw_ptr(chip);
  2714. /* calculate the processed frames since the
  2715. * last update
  2716. */
  2717. if (last_ptr < chip->last_ptr)
  2718. size = runtime->buffer_size + last_ptr
  2719. - chip->last_ptr;
  2720. else
  2721. size = last_ptr - chip->last_ptr;
  2722. /* remember the last updated point */
  2723. chip->last_ptr = last_ptr;
  2724. /* accumulate the size */
  2725. chip->size += size;
  2726. /* over the period boundary? */
  2727. if (chip->size >= runtime->period_size) {
  2728. /* reset the accumulator */
  2729. chip->size %= runtime->period_size;
  2730. /* call updater */
  2731. spin_unlock(&chip->lock);
  2732. snd_pcm_period_elapsed(substream);
  2733. spin_lock(&chip->lock);
  2734. }
  2735. /* acknowledge the interrupt if necessary */
  2736. }
  2737. ....
  2738. spin_unlock(&chip->lock);
  2739. return IRQ_HANDLED;
  2740. }
  2741. ]]>
  2742. </programlisting>
  2743. </example>
  2744. </para>
  2745. </section>
  2746. <section id="pcm-interface-interrupt-handler-both">
  2747. <title>On calling <function>snd_pcm_period_elapsed()</function></title>
  2748. <para>
  2749. In both cases, even if more than one period are elapsed, you
  2750. don't have to call
  2751. <function>snd_pcm_period_elapsed()</function> many times. Call
  2752. only once. And the pcm layer will check the current hardware
  2753. pointer and update to the latest status.
  2754. </para>
  2755. </section>
  2756. </section>
  2757. <section id="pcm-interface-atomicity">
  2758. <title>Atomicity</title>
  2759. <para>
  2760. One of the most important (and thus difficult to debug) problems
  2761. in kernel programming are race conditions.
  2762. In the Linux kernel, they are usually avoided via spin-locks, mutexes
  2763. or semaphores. In general, if a race condition can happen
  2764. in an interrupt handler, it has to be managed atomically, and you
  2765. have to use a spinlock to protect the critical session. If the
  2766. critical section is not in interrupt handler code and
  2767. if taking a relatively long time to execute is acceptable, you
  2768. should use mutexes or semaphores instead.
  2769. </para>
  2770. <para>
  2771. As already seen, some pcm callbacks are atomic and some are
  2772. not. For example, the <parameter>hw_params</parameter> callback is
  2773. non-atomic, while <parameter>trigger</parameter> callback is
  2774. atomic. This means, the latter is called already in a spinlock
  2775. held by the PCM middle layer. Please take this atomicity into
  2776. account when you choose a locking scheme in the callbacks.
  2777. </para>
  2778. <para>
  2779. In the atomic callbacks, you cannot use functions which may call
  2780. <function>schedule</function> or go to
  2781. <function>sleep</function>. Semaphores and mutexes can sleep,
  2782. and hence they cannot be used inside the atomic callbacks
  2783. (e.g. <parameter>trigger</parameter> callback).
  2784. To implement some delay in such a callback, please use
  2785. <function>udelay()</function> or <function>mdelay()</function>.
  2786. </para>
  2787. <para>
  2788. All three atomic callbacks (trigger, pointer, and ack) are
  2789. called with local interrupts disabled.
  2790. </para>
  2791. <para>
  2792. The recent changes in PCM core code, however, allow all PCM
  2793. operations to be non-atomic. This assumes that the all caller
  2794. sides are in non-atomic contexts. For example, the function
  2795. <function>snd_pcm_period_elapsed()</function> is called
  2796. typically from the interrupt handler. But, if you set up the
  2797. driver to use a threaded interrupt handler, this call can be in
  2798. non-atomic context, too. In such a case, you can set
  2799. <structfield>nonatomic</structfield> filed of
  2800. <structname>snd_pcm</structname> object after creating it.
  2801. When this flag is set, mutex and rwsem are used internally in
  2802. the PCM core instead of spin and rwlocks, so that you can call
  2803. all PCM functions safely in a non-atomic context.
  2804. </para>
  2805. </section>
  2806. <section id="pcm-interface-constraints">
  2807. <title>Constraints</title>
  2808. <para>
  2809. If your chip supports unconventional sample rates, or only the
  2810. limited samples, you need to set a constraint for the
  2811. condition.
  2812. </para>
  2813. <para>
  2814. For example, in order to restrict the sample rates in the some
  2815. supported values, use
  2816. <function>snd_pcm_hw_constraint_list()</function>.
  2817. You need to call this function in the open callback.
  2818. <example>
  2819. <title>Example of Hardware Constraints</title>
  2820. <programlisting>
  2821. <![CDATA[
  2822. static unsigned int rates[] =
  2823. {4000, 10000, 22050, 44100};
  2824. static struct snd_pcm_hw_constraint_list constraints_rates = {
  2825. .count = ARRAY_SIZE(rates),
  2826. .list = rates,
  2827. .mask = 0,
  2828. };
  2829. static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
  2830. {
  2831. int err;
  2832. ....
  2833. err = snd_pcm_hw_constraint_list(substream->runtime, 0,
  2834. SNDRV_PCM_HW_PARAM_RATE,
  2835. &constraints_rates);
  2836. if (err < 0)
  2837. return err;
  2838. ....
  2839. }
  2840. ]]>
  2841. </programlisting>
  2842. </example>
  2843. </para>
  2844. <para>
  2845. There are many different constraints.
  2846. Look at <filename>sound/pcm.h</filename> for a complete list.
  2847. You can even define your own constraint rules.
  2848. For example, let's suppose my_chip can manage a substream of 1 channel
  2849. if and only if the format is S16_LE, otherwise it supports any format
  2850. specified in the <structname>snd_pcm_hardware</structname> structure (or in any
  2851. other constraint_list). You can build a rule like this:
  2852. <example>
  2853. <title>Example of Hardware Constraints for Channels</title>
  2854. <programlisting>
  2855. <![CDATA[
  2856. static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
  2857. struct snd_pcm_hw_rule *rule)
  2858. {
  2859. struct snd_interval *c = hw_param_interval(params,
  2860. SNDRV_PCM_HW_PARAM_CHANNELS);
  2861. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  2862. struct snd_interval ch;
  2863. snd_interval_any(&ch);
  2864. if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
  2865. ch.min = ch.max = 1;
  2866. ch.integer = 1;
  2867. return snd_interval_refine(c, &ch);
  2868. }
  2869. return 0;
  2870. }
  2871. ]]>
  2872. </programlisting>
  2873. </example>
  2874. </para>
  2875. <para>
  2876. Then you need to call this function to add your rule:
  2877. <informalexample>
  2878. <programlisting>
  2879. <![CDATA[
  2880. snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  2881. hw_rule_channels_by_format, NULL,
  2882. SNDRV_PCM_HW_PARAM_FORMAT, -1);
  2883. ]]>
  2884. </programlisting>
  2885. </informalexample>
  2886. </para>
  2887. <para>
  2888. The rule function is called when an application sets the PCM
  2889. format, and it refines the number of channels accordingly.
  2890. But an application may set the number of channels before
  2891. setting the format. Thus you also need to define the inverse rule:
  2892. <example>
  2893. <title>Example of Hardware Constraints for Formats</title>
  2894. <programlisting>
  2895. <![CDATA[
  2896. static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
  2897. struct snd_pcm_hw_rule *rule)
  2898. {
  2899. struct snd_interval *c = hw_param_interval(params,
  2900. SNDRV_PCM_HW_PARAM_CHANNELS);
  2901. struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  2902. struct snd_mask fmt;
  2903. snd_mask_any(&fmt); /* Init the struct */
  2904. if (c->min < 2) {
  2905. fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE;
  2906. return snd_mask_refine(f, &fmt);
  2907. }
  2908. return 0;
  2909. }
  2910. ]]>
  2911. </programlisting>
  2912. </example>
  2913. </para>
  2914. <para>
  2915. ...and in the open callback:
  2916. <informalexample>
  2917. <programlisting>
  2918. <![CDATA[
  2919. snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
  2920. hw_rule_format_by_channels, NULL,
  2921. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  2922. ]]>
  2923. </programlisting>
  2924. </informalexample>
  2925. </para>
  2926. <para>
  2927. I won't give more details here, rather I
  2928. would like to say, <quote>Luke, use the source.</quote>
  2929. </para>
  2930. </section>
  2931. </chapter>
  2932. <!-- ****************************************************** -->
  2933. <!-- Control Interface -->
  2934. <!-- ****************************************************** -->
  2935. <chapter id="control-interface">
  2936. <title>Control Interface</title>
  2937. <section id="control-interface-general">
  2938. <title>General</title>
  2939. <para>
  2940. The control interface is used widely for many switches,
  2941. sliders, etc. which are accessed from user-space. Its most
  2942. important use is the mixer interface. In other words, since ALSA
  2943. 0.9.x, all the mixer stuff is implemented on the control kernel API.
  2944. </para>
  2945. <para>
  2946. ALSA has a well-defined AC97 control module. If your chip
  2947. supports only the AC97 and nothing else, you can skip this
  2948. section.
  2949. </para>
  2950. <para>
  2951. The control API is defined in
  2952. <filename>&lt;sound/control.h&gt;</filename>.
  2953. Include this file if you want to add your own controls.
  2954. </para>
  2955. </section>
  2956. <section id="control-interface-definition">
  2957. <title>Definition of Controls</title>
  2958. <para>
  2959. To create a new control, you need to define the
  2960. following three
  2961. callbacks: <structfield>info</structfield>,
  2962. <structfield>get</structfield> and
  2963. <structfield>put</structfield>. Then, define a
  2964. struct <structname>snd_kcontrol_new</structname> record, such as:
  2965. <example>
  2966. <title>Definition of a Control</title>
  2967. <programlisting>
  2968. <![CDATA[
  2969. static struct snd_kcontrol_new my_control = {
  2970. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2971. .name = "PCM Playback Switch",
  2972. .index = 0,
  2973. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
  2974. .private_value = 0xffff,
  2975. .info = my_control_info,
  2976. .get = my_control_get,
  2977. .put = my_control_put
  2978. };
  2979. ]]>
  2980. </programlisting>
  2981. </example>
  2982. </para>
  2983. <para>
  2984. The <structfield>iface</structfield> field specifies the control
  2985. type, <constant>SNDRV_CTL_ELEM_IFACE_XXX</constant>, which
  2986. is usually <constant>MIXER</constant>.
  2987. Use <constant>CARD</constant> for global controls that are not
  2988. logically part of the mixer.
  2989. If the control is closely associated with some specific device on
  2990. the sound card, use <constant>HWDEP</constant>,
  2991. <constant>PCM</constant>, <constant>RAWMIDI</constant>,
  2992. <constant>TIMER</constant>, or <constant>SEQUENCER</constant>, and
  2993. specify the device number with the
  2994. <structfield>device</structfield> and
  2995. <structfield>subdevice</structfield> fields.
  2996. </para>
  2997. <para>
  2998. The <structfield>name</structfield> is the name identifier
  2999. string. Since ALSA 0.9.x, the control name is very important,
  3000. because its role is classified from its name. There are
  3001. pre-defined standard control names. The details are described in
  3002. the <link linkend="control-interface-control-names"><citetitle>
  3003. Control Names</citetitle></link> subsection.
  3004. </para>
  3005. <para>
  3006. The <structfield>index</structfield> field holds the index number
  3007. of this control. If there are several different controls with
  3008. the same name, they can be distinguished by the index
  3009. number. This is the case when
  3010. several codecs exist on the card. If the index is zero, you can
  3011. omit the definition above.
  3012. </para>
  3013. <para>
  3014. The <structfield>access</structfield> field contains the access
  3015. type of this control. Give the combination of bit masks,
  3016. <constant>SNDRV_CTL_ELEM_ACCESS_XXX</constant>, there.
  3017. The details will be explained in
  3018. the <link linkend="control-interface-access-flags"><citetitle>
  3019. Access Flags</citetitle></link> subsection.
  3020. </para>
  3021. <para>
  3022. The <structfield>private_value</structfield> field contains
  3023. an arbitrary long integer value for this record. When using
  3024. the generic <structfield>info</structfield>,
  3025. <structfield>get</structfield> and
  3026. <structfield>put</structfield> callbacks, you can pass a value
  3027. through this field. If several small numbers are necessary, you can
  3028. combine them in bitwise. Or, it's possible to give a pointer
  3029. (casted to unsigned long) of some record to this field, too.
  3030. </para>
  3031. <para>
  3032. The <structfield>tlv</structfield> field can be used to provide
  3033. metadata about the control; see the
  3034. <link linkend="control-interface-tlv">
  3035. <citetitle>Metadata</citetitle></link> subsection.
  3036. </para>
  3037. <para>
  3038. The other three are
  3039. <link linkend="control-interface-callbacks"><citetitle>
  3040. callback functions</citetitle></link>.
  3041. </para>
  3042. </section>
  3043. <section id="control-interface-control-names">
  3044. <title>Control Names</title>
  3045. <para>
  3046. There are some standards to define the control names. A
  3047. control is usually defined from the three parts as
  3048. <quote>SOURCE DIRECTION FUNCTION</quote>.
  3049. </para>
  3050. <para>
  3051. The first, <constant>SOURCE</constant>, specifies the source
  3052. of the control, and is a string such as <quote>Master</quote>,
  3053. <quote>PCM</quote>, <quote>CD</quote> and
  3054. <quote>Line</quote>. There are many pre-defined sources.
  3055. </para>
  3056. <para>
  3057. The second, <constant>DIRECTION</constant>, is one of the
  3058. following strings according to the direction of the control:
  3059. <quote>Playback</quote>, <quote>Capture</quote>, <quote>Bypass
  3060. Playback</quote> and <quote>Bypass Capture</quote>. Or, it can
  3061. be omitted, meaning both playback and capture directions.
  3062. </para>
  3063. <para>
  3064. The third, <constant>FUNCTION</constant>, is one of the
  3065. following strings according to the function of the control:
  3066. <quote>Switch</quote>, <quote>Volume</quote> and
  3067. <quote>Route</quote>.
  3068. </para>
  3069. <para>
  3070. The example of control names are, thus, <quote>Master Capture
  3071. Switch</quote> or <quote>PCM Playback Volume</quote>.
  3072. </para>
  3073. <para>
  3074. There are some exceptions:
  3075. </para>
  3076. <section id="control-interface-control-names-global">
  3077. <title>Global capture and playback</title>
  3078. <para>
  3079. <quote>Capture Source</quote>, <quote>Capture Switch</quote>
  3080. and <quote>Capture Volume</quote> are used for the global
  3081. capture (input) source, switch and volume. Similarly,
  3082. <quote>Playback Switch</quote> and <quote>Playback
  3083. Volume</quote> are used for the global output gain switch and
  3084. volume.
  3085. </para>
  3086. </section>
  3087. <section id="control-interface-control-names-tone">
  3088. <title>Tone-controls</title>
  3089. <para>
  3090. tone-control switch and volumes are specified like
  3091. <quote>Tone Control - XXX</quote>, e.g. <quote>Tone Control -
  3092. Switch</quote>, <quote>Tone Control - Bass</quote>,
  3093. <quote>Tone Control - Center</quote>.
  3094. </para>
  3095. </section>
  3096. <section id="control-interface-control-names-3d">
  3097. <title>3D controls</title>
  3098. <para>
  3099. 3D-control switches and volumes are specified like <quote>3D
  3100. Control - XXX</quote>, e.g. <quote>3D Control -
  3101. Switch</quote>, <quote>3D Control - Center</quote>, <quote>3D
  3102. Control - Space</quote>.
  3103. </para>
  3104. </section>
  3105. <section id="control-interface-control-names-mic">
  3106. <title>Mic boost</title>
  3107. <para>
  3108. Mic-boost switch is set as <quote>Mic Boost</quote> or
  3109. <quote>Mic Boost (6dB)</quote>.
  3110. </para>
  3111. <para>
  3112. More precise information can be found in
  3113. <filename>Documentation/sound/alsa/ControlNames.txt</filename>.
  3114. </para>
  3115. </section>
  3116. </section>
  3117. <section id="control-interface-access-flags">
  3118. <title>Access Flags</title>
  3119. <para>
  3120. The access flag is the bitmask which specifies the access type
  3121. of the given control. The default access type is
  3122. <constant>SNDRV_CTL_ELEM_ACCESS_READWRITE</constant>,
  3123. which means both read and write are allowed to this control.
  3124. When the access flag is omitted (i.e. = 0), it is
  3125. considered as <constant>READWRITE</constant> access as default.
  3126. </para>
  3127. <para>
  3128. When the control is read-only, pass
  3129. <constant>SNDRV_CTL_ELEM_ACCESS_READ</constant> instead.
  3130. In this case, you don't have to define
  3131. the <structfield>put</structfield> callback.
  3132. Similarly, when the control is write-only (although it's a rare
  3133. case), you can use the <constant>WRITE</constant> flag instead, and
  3134. you don't need the <structfield>get</structfield> callback.
  3135. </para>
  3136. <para>
  3137. If the control value changes frequently (e.g. the VU meter),
  3138. <constant>VOLATILE</constant> flag should be given. This means
  3139. that the control may be changed without
  3140. <link linkend="control-interface-change-notification"><citetitle>
  3141. notification</citetitle></link>. Applications should poll such
  3142. a control constantly.
  3143. </para>
  3144. <para>
  3145. When the control is inactive, set
  3146. the <constant>INACTIVE</constant> flag, too.
  3147. There are <constant>LOCK</constant> and
  3148. <constant>OWNER</constant> flags to change the write
  3149. permissions.
  3150. </para>
  3151. </section>
  3152. <section id="control-interface-callbacks">
  3153. <title>Callbacks</title>
  3154. <section id="control-interface-callbacks-info">
  3155. <title>info callback</title>
  3156. <para>
  3157. The <structfield>info</structfield> callback is used to get
  3158. detailed information on this control. This must store the
  3159. values of the given struct <structname>snd_ctl_elem_info</structname>
  3160. object. For example, for a boolean control with a single
  3161. element:
  3162. <example>
  3163. <title>Example of info callback</title>
  3164. <programlisting>
  3165. <![CDATA[
  3166. static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol,
  3167. struct snd_ctl_elem_info *uinfo)
  3168. {
  3169. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  3170. uinfo->count = 1;
  3171. uinfo->value.integer.min = 0;
  3172. uinfo->value.integer.max = 1;
  3173. return 0;
  3174. }
  3175. ]]>
  3176. </programlisting>
  3177. </example>
  3178. </para>
  3179. <para>
  3180. The <structfield>type</structfield> field specifies the type
  3181. of the control. There are <constant>BOOLEAN</constant>,
  3182. <constant>INTEGER</constant>, <constant>ENUMERATED</constant>,
  3183. <constant>BYTES</constant>, <constant>IEC958</constant> and
  3184. <constant>INTEGER64</constant>. The
  3185. <structfield>count</structfield> field specifies the
  3186. number of elements in this control. For example, a stereo
  3187. volume would have count = 2. The
  3188. <structfield>value</structfield> field is a union, and
  3189. the values stored are depending on the type. The boolean and
  3190. integer types are identical.
  3191. </para>
  3192. <para>
  3193. The enumerated type is a bit different from others. You'll
  3194. need to set the string for the currently given item index.
  3195. <informalexample>
  3196. <programlisting>
  3197. <![CDATA[
  3198. static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
  3199. struct snd_ctl_elem_info *uinfo)
  3200. {
  3201. static char *texts[4] = {
  3202. "First", "Second", "Third", "Fourth"
  3203. };
  3204. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3205. uinfo->count = 1;
  3206. uinfo->value.enumerated.items = 4;
  3207. if (uinfo->value.enumerated.item > 3)
  3208. uinfo->value.enumerated.item = 3;
  3209. strcpy(uinfo->value.enumerated.name,
  3210. texts[uinfo->value.enumerated.item]);
  3211. return 0;
  3212. }
  3213. ]]>
  3214. </programlisting>
  3215. </informalexample>
  3216. </para>
  3217. <para>
  3218. The above callback can be simplified with a helper function,
  3219. <function>snd_ctl_enum_info</function>. The final code
  3220. looks like below.
  3221. (You can pass ARRAY_SIZE(texts) instead of 4 in the third
  3222. argument; it's a matter of taste.)
  3223. <informalexample>
  3224. <programlisting>
  3225. <![CDATA[
  3226. static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
  3227. struct snd_ctl_elem_info *uinfo)
  3228. {
  3229. static char *texts[4] = {
  3230. "First", "Second", "Third", "Fourth"
  3231. };
  3232. return snd_ctl_enum_info(uinfo, 1, 4, texts);
  3233. }
  3234. ]]>
  3235. </programlisting>
  3236. </informalexample>
  3237. </para>
  3238. <para>
  3239. Some common info callbacks are available for your convenience:
  3240. <function>snd_ctl_boolean_mono_info()</function> and
  3241. <function>snd_ctl_boolean_stereo_info()</function>.
  3242. Obviously, the former is an info callback for a mono channel
  3243. boolean item, just like <function>snd_myctl_mono_info</function>
  3244. above, and the latter is for a stereo channel boolean item.
  3245. </para>
  3246. </section>
  3247. <section id="control-interface-callbacks-get">
  3248. <title>get callback</title>
  3249. <para>
  3250. This callback is used to read the current value of the
  3251. control and to return to user-space.
  3252. </para>
  3253. <para>
  3254. For example,
  3255. <example>
  3256. <title>Example of get callback</title>
  3257. <programlisting>
  3258. <![CDATA[
  3259. static int snd_myctl_get(struct snd_kcontrol *kcontrol,
  3260. struct snd_ctl_elem_value *ucontrol)
  3261. {
  3262. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  3263. ucontrol->value.integer.value[0] = get_some_value(chip);
  3264. return 0;
  3265. }
  3266. ]]>
  3267. </programlisting>
  3268. </example>
  3269. </para>
  3270. <para>
  3271. The <structfield>value</structfield> field depends on
  3272. the type of control as well as on the info callback. For example,
  3273. the sb driver uses this field to store the register offset,
  3274. the bit-shift and the bit-mask. The
  3275. <structfield>private_value</structfield> field is set as follows:
  3276. <informalexample>
  3277. <programlisting>
  3278. <![CDATA[
  3279. .private_value = reg | (shift << 16) | (mask << 24)
  3280. ]]>
  3281. </programlisting>
  3282. </informalexample>
  3283. and is retrieved in callbacks like
  3284. <informalexample>
  3285. <programlisting>
  3286. <![CDATA[
  3287. static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol,
  3288. struct snd_ctl_elem_value *ucontrol)
  3289. {
  3290. int reg = kcontrol->private_value & 0xff;
  3291. int shift = (kcontrol->private_value >> 16) & 0xff;
  3292. int mask = (kcontrol->private_value >> 24) & 0xff;
  3293. ....
  3294. }
  3295. ]]>
  3296. </programlisting>
  3297. </informalexample>
  3298. </para>
  3299. <para>
  3300. In the <structfield>get</structfield> callback,
  3301. you have to fill all the elements if the
  3302. control has more than one elements,
  3303. i.e. <structfield>count</structfield> &gt; 1.
  3304. In the example above, we filled only one element
  3305. (<structfield>value.integer.value[0]</structfield>) since it's
  3306. assumed as <structfield>count</structfield> = 1.
  3307. </para>
  3308. </section>
  3309. <section id="control-interface-callbacks-put">
  3310. <title>put callback</title>
  3311. <para>
  3312. This callback is used to write a value from user-space.
  3313. </para>
  3314. <para>
  3315. For example,
  3316. <example>
  3317. <title>Example of put callback</title>
  3318. <programlisting>
  3319. <![CDATA[
  3320. static int snd_myctl_put(struct snd_kcontrol *kcontrol,
  3321. struct snd_ctl_elem_value *ucontrol)
  3322. {
  3323. struct mychip *chip = snd_kcontrol_chip(kcontrol);
  3324. int changed = 0;
  3325. if (chip->current_value !=
  3326. ucontrol->value.integer.value[0]) {
  3327. change_current_value(chip,
  3328. ucontrol->value.integer.value[0]);
  3329. changed = 1;
  3330. }
  3331. return changed;
  3332. }
  3333. ]]>
  3334. </programlisting>
  3335. </example>
  3336. As seen above, you have to return 1 if the value is
  3337. changed. If the value is not changed, return 0 instead.
  3338. If any fatal error happens, return a negative error code as
  3339. usual.
  3340. </para>
  3341. <para>
  3342. As in the <structfield>get</structfield> callback,
  3343. when the control has more than one elements,
  3344. all elements must be evaluated in this callback, too.
  3345. </para>
  3346. </section>
  3347. <section id="control-interface-callbacks-all">
  3348. <title>Callbacks are not atomic</title>
  3349. <para>
  3350. All these three callbacks are basically not atomic.
  3351. </para>
  3352. </section>
  3353. </section>
  3354. <section id="control-interface-constructor">
  3355. <title>Constructor</title>
  3356. <para>
  3357. When everything is ready, finally we can create a new
  3358. control. To create a control, there are two functions to be
  3359. called, <function>snd_ctl_new1()</function> and
  3360. <function>snd_ctl_add()</function>.
  3361. </para>
  3362. <para>
  3363. In the simplest way, you can do like this:
  3364. <informalexample>
  3365. <programlisting>
  3366. <![CDATA[
  3367. err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
  3368. if (err < 0)
  3369. return err;
  3370. ]]>
  3371. </programlisting>
  3372. </informalexample>
  3373. where <parameter>my_control</parameter> is the
  3374. struct <structname>snd_kcontrol_new</structname> object defined above, and chip
  3375. is the object pointer to be passed to
  3376. kcontrol-&gt;private_data
  3377. which can be referred to in callbacks.
  3378. </para>
  3379. <para>
  3380. <function>snd_ctl_new1()</function> allocates a new
  3381. <structname>snd_kcontrol</structname> instance,
  3382. and <function>snd_ctl_add</function> assigns the given
  3383. control component to the card.
  3384. </para>
  3385. </section>
  3386. <section id="control-interface-change-notification">
  3387. <title>Change Notification</title>
  3388. <para>
  3389. If you need to change and update a control in the interrupt
  3390. routine, you can call <function>snd_ctl_notify()</function>. For
  3391. example,
  3392. <informalexample>
  3393. <programlisting>
  3394. <![CDATA[
  3395. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);
  3396. ]]>
  3397. </programlisting>
  3398. </informalexample>
  3399. This function takes the card pointer, the event-mask, and the
  3400. control id pointer for the notification. The event-mask
  3401. specifies the types of notification, for example, in the above
  3402. example, the change of control values is notified.
  3403. The id pointer is the pointer of struct <structname>snd_ctl_elem_id</structname>
  3404. to be notified.
  3405. You can find some examples in <filename>es1938.c</filename> or
  3406. <filename>es1968.c</filename> for hardware volume interrupts.
  3407. </para>
  3408. </section>
  3409. <section id="control-interface-tlv">
  3410. <title>Metadata</title>
  3411. <para>
  3412. To provide information about the dB values of a mixer control, use
  3413. on of the <constant>DECLARE_TLV_xxx</constant> macros from
  3414. <filename>&lt;sound/tlv.h&gt;</filename> to define a variable
  3415. containing this information, set the<structfield>tlv.p
  3416. </structfield> field to point to this variable, and include the
  3417. <constant>SNDRV_CTL_ELEM_ACCESS_TLV_READ</constant> flag in the
  3418. <structfield>access</structfield> field; like this:
  3419. <informalexample>
  3420. <programlisting>
  3421. <![CDATA[
  3422. static DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0);
  3423. static struct snd_kcontrol_new my_control = {
  3424. ...
  3425. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  3426. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  3427. ...
  3428. .tlv.p = db_scale_my_control,
  3429. };
  3430. ]]>
  3431. </programlisting>
  3432. </informalexample>
  3433. </para>
  3434. <para>
  3435. The <function>DECLARE_TLV_DB_SCALE</function> macro defines
  3436. information about a mixer control where each step in the control's
  3437. value changes the dB value by a constant dB amount.
  3438. The first parameter is the name of the variable to be defined.
  3439. The second parameter is the minimum value, in units of 0.01 dB.
  3440. The third parameter is the step size, in units of 0.01 dB.
  3441. Set the fourth parameter to 1 if the minimum value actually mutes
  3442. the control.
  3443. </para>
  3444. <para>
  3445. The <function>DECLARE_TLV_DB_LINEAR</function> macro defines
  3446. information about a mixer control where the control's value affects
  3447. the output linearly.
  3448. The first parameter is the name of the variable to be defined.
  3449. The second parameter is the minimum value, in units of 0.01 dB.
  3450. The third parameter is the maximum value, in units of 0.01 dB.
  3451. If the minimum value mutes the control, set the second parameter to
  3452. <constant>TLV_DB_GAIN_MUTE</constant>.
  3453. </para>
  3454. </section>
  3455. </chapter>
  3456. <!-- ****************************************************** -->
  3457. <!-- API for AC97 Codec -->
  3458. <!-- ****************************************************** -->
  3459. <chapter id="api-ac97">
  3460. <title>API for AC97 Codec</title>
  3461. <section>
  3462. <title>General</title>
  3463. <para>
  3464. The ALSA AC97 codec layer is a well-defined one, and you don't
  3465. have to write much code to control it. Only low-level control
  3466. routines are necessary. The AC97 codec API is defined in
  3467. <filename>&lt;sound/ac97_codec.h&gt;</filename>.
  3468. </para>
  3469. </section>
  3470. <section id="api-ac97-example">
  3471. <title>Full Code Example</title>
  3472. <para>
  3473. <example>
  3474. <title>Example of AC97 Interface</title>
  3475. <programlisting>
  3476. <![CDATA[
  3477. struct mychip {
  3478. ....
  3479. struct snd_ac97 *ac97;
  3480. ....
  3481. };
  3482. static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
  3483. unsigned short reg)
  3484. {
  3485. struct mychip *chip = ac97->private_data;
  3486. ....
  3487. /* read a register value here from the codec */
  3488. return the_register_value;
  3489. }
  3490. static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
  3491. unsigned short reg, unsigned short val)
  3492. {
  3493. struct mychip *chip = ac97->private_data;
  3494. ....
  3495. /* write the given register value to the codec */
  3496. }
  3497. static int snd_mychip_ac97(struct mychip *chip)
  3498. {
  3499. struct snd_ac97_bus *bus;
  3500. struct snd_ac97_template ac97;
  3501. int err;
  3502. static struct snd_ac97_bus_ops ops = {
  3503. .write = snd_mychip_ac97_write,
  3504. .read = snd_mychip_ac97_read,
  3505. };
  3506. err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
  3507. if (err < 0)
  3508. return err;
  3509. memset(&ac97, 0, sizeof(ac97));
  3510. ac97.private_data = chip;
  3511. return snd_ac97_mixer(bus, &ac97, &chip->ac97);
  3512. }
  3513. ]]>
  3514. </programlisting>
  3515. </example>
  3516. </para>
  3517. </section>
  3518. <section id="api-ac97-constructor">
  3519. <title>Constructor</title>
  3520. <para>
  3521. To create an ac97 instance, first call <function>snd_ac97_bus</function>
  3522. with an <type>ac97_bus_ops_t</type> record with callback functions.
  3523. <informalexample>
  3524. <programlisting>
  3525. <![CDATA[
  3526. struct snd_ac97_bus *bus;
  3527. static struct snd_ac97_bus_ops ops = {
  3528. .write = snd_mychip_ac97_write,
  3529. .read = snd_mychip_ac97_read,
  3530. };
  3531. snd_ac97_bus(card, 0, &ops, NULL, &pbus);
  3532. ]]>
  3533. </programlisting>
  3534. </informalexample>
  3535. The bus record is shared among all belonging ac97 instances.
  3536. </para>
  3537. <para>
  3538. And then call <function>snd_ac97_mixer()</function> with an
  3539. struct <structname>snd_ac97_template</structname>
  3540. record together with the bus pointer created above.
  3541. <informalexample>
  3542. <programlisting>
  3543. <![CDATA[
  3544. struct snd_ac97_template ac97;
  3545. int err;
  3546. memset(&ac97, 0, sizeof(ac97));
  3547. ac97.private_data = chip;
  3548. snd_ac97_mixer(bus, &ac97, &chip->ac97);
  3549. ]]>
  3550. </programlisting>
  3551. </informalexample>
  3552. where chip-&gt;ac97 is a pointer to a newly created
  3553. <type>ac97_t</type> instance.
  3554. In this case, the chip pointer is set as the private data, so that
  3555. the read/write callback functions can refer to this chip instance.
  3556. This instance is not necessarily stored in the chip
  3557. record. If you need to change the register values from the
  3558. driver, or need the suspend/resume of ac97 codecs, keep this
  3559. pointer to pass to the corresponding functions.
  3560. </para>
  3561. </section>
  3562. <section id="api-ac97-callbacks">
  3563. <title>Callbacks</title>
  3564. <para>
  3565. The standard callbacks are <structfield>read</structfield> and
  3566. <structfield>write</structfield>. Obviously they
  3567. correspond to the functions for read and write accesses to the
  3568. hardware low-level codes.
  3569. </para>
  3570. <para>
  3571. The <structfield>read</structfield> callback returns the
  3572. register value specified in the argument.
  3573. <informalexample>
  3574. <programlisting>
  3575. <![CDATA[
  3576. static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
  3577. unsigned short reg)
  3578. {
  3579. struct mychip *chip = ac97->private_data;
  3580. ....
  3581. return the_register_value;
  3582. }
  3583. ]]>
  3584. </programlisting>
  3585. </informalexample>
  3586. Here, the chip can be cast from ac97-&gt;private_data.
  3587. </para>
  3588. <para>
  3589. Meanwhile, the <structfield>write</structfield> callback is
  3590. used to set the register value.
  3591. <informalexample>
  3592. <programlisting>
  3593. <![CDATA[
  3594. static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
  3595. unsigned short reg, unsigned short val)
  3596. ]]>
  3597. </programlisting>
  3598. </informalexample>
  3599. </para>
  3600. <para>
  3601. These callbacks are non-atomic like the control API callbacks.
  3602. </para>
  3603. <para>
  3604. There are also other callbacks:
  3605. <structfield>reset</structfield>,
  3606. <structfield>wait</structfield> and
  3607. <structfield>init</structfield>.
  3608. </para>
  3609. <para>
  3610. The <structfield>reset</structfield> callback is used to reset
  3611. the codec. If the chip requires a special kind of reset, you can
  3612. define this callback.
  3613. </para>
  3614. <para>
  3615. The <structfield>wait</structfield> callback is used to
  3616. add some waiting time in the standard initialization of the codec. If the
  3617. chip requires the extra waiting time, define this callback.
  3618. </para>
  3619. <para>
  3620. The <structfield>init</structfield> callback is used for
  3621. additional initialization of the codec.
  3622. </para>
  3623. </section>
  3624. <section id="api-ac97-updating-registers">
  3625. <title>Updating Registers in The Driver</title>
  3626. <para>
  3627. If you need to access to the codec from the driver, you can
  3628. call the following functions:
  3629. <function>snd_ac97_write()</function>,
  3630. <function>snd_ac97_read()</function>,
  3631. <function>snd_ac97_update()</function> and
  3632. <function>snd_ac97_update_bits()</function>.
  3633. </para>
  3634. <para>
  3635. Both <function>snd_ac97_write()</function> and
  3636. <function>snd_ac97_update()</function> functions are used to
  3637. set a value to the given register
  3638. (<constant>AC97_XXX</constant>). The difference between them is
  3639. that <function>snd_ac97_update()</function> doesn't write a
  3640. value if the given value has been already set, while
  3641. <function>snd_ac97_write()</function> always rewrites the
  3642. value.
  3643. <informalexample>
  3644. <programlisting>
  3645. <![CDATA[
  3646. snd_ac97_write(ac97, AC97_MASTER, 0x8080);
  3647. snd_ac97_update(ac97, AC97_MASTER, 0x8080);
  3648. ]]>
  3649. </programlisting>
  3650. </informalexample>
  3651. </para>
  3652. <para>
  3653. <function>snd_ac97_read()</function> is used to read the value
  3654. of the given register. For example,
  3655. <informalexample>
  3656. <programlisting>
  3657. <![CDATA[
  3658. value = snd_ac97_read(ac97, AC97_MASTER);
  3659. ]]>
  3660. </programlisting>
  3661. </informalexample>
  3662. </para>
  3663. <para>
  3664. <function>snd_ac97_update_bits()</function> is used to update
  3665. some bits in the given register.
  3666. <informalexample>
  3667. <programlisting>
  3668. <![CDATA[
  3669. snd_ac97_update_bits(ac97, reg, mask, value);
  3670. ]]>
  3671. </programlisting>
  3672. </informalexample>
  3673. </para>
  3674. <para>
  3675. Also, there is a function to change the sample rate (of a
  3676. given register such as
  3677. <constant>AC97_PCM_FRONT_DAC_RATE</constant>) when VRA or
  3678. DRA is supported by the codec:
  3679. <function>snd_ac97_set_rate()</function>.
  3680. <informalexample>
  3681. <programlisting>
  3682. <![CDATA[
  3683. snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);
  3684. ]]>
  3685. </programlisting>
  3686. </informalexample>
  3687. </para>
  3688. <para>
  3689. The following registers are available to set the rate:
  3690. <constant>AC97_PCM_MIC_ADC_RATE</constant>,
  3691. <constant>AC97_PCM_FRONT_DAC_RATE</constant>,
  3692. <constant>AC97_PCM_LR_ADC_RATE</constant>,
  3693. <constant>AC97_SPDIF</constant>. When
  3694. <constant>AC97_SPDIF</constant> is specified, the register is
  3695. not really changed but the corresponding IEC958 status bits will
  3696. be updated.
  3697. </para>
  3698. </section>
  3699. <section id="api-ac97-clock-adjustment">
  3700. <title>Clock Adjustment</title>
  3701. <para>
  3702. In some chips, the clock of the codec isn't 48000 but using a
  3703. PCI clock (to save a quartz!). In this case, change the field
  3704. bus-&gt;clock to the corresponding
  3705. value. For example, intel8x0
  3706. and es1968 drivers have their own function to read from the clock.
  3707. </para>
  3708. </section>
  3709. <section id="api-ac97-proc-files">
  3710. <title>Proc Files</title>
  3711. <para>
  3712. The ALSA AC97 interface will create a proc file such as
  3713. <filename>/proc/asound/card0/codec97#0/ac97#0-0</filename> and
  3714. <filename>ac97#0-0+regs</filename>. You can refer to these files to
  3715. see the current status and registers of the codec.
  3716. </para>
  3717. </section>
  3718. <section id="api-ac97-multiple-codecs">
  3719. <title>Multiple Codecs</title>
  3720. <para>
  3721. When there are several codecs on the same card, you need to
  3722. call <function>snd_ac97_mixer()</function> multiple times with
  3723. ac97.num=1 or greater. The <structfield>num</structfield> field
  3724. specifies the codec number.
  3725. </para>
  3726. <para>
  3727. If you set up multiple codecs, you either need to write
  3728. different callbacks for each codec or check
  3729. ac97-&gt;num in the callback routines.
  3730. </para>
  3731. </section>
  3732. </chapter>
  3733. <!-- ****************************************************** -->
  3734. <!-- MIDI (MPU401-UART) Interface -->
  3735. <!-- ****************************************************** -->
  3736. <chapter id="midi-interface">
  3737. <title>MIDI (MPU401-UART) Interface</title>
  3738. <section id="midi-interface-general">
  3739. <title>General</title>
  3740. <para>
  3741. Many soundcards have built-in MIDI (MPU401-UART)
  3742. interfaces. When the soundcard supports the standard MPU401-UART
  3743. interface, most likely you can use the ALSA MPU401-UART API. The
  3744. MPU401-UART API is defined in
  3745. <filename>&lt;sound/mpu401.h&gt;</filename>.
  3746. </para>
  3747. <para>
  3748. Some soundchips have a similar but slightly different
  3749. implementation of mpu401 stuff. For example, emu10k1 has its own
  3750. mpu401 routines.
  3751. </para>
  3752. </section>
  3753. <section id="midi-interface-constructor">
  3754. <title>Constructor</title>
  3755. <para>
  3756. To create a rawmidi object, call
  3757. <function>snd_mpu401_uart_new()</function>.
  3758. <informalexample>
  3759. <programlisting>
  3760. <![CDATA[
  3761. struct snd_rawmidi *rmidi;
  3762. snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags,
  3763. irq, &rmidi);
  3764. ]]>
  3765. </programlisting>
  3766. </informalexample>
  3767. </para>
  3768. <para>
  3769. The first argument is the card pointer, and the second is the
  3770. index of this component. You can create up to 8 rawmidi
  3771. devices.
  3772. </para>
  3773. <para>
  3774. The third argument is the type of the hardware,
  3775. <constant>MPU401_HW_XXX</constant>. If it's not a special one,
  3776. you can use <constant>MPU401_HW_MPU401</constant>.
  3777. </para>
  3778. <para>
  3779. The 4th argument is the I/O port address. Many
  3780. backward-compatible MPU401 have an I/O port such as 0x330. Or, it
  3781. might be a part of its own PCI I/O region. It depends on the
  3782. chip design.
  3783. </para>
  3784. <para>
  3785. The 5th argument is a bitflag for additional information.
  3786. When the I/O port address above is part of the PCI I/O
  3787. region, the MPU401 I/O port might have been already allocated
  3788. (reserved) by the driver itself. In such a case, pass a bit flag
  3789. <constant>MPU401_INFO_INTEGRATED</constant>,
  3790. and the mpu401-uart layer will allocate the I/O ports by itself.
  3791. </para>
  3792. <para>
  3793. When the controller supports only the input or output MIDI stream,
  3794. pass the <constant>MPU401_INFO_INPUT</constant> or
  3795. <constant>MPU401_INFO_OUTPUT</constant> bitflag, respectively.
  3796. Then the rawmidi instance is created as a single stream.
  3797. </para>
  3798. <para>
  3799. <constant>MPU401_INFO_MMIO</constant> bitflag is used to change
  3800. the access method to MMIO (via readb and writeb) instead of
  3801. iob and outb. In this case, you have to pass the iomapped address
  3802. to <function>snd_mpu401_uart_new()</function>.
  3803. </para>
  3804. <para>
  3805. When <constant>MPU401_INFO_TX_IRQ</constant> is set, the output
  3806. stream isn't checked in the default interrupt handler. The driver
  3807. needs to call <function>snd_mpu401_uart_interrupt_tx()</function>
  3808. by itself to start processing the output stream in the irq handler.
  3809. </para>
  3810. <para>
  3811. If the MPU-401 interface shares its interrupt with the other logical
  3812. devices on the card, set <constant>MPU401_INFO_IRQ_HOOK</constant>
  3813. (see <link linkend="midi-interface-interrupt-handler"><citetitle>
  3814. below</citetitle></link>).
  3815. </para>
  3816. <para>
  3817. Usually, the port address corresponds to the command port and
  3818. port + 1 corresponds to the data port. If not, you may change
  3819. the <structfield>cport</structfield> field of
  3820. struct <structname>snd_mpu401</structname> manually
  3821. afterward. However, <structname>snd_mpu401</structname> pointer is not
  3822. returned explicitly by
  3823. <function>snd_mpu401_uart_new()</function>. You need to cast
  3824. rmidi-&gt;private_data to
  3825. <structname>snd_mpu401</structname> explicitly,
  3826. <informalexample>
  3827. <programlisting>
  3828. <![CDATA[
  3829. struct snd_mpu401 *mpu;
  3830. mpu = rmidi->private_data;
  3831. ]]>
  3832. </programlisting>
  3833. </informalexample>
  3834. and reset the cport as you like:
  3835. <informalexample>
  3836. <programlisting>
  3837. <![CDATA[
  3838. mpu->cport = my_own_control_port;
  3839. ]]>
  3840. </programlisting>
  3841. </informalexample>
  3842. </para>
  3843. <para>
  3844. The 6th argument specifies the ISA irq number that will be
  3845. allocated. If no interrupt is to be allocated (because your
  3846. code is already allocating a shared interrupt, or because the
  3847. device does not use interrupts), pass -1 instead.
  3848. For a MPU-401 device without an interrupt, a polling timer
  3849. will be used instead.
  3850. </para>
  3851. </section>
  3852. <section id="midi-interface-interrupt-handler">
  3853. <title>Interrupt Handler</title>
  3854. <para>
  3855. When the interrupt is allocated in
  3856. <function>snd_mpu401_uart_new()</function>, an exclusive ISA
  3857. interrupt handler is automatically used, hence you don't have
  3858. anything else to do than creating the mpu401 stuff. Otherwise, you
  3859. have to set <constant>MPU401_INFO_IRQ_HOOK</constant>, and call
  3860. <function>snd_mpu401_uart_interrupt()</function> explicitly from your
  3861. own interrupt handler when it has determined that a UART interrupt
  3862. has occurred.
  3863. </para>
  3864. <para>
  3865. In this case, you need to pass the private_data of the
  3866. returned rawmidi object from
  3867. <function>snd_mpu401_uart_new()</function> as the second
  3868. argument of <function>snd_mpu401_uart_interrupt()</function>.
  3869. <informalexample>
  3870. <programlisting>
  3871. <![CDATA[
  3872. snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);
  3873. ]]>
  3874. </programlisting>
  3875. </informalexample>
  3876. </para>
  3877. </section>
  3878. </chapter>
  3879. <!-- ****************************************************** -->
  3880. <!-- RawMIDI Interface -->
  3881. <!-- ****************************************************** -->
  3882. <chapter id="rawmidi-interface">
  3883. <title>RawMIDI Interface</title>
  3884. <section id="rawmidi-interface-overview">
  3885. <title>Overview</title>
  3886. <para>
  3887. The raw MIDI interface is used for hardware MIDI ports that can
  3888. be accessed as a byte stream. It is not used for synthesizer
  3889. chips that do not directly understand MIDI.
  3890. </para>
  3891. <para>
  3892. ALSA handles file and buffer management. All you have to do is
  3893. to write some code to move data between the buffer and the
  3894. hardware.
  3895. </para>
  3896. <para>
  3897. The rawmidi API is defined in
  3898. <filename>&lt;sound/rawmidi.h&gt;</filename>.
  3899. </para>
  3900. </section>
  3901. <section id="rawmidi-interface-constructor">
  3902. <title>Constructor</title>
  3903. <para>
  3904. To create a rawmidi device, call the
  3905. <function>snd_rawmidi_new</function> function:
  3906. <informalexample>
  3907. <programlisting>
  3908. <![CDATA[
  3909. struct snd_rawmidi *rmidi;
  3910. err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi);
  3911. if (err < 0)
  3912. return err;
  3913. rmidi->private_data = chip;
  3914. strcpy(rmidi->name, "My MIDI");
  3915. rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  3916. SNDRV_RAWMIDI_INFO_INPUT |
  3917. SNDRV_RAWMIDI_INFO_DUPLEX;
  3918. ]]>
  3919. </programlisting>
  3920. </informalexample>
  3921. </para>
  3922. <para>
  3923. The first argument is the card pointer, the second argument is
  3924. the ID string.
  3925. </para>
  3926. <para>
  3927. The third argument is the index of this component. You can
  3928. create up to 8 rawmidi devices.
  3929. </para>
  3930. <para>
  3931. The fourth and fifth arguments are the number of output and
  3932. input substreams, respectively, of this device (a substream is
  3933. the equivalent of a MIDI port).
  3934. </para>
  3935. <para>
  3936. Set the <structfield>info_flags</structfield> field to specify
  3937. the capabilities of the device.
  3938. Set <constant>SNDRV_RAWMIDI_INFO_OUTPUT</constant> if there is
  3939. at least one output port,
  3940. <constant>SNDRV_RAWMIDI_INFO_INPUT</constant> if there is at
  3941. least one input port,
  3942. and <constant>SNDRV_RAWMIDI_INFO_DUPLEX</constant> if the device
  3943. can handle output and input at the same time.
  3944. </para>
  3945. <para>
  3946. After the rawmidi device is created, you need to set the
  3947. operators (callbacks) for each substream. There are helper
  3948. functions to set the operators for all the substreams of a device:
  3949. <informalexample>
  3950. <programlisting>
  3951. <![CDATA[
  3952. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops);
  3953. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);
  3954. ]]>
  3955. </programlisting>
  3956. </informalexample>
  3957. </para>
  3958. <para>
  3959. The operators are usually defined like this:
  3960. <informalexample>
  3961. <programlisting>
  3962. <![CDATA[
  3963. static struct snd_rawmidi_ops snd_mymidi_output_ops = {
  3964. .open = snd_mymidi_output_open,
  3965. .close = snd_mymidi_output_close,
  3966. .trigger = snd_mymidi_output_trigger,
  3967. };
  3968. ]]>
  3969. </programlisting>
  3970. </informalexample>
  3971. These callbacks are explained in the <link
  3972. linkend="rawmidi-interface-callbacks"><citetitle>Callbacks</citetitle></link>
  3973. section.
  3974. </para>
  3975. <para>
  3976. If there are more than one substream, you should give a
  3977. unique name to each of them:
  3978. <informalexample>
  3979. <programlisting>
  3980. <![CDATA[
  3981. struct snd_rawmidi_substream *substream;
  3982. list_for_each_entry(substream,
  3983. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  3984. list {
  3985. sprintf(substream->name, "My MIDI Port %d", substream->number + 1);
  3986. }
  3987. /* same for SNDRV_RAWMIDI_STREAM_INPUT */
  3988. ]]>
  3989. </programlisting>
  3990. </informalexample>
  3991. </para>
  3992. </section>
  3993. <section id="rawmidi-interface-callbacks">
  3994. <title>Callbacks</title>
  3995. <para>
  3996. In all the callbacks, the private data that you've set for the
  3997. rawmidi device can be accessed as
  3998. substream-&gt;rmidi-&gt;private_data.
  3999. <!-- <code> isn't available before DocBook 4.3 -->
  4000. </para>
  4001. <para>
  4002. If there is more than one port, your callbacks can determine the
  4003. port index from the struct snd_rawmidi_substream data passed to each
  4004. callback:
  4005. <informalexample>
  4006. <programlisting>
  4007. <![CDATA[
  4008. struct snd_rawmidi_substream *substream;
  4009. int index = substream->number;
  4010. ]]>
  4011. </programlisting>
  4012. </informalexample>
  4013. </para>
  4014. <section id="rawmidi-interface-op-open">
  4015. <title><function>open</function> callback</title>
  4016. <informalexample>
  4017. <programlisting>
  4018. <![CDATA[
  4019. static int snd_xxx_open(struct snd_rawmidi_substream *substream);
  4020. ]]>
  4021. </programlisting>
  4022. </informalexample>
  4023. <para>
  4024. This is called when a substream is opened.
  4025. You can initialize the hardware here, but you shouldn't
  4026. start transmitting/receiving data yet.
  4027. </para>
  4028. </section>
  4029. <section id="rawmidi-interface-op-close">
  4030. <title><function>close</function> callback</title>
  4031. <informalexample>
  4032. <programlisting>
  4033. <![CDATA[
  4034. static int snd_xxx_close(struct snd_rawmidi_substream *substream);
  4035. ]]>
  4036. </programlisting>
  4037. </informalexample>
  4038. <para>
  4039. Guess what.
  4040. </para>
  4041. <para>
  4042. The <function>open</function> and <function>close</function>
  4043. callbacks of a rawmidi device are serialized with a mutex,
  4044. and can sleep.
  4045. </para>
  4046. </section>
  4047. <section id="rawmidi-interface-op-trigger-out">
  4048. <title><function>trigger</function> callback for output
  4049. substreams</title>
  4050. <informalexample>
  4051. <programlisting>
  4052. <![CDATA[
  4053. static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);
  4054. ]]>
  4055. </programlisting>
  4056. </informalexample>
  4057. <para>
  4058. This is called with a nonzero <parameter>up</parameter>
  4059. parameter when there is some data in the substream buffer that
  4060. must be transmitted.
  4061. </para>
  4062. <para>
  4063. To read data from the buffer, call
  4064. <function>snd_rawmidi_transmit_peek</function>. It will
  4065. return the number of bytes that have been read; this will be
  4066. less than the number of bytes requested when there are no more
  4067. data in the buffer.
  4068. After the data have been transmitted successfully, call
  4069. <function>snd_rawmidi_transmit_ack</function> to remove the
  4070. data from the substream buffer:
  4071. <informalexample>
  4072. <programlisting>
  4073. <![CDATA[
  4074. unsigned char data;
  4075. while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
  4076. if (snd_mychip_try_to_transmit(data))
  4077. snd_rawmidi_transmit_ack(substream, 1);
  4078. else
  4079. break; /* hardware FIFO full */
  4080. }
  4081. ]]>
  4082. </programlisting>
  4083. </informalexample>
  4084. </para>
  4085. <para>
  4086. If you know beforehand that the hardware will accept data, you
  4087. can use the <function>snd_rawmidi_transmit</function> function
  4088. which reads some data and removes them from the buffer at once:
  4089. <informalexample>
  4090. <programlisting>
  4091. <![CDATA[
  4092. while (snd_mychip_transmit_possible()) {
  4093. unsigned char data;
  4094. if (snd_rawmidi_transmit(substream, &data, 1) != 1)
  4095. break; /* no more data */
  4096. snd_mychip_transmit(data);
  4097. }
  4098. ]]>
  4099. </programlisting>
  4100. </informalexample>
  4101. </para>
  4102. <para>
  4103. If you know beforehand how many bytes you can accept, you can
  4104. use a buffer size greater than one with the
  4105. <function>snd_rawmidi_transmit*</function> functions.
  4106. </para>
  4107. <para>
  4108. The <function>trigger</function> callback must not sleep. If
  4109. the hardware FIFO is full before the substream buffer has been
  4110. emptied, you have to continue transmitting data later, either
  4111. in an interrupt handler, or with a timer if the hardware
  4112. doesn't have a MIDI transmit interrupt.
  4113. </para>
  4114. <para>
  4115. The <function>trigger</function> callback is called with a
  4116. zero <parameter>up</parameter> parameter when the transmission
  4117. of data should be aborted.
  4118. </para>
  4119. </section>
  4120. <section id="rawmidi-interface-op-trigger-in">
  4121. <title><function>trigger</function> callback for input
  4122. substreams</title>
  4123. <informalexample>
  4124. <programlisting>
  4125. <![CDATA[
  4126. static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);
  4127. ]]>
  4128. </programlisting>
  4129. </informalexample>
  4130. <para>
  4131. This is called with a nonzero <parameter>up</parameter>
  4132. parameter to enable receiving data, or with a zero
  4133. <parameter>up</parameter> parameter do disable receiving data.
  4134. </para>
  4135. <para>
  4136. The <function>trigger</function> callback must not sleep; the
  4137. actual reading of data from the device is usually done in an
  4138. interrupt handler.
  4139. </para>
  4140. <para>
  4141. When data reception is enabled, your interrupt handler should
  4142. call <function>snd_rawmidi_receive</function> for all received
  4143. data:
  4144. <informalexample>
  4145. <programlisting>
  4146. <![CDATA[
  4147. void snd_mychip_midi_interrupt(...)
  4148. {
  4149. while (mychip_midi_available()) {
  4150. unsigned char data;
  4151. data = mychip_midi_read();
  4152. snd_rawmidi_receive(substream, &data, 1);
  4153. }
  4154. }
  4155. ]]>
  4156. </programlisting>
  4157. </informalexample>
  4158. </para>
  4159. </section>
  4160. <section id="rawmidi-interface-op-drain">
  4161. <title><function>drain</function> callback</title>
  4162. <informalexample>
  4163. <programlisting>
  4164. <![CDATA[
  4165. static void snd_xxx_drain(struct snd_rawmidi_substream *substream);
  4166. ]]>
  4167. </programlisting>
  4168. </informalexample>
  4169. <para>
  4170. This is only used with output substreams. This function should wait
  4171. until all data read from the substream buffer have been transmitted.
  4172. This ensures that the device can be closed and the driver unloaded
  4173. without losing data.
  4174. </para>
  4175. <para>
  4176. This callback is optional. If you do not set
  4177. <structfield>drain</structfield> in the struct snd_rawmidi_ops
  4178. structure, ALSA will simply wait for 50&nbsp;milliseconds
  4179. instead.
  4180. </para>
  4181. </section>
  4182. </section>
  4183. </chapter>
  4184. <!-- ****************************************************** -->
  4185. <!-- Miscellaneous Devices -->
  4186. <!-- ****************************************************** -->
  4187. <chapter id="misc-devices">
  4188. <title>Miscellaneous Devices</title>
  4189. <section id="misc-devices-opl3">
  4190. <title>FM OPL3</title>
  4191. <para>
  4192. The FM OPL3 is still used in many chips (mainly for backward
  4193. compatibility). ALSA has a nice OPL3 FM control layer, too. The
  4194. OPL3 API is defined in
  4195. <filename>&lt;sound/opl3.h&gt;</filename>.
  4196. </para>
  4197. <para>
  4198. FM registers can be directly accessed through the direct-FM API,
  4199. defined in <filename>&lt;sound/asound_fm.h&gt;</filename>. In
  4200. ALSA native mode, FM registers are accessed through
  4201. the Hardware-Dependent Device direct-FM extension API, whereas in
  4202. OSS compatible mode, FM registers can be accessed with the OSS
  4203. direct-FM compatible API in <filename>/dev/dmfmX</filename> device.
  4204. </para>
  4205. <para>
  4206. To create the OPL3 component, you have two functions to
  4207. call. The first one is a constructor for the <type>opl3_t</type>
  4208. instance.
  4209. <informalexample>
  4210. <programlisting>
  4211. <![CDATA[
  4212. struct snd_opl3 *opl3;
  4213. snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX,
  4214. integrated, &opl3);
  4215. ]]>
  4216. </programlisting>
  4217. </informalexample>
  4218. </para>
  4219. <para>
  4220. The first argument is the card pointer, the second one is the
  4221. left port address, and the third is the right port address. In
  4222. most cases, the right port is placed at the left port + 2.
  4223. </para>
  4224. <para>
  4225. The fourth argument is the hardware type.
  4226. </para>
  4227. <para>
  4228. When the left and right ports have been already allocated by
  4229. the card driver, pass non-zero to the fifth argument
  4230. (<parameter>integrated</parameter>). Otherwise, the opl3 module will
  4231. allocate the specified ports by itself.
  4232. </para>
  4233. <para>
  4234. When the accessing the hardware requires special method
  4235. instead of the standard I/O access, you can create opl3 instance
  4236. separately with <function>snd_opl3_new()</function>.
  4237. <informalexample>
  4238. <programlisting>
  4239. <![CDATA[
  4240. struct snd_opl3 *opl3;
  4241. snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);
  4242. ]]>
  4243. </programlisting>
  4244. </informalexample>
  4245. </para>
  4246. <para>
  4247. Then set <structfield>command</structfield>,
  4248. <structfield>private_data</structfield> and
  4249. <structfield>private_free</structfield> for the private
  4250. access function, the private data and the destructor.
  4251. The l_port and r_port are not necessarily set. Only the
  4252. command must be set properly. You can retrieve the data
  4253. from the opl3-&gt;private_data field.
  4254. </para>
  4255. <para>
  4256. After creating the opl3 instance via <function>snd_opl3_new()</function>,
  4257. call <function>snd_opl3_init()</function> to initialize the chip to the
  4258. proper state. Note that <function>snd_opl3_create()</function> always
  4259. calls it internally.
  4260. </para>
  4261. <para>
  4262. If the opl3 instance is created successfully, then create a
  4263. hwdep device for this opl3.
  4264. <informalexample>
  4265. <programlisting>
  4266. <![CDATA[
  4267. struct snd_hwdep *opl3hwdep;
  4268. snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);
  4269. ]]>
  4270. </programlisting>
  4271. </informalexample>
  4272. </para>
  4273. <para>
  4274. The first argument is the <type>opl3_t</type> instance you
  4275. created, and the second is the index number, usually 0.
  4276. </para>
  4277. <para>
  4278. The third argument is the index-offset for the sequencer
  4279. client assigned to the OPL3 port. When there is an MPU401-UART,
  4280. give 1 for here (UART always takes 0).
  4281. </para>
  4282. </section>
  4283. <section id="misc-devices-hardware-dependent">
  4284. <title>Hardware-Dependent Devices</title>
  4285. <para>
  4286. Some chips need user-space access for special
  4287. controls or for loading the micro code. In such a case, you can
  4288. create a hwdep (hardware-dependent) device. The hwdep API is
  4289. defined in <filename>&lt;sound/hwdep.h&gt;</filename>. You can
  4290. find examples in opl3 driver or
  4291. <filename>isa/sb/sb16_csp.c</filename>.
  4292. </para>
  4293. <para>
  4294. The creation of the <type>hwdep</type> instance is done via
  4295. <function>snd_hwdep_new()</function>.
  4296. <informalexample>
  4297. <programlisting>
  4298. <![CDATA[
  4299. struct snd_hwdep *hw;
  4300. snd_hwdep_new(card, "My HWDEP", 0, &hw);
  4301. ]]>
  4302. </programlisting>
  4303. </informalexample>
  4304. where the third argument is the index number.
  4305. </para>
  4306. <para>
  4307. You can then pass any pointer value to the
  4308. <parameter>private_data</parameter>.
  4309. If you assign a private data, you should define the
  4310. destructor, too. The destructor function is set in
  4311. the <structfield>private_free</structfield> field.
  4312. <informalexample>
  4313. <programlisting>
  4314. <![CDATA[
  4315. struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL);
  4316. hw->private_data = p;
  4317. hw->private_free = mydata_free;
  4318. ]]>
  4319. </programlisting>
  4320. </informalexample>
  4321. and the implementation of the destructor would be:
  4322. <informalexample>
  4323. <programlisting>
  4324. <![CDATA[
  4325. static void mydata_free(struct snd_hwdep *hw)
  4326. {
  4327. struct mydata *p = hw->private_data;
  4328. kfree(p);
  4329. }
  4330. ]]>
  4331. </programlisting>
  4332. </informalexample>
  4333. </para>
  4334. <para>
  4335. The arbitrary file operations can be defined for this
  4336. instance. The file operators are defined in
  4337. the <parameter>ops</parameter> table. For example, assume that
  4338. this chip needs an ioctl.
  4339. <informalexample>
  4340. <programlisting>
  4341. <![CDATA[
  4342. hw->ops.open = mydata_open;
  4343. hw->ops.ioctl = mydata_ioctl;
  4344. hw->ops.release = mydata_release;
  4345. ]]>
  4346. </programlisting>
  4347. </informalexample>
  4348. And implement the callback functions as you like.
  4349. </para>
  4350. </section>
  4351. <section id="misc-devices-IEC958">
  4352. <title>IEC958 (S/PDIF)</title>
  4353. <para>
  4354. Usually the controls for IEC958 devices are implemented via
  4355. the control interface. There is a macro to compose a name string for
  4356. IEC958 controls, <function>SNDRV_CTL_NAME_IEC958()</function>
  4357. defined in <filename>&lt;include/asound.h&gt;</filename>.
  4358. </para>
  4359. <para>
  4360. There are some standard controls for IEC958 status bits. These
  4361. controls use the type <type>SNDRV_CTL_ELEM_TYPE_IEC958</type>,
  4362. and the size of element is fixed as 4 bytes array
  4363. (value.iec958.status[x]). For the <structfield>info</structfield>
  4364. callback, you don't specify
  4365. the value field for this type (the count field must be set,
  4366. though).
  4367. </para>
  4368. <para>
  4369. <quote>IEC958 Playback Con Mask</quote> is used to return the
  4370. bit-mask for the IEC958 status bits of consumer mode. Similarly,
  4371. <quote>IEC958 Playback Pro Mask</quote> returns the bitmask for
  4372. professional mode. They are read-only controls, and are defined
  4373. as MIXER controls (iface =
  4374. <constant>SNDRV_CTL_ELEM_IFACE_MIXER</constant>).
  4375. </para>
  4376. <para>
  4377. Meanwhile, <quote>IEC958 Playback Default</quote> control is
  4378. defined for getting and setting the current default IEC958
  4379. bits. Note that this one is usually defined as a PCM control
  4380. (iface = <constant>SNDRV_CTL_ELEM_IFACE_PCM</constant>),
  4381. although in some places it's defined as a MIXER control.
  4382. </para>
  4383. <para>
  4384. In addition, you can define the control switches to
  4385. enable/disable or to set the raw bit mode. The implementation
  4386. will depend on the chip, but the control should be named as
  4387. <quote>IEC958 xxx</quote>, preferably using
  4388. the <function>SNDRV_CTL_NAME_IEC958()</function> macro.
  4389. </para>
  4390. <para>
  4391. You can find several cases, for example,
  4392. <filename>pci/emu10k1</filename>,
  4393. <filename>pci/ice1712</filename>, or
  4394. <filename>pci/cmipci.c</filename>.
  4395. </para>
  4396. </section>
  4397. </chapter>
  4398. <!-- ****************************************************** -->
  4399. <!-- Buffer and Memory Management -->
  4400. <!-- ****************************************************** -->
  4401. <chapter id="buffer-and-memory">
  4402. <title>Buffer and Memory Management</title>
  4403. <section id="buffer-and-memory-buffer-types">
  4404. <title>Buffer Types</title>
  4405. <para>
  4406. ALSA provides several different buffer allocation functions
  4407. depending on the bus and the architecture. All these have a
  4408. consistent API. The allocation of physically-contiguous pages is
  4409. done via
  4410. <function>snd_malloc_xxx_pages()</function> function, where xxx
  4411. is the bus type.
  4412. </para>
  4413. <para>
  4414. The allocation of pages with fallback is
  4415. <function>snd_malloc_xxx_pages_fallback()</function>. This
  4416. function tries to allocate the specified pages but if the pages
  4417. are not available, it tries to reduce the page sizes until
  4418. enough space is found.
  4419. </para>
  4420. <para>
  4421. The release the pages, call
  4422. <function>snd_free_xxx_pages()</function> function.
  4423. </para>
  4424. <para>
  4425. Usually, ALSA drivers try to allocate and reserve
  4426. a large contiguous physical space
  4427. at the time the module is loaded for the later use.
  4428. This is called <quote>pre-allocation</quote>.
  4429. As already written, you can call the following function at
  4430. pcm instance construction time (in the case of PCI bus).
  4431. <informalexample>
  4432. <programlisting>
  4433. <![CDATA[
  4434. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  4435. snd_dma_pci_data(pci), size, max);
  4436. ]]>
  4437. </programlisting>
  4438. </informalexample>
  4439. where <parameter>size</parameter> is the byte size to be
  4440. pre-allocated and the <parameter>max</parameter> is the maximum
  4441. size to be changed via the <filename>prealloc</filename> proc file.
  4442. The allocator will try to get an area as large as possible
  4443. within the given size.
  4444. </para>
  4445. <para>
  4446. The second argument (type) and the third argument (device pointer)
  4447. are dependent on the bus.
  4448. In the case of the ISA bus, pass <function>snd_dma_isa_data()</function>
  4449. as the third argument with <constant>SNDRV_DMA_TYPE_DEV</constant> type.
  4450. For the continuous buffer unrelated to the bus can be pre-allocated
  4451. with <constant>SNDRV_DMA_TYPE_CONTINUOUS</constant> type and the
  4452. <function>snd_dma_continuous_data(GFP_KERNEL)</function> device pointer,
  4453. where <constant>GFP_KERNEL</constant> is the kernel allocation flag to
  4454. use.
  4455. For the PCI scatter-gather buffers, use
  4456. <constant>SNDRV_DMA_TYPE_DEV_SG</constant> with
  4457. <function>snd_dma_pci_data(pci)</function>
  4458. (see the
  4459. <link linkend="buffer-and-memory-non-contiguous"><citetitle>Non-Contiguous Buffers
  4460. </citetitle></link> section).
  4461. </para>
  4462. <para>
  4463. Once the buffer is pre-allocated, you can use the
  4464. allocator in the <structfield>hw_params</structfield> callback:
  4465. <informalexample>
  4466. <programlisting>
  4467. <![CDATA[
  4468. snd_pcm_lib_malloc_pages(substream, size);
  4469. ]]>
  4470. </programlisting>
  4471. </informalexample>
  4472. Note that you have to pre-allocate to use this function.
  4473. </para>
  4474. </section>
  4475. <section id="buffer-and-memory-external-hardware">
  4476. <title>External Hardware Buffers</title>
  4477. <para>
  4478. Some chips have their own hardware buffers and the DMA
  4479. transfer from the host memory is not available. In such a case,
  4480. you need to either 1) copy/set the audio data directly to the
  4481. external hardware buffer, or 2) make an intermediate buffer and
  4482. copy/set the data from it to the external hardware buffer in
  4483. interrupts (or in tasklets, preferably).
  4484. </para>
  4485. <para>
  4486. The first case works fine if the external hardware buffer is large
  4487. enough. This method doesn't need any extra buffers and thus is
  4488. more effective. You need to define the
  4489. <structfield>copy</structfield> and
  4490. <structfield>silence</structfield> callbacks for
  4491. the data transfer. However, there is a drawback: it cannot
  4492. be mmapped. The examples are GUS's GF1 PCM or emu8000's
  4493. wavetable PCM.
  4494. </para>
  4495. <para>
  4496. The second case allows for mmap on the buffer, although you have
  4497. to handle an interrupt or a tasklet to transfer the data
  4498. from the intermediate buffer to the hardware buffer. You can find an
  4499. example in the vxpocket driver.
  4500. </para>
  4501. <para>
  4502. Another case is when the chip uses a PCI memory-map
  4503. region for the buffer instead of the host memory. In this case,
  4504. mmap is available only on certain architectures like the Intel one.
  4505. In non-mmap mode, the data cannot be transferred as in the normal
  4506. way. Thus you need to define the <structfield>copy</structfield> and
  4507. <structfield>silence</structfield> callbacks as well,
  4508. as in the cases above. The examples are found in
  4509. <filename>rme32.c</filename> and <filename>rme96.c</filename>.
  4510. </para>
  4511. <para>
  4512. The implementation of the <structfield>copy</structfield> and
  4513. <structfield>silence</structfield> callbacks depends upon
  4514. whether the hardware supports interleaved or non-interleaved
  4515. samples. The <structfield>copy</structfield> callback is
  4516. defined like below, a bit
  4517. differently depending whether the direction is playback or
  4518. capture:
  4519. <informalexample>
  4520. <programlisting>
  4521. <![CDATA[
  4522. static int playback_copy(struct snd_pcm_substream *substream, int channel,
  4523. snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count);
  4524. static int capture_copy(struct snd_pcm_substream *substream, int channel,
  4525. snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count);
  4526. ]]>
  4527. </programlisting>
  4528. </informalexample>
  4529. </para>
  4530. <para>
  4531. In the case of interleaved samples, the second argument
  4532. (<parameter>channel</parameter>) is not used. The third argument
  4533. (<parameter>pos</parameter>) points the
  4534. current position offset in frames.
  4535. </para>
  4536. <para>
  4537. The meaning of the fourth argument is different between
  4538. playback and capture. For playback, it holds the source data
  4539. pointer, and for capture, it's the destination data pointer.
  4540. </para>
  4541. <para>
  4542. The last argument is the number of frames to be copied.
  4543. </para>
  4544. <para>
  4545. What you have to do in this callback is again different
  4546. between playback and capture directions. In the
  4547. playback case, you copy the given amount of data
  4548. (<parameter>count</parameter>) at the specified pointer
  4549. (<parameter>src</parameter>) to the specified offset
  4550. (<parameter>pos</parameter>) on the hardware buffer. When
  4551. coded like memcpy-like way, the copy would be like:
  4552. <informalexample>
  4553. <programlisting>
  4554. <![CDATA[
  4555. my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src,
  4556. frames_to_bytes(runtime, count));
  4557. ]]>
  4558. </programlisting>
  4559. </informalexample>
  4560. </para>
  4561. <para>
  4562. For the capture direction, you copy the given amount of
  4563. data (<parameter>count</parameter>) at the specified offset
  4564. (<parameter>pos</parameter>) on the hardware buffer to the
  4565. specified pointer (<parameter>dst</parameter>).
  4566. <informalexample>
  4567. <programlisting>
  4568. <![CDATA[
  4569. my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos),
  4570. frames_to_bytes(runtime, count));
  4571. ]]>
  4572. </programlisting>
  4573. </informalexample>
  4574. Note that both the position and the amount of data are given
  4575. in frames.
  4576. </para>
  4577. <para>
  4578. In the case of non-interleaved samples, the implementation
  4579. will be a bit more complicated.
  4580. </para>
  4581. <para>
  4582. You need to check the channel argument, and if it's -1, copy
  4583. the whole channels. Otherwise, you have to copy only the
  4584. specified channel. Please check
  4585. <filename>isa/gus/gus_pcm.c</filename> as an example.
  4586. </para>
  4587. <para>
  4588. The <structfield>silence</structfield> callback is also
  4589. implemented in a similar way.
  4590. <informalexample>
  4591. <programlisting>
  4592. <![CDATA[
  4593. static int silence(struct snd_pcm_substream *substream, int channel,
  4594. snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
  4595. ]]>
  4596. </programlisting>
  4597. </informalexample>
  4598. </para>
  4599. <para>
  4600. The meanings of arguments are the same as in the
  4601. <structfield>copy</structfield>
  4602. callback, although there is no <parameter>src/dst</parameter>
  4603. argument. In the case of interleaved samples, the channel
  4604. argument has no meaning, as well as on
  4605. <structfield>copy</structfield> callback.
  4606. </para>
  4607. <para>
  4608. The role of <structfield>silence</structfield> callback is to
  4609. set the given amount
  4610. (<parameter>count</parameter>) of silence data at the
  4611. specified offset (<parameter>pos</parameter>) on the hardware
  4612. buffer. Suppose that the data format is signed (that is, the
  4613. silent-data is 0), and the implementation using a memset-like
  4614. function would be like:
  4615. <informalexample>
  4616. <programlisting>
  4617. <![CDATA[
  4618. my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0,
  4619. frames_to_bytes(runtime, count));
  4620. ]]>
  4621. </programlisting>
  4622. </informalexample>
  4623. </para>
  4624. <para>
  4625. In the case of non-interleaved samples, again, the
  4626. implementation becomes a bit more complicated. See, for example,
  4627. <filename>isa/gus/gus_pcm.c</filename>.
  4628. </para>
  4629. </section>
  4630. <section id="buffer-and-memory-non-contiguous">
  4631. <title>Non-Contiguous Buffers</title>
  4632. <para>
  4633. If your hardware supports the page table as in emu10k1 or the
  4634. buffer descriptors as in via82xx, you can use the scatter-gather
  4635. (SG) DMA. ALSA provides an interface for handling SG-buffers.
  4636. The API is provided in <filename>&lt;sound/pcm.h&gt;</filename>.
  4637. </para>
  4638. <para>
  4639. For creating the SG-buffer handler, call
  4640. <function>snd_pcm_lib_preallocate_pages()</function> or
  4641. <function>snd_pcm_lib_preallocate_pages_for_all()</function>
  4642. with <constant>SNDRV_DMA_TYPE_DEV_SG</constant>
  4643. in the PCM constructor like other PCI pre-allocator.
  4644. You need to pass <function>snd_dma_pci_data(pci)</function>,
  4645. where pci is the struct <structname>pci_dev</structname> pointer
  4646. of the chip as well.
  4647. The <type>struct snd_sg_buf</type> instance is created as
  4648. substream-&gt;dma_private. You can cast
  4649. the pointer like:
  4650. <informalexample>
  4651. <programlisting>
  4652. <![CDATA[
  4653. struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
  4654. ]]>
  4655. </programlisting>
  4656. </informalexample>
  4657. </para>
  4658. <para>
  4659. Then call <function>snd_pcm_lib_malloc_pages()</function>
  4660. in the <structfield>hw_params</structfield> callback
  4661. as well as in the case of normal PCI buffer.
  4662. The SG-buffer handler will allocate the non-contiguous kernel
  4663. pages of the given size and map them onto the virtually contiguous
  4664. memory. The virtual pointer is addressed in runtime-&gt;dma_area.
  4665. The physical address (runtime-&gt;dma_addr) is set to zero,
  4666. because the buffer is physically non-contiguous.
  4667. The physical address table is set up in sgbuf-&gt;table.
  4668. You can get the physical address at a certain offset via
  4669. <function>snd_pcm_sgbuf_get_addr()</function>.
  4670. </para>
  4671. <para>
  4672. When a SG-handler is used, you need to set
  4673. <function>snd_pcm_sgbuf_ops_page</function> as
  4674. the <structfield>page</structfield> callback.
  4675. (See <link linkend="pcm-interface-operators-page-callback">
  4676. <citetitle>page callback section</citetitle></link>.)
  4677. </para>
  4678. <para>
  4679. To release the data, call
  4680. <function>snd_pcm_lib_free_pages()</function> in the
  4681. <structfield>hw_free</structfield> callback as usual.
  4682. </para>
  4683. </section>
  4684. <section id="buffer-and-memory-vmalloced">
  4685. <title>Vmalloc'ed Buffers</title>
  4686. <para>
  4687. It's possible to use a buffer allocated via
  4688. <function>vmalloc</function>, for example, for an intermediate
  4689. buffer. Since the allocated pages are not contiguous, you need
  4690. to set the <structfield>page</structfield> callback to obtain
  4691. the physical address at every offset.
  4692. </para>
  4693. <para>
  4694. The implementation of <structfield>page</structfield> callback
  4695. would be like this:
  4696. <informalexample>
  4697. <programlisting>
  4698. <![CDATA[
  4699. #include <linux/vmalloc.h>
  4700. /* get the physical page pointer on the given offset */
  4701. static struct page *mychip_page(struct snd_pcm_substream *substream,
  4702. unsigned long offset)
  4703. {
  4704. void *pageptr = substream->runtime->dma_area + offset;
  4705. return vmalloc_to_page(pageptr);
  4706. }
  4707. ]]>
  4708. </programlisting>
  4709. </informalexample>
  4710. </para>
  4711. </section>
  4712. </chapter>
  4713. <!-- ****************************************************** -->
  4714. <!-- Proc Interface -->
  4715. <!-- ****************************************************** -->
  4716. <chapter id="proc-interface">
  4717. <title>Proc Interface</title>
  4718. <para>
  4719. ALSA provides an easy interface for procfs. The proc files are
  4720. very useful for debugging. I recommend you set up proc files if
  4721. you write a driver and want to get a running status or register
  4722. dumps. The API is found in
  4723. <filename>&lt;sound/info.h&gt;</filename>.
  4724. </para>
  4725. <para>
  4726. To create a proc file, call
  4727. <function>snd_card_proc_new()</function>.
  4728. <informalexample>
  4729. <programlisting>
  4730. <![CDATA[
  4731. struct snd_info_entry *entry;
  4732. int err = snd_card_proc_new(card, "my-file", &entry);
  4733. ]]>
  4734. </programlisting>
  4735. </informalexample>
  4736. where the second argument specifies the name of the proc file to be
  4737. created. The above example will create a file
  4738. <filename>my-file</filename> under the card directory,
  4739. e.g. <filename>/proc/asound/card0/my-file</filename>.
  4740. </para>
  4741. <para>
  4742. Like other components, the proc entry created via
  4743. <function>snd_card_proc_new()</function> will be registered and
  4744. released automatically in the card registration and release
  4745. functions.
  4746. </para>
  4747. <para>
  4748. When the creation is successful, the function stores a new
  4749. instance in the pointer given in the third argument.
  4750. It is initialized as a text proc file for read only. To use
  4751. this proc file as a read-only text file as it is, set the read
  4752. callback with a private data via
  4753. <function>snd_info_set_text_ops()</function>.
  4754. <informalexample>
  4755. <programlisting>
  4756. <![CDATA[
  4757. snd_info_set_text_ops(entry, chip, my_proc_read);
  4758. ]]>
  4759. </programlisting>
  4760. </informalexample>
  4761. where the second argument (<parameter>chip</parameter>) is the
  4762. private data to be used in the callbacks. The third parameter
  4763. specifies the read buffer size and the fourth
  4764. (<parameter>my_proc_read</parameter>) is the callback function, which
  4765. is defined like
  4766. <informalexample>
  4767. <programlisting>
  4768. <![CDATA[
  4769. static void my_proc_read(struct snd_info_entry *entry,
  4770. struct snd_info_buffer *buffer);
  4771. ]]>
  4772. </programlisting>
  4773. </informalexample>
  4774. </para>
  4775. <para>
  4776. In the read callback, use <function>snd_iprintf()</function> for
  4777. output strings, which works just like normal
  4778. <function>printf()</function>. For example,
  4779. <informalexample>
  4780. <programlisting>
  4781. <![CDATA[
  4782. static void my_proc_read(struct snd_info_entry *entry,
  4783. struct snd_info_buffer *buffer)
  4784. {
  4785. struct my_chip *chip = entry->private_data;
  4786. snd_iprintf(buffer, "This is my chip!\n");
  4787. snd_iprintf(buffer, "Port = %ld\n", chip->port);
  4788. }
  4789. ]]>
  4790. </programlisting>
  4791. </informalexample>
  4792. </para>
  4793. <para>
  4794. The file permissions can be changed afterwards. As default, it's
  4795. set as read only for all users. If you want to add write
  4796. permission for the user (root as default), do as follows:
  4797. <informalexample>
  4798. <programlisting>
  4799. <![CDATA[
  4800. entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
  4801. ]]>
  4802. </programlisting>
  4803. </informalexample>
  4804. and set the write buffer size and the callback
  4805. <informalexample>
  4806. <programlisting>
  4807. <![CDATA[
  4808. entry->c.text.write = my_proc_write;
  4809. ]]>
  4810. </programlisting>
  4811. </informalexample>
  4812. </para>
  4813. <para>
  4814. For the write callback, you can use
  4815. <function>snd_info_get_line()</function> to get a text line, and
  4816. <function>snd_info_get_str()</function> to retrieve a string from
  4817. the line. Some examples are found in
  4818. <filename>core/oss/mixer_oss.c</filename>, core/oss/and
  4819. <filename>pcm_oss.c</filename>.
  4820. </para>
  4821. <para>
  4822. For a raw-data proc-file, set the attributes as follows:
  4823. <informalexample>
  4824. <programlisting>
  4825. <![CDATA[
  4826. static struct snd_info_entry_ops my_file_io_ops = {
  4827. .read = my_file_io_read,
  4828. };
  4829. entry->content = SNDRV_INFO_CONTENT_DATA;
  4830. entry->private_data = chip;
  4831. entry->c.ops = &my_file_io_ops;
  4832. entry->size = 4096;
  4833. entry->mode = S_IFREG | S_IRUGO;
  4834. ]]>
  4835. </programlisting>
  4836. </informalexample>
  4837. For the raw data, <structfield>size</structfield> field must be
  4838. set properly. This specifies the maximum size of the proc file access.
  4839. </para>
  4840. <para>
  4841. The read/write callbacks of raw mode are more direct than the text mode.
  4842. You need to use a low-level I/O functions such as
  4843. <function>copy_from/to_user()</function> to transfer the
  4844. data.
  4845. <informalexample>
  4846. <programlisting>
  4847. <![CDATA[
  4848. static ssize_t my_file_io_read(struct snd_info_entry *entry,
  4849. void *file_private_data,
  4850. struct file *file,
  4851. char *buf,
  4852. size_t count,
  4853. loff_t pos)
  4854. {
  4855. if (copy_to_user(buf, local_data + pos, count))
  4856. return -EFAULT;
  4857. return count;
  4858. }
  4859. ]]>
  4860. </programlisting>
  4861. </informalexample>
  4862. If the size of the info entry has been set up properly,
  4863. <structfield>count</structfield> and <structfield>pos</structfield> are
  4864. guaranteed to fit within 0 and the given size.
  4865. You don't have to check the range in the callbacks unless any
  4866. other condition is required.
  4867. </para>
  4868. </chapter>
  4869. <!-- ****************************************************** -->
  4870. <!-- Power Management -->
  4871. <!-- ****************************************************** -->
  4872. <chapter id="power-management">
  4873. <title>Power Management</title>
  4874. <para>
  4875. If the chip is supposed to work with suspend/resume
  4876. functions, you need to add power-management code to the
  4877. driver. The additional code for power-management should be
  4878. <function>ifdef</function>'ed with
  4879. <constant>CONFIG_PM</constant>.
  4880. </para>
  4881. <para>
  4882. If the driver <emphasis>fully</emphasis> supports suspend/resume
  4883. that is, the device can be
  4884. properly resumed to its state when suspend was called,
  4885. you can set the <constant>SNDRV_PCM_INFO_RESUME</constant> flag
  4886. in the pcm info field. Usually, this is possible when the
  4887. registers of the chip can be safely saved and restored to
  4888. RAM. If this is set, the trigger callback is called with
  4889. <constant>SNDRV_PCM_TRIGGER_RESUME</constant> after the resume
  4890. callback completes.
  4891. </para>
  4892. <para>
  4893. Even if the driver doesn't support PM fully but
  4894. partial suspend/resume is still possible, it's still worthy to
  4895. implement suspend/resume callbacks. In such a case, applications
  4896. would reset the status by calling
  4897. <function>snd_pcm_prepare()</function> and restart the stream
  4898. appropriately. Hence, you can define suspend/resume callbacks
  4899. below but don't set <constant>SNDRV_PCM_INFO_RESUME</constant>
  4900. info flag to the PCM.
  4901. </para>
  4902. <para>
  4903. Note that the trigger with SUSPEND can always be called when
  4904. <function>snd_pcm_suspend_all</function> is called,
  4905. regardless of the <constant>SNDRV_PCM_INFO_RESUME</constant> flag.
  4906. The <constant>RESUME</constant> flag affects only the behavior
  4907. of <function>snd_pcm_resume()</function>.
  4908. (Thus, in theory,
  4909. <constant>SNDRV_PCM_TRIGGER_RESUME</constant> isn't needed
  4910. to be handled in the trigger callback when no
  4911. <constant>SNDRV_PCM_INFO_RESUME</constant> flag is set. But,
  4912. it's better to keep it for compatibility reasons.)
  4913. </para>
  4914. <para>
  4915. In the earlier version of ALSA drivers, a common
  4916. power-management layer was provided, but it has been removed.
  4917. The driver needs to define the suspend/resume hooks according to
  4918. the bus the device is connected to. In the case of PCI drivers, the
  4919. callbacks look like below:
  4920. <informalexample>
  4921. <programlisting>
  4922. <![CDATA[
  4923. #ifdef CONFIG_PM
  4924. static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
  4925. {
  4926. .... /* do things for suspend */
  4927. return 0;
  4928. }
  4929. static int snd_my_resume(struct pci_dev *pci)
  4930. {
  4931. .... /* do things for suspend */
  4932. return 0;
  4933. }
  4934. #endif
  4935. ]]>
  4936. </programlisting>
  4937. </informalexample>
  4938. </para>
  4939. <para>
  4940. The scheme of the real suspend job is as follows.
  4941. <orderedlist>
  4942. <listitem><para>Retrieve the card and the chip data.</para></listitem>
  4943. <listitem><para>Call <function>snd_power_change_state()</function> with
  4944. <constant>SNDRV_CTL_POWER_D3hot</constant> to change the
  4945. power status.</para></listitem>
  4946. <listitem><para>Call <function>snd_pcm_suspend_all()</function> to suspend the running PCM streams.</para></listitem>
  4947. <listitem><para>If AC97 codecs are used, call
  4948. <function>snd_ac97_suspend()</function> for each codec.</para></listitem>
  4949. <listitem><para>Save the register values if necessary.</para></listitem>
  4950. <listitem><para>Stop the hardware if necessary.</para></listitem>
  4951. <listitem><para>Disable the PCI device by calling
  4952. <function>pci_disable_device()</function>. Then, call
  4953. <function>pci_save_state()</function> at last.</para></listitem>
  4954. </orderedlist>
  4955. </para>
  4956. <para>
  4957. A typical code would be like:
  4958. <informalexample>
  4959. <programlisting>
  4960. <![CDATA[
  4961. static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
  4962. {
  4963. /* (1) */
  4964. struct snd_card *card = pci_get_drvdata(pci);
  4965. struct mychip *chip = card->private_data;
  4966. /* (2) */
  4967. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  4968. /* (3) */
  4969. snd_pcm_suspend_all(chip->pcm);
  4970. /* (4) */
  4971. snd_ac97_suspend(chip->ac97);
  4972. /* (5) */
  4973. snd_mychip_save_registers(chip);
  4974. /* (6) */
  4975. snd_mychip_stop_hardware(chip);
  4976. /* (7) */
  4977. pci_disable_device(pci);
  4978. pci_save_state(pci);
  4979. return 0;
  4980. }
  4981. ]]>
  4982. </programlisting>
  4983. </informalexample>
  4984. </para>
  4985. <para>
  4986. The scheme of the real resume job is as follows.
  4987. <orderedlist>
  4988. <listitem><para>Retrieve the card and the chip data.</para></listitem>
  4989. <listitem><para>Set up PCI. First, call <function>pci_restore_state()</function>.
  4990. Then enable the pci device again by calling <function>pci_enable_device()</function>.
  4991. Call <function>pci_set_master()</function> if necessary, too.</para></listitem>
  4992. <listitem><para>Re-initialize the chip.</para></listitem>
  4993. <listitem><para>Restore the saved registers if necessary.</para></listitem>
  4994. <listitem><para>Resume the mixer, e.g. calling
  4995. <function>snd_ac97_resume()</function>.</para></listitem>
  4996. <listitem><para>Restart the hardware (if any).</para></listitem>
  4997. <listitem><para>Call <function>snd_power_change_state()</function> with
  4998. <constant>SNDRV_CTL_POWER_D0</constant> to notify the processes.</para></listitem>
  4999. </orderedlist>
  5000. </para>
  5001. <para>
  5002. A typical code would be like:
  5003. <informalexample>
  5004. <programlisting>
  5005. <![CDATA[
  5006. static int mychip_resume(struct pci_dev *pci)
  5007. {
  5008. /* (1) */
  5009. struct snd_card *card = pci_get_drvdata(pci);
  5010. struct mychip *chip = card->private_data;
  5011. /* (2) */
  5012. pci_restore_state(pci);
  5013. pci_enable_device(pci);
  5014. pci_set_master(pci);
  5015. /* (3) */
  5016. snd_mychip_reinit_chip(chip);
  5017. /* (4) */
  5018. snd_mychip_restore_registers(chip);
  5019. /* (5) */
  5020. snd_ac97_resume(chip->ac97);
  5021. /* (6) */
  5022. snd_mychip_restart_chip(chip);
  5023. /* (7) */
  5024. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  5025. return 0;
  5026. }
  5027. ]]>
  5028. </programlisting>
  5029. </informalexample>
  5030. </para>
  5031. <para>
  5032. As shown in the above, it's better to save registers after
  5033. suspending the PCM operations via
  5034. <function>snd_pcm_suspend_all()</function> or
  5035. <function>snd_pcm_suspend()</function>. It means that the PCM
  5036. streams are already stopped when the register snapshot is
  5037. taken. But, remember that you don't have to restart the PCM
  5038. stream in the resume callback. It'll be restarted via
  5039. trigger call with <constant>SNDRV_PCM_TRIGGER_RESUME</constant>
  5040. when necessary.
  5041. </para>
  5042. <para>
  5043. OK, we have all callbacks now. Let's set them up. In the
  5044. initialization of the card, make sure that you can get the chip
  5045. data from the card instance, typically via
  5046. <structfield>private_data</structfield> field, in case you
  5047. created the chip data individually.
  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. 0, &card);
  5061. ....
  5062. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  5063. ....
  5064. card->private_data = chip;
  5065. ....
  5066. }
  5067. ]]>
  5068. </programlisting>
  5069. </informalexample>
  5070. When you created the chip data with
  5071. <function>snd_card_new()</function>, it's anyway accessible
  5072. via <structfield>private_data</structfield> field.
  5073. <informalexample>
  5074. <programlisting>
  5075. <![CDATA[
  5076. static int snd_mychip_probe(struct pci_dev *pci,
  5077. const struct pci_device_id *pci_id)
  5078. {
  5079. ....
  5080. struct snd_card *card;
  5081. struct mychip *chip;
  5082. int err;
  5083. ....
  5084. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  5085. sizeof(struct mychip), &card);
  5086. ....
  5087. chip = card->private_data;
  5088. ....
  5089. }
  5090. ]]>
  5091. </programlisting>
  5092. </informalexample>
  5093. </para>
  5094. <para>
  5095. If you need a space to save the registers, allocate the
  5096. buffer for it here, too, since it would be fatal
  5097. if you cannot allocate a memory in the suspend phase.
  5098. The allocated buffer should be released in the corresponding
  5099. destructor.
  5100. </para>
  5101. <para>
  5102. And next, set suspend/resume callbacks to the pci_driver.
  5103. <informalexample>
  5104. <programlisting>
  5105. <![CDATA[
  5106. static struct pci_driver driver = {
  5107. .name = KBUILD_MODNAME,
  5108. .id_table = snd_my_ids,
  5109. .probe = snd_my_probe,
  5110. .remove = snd_my_remove,
  5111. #ifdef CONFIG_PM
  5112. .suspend = snd_my_suspend,
  5113. .resume = snd_my_resume,
  5114. #endif
  5115. };
  5116. ]]>
  5117. </programlisting>
  5118. </informalexample>
  5119. </para>
  5120. </chapter>
  5121. <!-- ****************************************************** -->
  5122. <!-- Module Parameters -->
  5123. <!-- ****************************************************** -->
  5124. <chapter id="module-parameters">
  5125. <title>Module Parameters</title>
  5126. <para>
  5127. There are standard module options for ALSA. At least, each
  5128. module should have the <parameter>index</parameter>,
  5129. <parameter>id</parameter> and <parameter>enable</parameter>
  5130. options.
  5131. </para>
  5132. <para>
  5133. If the module supports multiple cards (usually up to
  5134. 8 = <constant>SNDRV_CARDS</constant> cards), they should be
  5135. arrays. The default initial values are defined already as
  5136. constants for easier programming:
  5137. <informalexample>
  5138. <programlisting>
  5139. <![CDATA[
  5140. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  5141. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  5142. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  5143. ]]>
  5144. </programlisting>
  5145. </informalexample>
  5146. </para>
  5147. <para>
  5148. If the module supports only a single card, they could be single
  5149. variables, instead. <parameter>enable</parameter> option is not
  5150. always necessary in this case, but it would be better to have a
  5151. dummy option for compatibility.
  5152. </para>
  5153. <para>
  5154. The module parameters must be declared with the standard
  5155. <function>module_param()()</function>,
  5156. <function>module_param_array()()</function> and
  5157. <function>MODULE_PARM_DESC()</function> macros.
  5158. </para>
  5159. <para>
  5160. The typical coding would be like below:
  5161. <informalexample>
  5162. <programlisting>
  5163. <![CDATA[
  5164. #define CARD_NAME "My Chip"
  5165. module_param_array(index, int, NULL, 0444);
  5166. MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
  5167. module_param_array(id, charp, NULL, 0444);
  5168. MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
  5169. module_param_array(enable, bool, NULL, 0444);
  5170. MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
  5171. ]]>
  5172. </programlisting>
  5173. </informalexample>
  5174. </para>
  5175. <para>
  5176. Also, don't forget to define the module description, classes,
  5177. license and devices. Especially, the recent modprobe requires to
  5178. define the module license as GPL, etc., otherwise the system is
  5179. shown as <quote>tainted</quote>.
  5180. <informalexample>
  5181. <programlisting>
  5182. <![CDATA[
  5183. MODULE_DESCRIPTION("My Chip");
  5184. MODULE_LICENSE("GPL");
  5185. MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
  5186. ]]>
  5187. </programlisting>
  5188. </informalexample>
  5189. </para>
  5190. </chapter>
  5191. <!-- ****************************************************** -->
  5192. <!-- How To Put Your Driver -->
  5193. <!-- ****************************************************** -->
  5194. <chapter id="how-to-put-your-driver">
  5195. <title>How To Put Your Driver Into ALSA Tree</title>
  5196. <section>
  5197. <title>General</title>
  5198. <para>
  5199. So far, you've learned how to write the driver codes.
  5200. And you might have a question now: how to put my own
  5201. driver into the ALSA driver tree?
  5202. Here (finally :) the standard procedure is described briefly.
  5203. </para>
  5204. <para>
  5205. Suppose that you create a new PCI driver for the card
  5206. <quote>xyz</quote>. The card module name would be
  5207. snd-xyz. The new driver is usually put into the alsa-driver
  5208. tree, <filename>alsa-driver/pci</filename> directory in
  5209. the case of PCI cards.
  5210. Then the driver is evaluated, audited and tested
  5211. by developers and users. After a certain time, the driver
  5212. will go to the alsa-kernel tree (to the corresponding directory,
  5213. such as <filename>alsa-kernel/pci</filename>) and eventually
  5214. will be integrated into the Linux 2.6 tree (the directory would be
  5215. <filename>linux/sound/pci</filename>).
  5216. </para>
  5217. <para>
  5218. In the following sections, the driver code is supposed
  5219. to be put into alsa-driver tree. The two cases are covered:
  5220. a driver consisting of a single source file and one consisting
  5221. of several source files.
  5222. </para>
  5223. </section>
  5224. <section>
  5225. <title>Driver with A Single Source File</title>
  5226. <para>
  5227. <orderedlist>
  5228. <listitem>
  5229. <para>
  5230. Modify alsa-driver/pci/Makefile
  5231. </para>
  5232. <para>
  5233. Suppose you have a file xyz.c. Add the following
  5234. two lines
  5235. <informalexample>
  5236. <programlisting>
  5237. <![CDATA[
  5238. snd-xyz-objs := xyz.o
  5239. obj-$(CONFIG_SND_XYZ) += snd-xyz.o
  5240. ]]>
  5241. </programlisting>
  5242. </informalexample>
  5243. </para>
  5244. </listitem>
  5245. <listitem>
  5246. <para>
  5247. Create the Kconfig entry
  5248. </para>
  5249. <para>
  5250. Add the new entry of Kconfig for your xyz driver.
  5251. <informalexample>
  5252. <programlisting>
  5253. <![CDATA[
  5254. config SND_XYZ
  5255. tristate "Foobar XYZ"
  5256. depends on SND
  5257. select SND_PCM
  5258. help
  5259. Say Y here to include support for Foobar XYZ soundcard.
  5260. To compile this driver as a module, choose M here: the module
  5261. will be called snd-xyz.
  5262. ]]>
  5263. </programlisting>
  5264. </informalexample>
  5265. the line, select SND_PCM, specifies that the driver xyz supports
  5266. PCM. In addition to SND_PCM, the following components are
  5267. supported for select command:
  5268. SND_RAWMIDI, SND_TIMER, SND_HWDEP, SND_MPU401_UART,
  5269. SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, SND_AC97_CODEC.
  5270. Add the select command for each supported component.
  5271. </para>
  5272. <para>
  5273. Note that some selections imply the lowlevel selections.
  5274. For example, PCM includes TIMER, MPU401_UART includes RAWMIDI,
  5275. AC97_CODEC includes PCM, and OPL3_LIB includes HWDEP.
  5276. You don't need to give the lowlevel selections again.
  5277. </para>
  5278. <para>
  5279. For the details of Kconfig script, refer to the kbuild
  5280. documentation.
  5281. </para>
  5282. </listitem>
  5283. <listitem>
  5284. <para>
  5285. Run cvscompile script to re-generate the configure script and
  5286. build the whole stuff again.
  5287. </para>
  5288. </listitem>
  5289. </orderedlist>
  5290. </para>
  5291. </section>
  5292. <section>
  5293. <title>Drivers with Several Source Files</title>
  5294. <para>
  5295. Suppose that the driver snd-xyz have several source files.
  5296. They are located in the new subdirectory,
  5297. pci/xyz.
  5298. <orderedlist>
  5299. <listitem>
  5300. <para>
  5301. Add a new directory (<filename>xyz</filename>) in
  5302. <filename>alsa-driver/pci/Makefile</filename> as below
  5303. <informalexample>
  5304. <programlisting>
  5305. <![CDATA[
  5306. obj-$(CONFIG_SND) += xyz/
  5307. ]]>
  5308. </programlisting>
  5309. </informalexample>
  5310. </para>
  5311. </listitem>
  5312. <listitem>
  5313. <para>
  5314. Under the directory <filename>xyz</filename>, create a Makefile
  5315. <example>
  5316. <title>Sample Makefile for a driver xyz</title>
  5317. <programlisting>
  5318. <![CDATA[
  5319. ifndef SND_TOPDIR
  5320. SND_TOPDIR=../..
  5321. endif
  5322. include $(SND_TOPDIR)/toplevel.config
  5323. include $(SND_TOPDIR)/Makefile.conf
  5324. snd-xyz-objs := xyz.o abc.o def.o
  5325. obj-$(CONFIG_SND_XYZ) += snd-xyz.o
  5326. include $(SND_TOPDIR)/Rules.make
  5327. ]]>
  5328. </programlisting>
  5329. </example>
  5330. </para>
  5331. </listitem>
  5332. <listitem>
  5333. <para>
  5334. Create the Kconfig entry
  5335. </para>
  5336. <para>
  5337. This procedure is as same as in the last section.
  5338. </para>
  5339. </listitem>
  5340. <listitem>
  5341. <para>
  5342. Run cvscompile script to re-generate the configure script and
  5343. build the whole stuff again.
  5344. </para>
  5345. </listitem>
  5346. </orderedlist>
  5347. </para>
  5348. </section>
  5349. </chapter>
  5350. <!-- ****************************************************** -->
  5351. <!-- Useful Functions -->
  5352. <!-- ****************************************************** -->
  5353. <chapter id="useful-functions">
  5354. <title>Useful Functions</title>
  5355. <section id="useful-functions-snd-printk">
  5356. <title><function>snd_printk()</function> and friends</title>
  5357. <para>
  5358. ALSA provides a verbose version of the
  5359. <function>printk()</function> function. If a kernel config
  5360. <constant>CONFIG_SND_VERBOSE_PRINTK</constant> is set, this
  5361. function prints the given message together with the file name
  5362. and the line of the caller. The <constant>KERN_XXX</constant>
  5363. prefix is processed as
  5364. well as the original <function>printk()</function> does, so it's
  5365. recommended to add this prefix, e.g.
  5366. <informalexample>
  5367. <programlisting>
  5368. <![CDATA[
  5369. snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\n");
  5370. ]]>
  5371. </programlisting>
  5372. </informalexample>
  5373. </para>
  5374. <para>
  5375. There are also <function>printk()</function>'s for
  5376. debugging. <function>snd_printd()</function> can be used for
  5377. general debugging purposes. If
  5378. <constant>CONFIG_SND_DEBUG</constant> is set, this function is
  5379. compiled, and works just like
  5380. <function>snd_printk()</function>. If the ALSA is compiled
  5381. without the debugging flag, it's ignored.
  5382. </para>
  5383. <para>
  5384. <function>snd_printdd()</function> is compiled in only when
  5385. <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is set. Please note
  5386. that <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is not set as default
  5387. even if you configure the alsa-driver with
  5388. <option>--with-debug=full</option> option. You need to give
  5389. explicitly <option>--with-debug=detect</option> option instead.
  5390. </para>
  5391. </section>
  5392. <section id="useful-functions-snd-bug">
  5393. <title><function>snd_BUG()</function></title>
  5394. <para>
  5395. It shows the <computeroutput>BUG?</computeroutput> message and
  5396. stack trace as well as <function>snd_BUG_ON</function> at the point.
  5397. It's useful to show that a fatal error happens there.
  5398. </para>
  5399. <para>
  5400. When no debug flag is set, this macro is ignored.
  5401. </para>
  5402. </section>
  5403. <section id="useful-functions-snd-bug-on">
  5404. <title><function>snd_BUG_ON()</function></title>
  5405. <para>
  5406. <function>snd_BUG_ON()</function> macro is similar with
  5407. <function>WARN_ON()</function> macro. For example,
  5408. <informalexample>
  5409. <programlisting>
  5410. <![CDATA[
  5411. snd_BUG_ON(!pointer);
  5412. ]]>
  5413. </programlisting>
  5414. </informalexample>
  5415. or it can be used as the condition,
  5416. <informalexample>
  5417. <programlisting>
  5418. <![CDATA[
  5419. if (snd_BUG_ON(non_zero_is_bug))
  5420. return -EINVAL;
  5421. ]]>
  5422. </programlisting>
  5423. </informalexample>
  5424. </para>
  5425. <para>
  5426. The macro takes an conditional expression to evaluate.
  5427. When <constant>CONFIG_SND_DEBUG</constant>, is set, if the
  5428. expression is non-zero, it shows the warning message such as
  5429. <computeroutput>BUG? (xxx)</computeroutput>
  5430. normally followed by stack trace.
  5431. In both cases it returns the evaluated value.
  5432. </para>
  5433. </section>
  5434. </chapter>
  5435. <!-- ****************************************************** -->
  5436. <!-- Acknowledgments -->
  5437. <!-- ****************************************************** -->
  5438. <chapter id="acknowledgments">
  5439. <title>Acknowledgments</title>
  5440. <para>
  5441. I would like to thank Phil Kerr for his help for improvement and
  5442. corrections of this document.
  5443. </para>
  5444. <para>
  5445. Kevin Conder reformatted the original plain-text to the
  5446. DocBook format.
  5447. </para>
  5448. <para>
  5449. Giuliano Pochini corrected typos and contributed the example codes
  5450. in the hardware constraints section.
  5451. </para>
  5452. </chapter>
  5453. </book>