writing-an-alsa-driver.tmpl 193 KB

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