writing-an-alsa-driver.tmpl 198 KB

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