writing-an-alsa-driver.tmpl 200 KB

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