writing-an-alsa-driver.tmpl 197 KB

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