writing-an-alsa-driver.tmpl 201 KB

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