hdsp.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377
  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[32];
  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. sprintf (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. snd_BUG_ON(offset < 0);
  2407. switch (hdsp->io_type) {
  2408. case Digiface:
  2409. case H9652:
  2410. if (offset >= 3)
  2411. return -EINVAL;
  2412. break;
  2413. case Multiface:
  2414. case H9632:
  2415. if (offset >= 1)
  2416. return -EINVAL;
  2417. break;
  2418. default:
  2419. return -EIO;
  2420. }
  2421. ucontrol->value.enumerated.item[0] = hdsp_adat_sync_check(hdsp, offset);
  2422. return 0;
  2423. }
  2424. #define HDSP_DDS_OFFSET(xname, xindex) \
  2425. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  2426. .name = xname, \
  2427. .index = xindex, \
  2428. .info = snd_hdsp_info_dds_offset, \
  2429. .get = snd_hdsp_get_dds_offset, \
  2430. .put = snd_hdsp_put_dds_offset \
  2431. }
  2432. static int hdsp_dds_offset(struct hdsp *hdsp)
  2433. {
  2434. u64 n;
  2435. unsigned int dds_value = hdsp->dds_value;
  2436. int system_sample_rate = hdsp->system_sample_rate;
  2437. if (!dds_value)
  2438. return 0;
  2439. n = DDS_NUMERATOR;
  2440. /*
  2441. * dds_value = n / rate
  2442. * rate = n / dds_value
  2443. */
  2444. n = div_u64(n, dds_value);
  2445. if (system_sample_rate >= 112000)
  2446. n *= 4;
  2447. else if (system_sample_rate >= 56000)
  2448. n *= 2;
  2449. return ((int)n) - system_sample_rate;
  2450. }
  2451. static int hdsp_set_dds_offset(struct hdsp *hdsp, int offset_hz)
  2452. {
  2453. int rate = hdsp->system_sample_rate + offset_hz;
  2454. hdsp_set_dds_value(hdsp, rate);
  2455. return 0;
  2456. }
  2457. static int snd_hdsp_info_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2458. {
  2459. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2460. uinfo->count = 1;
  2461. uinfo->value.integer.min = -5000;
  2462. uinfo->value.integer.max = 5000;
  2463. return 0;
  2464. }
  2465. static int snd_hdsp_get_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2466. {
  2467. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2468. ucontrol->value.enumerated.item[0] = hdsp_dds_offset(hdsp);
  2469. return 0;
  2470. }
  2471. static int snd_hdsp_put_dds_offset(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2472. {
  2473. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2474. int change;
  2475. int val;
  2476. if (!snd_hdsp_use_is_exclusive(hdsp))
  2477. return -EBUSY;
  2478. val = ucontrol->value.enumerated.item[0];
  2479. spin_lock_irq(&hdsp->lock);
  2480. if (val != hdsp_dds_offset(hdsp))
  2481. change = (hdsp_set_dds_offset(hdsp, val) == 0) ? 1 : 0;
  2482. else
  2483. change = 0;
  2484. spin_unlock_irq(&hdsp->lock);
  2485. return change;
  2486. }
  2487. static struct snd_kcontrol_new snd_hdsp_9632_controls[] = {
  2488. HDSP_DA_GAIN("DA Gain", 0),
  2489. HDSP_AD_GAIN("AD Gain", 0),
  2490. HDSP_PHONE_GAIN("Phones Gain", 0),
  2491. HDSP_TOGGLE_SETTING("XLR Breakout Cable", HDSP_XLRBreakoutCable),
  2492. HDSP_DDS_OFFSET("DDS Sample Rate Offset", 0)
  2493. };
  2494. static struct snd_kcontrol_new snd_hdsp_controls[] = {
  2495. {
  2496. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2497. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  2498. .info = snd_hdsp_control_spdif_info,
  2499. .get = snd_hdsp_control_spdif_get,
  2500. .put = snd_hdsp_control_spdif_put,
  2501. },
  2502. {
  2503. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2504. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2505. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2506. .info = snd_hdsp_control_spdif_stream_info,
  2507. .get = snd_hdsp_control_spdif_stream_get,
  2508. .put = snd_hdsp_control_spdif_stream_put,
  2509. },
  2510. {
  2511. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2512. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2513. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  2514. .info = snd_hdsp_control_spdif_mask_info,
  2515. .get = snd_hdsp_control_spdif_mask_get,
  2516. .private_value = IEC958_AES0_NONAUDIO |
  2517. IEC958_AES0_PROFESSIONAL |
  2518. IEC958_AES0_CON_EMPHASIS,
  2519. },
  2520. {
  2521. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2522. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2523. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
  2524. .info = snd_hdsp_control_spdif_mask_info,
  2525. .get = snd_hdsp_control_spdif_mask_get,
  2526. .private_value = IEC958_AES0_NONAUDIO |
  2527. IEC958_AES0_PROFESSIONAL |
  2528. IEC958_AES0_PRO_EMPHASIS,
  2529. },
  2530. HDSP_MIXER("Mixer", 0),
  2531. HDSP_SPDIF_IN("IEC958 Input Connector", 0),
  2532. HDSP_TOGGLE_SETTING("IEC958 Output also on ADAT1", HDSP_SPDIFOpticalOut),
  2533. HDSP_TOGGLE_SETTING("IEC958 Professional Bit", HDSP_SPDIFProfessional),
  2534. HDSP_TOGGLE_SETTING("IEC958 Emphasis Bit", HDSP_SPDIFEmphasis),
  2535. HDSP_TOGGLE_SETTING("IEC958 Non-audio Bit", HDSP_SPDIFNonAudio),
  2536. /* 'Sample Clock Source' complies with the alsa control naming scheme */
  2537. HDSP_CLOCK_SOURCE("Sample Clock Source", 0),
  2538. {
  2539. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2540. .name = "Sample Clock Source Locking",
  2541. .info = snd_hdsp_info_clock_source_lock,
  2542. .get = snd_hdsp_get_clock_source_lock,
  2543. .put = snd_hdsp_put_clock_source_lock,
  2544. },
  2545. HDSP_SYSTEM_CLOCK_MODE("System Clock Mode", 0),
  2546. HDSP_PREF_SYNC_REF("Preferred Sync Reference", 0),
  2547. HDSP_AUTOSYNC_REF("AutoSync Reference", 0),
  2548. HDSP_SPDIF_SAMPLE_RATE("SPDIF Sample Rate", 0),
  2549. HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
  2550. /* 'External Rate' complies with the alsa control naming scheme */
  2551. HDSP_AUTOSYNC_SAMPLE_RATE("External Rate", 0),
  2552. HDSP_WC_SYNC_CHECK("Word Clock Lock Status", 0),
  2553. HDSP_SPDIF_SYNC_CHECK("SPDIF Lock Status", 0),
  2554. HDSP_ADATSYNC_SYNC_CHECK("ADAT Sync Lock Status", 0),
  2555. HDSP_TOGGLE_SETTING("Line Out", HDSP_LineOut),
  2556. HDSP_PRECISE_POINTER("Precise Pointer", 0),
  2557. HDSP_USE_MIDI_TASKLET("Use Midi Tasklet", 0),
  2558. };
  2559. static int hdsp_rpm_input12(struct hdsp *hdsp)
  2560. {
  2561. switch (hdsp->control_register & HDSP_RPM_Inp12) {
  2562. case HDSP_RPM_Inp12_Phon_6dB:
  2563. return 0;
  2564. case HDSP_RPM_Inp12_Phon_n6dB:
  2565. return 2;
  2566. case HDSP_RPM_Inp12_Line_0dB:
  2567. return 3;
  2568. case HDSP_RPM_Inp12_Line_n6dB:
  2569. return 4;
  2570. }
  2571. return 1;
  2572. }
  2573. static int snd_hdsp_get_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2574. {
  2575. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2576. ucontrol->value.enumerated.item[0] = hdsp_rpm_input12(hdsp);
  2577. return 0;
  2578. }
  2579. static int hdsp_set_rpm_input12(struct hdsp *hdsp, int mode)
  2580. {
  2581. hdsp->control_register &= ~HDSP_RPM_Inp12;
  2582. switch (mode) {
  2583. case 0:
  2584. hdsp->control_register |= HDSP_RPM_Inp12_Phon_6dB;
  2585. break;
  2586. case 1:
  2587. break;
  2588. case 2:
  2589. hdsp->control_register |= HDSP_RPM_Inp12_Phon_n6dB;
  2590. break;
  2591. case 3:
  2592. hdsp->control_register |= HDSP_RPM_Inp12_Line_0dB;
  2593. break;
  2594. case 4:
  2595. hdsp->control_register |= HDSP_RPM_Inp12_Line_n6dB;
  2596. break;
  2597. default:
  2598. return -1;
  2599. }
  2600. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2601. return 0;
  2602. }
  2603. static int snd_hdsp_put_rpm_input12(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2604. {
  2605. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2606. int change;
  2607. int val;
  2608. if (!snd_hdsp_use_is_exclusive(hdsp))
  2609. return -EBUSY;
  2610. val = ucontrol->value.enumerated.item[0];
  2611. if (val < 0)
  2612. val = 0;
  2613. if (val > 4)
  2614. val = 4;
  2615. spin_lock_irq(&hdsp->lock);
  2616. if (val != hdsp_rpm_input12(hdsp))
  2617. change = (hdsp_set_rpm_input12(hdsp, val) == 0) ? 1 : 0;
  2618. else
  2619. change = 0;
  2620. spin_unlock_irq(&hdsp->lock);
  2621. return change;
  2622. }
  2623. static int snd_hdsp_info_rpm_input(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2624. {
  2625. static const char * const texts[] = {
  2626. "Phono +6dB", "Phono 0dB", "Phono -6dB", "Line 0dB", "Line -6dB"
  2627. };
  2628. return snd_ctl_enum_info(uinfo, 1, 5, texts);
  2629. }
  2630. static int hdsp_rpm_input34(struct hdsp *hdsp)
  2631. {
  2632. switch (hdsp->control_register & HDSP_RPM_Inp34) {
  2633. case HDSP_RPM_Inp34_Phon_6dB:
  2634. return 0;
  2635. case HDSP_RPM_Inp34_Phon_n6dB:
  2636. return 2;
  2637. case HDSP_RPM_Inp34_Line_0dB:
  2638. return 3;
  2639. case HDSP_RPM_Inp34_Line_n6dB:
  2640. return 4;
  2641. }
  2642. return 1;
  2643. }
  2644. static int snd_hdsp_get_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2645. {
  2646. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2647. ucontrol->value.enumerated.item[0] = hdsp_rpm_input34(hdsp);
  2648. return 0;
  2649. }
  2650. static int hdsp_set_rpm_input34(struct hdsp *hdsp, int mode)
  2651. {
  2652. hdsp->control_register &= ~HDSP_RPM_Inp34;
  2653. switch (mode) {
  2654. case 0:
  2655. hdsp->control_register |= HDSP_RPM_Inp34_Phon_6dB;
  2656. break;
  2657. case 1:
  2658. break;
  2659. case 2:
  2660. hdsp->control_register |= HDSP_RPM_Inp34_Phon_n6dB;
  2661. break;
  2662. case 3:
  2663. hdsp->control_register |= HDSP_RPM_Inp34_Line_0dB;
  2664. break;
  2665. case 4:
  2666. hdsp->control_register |= HDSP_RPM_Inp34_Line_n6dB;
  2667. break;
  2668. default:
  2669. return -1;
  2670. }
  2671. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2672. return 0;
  2673. }
  2674. static int snd_hdsp_put_rpm_input34(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2675. {
  2676. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2677. int change;
  2678. int val;
  2679. if (!snd_hdsp_use_is_exclusive(hdsp))
  2680. return -EBUSY;
  2681. val = ucontrol->value.enumerated.item[0];
  2682. if (val < 0)
  2683. val = 0;
  2684. if (val > 4)
  2685. val = 4;
  2686. spin_lock_irq(&hdsp->lock);
  2687. if (val != hdsp_rpm_input34(hdsp))
  2688. change = (hdsp_set_rpm_input34(hdsp, val) == 0) ? 1 : 0;
  2689. else
  2690. change = 0;
  2691. spin_unlock_irq(&hdsp->lock);
  2692. return change;
  2693. }
  2694. /* RPM Bypass switch */
  2695. static int hdsp_rpm_bypass(struct hdsp *hdsp)
  2696. {
  2697. return (hdsp->control_register & HDSP_RPM_Bypass) ? 1 : 0;
  2698. }
  2699. static int snd_hdsp_get_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2700. {
  2701. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2702. ucontrol->value.integer.value[0] = hdsp_rpm_bypass(hdsp);
  2703. return 0;
  2704. }
  2705. static int hdsp_set_rpm_bypass(struct hdsp *hdsp, int on)
  2706. {
  2707. if (on)
  2708. hdsp->control_register |= HDSP_RPM_Bypass;
  2709. else
  2710. hdsp->control_register &= ~HDSP_RPM_Bypass;
  2711. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2712. return 0;
  2713. }
  2714. static int snd_hdsp_put_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2715. {
  2716. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2717. int change;
  2718. unsigned int val;
  2719. if (!snd_hdsp_use_is_exclusive(hdsp))
  2720. return -EBUSY;
  2721. val = ucontrol->value.integer.value[0] & 1;
  2722. spin_lock_irq(&hdsp->lock);
  2723. change = (int)val != hdsp_rpm_bypass(hdsp);
  2724. hdsp_set_rpm_bypass(hdsp, val);
  2725. spin_unlock_irq(&hdsp->lock);
  2726. return change;
  2727. }
  2728. static int snd_hdsp_info_rpm_bypass(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2729. {
  2730. static const char * const texts[] = {"On", "Off"};
  2731. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2732. }
  2733. /* RPM Disconnect switch */
  2734. static int hdsp_rpm_disconnect(struct hdsp *hdsp)
  2735. {
  2736. return (hdsp->control_register & HDSP_RPM_Disconnect) ? 1 : 0;
  2737. }
  2738. static int snd_hdsp_get_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2739. {
  2740. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2741. ucontrol->value.integer.value[0] = hdsp_rpm_disconnect(hdsp);
  2742. return 0;
  2743. }
  2744. static int hdsp_set_rpm_disconnect(struct hdsp *hdsp, int on)
  2745. {
  2746. if (on)
  2747. hdsp->control_register |= HDSP_RPM_Disconnect;
  2748. else
  2749. hdsp->control_register &= ~HDSP_RPM_Disconnect;
  2750. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  2751. return 0;
  2752. }
  2753. static int snd_hdsp_put_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  2754. {
  2755. struct hdsp *hdsp = snd_kcontrol_chip(kcontrol);
  2756. int change;
  2757. unsigned int val;
  2758. if (!snd_hdsp_use_is_exclusive(hdsp))
  2759. return -EBUSY;
  2760. val = ucontrol->value.integer.value[0] & 1;
  2761. spin_lock_irq(&hdsp->lock);
  2762. change = (int)val != hdsp_rpm_disconnect(hdsp);
  2763. hdsp_set_rpm_disconnect(hdsp, val);
  2764. spin_unlock_irq(&hdsp->lock);
  2765. return change;
  2766. }
  2767. static int snd_hdsp_info_rpm_disconnect(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  2768. {
  2769. static const char * const texts[] = {"On", "Off"};
  2770. return snd_ctl_enum_info(uinfo, 1, 2, texts);
  2771. }
  2772. static struct snd_kcontrol_new snd_hdsp_rpm_controls[] = {
  2773. {
  2774. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2775. .name = "RPM Bypass",
  2776. .get = snd_hdsp_get_rpm_bypass,
  2777. .put = snd_hdsp_put_rpm_bypass,
  2778. .info = snd_hdsp_info_rpm_bypass
  2779. },
  2780. {
  2781. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2782. .name = "RPM Disconnect",
  2783. .get = snd_hdsp_get_rpm_disconnect,
  2784. .put = snd_hdsp_put_rpm_disconnect,
  2785. .info = snd_hdsp_info_rpm_disconnect
  2786. },
  2787. {
  2788. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2789. .name = "Input 1/2",
  2790. .get = snd_hdsp_get_rpm_input12,
  2791. .put = snd_hdsp_put_rpm_input12,
  2792. .info = snd_hdsp_info_rpm_input
  2793. },
  2794. {
  2795. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2796. .name = "Input 3/4",
  2797. .get = snd_hdsp_get_rpm_input34,
  2798. .put = snd_hdsp_put_rpm_input34,
  2799. .info = snd_hdsp_info_rpm_input
  2800. },
  2801. HDSP_SYSTEM_SAMPLE_RATE("System Sample Rate", 0),
  2802. HDSP_MIXER("Mixer", 0)
  2803. };
  2804. static struct snd_kcontrol_new snd_hdsp_96xx_aeb =
  2805. HDSP_TOGGLE_SETTING("Analog Extension Board",
  2806. HDSP_AnalogExtensionBoard);
  2807. static struct snd_kcontrol_new snd_hdsp_adat_sync_check = HDSP_ADAT_SYNC_CHECK;
  2808. static int snd_hdsp_create_controls(struct snd_card *card, struct hdsp *hdsp)
  2809. {
  2810. unsigned int idx;
  2811. int err;
  2812. struct snd_kcontrol *kctl;
  2813. if (hdsp->io_type == RPM) {
  2814. /* RPM Bypass, Disconnect and Input switches */
  2815. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_rpm_controls); idx++) {
  2816. err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_rpm_controls[idx], hdsp));
  2817. if (err < 0)
  2818. return err;
  2819. }
  2820. return 0;
  2821. }
  2822. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_controls); idx++) {
  2823. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_controls[idx], hdsp))) < 0)
  2824. return err;
  2825. if (idx == 1) /* IEC958 (S/PDIF) Stream */
  2826. hdsp->spdif_ctl = kctl;
  2827. }
  2828. /* ADAT SyncCheck status */
  2829. snd_hdsp_adat_sync_check.name = "ADAT Lock Status";
  2830. snd_hdsp_adat_sync_check.index = 1;
  2831. if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
  2832. return err;
  2833. if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
  2834. for (idx = 1; idx < 3; ++idx) {
  2835. snd_hdsp_adat_sync_check.index = idx+1;
  2836. if ((err = snd_ctl_add (card, kctl = snd_ctl_new1(&snd_hdsp_adat_sync_check, hdsp))))
  2837. return err;
  2838. }
  2839. }
  2840. /* DA, AD and Phone gain and XLR breakout cable controls for H9632 cards */
  2841. if (hdsp->io_type == H9632) {
  2842. for (idx = 0; idx < ARRAY_SIZE(snd_hdsp_9632_controls); idx++) {
  2843. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_9632_controls[idx], hdsp))) < 0)
  2844. return err;
  2845. }
  2846. }
  2847. /* AEB control for H96xx card */
  2848. if (hdsp->io_type == H9632 || hdsp->io_type == H9652) {
  2849. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_hdsp_96xx_aeb, hdsp))) < 0)
  2850. return err;
  2851. }
  2852. return 0;
  2853. }
  2854. /*------------------------------------------------------------
  2855. /proc interface
  2856. ------------------------------------------------------------*/
  2857. static void
  2858. snd_hdsp_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer)
  2859. {
  2860. struct hdsp *hdsp = entry->private_data;
  2861. unsigned int status;
  2862. unsigned int status2;
  2863. char *pref_sync_ref;
  2864. char *autosync_ref;
  2865. char *system_clock_mode;
  2866. char *clock_source;
  2867. int x;
  2868. status = hdsp_read(hdsp, HDSP_statusRegister);
  2869. status2 = hdsp_read(hdsp, HDSP_status2Register);
  2870. snd_iprintf(buffer, "%s (Card #%d)\n", hdsp->card_name,
  2871. hdsp->card->number + 1);
  2872. snd_iprintf(buffer, "Buffers: capture %p playback %p\n",
  2873. hdsp->capture_buffer, hdsp->playback_buffer);
  2874. snd_iprintf(buffer, "IRQ: %d Registers bus: 0x%lx VM: 0x%lx\n",
  2875. hdsp->irq, hdsp->port, (unsigned long)hdsp->iobase);
  2876. snd_iprintf(buffer, "Control register: 0x%x\n", hdsp->control_register);
  2877. snd_iprintf(buffer, "Control2 register: 0x%x\n",
  2878. hdsp->control2_register);
  2879. snd_iprintf(buffer, "Status register: 0x%x\n", status);
  2880. snd_iprintf(buffer, "Status2 register: 0x%x\n", status2);
  2881. if (hdsp_check_for_iobox(hdsp)) {
  2882. snd_iprintf(buffer, "No I/O box connected.\n"
  2883. "Please connect one and upload firmware.\n");
  2884. return;
  2885. }
  2886. if (hdsp_check_for_firmware(hdsp, 0)) {
  2887. if (hdsp->state & HDSP_FirmwareCached) {
  2888. if (snd_hdsp_load_firmware_from_cache(hdsp) != 0) {
  2889. snd_iprintf(buffer, "Firmware loading from "
  2890. "cache failed, "
  2891. "please upload manually.\n");
  2892. return;
  2893. }
  2894. } else {
  2895. int err = -EINVAL;
  2896. err = hdsp_request_fw_loader(hdsp);
  2897. if (err < 0) {
  2898. snd_iprintf(buffer,
  2899. "No firmware loaded nor cached, "
  2900. "please upload firmware.\n");
  2901. return;
  2902. }
  2903. }
  2904. }
  2905. snd_iprintf(buffer, "FIFO status: %d\n", hdsp_read(hdsp, HDSP_fifoStatus) & 0xff);
  2906. snd_iprintf(buffer, "MIDI1 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut0));
  2907. snd_iprintf(buffer, "MIDI1 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn0));
  2908. snd_iprintf(buffer, "MIDI2 Output status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusOut1));
  2909. snd_iprintf(buffer, "MIDI2 Input status: 0x%x\n", hdsp_read(hdsp, HDSP_midiStatusIn1));
  2910. snd_iprintf(buffer, "Use Midi Tasklet: %s\n", hdsp->use_midi_tasklet ? "on" : "off");
  2911. snd_iprintf(buffer, "\n");
  2912. x = 1 << (6 + hdsp_decode_latency(hdsp->control_register & HDSP_LatencyMask));
  2913. snd_iprintf(buffer, "Buffer Size (Latency): %d samples (2 periods of %lu bytes)\n", x, (unsigned long) hdsp->period_bytes);
  2914. snd_iprintf(buffer, "Hardware pointer (frames): %ld\n", hdsp_hw_pointer(hdsp));
  2915. snd_iprintf(buffer, "Precise pointer: %s\n", hdsp->precise_ptr ? "on" : "off");
  2916. snd_iprintf(buffer, "Line out: %s\n", (hdsp->control_register & HDSP_LineOut) ? "on" : "off");
  2917. snd_iprintf(buffer, "Firmware version: %d\n", (status2&HDSP_version0)|(status2&HDSP_version1)<<1|(status2&HDSP_version2)<<2);
  2918. snd_iprintf(buffer, "\n");
  2919. switch (hdsp_clock_source(hdsp)) {
  2920. case HDSP_CLOCK_SOURCE_AUTOSYNC:
  2921. clock_source = "AutoSync";
  2922. break;
  2923. case HDSP_CLOCK_SOURCE_INTERNAL_32KHZ:
  2924. clock_source = "Internal 32 kHz";
  2925. break;
  2926. case HDSP_CLOCK_SOURCE_INTERNAL_44_1KHZ:
  2927. clock_source = "Internal 44.1 kHz";
  2928. break;
  2929. case HDSP_CLOCK_SOURCE_INTERNAL_48KHZ:
  2930. clock_source = "Internal 48 kHz";
  2931. break;
  2932. case HDSP_CLOCK_SOURCE_INTERNAL_64KHZ:
  2933. clock_source = "Internal 64 kHz";
  2934. break;
  2935. case HDSP_CLOCK_SOURCE_INTERNAL_88_2KHZ:
  2936. clock_source = "Internal 88.2 kHz";
  2937. break;
  2938. case HDSP_CLOCK_SOURCE_INTERNAL_96KHZ:
  2939. clock_source = "Internal 96 kHz";
  2940. break;
  2941. case HDSP_CLOCK_SOURCE_INTERNAL_128KHZ:
  2942. clock_source = "Internal 128 kHz";
  2943. break;
  2944. case HDSP_CLOCK_SOURCE_INTERNAL_176_4KHZ:
  2945. clock_source = "Internal 176.4 kHz";
  2946. break;
  2947. case HDSP_CLOCK_SOURCE_INTERNAL_192KHZ:
  2948. clock_source = "Internal 192 kHz";
  2949. break;
  2950. default:
  2951. clock_source = "Error";
  2952. }
  2953. snd_iprintf (buffer, "Sample Clock Source: %s\n", clock_source);
  2954. if (hdsp_system_clock_mode(hdsp))
  2955. system_clock_mode = "Slave";
  2956. else
  2957. system_clock_mode = "Master";
  2958. switch (hdsp_pref_sync_ref (hdsp)) {
  2959. case HDSP_SYNC_FROM_WORD:
  2960. pref_sync_ref = "Word Clock";
  2961. break;
  2962. case HDSP_SYNC_FROM_ADAT_SYNC:
  2963. pref_sync_ref = "ADAT Sync";
  2964. break;
  2965. case HDSP_SYNC_FROM_SPDIF:
  2966. pref_sync_ref = "SPDIF";
  2967. break;
  2968. case HDSP_SYNC_FROM_ADAT1:
  2969. pref_sync_ref = "ADAT1";
  2970. break;
  2971. case HDSP_SYNC_FROM_ADAT2:
  2972. pref_sync_ref = "ADAT2";
  2973. break;
  2974. case HDSP_SYNC_FROM_ADAT3:
  2975. pref_sync_ref = "ADAT3";
  2976. break;
  2977. default:
  2978. pref_sync_ref = "Word Clock";
  2979. break;
  2980. }
  2981. snd_iprintf (buffer, "Preferred Sync Reference: %s\n", pref_sync_ref);
  2982. switch (hdsp_autosync_ref (hdsp)) {
  2983. case HDSP_AUTOSYNC_FROM_WORD:
  2984. autosync_ref = "Word Clock";
  2985. break;
  2986. case HDSP_AUTOSYNC_FROM_ADAT_SYNC:
  2987. autosync_ref = "ADAT Sync";
  2988. break;
  2989. case HDSP_AUTOSYNC_FROM_SPDIF:
  2990. autosync_ref = "SPDIF";
  2991. break;
  2992. case HDSP_AUTOSYNC_FROM_NONE:
  2993. autosync_ref = "None";
  2994. break;
  2995. case HDSP_AUTOSYNC_FROM_ADAT1:
  2996. autosync_ref = "ADAT1";
  2997. break;
  2998. case HDSP_AUTOSYNC_FROM_ADAT2:
  2999. autosync_ref = "ADAT2";
  3000. break;
  3001. case HDSP_AUTOSYNC_FROM_ADAT3:
  3002. autosync_ref = "ADAT3";
  3003. break;
  3004. default:
  3005. autosync_ref = "---";
  3006. break;
  3007. }
  3008. snd_iprintf (buffer, "AutoSync Reference: %s\n", autosync_ref);
  3009. snd_iprintf (buffer, "AutoSync Frequency: %d\n", hdsp_external_sample_rate(hdsp));
  3010. snd_iprintf (buffer, "System Clock Mode: %s\n", system_clock_mode);
  3011. snd_iprintf (buffer, "System Clock Frequency: %d\n", hdsp->system_sample_rate);
  3012. snd_iprintf (buffer, "System Clock Locked: %s\n", hdsp->clock_source_locked ? "Yes" : "No");
  3013. snd_iprintf(buffer, "\n");
  3014. if (hdsp->io_type != RPM) {
  3015. switch (hdsp_spdif_in(hdsp)) {
  3016. case HDSP_SPDIFIN_OPTICAL:
  3017. snd_iprintf(buffer, "IEC958 input: Optical\n");
  3018. break;
  3019. case HDSP_SPDIFIN_COAXIAL:
  3020. snd_iprintf(buffer, "IEC958 input: Coaxial\n");
  3021. break;
  3022. case HDSP_SPDIFIN_INTERNAL:
  3023. snd_iprintf(buffer, "IEC958 input: Internal\n");
  3024. break;
  3025. case HDSP_SPDIFIN_AES:
  3026. snd_iprintf(buffer, "IEC958 input: AES\n");
  3027. break;
  3028. default:
  3029. snd_iprintf(buffer, "IEC958 input: ???\n");
  3030. break;
  3031. }
  3032. }
  3033. if (RPM == hdsp->io_type) {
  3034. if (hdsp->control_register & HDSP_RPM_Bypass)
  3035. snd_iprintf(buffer, "RPM Bypass: disabled\n");
  3036. else
  3037. snd_iprintf(buffer, "RPM Bypass: enabled\n");
  3038. if (hdsp->control_register & HDSP_RPM_Disconnect)
  3039. snd_iprintf(buffer, "RPM disconnected\n");
  3040. else
  3041. snd_iprintf(buffer, "RPM connected\n");
  3042. switch (hdsp->control_register & HDSP_RPM_Inp12) {
  3043. case HDSP_RPM_Inp12_Phon_6dB:
  3044. snd_iprintf(buffer, "Input 1/2: Phono, 6dB\n");
  3045. break;
  3046. case HDSP_RPM_Inp12_Phon_0dB:
  3047. snd_iprintf(buffer, "Input 1/2: Phono, 0dB\n");
  3048. break;
  3049. case HDSP_RPM_Inp12_Phon_n6dB:
  3050. snd_iprintf(buffer, "Input 1/2: Phono, -6dB\n");
  3051. break;
  3052. case HDSP_RPM_Inp12_Line_0dB:
  3053. snd_iprintf(buffer, "Input 1/2: Line, 0dB\n");
  3054. break;
  3055. case HDSP_RPM_Inp12_Line_n6dB:
  3056. snd_iprintf(buffer, "Input 1/2: Line, -6dB\n");
  3057. break;
  3058. default:
  3059. snd_iprintf(buffer, "Input 1/2: ???\n");
  3060. }
  3061. switch (hdsp->control_register & HDSP_RPM_Inp34) {
  3062. case HDSP_RPM_Inp34_Phon_6dB:
  3063. snd_iprintf(buffer, "Input 3/4: Phono, 6dB\n");
  3064. break;
  3065. case HDSP_RPM_Inp34_Phon_0dB:
  3066. snd_iprintf(buffer, "Input 3/4: Phono, 0dB\n");
  3067. break;
  3068. case HDSP_RPM_Inp34_Phon_n6dB:
  3069. snd_iprintf(buffer, "Input 3/4: Phono, -6dB\n");
  3070. break;
  3071. case HDSP_RPM_Inp34_Line_0dB:
  3072. snd_iprintf(buffer, "Input 3/4: Line, 0dB\n");
  3073. break;
  3074. case HDSP_RPM_Inp34_Line_n6dB:
  3075. snd_iprintf(buffer, "Input 3/4: Line, -6dB\n");
  3076. break;
  3077. default:
  3078. snd_iprintf(buffer, "Input 3/4: ???\n");
  3079. }
  3080. } else {
  3081. if (hdsp->control_register & HDSP_SPDIFOpticalOut)
  3082. snd_iprintf(buffer, "IEC958 output: Coaxial & ADAT1\n");
  3083. else
  3084. snd_iprintf(buffer, "IEC958 output: Coaxial only\n");
  3085. if (hdsp->control_register & HDSP_SPDIFProfessional)
  3086. snd_iprintf(buffer, "IEC958 quality: Professional\n");
  3087. else
  3088. snd_iprintf(buffer, "IEC958 quality: Consumer\n");
  3089. if (hdsp->control_register & HDSP_SPDIFEmphasis)
  3090. snd_iprintf(buffer, "IEC958 emphasis: on\n");
  3091. else
  3092. snd_iprintf(buffer, "IEC958 emphasis: off\n");
  3093. if (hdsp->control_register & HDSP_SPDIFNonAudio)
  3094. snd_iprintf(buffer, "IEC958 NonAudio: on\n");
  3095. else
  3096. snd_iprintf(buffer, "IEC958 NonAudio: off\n");
  3097. x = hdsp_spdif_sample_rate(hdsp);
  3098. if (x != 0)
  3099. snd_iprintf(buffer, "IEC958 sample rate: %d\n", x);
  3100. else
  3101. snd_iprintf(buffer, "IEC958 sample rate: Error flag set\n");
  3102. }
  3103. snd_iprintf(buffer, "\n");
  3104. /* Sync Check */
  3105. x = status & HDSP_Sync0;
  3106. if (status & HDSP_Lock0)
  3107. snd_iprintf(buffer, "ADAT1: %s\n", x ? "Sync" : "Lock");
  3108. else
  3109. snd_iprintf(buffer, "ADAT1: No Lock\n");
  3110. switch (hdsp->io_type) {
  3111. case Digiface:
  3112. case H9652:
  3113. x = status & HDSP_Sync1;
  3114. if (status & HDSP_Lock1)
  3115. snd_iprintf(buffer, "ADAT2: %s\n", x ? "Sync" : "Lock");
  3116. else
  3117. snd_iprintf(buffer, "ADAT2: No Lock\n");
  3118. x = status & HDSP_Sync2;
  3119. if (status & HDSP_Lock2)
  3120. snd_iprintf(buffer, "ADAT3: %s\n", x ? "Sync" : "Lock");
  3121. else
  3122. snd_iprintf(buffer, "ADAT3: No Lock\n");
  3123. break;
  3124. default:
  3125. /* relax */
  3126. break;
  3127. }
  3128. x = status & HDSP_SPDIFSync;
  3129. if (status & HDSP_SPDIFErrorFlag)
  3130. snd_iprintf (buffer, "SPDIF: No Lock\n");
  3131. else
  3132. snd_iprintf (buffer, "SPDIF: %s\n", x ? "Sync" : "Lock");
  3133. x = status2 & HDSP_wc_sync;
  3134. if (status2 & HDSP_wc_lock)
  3135. snd_iprintf (buffer, "Word Clock: %s\n", x ? "Sync" : "Lock");
  3136. else
  3137. snd_iprintf (buffer, "Word Clock: No Lock\n");
  3138. x = status & HDSP_TimecodeSync;
  3139. if (status & HDSP_TimecodeLock)
  3140. snd_iprintf(buffer, "ADAT Sync: %s\n", x ? "Sync" : "Lock");
  3141. else
  3142. snd_iprintf(buffer, "ADAT Sync: No Lock\n");
  3143. snd_iprintf(buffer, "\n");
  3144. /* Informations about H9632 specific controls */
  3145. if (hdsp->io_type == H9632) {
  3146. char *tmp;
  3147. switch (hdsp_ad_gain(hdsp)) {
  3148. case 0:
  3149. tmp = "-10 dBV";
  3150. break;
  3151. case 1:
  3152. tmp = "+4 dBu";
  3153. break;
  3154. default:
  3155. tmp = "Lo Gain";
  3156. break;
  3157. }
  3158. snd_iprintf(buffer, "AD Gain : %s\n", tmp);
  3159. switch (hdsp_da_gain(hdsp)) {
  3160. case 0:
  3161. tmp = "Hi Gain";
  3162. break;
  3163. case 1:
  3164. tmp = "+4 dBu";
  3165. break;
  3166. default:
  3167. tmp = "-10 dBV";
  3168. break;
  3169. }
  3170. snd_iprintf(buffer, "DA Gain : %s\n", tmp);
  3171. switch (hdsp_phone_gain(hdsp)) {
  3172. case 0:
  3173. tmp = "0 dB";
  3174. break;
  3175. case 1:
  3176. tmp = "-6 dB";
  3177. break;
  3178. default:
  3179. tmp = "-12 dB";
  3180. break;
  3181. }
  3182. snd_iprintf(buffer, "Phones Gain : %s\n", tmp);
  3183. snd_iprintf(buffer, "XLR Breakout Cable : %s\n",
  3184. hdsp_toggle_setting(hdsp, HDSP_XLRBreakoutCable) ?
  3185. "yes" : "no");
  3186. if (hdsp->control_register & HDSP_AnalogExtensionBoard)
  3187. snd_iprintf(buffer, "AEB : on (ADAT1 internal)\n");
  3188. else
  3189. snd_iprintf(buffer, "AEB : off (ADAT1 external)\n");
  3190. snd_iprintf(buffer, "\n");
  3191. }
  3192. }
  3193. static void snd_hdsp_proc_init(struct hdsp *hdsp)
  3194. {
  3195. struct snd_info_entry *entry;
  3196. if (! snd_card_proc_new(hdsp->card, "hdsp", &entry))
  3197. snd_info_set_text_ops(entry, hdsp, snd_hdsp_proc_read);
  3198. }
  3199. static void snd_hdsp_free_buffers(struct hdsp *hdsp)
  3200. {
  3201. snd_hammerfall_free_buffer(&hdsp->capture_dma_buf, hdsp->pci);
  3202. snd_hammerfall_free_buffer(&hdsp->playback_dma_buf, hdsp->pci);
  3203. }
  3204. static int snd_hdsp_initialize_memory(struct hdsp *hdsp)
  3205. {
  3206. unsigned long pb_bus, cb_bus;
  3207. if (snd_hammerfall_get_buffer(hdsp->pci, &hdsp->capture_dma_buf, HDSP_DMA_AREA_BYTES) < 0 ||
  3208. snd_hammerfall_get_buffer(hdsp->pci, &hdsp->playback_dma_buf, HDSP_DMA_AREA_BYTES) < 0) {
  3209. if (hdsp->capture_dma_buf.area)
  3210. snd_dma_free_pages(&hdsp->capture_dma_buf);
  3211. dev_err(hdsp->card->dev,
  3212. "%s: no buffers available\n", hdsp->card_name);
  3213. return -ENOMEM;
  3214. }
  3215. /* Align to bus-space 64K boundary */
  3216. cb_bus = ALIGN(hdsp->capture_dma_buf.addr, 0x10000ul);
  3217. pb_bus = ALIGN(hdsp->playback_dma_buf.addr, 0x10000ul);
  3218. /* Tell the card where it is */
  3219. hdsp_write(hdsp, HDSP_inputBufferAddress, cb_bus);
  3220. hdsp_write(hdsp, HDSP_outputBufferAddress, pb_bus);
  3221. hdsp->capture_buffer = hdsp->capture_dma_buf.area + (cb_bus - hdsp->capture_dma_buf.addr);
  3222. hdsp->playback_buffer = hdsp->playback_dma_buf.area + (pb_bus - hdsp->playback_dma_buf.addr);
  3223. return 0;
  3224. }
  3225. static int snd_hdsp_set_defaults(struct hdsp *hdsp)
  3226. {
  3227. unsigned int i;
  3228. /* ASSUMPTION: hdsp->lock is either held, or
  3229. there is no need to hold it (e.g. during module
  3230. initialization).
  3231. */
  3232. /* set defaults:
  3233. SPDIF Input via Coax
  3234. Master clock mode
  3235. maximum latency (7 => 2^7 = 8192 samples, 64Kbyte buffer,
  3236. which implies 2 4096 sample, 32Kbyte periods).
  3237. Enable line out.
  3238. */
  3239. hdsp->control_register = HDSP_ClockModeMaster |
  3240. HDSP_SPDIFInputCoaxial |
  3241. hdsp_encode_latency(7) |
  3242. HDSP_LineOut;
  3243. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3244. #ifdef SNDRV_BIG_ENDIAN
  3245. hdsp->control2_register = HDSP_BIGENDIAN_MODE;
  3246. #else
  3247. hdsp->control2_register = 0;
  3248. #endif
  3249. if (hdsp->io_type == H9652)
  3250. snd_hdsp_9652_enable_mixer (hdsp);
  3251. else
  3252. hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
  3253. hdsp_reset_hw_pointer(hdsp);
  3254. hdsp_compute_period_size(hdsp);
  3255. /* silence everything */
  3256. for (i = 0; i < HDSP_MATRIX_MIXER_SIZE; ++i)
  3257. hdsp->mixer_matrix[i] = MINUS_INFINITY_GAIN;
  3258. for (i = 0; i < ((hdsp->io_type == H9652 || hdsp->io_type == H9632) ? 1352 : HDSP_MATRIX_MIXER_SIZE); ++i) {
  3259. if (hdsp_write_gain (hdsp, i, MINUS_INFINITY_GAIN))
  3260. return -EIO;
  3261. }
  3262. /* H9632 specific defaults */
  3263. if (hdsp->io_type == H9632) {
  3264. hdsp->control_register |= (HDSP_DAGainPlus4dBu | HDSP_ADGainPlus4dBu | HDSP_PhoneGain0dB);
  3265. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3266. }
  3267. /* set a default rate so that the channel map is set up.
  3268. */
  3269. hdsp_set_rate(hdsp, 48000, 1);
  3270. return 0;
  3271. }
  3272. static void hdsp_midi_tasklet(unsigned long arg)
  3273. {
  3274. struct hdsp *hdsp = (struct hdsp *)arg;
  3275. if (hdsp->midi[0].pending)
  3276. snd_hdsp_midi_input_read (&hdsp->midi[0]);
  3277. if (hdsp->midi[1].pending)
  3278. snd_hdsp_midi_input_read (&hdsp->midi[1]);
  3279. }
  3280. static irqreturn_t snd_hdsp_interrupt(int irq, void *dev_id)
  3281. {
  3282. struct hdsp *hdsp = (struct hdsp *) dev_id;
  3283. unsigned int status;
  3284. int audio;
  3285. int midi0;
  3286. int midi1;
  3287. unsigned int midi0status;
  3288. unsigned int midi1status;
  3289. int schedule = 0;
  3290. status = hdsp_read(hdsp, HDSP_statusRegister);
  3291. audio = status & HDSP_audioIRQPending;
  3292. midi0 = status & HDSP_midi0IRQPending;
  3293. midi1 = status & HDSP_midi1IRQPending;
  3294. if (!audio && !midi0 && !midi1)
  3295. return IRQ_NONE;
  3296. hdsp_write(hdsp, HDSP_interruptConfirmation, 0);
  3297. midi0status = hdsp_read (hdsp, HDSP_midiStatusIn0) & 0xff;
  3298. midi1status = hdsp_read (hdsp, HDSP_midiStatusIn1) & 0xff;
  3299. if (!(hdsp->state & HDSP_InitializationComplete))
  3300. return IRQ_HANDLED;
  3301. if (audio) {
  3302. if (hdsp->capture_substream)
  3303. snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream);
  3304. if (hdsp->playback_substream)
  3305. snd_pcm_period_elapsed(hdsp->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
  3306. }
  3307. if (midi0 && midi0status) {
  3308. if (hdsp->use_midi_tasklet) {
  3309. /* we disable interrupts for this input until processing is done */
  3310. hdsp->control_register &= ~HDSP_Midi0InterruptEnable;
  3311. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3312. hdsp->midi[0].pending = 1;
  3313. schedule = 1;
  3314. } else {
  3315. snd_hdsp_midi_input_read (&hdsp->midi[0]);
  3316. }
  3317. }
  3318. if (hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632 && midi1 && midi1status) {
  3319. if (hdsp->use_midi_tasklet) {
  3320. /* we disable interrupts for this input until processing is done */
  3321. hdsp->control_register &= ~HDSP_Midi1InterruptEnable;
  3322. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register);
  3323. hdsp->midi[1].pending = 1;
  3324. schedule = 1;
  3325. } else {
  3326. snd_hdsp_midi_input_read (&hdsp->midi[1]);
  3327. }
  3328. }
  3329. if (hdsp->use_midi_tasklet && schedule)
  3330. tasklet_schedule(&hdsp->midi_tasklet);
  3331. return IRQ_HANDLED;
  3332. }
  3333. static snd_pcm_uframes_t snd_hdsp_hw_pointer(struct snd_pcm_substream *substream)
  3334. {
  3335. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3336. return hdsp_hw_pointer(hdsp);
  3337. }
  3338. static char *hdsp_channel_buffer_location(struct hdsp *hdsp,
  3339. int stream,
  3340. int channel)
  3341. {
  3342. int mapped_channel;
  3343. if (snd_BUG_ON(channel < 0 || channel >= hdsp->max_channels))
  3344. return NULL;
  3345. if ((mapped_channel = hdsp->channel_map[channel]) < 0)
  3346. return NULL;
  3347. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  3348. return hdsp->capture_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
  3349. else
  3350. return hdsp->playback_buffer + (mapped_channel * HDSP_CHANNEL_BUFFER_BYTES);
  3351. }
  3352. static int snd_hdsp_playback_copy(struct snd_pcm_substream *substream, int channel,
  3353. snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
  3354. {
  3355. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3356. char *channel_buf;
  3357. if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
  3358. return -EINVAL;
  3359. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3360. if (snd_BUG_ON(!channel_buf))
  3361. return -EIO;
  3362. if (copy_from_user(channel_buf + pos * 4, src, count * 4))
  3363. return -EFAULT;
  3364. return count;
  3365. }
  3366. static int snd_hdsp_capture_copy(struct snd_pcm_substream *substream, int channel,
  3367. snd_pcm_uframes_t pos, void __user *dst, snd_pcm_uframes_t count)
  3368. {
  3369. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3370. char *channel_buf;
  3371. if (snd_BUG_ON(pos + count > HDSP_CHANNEL_BUFFER_BYTES / 4))
  3372. return -EINVAL;
  3373. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3374. if (snd_BUG_ON(!channel_buf))
  3375. return -EIO;
  3376. if (copy_to_user(dst, channel_buf + pos * 4, count * 4))
  3377. return -EFAULT;
  3378. return count;
  3379. }
  3380. static int snd_hdsp_hw_silence(struct snd_pcm_substream *substream, int channel,
  3381. snd_pcm_uframes_t pos, snd_pcm_uframes_t count)
  3382. {
  3383. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3384. char *channel_buf;
  3385. channel_buf = hdsp_channel_buffer_location (hdsp, substream->pstr->stream, channel);
  3386. if (snd_BUG_ON(!channel_buf))
  3387. return -EIO;
  3388. memset(channel_buf + pos * 4, 0, count * 4);
  3389. return count;
  3390. }
  3391. static int snd_hdsp_reset(struct snd_pcm_substream *substream)
  3392. {
  3393. struct snd_pcm_runtime *runtime = substream->runtime;
  3394. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3395. struct snd_pcm_substream *other;
  3396. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3397. other = hdsp->capture_substream;
  3398. else
  3399. other = hdsp->playback_substream;
  3400. if (hdsp->running)
  3401. runtime->status->hw_ptr = hdsp_hw_pointer(hdsp);
  3402. else
  3403. runtime->status->hw_ptr = 0;
  3404. if (other) {
  3405. struct snd_pcm_substream *s;
  3406. struct snd_pcm_runtime *oruntime = other->runtime;
  3407. snd_pcm_group_for_each_entry(s, substream) {
  3408. if (s == other) {
  3409. oruntime->status->hw_ptr = runtime->status->hw_ptr;
  3410. break;
  3411. }
  3412. }
  3413. }
  3414. return 0;
  3415. }
  3416. static int snd_hdsp_hw_params(struct snd_pcm_substream *substream,
  3417. struct snd_pcm_hw_params *params)
  3418. {
  3419. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3420. int err;
  3421. pid_t this_pid;
  3422. pid_t other_pid;
  3423. if (hdsp_check_for_iobox (hdsp))
  3424. return -EIO;
  3425. if (hdsp_check_for_firmware(hdsp, 1))
  3426. return -EIO;
  3427. spin_lock_irq(&hdsp->lock);
  3428. if (substream->pstr->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  3429. hdsp->control_register &= ~(HDSP_SPDIFProfessional | HDSP_SPDIFNonAudio | HDSP_SPDIFEmphasis);
  3430. hdsp_write(hdsp, HDSP_controlRegister, hdsp->control_register |= hdsp->creg_spdif_stream);
  3431. this_pid = hdsp->playback_pid;
  3432. other_pid = hdsp->capture_pid;
  3433. } else {
  3434. this_pid = hdsp->capture_pid;
  3435. other_pid = hdsp->playback_pid;
  3436. }
  3437. if ((other_pid > 0) && (this_pid != other_pid)) {
  3438. /* The other stream is open, and not by the same
  3439. task as this one. Make sure that the parameters
  3440. that matter are the same.
  3441. */
  3442. if (params_rate(params) != hdsp->system_sample_rate) {
  3443. spin_unlock_irq(&hdsp->lock);
  3444. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
  3445. return -EBUSY;
  3446. }
  3447. if (params_period_size(params) != hdsp->period_bytes / 4) {
  3448. spin_unlock_irq(&hdsp->lock);
  3449. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  3450. return -EBUSY;
  3451. }
  3452. /* We're fine. */
  3453. spin_unlock_irq(&hdsp->lock);
  3454. return 0;
  3455. } else {
  3456. spin_unlock_irq(&hdsp->lock);
  3457. }
  3458. /* how to make sure that the rate matches an externally-set one ?
  3459. */
  3460. spin_lock_irq(&hdsp->lock);
  3461. if (! hdsp->clock_source_locked) {
  3462. if ((err = hdsp_set_rate(hdsp, params_rate(params), 0)) < 0) {
  3463. spin_unlock_irq(&hdsp->lock);
  3464. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_RATE);
  3465. return err;
  3466. }
  3467. }
  3468. spin_unlock_irq(&hdsp->lock);
  3469. if ((err = hdsp_set_interrupt_interval(hdsp, params_period_size(params))) < 0) {
  3470. _snd_pcm_hw_param_setempty(params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE);
  3471. return err;
  3472. }
  3473. return 0;
  3474. }
  3475. static int snd_hdsp_channel_info(struct snd_pcm_substream *substream,
  3476. struct snd_pcm_channel_info *info)
  3477. {
  3478. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3479. int mapped_channel;
  3480. if (snd_BUG_ON(info->channel >= hdsp->max_channels))
  3481. return -EINVAL;
  3482. if ((mapped_channel = hdsp->channel_map[info->channel]) < 0)
  3483. return -EINVAL;
  3484. info->offset = mapped_channel * HDSP_CHANNEL_BUFFER_BYTES;
  3485. info->first = 0;
  3486. info->step = 32;
  3487. return 0;
  3488. }
  3489. static int snd_hdsp_ioctl(struct snd_pcm_substream *substream,
  3490. unsigned int cmd, void *arg)
  3491. {
  3492. switch (cmd) {
  3493. case SNDRV_PCM_IOCTL1_RESET:
  3494. return snd_hdsp_reset(substream);
  3495. case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
  3496. return snd_hdsp_channel_info(substream, arg);
  3497. default:
  3498. break;
  3499. }
  3500. return snd_pcm_lib_ioctl(substream, cmd, arg);
  3501. }
  3502. static int snd_hdsp_trigger(struct snd_pcm_substream *substream, int cmd)
  3503. {
  3504. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3505. struct snd_pcm_substream *other;
  3506. int running;
  3507. if (hdsp_check_for_iobox (hdsp))
  3508. return -EIO;
  3509. if (hdsp_check_for_firmware(hdsp, 0)) /* no auto-loading in trigger */
  3510. return -EIO;
  3511. spin_lock(&hdsp->lock);
  3512. running = hdsp->running;
  3513. switch (cmd) {
  3514. case SNDRV_PCM_TRIGGER_START:
  3515. running |= 1 << substream->stream;
  3516. break;
  3517. case SNDRV_PCM_TRIGGER_STOP:
  3518. running &= ~(1 << substream->stream);
  3519. break;
  3520. default:
  3521. snd_BUG();
  3522. spin_unlock(&hdsp->lock);
  3523. return -EINVAL;
  3524. }
  3525. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3526. other = hdsp->capture_substream;
  3527. else
  3528. other = hdsp->playback_substream;
  3529. if (other) {
  3530. struct snd_pcm_substream *s;
  3531. snd_pcm_group_for_each_entry(s, substream) {
  3532. if (s == other) {
  3533. snd_pcm_trigger_done(s, substream);
  3534. if (cmd == SNDRV_PCM_TRIGGER_START)
  3535. running |= 1 << s->stream;
  3536. else
  3537. running &= ~(1 << s->stream);
  3538. goto _ok;
  3539. }
  3540. }
  3541. if (cmd == SNDRV_PCM_TRIGGER_START) {
  3542. if (!(running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) &&
  3543. substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  3544. hdsp_silence_playback(hdsp);
  3545. } else {
  3546. if (running &&
  3547. substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  3548. hdsp_silence_playback(hdsp);
  3549. }
  3550. } else {
  3551. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  3552. hdsp_silence_playback(hdsp);
  3553. }
  3554. _ok:
  3555. snd_pcm_trigger_done(substream, substream);
  3556. if (!hdsp->running && running)
  3557. hdsp_start_audio(hdsp);
  3558. else if (hdsp->running && !running)
  3559. hdsp_stop_audio(hdsp);
  3560. hdsp->running = running;
  3561. spin_unlock(&hdsp->lock);
  3562. return 0;
  3563. }
  3564. static int snd_hdsp_prepare(struct snd_pcm_substream *substream)
  3565. {
  3566. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3567. int result = 0;
  3568. if (hdsp_check_for_iobox (hdsp))
  3569. return -EIO;
  3570. if (hdsp_check_for_firmware(hdsp, 1))
  3571. return -EIO;
  3572. spin_lock_irq(&hdsp->lock);
  3573. if (!hdsp->running)
  3574. hdsp_reset_hw_pointer(hdsp);
  3575. spin_unlock_irq(&hdsp->lock);
  3576. return result;
  3577. }
  3578. static struct snd_pcm_hardware snd_hdsp_playback_subinfo =
  3579. {
  3580. .info = (SNDRV_PCM_INFO_MMAP |
  3581. SNDRV_PCM_INFO_MMAP_VALID |
  3582. SNDRV_PCM_INFO_NONINTERLEAVED |
  3583. SNDRV_PCM_INFO_SYNC_START |
  3584. SNDRV_PCM_INFO_DOUBLE),
  3585. #ifdef SNDRV_BIG_ENDIAN
  3586. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  3587. #else
  3588. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  3589. #endif
  3590. .rates = (SNDRV_PCM_RATE_32000 |
  3591. SNDRV_PCM_RATE_44100 |
  3592. SNDRV_PCM_RATE_48000 |
  3593. SNDRV_PCM_RATE_64000 |
  3594. SNDRV_PCM_RATE_88200 |
  3595. SNDRV_PCM_RATE_96000),
  3596. .rate_min = 32000,
  3597. .rate_max = 96000,
  3598. .channels_min = 6,
  3599. .channels_max = HDSP_MAX_CHANNELS,
  3600. .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
  3601. .period_bytes_min = (64 * 4) * 10,
  3602. .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
  3603. .periods_min = 2,
  3604. .periods_max = 2,
  3605. .fifo_size = 0
  3606. };
  3607. static struct snd_pcm_hardware snd_hdsp_capture_subinfo =
  3608. {
  3609. .info = (SNDRV_PCM_INFO_MMAP |
  3610. SNDRV_PCM_INFO_MMAP_VALID |
  3611. SNDRV_PCM_INFO_NONINTERLEAVED |
  3612. SNDRV_PCM_INFO_SYNC_START),
  3613. #ifdef SNDRV_BIG_ENDIAN
  3614. .formats = SNDRV_PCM_FMTBIT_S32_BE,
  3615. #else
  3616. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  3617. #endif
  3618. .rates = (SNDRV_PCM_RATE_32000 |
  3619. SNDRV_PCM_RATE_44100 |
  3620. SNDRV_PCM_RATE_48000 |
  3621. SNDRV_PCM_RATE_64000 |
  3622. SNDRV_PCM_RATE_88200 |
  3623. SNDRV_PCM_RATE_96000),
  3624. .rate_min = 32000,
  3625. .rate_max = 96000,
  3626. .channels_min = 5,
  3627. .channels_max = HDSP_MAX_CHANNELS,
  3628. .buffer_bytes_max = HDSP_CHANNEL_BUFFER_BYTES * HDSP_MAX_CHANNELS,
  3629. .period_bytes_min = (64 * 4) * 10,
  3630. .period_bytes_max = (8192 * 4) * HDSP_MAX_CHANNELS,
  3631. .periods_min = 2,
  3632. .periods_max = 2,
  3633. .fifo_size = 0
  3634. };
  3635. static unsigned int hdsp_period_sizes[] = { 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
  3636. static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_period_sizes = {
  3637. .count = ARRAY_SIZE(hdsp_period_sizes),
  3638. .list = hdsp_period_sizes,
  3639. .mask = 0
  3640. };
  3641. static unsigned int hdsp_9632_sample_rates[] = { 32000, 44100, 48000, 64000, 88200, 96000, 128000, 176400, 192000 };
  3642. static struct snd_pcm_hw_constraint_list hdsp_hw_constraints_9632_sample_rates = {
  3643. .count = ARRAY_SIZE(hdsp_9632_sample_rates),
  3644. .list = hdsp_9632_sample_rates,
  3645. .mask = 0
  3646. };
  3647. static int snd_hdsp_hw_rule_in_channels(struct snd_pcm_hw_params *params,
  3648. struct snd_pcm_hw_rule *rule)
  3649. {
  3650. struct hdsp *hdsp = rule->private;
  3651. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3652. if (hdsp->io_type == H9632) {
  3653. unsigned int list[3];
  3654. list[0] = hdsp->qs_in_channels;
  3655. list[1] = hdsp->ds_in_channels;
  3656. list[2] = hdsp->ss_in_channels;
  3657. return snd_interval_list(c, 3, list, 0);
  3658. } else {
  3659. unsigned int list[2];
  3660. list[0] = hdsp->ds_in_channels;
  3661. list[1] = hdsp->ss_in_channels;
  3662. return snd_interval_list(c, 2, list, 0);
  3663. }
  3664. }
  3665. static int snd_hdsp_hw_rule_out_channels(struct snd_pcm_hw_params *params,
  3666. struct snd_pcm_hw_rule *rule)
  3667. {
  3668. unsigned int list[3];
  3669. struct hdsp *hdsp = rule->private;
  3670. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3671. if (hdsp->io_type == H9632) {
  3672. list[0] = hdsp->qs_out_channels;
  3673. list[1] = hdsp->ds_out_channels;
  3674. list[2] = hdsp->ss_out_channels;
  3675. return snd_interval_list(c, 3, list, 0);
  3676. } else {
  3677. list[0] = hdsp->ds_out_channels;
  3678. list[1] = hdsp->ss_out_channels;
  3679. }
  3680. return snd_interval_list(c, 2, list, 0);
  3681. }
  3682. static int snd_hdsp_hw_rule_in_channels_rate(struct snd_pcm_hw_params *params,
  3683. struct snd_pcm_hw_rule *rule)
  3684. {
  3685. struct hdsp *hdsp = rule->private;
  3686. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3687. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3688. if (r->min > 96000 && hdsp->io_type == H9632) {
  3689. struct snd_interval t = {
  3690. .min = hdsp->qs_in_channels,
  3691. .max = hdsp->qs_in_channels,
  3692. .integer = 1,
  3693. };
  3694. return snd_interval_refine(c, &t);
  3695. } else if (r->min > 48000 && r->max <= 96000) {
  3696. struct snd_interval t = {
  3697. .min = hdsp->ds_in_channels,
  3698. .max = hdsp->ds_in_channels,
  3699. .integer = 1,
  3700. };
  3701. return snd_interval_refine(c, &t);
  3702. } else if (r->max < 64000) {
  3703. struct snd_interval t = {
  3704. .min = hdsp->ss_in_channels,
  3705. .max = hdsp->ss_in_channels,
  3706. .integer = 1,
  3707. };
  3708. return snd_interval_refine(c, &t);
  3709. }
  3710. return 0;
  3711. }
  3712. static int snd_hdsp_hw_rule_out_channels_rate(struct snd_pcm_hw_params *params,
  3713. struct snd_pcm_hw_rule *rule)
  3714. {
  3715. struct hdsp *hdsp = rule->private;
  3716. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3717. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3718. if (r->min > 96000 && hdsp->io_type == H9632) {
  3719. struct snd_interval t = {
  3720. .min = hdsp->qs_out_channels,
  3721. .max = hdsp->qs_out_channels,
  3722. .integer = 1,
  3723. };
  3724. return snd_interval_refine(c, &t);
  3725. } else if (r->min > 48000 && r->max <= 96000) {
  3726. struct snd_interval t = {
  3727. .min = hdsp->ds_out_channels,
  3728. .max = hdsp->ds_out_channels,
  3729. .integer = 1,
  3730. };
  3731. return snd_interval_refine(c, &t);
  3732. } else if (r->max < 64000) {
  3733. struct snd_interval t = {
  3734. .min = hdsp->ss_out_channels,
  3735. .max = hdsp->ss_out_channels,
  3736. .integer = 1,
  3737. };
  3738. return snd_interval_refine(c, &t);
  3739. }
  3740. return 0;
  3741. }
  3742. static int snd_hdsp_hw_rule_rate_out_channels(struct snd_pcm_hw_params *params,
  3743. struct snd_pcm_hw_rule *rule)
  3744. {
  3745. struct hdsp *hdsp = rule->private;
  3746. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3747. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3748. if (c->min >= hdsp->ss_out_channels) {
  3749. struct snd_interval t = {
  3750. .min = 32000,
  3751. .max = 48000,
  3752. .integer = 1,
  3753. };
  3754. return snd_interval_refine(r, &t);
  3755. } else if (c->max <= hdsp->qs_out_channels && hdsp->io_type == H9632) {
  3756. struct snd_interval t = {
  3757. .min = 128000,
  3758. .max = 192000,
  3759. .integer = 1,
  3760. };
  3761. return snd_interval_refine(r, &t);
  3762. } else if (c->max <= hdsp->ds_out_channels) {
  3763. struct snd_interval t = {
  3764. .min = 64000,
  3765. .max = 96000,
  3766. .integer = 1,
  3767. };
  3768. return snd_interval_refine(r, &t);
  3769. }
  3770. return 0;
  3771. }
  3772. static int snd_hdsp_hw_rule_rate_in_channels(struct snd_pcm_hw_params *params,
  3773. struct snd_pcm_hw_rule *rule)
  3774. {
  3775. struct hdsp *hdsp = rule->private;
  3776. struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  3777. struct snd_interval *r = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  3778. if (c->min >= hdsp->ss_in_channels) {
  3779. struct snd_interval t = {
  3780. .min = 32000,
  3781. .max = 48000,
  3782. .integer = 1,
  3783. };
  3784. return snd_interval_refine(r, &t);
  3785. } else if (c->max <= hdsp->qs_in_channels && hdsp->io_type == H9632) {
  3786. struct snd_interval t = {
  3787. .min = 128000,
  3788. .max = 192000,
  3789. .integer = 1,
  3790. };
  3791. return snd_interval_refine(r, &t);
  3792. } else if (c->max <= hdsp->ds_in_channels) {
  3793. struct snd_interval t = {
  3794. .min = 64000,
  3795. .max = 96000,
  3796. .integer = 1,
  3797. };
  3798. return snd_interval_refine(r, &t);
  3799. }
  3800. return 0;
  3801. }
  3802. static int snd_hdsp_playback_open(struct snd_pcm_substream *substream)
  3803. {
  3804. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3805. struct snd_pcm_runtime *runtime = substream->runtime;
  3806. if (hdsp_check_for_iobox (hdsp))
  3807. return -EIO;
  3808. if (hdsp_check_for_firmware(hdsp, 1))
  3809. return -EIO;
  3810. spin_lock_irq(&hdsp->lock);
  3811. snd_pcm_set_sync(substream);
  3812. runtime->hw = snd_hdsp_playback_subinfo;
  3813. runtime->dma_area = hdsp->playback_buffer;
  3814. runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
  3815. hdsp->playback_pid = current->pid;
  3816. hdsp->playback_substream = substream;
  3817. spin_unlock_irq(&hdsp->lock);
  3818. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  3819. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
  3820. if (hdsp->clock_source_locked) {
  3821. runtime->hw.rate_min = runtime->hw.rate_max = hdsp->system_sample_rate;
  3822. } else if (hdsp->io_type == H9632) {
  3823. runtime->hw.rate_max = 192000;
  3824. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  3825. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
  3826. }
  3827. if (hdsp->io_type == H9632) {
  3828. runtime->hw.channels_min = hdsp->qs_out_channels;
  3829. runtime->hw.channels_max = hdsp->ss_out_channels;
  3830. }
  3831. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3832. snd_hdsp_hw_rule_out_channels, hdsp,
  3833. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3834. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3835. snd_hdsp_hw_rule_out_channels_rate, hdsp,
  3836. SNDRV_PCM_HW_PARAM_RATE, -1);
  3837. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  3838. snd_hdsp_hw_rule_rate_out_channels, hdsp,
  3839. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3840. if (RPM != hdsp->io_type) {
  3841. hdsp->creg_spdif_stream = hdsp->creg_spdif;
  3842. hdsp->spdif_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  3843. snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
  3844. SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
  3845. }
  3846. return 0;
  3847. }
  3848. static int snd_hdsp_playback_release(struct snd_pcm_substream *substream)
  3849. {
  3850. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3851. spin_lock_irq(&hdsp->lock);
  3852. hdsp->playback_pid = -1;
  3853. hdsp->playback_substream = NULL;
  3854. spin_unlock_irq(&hdsp->lock);
  3855. if (RPM != hdsp->io_type) {
  3856. hdsp->spdif_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  3857. snd_ctl_notify(hdsp->card, SNDRV_CTL_EVENT_MASK_VALUE |
  3858. SNDRV_CTL_EVENT_MASK_INFO, &hdsp->spdif_ctl->id);
  3859. }
  3860. return 0;
  3861. }
  3862. static int snd_hdsp_capture_open(struct snd_pcm_substream *substream)
  3863. {
  3864. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3865. struct snd_pcm_runtime *runtime = substream->runtime;
  3866. if (hdsp_check_for_iobox (hdsp))
  3867. return -EIO;
  3868. if (hdsp_check_for_firmware(hdsp, 1))
  3869. return -EIO;
  3870. spin_lock_irq(&hdsp->lock);
  3871. snd_pcm_set_sync(substream);
  3872. runtime->hw = snd_hdsp_capture_subinfo;
  3873. runtime->dma_area = hdsp->capture_buffer;
  3874. runtime->dma_bytes = HDSP_DMA_AREA_BYTES;
  3875. hdsp->capture_pid = current->pid;
  3876. hdsp->capture_substream = substream;
  3877. spin_unlock_irq(&hdsp->lock);
  3878. snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
  3879. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, &hdsp_hw_constraints_period_sizes);
  3880. if (hdsp->io_type == H9632) {
  3881. runtime->hw.channels_min = hdsp->qs_in_channels;
  3882. runtime->hw.channels_max = hdsp->ss_in_channels;
  3883. runtime->hw.rate_max = 192000;
  3884. runtime->hw.rates = SNDRV_PCM_RATE_KNOT;
  3885. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hdsp_hw_constraints_9632_sample_rates);
  3886. }
  3887. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3888. snd_hdsp_hw_rule_in_channels, hdsp,
  3889. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3890. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
  3891. snd_hdsp_hw_rule_in_channels_rate, hdsp,
  3892. SNDRV_PCM_HW_PARAM_RATE, -1);
  3893. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  3894. snd_hdsp_hw_rule_rate_in_channels, hdsp,
  3895. SNDRV_PCM_HW_PARAM_CHANNELS, -1);
  3896. return 0;
  3897. }
  3898. static int snd_hdsp_capture_release(struct snd_pcm_substream *substream)
  3899. {
  3900. struct hdsp *hdsp = snd_pcm_substream_chip(substream);
  3901. spin_lock_irq(&hdsp->lock);
  3902. hdsp->capture_pid = -1;
  3903. hdsp->capture_substream = NULL;
  3904. spin_unlock_irq(&hdsp->lock);
  3905. return 0;
  3906. }
  3907. /* helper functions for copying meter values */
  3908. static inline int copy_u32_le(void __user *dest, void __iomem *src)
  3909. {
  3910. u32 val = readl(src);
  3911. return copy_to_user(dest, &val, 4);
  3912. }
  3913. static inline int copy_u64_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
  3914. {
  3915. u32 rms_low, rms_high;
  3916. u64 rms;
  3917. rms_low = readl(src_low);
  3918. rms_high = readl(src_high);
  3919. rms = ((u64)rms_high << 32) | rms_low;
  3920. return copy_to_user(dest, &rms, 8);
  3921. }
  3922. static inline int copy_u48_le(void __user *dest, void __iomem *src_low, void __iomem *src_high)
  3923. {
  3924. u32 rms_low, rms_high;
  3925. u64 rms;
  3926. rms_low = readl(src_low) & 0xffffff00;
  3927. rms_high = readl(src_high) & 0xffffff00;
  3928. rms = ((u64)rms_high << 32) | rms_low;
  3929. return copy_to_user(dest, &rms, 8);
  3930. }
  3931. static int hdsp_9652_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3932. {
  3933. int doublespeed = 0;
  3934. int i, j, channels, ofs;
  3935. if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
  3936. doublespeed = 1;
  3937. channels = doublespeed ? 14 : 26;
  3938. for (i = 0, j = 0; i < 26; ++i) {
  3939. if (doublespeed && (i & 4))
  3940. continue;
  3941. ofs = HDSP_9652_peakBase - j * 4;
  3942. if (copy_u32_le(&peak_rms->input_peaks[i], hdsp->iobase + ofs))
  3943. return -EFAULT;
  3944. ofs -= channels * 4;
  3945. if (copy_u32_le(&peak_rms->playback_peaks[i], hdsp->iobase + ofs))
  3946. return -EFAULT;
  3947. ofs -= channels * 4;
  3948. if (copy_u32_le(&peak_rms->output_peaks[i], hdsp->iobase + ofs))
  3949. return -EFAULT;
  3950. ofs = HDSP_9652_rmsBase + j * 8;
  3951. if (copy_u48_le(&peak_rms->input_rms[i], hdsp->iobase + ofs,
  3952. hdsp->iobase + ofs + 4))
  3953. return -EFAULT;
  3954. ofs += channels * 8;
  3955. if (copy_u48_le(&peak_rms->playback_rms[i], hdsp->iobase + ofs,
  3956. hdsp->iobase + ofs + 4))
  3957. return -EFAULT;
  3958. ofs += channels * 8;
  3959. if (copy_u48_le(&peak_rms->output_rms[i], hdsp->iobase + ofs,
  3960. hdsp->iobase + ofs + 4))
  3961. return -EFAULT;
  3962. j++;
  3963. }
  3964. return 0;
  3965. }
  3966. static int hdsp_9632_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3967. {
  3968. int i, j;
  3969. struct hdsp_9632_meters __iomem *m;
  3970. int doublespeed = 0;
  3971. if (hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DoubleSpeedStatus)
  3972. doublespeed = 1;
  3973. m = (struct hdsp_9632_meters __iomem *)(hdsp->iobase+HDSP_9632_metersBase);
  3974. for (i = 0, j = 0; i < 16; ++i, ++j) {
  3975. if (copy_u32_le(&peak_rms->input_peaks[i], &m->input_peak[j]))
  3976. return -EFAULT;
  3977. if (copy_u32_le(&peak_rms->playback_peaks[i], &m->playback_peak[j]))
  3978. return -EFAULT;
  3979. if (copy_u32_le(&peak_rms->output_peaks[i], &m->output_peak[j]))
  3980. return -EFAULT;
  3981. if (copy_u64_le(&peak_rms->input_rms[i], &m->input_rms_low[j],
  3982. &m->input_rms_high[j]))
  3983. return -EFAULT;
  3984. if (copy_u64_le(&peak_rms->playback_rms[i], &m->playback_rms_low[j],
  3985. &m->playback_rms_high[j]))
  3986. return -EFAULT;
  3987. if (copy_u64_le(&peak_rms->output_rms[i], &m->output_rms_low[j],
  3988. &m->output_rms_high[j]))
  3989. return -EFAULT;
  3990. if (doublespeed && i == 3) i += 4;
  3991. }
  3992. return 0;
  3993. }
  3994. static int hdsp_get_peak(struct hdsp *hdsp, struct hdsp_peak_rms __user *peak_rms)
  3995. {
  3996. int i;
  3997. for (i = 0; i < 26; i++) {
  3998. if (copy_u32_le(&peak_rms->playback_peaks[i],
  3999. hdsp->iobase + HDSP_playbackPeakLevel + i * 4))
  4000. return -EFAULT;
  4001. if (copy_u32_le(&peak_rms->input_peaks[i],
  4002. hdsp->iobase + HDSP_inputPeakLevel + i * 4))
  4003. return -EFAULT;
  4004. }
  4005. for (i = 0; i < 28; i++) {
  4006. if (copy_u32_le(&peak_rms->output_peaks[i],
  4007. hdsp->iobase + HDSP_outputPeakLevel + i * 4))
  4008. return -EFAULT;
  4009. }
  4010. for (i = 0; i < 26; ++i) {
  4011. if (copy_u64_le(&peak_rms->playback_rms[i],
  4012. hdsp->iobase + HDSP_playbackRmsLevel + i * 8 + 4,
  4013. hdsp->iobase + HDSP_playbackRmsLevel + i * 8))
  4014. return -EFAULT;
  4015. if (copy_u64_le(&peak_rms->input_rms[i],
  4016. hdsp->iobase + HDSP_inputRmsLevel + i * 8 + 4,
  4017. hdsp->iobase + HDSP_inputRmsLevel + i * 8))
  4018. return -EFAULT;
  4019. }
  4020. return 0;
  4021. }
  4022. static int snd_hdsp_hwdep_ioctl(struct snd_hwdep *hw, struct file *file, unsigned int cmd, unsigned long arg)
  4023. {
  4024. struct hdsp *hdsp = hw->private_data;
  4025. void __user *argp = (void __user *)arg;
  4026. int err;
  4027. switch (cmd) {
  4028. case SNDRV_HDSP_IOCTL_GET_PEAK_RMS: {
  4029. struct hdsp_peak_rms __user *peak_rms = (struct hdsp_peak_rms __user *)arg;
  4030. err = hdsp_check_for_iobox(hdsp);
  4031. if (err < 0)
  4032. return err;
  4033. err = hdsp_check_for_firmware(hdsp, 1);
  4034. if (err < 0)
  4035. return err;
  4036. if (!(hdsp->state & HDSP_FirmwareLoaded)) {
  4037. dev_err(hdsp->card->dev,
  4038. "firmware needs to be uploaded to the card.\n");
  4039. return -EINVAL;
  4040. }
  4041. switch (hdsp->io_type) {
  4042. case H9652:
  4043. return hdsp_9652_get_peak(hdsp, peak_rms);
  4044. case H9632:
  4045. return hdsp_9632_get_peak(hdsp, peak_rms);
  4046. default:
  4047. return hdsp_get_peak(hdsp, peak_rms);
  4048. }
  4049. }
  4050. case SNDRV_HDSP_IOCTL_GET_CONFIG_INFO: {
  4051. struct hdsp_config_info info;
  4052. unsigned long flags;
  4053. int i;
  4054. err = hdsp_check_for_iobox(hdsp);
  4055. if (err < 0)
  4056. return err;
  4057. err = hdsp_check_for_firmware(hdsp, 1);
  4058. if (err < 0)
  4059. return err;
  4060. memset(&info, 0, sizeof(info));
  4061. spin_lock_irqsave(&hdsp->lock, flags);
  4062. info.pref_sync_ref = (unsigned char)hdsp_pref_sync_ref(hdsp);
  4063. info.wordclock_sync_check = (unsigned char)hdsp_wc_sync_check(hdsp);
  4064. if (hdsp->io_type != H9632)
  4065. info.adatsync_sync_check = (unsigned char)hdsp_adatsync_sync_check(hdsp);
  4066. info.spdif_sync_check = (unsigned char)hdsp_spdif_sync_check(hdsp);
  4067. for (i = 0; i < ((hdsp->io_type != Multiface && hdsp->io_type != RPM && hdsp->io_type != H9632) ? 3 : 1); ++i)
  4068. info.adat_sync_check[i] = (unsigned char)hdsp_adat_sync_check(hdsp, i);
  4069. info.spdif_in = (unsigned char)hdsp_spdif_in(hdsp);
  4070. info.spdif_out = (unsigned char)hdsp_toggle_setting(hdsp,
  4071. HDSP_SPDIFOpticalOut);
  4072. info.spdif_professional = (unsigned char)
  4073. hdsp_toggle_setting(hdsp, HDSP_SPDIFProfessional);
  4074. info.spdif_emphasis = (unsigned char)
  4075. hdsp_toggle_setting(hdsp, HDSP_SPDIFEmphasis);
  4076. info.spdif_nonaudio = (unsigned char)
  4077. hdsp_toggle_setting(hdsp, HDSP_SPDIFNonAudio);
  4078. info.spdif_sample_rate = hdsp_spdif_sample_rate(hdsp);
  4079. info.system_sample_rate = hdsp->system_sample_rate;
  4080. info.autosync_sample_rate = hdsp_external_sample_rate(hdsp);
  4081. info.system_clock_mode = (unsigned char)hdsp_system_clock_mode(hdsp);
  4082. info.clock_source = (unsigned char)hdsp_clock_source(hdsp);
  4083. info.autosync_ref = (unsigned char)hdsp_autosync_ref(hdsp);
  4084. info.line_out = (unsigned char)
  4085. hdsp_toggle_setting(hdsp, HDSP_LineOut);
  4086. if (hdsp->io_type == H9632) {
  4087. info.da_gain = (unsigned char)hdsp_da_gain(hdsp);
  4088. info.ad_gain = (unsigned char)hdsp_ad_gain(hdsp);
  4089. info.phone_gain = (unsigned char)hdsp_phone_gain(hdsp);
  4090. info.xlr_breakout_cable =
  4091. (unsigned char)hdsp_toggle_setting(hdsp,
  4092. HDSP_XLRBreakoutCable);
  4093. } else if (hdsp->io_type == RPM) {
  4094. info.da_gain = (unsigned char) hdsp_rpm_input12(hdsp);
  4095. info.ad_gain = (unsigned char) hdsp_rpm_input34(hdsp);
  4096. }
  4097. if (hdsp->io_type == H9632 || hdsp->io_type == H9652)
  4098. info.analog_extension_board =
  4099. (unsigned char)hdsp_toggle_setting(hdsp,
  4100. HDSP_AnalogExtensionBoard);
  4101. spin_unlock_irqrestore(&hdsp->lock, flags);
  4102. if (copy_to_user(argp, &info, sizeof(info)))
  4103. return -EFAULT;
  4104. break;
  4105. }
  4106. case SNDRV_HDSP_IOCTL_GET_9632_AEB: {
  4107. struct hdsp_9632_aeb h9632_aeb;
  4108. if (hdsp->io_type != H9632) return -EINVAL;
  4109. h9632_aeb.aebi = hdsp->ss_in_channels - H9632_SS_CHANNELS;
  4110. h9632_aeb.aebo = hdsp->ss_out_channels - H9632_SS_CHANNELS;
  4111. if (copy_to_user(argp, &h9632_aeb, sizeof(h9632_aeb)))
  4112. return -EFAULT;
  4113. break;
  4114. }
  4115. case SNDRV_HDSP_IOCTL_GET_VERSION: {
  4116. struct hdsp_version hdsp_version;
  4117. int err;
  4118. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
  4119. if (hdsp->io_type == Undefined) {
  4120. if ((err = hdsp_get_iobox_version(hdsp)) < 0)
  4121. return err;
  4122. }
  4123. memset(&hdsp_version, 0, sizeof(hdsp_version));
  4124. hdsp_version.io_type = hdsp->io_type;
  4125. hdsp_version.firmware_rev = hdsp->firmware_rev;
  4126. if ((err = copy_to_user(argp, &hdsp_version, sizeof(hdsp_version))))
  4127. return -EFAULT;
  4128. break;
  4129. }
  4130. case SNDRV_HDSP_IOCTL_UPLOAD_FIRMWARE: {
  4131. struct hdsp_firmware __user *firmware;
  4132. u32 __user *firmware_data;
  4133. int err;
  4134. if (hdsp->io_type == H9652 || hdsp->io_type == H9632) return -EINVAL;
  4135. /* SNDRV_HDSP_IOCTL_GET_VERSION must have been called */
  4136. if (hdsp->io_type == Undefined) return -EINVAL;
  4137. if (hdsp->state & (HDSP_FirmwareCached | HDSP_FirmwareLoaded))
  4138. return -EBUSY;
  4139. dev_info(hdsp->card->dev,
  4140. "initializing firmware upload\n");
  4141. firmware = (struct hdsp_firmware __user *)argp;
  4142. if (get_user(firmware_data, &firmware->firmware_data))
  4143. return -EFAULT;
  4144. if (hdsp_check_for_iobox (hdsp))
  4145. return -EIO;
  4146. if (!hdsp->fw_uploaded) {
  4147. hdsp->fw_uploaded = vmalloc(HDSP_FIRMWARE_SIZE);
  4148. if (!hdsp->fw_uploaded)
  4149. return -ENOMEM;
  4150. }
  4151. if (copy_from_user(hdsp->fw_uploaded, firmware_data,
  4152. HDSP_FIRMWARE_SIZE)) {
  4153. vfree(hdsp->fw_uploaded);
  4154. hdsp->fw_uploaded = NULL;
  4155. return -EFAULT;
  4156. }
  4157. hdsp->state |= HDSP_FirmwareCached;
  4158. if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
  4159. return err;
  4160. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4161. if ((err = snd_hdsp_enable_io(hdsp)) < 0)
  4162. return err;
  4163. snd_hdsp_initialize_channels(hdsp);
  4164. snd_hdsp_initialize_midi_flush(hdsp);
  4165. if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
  4166. dev_err(hdsp->card->dev,
  4167. "error creating alsa devices\n");
  4168. return err;
  4169. }
  4170. }
  4171. break;
  4172. }
  4173. case SNDRV_HDSP_IOCTL_GET_MIXER: {
  4174. struct hdsp_mixer __user *mixer = (struct hdsp_mixer __user *)argp;
  4175. if (copy_to_user(mixer->matrix, hdsp->mixer_matrix, sizeof(unsigned short)*HDSP_MATRIX_MIXER_SIZE))
  4176. return -EFAULT;
  4177. break;
  4178. }
  4179. default:
  4180. return -EINVAL;
  4181. }
  4182. return 0;
  4183. }
  4184. static struct snd_pcm_ops snd_hdsp_playback_ops = {
  4185. .open = snd_hdsp_playback_open,
  4186. .close = snd_hdsp_playback_release,
  4187. .ioctl = snd_hdsp_ioctl,
  4188. .hw_params = snd_hdsp_hw_params,
  4189. .prepare = snd_hdsp_prepare,
  4190. .trigger = snd_hdsp_trigger,
  4191. .pointer = snd_hdsp_hw_pointer,
  4192. .copy = snd_hdsp_playback_copy,
  4193. .silence = snd_hdsp_hw_silence,
  4194. };
  4195. static struct snd_pcm_ops snd_hdsp_capture_ops = {
  4196. .open = snd_hdsp_capture_open,
  4197. .close = snd_hdsp_capture_release,
  4198. .ioctl = snd_hdsp_ioctl,
  4199. .hw_params = snd_hdsp_hw_params,
  4200. .prepare = snd_hdsp_prepare,
  4201. .trigger = snd_hdsp_trigger,
  4202. .pointer = snd_hdsp_hw_pointer,
  4203. .copy = snd_hdsp_capture_copy,
  4204. };
  4205. static int snd_hdsp_create_hwdep(struct snd_card *card, struct hdsp *hdsp)
  4206. {
  4207. struct snd_hwdep *hw;
  4208. int err;
  4209. if ((err = snd_hwdep_new(card, "HDSP hwdep", 0, &hw)) < 0)
  4210. return err;
  4211. hdsp->hwdep = hw;
  4212. hw->private_data = hdsp;
  4213. strcpy(hw->name, "HDSP hwdep interface");
  4214. hw->ops.ioctl = snd_hdsp_hwdep_ioctl;
  4215. hw->ops.ioctl_compat = snd_hdsp_hwdep_ioctl;
  4216. return 0;
  4217. }
  4218. static int snd_hdsp_create_pcm(struct snd_card *card, struct hdsp *hdsp)
  4219. {
  4220. struct snd_pcm *pcm;
  4221. int err;
  4222. if ((err = snd_pcm_new(card, hdsp->card_name, 0, 1, 1, &pcm)) < 0)
  4223. return err;
  4224. hdsp->pcm = pcm;
  4225. pcm->private_data = hdsp;
  4226. strcpy(pcm->name, hdsp->card_name);
  4227. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_hdsp_playback_ops);
  4228. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_hdsp_capture_ops);
  4229. pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
  4230. return 0;
  4231. }
  4232. static void snd_hdsp_9652_enable_mixer (struct hdsp *hdsp)
  4233. {
  4234. hdsp->control2_register |= HDSP_9652_ENABLE_MIXER;
  4235. hdsp_write (hdsp, HDSP_control2Reg, hdsp->control2_register);
  4236. }
  4237. static int snd_hdsp_enable_io (struct hdsp *hdsp)
  4238. {
  4239. int i;
  4240. if (hdsp_fifo_wait (hdsp, 0, 100)) {
  4241. dev_err(hdsp->card->dev,
  4242. "enable_io fifo_wait failed\n");
  4243. return -EIO;
  4244. }
  4245. for (i = 0; i < hdsp->max_channels; ++i) {
  4246. hdsp_write (hdsp, HDSP_inputEnable + (4 * i), 1);
  4247. hdsp_write (hdsp, HDSP_outputEnable + (4 * i), 1);
  4248. }
  4249. return 0;
  4250. }
  4251. static void snd_hdsp_initialize_channels(struct hdsp *hdsp)
  4252. {
  4253. int status, aebi_channels, aebo_channels;
  4254. switch (hdsp->io_type) {
  4255. case Digiface:
  4256. hdsp->card_name = "RME Hammerfall DSP + Digiface";
  4257. hdsp->ss_in_channels = hdsp->ss_out_channels = DIGIFACE_SS_CHANNELS;
  4258. hdsp->ds_in_channels = hdsp->ds_out_channels = DIGIFACE_DS_CHANNELS;
  4259. break;
  4260. case H9652:
  4261. hdsp->card_name = "RME Hammerfall HDSP 9652";
  4262. hdsp->ss_in_channels = hdsp->ss_out_channels = H9652_SS_CHANNELS;
  4263. hdsp->ds_in_channels = hdsp->ds_out_channels = H9652_DS_CHANNELS;
  4264. break;
  4265. case H9632:
  4266. status = hdsp_read(hdsp, HDSP_statusRegister);
  4267. /* HDSP_AEBx bits are low when AEB are connected */
  4268. aebi_channels = (status & HDSP_AEBI) ? 0 : 4;
  4269. aebo_channels = (status & HDSP_AEBO) ? 0 : 4;
  4270. hdsp->card_name = "RME Hammerfall HDSP 9632";
  4271. hdsp->ss_in_channels = H9632_SS_CHANNELS+aebi_channels;
  4272. hdsp->ds_in_channels = H9632_DS_CHANNELS+aebi_channels;
  4273. hdsp->qs_in_channels = H9632_QS_CHANNELS+aebi_channels;
  4274. hdsp->ss_out_channels = H9632_SS_CHANNELS+aebo_channels;
  4275. hdsp->ds_out_channels = H9632_DS_CHANNELS+aebo_channels;
  4276. hdsp->qs_out_channels = H9632_QS_CHANNELS+aebo_channels;
  4277. break;
  4278. case Multiface:
  4279. hdsp->card_name = "RME Hammerfall DSP + Multiface";
  4280. hdsp->ss_in_channels = hdsp->ss_out_channels = MULTIFACE_SS_CHANNELS;
  4281. hdsp->ds_in_channels = hdsp->ds_out_channels = MULTIFACE_DS_CHANNELS;
  4282. break;
  4283. case RPM:
  4284. hdsp->card_name = "RME Hammerfall DSP + RPM";
  4285. hdsp->ss_in_channels = RPM_CHANNELS-1;
  4286. hdsp->ss_out_channels = RPM_CHANNELS;
  4287. hdsp->ds_in_channels = RPM_CHANNELS-1;
  4288. hdsp->ds_out_channels = RPM_CHANNELS;
  4289. break;
  4290. default:
  4291. /* should never get here */
  4292. break;
  4293. }
  4294. }
  4295. static void snd_hdsp_initialize_midi_flush (struct hdsp *hdsp)
  4296. {
  4297. snd_hdsp_flush_midi_input (hdsp, 0);
  4298. snd_hdsp_flush_midi_input (hdsp, 1);
  4299. }
  4300. static int snd_hdsp_create_alsa_devices(struct snd_card *card, struct hdsp *hdsp)
  4301. {
  4302. int err;
  4303. if ((err = snd_hdsp_create_pcm(card, hdsp)) < 0) {
  4304. dev_err(card->dev,
  4305. "Error creating pcm interface\n");
  4306. return err;
  4307. }
  4308. if ((err = snd_hdsp_create_midi(card, hdsp, 0)) < 0) {
  4309. dev_err(card->dev,
  4310. "Error creating first midi interface\n");
  4311. return err;
  4312. }
  4313. if (hdsp->io_type == Digiface || hdsp->io_type == H9652) {
  4314. if ((err = snd_hdsp_create_midi(card, hdsp, 1)) < 0) {
  4315. dev_err(card->dev,
  4316. "Error creating second midi interface\n");
  4317. return err;
  4318. }
  4319. }
  4320. if ((err = snd_hdsp_create_controls(card, hdsp)) < 0) {
  4321. dev_err(card->dev,
  4322. "Error creating ctl interface\n");
  4323. return err;
  4324. }
  4325. snd_hdsp_proc_init(hdsp);
  4326. hdsp->system_sample_rate = -1;
  4327. hdsp->playback_pid = -1;
  4328. hdsp->capture_pid = -1;
  4329. hdsp->capture_substream = NULL;
  4330. hdsp->playback_substream = NULL;
  4331. if ((err = snd_hdsp_set_defaults(hdsp)) < 0) {
  4332. dev_err(card->dev,
  4333. "Error setting default values\n");
  4334. return err;
  4335. }
  4336. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4337. strcpy(card->shortname, "Hammerfall DSP");
  4338. sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
  4339. hdsp->port, hdsp->irq);
  4340. if ((err = snd_card_register(card)) < 0) {
  4341. dev_err(card->dev,
  4342. "error registering card\n");
  4343. return err;
  4344. }
  4345. hdsp->state |= HDSP_InitializationComplete;
  4346. }
  4347. return 0;
  4348. }
  4349. /* load firmware via hotplug fw loader */
  4350. static int hdsp_request_fw_loader(struct hdsp *hdsp)
  4351. {
  4352. const char *fwfile;
  4353. const struct firmware *fw;
  4354. int err;
  4355. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  4356. return 0;
  4357. if (hdsp->io_type == Undefined) {
  4358. if ((err = hdsp_get_iobox_version(hdsp)) < 0)
  4359. return err;
  4360. if (hdsp->io_type == H9652 || hdsp->io_type == H9632)
  4361. return 0;
  4362. }
  4363. /* caution: max length of firmware filename is 30! */
  4364. switch (hdsp->io_type) {
  4365. case RPM:
  4366. fwfile = "/*(DEBLOBBED)*/";
  4367. break;
  4368. case Multiface:
  4369. if (hdsp->firmware_rev == 0xa)
  4370. fwfile = "/*(DEBLOBBED)*/";
  4371. else
  4372. fwfile = "/*(DEBLOBBED)*/";
  4373. break;
  4374. case Digiface:
  4375. if (hdsp->firmware_rev == 0xa)
  4376. fwfile = "/*(DEBLOBBED)*/";
  4377. else
  4378. fwfile = "/*(DEBLOBBED)*/";
  4379. break;
  4380. default:
  4381. dev_err(hdsp->card->dev,
  4382. "invalid io_type %d\n", hdsp->io_type);
  4383. return -EINVAL;
  4384. }
  4385. if (reject_firmware(&fw, fwfile, &hdsp->pci->dev)) {
  4386. dev_err(hdsp->card->dev,
  4387. "cannot load firmware %s\n", fwfile);
  4388. return -ENOENT;
  4389. }
  4390. if (fw->size < HDSP_FIRMWARE_SIZE) {
  4391. dev_err(hdsp->card->dev,
  4392. "too short firmware size %d (expected %d)\n",
  4393. (int)fw->size, HDSP_FIRMWARE_SIZE);
  4394. return -EINVAL;
  4395. }
  4396. hdsp->firmware = fw;
  4397. hdsp->state |= HDSP_FirmwareCached;
  4398. if ((err = snd_hdsp_load_firmware_from_cache(hdsp)) < 0)
  4399. return err;
  4400. if (!(hdsp->state & HDSP_InitializationComplete)) {
  4401. if ((err = snd_hdsp_enable_io(hdsp)) < 0)
  4402. return err;
  4403. if ((err = snd_hdsp_create_hwdep(hdsp->card, hdsp)) < 0) {
  4404. dev_err(hdsp->card->dev,
  4405. "error creating hwdep device\n");
  4406. return err;
  4407. }
  4408. snd_hdsp_initialize_channels(hdsp);
  4409. snd_hdsp_initialize_midi_flush(hdsp);
  4410. if ((err = snd_hdsp_create_alsa_devices(hdsp->card, hdsp)) < 0) {
  4411. dev_err(hdsp->card->dev,
  4412. "error creating alsa devices\n");
  4413. return err;
  4414. }
  4415. }
  4416. return 0;
  4417. }
  4418. static int snd_hdsp_create(struct snd_card *card,
  4419. struct hdsp *hdsp)
  4420. {
  4421. struct pci_dev *pci = hdsp->pci;
  4422. int err;
  4423. int is_9652 = 0;
  4424. int is_9632 = 0;
  4425. hdsp->irq = -1;
  4426. hdsp->state = 0;
  4427. hdsp->midi[0].rmidi = NULL;
  4428. hdsp->midi[1].rmidi = NULL;
  4429. hdsp->midi[0].input = NULL;
  4430. hdsp->midi[1].input = NULL;
  4431. hdsp->midi[0].output = NULL;
  4432. hdsp->midi[1].output = NULL;
  4433. hdsp->midi[0].pending = 0;
  4434. hdsp->midi[1].pending = 0;
  4435. spin_lock_init(&hdsp->midi[0].lock);
  4436. spin_lock_init(&hdsp->midi[1].lock);
  4437. hdsp->iobase = NULL;
  4438. hdsp->control_register = 0;
  4439. hdsp->control2_register = 0;
  4440. hdsp->io_type = Undefined;
  4441. hdsp->max_channels = 26;
  4442. hdsp->card = card;
  4443. spin_lock_init(&hdsp->lock);
  4444. tasklet_init(&hdsp->midi_tasklet, hdsp_midi_tasklet, (unsigned long)hdsp);
  4445. pci_read_config_word(hdsp->pci, PCI_CLASS_REVISION, &hdsp->firmware_rev);
  4446. hdsp->firmware_rev &= 0xff;
  4447. /* From Martin Bjoernsen :
  4448. "It is important that the card's latency timer register in
  4449. the PCI configuration space is set to a value much larger
  4450. than 0 by the computer's BIOS or the driver.
  4451. The windows driver always sets this 8 bit register [...]
  4452. to its maximum 255 to avoid problems with some computers."
  4453. */
  4454. pci_write_config_byte(hdsp->pci, PCI_LATENCY_TIMER, 0xFF);
  4455. strcpy(card->driver, "H-DSP");
  4456. strcpy(card->mixername, "Xilinx FPGA");
  4457. if (hdsp->firmware_rev < 0xa)
  4458. return -ENODEV;
  4459. else if (hdsp->firmware_rev < 0x64)
  4460. hdsp->card_name = "RME Hammerfall DSP";
  4461. else if (hdsp->firmware_rev < 0x96) {
  4462. hdsp->card_name = "RME HDSP 9652";
  4463. is_9652 = 1;
  4464. } else {
  4465. hdsp->card_name = "RME HDSP 9632";
  4466. hdsp->max_channels = 16;
  4467. is_9632 = 1;
  4468. }
  4469. if ((err = pci_enable_device(pci)) < 0)
  4470. return err;
  4471. pci_set_master(hdsp->pci);
  4472. if ((err = pci_request_regions(pci, "hdsp")) < 0)
  4473. return err;
  4474. hdsp->port = pci_resource_start(pci, 0);
  4475. if ((hdsp->iobase = ioremap_nocache(hdsp->port, HDSP_IO_EXTENT)) == NULL) {
  4476. dev_err(hdsp->card->dev, "unable to remap region 0x%lx-0x%lx\n",
  4477. hdsp->port, hdsp->port + HDSP_IO_EXTENT - 1);
  4478. return -EBUSY;
  4479. }
  4480. if (request_irq(pci->irq, snd_hdsp_interrupt, IRQF_SHARED,
  4481. KBUILD_MODNAME, hdsp)) {
  4482. dev_err(hdsp->card->dev, "unable to use IRQ %d\n", pci->irq);
  4483. return -EBUSY;
  4484. }
  4485. hdsp->irq = pci->irq;
  4486. hdsp->precise_ptr = 0;
  4487. hdsp->use_midi_tasklet = 1;
  4488. hdsp->dds_value = 0;
  4489. if ((err = snd_hdsp_initialize_memory(hdsp)) < 0)
  4490. return err;
  4491. if (!is_9652 && !is_9632) {
  4492. /* we wait a maximum of 10 seconds to let freshly
  4493. * inserted cardbus cards do their hardware init */
  4494. err = hdsp_wait_for_iobox(hdsp, 1000, 10);
  4495. if (err < 0)
  4496. return err;
  4497. if ((hdsp_read (hdsp, HDSP_statusRegister) & HDSP_DllError) != 0) {
  4498. if ((err = hdsp_request_fw_loader(hdsp)) < 0)
  4499. /* we don't fail as this can happen
  4500. if userspace is not ready for
  4501. firmware upload
  4502. */
  4503. dev_err(hdsp->card->dev,
  4504. "couldn't get firmware from userspace. try using hdsploader\n");
  4505. else
  4506. /* init is complete, we return */
  4507. return 0;
  4508. /* we defer initialization */
  4509. dev_info(hdsp->card->dev,
  4510. "card initialization pending : waiting for firmware\n");
  4511. if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
  4512. return err;
  4513. return 0;
  4514. } else {
  4515. dev_info(hdsp->card->dev,
  4516. "Firmware already present, initializing card.\n");
  4517. if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version2)
  4518. hdsp->io_type = RPM;
  4519. else if (hdsp_read(hdsp, HDSP_status2Register) & HDSP_version1)
  4520. hdsp->io_type = Multiface;
  4521. else
  4522. hdsp->io_type = Digiface;
  4523. }
  4524. }
  4525. if ((err = snd_hdsp_enable_io(hdsp)) != 0)
  4526. return err;
  4527. if (is_9652)
  4528. hdsp->io_type = H9652;
  4529. if (is_9632)
  4530. hdsp->io_type = H9632;
  4531. if ((err = snd_hdsp_create_hwdep(card, hdsp)) < 0)
  4532. return err;
  4533. snd_hdsp_initialize_channels(hdsp);
  4534. snd_hdsp_initialize_midi_flush(hdsp);
  4535. hdsp->state |= HDSP_FirmwareLoaded;
  4536. if ((err = snd_hdsp_create_alsa_devices(card, hdsp)) < 0)
  4537. return err;
  4538. return 0;
  4539. }
  4540. static int snd_hdsp_free(struct hdsp *hdsp)
  4541. {
  4542. if (hdsp->port) {
  4543. /* stop the audio, and cancel all interrupts */
  4544. tasklet_kill(&hdsp->midi_tasklet);
  4545. hdsp->control_register &= ~(HDSP_Start|HDSP_AudioInterruptEnable|HDSP_Midi0InterruptEnable|HDSP_Midi1InterruptEnable);
  4546. hdsp_write (hdsp, HDSP_controlRegister, hdsp->control_register);
  4547. }
  4548. if (hdsp->irq >= 0)
  4549. free_irq(hdsp->irq, (void *)hdsp);
  4550. snd_hdsp_free_buffers(hdsp);
  4551. release_firmware(hdsp->firmware);
  4552. vfree(hdsp->fw_uploaded);
  4553. iounmap(hdsp->iobase);
  4554. if (hdsp->port)
  4555. pci_release_regions(hdsp->pci);
  4556. pci_disable_device(hdsp->pci);
  4557. return 0;
  4558. }
  4559. static void snd_hdsp_card_free(struct snd_card *card)
  4560. {
  4561. struct hdsp *hdsp = card->private_data;
  4562. if (hdsp)
  4563. snd_hdsp_free(hdsp);
  4564. }
  4565. static int snd_hdsp_probe(struct pci_dev *pci,
  4566. const struct pci_device_id *pci_id)
  4567. {
  4568. static int dev;
  4569. struct hdsp *hdsp;
  4570. struct snd_card *card;
  4571. int err;
  4572. if (dev >= SNDRV_CARDS)
  4573. return -ENODEV;
  4574. if (!enable[dev]) {
  4575. dev++;
  4576. return -ENOENT;
  4577. }
  4578. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  4579. sizeof(struct hdsp), &card);
  4580. if (err < 0)
  4581. return err;
  4582. hdsp = card->private_data;
  4583. card->private_free = snd_hdsp_card_free;
  4584. hdsp->dev = dev;
  4585. hdsp->pci = pci;
  4586. if ((err = snd_hdsp_create(card, hdsp)) < 0) {
  4587. snd_card_free(card);
  4588. return err;
  4589. }
  4590. strcpy(card->shortname, "Hammerfall DSP");
  4591. sprintf(card->longname, "%s at 0x%lx, irq %d", hdsp->card_name,
  4592. hdsp->port, hdsp->irq);
  4593. if ((err = snd_card_register(card)) < 0) {
  4594. snd_card_free(card);
  4595. return err;
  4596. }
  4597. pci_set_drvdata(pci, card);
  4598. dev++;
  4599. return 0;
  4600. }
  4601. static void snd_hdsp_remove(struct pci_dev *pci)
  4602. {
  4603. snd_card_free(pci_get_drvdata(pci));
  4604. }
  4605. static struct pci_driver hdsp_driver = {
  4606. .name = KBUILD_MODNAME,
  4607. .id_table = snd_hdsp_ids,
  4608. .probe = snd_hdsp_probe,
  4609. .remove = snd_hdsp_remove,
  4610. };
  4611. module_pci_driver(hdsp_driver);