writing-an-alsa-driver.tmpl 199 KB

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