awe_wave.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147
  1. /*
  2. * sound/awe_wave.c
  3. *
  4. * The low level driver for the AWE32/SB32/AWE64 wave table synth.
  5. * version 0.4.4; Jan. 4, 2000
  6. *
  7. * Copyright (C) 1996-2000 Takashi Iwai
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. /*
  24. * Changelog:
  25. * Aug 18, 2003, Adam Belay <ambx1@neo.rr.com>
  26. * - detection code rewrite
  27. */
  28. #include <linux/awe_voice.h>
  29. #include <linux/config.h>
  30. #include <linux/init.h>
  31. #include <linux/module.h>
  32. #include <linux/string.h>
  33. #include <linux/pnp.h>
  34. #include "sound_config.h"
  35. #include "awe_wave.h"
  36. #include "awe_hw.h"
  37. #ifdef AWE_HAS_GUS_COMPATIBILITY
  38. #include "tuning.h"
  39. #include <linux/ultrasound.h>
  40. #endif
  41. /*
  42. * debug message
  43. */
  44. #ifdef AWE_DEBUG_ON
  45. #define DEBUG(LVL,XXX) {if (ctrls[AWE_MD_DEBUG_MODE] > LVL) { XXX; }}
  46. #define ERRMSG(XXX) {if (ctrls[AWE_MD_DEBUG_MODE]) { XXX; }}
  47. #define FATALERR(XXX) XXX
  48. #else
  49. #define DEBUG(LVL,XXX) /**/
  50. #define ERRMSG(XXX) XXX
  51. #define FATALERR(XXX) XXX
  52. #endif
  53. /*
  54. * bank and voice record
  55. */
  56. typedef struct _sf_list sf_list;
  57. typedef struct _awe_voice_list awe_voice_list;
  58. typedef struct _awe_sample_list awe_sample_list;
  59. /* soundfont record */
  60. struct _sf_list {
  61. unsigned short sf_id; /* id number */
  62. unsigned short type; /* lock & shared flags */
  63. int num_info; /* current info table index */
  64. int num_sample; /* current sample table index */
  65. int mem_ptr; /* current word byte pointer */
  66. awe_voice_list *infos, *last_infos; /* instruments */
  67. awe_sample_list *samples, *last_samples; /* samples */
  68. #ifdef AWE_ALLOW_SAMPLE_SHARING
  69. sf_list *shared; /* shared list */
  70. unsigned char name[AWE_PATCH_NAME_LEN]; /* sharing id */
  71. #endif
  72. sf_list *next, *prev;
  73. };
  74. /* instrument list */
  75. struct _awe_voice_list {
  76. awe_voice_info v; /* instrument information */
  77. sf_list *holder; /* parent sf_list of this record */
  78. unsigned char bank, instr; /* preset number information */
  79. char type, disabled; /* type=normal/mapped, disabled=boolean */
  80. awe_voice_list *next; /* linked list with same sf_id */
  81. awe_voice_list *next_instr; /* instrument list */
  82. awe_voice_list *next_bank; /* hash table list */
  83. };
  84. /* voice list type */
  85. #define V_ST_NORMAL 0
  86. #define V_ST_MAPPED 1
  87. /* sample list */
  88. struct _awe_sample_list {
  89. awe_sample_info v; /* sample information */
  90. sf_list *holder; /* parent sf_list of this record */
  91. awe_sample_list *next; /* linked list with same sf_id */
  92. };
  93. /* sample and information table */
  94. static int current_sf_id; /* current number of fonts */
  95. static int locked_sf_id; /* locked position */
  96. static sf_list *sfhead, *sftail; /* linked-lists */
  97. #define awe_free_mem_ptr() (sftail ? sftail->mem_ptr : 0)
  98. #define awe_free_info() (sftail ? sftail->num_info : 0)
  99. #define awe_free_sample() (sftail ? sftail->num_sample : 0)
  100. #define AWE_MAX_PRESETS 256
  101. #define AWE_DEFAULT_PRESET 0
  102. #define AWE_DEFAULT_BANK 0
  103. #define AWE_DEFAULT_DRUM 0
  104. #define AWE_DRUM_BANK 128
  105. #define MAX_LAYERS AWE_MAX_VOICES
  106. /* preset table index */
  107. static awe_voice_list *preset_table[AWE_MAX_PRESETS];
  108. /*
  109. * voice table
  110. */
  111. /* effects table */
  112. typedef struct FX_Rec { /* channel effects */
  113. unsigned char flags[AWE_FX_END];
  114. short val[AWE_FX_END];
  115. } FX_Rec;
  116. /* channel parameters */
  117. typedef struct _awe_chan_info {
  118. int channel; /* channel number */
  119. int bank; /* current tone bank */
  120. int instr; /* current program */
  121. int bender; /* midi pitchbend (-8192 - 8192) */
  122. int bender_range; /* midi bender range (x100) */
  123. int panning; /* panning (0-127) */
  124. int main_vol; /* channel volume (0-127) */
  125. int expression_vol; /* midi expression (0-127) */
  126. int chan_press; /* channel pressure */
  127. int sustained; /* sustain status in MIDI */
  128. FX_Rec fx; /* effects */
  129. FX_Rec fx_layer[MAX_LAYERS]; /* layer effects */
  130. } awe_chan_info;
  131. /* voice parameters */
  132. typedef struct _voice_info {
  133. int state;
  134. #define AWE_ST_OFF (1<<0) /* no sound */
  135. #define AWE_ST_ON (1<<1) /* playing */
  136. #define AWE_ST_STANDBY (1<<2) /* stand by for playing */
  137. #define AWE_ST_SUSTAINED (1<<3) /* sustained */
  138. #define AWE_ST_MARK (1<<4) /* marked for allocation */
  139. #define AWE_ST_DRAM (1<<5) /* DRAM read/write */
  140. #define AWE_ST_FM (1<<6) /* reserved for FM */
  141. #define AWE_ST_RELEASED (1<<7) /* released */
  142. int ch; /* midi channel */
  143. int key; /* internal key for search */
  144. int layer; /* layer number (for channel mode only) */
  145. int time; /* allocated time */
  146. awe_chan_info *cinfo; /* channel info */
  147. int note; /* midi key (0-127) */
  148. int velocity; /* midi velocity (0-127) */
  149. int sostenuto; /* sostenuto on/off */
  150. awe_voice_info *sample; /* assigned voice */
  151. /* EMU8000 parameters */
  152. int apitch; /* pitch parameter */
  153. int avol; /* volume parameter */
  154. int apan; /* panning parameter */
  155. int acutoff; /* cutoff parameter */
  156. short aaux; /* aux word */
  157. } voice_info;
  158. /* voice information */
  159. static voice_info voices[AWE_MAX_VOICES];
  160. #define IS_NO_SOUND(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_RELEASED|AWE_ST_STANDBY|AWE_ST_SUSTAINED))
  161. #define IS_NO_EFFECT(v) (voices[v].state != AWE_ST_ON)
  162. #define IS_PLAYING(v) (voices[v].state & (AWE_ST_ON|AWE_ST_SUSTAINED|AWE_ST_RELEASED))
  163. #define IS_EMPTY(v) (voices[v].state & (AWE_ST_OFF|AWE_ST_MARK|AWE_ST_DRAM|AWE_ST_FM))
  164. /* MIDI channel effects information (for hw control) */
  165. static awe_chan_info channels[AWE_MAX_CHANNELS];
  166. /*
  167. * global variables
  168. */
  169. #ifndef AWE_DEFAULT_BASE_ADDR
  170. #define AWE_DEFAULT_BASE_ADDR 0 /* autodetect */
  171. #endif
  172. #ifndef AWE_DEFAULT_MEM_SIZE
  173. #define AWE_DEFAULT_MEM_SIZE -1 /* autodetect */
  174. #endif
  175. static int io = AWE_DEFAULT_BASE_ADDR; /* Emu8000 base address */
  176. static int memsize = AWE_DEFAULT_MEM_SIZE; /* memory size in Kbytes */
  177. #ifdef CONFIG_PNP
  178. static int isapnp = -1;
  179. #else
  180. static int isapnp;
  181. #endif
  182. MODULE_AUTHOR("Takashi Iwai <iwai@ww.uni-erlangen.de>");
  183. MODULE_DESCRIPTION("SB AWE32/64 WaveTable driver");
  184. MODULE_LICENSE("GPL");
  185. module_param(io, int, 0);
  186. MODULE_PARM_DESC(io, "base i/o port of Emu8000");
  187. module_param(memsize, int, 0);
  188. MODULE_PARM_DESC(memsize, "onboard DRAM size in Kbytes");
  189. module_param(isapnp, bool, 0);
  190. MODULE_PARM_DESC(isapnp, "use ISAPnP detection");
  191. /* DRAM start offset */
  192. static int awe_mem_start = AWE_DRAM_OFFSET;
  193. /* maximum channels for playing */
  194. static int awe_max_voices = AWE_MAX_VOICES;
  195. static int patch_opened; /* sample already loaded? */
  196. static char atten_relative = FALSE;
  197. static short atten_offset;
  198. static int awe_present = FALSE; /* awe device present? */
  199. static int awe_busy = FALSE; /* awe device opened? */
  200. static int my_dev = -1;
  201. #define DEFAULT_DRUM_FLAGS ((1 << 9) | (1 << 25))
  202. #define IS_DRUM_CHANNEL(c) (drum_flags & (1 << (c)))
  203. #define DRUM_CHANNEL_ON(c) (drum_flags |= (1 << (c)))
  204. #define DRUM_CHANNEL_OFF(c) (drum_flags &= ~(1 << (c)))
  205. static unsigned int drum_flags = DEFAULT_DRUM_FLAGS; /* channel flags */
  206. static int playing_mode = AWE_PLAY_INDIRECT;
  207. #define SINGLE_LAYER_MODE() (playing_mode == AWE_PLAY_INDIRECT || playing_mode == AWE_PLAY_DIRECT)
  208. #define MULTI_LAYER_MODE() (playing_mode == AWE_PLAY_MULTI || playing_mode == AWE_PLAY_MULTI2)
  209. static int current_alloc_time; /* voice allocation index for channel mode */
  210. static struct synth_info awe_info = {
  211. "AWE32 Synth", /* name */
  212. 0, /* device */
  213. SYNTH_TYPE_SAMPLE, /* synth_type */
  214. SAMPLE_TYPE_AWE32, /* synth_subtype */
  215. 0, /* perc_mode (obsolete) */
  216. AWE_MAX_VOICES, /* nr_voices */
  217. 0, /* nr_drums (obsolete) */
  218. 400 /* instr_bank_size */
  219. };
  220. static struct voice_alloc_info *voice_alloc; /* set at initialization */
  221. /*
  222. * function prototypes
  223. */
  224. static int awe_request_region(void);
  225. static void awe_release_region(void);
  226. static void awe_reset_samples(void);
  227. /* emu8000 chip i/o access */
  228. static void setup_ports(int p1, int p2, int p3);
  229. static void awe_poke(unsigned short cmd, unsigned short port, unsigned short data);
  230. static void awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data);
  231. static unsigned short awe_peek(unsigned short cmd, unsigned short port);
  232. static unsigned int awe_peek_dw(unsigned short cmd, unsigned short port);
  233. static void awe_wait(unsigned short delay);
  234. /* initialize emu8000 chip */
  235. static void awe_initialize(void);
  236. /* set voice parameters */
  237. static void awe_init_ctrl_parms(int init_all);
  238. static void awe_init_voice_info(awe_voice_info *vp);
  239. static void awe_init_voice_parm(awe_voice_parm *pp);
  240. #ifdef AWE_HAS_GUS_COMPATIBILITY
  241. static int freq_to_note(int freq);
  242. static int calc_rate_offset(int Hz);
  243. /*static int calc_parm_delay(int msec);*/
  244. static int calc_parm_hold(int msec);
  245. static int calc_parm_attack(int msec);
  246. static int calc_parm_decay(int msec);
  247. static int calc_parm_search(int msec, short *table);
  248. #endif /* gus compat */
  249. /* turn on/off note */
  250. static void awe_note_on(int voice);
  251. static void awe_note_off(int voice);
  252. static void awe_terminate(int voice);
  253. static void awe_exclusive_off(int voice);
  254. static void awe_note_off_all(int do_sustain);
  255. /* calculate voice parameters */
  256. typedef void (*fx_affect_func)(int voice, int forced);
  257. static void awe_set_pitch(int voice, int forced);
  258. static void awe_set_voice_pitch(int voice, int forced);
  259. static void awe_set_volume(int voice, int forced);
  260. static void awe_set_voice_vol(int voice, int forced);
  261. static void awe_set_pan(int voice, int forced);
  262. static void awe_fx_fmmod(int voice, int forced);
  263. static void awe_fx_tremfrq(int voice, int forced);
  264. static void awe_fx_fm2frq2(int voice, int forced);
  265. static void awe_fx_filterQ(int voice, int forced);
  266. static void awe_calc_pitch(int voice);
  267. #ifdef AWE_HAS_GUS_COMPATIBILITY
  268. static void awe_calc_pitch_from_freq(int voice, int freq);
  269. #endif
  270. static void awe_calc_volume(int voice);
  271. static void awe_update_volume(void);
  272. static void awe_change_master_volume(short val);
  273. static void awe_voice_init(int voice, int init_all);
  274. static void awe_channel_init(int ch, int init_all);
  275. static void awe_fx_init(int ch);
  276. static void awe_send_effect(int voice, int layer, int type, int val);
  277. static void awe_modwheel_change(int voice, int value);
  278. /* sequencer interface */
  279. static int awe_open(int dev, int mode);
  280. static void awe_close(int dev);
  281. static int awe_ioctl(int dev, unsigned int cmd, void __user * arg);
  282. static int awe_kill_note(int dev, int voice, int note, int velocity);
  283. static int awe_start_note(int dev, int v, int note_num, int volume);
  284. static int awe_set_instr(int dev, int voice, int instr_no);
  285. static int awe_set_instr_2(int dev, int voice, int instr_no);
  286. static void awe_reset(int dev);
  287. static void awe_hw_control(int dev, unsigned char *event);
  288. static int awe_load_patch(int dev, int format, const char __user *addr,
  289. int offs, int count, int pmgr_flag);
  290. static void awe_aftertouch(int dev, int voice, int pressure);
  291. static void awe_controller(int dev, int voice, int ctrl_num, int value);
  292. static void awe_panning(int dev, int voice, int value);
  293. static void awe_volume_method(int dev, int mode);
  294. static void awe_bender(int dev, int voice, int value);
  295. static int awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc);
  296. static void awe_setup_voice(int dev, int voice, int chn);
  297. #define awe_key_pressure(dev,voice,key,press) awe_start_note(dev,voice,(key)+128,press)
  298. /* hardware controls */
  299. #ifdef AWE_HAS_GUS_COMPATIBILITY
  300. static void awe_hw_gus_control(int dev, int cmd, unsigned char *event);
  301. #endif
  302. static void awe_hw_awe_control(int dev, int cmd, unsigned char *event);
  303. static void awe_voice_change(int voice, fx_affect_func func);
  304. static void awe_sostenuto_on(int voice, int forced);
  305. static void awe_sustain_off(int voice, int forced);
  306. static void awe_terminate_and_init(int voice, int forced);
  307. /* voice search */
  308. static int awe_search_key(int bank, int preset, int note);
  309. static awe_voice_list *awe_search_instr(int bank, int preset, int note);
  310. static int awe_search_multi_voices(awe_voice_list *rec, int note, int velocity, awe_voice_info **vlist);
  311. static void awe_alloc_multi_voices(int ch, int note, int velocity, int key);
  312. static void awe_alloc_one_voice(int voice, int note, int velocity);
  313. static int awe_clear_voice(void);
  314. /* load / remove patches */
  315. static int awe_open_patch(awe_patch_info *patch, const char __user *addr, int count);
  316. static int awe_close_patch(awe_patch_info *patch, const char __user *addr, int count);
  317. static int awe_unload_patch(awe_patch_info *patch, const char __user *addr, int count);
  318. static int awe_load_info(awe_patch_info *patch, const char __user *addr, int count);
  319. static int awe_remove_info(awe_patch_info *patch, const char __user *addr, int count);
  320. static int awe_load_data(awe_patch_info *patch, const char __user *addr, int count);
  321. static int awe_replace_data(awe_patch_info *patch, const char __user *addr, int count);
  322. static int awe_load_map(awe_patch_info *patch, const char __user *addr, int count);
  323. #ifdef AWE_HAS_GUS_COMPATIBILITY
  324. static int awe_load_guspatch(const char __user *addr, int offs, int size, int pmgr_flag);
  325. #endif
  326. /*static int awe_probe_info(awe_patch_info *patch, const char __user *addr, int count);*/
  327. static int awe_probe_data(awe_patch_info *patch, const char __user *addr, int count);
  328. static sf_list *check_patch_opened(int type, char *name);
  329. static int awe_write_wave_data(const char __user *addr, int offset, awe_sample_list *sp, int channels);
  330. static int awe_create_sf(int type, char *name);
  331. static void awe_free_sf(sf_list *sf);
  332. static void add_sf_info(sf_list *sf, awe_voice_list *rec);
  333. static void add_sf_sample(sf_list *sf, awe_sample_list *smp);
  334. static void purge_old_list(awe_voice_list *rec, awe_voice_list *next);
  335. static void add_info_list(awe_voice_list *rec);
  336. static void awe_remove_samples(int sf_id);
  337. static void rebuild_preset_list(void);
  338. static short awe_set_sample(awe_voice_list *rec);
  339. static awe_sample_list *search_sample_index(sf_list *sf, int sample);
  340. static int is_identical_holder(sf_list *sf1, sf_list *sf2);
  341. #ifdef AWE_ALLOW_SAMPLE_SHARING
  342. static int is_identical_name(unsigned char *name, sf_list *p);
  343. static int is_shared_sf(unsigned char *name);
  344. static int info_duplicated(sf_list *sf, awe_voice_list *rec);
  345. #endif /* allow sharing */
  346. /* lowlevel functions */
  347. static void awe_init_audio(void);
  348. static void awe_init_dma(void);
  349. static void awe_init_array(void);
  350. static void awe_send_array(unsigned short *data);
  351. static void awe_tweak_voice(int voice);
  352. static void awe_tweak(void);
  353. static void awe_init_fm(void);
  354. static int awe_open_dram_for_write(int offset, int channels);
  355. static void awe_open_dram_for_check(void);
  356. static void awe_close_dram(void);
  357. /*static void awe_write_dram(unsigned short c);*/
  358. static int awe_detect_base(int addr);
  359. static int awe_detect(void);
  360. static void awe_check_dram(void);
  361. static int awe_load_chorus_fx(awe_patch_info *patch, const char __user *addr, int count);
  362. static void awe_set_chorus_mode(int mode);
  363. static void awe_update_chorus_mode(void);
  364. static int awe_load_reverb_fx(awe_patch_info *patch, const char __user *addr, int count);
  365. static void awe_set_reverb_mode(int mode);
  366. static void awe_update_reverb_mode(void);
  367. static void awe_equalizer(int bass, int treble);
  368. static void awe_update_equalizer(void);
  369. #ifdef CONFIG_AWE32_MIXER
  370. static void attach_mixer(void);
  371. static void unload_mixer(void);
  372. #endif
  373. #ifdef CONFIG_AWE32_MIDIEMU
  374. static void attach_midiemu(void);
  375. static void unload_midiemu(void);
  376. #endif
  377. #define limitvalue(x, a, b) if ((x) < (a)) (x) = (a); else if ((x) > (b)) (x) = (b)
  378. /*
  379. * control parameters
  380. */
  381. #ifdef AWE_USE_NEW_VOLUME_CALC
  382. #define DEF_VOLUME_CALC TRUE
  383. #else
  384. #define DEF_VOLUME_CALC FALSE
  385. #endif /* new volume */
  386. #define DEF_ZERO_ATTEN 32 /* 12dB below */
  387. #define DEF_MOD_SENSE 18
  388. #define DEF_CHORUS_MODE 2
  389. #define DEF_REVERB_MODE 4
  390. #define DEF_BASS_LEVEL 5
  391. #define DEF_TREBLE_LEVEL 9
  392. static struct CtrlParmsDef {
  393. int value;
  394. int init_each_time;
  395. void (*update)(void);
  396. } ctrl_parms[AWE_MD_END] = {
  397. {0,0, NULL}, {0,0, NULL}, /* <-- not used */
  398. {AWE_VERSION_NUMBER, FALSE, NULL},
  399. {TRUE, FALSE, NULL}, /* exclusive */
  400. {TRUE, FALSE, NULL}, /* realpan */
  401. {AWE_DEFAULT_BANK, FALSE, NULL}, /* gusbank */
  402. {FALSE, TRUE, NULL}, /* keep effect */
  403. {DEF_ZERO_ATTEN, FALSE, awe_update_volume}, /* zero_atten */
  404. {FALSE, FALSE, NULL}, /* chn_prior */
  405. {DEF_MOD_SENSE, FALSE, NULL}, /* modwheel sense */
  406. {AWE_DEFAULT_PRESET, FALSE, NULL}, /* def_preset */
  407. {AWE_DEFAULT_BANK, FALSE, NULL}, /* def_bank */
  408. {AWE_DEFAULT_DRUM, FALSE, NULL}, /* def_drum */
  409. {FALSE, FALSE, NULL}, /* toggle_drum_bank */
  410. {DEF_VOLUME_CALC, FALSE, awe_update_volume}, /* new_volume_calc */
  411. {DEF_CHORUS_MODE, FALSE, awe_update_chorus_mode}, /* chorus mode */
  412. {DEF_REVERB_MODE, FALSE, awe_update_reverb_mode}, /* reverb mode */
  413. {DEF_BASS_LEVEL, FALSE, awe_update_equalizer}, /* bass level */
  414. {DEF_TREBLE_LEVEL, FALSE, awe_update_equalizer}, /* treble level */
  415. {0, FALSE, NULL}, /* debug mode */
  416. {FALSE, FALSE, NULL}, /* pan exchange */
  417. };
  418. static int ctrls[AWE_MD_END];
  419. /*
  420. * synth operation table
  421. */
  422. static struct synth_operations awe_operations =
  423. {
  424. .owner = THIS_MODULE,
  425. .id = "EMU8K",
  426. .info = &awe_info,
  427. .midi_dev = 0,
  428. .synth_type = SYNTH_TYPE_SAMPLE,
  429. .synth_subtype = SAMPLE_TYPE_AWE32,
  430. .open = awe_open,
  431. .close = awe_close,
  432. .ioctl = awe_ioctl,
  433. .kill_note = awe_kill_note,
  434. .start_note = awe_start_note,
  435. .set_instr = awe_set_instr_2,
  436. .reset = awe_reset,
  437. .hw_control = awe_hw_control,
  438. .load_patch = awe_load_patch,
  439. .aftertouch = awe_aftertouch,
  440. .controller = awe_controller,
  441. .panning = awe_panning,
  442. .volume_method = awe_volume_method,
  443. .bender = awe_bender,
  444. .alloc_voice = awe_alloc,
  445. .setup_voice = awe_setup_voice
  446. };
  447. static void free_tables(void)
  448. {
  449. if (sftail) {
  450. sf_list *p, *prev;
  451. for (p = sftail; p; p = prev) {
  452. prev = p->prev;
  453. awe_free_sf(p);
  454. }
  455. }
  456. sfhead = sftail = NULL;
  457. }
  458. /*
  459. * clear sample tables
  460. */
  461. static void
  462. awe_reset_samples(void)
  463. {
  464. /* free all bank tables */
  465. memset(preset_table, 0, sizeof(preset_table));
  466. free_tables();
  467. current_sf_id = 0;
  468. locked_sf_id = 0;
  469. patch_opened = 0;
  470. }
  471. /*
  472. * EMU register access
  473. */
  474. /* select a given AWE32 pointer */
  475. static int awe_ports[5];
  476. static int port_setuped = FALSE;
  477. static int awe_cur_cmd = -1;
  478. #define awe_set_cmd(cmd) \
  479. if (awe_cur_cmd != cmd) { outw(cmd, awe_ports[Pointer]); awe_cur_cmd = cmd; }
  480. /* write 16bit data */
  481. static void
  482. awe_poke(unsigned short cmd, unsigned short port, unsigned short data)
  483. {
  484. awe_set_cmd(cmd);
  485. outw(data, awe_ports[port]);
  486. }
  487. /* write 32bit data */
  488. static void
  489. awe_poke_dw(unsigned short cmd, unsigned short port, unsigned int data)
  490. {
  491. unsigned short addr = awe_ports[port];
  492. awe_set_cmd(cmd);
  493. outw(data, addr); /* write lower 16 bits */
  494. outw(data >> 16, addr + 2); /* write higher 16 bits */
  495. }
  496. /* read 16bit data */
  497. static unsigned short
  498. awe_peek(unsigned short cmd, unsigned short port)
  499. {
  500. unsigned short k;
  501. awe_set_cmd(cmd);
  502. k = inw(awe_ports[port]);
  503. return k;
  504. }
  505. /* read 32bit data */
  506. static unsigned int
  507. awe_peek_dw(unsigned short cmd, unsigned short port)
  508. {
  509. unsigned int k1, k2;
  510. unsigned short addr = awe_ports[port];
  511. awe_set_cmd(cmd);
  512. k1 = inw(addr);
  513. k2 = inw(addr + 2);
  514. k1 |= k2 << 16;
  515. return k1;
  516. }
  517. /* wait delay number of AWE32 44100Hz clocks */
  518. #ifdef WAIT_BY_LOOP /* wait by loop -- that's not good.. */
  519. static void
  520. awe_wait(unsigned short delay)
  521. {
  522. unsigned short clock, target;
  523. unsigned short port = awe_ports[AWE_WC_Port];
  524. int counter;
  525. /* sample counter */
  526. awe_set_cmd(AWE_WC_Cmd);
  527. clock = (unsigned short)inw(port);
  528. target = clock + delay;
  529. counter = 0;
  530. if (target < clock) {
  531. for (; (unsigned short)inw(port) > target; counter++)
  532. if (counter > 65536)
  533. break;
  534. }
  535. for (; (unsigned short)inw(port) < target; counter++)
  536. if (counter > 65536)
  537. break;
  538. }
  539. #else
  540. static void awe_wait(unsigned short delay)
  541. {
  542. current->state = TASK_INTERRUPTIBLE;
  543. schedule_timeout((HZ*(unsigned long)delay + 44099)/44100);
  544. }
  545. /*
  546. static void awe_wait(unsigned short delay)
  547. {
  548. udelay(((unsigned long)delay * 1000000L + 44099) / 44100);
  549. }
  550. */
  551. #endif /* wait by loop */
  552. /* write a word data */
  553. #define awe_write_dram(c) awe_poke(AWE_SMLD, c)
  554. /*
  555. * AWE32 voice parameters
  556. */
  557. /* initialize voice_info record */
  558. static void
  559. awe_init_voice_info(awe_voice_info *vp)
  560. {
  561. vp->sample = 0;
  562. vp->rate_offset = 0;
  563. vp->start = 0;
  564. vp->end = 0;
  565. vp->loopstart = 0;
  566. vp->loopend = 0;
  567. vp->mode = 0;
  568. vp->root = 60;
  569. vp->tune = 0;
  570. vp->low = 0;
  571. vp->high = 127;
  572. vp->vellow = 0;
  573. vp->velhigh = 127;
  574. vp->fixkey = -1;
  575. vp->fixvel = -1;
  576. vp->fixpan = -1;
  577. vp->pan = -1;
  578. vp->exclusiveClass = 0;
  579. vp->amplitude = 127;
  580. vp->attenuation = 0;
  581. vp->scaleTuning = 100;
  582. awe_init_voice_parm(&vp->parm);
  583. }
  584. /* initialize voice_parm record:
  585. * Env1/2: delay=0, attack=0, hold=0, sustain=0, decay=0, release=0.
  586. * Vibrato and Tremolo effects are zero.
  587. * Cutoff is maximum.
  588. * Chorus and Reverb effects are zero.
  589. */
  590. static void
  591. awe_init_voice_parm(awe_voice_parm *pp)
  592. {
  593. pp->moddelay = 0x8000;
  594. pp->modatkhld = 0x7f7f;
  595. pp->moddcysus = 0x7f7f;
  596. pp->modrelease = 0x807f;
  597. pp->modkeyhold = 0;
  598. pp->modkeydecay = 0;
  599. pp->voldelay = 0x8000;
  600. pp->volatkhld = 0x7f7f;
  601. pp->voldcysus = 0x7f7f;
  602. pp->volrelease = 0x807f;
  603. pp->volkeyhold = 0;
  604. pp->volkeydecay = 0;
  605. pp->lfo1delay = 0x8000;
  606. pp->lfo2delay = 0x8000;
  607. pp->pefe = 0;
  608. pp->fmmod = 0;
  609. pp->tremfrq = 0;
  610. pp->fm2frq2 = 0;
  611. pp->cutoff = 0xff;
  612. pp->filterQ = 0;
  613. pp->chorus = 0;
  614. pp->reverb = 0;
  615. }
  616. #ifdef AWE_HAS_GUS_COMPATIBILITY
  617. /* convert frequency mHz to abstract cents (= midi key * 100) */
  618. static int
  619. freq_to_note(int mHz)
  620. {
  621. /* abscents = log(mHz/8176) / log(2) * 1200 */
  622. unsigned int max_val = (unsigned int)0xffffffff / 10000;
  623. int i, times;
  624. unsigned int base;
  625. unsigned int freq;
  626. int note, tune;
  627. if (mHz == 0)
  628. return 0;
  629. if (mHz < 0)
  630. return 12799; /* maximum */
  631. freq = mHz;
  632. note = 0;
  633. for (base = 8176 * 2; freq >= base; base *= 2) {
  634. note += 12;
  635. if (note >= 128) /* over maximum */
  636. return 12799;
  637. }
  638. base /= 2;
  639. /* to avoid overflow... */
  640. times = 10000;
  641. while (freq > max_val) {
  642. max_val *= 10;
  643. times /= 10;
  644. base /= 10;
  645. }
  646. freq = freq * times / base;
  647. for (i = 0; i < 12; i++) {
  648. if (freq < semitone_tuning[i+1])
  649. break;
  650. note++;
  651. }
  652. tune = 0;
  653. freq = freq * 10000 / semitone_tuning[i];
  654. for (i = 0; i < 100; i++) {
  655. if (freq < cent_tuning[i+1])
  656. break;
  657. tune++;
  658. }
  659. return note * 100 + tune;
  660. }
  661. /* convert Hz to AWE32 rate offset:
  662. * sample pitch offset for the specified sample rate
  663. * rate=44100 is no offset, each 4096 is 1 octave (twice).
  664. * eg, when rate is 22050, this offset becomes -4096.
  665. */
  666. static int
  667. calc_rate_offset(int Hz)
  668. {
  669. /* offset = log(Hz / 44100) / log(2) * 4096 */
  670. int freq, base, i;
  671. /* maybe smaller than max (44100Hz) */
  672. if (Hz <= 0 || Hz >= 44100) return 0;
  673. base = 0;
  674. for (freq = Hz * 2; freq < 44100; freq *= 2)
  675. base++;
  676. base *= 1200;
  677. freq = 44100 * 10000 / (freq/2);
  678. for (i = 0; i < 12; i++) {
  679. if (freq < semitone_tuning[i+1])
  680. break;
  681. base += 100;
  682. }
  683. freq = freq * 10000 / semitone_tuning[i];
  684. for (i = 0; i < 100; i++) {
  685. if (freq < cent_tuning[i+1])
  686. break;
  687. base++;
  688. }
  689. return -base * 4096 / 1200;
  690. }
  691. /*
  692. * convert envelope time parameter to AWE32 raw parameter
  693. */
  694. /* attack & decay/release time table (msec) */
  695. static short attack_time_tbl[128] = {
  696. 32767, 32767, 5989, 4235, 2994, 2518, 2117, 1780, 1497, 1373, 1259, 1154, 1058, 970, 890, 816,
  697. 707, 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377,
  698. 361, 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188,
  699. 180, 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94,
  700. 90, 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47,
  701. 45, 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23,
  702. 22, 21, 20, 19, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
  703. 11, 11, 10, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 6, 0,
  704. };
  705. static short decay_time_tbl[128] = {
  706. 32767, 32767, 22614, 15990, 11307, 9508, 7995, 6723, 5653, 5184, 4754, 4359, 3997, 3665, 3361, 3082,
  707. 2828, 2765, 2648, 2535, 2428, 2325, 2226, 2132, 2042, 1955, 1872, 1793, 1717, 1644, 1574, 1507,
  708. 1443, 1382, 1324, 1267, 1214, 1162, 1113, 1066, 978, 936, 897, 859, 822, 787, 754, 722,
  709. 691, 662, 634, 607, 581, 557, 533, 510, 489, 468, 448, 429, 411, 393, 377, 361,
  710. 345, 331, 317, 303, 290, 278, 266, 255, 244, 234, 224, 214, 205, 196, 188, 180,
  711. 172, 165, 158, 151, 145, 139, 133, 127, 122, 117, 112, 107, 102, 98, 94, 90,
  712. 86, 82, 79, 75, 72, 69, 66, 63, 61, 58, 56, 53, 51, 49, 47, 45,
  713. 43, 41, 39, 37, 36, 34, 33, 31, 30, 29, 28, 26, 25, 24, 23, 22,
  714. };
  715. #define calc_parm_delay(msec) (0x8000 - (msec) * 1000 / 725);
  716. /* delay time = 0x8000 - msec/92 */
  717. static int
  718. calc_parm_hold(int msec)
  719. {
  720. int val = (0x7f * 92 - msec) / 92;
  721. if (val < 1) val = 1;
  722. if (val > 127) val = 127;
  723. return val;
  724. }
  725. /* attack time: search from time table */
  726. static int
  727. calc_parm_attack(int msec)
  728. {
  729. return calc_parm_search(msec, attack_time_tbl);
  730. }
  731. /* decay/release time: search from time table */
  732. static int
  733. calc_parm_decay(int msec)
  734. {
  735. return calc_parm_search(msec, decay_time_tbl);
  736. }
  737. /* search an index for specified time from given time table */
  738. static int
  739. calc_parm_search(int msec, short *table)
  740. {
  741. int left = 1, right = 127, mid;
  742. while (left < right) {
  743. mid = (left + right) / 2;
  744. if (msec < (int)table[mid])
  745. left = mid + 1;
  746. else
  747. right = mid;
  748. }
  749. return left;
  750. }
  751. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  752. /*
  753. * effects table
  754. */
  755. /* set an effect value */
  756. #define FX_FLAG_OFF 0
  757. #define FX_FLAG_SET 1
  758. #define FX_FLAG_ADD 2
  759. #define FX_SET(rec,type,value) \
  760. ((rec)->flags[type] = FX_FLAG_SET, (rec)->val[type] = (value))
  761. #define FX_ADD(rec,type,value) \
  762. ((rec)->flags[type] = FX_FLAG_ADD, (rec)->val[type] = (value))
  763. #define FX_UNSET(rec,type) \
  764. ((rec)->flags[type] = FX_FLAG_OFF, (rec)->val[type] = 0)
  765. /* check the effect value is set */
  766. #define FX_ON(rec,type) ((rec)->flags[type])
  767. #define PARM_BYTE 0
  768. #define PARM_WORD 1
  769. #define PARM_SIGN 2
  770. static struct PARM_DEFS {
  771. int type; /* byte or word */
  772. int low, high; /* value range */
  773. fx_affect_func realtime; /* realtime paramater change */
  774. } parm_defs[] = {
  775. {PARM_WORD, 0, 0x8000, NULL}, /* env1 delay */
  776. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 attack */
  777. {PARM_BYTE, 0, 0x7e, NULL}, /* env1 hold */
  778. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 decay */
  779. {PARM_BYTE, 1, 0x7f, NULL}, /* env1 release */
  780. {PARM_BYTE, 0, 0x7f, NULL}, /* env1 sustain */
  781. {PARM_BYTE, 0, 0xff, NULL}, /* env1 pitch */
  782. {PARM_BYTE, 0, 0xff, NULL}, /* env1 cutoff */
  783. {PARM_WORD, 0, 0x8000, NULL}, /* env2 delay */
  784. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 attack */
  785. {PARM_BYTE, 0, 0x7e, NULL}, /* env2 hold */
  786. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 decay */
  787. {PARM_BYTE, 1, 0x7f, NULL}, /* env2 release */
  788. {PARM_BYTE, 0, 0x7f, NULL}, /* env2 sustain */
  789. {PARM_WORD, 0, 0x8000, NULL}, /* lfo1 delay */
  790. {PARM_BYTE, 0, 0xff, awe_fx_tremfrq}, /* lfo1 freq */
  791. {PARM_SIGN, -128, 127, awe_fx_tremfrq}, /* lfo1 volume */
  792. {PARM_SIGN, -128, 127, awe_fx_fmmod}, /* lfo1 pitch */
  793. {PARM_BYTE, 0, 0xff, awe_fx_fmmod}, /* lfo1 cutoff */
  794. {PARM_WORD, 0, 0x8000, NULL}, /* lfo2 delay */
  795. {PARM_BYTE, 0, 0xff, awe_fx_fm2frq2}, /* lfo2 freq */
  796. {PARM_SIGN, -128, 127, awe_fx_fm2frq2}, /* lfo2 pitch */
  797. {PARM_WORD, 0, 0xffff, awe_set_voice_pitch}, /* initial pitch */
  798. {PARM_BYTE, 0, 0xff, NULL}, /* chorus */
  799. {PARM_BYTE, 0, 0xff, NULL}, /* reverb */
  800. {PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial cutoff */
  801. {PARM_BYTE, 0, 15, awe_fx_filterQ}, /* initial resonance */
  802. {PARM_WORD, 0, 0xffff, NULL}, /* sample start */
  803. {PARM_WORD, 0, 0xffff, NULL}, /* loop start */
  804. {PARM_WORD, 0, 0xffff, NULL}, /* loop end */
  805. {PARM_WORD, 0, 0xffff, NULL}, /* coarse sample start */
  806. {PARM_WORD, 0, 0xffff, NULL}, /* coarse loop start */
  807. {PARM_WORD, 0, 0xffff, NULL}, /* coarse loop end */
  808. {PARM_BYTE, 0, 0xff, awe_set_volume}, /* initial attenuation */
  809. };
  810. static unsigned char
  811. FX_BYTE(FX_Rec *rec, FX_Rec *lay, int type, unsigned char value)
  812. {
  813. int effect = 0;
  814. int on = 0;
  815. if (lay && (on = FX_ON(lay, type)) != 0)
  816. effect = lay->val[type];
  817. if (!on && (on = FX_ON(rec, type)) != 0)
  818. effect = rec->val[type];
  819. if (on == FX_FLAG_ADD) {
  820. if (parm_defs[type].type == PARM_SIGN) {
  821. if (value > 0x7f)
  822. effect += (int)value - 0x100;
  823. else
  824. effect += (int)value;
  825. } else {
  826. effect += (int)value;
  827. }
  828. }
  829. if (on) {
  830. if (effect < parm_defs[type].low)
  831. effect = parm_defs[type].low;
  832. else if (effect > parm_defs[type].high)
  833. effect = parm_defs[type].high;
  834. return (unsigned char)effect;
  835. }
  836. return value;
  837. }
  838. /* get word effect value */
  839. static unsigned short
  840. FX_WORD(FX_Rec *rec, FX_Rec *lay, int type, unsigned short value)
  841. {
  842. int effect = 0;
  843. int on = 0;
  844. if (lay && (on = FX_ON(lay, type)) != 0)
  845. effect = lay->val[type];
  846. if (!on && (on = FX_ON(rec, type)) != 0)
  847. effect = rec->val[type];
  848. if (on == FX_FLAG_ADD)
  849. effect += (int)value;
  850. if (on) {
  851. if (effect < parm_defs[type].low)
  852. effect = parm_defs[type].low;
  853. else if (effect > parm_defs[type].high)
  854. effect = parm_defs[type].high;
  855. return (unsigned short)effect;
  856. }
  857. return value;
  858. }
  859. /* get word (upper=type1/lower=type2) effect value */
  860. static unsigned short
  861. FX_COMB(FX_Rec *rec, FX_Rec *lay, int type1, int type2, unsigned short value)
  862. {
  863. unsigned short tmp;
  864. tmp = FX_BYTE(rec, lay, type1, (unsigned char)(value >> 8));
  865. tmp <<= 8;
  866. tmp |= FX_BYTE(rec, lay, type2, (unsigned char)(value & 0xff));
  867. return tmp;
  868. }
  869. /* address offset */
  870. static int
  871. FX_OFFSET(FX_Rec *rec, FX_Rec *lay, int lo, int hi, int mode)
  872. {
  873. int addr = 0;
  874. if (lay && FX_ON(lay, hi))
  875. addr = (short)lay->val[hi];
  876. else if (FX_ON(rec, hi))
  877. addr = (short)rec->val[hi];
  878. addr = addr << 15;
  879. if (lay && FX_ON(lay, lo))
  880. addr += (short)lay->val[lo];
  881. else if (FX_ON(rec, lo))
  882. addr += (short)rec->val[lo];
  883. if (!(mode & AWE_SAMPLE_8BITS))
  884. addr /= 2;
  885. return addr;
  886. }
  887. /*
  888. * turn on/off sample
  889. */
  890. /* table for volume target calculation */
  891. static unsigned short voltarget[16] = {
  892. 0xEAC0, 0XE0C8, 0XD740, 0XCE20, 0XC560, 0XBD08, 0XB500, 0XAD58,
  893. 0XA5F8, 0X9EF0, 0X9830, 0X91C0, 0X8B90, 0X85A8, 0X8000, 0X7A90
  894. };
  895. static void
  896. awe_note_on(int voice)
  897. {
  898. unsigned int temp;
  899. int addr;
  900. int vtarget, ftarget, ptarget, pitch;
  901. awe_voice_info *vp;
  902. awe_voice_parm_block *parm;
  903. FX_Rec *fx = &voices[voice].cinfo->fx;
  904. FX_Rec *fx_lay = NULL;
  905. if (voices[voice].layer < MAX_LAYERS)
  906. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  907. /* A voice sample must assigned before calling */
  908. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  909. return;
  910. parm = (awe_voice_parm_block*)&vp->parm;
  911. /* channel to be silent and idle */
  912. awe_poke(AWE_DCYSUSV(voice), 0x0080);
  913. awe_poke(AWE_VTFT(voice), 0x0000FFFF);
  914. awe_poke(AWE_CVCF(voice), 0x0000FFFF);
  915. awe_poke(AWE_PTRX(voice), 0);
  916. awe_poke(AWE_CPF(voice), 0);
  917. /* set pitch offset */
  918. awe_set_pitch(voice, TRUE);
  919. /* modulation & volume envelope */
  920. if (parm->modatk >= 0x80 && parm->moddelay >= 0x8000) {
  921. awe_poke(AWE_ENVVAL(voice), 0xBFFF);
  922. pitch = (parm->env1pit<<4) + voices[voice].apitch;
  923. if (pitch > 0xffff) pitch = 0xffff;
  924. /* calculate filter target */
  925. ftarget = parm->cutoff + parm->env1fc;
  926. limitvalue(ftarget, 0, 255);
  927. ftarget <<= 8;
  928. } else {
  929. awe_poke(AWE_ENVVAL(voice),
  930. FX_WORD(fx, fx_lay, AWE_FX_ENV1_DELAY, parm->moddelay));
  931. ftarget = parm->cutoff;
  932. ftarget <<= 8;
  933. pitch = voices[voice].apitch;
  934. }
  935. /* calcualte pitch target */
  936. if (pitch != 0xffff) {
  937. ptarget = 1 << (pitch >> 12);
  938. if (pitch & 0x800) ptarget += (ptarget*0x102e)/0x2710;
  939. if (pitch & 0x400) ptarget += (ptarget*0x764)/0x2710;
  940. if (pitch & 0x200) ptarget += (ptarget*0x389)/0x2710;
  941. ptarget += (ptarget>>1);
  942. if (ptarget > 0xffff) ptarget = 0xffff;
  943. } else ptarget = 0xffff;
  944. if (parm->modatk >= 0x80)
  945. awe_poke(AWE_ATKHLD(voice),
  946. FX_BYTE(fx, fx_lay, AWE_FX_ENV1_HOLD, parm->modhld) << 8 | 0x7f);
  947. else
  948. awe_poke(AWE_ATKHLD(voice),
  949. FX_COMB(fx, fx_lay, AWE_FX_ENV1_HOLD, AWE_FX_ENV1_ATTACK,
  950. vp->parm.modatkhld));
  951. awe_poke(AWE_DCYSUS(voice),
  952. FX_COMB(fx, fx_lay, AWE_FX_ENV1_SUSTAIN, AWE_FX_ENV1_DECAY,
  953. vp->parm.moddcysus));
  954. if (parm->volatk >= 0x80 && parm->voldelay >= 0x8000) {
  955. awe_poke(AWE_ENVVOL(voice), 0xBFFF);
  956. vtarget = voltarget[voices[voice].avol%0x10]>>(voices[voice].avol>>4);
  957. } else {
  958. awe_poke(AWE_ENVVOL(voice),
  959. FX_WORD(fx, fx_lay, AWE_FX_ENV2_DELAY, vp->parm.voldelay));
  960. vtarget = 0;
  961. }
  962. if (parm->volatk >= 0x80)
  963. awe_poke(AWE_ATKHLDV(voice),
  964. FX_BYTE(fx, fx_lay, AWE_FX_ENV2_HOLD, parm->volhld) << 8 | 0x7f);
  965. else
  966. awe_poke(AWE_ATKHLDV(voice),
  967. FX_COMB(fx, fx_lay, AWE_FX_ENV2_HOLD, AWE_FX_ENV2_ATTACK,
  968. vp->parm.volatkhld));
  969. /* decay/sustain parameter for volume envelope must be set at last */
  970. /* cutoff and volume */
  971. awe_set_volume(voice, TRUE);
  972. /* modulation envelope heights */
  973. awe_poke(AWE_PEFE(voice),
  974. FX_COMB(fx, fx_lay, AWE_FX_ENV1_PITCH, AWE_FX_ENV1_CUTOFF,
  975. vp->parm.pefe));
  976. /* lfo1/2 delay */
  977. awe_poke(AWE_LFO1VAL(voice),
  978. FX_WORD(fx, fx_lay, AWE_FX_LFO1_DELAY, vp->parm.lfo1delay));
  979. awe_poke(AWE_LFO2VAL(voice),
  980. FX_WORD(fx, fx_lay, AWE_FX_LFO2_DELAY, vp->parm.lfo2delay));
  981. /* lfo1 pitch & cutoff shift */
  982. awe_fx_fmmod(voice, TRUE);
  983. /* lfo1 volume & freq */
  984. awe_fx_tremfrq(voice, TRUE);
  985. /* lfo2 pitch & freq */
  986. awe_fx_fm2frq2(voice, TRUE);
  987. /* pan & loop start */
  988. awe_set_pan(voice, TRUE);
  989. /* chorus & loop end (chorus 8bit, MSB) */
  990. addr = vp->loopend - 1;
  991. addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_END,
  992. AWE_FX_COARSE_LOOP_END, vp->mode);
  993. temp = FX_BYTE(fx, fx_lay, AWE_FX_CHORUS, vp->parm.chorus);
  994. temp = (temp <<24) | (unsigned int)addr;
  995. awe_poke_dw(AWE_CSL(voice), temp);
  996. DEBUG(4,printk("AWE32: [-- loopend=%x/%x]\n", vp->loopend, addr));
  997. /* Q & current address (Q 4bit value, MSB) */
  998. addr = vp->start - 1;
  999. addr += FX_OFFSET(fx, fx_lay, AWE_FX_SAMPLE_START,
  1000. AWE_FX_COARSE_SAMPLE_START, vp->mode);
  1001. temp = FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ);
  1002. temp = (temp<<28) | (unsigned int)addr;
  1003. awe_poke_dw(AWE_CCCA(voice), temp);
  1004. DEBUG(4,printk("AWE32: [-- startaddr=%x/%x]\n", vp->start, addr));
  1005. /* clear unknown registers */
  1006. awe_poke_dw(AWE_00A0(voice), 0);
  1007. awe_poke_dw(AWE_0080(voice), 0);
  1008. /* reset volume */
  1009. awe_poke_dw(AWE_VTFT(voice), (vtarget<<16)|ftarget);
  1010. awe_poke_dw(AWE_CVCF(voice), (vtarget<<16)|ftarget);
  1011. /* set reverb */
  1012. temp = FX_BYTE(fx, fx_lay, AWE_FX_REVERB, vp->parm.reverb);
  1013. temp = (temp << 8) | (ptarget << 16) | voices[voice].aaux;
  1014. awe_poke_dw(AWE_PTRX(voice), temp);
  1015. awe_poke_dw(AWE_CPF(voice), ptarget << 16);
  1016. /* turn on envelope */
  1017. awe_poke(AWE_DCYSUSV(voice),
  1018. FX_COMB(fx, fx_lay, AWE_FX_ENV2_SUSTAIN, AWE_FX_ENV2_DECAY,
  1019. vp->parm.voldcysus));
  1020. voices[voice].state = AWE_ST_ON;
  1021. /* clear voice position for the next note on this channel */
  1022. if (SINGLE_LAYER_MODE()) {
  1023. FX_UNSET(fx, AWE_FX_SAMPLE_START);
  1024. FX_UNSET(fx, AWE_FX_COARSE_SAMPLE_START);
  1025. }
  1026. }
  1027. /* turn off the voice */
  1028. static void
  1029. awe_note_off(int voice)
  1030. {
  1031. awe_voice_info *vp;
  1032. unsigned short tmp;
  1033. FX_Rec *fx = &voices[voice].cinfo->fx;
  1034. FX_Rec *fx_lay = NULL;
  1035. if (voices[voice].layer < MAX_LAYERS)
  1036. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1037. if ((vp = voices[voice].sample) == NULL) {
  1038. voices[voice].state = AWE_ST_OFF;
  1039. return;
  1040. }
  1041. tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV1_RELEASE,
  1042. (unsigned char)vp->parm.modrelease);
  1043. awe_poke(AWE_DCYSUS(voice), tmp);
  1044. tmp = 0x8000 | FX_BYTE(fx, fx_lay, AWE_FX_ENV2_RELEASE,
  1045. (unsigned char)vp->parm.volrelease);
  1046. awe_poke(AWE_DCYSUSV(voice), tmp);
  1047. voices[voice].state = AWE_ST_RELEASED;
  1048. }
  1049. /* force to terminate the voice (no releasing echo) */
  1050. static void
  1051. awe_terminate(int voice)
  1052. {
  1053. awe_poke(AWE_DCYSUSV(voice), 0x807F);
  1054. awe_tweak_voice(voice);
  1055. voices[voice].state = AWE_ST_OFF;
  1056. }
  1057. /* turn off other voices with the same exclusive class (for drums) */
  1058. static void
  1059. awe_exclusive_off(int voice)
  1060. {
  1061. int i, exclass;
  1062. if (voices[voice].sample == NULL)
  1063. return;
  1064. if ((exclass = voices[voice].sample->exclusiveClass) == 0)
  1065. return; /* not exclusive */
  1066. /* turn off voices with the same class */
  1067. for (i = 0; i < awe_max_voices; i++) {
  1068. if (i != voice && IS_PLAYING(i) &&
  1069. voices[i].sample && voices[i].ch == voices[voice].ch &&
  1070. voices[i].sample->exclusiveClass == exclass) {
  1071. DEBUG(4,printk("AWE32: [exoff(%d)]\n", i));
  1072. awe_terminate(i);
  1073. awe_voice_init(i, TRUE);
  1074. }
  1075. }
  1076. }
  1077. /*
  1078. * change the parameters of an audible voice
  1079. */
  1080. /* change pitch */
  1081. static void
  1082. awe_set_pitch(int voice, int forced)
  1083. {
  1084. if (IS_NO_EFFECT(voice) && !forced) return;
  1085. awe_poke(AWE_IP(voice), voices[voice].apitch);
  1086. DEBUG(3,printk("AWE32: [-- pitch=%x]\n", voices[voice].apitch));
  1087. }
  1088. /* calculate & change pitch */
  1089. static void
  1090. awe_set_voice_pitch(int voice, int forced)
  1091. {
  1092. awe_calc_pitch(voice);
  1093. awe_set_pitch(voice, forced);
  1094. }
  1095. /* change volume & cutoff */
  1096. static void
  1097. awe_set_volume(int voice, int forced)
  1098. {
  1099. awe_voice_info *vp;
  1100. unsigned short tmp2;
  1101. FX_Rec *fx = &voices[voice].cinfo->fx;
  1102. FX_Rec *fx_lay = NULL;
  1103. if (voices[voice].layer < MAX_LAYERS)
  1104. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1105. if (!IS_PLAYING(voice) && !forced) return;
  1106. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1107. return;
  1108. tmp2 = FX_BYTE(fx, fx_lay, AWE_FX_CUTOFF,
  1109. (unsigned char)voices[voice].acutoff);
  1110. tmp2 = (tmp2 << 8);
  1111. tmp2 |= FX_BYTE(fx, fx_lay, AWE_FX_ATTEN,
  1112. (unsigned char)voices[voice].avol);
  1113. awe_poke(AWE_IFATN(voice), tmp2);
  1114. }
  1115. /* calculate & change volume */
  1116. static void
  1117. awe_set_voice_vol(int voice, int forced)
  1118. {
  1119. if (IS_EMPTY(voice))
  1120. return;
  1121. awe_calc_volume(voice);
  1122. awe_set_volume(voice, forced);
  1123. }
  1124. /* change pan; this could make a click noise.. */
  1125. static void
  1126. awe_set_pan(int voice, int forced)
  1127. {
  1128. unsigned int temp;
  1129. int addr;
  1130. awe_voice_info *vp;
  1131. FX_Rec *fx = &voices[voice].cinfo->fx;
  1132. FX_Rec *fx_lay = NULL;
  1133. if (voices[voice].layer < MAX_LAYERS)
  1134. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1135. if (IS_NO_EFFECT(voice) && !forced) return;
  1136. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1137. return;
  1138. /* pan & loop start (pan 8bit, MSB, 0:right, 0xff:left) */
  1139. if (vp->fixpan > 0) /* 0-127 */
  1140. temp = 255 - (int)vp->fixpan * 2;
  1141. else {
  1142. int pos = 0;
  1143. if (vp->pan >= 0) /* 0-127 */
  1144. pos = (int)vp->pan * 2 - 128;
  1145. pos += voices[voice].cinfo->panning; /* -128 - 127 */
  1146. temp = 127 - pos;
  1147. }
  1148. limitvalue(temp, 0, 255);
  1149. if (ctrls[AWE_MD_PAN_EXCHANGE]) {
  1150. temp = 255 - temp;
  1151. }
  1152. if (forced || temp != voices[voice].apan) {
  1153. voices[voice].apan = temp;
  1154. if (temp == 0)
  1155. voices[voice].aaux = 0xff;
  1156. else
  1157. voices[voice].aaux = (-temp) & 0xff;
  1158. addr = vp->loopstart - 1;
  1159. addr += FX_OFFSET(fx, fx_lay, AWE_FX_LOOP_START,
  1160. AWE_FX_COARSE_LOOP_START, vp->mode);
  1161. temp = (temp<<24) | (unsigned int)addr;
  1162. awe_poke_dw(AWE_PSST(voice), temp);
  1163. DEBUG(4,printk("AWE32: [-- loopstart=%x/%x]\n", vp->loopstart, addr));
  1164. }
  1165. }
  1166. /* effects change during playing */
  1167. static void
  1168. awe_fx_fmmod(int voice, int forced)
  1169. {
  1170. awe_voice_info *vp;
  1171. FX_Rec *fx = &voices[voice].cinfo->fx;
  1172. FX_Rec *fx_lay = NULL;
  1173. if (voices[voice].layer < MAX_LAYERS)
  1174. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1175. if (IS_NO_EFFECT(voice) && !forced) return;
  1176. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1177. return;
  1178. awe_poke(AWE_FMMOD(voice),
  1179. FX_COMB(fx, fx_lay, AWE_FX_LFO1_PITCH, AWE_FX_LFO1_CUTOFF,
  1180. vp->parm.fmmod));
  1181. }
  1182. /* set tremolo (lfo1) volume & frequency */
  1183. static void
  1184. awe_fx_tremfrq(int voice, int forced)
  1185. {
  1186. awe_voice_info *vp;
  1187. FX_Rec *fx = &voices[voice].cinfo->fx;
  1188. FX_Rec *fx_lay = NULL;
  1189. if (voices[voice].layer < MAX_LAYERS)
  1190. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1191. if (IS_NO_EFFECT(voice) && !forced) return;
  1192. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1193. return;
  1194. awe_poke(AWE_TREMFRQ(voice),
  1195. FX_COMB(fx, fx_lay, AWE_FX_LFO1_VOLUME, AWE_FX_LFO1_FREQ,
  1196. vp->parm.tremfrq));
  1197. }
  1198. /* set lfo2 pitch & frequency */
  1199. static void
  1200. awe_fx_fm2frq2(int voice, int forced)
  1201. {
  1202. awe_voice_info *vp;
  1203. FX_Rec *fx = &voices[voice].cinfo->fx;
  1204. FX_Rec *fx_lay = NULL;
  1205. if (voices[voice].layer < MAX_LAYERS)
  1206. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1207. if (IS_NO_EFFECT(voice) && !forced) return;
  1208. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1209. return;
  1210. awe_poke(AWE_FM2FRQ2(voice),
  1211. FX_COMB(fx, fx_lay, AWE_FX_LFO2_PITCH, AWE_FX_LFO2_FREQ,
  1212. vp->parm.fm2frq2));
  1213. }
  1214. /* Q & current address (Q 4bit value, MSB) */
  1215. static void
  1216. awe_fx_filterQ(int voice, int forced)
  1217. {
  1218. unsigned int addr;
  1219. awe_voice_info *vp;
  1220. FX_Rec *fx = &voices[voice].cinfo->fx;
  1221. FX_Rec *fx_lay = NULL;
  1222. if (voices[voice].layer < MAX_LAYERS)
  1223. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1224. if (IS_NO_EFFECT(voice) && !forced) return;
  1225. if ((vp = voices[voice].sample) == NULL || vp->index == 0)
  1226. return;
  1227. addr = awe_peek_dw(AWE_CCCA(voice)) & 0xffffff;
  1228. addr |= (FX_BYTE(fx, fx_lay, AWE_FX_FILTERQ, vp->parm.filterQ) << 28);
  1229. awe_poke_dw(AWE_CCCA(voice), addr);
  1230. }
  1231. /*
  1232. * calculate pitch offset
  1233. *
  1234. * 0xE000 is no pitch offset at 44100Hz sample.
  1235. * Every 4096 is one octave.
  1236. */
  1237. static void
  1238. awe_calc_pitch(int voice)
  1239. {
  1240. voice_info *vp = &voices[voice];
  1241. awe_voice_info *ap;
  1242. awe_chan_info *cp = voices[voice].cinfo;
  1243. int offset;
  1244. /* search voice information */
  1245. if ((ap = vp->sample) == NULL)
  1246. return;
  1247. if (ap->index == 0) {
  1248. DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
  1249. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1250. return;
  1251. }
  1252. /* calculate offset */
  1253. if (ap->fixkey >= 0) {
  1254. DEBUG(3,printk("AWE32: p-> fixkey(%d) tune(%d)\n", ap->fixkey, ap->tune));
  1255. offset = (ap->fixkey - ap->root) * 4096 / 12;
  1256. } else {
  1257. DEBUG(3,printk("AWE32: p(%d)-> root(%d) tune(%d)\n", vp->note, ap->root, ap->tune));
  1258. offset = (vp->note - ap->root) * 4096 / 12;
  1259. DEBUG(4,printk("AWE32: p-> ofs=%d\n", offset));
  1260. }
  1261. offset = (offset * ap->scaleTuning) / 100;
  1262. DEBUG(4,printk("AWE32: p-> scale* ofs=%d\n", offset));
  1263. offset += ap->tune * 4096 / 1200;
  1264. DEBUG(4,printk("AWE32: p-> tune+ ofs=%d\n", offset));
  1265. if (cp->bender != 0) {
  1266. DEBUG(3,printk("AWE32: p-> bend(%d) %d\n", voice, cp->bender));
  1267. /* (819200: 1 semitone) ==> (4096: 12 semitones) */
  1268. offset += cp->bender * cp->bender_range / 2400;
  1269. }
  1270. /* add initial pitch correction */
  1271. if (FX_ON(&cp->fx_layer[vp->layer], AWE_FX_INIT_PITCH))
  1272. offset += cp->fx_layer[vp->layer].val[AWE_FX_INIT_PITCH];
  1273. else if (FX_ON(&cp->fx, AWE_FX_INIT_PITCH))
  1274. offset += cp->fx.val[AWE_FX_INIT_PITCH];
  1275. /* 0xe000: root pitch */
  1276. vp->apitch = 0xe000 + ap->rate_offset + offset;
  1277. DEBUG(4,printk("AWE32: p-> sum aofs=%x, rate_ofs=%d\n", vp->apitch, ap->rate_offset));
  1278. if (vp->apitch > 0xffff)
  1279. vp->apitch = 0xffff;
  1280. if (vp->apitch < 0)
  1281. vp->apitch = 0;
  1282. }
  1283. #ifdef AWE_HAS_GUS_COMPATIBILITY
  1284. /* calculate MIDI key and semitone from the specified frequency */
  1285. static void
  1286. awe_calc_pitch_from_freq(int voice, int freq)
  1287. {
  1288. voice_info *vp = &voices[voice];
  1289. awe_voice_info *ap;
  1290. FX_Rec *fx = &voices[voice].cinfo->fx;
  1291. FX_Rec *fx_lay = NULL;
  1292. int offset;
  1293. int note;
  1294. if (voices[voice].layer < MAX_LAYERS)
  1295. fx_lay = &voices[voice].cinfo->fx_layer[voices[voice].layer];
  1296. /* search voice information */
  1297. if ((ap = vp->sample) == NULL)
  1298. return;
  1299. if (ap->index == 0) {
  1300. DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
  1301. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1302. return;
  1303. }
  1304. note = freq_to_note(freq);
  1305. offset = (note - ap->root * 100 + ap->tune) * 4096 / 1200;
  1306. offset = (offset * ap->scaleTuning) / 100;
  1307. if (fx_lay && FX_ON(fx_lay, AWE_FX_INIT_PITCH))
  1308. offset += fx_lay->val[AWE_FX_INIT_PITCH];
  1309. else if (FX_ON(fx, AWE_FX_INIT_PITCH))
  1310. offset += fx->val[AWE_FX_INIT_PITCH];
  1311. vp->apitch = 0xe000 + ap->rate_offset + offset;
  1312. if (vp->apitch > 0xffff)
  1313. vp->apitch = 0xffff;
  1314. if (vp->apitch < 0)
  1315. vp->apitch = 0;
  1316. }
  1317. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  1318. /*
  1319. * calculate volume attenuation
  1320. *
  1321. * Voice volume is controlled by volume attenuation parameter.
  1322. * So volume becomes maximum when avol is 0 (no attenuation), and
  1323. * minimum when 255 (-96dB or silence).
  1324. */
  1325. static int vol_table[128] = {
  1326. 255,111,95,86,79,74,70,66,63,61,58,56,54,52,50,49,
  1327. 47,46,45,43,42,41,40,39,38,37,36,35,34,34,33,32,
  1328. 31,31,30,29,29,28,27,27,26,26,25,24,24,23,23,22,
  1329. 22,21,21,21,20,20,19,19,18,18,18,17,17,16,16,16,
  1330. 15,15,15,14,14,14,13,13,13,12,12,12,11,11,11,10,
  1331. 10,10,10,9,9,9,8,8,8,8,7,7,7,7,6,6,
  1332. 6,6,5,5,5,5,5,4,4,4,4,3,3,3,3,3,
  1333. 2,2,2,2,2,1,1,1,1,1,0,0,0,0,0,0,
  1334. };
  1335. /* tables for volume->attenuation calculation */
  1336. static unsigned char voltab1[128] = {
  1337. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1338. 0x63, 0x2b, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22,
  1339. 0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a,
  1340. 0x19, 0x19, 0x18, 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x14,
  1341. 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10,
  1342. 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0e, 0x0d,
  1343. 0x0d, 0x0d, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b,
  1344. 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09,
  1345. 0x08, 0x08, 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06,
  1346. 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x04,
  1347. 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02,
  1348. 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01,
  1349. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1350. };
  1351. static unsigned char voltab2[128] = {
  1352. 0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x2a,
  1353. 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x24, 0x23, 0x22, 0x21,
  1354. 0x21, 0x20, 0x1f, 0x1e, 0x1e, 0x1d, 0x1c, 0x1c, 0x1b, 0x1a,
  1355. 0x1a, 0x19, 0x19, 0x18, 0x18, 0x17, 0x16, 0x16, 0x15, 0x15,
  1356. 0x14, 0x14, 0x13, 0x13, 0x13, 0x12, 0x12, 0x11, 0x11, 0x10,
  1357. 0x10, 0x10, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d,
  1358. 0x0d, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b, 0x0a, 0x0a,
  1359. 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08,
  1360. 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  1361. 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  1362. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
  1363. 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01,
  1364. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
  1365. };
  1366. static unsigned char expressiontab[128] = {
  1367. 0x7f, 0x6c, 0x62, 0x5a, 0x54, 0x50, 0x4b, 0x48, 0x45, 0x42,
  1368. 0x40, 0x3d, 0x3b, 0x39, 0x38, 0x36, 0x34, 0x33, 0x31, 0x30,
  1369. 0x2f, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25,
  1370. 0x24, 0x24, 0x23, 0x22, 0x21, 0x21, 0x20, 0x1f, 0x1e, 0x1e,
  1371. 0x1d, 0x1d, 0x1c, 0x1b, 0x1b, 0x1a, 0x1a, 0x19, 0x18, 0x18,
  1372. 0x17, 0x17, 0x16, 0x16, 0x15, 0x15, 0x15, 0x14, 0x14, 0x13,
  1373. 0x13, 0x12, 0x12, 0x11, 0x11, 0x11, 0x10, 0x10, 0x0f, 0x0f,
  1374. 0x0f, 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0c, 0x0c, 0x0c,
  1375. 0x0b, 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x09, 0x09, 0x09, 0x09,
  1376. 0x08, 0x08, 0x08, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  1377. 0x06, 0x05, 0x05, 0x05, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03,
  1378. 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01,
  1379. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  1380. };
  1381. static void
  1382. awe_calc_volume(int voice)
  1383. {
  1384. voice_info *vp = &voices[voice];
  1385. awe_voice_info *ap;
  1386. awe_chan_info *cp = voices[voice].cinfo;
  1387. int vol;
  1388. /* search voice information */
  1389. if ((ap = vp->sample) == NULL)
  1390. return;
  1391. ap = vp->sample;
  1392. if (ap->index == 0) {
  1393. DEBUG(3,printk("AWE32: set sample (%d)\n", ap->sample));
  1394. if (awe_set_sample((awe_voice_list*)ap) == 0)
  1395. return;
  1396. }
  1397. if (ctrls[AWE_MD_NEW_VOLUME_CALC]) {
  1398. int main_vol = cp->main_vol * ap->amplitude / 127;
  1399. limitvalue(vp->velocity, 0, 127);
  1400. limitvalue(main_vol, 0, 127);
  1401. limitvalue(cp->expression_vol, 0, 127);
  1402. vol = voltab1[main_vol] + voltab2[vp->velocity];
  1403. vol = (vol * 8) / 3;
  1404. vol += ap->attenuation;
  1405. if (cp->expression_vol < 127)
  1406. vol += ((0x100 - vol) * expressiontab[cp->expression_vol])/128;
  1407. vol += atten_offset;
  1408. if (atten_relative)
  1409. vol += ctrls[AWE_MD_ZERO_ATTEN];
  1410. limitvalue(vol, 0, 255);
  1411. vp->avol = vol;
  1412. } else {
  1413. /* 0 - 127 */
  1414. vol = (vp->velocity * cp->main_vol * cp->expression_vol) / (127*127);
  1415. vol = vol * ap->amplitude / 127;
  1416. if (vol < 0) vol = 0;
  1417. if (vol > 127) vol = 127;
  1418. /* calc to attenuation */
  1419. vol = vol_table[vol];
  1420. vol += (int)ap->attenuation;
  1421. vol += atten_offset;
  1422. if (atten_relative)
  1423. vol += ctrls[AWE_MD_ZERO_ATTEN];
  1424. if (vol > 255) vol = 255;
  1425. vp->avol = vol;
  1426. }
  1427. if (cp->bank != AWE_DRUM_BANK && ((awe_voice_parm_block*)(&ap->parm))->volatk < 0x7d) {
  1428. int atten;
  1429. if (vp->velocity < 70) atten = 70;
  1430. else atten = vp->velocity;
  1431. vp->acutoff = (atten * ap->parm.cutoff + 0xa0) >> 7;
  1432. } else {
  1433. vp->acutoff = ap->parm.cutoff;
  1434. }
  1435. DEBUG(3,printk("AWE32: [-- voice(%d) vol=%x]\n", voice, vol));
  1436. }
  1437. /* change master volume */
  1438. static void
  1439. awe_change_master_volume(short val)
  1440. {
  1441. limitvalue(val, 0, 127);
  1442. atten_offset = vol_table[val];
  1443. atten_relative = TRUE;
  1444. awe_update_volume();
  1445. }
  1446. /* update volumes of all available channels */
  1447. static void awe_update_volume(void)
  1448. {
  1449. int i;
  1450. for (i = 0; i < awe_max_voices; i++)
  1451. awe_set_voice_vol(i, TRUE);
  1452. }
  1453. /* set sostenuto on */
  1454. static void awe_sostenuto_on(int voice, int forced)
  1455. {
  1456. if (IS_NO_EFFECT(voice) && !forced) return;
  1457. voices[voice].sostenuto = 127;
  1458. }
  1459. /* drop sustain */
  1460. static void awe_sustain_off(int voice, int forced)
  1461. {
  1462. if (voices[voice].state == AWE_ST_SUSTAINED) {
  1463. awe_note_off(voice);
  1464. awe_fx_init(voices[voice].ch);
  1465. awe_voice_init(voice, FALSE);
  1466. }
  1467. }
  1468. /* terminate and initialize voice */
  1469. static void awe_terminate_and_init(int voice, int forced)
  1470. {
  1471. awe_terminate(voice);
  1472. awe_fx_init(voices[voice].ch);
  1473. awe_voice_init(voice, TRUE);
  1474. }
  1475. /*
  1476. * synth operation routines
  1477. */
  1478. #define AWE_VOICE_KEY(v) (0x8000 | (v))
  1479. #define AWE_CHAN_KEY(c,n) (((c) << 8) | ((n) + 1))
  1480. #define KEY_CHAN_MATCH(key,c) (((key) >> 8) == (c))
  1481. /* initialize the voice */
  1482. static void
  1483. awe_voice_init(int voice, int init_all)
  1484. {
  1485. voice_info *vp = &voices[voice];
  1486. /* reset voice search key */
  1487. if (playing_mode == AWE_PLAY_DIRECT)
  1488. vp->key = AWE_VOICE_KEY(voice);
  1489. else
  1490. vp->key = 0;
  1491. /* clear voice mapping */
  1492. voice_alloc->map[voice] = 0;
  1493. /* touch the timing flag */
  1494. vp->time = current_alloc_time;
  1495. /* initialize other parameters if necessary */
  1496. if (init_all) {
  1497. vp->note = -1;
  1498. vp->velocity = 0;
  1499. vp->sostenuto = 0;
  1500. vp->sample = NULL;
  1501. vp->cinfo = &channels[voice];
  1502. vp->ch = voice;
  1503. vp->state = AWE_ST_OFF;
  1504. /* emu8000 parameters */
  1505. vp->apitch = 0;
  1506. vp->avol = 255;
  1507. vp->apan = -1;
  1508. }
  1509. }
  1510. /* clear effects */
  1511. static void awe_fx_init(int ch)
  1512. {
  1513. if (SINGLE_LAYER_MODE() && !ctrls[AWE_MD_KEEP_EFFECT]) {
  1514. memset(&channels[ch].fx, 0, sizeof(channels[ch].fx));
  1515. memset(&channels[ch].fx_layer, 0, sizeof(&channels[ch].fx_layer));
  1516. }
  1517. }
  1518. /* initialize channel info */
  1519. static void awe_channel_init(int ch, int init_all)
  1520. {
  1521. awe_chan_info *cp = &channels[ch];
  1522. cp->channel = ch;
  1523. if (init_all) {
  1524. cp->panning = 0; /* zero center */
  1525. cp->bender_range = 200; /* sense * 100 */
  1526. cp->main_vol = 127;
  1527. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch)) {
  1528. cp->instr = ctrls[AWE_MD_DEF_DRUM];
  1529. cp->bank = AWE_DRUM_BANK;
  1530. } else {
  1531. cp->instr = ctrls[AWE_MD_DEF_PRESET];
  1532. cp->bank = ctrls[AWE_MD_DEF_BANK];
  1533. }
  1534. }
  1535. cp->bender = 0; /* zero tune skew */
  1536. cp->expression_vol = 127;
  1537. cp->chan_press = 0;
  1538. cp->sustained = 0;
  1539. if (! ctrls[AWE_MD_KEEP_EFFECT]) {
  1540. memset(&cp->fx, 0, sizeof(cp->fx));
  1541. memset(&cp->fx_layer, 0, sizeof(cp->fx_layer));
  1542. }
  1543. }
  1544. /* change the voice parameters; voice = channel */
  1545. static void awe_voice_change(int voice, fx_affect_func func)
  1546. {
  1547. int i;
  1548. switch (playing_mode) {
  1549. case AWE_PLAY_DIRECT:
  1550. func(voice, FALSE);
  1551. break;
  1552. case AWE_PLAY_INDIRECT:
  1553. for (i = 0; i < awe_max_voices; i++)
  1554. if (voices[i].key == AWE_VOICE_KEY(voice))
  1555. func(i, FALSE);
  1556. break;
  1557. default:
  1558. for (i = 0; i < awe_max_voices; i++)
  1559. if (KEY_CHAN_MATCH(voices[i].key, voice))
  1560. func(i, FALSE);
  1561. break;
  1562. }
  1563. }
  1564. /*
  1565. * device open / close
  1566. */
  1567. /* open device:
  1568. * reset status of all voices, and clear sample position flag
  1569. */
  1570. static int
  1571. awe_open(int dev, int mode)
  1572. {
  1573. if (awe_busy)
  1574. return -EBUSY;
  1575. awe_busy = TRUE;
  1576. /* set default mode */
  1577. awe_init_ctrl_parms(FALSE);
  1578. atten_relative = TRUE;
  1579. atten_offset = 0;
  1580. drum_flags = DEFAULT_DRUM_FLAGS;
  1581. playing_mode = AWE_PLAY_INDIRECT;
  1582. /* reset voices & channels */
  1583. awe_reset(dev);
  1584. patch_opened = 0;
  1585. return 0;
  1586. }
  1587. /* close device:
  1588. * reset all voices again (terminate sounds)
  1589. */
  1590. static void
  1591. awe_close(int dev)
  1592. {
  1593. awe_reset(dev);
  1594. awe_busy = FALSE;
  1595. }
  1596. /* set miscellaneous mode parameters
  1597. */
  1598. static void
  1599. awe_init_ctrl_parms(int init_all)
  1600. {
  1601. int i;
  1602. for (i = 0; i < AWE_MD_END; i++) {
  1603. if (init_all || ctrl_parms[i].init_each_time)
  1604. ctrls[i] = ctrl_parms[i].value;
  1605. }
  1606. }
  1607. /* sequencer I/O control:
  1608. */
  1609. static int
  1610. awe_ioctl(int dev, unsigned int cmd, void __user *arg)
  1611. {
  1612. switch (cmd) {
  1613. case SNDCTL_SYNTH_INFO:
  1614. if (playing_mode == AWE_PLAY_DIRECT)
  1615. awe_info.nr_voices = awe_max_voices;
  1616. else
  1617. awe_info.nr_voices = AWE_MAX_CHANNELS;
  1618. if (copy_to_user(arg, &awe_info, sizeof(awe_info)))
  1619. return -EFAULT;
  1620. return 0;
  1621. break;
  1622. case SNDCTL_SEQ_RESETSAMPLES:
  1623. awe_reset(dev);
  1624. awe_reset_samples();
  1625. return 0;
  1626. break;
  1627. case SNDCTL_SEQ_PERCMODE:
  1628. /* what's this? */
  1629. return 0;
  1630. break;
  1631. case SNDCTL_SYNTH_MEMAVL:
  1632. return memsize - awe_free_mem_ptr() * 2;
  1633. break;
  1634. default:
  1635. printk(KERN_WARNING "AWE32: unsupported ioctl %d\n", cmd);
  1636. return -EINVAL;
  1637. break;
  1638. }
  1639. }
  1640. static int voice_in_range(int voice)
  1641. {
  1642. if (playing_mode == AWE_PLAY_DIRECT) {
  1643. if (voice < 0 || voice >= awe_max_voices)
  1644. return FALSE;
  1645. } else {
  1646. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1647. return FALSE;
  1648. }
  1649. return TRUE;
  1650. }
  1651. static void release_voice(int voice, int do_sustain)
  1652. {
  1653. if (IS_NO_SOUND(voice))
  1654. return;
  1655. if (do_sustain && (voices[voice].cinfo->sustained == 127 ||
  1656. voices[voice].sostenuto == 127))
  1657. voices[voice].state = AWE_ST_SUSTAINED;
  1658. else {
  1659. awe_note_off(voice);
  1660. awe_fx_init(voices[voice].ch);
  1661. awe_voice_init(voice, FALSE);
  1662. }
  1663. }
  1664. /* release all notes */
  1665. static void awe_note_off_all(int do_sustain)
  1666. {
  1667. int i;
  1668. for (i = 0; i < awe_max_voices; i++)
  1669. release_voice(i, do_sustain);
  1670. }
  1671. /* kill a voice:
  1672. * not terminate, just release the voice.
  1673. */
  1674. static int
  1675. awe_kill_note(int dev, int voice, int note, int velocity)
  1676. {
  1677. int i, v2, key;
  1678. DEBUG(2,printk("AWE32: [off(%d) nt=%d vl=%d]\n", voice, note, velocity));
  1679. if (! voice_in_range(voice))
  1680. return -EINVAL;
  1681. switch (playing_mode) {
  1682. case AWE_PLAY_DIRECT:
  1683. case AWE_PLAY_INDIRECT:
  1684. key = AWE_VOICE_KEY(voice);
  1685. break;
  1686. case AWE_PLAY_MULTI2:
  1687. v2 = voice_alloc->map[voice] >> 8;
  1688. voice_alloc->map[voice] = 0;
  1689. voice = v2;
  1690. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1691. return -EINVAL;
  1692. /* continue to below */
  1693. default:
  1694. key = AWE_CHAN_KEY(voice, note);
  1695. break;
  1696. }
  1697. for (i = 0; i < awe_max_voices; i++) {
  1698. if (voices[i].key == key)
  1699. release_voice(i, TRUE);
  1700. }
  1701. return 0;
  1702. }
  1703. static void start_or_volume_change(int voice, int velocity)
  1704. {
  1705. voices[voice].velocity = velocity;
  1706. awe_calc_volume(voice);
  1707. if (voices[voice].state == AWE_ST_STANDBY)
  1708. awe_note_on(voice);
  1709. else if (voices[voice].state == AWE_ST_ON)
  1710. awe_set_volume(voice, FALSE);
  1711. }
  1712. static void set_and_start_voice(int voice, int state)
  1713. {
  1714. /* calculate pitch & volume parameters */
  1715. voices[voice].state = state;
  1716. awe_calc_pitch(voice);
  1717. awe_calc_volume(voice);
  1718. if (state == AWE_ST_ON)
  1719. awe_note_on(voice);
  1720. }
  1721. /* start a voice:
  1722. * if note is 255, identical with aftertouch function.
  1723. * Otherwise, start a voice with specified not and volume.
  1724. */
  1725. static int
  1726. awe_start_note(int dev, int voice, int note, int velocity)
  1727. {
  1728. int i, key, state, volonly;
  1729. DEBUG(2,printk("AWE32: [on(%d) nt=%d vl=%d]\n", voice, note, velocity));
  1730. if (! voice_in_range(voice))
  1731. return -EINVAL;
  1732. if (velocity == 0)
  1733. state = AWE_ST_STANDBY; /* stand by for playing */
  1734. else
  1735. state = AWE_ST_ON; /* really play */
  1736. volonly = FALSE;
  1737. switch (playing_mode) {
  1738. case AWE_PLAY_DIRECT:
  1739. case AWE_PLAY_INDIRECT:
  1740. key = AWE_VOICE_KEY(voice);
  1741. if (note == 255)
  1742. volonly = TRUE;
  1743. break;
  1744. case AWE_PLAY_MULTI2:
  1745. voice = voice_alloc->map[voice] >> 8;
  1746. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1747. return -EINVAL;
  1748. /* continue to below */
  1749. default:
  1750. if (note >= 128) { /* key volume mode */
  1751. note -= 128;
  1752. volonly = TRUE;
  1753. }
  1754. key = AWE_CHAN_KEY(voice, note);
  1755. break;
  1756. }
  1757. /* dynamic volume change */
  1758. if (volonly) {
  1759. for (i = 0; i < awe_max_voices; i++) {
  1760. if (voices[i].key == key)
  1761. start_or_volume_change(i, velocity);
  1762. }
  1763. return 0;
  1764. }
  1765. /* if the same note still playing, stop it */
  1766. if (playing_mode != AWE_PLAY_DIRECT || ctrls[AWE_MD_EXCLUSIVE_SOUND]) {
  1767. for (i = 0; i < awe_max_voices; i++)
  1768. if (voices[i].key == key) {
  1769. if (voices[i].state == AWE_ST_ON) {
  1770. awe_note_off(i);
  1771. awe_voice_init(i, FALSE);
  1772. } else if (voices[i].state == AWE_ST_STANDBY)
  1773. awe_voice_init(i, TRUE);
  1774. }
  1775. }
  1776. /* allocate voices */
  1777. if (playing_mode == AWE_PLAY_DIRECT)
  1778. awe_alloc_one_voice(voice, note, velocity);
  1779. else
  1780. awe_alloc_multi_voices(voice, note, velocity, key);
  1781. /* turn off other voices exlusively (for drums) */
  1782. for (i = 0; i < awe_max_voices; i++)
  1783. if (voices[i].key == key)
  1784. awe_exclusive_off(i);
  1785. /* set up pitch and volume parameters */
  1786. for (i = 0; i < awe_max_voices; i++) {
  1787. if (voices[i].key == key && voices[i].state == AWE_ST_OFF)
  1788. set_and_start_voice(i, state);
  1789. }
  1790. return 0;
  1791. }
  1792. /* calculate hash key */
  1793. static int
  1794. awe_search_key(int bank, int preset, int note)
  1795. {
  1796. unsigned int key;
  1797. #if 1 /* new hash table */
  1798. if (bank == AWE_DRUM_BANK)
  1799. key = preset + note + 128;
  1800. else
  1801. key = bank + preset;
  1802. #else
  1803. key = preset;
  1804. #endif
  1805. key %= AWE_MAX_PRESETS;
  1806. return (int)key;
  1807. }
  1808. /* search instrument from hash table */
  1809. static awe_voice_list *
  1810. awe_search_instr(int bank, int preset, int note)
  1811. {
  1812. awe_voice_list *p;
  1813. int key, key2;
  1814. key = awe_search_key(bank, preset, note);
  1815. for (p = preset_table[key]; p; p = p->next_bank) {
  1816. if (p->instr == preset && p->bank == bank)
  1817. return p;
  1818. }
  1819. key2 = awe_search_key(bank, preset, 0); /* search default */
  1820. if (key == key2)
  1821. return NULL;
  1822. for (p = preset_table[key2]; p; p = p->next_bank) {
  1823. if (p->instr == preset && p->bank == bank)
  1824. return p;
  1825. }
  1826. return NULL;
  1827. }
  1828. /* assign the instrument to a voice */
  1829. static int
  1830. awe_set_instr_2(int dev, int voice, int instr_no)
  1831. {
  1832. if (playing_mode == AWE_PLAY_MULTI2) {
  1833. voice = voice_alloc->map[voice] >> 8;
  1834. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1835. return -EINVAL;
  1836. }
  1837. return awe_set_instr(dev, voice, instr_no);
  1838. }
  1839. /* assign the instrument to a channel; voice is the channel number */
  1840. static int
  1841. awe_set_instr(int dev, int voice, int instr_no)
  1842. {
  1843. awe_chan_info *cinfo;
  1844. if (! voice_in_range(voice))
  1845. return -EINVAL;
  1846. if (instr_no < 0 || instr_no >= AWE_MAX_PRESETS)
  1847. return -EINVAL;
  1848. cinfo = &channels[voice];
  1849. cinfo->instr = instr_no;
  1850. DEBUG(2,printk("AWE32: [program(%d) %d]\n", voice, instr_no));
  1851. return 0;
  1852. }
  1853. /* reset all voices; terminate sounds and initialize parameters */
  1854. static void
  1855. awe_reset(int dev)
  1856. {
  1857. int i;
  1858. current_alloc_time = 0;
  1859. /* don't turn off voice 31 and 32. they are used also for FM voices */
  1860. for (i = 0; i < awe_max_voices; i++) {
  1861. awe_terminate(i);
  1862. awe_voice_init(i, TRUE);
  1863. }
  1864. for (i = 0; i < AWE_MAX_CHANNELS; i++)
  1865. awe_channel_init(i, TRUE);
  1866. for (i = 0; i < 16; i++) {
  1867. awe_operations.chn_info[i].controllers[CTL_MAIN_VOLUME] = 127;
  1868. awe_operations.chn_info[i].controllers[CTL_EXPRESSION] = 127;
  1869. }
  1870. awe_init_fm();
  1871. awe_tweak();
  1872. }
  1873. /* hardware specific control:
  1874. * GUS specific and AWE32 specific controls are available.
  1875. */
  1876. static void
  1877. awe_hw_control(int dev, unsigned char *event)
  1878. {
  1879. int cmd = event[2];
  1880. if (cmd & _AWE_MODE_FLAG)
  1881. awe_hw_awe_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
  1882. #ifdef AWE_HAS_GUS_COMPATIBILITY
  1883. else
  1884. awe_hw_gus_control(dev, cmd & _AWE_MODE_VALUE_MASK, event);
  1885. #endif
  1886. }
  1887. #ifdef AWE_HAS_GUS_COMPATIBILITY
  1888. /* GUS compatible controls */
  1889. static void
  1890. awe_hw_gus_control(int dev, int cmd, unsigned char *event)
  1891. {
  1892. int voice, i, key;
  1893. unsigned short p1;
  1894. short p2;
  1895. int plong;
  1896. if (MULTI_LAYER_MODE())
  1897. return;
  1898. if (cmd == _GUS_NUMVOICES)
  1899. return;
  1900. voice = event[3];
  1901. if (! voice_in_range(voice))
  1902. return;
  1903. p1 = *(unsigned short *) &event[4];
  1904. p2 = *(short *) &event[6];
  1905. plong = *(int*) &event[4];
  1906. switch (cmd) {
  1907. case _GUS_VOICESAMPLE:
  1908. awe_set_instr(dev, voice, p1);
  1909. return;
  1910. case _GUS_VOICEBALA:
  1911. /* 0 to 15 --> -128 to 127 */
  1912. awe_panning(dev, voice, ((int)p1 << 4) - 128);
  1913. return;
  1914. case _GUS_VOICEVOL:
  1915. case _GUS_VOICEVOL2:
  1916. /* not supported yet */
  1917. return;
  1918. case _GUS_RAMPRANGE:
  1919. case _GUS_RAMPRATE:
  1920. case _GUS_RAMPMODE:
  1921. case _GUS_RAMPON:
  1922. case _GUS_RAMPOFF:
  1923. /* volume ramping not supported */
  1924. return;
  1925. case _GUS_VOLUME_SCALE:
  1926. return;
  1927. case _GUS_VOICE_POS:
  1928. FX_SET(&channels[voice].fx, AWE_FX_SAMPLE_START,
  1929. (short)(plong & 0x7fff));
  1930. FX_SET(&channels[voice].fx, AWE_FX_COARSE_SAMPLE_START,
  1931. (plong >> 15) & 0xffff);
  1932. return;
  1933. }
  1934. key = AWE_VOICE_KEY(voice);
  1935. for (i = 0; i < awe_max_voices; i++) {
  1936. if (voices[i].key == key) {
  1937. switch (cmd) {
  1938. case _GUS_VOICEON:
  1939. awe_note_on(i);
  1940. break;
  1941. case _GUS_VOICEOFF:
  1942. awe_terminate(i);
  1943. awe_fx_init(voices[i].ch);
  1944. awe_voice_init(i, TRUE);
  1945. break;
  1946. case _GUS_VOICEFADE:
  1947. awe_note_off(i);
  1948. awe_fx_init(voices[i].ch);
  1949. awe_voice_init(i, FALSE);
  1950. break;
  1951. case _GUS_VOICEFREQ:
  1952. awe_calc_pitch_from_freq(i, plong);
  1953. break;
  1954. }
  1955. }
  1956. }
  1957. }
  1958. #endif /* gus_compat */
  1959. /* AWE32 specific controls */
  1960. static void
  1961. awe_hw_awe_control(int dev, int cmd, unsigned char *event)
  1962. {
  1963. int voice;
  1964. unsigned short p1;
  1965. short p2;
  1966. int i;
  1967. voice = event[3];
  1968. if (! voice_in_range(voice))
  1969. return;
  1970. if (playing_mode == AWE_PLAY_MULTI2) {
  1971. voice = voice_alloc->map[voice] >> 8;
  1972. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  1973. return;
  1974. }
  1975. p1 = *(unsigned short *) &event[4];
  1976. p2 = *(short *) &event[6];
  1977. switch (cmd) {
  1978. case _AWE_DEBUG_MODE:
  1979. ctrls[AWE_MD_DEBUG_MODE] = p1;
  1980. printk(KERN_DEBUG "AWE32: debug mode = %d\n", ctrls[AWE_MD_DEBUG_MODE]);
  1981. break;
  1982. case _AWE_REVERB_MODE:
  1983. ctrls[AWE_MD_REVERB_MODE] = p1;
  1984. awe_update_reverb_mode();
  1985. break;
  1986. case _AWE_CHORUS_MODE:
  1987. ctrls[AWE_MD_CHORUS_MODE] = p1;
  1988. awe_update_chorus_mode();
  1989. break;
  1990. case _AWE_REMOVE_LAST_SAMPLES:
  1991. DEBUG(0,printk("AWE32: remove last samples\n"));
  1992. awe_reset(0);
  1993. if (locked_sf_id > 0)
  1994. awe_remove_samples(locked_sf_id);
  1995. break;
  1996. case _AWE_INITIALIZE_CHIP:
  1997. awe_initialize();
  1998. break;
  1999. case _AWE_SEND_EFFECT:
  2000. i = -1;
  2001. if (p1 >= 0x100) {
  2002. i = (p1 >> 8);
  2003. if (i < 0 || i >= MAX_LAYERS)
  2004. break;
  2005. }
  2006. awe_send_effect(voice, i, p1, p2);
  2007. break;
  2008. case _AWE_RESET_CHANNEL:
  2009. awe_channel_init(voice, !p1);
  2010. break;
  2011. case _AWE_TERMINATE_ALL:
  2012. awe_reset(0);
  2013. break;
  2014. case _AWE_TERMINATE_CHANNEL:
  2015. awe_voice_change(voice, awe_terminate_and_init);
  2016. break;
  2017. case _AWE_RELEASE_ALL:
  2018. awe_note_off_all(FALSE);
  2019. break;
  2020. case _AWE_NOTEOFF_ALL:
  2021. awe_note_off_all(TRUE);
  2022. break;
  2023. case _AWE_INITIAL_VOLUME:
  2024. DEBUG(0,printk("AWE32: init attenuation %d\n", p1));
  2025. atten_relative = (char)p2;
  2026. atten_offset = (short)p1;
  2027. awe_update_volume();
  2028. break;
  2029. case _AWE_CHN_PRESSURE:
  2030. channels[voice].chan_press = p1;
  2031. awe_modwheel_change(voice, p1);
  2032. break;
  2033. case _AWE_CHANNEL_MODE:
  2034. DEBUG(0,printk("AWE32: channel mode = %d\n", p1));
  2035. playing_mode = p1;
  2036. awe_reset(0);
  2037. break;
  2038. case _AWE_DRUM_CHANNELS:
  2039. DEBUG(0,printk("AWE32: drum flags = %x\n", p1));
  2040. drum_flags = *(unsigned int*)&event[4];
  2041. break;
  2042. case _AWE_MISC_MODE:
  2043. DEBUG(0,printk("AWE32: ctrl parms = %d %d\n", p1, p2));
  2044. if (p1 > AWE_MD_VERSION && p1 < AWE_MD_END) {
  2045. ctrls[p1] = p2;
  2046. if (ctrl_parms[p1].update)
  2047. ctrl_parms[p1].update();
  2048. }
  2049. break;
  2050. case _AWE_EQUALIZER:
  2051. ctrls[AWE_MD_BASS_LEVEL] = p1;
  2052. ctrls[AWE_MD_TREBLE_LEVEL] = p2;
  2053. awe_update_equalizer();
  2054. break;
  2055. default:
  2056. DEBUG(0,printk("AWE32: hw control cmd=%d voice=%d\n", cmd, voice));
  2057. break;
  2058. }
  2059. }
  2060. /* change effects */
  2061. static void
  2062. awe_send_effect(int voice, int layer, int type, int val)
  2063. {
  2064. awe_chan_info *cinfo;
  2065. FX_Rec *fx;
  2066. int mode;
  2067. cinfo = &channels[voice];
  2068. if (layer >= 0 && layer < MAX_LAYERS)
  2069. fx = &cinfo->fx_layer[layer];
  2070. else
  2071. fx = &cinfo->fx;
  2072. if (type & 0x40)
  2073. mode = FX_FLAG_OFF;
  2074. else if (type & 0x80)
  2075. mode = FX_FLAG_ADD;
  2076. else
  2077. mode = FX_FLAG_SET;
  2078. type &= 0x3f;
  2079. if (type >= 0 && type < AWE_FX_END) {
  2080. DEBUG(2,printk("AWE32: effects (%d) %d %d\n", voice, type, val));
  2081. if (mode == FX_FLAG_SET)
  2082. FX_SET(fx, type, val);
  2083. else if (mode == FX_FLAG_ADD)
  2084. FX_ADD(fx, type, val);
  2085. else
  2086. FX_UNSET(fx, type);
  2087. if (mode != FX_FLAG_OFF && parm_defs[type].realtime) {
  2088. DEBUG(2,printk("AWE32: fx_realtime (%d)\n", voice));
  2089. awe_voice_change(voice, parm_defs[type].realtime);
  2090. }
  2091. }
  2092. }
  2093. /* change modulation wheel; voice is already mapped on multi2 mode */
  2094. static void
  2095. awe_modwheel_change(int voice, int value)
  2096. {
  2097. int i;
  2098. awe_chan_info *cinfo;
  2099. cinfo = &channels[voice];
  2100. i = value * ctrls[AWE_MD_MOD_SENSE] / 1200;
  2101. FX_ADD(&cinfo->fx, AWE_FX_LFO1_PITCH, i);
  2102. awe_voice_change(voice, awe_fx_fmmod);
  2103. FX_ADD(&cinfo->fx, AWE_FX_LFO2_PITCH, i);
  2104. awe_voice_change(voice, awe_fx_fm2frq2);
  2105. }
  2106. /* voice pressure change */
  2107. static void
  2108. awe_aftertouch(int dev, int voice, int pressure)
  2109. {
  2110. int note;
  2111. DEBUG(2,printk("AWE32: [after(%d) %d]\n", voice, pressure));
  2112. if (! voice_in_range(voice))
  2113. return;
  2114. switch (playing_mode) {
  2115. case AWE_PLAY_DIRECT:
  2116. case AWE_PLAY_INDIRECT:
  2117. awe_start_note(dev, voice, 255, pressure);
  2118. break;
  2119. case AWE_PLAY_MULTI2:
  2120. note = (voice_alloc->map[voice] & 0xff) - 1;
  2121. awe_key_pressure(dev, voice, note + 0x80, pressure);
  2122. break;
  2123. }
  2124. }
  2125. /* voice control change */
  2126. static void
  2127. awe_controller(int dev, int voice, int ctrl_num, int value)
  2128. {
  2129. awe_chan_info *cinfo;
  2130. if (! voice_in_range(voice))
  2131. return;
  2132. if (playing_mode == AWE_PLAY_MULTI2) {
  2133. voice = voice_alloc->map[voice] >> 8;
  2134. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2135. return;
  2136. }
  2137. cinfo = &channels[voice];
  2138. switch (ctrl_num) {
  2139. case CTL_BANK_SELECT: /* MIDI control #0 */
  2140. DEBUG(2,printk("AWE32: [bank(%d) %d]\n", voice, value));
  2141. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice) &&
  2142. !ctrls[AWE_MD_TOGGLE_DRUM_BANK])
  2143. break;
  2144. if (value < 0 || value > 255)
  2145. break;
  2146. cinfo->bank = value;
  2147. if (cinfo->bank == AWE_DRUM_BANK)
  2148. DRUM_CHANNEL_ON(cinfo->channel);
  2149. else
  2150. DRUM_CHANNEL_OFF(cinfo->channel);
  2151. awe_set_instr(dev, voice, cinfo->instr);
  2152. break;
  2153. case CTL_MODWHEEL: /* MIDI control #1 */
  2154. DEBUG(2,printk("AWE32: [modwheel(%d) %d]\n", voice, value));
  2155. awe_modwheel_change(voice, value);
  2156. break;
  2157. case CTRL_PITCH_BENDER: /* SEQ1 V2 contorl */
  2158. DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, value));
  2159. /* zero centered */
  2160. cinfo->bender = value;
  2161. awe_voice_change(voice, awe_set_voice_pitch);
  2162. break;
  2163. case CTRL_PITCH_BENDER_RANGE: /* SEQ1 V2 control */
  2164. DEBUG(2,printk("AWE32: [range(%d) %d]\n", voice, value));
  2165. /* value = sense x 100 */
  2166. cinfo->bender_range = value;
  2167. /* no audible pitch change yet.. */
  2168. break;
  2169. case CTL_EXPRESSION: /* MIDI control #11 */
  2170. if (SINGLE_LAYER_MODE())
  2171. value /= 128;
  2172. case CTRL_EXPRESSION: /* SEQ1 V2 control */
  2173. DEBUG(2,printk("AWE32: [expr(%d) %d]\n", voice, value));
  2174. /* 0 - 127 */
  2175. cinfo->expression_vol = value;
  2176. awe_voice_change(voice, awe_set_voice_vol);
  2177. break;
  2178. case CTL_PAN: /* MIDI control #10 */
  2179. DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, value));
  2180. /* (0-127) -> signed 8bit */
  2181. cinfo->panning = value * 2 - 128;
  2182. if (ctrls[AWE_MD_REALTIME_PAN])
  2183. awe_voice_change(voice, awe_set_pan);
  2184. break;
  2185. case CTL_MAIN_VOLUME: /* MIDI control #7 */
  2186. if (SINGLE_LAYER_MODE())
  2187. value = (value * 100) / 16383;
  2188. case CTRL_MAIN_VOLUME: /* SEQ1 V2 control */
  2189. DEBUG(2,printk("AWE32: [mainvol(%d) %d]\n", voice, value));
  2190. /* 0 - 127 */
  2191. cinfo->main_vol = value;
  2192. awe_voice_change(voice, awe_set_voice_vol);
  2193. break;
  2194. case CTL_EXT_EFF_DEPTH: /* reverb effects: 0-127 */
  2195. DEBUG(2,printk("AWE32: [reverb(%d) %d]\n", voice, value));
  2196. FX_SET(&cinfo->fx, AWE_FX_REVERB, value * 2);
  2197. break;
  2198. case CTL_CHORUS_DEPTH: /* chorus effects: 0-127 */
  2199. DEBUG(2,printk("AWE32: [chorus(%d) %d]\n", voice, value));
  2200. FX_SET(&cinfo->fx, AWE_FX_CHORUS, value * 2);
  2201. break;
  2202. case 120: /* all sounds off */
  2203. awe_note_off_all(FALSE);
  2204. break;
  2205. case 123: /* all notes off */
  2206. awe_note_off_all(TRUE);
  2207. break;
  2208. case CTL_SUSTAIN: /* MIDI control #64 */
  2209. cinfo->sustained = value;
  2210. if (value != 127)
  2211. awe_voice_change(voice, awe_sustain_off);
  2212. break;
  2213. case CTL_SOSTENUTO: /* MIDI control #66 */
  2214. if (value == 127)
  2215. awe_voice_change(voice, awe_sostenuto_on);
  2216. else
  2217. awe_voice_change(voice, awe_sustain_off);
  2218. break;
  2219. default:
  2220. DEBUG(0,printk("AWE32: [control(%d) ctrl=%d val=%d]\n",
  2221. voice, ctrl_num, value));
  2222. break;
  2223. }
  2224. }
  2225. /* voice pan change (value = -128 - 127) */
  2226. static void
  2227. awe_panning(int dev, int voice, int value)
  2228. {
  2229. awe_chan_info *cinfo;
  2230. if (! voice_in_range(voice))
  2231. return;
  2232. if (playing_mode == AWE_PLAY_MULTI2) {
  2233. voice = voice_alloc->map[voice] >> 8;
  2234. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2235. return;
  2236. }
  2237. cinfo = &channels[voice];
  2238. cinfo->panning = value;
  2239. DEBUG(2,printk("AWE32: [pan(%d) %d]\n", voice, cinfo->panning));
  2240. if (ctrls[AWE_MD_REALTIME_PAN])
  2241. awe_voice_change(voice, awe_set_pan);
  2242. }
  2243. /* volume mode change */
  2244. static void
  2245. awe_volume_method(int dev, int mode)
  2246. {
  2247. /* not impremented */
  2248. DEBUG(0,printk("AWE32: [volmethod mode=%d]\n", mode));
  2249. }
  2250. /* pitch wheel change: 0-16384 */
  2251. static void
  2252. awe_bender(int dev, int voice, int value)
  2253. {
  2254. awe_chan_info *cinfo;
  2255. if (! voice_in_range(voice))
  2256. return;
  2257. if (playing_mode == AWE_PLAY_MULTI2) {
  2258. voice = voice_alloc->map[voice] >> 8;
  2259. if (voice < 0 || voice >= AWE_MAX_CHANNELS)
  2260. return;
  2261. }
  2262. /* convert to zero centered value */
  2263. cinfo = &channels[voice];
  2264. cinfo->bender = value - 8192;
  2265. DEBUG(2,printk("AWE32: [bend(%d) %d]\n", voice, cinfo->bender));
  2266. awe_voice_change(voice, awe_set_voice_pitch);
  2267. }
  2268. /*
  2269. * load a sound patch:
  2270. * three types of patches are accepted: AWE, GUS, and SYSEX.
  2271. */
  2272. static int
  2273. awe_load_patch(int dev, int format, const char __user *addr,
  2274. int offs, int count, int pmgr_flag)
  2275. {
  2276. awe_patch_info patch;
  2277. int rc = 0;
  2278. #ifdef AWE_HAS_GUS_COMPATIBILITY
  2279. if (format == GUS_PATCH) {
  2280. return awe_load_guspatch(addr, offs, count, pmgr_flag);
  2281. } else
  2282. #endif
  2283. if (format == SYSEX_PATCH) {
  2284. /* no system exclusive message supported yet */
  2285. return 0;
  2286. } else if (format != AWE_PATCH) {
  2287. printk(KERN_WARNING "AWE32 Error: Invalid patch format (key) 0x%x\n", format);
  2288. return -EINVAL;
  2289. }
  2290. if (count < AWE_PATCH_INFO_SIZE) {
  2291. printk(KERN_WARNING "AWE32 Error: Patch header too short\n");
  2292. return -EINVAL;
  2293. }
  2294. if (copy_from_user(((char*)&patch) + offs, addr + offs,
  2295. AWE_PATCH_INFO_SIZE - offs))
  2296. return -EFAULT;
  2297. count -= AWE_PATCH_INFO_SIZE;
  2298. if (count < patch.len) {
  2299. printk(KERN_WARNING "AWE32: sample: Patch record too short (%d<%d)\n",
  2300. count, patch.len);
  2301. return -EINVAL;
  2302. }
  2303. switch (patch.type) {
  2304. case AWE_LOAD_INFO:
  2305. rc = awe_load_info(&patch, addr, count);
  2306. break;
  2307. case AWE_LOAD_DATA:
  2308. rc = awe_load_data(&patch, addr, count);
  2309. break;
  2310. case AWE_OPEN_PATCH:
  2311. rc = awe_open_patch(&patch, addr, count);
  2312. break;
  2313. case AWE_CLOSE_PATCH:
  2314. rc = awe_close_patch(&patch, addr, count);
  2315. break;
  2316. case AWE_UNLOAD_PATCH:
  2317. rc = awe_unload_patch(&patch, addr, count);
  2318. break;
  2319. case AWE_REPLACE_DATA:
  2320. rc = awe_replace_data(&patch, addr, count);
  2321. break;
  2322. case AWE_MAP_PRESET:
  2323. rc = awe_load_map(&patch, addr, count);
  2324. break;
  2325. /* case AWE_PROBE_INFO:
  2326. rc = awe_probe_info(&patch, addr, count);
  2327. break;*/
  2328. case AWE_PROBE_DATA:
  2329. rc = awe_probe_data(&patch, addr, count);
  2330. break;
  2331. case AWE_REMOVE_INFO:
  2332. rc = awe_remove_info(&patch, addr, count);
  2333. break;
  2334. case AWE_LOAD_CHORUS_FX:
  2335. rc = awe_load_chorus_fx(&patch, addr, count);
  2336. break;
  2337. case AWE_LOAD_REVERB_FX:
  2338. rc = awe_load_reverb_fx(&patch, addr, count);
  2339. break;
  2340. default:
  2341. printk(KERN_WARNING "AWE32 Error: unknown patch format type %d\n",
  2342. patch.type);
  2343. rc = -EINVAL;
  2344. }
  2345. return rc;
  2346. }
  2347. /* create an sf list record */
  2348. static int
  2349. awe_create_sf(int type, char *name)
  2350. {
  2351. sf_list *rec;
  2352. /* terminate sounds */
  2353. awe_reset(0);
  2354. rec = (sf_list *)kmalloc(sizeof(*rec), GFP_KERNEL);
  2355. if (rec == NULL)
  2356. return 1; /* no memory */
  2357. rec->sf_id = current_sf_id + 1;
  2358. rec->type = type;
  2359. if (/*current_sf_id == 0 ||*/ (type & AWE_PAT_LOCKED) != 0)
  2360. locked_sf_id = current_sf_id + 1;
  2361. rec->num_info = awe_free_info();
  2362. rec->num_sample = awe_free_sample();
  2363. rec->mem_ptr = awe_free_mem_ptr();
  2364. rec->infos = rec->last_infos = NULL;
  2365. rec->samples = rec->last_samples = NULL;
  2366. /* add to linked-list */
  2367. rec->next = NULL;
  2368. rec->prev = sftail;
  2369. if (sftail)
  2370. sftail->next = rec;
  2371. else
  2372. sfhead = rec;
  2373. sftail = rec;
  2374. current_sf_id++;
  2375. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2376. rec->shared = NULL;
  2377. if (name)
  2378. memcpy(rec->name, name, AWE_PATCH_NAME_LEN);
  2379. else
  2380. strcpy(rec->name, "*TEMPORARY*");
  2381. if (current_sf_id > 1 && name && (type & AWE_PAT_SHARED) != 0) {
  2382. /* is the current font really a shared font? */
  2383. if (is_shared_sf(rec->name)) {
  2384. /* check if the shared font is already installed */
  2385. sf_list *p;
  2386. for (p = rec->prev; p; p = p->prev) {
  2387. if (is_identical_name(rec->name, p)) {
  2388. rec->shared = p;
  2389. break;
  2390. }
  2391. }
  2392. }
  2393. }
  2394. #endif /* allow sharing */
  2395. return 0;
  2396. }
  2397. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2398. /* check if the given name is a valid shared name */
  2399. #define ASC_TO_KEY(c) ((c) - 'A' + 1)
  2400. static int is_shared_sf(unsigned char *name)
  2401. {
  2402. static unsigned char id_head[4] = {
  2403. ASC_TO_KEY('A'), ASC_TO_KEY('W'), ASC_TO_KEY('E'),
  2404. AWE_MAJOR_VERSION,
  2405. };
  2406. if (memcmp(name, id_head, 4) == 0)
  2407. return TRUE;
  2408. return FALSE;
  2409. }
  2410. /* check if the given name matches to the existing list */
  2411. static int is_identical_name(unsigned char *name, sf_list *p)
  2412. {
  2413. char *id = p->name;
  2414. if (is_shared_sf(id) && memcmp(id, name, AWE_PATCH_NAME_LEN) == 0)
  2415. return TRUE;
  2416. return FALSE;
  2417. }
  2418. /* check if the given voice info exists */
  2419. static int info_duplicated(sf_list *sf, awe_voice_list *rec)
  2420. {
  2421. /* search for all sharing lists */
  2422. for (; sf; sf = sf->shared) {
  2423. awe_voice_list *p;
  2424. for (p = sf->infos; p; p = p->next) {
  2425. if (p->type == V_ST_NORMAL &&
  2426. p->bank == rec->bank &&
  2427. p->instr == rec->instr &&
  2428. p->v.low == rec->v.low &&
  2429. p->v.high == rec->v.high &&
  2430. p->v.sample == rec->v.sample)
  2431. return TRUE;
  2432. }
  2433. }
  2434. return FALSE;
  2435. }
  2436. #endif /* AWE_ALLOW_SAMPLE_SHARING */
  2437. /* free sf_list record */
  2438. /* linked-list in this function is not cared */
  2439. static void
  2440. awe_free_sf(sf_list *sf)
  2441. {
  2442. if (sf->infos) {
  2443. awe_voice_list *p, *next;
  2444. for (p = sf->infos; p; p = next) {
  2445. next = p->next;
  2446. kfree(p);
  2447. }
  2448. }
  2449. if (sf->samples) {
  2450. awe_sample_list *p, *next;
  2451. for (p = sf->samples; p; p = next) {
  2452. next = p->next;
  2453. kfree(p);
  2454. }
  2455. }
  2456. kfree(sf);
  2457. }
  2458. /* open patch; create sf list and set opened flag */
  2459. static int
  2460. awe_open_patch(awe_patch_info *patch, const char __user *addr, int count)
  2461. {
  2462. awe_open_parm parm;
  2463. int shared;
  2464. if (copy_from_user(&parm, addr + AWE_PATCH_INFO_SIZE, sizeof(parm)))
  2465. return -EFAULT;
  2466. shared = FALSE;
  2467. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2468. if (sftail && (parm.type & AWE_PAT_SHARED) != 0) {
  2469. /* is the previous font the same font? */
  2470. if (is_identical_name(parm.name, sftail)) {
  2471. /* then append to the previous */
  2472. shared = TRUE;
  2473. awe_reset(0);
  2474. if (parm.type & AWE_PAT_LOCKED)
  2475. locked_sf_id = current_sf_id;
  2476. }
  2477. }
  2478. #endif /* allow sharing */
  2479. if (! shared) {
  2480. if (awe_create_sf(parm.type, parm.name)) {
  2481. printk(KERN_ERR "AWE32: can't open: failed to alloc new list\n");
  2482. return -ENOMEM;
  2483. }
  2484. }
  2485. patch_opened = TRUE;
  2486. return current_sf_id;
  2487. }
  2488. /* check if the patch is already opened */
  2489. static sf_list *
  2490. check_patch_opened(int type, char *name)
  2491. {
  2492. if (! patch_opened) {
  2493. if (awe_create_sf(type, name)) {
  2494. printk(KERN_ERR "AWE32: failed to alloc new list\n");
  2495. return NULL;
  2496. }
  2497. patch_opened = TRUE;
  2498. return sftail;
  2499. }
  2500. return sftail;
  2501. }
  2502. /* close the patch; if no voice is loaded, remove the patch */
  2503. static int
  2504. awe_close_patch(awe_patch_info *patch, const char __user *addr, int count)
  2505. {
  2506. if (patch_opened && sftail) {
  2507. /* if no voice is loaded, release the current patch */
  2508. if (sftail->infos == NULL) {
  2509. awe_reset(0);
  2510. awe_remove_samples(current_sf_id - 1);
  2511. }
  2512. }
  2513. patch_opened = 0;
  2514. return 0;
  2515. }
  2516. /* remove the latest patch */
  2517. static int
  2518. awe_unload_patch(awe_patch_info *patch, const char __user *addr, int count)
  2519. {
  2520. if (current_sf_id > 0 && current_sf_id > locked_sf_id) {
  2521. awe_reset(0);
  2522. awe_remove_samples(current_sf_id - 1);
  2523. }
  2524. return 0;
  2525. }
  2526. /* allocate voice info list records */
  2527. static awe_voice_list *
  2528. alloc_new_info(void)
  2529. {
  2530. awe_voice_list *newlist;
  2531. newlist = (awe_voice_list *)kmalloc(sizeof(*newlist), GFP_KERNEL);
  2532. if (newlist == NULL) {
  2533. printk(KERN_ERR "AWE32: can't alloc info table\n");
  2534. return NULL;
  2535. }
  2536. return newlist;
  2537. }
  2538. /* allocate sample info list records */
  2539. static awe_sample_list *
  2540. alloc_new_sample(void)
  2541. {
  2542. awe_sample_list *newlist;
  2543. newlist = (awe_sample_list *)kmalloc(sizeof(*newlist), GFP_KERNEL);
  2544. if (newlist == NULL) {
  2545. printk(KERN_ERR "AWE32: can't alloc sample table\n");
  2546. return NULL;
  2547. }
  2548. return newlist;
  2549. }
  2550. /* load voice map */
  2551. static int
  2552. awe_load_map(awe_patch_info *patch, const char __user *addr, int count)
  2553. {
  2554. awe_voice_map map;
  2555. awe_voice_list *rec, *p;
  2556. sf_list *sf;
  2557. /* get the link info */
  2558. if (count < sizeof(map)) {
  2559. printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
  2560. return -EINVAL;
  2561. }
  2562. if (copy_from_user(&map, addr + AWE_PATCH_INFO_SIZE, sizeof(map)))
  2563. return -EFAULT;
  2564. /* check if the identical mapping already exists */
  2565. p = awe_search_instr(map.map_bank, map.map_instr, map.map_key);
  2566. for (; p; p = p->next_instr) {
  2567. if (p->type == V_ST_MAPPED &&
  2568. p->v.start == map.src_instr &&
  2569. p->v.end == map.src_bank &&
  2570. p->v.fixkey == map.src_key)
  2571. return 0; /* already present! */
  2572. }
  2573. if ((sf = check_patch_opened(AWE_PAT_TYPE_MAP, NULL)) == NULL)
  2574. return -ENOMEM;
  2575. if ((rec = alloc_new_info()) == NULL)
  2576. return -ENOMEM;
  2577. rec->bank = map.map_bank;
  2578. rec->instr = map.map_instr;
  2579. rec->type = V_ST_MAPPED;
  2580. rec->disabled = FALSE;
  2581. awe_init_voice_info(&rec->v);
  2582. if (map.map_key >= 0) {
  2583. rec->v.low = map.map_key;
  2584. rec->v.high = map.map_key;
  2585. }
  2586. rec->v.start = map.src_instr;
  2587. rec->v.end = map.src_bank;
  2588. rec->v.fixkey = map.src_key;
  2589. add_sf_info(sf, rec);
  2590. add_info_list(rec);
  2591. return 0;
  2592. }
  2593. #if 0
  2594. /* probe preset in the current list -- nothing to be loaded */
  2595. static int
  2596. awe_probe_info(awe_patch_info *patch, const char __user *addr, int count)
  2597. {
  2598. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2599. awe_voice_map map;
  2600. awe_voice_list *p;
  2601. if (! patch_opened)
  2602. return -EINVAL;
  2603. /* get the link info */
  2604. if (count < sizeof(map)) {
  2605. printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
  2606. return -EINVAL;
  2607. }
  2608. if (copy_from_user(&map, addr + AWE_PATCH_INFO_SIZE, sizeof(map)))
  2609. return -EFAULT;
  2610. /* check if the identical mapping already exists */
  2611. if (sftail == NULL)
  2612. return -EINVAL;
  2613. p = awe_search_instr(map.src_bank, map.src_instr, map.src_key);
  2614. for (; p; p = p->next_instr) {
  2615. if (p->type == V_ST_NORMAL &&
  2616. is_identical_holder(p->holder, sftail) &&
  2617. p->v.low <= map.src_key &&
  2618. p->v.high >= map.src_key)
  2619. return 0; /* already present! */
  2620. }
  2621. #endif /* allow sharing */
  2622. return -EINVAL;
  2623. }
  2624. #endif
  2625. /* probe sample in the current list -- nothing to be loaded */
  2626. static int
  2627. awe_probe_data(awe_patch_info *patch, const char __user *addr, int count)
  2628. {
  2629. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2630. if (! patch_opened)
  2631. return -EINVAL;
  2632. /* search the specified sample by optarg */
  2633. if (search_sample_index(sftail, patch->optarg) != NULL)
  2634. return 0;
  2635. #endif /* allow sharing */
  2636. return -EINVAL;
  2637. }
  2638. /* remove the present instrument layers */
  2639. static int
  2640. remove_info(sf_list *sf, int bank, int instr)
  2641. {
  2642. awe_voice_list *prev, *next, *p;
  2643. int removed = 0;
  2644. prev = NULL;
  2645. for (p = sf->infos; p; p = next) {
  2646. next = p->next;
  2647. if (p->type == V_ST_NORMAL &&
  2648. p->bank == bank && p->instr == instr) {
  2649. /* remove this layer */
  2650. if (prev)
  2651. prev->next = next;
  2652. else
  2653. sf->infos = next;
  2654. if (p == sf->last_infos)
  2655. sf->last_infos = prev;
  2656. sf->num_info--;
  2657. removed++;
  2658. kfree(p);
  2659. } else
  2660. prev = p;
  2661. }
  2662. if (removed)
  2663. rebuild_preset_list();
  2664. return removed;
  2665. }
  2666. /* load voice information data */
  2667. static int
  2668. awe_load_info(awe_patch_info *patch, const char __user *addr, int count)
  2669. {
  2670. int offset;
  2671. awe_voice_rec_hdr hdr;
  2672. int i;
  2673. int total_size;
  2674. sf_list *sf;
  2675. awe_voice_list *rec;
  2676. if (count < AWE_VOICE_REC_SIZE) {
  2677. printk(KERN_WARNING "AWE32 Error: invalid patch info length\n");
  2678. return -EINVAL;
  2679. }
  2680. offset = AWE_PATCH_INFO_SIZE;
  2681. if (copy_from_user((char*)&hdr, addr + offset, AWE_VOICE_REC_SIZE))
  2682. return -EFAULT;
  2683. offset += AWE_VOICE_REC_SIZE;
  2684. if (hdr.nvoices <= 0 || hdr.nvoices >= 100) {
  2685. printk(KERN_WARNING "AWE32 Error: Invalid voice number %d\n", hdr.nvoices);
  2686. return -EINVAL;
  2687. }
  2688. total_size = AWE_VOICE_REC_SIZE + AWE_VOICE_INFO_SIZE * hdr.nvoices;
  2689. if (count < total_size) {
  2690. printk(KERN_WARNING "AWE32 Error: patch length(%d) is smaller than nvoices(%d)\n",
  2691. count, hdr.nvoices);
  2692. return -EINVAL;
  2693. }
  2694. if ((sf = check_patch_opened(AWE_PAT_TYPE_MISC, NULL)) == NULL)
  2695. return -ENOMEM;
  2696. switch (hdr.write_mode) {
  2697. case AWE_WR_EXCLUSIVE:
  2698. /* exclusive mode - if the instrument already exists,
  2699. return error */
  2700. for (rec = sf->infos; rec; rec = rec->next) {
  2701. if (rec->type == V_ST_NORMAL &&
  2702. rec->bank == hdr.bank &&
  2703. rec->instr == hdr.instr)
  2704. return -EINVAL;
  2705. }
  2706. break;
  2707. case AWE_WR_REPLACE:
  2708. /* replace mode - remove the instrument if it already exists */
  2709. remove_info(sf, hdr.bank, hdr.instr);
  2710. break;
  2711. }
  2712. /* append new layers */
  2713. for (i = 0; i < hdr.nvoices; i++) {
  2714. rec = alloc_new_info();
  2715. if (rec == NULL)
  2716. return -ENOMEM;
  2717. rec->bank = hdr.bank;
  2718. rec->instr = hdr.instr;
  2719. rec->type = V_ST_NORMAL;
  2720. rec->disabled = FALSE;
  2721. /* copy awe_voice_info parameters */
  2722. if (copy_from_user(&rec->v, addr + offset, AWE_VOICE_INFO_SIZE)) {
  2723. kfree(rec);
  2724. return -EFAULT;
  2725. }
  2726. offset += AWE_VOICE_INFO_SIZE;
  2727. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2728. if (sf && sf->shared) {
  2729. if (info_duplicated(sf, rec)) {
  2730. kfree(rec);
  2731. continue;
  2732. }
  2733. }
  2734. #endif /* allow sharing */
  2735. if (rec->v.mode & AWE_MODE_INIT_PARM)
  2736. awe_init_voice_parm(&rec->v.parm);
  2737. add_sf_info(sf, rec);
  2738. awe_set_sample(rec);
  2739. add_info_list(rec);
  2740. }
  2741. return 0;
  2742. }
  2743. /* remove instrument layers */
  2744. static int
  2745. awe_remove_info(awe_patch_info *patch, const char __user *addr, int count)
  2746. {
  2747. unsigned char bank, instr;
  2748. sf_list *sf;
  2749. if (! patch_opened || (sf = sftail) == NULL) {
  2750. printk(KERN_WARNING "AWE32: remove_info: patch not opened\n");
  2751. return -EINVAL;
  2752. }
  2753. bank = ((unsigned short)patch->optarg >> 8) & 0xff;
  2754. instr = (unsigned short)patch->optarg & 0xff;
  2755. if (! remove_info(sf, bank, instr))
  2756. return -EINVAL;
  2757. return 0;
  2758. }
  2759. /* load wave sample data */
  2760. static int
  2761. awe_load_data(awe_patch_info *patch, const char __user *addr, int count)
  2762. {
  2763. int offset, size;
  2764. int rc;
  2765. awe_sample_info tmprec;
  2766. awe_sample_list *rec;
  2767. sf_list *sf;
  2768. if ((sf = check_patch_opened(AWE_PAT_TYPE_MISC, NULL)) == NULL)
  2769. return -ENOMEM;
  2770. size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
  2771. offset = AWE_PATCH_INFO_SIZE;
  2772. if (copy_from_user(&tmprec, addr + offset, AWE_SAMPLE_INFO_SIZE))
  2773. return -EFAULT;
  2774. offset += AWE_SAMPLE_INFO_SIZE;
  2775. if (size != tmprec.size) {
  2776. printk(KERN_WARNING "AWE32: load: sample size differed (%d != %d)\n",
  2777. tmprec.size, size);
  2778. return -EINVAL;
  2779. }
  2780. if (search_sample_index(sf, tmprec.sample) != NULL) {
  2781. #ifdef AWE_ALLOW_SAMPLE_SHARING
  2782. /* if shared sample, skip this data */
  2783. if (sf->type & AWE_PAT_SHARED)
  2784. return 0;
  2785. #endif /* allow sharing */
  2786. DEBUG(1,printk("AWE32: sample data %d already present\n", tmprec.sample));
  2787. return -EINVAL;
  2788. }
  2789. if ((rec = alloc_new_sample()) == NULL)
  2790. return -ENOMEM;
  2791. memcpy(&rec->v, &tmprec, sizeof(tmprec));
  2792. if (rec->v.size > 0) {
  2793. if ((rc = awe_write_wave_data(addr, offset, rec, -1)) < 0) {
  2794. kfree(rec);
  2795. return rc;
  2796. }
  2797. sf->mem_ptr += rc;
  2798. }
  2799. add_sf_sample(sf, rec);
  2800. return 0;
  2801. }
  2802. /* replace wave sample data */
  2803. static int
  2804. awe_replace_data(awe_patch_info *patch, const char __user *addr, int count)
  2805. {
  2806. int offset;
  2807. int size;
  2808. int rc;
  2809. int channels;
  2810. awe_sample_info cursmp;
  2811. int save_mem_ptr;
  2812. sf_list *sf;
  2813. awe_sample_list *rec;
  2814. if (! patch_opened || (sf = sftail) == NULL) {
  2815. printk(KERN_WARNING "AWE32: replace: patch not opened\n");
  2816. return -EINVAL;
  2817. }
  2818. size = (count - AWE_SAMPLE_INFO_SIZE) / 2;
  2819. offset = AWE_PATCH_INFO_SIZE;
  2820. if (copy_from_user(&cursmp, addr + offset, AWE_SAMPLE_INFO_SIZE))
  2821. return -EFAULT;
  2822. offset += AWE_SAMPLE_INFO_SIZE;
  2823. if (cursmp.size == 0 || size != cursmp.size) {
  2824. printk(KERN_WARNING "AWE32: replace: invalid sample size (%d!=%d)\n",
  2825. cursmp.size, size);
  2826. return -EINVAL;
  2827. }
  2828. channels = patch->optarg;
  2829. if (channels <= 0 || channels > AWE_NORMAL_VOICES) {
  2830. printk(KERN_WARNING "AWE32: replace: invalid channels %d\n", channels);
  2831. return -EINVAL;
  2832. }
  2833. for (rec = sf->samples; rec; rec = rec->next) {
  2834. if (rec->v.sample == cursmp.sample)
  2835. break;
  2836. }
  2837. if (rec == NULL) {
  2838. printk(KERN_WARNING "AWE32: replace: cannot find existing sample data %d\n",
  2839. cursmp.sample);
  2840. return -EINVAL;
  2841. }
  2842. if (rec->v.size != cursmp.size) {
  2843. printk(KERN_WARNING "AWE32: replace: exiting size differed (%d!=%d)\n",
  2844. rec->v.size, cursmp.size);
  2845. return -EINVAL;
  2846. }
  2847. save_mem_ptr = awe_free_mem_ptr();
  2848. sftail->mem_ptr = rec->v.start - awe_mem_start;
  2849. memcpy(&rec->v, &cursmp, sizeof(cursmp));
  2850. rec->v.sf_id = current_sf_id;
  2851. if ((rc = awe_write_wave_data(addr, offset, rec, channels)) < 0)
  2852. return rc;
  2853. sftail->mem_ptr = save_mem_ptr;
  2854. return 0;
  2855. }
  2856. /*----------------------------------------------------------------*/
  2857. static const char __user *readbuf_addr;
  2858. static int readbuf_offs;
  2859. static int readbuf_flags;
  2860. /* initialize read buffer */
  2861. static int
  2862. readbuf_init(const char __user *addr, int offset, awe_sample_info *sp)
  2863. {
  2864. readbuf_addr = addr;
  2865. readbuf_offs = offset;
  2866. readbuf_flags = sp->mode_flags;
  2867. return 0;
  2868. }
  2869. /* read directly from user buffer */
  2870. static unsigned short
  2871. readbuf_word(int pos)
  2872. {
  2873. unsigned short c;
  2874. /* read from user buffer */
  2875. if (readbuf_flags & AWE_SAMPLE_8BITS) {
  2876. unsigned char cc;
  2877. get_user(cc, (unsigned char __user *)(readbuf_addr + readbuf_offs + pos));
  2878. c = (unsigned short)cc << 8; /* convert 8bit -> 16bit */
  2879. } else {
  2880. get_user(c, (unsigned short __user *)(readbuf_addr + readbuf_offs + pos * 2));
  2881. }
  2882. if (readbuf_flags & AWE_SAMPLE_UNSIGNED)
  2883. c ^= 0x8000; /* unsigned -> signed */
  2884. return c;
  2885. }
  2886. #define readbuf_word_cache readbuf_word
  2887. #define readbuf_end() /**/
  2888. /*----------------------------------------------------------------*/
  2889. #define BLANK_LOOP_START 8
  2890. #define BLANK_LOOP_END 40
  2891. #define BLANK_LOOP_SIZE 48
  2892. /* loading onto memory - return the actual written size */
  2893. static int
  2894. awe_write_wave_data(const char __user *addr, int offset, awe_sample_list *list, int channels)
  2895. {
  2896. int i, truesize, dram_offset;
  2897. awe_sample_info *sp = &list->v;
  2898. int rc;
  2899. /* be sure loop points start < end */
  2900. if (sp->loopstart > sp->loopend) {
  2901. int tmp = sp->loopstart;
  2902. sp->loopstart = sp->loopend;
  2903. sp->loopend = tmp;
  2904. }
  2905. /* compute true data size to be loaded */
  2906. truesize = sp->size;
  2907. if (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP))
  2908. truesize += sp->loopend - sp->loopstart;
  2909. if (sp->mode_flags & AWE_SAMPLE_NO_BLANK)
  2910. truesize += BLANK_LOOP_SIZE;
  2911. if (awe_free_mem_ptr() + truesize >= memsize/2) {
  2912. DEBUG(-1,printk("AWE32 Error: Sample memory full\n"));
  2913. return -ENOSPC;
  2914. }
  2915. /* recalculate address offset */
  2916. sp->end -= sp->start;
  2917. sp->loopstart -= sp->start;
  2918. sp->loopend -= sp->start;
  2919. dram_offset = awe_free_mem_ptr() + awe_mem_start;
  2920. sp->start = dram_offset;
  2921. sp->end += dram_offset;
  2922. sp->loopstart += dram_offset;
  2923. sp->loopend += dram_offset;
  2924. /* set the total size (store onto obsolete checksum value) */
  2925. if (sp->size == 0)
  2926. sp->checksum = 0;
  2927. else
  2928. sp->checksum = truesize;
  2929. if ((rc = awe_open_dram_for_write(dram_offset, channels)) != 0)
  2930. return rc;
  2931. if (readbuf_init(addr, offset, sp) < 0)
  2932. return -ENOSPC;
  2933. for (i = 0; i < sp->size; i++) {
  2934. unsigned short c;
  2935. c = readbuf_word(i);
  2936. awe_write_dram(c);
  2937. if (i == sp->loopend &&
  2938. (sp->mode_flags & (AWE_SAMPLE_BIDIR_LOOP|AWE_SAMPLE_REVERSE_LOOP))) {
  2939. int looplen = sp->loopend - sp->loopstart;
  2940. /* copy reverse loop */
  2941. int k;
  2942. for (k = 1; k <= looplen; k++) {
  2943. c = readbuf_word_cache(i - k);
  2944. awe_write_dram(c);
  2945. }
  2946. if (sp->mode_flags & AWE_SAMPLE_BIDIR_LOOP) {
  2947. sp->end += looplen;
  2948. } else {
  2949. sp->start += looplen;
  2950. sp->end += looplen;
  2951. }
  2952. }
  2953. }
  2954. readbuf_end();
  2955. /* if no blank loop is attached in the sample, add it */
  2956. if (sp->mode_flags & AWE_SAMPLE_NO_BLANK) {
  2957. for (i = 0; i < BLANK_LOOP_SIZE; i++)
  2958. awe_write_dram(0);
  2959. if (sp->mode_flags & AWE_SAMPLE_SINGLESHOT) {
  2960. sp->loopstart = sp->end + BLANK_LOOP_START;
  2961. sp->loopend = sp->end + BLANK_LOOP_END;
  2962. }
  2963. }
  2964. awe_close_dram();
  2965. /* initialize FM */
  2966. awe_init_fm();
  2967. return truesize;
  2968. }
  2969. /*----------------------------------------------------------------*/
  2970. #ifdef AWE_HAS_GUS_COMPATIBILITY
  2971. /* calculate GUS envelope time:
  2972. * is this correct? i have no idea..
  2973. */
  2974. static int
  2975. calc_gus_envelope_time(int rate, int start, int end)
  2976. {
  2977. int r, p, t;
  2978. r = (3 - ((rate >> 6) & 3)) * 3;
  2979. p = rate & 0x3f;
  2980. t = end - start;
  2981. if (t < 0) t = -t;
  2982. if (13 > r)
  2983. t = t << (13 - r);
  2984. else
  2985. t = t >> (r - 13);
  2986. return (t * 10) / (p * 441);
  2987. }
  2988. #define calc_gus_sustain(val) (0x7f - vol_table[(val)/2])
  2989. #define calc_gus_attenuation(val) vol_table[(val)/2]
  2990. /* load GUS patch */
  2991. static int
  2992. awe_load_guspatch(const char __user *addr, int offs, int size, int pmgr_flag)
  2993. {
  2994. struct patch_info patch;
  2995. awe_voice_info *rec;
  2996. awe_sample_info *smp;
  2997. awe_voice_list *vrec;
  2998. awe_sample_list *smprec;
  2999. int sizeof_patch;
  3000. int note, rc;
  3001. sf_list *sf;
  3002. sizeof_patch = (int)((long)&patch.data[0] - (long)&patch); /* header size */
  3003. if (size < sizeof_patch) {
  3004. printk(KERN_WARNING "AWE32 Error: Patch header too short\n");
  3005. return -EINVAL;
  3006. }
  3007. if (copy_from_user(((char*)&patch) + offs, addr + offs, sizeof_patch - offs))
  3008. return -EFAULT;
  3009. size -= sizeof_patch;
  3010. if (size < patch.len) {
  3011. printk(KERN_WARNING "AWE32 Error: Patch record too short (%d<%d)\n",
  3012. size, patch.len);
  3013. return -EINVAL;
  3014. }
  3015. if ((sf = check_patch_opened(AWE_PAT_TYPE_GUS, NULL)) == NULL)
  3016. return -ENOMEM;
  3017. if ((smprec = alloc_new_sample()) == NULL)
  3018. return -ENOMEM;
  3019. if ((vrec = alloc_new_info()) == NULL) {
  3020. kfree(smprec);
  3021. return -ENOMEM;
  3022. }
  3023. smp = &smprec->v;
  3024. smp->sample = sf->num_sample;
  3025. smp->start = 0;
  3026. smp->end = patch.len;
  3027. smp->loopstart = patch.loop_start;
  3028. smp->loopend = patch.loop_end;
  3029. smp->size = patch.len;
  3030. /* set up mode flags */
  3031. smp->mode_flags = 0;
  3032. if (!(patch.mode & WAVE_16_BITS))
  3033. smp->mode_flags |= AWE_SAMPLE_8BITS;
  3034. if (patch.mode & WAVE_UNSIGNED)
  3035. smp->mode_flags |= AWE_SAMPLE_UNSIGNED;
  3036. smp->mode_flags |= AWE_SAMPLE_NO_BLANK;
  3037. if (!(patch.mode & (WAVE_LOOPING|WAVE_BIDIR_LOOP|WAVE_LOOP_BACK)))
  3038. smp->mode_flags |= AWE_SAMPLE_SINGLESHOT;
  3039. if (patch.mode & WAVE_BIDIR_LOOP)
  3040. smp->mode_flags |= AWE_SAMPLE_BIDIR_LOOP;
  3041. if (patch.mode & WAVE_LOOP_BACK)
  3042. smp->mode_flags |= AWE_SAMPLE_REVERSE_LOOP;
  3043. DEBUG(0,printk("AWE32: [sample %d mode %x]\n", patch.instr_no, smp->mode_flags));
  3044. if (patch.mode & WAVE_16_BITS) {
  3045. /* convert to word offsets */
  3046. smp->size /= 2;
  3047. smp->end /= 2;
  3048. smp->loopstart /= 2;
  3049. smp->loopend /= 2;
  3050. }
  3051. smp->checksum_flag = 0;
  3052. smp->checksum = 0;
  3053. if ((rc = awe_write_wave_data(addr, sizeof_patch, smprec, -1)) < 0)
  3054. return rc;
  3055. sf->mem_ptr += rc;
  3056. add_sf_sample(sf, smprec);
  3057. /* set up voice info */
  3058. rec = &vrec->v;
  3059. awe_init_voice_info(rec);
  3060. rec->sample = sf->num_info; /* the last sample */
  3061. rec->rate_offset = calc_rate_offset(patch.base_freq);
  3062. note = freq_to_note(patch.base_note);
  3063. rec->root = note / 100;
  3064. rec->tune = -(note % 100);
  3065. rec->low = freq_to_note(patch.low_note) / 100;
  3066. rec->high = freq_to_note(patch.high_note) / 100;
  3067. DEBUG(1,printk("AWE32: [gus base offset=%d, note=%d, range=%d-%d(%d-%d)]\n",
  3068. rec->rate_offset, note,
  3069. rec->low, rec->high,
  3070. patch.low_note, patch.high_note));
  3071. /* panning position; -128 - 127 => 0-127 */
  3072. rec->pan = (patch.panning + 128) / 2;
  3073. /* detuning is ignored */
  3074. /* 6points volume envelope */
  3075. if (patch.mode & WAVE_ENVELOPES) {
  3076. int attack, hold, decay, release;
  3077. attack = calc_gus_envelope_time
  3078. (patch.env_rate[0], 0, patch.env_offset[0]);
  3079. hold = calc_gus_envelope_time
  3080. (patch.env_rate[1], patch.env_offset[0],
  3081. patch.env_offset[1]);
  3082. decay = calc_gus_envelope_time
  3083. (patch.env_rate[2], patch.env_offset[1],
  3084. patch.env_offset[2]);
  3085. release = calc_gus_envelope_time
  3086. (patch.env_rate[3], patch.env_offset[1],
  3087. patch.env_offset[4]);
  3088. release += calc_gus_envelope_time
  3089. (patch.env_rate[4], patch.env_offset[3],
  3090. patch.env_offset[4]);
  3091. release += calc_gus_envelope_time
  3092. (patch.env_rate[5], patch.env_offset[4],
  3093. patch.env_offset[5]);
  3094. rec->parm.volatkhld = (calc_parm_hold(hold) << 8) |
  3095. calc_parm_attack(attack);
  3096. rec->parm.voldcysus = (calc_gus_sustain(patch.env_offset[2]) << 8) |
  3097. calc_parm_decay(decay);
  3098. rec->parm.volrelease = 0x8000 | calc_parm_decay(release);
  3099. DEBUG(2,printk("AWE32: [gusenv atk=%d, hld=%d, dcy=%d, rel=%d]\n", attack, hold, decay, release));
  3100. rec->attenuation = calc_gus_attenuation(patch.env_offset[0]);
  3101. }
  3102. /* tremolo effect */
  3103. if (patch.mode & WAVE_TREMOLO) {
  3104. int rate = (patch.tremolo_rate * 1000 / 38) / 42;
  3105. rec->parm.tremfrq = ((patch.tremolo_depth / 2) << 8) | rate;
  3106. DEBUG(2,printk("AWE32: [gusenv tremolo rate=%d, dep=%d, tremfrq=%x]\n",
  3107. patch.tremolo_rate, patch.tremolo_depth,
  3108. rec->parm.tremfrq));
  3109. }
  3110. /* vibrato effect */
  3111. if (patch.mode & WAVE_VIBRATO) {
  3112. int rate = (patch.vibrato_rate * 1000 / 38) / 42;
  3113. rec->parm.fm2frq2 = ((patch.vibrato_depth / 6) << 8) | rate;
  3114. DEBUG(2,printk("AWE32: [gusenv vibrato rate=%d, dep=%d, tremfrq=%x]\n",
  3115. patch.tremolo_rate, patch.tremolo_depth,
  3116. rec->parm.tremfrq));
  3117. }
  3118. /* scale_freq, scale_factor, volume, and fractions not implemented */
  3119. /* append to the tail of the list */
  3120. vrec->bank = ctrls[AWE_MD_GUS_BANK];
  3121. vrec->instr = patch.instr_no;
  3122. vrec->disabled = FALSE;
  3123. vrec->type = V_ST_NORMAL;
  3124. add_sf_info(sf, vrec);
  3125. add_info_list(vrec);
  3126. /* set the voice index */
  3127. awe_set_sample(vrec);
  3128. return 0;
  3129. }
  3130. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  3131. /*
  3132. * sample and voice list handlers
  3133. */
  3134. /* append this to the current sf list */
  3135. static void add_sf_info(sf_list *sf, awe_voice_list *rec)
  3136. {
  3137. if (sf == NULL)
  3138. return;
  3139. rec->holder = sf;
  3140. rec->v.sf_id = sf->sf_id;
  3141. if (sf->last_infos)
  3142. sf->last_infos->next = rec;
  3143. else
  3144. sf->infos = rec;
  3145. sf->last_infos = rec;
  3146. rec->next = NULL;
  3147. sf->num_info++;
  3148. }
  3149. /* prepend this sample to sf list */
  3150. static void add_sf_sample(sf_list *sf, awe_sample_list *rec)
  3151. {
  3152. if (sf == NULL)
  3153. return;
  3154. rec->holder = sf;
  3155. rec->v.sf_id = sf->sf_id;
  3156. if (sf->last_samples)
  3157. sf->last_samples->next = rec;
  3158. else
  3159. sf->samples = rec;
  3160. sf->last_samples = rec;
  3161. rec->next = NULL;
  3162. sf->num_sample++;
  3163. }
  3164. /* purge the old records which don't belong with the same file id */
  3165. static void purge_old_list(awe_voice_list *rec, awe_voice_list *next)
  3166. {
  3167. rec->next_instr = next;
  3168. if (rec->bank == AWE_DRUM_BANK) {
  3169. /* remove samples with the same note range */
  3170. awe_voice_list *cur, *prev = rec;
  3171. int low = rec->v.low;
  3172. int high = rec->v.high;
  3173. for (cur = next; cur; cur = cur->next_instr) {
  3174. if (cur->v.low == low &&
  3175. cur->v.high == high &&
  3176. ! is_identical_holder(cur->holder, rec->holder))
  3177. prev->next_instr = cur->next_instr;
  3178. else
  3179. prev = cur;
  3180. }
  3181. } else {
  3182. if (! is_identical_holder(next->holder, rec->holder))
  3183. /* remove all samples */
  3184. rec->next_instr = NULL;
  3185. }
  3186. }
  3187. /* prepend to top of the preset table */
  3188. static void add_info_list(awe_voice_list *rec)
  3189. {
  3190. awe_voice_list *prev, *cur;
  3191. int key;
  3192. if (rec->disabled)
  3193. return;
  3194. key = awe_search_key(rec->bank, rec->instr, rec->v.low);
  3195. prev = NULL;
  3196. for (cur = preset_table[key]; cur; cur = cur->next_bank) {
  3197. /* search the first record with the same bank number */
  3198. if (cur->instr == rec->instr && cur->bank == rec->bank) {
  3199. /* replace the list with the new record */
  3200. rec->next_bank = cur->next_bank;
  3201. if (prev)
  3202. prev->next_bank = rec;
  3203. else
  3204. preset_table[key] = rec;
  3205. purge_old_list(rec, cur);
  3206. return;
  3207. }
  3208. prev = cur;
  3209. }
  3210. /* this is the first bank record.. just add this */
  3211. rec->next_instr = NULL;
  3212. rec->next_bank = preset_table[key];
  3213. preset_table[key] = rec;
  3214. }
  3215. /* remove samples later than the specified sf_id */
  3216. static void
  3217. awe_remove_samples(int sf_id)
  3218. {
  3219. sf_list *p, *prev;
  3220. if (sf_id <= 0) {
  3221. awe_reset_samples();
  3222. return;
  3223. }
  3224. /* already removed? */
  3225. if (current_sf_id <= sf_id)
  3226. return;
  3227. for (p = sftail; p; p = prev) {
  3228. if (p->sf_id <= sf_id)
  3229. break;
  3230. prev = p->prev;
  3231. awe_free_sf(p);
  3232. }
  3233. sftail = p;
  3234. if (sftail) {
  3235. sf_id = sftail->sf_id;
  3236. sftail->next = NULL;
  3237. } else {
  3238. sf_id = 0;
  3239. sfhead = NULL;
  3240. }
  3241. current_sf_id = sf_id;
  3242. if (locked_sf_id > sf_id)
  3243. locked_sf_id = sf_id;
  3244. rebuild_preset_list();
  3245. }
  3246. /* rebuild preset search list */
  3247. static void rebuild_preset_list(void)
  3248. {
  3249. sf_list *p;
  3250. awe_voice_list *rec;
  3251. memset(preset_table, 0, sizeof(preset_table));
  3252. for (p = sfhead; p; p = p->next) {
  3253. for (rec = p->infos; rec; rec = rec->next)
  3254. add_info_list(rec);
  3255. }
  3256. }
  3257. /* compare the given sf_id pair */
  3258. static int is_identical_holder(sf_list *sf1, sf_list *sf2)
  3259. {
  3260. if (sf1 == NULL || sf2 == NULL)
  3261. return FALSE;
  3262. if (sf1 == sf2)
  3263. return TRUE;
  3264. #ifdef AWE_ALLOW_SAMPLE_SHARING
  3265. {
  3266. /* compare with the sharing id */
  3267. sf_list *p;
  3268. int counter = 0;
  3269. if (sf1->sf_id < sf2->sf_id) { /* make sure id1 > id2 */
  3270. sf_list *tmp; tmp = sf1; sf1 = sf2; sf2 = tmp;
  3271. }
  3272. for (p = sf1->shared; p; p = p->shared) {
  3273. if (counter++ > current_sf_id)
  3274. break; /* strange sharing loop.. quit */
  3275. if (p == sf2)
  3276. return TRUE;
  3277. }
  3278. }
  3279. #endif /* allow sharing */
  3280. return FALSE;
  3281. }
  3282. /* search the sample index matching with the given sample id */
  3283. static awe_sample_list *
  3284. search_sample_index(sf_list *sf, int sample)
  3285. {
  3286. awe_sample_list *p;
  3287. #ifdef AWE_ALLOW_SAMPLE_SHARING
  3288. int counter = 0;
  3289. while (sf) {
  3290. for (p = sf->samples; p; p = p->next) {
  3291. if (p->v.sample == sample)
  3292. return p;
  3293. }
  3294. sf = sf->shared;
  3295. if (counter++ > current_sf_id)
  3296. break; /* strange sharing loop.. quit */
  3297. }
  3298. #else
  3299. if (sf) {
  3300. for (p = sf->samples; p; p = p->next) {
  3301. if (p->v.sample == sample)
  3302. return p;
  3303. }
  3304. }
  3305. #endif
  3306. return NULL;
  3307. }
  3308. /* search the specified sample */
  3309. /* non-zero = found */
  3310. static short
  3311. awe_set_sample(awe_voice_list *rec)
  3312. {
  3313. awe_sample_list *smp;
  3314. awe_voice_info *vp = &rec->v;
  3315. vp->index = 0;
  3316. if ((smp = search_sample_index(rec->holder, vp->sample)) == NULL)
  3317. return 0;
  3318. /* set the actual sample offsets */
  3319. vp->start += smp->v.start;
  3320. vp->end += smp->v.end;
  3321. vp->loopstart += smp->v.loopstart;
  3322. vp->loopend += smp->v.loopend;
  3323. /* copy mode flags */
  3324. vp->mode = smp->v.mode_flags;
  3325. /* set flag */
  3326. vp->index = 1;
  3327. return 1;
  3328. }
  3329. /*
  3330. * voice allocation
  3331. */
  3332. /* look for all voices associated with the specified note & velocity */
  3333. static int
  3334. awe_search_multi_voices(awe_voice_list *rec, int note, int velocity,
  3335. awe_voice_info **vlist)
  3336. {
  3337. int nvoices;
  3338. nvoices = 0;
  3339. for (; rec; rec = rec->next_instr) {
  3340. if (note >= rec->v.low &&
  3341. note <= rec->v.high &&
  3342. velocity >= rec->v.vellow &&
  3343. velocity <= rec->v.velhigh) {
  3344. if (rec->type == V_ST_MAPPED) {
  3345. /* mapper */
  3346. vlist[0] = &rec->v;
  3347. return -1;
  3348. }
  3349. vlist[nvoices++] = &rec->v;
  3350. if (nvoices >= AWE_MAX_VOICES)
  3351. break;
  3352. }
  3353. }
  3354. return nvoices;
  3355. }
  3356. /* store the voice list from the specified note and velocity.
  3357. if the preset is mapped, seek for the destination preset, and rewrite
  3358. the note number if necessary.
  3359. */
  3360. static int
  3361. really_alloc_voices(int bank, int instr, int *note, int velocity, awe_voice_info **vlist)
  3362. {
  3363. int nvoices;
  3364. awe_voice_list *vrec;
  3365. int level = 0;
  3366. for (;;) {
  3367. vrec = awe_search_instr(bank, instr, *note);
  3368. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3369. if (nvoices == 0) {
  3370. if (bank == AWE_DRUM_BANK)
  3371. /* search default drumset */
  3372. vrec = awe_search_instr(bank, ctrls[AWE_MD_DEF_DRUM], *note);
  3373. else
  3374. /* search default preset */
  3375. vrec = awe_search_instr(ctrls[AWE_MD_DEF_BANK], instr, *note);
  3376. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3377. }
  3378. if (nvoices == 0) {
  3379. if (bank == AWE_DRUM_BANK && ctrls[AWE_MD_DEF_DRUM] != 0)
  3380. /* search default drumset */
  3381. vrec = awe_search_instr(bank, 0, *note);
  3382. else if (bank != AWE_DRUM_BANK && ctrls[AWE_MD_DEF_BANK] != 0)
  3383. /* search default preset */
  3384. vrec = awe_search_instr(0, instr, *note);
  3385. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3386. }
  3387. if (nvoices < 0) { /* mapping */
  3388. int key = vlist[0]->fixkey;
  3389. instr = vlist[0]->start;
  3390. bank = vlist[0]->end;
  3391. if (level++ > 5) {
  3392. printk(KERN_ERR "AWE32: too deep mapping level\n");
  3393. return 0;
  3394. }
  3395. if (key >= 0)
  3396. *note = key;
  3397. } else
  3398. break;
  3399. }
  3400. return nvoices;
  3401. }
  3402. /* allocate voices corresponding note and velocity; supports multiple insts. */
  3403. static void
  3404. awe_alloc_multi_voices(int ch, int note, int velocity, int key)
  3405. {
  3406. int i, v, nvoices, bank;
  3407. awe_voice_info *vlist[AWE_MAX_VOICES];
  3408. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch))
  3409. bank = AWE_DRUM_BANK; /* always search drumset */
  3410. else
  3411. bank = channels[ch].bank;
  3412. /* check the possible voices; note may be changeable if mapped */
  3413. nvoices = really_alloc_voices(bank, channels[ch].instr,
  3414. &note, velocity, vlist);
  3415. /* set the voices */
  3416. current_alloc_time++;
  3417. for (i = 0; i < nvoices; i++) {
  3418. v = awe_clear_voice();
  3419. voices[v].key = key;
  3420. voices[v].ch = ch;
  3421. voices[v].note = note;
  3422. voices[v].velocity = velocity;
  3423. voices[v].time = current_alloc_time;
  3424. voices[v].cinfo = &channels[ch];
  3425. voices[v].sample = vlist[i];
  3426. voices[v].state = AWE_ST_MARK;
  3427. voices[v].layer = nvoices - i - 1; /* in reverse order */
  3428. }
  3429. /* clear the mark in allocated voices */
  3430. for (i = 0; i < awe_max_voices; i++) {
  3431. if (voices[i].state == AWE_ST_MARK)
  3432. voices[i].state = AWE_ST_OFF;
  3433. }
  3434. }
  3435. /* search an empty voice.
  3436. if no empty voice is found, at least terminate a voice
  3437. */
  3438. static int
  3439. awe_clear_voice(void)
  3440. {
  3441. enum {
  3442. OFF=0, RELEASED, SUSTAINED, PLAYING, END
  3443. };
  3444. struct voice_candidate_t {
  3445. int best;
  3446. int time;
  3447. int vtarget;
  3448. } candidate[END];
  3449. int i, type, vtarget;
  3450. vtarget = 0xffff;
  3451. for (type = OFF; type < END; type++) {
  3452. candidate[type].best = -1;
  3453. candidate[type].time = current_alloc_time + 1;
  3454. candidate[type].vtarget = vtarget;
  3455. }
  3456. for (i = 0; i < awe_max_voices; i++) {
  3457. if (voices[i].state & AWE_ST_OFF)
  3458. type = OFF;
  3459. else if (voices[i].state & AWE_ST_RELEASED)
  3460. type = RELEASED;
  3461. else if (voices[i].state & AWE_ST_SUSTAINED)
  3462. type = SUSTAINED;
  3463. else if (voices[i].state & ~AWE_ST_MARK)
  3464. type = PLAYING;
  3465. else
  3466. continue;
  3467. #ifdef AWE_CHECK_VTARGET
  3468. /* get current volume */
  3469. vtarget = (awe_peek_dw(AWE_VTFT(i)) >> 16) & 0xffff;
  3470. #endif
  3471. if (candidate[type].best < 0 ||
  3472. vtarget < candidate[type].vtarget ||
  3473. (vtarget == candidate[type].vtarget &&
  3474. voices[i].time < candidate[type].time)) {
  3475. candidate[type].best = i;
  3476. candidate[type].time = voices[i].time;
  3477. candidate[type].vtarget = vtarget;
  3478. }
  3479. }
  3480. for (type = OFF; type < END; type++) {
  3481. if ((i = candidate[type].best) >= 0) {
  3482. if (voices[i].state != AWE_ST_OFF)
  3483. awe_terminate(i);
  3484. awe_voice_init(i, TRUE);
  3485. return i;
  3486. }
  3487. }
  3488. return 0;
  3489. }
  3490. /* search sample for the specified note & velocity and set it on the voice;
  3491. * note that voice is the voice index (not channel index)
  3492. */
  3493. static void
  3494. awe_alloc_one_voice(int voice, int note, int velocity)
  3495. {
  3496. int ch, nvoices, bank;
  3497. awe_voice_info *vlist[AWE_MAX_VOICES];
  3498. ch = voices[voice].ch;
  3499. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice))
  3500. bank = AWE_DRUM_BANK; /* always search drumset */
  3501. else
  3502. bank = voices[voice].cinfo->bank;
  3503. nvoices = really_alloc_voices(bank, voices[voice].cinfo->instr,
  3504. &note, velocity, vlist);
  3505. if (nvoices > 0) {
  3506. voices[voice].time = ++current_alloc_time;
  3507. voices[voice].sample = vlist[0]; /* use the first one */
  3508. voices[voice].layer = 0;
  3509. voices[voice].note = note;
  3510. voices[voice].velocity = velocity;
  3511. }
  3512. }
  3513. /*
  3514. * sequencer2 functions
  3515. */
  3516. /* search an empty voice; used by sequencer2 */
  3517. static int
  3518. awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
  3519. {
  3520. playing_mode = AWE_PLAY_MULTI2;
  3521. awe_info.nr_voices = AWE_MAX_CHANNELS;
  3522. return awe_clear_voice();
  3523. }
  3524. /* set up voice; used by sequencer2 */
  3525. static void
  3526. awe_setup_voice(int dev, int voice, int chn)
  3527. {
  3528. struct channel_info *info;
  3529. if (synth_devs[dev] == NULL ||
  3530. (info = &synth_devs[dev]->chn_info[chn]) == NULL)
  3531. return;
  3532. if (voice < 0 || voice >= awe_max_voices)
  3533. return;
  3534. DEBUG(2,printk("AWE32: [setup(%d) ch=%d]\n", voice, chn));
  3535. channels[chn].expression_vol = info->controllers[CTL_EXPRESSION];
  3536. channels[chn].main_vol = info->controllers[CTL_MAIN_VOLUME];
  3537. channels[chn].panning =
  3538. info->controllers[CTL_PAN] * 2 - 128; /* signed 8bit */
  3539. channels[chn].bender = info->bender_value; /* zero center */
  3540. channels[chn].bank = info->controllers[CTL_BANK_SELECT];
  3541. channels[chn].sustained = info->controllers[CTL_SUSTAIN];
  3542. if (info->controllers[CTL_EXT_EFF_DEPTH]) {
  3543. FX_SET(&channels[chn].fx, AWE_FX_REVERB,
  3544. info->controllers[CTL_EXT_EFF_DEPTH] * 2);
  3545. }
  3546. if (info->controllers[CTL_CHORUS_DEPTH]) {
  3547. FX_SET(&channels[chn].fx, AWE_FX_CHORUS,
  3548. info->controllers[CTL_CHORUS_DEPTH] * 2);
  3549. }
  3550. awe_set_instr(dev, chn, info->pgm_num);
  3551. }
  3552. #ifdef CONFIG_AWE32_MIXER
  3553. /*
  3554. * AWE32 mixer device control
  3555. */
  3556. static int awe_mixer_ioctl(int dev, unsigned int cmd, void __user *arg);
  3557. static int my_mixerdev = -1;
  3558. static struct mixer_operations awe_mixer_operations = {
  3559. .owner = THIS_MODULE,
  3560. .id = "AWE",
  3561. .name = "AWE32 Equalizer",
  3562. .ioctl = awe_mixer_ioctl,
  3563. };
  3564. static void __init attach_mixer(void)
  3565. {
  3566. if ((my_mixerdev = sound_alloc_mixerdev()) >= 0) {
  3567. mixer_devs[my_mixerdev] = &awe_mixer_operations;
  3568. }
  3569. }
  3570. static void unload_mixer(void)
  3571. {
  3572. if (my_mixerdev >= 0)
  3573. sound_unload_mixerdev(my_mixerdev);
  3574. }
  3575. static int
  3576. awe_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
  3577. {
  3578. int i, level, value;
  3579. if (((cmd >> 8) & 0xff) != 'M')
  3580. return -EINVAL;
  3581. if (get_user(level, (int __user *)arg))
  3582. return -EFAULT;
  3583. level = ((level & 0xff) + (level >> 8)) / 2;
  3584. DEBUG(0,printk("AWEMix: cmd=%x val=%d\n", cmd & 0xff, level));
  3585. if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
  3586. switch (cmd & 0xff) {
  3587. case SOUND_MIXER_BASS:
  3588. value = level * 12 / 100;
  3589. if (value >= 12)
  3590. value = 11;
  3591. ctrls[AWE_MD_BASS_LEVEL] = value;
  3592. awe_update_equalizer();
  3593. break;
  3594. case SOUND_MIXER_TREBLE:
  3595. value = level * 12 / 100;
  3596. if (value >= 12)
  3597. value = 11;
  3598. ctrls[AWE_MD_TREBLE_LEVEL] = value;
  3599. awe_update_equalizer();
  3600. break;
  3601. case SOUND_MIXER_VOLUME:
  3602. level = level * 127 / 100;
  3603. if (level >= 128) level = 127;
  3604. atten_relative = FALSE;
  3605. atten_offset = vol_table[level];
  3606. awe_update_volume();
  3607. break;
  3608. }
  3609. }
  3610. switch (cmd & 0xff) {
  3611. case SOUND_MIXER_BASS:
  3612. level = ctrls[AWE_MD_BASS_LEVEL] * 100 / 24;
  3613. level = (level << 8) | level;
  3614. break;
  3615. case SOUND_MIXER_TREBLE:
  3616. level = ctrls[AWE_MD_TREBLE_LEVEL] * 100 / 24;
  3617. level = (level << 8) | level;
  3618. break;
  3619. case SOUND_MIXER_VOLUME:
  3620. value = atten_offset;
  3621. if (atten_relative)
  3622. value += ctrls[AWE_MD_ZERO_ATTEN];
  3623. for (i = 127; i > 0; i--) {
  3624. if (value <= vol_table[i])
  3625. break;
  3626. }
  3627. level = i * 100 / 127;
  3628. level = (level << 8) | level;
  3629. break;
  3630. case SOUND_MIXER_DEVMASK:
  3631. level = SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_VOLUME;
  3632. break;
  3633. default:
  3634. level = 0;
  3635. break;
  3636. }
  3637. if (put_user(level, (int __user *)arg))
  3638. return -EFAULT;
  3639. return level;
  3640. }
  3641. #endif /* CONFIG_AWE32_MIXER */
  3642. /*
  3643. * initialization of Emu8000
  3644. */
  3645. /* intiailize audio channels */
  3646. static void
  3647. awe_init_audio(void)
  3648. {
  3649. int ch;
  3650. /* turn off envelope engines */
  3651. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3652. awe_poke(AWE_DCYSUSV(ch), 0x80);
  3653. }
  3654. /* reset all other parameters to zero */
  3655. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3656. awe_poke(AWE_ENVVOL(ch), 0);
  3657. awe_poke(AWE_ENVVAL(ch), 0);
  3658. awe_poke(AWE_DCYSUS(ch), 0);
  3659. awe_poke(AWE_ATKHLDV(ch), 0);
  3660. awe_poke(AWE_LFO1VAL(ch), 0);
  3661. awe_poke(AWE_ATKHLD(ch), 0);
  3662. awe_poke(AWE_LFO2VAL(ch), 0);
  3663. awe_poke(AWE_IP(ch), 0);
  3664. awe_poke(AWE_IFATN(ch), 0);
  3665. awe_poke(AWE_PEFE(ch), 0);
  3666. awe_poke(AWE_FMMOD(ch), 0);
  3667. awe_poke(AWE_TREMFRQ(ch), 0);
  3668. awe_poke(AWE_FM2FRQ2(ch), 0);
  3669. awe_poke_dw(AWE_PTRX(ch), 0);
  3670. awe_poke_dw(AWE_VTFT(ch), 0);
  3671. awe_poke_dw(AWE_PSST(ch), 0);
  3672. awe_poke_dw(AWE_CSL(ch), 0);
  3673. awe_poke_dw(AWE_CCCA(ch), 0);
  3674. }
  3675. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3676. awe_poke_dw(AWE_CPF(ch), 0);
  3677. awe_poke_dw(AWE_CVCF(ch), 0);
  3678. }
  3679. }
  3680. /* initialize DMA address */
  3681. static void
  3682. awe_init_dma(void)
  3683. {
  3684. awe_poke_dw(AWE_SMALR, 0);
  3685. awe_poke_dw(AWE_SMARR, 0);
  3686. awe_poke_dw(AWE_SMALW, 0);
  3687. awe_poke_dw(AWE_SMARW, 0);
  3688. }
  3689. /* initialization arrays; from ADIP */
  3690. static unsigned short init1[128] = {
  3691. 0x03ff, 0x0030, 0x07ff, 0x0130, 0x0bff, 0x0230, 0x0fff, 0x0330,
  3692. 0x13ff, 0x0430, 0x17ff, 0x0530, 0x1bff, 0x0630, 0x1fff, 0x0730,
  3693. 0x23ff, 0x0830, 0x27ff, 0x0930, 0x2bff, 0x0a30, 0x2fff, 0x0b30,
  3694. 0x33ff, 0x0c30, 0x37ff, 0x0d30, 0x3bff, 0x0e30, 0x3fff, 0x0f30,
  3695. 0x43ff, 0x0030, 0x47ff, 0x0130, 0x4bff, 0x0230, 0x4fff, 0x0330,
  3696. 0x53ff, 0x0430, 0x57ff, 0x0530, 0x5bff, 0x0630, 0x5fff, 0x0730,
  3697. 0x63ff, 0x0830, 0x67ff, 0x0930, 0x6bff, 0x0a30, 0x6fff, 0x0b30,
  3698. 0x73ff, 0x0c30, 0x77ff, 0x0d30, 0x7bff, 0x0e30, 0x7fff, 0x0f30,
  3699. 0x83ff, 0x0030, 0x87ff, 0x0130, 0x8bff, 0x0230, 0x8fff, 0x0330,
  3700. 0x93ff, 0x0430, 0x97ff, 0x0530, 0x9bff, 0x0630, 0x9fff, 0x0730,
  3701. 0xa3ff, 0x0830, 0xa7ff, 0x0930, 0xabff, 0x0a30, 0xafff, 0x0b30,
  3702. 0xb3ff, 0x0c30, 0xb7ff, 0x0d30, 0xbbff, 0x0e30, 0xbfff, 0x0f30,
  3703. 0xc3ff, 0x0030, 0xc7ff, 0x0130, 0xcbff, 0x0230, 0xcfff, 0x0330,
  3704. 0xd3ff, 0x0430, 0xd7ff, 0x0530, 0xdbff, 0x0630, 0xdfff, 0x0730,
  3705. 0xe3ff, 0x0830, 0xe7ff, 0x0930, 0xebff, 0x0a30, 0xefff, 0x0b30,
  3706. 0xf3ff, 0x0c30, 0xf7ff, 0x0d30, 0xfbff, 0x0e30, 0xffff, 0x0f30,
  3707. };
  3708. static unsigned short init2[128] = {
  3709. 0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330,
  3710. 0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730,
  3711. 0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30,
  3712. 0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30,
  3713. 0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330,
  3714. 0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730,
  3715. 0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30,
  3716. 0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30,
  3717. 0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330,
  3718. 0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730,
  3719. 0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30,
  3720. 0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30,
  3721. 0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330,
  3722. 0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730,
  3723. 0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30,
  3724. 0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30,
  3725. };
  3726. static unsigned short init3[128] = {
  3727. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  3728. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254,
  3729. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234,
  3730. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224,
  3731. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254,
  3732. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264,
  3733. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294,
  3734. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3,
  3735. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287,
  3736. 0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7,
  3737. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386,
  3738. 0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55,
  3739. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308,
  3740. 0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F,
  3741. 0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319,
  3742. 0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570,
  3743. };
  3744. static unsigned short init4[128] = {
  3745. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  3746. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254,
  3747. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234,
  3748. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224,
  3749. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254,
  3750. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264,
  3751. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294,
  3752. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3,
  3753. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287,
  3754. 0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7,
  3755. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386,
  3756. 0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55,
  3757. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308,
  3758. 0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F,
  3759. 0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319,
  3760. 0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570,
  3761. };
  3762. /* send initialization arrays to start up */
  3763. static void
  3764. awe_init_array(void)
  3765. {
  3766. awe_send_array(init1);
  3767. awe_wait(1024);
  3768. awe_send_array(init2);
  3769. awe_send_array(init3);
  3770. awe_poke_dw(AWE_HWCF4, 0);
  3771. awe_poke_dw(AWE_HWCF5, 0x83);
  3772. awe_poke_dw(AWE_HWCF6, 0x8000);
  3773. awe_send_array(init4);
  3774. }
  3775. /* send an initialization array */
  3776. static void
  3777. awe_send_array(unsigned short *data)
  3778. {
  3779. int i;
  3780. unsigned short *p;
  3781. p = data;
  3782. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3783. awe_poke(AWE_INIT1(i), *p);
  3784. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3785. awe_poke(AWE_INIT2(i), *p);
  3786. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3787. awe_poke(AWE_INIT3(i), *p);
  3788. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3789. awe_poke(AWE_INIT4(i), *p);
  3790. }
  3791. /*
  3792. * set up awe32 channels to some known state.
  3793. */
  3794. /* set the envelope & LFO parameters to the default values; see ADIP */
  3795. static void
  3796. awe_tweak_voice(int i)
  3797. {
  3798. /* set all mod/vol envelope shape to minimum */
  3799. awe_poke(AWE_ENVVOL(i), 0x8000);
  3800. awe_poke(AWE_ENVVAL(i), 0x8000);
  3801. awe_poke(AWE_DCYSUS(i), 0x7F7F);
  3802. awe_poke(AWE_ATKHLDV(i), 0x7F7F);
  3803. awe_poke(AWE_ATKHLD(i), 0x7F7F);
  3804. awe_poke(AWE_PEFE(i), 0); /* mod envelope height to zero */
  3805. awe_poke(AWE_LFO1VAL(i), 0x8000); /* no delay for LFO1 */
  3806. awe_poke(AWE_LFO2VAL(i), 0x8000);
  3807. awe_poke(AWE_IP(i), 0xE000); /* no pitch shift */
  3808. awe_poke(AWE_IFATN(i), 0xFF00); /* volume to minimum */
  3809. awe_poke(AWE_FMMOD(i), 0);
  3810. awe_poke(AWE_TREMFRQ(i), 0);
  3811. awe_poke(AWE_FM2FRQ2(i), 0);
  3812. }
  3813. static void
  3814. awe_tweak(void)
  3815. {
  3816. int i;
  3817. /* reset all channels */
  3818. for (i = 0; i < awe_max_voices; i++)
  3819. awe_tweak_voice(i);
  3820. }
  3821. /*
  3822. * initializes the FM section of AWE32;
  3823. * see Vince Vu's unofficial AWE32 programming guide
  3824. */
  3825. static void
  3826. awe_init_fm(void)
  3827. {
  3828. #ifndef AWE_ALWAYS_INIT_FM
  3829. /* if no extended memory is on board.. */
  3830. if (memsize <= 0)
  3831. return;
  3832. #endif
  3833. DEBUG(3,printk("AWE32: initializing FM\n"));
  3834. /* Initialize the last two channels for DRAM refresh and producing
  3835. the reverb and chorus effects for Yamaha OPL-3 synthesizer */
  3836. /* 31: FM left channel, 0xffffe0-0xffffe8 */
  3837. awe_poke(AWE_DCYSUSV(30), 0x80);
  3838. awe_poke_dw(AWE_PSST(30), 0xFFFFFFE0); /* full left */
  3839. awe_poke_dw(AWE_CSL(30), 0x00FFFFE8 |
  3840. (DEF_FM_CHORUS_DEPTH << 24));
  3841. awe_poke_dw(AWE_PTRX(30), (DEF_FM_REVERB_DEPTH << 8));
  3842. awe_poke_dw(AWE_CPF(30), 0);
  3843. awe_poke_dw(AWE_CCCA(30), 0x00FFFFE3);
  3844. /* 32: FM right channel, 0xfffff0-0xfffff8 */
  3845. awe_poke(AWE_DCYSUSV(31), 0x80);
  3846. awe_poke_dw(AWE_PSST(31), 0x00FFFFF0); /* full right */
  3847. awe_poke_dw(AWE_CSL(31), 0x00FFFFF8 |
  3848. (DEF_FM_CHORUS_DEPTH << 24));
  3849. awe_poke_dw(AWE_PTRX(31), (DEF_FM_REVERB_DEPTH << 8));
  3850. awe_poke_dw(AWE_CPF(31), 0x8000);
  3851. awe_poke_dw(AWE_CCCA(31), 0x00FFFFF3);
  3852. /* skew volume & cutoff */
  3853. awe_poke_dw(AWE_VTFT(30), 0x8000FFFF);
  3854. awe_poke_dw(AWE_VTFT(31), 0x8000FFFF);
  3855. voices[30].state = AWE_ST_FM;
  3856. voices[31].state = AWE_ST_FM;
  3857. /* change maximum channels to 30 */
  3858. awe_max_voices = AWE_NORMAL_VOICES;
  3859. if (playing_mode == AWE_PLAY_DIRECT)
  3860. awe_info.nr_voices = awe_max_voices;
  3861. else
  3862. awe_info.nr_voices = AWE_MAX_CHANNELS;
  3863. voice_alloc->max_voice = awe_max_voices;
  3864. }
  3865. /*
  3866. * AWE32 DRAM access routines
  3867. */
  3868. /* open DRAM write accessing mode */
  3869. static int
  3870. awe_open_dram_for_write(int offset, int channels)
  3871. {
  3872. int vidx[AWE_NORMAL_VOICES];
  3873. int i;
  3874. if (channels < 0 || channels >= AWE_NORMAL_VOICES) {
  3875. channels = AWE_NORMAL_VOICES;
  3876. for (i = 0; i < AWE_NORMAL_VOICES; i++)
  3877. vidx[i] = i;
  3878. } else {
  3879. for (i = 0; i < channels; i++) {
  3880. vidx[i] = awe_clear_voice();
  3881. voices[vidx[i]].state = AWE_ST_MARK;
  3882. }
  3883. }
  3884. /* use all channels for DMA transfer */
  3885. for (i = 0; i < channels; i++) {
  3886. if (vidx[i] < 0) continue;
  3887. awe_poke(AWE_DCYSUSV(vidx[i]), 0x80);
  3888. awe_poke_dw(AWE_VTFT(vidx[i]), 0);
  3889. awe_poke_dw(AWE_CVCF(vidx[i]), 0);
  3890. awe_poke_dw(AWE_PTRX(vidx[i]), 0x40000000);
  3891. awe_poke_dw(AWE_CPF(vidx[i]), 0x40000000);
  3892. awe_poke_dw(AWE_PSST(vidx[i]), 0);
  3893. awe_poke_dw(AWE_CSL(vidx[i]), 0);
  3894. awe_poke_dw(AWE_CCCA(vidx[i]), 0x06000000);
  3895. voices[vidx[i]].state = AWE_ST_DRAM;
  3896. }
  3897. /* point channels 31 & 32 to ROM samples for DRAM refresh */
  3898. awe_poke_dw(AWE_VTFT(30), 0);
  3899. awe_poke_dw(AWE_PSST(30), 0x1d8);
  3900. awe_poke_dw(AWE_CSL(30), 0x1e0);
  3901. awe_poke_dw(AWE_CCCA(30), 0x1d8);
  3902. awe_poke_dw(AWE_VTFT(31), 0);
  3903. awe_poke_dw(AWE_PSST(31), 0x1d8);
  3904. awe_poke_dw(AWE_CSL(31), 0x1e0);
  3905. awe_poke_dw(AWE_CCCA(31), 0x1d8);
  3906. voices[30].state = AWE_ST_FM;
  3907. voices[31].state = AWE_ST_FM;
  3908. /* if full bit is on, not ready to write on */
  3909. if (awe_peek_dw(AWE_SMALW) & 0x80000000) {
  3910. for (i = 0; i < channels; i++) {
  3911. awe_poke_dw(AWE_CCCA(vidx[i]), 0);
  3912. voices[vidx[i]].state = AWE_ST_OFF;
  3913. }
  3914. printk("awe: not ready to write..\n");
  3915. return -EPERM;
  3916. }
  3917. /* set address to write */
  3918. awe_poke_dw(AWE_SMALW, offset);
  3919. return 0;
  3920. }
  3921. /* open DRAM for RAM size detection */
  3922. static void
  3923. awe_open_dram_for_check(void)
  3924. {
  3925. int i;
  3926. for (i = 0; i < AWE_NORMAL_VOICES; i++) {
  3927. awe_poke(AWE_DCYSUSV(i), 0x80);
  3928. awe_poke_dw(AWE_VTFT(i), 0);
  3929. awe_poke_dw(AWE_CVCF(i), 0);
  3930. awe_poke_dw(AWE_PTRX(i), 0x40000000);
  3931. awe_poke_dw(AWE_CPF(i), 0x40000000);
  3932. awe_poke_dw(AWE_PSST(i), 0);
  3933. awe_poke_dw(AWE_CSL(i), 0);
  3934. if (i & 1) /* DMA write */
  3935. awe_poke_dw(AWE_CCCA(i), 0x06000000);
  3936. else /* DMA read */
  3937. awe_poke_dw(AWE_CCCA(i), 0x04000000);
  3938. voices[i].state = AWE_ST_DRAM;
  3939. }
  3940. }
  3941. /* close dram access */
  3942. static void
  3943. awe_close_dram(void)
  3944. {
  3945. int i;
  3946. /* wait until FULL bit in SMAxW register be false */
  3947. for (i = 0; i < 10000; i++) {
  3948. if (!(awe_peek_dw(AWE_SMALW) & 0x80000000))
  3949. break;
  3950. awe_wait(10);
  3951. }
  3952. for (i = 0; i < AWE_NORMAL_VOICES; i++) {
  3953. if (voices[i].state == AWE_ST_DRAM) {
  3954. awe_poke_dw(AWE_CCCA(i), 0);
  3955. awe_poke(AWE_DCYSUSV(i), 0x807F);
  3956. voices[i].state = AWE_ST_OFF;
  3957. }
  3958. }
  3959. }
  3960. /*
  3961. * check dram size on AWE board
  3962. */
  3963. /* any three numbers you like */
  3964. #define UNIQUE_ID1 0x1234
  3965. #define UNIQUE_ID2 0x4321
  3966. #define UNIQUE_ID3 0xABCD
  3967. static void __init
  3968. awe_check_dram(void)
  3969. {
  3970. if (awe_present) /* already initialized */
  3971. return;
  3972. if (memsize >= 0) { /* given by config file or module option */
  3973. memsize *= 1024; /* convert to Kbytes */
  3974. return;
  3975. }
  3976. awe_open_dram_for_check();
  3977. memsize = 0;
  3978. /* set up unique two id numbers */
  3979. awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET);
  3980. awe_poke(AWE_SMLD, UNIQUE_ID1);
  3981. awe_poke(AWE_SMLD, UNIQUE_ID2);
  3982. while (memsize < AWE_MAX_DRAM_SIZE) {
  3983. awe_wait(5);
  3984. /* read a data on the DRAM start address */
  3985. awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET);
  3986. awe_peek(AWE_SMLD); /* discard stale data */
  3987. if (awe_peek(AWE_SMLD) != UNIQUE_ID1)
  3988. break;
  3989. if (awe_peek(AWE_SMLD) != UNIQUE_ID2)
  3990. break;
  3991. memsize += 512; /* increment 512kbytes */
  3992. /* Write a unique data on the test address;
  3993. * if the address is out of range, the data is written on
  3994. * 0x200000(=AWE_DRAM_OFFSET). Then the two id words are
  3995. * broken by this data.
  3996. */
  3997. awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET + memsize*512L);
  3998. awe_poke(AWE_SMLD, UNIQUE_ID3);
  3999. awe_wait(5);
  4000. /* read a data on the just written DRAM address */
  4001. awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET + memsize*512L);
  4002. awe_peek(AWE_SMLD); /* discard stale data */
  4003. if (awe_peek(AWE_SMLD) != UNIQUE_ID3)
  4004. break;
  4005. }
  4006. awe_close_dram();
  4007. DEBUG(0,printk("AWE32: %d Kbytes memory detected\n", memsize));
  4008. /* convert to Kbytes */
  4009. memsize *= 1024;
  4010. }
  4011. /*----------------------------------------------------------------*/
  4012. /*
  4013. * chorus and reverb controls; from VV's guide
  4014. */
  4015. /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */
  4016. static char chorus_defined[AWE_CHORUS_NUMBERS];
  4017. static awe_chorus_fx_rec chorus_parm[AWE_CHORUS_NUMBERS] = {
  4018. {0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
  4019. {0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
  4020. {0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
  4021. {0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
  4022. {0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
  4023. {0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
  4024. {0xE600, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay */
  4025. {0xE6C0, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay + feedback */
  4026. };
  4027. static int
  4028. awe_load_chorus_fx(awe_patch_info *patch, const char __user *addr, int count)
  4029. {
  4030. if (patch->optarg < AWE_CHORUS_PREDEFINED || patch->optarg >= AWE_CHORUS_NUMBERS) {
  4031. printk(KERN_WARNING "AWE32 Error: invalid chorus mode %d for uploading\n", patch->optarg);
  4032. return -EINVAL;
  4033. }
  4034. if (count < sizeof(awe_chorus_fx_rec)) {
  4035. printk(KERN_WARNING "AWE32 Error: too short chorus fx parameters\n");
  4036. return -EINVAL;
  4037. }
  4038. if (copy_from_user(&chorus_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
  4039. sizeof(awe_chorus_fx_rec)))
  4040. return -EFAULT;
  4041. chorus_defined[patch->optarg] = TRUE;
  4042. return 0;
  4043. }
  4044. static void
  4045. awe_set_chorus_mode(int effect)
  4046. {
  4047. if (effect < 0 || effect >= AWE_CHORUS_NUMBERS ||
  4048. (effect >= AWE_CHORUS_PREDEFINED && !chorus_defined[effect]))
  4049. return;
  4050. awe_poke(AWE_INIT3(9), chorus_parm[effect].feedback);
  4051. awe_poke(AWE_INIT3(12), chorus_parm[effect].delay_offset);
  4052. awe_poke(AWE_INIT4(3), chorus_parm[effect].lfo_depth);
  4053. awe_poke_dw(AWE_HWCF4, chorus_parm[effect].delay);
  4054. awe_poke_dw(AWE_HWCF5, chorus_parm[effect].lfo_freq);
  4055. awe_poke_dw(AWE_HWCF6, 0x8000);
  4056. awe_poke_dw(AWE_HWCF7, 0x0000);
  4057. }
  4058. static void
  4059. awe_update_chorus_mode(void)
  4060. {
  4061. awe_set_chorus_mode(ctrls[AWE_MD_CHORUS_MODE]);
  4062. }
  4063. /*----------------------------------------------------------------*/
  4064. /* reverb mode settings; write the following 28 data of 16 bit length
  4065. * on the corresponding ports in the reverb_cmds array
  4066. */
  4067. static char reverb_defined[AWE_CHORUS_NUMBERS];
  4068. static awe_reverb_fx_rec reverb_parm[AWE_REVERB_NUMBERS] = {
  4069. {{ /* room 1 */
  4070. 0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
  4071. 0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
  4072. 0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4073. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4074. }},
  4075. {{ /* room 2 */
  4076. 0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4077. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  4078. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4079. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4080. }},
  4081. {{ /* room 3 */
  4082. 0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4083. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
  4084. 0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
  4085. 0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
  4086. }},
  4087. {{ /* hall 1 */
  4088. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4089. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  4090. 0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
  4091. 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
  4092. }},
  4093. {{ /* hall 2 */
  4094. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
  4095. 0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
  4096. 0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4097. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4098. }},
  4099. {{ /* plate */
  4100. 0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
  4101. 0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
  4102. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4103. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4104. }},
  4105. {{ /* delay */
  4106. 0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
  4107. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  4108. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  4109. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  4110. }},
  4111. {{ /* panning delay */
  4112. 0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
  4113. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  4114. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  4115. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  4116. }},
  4117. };
  4118. static struct ReverbCmdPair {
  4119. unsigned short cmd, port;
  4120. } reverb_cmds[28] = {
  4121. {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)},
  4122. {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)},
  4123. {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)},
  4124. {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)},
  4125. {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)},
  4126. {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)},
  4127. {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)},
  4128. };
  4129. static int
  4130. awe_load_reverb_fx(awe_patch_info *patch, const char __user *addr, int count)
  4131. {
  4132. if (patch->optarg < AWE_REVERB_PREDEFINED || patch->optarg >= AWE_REVERB_NUMBERS) {
  4133. printk(KERN_WARNING "AWE32 Error: invalid reverb mode %d for uploading\n", patch->optarg);
  4134. return -EINVAL;
  4135. }
  4136. if (count < sizeof(awe_reverb_fx_rec)) {
  4137. printk(KERN_WARNING "AWE32 Error: too short reverb fx parameters\n");
  4138. return -EINVAL;
  4139. }
  4140. if (copy_from_user(&reverb_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
  4141. sizeof(awe_reverb_fx_rec)))
  4142. return -EFAULT;
  4143. reverb_defined[patch->optarg] = TRUE;
  4144. return 0;
  4145. }
  4146. static void
  4147. awe_set_reverb_mode(int effect)
  4148. {
  4149. int i;
  4150. if (effect < 0 || effect >= AWE_REVERB_NUMBERS ||
  4151. (effect >= AWE_REVERB_PREDEFINED && !reverb_defined[effect]))
  4152. return;
  4153. for (i = 0; i < 28; i++)
  4154. awe_poke(reverb_cmds[i].cmd, reverb_cmds[i].port,
  4155. reverb_parm[effect].parms[i]);
  4156. }
  4157. static void
  4158. awe_update_reverb_mode(void)
  4159. {
  4160. awe_set_reverb_mode(ctrls[AWE_MD_REVERB_MODE]);
  4161. }
  4162. /*
  4163. * treble/bass equalizer control
  4164. */
  4165. static unsigned short bass_parm[12][3] = {
  4166. {0xD26A, 0xD36A, 0x0000}, /* -12 dB */
  4167. {0xD25B, 0xD35B, 0x0000}, /* -8 */
  4168. {0xD24C, 0xD34C, 0x0000}, /* -6 */
  4169. {0xD23D, 0xD33D, 0x0000}, /* -4 */
  4170. {0xD21F, 0xD31F, 0x0000}, /* -2 */
  4171. {0xC208, 0xC308, 0x0001}, /* 0 (HW default) */
  4172. {0xC219, 0xC319, 0x0001}, /* +2 */
  4173. {0xC22A, 0xC32A, 0x0001}, /* +4 */
  4174. {0xC24C, 0xC34C, 0x0001}, /* +6 */
  4175. {0xC26E, 0xC36E, 0x0001}, /* +8 */
  4176. {0xC248, 0xC348, 0x0002}, /* +10 */
  4177. {0xC26A, 0xC36A, 0x0002}, /* +12 dB */
  4178. };
  4179. static unsigned short treble_parm[12][9] = {
  4180. {0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */
  4181. {0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4182. {0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4183. {0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4184. {0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4185. {0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002},
  4186. {0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002},
  4187. {0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002},
  4188. {0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002},
  4189. {0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */
  4190. {0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002},
  4191. {0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +12 dB */
  4192. };
  4193. /*
  4194. * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB]
  4195. */
  4196. static void
  4197. awe_equalizer(int bass, int treble)
  4198. {
  4199. unsigned short w;
  4200. if (bass < 0 || bass > 11 || treble < 0 || treble > 11)
  4201. return;
  4202. awe_poke(AWE_INIT4(0x01), bass_parm[bass][0]);
  4203. awe_poke(AWE_INIT4(0x11), bass_parm[bass][1]);
  4204. awe_poke(AWE_INIT3(0x11), treble_parm[treble][0]);
  4205. awe_poke(AWE_INIT3(0x13), treble_parm[treble][1]);
  4206. awe_poke(AWE_INIT3(0x1B), treble_parm[treble][2]);
  4207. awe_poke(AWE_INIT4(0x07), treble_parm[treble][3]);
  4208. awe_poke(AWE_INIT4(0x0B), treble_parm[treble][4]);
  4209. awe_poke(AWE_INIT4(0x0D), treble_parm[treble][5]);
  4210. awe_poke(AWE_INIT4(0x17), treble_parm[treble][6]);
  4211. awe_poke(AWE_INIT4(0x19), treble_parm[treble][7]);
  4212. w = bass_parm[bass][2] + treble_parm[treble][8];
  4213. awe_poke(AWE_INIT4(0x15), (unsigned short)(w + 0x0262));
  4214. awe_poke(AWE_INIT4(0x1D), (unsigned short)(w + 0x8362));
  4215. }
  4216. static void awe_update_equalizer(void)
  4217. {
  4218. awe_equalizer(ctrls[AWE_MD_BASS_LEVEL], ctrls[AWE_MD_TREBLE_LEVEL]);
  4219. }
  4220. /*----------------------------------------------------------------*/
  4221. #ifdef CONFIG_AWE32_MIDIEMU
  4222. /*
  4223. * Emu8000 MIDI Emulation
  4224. */
  4225. /*
  4226. * midi queue record
  4227. */
  4228. /* queue type */
  4229. enum { Q_NONE, Q_VARLEN, Q_READ, Q_SYSEX, };
  4230. #define MAX_MIDIBUF 64
  4231. /* midi status */
  4232. typedef struct MidiStatus {
  4233. int queue; /* queue type */
  4234. int qlen; /* queue length */
  4235. int read; /* chars read */
  4236. int status; /* current status */
  4237. int chan; /* current channel */
  4238. unsigned char buf[MAX_MIDIBUF];
  4239. } MidiStatus;
  4240. /* MIDI mode type */
  4241. enum { MODE_GM, MODE_GS, MODE_XG, };
  4242. /* NRPN / CC -> Emu8000 parameter converter */
  4243. typedef struct {
  4244. int control;
  4245. int awe_effect;
  4246. unsigned short (*convert)(int val);
  4247. } ConvTable;
  4248. /*
  4249. * prototypes
  4250. */
  4251. static int awe_midi_open(int dev, int mode, void (*input)(int,unsigned char), void (*output)(int));
  4252. static void awe_midi_close(int dev);
  4253. static int awe_midi_ioctl(int dev, unsigned cmd, void __user * arg);
  4254. static int awe_midi_outputc(int dev, unsigned char midi_byte);
  4255. static void init_midi_status(MidiStatus *st);
  4256. static void clear_rpn(void);
  4257. static void get_midi_char(MidiStatus *st, int c);
  4258. /*static void queue_varlen(MidiStatus *st, int c);*/
  4259. static void special_event(MidiStatus *st, int c);
  4260. static void queue_read(MidiStatus *st, int c);
  4261. static void midi_note_on(MidiStatus *st);
  4262. static void midi_note_off(MidiStatus *st);
  4263. static void midi_key_pressure(MidiStatus *st);
  4264. static void midi_channel_pressure(MidiStatus *st);
  4265. static void midi_pitch_wheel(MidiStatus *st);
  4266. static void midi_program_change(MidiStatus *st);
  4267. static void midi_control_change(MidiStatus *st);
  4268. static void midi_select_bank(MidiStatus *st, int val);
  4269. static void midi_nrpn_event(MidiStatus *st);
  4270. static void midi_rpn_event(MidiStatus *st);
  4271. static void midi_detune(int chan, int coarse, int fine);
  4272. static void midi_system_exclusive(MidiStatus *st);
  4273. static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
  4274. static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
  4275. static int xg_control_change(MidiStatus *st, int cmd, int val);
  4276. #define numberof(ary) (sizeof(ary)/sizeof(ary[0]))
  4277. /*
  4278. * OSS Midi device record
  4279. */
  4280. static struct midi_operations awe_midi_operations =
  4281. {
  4282. .owner = THIS_MODULE,
  4283. .info = {"AWE Midi Emu", 0, 0, SNDCARD_SB},
  4284. .in_info = {0},
  4285. .open = awe_midi_open, /*open*/
  4286. .close = awe_midi_close, /*close*/
  4287. .ioctl = awe_midi_ioctl, /*ioctl*/
  4288. .outputc = awe_midi_outputc, /*outputc*/
  4289. };
  4290. static int my_mididev = -1;
  4291. static void __init attach_midiemu(void)
  4292. {
  4293. if ((my_mididev = sound_alloc_mididev()) < 0)
  4294. printk ("Sound: Too many midi devices detected\n");
  4295. else
  4296. midi_devs[my_mididev] = &awe_midi_operations;
  4297. }
  4298. static void unload_midiemu(void)
  4299. {
  4300. if (my_mididev >= 0)
  4301. sound_unload_mididev(my_mididev);
  4302. }
  4303. /*
  4304. * open/close midi device
  4305. */
  4306. static int midi_opened = FALSE;
  4307. static int midi_mode;
  4308. static int coarsetune, finetune;
  4309. static int xg_mapping = TRUE;
  4310. static int xg_bankmode;
  4311. /* effect sensitivity */
  4312. #define FX_CUTOFF 0
  4313. #define FX_RESONANCE 1
  4314. #define FX_ATTACK 2
  4315. #define FX_RELEASE 3
  4316. #define FX_VIBRATE 4
  4317. #define FX_VIBDEPTH 5
  4318. #define FX_VIBDELAY 6
  4319. #define FX_NUMS 7
  4320. #define DEF_FX_CUTOFF 170
  4321. #define DEF_FX_RESONANCE 6
  4322. #define DEF_FX_ATTACK 50
  4323. #define DEF_FX_RELEASE 50
  4324. #define DEF_FX_VIBRATE 30
  4325. #define DEF_FX_VIBDEPTH 4
  4326. #define DEF_FX_VIBDELAY 1500
  4327. /* effect sense: */
  4328. static int gs_sense[] =
  4329. {
  4330. DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
  4331. DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
  4332. };
  4333. static int xg_sense[] =
  4334. {
  4335. DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
  4336. DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
  4337. };
  4338. /* current status */
  4339. static MidiStatus curst;
  4340. static int
  4341. awe_midi_open (int dev, int mode,
  4342. void (*input)(int,unsigned char),
  4343. void (*output)(int))
  4344. {
  4345. if (midi_opened)
  4346. return -EBUSY;
  4347. midi_opened = TRUE;
  4348. midi_mode = MODE_GM;
  4349. curst.queue = Q_NONE;
  4350. curst.qlen = 0;
  4351. curst.read = 0;
  4352. curst.status = 0;
  4353. curst.chan = 0;
  4354. memset(curst.buf, 0, sizeof(curst.buf));
  4355. init_midi_status(&curst);
  4356. return 0;
  4357. }
  4358. static void
  4359. awe_midi_close (int dev)
  4360. {
  4361. midi_opened = FALSE;
  4362. }
  4363. static int
  4364. awe_midi_ioctl (int dev, unsigned cmd, void __user *arg)
  4365. {
  4366. return -EPERM;
  4367. }
  4368. static int
  4369. awe_midi_outputc (int dev, unsigned char midi_byte)
  4370. {
  4371. if (! midi_opened)
  4372. return 1;
  4373. /* force to change playing mode */
  4374. playing_mode = AWE_PLAY_MULTI;
  4375. get_midi_char(&curst, midi_byte);
  4376. return 1;
  4377. }
  4378. /*
  4379. * initialize
  4380. */
  4381. static void init_midi_status(MidiStatus *st)
  4382. {
  4383. clear_rpn();
  4384. coarsetune = 0;
  4385. finetune = 0;
  4386. }
  4387. /*
  4388. * RPN & NRPN
  4389. */
  4390. #define MAX_MIDI_CHANNELS 16
  4391. /* RPN & NRPN */
  4392. static unsigned char nrpn[MAX_MIDI_CHANNELS]; /* current event is NRPN? */
  4393. static int msb_bit; /* current event is msb for RPN/NRPN */
  4394. /* RPN & NRPN indeces */
  4395. static unsigned char rpn_msb[MAX_MIDI_CHANNELS], rpn_lsb[MAX_MIDI_CHANNELS];
  4396. /* RPN & NRPN values */
  4397. static int rpn_val[MAX_MIDI_CHANNELS];
  4398. static void clear_rpn(void)
  4399. {
  4400. int i;
  4401. for (i = 0; i < MAX_MIDI_CHANNELS; i++) {
  4402. nrpn[i] = 0;
  4403. rpn_msb[i] = 127;
  4404. rpn_lsb[i] = 127;
  4405. rpn_val[i] = 0;
  4406. }
  4407. msb_bit = 0;
  4408. }
  4409. /*
  4410. * process midi queue
  4411. */
  4412. /* status event types */
  4413. typedef void (*StatusEvent)(MidiStatus *st);
  4414. static struct StatusEventList {
  4415. StatusEvent process;
  4416. int qlen;
  4417. } status_event[8] = {
  4418. {midi_note_off, 2},
  4419. {midi_note_on, 2},
  4420. {midi_key_pressure, 2},
  4421. {midi_control_change, 2},
  4422. {midi_program_change, 1},
  4423. {midi_channel_pressure, 1},
  4424. {midi_pitch_wheel, 2},
  4425. {NULL, 0},
  4426. };
  4427. /* read a char from fifo and process it */
  4428. static void get_midi_char(MidiStatus *st, int c)
  4429. {
  4430. if (c == 0xfe) {
  4431. /* ignore active sense */
  4432. st->queue = Q_NONE;
  4433. return;
  4434. }
  4435. switch (st->queue) {
  4436. /* case Q_VARLEN: queue_varlen(st, c); break;*/
  4437. case Q_READ:
  4438. case Q_SYSEX:
  4439. queue_read(st, c);
  4440. break;
  4441. case Q_NONE:
  4442. st->read = 0;
  4443. if ((c & 0xf0) == 0xf0) {
  4444. special_event(st, c);
  4445. } else if (c & 0x80) { /* status change */
  4446. st->status = (c >> 4) & 0x07;
  4447. st->chan = c & 0x0f;
  4448. st->queue = Q_READ;
  4449. st->qlen = status_event[st->status].qlen;
  4450. if (st->qlen == 0)
  4451. st->queue = Q_NONE;
  4452. }
  4453. break;
  4454. }
  4455. }
  4456. /* 0xfx events */
  4457. static void special_event(MidiStatus *st, int c)
  4458. {
  4459. switch (c) {
  4460. case 0xf0: /* system exclusive */
  4461. st->queue = Q_SYSEX;
  4462. st->qlen = 0;
  4463. break;
  4464. case 0xf1: /* MTC quarter frame */
  4465. case 0xf3: /* song select */
  4466. st->queue = Q_READ;
  4467. st->qlen = 1;
  4468. break;
  4469. case 0xf2: /* song position */
  4470. st->queue = Q_READ;
  4471. st->qlen = 2;
  4472. break;
  4473. }
  4474. }
  4475. #if 0
  4476. /* read variable length value */
  4477. static void queue_varlen(MidiStatus *st, int c)
  4478. {
  4479. st->qlen += (c & 0x7f);
  4480. if (c & 0x80) {
  4481. st->qlen <<= 7;
  4482. return;
  4483. }
  4484. if (st->qlen <= 0) {
  4485. st->qlen = 0;
  4486. st->queue = Q_NONE;
  4487. }
  4488. st->queue = Q_READ;
  4489. st->read = 0;
  4490. }
  4491. #endif
  4492. /* read a char */
  4493. static void queue_read(MidiStatus *st, int c)
  4494. {
  4495. if (st->read < MAX_MIDIBUF) {
  4496. if (st->queue != Q_SYSEX)
  4497. c &= 0x7f;
  4498. st->buf[st->read] = (unsigned char)c;
  4499. }
  4500. st->read++;
  4501. if (st->queue == Q_SYSEX && c == 0xf7) {
  4502. midi_system_exclusive(st);
  4503. st->queue = Q_NONE;
  4504. } else if (st->queue == Q_READ && st->read >= st->qlen) {
  4505. if (status_event[st->status].process)
  4506. status_event[st->status].process(st);
  4507. st->queue = Q_NONE;
  4508. }
  4509. }
  4510. /*
  4511. * status events
  4512. */
  4513. /* note on */
  4514. static void midi_note_on(MidiStatus *st)
  4515. {
  4516. DEBUG(2,printk("midi: note_on (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
  4517. if (st->buf[1] == 0)
  4518. midi_note_off(st);
  4519. else
  4520. awe_start_note(0, st->chan, st->buf[0], st->buf[1]);
  4521. }
  4522. /* note off */
  4523. static void midi_note_off(MidiStatus *st)
  4524. {
  4525. DEBUG(2,printk("midi: note_off (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
  4526. awe_kill_note(0, st->chan, st->buf[0], st->buf[1]);
  4527. }
  4528. /* key pressure change */
  4529. static void midi_key_pressure(MidiStatus *st)
  4530. {
  4531. awe_key_pressure(0, st->chan, st->buf[0], st->buf[1]);
  4532. }
  4533. /* channel pressure change */
  4534. static void midi_channel_pressure(MidiStatus *st)
  4535. {
  4536. channels[st->chan].chan_press = st->buf[0];
  4537. awe_modwheel_change(st->chan, st->buf[0]);
  4538. }
  4539. /* pitch wheel change */
  4540. static void midi_pitch_wheel(MidiStatus *st)
  4541. {
  4542. int val = (int)st->buf[1] * 128 + st->buf[0];
  4543. awe_bender(0, st->chan, val);
  4544. }
  4545. /* program change */
  4546. static void midi_program_change(MidiStatus *st)
  4547. {
  4548. int preset;
  4549. preset = st->buf[0];
  4550. if (midi_mode == MODE_GS && IS_DRUM_CHANNEL(st->chan) && preset == 127)
  4551. preset = 0;
  4552. else if (midi_mode == MODE_XG && xg_mapping && IS_DRUM_CHANNEL(st->chan))
  4553. preset += 64;
  4554. awe_set_instr(0, st->chan, preset);
  4555. }
  4556. #define send_effect(chan,type,val) awe_send_effect(chan,-1,type,val)
  4557. #define add_effect(chan,type,val) awe_send_effect(chan,-1,(type)|0x80,val)
  4558. #define unset_effect(chan,type) awe_send_effect(chan,-1,(type)|0x40,0)
  4559. /* midi control change */
  4560. static void midi_control_change(MidiStatus *st)
  4561. {
  4562. int cmd = st->buf[0];
  4563. int val = st->buf[1];
  4564. DEBUG(2,printk("midi: control (%d) %d %d\n", st->chan, cmd, val));
  4565. if (midi_mode == MODE_XG) {
  4566. if (xg_control_change(st, cmd, val))
  4567. return;
  4568. }
  4569. /* controls #31 - #64 are LSB of #0 - #31 */
  4570. msb_bit = 1;
  4571. if (cmd >= 0x20 && cmd < 0x40) {
  4572. msb_bit = 0;
  4573. cmd -= 0x20;
  4574. }
  4575. switch (cmd) {
  4576. case CTL_SOFT_PEDAL:
  4577. if (val == 127)
  4578. add_effect(st->chan, AWE_FX_CUTOFF, -160);
  4579. else
  4580. unset_effect(st->chan, AWE_FX_CUTOFF);
  4581. break;
  4582. case CTL_BANK_SELECT:
  4583. midi_select_bank(st, val);
  4584. break;
  4585. /* set RPN/NRPN parameter */
  4586. case CTL_REGIST_PARM_NUM_MSB:
  4587. nrpn[st->chan]=0; rpn_msb[st->chan]=val;
  4588. break;
  4589. case CTL_REGIST_PARM_NUM_LSB:
  4590. nrpn[st->chan]=0; rpn_lsb[st->chan]=val;
  4591. break;
  4592. case CTL_NONREG_PARM_NUM_MSB:
  4593. nrpn[st->chan]=1; rpn_msb[st->chan]=val;
  4594. break;
  4595. case CTL_NONREG_PARM_NUM_LSB:
  4596. nrpn[st->chan]=1; rpn_lsb[st->chan]=val;
  4597. break;
  4598. /* send RPN/NRPN entry */
  4599. case CTL_DATA_ENTRY:
  4600. if (msb_bit)
  4601. rpn_val[st->chan] = val * 128;
  4602. else
  4603. rpn_val[st->chan] |= val;
  4604. if (nrpn[st->chan])
  4605. midi_nrpn_event(st);
  4606. else
  4607. midi_rpn_event(st);
  4608. break;
  4609. /* increase/decrease data entry */
  4610. case CTL_DATA_INCREMENT:
  4611. rpn_val[st->chan]++;
  4612. midi_rpn_event(st);
  4613. break;
  4614. case CTL_DATA_DECREMENT:
  4615. rpn_val[st->chan]--;
  4616. midi_rpn_event(st);
  4617. break;
  4618. /* default */
  4619. default:
  4620. awe_controller(0, st->chan, cmd, val);
  4621. break;
  4622. }
  4623. }
  4624. /* tone bank change */
  4625. static void midi_select_bank(MidiStatus *st, int val)
  4626. {
  4627. if (midi_mode == MODE_XG && msb_bit) {
  4628. xg_bankmode = val;
  4629. /* XG MSB value; not normal bank selection */
  4630. switch (val) {
  4631. case 127: /* remap to drum channel */
  4632. awe_controller(0, st->chan, CTL_BANK_SELECT, 128);
  4633. break;
  4634. default: /* remap to normal channel */
  4635. awe_controller(0, st->chan, CTL_BANK_SELECT, val);
  4636. break;
  4637. }
  4638. return;
  4639. } else if (midi_mode == MODE_GS && !msb_bit)
  4640. /* ignore LSB bank in GS mode (used for mapping) */
  4641. return;
  4642. /* normal bank controls; accept both MSB and LSB */
  4643. if (! IS_DRUM_CHANNEL(st->chan)) {
  4644. if (midi_mode == MODE_XG) {
  4645. if (xg_bankmode) return;
  4646. if (val == 64 || val == 126)
  4647. val = 0;
  4648. } else if (midi_mode == MODE_GS && val == 127)
  4649. val = 0;
  4650. awe_controller(0, st->chan, CTL_BANK_SELECT, val);
  4651. }
  4652. }
  4653. /*
  4654. * RPN events
  4655. */
  4656. static void midi_rpn_event(MidiStatus *st)
  4657. {
  4658. int type;
  4659. type = (rpn_msb[st->chan]<<8) | rpn_lsb[st->chan];
  4660. switch (type) {
  4661. case 0x0000: /* Pitch bend sensitivity */
  4662. /* MSB only / 1 semitone per 128 */
  4663. if (msb_bit) {
  4664. channels[st->chan].bender_range =
  4665. rpn_val[st->chan] * 100 / 128;
  4666. }
  4667. break;
  4668. case 0x0001: /* fine tuning: */
  4669. /* MSB/LSB, 8192=center, 100/8192 cent step */
  4670. finetune = rpn_val[st->chan] - 8192;
  4671. midi_detune(st->chan, coarsetune, finetune);
  4672. break;
  4673. case 0x0002: /* coarse tuning */
  4674. /* MSB only / 8192=center, 1 semitone per 128 */
  4675. if (msb_bit) {
  4676. coarsetune = rpn_val[st->chan] - 8192;
  4677. midi_detune(st->chan, coarsetune, finetune);
  4678. }
  4679. break;
  4680. case 0x7F7F: /* "lock-in" RPN */
  4681. break;
  4682. }
  4683. }
  4684. /* tuning:
  4685. * coarse = -8192 to 8192 (100 cent per 128)
  4686. * fine = -8192 to 8192 (max=100cent)
  4687. */
  4688. static void midi_detune(int chan, int coarse, int fine)
  4689. {
  4690. /* 4096 = 1200 cents in AWE parameter */
  4691. int val;
  4692. val = coarse * 4096 / (12 * 128);
  4693. val += fine / 24;
  4694. if (val)
  4695. send_effect(chan, AWE_FX_INIT_PITCH, val);
  4696. else
  4697. unset_effect(chan, AWE_FX_INIT_PITCH);
  4698. }
  4699. /*
  4700. * system exclusive message
  4701. * GM/GS/XG macros are accepted
  4702. */
  4703. static void midi_system_exclusive(MidiStatus *st)
  4704. {
  4705. /* GM on */
  4706. static unsigned char gm_on_macro[] = {
  4707. 0x7e,0x7f,0x09,0x01,
  4708. };
  4709. /* XG on */
  4710. static unsigned char xg_on_macro[] = {
  4711. 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
  4712. };
  4713. /* GS prefix
  4714. * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
  4715. * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
  4716. * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
  4717. */
  4718. static unsigned char gs_pfx_macro[] = {
  4719. 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
  4720. };
  4721. #if 0
  4722. /* SC88 system mode set
  4723. * single module mode: XX=1
  4724. * double module mode: XX=0
  4725. */
  4726. static unsigned char gs_mode_macro[] = {
  4727. 0x41,0x10,0x42,0x12,0x00,0x00,0x7F,/*ZZ*/
  4728. };
  4729. /* SC88 display macro: XX=01:bitmap, 00:text
  4730. */
  4731. static unsigned char gs_disp_macro[] = {
  4732. 0x41,0x10,0x45,0x12,0x10,/*XX,00*/
  4733. };
  4734. #endif
  4735. /* GM on */
  4736. if (memcmp(st->buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
  4737. if (midi_mode != MODE_GS && midi_mode != MODE_XG)
  4738. midi_mode = MODE_GM;
  4739. init_midi_status(st);
  4740. }
  4741. /* GS macros */
  4742. else if (memcmp(st->buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
  4743. if (midi_mode != MODE_GS && midi_mode != MODE_XG)
  4744. midi_mode = MODE_GS;
  4745. if (st->buf[5] == 0x00 && st->buf[6] == 0x7f && st->buf[7] == 0x00) {
  4746. /* GS reset */
  4747. init_midi_status(st);
  4748. }
  4749. else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x15) {
  4750. /* drum pattern */
  4751. int p = st->buf[5] & 0x0f;
  4752. if (p == 0) p = 9;
  4753. else if (p < 10) p--;
  4754. if (st->buf[7] == 0)
  4755. DRUM_CHANNEL_OFF(p);
  4756. else
  4757. DRUM_CHANNEL_ON(p);
  4758. } else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x21) {
  4759. /* program */
  4760. int p = st->buf[5] & 0x0f;
  4761. if (p == 0) p = 9;
  4762. else if (p < 10) p--;
  4763. if (! IS_DRUM_CHANNEL(p))
  4764. awe_set_instr(0, p, st->buf[7]);
  4765. } else if (st->buf[5] == 0x01 && st->buf[6] == 0x30) {
  4766. /* reverb mode */
  4767. awe_set_reverb_mode(st->buf[7]);
  4768. } else if (st->buf[5] == 0x01 && st->buf[6] == 0x38) {
  4769. /* chorus mode */
  4770. awe_set_chorus_mode(st->buf[7]);
  4771. } else if (st->buf[5] == 0x00 && st->buf[6] == 0x04) {
  4772. /* master volume */
  4773. awe_change_master_volume(st->buf[7]);
  4774. }
  4775. }
  4776. /* XG on */
  4777. else if (memcmp(st->buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
  4778. midi_mode = MODE_XG;
  4779. xg_mapping = TRUE;
  4780. xg_bankmode = 0;
  4781. }
  4782. }
  4783. /*----------------------------------------------------------------*/
  4784. /*
  4785. * convert NRPN/control values
  4786. */
  4787. static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
  4788. {
  4789. int i, cval;
  4790. for (i = 0; i < num_tables; i++) {
  4791. if (table[i].control == type) {
  4792. cval = table[i].convert(val);
  4793. send_effect(st->chan, table[i].awe_effect, cval);
  4794. return TRUE;
  4795. }
  4796. }
  4797. return FALSE;
  4798. }
  4799. static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
  4800. {
  4801. int i, cval;
  4802. for (i = 0; i < num_tables; i++) {
  4803. if (table[i].control == type) {
  4804. cval = table[i].convert(val);
  4805. add_effect(st->chan, table[i].awe_effect|0x80, cval);
  4806. return TRUE;
  4807. }
  4808. }
  4809. return FALSE;
  4810. }
  4811. /*
  4812. * AWE32 NRPN effects
  4813. */
  4814. static unsigned short fx_delay(int val);
  4815. static unsigned short fx_attack(int val);
  4816. static unsigned short fx_hold(int val);
  4817. static unsigned short fx_decay(int val);
  4818. static unsigned short fx_the_value(int val);
  4819. static unsigned short fx_twice_value(int val);
  4820. static unsigned short fx_conv_pitch(int val);
  4821. static unsigned short fx_conv_Q(int val);
  4822. /* function for each NRPN */ /* [range] units */
  4823. #define fx_env1_delay fx_delay /* [0,5900] 4msec */
  4824. #define fx_env1_attack fx_attack /* [0,5940] 1msec */
  4825. #define fx_env1_hold fx_hold /* [0,8191] 1msec */
  4826. #define fx_env1_decay fx_decay /* [0,5940] 4msec */
  4827. #define fx_env1_release fx_decay /* [0,5940] 4msec */
  4828. #define fx_env1_sustain fx_the_value /* [0,127] 0.75dB */
  4829. #define fx_env1_pitch fx_the_value /* [-127,127] 9.375cents */
  4830. #define fx_env1_cutoff fx_the_value /* [-127,127] 56.25cents */
  4831. #define fx_env2_delay fx_delay /* [0,5900] 4msec */
  4832. #define fx_env2_attack fx_attack /* [0,5940] 1msec */
  4833. #define fx_env2_hold fx_hold /* [0,8191] 1msec */
  4834. #define fx_env2_decay fx_decay /* [0,5940] 4msec */
  4835. #define fx_env2_release fx_decay /* [0,5940] 4msec */
  4836. #define fx_env2_sustain fx_the_value /* [0,127] 0.75dB */
  4837. #define fx_lfo1_delay fx_delay /* [0,5900] 4msec */
  4838. #define fx_lfo1_freq fx_twice_value /* [0,127] 84mHz */
  4839. #define fx_lfo1_volume fx_twice_value /* [0,127] 0.1875dB */
  4840. #define fx_lfo1_pitch fx_the_value /* [-127,127] 9.375cents */
  4841. #define fx_lfo1_cutoff fx_twice_value /* [-64,63] 56.25cents */
  4842. #define fx_lfo2_delay fx_delay /* [0,5900] 4msec */
  4843. #define fx_lfo2_freq fx_twice_value /* [0,127] 84mHz */
  4844. #define fx_lfo2_pitch fx_the_value /* [-127,127] 9.375cents */
  4845. #define fx_init_pitch fx_conv_pitch /* [-8192,8192] cents */
  4846. #define fx_chorus fx_the_value /* [0,255] -- */
  4847. #define fx_reverb fx_the_value /* [0,255] -- */
  4848. #define fx_cutoff fx_twice_value /* [0,127] 62Hz */
  4849. #define fx_filterQ fx_conv_Q /* [0,127] -- */
  4850. static unsigned short fx_delay(int val)
  4851. {
  4852. return (unsigned short)calc_parm_delay(val);
  4853. }
  4854. static unsigned short fx_attack(int val)
  4855. {
  4856. return (unsigned short)calc_parm_attack(val);
  4857. }
  4858. static unsigned short fx_hold(int val)
  4859. {
  4860. return (unsigned short)calc_parm_hold(val);
  4861. }
  4862. static unsigned short fx_decay(int val)
  4863. {
  4864. return (unsigned short)calc_parm_decay(val);
  4865. }
  4866. static unsigned short fx_the_value(int val)
  4867. {
  4868. return (unsigned short)(val & 0xff);
  4869. }
  4870. static unsigned short fx_twice_value(int val)
  4871. {
  4872. return (unsigned short)((val * 2) & 0xff);
  4873. }
  4874. static unsigned short fx_conv_pitch(int val)
  4875. {
  4876. return (short)(val * 4096 / 1200);
  4877. }
  4878. static unsigned short fx_conv_Q(int val)
  4879. {
  4880. return (unsigned short)((val / 8) & 0xff);
  4881. }
  4882. static ConvTable awe_effects[] =
  4883. {
  4884. { 0, AWE_FX_LFO1_DELAY, fx_lfo1_delay},
  4885. { 1, AWE_FX_LFO1_FREQ, fx_lfo1_freq},
  4886. { 2, AWE_FX_LFO2_DELAY, fx_lfo2_delay},
  4887. { 3, AWE_FX_LFO2_FREQ, fx_lfo2_freq},
  4888. { 4, AWE_FX_ENV1_DELAY, fx_env1_delay},
  4889. { 5, AWE_FX_ENV1_ATTACK,fx_env1_attack},
  4890. { 6, AWE_FX_ENV1_HOLD, fx_env1_hold},
  4891. { 7, AWE_FX_ENV1_DECAY, fx_env1_decay},
  4892. { 8, AWE_FX_ENV1_SUSTAIN, fx_env1_sustain},
  4893. { 9, AWE_FX_ENV1_RELEASE, fx_env1_release},
  4894. {10, AWE_FX_ENV2_DELAY, fx_env2_delay},
  4895. {11, AWE_FX_ENV2_ATTACK, fx_env2_attack},
  4896. {12, AWE_FX_ENV2_HOLD, fx_env2_hold},
  4897. {13, AWE_FX_ENV2_DECAY, fx_env2_decay},
  4898. {14, AWE_FX_ENV2_SUSTAIN, fx_env2_sustain},
  4899. {15, AWE_FX_ENV2_RELEASE, fx_env2_release},
  4900. {16, AWE_FX_INIT_PITCH, fx_init_pitch},
  4901. {17, AWE_FX_LFO1_PITCH, fx_lfo1_pitch},
  4902. {18, AWE_FX_LFO2_PITCH, fx_lfo2_pitch},
  4903. {19, AWE_FX_ENV1_PITCH, fx_env1_pitch},
  4904. {20, AWE_FX_LFO1_VOLUME, fx_lfo1_volume},
  4905. {21, AWE_FX_CUTOFF, fx_cutoff},
  4906. {22, AWE_FX_FILTERQ, fx_filterQ},
  4907. {23, AWE_FX_LFO1_CUTOFF, fx_lfo1_cutoff},
  4908. {24, AWE_FX_ENV1_CUTOFF, fx_env1_cutoff},
  4909. {25, AWE_FX_CHORUS, fx_chorus},
  4910. {26, AWE_FX_REVERB, fx_reverb},
  4911. };
  4912. static int num_awe_effects = numberof(awe_effects);
  4913. /*
  4914. * GS(SC88) NRPN effects; still experimental
  4915. */
  4916. /* cutoff: quarter semitone step, max=255 */
  4917. static unsigned short gs_cutoff(int val)
  4918. {
  4919. return (val - 64) * gs_sense[FX_CUTOFF] / 50;
  4920. }
  4921. /* resonance: 0 to 15(max) */
  4922. static unsigned short gs_filterQ(int val)
  4923. {
  4924. return (val - 64) * gs_sense[FX_RESONANCE] / 50;
  4925. }
  4926. /* attack: */
  4927. static unsigned short gs_attack(int val)
  4928. {
  4929. return -(val - 64) * gs_sense[FX_ATTACK] / 50;
  4930. }
  4931. /* decay: */
  4932. static unsigned short gs_decay(int val)
  4933. {
  4934. return -(val - 64) * gs_sense[FX_RELEASE] / 50;
  4935. }
  4936. /* release: */
  4937. static unsigned short gs_release(int val)
  4938. {
  4939. return -(val - 64) * gs_sense[FX_RELEASE] / 50;
  4940. }
  4941. /* vibrato freq: 0.042Hz step, max=255 */
  4942. static unsigned short gs_vib_rate(int val)
  4943. {
  4944. return (val - 64) * gs_sense[FX_VIBRATE] / 50;
  4945. }
  4946. /* vibrato depth: max=127, 1 octave */
  4947. static unsigned short gs_vib_depth(int val)
  4948. {
  4949. return (val - 64) * gs_sense[FX_VIBDEPTH] / 50;
  4950. }
  4951. /* vibrato delay: -0.725msec step */
  4952. static unsigned short gs_vib_delay(int val)
  4953. {
  4954. return -(val - 64) * gs_sense[FX_VIBDELAY] / 50;
  4955. }
  4956. static ConvTable gs_effects[] =
  4957. {
  4958. {32, AWE_FX_CUTOFF, gs_cutoff},
  4959. {33, AWE_FX_FILTERQ, gs_filterQ},
  4960. {99, AWE_FX_ENV2_ATTACK, gs_attack},
  4961. {100, AWE_FX_ENV2_DECAY, gs_decay},
  4962. {102, AWE_FX_ENV2_RELEASE, gs_release},
  4963. {8, AWE_FX_LFO1_FREQ, gs_vib_rate},
  4964. {9, AWE_FX_LFO1_VOLUME, gs_vib_depth},
  4965. {10, AWE_FX_LFO1_DELAY, gs_vib_delay},
  4966. };
  4967. static int num_gs_effects = numberof(gs_effects);
  4968. /*
  4969. * NRPN events: accept as AWE32/SC88 specific controls
  4970. */
  4971. static void midi_nrpn_event(MidiStatus *st)
  4972. {
  4973. if (rpn_msb[st->chan] == 127 && rpn_lsb[st->chan] <= 26) {
  4974. if (! msb_bit) /* both MSB/LSB necessary */
  4975. send_converted_effect(awe_effects, num_awe_effects,
  4976. st, rpn_lsb[st->chan],
  4977. rpn_val[st->chan] - 8192);
  4978. } else if (rpn_msb[st->chan] == 1) {
  4979. if (msb_bit) /* only MSB is valid */
  4980. add_converted_effect(gs_effects, num_gs_effects,
  4981. st, rpn_lsb[st->chan],
  4982. rpn_val[st->chan] / 128);
  4983. }
  4984. }
  4985. /*
  4986. * XG control effects; still experimental
  4987. */
  4988. /* cutoff: quarter semitone step, max=255 */
  4989. static unsigned short xg_cutoff(int val)
  4990. {
  4991. return (val - 64) * xg_sense[FX_CUTOFF] / 64;
  4992. }
  4993. /* resonance: 0(open) to 15(most nasal) */
  4994. static unsigned short xg_filterQ(int val)
  4995. {
  4996. return (val - 64) * xg_sense[FX_RESONANCE] / 64;
  4997. }
  4998. /* attack: */
  4999. static unsigned short xg_attack(int val)
  5000. {
  5001. return -(val - 64) * xg_sense[FX_ATTACK] / 64;
  5002. }
  5003. /* release: */
  5004. static unsigned short xg_release(int val)
  5005. {
  5006. return -(val - 64) * xg_sense[FX_RELEASE] / 64;
  5007. }
  5008. static ConvTable xg_effects[] =
  5009. {
  5010. {71, AWE_FX_CUTOFF, xg_cutoff},
  5011. {74, AWE_FX_FILTERQ, xg_filterQ},
  5012. {72, AWE_FX_ENV2_RELEASE, xg_release},
  5013. {73, AWE_FX_ENV2_ATTACK, xg_attack},
  5014. };
  5015. static int num_xg_effects = numberof(xg_effects);
  5016. static int xg_control_change(MidiStatus *st, int cmd, int val)
  5017. {
  5018. return add_converted_effect(xg_effects, num_xg_effects, st, cmd, val);
  5019. }
  5020. #endif /* CONFIG_AWE32_MIDIEMU */
  5021. /*----------------------------------------------------------------*/
  5022. /*
  5023. * initialization of AWE driver
  5024. */
  5025. static void
  5026. awe_initialize(void)
  5027. {
  5028. DEBUG(0,printk("AWE32: initializing..\n"));
  5029. /* initialize hardware configuration */
  5030. awe_poke(AWE_HWCF1, 0x0059);
  5031. awe_poke(AWE_HWCF2, 0x0020);
  5032. /* disable audio; this seems to reduce a clicking noise a bit.. */
  5033. awe_poke(AWE_HWCF3, 0);
  5034. /* initialize audio channels */
  5035. awe_init_audio();
  5036. /* initialize DMA */
  5037. awe_init_dma();
  5038. /* initialize init array */
  5039. awe_init_array();
  5040. /* check DRAM memory size */
  5041. awe_check_dram();
  5042. /* initialize the FM section of the AWE32 */
  5043. awe_init_fm();
  5044. /* set up voice envelopes */
  5045. awe_tweak();
  5046. /* enable audio */
  5047. awe_poke(AWE_HWCF3, 0x0004);
  5048. /* set default values */
  5049. awe_init_ctrl_parms(TRUE);
  5050. /* set equalizer */
  5051. awe_update_equalizer();
  5052. /* set reverb & chorus modes */
  5053. awe_update_reverb_mode();
  5054. awe_update_chorus_mode();
  5055. }
  5056. /*
  5057. * Core Device Management Functions
  5058. */
  5059. /* store values to i/o port array */
  5060. static void setup_ports(int port1, int port2, int port3)
  5061. {
  5062. awe_ports[0] = port1;
  5063. if (port2 == 0)
  5064. port2 = port1 + 0x400;
  5065. awe_ports[1] = port2;
  5066. awe_ports[2] = port2 + 2;
  5067. if (port3 == 0)
  5068. port3 = port1 + 0x800;
  5069. awe_ports[3] = port3;
  5070. awe_ports[4] = port3 + 2;
  5071. port_setuped = TRUE;
  5072. }
  5073. /*
  5074. * port request
  5075. * 0x620-623, 0xA20-A23, 0xE20-E23
  5076. */
  5077. static int
  5078. awe_request_region(void)
  5079. {
  5080. if (! port_setuped)
  5081. return 0;
  5082. if (! request_region(awe_ports[0], 4, "sound driver (AWE32)"))
  5083. return 0;
  5084. if (! request_region(awe_ports[1], 4, "sound driver (AWE32)"))
  5085. goto err_out;
  5086. if (! request_region(awe_ports[3], 4, "sound driver (AWE32)"))
  5087. goto err_out1;
  5088. return 1;
  5089. err_out1:
  5090. release_region(awe_ports[1], 4);
  5091. err_out:
  5092. release_region(awe_ports[0], 4);
  5093. return 0;
  5094. }
  5095. static void
  5096. awe_release_region(void)
  5097. {
  5098. if (! port_setuped) return;
  5099. release_region(awe_ports[0], 4);
  5100. release_region(awe_ports[1], 4);
  5101. release_region(awe_ports[3], 4);
  5102. }
  5103. static int awe_attach_device(void)
  5104. {
  5105. if (awe_present) return 0; /* for OSS38.. called twice? */
  5106. /* reserve I/O ports for awedrv */
  5107. if (! awe_request_region()) {
  5108. printk(KERN_ERR "AWE32: I/O area already used.\n");
  5109. return 0;
  5110. }
  5111. /* set buffers to NULL */
  5112. sfhead = sftail = NULL;
  5113. my_dev = sound_alloc_synthdev();
  5114. if (my_dev == -1) {
  5115. printk(KERN_ERR "AWE32 Error: too many synthesizers\n");
  5116. awe_release_region();
  5117. return 0;
  5118. }
  5119. voice_alloc = &awe_operations.alloc;
  5120. voice_alloc->max_voice = awe_max_voices;
  5121. synth_devs[my_dev] = &awe_operations;
  5122. #ifdef CONFIG_AWE32_MIXER
  5123. attach_mixer();
  5124. #endif
  5125. #ifdef CONFIG_AWE32_MIDIEMU
  5126. attach_midiemu();
  5127. #endif
  5128. /* clear all samples */
  5129. awe_reset_samples();
  5130. /* initialize AWE32 hardware */
  5131. awe_initialize();
  5132. sprintf(awe_info.name, "AWE32-%s (RAM%dk)",
  5133. AWEDRV_VERSION, memsize/1024);
  5134. printk(KERN_INFO "<SoundBlaster EMU8000 (RAM%dk)>\n", memsize/1024);
  5135. awe_present = TRUE;
  5136. return 1;
  5137. }
  5138. static void awe_dettach_device(void)
  5139. {
  5140. if (awe_present) {
  5141. awe_reset_samples();
  5142. awe_release_region();
  5143. free_tables();
  5144. #ifdef CONFIG_AWE32_MIXER
  5145. unload_mixer();
  5146. #endif
  5147. #ifdef CONFIG_AWE32_MIDIEMU
  5148. unload_midiemu();
  5149. #endif
  5150. sound_unload_synthdev(my_dev);
  5151. awe_present = FALSE;
  5152. }
  5153. }
  5154. /*
  5155. * Legacy device Probing
  5156. */
  5157. /* detect emu8000 chip on the specified address; from VV's guide */
  5158. static int __init
  5159. awe_detect_base(int addr)
  5160. {
  5161. setup_ports(addr, 0, 0);
  5162. if ((awe_peek(AWE_U1) & 0x000F) != 0x000C)
  5163. return 0;
  5164. if ((awe_peek(AWE_HWCF1) & 0x007E) != 0x0058)
  5165. return 0;
  5166. if ((awe_peek(AWE_HWCF2) & 0x0003) != 0x0003)
  5167. return 0;
  5168. DEBUG(0,printk("AWE32 found at %x\n", addr));
  5169. return 1;
  5170. }
  5171. static int __init awe_detect_legacy_devices(void)
  5172. {
  5173. int base;
  5174. for (base = 0x620; base <= 0x680; base += 0x20)
  5175. if (awe_detect_base(base)) {
  5176. awe_attach_device();
  5177. return 1;
  5178. }
  5179. DEBUG(0,printk("AWE32 Legacy detection failed\n"));
  5180. return 0;
  5181. }
  5182. /*
  5183. * PnP device Probing
  5184. */
  5185. static struct pnp_device_id awe_pnp_ids[] = {
  5186. {.id = "CTL0021", .driver_data = 0}, /* AWE32 WaveTable */
  5187. {.id = "CTL0022", .driver_data = 0}, /* AWE64 WaveTable */
  5188. {.id = "CTL0023", .driver_data = 0}, /* AWE64 Gold WaveTable */
  5189. { } /* terminator */
  5190. };
  5191. MODULE_DEVICE_TABLE(pnp, awe_pnp_ids);
  5192. static int awe_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  5193. {
  5194. int io1, io2, io3;
  5195. if (awe_present) {
  5196. printk(KERN_ERR "AWE32: This driver only supports one AWE32 device, skipping.\n");
  5197. }
  5198. if (!pnp_port_valid(dev,0) ||
  5199. !pnp_port_valid(dev,1) ||
  5200. !pnp_port_valid(dev,2)) {
  5201. printk(KERN_ERR "AWE32: The PnP device does not have the required resources.\n");
  5202. return -EINVAL;
  5203. }
  5204. io1 = pnp_port_start(dev,0);
  5205. io2 = pnp_port_start(dev,1);
  5206. io3 = pnp_port_start(dev,2);
  5207. printk(KERN_INFO "AWE32: A PnP Wave Table was detected at IO's %#x,%#x,%#x.\n",
  5208. io1, io2, io3);
  5209. setup_ports(io1, io2, io3);
  5210. awe_attach_device();
  5211. return 0;
  5212. }
  5213. static void awe_pnp_remove(struct pnp_dev *dev)
  5214. {
  5215. awe_dettach_device();
  5216. }
  5217. static struct pnp_driver awe_pnp_driver = {
  5218. .name = "AWE32",
  5219. .id_table = awe_pnp_ids,
  5220. .probe = awe_pnp_probe,
  5221. .remove = awe_pnp_remove,
  5222. };
  5223. static int __init awe_detect_pnp_devices(void)
  5224. {
  5225. int ret;
  5226. ret = pnp_register_driver(&awe_pnp_driver);
  5227. if (ret<0)
  5228. printk(KERN_ERR "AWE32: PnP support is unavailable.\n");
  5229. return ret;
  5230. }
  5231. /*
  5232. * device / lowlevel (module) interface
  5233. */
  5234. static int __init
  5235. awe_detect(void)
  5236. {
  5237. printk(KERN_INFO "AWE32: Probing for WaveTable...\n");
  5238. if (isapnp) {
  5239. if (awe_detect_pnp_devices()>=0)
  5240. return 1;
  5241. } else
  5242. printk(KERN_INFO "AWE32: Skipping PnP detection.\n");
  5243. if (awe_detect_legacy_devices())
  5244. return 1;
  5245. return 0;
  5246. }
  5247. static int __init attach_awe(void)
  5248. {
  5249. return awe_detect() ? 0 : -ENODEV;
  5250. }
  5251. static void __exit unload_awe(void)
  5252. {
  5253. pnp_unregister_driver(&awe_pnp_driver);
  5254. awe_dettach_device();
  5255. }
  5256. module_init(attach_awe);
  5257. module_exit(unload_awe);
  5258. #ifndef MODULE
  5259. static int __init setup_awe(char *str)
  5260. {
  5261. /* io, memsize, isapnp */
  5262. int ints[4];
  5263. str = get_options(str, ARRAY_SIZE(ints), ints);
  5264. io = ints[1];
  5265. memsize = ints[2];
  5266. isapnp = ints[3];
  5267. return 1;
  5268. }
  5269. __setup("awe=", setup_awe);
  5270. #endif