hdsp.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379
  1. /*
  2. * ALSA driver for RME Hammerfall DSP audio interface(s)
  3. *
  4. * Copyright (c) 2002 Paul Davis
  5. * Marcus Andersson
  6. * Thomas Charbonnel
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pci.h>
  27. #include <linux/firmware.h>
  28. #include <linux/module.h>
  29. #include <linux/math64.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/io.h>
  32. #include <sound/core.h>
  33. #include <sound/control.h>
  34. #include <sound/pcm.h>
  35. #include <sound/info.h>
  36. #include <sound/asoundef.h>
  37. #include <sound/rawmidi.h>
  38. #include <sound/hwdep.h>
  39. #include <sound/initval.h>
  40. #include <sound/hdsp.h>
  41. #include <asm/byteorder.h>
  42. #include <asm/current.h>
  43. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  44. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  45. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  46. module_param_array(index, int, NULL, 0444);
  47. MODULE_PARM_DESC(index, "Index value for RME Hammerfall DSP interface.");
  48. module_param_array(id, charp, NULL, 0444);
  49. MODULE_PARM_DESC(id, "ID string for RME Hammerfall DSP interface.");
  50. module_param_array(enable, bool, NULL, 0444);
  51. MODULE_PARM_DESC(enable, "Enable/disable specific Hammerfall DSP soundcards.");
  52. MODULE_AUTHOR("Paul Davis <paul@linuxaudiosystems.com>, Marcus Andersson, Thomas Charbonnel <thomas@undata.org>");
  53. MODULE_DESCRIPTION("RME Hammerfall DSP");
  54. MODULE_LICENSE("GPL");
  55. MODULE_SUPPORTED_DEVICE("{{RME Hammerfall-DSP},"
  56. "{RME HDSP-9652},"
  57. "{RME HDSP-9632}}");
  58. /*(DEBLOBBED)*/
  59. #define HDSP_MAX_CHANNELS 26
  60. #define HDSP_MAX_DS_CHANNELS 14
  61. #define HDSP_MAX_QS_CHANNELS 8
  62. #define DIGIFACE_SS_CHANNELS 26
  63. #define DIGIFACE_DS_CHANNELS 14
  64. #define MULTIFACE_SS_CHANNELS 18
  65. #define MULTIFACE_DS_CHANNELS 14
  66. #define H9652_SS_CHANNELS 26
  67. #define H9652_DS_CHANNELS 14
  68. /* This does not include possible Analog Extension Boards
  69. AEBs are detected at card initialization
  70. */
  71. #define H9632_SS_CHANNELS 12
  72. #define H9632_DS_CHANNELS 8
  73. #define H9632_QS_CHANNELS 4
  74. #define RPM_CHANNELS 6
  75. /* Write registers. These are defined as byte-offsets from the iobase value.
  76. */
  77. #define HDSP_resetPointer 0
  78. #define HDSP_freqReg 0
  79. #define HDSP_outputBufferAddress 32
  80. #define HDSP_inputBufferAddress 36
  81. #define HDSP_controlRegister 64
  82. #define HDSP_interruptConfirmation 96
  83. #define HDSP_outputEnable 128
  84. #define HDSP_control2Reg 256
  85. #define HDSP_midiDataOut0 352
  86. #define HDSP_midiDataOut1 356
  87. #define HDSP_fifoData 368
  88. #define HDSP_inputEnable 384
  89. /* Read registers. These are defined as byte-offsets from the iobase value
  90. */
  91. #define HDSP_statusRegister 0
  92. #define HDSP_timecode 128
  93. #define HDSP_status2Register 192
  94. #define HDSP_midiDataIn0 360
  95. #define HDSP_midiDataIn1 364
  96. #define HDSP_midiStatusOut0 384
  97. #define HDSP_midiStatusOut1 388
  98. #define HDSP_midiStatusIn0 392
  99. #define HDSP_midiStatusIn1 396
  100. #define HDSP_fifoStatus 400
  101. /* the meters are regular i/o-mapped registers, but offset
  102. considerably from the rest. the peak registers are reset
  103. when read; the least-significant 4 bits are full-scale counters;
  104. the actual peak value is in the most-significant 24 bits.
  105. */
  106. #define HDSP_playbackPeakLevel 4096 /* 26 * 32 bit values */
  107. #define HDSP_inputPeakLevel 4224 /* 26 * 32 bit values */
  108. #define HDSP_outputPeakLevel 4352 /* (26+2) * 32 bit values */
  109. #define HDSP_playbackRmsLevel 4612 /* 26 * 64 bit values */
  110. #define HDSP_inputRmsLevel 4868 /* 26 * 64 bit values */
  111. /* This is for H9652 cards
  112. Peak values are read downward from the base
  113. Rms values are read upward
  114. There are rms values for the outputs too
  115. 26*3 values are read in ss mode
  116. 14*3 in ds mode, with no gap between values
  117. */
  118. #define HDSP_9652_peakBase 7164
  119. #define HDSP_9652_rmsBase 4096
  120. /* c.f. the hdsp_9632_meters_t struct */
  121. #define HDSP_9632_metersBase 4096
  122. #define HDSP_IO_EXTENT 7168
  123. /* control2 register bits */
  124. #define HDSP_TMS 0x01
  125. #define HDSP_TCK 0x02
  126. #define HDSP_TDI 0x04
  127. #define HDSP_JTAG 0x08
  128. #define HDSP_PWDN 0x10
  129. #define HDSP_PROGRAM 0x020
  130. #define HDSP_CONFIG_MODE_0 0x040
  131. #define HDSP_CONFIG_MODE_1 0x080
  132. #define HDSP_VERSION_BIT (0x100 | HDSP_S_LOAD)
  133. #define HDSP_BIGENDIAN_MODE 0x200
  134. #define HDSP_RD_MULTIPLE 0x400
  135. #define HDSP_9652_ENABLE_MIXER 0x800
  136. #define HDSP_S200 0x800
  137. #define HDSP_S300 (0x100 | HDSP_S200) /* dummy, purpose of 0x100 unknown */
  138. #define HDSP_CYCLIC_MODE 0x1000
  139. #define HDSP_TDO 0x10000000
  140. #define HDSP_S_PROGRAM (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_0)
  141. #define HDSP_S_LOAD (HDSP_CYCLIC_MODE|HDSP_PROGRAM|HDSP_CONFIG_MODE_1)
  142. /* Control Register bits */
  143. #define HDSP_Start (1<<0) /* start engine */
  144. #define HDSP_Latency0 (1<<1) /* buffer size = 2^n where n is defined by Latency{2,1,0} */
  145. #define HDSP_Latency1 (1<<2) /* [ see above ] */
  146. #define HDSP_Latency2 (1<<3) /* [ see above ] */
  147. #define HDSP_ClockModeMaster (1<<4) /* 1=Master, 0=Slave/Autosync */
  148. #define HDSP_AudioInterruptEnable (1<<5) /* what do you think ? */
  149. #define HDSP_Frequency0 (1<<6) /* 0=44.1kHz/88.2kHz/176.4kHz 1=48kHz/96kHz/192kHz */
  150. #define HDSP_Frequency1 (1<<7) /* 0=32kHz/64kHz/128kHz */
  151. #define HDSP_DoubleSpeed (1<<8) /* 0=normal speed, 1=double speed */
  152. #define HDSP_SPDIFProfessional (1<<9) /* 0=consumer, 1=professional */
  153. #define HDSP_SPDIFEmphasis (1<<10) /* 0=none, 1=on */
  154. #define HDSP_SPDIFNonAudio (1<<11) /* 0=off, 1=on */
  155. #define HDSP_SPDIFOpticalOut (1<<12) /* 1=use 1st ADAT connector for SPDIF, 0=do not */
  156. #define HDSP_SyncRef2 (1<<13)
  157. #define HDSP_SPDIFInputSelect0 (1<<14)
  158. #define HDSP_SPDIFInputSelect1 (1<<15)
  159. #define HDSP_SyncRef0 (1<<16)
  160. #define HDSP_SyncRef1 (1<<17)
  161. #define HDSP_AnalogExtensionBoard (1<<18) /* For H9632 cards */
  162. #define HDSP_XLRBreakoutCable (1<<20) /* For H9632 cards */
  163. #define HDSP_Midi0InterruptEnable (1<<22)
  164. #define HDSP_Midi1InterruptEnable (1<<23)
  165. #define HDSP_LineOut (1<<24)
  166. #define HDSP_ADGain0 (1<<25) /* From here : H9632 specific */
  167. #define HDSP_ADGain1 (1<<26)
  168. #define HDSP_DAGain0 (1<<27)
  169. #define HDSP_DAGain1 (1<<28)
  170. #define HDSP_PhoneGain0 (1<<29)
  171. #define HDSP_PhoneGain1 (1<<30)
  172. #define HDSP_QuadSpeed (1<<31)
  173. /* RPM uses some of the registers for special purposes */
  174. #define HDSP_RPM_Inp12 0x04A00
  175. #define HDSP_RPM_Inp12_Phon_6dB 0x00800 /* Dolby */
  176. #define HDSP_RPM_Inp12_Phon_0dB 0x00000 /* .. */
  177. #define HDSP_RPM_Inp12_Phon_n6dB 0x04000 /* inp_0 */
  178. #define HDSP_RPM_Inp12_Line_0dB 0x04200 /* Dolby+PRO */
  179. #define HDSP_RPM_Inp12_Line_n6dB 0x00200 /* PRO */
  180. #define HDSP_RPM_Inp34 0x32000
  181. #define HDSP_RPM_Inp34_Phon_6dB 0x20000 /* SyncRef1 */
  182. #define HDSP_RPM_Inp34_Phon_0dB 0x00000 /* .. */
  183. #define HDSP_RPM_Inp34_Phon_n6dB 0x02000 /* SyncRef2 */
  184. #define HDSP_RPM_Inp34_Line_0dB 0x30000 /* SyncRef1+SyncRef0 */
  185. #define HDSP_RPM_Inp34_Line_n6dB 0x10000 /* SyncRef0 */
  186. #define HDSP_RPM_Bypass 0x01000
  187. #define HDSP_RPM_Disconnect 0x00001
  188. #define HDSP_ADGainMask (HDSP_ADGain0|HDSP_ADGain1)
  189. #define HDSP_ADGainMinus10dBV HDSP_ADGainMask
  190. #define HDSP_ADGainPlus4dBu (HDSP_ADGain0)
  191. #define HDSP_ADGainLowGain 0
  192. #define HDSP_DAGainMask (HDSP_DAGain0|HDSP_DAGain1)
  193. #define HDSP_DAGainHighGain HDSP_DAGainMask
  194. #define HDSP_DAGainPlus4dBu (HDSP_DAGain0)
  195. #define HDSP_DAGainMinus10dBV 0
  196. #define HDSP_PhoneGainMask (HDSP_PhoneGain0|HDSP_PhoneGain1)
  197. #define HDSP_PhoneGain0dB HDSP_PhoneGainMask
  198. #define HDSP_PhoneGainMinus6dB (HDSP_PhoneGain0)
  199. #define HDSP_PhoneGainMinus12dB 0
  200. #define HDSP_LatencyMask (HDSP_Latency0|HDSP_Latency1|HDSP_Latency2)
  201. #define HDSP_FrequencyMask (HDSP_Frequency0|HDSP_Frequency1|HDSP_DoubleSpeed|HDSP_QuadSpeed)
  202. #define HDSP_SPDIFInputMask (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
  203. #define HDSP_SPDIFInputADAT1 0
  204. #define HDSP_SPDIFInputCoaxial (HDSP_SPDIFInputSelect0)
  205. #define HDSP_SPDIFInputCdrom (HDSP_SPDIFInputSelect1)
  206. #define HDSP_SPDIFInputAES (HDSP_SPDIFInputSelect0|HDSP_SPDIFInputSelect1)
  207. #define HDSP_SyncRefMask (HDSP_SyncRef0|HDSP_SyncRef1|HDSP_SyncRef2)
  208. #define HDSP_SyncRef_ADAT1 0
  209. #define HDSP_SyncRef_ADAT2 (HDSP_SyncRef0)
  210. #define HDSP_SyncRef_ADAT3 (HDSP_SyncRef1)
  211. #define HDSP_SyncRef_SPDIF (HDSP_SyncRef0|HDSP_SyncRef1)
  212. #define HDSP_SyncRef_WORD (HDSP_SyncRef2)
  213. #define HDSP_SyncRef_ADAT_SYNC (HDSP_SyncRef0|HDSP_SyncRef2)
  214. /* Sample Clock Sources */
  215. #define HDSP_CLOCK_SOURCE_AUTOSYNC 0
  216. #define HDSP_CLOCK_SOURCE_INTERNAL_32KHZ 1
  217. #define HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ 2
  218. #define HDSP_CLOCK_SOURCE_INTERNAL_48KHZ 3
  219. #define HDSP_CLOCK_SOURCE_INTERNAL_64KHZ 4
  220. #define HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ 5
  221. #define HDSP_CLOCK_SOURCE_INTERNAL_96KHZ 6
  222. #define HDSP_CLOCK_SOURCE_INTERNAL_128KHZ 7
  223. #define HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ 8
  224. #define HDSP_CLOCK_SOURCE_INTERNAL_192KHZ 9
  225. /* Preferred sync reference choices - used by "pref_sync_ref" control switch */
  226. #define HDSP_SYNC_FROM_WORD 0
  227. #define HDSP_SYNC_FROM_SPDIF 1
  228. #define HDSP_SYNC_FROM_ADAT1 2
  229. #define HDSP_SYNC_FROM_ADAT_SYNC 3
  230. #define HDSP_SYNC_FROM_ADAT2 4
  231. #define HDSP_SYNC_FROM_ADAT3 5
  232. /* SyncCheck status */
  233. #define HDSP_SYNC_CHECK_NO_LOCK 0
  234. #define HDSP_SYNC_CHECK_LOCK 1
  235. #define HDSP_SYNC_CHECK_SYNC 2
  236. /* AutoSync references - used by "autosync_ref" control switch */
  237. #define HDSP_AUTOSYNC_FROM_WORD 0
  238. #define HDSP_AUTOSYNC_FROM_ADAT_SYNC 1
  239. #define HDSP_AUTOSYNC_FROM_SPDIF 2
  240. #define HDSP_AUTOSYNC_FROM_NONE 3
  241. #define HDSP_AUTOSYNC_FROM_ADAT1 4
  242. #define HDSP_AUTOSYNC_FROM_ADAT2 5
  243. #define HDSP_AUTOSYNC_FROM_ADAT3 6
  244. /* Possible sources of S/PDIF input */
  245. #define HDSP_SPDIFIN_OPTICAL 0 /* optical (ADAT1) */
  246. #define HDSP_SPDIFIN_COAXIAL 1 /* coaxial (RCA) */
  247. #define HDSP_SPDIFIN_INTERNAL 2 /* internal (CDROM) */
  248. #define HDSP_SPDIFIN_AES 3 /* xlr for H9632 (AES)*/
  249. #define HDSP_Frequency32KHz HDSP_Frequency0
  250. #define HDSP_Frequency44_1KHz HDSP_Frequency1
  251. #define HDSP_Frequency48KHz (HDSP_Frequency1|HDSP_Frequency0)
  252. #define HDSP_Frequency64KHz (HDSP_DoubleSpeed|HDSP_Frequency0)
  253. #define HDSP_Frequency88_2KHz (HDSP_DoubleSpeed|HDSP_Frequency1)
  254. #define HDSP_Frequency96KHz (HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
  255. /* For H9632 cards */
  256. #define HDSP_Frequency128KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency0)
  257. #define HDSP_Frequency176_4KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1)
  258. #define HDSP_Frequency192KHz (HDSP_QuadSpeed|HDSP_DoubleSpeed|HDSP_Frequency1|HDSP_Frequency0)
  259. /* RME says n = 104857600000000, but in the windows MADI driver, I see:
  260. return 104857600000000 / rate; // 100 MHz
  261. return 110100480000000 / rate; // 105 MHz
  262. */
  263. #define DDS_NUMERATOR 104857600000000ULL; /* = 2^20 * 10^8 */
  264. #define hdsp_encode_latency(x) (((x)<<1) & HDSP_LatencyMask)
  265. #define hdsp_decode_latency(x) (((x) & HDSP_LatencyMask)>>1)
  266. #define hdsp_encode_spdif_in(x) (((x)&0x3)<<14)
  267. #define hdsp_decode_spdif_in(x) (((x)>>14)&0x3)
  268. /* Status Register bits */
  269. #define HDSP_audioIRQPending (1<<0)
  270. #define HDSP_Lock2 (1<<1) /* this is for Digiface and H9652 */
  271. #define HDSP_spdifFrequency3 HDSP_Lock2 /* this is for H9632 only */
  272. #define HDSP_Lock1 (1<<2)
  273. #define HDSP_Lock0 (1<<3)
  274. #define HDSP_SPDIFSync (1<<4)
  275. #define HDSP_TimecodeLock (1<<5)
  276. #define HDSP_BufferPositionMask 0x000FFC0 /* Bit 6..15 : h/w buffer pointer */
  277. #define HDSP_Sync2 (1<<16)
  278. #define HDSP_Sync1 (1<<17)
  279. #define HDSP_Sync0 (1<<18)
  280. #define HDSP_DoubleSpeedStatus (1<<19)
  281. #define HDSP_ConfigError (1<<20)
  282. #define HDSP_DllError (1<<21)
  283. #define HDSP_spdifFrequency0 (1<<22)
  284. #define HDSP_spdifFrequency1 (1<<23)
  285. #define HDSP_spdifFrequency2 (1<<24)
  286. #define HDSP_SPDIFErrorFlag (1<<25)
  287. #define HDSP_BufferID (1<<26)
  288. #define HDSP_TimecodeSync (1<<27)
  289. #define HDSP_AEBO (1<<28) /* H9632 specific Analog Extension Boards */
  290. #define HDSP_AEBI (1<<29) /* 0 = present, 1 = absent */
  291. #define HDSP_midi0IRQPending (1<<30)
  292. #define HDSP_midi1IRQPending (1<<31)
  293. #define HDSP_spdifFrequencyMask (HDSP_spdifFrequency0|HDSP_spdifFrequency1|HDSP_spdifFrequency2)
  294. #define HDSP_spdifFrequencyMask_9632 (HDSP_spdifFrequency0|\
  295. HDSP_spdifFrequency1|\
  296. HDSP_spdifFrequency2|\
  297. HDSP_spdifFrequency3)
  298. #define HDSP_spdifFrequency32KHz (HDSP_spdifFrequency0)
  299. #define HDSP_spdifFrequency44_1KHz (HDSP_spdifFrequency1)
  300. #define HDSP_spdifFrequency48KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency1)
  301. #define HDSP_spdifFrequency64KHz (HDSP_spdifFrequency2)
  302. #define HDSP_spdifFrequency88_2KHz (HDSP_spdifFrequency0|HDSP_spdifFrequency2)
  303. #define HDSP_spdifFrequency96KHz (HDSP_spdifFrequency2|HDSP_spdifFrequency1)
  304. /* This is for H9632 cards */
  305. #define HDSP_spdifFrequency128KHz (HDSP_spdifFrequency0|\
  306. HDSP_spdifFrequency1|\
  307. HDSP_spdifFrequency2)
  308. #define HDSP_spdifFrequency176_4KHz HDSP_spdifFrequency3
  309. #define HDSP_spdifFrequency192KHz (HDSP_spdifFrequency3|HDSP_spdifFrequency0)
  310. /* Status2 Register bits */
  311. #define HDSP_version0 (1<<0)
  312. #define HDSP_version1 (1<<1)
  313. #define HDSP_version2 (1<<2)
  314. #define HDSP_wc_lock (1<<3)
  315. #define HDSP_wc_sync (1<<4)
  316. #define HDSP_inp_freq0 (1<<5)
  317. #define HDSP_inp_freq1 (1<<6)
  318. #define HDSP_inp_freq2 (1<<7)
  319. #define HDSP_SelSyncRef0 (1<<8)
  320. #define HDSP_SelSyncRef1 (1<<9)
  321. #define HDSP_SelSyncRef2 (1<<10)
  322. #define HDSP_wc_valid (HDSP_wc_lock|HDSP_wc_sync)
  323. #define HDSP_systemFrequencyMask (HDSP_inp_freq0|HDSP_inp_freq1|HDSP_inp_freq2)
  324. #define HDSP_systemFrequency32 (HDSP_inp_freq0)
  325. #define HDSP_systemFrequency44_1 (HDSP_inp_freq1)
  326. #define HDSP_systemFrequency48 (HDSP_inp_freq0|HDSP_inp_freq1)
  327. #define HDSP_systemFrequency64 (HDSP_inp_freq2)
  328. #define HDSP_systemFrequency88_2 (HDSP_inp_freq0|HDSP_inp_freq2)
  329. #define HDSP_systemFrequency96 (HDSP_inp_freq1|HDSP_inp_freq2)
  330. /* FIXME : more values for 9632 cards ? */
  331. #define HDSP_SelSyncRefMask (HDSP_SelSyncRef0|HDSP_SelSyncRef1|HDSP_SelSyncRef2)
  332. #define HDSP_SelSyncRef_ADAT1 0
  333. #define HDSP_SelSyncRef_ADAT2 (HDSP_SelSyncRef0)
  334. #define HDSP_SelSyncRef_ADAT3 (HDSP_SelSyncRef1)
  335. #define HDSP_SelSyncRef_SPDIF (HDSP_SelSyncRef0|HDSP_SelSyncRef1)
  336. #define HDSP_SelSyncRef_WORD (HDSP_SelSyncRef2)
  337. #define HDSP_SelSyncRef_ADAT_SYNC (HDSP_SelSyncRef0|HDSP_SelSyncRef2)
  338. /* Card state flags */
  339. #define HDSP_InitializationComplete (1<<0)
  340. #define HDSP_FirmwareLoaded (1<<1)
  341. #define HDSP_FirmwareCached (1<<2)
  342. /* FIFO wait times, defined in terms of 1/10ths of msecs */
  343. #define HDSP_LONG_WAIT 5000
  344. #define HDSP_SHORT_WAIT 30
  345. #define UNITY_GAIN 32768
  346. #define MINUS_INFINITY_GAIN 0
  347. /* the size of a substream (1 mono data stream) */
  348. #define HDSP_CHANNEL_BUFFER_SAMPLES (16*1024)
  349. #define HDSP_CHANNEL_BUFFER_BYTES (4*HDSP_CHANNEL_BUFFER_SAMPLES)
  350. /* the size of the area we need to allocate for DMA transfers. the
  351. size is the same regardless of the number of channels - the
  352. Multiface still uses the same memory area.
  353. Note that we allocate 1 more channel than is apparently needed
  354. because the h/w seems to write 1 byte beyond the end of the last
  355. page. Sigh.
  356. */
  357. #define HDSP_DMA_AREA_BYTES ((HDSP_MAX_CHANNELS+1) * HDSP_CHANNEL_BUFFER_BYTES)
  358. #define HDSP_DMA_AREA_KILOBYTES (HDSP_DMA_AREA_BYTES/1024)
  359. #define HDSP_FIRMWARE_SIZE (24413 * 4)
  360. struct hdsp_9632_meters {
  361. u32 input_peak[16];
  362. u32 playback_peak[16];
  363. u32 output_peak[16];
  364. u32 xxx_peak[16];
  365. u32 padding[64];
  366. u32 input_rms_low[16];
  367. u32 playback_rms_low[16];
  368. u32 output_rms_low[16];
  369. u32 xxx_rms_low[16];
  370. u32 input_rms_high[16];
  371. u32 playback_rms_high[16];
  372. u32 output_rms_high[16];
  373. u32 xxx_rms_high[16];
  374. };
  375. struct hdsp_midi {
  376. struct hdsp *hdsp;
  377. int id;
  378. struct snd_rawmidi *rmidi;
  379. struct snd_rawmidi_substream *input;
  380. struct snd_rawmidi_substream *output;
  381. char istimer; /* timer in use */
  382. struct timer_list timer;
  383. spinlock_t lock;
  384. int pending;
  385. };
  386. struct hdsp {
  387. spinlock_t lock;
  388. struct snd_pcm_substream *capture_substream;
  389. struct snd_pcm_substream *playback_substream;
  390. struct hdsp_midi midi[2];
  391. struct tasklet_struct midi_tasklet;
  392. int use_midi_tasklet;
  393. int precise_ptr;
  394. u32 control_register; /* cached value */
  395. u32 control2_register; /* cached value */
  396. u32 creg_spdif;
  397. u32 creg_spdif_stream;
  398. int clock_source_locked;
  399. char *card_name; /* digiface/multiface/rpm */
  400. enum HDSP_IO_Type io_type; /* ditto, but for code use */
  401. unsigned short firmware_rev;
  402. unsigned short state; /* stores state bits */
  403. const struct firmware *firmware;
  404. u32 *fw_uploaded;
  405. size_t period_bytes; /* guess what this is */
  406. unsigned char max_channels;
  407. unsigned char qs_in_channels; /* quad speed mode for H9632 */
  408. unsigned char ds_in_channels;
  409. unsigned char ss_in_channels; /* different for multiface/digiface */
  410. unsigned char qs_out_channels;
  411. unsigned char ds_out_channels;
  412. unsigned char ss_out_channels;
  413. struct snd_dma_buffer capture_dma_buf;
  414. struct snd_dma_buffer playback_dma_buf;
  415. unsigned char *capture_buffer; /* suitably aligned address */
  416. unsigned char *playback_buffer; /* suitably aligned address */
  417. pid_t capture_pid;
  418. pid_t playback_pid;
  419. int running;
  420. int system_sample_rate;
  421. char *channel_map;
  422. int dev;
  423. int irq;
  424. unsigned long port;
  425. void __iomem *iobase;
  426. struct snd_card *card;
  427. struct snd_pcm *pcm;
  428. struct snd_hwdep *hwdep;
  429. struct pci_dev *pci;
  430. struct snd_kcontrol *spdif_ctl;
  431. unsigned short mixer_matrix[HDSP_MATRIX_MIXER_SIZE];
  432. unsigned int dds_value; /* last value written to freq register */
  433. };
  434. /* These tables map the ALSA channels 1..N to the channels that we
  435. need to use in order to find the relevant channel buffer. RME
  436. refer to this kind of mapping as between "the ADAT channel and
  437. the DMA channel." We index it using the logical audio channel,
  438. and the value is the DMA channel (i.e. channel buffer number)
  439. where the data for that channel can be read/written from/to.
  440. */
  441. static char channel_map_df_ss[HDSP_MAX_CHANNELS] = {
  442. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  443. 18, 19, 20, 21, 22, 23, 24, 25
  444. };
  445. static char channel_map_mf_ss[HDSP_MAX_CHANNELS] = { /* Multiface */
  446. /* Analog */
  447. 0, 1, 2, 3, 4, 5, 6, 7,
  448. /* ADAT 2 */
  449. 16, 17, 18, 19, 20, 21, 22, 23,
  450. /* SPDIF */
  451. 24, 25,
  452. -1, -1, -1, -1, -1, -1, -1, -1
  453. };
  454. static char channel_map_ds[HDSP_MAX_CHANNELS] = {
  455. /* ADAT channels are remapped */
  456. 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,
  457. /* channels 12 and 13 are S/PDIF */
  458. 24, 25,
  459. /* others don't exist */
  460. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  461. };
  462. static char channel_map_H9632_ss[HDSP_MAX_CHANNELS] = {
  463. /* ADAT channels */
  464. 0, 1, 2, 3, 4, 5, 6, 7,
  465. /* SPDIF */
  466. 8, 9,
  467. /* Analog */
  468. 10, 11,
  469. /* AO4S-192 and AI4S-192 extension boards */
  470. 12, 13, 14, 15,
  471. /* others don't exist */
  472. -1, -1, -1, -1, -1, -1, -1, -1,
  473. -1, -1
  474. };
  475. static char channel_map_H9632_ds[HDSP_MAX_CHANNELS] = {
  476. /* ADAT */
  477. 1, 3, 5, 7,
  478. /* SPDIF */
  479. 8, 9,
  480. /* Analog */
  481. 10, 11,
  482. /* AO4S-192 and AI4S-192 extension boards */
  483. 12, 13, 14, 15,
  484. /* others don't exist */
  485. -1, -1, -1, -1, -1, -1, -1, -1,
  486. -1, -1, -1, -1, -1, -1
  487. };
  488. static char channel_map_H9632_qs[HDSP_MAX_CHANNELS] = {
  489. /* ADAT is disabled in this mode */
  490. /* SPDIF */
  491. 8, 9,
  492. /* Analog */
  493. 10, 11,
  494. /* AO4S-192 and AI4S-192 extension boards */
  495. 12, 13, 14, 15,
  496. /* others don't exist */
  497. -1, -1, -1, -1, -1, -1, -1, -1,
  498. -1, -1, -1, -1, -1, -1, -1, -1,
  499. -1, -1
  500. };
  501. static int snd_hammerfall_get_buffer(struct pci_dev *pci, struct snd_dma_buffer *dmab, size_t size)
  502. {
  503. dmab->dev.type = SNDRV_DMA_TYPE_DEV;
  504. dmab->dev.dev = snd_dma_pci_data(pci);
  505. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  506. size, dmab) < 0)
  507. return -ENOMEM;
  508. return 0;
  509. }
  510. static void snd_hammerfall_free_buffer(struct snd_dma_buffer *dmab, struct pci_dev *pci)
  511. {
  512. if (dmab->area)
  513. snd_dma_free_pages(dmab);
  514. }
  515. static const struct pci_device_id snd_hdsp_ids[] = {
  516. {
  517. .vendor = PCI_VENDOR_ID_XILINX,
  518. .device = PCI_DEVICE_ID_XILINX_HAMMERFALL_DSP,
  519. .subvendor = PCI_ANY_ID,
  520. .subdevice = PCI_ANY_ID,
  521. }, /* RME Hammerfall-DSP */
  522. { 0, },
  523. };
  524. MODULE_DEVICE_TABLE(pci, snd_hdsp_ids);
  525. /* prototypes */
  526. static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp);
  527. static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp);
  528. static int snd_hdsp_enable_io (struct hdsp *hdsp);
  529. static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp);
  530. static void snd_hdsp_initialize_channels (struct hdsp *hdsp);
  531. static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout);
  532. static int hdsp_autosync_ref(struct hdsp *hdsp);
  533. static int snd_hdsp_set_defaults(struct hdsp *hdsp);
  534. static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp);
  535. static int hdsp_playback_to_output_key (struct hdsp *hdsp, int in, int out)
  536. {
  537. switch (hdsp->io_type) {
  538. case Multiface:
  539. case Digiface:
  540. case RPM:
  541. default:
  542. if (hdsp->firmware_rev == 0xa)
  543. return (64 * out) + (32 + (in));
  544. else
  545. return (52 * out) + (26 + (in));
  546. case H9632:
  547. return (32 * out) + (16 + (in));
  548. case H9652:
  549. return (52 * out) + (26 + (in));
  550. }
  551. }
  552. static int hdsp_input_to_output_key (struct hdsp *hdsp, int in, int out)
  553. {
  554. switch (hdsp->io_type) {
  555. case Multiface:
  556. case Digiface:
  557. case RPM:
  558. default:
  559. if (hdsp->firmware_rev == 0xa)
  560. return (64 * out) + in;
  561. else
  562. return (52 * out) + in;
  563. case H9632:
  564. return (32 * out) + in;
  565. case H9652:
  566. return (52 * out) + in;
  567. }
  568. }
  569. static void hdsp_write(struct hdsp *hdsp, int reg, int val)
  570. {
  571. writel(val, hdsp->iobase + reg);
  572. }
  573. static unsigned int hdsp_read(struct hdsp *hdsp, int reg)
  574. {
  575. return readl (hdsp->iobase + reg);
  576. }
  577. static int hdsp_check_for_iobox (struct hdsp *hdsp)
  578. {
  579. int i;
  580. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return 0;
  581. for (i = 0; i < 500; i++) {
  582. if (0 == (hdsp_read(hdsp, HDSP_statusRegister) &
  583. HDSP_ConfigError)) {
  584. if (i) {
  585. dev_dbg(hdsp->card->dev,
  586. "IO box found after %d ms\n",
  587. (20 * i));
  588. }
  589. return 0;
  590. }
  591. msleep(20);
  592. }
  593. dev_err(hdsp->card->dev, "no IO box connected!\n");
  594. hdsp->state &= ~HDSP_FirmwareLoaded;
  595. return -EIO;
  596. }
  597. static int hdsp_wait_for_iobox(struct hdsp *hdsp, unsigned int loops,
  598. unsigned int delay)
  599. {
  600. unsigned int i;
  601. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  602. return 0;
  603. for (i = 0; i != loops; ++i) {
  604. if (hdsp_read(hdsp, HDSP_statusRegister) & HDSP_ConfigError)
  605. msleep(delay);
  606. else {
  607. dev_dbg(hdsp->card->dev, "iobox found after %ums!\n",
  608. i * delay);
  609. return 0;
  610. }
  611. }
  612. dev_info(hdsp->card->dev, "no IO box connected!\n");
  613. hdsp->state &= ~HDSP_FirmwareLoaded;
  614. return -EIO;
  615. }
  616. static int snd_hdsp_load_firmware_from_cache(struct hdsp *hdsp) {
  617. int i;
  618. unsigned long flags;
  619. const u32 *cache;
  620. if (hdsp->fw_uploaded)
  621. cache = hdsp->fw_uploaded;
  622. else {
  623. if (!hdsp->firmware)
  624. return -ENODEV;
  625. cache = (u32 *)hdsp->firmware->data;
  626. if (!cache)
  627. return -ENODEV;
  628. }
  629. if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
  630. dev_info(hdsp->card->dev, "loading firmware\n");
  631. hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_PROGRAM);
  632. hdsp_write (hdsp, HDSP_fifoData, 0);
  633. if (hdsp_fifo_wait (hdsp, 0, HDSP_LONG_WAIT)) {
  634. dev_info(hdsp->card->dev,
  635. "timeout waiting for download preparation\n");
  636. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
  637. return -EIO;
  638. }
  639. hdsp_write (hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  640. for (i = 0; i < HDSP_FIRMWARE_SIZE / 4; ++i) {
  641. hdsp_write(hdsp, HDSP_fifoData, cache[i]);
  642. if (hdsp_fifo_wait (hdsp, 127, HDSP_LONG_WAIT)) {
  643. dev_info(hdsp->card->dev,
  644. "timeout during firmware loading\n");
  645. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
  646. return -EIO;
  647. }
  648. }
  649. hdsp_fifo_wait(hdsp, 3, HDSP_LONG_WAIT);
  650. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200);
  651. ssleep(3);
  652. #ifdef SNDRV_BIG_ENDIAN
  653. hdsp->control2_register = HDSP_BIGENDIAN_MODE;
  654. #else
  655. hdsp->control2_register = 0;
  656. #endif
  657. hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
  658. dev_info(hdsp->card->dev, "finished firmware loading\n");
  659. }
  660. if (hdsp->state & HDSP_InitializationComplete) {
  661. dev_info(hdsp->card->dev,
  662. "firmware loaded from cache, restoring defaults\n");
  663. spin_lock_irqsave(&hdsp->lock, flags);
  664. snd_hdsp_set_defaults(hdsp);
  665. spin_unlock_irqrestore(&hdsp->lock, flags);
  666. }
  667. hdsp->state |= HDSP_FirmwareLoaded;
  668. return 0;
  669. }
  670. static int hdsp_get_iobox_version (struct hdsp *hdsp)
  671. {
  672. if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
  673. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  674. hdsp_write(hdsp, HDSP_fifoData, 0);
  675. if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
  676. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
  677. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  678. }
  679. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S200 | HDSP_PROGRAM);
  680. hdsp_write (hdsp, HDSP_fifoData, 0);
  681. if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
  682. hdsp->io_type = Multiface;
  683. dev_info(hdsp->card->dev, "Multiface found\n");
  684. return 0;
  685. }
  686. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  687. hdsp_write(hdsp, HDSP_fifoData, 0);
  688. if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
  689. hdsp->io_type = Digiface;
  690. dev_info(hdsp->card->dev, "Digiface found\n");
  691. return 0;
  692. }
  693. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
  694. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  695. hdsp_write(hdsp, HDSP_fifoData, 0);
  696. if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) == 0) {
  697. hdsp->io_type = Multiface;
  698. dev_info(hdsp->card->dev, "Multiface found\n");
  699. return 0;
  700. }
  701. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S300);
  702. hdsp_write(hdsp, HDSP_control2Reg, HDSP_S_LOAD);
  703. hdsp_write(hdsp, HDSP_fifoData, 0);
  704. if (hdsp_fifo_wait(hdsp, 0, HDSP_SHORT_WAIT) < 0) {
  705. hdsp->io_type = Multiface;
  706. dev_info(hdsp->card->dev, "Multiface found\n");
  707. return 0;
  708. }
  709. hdsp->io_type = RPM;
  710. dev_info(hdsp->card->dev, "RPM found\n");
  711. return 0;
  712. } else {
  713. /* firmware was already loaded, get iobox type */
  714. if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
  715. hdsp->io_type = RPM;
  716. else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
  717. hdsp->io_type = Multiface;
  718. else
  719. hdsp->io_type = Digiface;
  720. }
  721. return 0;
  722. }
  723. static int hdsp_request_fw_loader(struct hdsp *hdsp);
  724. static int hdsp_check_for_firmware (struct hdsp *hdsp, int load_on_demand)
  725. {
  726. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  727. return 0;
  728. if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
  729. hdsp->state &= ~HDSP_FirmwareLoaded;
  730. if (! load_on_demand)
  731. return -EIO;
  732. dev_err(hdsp->card->dev, "firmware not present.\n");
  733. /* try to load firmware */
  734. if (! (hdsp->state & HDSP_FirmwareCached)) {
  735. if (! hdsp_request_fw_loader(hdsp))
  736. return 0;
  737. dev_err(hdsp->card->dev,
  738. "No firmware loaded nor cached, please upload firmware.\n");
  739. return -EIO;
  740. }
  741. if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
  742. dev_err(hdsp->card->dev,
  743. "Firmware loading from cache failed, please upload manually.\n");
  744. return -EIO;
  745. }
  746. }
  747. return 0;
  748. }
  749. static int hdsp_fifo_wait(struct hdsp *hdsp, int count, int timeout)
  750. {
  751. int i;
  752. /* the fifoStatus registers reports on how many words
  753. are available in the command FIFO.
  754. */
  755. for (i = 0; i < timeout; i++) {
  756. if ((int)(hdsp_read (hdsp, HDSP_fifoStatus) & 0xff) <= count)
  757. return 0;
  758. /* not very friendly, but we only do this during a firmware
  759. load and changing the mixer, so we just put up with it.
  760. */
  761. udelay (100);
  762. }
  763. dev_warn(hdsp->card->dev,
  764. "wait for FIFO status <= %d failed after %d iterations\n",
  765. count, timeout);
  766. return -1;
  767. }
  768. static int hdsp_read_gain (struct hdsp *hdsp, unsigned int addr)
  769. {
  770. if (addr >= HDSP_MATRIX_MIXER_SIZE)
  771. return 0;
  772. return hdsp->mixer_matrix[addr];
  773. }
  774. static int hdsp_write_gain(struct hdsp *hdsp, unsigned int addr, unsigned short data)
  775. {
  776. unsigned int ad;
  777. if (addr >= HDSP_MATRIX_MIXER_SIZE)
  778. return -1;
  779. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) {
  780. /* from martin bjornsen:
  781. "You can only write dwords to the
  782. mixer memory which contain two
  783. mixer values in the low and high
  784. word. So if you want to change
  785. value 0 you have to read value 1
  786. from the cache and write both to
  787. the first dword in the mixer
  788. memory."
  789. */
  790. if (hdsp->io_type == H9632 && addr >= 512)
  791. return 0;
  792. if (hdsp->io_type == H9652 && addr >= 1352)
  793. return 0;
  794. hdsp->mixer_matrix[addr] = data;
  795. /* `addr' addresses a 16-bit wide address, but
  796. the address space accessed via hdsp_write
  797. uses byte offsets. put another way, addr
  798. varies from 0 to 1351, but to access the
  799. corresponding memory location, we need
  800. to access 0 to 2703 ...
  801. */
  802. ad = addr/2;
  803. hdsp_write (hdsp, 4096 + (ad*4),
  804. (hdsp->mixer_matrix[(addr&0x7fe)+1] << 16) +
  805. hdsp->mixer_matrix[addr&0x7fe]);
  806. return 0;
  807. } else {
  808. ad = (addr << 16) + data;
  809. if (hdsp_fifo_wait(hdsp, 127, HDSP_LONG_WAIT))
  810. return -1;
  811. hdsp_write (hdsp, HDSP_fifoData, ad);
  812. hdsp->mixer_matrix[addr] = data;
  813. }
  814. return 0;
  815. }
  816. static int snd_hdsp_use_is_exclusive(struct hdsp *hdsp)
  817. {
  818. unsigned long flags;
  819. int ret = 1;
  820. spin_lock_irqsave(&hdsp->lock, flags);
  821. if ((hdsp->playback_pid != hdsp->capture_pid) &&
  822. (hdsp->playback_pid >= 0) && (hdsp->capture_pid >= 0))
  823. ret = 0;
  824. spin_unlock_irqrestore(&hdsp->lock, flags);
  825. return ret;
  826. }
  827. static int hdsp_spdif_sample_rate(struct hdsp *hdsp)
  828. {
  829. unsigned int status = hdsp_read(hdsp, HDSP_statusRegister);
  830. unsigned int rate_bits = (status & HDSP_spdifFrequencyMask);
  831. /* For the 9632, the mask is different */
  832. if (hdsp->io_type == H9632)
  833. rate_bits = (status & HDSP_spdifFrequencyMask_9632);
  834. if (status & HDSP_SPDIFErrorFlag)
  835. return 0;
  836. switch (rate_bits) {
  837. case HDSP_spdifFrequency32KHz: return 32000;
  838. case HDSP_spdifFrequency44_1KHz: return 44100;
  839. case HDSP_spdifFrequency48KHz: return 48000;
  840. case HDSP_spdifFrequency64KHz: return 64000;
  841. case HDSP_spdifFrequency88_2KHz: return 88200;
  842. case HDSP_spdifFrequency96KHz: return 96000;
  843. case HDSP_spdifFrequency128KHz:
  844. if (hdsp->io_type == H9632) return 128000;
  845. break;
  846. case HDSP_spdifFrequency176_4KHz:
  847. if (hdsp->io_type == H9632) return 176400;
  848. break;
  849. case HDSP_spdifFrequency192KHz:
  850. if (hdsp->io_type == H9632) return 192000;
  851. break;
  852. default:
  853. break;
  854. }
  855. dev_warn(hdsp->card->dev,
  856. "unknown spdif frequency status; bits = 0x%x, status = 0x%x\n",
  857. rate_bits, status);
  858. return 0;
  859. }
  860. static int hdsp_external_sample_rate(struct hdsp *hdsp)
  861. {
  862. unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
  863. unsigned int rate_bits = status2 & HDSP_systemFrequencyMask;
  864. /* For the 9632 card, there seems to be no bit for indicating external
  865. * sample rate greater than 96kHz. The card reports the corresponding
  866. * single speed. So the best means seems to get spdif rate when
  867. * autosync reference is spdif */
  868. if (hdsp->io_type == H9632 &&
  869. hdsp_autosync_ref(hdsp) == HDSP_AUTOSYNC_FROM_SPDIF)
  870. return hdsp_spdif_sample_rate(hdsp);
  871. switch (rate_bits) {
  872. case HDSP_systemFrequency32: return 32000;
  873. case HDSP_systemFrequency44_1: return 44100;
  874. case HDSP_systemFrequency48: return 48000;
  875. case HDSP_systemFrequency64: return 64000;
  876. case HDSP_systemFrequency88_2: return 88200;
  877. case HDSP_systemFrequency96: return 96000;
  878. default:
  879. return 0;
  880. }
  881. }
  882. static void hdsp_compute_period_size(struct hdsp *hdsp)
  883. {
  884. hdsp->period_bytes = 1 << ((hdsp_decode_latency(hdsp->control_register) + 8));
  885. }
  886. static snd_pcm_uframes_t hdsp_hw_pointer(struct hdsp *hdsp)
  887. {
  888. int position;
  889. position = hdsp_read(hdsp, HDSP_statusRegister);
  890. if (!hdsp->precise_ptr)
  891. return (position & HDSP_BufferID) ? (hdsp->period_bytes / 4) : 0;
  892. position &= HDSP_BufferPositionMask;
  893. position /= 4;
  894. position &= (hdsp->period_bytes/2) - 1;
  895. return position;
  896. }
  897. static void hdsp_reset_hw_pointer(struct hdsp *hdsp)
  898. {
  899. hdsp_write (hdsp, HDSP_resetPointer, 0);
  900. if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
  901. /* HDSP_resetPointer = HDSP_freqReg, which is strange and
  902. * requires (?) to write again DDS value after a reset pointer
  903. * (at least, it works like this) */
  904. hdsp_write (hdsp, HDSP_freqReg, hdsp->dds_value);
  905. }
  906. static void hdsp_start_audio(struct hdsp *s)
  907. {
  908. s->control_register |= (HDSP_AudioInterruptEnable | HDSP_Start);
  909. hdsp_write(s, HDSP_controlRegister, s->control_register);
  910. }
  911. static void hdsp_stop_audio(struct hdsp *s)
  912. {
  913. s->control_register &= ~(HDSP_Start | HDSP_AudioInterruptEnable);
  914. hdsp_write(s, HDSP_controlRegister, s->control_register);
  915. }
  916. static void hdsp_silence_playback(struct hdsp *hdsp)
  917. {
  918. memset(hdsp->playback_buffer, 0, HDSP_DMA_AREA_BYTES);
  919. }
  920. static int hdsp_set_interrupt_interval(struct hdsp *s, unsigned int frames)
  921. {
  922. int n;
  923. spin_lock_irq(&s->lock);
  924. frames >>= 7;
  925. n = 0;
  926. while (frames) {
  927. n++;
  928. frames >>= 1;
  929. }
  930. s->control_register &= ~HDSP_LatencyMask;
  931. s->control_register |= hdsp_encode_latency(n);
  932. hdsp_write(s, HDSP_controlRegister, s->control_register);
  933. hdsp_compute_period_size(s);
  934. spin_unlock_irq(&s->lock);
  935. return 0;
  936. }
  937. static void hdsp_set_dds_value(struct hdsp *hdsp, int rate)
  938. {
  939. u64 n;
  940. if (rate >= 112000)
  941. rate /= 4;
  942. else if (rate >= 56000)
  943. rate /= 2;
  944. n = DDS_NUMERATOR;
  945. n = div_u64(n, rate);
  946. /* n should be less than 2^32 for being written to FREQ register */
  947. snd_BUG_ON(n >> 32);
  948. /* HDSP_freqReg and HDSP_resetPointer are the same, so keep the DDS
  949. value to write it after a reset */
  950. hdsp->dds_value = n;
  951. hdsp_write(hdsp, HDSP_freqReg, hdsp->dds_value);
  952. }
  953. static int hdsp_set_rate(struct hdsp *hdsp, int rate, int called_internally)
  954. {
  955. int reject_if_open = 0;
  956. int current_rate;
  957. int rate_bits;
  958. /* ASSUMPTION: hdsp->lock is either held, or
  959. there is no need for it (e.g. during module
  960. initialization).
  961. */
  962. if (!(hdsp->control_register & HDSP_ClockModeMaster)) {
  963. if (called_internally) {
  964. /* request from ctl or card initialization */
  965. dev_err(hdsp->card->dev,
  966. "device is not running as a clock master: cannot set sample rate.\n");
  967. return -1;
  968. } else {
  969. /* hw_param request while in AutoSync mode */
  970. int external_freq = hdsp_external_sample_rate(hdsp);
  971. int spdif_freq = hdsp_spdif_sample_rate(hdsp);
  972. if ((spdif_freq == external_freq*2) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
  973. dev_info(hdsp->card->dev,
  974. "Detected ADAT in double speed mode\n");
  975. else if (hdsp->io_type == H9632 && (spdif_freq == external_freq*4) && (hdsp_autosync_ref(hdsp) >= HDSP_AUTOSYNC_FROM_ADAT1))
  976. dev_info(hdsp->card->dev,
  977. "Detected ADAT in quad speed mode\n");
  978. else if (rate != external_freq) {
  979. dev_info(hdsp->card->dev,
  980. "No AutoSync source for requested rate\n");
  981. return -1;
  982. }
  983. }
  984. }
  985. current_rate = hdsp->system_sample_rate;
  986. /* Changing from a "single speed" to a "double speed" rate is
  987. not allowed if any substreams are open. This is because
  988. such a change causes a shift in the location of
  989. the DMA buffers and a reduction in the number of available
  990. buffers.
  991. Note that a similar but essentially insoluble problem
  992. exists for externally-driven rate changes. All we can do
  993. is to flag rate changes in the read/write routines. */
  994. if (rate > 96000 && hdsp->io_type != H9632)
  995. return -EINVAL;
  996. switch (rate) {
  997. case 32000:
  998. if (current_rate > 48000)
  999. reject_if_open = 1;
  1000. rate_bits = HDSP_Frequency32KHz;
  1001. break;
  1002. case 44100:
  1003. if (current_rate > 48000)
  1004. reject_if_open = 1;
  1005. rate_bits = HDSP_Frequency44_1KHz;
  1006. break;
  1007. case 48000:
  1008. if (current_rate > 48000)
  1009. reject_if_open = 1;
  1010. rate_bits = HDSP_Frequency48KHz;
  1011. break;
  1012. case 64000:
  1013. if (current_rate <= 48000 || current_rate > 96000)
  1014. reject_if_open = 1;
  1015. rate_bits = HDSP_Frequency64KHz;
  1016. break;
  1017. case 88200:
  1018. if (current_rate <= 48000 || current_rate > 96000)
  1019. reject_if_open = 1;
  1020. rate_bits = HDSP_Frequency88_2KHz;
  1021. break;
  1022. case 96000:
  1023. if (current_rate <= 48000 || current_rate > 96000)
  1024. reject_if_open = 1;
  1025. rate_bits = HDSP_Frequency96KHz;
  1026. break;
  1027. case 128000:
  1028. if (current_rate < 128000)
  1029. reject_if_open = 1;
  1030. rate_bits = HDSP_Frequency128KHz;
  1031. break;
  1032. case 176400:
  1033. if (current_rate < 128000)
  1034. reject_if_open = 1;
  1035. rate_bits = HDSP_Frequency176_4KHz;
  1036. break;
  1037. case 192000:
  1038. if (current_rate < 128000)
  1039. reject_if_open = 1;
  1040. rate_bits = HDSP_Frequency192KHz;
  1041. break;
  1042. default:
  1043. return -EINVAL;
  1044. }
  1045. if (reject_if_open && (hdsp->capture_pid >= 0 || hdsp->playback_pid >= 0)) {
  1046. dev_warn(hdsp->card->dev,
  1047. "cannot change speed mode (capture PID = %d, playback PID = %d)\n",
  1048. hdsp->capture_pid,
  1049. hdsp->playback_pid);
  1050. return -EBUSY;
  1051. }
  1052. hdsp->control_register &= ~HDSP_FrequencyMask;
  1053. hdsp->control_register |= rate_bits;
  1054. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1055. /* For HDSP9632 rev 152, need to set DDS value in FREQ register */
  1056. if (hdsp->io_type == H9632 && hdsp->firmware_rev >= 152)
  1057. hdsp_set_dds_value(hdsp, rate);
  1058. if (rate >= 128000) {
  1059. hdsp->channel_map = channel_map_H9632_qs;
  1060. } else if (rate > 48000) {
  1061. if (hdsp->io_type == H9632)
  1062. hdsp->channel_map = channel_map_H9632_ds;
  1063. else
  1064. hdsp->channel_map = channel_map_ds;
  1065. } else {
  1066. switch (hdsp->io_type) {
  1067. case RPM:
  1068. case Multiface:
  1069. hdsp->channel_map = channel_map_mf_ss;
  1070. break;
  1071. case Digiface:
  1072. case H9652:
  1073. hdsp->channel_map = channel_map_df_ss;
  1074. break;
  1075. case H9632:
  1076. hdsp->channel_map = channel_map_H9632_ss;
  1077. break;
  1078. default:
  1079. /* should never happen */
  1080. break;
  1081. }
  1082. }
  1083. hdsp->system_sample_rate = rate;
  1084. return 0;
  1085. }
  1086. /*----------------------------------------------------------------------------
  1087. MIDI
  1088. ----------------------------------------------------------------------------*/
  1089. static unsigned char snd_hdsp_midi_read_byte (struct hdsp *hdsp, int id)
  1090. {
  1091. /* the hardware already does the relevant bit-mask with 0xff */
  1092. if (id)
  1093. return hdsp_read(hdsp, HDSP_midiDataIn1);
  1094. else
  1095. return hdsp_read(hdsp, HDSP_midiDataIn0);
  1096. }
  1097. static void snd_hdsp_midi_write_byte (struct hdsp *hdsp, int id, int val)
  1098. {
  1099. /* the hardware already does the relevant bit-mask with 0xff */
  1100. if (id)
  1101. hdsp_write(hdsp, HDSP_midiDataOut1, val);
  1102. else
  1103. hdsp_write(hdsp, HDSP_midiDataOut0, val);
  1104. }
  1105. static int snd_hdsp_midi_input_available (struct hdsp *hdsp, int id)
  1106. {
  1107. if (id)
  1108. return (hdsp_read(hdsp, HDSP_midiStatusIn1) & 0xff);
  1109. else
  1110. return (hdsp_read(hdsp, HDSP_midiStatusIn0) & 0xff);
  1111. }
  1112. static int snd_hdsp_midi_output_possible (struct hdsp *hdsp, int id)
  1113. {
  1114. int fifo_bytes_used;
  1115. if (id)
  1116. fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut1) & 0xff;
  1117. else
  1118. fifo_bytes_used = hdsp_read(hdsp, HDSP_midiStatusOut0) & 0xff;
  1119. if (fifo_bytes_used < 128)
  1120. return 128 - fifo_bytes_used;
  1121. else
  1122. return 0;
  1123. }
  1124. static void snd_hdsp_flush_midi_input (struct hdsp *hdsp, int id)
  1125. {
  1126. while (snd_hdsp_midi_input_available (hdsp, id))
  1127. snd_hdsp_midi_read_byte (hdsp, id);
  1128. }
  1129. static int snd_hdsp_midi_output_write (struct hdsp_midi *hmidi)
  1130. {
  1131. unsigned long flags;
  1132. int n_pending;
  1133. int to_write;
  1134. int i;
  1135. unsigned char buf[128];
  1136. /* Output is not interrupt driven */
  1137. spin_lock_irqsave (&hmidi->lock, flags);
  1138. if (hmidi->output) {
  1139. if (!snd_rawmidi_transmit_empty (hmidi->output)) {
  1140. if ((n_pending = snd_hdsp_midi_output_possible (hmidi->hdsp, hmidi->id)) > 0) {
  1141. if (n_pending > (int)sizeof (buf))
  1142. n_pending = sizeof (buf);
  1143. if ((to_write = snd_rawmidi_transmit (hmidi->output, buf, n_pending)) > 0) {
  1144. for (i = 0; i < to_write; ++i)
  1145. snd_hdsp_midi_write_byte (hmidi->hdsp, hmidi->id, buf[i]);
  1146. }
  1147. }
  1148. }
  1149. }
  1150. spin_unlock_irqrestore (&hmidi->lock, flags);
  1151. return 0;
  1152. }
  1153. static int snd_hdsp_midi_input_read (struct hdsp_midi *hmidi)
  1154. {
  1155. unsigned char buf[128]; /* this buffer is designed to match the MIDI input FIFO size */
  1156. unsigned long flags;
  1157. int n_pending;
  1158. int i;
  1159. spin_lock_irqsave (&hmidi->lock, flags);
  1160. if ((n_pending = snd_hdsp_midi_input_available (hmidi->hdsp, hmidi->id)) > 0) {
  1161. if (hmidi->input) {
  1162. if (n_pending > (int)sizeof (buf))
  1163. n_pending = sizeof (buf);
  1164. for (i = 0; i < n_pending; ++i)
  1165. buf[i] = snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
  1166. if (n_pending)
  1167. snd_rawmidi_receive (hmidi->input, buf, n_pending);
  1168. } else {
  1169. /* flush the MIDI input FIFO */
  1170. while (--n_pending)
  1171. snd_hdsp_midi_read_byte (hmidi->hdsp, hmidi->id);
  1172. }
  1173. }
  1174. hmidi->pending = 0;
  1175. if (hmidi->id)
  1176. hmidi->hdsp->control_register |= HDSP_Midi1InterruptEnable;
  1177. else
  1178. hmidi->hdsp->control_register |= HDSP_Midi0InterruptEnable;
  1179. hdsp_write(hmidi->hdsp, HDSP_controlRegister, hmidi->hdsp->control_register);
  1180. spin_unlock_irqrestore (&hmidi->lock, flags);
  1181. return snd_hdsp_midi_output_write (hmidi);
  1182. }
  1183. static void snd_hdsp_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1184. {
  1185. struct hdsp *hdsp;
  1186. struct hdsp_midi *hmidi;
  1187. unsigned long flags;
  1188. u32 ie;
  1189. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1190. hdsp = hmidi->hdsp;
  1191. ie = hmidi->id ? HDSP_Midi1InterruptEnable : HDSP_Midi0InterruptEnable;
  1192. spin_lock_irqsave (&hdsp->lock, flags);
  1193. if (up) {
  1194. if (!(hdsp->control_register & ie)) {
  1195. snd_hdsp_flush_midi_input (hdsp, hmidi->id);
  1196. hdsp->control_register |= ie;
  1197. }
  1198. } else {
  1199. hdsp->control_register &= ~ie;
  1200. tasklet_kill(&hdsp->midi_tasklet);
  1201. }
  1202. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1203. spin_unlock_irqrestore (&hdsp->lock, flags);
  1204. }
  1205. static void snd_hdsp_midi_output_timer(unsigned long data)
  1206. {
  1207. struct hdsp_midi *hmidi = (struct hdsp_midi *) data;
  1208. unsigned long flags;
  1209. snd_hdsp_midi_output_write(hmidi);
  1210. spin_lock_irqsave (&hmidi->lock, flags);
  1211. /* this does not bump hmidi->istimer, because the
  1212. kernel automatically removed the timer when it
  1213. expired, and we are now adding it back, thus
  1214. leaving istimer wherever it was set before.
  1215. */
  1216. if (hmidi->istimer)
  1217. mod_timer(&hmidi->timer, 1 + jiffies);
  1218. spin_unlock_irqrestore (&hmidi->lock, flags);
  1219. }
  1220. static void snd_hdsp_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  1221. {
  1222. struct hdsp_midi *hmidi;
  1223. unsigned long flags;
  1224. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1225. spin_lock_irqsave (&hmidi->lock, flags);
  1226. if (up) {
  1227. if (!hmidi->istimer) {
  1228. setup_timer(&hmidi->timer, snd_hdsp_midi_output_timer,
  1229. (unsigned long) hmidi);
  1230. mod_timer(&hmidi->timer, 1 + jiffies);
  1231. hmidi->istimer++;
  1232. }
  1233. } else {
  1234. if (hmidi->istimer && --hmidi->istimer <= 0)
  1235. del_timer (&hmidi->timer);
  1236. }
  1237. spin_unlock_irqrestore (&hmidi->lock, flags);
  1238. if (up)
  1239. snd_hdsp_midi_output_write(hmidi);
  1240. }
  1241. static int snd_hdsp_midi_input_open(struct snd_rawmidi_substream *substream)
  1242. {
  1243. struct hdsp_midi *hmidi;
  1244. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1245. spin_lock_irq (&hmidi->lock);
  1246. snd_hdsp_flush_midi_input (hmidi->hdsp, hmidi->id);
  1247. hmidi->input = substream;
  1248. spin_unlock_irq (&hmidi->lock);
  1249. return 0;
  1250. }
  1251. static int snd_hdsp_midi_output_open(struct snd_rawmidi_substream *substream)
  1252. {
  1253. struct hdsp_midi *hmidi;
  1254. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1255. spin_lock_irq (&hmidi->lock);
  1256. hmidi->output = substream;
  1257. spin_unlock_irq (&hmidi->lock);
  1258. return 0;
  1259. }
  1260. static int snd_hdsp_midi_input_close(struct snd_rawmidi_substream *substream)
  1261. {
  1262. struct hdsp_midi *hmidi;
  1263. snd_hdsp_midi_input_trigger (substream, 0);
  1264. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1265. spin_lock_irq (&hmidi->lock);
  1266. hmidi->input = NULL;
  1267. spin_unlock_irq (&hmidi->lock);
  1268. return 0;
  1269. }
  1270. static int snd_hdsp_midi_output_close(struct snd_rawmidi_substream *substream)
  1271. {
  1272. struct hdsp_midi *hmidi;
  1273. snd_hdsp_midi_output_trigger (substream, 0);
  1274. hmidi = (struct hdsp_midi *) substream->rmidi->private_data;
  1275. spin_lock_irq (&hmidi->lock);
  1276. hmidi->output = NULL;
  1277. spin_unlock_irq (&hmidi->lock);
  1278. return 0;
  1279. }
  1280. static struct snd_rawmidi_ops snd_hdsp_midi_output =
  1281. {
  1282. .open = snd_hdsp_midi_output_open,
  1283. .close = snd_hdsp_midi_output_close,
  1284. .trigger = snd_hdsp_midi_output_trigger,
  1285. };
  1286. static struct snd_rawmidi_ops snd_hdsp_midi_input =
  1287. {
  1288. .open = snd_hdsp_midi_input_open,
  1289. .close = snd_hdsp_midi_input_close,
  1290. .trigger = snd_hdsp_midi_input_trigger,
  1291. };
  1292. static int snd_hdsp_create_midi (struct snd_card *card, struct hdsp *hdsp, int id)
  1293. {
  1294. char buf[40];
  1295. hdsp->midi[id].id = id;
  1296. hdsp->midi[id].rmidi = NULL;
  1297. hdsp->midi[id].input = NULL;
  1298. hdsp->midi[id].output = NULL;
  1299. hdsp->midi[id].hdsp = hdsp;
  1300. hdsp->midi[id].istimer = 0;
  1301. hdsp->midi[id].pending = 0;
  1302. spin_lock_init (&hdsp->midi[id].lock);
  1303. snprintf(buf, sizeof(buf), "%s MIDI %d", card->shortname, id + 1);
  1304. if (snd_rawmidi_new (card, buf, id, 1, 1, &hdsp->midi[id].rmidi) < 0)
  1305. return -1;
  1306. sprintf(hdsp->midi[id].rmidi->name, "HDSP MIDI %d", id+1);
  1307. hdsp->midi[id].rmidi->private_data = &hdsp->midi[id];
  1308. snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_hdsp_midi_output);
  1309. snd_rawmidi_set_ops (hdsp->midi[id].rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_hdsp_midi_input);
  1310. hdsp->midi[id].rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
  1311. SNDRV_RAWMIDI_INFO_INPUT |
  1312. SNDRV_RAWMIDI_INFO_DUPLEX;
  1313. return 0;
  1314. }
  1315. /*-----------------------------------------------------------------------------
  1316. Control Interface
  1317. ----------------------------------------------------------------------------*/
  1318. static u32 snd_hdsp_convert_from_aes(struct snd_aes_iec958 *aes)
  1319. {
  1320. u32 val = 0;
  1321. val |= (aes->status[0] & IEC958_AES0_PROFESSIONAL) ? HDSP_SPDIFProfessional : 0;
  1322. val |= (aes->status[0] & IEC958_AES0_NONAUDIO) ? HDSP_SPDIFNonAudio : 0;
  1323. if (val & HDSP_SPDIFProfessional)
  1324. val |= (aes->status[0] & IEC958_AES0_PRO_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
  1325. else
  1326. val |= (aes->status[0] & IEC958_AES0_CON_EMPHASIS_5015) ? HDSP_SPDIFEmphasis : 0;
  1327. return val;
  1328. }
  1329. static void snd_hdsp_convert_to_aes(struct snd_aes_iec958 *aes, u32 val)
  1330. {
  1331. aes->status[0] = ((val & HDSP_SPDIFProfessional) ? IEC958_AES0_PROFESSIONAL : 0) |
  1332. ((val & HDSP_SPDIFNonAudio) ? IEC958_AES0_NONAUDIO : 0);
  1333. if (val & HDSP_SPDIFProfessional)
  1334. aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_PRO_EMPHASIS_5015 : 0;
  1335. else
  1336. aes->status[0] |= (val & HDSP_SPDIFEmphasis) ? IEC958_AES0_CON_EMPHASIS_5015 : 0;
  1337. }
  1338. static int snd_hdsp_control_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1339. {
  1340. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1341. uinfo->count = 1;
  1342. return 0;
  1343. }
  1344. static int snd_hdsp_control_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1345. {
  1346. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1347. snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif);
  1348. return 0;
  1349. }
  1350. static int snd_hdsp_control_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1351. {
  1352. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1353. int change;
  1354. u32 val;
  1355. val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
  1356. spin_lock_irq(&hdsp->lock);
  1357. change = val != hdsp->creg_spdif;
  1358. hdsp->creg_spdif = val;
  1359. spin_unlock_irq(&hdsp->lock);
  1360. return change;
  1361. }
  1362. static int snd_hdsp_control_spdif_stream_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1363. {
  1364. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1365. uinfo->count = 1;
  1366. return 0;
  1367. }
  1368. static int snd_hdsp_control_spdif_stream_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1369. {
  1370. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1371. snd_hdsp_convert_to_aes(&ucontrol->value.iec958, hdsp->creg_spdif_stream);
  1372. return 0;
  1373. }
  1374. static int snd_hdsp_control_spdif_stream_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1375. {
  1376. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1377. int change;
  1378. u32 val;
  1379. val = snd_hdsp_convert_from_aes(&ucontrol->value.iec958);
  1380. spin_lock_irq(&hdsp->lock);
  1381. change = val != hdsp->creg_spdif_stream;
  1382. hdsp->creg_spdif_stream = val;
  1383. hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
  1384. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= val);
  1385. spin_unlock_irq(&hdsp->lock);
  1386. return change;
  1387. }
  1388. static int snd_hdsp_control_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1389. {
  1390. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1391. uinfo->count = 1;
  1392. return 0;
  1393. }
  1394. static int snd_hdsp_control_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1395. {
  1396. ucontrol->value.iec958.status[0] = kcontrol->private_value;
  1397. return 0;
  1398. }
  1399. #define HDSP_SPDIF_IN(xname, xindex) \
  1400. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1401. .name = xname, \
  1402. .index = xindex, \
  1403. .info = snd_hdsp_info_spdif_in, \
  1404. .get = snd_hdsp_get_spdif_in, \
  1405. .put = snd_hdsp_put_spdif_in }
  1406. static unsigned int hdsp_spdif_in(struct hdsp *hdsp)
  1407. {
  1408. return hdsp_decode_spdif_in(hdsp->control_register & HDSP_SPDIFInputMask);
  1409. }
  1410. static int hdsp_set_spdif_input(struct hdsp *hdsp, int in)
  1411. {
  1412. hdsp->control_register &= ~HDSP_SPDIFInputMask;
  1413. hdsp->control_register |= hdsp_encode_spdif_in(in);
  1414. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1415. return 0;
  1416. }
  1417. static int snd_hdsp_info_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1418. {
  1419. static const char * const texts[4] = {
  1420. "Optical", "Coaxial", "Internal", "AES"
  1421. };
  1422. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1423. return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 4 : 3,
  1424. texts);
  1425. }
  1426. static int snd_hdsp_get_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1427. {
  1428. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1429. ucontrol->value.enumerated.item[0] = hdsp_spdif_in(hdsp);
  1430. return 0;
  1431. }
  1432. static int snd_hdsp_put_spdif_in(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1433. {
  1434. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1435. int change;
  1436. unsigned int val;
  1437. if (!snd_hdsp_use_is_exclusive(hdsp))
  1438. return -EBUSY;
  1439. val = ucontrol->value.enumerated.item[0] % ((hdsp->io_type == H9632) ? 4 : 3);
  1440. spin_lock_irq(&hdsp->lock);
  1441. change = val != hdsp_spdif_in(hdsp);
  1442. if (change)
  1443. hdsp_set_spdif_input(hdsp, val);
  1444. spin_unlock_irq(&hdsp->lock);
  1445. return change;
  1446. }
  1447. #define HDSP_TOGGLE_SETTING(xname, xindex) \
  1448. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1449. .name = xname, \
  1450. .private_value = xindex, \
  1451. .info = snd_hdsp_info_toggle_setting, \
  1452. .get = snd_hdsp_get_toggle_setting, \
  1453. .put = snd_hdsp_put_toggle_setting \
  1454. }
  1455. static int hdsp_toggle_setting(struct hdsp *hdsp, u32 regmask)
  1456. {
  1457. return (hdsp->control_register & regmask) ? 1 : 0;
  1458. }
  1459. static int hdsp_set_toggle_setting(struct hdsp *hdsp, u32 regmask, int out)
  1460. {
  1461. if (out)
  1462. hdsp->control_register |= regmask;
  1463. else
  1464. hdsp->control_register &= ~regmask;
  1465. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1466. return 0;
  1467. }
  1468. #define snd_hdsp_info_toggle_setting snd_ctl_boolean_mono_info
  1469. static int snd_hdsp_get_toggle_setting(struct snd_kcontrol *kcontrol,
  1470. struct snd_ctl_elem_value *ucontrol)
  1471. {
  1472. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1473. u32 regmask = kcontrol->private_value;
  1474. spin_lock_irq(&hdsp->lock);
  1475. ucontrol->value.integer.value[0] = hdsp_toggle_setting(hdsp, regmask);
  1476. spin_unlock_irq(&hdsp->lock);
  1477. return 0;
  1478. }
  1479. static int snd_hdsp_put_toggle_setting(struct snd_kcontrol *kcontrol,
  1480. struct snd_ctl_elem_value *ucontrol)
  1481. {
  1482. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1483. u32 regmask = kcontrol->private_value;
  1484. int change;
  1485. unsigned int val;
  1486. if (!snd_hdsp_use_is_exclusive(hdsp))
  1487. return -EBUSY;
  1488. val = ucontrol->value.integer.value[0] & 1;
  1489. spin_lock_irq(&hdsp->lock);
  1490. change = (int) val != hdsp_toggle_setting(hdsp, regmask);
  1491. if (change)
  1492. hdsp_set_toggle_setting(hdsp, regmask, val);
  1493. spin_unlock_irq(&hdsp->lock);
  1494. return change;
  1495. }
  1496. #define HDSP_SPDIF_SAMPLE_RATE(xname, xindex) \
  1497. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1498. .name = xname, \
  1499. .index = xindex, \
  1500. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  1501. .info = snd_hdsp_info_spdif_sample_rate, \
  1502. .get = snd_hdsp_get_spdif_sample_rate \
  1503. }
  1504. static int snd_hdsp_info_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1505. {
  1506. static const char * const texts[] = {
  1507. "32000", "44100", "48000", "64000", "88200", "96000",
  1508. "None", "128000", "176400", "192000"
  1509. };
  1510. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1511. return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
  1512. texts);
  1513. }
  1514. static int snd_hdsp_get_spdif_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1515. {
  1516. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1517. switch (hdsp_spdif_sample_rate(hdsp)) {
  1518. case 32000:
  1519. ucontrol->value.enumerated.item[0] = 0;
  1520. break;
  1521. case 44100:
  1522. ucontrol->value.enumerated.item[0] = 1;
  1523. break;
  1524. case 48000:
  1525. ucontrol->value.enumerated.item[0] = 2;
  1526. break;
  1527. case 64000:
  1528. ucontrol->value.enumerated.item[0] = 3;
  1529. break;
  1530. case 88200:
  1531. ucontrol->value.enumerated.item[0] = 4;
  1532. break;
  1533. case 96000:
  1534. ucontrol->value.enumerated.item[0] = 5;
  1535. break;
  1536. case 128000:
  1537. ucontrol->value.enumerated.item[0] = 7;
  1538. break;
  1539. case 176400:
  1540. ucontrol->value.enumerated.item[0] = 8;
  1541. break;
  1542. case 192000:
  1543. ucontrol->value.enumerated.item[0] = 9;
  1544. break;
  1545. default:
  1546. ucontrol->value.enumerated.item[0] = 6;
  1547. }
  1548. return 0;
  1549. }
  1550. #define HDSP_SYSTEM_SAMPLE_RATE(xname, xindex) \
  1551. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1552. .name = xname, \
  1553. .index = xindex, \
  1554. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  1555. .info = snd_hdsp_info_system_sample_rate, \
  1556. .get = snd_hdsp_get_system_sample_rate \
  1557. }
  1558. static int snd_hdsp_info_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1559. {
  1560. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1561. uinfo->count = 1;
  1562. return 0;
  1563. }
  1564. static int snd_hdsp_get_system_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1565. {
  1566. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1567. ucontrol->value.enumerated.item[0] = hdsp->system_sample_rate;
  1568. return 0;
  1569. }
  1570. #define HDSP_AUTOSYNC_SAMPLE_RATE(xname, xindex) \
  1571. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1572. .name = xname, \
  1573. .index = xindex, \
  1574. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  1575. .info = snd_hdsp_info_autosync_sample_rate, \
  1576. .get = snd_hdsp_get_autosync_sample_rate \
  1577. }
  1578. static int snd_hdsp_info_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1579. {
  1580. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1581. static const char * const texts[] = {
  1582. "32000", "44100", "48000", "64000", "88200", "96000",
  1583. "None", "128000", "176400", "192000"
  1584. };
  1585. return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
  1586. texts);
  1587. }
  1588. static int snd_hdsp_get_autosync_sample_rate(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1589. {
  1590. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1591. switch (hdsp_external_sample_rate(hdsp)) {
  1592. case 32000:
  1593. ucontrol->value.enumerated.item[0] = 0;
  1594. break;
  1595. case 44100:
  1596. ucontrol->value.enumerated.item[0] = 1;
  1597. break;
  1598. case 48000:
  1599. ucontrol->value.enumerated.item[0] = 2;
  1600. break;
  1601. case 64000:
  1602. ucontrol->value.enumerated.item[0] = 3;
  1603. break;
  1604. case 88200:
  1605. ucontrol->value.enumerated.item[0] = 4;
  1606. break;
  1607. case 96000:
  1608. ucontrol->value.enumerated.item[0] = 5;
  1609. break;
  1610. case 128000:
  1611. ucontrol->value.enumerated.item[0] = 7;
  1612. break;
  1613. case 176400:
  1614. ucontrol->value.enumerated.item[0] = 8;
  1615. break;
  1616. case 192000:
  1617. ucontrol->value.enumerated.item[0] = 9;
  1618. break;
  1619. default:
  1620. ucontrol->value.enumerated.item[0] = 6;
  1621. }
  1622. return 0;
  1623. }
  1624. #define HDSP_SYSTEM_CLOCK_MODE(xname, xindex) \
  1625. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1626. .name = xname, \
  1627. .index = xindex, \
  1628. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  1629. .info = snd_hdsp_info_system_clock_mode, \
  1630. .get = snd_hdsp_get_system_clock_mode \
  1631. }
  1632. static int hdsp_system_clock_mode(struct hdsp *hdsp)
  1633. {
  1634. if (hdsp->control_register & HDSP_ClockModeMaster)
  1635. return 0;
  1636. else if (hdsp_external_sample_rate(hdsp) != hdsp->system_sample_rate)
  1637. return 0;
  1638. return 1;
  1639. }
  1640. static int snd_hdsp_info_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1641. {
  1642. static const char * const texts[] = {"Master", "Slave" };
  1643. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  1644. }
  1645. static int snd_hdsp_get_system_clock_mode(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1646. {
  1647. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1648. ucontrol->value.enumerated.item[0] = hdsp_system_clock_mode(hdsp);
  1649. return 0;
  1650. }
  1651. #define HDSP_CLOCK_SOURCE(xname, xindex) \
  1652. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1653. .name = xname, \
  1654. .index = xindex, \
  1655. .info = snd_hdsp_info_clock_source, \
  1656. .get = snd_hdsp_get_clock_source, \
  1657. .put = snd_hdsp_put_clock_source \
  1658. }
  1659. static int hdsp_clock_source(struct hdsp *hdsp)
  1660. {
  1661. if (hdsp->control_register & HDSP_ClockModeMaster) {
  1662. switch (hdsp->system_sample_rate) {
  1663. case 32000:
  1664. return 1;
  1665. case 44100:
  1666. return 2;
  1667. case 48000:
  1668. return 3;
  1669. case 64000:
  1670. return 4;
  1671. case 88200:
  1672. return 5;
  1673. case 96000:
  1674. return 6;
  1675. case 128000:
  1676. return 7;
  1677. case 176400:
  1678. return 8;
  1679. case 192000:
  1680. return 9;
  1681. default:
  1682. return 3;
  1683. }
  1684. } else {
  1685. return 0;
  1686. }
  1687. }
  1688. static int hdsp_set_clock_source(struct hdsp *hdsp, int mode)
  1689. {
  1690. int rate;
  1691. switch (mode) {
  1692. case HDSP_CLOCK_SOURCE_AUTOSYNC:
  1693. if (hdsp_external_sample_rate(hdsp) != 0) {
  1694. if (!hdsp_set_rate(hdsp, hdsp_external_sample_rate(hdsp), 1)) {
  1695. hdsp->control_register &= ~HDSP_ClockModeMaster;
  1696. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1697. return 0;
  1698. }
  1699. }
  1700. return -1;
  1701. case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
  1702. rate = 32000;
  1703. break;
  1704. case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
  1705. rate = 44100;
  1706. break;
  1707. case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
  1708. rate = 48000;
  1709. break;
  1710. case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
  1711. rate = 64000;
  1712. break;
  1713. case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
  1714. rate = 88200;
  1715. break;
  1716. case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
  1717. rate = 96000;
  1718. break;
  1719. case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
  1720. rate = 128000;
  1721. break;
  1722. case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
  1723. rate = 176400;
  1724. break;
  1725. case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
  1726. rate = 192000;
  1727. break;
  1728. default:
  1729. rate = 48000;
  1730. }
  1731. hdsp->control_register |= HDSP_ClockModeMaster;
  1732. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1733. hdsp_set_rate(hdsp, rate, 1);
  1734. return 0;
  1735. }
  1736. static int snd_hdsp_info_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1737. {
  1738. static const char * const texts[] = {
  1739. "AutoSync", "Internal 32.0 kHz", "Internal 44.1 kHz",
  1740. "Internal 48.0 kHz", "Internal 64.0 kHz", "Internal 88.2 kHz",
  1741. "Internal 96.0 kHz", "Internal 128 kHz", "Internal 176.4 kHz",
  1742. "Internal 192.0 KHz"
  1743. };
  1744. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1745. return snd_ctl_enum_info(uinfo, 1, (hdsp->io_type == H9632) ? 10 : 7,
  1746. texts);
  1747. }
  1748. static int snd_hdsp_get_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1749. {
  1750. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1751. ucontrol->value.enumerated.item[0] = hdsp_clock_source(hdsp);
  1752. return 0;
  1753. }
  1754. static int snd_hdsp_put_clock_source(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1755. {
  1756. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1757. int change;
  1758. int val;
  1759. if (!snd_hdsp_use_is_exclusive(hdsp))
  1760. return -EBUSY;
  1761. val = ucontrol->value.enumerated.item[0];
  1762. if (val < 0) val = 0;
  1763. if (hdsp->io_type == H9632) {
  1764. if (val > 9)
  1765. val = 9;
  1766. } else {
  1767. if (val > 6)
  1768. val = 6;
  1769. }
  1770. spin_lock_irq(&hdsp->lock);
  1771. if (val != hdsp_clock_source(hdsp))
  1772. change = (hdsp_set_clock_source(hdsp, val) == 0) ? 1 : 0;
  1773. else
  1774. change = 0;
  1775. spin_unlock_irq(&hdsp->lock);
  1776. return change;
  1777. }
  1778. #define snd_hdsp_info_clock_source_lock snd_ctl_boolean_mono_info
  1779. static int snd_hdsp_get_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1780. {
  1781. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1782. ucontrol->value.integer.value[0] = hdsp->clock_source_locked;
  1783. return 0;
  1784. }
  1785. static int snd_hdsp_put_clock_source_lock(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1786. {
  1787. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1788. int change;
  1789. change = (int)ucontrol->value.integer.value[0] != hdsp->clock_source_locked;
  1790. if (change)
  1791. hdsp->clock_source_locked = !!ucontrol->value.integer.value[0];
  1792. return change;
  1793. }
  1794. #define HDSP_DA_GAIN(xname, xindex) \
  1795. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1796. .name = xname, \
  1797. .index = xindex, \
  1798. .info = snd_hdsp_info_da_gain, \
  1799. .get = snd_hdsp_get_da_gain, \
  1800. .put = snd_hdsp_put_da_gain \
  1801. }
  1802. static int hdsp_da_gain(struct hdsp *hdsp)
  1803. {
  1804. switch (hdsp->control_register & HDSP_DAGainMask) {
  1805. case HDSP_DAGainHighGain:
  1806. return 0;
  1807. case HDSP_DAGainPlus4dBu:
  1808. return 1;
  1809. case HDSP_DAGainMinus10dBV:
  1810. return 2;
  1811. default:
  1812. return 1;
  1813. }
  1814. }
  1815. static int hdsp_set_da_gain(struct hdsp *hdsp, int mode)
  1816. {
  1817. hdsp->control_register &= ~HDSP_DAGainMask;
  1818. switch (mode) {
  1819. case 0:
  1820. hdsp->control_register |= HDSP_DAGainHighGain;
  1821. break;
  1822. case 1:
  1823. hdsp->control_register |= HDSP_DAGainPlus4dBu;
  1824. break;
  1825. case 2:
  1826. hdsp->control_register |= HDSP_DAGainMinus10dBV;
  1827. break;
  1828. default:
  1829. return -1;
  1830. }
  1831. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1832. return 0;
  1833. }
  1834. static int snd_hdsp_info_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1835. {
  1836. static const char * const texts[] = {"Hi Gain", "+4 dBu", "-10 dbV"};
  1837. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1838. }
  1839. static int snd_hdsp_get_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1840. {
  1841. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1842. ucontrol->value.enumerated.item[0] = hdsp_da_gain(hdsp);
  1843. return 0;
  1844. }
  1845. static int snd_hdsp_put_da_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1846. {
  1847. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1848. int change;
  1849. int val;
  1850. if (!snd_hdsp_use_is_exclusive(hdsp))
  1851. return -EBUSY;
  1852. val = ucontrol->value.enumerated.item[0];
  1853. if (val < 0) val = 0;
  1854. if (val > 2) val = 2;
  1855. spin_lock_irq(&hdsp->lock);
  1856. if (val != hdsp_da_gain(hdsp))
  1857. change = (hdsp_set_da_gain(hdsp, val) == 0) ? 1 : 0;
  1858. else
  1859. change = 0;
  1860. spin_unlock_irq(&hdsp->lock);
  1861. return change;
  1862. }
  1863. #define HDSP_AD_GAIN(xname, xindex) \
  1864. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1865. .name = xname, \
  1866. .index = xindex, \
  1867. .info = snd_hdsp_info_ad_gain, \
  1868. .get = snd_hdsp_get_ad_gain, \
  1869. .put = snd_hdsp_put_ad_gain \
  1870. }
  1871. static int hdsp_ad_gain(struct hdsp *hdsp)
  1872. {
  1873. switch (hdsp->control_register & HDSP_ADGainMask) {
  1874. case HDSP_ADGainMinus10dBV:
  1875. return 0;
  1876. case HDSP_ADGainPlus4dBu:
  1877. return 1;
  1878. case HDSP_ADGainLowGain:
  1879. return 2;
  1880. default:
  1881. return 1;
  1882. }
  1883. }
  1884. static int hdsp_set_ad_gain(struct hdsp *hdsp, int mode)
  1885. {
  1886. hdsp->control_register &= ~HDSP_ADGainMask;
  1887. switch (mode) {
  1888. case 0:
  1889. hdsp->control_register |= HDSP_ADGainMinus10dBV;
  1890. break;
  1891. case 1:
  1892. hdsp->control_register |= HDSP_ADGainPlus4dBu;
  1893. break;
  1894. case 2:
  1895. hdsp->control_register |= HDSP_ADGainLowGain;
  1896. break;
  1897. default:
  1898. return -1;
  1899. }
  1900. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1901. return 0;
  1902. }
  1903. static int snd_hdsp_info_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1904. {
  1905. static const char * const texts[] = {"-10 dBV", "+4 dBu", "Lo Gain"};
  1906. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1907. }
  1908. static int snd_hdsp_get_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1909. {
  1910. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1911. ucontrol->value.enumerated.item[0] = hdsp_ad_gain(hdsp);
  1912. return 0;
  1913. }
  1914. static int snd_hdsp_put_ad_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1915. {
  1916. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1917. int change;
  1918. int val;
  1919. if (!snd_hdsp_use_is_exclusive(hdsp))
  1920. return -EBUSY;
  1921. val = ucontrol->value.enumerated.item[0];
  1922. if (val < 0) val = 0;
  1923. if (val > 2) val = 2;
  1924. spin_lock_irq(&hdsp->lock);
  1925. if (val != hdsp_ad_gain(hdsp))
  1926. change = (hdsp_set_ad_gain(hdsp, val) == 0) ? 1 : 0;
  1927. else
  1928. change = 0;
  1929. spin_unlock_irq(&hdsp->lock);
  1930. return change;
  1931. }
  1932. #define HDSP_PHONE_GAIN(xname, xindex) \
  1933. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  1934. .name = xname, \
  1935. .index = xindex, \
  1936. .info = snd_hdsp_info_phone_gain, \
  1937. .get = snd_hdsp_get_phone_gain, \
  1938. .put = snd_hdsp_put_phone_gain \
  1939. }
  1940. static int hdsp_phone_gain(struct hdsp *hdsp)
  1941. {
  1942. switch (hdsp->control_register & HDSP_PhoneGainMask) {
  1943. case HDSP_PhoneGain0dB:
  1944. return 0;
  1945. case HDSP_PhoneGainMinus6dB:
  1946. return 1;
  1947. case HDSP_PhoneGainMinus12dB:
  1948. return 2;
  1949. default:
  1950. return 0;
  1951. }
  1952. }
  1953. static int hdsp_set_phone_gain(struct hdsp *hdsp, int mode)
  1954. {
  1955. hdsp->control_register &= ~HDSP_PhoneGainMask;
  1956. switch (mode) {
  1957. case 0:
  1958. hdsp->control_register |= HDSP_PhoneGain0dB;
  1959. break;
  1960. case 1:
  1961. hdsp->control_register |= HDSP_PhoneGainMinus6dB;
  1962. break;
  1963. case 2:
  1964. hdsp->control_register |= HDSP_PhoneGainMinus12dB;
  1965. break;
  1966. default:
  1967. return -1;
  1968. }
  1969. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  1970. return 0;
  1971. }
  1972. static int snd_hdsp_info_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1973. {
  1974. static const char * const texts[] = {"0 dB", "-6 dB", "-12 dB"};
  1975. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  1976. }
  1977. static int snd_hdsp_get_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1978. {
  1979. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1980. ucontrol->value.enumerated.item[0] = hdsp_phone_gain(hdsp);
  1981. return 0;
  1982. }
  1983. static int snd_hdsp_put_phone_gain(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1984. {
  1985. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  1986. int change;
  1987. int val;
  1988. if (!snd_hdsp_use_is_exclusive(hdsp))
  1989. return -EBUSY;
  1990. val = ucontrol->value.enumerated.item[0];
  1991. if (val < 0) val = 0;
  1992. if (val > 2) val = 2;
  1993. spin_lock_irq(&hdsp->lock);
  1994. if (val != hdsp_phone_gain(hdsp))
  1995. change = (hdsp_set_phone_gain(hdsp, val) == 0) ? 1 : 0;
  1996. else
  1997. change = 0;
  1998. spin_unlock_irq(&hdsp->lock);
  1999. return change;
  2000. }
  2001. #define HDSP_PREF_SYNC_REF(xname, xindex) \
  2002. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2003. .name = xname, \
  2004. .index = xindex, \
  2005. .info = snd_hdsp_info_pref_sync_ref, \
  2006. .get = snd_hdsp_get_pref_sync_ref, \
  2007. .put = snd_hdsp_put_pref_sync_ref \
  2008. }
  2009. static int hdsp_pref_sync_ref(struct hdsp *hdsp)
  2010. {
  2011. /* Notice that this looks at the requested sync source,
  2012. not the one actually in use.
  2013. */
  2014. switch (hdsp->control_register & HDSP_SyncRefMask) {
  2015. case HDSP_SyncRef_ADAT1:
  2016. return HDSP_SYNC_FROM_ADAT1;
  2017. case HDSP_SyncRef_ADAT2:
  2018. return HDSP_SYNC_FROM_ADAT2;
  2019. case HDSP_SyncRef_ADAT3:
  2020. return HDSP_SYNC_FROM_ADAT3;
  2021. case HDSP_SyncRef_SPDIF:
  2022. return HDSP_SYNC_FROM_SPDIF;
  2023. case HDSP_SyncRef_WORD:
  2024. return HDSP_SYNC_FROM_WORD;
  2025. case HDSP_SyncRef_ADAT_SYNC:
  2026. return HDSP_SYNC_FROM_ADAT_SYNC;
  2027. default:
  2028. return HDSP_SYNC_FROM_WORD;
  2029. }
  2030. return 0;
  2031. }
  2032. static int hdsp_set_pref_sync_ref(struct hdsp *hdsp, int pref)
  2033. {
  2034. hdsp->control_register &= ~HDSP_SyncRefMask;
  2035. switch (pref) {
  2036. case HDSP_SYNC_FROM_ADAT1:
  2037. hdsp->control_register &= ~HDSP_SyncRefMask; /* clear SyncRef bits */
  2038. break;
  2039. case HDSP_SYNC_FROM_ADAT2:
  2040. hdsp->control_register |= HDSP_SyncRef_ADAT2;
  2041. break;
  2042. case HDSP_SYNC_FROM_ADAT3:
  2043. hdsp->control_register |= HDSP_SyncRef_ADAT3;
  2044. break;
  2045. case HDSP_SYNC_FROM_SPDIF:
  2046. hdsp->control_register |= HDSP_SyncRef_SPDIF;
  2047. break;
  2048. case HDSP_SYNC_FROM_WORD:
  2049. hdsp->control_register |= HDSP_SyncRef_WORD;
  2050. break;
  2051. case HDSP_SYNC_FROM_ADAT_SYNC:
  2052. hdsp->control_register |= HDSP_SyncRef_ADAT_SYNC;
  2053. break;
  2054. default:
  2055. return -1;
  2056. }
  2057. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2058. return 0;
  2059. }
  2060. static int snd_hdsp_info_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2061. {
  2062. static const char * const texts[] = {
  2063. "Word", "IEC958", "ADAT1", "ADAT Sync", "ADAT2", "ADAT3"
  2064. };
  2065. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2066. int num_items;
  2067. switch (hdsp->io_type) {
  2068. case Digiface:
  2069. case H9652:
  2070. num_items = 6;
  2071. break;
  2072. case Multiface:
  2073. num_items = 4;
  2074. break;
  2075. case H9632:
  2076. num_items = 3;
  2077. break;
  2078. default:
  2079. return -EINVAL;
  2080. }
  2081. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  2082. }
  2083. static int snd_hdsp_get_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2084. {
  2085. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2086. ucontrol->value.enumerated.item[0] = hdsp_pref_sync_ref(hdsp);
  2087. return 0;
  2088. }
  2089. static int snd_hdsp_put_pref_sync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2090. {
  2091. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2092. int change, max;
  2093. unsigned int val;
  2094. if (!snd_hdsp_use_is_exclusive(hdsp))
  2095. return -EBUSY;
  2096. switch (hdsp->io_type) {
  2097. case Digiface:
  2098. case H9652:
  2099. max = 6;
  2100. break;
  2101. case Multiface:
  2102. max = 4;
  2103. break;
  2104. case H9632:
  2105. max = 3;
  2106. break;
  2107. default:
  2108. return -EIO;
  2109. }
  2110. val = ucontrol->value.enumerated.item[0] % max;
  2111. spin_lock_irq(&hdsp->lock);
  2112. change = (int)val != hdsp_pref_sync_ref(hdsp);
  2113. hdsp_set_pref_sync_ref(hdsp, val);
  2114. spin_unlock_irq(&hdsp->lock);
  2115. return change;
  2116. }
  2117. #define HDSP_AUTOSYNC_REF(xname, xindex) \
  2118. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2119. .name = xname, \
  2120. .index = xindex, \
  2121. .access = SNDRV_CTL_ELEM_ACCESS_READ, \
  2122. .info = snd_hdsp_info_autosync_ref, \
  2123. .get = snd_hdsp_get_autosync_ref, \
  2124. }
  2125. static int hdsp_autosync_ref(struct hdsp *hdsp)
  2126. {
  2127. /* This looks at the autosync selected sync reference */
  2128. unsigned int status2 = hdsp_read(hdsp, HDSP_status2Register);
  2129. switch (status2 & HDSP_SelSyncRefMask) {
  2130. case HDSP_SelSyncRef_WORD:
  2131. return HDSP_AUTOSYNC_FROM_WORD;
  2132. case HDSP_SelSyncRef_ADAT_SYNC:
  2133. return HDSP_AUTOSYNC_FROM_ADAT_SYNC;
  2134. case HDSP_SelSyncRef_SPDIF:
  2135. return HDSP_AUTOSYNC_FROM_SPDIF;
  2136. case HDSP_SelSyncRefMask:
  2137. return HDSP_AUTOSYNC_FROM_NONE;
  2138. case HDSP_SelSyncRef_ADAT1:
  2139. return HDSP_AUTOSYNC_FROM_ADAT1;
  2140. case HDSP_SelSyncRef_ADAT2:
  2141. return HDSP_AUTOSYNC_FROM_ADAT2;
  2142. case HDSP_SelSyncRef_ADAT3:
  2143. return HDSP_AUTOSYNC_FROM_ADAT3;
  2144. default:
  2145. return HDSP_AUTOSYNC_FROM_WORD;
  2146. }
  2147. return 0;
  2148. }
  2149. static int snd_hdsp_info_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2150. {
  2151. static const char * const texts[] = {
  2152. "Word", "ADAT Sync", "IEC958", "None", "ADAT1", "ADAT2", "ADAT3"
  2153. };
  2154. return snd_ctl_enum_info(uinfo, 1, 7, texts);
  2155. }
  2156. static int snd_hdsp_get_autosync_ref(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2157. {
  2158. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2159. ucontrol->value.enumerated.item[0] = hdsp_autosync_ref(hdsp);
  2160. return 0;
  2161. }
  2162. #define HDSP_PRECISE_POINTER(xname, xindex) \
  2163. { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
  2164. .name = xname, \
  2165. .index = xindex, \
  2166. .info = snd_hdsp_info_precise_pointer, \
  2167. .get = snd_hdsp_get_precise_pointer, \
  2168. .put = snd_hdsp_put_precise_pointer \
  2169. }
  2170. static int hdsp_set_precise_pointer(struct hdsp *hdsp, int precise)
  2171. {
  2172. if (precise)
  2173. hdsp->precise_ptr = 1;
  2174. else
  2175. hdsp->precise_ptr = 0;
  2176. return 0;
  2177. }
  2178. #define snd_hdsp_info_precise_pointer snd_ctl_boolean_mono_info
  2179. static int snd_hdsp_get_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2180. {
  2181. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2182. spin_lock_irq(&hdsp->lock);
  2183. ucontrol->value.integer.value[0] = hdsp->precise_ptr;
  2184. spin_unlock_irq(&hdsp->lock);
  2185. return 0;
  2186. }
  2187. static int snd_hdsp_put_precise_pointer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2188. {
  2189. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2190. int change;
  2191. unsigned int val;
  2192. if (!snd_hdsp_use_is_exclusive(hdsp))
  2193. return -EBUSY;
  2194. val = ucontrol->value.integer.value[0] & 1;
  2195. spin_lock_irq(&hdsp->lock);
  2196. change = (int)val != hdsp->precise_ptr;
  2197. hdsp_set_precise_pointer(hdsp, val);
  2198. spin_unlock_irq(&hdsp->lock);
  2199. return change;
  2200. }
  2201. #define HDSP_USE_MIDI_TASKLET(xname, xindex) \
  2202. { .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
  2203. .name = xname, \
  2204. .index = xindex, \
  2205. .info = snd_hdsp_info_use_midi_tasklet, \
  2206. .get = snd_hdsp_get_use_midi_tasklet, \
  2207. .put = snd_hdsp_put_use_midi_tasklet \
  2208. }
  2209. static int hdsp_set_use_midi_tasklet(struct hdsp *hdsp, int use_tasklet)
  2210. {
  2211. if (use_tasklet)
  2212. hdsp->use_midi_tasklet = 1;
  2213. else
  2214. hdsp->use_midi_tasklet = 0;
  2215. return 0;
  2216. }
  2217. #define snd_hdsp_info_use_midi_tasklet snd_ctl_boolean_mono_info
  2218. static int snd_hdsp_get_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2219. {
  2220. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2221. spin_lock_irq(&hdsp->lock);
  2222. ucontrol->value.integer.value[0] = hdsp->use_midi_tasklet;
  2223. spin_unlock_irq(&hdsp->lock);
  2224. return 0;
  2225. }
  2226. static int snd_hdsp_put_use_midi_tasklet(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2227. {
  2228. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2229. int change;
  2230. unsigned int val;
  2231. if (!snd_hdsp_use_is_exclusive(hdsp))
  2232. return -EBUSY;
  2233. val = ucontrol->value.integer.value[0] & 1;
  2234. spin_lock_irq(&hdsp->lock);
  2235. change = (int)val != hdsp->use_midi_tasklet;
  2236. hdsp_set_use_midi_tasklet(hdsp, val);
  2237. spin_unlock_irq(&hdsp->lock);
  2238. return change;
  2239. }
  2240. #define HDSP_MIXER(xname, xindex) \
  2241. { .iface = SNDRV_CTL_ELEM_IFACE_HWDEP, \
  2242. .name = xname, \
  2243. .index = xindex, \
  2244. .device = 0, \
  2245. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | \
  2246. SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  2247. .info = snd_hdsp_info_mixer, \
  2248. .get = snd_hdsp_get_mixer, \
  2249. .put = snd_hdsp_put_mixer \
  2250. }
  2251. static int snd_hdsp_info_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2252. {
  2253. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2254. uinfo->count = 3;
  2255. uinfo->value.integer.min = 0;
  2256. uinfo->value.integer.max = 65536;
  2257. uinfo->value.integer.step = 1;
  2258. return 0;
  2259. }
  2260. static int snd_hdsp_get_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2261. {
  2262. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2263. int source;
  2264. int destination;
  2265. int addr;
  2266. source = ucontrol->value.integer.value[0];
  2267. destination = ucontrol->value.integer.value[1];
  2268. if (source >= hdsp->max_channels)
  2269. addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels,destination);
  2270. else
  2271. addr = hdsp_input_to_output_key(hdsp,source, destination);
  2272. spin_lock_irq(&hdsp->lock);
  2273. ucontrol->value.integer.value[2] = hdsp_read_gain (hdsp, addr);
  2274. spin_unlock_irq(&hdsp->lock);
  2275. return 0;
  2276. }
  2277. static int snd_hdsp_put_mixer(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2278. {
  2279. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2280. int change;
  2281. int source;
  2282. int destination;
  2283. int gain;
  2284. int addr;
  2285. if (!snd_hdsp_use_is_exclusive(hdsp))
  2286. return -EBUSY;
  2287. source = ucontrol->value.integer.value[0];
  2288. destination = ucontrol->value.integer.value[1];
  2289. if (source >= hdsp->max_channels)
  2290. addr = hdsp_playback_to_output_key(hdsp,source-hdsp->max_channels, destination);
  2291. else
  2292. addr = hdsp_input_to_output_key(hdsp,source, destination);
  2293. gain = ucontrol->value.integer.value[2];
  2294. spin_lock_irq(&hdsp->lock);
  2295. change = gain != hdsp_read_gain(hdsp, addr);
  2296. if (change)
  2297. hdsp_write_gain(hdsp, addr, gain);
  2298. spin_unlock_irq(&hdsp->lock);
  2299. return change;
  2300. }
  2301. #define HDSP_WC_SYNC_CHECK(xname, xindex) \
  2302. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2303. .name = xname, \
  2304. .index = xindex, \
  2305. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  2306. .info = snd_hdsp_info_sync_check, \
  2307. .get = snd_hdsp_get_wc_sync_check \
  2308. }
  2309. static int snd_hdsp_info_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2310. {
  2311. static const char * const texts[] = {"No Lock", "Lock", "Sync" };
  2312. return snd_ctl_enum_info(uinfo, 1, 3, texts);
  2313. }
  2314. static int hdsp_wc_sync_check(struct hdsp *hdsp)
  2315. {
  2316. int status2 = hdsp_read(hdsp, HDSP_status2Register);
  2317. if (status2 & HDSP_wc_lock) {
  2318. if (status2 & HDSP_wc_sync)
  2319. return 2;
  2320. else
  2321. return 1;
  2322. } else
  2323. return 0;
  2324. return 0;
  2325. }
  2326. static int snd_hdsp_get_wc_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2327. {
  2328. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2329. ucontrol->value.enumerated.item[0] = hdsp_wc_sync_check(hdsp);
  2330. return 0;
  2331. }
  2332. #define HDSP_SPDIF_SYNC_CHECK(xname, xindex) \
  2333. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2334. .name = xname, \
  2335. .index = xindex, \
  2336. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  2337. .info = snd_hdsp_info_sync_check, \
  2338. .get = snd_hdsp_get_spdif_sync_check \
  2339. }
  2340. static int hdsp_spdif_sync_check(struct hdsp *hdsp)
  2341. {
  2342. int status = hdsp_read(hdsp, HDSP_statusRegister);
  2343. if (status & HDSP_SPDIFErrorFlag)
  2344. return 0;
  2345. else {
  2346. if (status & HDSP_SPDIFSync)
  2347. return 2;
  2348. else
  2349. return 1;
  2350. }
  2351. return 0;
  2352. }
  2353. static int snd_hdsp_get_spdif_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2354. {
  2355. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2356. ucontrol->value.enumerated.item[0] = hdsp_spdif_sync_check(hdsp);
  2357. return 0;
  2358. }
  2359. #define HDSP_ADATSYNC_SYNC_CHECK(xname, xindex) \
  2360. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2361. .name = xname, \
  2362. .index = xindex, \
  2363. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  2364. .info = snd_hdsp_info_sync_check, \
  2365. .get = snd_hdsp_get_adatsync_sync_check \
  2366. }
  2367. static int hdsp_adatsync_sync_check(struct hdsp *hdsp)
  2368. {
  2369. int status = hdsp_read(hdsp, HDSP_statusRegister);
  2370. if (status & HDSP_TimecodeLock) {
  2371. if (status & HDSP_TimecodeSync)
  2372. return 2;
  2373. else
  2374. return 1;
  2375. } else
  2376. return 0;
  2377. }
  2378. static int snd_hdsp_get_adatsync_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2379. {
  2380. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2381. ucontrol->value.enumerated.item[0] = hdsp_adatsync_sync_check(hdsp);
  2382. return 0;
  2383. }
  2384. #define HDSP_ADAT_SYNC_CHECK \
  2385. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2386. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
  2387. .info = snd_hdsp_info_sync_check, \
  2388. .get = snd_hdsp_get_adat_sync_check \
  2389. }
  2390. static int hdsp_adat_sync_check(struct hdsp *hdsp, int idx)
  2391. {
  2392. int status = hdsp_read(hdsp, HDSP_statusRegister);
  2393. if (status & (HDSP_Lock0>>idx)) {
  2394. if (status & (HDSP_Sync0>>idx))
  2395. return 2;
  2396. else
  2397. return 1;
  2398. } else
  2399. return 0;
  2400. }
  2401. static int snd_hdsp_get_adat_sync_check(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2402. {
  2403. int offset;
  2404. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2405. offset = ucontrol->id.index - 1;
  2406. if (snd_BUG_ON(offset < 0))
  2407. return -EINVAL;
  2408. switch (hdsp->io_type) {
  2409. case Digiface:
  2410. case H9652:
  2411. if (offset >= 3)
  2412. return -EINVAL;
  2413. break;
  2414. case Multiface:
  2415. case H9632:
  2416. if (offset >= 1)
  2417. return -EINVAL;
  2418. break;
  2419. default:
  2420. return -EIO;
  2421. }
  2422. ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
  2423. return 0;
  2424. }
  2425. #define HDSP_DDS_OFFSET(xname, xindex) \
  2426. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2427. .name = xname, \
  2428. .index = xindex, \
  2429. .info = snd_hdsp_info_dds_offset, \
  2430. .get = snd_hdsp_get_dds_offset, \
  2431. .put = snd_hdsp_put_dds_offset \
  2432. }
  2433. static int hdsp_dds_offset(struct hdsp *hdsp)
  2434. {
  2435. u64 n;
  2436. unsigned int dds_value = hdsp->dds_value;
  2437. int system_sample_rate = hdsp->system_sample_rate;
  2438. if (!dds_value)
  2439. return 0;
  2440. n = DDS_NUMERATOR;
  2441. /*
  2442. * dds_value = n / rate
  2443. * rate = n / dds_value
  2444. */
  2445. n = div_u64(n, dds_value);
  2446. if (system_sample_rate >= 112000)
  2447. n *= 4;
  2448. else if (system_sample_rate >= 56000)
  2449. n *= 2;
  2450. return ((int)n) - system_sample_rate;
  2451. }
  2452. static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
  2453. {
  2454. int rate = hdsp->system_sample_rate + offset_hz;
  2455. hdsp_set_dds_value(hdsp, rate);
  2456. return 0;
  2457. }
  2458. static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2459. {
  2460. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2461. uinfo->count = 1;
  2462. uinfo->value.integer.min = -5000;
  2463. uinfo->value.integer.max = 5000;
  2464. return 0;
  2465. }
  2466. static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2467. {
  2468. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2469. ucontrol->value.integer.value[0] = hdsp_dds_offset(hdsp);
  2470. return 0;
  2471. }
  2472. static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2473. {
  2474. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2475. int change;
  2476. int val;
  2477. if (!snd_hdsp_use_is_exclusive(hdsp))
  2478. return -EBUSY;
  2479. val = ucontrol->value.integer.value[0];
  2480. spin_lock_irq(&hdsp->lock);
  2481. if (val != hdsp_dds_offset(hdsp))
  2482. change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
  2483. else
  2484. change = 0;
  2485. spin_unlock_irq(&hdsp->lock);
  2486. return change;
  2487. }
  2488. static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
  2489. HDSP_DA_GAIN("DA Gain", 0),
  2490. HDSP_AD_GAIN("AD Gain", 0),
  2491. HDSP_PHONE_GAIN("Phones Gain", 0),
  2492. HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
  2493. HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
  2494. };
  2495. static struct snd_kcontrol_new snd_hdsp_controls[] = {
  2496. {
  2497. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2498. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  2499. .info = snd_hdsp_control_spdif_info,
  2500. .get = snd_hdsp_control_spdif_get,
  2501. .put = snd_hdsp_control_spdif_put,
  2502. },
  2503. {
  2504. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2505. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2506. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2507. .info = snd_hdsp_control_spdif_stream_info,
  2508. .get = snd_hdsp_control_spdif_stream_get,
  2509. .put = snd_hdsp_control_spdif_stream_put,
  2510. },
  2511. {
  2512. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2513. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2514. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  2515. .info = snd_hdsp_control_spdif_mask_info,
  2516. .get = snd_hdsp_control_spdif_mask_get,
  2517. .private_value = IEC958_AES0_NONAUDIO |
  2518. IEC958_AES0_PROFESSIONAL |
  2519. IEC958_AES0_CON_EMPHASIS,
  2520. },
  2521. {
  2522. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2523. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2524. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  2525. .info = snd_hdsp_control_spdif_mask_info,
  2526. .get = snd_hdsp_control_spdif_mask_get,
  2527. .private_value = IEC958_AES0_NONAUDIO |
  2528. IEC958_AES0_PROFESSIONAL |
  2529. IEC958_AES0_PRO_EMPHASIS,
  2530. },
  2531. HDSP_MIXER("Mixer", 0),
  2532. HDSP_SPDIF_IN("IEC958 Input Connector", 0),
  2533. HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
  2534. HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
  2535. HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
  2536. HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
  2537. /* 'Sample Clock Source' complies with the alsa control naming scheme */
  2538. HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
  2539. {
  2540. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2541. .name = "Sample Clock Source Locking",
  2542. .info = snd_hdsp_info_clock_source_lock,
  2543. .get = snd_hdsp_get_clock_source_lock,
  2544. .put = snd_hdsp_put_clock_source_lock,
  2545. },
  2546. HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
  2547. HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
  2548. HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
  2549. HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
  2550. HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
  2551. /* 'External Rate' complies with the alsa control naming scheme */
  2552. HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
  2553. HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
  2554. HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
  2555. HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
  2556. HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
  2557. HDSP_PRECISE_POINTER("Precise Pointer", 0),
  2558. HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
  2559. };
  2560. static int hdsp_rpm_input12(struct hdsp *hdsp)
  2561. {
  2562. switch (hdsp->control_register & HDSP_RPM_Inp12) {
  2563. case HDSP_RPM_Inp12_Phon_6dB:
  2564. return 0;
  2565. case HDSP_RPM_Inp12_Phon_n6dB:
  2566. return 2;
  2567. case HDSP_RPM_Inp12_Line_0dB:
  2568. return 3;
  2569. case HDSP_RPM_Inp12_Line_n6dB:
  2570. return 4;
  2571. }
  2572. return 1;
  2573. }
  2574. static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2575. {
  2576. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2577. ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
  2578. return 0;
  2579. }
  2580. static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
  2581. {
  2582. hdsp->control_register &= ~HDSP_RPM_Inp12;
  2583. switch (mode) {
  2584. case 0:
  2585. hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
  2586. break;
  2587. case 1:
  2588. break;
  2589. case 2:
  2590. hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
  2591. break;
  2592. case 3:
  2593. hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
  2594. break;
  2595. case 4:
  2596. hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
  2597. break;
  2598. default:
  2599. return -1;
  2600. }
  2601. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2602. return 0;
  2603. }
  2604. static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2605. {
  2606. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2607. int change;
  2608. int val;
  2609. if (!snd_hdsp_use_is_exclusive(hdsp))
  2610. return -EBUSY;
  2611. val = ucontrol->value.enumerated.item[0];
  2612. if (val < 0)
  2613. val = 0;
  2614. if (val > 4)
  2615. val = 4;
  2616. spin_lock_irq(&hdsp->lock);
  2617. if (val != hdsp_rpm_input12(hdsp))
  2618. change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
  2619. else
  2620. change = 0;
  2621. spin_unlock_irq(&hdsp->lock);
  2622. return change;
  2623. }
  2624. static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2625. {
  2626. static const char * const texts[] = {
  2627. "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
  2628. };
  2629. return snd_ctl_enum_info(uinfo, 1, 5, texts);
  2630. }
  2631. static int hdsp_rpm_input34(struct hdsp *hdsp)
  2632. {
  2633. switch (hdsp->control_register & HDSP_RPM_Inp34) {
  2634. case HDSP_RPM_Inp34_Phon_6dB:
  2635. return 0;
  2636. case HDSP_RPM_Inp34_Phon_n6dB:
  2637. return 2;
  2638. case HDSP_RPM_Inp34_Line_0dB:
  2639. return 3;
  2640. case HDSP_RPM_Inp34_Line_n6dB:
  2641. return 4;
  2642. }
  2643. return 1;
  2644. }
  2645. static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2646. {
  2647. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2648. ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
  2649. return 0;
  2650. }
  2651. static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
  2652. {
  2653. hdsp->control_register &= ~HDSP_RPM_Inp34;
  2654. switch (mode) {
  2655. case 0:
  2656. hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
  2657. break;
  2658. case 1:
  2659. break;
  2660. case 2:
  2661. hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
  2662. break;
  2663. case 3:
  2664. hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
  2665. break;
  2666. case 4:
  2667. hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
  2668. break;
  2669. default:
  2670. return -1;
  2671. }
  2672. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2673. return 0;
  2674. }
  2675. static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2676. {
  2677. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2678. int change;
  2679. int val;
  2680. if (!snd_hdsp_use_is_exclusive(hdsp))
  2681. return -EBUSY;
  2682. val = ucontrol->value.enumerated.item[0];
  2683. if (val < 0)
  2684. val = 0;
  2685. if (val > 4)
  2686. val = 4;
  2687. spin_lock_irq(&hdsp->lock);
  2688. if (val != hdsp_rpm_input34(hdsp))
  2689. change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
  2690. else
  2691. change = 0;
  2692. spin_unlock_irq(&hdsp->lock);
  2693. return change;
  2694. }
  2695. /* RPM Bypass switch */
  2696. static int hdsp_rpm_bypass(struct hdsp *hdsp)
  2697. {
  2698. return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
  2699. }
  2700. static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2701. {
  2702. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2703. ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
  2704. return 0;
  2705. }
  2706. static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
  2707. {
  2708. if (on)
  2709. hdsp->control_register |= HDSP_RPM_Bypass;
  2710. else
  2711. hdsp->control_register &= ~HDSP_RPM_Bypass;
  2712. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2713. return 0;
  2714. }
  2715. static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2716. {
  2717. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2718. int change;
  2719. unsigned int val;
  2720. if (!snd_hdsp_use_is_exclusive(hdsp))
  2721. return -EBUSY;
  2722. val = ucontrol->value.integer.value[0] & 1;
  2723. spin_lock_irq(&hdsp->lock);
  2724. change = (int)val != hdsp_rpm_bypass(hdsp);
  2725. hdsp_set_rpm_bypass(hdsp, val);
  2726. spin_unlock_irq(&hdsp->lock);
  2727. return change;
  2728. }
  2729. static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2730. {
  2731. static const char * const texts[] = {"On", "Off"};
  2732. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2733. }
  2734. /* RPM Disconnect switch */
  2735. static int hdsp_rpm_disconnect(struct hdsp *hdsp)
  2736. {
  2737. return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
  2738. }
  2739. static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2740. {
  2741. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2742. ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
  2743. return 0;
  2744. }
  2745. static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
  2746. {
  2747. if (on)
  2748. hdsp->control_register |= HDSP_RPM_Disconnect;
  2749. else
  2750. hdsp->control_register &= ~HDSP_RPM_Disconnect;
  2751. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2752. return 0;
  2753. }
  2754. static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2755. {
  2756. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2757. int change;
  2758. unsigned int val;
  2759. if (!snd_hdsp_use_is_exclusive(hdsp))
  2760. return -EBUSY;
  2761. val = ucontrol->value.integer.value[0] & 1;
  2762. spin_lock_irq(&hdsp->lock);
  2763. change = (int)val != hdsp_rpm_disconnect(hdsp);
  2764. hdsp_set_rpm_disconnect(hdsp, val);
  2765. spin_unlock_irq(&hdsp->lock);
  2766. return change;
  2767. }
  2768. static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2769. {
  2770. static const char * const texts[] = {"On", "Off"};
  2771. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2772. }
  2773. static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
  2774. {
  2775. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2776. .name = "RPM Bypass",
  2777. .get = snd_hdsp_get_rpm_bypass,
  2778. .put = snd_hdsp_put_rpm_bypass,
  2779. .info = snd_hdsp_info_rpm_bypass
  2780. },
  2781. {
  2782. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2783. .name = "RPM Disconnect",
  2784. .get = snd_hdsp_get_rpm_disconnect,
  2785. .put = snd_hdsp_put_rpm_disconnect,
  2786. .info = snd_hdsp_info_rpm_disconnect
  2787. },
  2788. {
  2789. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2790. .name = "Input 1/2",
  2791. .get = snd_hdsp_get_rpm_input12,
  2792. .put = snd_hdsp_put_rpm_input12,
  2793. .info = snd_hdsp_info_rpm_input
  2794. },
  2795. {
  2796. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2797. .name = "Input 3/4",
  2798. .get = snd_hdsp_get_rpm_input34,
  2799. .put = snd_hdsp_put_rpm_input34,
  2800. .info = snd_hdsp_info_rpm_input
  2801. },
  2802. HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
  2803. HDSP_MIXER("Mixer", 0)
  2804. };
  2805. static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
  2806. HDSP_TOGGLE_SETTING("Analog Extension Board",
  2807. HDSP_AnalogExtensionBoard);
  2808. static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
  2809. static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
  2810. {
  2811. unsigned int idx;
  2812. int err;
  2813. struct snd_kcontrol *kctl;
  2814. if (hdsp->io_type == RPM) {
  2815. /* RPM Bypass, Disconnect and Input switches */
  2816. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
  2817. err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
  2818. if (err < 0)
  2819. return err;
  2820. }
  2821. return 0;
  2822. }
  2823. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
  2824. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
  2825. return err;
  2826. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  2827. hdsp->spdif_ctl = kctl;
  2828. }
  2829. /* ADAT SyncCheck status */
  2830. snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
  2831. snd_hdsp_adat_sync_check.index = 1;
  2832. if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
  2833. return err;
  2834. if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
  2835. for (idx = 1; idx < 3; ++idx) {
  2836. snd_hdsp_adat_sync_check.index = idx+1;
  2837. if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
  2838. return err;
  2839. }
  2840. }
  2841. /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
  2842. if (hdsp->io_type == H9632) {
  2843. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
  2844. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
  2845. return err;
  2846. }
  2847. }
  2848. /* AEB control for H96xx card */
  2849. if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
  2850. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
  2851. return err;
  2852. }
  2853. return 0;
  2854. }
  2855. /*------------------------------------------------------------
  2856. /proc interface
  2857. ------------------------------------------------------------*/
  2858. static void
  2859. snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  2860. {
  2861. struct hdsp *hdsp = entry->private_data;
  2862. unsigned int status;
  2863. unsigned int status2;
  2864. char *pref_sync_ref;
  2865. char *autosync_ref;
  2866. char *system_clock_mode;
  2867. char *clock_source;
  2868. int x;
  2869. status = hdsp_read(hdsp, HDSP_statusRegister);
  2870. status2 = hdsp_read(hdsp, HDSP_status2Register);
  2871. snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
  2872. hdsp->card->number + 1);
  2873. snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
  2874. hdsp->capture_buffer, hdsp->playback_buffer);
  2875. snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
  2876. hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
  2877. snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
  2878. snd_iprintf(buffer, "Control2 register: 0x%x\n",
  2879. hdsp->control2_register);
  2880. snd_iprintf(buffer, "Status register: 0x%x\n", status);
  2881. snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
  2882. if (hdsp_check_for_iobox(hdsp)) {
  2883. snd_iprintf(buffer, "No I/O box connected.\n"
  2884. "Please connect one and upload firmware.\n");
  2885. return;
  2886. }
  2887. if (hdsp_check_for_firmware(hdsp, 0)) {
  2888. if (hdsp->state & HDSP_FirmwareCached) {
  2889. if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
  2890. snd_iprintf(buffer, "Firmware loading from "
  2891. "cache failed, "
  2892. "please upload manually.\n");
  2893. return;
  2894. }
  2895. } else {
  2896. int err = -EINVAL;
  2897. err = hdsp_request_fw_loader(hdsp);
  2898. if (err < 0) {
  2899. snd_iprintf(buffer,
  2900. "No firmware loaded nor cached, "
  2901. "please upload firmware.\n");
  2902. return;
  2903. }
  2904. }
  2905. }
  2906. snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
  2907. snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
  2908. snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
  2909. snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
  2910. snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
  2911. snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
  2912. snd_iprintf(buffer, "\n");
  2913. x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
  2914. snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
  2915. snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
  2916. snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
  2917. snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
  2918. snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
  2919. snd_iprintf(buffer, "\n");
  2920. switch (hdsp_clock_source(hdsp)) {
  2921. case HDSP_CLOCK_SOURCE_AUTOSYNC:
  2922. clock_source = "AutoSync";
  2923. break;
  2924. case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
  2925. clock_source = "Internal 32 kHz";
  2926. break;
  2927. case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
  2928. clock_source = "Internal 44.1 kHz";
  2929. break;
  2930. case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
  2931. clock_source = "Internal 48 kHz";
  2932. break;
  2933. case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
  2934. clock_source = "Internal 64 kHz";
  2935. break;
  2936. case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
  2937. clock_source = "Internal 88.2 kHz";
  2938. break;
  2939. case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
  2940. clock_source = "Internal 96 kHz";
  2941. break;
  2942. case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
  2943. clock_source = "Internal 128 kHz";
  2944. break;
  2945. case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
  2946. clock_source = "Internal 176.4 kHz";
  2947. break;
  2948. case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
  2949. clock_source = "Internal 192 kHz";
  2950. break;
  2951. default:
  2952. clock_source = "Error";
  2953. }
  2954. snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
  2955. if (hdsp_system_clock_mode(hdsp))
  2956. system_clock_mode = "Slave";
  2957. else
  2958. system_clock_mode = "Master";
  2959. switch (hdsp_pref_sync_ref (hdsp)) {
  2960. case HDSP_SYNC_FROM_WORD:
  2961. pref_sync_ref = "Word Clock";
  2962. break;
  2963. case HDSP_SYNC_FROM_ADAT_SYNC:
  2964. pref_sync_ref = "ADAT Sync";
  2965. break;
  2966. case HDSP_SYNC_FROM_SPDIF:
  2967. pref_sync_ref = "SPDIF";
  2968. break;
  2969. case HDSP_SYNC_FROM_ADAT1:
  2970. pref_sync_ref = "ADAT1";
  2971. break;
  2972. case HDSP_SYNC_FROM_ADAT2:
  2973. pref_sync_ref = "ADAT2";
  2974. break;
  2975. case HDSP_SYNC_FROM_ADAT3:
  2976. pref_sync_ref = "ADAT3";
  2977. break;
  2978. default:
  2979. pref_sync_ref = "Word Clock";
  2980. break;
  2981. }
  2982. snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
  2983. switch (hdsp_autosync_ref (hdsp)) {
  2984. case HDSP_AUTOSYNC_FROM_WORD:
  2985. autosync_ref = "Word Clock";
  2986. break;
  2987. case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
  2988. autosync_ref = "ADAT Sync";
  2989. break;
  2990. case HDSP_AUTOSYNC_FROM_SPDIF:
  2991. autosync_ref = "SPDIF";
  2992. break;
  2993. case HDSP_AUTOSYNC_FROM_NONE:
  2994. autosync_ref = "None";
  2995. break;
  2996. case HDSP_AUTOSYNC_FROM_ADAT1:
  2997. autosync_ref = "ADAT1";
  2998. break;
  2999. case HDSP_AUTOSYNC_FROM_ADAT2:
  3000. autosync_ref = "ADAT2";
  3001. break;
  3002. case HDSP_AUTOSYNC_FROM_ADAT3:
  3003. autosync_ref = "ADAT3";
  3004. break;
  3005. default:
  3006. autosync_ref = "---";
  3007. break;
  3008. }
  3009. snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
  3010. snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
  3011. snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
  3012. snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
  3013. snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
  3014. snd_iprintf(buffer, "\n");
  3015. if (hdsp->io_type != RPM) {
  3016. switch (hdsp_spdif_in(hdsp)) {
  3017. case HDSP_SPDIFIN_OPTICAL:
  3018. snd_iprintf(buffer, "IEC958 input: Optical\n");
  3019. break;
  3020. case HDSP_SPDIFIN_COAXIAL:
  3021. snd_iprintf(buffer, "IEC958 input: Coaxial\n");
  3022. break;
  3023. case HDSP_SPDIFIN_INTERNAL:
  3024. snd_iprintf(buffer, "IEC958 input: Internal\n");
  3025. break;
  3026. case HDSP_SPDIFIN_AES:
  3027. snd_iprintf(buffer, "IEC958 input: AES\n");
  3028. break;
  3029. default:
  3030. snd_iprintf(buffer, "IEC958 input: ???\n");
  3031. break;
  3032. }
  3033. }
  3034. if (RPM == hdsp->io_type) {
  3035. if (hdsp->control_register & HDSP_RPM_Bypass)
  3036. snd_iprintf(buffer, "RPM Bypass: disabled\n");
  3037. else
  3038. snd_iprintf(buffer, "RPM Bypass: enabled\n");
  3039. if (hdsp->control_register & HDSP_RPM_Disconnect)
  3040. snd_iprintf(buffer, "RPM disconnected\n");
  3041. else
  3042. snd_iprintf(buffer, "RPM connected\n");
  3043. switch (hdsp->control_register & HDSP_RPM_Inp12) {
  3044. case HDSP_RPM_Inp12_Phon_6dB:
  3045. snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
  3046. break;
  3047. case HDSP_RPM_Inp12_Phon_0dB:
  3048. snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
  3049. break;
  3050. case HDSP_RPM_Inp12_Phon_n6dB:
  3051. snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
  3052. break;
  3053. case HDSP_RPM_Inp12_Line_0dB:
  3054. snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
  3055. break;
  3056. case HDSP_RPM_Inp12_Line_n6dB:
  3057. snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
  3058. break;
  3059. default:
  3060. snd_iprintf(buffer, "Input 1/2: ???\n");
  3061. }
  3062. switch (hdsp->control_register & HDSP_RPM_Inp34) {
  3063. case HDSP_RPM_Inp34_Phon_6dB:
  3064. snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
  3065. break;
  3066. case HDSP_RPM_Inp34_Phon_0dB:
  3067. snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
  3068. break;
  3069. case HDSP_RPM_Inp34_Phon_n6dB:
  3070. snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
  3071. break;
  3072. case HDSP_RPM_Inp34_Line_0dB:
  3073. snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
  3074. break;
  3075. case HDSP_RPM_Inp34_Line_n6dB:
  3076. snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
  3077. break;
  3078. default:
  3079. snd_iprintf(buffer, "Input 3/4: ???\n");
  3080. }
  3081. } else {
  3082. if (hdsp->control_register & HDSP_SPDIFOpticalOut)
  3083. snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
  3084. else
  3085. snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
  3086. if (hdsp->control_register & HDSP_SPDIFProfessional)
  3087. snd_iprintf(buffer, "IEC958 quality: Professional\n");
  3088. else
  3089. snd_iprintf(buffer, "IEC958 quality: Consumer\n");
  3090. if (hdsp->control_register & HDSP_SPDIFEmphasis)
  3091. snd_iprintf(buffer, "IEC958 emphasis: on\n");
  3092. else
  3093. snd_iprintf(buffer, "IEC958 emphasis: off\n");
  3094. if (hdsp->control_register & HDSP_SPDIFNonAudio)
  3095. snd_iprintf(buffer, "IEC958 NonAudio: on\n");
  3096. else
  3097. snd_iprintf(buffer, "IEC958 NonAudio: off\n");
  3098. x = hdsp_spdif_sample_rate(hdsp);
  3099. if (x != 0)
  3100. snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
  3101. else
  3102. snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
  3103. }
  3104. snd_iprintf(buffer, "\n");
  3105. /* Sync Check */
  3106. x = status & HDSP_Sync0;
  3107. if (status & HDSP_Lock0)
  3108. snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
  3109. else
  3110. snd_iprintf(buffer, "ADAT1: No Lock\n");
  3111. switch (hdsp->io_type) {
  3112. case Digiface:
  3113. case H9652:
  3114. x = status & HDSP_Sync1;
  3115. if (status & HDSP_Lock1)
  3116. snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
  3117. else
  3118. snd_iprintf(buffer, "ADAT2: No Lock\n");
  3119. x = status & HDSP_Sync2;
  3120. if (status & HDSP_Lock2)
  3121. snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
  3122. else
  3123. snd_iprintf(buffer, "ADAT3: No Lock\n");
  3124. break;
  3125. default:
  3126. /* relax */
  3127. break;
  3128. }
  3129. x = status & HDSP_SPDIFSync;
  3130. if (status & HDSP_SPDIFErrorFlag)
  3131. snd_iprintf (buffer, "SPDIF: No Lock\n");
  3132. else
  3133. snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
  3134. x = status2 & HDSP_wc_sync;
  3135. if (status2 & HDSP_wc_lock)
  3136. snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
  3137. else
  3138. snd_iprintf (buffer, "Word Clock: No Lock\n");
  3139. x = status & HDSP_TimecodeSync;
  3140. if (status & HDSP_TimecodeLock)
  3141. snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
  3142. else
  3143. snd_iprintf(buffer, "ADAT Sync: No Lock\n");
  3144. snd_iprintf(buffer, "\n");
  3145. /* Informations about H9632 specific controls */
  3146. if (hdsp->io_type == H9632) {
  3147. char *tmp;
  3148. switch (hdsp_ad_gain(hdsp)) {
  3149. case 0:
  3150. tmp = "-10 dBV";
  3151. break;
  3152. case 1:
  3153. tmp = "+4 dBu";
  3154. break;
  3155. default:
  3156. tmp = "Lo Gain";
  3157. break;
  3158. }
  3159. snd_iprintf(buffer, "AD Gain : %s\n", tmp);
  3160. switch (hdsp_da_gain(hdsp)) {
  3161. case 0:
  3162. tmp = "Hi Gain";
  3163. break;
  3164. case 1:
  3165. tmp = "+4 dBu";
  3166. break;
  3167. default:
  3168. tmp = "-10 dBV";
  3169. break;
  3170. }
  3171. snd_iprintf(buffer, "DA Gain : %s\n", tmp);
  3172. switch (hdsp_phone_gain(hdsp)) {
  3173. case 0:
  3174. tmp = "0 dB";
  3175. break;
  3176. case 1:
  3177. tmp = "-6 dB";
  3178. break;
  3179. default:
  3180. tmp = "-12 dB";
  3181. break;
  3182. }
  3183. snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
  3184. snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
  3185. hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
  3186. "yes" : "no");
  3187. if (hdsp->control_register & HDSP_AnalogExtensionBoard)
  3188. snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
  3189. else
  3190. snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
  3191. snd_iprintf(buffer, "\n");
  3192. }
  3193. }
  3194. static void snd_hdsp_proc_init(struct hdsp *hdsp)
  3195. {
  3196. struct snd_info_entry *entry;
  3197. if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
  3198. snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
  3199. }
  3200. static void snd_hdsp_free_buffers(struct hdsp *hdsp)
  3201. {
  3202. snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
  3203. snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
  3204. }
  3205. static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
  3206. {
  3207. unsigned long pb_bus, cb_bus;
  3208. if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
  3209. snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
  3210. if (hdsp->capture_dma_buf.area)
  3211. snd_dma_free_pages(&hdsp->capture_dma_buf);
  3212. dev_err(hdsp->card->dev,
  3213. "%s: no buffers available\n", hdsp->card_name);
  3214. return -ENOMEM;
  3215. }
  3216. /* Align to bus-space 64K boundary */
  3217. cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
  3218. pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
  3219. /* Tell the card where it is */
  3220. hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
  3221. hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
  3222. hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
  3223. hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
  3224. return 0;
  3225. }
  3226. static int snd_hdsp_set_defaults(struct hdsp *hdsp)
  3227. {
  3228. unsigned int i;
  3229. /* ASSUMPTION: hdsp->lock is either held, or
  3230. there is no need to hold it (e.g. during module
  3231. initialization).
  3232. */
  3233. /* set defaults:
  3234. SPDIF Input via Coax
  3235. Master clock mode
  3236. maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
  3237. which implies 2 4096 sample, 32Kbyte periods).
  3238. Enable line out.
  3239. */
  3240. hdsp->control_register = HDSP_ClockModeMaster |
  3241. HDSP_SPDIFInputCoaxial |
  3242. hdsp_encode_latency(7) |
  3243. HDSP_LineOut;
  3244. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3245. #ifdef SNDRV_BIG_ENDIAN
  3246. hdsp->control2_register = HDSP_BIGENDIAN_MODE;
  3247. #else
  3248. hdsp->control2_register = 0;
  3249. #endif
  3250. if (hdsp->io_type == H9652)
  3251. snd_hdsp_9652_enable_mixer (hdsp);
  3252. else
  3253. hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
  3254. hdsp_reset_hw_pointer(hdsp);
  3255. hdsp_compute_period_size(hdsp);
  3256. /* silence everything */
  3257. for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
  3258. hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
  3259. for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
  3260. if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
  3261. return -EIO;
  3262. }
  3263. /* H9632 specific defaults */
  3264. if (hdsp->io_type == H9632) {
  3265. hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
  3266. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3267. }
  3268. /* set a default rate so that the channel map is set up.
  3269. */
  3270. hdsp_set_rate(hdsp, 48000, 1);
  3271. return 0;
  3272. }
  3273. static void hdsp_midi_tasklet(unsigned long arg)
  3274. {
  3275. struct hdsp *hdsp = (struct hdsp *)arg;
  3276. if (hdsp->midi[0].pending)
  3277. snd_hdsp_midi_input_read (&hdsp->midi[0]);
  3278. if (hdsp->midi[1].pending)
  3279. snd_hdsp_midi_input_read (&hdsp->midi[1]);
  3280. }
  3281. static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
  3282. {
  3283. struct hdsp *hdsp = (struct hdsp *) dev_id;
  3284. unsigned int status;
  3285. int audio;
  3286. int midi0;
  3287. int midi1;
  3288. unsigned int midi0status;
  3289. unsigned int midi1status;
  3290. int schedule = 0;
  3291. status = hdsp_read(hdsp, HDSP_statusRegister);
  3292. audio = status & HDSP_audioIRQPending;
  3293. midi0 = status & HDSP_midi0IRQPending;
  3294. midi1 = status & HDSP_midi1IRQPending;
  3295. if (!audio && !midi0 && !midi1)
  3296. return IRQ_NONE;
  3297. hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
  3298. midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
  3299. midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
  3300. if (!(hdsp->state & HDSP_InitializationComplete))
  3301. return IRQ_HANDLED;
  3302. if (audio) {
  3303. if (hdsp->capture_substream)
  3304. snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  3305. if (hdsp->playback_substream)
  3306. snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
  3307. }
  3308. if (midi0 && midi0status) {
  3309. if (hdsp->use_midi_tasklet) {
  3310. /* we disable interrupts for this input until processing is done */
  3311. hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
  3312. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3313. hdsp->midi[0].pending = 1;
  3314. schedule = 1;
  3315. } else {
  3316. snd_hdsp_midi_input_read (&hdsp->midi[0]);
  3317. }
  3318. }
  3319. if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
  3320. if (hdsp->use_midi_tasklet) {
  3321. /* we disable interrupts for this input until processing is done */
  3322. hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
  3323. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3324. hdsp->midi[1].pending = 1;
  3325. schedule = 1;
  3326. } else {
  3327. snd_hdsp_midi_input_read (&hdsp->midi[1]);
  3328. }
  3329. }
  3330. if (hdsp->use_midi_tasklet && schedule)
  3331. tasklet_schedule(&hdsp->midi_tasklet);
  3332. return IRQ_HANDLED;
  3333. }
  3334. static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
  3335. {
  3336. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3337. return hdsp_hw_pointer(hdsp);
  3338. }
  3339. static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
  3340. int stream,
  3341. int channel)
  3342. {
  3343. int mapped_channel;
  3344. if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
  3345. return NULL;
  3346. if ((mapped_channel = hdsp->channel_map[channel]) < 0)
  3347. return NULL;
  3348. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  3349. return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
  3350. else
  3351. return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
  3352. }
  3353. static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
  3354. snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
  3355. {
  3356. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3357. char *channel_buf;
  3358. if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
  3359. return -EINVAL;
  3360. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3361. if (snd_BUG_ON(!channel_buf))
  3362. return -EIO;
  3363. if (copy_from_user(channel_buf + pos * 4, src, count * 4))
  3364. return -EFAULT;
  3365. return count;
  3366. }
  3367. static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
  3368. snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
  3369. {
  3370. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3371. char *channel_buf;
  3372. if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
  3373. return -EINVAL;
  3374. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3375. if (snd_BUG_ON(!channel_buf))
  3376. return -EIO;
  3377. if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
  3378. return -EFAULT;
  3379. return count;
  3380. }
  3381. static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
  3382. snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  3383. {
  3384. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3385. char *channel_buf;
  3386. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3387. if (snd_BUG_ON(!channel_buf))
  3388. return -EIO;
  3389. memset(channel_buf + pos * 4, 0, count * 4);
  3390. return count;
  3391. }
  3392. static int snd_hdsp_reset(struct snd_pcm_substream *substream)
  3393. {
  3394. struct snd_pcm_runtime *runtime = substream->runtime;
  3395. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3396. struct snd_pcm_substream *other;
  3397. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3398. other = hdsp->capture_substream;
  3399. else
  3400. other = hdsp->playback_substream;
  3401. if (hdsp->running)
  3402. runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
  3403. else
  3404. runtime->status->hw_ptr = 0;
  3405. if (other) {
  3406. struct snd_pcm_substream *s;
  3407. struct snd_pcm_runtime *oruntime = other->runtime;
  3408. snd_pcm_group_for_each_entry(s, substream) {
  3409. if (s == other) {
  3410. oruntime->status->hw_ptr = runtime->status->hw_ptr;
  3411. break;
  3412. }
  3413. }
  3414. }
  3415. return 0;
  3416. }
  3417. static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
  3418. struct snd_pcm_hw_params *params)
  3419. {
  3420. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3421. int err;
  3422. pid_t this_pid;
  3423. pid_t other_pid;
  3424. if (hdsp_check_for_iobox (hdsp))
  3425. return -EIO;
  3426. if (hdsp_check_for_firmware(hdsp, 1))
  3427. return -EIO;
  3428. spin_lock_irq(&hdsp->lock);
  3429. if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  3430. hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
  3431. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
  3432. this_pid = hdsp->playback_pid;
  3433. other_pid = hdsp->capture_pid;
  3434. } else {
  3435. this_pid = hdsp->capture_pid;
  3436. other_pid = hdsp->playback_pid;
  3437. }
  3438. if ((other_pid > 0) && (this_pid != other_pid)) {
  3439. /* The other stream is open, and not by the same
  3440. task as this one. Make sure that the parameters
  3441. that matter are the same.
  3442. */
  3443. if (params_rate(params) != hdsp->system_sample_rate) {
  3444. spin_unlock_irq(&hdsp->lock);
  3445. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
  3446. return -EBUSY;
  3447. }
  3448. if (params_period_size(params) != hdsp->period_bytes / 4) {
  3449. spin_unlock_irq(&hdsp->lock);
  3450. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  3451. return -EBUSY;
  3452. }
  3453. /* We're fine. */
  3454. spin_unlock_irq(&hdsp->lock);
  3455. return 0;
  3456. } else {
  3457. spin_unlock_irq(&hdsp->lock);
  3458. }
  3459. /* how to make sure that the rate matches an externally-set one ?
  3460. */
  3461. spin_lock_irq(&hdsp->lock);
  3462. if (! hdsp->clock_source_locked) {
  3463. if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
  3464. spin_unlock_irq(&hdsp->lock);
  3465. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
  3466. return err;
  3467. }
  3468. }
  3469. spin_unlock_irq(&hdsp->lock);
  3470. if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
  3471. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  3472. return err;
  3473. }
  3474. return 0;
  3475. }
  3476. static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
  3477. struct snd_pcm_channel_info *info)
  3478. {
  3479. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3480. int mapped_channel;
  3481. if (snd_BUG_ON(info->channel >= hdsp->max_channels))
  3482. return -EINVAL;
  3483. if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
  3484. return -EINVAL;
  3485. info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
  3486. info->first = 0;
  3487. info->step = 32;
  3488. return 0;
  3489. }
  3490. static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
  3491. unsigned int cmd, void *arg)
  3492. {
  3493. switch (cmd) {
  3494. case SNDRV_PCM_IOCTL1_RESET:
  3495. return snd_hdsp_reset(substream);
  3496. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  3497. return snd_hdsp_channel_info(substream, arg);
  3498. default:
  3499. break;
  3500. }
  3501. return snd_pcm_lib_ioctl(substream, cmd, arg);
  3502. }
  3503. static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
  3504. {
  3505. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3506. struct snd_pcm_substream *other;
  3507. int running;
  3508. if (hdsp_check_for_iobox (hdsp))
  3509. return -EIO;
  3510. if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
  3511. return -EIO;
  3512. spin_lock(&hdsp->lock);
  3513. running = hdsp->running;
  3514. switch (cmd) {
  3515. case SNDRV_PCM_TRIGGER_START:
  3516. running |= 1 << substream->stream;
  3517. break;
  3518. case SNDRV_PCM_TRIGGER_STOP:
  3519. running &= ~(1 << substream->stream);
  3520. break;
  3521. default:
  3522. snd_BUG();
  3523. spin_unlock(&hdsp->lock);
  3524. return -EINVAL;
  3525. }
  3526. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3527. other = hdsp->capture_substream;
  3528. else
  3529. other = hdsp->playback_substream;
  3530. if (other) {
  3531. struct snd_pcm_substream *s;
  3532. snd_pcm_group_for_each_entry(s, substream) {
  3533. if (s == other) {
  3534. snd_pcm_trigger_done(s, substream);
  3535. if (cmd == SNDRV_PCM_TRIGGER_START)
  3536. running |= 1 << s->stream;
  3537. else
  3538. running &= ~(1 << s->stream);
  3539. goto _ok;
  3540. }
  3541. }
  3542. if (cmd == SNDRV_PCM_TRIGGER_START) {
  3543. if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
  3544. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  3545. hdsp_silence_playback(hdsp);
  3546. } else {
  3547. if (running &&
  3548. substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3549. hdsp_silence_playback(hdsp);
  3550. }
  3551. } else {
  3552. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  3553. hdsp_silence_playback(hdsp);
  3554. }
  3555. _ok:
  3556. snd_pcm_trigger_done(substream, substream);
  3557. if (!hdsp->running && running)
  3558. hdsp_start_audio(hdsp);
  3559. else if (hdsp->running && !running)
  3560. hdsp_stop_audio(hdsp);
  3561. hdsp->running = running;
  3562. spin_unlock(&hdsp->lock);
  3563. return 0;
  3564. }
  3565. static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
  3566. {
  3567. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3568. int result = 0;
  3569. if (hdsp_check_for_iobox (hdsp))
  3570. return -EIO;
  3571. if (hdsp_check_for_firmware(hdsp, 1))
  3572. return -EIO;
  3573. spin_lock_irq(&hdsp->lock);
  3574. if (!hdsp->running)
  3575. hdsp_reset_hw_pointer(hdsp);
  3576. spin_unlock_irq(&hdsp->lock);
  3577. return result;
  3578. }
  3579. static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
  3580. {
  3581. .info = (SNDRV_PCM_INFO_MMAP |
  3582. SNDRV_PCM_INFO_MMAP_VALID |
  3583. SNDRV_PCM_INFO_NONINTERLEAVED |
  3584. SNDRV_PCM_INFO_SYNC_START |
  3585. SNDRV_PCM_INFO_DOUBLE),
  3586. #ifdef SNDRV_BIG_ENDIAN
  3587. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  3588. #else
  3589. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  3590. #endif
  3591. .rates = (SNDRV_PCM_RATE_32000 |
  3592. SNDRV_PCM_RATE_44100 |
  3593. SNDRV_PCM_RATE_48000 |
  3594. SNDRV_PCM_RATE_64000 |
  3595. SNDRV_PCM_RATE_88200 |
  3596. SNDRV_PCM_RATE_96000),
  3597. .rate_min = 32000,
  3598. .rate_max = 96000,
  3599. .channels_min = 6,
  3600. .channels_max = HDSP_MAX_CHANNELS,
  3601. .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
  3602. .period_bytes_min = (64 * 4) * 10,
  3603. .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
  3604. .periods_min = 2,
  3605. .periods_max = 2,
  3606. .fifo_size = 0
  3607. };
  3608. static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
  3609. {
  3610. .info = (SNDRV_PCM_INFO_MMAP |
  3611. SNDRV_PCM_INFO_MMAP_VALID |
  3612. SNDRV_PCM_INFO_NONINTERLEAVED |
  3613. SNDRV_PCM_INFO_SYNC_START),
  3614. #ifdef SNDRV_BIG_ENDIAN
  3615. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  3616. #else
  3617. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  3618. #endif
  3619. .rates = (SNDRV_PCM_RATE_32000 |
  3620. SNDRV_PCM_RATE_44100 |
  3621. SNDRV_PCM_RATE_48000 |
  3622. SNDRV_PCM_RATE_64000 |
  3623. SNDRV_PCM_RATE_88200 |
  3624. SNDRV_PCM_RATE_96000),
  3625. .rate_min = 32000,
  3626. .rate_max = 96000,
  3627. .channels_min = 5,
  3628. .channels_max = HDSP_MAX_CHANNELS,
  3629. .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
  3630. .period_bytes_min = (64 * 4) * 10,
  3631. .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
  3632. .periods_min = 2,
  3633. .periods_max = 2,
  3634. .fifo_size = 0
  3635. };
  3636. static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
  3637. static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
  3638. .count = ARRAY_SIZE(hdsp_period_sizes),
  3639. .list = hdsp_period_sizes,
  3640. .mask = 0
  3641. };
  3642. static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
  3643. static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
  3644. .count = ARRAY_SIZE(hdsp_9632_sample_rates),
  3645. .list = hdsp_9632_sample_rates,
  3646. .mask = 0
  3647. };
  3648. static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
  3649. struct snd_pcm_hw_rule *rule)
  3650. {
  3651. struct hdsp *hdsp = rule->private;
  3652. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3653. if (hdsp->io_type == H9632) {
  3654. unsigned int list[3];
  3655. list[0] = hdsp->qs_in_channels;
  3656. list[1] = hdsp->ds_in_channels;
  3657. list[2] = hdsp->ss_in_channels;
  3658. return snd_interval_list(c, 3, list, 0);
  3659. } else {
  3660. unsigned int list[2];
  3661. list[0] = hdsp->ds_in_channels;
  3662. list[1] = hdsp->ss_in_channels;
  3663. return snd_interval_list(c, 2, list, 0);
  3664. }
  3665. }
  3666. static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
  3667. struct snd_pcm_hw_rule *rule)
  3668. {
  3669. unsigned int list[3];
  3670. struct hdsp *hdsp = rule->private;
  3671. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3672. if (hdsp->io_type == H9632) {
  3673. list[0] = hdsp->qs_out_channels;
  3674. list[1] = hdsp->ds_out_channels;
  3675. list[2] = hdsp->ss_out_channels;
  3676. return snd_interval_list(c, 3, list, 0);
  3677. } else {
  3678. list[0] = hdsp->ds_out_channels;
  3679. list[1] = hdsp->ss_out_channels;
  3680. }
  3681. return snd_interval_list(c, 2, list, 0);
  3682. }
  3683. static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
  3684. struct snd_pcm_hw_rule *rule)
  3685. {
  3686. struct hdsp *hdsp = rule->private;
  3687. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3688. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3689. if (r->min > 96000 && hdsp->io_type == H9632) {
  3690. struct snd_interval t = {
  3691. .min = hdsp->qs_in_channels,
  3692. .max = hdsp->qs_in_channels,
  3693. .integer = 1,
  3694. };
  3695. return snd_interval_refine(c, &t);
  3696. } else if (r->min > 48000 && r->max <= 96000) {
  3697. struct snd_interval t = {
  3698. .min = hdsp->ds_in_channels,
  3699. .max = hdsp->ds_in_channels,
  3700. .integer = 1,
  3701. };
  3702. return snd_interval_refine(c, &t);
  3703. } else if (r->max < 64000) {
  3704. struct snd_interval t = {
  3705. .min = hdsp->ss_in_channels,
  3706. .max = hdsp->ss_in_channels,
  3707. .integer = 1,
  3708. };
  3709. return snd_interval_refine(c, &t);
  3710. }
  3711. return 0;
  3712. }
  3713. static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
  3714. struct snd_pcm_hw_rule *rule)
  3715. {
  3716. struct hdsp *hdsp = rule->private;
  3717. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3718. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3719. if (r->min > 96000 && hdsp->io_type == H9632) {
  3720. struct snd_interval t = {
  3721. .min = hdsp->qs_out_channels,
  3722. .max = hdsp->qs_out_channels,
  3723. .integer = 1,
  3724. };
  3725. return snd_interval_refine(c, &t);
  3726. } else if (r->min > 48000 && r->max <= 96000) {
  3727. struct snd_interval t = {
  3728. .min = hdsp->ds_out_channels,
  3729. .max = hdsp->ds_out_channels,
  3730. .integer = 1,
  3731. };
  3732. return snd_interval_refine(c, &t);
  3733. } else if (r->max < 64000) {
  3734. struct snd_interval t = {
  3735. .min = hdsp->ss_out_channels,
  3736. .max = hdsp->ss_out_channels,
  3737. .integer = 1,
  3738. };
  3739. return snd_interval_refine(c, &t);
  3740. }
  3741. return 0;
  3742. }
  3743. static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
  3744. struct snd_pcm_hw_rule *rule)
  3745. {
  3746. struct hdsp *hdsp = rule->private;
  3747. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3748. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3749. if (c->min >= hdsp->ss_out_channels) {
  3750. struct snd_interval t = {
  3751. .min = 32000,
  3752. .max = 48000,
  3753. .integer = 1,
  3754. };
  3755. return snd_interval_refine(r, &t);
  3756. } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
  3757. struct snd_interval t = {
  3758. .min = 128000,
  3759. .max = 192000,
  3760. .integer = 1,
  3761. };
  3762. return snd_interval_refine(r, &t);
  3763. } else if (c->max <= hdsp->ds_out_channels) {
  3764. struct snd_interval t = {
  3765. .min = 64000,
  3766. .max = 96000,
  3767. .integer = 1,
  3768. };
  3769. return snd_interval_refine(r, &t);
  3770. }
  3771. return 0;
  3772. }
  3773. static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
  3774. struct snd_pcm_hw_rule *rule)
  3775. {
  3776. struct hdsp *hdsp = rule->private;
  3777. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3778. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3779. if (c->min >= hdsp->ss_in_channels) {
  3780. struct snd_interval t = {
  3781. .min = 32000,
  3782. .max = 48000,
  3783. .integer = 1,
  3784. };
  3785. return snd_interval_refine(r, &t);
  3786. } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
  3787. struct snd_interval t = {
  3788. .min = 128000,
  3789. .max = 192000,
  3790. .integer = 1,
  3791. };
  3792. return snd_interval_refine(r, &t);
  3793. } else if (c->max <= hdsp->ds_in_channels) {
  3794. struct snd_interval t = {
  3795. .min = 64000,
  3796. .max = 96000,
  3797. .integer = 1,
  3798. };
  3799. return snd_interval_refine(r, &t);
  3800. }
  3801. return 0;
  3802. }
  3803. static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
  3804. {
  3805. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3806. struct snd_pcm_runtime *runtime = substream->runtime;
  3807. if (hdsp_check_for_iobox (hdsp))
  3808. return -EIO;
  3809. if (hdsp_check_for_firmware(hdsp, 1))
  3810. return -EIO;
  3811. spin_lock_irq(&hdsp->lock);
  3812. snd_pcm_set_sync(substream);
  3813. runtime->hw = snd_hdsp_playback_subinfo;
  3814. runtime->dma_area = hdsp->playback_buffer;
  3815. runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
  3816. hdsp->playback_pid = current->pid;
  3817. hdsp->playback_substream = substream;
  3818. spin_unlock_irq(&hdsp->lock);
  3819. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  3820. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
  3821. if (hdsp->clock_source_locked) {
  3822. runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
  3823. } else if (hdsp->io_type == H9632) {
  3824. runtime->hw.rate_max = 192000;
  3825. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  3826. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
  3827. }
  3828. if (hdsp->io_type == H9632) {
  3829. runtime->hw.channels_min = hdsp->qs_out_channels;
  3830. runtime->hw.channels_max = hdsp->ss_out_channels;
  3831. }
  3832. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3833. snd_hdsp_hw_rule_out_channels, hdsp,
  3834. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3835. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3836. snd_hdsp_hw_rule_out_channels_rate, hdsp,
  3837. SNDRV_PCM_HW_PARAM_RATE, -1);
  3838. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  3839. snd_hdsp_hw_rule_rate_out_channels, hdsp,
  3840. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3841. if (RPM != hdsp->io_type) {
  3842. hdsp->creg_spdif_stream = hdsp->creg_spdif;
  3843. hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  3844. snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
  3845. SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
  3846. }
  3847. return 0;
  3848. }
  3849. static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
  3850. {
  3851. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3852. spin_lock_irq(&hdsp->lock);
  3853. hdsp->playback_pid = -1;
  3854. hdsp->playback_substream = NULL;
  3855. spin_unlock_irq(&hdsp->lock);
  3856. if (RPM != hdsp->io_type) {
  3857. hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  3858. snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
  3859. SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
  3860. }
  3861. return 0;
  3862. }
  3863. static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
  3864. {
  3865. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3866. struct snd_pcm_runtime *runtime = substream->runtime;
  3867. if (hdsp_check_for_iobox (hdsp))
  3868. return -EIO;
  3869. if (hdsp_check_for_firmware(hdsp, 1))
  3870. return -EIO;
  3871. spin_lock_irq(&hdsp->lock);
  3872. snd_pcm_set_sync(substream);
  3873. runtime->hw = snd_hdsp_capture_subinfo;
  3874. runtime->dma_area = hdsp->capture_buffer;
  3875. runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
  3876. hdsp->capture_pid = current->pid;
  3877. hdsp->capture_substream = substream;
  3878. spin_unlock_irq(&hdsp->lock);
  3879. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  3880. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
  3881. if (hdsp->io_type == H9632) {
  3882. runtime->hw.channels_min = hdsp->qs_in_channels;
  3883. runtime->hw.channels_max = hdsp->ss_in_channels;
  3884. runtime->hw.rate_max = 192000;
  3885. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  3886. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
  3887. }
  3888. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3889. snd_hdsp_hw_rule_in_channels, hdsp,
  3890. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3891. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3892. snd_hdsp_hw_rule_in_channels_rate, hdsp,
  3893. SNDRV_PCM_HW_PARAM_RATE, -1);
  3894. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  3895. snd_hdsp_hw_rule_rate_in_channels, hdsp,
  3896. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3897. return 0;
  3898. }
  3899. static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
  3900. {
  3901. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3902. spin_lock_irq(&hdsp->lock);
  3903. hdsp->capture_pid = -1;
  3904. hdsp->capture_substream = NULL;
  3905. spin_unlock_irq(&hdsp->lock);
  3906. return 0;
  3907. }
  3908. /* helper functions for copying meter values */
  3909. static inline int copy_u32_le(void __user *dest, void __iomem *src)
  3910. {
  3911. u32 val = readl(src);
  3912. return copy_to_user(dest, &val, 4);
  3913. }
  3914. static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
  3915. {
  3916. u32 rms_low, rms_high;
  3917. u64 rms;
  3918. rms_low = readl(src_low);
  3919. rms_high = readl(src_high);
  3920. rms = ((u64)rms_high << 32) | rms_low;
  3921. return copy_to_user(dest, &rms, 8);
  3922. }
  3923. static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
  3924. {
  3925. u32 rms_low, rms_high;
  3926. u64 rms;
  3927. rms_low = readl(src_low) & 0xffffff00;
  3928. rms_high = readl(src_high) & 0xffffff00;
  3929. rms = ((u64)rms_high << 32) | rms_low;
  3930. return copy_to_user(dest, &rms, 8);
  3931. }
  3932. static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3933. {
  3934. int doublespeed = 0;
  3935. int i, j, channels, ofs;
  3936. if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
  3937. doublespeed = 1;
  3938. channels = doublespeed ? 14 : 26;
  3939. for (i = 0, j = 0; i < 26; ++i) {
  3940. if (doublespeed && (i & 4))
  3941. continue;
  3942. ofs = HDSP_9652_peakBase - j * 4;
  3943. if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
  3944. return -EFAULT;
  3945. ofs -= channels * 4;
  3946. if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
  3947. return -EFAULT;
  3948. ofs -= channels * 4;
  3949. if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
  3950. return -EFAULT;
  3951. ofs = HDSP_9652_rmsBase + j * 8;
  3952. if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
  3953. hdsp->iobase + ofs + 4))
  3954. return -EFAULT;
  3955. ofs += channels * 8;
  3956. if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
  3957. hdsp->iobase + ofs + 4))
  3958. return -EFAULT;
  3959. ofs += channels * 8;
  3960. if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
  3961. hdsp->iobase + ofs + 4))
  3962. return -EFAULT;
  3963. j++;
  3964. }
  3965. return 0;
  3966. }
  3967. static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3968. {
  3969. int i, j;
  3970. struct hdsp_9632_meters __iomem *m;
  3971. int doublespeed = 0;
  3972. if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
  3973. doublespeed = 1;
  3974. m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
  3975. for (i = 0, j = 0; i < 16; ++i, ++j) {
  3976. if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
  3977. return -EFAULT;
  3978. if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
  3979. return -EFAULT;
  3980. if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
  3981. return -EFAULT;
  3982. if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
  3983. &m->input_rms_high[j]))
  3984. return -EFAULT;
  3985. if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
  3986. &m->playback_rms_high[j]))
  3987. return -EFAULT;
  3988. if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
  3989. &m->output_rms_high[j]))
  3990. return -EFAULT;
  3991. if (doublespeed && i == 3) i += 4;
  3992. }
  3993. return 0;
  3994. }
  3995. static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3996. {
  3997. int i;
  3998. for (i = 0; i < 26; i++) {
  3999. if (copy_u32_le(&peak_rms->playback_peaks[i],
  4000. hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
  4001. return -EFAULT;
  4002. if (copy_u32_le(&peak_rms->input_peaks[i],
  4003. hdsp->iobase + HDSP_inputPeakLevel + i * 4))
  4004. return -EFAULT;
  4005. }
  4006. for (i = 0; i < 28; i++) {
  4007. if (copy_u32_le(&peak_rms->output_peaks[i],
  4008. hdsp->iobase + HDSP_outputPeakLevel + i * 4))
  4009. return -EFAULT;
  4010. }
  4011. for (i = 0; i < 26; ++i) {
  4012. if (copy_u64_le(&peak_rms->playback_rms[i],
  4013. hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
  4014. hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
  4015. return -EFAULT;
  4016. if (copy_u64_le(&peak_rms->input_rms[i],
  4017. hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
  4018. hdsp->iobase + HDSP_inputRmsLevel + i * 8))
  4019. return -EFAULT;
  4020. }
  4021. return 0;
  4022. }
  4023. static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
  4024. {
  4025. struct hdsp *hdsp = hw->private_data;
  4026. void __user *argp = (void __user *)arg;
  4027. int err;
  4028. switch (cmd) {
  4029. case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
  4030. struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
  4031. err = hdsp_check_for_iobox(hdsp);
  4032. if (err < 0)
  4033. return err;
  4034. err = hdsp_check_for_firmware(hdsp, 1);
  4035. if (err < 0)
  4036. return err;
  4037. if (!(hdsp->state & HDSP_FirmwareLoaded)) {
  4038. dev_err(hdsp->card->dev,
  4039. "firmware needs to be uploaded to the card.\n");
  4040. return -EINVAL;
  4041. }
  4042. switch (hdsp->io_type) {
  4043. case H9652:
  4044. return hdsp_9652_get_peak(hdsp, peak_rms);
  4045. case H9632:
  4046. return hdsp_9632_get_peak(hdsp, peak_rms);
  4047. default:
  4048. return hdsp_get_peak(hdsp, peak_rms);
  4049. }
  4050. }
  4051. case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
  4052. struct hdsp_config_info info;
  4053. unsigned long flags;
  4054. int i;
  4055. err = hdsp_check_for_iobox(hdsp);
  4056. if (err < 0)
  4057. return err;
  4058. err = hdsp_check_for_firmware(hdsp, 1);
  4059. if (err < 0)
  4060. return err;
  4061. memset(&info, 0, sizeof(info));
  4062. spin_lock_irqsave(&hdsp->lock, flags);
  4063. info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
  4064. info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
  4065. if (hdsp->io_type != H9632)
  4066. info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
  4067. info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
  4068. for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
  4069. info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
  4070. info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
  4071. info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
  4072. HDSP_SPDIFOpticalOut);
  4073. info.spdif_professional = (unsigned char)
  4074. hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
  4075. info.spdif_emphasis = (unsigned char)
  4076. hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
  4077. info.spdif_nonaudio = (unsigned char)
  4078. hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
  4079. info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
  4080. info.system_sample_rate = hdsp->system_sample_rate;
  4081. info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
  4082. info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
  4083. info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
  4084. info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
  4085. info.line_out = (unsigned char)
  4086. hdsp_toggle_setting(hdsp, HDSP_LineOut);
  4087. if (hdsp->io_type == H9632) {
  4088. info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
  4089. info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
  4090. info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
  4091. info.xlr_breakout_cable =
  4092. (unsigned char)hdsp_toggle_setting(hdsp,
  4093. HDSP_XLRBreakoutCable);
  4094. } else if (hdsp->io_type == RPM) {
  4095. info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
  4096. info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
  4097. }
  4098. if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
  4099. info.analog_extension_board =
  4100. (unsigned char)hdsp_toggle_setting(hdsp,
  4101. HDSP_AnalogExtensionBoard);
  4102. spin_unlock_irqrestore(&hdsp->lock, flags);
  4103. if (copy_to_user(argp, &info, sizeof(info)))
  4104. return -EFAULT;
  4105. break;
  4106. }
  4107. case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
  4108. struct hdsp_9632_aeb h9632_aeb;
  4109. if (hdsp->io_type != H9632) return -EINVAL;
  4110. h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
  4111. h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
  4112. if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
  4113. return -EFAULT;
  4114. break;
  4115. }
  4116. case SNDRV_HDSP_IOCTL_GET_VERSION: {
  4117. struct hdsp_version hdsp_version;
  4118. int err;
  4119. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
  4120. if (hdsp->io_type == Undefined) {
  4121. if ((err = hdsp_get_iobox_version(hdsp)) < 0)
  4122. return err;
  4123. }
  4124. memset(&hdsp_version, 0, sizeof(hdsp_version));
  4125. hdsp_version.io_type = hdsp->io_type;
  4126. hdsp_version.firmware_rev = hdsp->firmware_rev;
  4127. if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
  4128. return -EFAULT;
  4129. break;
  4130. }
  4131. case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
  4132. struct hdsp_firmware __user *firmware;
  4133. u32 __user *firmware_data;
  4134. int err;
  4135. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
  4136. /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
  4137. if (hdsp->io_type == Undefined) return -EINVAL;
  4138. if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
  4139. return -EBUSY;
  4140. dev_info(hdsp->card->dev,
  4141. "initializing firmware upload\n");
  4142. firmware = (struct hdsp_firmware __user *)argp;
  4143. if (get_user(firmware_data, &firmware->firmware_data))
  4144. return -EFAULT;
  4145. if (hdsp_check_for_iobox (hdsp))
  4146. return -EIO;
  4147. if (!hdsp->fw_uploaded) {
  4148. hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
  4149. if (!hdsp->fw_uploaded)
  4150. return -ENOMEM;
  4151. }
  4152. if (copy_from_user(hdsp->fw_uploaded, firmware_data,
  4153. HDSP_FIRMWARE_SIZE)) {
  4154. vfree(hdsp->fw_uploaded);
  4155. hdsp->fw_uploaded = NULL;
  4156. return -EFAULT;
  4157. }
  4158. hdsp->state |= HDSP_FirmwareCached;
  4159. if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
  4160. return err;
  4161. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4162. if ((err = snd_hdsp_enable_io(hdsp)) < 0)
  4163. return err;
  4164. snd_hdsp_initialize_channels(hdsp);
  4165. snd_hdsp_initialize_midi_flush(hdsp);
  4166. if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
  4167. dev_err(hdsp->card->dev,
  4168. "error creating alsa devices\n");
  4169. return err;
  4170. }
  4171. }
  4172. break;
  4173. }
  4174. case SNDRV_HDSP_IOCTL_GET_MIXER: {
  4175. struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
  4176. if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
  4177. return -EFAULT;
  4178. break;
  4179. }
  4180. default:
  4181. return -EINVAL;
  4182. }
  4183. return 0;
  4184. }
  4185. static const struct snd_pcm_ops snd_hdsp_playback_ops = {
  4186. .open = snd_hdsp_playback_open,
  4187. .close = snd_hdsp_playback_release,
  4188. .ioctl = snd_hdsp_ioctl,
  4189. .hw_params = snd_hdsp_hw_params,
  4190. .prepare = snd_hdsp_prepare,
  4191. .trigger = snd_hdsp_trigger,
  4192. .pointer = snd_hdsp_hw_pointer,
  4193. .copy = snd_hdsp_playback_copy,
  4194. .silence = snd_hdsp_hw_silence,
  4195. };
  4196. static const struct snd_pcm_ops snd_hdsp_capture_ops = {
  4197. .open = snd_hdsp_capture_open,
  4198. .close = snd_hdsp_capture_release,
  4199. .ioctl = snd_hdsp_ioctl,
  4200. .hw_params = snd_hdsp_hw_params,
  4201. .prepare = snd_hdsp_prepare,
  4202. .trigger = snd_hdsp_trigger,
  4203. .pointer = snd_hdsp_hw_pointer,
  4204. .copy = snd_hdsp_capture_copy,
  4205. };
  4206. static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
  4207. {
  4208. struct snd_hwdep *hw;
  4209. int err;
  4210. if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
  4211. return err;
  4212. hdsp->hwdep = hw;
  4213. hw->private_data = hdsp;
  4214. strcpy(hw->name, "HDSP hwdep interface");
  4215. hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
  4216. hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
  4217. return 0;
  4218. }
  4219. static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
  4220. {
  4221. struct snd_pcm *pcm;
  4222. int err;
  4223. if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
  4224. return err;
  4225. hdsp->pcm = pcm;
  4226. pcm->private_data = hdsp;
  4227. strcpy(pcm->name, hdsp->card_name);
  4228. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
  4229. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
  4230. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  4231. return 0;
  4232. }
  4233. static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
  4234. {
  4235. hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
  4236. hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
  4237. }
  4238. static int snd_hdsp_enable_io (struct hdsp *hdsp)
  4239. {
  4240. int i;
  4241. if (hdsp_fifo_wait (hdsp, 0, 100)) {
  4242. dev_err(hdsp->card->dev,
  4243. "enable_io fifo_wait failed\n");
  4244. return -EIO;
  4245. }
  4246. for (i = 0; i < hdsp->max_channels; ++i) {
  4247. hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
  4248. hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
  4249. }
  4250. return 0;
  4251. }
  4252. static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
  4253. {
  4254. int status, aebi_channels, aebo_channels;
  4255. switch (hdsp->io_type) {
  4256. case Digiface:
  4257. hdsp->card_name = "RME Hammerfall DSP + Digiface";
  4258. hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
  4259. hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
  4260. break;
  4261. case H9652:
  4262. hdsp->card_name = "RME Hammerfall HDSP 9652";
  4263. hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
  4264. hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
  4265. break;
  4266. case H9632:
  4267. status = hdsp_read(hdsp, HDSP_statusRegister);
  4268. /* HDSP_AEBx bits are low when AEB are connected */
  4269. aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
  4270. aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
  4271. hdsp->card_name = "RME Hammerfall HDSP 9632";
  4272. hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
  4273. hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
  4274. hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
  4275. hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
  4276. hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
  4277. hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
  4278. break;
  4279. case Multiface:
  4280. hdsp->card_name = "RME Hammerfall DSP + Multiface";
  4281. hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
  4282. hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
  4283. break;
  4284. case RPM:
  4285. hdsp->card_name = "RME Hammerfall DSP + RPM";
  4286. hdsp->ss_in_channels = RPM_CHANNELS-1;
  4287. hdsp->ss_out_channels = RPM_CHANNELS;
  4288. hdsp->ds_in_channels = RPM_CHANNELS-1;
  4289. hdsp->ds_out_channels = RPM_CHANNELS;
  4290. break;
  4291. default:
  4292. /* should never get here */
  4293. break;
  4294. }
  4295. }
  4296. static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
  4297. {
  4298. snd_hdsp_flush_midi_input (hdsp, 0);
  4299. snd_hdsp_flush_midi_input (hdsp, 1);
  4300. }
  4301. static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
  4302. {
  4303. int err;
  4304. if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
  4305. dev_err(card->dev,
  4306. "Error creating pcm interface\n");
  4307. return err;
  4308. }
  4309. if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
  4310. dev_err(card->dev,
  4311. "Error creating first midi interface\n");
  4312. return err;
  4313. }
  4314. if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
  4315. if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
  4316. dev_err(card->dev,
  4317. "Error creating second midi interface\n");
  4318. return err;
  4319. }
  4320. }
  4321. if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
  4322. dev_err(card->dev,
  4323. "Error creating ctl interface\n");
  4324. return err;
  4325. }
  4326. snd_hdsp_proc_init(hdsp);
  4327. hdsp->system_sample_rate = -1;
  4328. hdsp->playback_pid = -1;
  4329. hdsp->capture_pid = -1;
  4330. hdsp->capture_substream = NULL;
  4331. hdsp->playback_substream = NULL;
  4332. if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
  4333. dev_err(card->dev,
  4334. "Error setting default values\n");
  4335. return err;
  4336. }
  4337. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4338. strcpy(card->shortname, "Hammerfall DSP");
  4339. sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
  4340. hdsp->port, hdsp->irq);
  4341. if ((err = snd_card_register(card)) < 0) {
  4342. dev_err(card->dev,
  4343. "error registering card\n");
  4344. return err;
  4345. }
  4346. hdsp->state |= HDSP_InitializationComplete;
  4347. }
  4348. return 0;
  4349. }
  4350. /* load firmware via hotplug fw loader */
  4351. static int hdsp_request_fw_loader(struct hdsp *hdsp)
  4352. {
  4353. const char *fwfile;
  4354. const struct firmware *fw;
  4355. int err;
  4356. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  4357. return 0;
  4358. if (hdsp->io_type == Undefined) {
  4359. if ((err = hdsp_get_iobox_version(hdsp)) < 0)
  4360. return err;
  4361. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  4362. return 0;
  4363. }
  4364. /* caution: max length of firmware filename is 30! */
  4365. switch (hdsp->io_type) {
  4366. case RPM:
  4367. fwfile = "/*(DEBLOBBED)*/";
  4368. break;
  4369. case Multiface:
  4370. if (hdsp->firmware_rev == 0xa)
  4371. fwfile = "/*(DEBLOBBED)*/";
  4372. else
  4373. fwfile = "/*(DEBLOBBED)*/";
  4374. break;
  4375. case Digiface:
  4376. if (hdsp->firmware_rev == 0xa)
  4377. fwfile = "/*(DEBLOBBED)*/";
  4378. else
  4379. fwfile = "/*(DEBLOBBED)*/";
  4380. break;
  4381. default:
  4382. dev_err(hdsp->card->dev,
  4383. "invalid io_type %d\n", hdsp->io_type);
  4384. return -EINVAL;
  4385. }
  4386. if (reject_firmware(&fw, fwfile, &hdsp->pci->dev)) {
  4387. dev_err(hdsp->card->dev,
  4388. "cannot load firmware %s\n", fwfile);
  4389. return -ENOENT;
  4390. }
  4391. if (fw->size < HDSP_FIRMWARE_SIZE) {
  4392. dev_err(hdsp->card->dev,
  4393. "too short firmware size %d (expected %d)\n",
  4394. (int)fw->size, HDSP_FIRMWARE_SIZE);
  4395. release_firmware(fw);
  4396. return -EINVAL;
  4397. }
  4398. hdsp->firmware = fw;
  4399. hdsp->state |= HDSP_FirmwareCached;
  4400. if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
  4401. return err;
  4402. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4403. if ((err = snd_hdsp_enable_io(hdsp)) < 0)
  4404. return err;
  4405. if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
  4406. dev_err(hdsp->card->dev,
  4407. "error creating hwdep device\n");
  4408. return err;
  4409. }
  4410. snd_hdsp_initialize_channels(hdsp);
  4411. snd_hdsp_initialize_midi_flush(hdsp);
  4412. if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
  4413. dev_err(hdsp->card->dev,
  4414. "error creating alsa devices\n");
  4415. return err;
  4416. }
  4417. }
  4418. return 0;
  4419. }
  4420. static int snd_hdsp_create(struct snd_card *card,
  4421. struct hdsp *hdsp)
  4422. {
  4423. struct pci_dev *pci = hdsp->pci;
  4424. int err;
  4425. int is_9652 = 0;
  4426. int is_9632 = 0;
  4427. hdsp->irq = -1;
  4428. hdsp->state = 0;
  4429. hdsp->midi[0].rmidi = NULL;
  4430. hdsp->midi[1].rmidi = NULL;
  4431. hdsp->midi[0].input = NULL;
  4432. hdsp->midi[1].input = NULL;
  4433. hdsp->midi[0].output = NULL;
  4434. hdsp->midi[1].output = NULL;
  4435. hdsp->midi[0].pending = 0;
  4436. hdsp->midi[1].pending = 0;
  4437. spin_lock_init(&hdsp->midi[0].lock);
  4438. spin_lock_init(&hdsp->midi[1].lock);
  4439. hdsp->iobase = NULL;
  4440. hdsp->control_register = 0;
  4441. hdsp->control2_register = 0;
  4442. hdsp->io_type = Undefined;
  4443. hdsp->max_channels = 26;
  4444. hdsp->card = card;
  4445. spin_lock_init(&hdsp->lock);
  4446. tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
  4447. pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
  4448. hdsp->firmware_rev &= 0xff;
  4449. /* From Martin Bjoernsen :
  4450. "It is important that the card's latency timer register in
  4451. the PCI configuration space is set to a value much larger
  4452. than 0 by the computer's BIOS or the driver.
  4453. The windows driver always sets this 8 bit register [...]
  4454. to its maximum 255 to avoid problems with some computers."
  4455. */
  4456. pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
  4457. strcpy(card->driver, "H-DSP");
  4458. strcpy(card->mixername, "Xilinx FPGA");
  4459. if (hdsp->firmware_rev < 0xa)
  4460. return -ENODEV;
  4461. else if (hdsp->firmware_rev < 0x64)
  4462. hdsp->card_name = "RME Hammerfall DSP";
  4463. else if (hdsp->firmware_rev < 0x96) {
  4464. hdsp->card_name = "RME HDSP 9652";
  4465. is_9652 = 1;
  4466. } else {
  4467. hdsp->card_name = "RME HDSP 9632";
  4468. hdsp->max_channels = 16;
  4469. is_9632 = 1;
  4470. }
  4471. if ((err = pci_enable_device(pci)) < 0)
  4472. return err;
  4473. pci_set_master(hdsp->pci);
  4474. if ((err = pci_request_regions(pci, "hdsp")) < 0)
  4475. return err;
  4476. hdsp->port = pci_resource_start(pci, 0);
  4477. if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
  4478. dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
  4479. hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
  4480. return -EBUSY;
  4481. }
  4482. if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
  4483. KBUILD_MODNAME, hdsp)) {
  4484. dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
  4485. return -EBUSY;
  4486. }
  4487. hdsp->irq = pci->irq;
  4488. hdsp->precise_ptr = 0;
  4489. hdsp->use_midi_tasklet = 1;
  4490. hdsp->dds_value = 0;
  4491. if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
  4492. return err;
  4493. if (!is_9652 && !is_9632) {
  4494. /* we wait a maximum of 10 seconds to let freshly
  4495. * inserted cardbus cards do their hardware init */
  4496. err = hdsp_wait_for_iobox(hdsp, 1000, 10);
  4497. if (err < 0)
  4498. return err;
  4499. if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
  4500. if ((err = hdsp_request_fw_loader(hdsp)) < 0)
  4501. /* we don't fail as this can happen
  4502. if userspace is not ready for
  4503. firmware upload
  4504. */
  4505. dev_err(hdsp->card->dev,
  4506. "couldn't get firmware from userspace. try using hdsploader\n");
  4507. else
  4508. /* init is complete, we return */
  4509. return 0;
  4510. /* we defer initialization */
  4511. dev_info(hdsp->card->dev,
  4512. "card initialization pending : waiting for firmware\n");
  4513. if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
  4514. return err;
  4515. return 0;
  4516. } else {
  4517. dev_info(hdsp->card->dev,
  4518. "Firmware already present, initializing card.\n");
  4519. if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
  4520. hdsp->io_type = RPM;
  4521. else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
  4522. hdsp->io_type = Multiface;
  4523. else
  4524. hdsp->io_type = Digiface;
  4525. }
  4526. }
  4527. if ((err = snd_hdsp_enable_io(hdsp)) != 0)
  4528. return err;
  4529. if (is_9652)
  4530. hdsp->io_type = H9652;
  4531. if (is_9632)
  4532. hdsp->io_type = H9632;
  4533. if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
  4534. return err;
  4535. snd_hdsp_initialize_channels(hdsp);
  4536. snd_hdsp_initialize_midi_flush(hdsp);
  4537. hdsp->state |= HDSP_FirmwareLoaded;
  4538. if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
  4539. return err;
  4540. return 0;
  4541. }
  4542. static int snd_hdsp_free(struct hdsp *hdsp)
  4543. {
  4544. if (hdsp->port) {
  4545. /* stop the audio, and cancel all interrupts */
  4546. tasklet_kill(&hdsp->midi_tasklet);
  4547. hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
  4548. hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
  4549. }
  4550. if (hdsp->irq >= 0)
  4551. free_irq(hdsp->irq, (void *)hdsp);
  4552. snd_hdsp_free_buffers(hdsp);
  4553. release_firmware(hdsp->firmware);
  4554. vfree(hdsp->fw_uploaded);
  4555. iounmap(hdsp->iobase);
  4556. if (hdsp->port)
  4557. pci_release_regions(hdsp->pci);
  4558. pci_disable_device(hdsp->pci);
  4559. return 0;
  4560. }
  4561. static void snd_hdsp_card_free(struct snd_card *card)
  4562. {
  4563. struct hdsp *hdsp = card->private_data;
  4564. if (hdsp)
  4565. snd_hdsp_free(hdsp);
  4566. }
  4567. static int snd_hdsp_probe(struct pci_dev *pci,
  4568. const struct pci_device_id *pci_id)
  4569. {
  4570. static int dev;
  4571. struct hdsp *hdsp;
  4572. struct snd_card *card;
  4573. int err;
  4574. if (dev >= SNDRV_CARDS)
  4575. return -ENODEV;
  4576. if (!enable[dev]) {
  4577. dev++;
  4578. return -ENOENT;
  4579. }
  4580. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  4581. sizeof(struct hdsp), &card);
  4582. if (err < 0)
  4583. return err;
  4584. hdsp = card->private_data;
  4585. card->private_free = snd_hdsp_card_free;
  4586. hdsp->dev = dev;
  4587. hdsp->pci = pci;
  4588. if ((err = snd_hdsp_create(card, hdsp)) < 0) {
  4589. snd_card_free(card);
  4590. return err;
  4591. }
  4592. strcpy(card->shortname, "Hammerfall DSP");
  4593. sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
  4594. hdsp->port, hdsp->irq);
  4595. if ((err = snd_card_register(card)) < 0) {
  4596. snd_card_free(card);
  4597. return err;
  4598. }
  4599. pci_set_drvdata(pci, card);
  4600. dev++;
  4601. return 0;
  4602. }
  4603. static void snd_hdsp_remove(struct pci_dev *pci)
  4604. {
  4605. snd_card_free(pci_get_drvdata(pci));
  4606. }
  4607. static struct pci_driver hdsp_driver = {
  4608. .name = KBUILD_MODNAME,
  4609. .id_table = snd_hdsp_ids,
  4610. .probe = snd_hdsp_probe,
  4611. .remove = snd_hdsp_remove,
  4612. };
  4613. module_pci_driver(hdsp_driver);