awe_wave.c 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149
  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 = 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. kfree(vrec);
  3055. return rc;
  3056. }
  3057. sf->mem_ptr += rc;
  3058. add_sf_sample(sf, smprec);
  3059. /* set up voice info */
  3060. rec = &vrec->v;
  3061. awe_init_voice_info(rec);
  3062. rec->sample = sf->num_info; /* the last sample */
  3063. rec->rate_offset = calc_rate_offset(patch.base_freq);
  3064. note = freq_to_note(patch.base_note);
  3065. rec->root = note / 100;
  3066. rec->tune = -(note % 100);
  3067. rec->low = freq_to_note(patch.low_note) / 100;
  3068. rec->high = freq_to_note(patch.high_note) / 100;
  3069. DEBUG(1,printk("AWE32: [gus base offset=%d, note=%d, range=%d-%d(%d-%d)]\n",
  3070. rec->rate_offset, note,
  3071. rec->low, rec->high,
  3072. patch.low_note, patch.high_note));
  3073. /* panning position; -128 - 127 => 0-127 */
  3074. rec->pan = (patch.panning + 128) / 2;
  3075. /* detuning is ignored */
  3076. /* 6points volume envelope */
  3077. if (patch.mode & WAVE_ENVELOPES) {
  3078. int attack, hold, decay, release;
  3079. attack = calc_gus_envelope_time
  3080. (patch.env_rate[0], 0, patch.env_offset[0]);
  3081. hold = calc_gus_envelope_time
  3082. (patch.env_rate[1], patch.env_offset[0],
  3083. patch.env_offset[1]);
  3084. decay = calc_gus_envelope_time
  3085. (patch.env_rate[2], patch.env_offset[1],
  3086. patch.env_offset[2]);
  3087. release = calc_gus_envelope_time
  3088. (patch.env_rate[3], patch.env_offset[1],
  3089. patch.env_offset[4]);
  3090. release += calc_gus_envelope_time
  3091. (patch.env_rate[4], patch.env_offset[3],
  3092. patch.env_offset[4]);
  3093. release += calc_gus_envelope_time
  3094. (patch.env_rate[5], patch.env_offset[4],
  3095. patch.env_offset[5]);
  3096. rec->parm.volatkhld = (calc_parm_hold(hold) << 8) |
  3097. calc_parm_attack(attack);
  3098. rec->parm.voldcysus = (calc_gus_sustain(patch.env_offset[2]) << 8) |
  3099. calc_parm_decay(decay);
  3100. rec->parm.volrelease = 0x8000 | calc_parm_decay(release);
  3101. DEBUG(2,printk("AWE32: [gusenv atk=%d, hld=%d, dcy=%d, rel=%d]\n", attack, hold, decay, release));
  3102. rec->attenuation = calc_gus_attenuation(patch.env_offset[0]);
  3103. }
  3104. /* tremolo effect */
  3105. if (patch.mode & WAVE_TREMOLO) {
  3106. int rate = (patch.tremolo_rate * 1000 / 38) / 42;
  3107. rec->parm.tremfrq = ((patch.tremolo_depth / 2) << 8) | rate;
  3108. DEBUG(2,printk("AWE32: [gusenv tremolo rate=%d, dep=%d, tremfrq=%x]\n",
  3109. patch.tremolo_rate, patch.tremolo_depth,
  3110. rec->parm.tremfrq));
  3111. }
  3112. /* vibrato effect */
  3113. if (patch.mode & WAVE_VIBRATO) {
  3114. int rate = (patch.vibrato_rate * 1000 / 38) / 42;
  3115. rec->parm.fm2frq2 = ((patch.vibrato_depth / 6) << 8) | rate;
  3116. DEBUG(2,printk("AWE32: [gusenv vibrato rate=%d, dep=%d, tremfrq=%x]\n",
  3117. patch.tremolo_rate, patch.tremolo_depth,
  3118. rec->parm.tremfrq));
  3119. }
  3120. /* scale_freq, scale_factor, volume, and fractions not implemented */
  3121. /* append to the tail of the list */
  3122. vrec->bank = ctrls[AWE_MD_GUS_BANK];
  3123. vrec->instr = patch.instr_no;
  3124. vrec->disabled = FALSE;
  3125. vrec->type = V_ST_NORMAL;
  3126. add_sf_info(sf, vrec);
  3127. add_info_list(vrec);
  3128. /* set the voice index */
  3129. awe_set_sample(vrec);
  3130. return 0;
  3131. }
  3132. #endif /* AWE_HAS_GUS_COMPATIBILITY */
  3133. /*
  3134. * sample and voice list handlers
  3135. */
  3136. /* append this to the current sf list */
  3137. static void add_sf_info(sf_list *sf, awe_voice_list *rec)
  3138. {
  3139. if (sf == NULL)
  3140. return;
  3141. rec->holder = sf;
  3142. rec->v.sf_id = sf->sf_id;
  3143. if (sf->last_infos)
  3144. sf->last_infos->next = rec;
  3145. else
  3146. sf->infos = rec;
  3147. sf->last_infos = rec;
  3148. rec->next = NULL;
  3149. sf->num_info++;
  3150. }
  3151. /* prepend this sample to sf list */
  3152. static void add_sf_sample(sf_list *sf, awe_sample_list *rec)
  3153. {
  3154. if (sf == NULL)
  3155. return;
  3156. rec->holder = sf;
  3157. rec->v.sf_id = sf->sf_id;
  3158. if (sf->last_samples)
  3159. sf->last_samples->next = rec;
  3160. else
  3161. sf->samples = rec;
  3162. sf->last_samples = rec;
  3163. rec->next = NULL;
  3164. sf->num_sample++;
  3165. }
  3166. /* purge the old records which don't belong with the same file id */
  3167. static void purge_old_list(awe_voice_list *rec, awe_voice_list *next)
  3168. {
  3169. rec->next_instr = next;
  3170. if (rec->bank == AWE_DRUM_BANK) {
  3171. /* remove samples with the same note range */
  3172. awe_voice_list *cur, *prev = rec;
  3173. int low = rec->v.low;
  3174. int high = rec->v.high;
  3175. for (cur = next; cur; cur = cur->next_instr) {
  3176. if (cur->v.low == low &&
  3177. cur->v.high == high &&
  3178. ! is_identical_holder(cur->holder, rec->holder))
  3179. prev->next_instr = cur->next_instr;
  3180. else
  3181. prev = cur;
  3182. }
  3183. } else {
  3184. if (! is_identical_holder(next->holder, rec->holder))
  3185. /* remove all samples */
  3186. rec->next_instr = NULL;
  3187. }
  3188. }
  3189. /* prepend to top of the preset table */
  3190. static void add_info_list(awe_voice_list *rec)
  3191. {
  3192. awe_voice_list *prev, *cur;
  3193. int key;
  3194. if (rec->disabled)
  3195. return;
  3196. key = awe_search_key(rec->bank, rec->instr, rec->v.low);
  3197. prev = NULL;
  3198. for (cur = preset_table[key]; cur; cur = cur->next_bank) {
  3199. /* search the first record with the same bank number */
  3200. if (cur->instr == rec->instr && cur->bank == rec->bank) {
  3201. /* replace the list with the new record */
  3202. rec->next_bank = cur->next_bank;
  3203. if (prev)
  3204. prev->next_bank = rec;
  3205. else
  3206. preset_table[key] = rec;
  3207. purge_old_list(rec, cur);
  3208. return;
  3209. }
  3210. prev = cur;
  3211. }
  3212. /* this is the first bank record.. just add this */
  3213. rec->next_instr = NULL;
  3214. rec->next_bank = preset_table[key];
  3215. preset_table[key] = rec;
  3216. }
  3217. /* remove samples later than the specified sf_id */
  3218. static void
  3219. awe_remove_samples(int sf_id)
  3220. {
  3221. sf_list *p, *prev;
  3222. if (sf_id <= 0) {
  3223. awe_reset_samples();
  3224. return;
  3225. }
  3226. /* already removed? */
  3227. if (current_sf_id <= sf_id)
  3228. return;
  3229. for (p = sftail; p; p = prev) {
  3230. if (p->sf_id <= sf_id)
  3231. break;
  3232. prev = p->prev;
  3233. awe_free_sf(p);
  3234. }
  3235. sftail = p;
  3236. if (sftail) {
  3237. sf_id = sftail->sf_id;
  3238. sftail->next = NULL;
  3239. } else {
  3240. sf_id = 0;
  3241. sfhead = NULL;
  3242. }
  3243. current_sf_id = sf_id;
  3244. if (locked_sf_id > sf_id)
  3245. locked_sf_id = sf_id;
  3246. rebuild_preset_list();
  3247. }
  3248. /* rebuild preset search list */
  3249. static void rebuild_preset_list(void)
  3250. {
  3251. sf_list *p;
  3252. awe_voice_list *rec;
  3253. memset(preset_table, 0, sizeof(preset_table));
  3254. for (p = sfhead; p; p = p->next) {
  3255. for (rec = p->infos; rec; rec = rec->next)
  3256. add_info_list(rec);
  3257. }
  3258. }
  3259. /* compare the given sf_id pair */
  3260. static int is_identical_holder(sf_list *sf1, sf_list *sf2)
  3261. {
  3262. if (sf1 == NULL || sf2 == NULL)
  3263. return FALSE;
  3264. if (sf1 == sf2)
  3265. return TRUE;
  3266. #ifdef AWE_ALLOW_SAMPLE_SHARING
  3267. {
  3268. /* compare with the sharing id */
  3269. sf_list *p;
  3270. int counter = 0;
  3271. if (sf1->sf_id < sf2->sf_id) { /* make sure id1 > id2 */
  3272. sf_list *tmp; tmp = sf1; sf1 = sf2; sf2 = tmp;
  3273. }
  3274. for (p = sf1->shared; p; p = p->shared) {
  3275. if (counter++ > current_sf_id)
  3276. break; /* strange sharing loop.. quit */
  3277. if (p == sf2)
  3278. return TRUE;
  3279. }
  3280. }
  3281. #endif /* allow sharing */
  3282. return FALSE;
  3283. }
  3284. /* search the sample index matching with the given sample id */
  3285. static awe_sample_list *
  3286. search_sample_index(sf_list *sf, int sample)
  3287. {
  3288. awe_sample_list *p;
  3289. #ifdef AWE_ALLOW_SAMPLE_SHARING
  3290. int counter = 0;
  3291. while (sf) {
  3292. for (p = sf->samples; p; p = p->next) {
  3293. if (p->v.sample == sample)
  3294. return p;
  3295. }
  3296. sf = sf->shared;
  3297. if (counter++ > current_sf_id)
  3298. break; /* strange sharing loop.. quit */
  3299. }
  3300. #else
  3301. if (sf) {
  3302. for (p = sf->samples; p; p = p->next) {
  3303. if (p->v.sample == sample)
  3304. return p;
  3305. }
  3306. }
  3307. #endif
  3308. return NULL;
  3309. }
  3310. /* search the specified sample */
  3311. /* non-zero = found */
  3312. static short
  3313. awe_set_sample(awe_voice_list *rec)
  3314. {
  3315. awe_sample_list *smp;
  3316. awe_voice_info *vp = &rec->v;
  3317. vp->index = 0;
  3318. if ((smp = search_sample_index(rec->holder, vp->sample)) == NULL)
  3319. return 0;
  3320. /* set the actual sample offsets */
  3321. vp->start += smp->v.start;
  3322. vp->end += smp->v.end;
  3323. vp->loopstart += smp->v.loopstart;
  3324. vp->loopend += smp->v.loopend;
  3325. /* copy mode flags */
  3326. vp->mode = smp->v.mode_flags;
  3327. /* set flag */
  3328. vp->index = 1;
  3329. return 1;
  3330. }
  3331. /*
  3332. * voice allocation
  3333. */
  3334. /* look for all voices associated with the specified note & velocity */
  3335. static int
  3336. awe_search_multi_voices(awe_voice_list *rec, int note, int velocity,
  3337. awe_voice_info **vlist)
  3338. {
  3339. int nvoices;
  3340. nvoices = 0;
  3341. for (; rec; rec = rec->next_instr) {
  3342. if (note >= rec->v.low &&
  3343. note <= rec->v.high &&
  3344. velocity >= rec->v.vellow &&
  3345. velocity <= rec->v.velhigh) {
  3346. if (rec->type == V_ST_MAPPED) {
  3347. /* mapper */
  3348. vlist[0] = &rec->v;
  3349. return -1;
  3350. }
  3351. vlist[nvoices++] = &rec->v;
  3352. if (nvoices >= AWE_MAX_VOICES)
  3353. break;
  3354. }
  3355. }
  3356. return nvoices;
  3357. }
  3358. /* store the voice list from the specified note and velocity.
  3359. if the preset is mapped, seek for the destination preset, and rewrite
  3360. the note number if necessary.
  3361. */
  3362. static int
  3363. really_alloc_voices(int bank, int instr, int *note, int velocity, awe_voice_info **vlist)
  3364. {
  3365. int nvoices;
  3366. awe_voice_list *vrec;
  3367. int level = 0;
  3368. for (;;) {
  3369. vrec = awe_search_instr(bank, instr, *note);
  3370. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3371. if (nvoices == 0) {
  3372. if (bank == AWE_DRUM_BANK)
  3373. /* search default drumset */
  3374. vrec = awe_search_instr(bank, ctrls[AWE_MD_DEF_DRUM], *note);
  3375. else
  3376. /* search default preset */
  3377. vrec = awe_search_instr(ctrls[AWE_MD_DEF_BANK], instr, *note);
  3378. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3379. }
  3380. if (nvoices == 0) {
  3381. if (bank == AWE_DRUM_BANK && ctrls[AWE_MD_DEF_DRUM] != 0)
  3382. /* search default drumset */
  3383. vrec = awe_search_instr(bank, 0, *note);
  3384. else if (bank != AWE_DRUM_BANK && ctrls[AWE_MD_DEF_BANK] != 0)
  3385. /* search default preset */
  3386. vrec = awe_search_instr(0, instr, *note);
  3387. nvoices = awe_search_multi_voices(vrec, *note, velocity, vlist);
  3388. }
  3389. if (nvoices < 0) { /* mapping */
  3390. int key = vlist[0]->fixkey;
  3391. instr = vlist[0]->start;
  3392. bank = vlist[0]->end;
  3393. if (level++ > 5) {
  3394. printk(KERN_ERR "AWE32: too deep mapping level\n");
  3395. return 0;
  3396. }
  3397. if (key >= 0)
  3398. *note = key;
  3399. } else
  3400. break;
  3401. }
  3402. return nvoices;
  3403. }
  3404. /* allocate voices corresponding note and velocity; supports multiple insts. */
  3405. static void
  3406. awe_alloc_multi_voices(int ch, int note, int velocity, int key)
  3407. {
  3408. int i, v, nvoices, bank;
  3409. awe_voice_info *vlist[AWE_MAX_VOICES];
  3410. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(ch))
  3411. bank = AWE_DRUM_BANK; /* always search drumset */
  3412. else
  3413. bank = channels[ch].bank;
  3414. /* check the possible voices; note may be changeable if mapped */
  3415. nvoices = really_alloc_voices(bank, channels[ch].instr,
  3416. &note, velocity, vlist);
  3417. /* set the voices */
  3418. current_alloc_time++;
  3419. for (i = 0; i < nvoices; i++) {
  3420. v = awe_clear_voice();
  3421. voices[v].key = key;
  3422. voices[v].ch = ch;
  3423. voices[v].note = note;
  3424. voices[v].velocity = velocity;
  3425. voices[v].time = current_alloc_time;
  3426. voices[v].cinfo = &channels[ch];
  3427. voices[v].sample = vlist[i];
  3428. voices[v].state = AWE_ST_MARK;
  3429. voices[v].layer = nvoices - i - 1; /* in reverse order */
  3430. }
  3431. /* clear the mark in allocated voices */
  3432. for (i = 0; i < awe_max_voices; i++) {
  3433. if (voices[i].state == AWE_ST_MARK)
  3434. voices[i].state = AWE_ST_OFF;
  3435. }
  3436. }
  3437. /* search an empty voice.
  3438. if no empty voice is found, at least terminate a voice
  3439. */
  3440. static int
  3441. awe_clear_voice(void)
  3442. {
  3443. enum {
  3444. OFF=0, RELEASED, SUSTAINED, PLAYING, END
  3445. };
  3446. struct voice_candidate_t {
  3447. int best;
  3448. int time;
  3449. int vtarget;
  3450. } candidate[END];
  3451. int i, type, vtarget;
  3452. vtarget = 0xffff;
  3453. for (type = OFF; type < END; type++) {
  3454. candidate[type].best = -1;
  3455. candidate[type].time = current_alloc_time + 1;
  3456. candidate[type].vtarget = vtarget;
  3457. }
  3458. for (i = 0; i < awe_max_voices; i++) {
  3459. if (voices[i].state & AWE_ST_OFF)
  3460. type = OFF;
  3461. else if (voices[i].state & AWE_ST_RELEASED)
  3462. type = RELEASED;
  3463. else if (voices[i].state & AWE_ST_SUSTAINED)
  3464. type = SUSTAINED;
  3465. else if (voices[i].state & ~AWE_ST_MARK)
  3466. type = PLAYING;
  3467. else
  3468. continue;
  3469. #ifdef AWE_CHECK_VTARGET
  3470. /* get current volume */
  3471. vtarget = (awe_peek_dw(AWE_VTFT(i)) >> 16) & 0xffff;
  3472. #endif
  3473. if (candidate[type].best < 0 ||
  3474. vtarget < candidate[type].vtarget ||
  3475. (vtarget == candidate[type].vtarget &&
  3476. voices[i].time < candidate[type].time)) {
  3477. candidate[type].best = i;
  3478. candidate[type].time = voices[i].time;
  3479. candidate[type].vtarget = vtarget;
  3480. }
  3481. }
  3482. for (type = OFF; type < END; type++) {
  3483. if ((i = candidate[type].best) >= 0) {
  3484. if (voices[i].state != AWE_ST_OFF)
  3485. awe_terminate(i);
  3486. awe_voice_init(i, TRUE);
  3487. return i;
  3488. }
  3489. }
  3490. return 0;
  3491. }
  3492. /* search sample for the specified note & velocity and set it on the voice;
  3493. * note that voice is the voice index (not channel index)
  3494. */
  3495. static void
  3496. awe_alloc_one_voice(int voice, int note, int velocity)
  3497. {
  3498. int ch, nvoices, bank;
  3499. awe_voice_info *vlist[AWE_MAX_VOICES];
  3500. ch = voices[voice].ch;
  3501. if (MULTI_LAYER_MODE() && IS_DRUM_CHANNEL(voice))
  3502. bank = AWE_DRUM_BANK; /* always search drumset */
  3503. else
  3504. bank = voices[voice].cinfo->bank;
  3505. nvoices = really_alloc_voices(bank, voices[voice].cinfo->instr,
  3506. &note, velocity, vlist);
  3507. if (nvoices > 0) {
  3508. voices[voice].time = ++current_alloc_time;
  3509. voices[voice].sample = vlist[0]; /* use the first one */
  3510. voices[voice].layer = 0;
  3511. voices[voice].note = note;
  3512. voices[voice].velocity = velocity;
  3513. }
  3514. }
  3515. /*
  3516. * sequencer2 functions
  3517. */
  3518. /* search an empty voice; used by sequencer2 */
  3519. static int
  3520. awe_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
  3521. {
  3522. playing_mode = AWE_PLAY_MULTI2;
  3523. awe_info.nr_voices = AWE_MAX_CHANNELS;
  3524. return awe_clear_voice();
  3525. }
  3526. /* set up voice; used by sequencer2 */
  3527. static void
  3528. awe_setup_voice(int dev, int voice, int chn)
  3529. {
  3530. struct channel_info *info;
  3531. if (synth_devs[dev] == NULL ||
  3532. (info = &synth_devs[dev]->chn_info[chn]) == NULL)
  3533. return;
  3534. if (voice < 0 || voice >= awe_max_voices)
  3535. return;
  3536. DEBUG(2,printk("AWE32: [setup(%d) ch=%d]\n", voice, chn));
  3537. channels[chn].expression_vol = info->controllers[CTL_EXPRESSION];
  3538. channels[chn].main_vol = info->controllers[CTL_MAIN_VOLUME];
  3539. channels[chn].panning =
  3540. info->controllers[CTL_PAN] * 2 - 128; /* signed 8bit */
  3541. channels[chn].bender = info->bender_value; /* zero center */
  3542. channels[chn].bank = info->controllers[CTL_BANK_SELECT];
  3543. channels[chn].sustained = info->controllers[CTL_SUSTAIN];
  3544. if (info->controllers[CTL_EXT_EFF_DEPTH]) {
  3545. FX_SET(&channels[chn].fx, AWE_FX_REVERB,
  3546. info->controllers[CTL_EXT_EFF_DEPTH] * 2);
  3547. }
  3548. if (info->controllers[CTL_CHORUS_DEPTH]) {
  3549. FX_SET(&channels[chn].fx, AWE_FX_CHORUS,
  3550. info->controllers[CTL_CHORUS_DEPTH] * 2);
  3551. }
  3552. awe_set_instr(dev, chn, info->pgm_num);
  3553. }
  3554. #ifdef CONFIG_AWE32_MIXER
  3555. /*
  3556. * AWE32 mixer device control
  3557. */
  3558. static int awe_mixer_ioctl(int dev, unsigned int cmd, void __user *arg);
  3559. static int my_mixerdev = -1;
  3560. static struct mixer_operations awe_mixer_operations = {
  3561. .owner = THIS_MODULE,
  3562. .id = "AWE",
  3563. .name = "AWE32 Equalizer",
  3564. .ioctl = awe_mixer_ioctl,
  3565. };
  3566. static void __init attach_mixer(void)
  3567. {
  3568. if ((my_mixerdev = sound_alloc_mixerdev()) >= 0) {
  3569. mixer_devs[my_mixerdev] = &awe_mixer_operations;
  3570. }
  3571. }
  3572. static void unload_mixer(void)
  3573. {
  3574. if (my_mixerdev >= 0)
  3575. sound_unload_mixerdev(my_mixerdev);
  3576. }
  3577. static int
  3578. awe_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
  3579. {
  3580. int i, level, value;
  3581. if (((cmd >> 8) & 0xff) != 'M')
  3582. return -EINVAL;
  3583. if (get_user(level, (int __user *)arg))
  3584. return -EFAULT;
  3585. level = ((level & 0xff) + (level >> 8)) / 2;
  3586. DEBUG(0,printk("AWEMix: cmd=%x val=%d\n", cmd & 0xff, level));
  3587. if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
  3588. switch (cmd & 0xff) {
  3589. case SOUND_MIXER_BASS:
  3590. value = level * 12 / 100;
  3591. if (value >= 12)
  3592. value = 11;
  3593. ctrls[AWE_MD_BASS_LEVEL] = value;
  3594. awe_update_equalizer();
  3595. break;
  3596. case SOUND_MIXER_TREBLE:
  3597. value = level * 12 / 100;
  3598. if (value >= 12)
  3599. value = 11;
  3600. ctrls[AWE_MD_TREBLE_LEVEL] = value;
  3601. awe_update_equalizer();
  3602. break;
  3603. case SOUND_MIXER_VOLUME:
  3604. level = level * 127 / 100;
  3605. if (level >= 128) level = 127;
  3606. atten_relative = FALSE;
  3607. atten_offset = vol_table[level];
  3608. awe_update_volume();
  3609. break;
  3610. }
  3611. }
  3612. switch (cmd & 0xff) {
  3613. case SOUND_MIXER_BASS:
  3614. level = ctrls[AWE_MD_BASS_LEVEL] * 100 / 24;
  3615. level = (level << 8) | level;
  3616. break;
  3617. case SOUND_MIXER_TREBLE:
  3618. level = ctrls[AWE_MD_TREBLE_LEVEL] * 100 / 24;
  3619. level = (level << 8) | level;
  3620. break;
  3621. case SOUND_MIXER_VOLUME:
  3622. value = atten_offset;
  3623. if (atten_relative)
  3624. value += ctrls[AWE_MD_ZERO_ATTEN];
  3625. for (i = 127; i > 0; i--) {
  3626. if (value <= vol_table[i])
  3627. break;
  3628. }
  3629. level = i * 100 / 127;
  3630. level = (level << 8) | level;
  3631. break;
  3632. case SOUND_MIXER_DEVMASK:
  3633. level = SOUND_MASK_BASS|SOUND_MASK_TREBLE|SOUND_MASK_VOLUME;
  3634. break;
  3635. default:
  3636. level = 0;
  3637. break;
  3638. }
  3639. if (put_user(level, (int __user *)arg))
  3640. return -EFAULT;
  3641. return level;
  3642. }
  3643. #endif /* CONFIG_AWE32_MIXER */
  3644. /*
  3645. * initialization of Emu8000
  3646. */
  3647. /* intiailize audio channels */
  3648. static void
  3649. awe_init_audio(void)
  3650. {
  3651. int ch;
  3652. /* turn off envelope engines */
  3653. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3654. awe_poke(AWE_DCYSUSV(ch), 0x80);
  3655. }
  3656. /* reset all other parameters to zero */
  3657. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3658. awe_poke(AWE_ENVVOL(ch), 0);
  3659. awe_poke(AWE_ENVVAL(ch), 0);
  3660. awe_poke(AWE_DCYSUS(ch), 0);
  3661. awe_poke(AWE_ATKHLDV(ch), 0);
  3662. awe_poke(AWE_LFO1VAL(ch), 0);
  3663. awe_poke(AWE_ATKHLD(ch), 0);
  3664. awe_poke(AWE_LFO2VAL(ch), 0);
  3665. awe_poke(AWE_IP(ch), 0);
  3666. awe_poke(AWE_IFATN(ch), 0);
  3667. awe_poke(AWE_PEFE(ch), 0);
  3668. awe_poke(AWE_FMMOD(ch), 0);
  3669. awe_poke(AWE_TREMFRQ(ch), 0);
  3670. awe_poke(AWE_FM2FRQ2(ch), 0);
  3671. awe_poke_dw(AWE_PTRX(ch), 0);
  3672. awe_poke_dw(AWE_VTFT(ch), 0);
  3673. awe_poke_dw(AWE_PSST(ch), 0);
  3674. awe_poke_dw(AWE_CSL(ch), 0);
  3675. awe_poke_dw(AWE_CCCA(ch), 0);
  3676. }
  3677. for (ch = 0; ch < AWE_MAX_VOICES; ch++) {
  3678. awe_poke_dw(AWE_CPF(ch), 0);
  3679. awe_poke_dw(AWE_CVCF(ch), 0);
  3680. }
  3681. }
  3682. /* initialize DMA address */
  3683. static void
  3684. awe_init_dma(void)
  3685. {
  3686. awe_poke_dw(AWE_SMALR, 0);
  3687. awe_poke_dw(AWE_SMARR, 0);
  3688. awe_poke_dw(AWE_SMALW, 0);
  3689. awe_poke_dw(AWE_SMARW, 0);
  3690. }
  3691. /* initialization arrays; from ADIP */
  3692. static unsigned short init1[128] = {
  3693. 0x03ff, 0x0030, 0x07ff, 0x0130, 0x0bff, 0x0230, 0x0fff, 0x0330,
  3694. 0x13ff, 0x0430, 0x17ff, 0x0530, 0x1bff, 0x0630, 0x1fff, 0x0730,
  3695. 0x23ff, 0x0830, 0x27ff, 0x0930, 0x2bff, 0x0a30, 0x2fff, 0x0b30,
  3696. 0x33ff, 0x0c30, 0x37ff, 0x0d30, 0x3bff, 0x0e30, 0x3fff, 0x0f30,
  3697. 0x43ff, 0x0030, 0x47ff, 0x0130, 0x4bff, 0x0230, 0x4fff, 0x0330,
  3698. 0x53ff, 0x0430, 0x57ff, 0x0530, 0x5bff, 0x0630, 0x5fff, 0x0730,
  3699. 0x63ff, 0x0830, 0x67ff, 0x0930, 0x6bff, 0x0a30, 0x6fff, 0x0b30,
  3700. 0x73ff, 0x0c30, 0x77ff, 0x0d30, 0x7bff, 0x0e30, 0x7fff, 0x0f30,
  3701. 0x83ff, 0x0030, 0x87ff, 0x0130, 0x8bff, 0x0230, 0x8fff, 0x0330,
  3702. 0x93ff, 0x0430, 0x97ff, 0x0530, 0x9bff, 0x0630, 0x9fff, 0x0730,
  3703. 0xa3ff, 0x0830, 0xa7ff, 0x0930, 0xabff, 0x0a30, 0xafff, 0x0b30,
  3704. 0xb3ff, 0x0c30, 0xb7ff, 0x0d30, 0xbbff, 0x0e30, 0xbfff, 0x0f30,
  3705. 0xc3ff, 0x0030, 0xc7ff, 0x0130, 0xcbff, 0x0230, 0xcfff, 0x0330,
  3706. 0xd3ff, 0x0430, 0xd7ff, 0x0530, 0xdbff, 0x0630, 0xdfff, 0x0730,
  3707. 0xe3ff, 0x0830, 0xe7ff, 0x0930, 0xebff, 0x0a30, 0xefff, 0x0b30,
  3708. 0xf3ff, 0x0c30, 0xf7ff, 0x0d30, 0xfbff, 0x0e30, 0xffff, 0x0f30,
  3709. };
  3710. static unsigned short init2[128] = {
  3711. 0x03ff, 0x8030, 0x07ff, 0x8130, 0x0bff, 0x8230, 0x0fff, 0x8330,
  3712. 0x13ff, 0x8430, 0x17ff, 0x8530, 0x1bff, 0x8630, 0x1fff, 0x8730,
  3713. 0x23ff, 0x8830, 0x27ff, 0x8930, 0x2bff, 0x8a30, 0x2fff, 0x8b30,
  3714. 0x33ff, 0x8c30, 0x37ff, 0x8d30, 0x3bff, 0x8e30, 0x3fff, 0x8f30,
  3715. 0x43ff, 0x8030, 0x47ff, 0x8130, 0x4bff, 0x8230, 0x4fff, 0x8330,
  3716. 0x53ff, 0x8430, 0x57ff, 0x8530, 0x5bff, 0x8630, 0x5fff, 0x8730,
  3717. 0x63ff, 0x8830, 0x67ff, 0x8930, 0x6bff, 0x8a30, 0x6fff, 0x8b30,
  3718. 0x73ff, 0x8c30, 0x77ff, 0x8d30, 0x7bff, 0x8e30, 0x7fff, 0x8f30,
  3719. 0x83ff, 0x8030, 0x87ff, 0x8130, 0x8bff, 0x8230, 0x8fff, 0x8330,
  3720. 0x93ff, 0x8430, 0x97ff, 0x8530, 0x9bff, 0x8630, 0x9fff, 0x8730,
  3721. 0xa3ff, 0x8830, 0xa7ff, 0x8930, 0xabff, 0x8a30, 0xafff, 0x8b30,
  3722. 0xb3ff, 0x8c30, 0xb7ff, 0x8d30, 0xbbff, 0x8e30, 0xbfff, 0x8f30,
  3723. 0xc3ff, 0x8030, 0xc7ff, 0x8130, 0xcbff, 0x8230, 0xcfff, 0x8330,
  3724. 0xd3ff, 0x8430, 0xd7ff, 0x8530, 0xdbff, 0x8630, 0xdfff, 0x8730,
  3725. 0xe3ff, 0x8830, 0xe7ff, 0x8930, 0xebff, 0x8a30, 0xefff, 0x8b30,
  3726. 0xf3ff, 0x8c30, 0xf7ff, 0x8d30, 0xfbff, 0x8e30, 0xffff, 0x8f30,
  3727. };
  3728. static unsigned short init3[128] = {
  3729. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  3730. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x8F7C, 0x167E, 0xF254,
  3731. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x8BAA, 0x1B6D, 0xF234,
  3732. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x86E7, 0x229E, 0xF224,
  3733. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x87F6, 0x2C28, 0xF254,
  3734. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x8F02, 0x1341, 0xF264,
  3735. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x8FA9, 0x3EB5, 0xF294,
  3736. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0xC4C3, 0x3EBB, 0xC5C3,
  3737. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x8671, 0x14FD, 0x8287,
  3738. 0x3EBC, 0xE610, 0x3EC8, 0x8C7B, 0x031A, 0x87E6, 0x3EC8, 0x86F7,
  3739. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x821F, 0x3ECA, 0x8386,
  3740. 0x3EC1, 0x8C03, 0x3EC9, 0x831E, 0x3ECA, 0x8C4C, 0x3EBF, 0x8C55,
  3741. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x8EAD, 0x3EC8, 0xD308,
  3742. 0x3EC2, 0x8F7E, 0x3ECB, 0x8219, 0x3ECB, 0xD26E, 0x3EC5, 0x831F,
  3743. 0x3EC6, 0xC308, 0x3EC3, 0xB2FF, 0x3EC9, 0x8265, 0x3EC9, 0x8319,
  3744. 0x1342, 0xD36E, 0x3EC7, 0xB3FF, 0x0000, 0x8365, 0x1420, 0x9570,
  3745. };
  3746. static unsigned short init4[128] = {
  3747. 0x0C10, 0x8470, 0x14FE, 0xB488, 0x167F, 0xA470, 0x18E7, 0x84B5,
  3748. 0x1B6E, 0x842A, 0x1F1D, 0x852A, 0x0DA3, 0x0F7C, 0x167E, 0x7254,
  3749. 0x0000, 0x842A, 0x0001, 0x852A, 0x18E6, 0x0BAA, 0x1B6D, 0x7234,
  3750. 0x229F, 0x8429, 0x2746, 0x8529, 0x1F1C, 0x06E7, 0x229E, 0x7224,
  3751. 0x0DA4, 0x8429, 0x2C29, 0x8529, 0x2745, 0x07F6, 0x2C28, 0x7254,
  3752. 0x383B, 0x8428, 0x320F, 0x8528, 0x320E, 0x0F02, 0x1341, 0x7264,
  3753. 0x3EB6, 0x8428, 0x3EB9, 0x8528, 0x383A, 0x0FA9, 0x3EB5, 0x7294,
  3754. 0x3EB7, 0x8474, 0x3EBA, 0x8575, 0x3EB8, 0x44C3, 0x3EBB, 0x45C3,
  3755. 0x0000, 0xA404, 0x0001, 0xA504, 0x141F, 0x0671, 0x14FD, 0x0287,
  3756. 0x3EBC, 0xE610, 0x3EC8, 0x0C7B, 0x031A, 0x07E6, 0x3EC8, 0x86F7,
  3757. 0x3EC0, 0x821E, 0x3EBE, 0xD208, 0x3EBD, 0x021F, 0x3ECA, 0x0386,
  3758. 0x3EC1, 0x0C03, 0x3EC9, 0x031E, 0x3ECA, 0x8C4C, 0x3EBF, 0x0C55,
  3759. 0x3EC9, 0xC208, 0x3EC4, 0xBC84, 0x3EC8, 0x0EAD, 0x3EC8, 0xD308,
  3760. 0x3EC2, 0x8F7E, 0x3ECB, 0x0219, 0x3ECB, 0xD26E, 0x3EC5, 0x031F,
  3761. 0x3EC6, 0xC308, 0x3EC3, 0x32FF, 0x3EC9, 0x0265, 0x3EC9, 0x8319,
  3762. 0x1342, 0xD36E, 0x3EC7, 0x33FF, 0x0000, 0x8365, 0x1420, 0x9570,
  3763. };
  3764. /* send initialization arrays to start up */
  3765. static void
  3766. awe_init_array(void)
  3767. {
  3768. awe_send_array(init1);
  3769. awe_wait(1024);
  3770. awe_send_array(init2);
  3771. awe_send_array(init3);
  3772. awe_poke_dw(AWE_HWCF4, 0);
  3773. awe_poke_dw(AWE_HWCF5, 0x83);
  3774. awe_poke_dw(AWE_HWCF6, 0x8000);
  3775. awe_send_array(init4);
  3776. }
  3777. /* send an initialization array */
  3778. static void
  3779. awe_send_array(unsigned short *data)
  3780. {
  3781. int i;
  3782. unsigned short *p;
  3783. p = data;
  3784. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3785. awe_poke(AWE_INIT1(i), *p);
  3786. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3787. awe_poke(AWE_INIT2(i), *p);
  3788. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3789. awe_poke(AWE_INIT3(i), *p);
  3790. for (i = 0; i < AWE_MAX_VOICES; i++, p++)
  3791. awe_poke(AWE_INIT4(i), *p);
  3792. }
  3793. /*
  3794. * set up awe32 channels to some known state.
  3795. */
  3796. /* set the envelope & LFO parameters to the default values; see ADIP */
  3797. static void
  3798. awe_tweak_voice(int i)
  3799. {
  3800. /* set all mod/vol envelope shape to minimum */
  3801. awe_poke(AWE_ENVVOL(i), 0x8000);
  3802. awe_poke(AWE_ENVVAL(i), 0x8000);
  3803. awe_poke(AWE_DCYSUS(i), 0x7F7F);
  3804. awe_poke(AWE_ATKHLDV(i), 0x7F7F);
  3805. awe_poke(AWE_ATKHLD(i), 0x7F7F);
  3806. awe_poke(AWE_PEFE(i), 0); /* mod envelope height to zero */
  3807. awe_poke(AWE_LFO1VAL(i), 0x8000); /* no delay for LFO1 */
  3808. awe_poke(AWE_LFO2VAL(i), 0x8000);
  3809. awe_poke(AWE_IP(i), 0xE000); /* no pitch shift */
  3810. awe_poke(AWE_IFATN(i), 0xFF00); /* volume to minimum */
  3811. awe_poke(AWE_FMMOD(i), 0);
  3812. awe_poke(AWE_TREMFRQ(i), 0);
  3813. awe_poke(AWE_FM2FRQ2(i), 0);
  3814. }
  3815. static void
  3816. awe_tweak(void)
  3817. {
  3818. int i;
  3819. /* reset all channels */
  3820. for (i = 0; i < awe_max_voices; i++)
  3821. awe_tweak_voice(i);
  3822. }
  3823. /*
  3824. * initializes the FM section of AWE32;
  3825. * see Vince Vu's unofficial AWE32 programming guide
  3826. */
  3827. static void
  3828. awe_init_fm(void)
  3829. {
  3830. #ifndef AWE_ALWAYS_INIT_FM
  3831. /* if no extended memory is on board.. */
  3832. if (memsize <= 0)
  3833. return;
  3834. #endif
  3835. DEBUG(3,printk("AWE32: initializing FM\n"));
  3836. /* Initialize the last two channels for DRAM refresh and producing
  3837. the reverb and chorus effects for Yamaha OPL-3 synthesizer */
  3838. /* 31: FM left channel, 0xffffe0-0xffffe8 */
  3839. awe_poke(AWE_DCYSUSV(30), 0x80);
  3840. awe_poke_dw(AWE_PSST(30), 0xFFFFFFE0); /* full left */
  3841. awe_poke_dw(AWE_CSL(30), 0x00FFFFE8 |
  3842. (DEF_FM_CHORUS_DEPTH << 24));
  3843. awe_poke_dw(AWE_PTRX(30), (DEF_FM_REVERB_DEPTH << 8));
  3844. awe_poke_dw(AWE_CPF(30), 0);
  3845. awe_poke_dw(AWE_CCCA(30), 0x00FFFFE3);
  3846. /* 32: FM right channel, 0xfffff0-0xfffff8 */
  3847. awe_poke(AWE_DCYSUSV(31), 0x80);
  3848. awe_poke_dw(AWE_PSST(31), 0x00FFFFF0); /* full right */
  3849. awe_poke_dw(AWE_CSL(31), 0x00FFFFF8 |
  3850. (DEF_FM_CHORUS_DEPTH << 24));
  3851. awe_poke_dw(AWE_PTRX(31), (DEF_FM_REVERB_DEPTH << 8));
  3852. awe_poke_dw(AWE_CPF(31), 0x8000);
  3853. awe_poke_dw(AWE_CCCA(31), 0x00FFFFF3);
  3854. /* skew volume & cutoff */
  3855. awe_poke_dw(AWE_VTFT(30), 0x8000FFFF);
  3856. awe_poke_dw(AWE_VTFT(31), 0x8000FFFF);
  3857. voices[30].state = AWE_ST_FM;
  3858. voices[31].state = AWE_ST_FM;
  3859. /* change maximum channels to 30 */
  3860. awe_max_voices = AWE_NORMAL_VOICES;
  3861. if (playing_mode == AWE_PLAY_DIRECT)
  3862. awe_info.nr_voices = awe_max_voices;
  3863. else
  3864. awe_info.nr_voices = AWE_MAX_CHANNELS;
  3865. voice_alloc->max_voice = awe_max_voices;
  3866. }
  3867. /*
  3868. * AWE32 DRAM access routines
  3869. */
  3870. /* open DRAM write accessing mode */
  3871. static int
  3872. awe_open_dram_for_write(int offset, int channels)
  3873. {
  3874. int vidx[AWE_NORMAL_VOICES];
  3875. int i;
  3876. if (channels < 0 || channels >= AWE_NORMAL_VOICES) {
  3877. channels = AWE_NORMAL_VOICES;
  3878. for (i = 0; i < AWE_NORMAL_VOICES; i++)
  3879. vidx[i] = i;
  3880. } else {
  3881. for (i = 0; i < channels; i++) {
  3882. vidx[i] = awe_clear_voice();
  3883. voices[vidx[i]].state = AWE_ST_MARK;
  3884. }
  3885. }
  3886. /* use all channels for DMA transfer */
  3887. for (i = 0; i < channels; i++) {
  3888. if (vidx[i] < 0) continue;
  3889. awe_poke(AWE_DCYSUSV(vidx[i]), 0x80);
  3890. awe_poke_dw(AWE_VTFT(vidx[i]), 0);
  3891. awe_poke_dw(AWE_CVCF(vidx[i]), 0);
  3892. awe_poke_dw(AWE_PTRX(vidx[i]), 0x40000000);
  3893. awe_poke_dw(AWE_CPF(vidx[i]), 0x40000000);
  3894. awe_poke_dw(AWE_PSST(vidx[i]), 0);
  3895. awe_poke_dw(AWE_CSL(vidx[i]), 0);
  3896. awe_poke_dw(AWE_CCCA(vidx[i]), 0x06000000);
  3897. voices[vidx[i]].state = AWE_ST_DRAM;
  3898. }
  3899. /* point channels 31 & 32 to ROM samples for DRAM refresh */
  3900. awe_poke_dw(AWE_VTFT(30), 0);
  3901. awe_poke_dw(AWE_PSST(30), 0x1d8);
  3902. awe_poke_dw(AWE_CSL(30), 0x1e0);
  3903. awe_poke_dw(AWE_CCCA(30), 0x1d8);
  3904. awe_poke_dw(AWE_VTFT(31), 0);
  3905. awe_poke_dw(AWE_PSST(31), 0x1d8);
  3906. awe_poke_dw(AWE_CSL(31), 0x1e0);
  3907. awe_poke_dw(AWE_CCCA(31), 0x1d8);
  3908. voices[30].state = AWE_ST_FM;
  3909. voices[31].state = AWE_ST_FM;
  3910. /* if full bit is on, not ready to write on */
  3911. if (awe_peek_dw(AWE_SMALW) & 0x80000000) {
  3912. for (i = 0; i < channels; i++) {
  3913. awe_poke_dw(AWE_CCCA(vidx[i]), 0);
  3914. voices[vidx[i]].state = AWE_ST_OFF;
  3915. }
  3916. printk("awe: not ready to write..\n");
  3917. return -EPERM;
  3918. }
  3919. /* set address to write */
  3920. awe_poke_dw(AWE_SMALW, offset);
  3921. return 0;
  3922. }
  3923. /* open DRAM for RAM size detection */
  3924. static void
  3925. awe_open_dram_for_check(void)
  3926. {
  3927. int i;
  3928. for (i = 0; i < AWE_NORMAL_VOICES; i++) {
  3929. awe_poke(AWE_DCYSUSV(i), 0x80);
  3930. awe_poke_dw(AWE_VTFT(i), 0);
  3931. awe_poke_dw(AWE_CVCF(i), 0);
  3932. awe_poke_dw(AWE_PTRX(i), 0x40000000);
  3933. awe_poke_dw(AWE_CPF(i), 0x40000000);
  3934. awe_poke_dw(AWE_PSST(i), 0);
  3935. awe_poke_dw(AWE_CSL(i), 0);
  3936. if (i & 1) /* DMA write */
  3937. awe_poke_dw(AWE_CCCA(i), 0x06000000);
  3938. else /* DMA read */
  3939. awe_poke_dw(AWE_CCCA(i), 0x04000000);
  3940. voices[i].state = AWE_ST_DRAM;
  3941. }
  3942. }
  3943. /* close dram access */
  3944. static void
  3945. awe_close_dram(void)
  3946. {
  3947. int i;
  3948. /* wait until FULL bit in SMAxW register be false */
  3949. for (i = 0; i < 10000; i++) {
  3950. if (!(awe_peek_dw(AWE_SMALW) & 0x80000000))
  3951. break;
  3952. awe_wait(10);
  3953. }
  3954. for (i = 0; i < AWE_NORMAL_VOICES; i++) {
  3955. if (voices[i].state == AWE_ST_DRAM) {
  3956. awe_poke_dw(AWE_CCCA(i), 0);
  3957. awe_poke(AWE_DCYSUSV(i), 0x807F);
  3958. voices[i].state = AWE_ST_OFF;
  3959. }
  3960. }
  3961. }
  3962. /*
  3963. * check dram size on AWE board
  3964. */
  3965. /* any three numbers you like */
  3966. #define UNIQUE_ID1 0x1234
  3967. #define UNIQUE_ID2 0x4321
  3968. #define UNIQUE_ID3 0xABCD
  3969. static void __init
  3970. awe_check_dram(void)
  3971. {
  3972. if (awe_present) /* already initialized */
  3973. return;
  3974. if (memsize >= 0) { /* given by config file or module option */
  3975. memsize *= 1024; /* convert to Kbytes */
  3976. return;
  3977. }
  3978. awe_open_dram_for_check();
  3979. memsize = 0;
  3980. /* set up unique two id numbers */
  3981. awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET);
  3982. awe_poke(AWE_SMLD, UNIQUE_ID1);
  3983. awe_poke(AWE_SMLD, UNIQUE_ID2);
  3984. while (memsize < AWE_MAX_DRAM_SIZE) {
  3985. awe_wait(5);
  3986. /* read a data on the DRAM start address */
  3987. awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET);
  3988. awe_peek(AWE_SMLD); /* discard stale data */
  3989. if (awe_peek(AWE_SMLD) != UNIQUE_ID1)
  3990. break;
  3991. if (awe_peek(AWE_SMLD) != UNIQUE_ID2)
  3992. break;
  3993. memsize += 512; /* increment 512kbytes */
  3994. /* Write a unique data on the test address;
  3995. * if the address is out of range, the data is written on
  3996. * 0x200000(=AWE_DRAM_OFFSET). Then the two id words are
  3997. * broken by this data.
  3998. */
  3999. awe_poke_dw(AWE_SMALW, AWE_DRAM_OFFSET + memsize*512L);
  4000. awe_poke(AWE_SMLD, UNIQUE_ID3);
  4001. awe_wait(5);
  4002. /* read a data on the just written DRAM address */
  4003. awe_poke_dw(AWE_SMALR, AWE_DRAM_OFFSET + memsize*512L);
  4004. awe_peek(AWE_SMLD); /* discard stale data */
  4005. if (awe_peek(AWE_SMLD) != UNIQUE_ID3)
  4006. break;
  4007. }
  4008. awe_close_dram();
  4009. DEBUG(0,printk("AWE32: %d Kbytes memory detected\n", memsize));
  4010. /* convert to Kbytes */
  4011. memsize *= 1024;
  4012. }
  4013. /*----------------------------------------------------------------*/
  4014. /*
  4015. * chorus and reverb controls; from VV's guide
  4016. */
  4017. /* 5 parameters for each chorus mode; 3 x 16bit, 2 x 32bit */
  4018. static char chorus_defined[AWE_CHORUS_NUMBERS];
  4019. static awe_chorus_fx_rec chorus_parm[AWE_CHORUS_NUMBERS] = {
  4020. {0xE600, 0x03F6, 0xBC2C ,0x00000000, 0x0000006D}, /* chorus 1 */
  4021. {0xE608, 0x031A, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 2 */
  4022. {0xE610, 0x031A, 0xBC84, 0x00000000, 0x00000083}, /* chorus 3 */
  4023. {0xE620, 0x0269, 0xBC6E, 0x00000000, 0x0000017C}, /* chorus 4 */
  4024. {0xE680, 0x04D3, 0xBCA6, 0x00000000, 0x0000005B}, /* feedback */
  4025. {0xE6E0, 0x044E, 0xBC37, 0x00000000, 0x00000026}, /* flanger */
  4026. {0xE600, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay */
  4027. {0xE6C0, 0x0B06, 0xBC00, 0x0000E000, 0x00000083}, /* short delay + feedback */
  4028. };
  4029. static int
  4030. awe_load_chorus_fx(awe_patch_info *patch, const char __user *addr, int count)
  4031. {
  4032. if (patch->optarg < AWE_CHORUS_PREDEFINED || patch->optarg >= AWE_CHORUS_NUMBERS) {
  4033. printk(KERN_WARNING "AWE32 Error: invalid chorus mode %d for uploading\n", patch->optarg);
  4034. return -EINVAL;
  4035. }
  4036. if (count < sizeof(awe_chorus_fx_rec)) {
  4037. printk(KERN_WARNING "AWE32 Error: too short chorus fx parameters\n");
  4038. return -EINVAL;
  4039. }
  4040. if (copy_from_user(&chorus_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
  4041. sizeof(awe_chorus_fx_rec)))
  4042. return -EFAULT;
  4043. chorus_defined[patch->optarg] = TRUE;
  4044. return 0;
  4045. }
  4046. static void
  4047. awe_set_chorus_mode(int effect)
  4048. {
  4049. if (effect < 0 || effect >= AWE_CHORUS_NUMBERS ||
  4050. (effect >= AWE_CHORUS_PREDEFINED && !chorus_defined[effect]))
  4051. return;
  4052. awe_poke(AWE_INIT3(9), chorus_parm[effect].feedback);
  4053. awe_poke(AWE_INIT3(12), chorus_parm[effect].delay_offset);
  4054. awe_poke(AWE_INIT4(3), chorus_parm[effect].lfo_depth);
  4055. awe_poke_dw(AWE_HWCF4, chorus_parm[effect].delay);
  4056. awe_poke_dw(AWE_HWCF5, chorus_parm[effect].lfo_freq);
  4057. awe_poke_dw(AWE_HWCF6, 0x8000);
  4058. awe_poke_dw(AWE_HWCF7, 0x0000);
  4059. }
  4060. static void
  4061. awe_update_chorus_mode(void)
  4062. {
  4063. awe_set_chorus_mode(ctrls[AWE_MD_CHORUS_MODE]);
  4064. }
  4065. /*----------------------------------------------------------------*/
  4066. /* reverb mode settings; write the following 28 data of 16 bit length
  4067. * on the corresponding ports in the reverb_cmds array
  4068. */
  4069. static char reverb_defined[AWE_CHORUS_NUMBERS];
  4070. static awe_reverb_fx_rec reverb_parm[AWE_REVERB_NUMBERS] = {
  4071. {{ /* room 1 */
  4072. 0xB488, 0xA450, 0x9550, 0x84B5, 0x383A, 0x3EB5, 0x72F4,
  4073. 0x72A4, 0x7254, 0x7204, 0x7204, 0x7204, 0x4416, 0x4516,
  4074. 0xA490, 0xA590, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4075. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4076. }},
  4077. {{ /* room 2 */
  4078. 0xB488, 0xA458, 0x9558, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4079. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  4080. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4081. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4082. }},
  4083. {{ /* room 3 */
  4084. 0xB488, 0xA460, 0x9560, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4085. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4416, 0x4516,
  4086. 0xA490, 0xA590, 0x842C, 0x852C, 0x842C, 0x852C, 0x842B,
  4087. 0x852B, 0x842B, 0x852B, 0x842A, 0x852A, 0x842A, 0x852A,
  4088. }},
  4089. {{ /* hall 1 */
  4090. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7284,
  4091. 0x7254, 0x7224, 0x7224, 0x7254, 0x7284, 0x4448, 0x4548,
  4092. 0xA440, 0xA540, 0x842B, 0x852B, 0x842B, 0x852B, 0x842A,
  4093. 0x852A, 0x842A, 0x852A, 0x8429, 0x8529, 0x8429, 0x8529,
  4094. }},
  4095. {{ /* hall 2 */
  4096. 0xB488, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7254,
  4097. 0x7234, 0x7224, 0x7254, 0x7264, 0x7294, 0x44C3, 0x45C3,
  4098. 0xA404, 0xA504, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4099. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4100. }},
  4101. {{ /* plate */
  4102. 0xB4FF, 0xA470, 0x9570, 0x84B5, 0x383A, 0x3EB5, 0x7234,
  4103. 0x7234, 0x7234, 0x7234, 0x7234, 0x7234, 0x4448, 0x4548,
  4104. 0xA440, 0xA540, 0x842A, 0x852A, 0x842A, 0x852A, 0x8429,
  4105. 0x8529, 0x8429, 0x8529, 0x8428, 0x8528, 0x8428, 0x8528,
  4106. }},
  4107. {{ /* delay */
  4108. 0xB4FF, 0xA470, 0x9500, 0x84B5, 0x333A, 0x39B5, 0x7204,
  4109. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  4110. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  4111. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  4112. }},
  4113. {{ /* panning delay */
  4114. 0xB4FF, 0xA490, 0x9590, 0x8474, 0x333A, 0x39B5, 0x7204,
  4115. 0x7204, 0x7204, 0x7204, 0x7204, 0x72F4, 0x4400, 0x4500,
  4116. 0xA4FF, 0xA5FF, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420,
  4117. 0x8520, 0x8420, 0x8520, 0x8420, 0x8520, 0x8420, 0x8520,
  4118. }},
  4119. };
  4120. static struct ReverbCmdPair {
  4121. unsigned short cmd, port;
  4122. } reverb_cmds[28] = {
  4123. {AWE_INIT1(0x03)}, {AWE_INIT1(0x05)}, {AWE_INIT4(0x1F)}, {AWE_INIT1(0x07)},
  4124. {AWE_INIT2(0x14)}, {AWE_INIT2(0x16)}, {AWE_INIT1(0x0F)}, {AWE_INIT1(0x17)},
  4125. {AWE_INIT1(0x1F)}, {AWE_INIT2(0x07)}, {AWE_INIT2(0x0F)}, {AWE_INIT2(0x17)},
  4126. {AWE_INIT2(0x1D)}, {AWE_INIT2(0x1F)}, {AWE_INIT3(0x01)}, {AWE_INIT3(0x03)},
  4127. {AWE_INIT1(0x09)}, {AWE_INIT1(0x0B)}, {AWE_INIT1(0x11)}, {AWE_INIT1(0x13)},
  4128. {AWE_INIT1(0x19)}, {AWE_INIT1(0x1B)}, {AWE_INIT2(0x01)}, {AWE_INIT2(0x03)},
  4129. {AWE_INIT2(0x09)}, {AWE_INIT2(0x0B)}, {AWE_INIT2(0x11)}, {AWE_INIT2(0x13)},
  4130. };
  4131. static int
  4132. awe_load_reverb_fx(awe_patch_info *patch, const char __user *addr, int count)
  4133. {
  4134. if (patch->optarg < AWE_REVERB_PREDEFINED || patch->optarg >= AWE_REVERB_NUMBERS) {
  4135. printk(KERN_WARNING "AWE32 Error: invalid reverb mode %d for uploading\n", patch->optarg);
  4136. return -EINVAL;
  4137. }
  4138. if (count < sizeof(awe_reverb_fx_rec)) {
  4139. printk(KERN_WARNING "AWE32 Error: too short reverb fx parameters\n");
  4140. return -EINVAL;
  4141. }
  4142. if (copy_from_user(&reverb_parm[patch->optarg], addr + AWE_PATCH_INFO_SIZE,
  4143. sizeof(awe_reverb_fx_rec)))
  4144. return -EFAULT;
  4145. reverb_defined[patch->optarg] = TRUE;
  4146. return 0;
  4147. }
  4148. static void
  4149. awe_set_reverb_mode(int effect)
  4150. {
  4151. int i;
  4152. if (effect < 0 || effect >= AWE_REVERB_NUMBERS ||
  4153. (effect >= AWE_REVERB_PREDEFINED && !reverb_defined[effect]))
  4154. return;
  4155. for (i = 0; i < 28; i++)
  4156. awe_poke(reverb_cmds[i].cmd, reverb_cmds[i].port,
  4157. reverb_parm[effect].parms[i]);
  4158. }
  4159. static void
  4160. awe_update_reverb_mode(void)
  4161. {
  4162. awe_set_reverb_mode(ctrls[AWE_MD_REVERB_MODE]);
  4163. }
  4164. /*
  4165. * treble/bass equalizer control
  4166. */
  4167. static unsigned short bass_parm[12][3] = {
  4168. {0xD26A, 0xD36A, 0x0000}, /* -12 dB */
  4169. {0xD25B, 0xD35B, 0x0000}, /* -8 */
  4170. {0xD24C, 0xD34C, 0x0000}, /* -6 */
  4171. {0xD23D, 0xD33D, 0x0000}, /* -4 */
  4172. {0xD21F, 0xD31F, 0x0000}, /* -2 */
  4173. {0xC208, 0xC308, 0x0001}, /* 0 (HW default) */
  4174. {0xC219, 0xC319, 0x0001}, /* +2 */
  4175. {0xC22A, 0xC32A, 0x0001}, /* +4 */
  4176. {0xC24C, 0xC34C, 0x0001}, /* +6 */
  4177. {0xC26E, 0xC36E, 0x0001}, /* +8 */
  4178. {0xC248, 0xC348, 0x0002}, /* +10 */
  4179. {0xC26A, 0xC36A, 0x0002}, /* +12 dB */
  4180. };
  4181. static unsigned short treble_parm[12][9] = {
  4182. {0x821E, 0xC26A, 0x031E, 0xC36A, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001}, /* -12 dB */
  4183. {0x821E, 0xC25B, 0x031E, 0xC35B, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4184. {0x821E, 0xC24C, 0x031E, 0xC34C, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4185. {0x821E, 0xC23D, 0x031E, 0xC33D, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4186. {0x821E, 0xC21F, 0x031E, 0xC31F, 0x021E, 0xD208, 0x831E, 0xD308, 0x0001},
  4187. {0x821E, 0xD208, 0x031E, 0xD308, 0x021E, 0xD208, 0x831E, 0xD308, 0x0002},
  4188. {0x821E, 0xD208, 0x031E, 0xD308, 0x021D, 0xD219, 0x831D, 0xD319, 0x0002},
  4189. {0x821E, 0xD208, 0x031E, 0xD308, 0x021C, 0xD22A, 0x831C, 0xD32A, 0x0002},
  4190. {0x821E, 0xD208, 0x031E, 0xD308, 0x021A, 0xD24C, 0x831A, 0xD34C, 0x0002},
  4191. {0x821E, 0xD208, 0x031E, 0xD308, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +8 (HW default) */
  4192. {0x821D, 0xD219, 0x031D, 0xD319, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002},
  4193. {0x821C, 0xD22A, 0x031C, 0xD32A, 0x0219, 0xD26E, 0x8319, 0xD36E, 0x0002}, /* +12 dB */
  4194. };
  4195. /*
  4196. * set Emu8000 digital equalizer; from 0 to 11 [-12dB - 12dB]
  4197. */
  4198. static void
  4199. awe_equalizer(int bass, int treble)
  4200. {
  4201. unsigned short w;
  4202. if (bass < 0 || bass > 11 || treble < 0 || treble > 11)
  4203. return;
  4204. awe_poke(AWE_INIT4(0x01), bass_parm[bass][0]);
  4205. awe_poke(AWE_INIT4(0x11), bass_parm[bass][1]);
  4206. awe_poke(AWE_INIT3(0x11), treble_parm[treble][0]);
  4207. awe_poke(AWE_INIT3(0x13), treble_parm[treble][1]);
  4208. awe_poke(AWE_INIT3(0x1B), treble_parm[treble][2]);
  4209. awe_poke(AWE_INIT4(0x07), treble_parm[treble][3]);
  4210. awe_poke(AWE_INIT4(0x0B), treble_parm[treble][4]);
  4211. awe_poke(AWE_INIT4(0x0D), treble_parm[treble][5]);
  4212. awe_poke(AWE_INIT4(0x17), treble_parm[treble][6]);
  4213. awe_poke(AWE_INIT4(0x19), treble_parm[treble][7]);
  4214. w = bass_parm[bass][2] + treble_parm[treble][8];
  4215. awe_poke(AWE_INIT4(0x15), (unsigned short)(w + 0x0262));
  4216. awe_poke(AWE_INIT4(0x1D), (unsigned short)(w + 0x8362));
  4217. }
  4218. static void awe_update_equalizer(void)
  4219. {
  4220. awe_equalizer(ctrls[AWE_MD_BASS_LEVEL], ctrls[AWE_MD_TREBLE_LEVEL]);
  4221. }
  4222. /*----------------------------------------------------------------*/
  4223. #ifdef CONFIG_AWE32_MIDIEMU
  4224. /*
  4225. * Emu8000 MIDI Emulation
  4226. */
  4227. /*
  4228. * midi queue record
  4229. */
  4230. /* queue type */
  4231. enum { Q_NONE, Q_VARLEN, Q_READ, Q_SYSEX, };
  4232. #define MAX_MIDIBUF 64
  4233. /* midi status */
  4234. typedef struct MidiStatus {
  4235. int queue; /* queue type */
  4236. int qlen; /* queue length */
  4237. int read; /* chars read */
  4238. int status; /* current status */
  4239. int chan; /* current channel */
  4240. unsigned char buf[MAX_MIDIBUF];
  4241. } MidiStatus;
  4242. /* MIDI mode type */
  4243. enum { MODE_GM, MODE_GS, MODE_XG, };
  4244. /* NRPN / CC -> Emu8000 parameter converter */
  4245. typedef struct {
  4246. int control;
  4247. int awe_effect;
  4248. unsigned short (*convert)(int val);
  4249. } ConvTable;
  4250. /*
  4251. * prototypes
  4252. */
  4253. static int awe_midi_open(int dev, int mode, void (*input)(int,unsigned char), void (*output)(int));
  4254. static void awe_midi_close(int dev);
  4255. static int awe_midi_ioctl(int dev, unsigned cmd, void __user * arg);
  4256. static int awe_midi_outputc(int dev, unsigned char midi_byte);
  4257. static void init_midi_status(MidiStatus *st);
  4258. static void clear_rpn(void);
  4259. static void get_midi_char(MidiStatus *st, int c);
  4260. /*static void queue_varlen(MidiStatus *st, int c);*/
  4261. static void special_event(MidiStatus *st, int c);
  4262. static void queue_read(MidiStatus *st, int c);
  4263. static void midi_note_on(MidiStatus *st);
  4264. static void midi_note_off(MidiStatus *st);
  4265. static void midi_key_pressure(MidiStatus *st);
  4266. static void midi_channel_pressure(MidiStatus *st);
  4267. static void midi_pitch_wheel(MidiStatus *st);
  4268. static void midi_program_change(MidiStatus *st);
  4269. static void midi_control_change(MidiStatus *st);
  4270. static void midi_select_bank(MidiStatus *st, int val);
  4271. static void midi_nrpn_event(MidiStatus *st);
  4272. static void midi_rpn_event(MidiStatus *st);
  4273. static void midi_detune(int chan, int coarse, int fine);
  4274. static void midi_system_exclusive(MidiStatus *st);
  4275. static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
  4276. static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val);
  4277. static int xg_control_change(MidiStatus *st, int cmd, int val);
  4278. #define numberof(ary) (sizeof(ary)/sizeof(ary[0]))
  4279. /*
  4280. * OSS Midi device record
  4281. */
  4282. static struct midi_operations awe_midi_operations =
  4283. {
  4284. .owner = THIS_MODULE,
  4285. .info = {"AWE Midi Emu", 0, 0, SNDCARD_SB},
  4286. .in_info = {0},
  4287. .open = awe_midi_open, /*open*/
  4288. .close = awe_midi_close, /*close*/
  4289. .ioctl = awe_midi_ioctl, /*ioctl*/
  4290. .outputc = awe_midi_outputc, /*outputc*/
  4291. };
  4292. static int my_mididev = -1;
  4293. static void __init attach_midiemu(void)
  4294. {
  4295. if ((my_mididev = sound_alloc_mididev()) < 0)
  4296. printk ("Sound: Too many midi devices detected\n");
  4297. else
  4298. midi_devs[my_mididev] = &awe_midi_operations;
  4299. }
  4300. static void unload_midiemu(void)
  4301. {
  4302. if (my_mididev >= 0)
  4303. sound_unload_mididev(my_mididev);
  4304. }
  4305. /*
  4306. * open/close midi device
  4307. */
  4308. static int midi_opened = FALSE;
  4309. static int midi_mode;
  4310. static int coarsetune, finetune;
  4311. static int xg_mapping = TRUE;
  4312. static int xg_bankmode;
  4313. /* effect sensitivity */
  4314. #define FX_CUTOFF 0
  4315. #define FX_RESONANCE 1
  4316. #define FX_ATTACK 2
  4317. #define FX_RELEASE 3
  4318. #define FX_VIBRATE 4
  4319. #define FX_VIBDEPTH 5
  4320. #define FX_VIBDELAY 6
  4321. #define FX_NUMS 7
  4322. #define DEF_FX_CUTOFF 170
  4323. #define DEF_FX_RESONANCE 6
  4324. #define DEF_FX_ATTACK 50
  4325. #define DEF_FX_RELEASE 50
  4326. #define DEF_FX_VIBRATE 30
  4327. #define DEF_FX_VIBDEPTH 4
  4328. #define DEF_FX_VIBDELAY 1500
  4329. /* effect sense: */
  4330. static int gs_sense[] =
  4331. {
  4332. DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
  4333. DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
  4334. };
  4335. static int xg_sense[] =
  4336. {
  4337. DEF_FX_CUTOFF, DEF_FX_RESONANCE, DEF_FX_ATTACK, DEF_FX_RELEASE,
  4338. DEF_FX_VIBRATE, DEF_FX_VIBDEPTH, DEF_FX_VIBDELAY
  4339. };
  4340. /* current status */
  4341. static MidiStatus curst;
  4342. static int
  4343. awe_midi_open (int dev, int mode,
  4344. void (*input)(int,unsigned char),
  4345. void (*output)(int))
  4346. {
  4347. if (midi_opened)
  4348. return -EBUSY;
  4349. midi_opened = TRUE;
  4350. midi_mode = MODE_GM;
  4351. curst.queue = Q_NONE;
  4352. curst.qlen = 0;
  4353. curst.read = 0;
  4354. curst.status = 0;
  4355. curst.chan = 0;
  4356. memset(curst.buf, 0, sizeof(curst.buf));
  4357. init_midi_status(&curst);
  4358. return 0;
  4359. }
  4360. static void
  4361. awe_midi_close (int dev)
  4362. {
  4363. midi_opened = FALSE;
  4364. }
  4365. static int
  4366. awe_midi_ioctl (int dev, unsigned cmd, void __user *arg)
  4367. {
  4368. return -EPERM;
  4369. }
  4370. static int
  4371. awe_midi_outputc (int dev, unsigned char midi_byte)
  4372. {
  4373. if (! midi_opened)
  4374. return 1;
  4375. /* force to change playing mode */
  4376. playing_mode = AWE_PLAY_MULTI;
  4377. get_midi_char(&curst, midi_byte);
  4378. return 1;
  4379. }
  4380. /*
  4381. * initialize
  4382. */
  4383. static void init_midi_status(MidiStatus *st)
  4384. {
  4385. clear_rpn();
  4386. coarsetune = 0;
  4387. finetune = 0;
  4388. }
  4389. /*
  4390. * RPN & NRPN
  4391. */
  4392. #define MAX_MIDI_CHANNELS 16
  4393. /* RPN & NRPN */
  4394. static unsigned char nrpn[MAX_MIDI_CHANNELS]; /* current event is NRPN? */
  4395. static int msb_bit; /* current event is msb for RPN/NRPN */
  4396. /* RPN & NRPN indeces */
  4397. static unsigned char rpn_msb[MAX_MIDI_CHANNELS], rpn_lsb[MAX_MIDI_CHANNELS];
  4398. /* RPN & NRPN values */
  4399. static int rpn_val[MAX_MIDI_CHANNELS];
  4400. static void clear_rpn(void)
  4401. {
  4402. int i;
  4403. for (i = 0; i < MAX_MIDI_CHANNELS; i++) {
  4404. nrpn[i] = 0;
  4405. rpn_msb[i] = 127;
  4406. rpn_lsb[i] = 127;
  4407. rpn_val[i] = 0;
  4408. }
  4409. msb_bit = 0;
  4410. }
  4411. /*
  4412. * process midi queue
  4413. */
  4414. /* status event types */
  4415. typedef void (*StatusEvent)(MidiStatus *st);
  4416. static struct StatusEventList {
  4417. StatusEvent process;
  4418. int qlen;
  4419. } status_event[8] = {
  4420. {midi_note_off, 2},
  4421. {midi_note_on, 2},
  4422. {midi_key_pressure, 2},
  4423. {midi_control_change, 2},
  4424. {midi_program_change, 1},
  4425. {midi_channel_pressure, 1},
  4426. {midi_pitch_wheel, 2},
  4427. {NULL, 0},
  4428. };
  4429. /* read a char from fifo and process it */
  4430. static void get_midi_char(MidiStatus *st, int c)
  4431. {
  4432. if (c == 0xfe) {
  4433. /* ignore active sense */
  4434. st->queue = Q_NONE;
  4435. return;
  4436. }
  4437. switch (st->queue) {
  4438. /* case Q_VARLEN: queue_varlen(st, c); break;*/
  4439. case Q_READ:
  4440. case Q_SYSEX:
  4441. queue_read(st, c);
  4442. break;
  4443. case Q_NONE:
  4444. st->read = 0;
  4445. if ((c & 0xf0) == 0xf0) {
  4446. special_event(st, c);
  4447. } else if (c & 0x80) { /* status change */
  4448. st->status = (c >> 4) & 0x07;
  4449. st->chan = c & 0x0f;
  4450. st->queue = Q_READ;
  4451. st->qlen = status_event[st->status].qlen;
  4452. if (st->qlen == 0)
  4453. st->queue = Q_NONE;
  4454. }
  4455. break;
  4456. }
  4457. }
  4458. /* 0xfx events */
  4459. static void special_event(MidiStatus *st, int c)
  4460. {
  4461. switch (c) {
  4462. case 0xf0: /* system exclusive */
  4463. st->queue = Q_SYSEX;
  4464. st->qlen = 0;
  4465. break;
  4466. case 0xf1: /* MTC quarter frame */
  4467. case 0xf3: /* song select */
  4468. st->queue = Q_READ;
  4469. st->qlen = 1;
  4470. break;
  4471. case 0xf2: /* song position */
  4472. st->queue = Q_READ;
  4473. st->qlen = 2;
  4474. break;
  4475. }
  4476. }
  4477. #if 0
  4478. /* read variable length value */
  4479. static void queue_varlen(MidiStatus *st, int c)
  4480. {
  4481. st->qlen += (c & 0x7f);
  4482. if (c & 0x80) {
  4483. st->qlen <<= 7;
  4484. return;
  4485. }
  4486. if (st->qlen <= 0) {
  4487. st->qlen = 0;
  4488. st->queue = Q_NONE;
  4489. }
  4490. st->queue = Q_READ;
  4491. st->read = 0;
  4492. }
  4493. #endif
  4494. /* read a char */
  4495. static void queue_read(MidiStatus *st, int c)
  4496. {
  4497. if (st->read < MAX_MIDIBUF) {
  4498. if (st->queue != Q_SYSEX)
  4499. c &= 0x7f;
  4500. st->buf[st->read] = (unsigned char)c;
  4501. }
  4502. st->read++;
  4503. if (st->queue == Q_SYSEX && c == 0xf7) {
  4504. midi_system_exclusive(st);
  4505. st->queue = Q_NONE;
  4506. } else if (st->queue == Q_READ && st->read >= st->qlen) {
  4507. if (status_event[st->status].process)
  4508. status_event[st->status].process(st);
  4509. st->queue = Q_NONE;
  4510. }
  4511. }
  4512. /*
  4513. * status events
  4514. */
  4515. /* note on */
  4516. static void midi_note_on(MidiStatus *st)
  4517. {
  4518. DEBUG(2,printk("midi: note_on (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
  4519. if (st->buf[1] == 0)
  4520. midi_note_off(st);
  4521. else
  4522. awe_start_note(0, st->chan, st->buf[0], st->buf[1]);
  4523. }
  4524. /* note off */
  4525. static void midi_note_off(MidiStatus *st)
  4526. {
  4527. DEBUG(2,printk("midi: note_off (%d) %d %d\n", st->chan, st->buf[0], st->buf[1]));
  4528. awe_kill_note(0, st->chan, st->buf[0], st->buf[1]);
  4529. }
  4530. /* key pressure change */
  4531. static void midi_key_pressure(MidiStatus *st)
  4532. {
  4533. awe_key_pressure(0, st->chan, st->buf[0], st->buf[1]);
  4534. }
  4535. /* channel pressure change */
  4536. static void midi_channel_pressure(MidiStatus *st)
  4537. {
  4538. channels[st->chan].chan_press = st->buf[0];
  4539. awe_modwheel_change(st->chan, st->buf[0]);
  4540. }
  4541. /* pitch wheel change */
  4542. static void midi_pitch_wheel(MidiStatus *st)
  4543. {
  4544. int val = (int)st->buf[1] * 128 + st->buf[0];
  4545. awe_bender(0, st->chan, val);
  4546. }
  4547. /* program change */
  4548. static void midi_program_change(MidiStatus *st)
  4549. {
  4550. int preset;
  4551. preset = st->buf[0];
  4552. if (midi_mode == MODE_GS && IS_DRUM_CHANNEL(st->chan) && preset == 127)
  4553. preset = 0;
  4554. else if (midi_mode == MODE_XG && xg_mapping && IS_DRUM_CHANNEL(st->chan))
  4555. preset += 64;
  4556. awe_set_instr(0, st->chan, preset);
  4557. }
  4558. #define send_effect(chan,type,val) awe_send_effect(chan,-1,type,val)
  4559. #define add_effect(chan,type,val) awe_send_effect(chan,-1,(type)|0x80,val)
  4560. #define unset_effect(chan,type) awe_send_effect(chan,-1,(type)|0x40,0)
  4561. /* midi control change */
  4562. static void midi_control_change(MidiStatus *st)
  4563. {
  4564. int cmd = st->buf[0];
  4565. int val = st->buf[1];
  4566. DEBUG(2,printk("midi: control (%d) %d %d\n", st->chan, cmd, val));
  4567. if (midi_mode == MODE_XG) {
  4568. if (xg_control_change(st, cmd, val))
  4569. return;
  4570. }
  4571. /* controls #31 - #64 are LSB of #0 - #31 */
  4572. msb_bit = 1;
  4573. if (cmd >= 0x20 && cmd < 0x40) {
  4574. msb_bit = 0;
  4575. cmd -= 0x20;
  4576. }
  4577. switch (cmd) {
  4578. case CTL_SOFT_PEDAL:
  4579. if (val == 127)
  4580. add_effect(st->chan, AWE_FX_CUTOFF, -160);
  4581. else
  4582. unset_effect(st->chan, AWE_FX_CUTOFF);
  4583. break;
  4584. case CTL_BANK_SELECT:
  4585. midi_select_bank(st, val);
  4586. break;
  4587. /* set RPN/NRPN parameter */
  4588. case CTL_REGIST_PARM_NUM_MSB:
  4589. nrpn[st->chan]=0; rpn_msb[st->chan]=val;
  4590. break;
  4591. case CTL_REGIST_PARM_NUM_LSB:
  4592. nrpn[st->chan]=0; rpn_lsb[st->chan]=val;
  4593. break;
  4594. case CTL_NONREG_PARM_NUM_MSB:
  4595. nrpn[st->chan]=1; rpn_msb[st->chan]=val;
  4596. break;
  4597. case CTL_NONREG_PARM_NUM_LSB:
  4598. nrpn[st->chan]=1; rpn_lsb[st->chan]=val;
  4599. break;
  4600. /* send RPN/NRPN entry */
  4601. case CTL_DATA_ENTRY:
  4602. if (msb_bit)
  4603. rpn_val[st->chan] = val * 128;
  4604. else
  4605. rpn_val[st->chan] |= val;
  4606. if (nrpn[st->chan])
  4607. midi_nrpn_event(st);
  4608. else
  4609. midi_rpn_event(st);
  4610. break;
  4611. /* increase/decrease data entry */
  4612. case CTL_DATA_INCREMENT:
  4613. rpn_val[st->chan]++;
  4614. midi_rpn_event(st);
  4615. break;
  4616. case CTL_DATA_DECREMENT:
  4617. rpn_val[st->chan]--;
  4618. midi_rpn_event(st);
  4619. break;
  4620. /* default */
  4621. default:
  4622. awe_controller(0, st->chan, cmd, val);
  4623. break;
  4624. }
  4625. }
  4626. /* tone bank change */
  4627. static void midi_select_bank(MidiStatus *st, int val)
  4628. {
  4629. if (midi_mode == MODE_XG && msb_bit) {
  4630. xg_bankmode = val;
  4631. /* XG MSB value; not normal bank selection */
  4632. switch (val) {
  4633. case 127: /* remap to drum channel */
  4634. awe_controller(0, st->chan, CTL_BANK_SELECT, 128);
  4635. break;
  4636. default: /* remap to normal channel */
  4637. awe_controller(0, st->chan, CTL_BANK_SELECT, val);
  4638. break;
  4639. }
  4640. return;
  4641. } else if (midi_mode == MODE_GS && !msb_bit)
  4642. /* ignore LSB bank in GS mode (used for mapping) */
  4643. return;
  4644. /* normal bank controls; accept both MSB and LSB */
  4645. if (! IS_DRUM_CHANNEL(st->chan)) {
  4646. if (midi_mode == MODE_XG) {
  4647. if (xg_bankmode) return;
  4648. if (val == 64 || val == 126)
  4649. val = 0;
  4650. } else if (midi_mode == MODE_GS && val == 127)
  4651. val = 0;
  4652. awe_controller(0, st->chan, CTL_BANK_SELECT, val);
  4653. }
  4654. }
  4655. /*
  4656. * RPN events
  4657. */
  4658. static void midi_rpn_event(MidiStatus *st)
  4659. {
  4660. int type;
  4661. type = (rpn_msb[st->chan]<<8) | rpn_lsb[st->chan];
  4662. switch (type) {
  4663. case 0x0000: /* Pitch bend sensitivity */
  4664. /* MSB only / 1 semitone per 128 */
  4665. if (msb_bit) {
  4666. channels[st->chan].bender_range =
  4667. rpn_val[st->chan] * 100 / 128;
  4668. }
  4669. break;
  4670. case 0x0001: /* fine tuning: */
  4671. /* MSB/LSB, 8192=center, 100/8192 cent step */
  4672. finetune = rpn_val[st->chan] - 8192;
  4673. midi_detune(st->chan, coarsetune, finetune);
  4674. break;
  4675. case 0x0002: /* coarse tuning */
  4676. /* MSB only / 8192=center, 1 semitone per 128 */
  4677. if (msb_bit) {
  4678. coarsetune = rpn_val[st->chan] - 8192;
  4679. midi_detune(st->chan, coarsetune, finetune);
  4680. }
  4681. break;
  4682. case 0x7F7F: /* "lock-in" RPN */
  4683. break;
  4684. }
  4685. }
  4686. /* tuning:
  4687. * coarse = -8192 to 8192 (100 cent per 128)
  4688. * fine = -8192 to 8192 (max=100cent)
  4689. */
  4690. static void midi_detune(int chan, int coarse, int fine)
  4691. {
  4692. /* 4096 = 1200 cents in AWE parameter */
  4693. int val;
  4694. val = coarse * 4096 / (12 * 128);
  4695. val += fine / 24;
  4696. if (val)
  4697. send_effect(chan, AWE_FX_INIT_PITCH, val);
  4698. else
  4699. unset_effect(chan, AWE_FX_INIT_PITCH);
  4700. }
  4701. /*
  4702. * system exclusive message
  4703. * GM/GS/XG macros are accepted
  4704. */
  4705. static void midi_system_exclusive(MidiStatus *st)
  4706. {
  4707. /* GM on */
  4708. static unsigned char gm_on_macro[] = {
  4709. 0x7e,0x7f,0x09,0x01,
  4710. };
  4711. /* XG on */
  4712. static unsigned char xg_on_macro[] = {
  4713. 0x43,0x10,0x4c,0x00,0x00,0x7e,0x00,
  4714. };
  4715. /* GS prefix
  4716. * drum channel: XX=0x1?(channel), YY=0x15, ZZ=on/off
  4717. * reverb mode: XX=0x01, YY=0x30, ZZ=0-7
  4718. * chorus mode: XX=0x01, YY=0x38, ZZ=0-7
  4719. */
  4720. static unsigned char gs_pfx_macro[] = {
  4721. 0x41,0x10,0x42,0x12,0x40,/*XX,YY,ZZ*/
  4722. };
  4723. #if 0
  4724. /* SC88 system mode set
  4725. * single module mode: XX=1
  4726. * double module mode: XX=0
  4727. */
  4728. static unsigned char gs_mode_macro[] = {
  4729. 0x41,0x10,0x42,0x12,0x00,0x00,0x7F,/*ZZ*/
  4730. };
  4731. /* SC88 display macro: XX=01:bitmap, 00:text
  4732. */
  4733. static unsigned char gs_disp_macro[] = {
  4734. 0x41,0x10,0x45,0x12,0x10,/*XX,00*/
  4735. };
  4736. #endif
  4737. /* GM on */
  4738. if (memcmp(st->buf, gm_on_macro, sizeof(gm_on_macro)) == 0) {
  4739. if (midi_mode != MODE_GS && midi_mode != MODE_XG)
  4740. midi_mode = MODE_GM;
  4741. init_midi_status(st);
  4742. }
  4743. /* GS macros */
  4744. else if (memcmp(st->buf, gs_pfx_macro, sizeof(gs_pfx_macro)) == 0) {
  4745. if (midi_mode != MODE_GS && midi_mode != MODE_XG)
  4746. midi_mode = MODE_GS;
  4747. if (st->buf[5] == 0x00 && st->buf[6] == 0x7f && st->buf[7] == 0x00) {
  4748. /* GS reset */
  4749. init_midi_status(st);
  4750. }
  4751. else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x15) {
  4752. /* drum pattern */
  4753. int p = st->buf[5] & 0x0f;
  4754. if (p == 0) p = 9;
  4755. else if (p < 10) p--;
  4756. if (st->buf[7] == 0)
  4757. DRUM_CHANNEL_OFF(p);
  4758. else
  4759. DRUM_CHANNEL_ON(p);
  4760. } else if ((st->buf[5] & 0xf0) == 0x10 && st->buf[6] == 0x21) {
  4761. /* program */
  4762. int p = st->buf[5] & 0x0f;
  4763. if (p == 0) p = 9;
  4764. else if (p < 10) p--;
  4765. if (! IS_DRUM_CHANNEL(p))
  4766. awe_set_instr(0, p, st->buf[7]);
  4767. } else if (st->buf[5] == 0x01 && st->buf[6] == 0x30) {
  4768. /* reverb mode */
  4769. awe_set_reverb_mode(st->buf[7]);
  4770. } else if (st->buf[5] == 0x01 && st->buf[6] == 0x38) {
  4771. /* chorus mode */
  4772. awe_set_chorus_mode(st->buf[7]);
  4773. } else if (st->buf[5] == 0x00 && st->buf[6] == 0x04) {
  4774. /* master volume */
  4775. awe_change_master_volume(st->buf[7]);
  4776. }
  4777. }
  4778. /* XG on */
  4779. else if (memcmp(st->buf, xg_on_macro, sizeof(xg_on_macro)) == 0) {
  4780. midi_mode = MODE_XG;
  4781. xg_mapping = TRUE;
  4782. xg_bankmode = 0;
  4783. }
  4784. }
  4785. /*----------------------------------------------------------------*/
  4786. /*
  4787. * convert NRPN/control values
  4788. */
  4789. static int send_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
  4790. {
  4791. int i, cval;
  4792. for (i = 0; i < num_tables; i++) {
  4793. if (table[i].control == type) {
  4794. cval = table[i].convert(val);
  4795. send_effect(st->chan, table[i].awe_effect, cval);
  4796. return TRUE;
  4797. }
  4798. }
  4799. return FALSE;
  4800. }
  4801. static int add_converted_effect(ConvTable *table, int num_tables, MidiStatus *st, int type, int val)
  4802. {
  4803. int i, cval;
  4804. for (i = 0; i < num_tables; i++) {
  4805. if (table[i].control == type) {
  4806. cval = table[i].convert(val);
  4807. add_effect(st->chan, table[i].awe_effect|0x80, cval);
  4808. return TRUE;
  4809. }
  4810. }
  4811. return FALSE;
  4812. }
  4813. /*
  4814. * AWE32 NRPN effects
  4815. */
  4816. static unsigned short fx_delay(int val);
  4817. static unsigned short fx_attack(int val);
  4818. static unsigned short fx_hold(int val);
  4819. static unsigned short fx_decay(int val);
  4820. static unsigned short fx_the_value(int val);
  4821. static unsigned short fx_twice_value(int val);
  4822. static unsigned short fx_conv_pitch(int val);
  4823. static unsigned short fx_conv_Q(int val);
  4824. /* function for each NRPN */ /* [range] units */
  4825. #define fx_env1_delay fx_delay /* [0,5900] 4msec */
  4826. #define fx_env1_attack fx_attack /* [0,5940] 1msec */
  4827. #define fx_env1_hold fx_hold /* [0,8191] 1msec */
  4828. #define fx_env1_decay fx_decay /* [0,5940] 4msec */
  4829. #define fx_env1_release fx_decay /* [0,5940] 4msec */
  4830. #define fx_env1_sustain fx_the_value /* [0,127] 0.75dB */
  4831. #define fx_env1_pitch fx_the_value /* [-127,127] 9.375cents */
  4832. #define fx_env1_cutoff fx_the_value /* [-127,127] 56.25cents */
  4833. #define fx_env2_delay fx_delay /* [0,5900] 4msec */
  4834. #define fx_env2_attack fx_attack /* [0,5940] 1msec */
  4835. #define fx_env2_hold fx_hold /* [0,8191] 1msec */
  4836. #define fx_env2_decay fx_decay /* [0,5940] 4msec */
  4837. #define fx_env2_release fx_decay /* [0,5940] 4msec */
  4838. #define fx_env2_sustain fx_the_value /* [0,127] 0.75dB */
  4839. #define fx_lfo1_delay fx_delay /* [0,5900] 4msec */
  4840. #define fx_lfo1_freq fx_twice_value /* [0,127] 84mHz */
  4841. #define fx_lfo1_volume fx_twice_value /* [0,127] 0.1875dB */
  4842. #define fx_lfo1_pitch fx_the_value /* [-127,127] 9.375cents */
  4843. #define fx_lfo1_cutoff fx_twice_value /* [-64,63] 56.25cents */
  4844. #define fx_lfo2_delay fx_delay /* [0,5900] 4msec */
  4845. #define fx_lfo2_freq fx_twice_value /* [0,127] 84mHz */
  4846. #define fx_lfo2_pitch fx_the_value /* [-127,127] 9.375cents */
  4847. #define fx_init_pitch fx_conv_pitch /* [-8192,8192] cents */
  4848. #define fx_chorus fx_the_value /* [0,255] -- */
  4849. #define fx_reverb fx_the_value /* [0,255] -- */
  4850. #define fx_cutoff fx_twice_value /* [0,127] 62Hz */
  4851. #define fx_filterQ fx_conv_Q /* [0,127] -- */
  4852. static unsigned short fx_delay(int val)
  4853. {
  4854. return (unsigned short)calc_parm_delay(val);
  4855. }
  4856. static unsigned short fx_attack(int val)
  4857. {
  4858. return (unsigned short)calc_parm_attack(val);
  4859. }
  4860. static unsigned short fx_hold(int val)
  4861. {
  4862. return (unsigned short)calc_parm_hold(val);
  4863. }
  4864. static unsigned short fx_decay(int val)
  4865. {
  4866. return (unsigned short)calc_parm_decay(val);
  4867. }
  4868. static unsigned short fx_the_value(int val)
  4869. {
  4870. return (unsigned short)(val & 0xff);
  4871. }
  4872. static unsigned short fx_twice_value(int val)
  4873. {
  4874. return (unsigned short)((val * 2) & 0xff);
  4875. }
  4876. static unsigned short fx_conv_pitch(int val)
  4877. {
  4878. return (short)(val * 4096 / 1200);
  4879. }
  4880. static unsigned short fx_conv_Q(int val)
  4881. {
  4882. return (unsigned short)((val / 8) & 0xff);
  4883. }
  4884. static ConvTable awe_effects[] =
  4885. {
  4886. { 0, AWE_FX_LFO1_DELAY, fx_lfo1_delay},
  4887. { 1, AWE_FX_LFO1_FREQ, fx_lfo1_freq},
  4888. { 2, AWE_FX_LFO2_DELAY, fx_lfo2_delay},
  4889. { 3, AWE_FX_LFO2_FREQ, fx_lfo2_freq},
  4890. { 4, AWE_FX_ENV1_DELAY, fx_env1_delay},
  4891. { 5, AWE_FX_ENV1_ATTACK,fx_env1_attack},
  4892. { 6, AWE_FX_ENV1_HOLD, fx_env1_hold},
  4893. { 7, AWE_FX_ENV1_DECAY, fx_env1_decay},
  4894. { 8, AWE_FX_ENV1_SUSTAIN, fx_env1_sustain},
  4895. { 9, AWE_FX_ENV1_RELEASE, fx_env1_release},
  4896. {10, AWE_FX_ENV2_DELAY, fx_env2_delay},
  4897. {11, AWE_FX_ENV2_ATTACK, fx_env2_attack},
  4898. {12, AWE_FX_ENV2_HOLD, fx_env2_hold},
  4899. {13, AWE_FX_ENV2_DECAY, fx_env2_decay},
  4900. {14, AWE_FX_ENV2_SUSTAIN, fx_env2_sustain},
  4901. {15, AWE_FX_ENV2_RELEASE, fx_env2_release},
  4902. {16, AWE_FX_INIT_PITCH, fx_init_pitch},
  4903. {17, AWE_FX_LFO1_PITCH, fx_lfo1_pitch},
  4904. {18, AWE_FX_LFO2_PITCH, fx_lfo2_pitch},
  4905. {19, AWE_FX_ENV1_PITCH, fx_env1_pitch},
  4906. {20, AWE_FX_LFO1_VOLUME, fx_lfo1_volume},
  4907. {21, AWE_FX_CUTOFF, fx_cutoff},
  4908. {22, AWE_FX_FILTERQ, fx_filterQ},
  4909. {23, AWE_FX_LFO1_CUTOFF, fx_lfo1_cutoff},
  4910. {24, AWE_FX_ENV1_CUTOFF, fx_env1_cutoff},
  4911. {25, AWE_FX_CHORUS, fx_chorus},
  4912. {26, AWE_FX_REVERB, fx_reverb},
  4913. };
  4914. static int num_awe_effects = numberof(awe_effects);
  4915. /*
  4916. * GS(SC88) NRPN effects; still experimental
  4917. */
  4918. /* cutoff: quarter semitone step, max=255 */
  4919. static unsigned short gs_cutoff(int val)
  4920. {
  4921. return (val - 64) * gs_sense[FX_CUTOFF] / 50;
  4922. }
  4923. /* resonance: 0 to 15(max) */
  4924. static unsigned short gs_filterQ(int val)
  4925. {
  4926. return (val - 64) * gs_sense[FX_RESONANCE] / 50;
  4927. }
  4928. /* attack: */
  4929. static unsigned short gs_attack(int val)
  4930. {
  4931. return -(val - 64) * gs_sense[FX_ATTACK] / 50;
  4932. }
  4933. /* decay: */
  4934. static unsigned short gs_decay(int val)
  4935. {
  4936. return -(val - 64) * gs_sense[FX_RELEASE] / 50;
  4937. }
  4938. /* release: */
  4939. static unsigned short gs_release(int val)
  4940. {
  4941. return -(val - 64) * gs_sense[FX_RELEASE] / 50;
  4942. }
  4943. /* vibrato freq: 0.042Hz step, max=255 */
  4944. static unsigned short gs_vib_rate(int val)
  4945. {
  4946. return (val - 64) * gs_sense[FX_VIBRATE] / 50;
  4947. }
  4948. /* vibrato depth: max=127, 1 octave */
  4949. static unsigned short gs_vib_depth(int val)
  4950. {
  4951. return (val - 64) * gs_sense[FX_VIBDEPTH] / 50;
  4952. }
  4953. /* vibrato delay: -0.725msec step */
  4954. static unsigned short gs_vib_delay(int val)
  4955. {
  4956. return -(val - 64) * gs_sense[FX_VIBDELAY] / 50;
  4957. }
  4958. static ConvTable gs_effects[] =
  4959. {
  4960. {32, AWE_FX_CUTOFF, gs_cutoff},
  4961. {33, AWE_FX_FILTERQ, gs_filterQ},
  4962. {99, AWE_FX_ENV2_ATTACK, gs_attack},
  4963. {100, AWE_FX_ENV2_DECAY, gs_decay},
  4964. {102, AWE_FX_ENV2_RELEASE, gs_release},
  4965. {8, AWE_FX_LFO1_FREQ, gs_vib_rate},
  4966. {9, AWE_FX_LFO1_VOLUME, gs_vib_depth},
  4967. {10, AWE_FX_LFO1_DELAY, gs_vib_delay},
  4968. };
  4969. static int num_gs_effects = numberof(gs_effects);
  4970. /*
  4971. * NRPN events: accept as AWE32/SC88 specific controls
  4972. */
  4973. static void midi_nrpn_event(MidiStatus *st)
  4974. {
  4975. if (rpn_msb[st->chan] == 127 && rpn_lsb[st->chan] <= 26) {
  4976. if (! msb_bit) /* both MSB/LSB necessary */
  4977. send_converted_effect(awe_effects, num_awe_effects,
  4978. st, rpn_lsb[st->chan],
  4979. rpn_val[st->chan] - 8192);
  4980. } else if (rpn_msb[st->chan] == 1) {
  4981. if (msb_bit) /* only MSB is valid */
  4982. add_converted_effect(gs_effects, num_gs_effects,
  4983. st, rpn_lsb[st->chan],
  4984. rpn_val[st->chan] / 128);
  4985. }
  4986. }
  4987. /*
  4988. * XG control effects; still experimental
  4989. */
  4990. /* cutoff: quarter semitone step, max=255 */
  4991. static unsigned short xg_cutoff(int val)
  4992. {
  4993. return (val - 64) * xg_sense[FX_CUTOFF] / 64;
  4994. }
  4995. /* resonance: 0(open) to 15(most nasal) */
  4996. static unsigned short xg_filterQ(int val)
  4997. {
  4998. return (val - 64) * xg_sense[FX_RESONANCE] / 64;
  4999. }
  5000. /* attack: */
  5001. static unsigned short xg_attack(int val)
  5002. {
  5003. return -(val - 64) * xg_sense[FX_ATTACK] / 64;
  5004. }
  5005. /* release: */
  5006. static unsigned short xg_release(int val)
  5007. {
  5008. return -(val - 64) * xg_sense[FX_RELEASE] / 64;
  5009. }
  5010. static ConvTable xg_effects[] =
  5011. {
  5012. {71, AWE_FX_CUTOFF, xg_cutoff},
  5013. {74, AWE_FX_FILTERQ, xg_filterQ},
  5014. {72, AWE_FX_ENV2_RELEASE, xg_release},
  5015. {73, AWE_FX_ENV2_ATTACK, xg_attack},
  5016. };
  5017. static int num_xg_effects = numberof(xg_effects);
  5018. static int xg_control_change(MidiStatus *st, int cmd, int val)
  5019. {
  5020. return add_converted_effect(xg_effects, num_xg_effects, st, cmd, val);
  5021. }
  5022. #endif /* CONFIG_AWE32_MIDIEMU */
  5023. /*----------------------------------------------------------------*/
  5024. /*
  5025. * initialization of AWE driver
  5026. */
  5027. static void
  5028. awe_initialize(void)
  5029. {
  5030. DEBUG(0,printk("AWE32: initializing..\n"));
  5031. /* initialize hardware configuration */
  5032. awe_poke(AWE_HWCF1, 0x0059);
  5033. awe_poke(AWE_HWCF2, 0x0020);
  5034. /* disable audio; this seems to reduce a clicking noise a bit.. */
  5035. awe_poke(AWE_HWCF3, 0);
  5036. /* initialize audio channels */
  5037. awe_init_audio();
  5038. /* initialize DMA */
  5039. awe_init_dma();
  5040. /* initialize init array */
  5041. awe_init_array();
  5042. /* check DRAM memory size */
  5043. awe_check_dram();
  5044. /* initialize the FM section of the AWE32 */
  5045. awe_init_fm();
  5046. /* set up voice envelopes */
  5047. awe_tweak();
  5048. /* enable audio */
  5049. awe_poke(AWE_HWCF3, 0x0004);
  5050. /* set default values */
  5051. awe_init_ctrl_parms(TRUE);
  5052. /* set equalizer */
  5053. awe_update_equalizer();
  5054. /* set reverb & chorus modes */
  5055. awe_update_reverb_mode();
  5056. awe_update_chorus_mode();
  5057. }
  5058. /*
  5059. * Core Device Management Functions
  5060. */
  5061. /* store values to i/o port array */
  5062. static void setup_ports(int port1, int port2, int port3)
  5063. {
  5064. awe_ports[0] = port1;
  5065. if (port2 == 0)
  5066. port2 = port1 + 0x400;
  5067. awe_ports[1] = port2;
  5068. awe_ports[2] = port2 + 2;
  5069. if (port3 == 0)
  5070. port3 = port1 + 0x800;
  5071. awe_ports[3] = port3;
  5072. awe_ports[4] = port3 + 2;
  5073. port_setuped = TRUE;
  5074. }
  5075. /*
  5076. * port request
  5077. * 0x620-623, 0xA20-A23, 0xE20-E23
  5078. */
  5079. static int
  5080. awe_request_region(void)
  5081. {
  5082. if (! port_setuped)
  5083. return 0;
  5084. if (! request_region(awe_ports[0], 4, "sound driver (AWE32)"))
  5085. return 0;
  5086. if (! request_region(awe_ports[1], 4, "sound driver (AWE32)"))
  5087. goto err_out;
  5088. if (! request_region(awe_ports[3], 4, "sound driver (AWE32)"))
  5089. goto err_out1;
  5090. return 1;
  5091. err_out1:
  5092. release_region(awe_ports[1], 4);
  5093. err_out:
  5094. release_region(awe_ports[0], 4);
  5095. return 0;
  5096. }
  5097. static void
  5098. awe_release_region(void)
  5099. {
  5100. if (! port_setuped) return;
  5101. release_region(awe_ports[0], 4);
  5102. release_region(awe_ports[1], 4);
  5103. release_region(awe_ports[3], 4);
  5104. }
  5105. static int awe_attach_device(void)
  5106. {
  5107. if (awe_present) return 0; /* for OSS38.. called twice? */
  5108. /* reserve I/O ports for awedrv */
  5109. if (! awe_request_region()) {
  5110. printk(KERN_ERR "AWE32: I/O area already used.\n");
  5111. return 0;
  5112. }
  5113. /* set buffers to NULL */
  5114. sfhead = sftail = NULL;
  5115. my_dev = sound_alloc_synthdev();
  5116. if (my_dev == -1) {
  5117. printk(KERN_ERR "AWE32 Error: too many synthesizers\n");
  5118. awe_release_region();
  5119. return 0;
  5120. }
  5121. voice_alloc = &awe_operations.alloc;
  5122. voice_alloc->max_voice = awe_max_voices;
  5123. synth_devs[my_dev] = &awe_operations;
  5124. #ifdef CONFIG_AWE32_MIXER
  5125. attach_mixer();
  5126. #endif
  5127. #ifdef CONFIG_AWE32_MIDIEMU
  5128. attach_midiemu();
  5129. #endif
  5130. /* clear all samples */
  5131. awe_reset_samples();
  5132. /* initialize AWE32 hardware */
  5133. awe_initialize();
  5134. sprintf(awe_info.name, "AWE32-%s (RAM%dk)",
  5135. AWEDRV_VERSION, memsize/1024);
  5136. printk(KERN_INFO "<SoundBlaster EMU8000 (RAM%dk)>\n", memsize/1024);
  5137. awe_present = TRUE;
  5138. return 1;
  5139. }
  5140. static void awe_dettach_device(void)
  5141. {
  5142. if (awe_present) {
  5143. awe_reset_samples();
  5144. awe_release_region();
  5145. free_tables();
  5146. #ifdef CONFIG_AWE32_MIXER
  5147. unload_mixer();
  5148. #endif
  5149. #ifdef CONFIG_AWE32_MIDIEMU
  5150. unload_midiemu();
  5151. #endif
  5152. sound_unload_synthdev(my_dev);
  5153. awe_present = FALSE;
  5154. }
  5155. }
  5156. /*
  5157. * Legacy device Probing
  5158. */
  5159. /* detect emu8000 chip on the specified address; from VV's guide */
  5160. static int __init
  5161. awe_detect_base(int addr)
  5162. {
  5163. setup_ports(addr, 0, 0);
  5164. if ((awe_peek(AWE_U1) & 0x000F) != 0x000C)
  5165. return 0;
  5166. if ((awe_peek(AWE_HWCF1) & 0x007E) != 0x0058)
  5167. return 0;
  5168. if ((awe_peek(AWE_HWCF2) & 0x0003) != 0x0003)
  5169. return 0;
  5170. DEBUG(0,printk("AWE32 found at %x\n", addr));
  5171. return 1;
  5172. }
  5173. static int __init awe_detect_legacy_devices(void)
  5174. {
  5175. int base;
  5176. for (base = 0x620; base <= 0x680; base += 0x20)
  5177. if (awe_detect_base(base)) {
  5178. awe_attach_device();
  5179. return 1;
  5180. }
  5181. DEBUG(0,printk("AWE32 Legacy detection failed\n"));
  5182. return 0;
  5183. }
  5184. /*
  5185. * PnP device Probing
  5186. */
  5187. static struct pnp_device_id awe_pnp_ids[] = {
  5188. {.id = "CTL0021", .driver_data = 0}, /* AWE32 WaveTable */
  5189. {.id = "CTL0022", .driver_data = 0}, /* AWE64 WaveTable */
  5190. {.id = "CTL0023", .driver_data = 0}, /* AWE64 Gold WaveTable */
  5191. { } /* terminator */
  5192. };
  5193. MODULE_DEVICE_TABLE(pnp, awe_pnp_ids);
  5194. static int awe_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  5195. {
  5196. int io1, io2, io3;
  5197. if (awe_present) {
  5198. printk(KERN_ERR "AWE32: This driver only supports one AWE32 device, skipping.\n");
  5199. }
  5200. if (!pnp_port_valid(dev,0) ||
  5201. !pnp_port_valid(dev,1) ||
  5202. !pnp_port_valid(dev,2)) {
  5203. printk(KERN_ERR "AWE32: The PnP device does not have the required resources.\n");
  5204. return -EINVAL;
  5205. }
  5206. io1 = pnp_port_start(dev,0);
  5207. io2 = pnp_port_start(dev,1);
  5208. io3 = pnp_port_start(dev,2);
  5209. printk(KERN_INFO "AWE32: A PnP Wave Table was detected at IO's %#x,%#x,%#x.\n",
  5210. io1, io2, io3);
  5211. setup_ports(io1, io2, io3);
  5212. awe_attach_device();
  5213. return 0;
  5214. }
  5215. static void awe_pnp_remove(struct pnp_dev *dev)
  5216. {
  5217. awe_dettach_device();
  5218. }
  5219. static struct pnp_driver awe_pnp_driver = {
  5220. .name = "AWE32",
  5221. .id_table = awe_pnp_ids,
  5222. .probe = awe_pnp_probe,
  5223. .remove = awe_pnp_remove,
  5224. };
  5225. static int __init awe_detect_pnp_devices(void)
  5226. {
  5227. int ret;
  5228. ret = pnp_register_driver(&awe_pnp_driver);
  5229. if (ret<0)
  5230. printk(KERN_ERR "AWE32: PnP support is unavailable.\n");
  5231. return ret;
  5232. }
  5233. /*
  5234. * device / lowlevel (module) interface
  5235. */
  5236. static int __init
  5237. awe_detect(void)
  5238. {
  5239. printk(KERN_INFO "AWE32: Probing for WaveTable...\n");
  5240. if (isapnp) {
  5241. if (awe_detect_pnp_devices()>=0)
  5242. return 1;
  5243. } else
  5244. printk(KERN_INFO "AWE32: Skipping PnP detection.\n");
  5245. if (awe_detect_legacy_devices())
  5246. return 1;
  5247. return 0;
  5248. }
  5249. static int __init attach_awe(void)
  5250. {
  5251. return awe_detect() ? 0 : -ENODEV;
  5252. }
  5253. static void __exit unload_awe(void)
  5254. {
  5255. pnp_unregister_driver(&awe_pnp_driver);
  5256. awe_dettach_device();
  5257. }
  5258. module_init(attach_awe);
  5259. module_exit(unload_awe);
  5260. #ifndef MODULE
  5261. static int __init setup_awe(char *str)
  5262. {
  5263. /* io, memsize, isapnp */
  5264. int ints[4];
  5265. str = get_options(str, ARRAY_SIZE(ints), ints);
  5266. io = ints[1];
  5267. memsize = ints[2];
  5268. isapnp = ints[3];
  5269. return 1;
  5270. }
  5271. __setup("awe=", setup_awe);
  5272. #endif