writing-an-alsa-driver.tmpl 192 KB

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