gus_wave.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464
  1. /*
  2. * sound/gus_wave.c
  3. *
  4. * Driver for the Gravis UltraSound wave table synth.
  5. *
  6. *
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. *
  13. *
  14. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. * Frank van de Pol : Fixed GUS MAX interrupt handling. Enabled simultanious
  16. * usage of CS4231A codec, GUS wave and MIDI for GUS MAX.
  17. * Bartlomiej Zolnierkiewicz : added some __init/__exit
  18. */
  19. #include <linux/init.h>
  20. #include <linux/config.h>
  21. #include <linux/spinlock.h>
  22. #define GUSPNP_AUTODETECT
  23. #include "sound_config.h"
  24. #include <linux/ultrasound.h>
  25. #include "gus.h"
  26. #include "gus_hw.h"
  27. #define GUS_BANK_SIZE (((iw_mode) ? 256*1024*1024 : 256*1024))
  28. #define MAX_SAMPLE 150
  29. #define MAX_PATCH 256
  30. #define NOT_SAMPLE 0xffff
  31. struct voice_info
  32. {
  33. unsigned long orig_freq;
  34. unsigned long current_freq;
  35. unsigned long mode;
  36. int fixed_pitch;
  37. int bender;
  38. int bender_range;
  39. int panning;
  40. int midi_volume;
  41. unsigned int initial_volume;
  42. unsigned int current_volume;
  43. int loop_irq_mode, loop_irq_parm;
  44. #define LMODE_FINISH 1
  45. #define LMODE_PCM 2
  46. #define LMODE_PCM_STOP 3
  47. int volume_irq_mode, volume_irq_parm;
  48. #define VMODE_HALT 1
  49. #define VMODE_ENVELOPE 2
  50. #define VMODE_START_NOTE 3
  51. int env_phase;
  52. unsigned char env_rate[6];
  53. unsigned char env_offset[6];
  54. /*
  55. * Volume computation parameters for gus_adagio_vol()
  56. */
  57. int main_vol, expression_vol, patch_vol;
  58. /* Variables for "Ultraclick" removal */
  59. int dev_pending, note_pending, volume_pending,
  60. sample_pending;
  61. char kill_pending;
  62. long offset_pending;
  63. };
  64. static struct voice_alloc_info *voice_alloc;
  65. static struct address_info *gus_hw_config;
  66. extern int gus_base;
  67. extern int gus_irq, gus_dma;
  68. extern int gus_pnp_flag;
  69. extern int gus_no_wave_dma;
  70. static int gus_dma2 = -1;
  71. static int dual_dma_mode;
  72. static long gus_mem_size;
  73. static long free_mem_ptr;
  74. static int gus_busy;
  75. static int gus_no_dma;
  76. static int nr_voices;
  77. static int gus_devnum;
  78. static int volume_base, volume_scale, volume_method;
  79. static int gus_recmask = SOUND_MASK_MIC;
  80. static int recording_active;
  81. static int only_read_access;
  82. static int only_8_bits;
  83. static int iw_mode = 0;
  84. int gus_wave_volume = 60;
  85. int gus_pcm_volume = 80;
  86. int have_gus_max = 0;
  87. static int gus_line_vol = 100, gus_mic_vol;
  88. static unsigned char mix_image = 0x00;
  89. int gus_timer_enabled = 0;
  90. /*
  91. * Current version of this driver doesn't allow synth and PCM functions
  92. * at the same time. The active_device specifies the active driver
  93. */
  94. static int active_device;
  95. #define GUS_DEV_WAVE 1 /* Wave table synth */
  96. #define GUS_DEV_PCM_DONE 2 /* PCM device, transfer done */
  97. #define GUS_DEV_PCM_CONTINUE 3 /* PCM device, transfer done ch. 1/2 */
  98. static int gus_audio_speed;
  99. static int gus_audio_channels;
  100. static int gus_audio_bits;
  101. static int gus_audio_bsize;
  102. static char bounce_buf[8 * 1024]; /* Must match value set to max_fragment */
  103. static DECLARE_WAIT_QUEUE_HEAD(dram_sleeper);
  104. /*
  105. * Variables and buffers for PCM output
  106. */
  107. #define MAX_PCM_BUFFERS (128*MAX_REALTIME_FACTOR) /* Don't change */
  108. static int pcm_bsize, pcm_nblk, pcm_banksize;
  109. static int pcm_datasize[MAX_PCM_BUFFERS];
  110. static volatile int pcm_head, pcm_tail, pcm_qlen;
  111. static volatile int pcm_active;
  112. static volatile int dma_active;
  113. static int pcm_opened;
  114. static int pcm_current_dev;
  115. static int pcm_current_block;
  116. static unsigned long pcm_current_buf;
  117. static int pcm_current_count;
  118. static int pcm_current_intrflag;
  119. DEFINE_SPINLOCK(gus_lock);
  120. extern int *gus_osp;
  121. static struct voice_info voices[32];
  122. static int freq_div_table[] =
  123. {
  124. 44100, /* 14 */
  125. 41160, /* 15 */
  126. 38587, /* 16 */
  127. 36317, /* 17 */
  128. 34300, /* 18 */
  129. 32494, /* 19 */
  130. 30870, /* 20 */
  131. 29400, /* 21 */
  132. 28063, /* 22 */
  133. 26843, /* 23 */
  134. 25725, /* 24 */
  135. 24696, /* 25 */
  136. 23746, /* 26 */
  137. 22866, /* 27 */
  138. 22050, /* 28 */
  139. 21289, /* 29 */
  140. 20580, /* 30 */
  141. 19916, /* 31 */
  142. 19293 /* 32 */
  143. };
  144. static struct patch_info *samples;
  145. static long sample_ptrs[MAX_SAMPLE + 1];
  146. static int sample_map[32];
  147. static int free_sample;
  148. static int mixer_type;
  149. static int patch_table[MAX_PATCH];
  150. static int patch_map[32];
  151. static struct synth_info gus_info = {
  152. "Gravis UltraSound", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_GUS,
  153. 0, 16, 0, MAX_PATCH
  154. };
  155. static void gus_poke(long addr, unsigned char data);
  156. static void compute_and_set_volume(int voice, int volume, int ramp_time);
  157. extern unsigned short gus_adagio_vol(int vel, int mainv, int xpn, int voicev);
  158. extern unsigned short gus_linear_vol(int vol, int mainvol);
  159. static void compute_volume(int voice, int volume);
  160. static void do_volume_irq(int voice);
  161. static void set_input_volumes(void);
  162. static void gus_tmr_install(int io_base);
  163. #define INSTANT_RAMP -1 /* Instant change. No ramping */
  164. #define FAST_RAMP 0 /* Fastest possible ramp */
  165. static void reset_sample_memory(void)
  166. {
  167. int i;
  168. for (i = 0; i <= MAX_SAMPLE; i++)
  169. sample_ptrs[i] = -1;
  170. for (i = 0; i < 32; i++)
  171. sample_map[i] = -1;
  172. for (i = 0; i < 32; i++)
  173. patch_map[i] = -1;
  174. gus_poke(0, 0); /* Put a silent sample to the beginning */
  175. gus_poke(1, 0);
  176. free_mem_ptr = 2;
  177. free_sample = 0;
  178. for (i = 0; i < MAX_PATCH; i++)
  179. patch_table[i] = NOT_SAMPLE;
  180. }
  181. void gus_delay(void)
  182. {
  183. int i;
  184. for (i = 0; i < 7; i++)
  185. inb(u_DRAMIO);
  186. }
  187. static void gus_poke(long addr, unsigned char data)
  188. { /* Writes a byte to the DRAM */
  189. outb((0x43), u_Command);
  190. outb((addr & 0xff), u_DataLo);
  191. outb(((addr >> 8) & 0xff), u_DataHi);
  192. outb((0x44), u_Command);
  193. outb(((addr >> 16) & 0xff), u_DataHi);
  194. outb((data), u_DRAMIO);
  195. }
  196. static unsigned char gus_peek(long addr)
  197. { /* Reads a byte from the DRAM */
  198. unsigned char tmp;
  199. outb((0x43), u_Command);
  200. outb((addr & 0xff), u_DataLo);
  201. outb(((addr >> 8) & 0xff), u_DataHi);
  202. outb((0x44), u_Command);
  203. outb(((addr >> 16) & 0xff), u_DataHi);
  204. tmp = inb(u_DRAMIO);
  205. return tmp;
  206. }
  207. void gus_write8(int reg, unsigned int data)
  208. { /* Writes to an indirect register (8 bit) */
  209. outb((reg), u_Command);
  210. outb(((unsigned char) (data & 0xff)), u_DataHi);
  211. }
  212. static unsigned char gus_read8(int reg)
  213. {
  214. /* Reads from an indirect register (8 bit). Offset 0x80. */
  215. unsigned char val;
  216. outb((reg | 0x80), u_Command);
  217. val = inb(u_DataHi);
  218. return val;
  219. }
  220. static unsigned char gus_look8(int reg)
  221. {
  222. /* Reads from an indirect register (8 bit). No additional offset. */
  223. unsigned char val;
  224. outb((reg), u_Command);
  225. val = inb(u_DataHi);
  226. return val;
  227. }
  228. static void gus_write16(int reg, unsigned int data)
  229. {
  230. /* Writes to an indirect register (16 bit) */
  231. outb((reg), u_Command);
  232. outb(((unsigned char) (data & 0xff)), u_DataLo);
  233. outb(((unsigned char) ((data >> 8) & 0xff)), u_DataHi);
  234. }
  235. static unsigned short gus_read16(int reg)
  236. {
  237. /* Reads from an indirect register (16 bit). Offset 0x80. */
  238. unsigned char hi, lo;
  239. outb((reg | 0x80), u_Command);
  240. lo = inb(u_DataLo);
  241. hi = inb(u_DataHi);
  242. return ((hi << 8) & 0xff00) | lo;
  243. }
  244. static unsigned short gus_look16(int reg)
  245. {
  246. /* Reads from an indirect register (16 bit). No additional offset. */
  247. unsigned char hi, lo;
  248. outb((reg), u_Command);
  249. lo = inb(u_DataLo);
  250. hi = inb(u_DataHi);
  251. return ((hi << 8) & 0xff00) | lo;
  252. }
  253. static void gus_write_addr(int reg, unsigned long address, int frac, int is16bit)
  254. {
  255. /* Writes an 24 bit memory address */
  256. unsigned long hold_address;
  257. if (is16bit)
  258. {
  259. if (iw_mode)
  260. {
  261. /* Interwave spesific address translations */
  262. address >>= 1;
  263. }
  264. else
  265. {
  266. /*
  267. * Special processing required for 16 bit patches
  268. */
  269. hold_address = address;
  270. address = address >> 1;
  271. address &= 0x0001ffffL;
  272. address |= (hold_address & 0x000c0000L);
  273. }
  274. }
  275. gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
  276. gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
  277. + (frac << 5));
  278. /* Could writing twice fix problems with GUS_VOICE_POS()? Let's try. */
  279. gus_delay();
  280. gus_write16(reg, (unsigned short) ((address >> 7) & 0xffff));
  281. gus_write16(reg + 1, (unsigned short) ((address << 9) & 0xffff)
  282. + (frac << 5));
  283. }
  284. static void gus_select_voice(int voice)
  285. {
  286. if (voice < 0 || voice > 31)
  287. return;
  288. outb((voice), u_Voice);
  289. }
  290. static void gus_select_max_voices(int nvoices)
  291. {
  292. if (iw_mode)
  293. nvoices = 32;
  294. if (nvoices < 14)
  295. nvoices = 14;
  296. if (nvoices > 32)
  297. nvoices = 32;
  298. voice_alloc->max_voice = nr_voices = nvoices;
  299. gus_write8(0x0e, (nvoices - 1) | 0xc0);
  300. }
  301. static void gus_voice_on(unsigned int mode)
  302. {
  303. gus_write8(0x00, (unsigned char) (mode & 0xfc));
  304. gus_delay();
  305. gus_write8(0x00, (unsigned char) (mode & 0xfc));
  306. }
  307. static void gus_voice_off(void)
  308. {
  309. gus_write8(0x00, gus_read8(0x00) | 0x03);
  310. }
  311. static void gus_voice_mode(unsigned int m)
  312. {
  313. unsigned char mode = (unsigned char) (m & 0xff);
  314. gus_write8(0x00, (gus_read8(0x00) & 0x03) |
  315. (mode & 0xfc)); /* Don't touch last two bits */
  316. gus_delay();
  317. gus_write8(0x00, (gus_read8(0x00) & 0x03) | (mode & 0xfc));
  318. }
  319. static void gus_voice_freq(unsigned long freq)
  320. {
  321. unsigned long divisor = freq_div_table[nr_voices - 14];
  322. unsigned short fc;
  323. /* Interwave plays at 44100 Hz with any number of voices */
  324. if (iw_mode)
  325. fc = (unsigned short) (((freq << 9) + (44100 >> 1)) / 44100);
  326. else
  327. fc = (unsigned short) (((freq << 9) + (divisor >> 1)) / divisor);
  328. fc = fc << 1;
  329. gus_write16(0x01, fc);
  330. }
  331. static void gus_voice_volume(unsigned int vol)
  332. {
  333. gus_write8(0x0d, 0x03); /* Stop ramp before setting volume */
  334. gus_write16(0x09, (unsigned short) (vol << 4));
  335. }
  336. static void gus_voice_balance(unsigned int balance)
  337. {
  338. gus_write8(0x0c, (unsigned char) (balance & 0xff));
  339. }
  340. static void gus_ramp_range(unsigned int low, unsigned int high)
  341. {
  342. gus_write8(0x07, (unsigned char) ((low >> 4) & 0xff));
  343. gus_write8(0x08, (unsigned char) ((high >> 4) & 0xff));
  344. }
  345. static void gus_ramp_rate(unsigned int scale, unsigned int rate)
  346. {
  347. gus_write8(0x06, (unsigned char) (((scale & 0x03) << 6) | (rate & 0x3f)));
  348. }
  349. static void gus_rampon(unsigned int m)
  350. {
  351. unsigned char mode = (unsigned char) (m & 0xff);
  352. gus_write8(0x0d, mode & 0xfc);
  353. gus_delay();
  354. gus_write8(0x0d, mode & 0xfc);
  355. }
  356. static void gus_ramp_mode(unsigned int m)
  357. {
  358. unsigned char mode = (unsigned char) (m & 0xff);
  359. gus_write8(0x0d, (gus_read8(0x0d) & 0x03) |
  360. (mode & 0xfc)); /* Leave the last 2 bits alone */
  361. gus_delay();
  362. gus_write8(0x0d, (gus_read8(0x0d) & 0x03) | (mode & 0xfc));
  363. }
  364. static void gus_rampoff(void)
  365. {
  366. gus_write8(0x0d, 0x03);
  367. }
  368. static void gus_set_voice_pos(int voice, long position)
  369. {
  370. int sample_no;
  371. if ((sample_no = sample_map[voice]) != -1) {
  372. if (position < samples[sample_no].len) {
  373. if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
  374. voices[voice].offset_pending = position;
  375. else
  376. gus_write_addr(0x0a, sample_ptrs[sample_no] + position, 0,
  377. samples[sample_no].mode & WAVE_16_BITS);
  378. }
  379. }
  380. }
  381. static void gus_voice_init(int voice)
  382. {
  383. unsigned long flags;
  384. spin_lock_irqsave(&gus_lock,flags);
  385. gus_select_voice(voice);
  386. gus_voice_volume(0);
  387. gus_voice_off();
  388. gus_write_addr(0x0a, 0, 0, 0); /* Set current position to 0 */
  389. gus_write8(0x00, 0x03); /* Voice off */
  390. gus_write8(0x0d, 0x03); /* Ramping off */
  391. voice_alloc->map[voice] = 0;
  392. voice_alloc->alloc_times[voice] = 0;
  393. spin_unlock_irqrestore(&gus_lock,flags);
  394. }
  395. static void gus_voice_init2(int voice)
  396. {
  397. voices[voice].panning = 0;
  398. voices[voice].mode = 0;
  399. voices[voice].orig_freq = 20000;
  400. voices[voice].current_freq = 20000;
  401. voices[voice].bender = 0;
  402. voices[voice].bender_range = 200;
  403. voices[voice].initial_volume = 0;
  404. voices[voice].current_volume = 0;
  405. voices[voice].loop_irq_mode = 0;
  406. voices[voice].loop_irq_parm = 0;
  407. voices[voice].volume_irq_mode = 0;
  408. voices[voice].volume_irq_parm = 0;
  409. voices[voice].env_phase = 0;
  410. voices[voice].main_vol = 127;
  411. voices[voice].patch_vol = 127;
  412. voices[voice].expression_vol = 127;
  413. voices[voice].sample_pending = -1;
  414. voices[voice].fixed_pitch = 0;
  415. }
  416. static void step_envelope(int voice)
  417. {
  418. unsigned vol, prev_vol, phase;
  419. unsigned char rate;
  420. unsigned long flags;
  421. if (voices[voice].mode & WAVE_SUSTAIN_ON && voices[voice].env_phase == 2)
  422. {
  423. spin_lock_irqsave(&gus_lock,flags);
  424. gus_select_voice(voice);
  425. gus_rampoff();
  426. spin_unlock_irqrestore(&gus_lock,flags);
  427. return;
  428. /*
  429. * Sustain phase begins. Continue envelope after receiving note off.
  430. */
  431. }
  432. if (voices[voice].env_phase >= 5)
  433. {
  434. /* Envelope finished. Shoot the voice down */
  435. gus_voice_init(voice);
  436. return;
  437. }
  438. prev_vol = voices[voice].current_volume;
  439. phase = ++voices[voice].env_phase;
  440. compute_volume(voice, voices[voice].midi_volume);
  441. vol = voices[voice].initial_volume * voices[voice].env_offset[phase] / 255;
  442. rate = voices[voice].env_rate[phase];
  443. spin_lock_irqsave(&gus_lock,flags);
  444. gus_select_voice(voice);
  445. gus_voice_volume(prev_vol);
  446. gus_write8(0x06, rate); /* Ramping rate */
  447. voices[voice].volume_irq_mode = VMODE_ENVELOPE;
  448. if (((vol - prev_vol) / 64) == 0) /* No significant volume change */
  449. {
  450. spin_unlock_irqrestore(&gus_lock,flags);
  451. step_envelope(voice); /* Continue the envelope on the next step */
  452. return;
  453. }
  454. if (vol > prev_vol)
  455. {
  456. if (vol >= (4096 - 64))
  457. vol = 4096 - 65;
  458. gus_ramp_range(0, vol);
  459. gus_rampon(0x20); /* Increasing volume, with IRQ */
  460. }
  461. else
  462. {
  463. if (vol <= 64)
  464. vol = 65;
  465. gus_ramp_range(vol, 4030);
  466. gus_rampon(0x60); /* Decreasing volume, with IRQ */
  467. }
  468. voices[voice].current_volume = vol;
  469. spin_unlock_irqrestore(&gus_lock,flags);
  470. }
  471. static void init_envelope(int voice)
  472. {
  473. voices[voice].env_phase = -1;
  474. voices[voice].current_volume = 64;
  475. step_envelope(voice);
  476. }
  477. static void start_release(int voice)
  478. {
  479. if (gus_read8(0x00) & 0x03)
  480. return; /* Voice already stopped */
  481. voices[voice].env_phase = 2; /* Will be incremented by step_envelope */
  482. voices[voice].current_volume = voices[voice].initial_volume =
  483. gus_read16(0x09) >> 4; /* Get current volume */
  484. voices[voice].mode &= ~WAVE_SUSTAIN_ON;
  485. gus_rampoff();
  486. step_envelope(voice);
  487. }
  488. static void gus_voice_fade(int voice)
  489. {
  490. int instr_no = sample_map[voice], is16bits;
  491. unsigned long flags;
  492. spin_lock_irqsave(&gus_lock,flags);
  493. gus_select_voice(voice);
  494. if (instr_no < 0 || instr_no > MAX_SAMPLE)
  495. {
  496. gus_write8(0x00, 0x03); /* Hard stop */
  497. voice_alloc->map[voice] = 0;
  498. spin_unlock_irqrestore(&gus_lock,flags);
  499. return;
  500. }
  501. is16bits = (samples[instr_no].mode & WAVE_16_BITS) ? 1 : 0; /* 8 or 16 bits */
  502. if (voices[voice].mode & WAVE_ENVELOPES)
  503. {
  504. start_release(voice);
  505. spin_unlock_irqrestore(&gus_lock,flags);
  506. return;
  507. }
  508. /*
  509. * Ramp the volume down but not too quickly.
  510. */
  511. if ((int) (gus_read16(0x09) >> 4) < 100) /* Get current volume */
  512. {
  513. gus_voice_off();
  514. gus_rampoff();
  515. gus_voice_init(voice);
  516. spin_unlock_irqrestore(&gus_lock,flags);
  517. return;
  518. }
  519. gus_ramp_range(65, 4030);
  520. gus_ramp_rate(2, 4);
  521. gus_rampon(0x40 | 0x20); /* Down, once, with IRQ */
  522. voices[voice].volume_irq_mode = VMODE_HALT;
  523. spin_unlock_irqrestore(&gus_lock,flags);
  524. }
  525. static void gus_reset(void)
  526. {
  527. int i;
  528. gus_select_max_voices(24);
  529. volume_base = 3071;
  530. volume_scale = 4;
  531. volume_method = VOL_METHOD_ADAGIO;
  532. for (i = 0; i < 32; i++)
  533. {
  534. gus_voice_init(i); /* Turn voice off */
  535. gus_voice_init2(i);
  536. }
  537. }
  538. static void gus_initialize(void)
  539. {
  540. unsigned long flags;
  541. unsigned char dma_image, irq_image, tmp;
  542. static unsigned char gus_irq_map[16] = {
  543. 0, 0, 0, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7
  544. };
  545. static unsigned char gus_dma_map[8] = {
  546. 0, 1, 0, 2, 0, 3, 4, 5
  547. };
  548. spin_lock_irqsave(&gus_lock,flags);
  549. gus_write8(0x4c, 0); /* Reset GF1 */
  550. gus_delay();
  551. gus_delay();
  552. gus_write8(0x4c, 1); /* Release Reset */
  553. gus_delay();
  554. gus_delay();
  555. /*
  556. * Clear all interrupts
  557. */
  558. gus_write8(0x41, 0); /* DMA control */
  559. gus_write8(0x45, 0); /* Timer control */
  560. gus_write8(0x49, 0); /* Sample control */
  561. gus_select_max_voices(24);
  562. inb(u_Status); /* Touch the status register */
  563. gus_look8(0x41); /* Clear any pending DMA IRQs */
  564. gus_look8(0x49); /* Clear any pending sample IRQs */
  565. gus_read8(0x0f); /* Clear pending IRQs */
  566. gus_reset(); /* Resets all voices */
  567. gus_look8(0x41); /* Clear any pending DMA IRQs */
  568. gus_look8(0x49); /* Clear any pending sample IRQs */
  569. gus_read8(0x0f); /* Clear pending IRQs */
  570. gus_write8(0x4c, 7); /* Master reset | DAC enable | IRQ enable */
  571. /*
  572. * Set up for Digital ASIC
  573. */
  574. outb((0x05), gus_base + 0x0f);
  575. mix_image |= 0x02; /* Disable line out (for a moment) */
  576. outb((mix_image), u_Mixer);
  577. outb((0x00), u_IRQDMAControl);
  578. outb((0x00), gus_base + 0x0f);
  579. /*
  580. * Now set up the DMA and IRQ interface
  581. *
  582. * The GUS supports two IRQs and two DMAs.
  583. *
  584. * Just one DMA channel is used. This prevents simultaneous ADC and DAC.
  585. * Adding this support requires significant changes to the dmabuf.c, dsp.c
  586. * and audio.c also.
  587. */
  588. irq_image = 0;
  589. tmp = gus_irq_map[gus_irq];
  590. if (!gus_pnp_flag && !tmp)
  591. printk(KERN_WARNING "Warning! GUS IRQ not selected\n");
  592. irq_image |= tmp;
  593. irq_image |= 0x40; /* Combine IRQ1 (GF1) and IRQ2 (Midi) */
  594. dual_dma_mode = 1;
  595. if (gus_dma2 == gus_dma || gus_dma2 == -1)
  596. {
  597. dual_dma_mode = 0;
  598. dma_image = 0x40; /* Combine DMA1 (DRAM) and IRQ2 (ADC) */
  599. tmp = gus_dma_map[gus_dma];
  600. if (!tmp)
  601. printk(KERN_WARNING "Warning! GUS DMA not selected\n");
  602. dma_image |= tmp;
  603. }
  604. else
  605. {
  606. /* Setup dual DMA channel mode for GUS MAX */
  607. dma_image = gus_dma_map[gus_dma];
  608. if (!dma_image)
  609. printk(KERN_WARNING "Warning! GUS DMA not selected\n");
  610. tmp = gus_dma_map[gus_dma2] << 3;
  611. if (!tmp)
  612. {
  613. printk(KERN_WARNING "Warning! Invalid GUS MAX DMA\n");
  614. tmp = 0x40; /* Combine DMA channels */
  615. dual_dma_mode = 0;
  616. }
  617. dma_image |= tmp;
  618. }
  619. /*
  620. * For some reason the IRQ and DMA addresses must be written twice
  621. */
  622. /*
  623. * Doing it first time
  624. */
  625. outb((mix_image), u_Mixer); /* Select DMA control */
  626. outb((dma_image | 0x80), u_IRQDMAControl); /* Set DMA address */
  627. outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
  628. outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
  629. /*
  630. * Doing it second time
  631. */
  632. outb((mix_image), u_Mixer); /* Select DMA control */
  633. outb((dma_image), u_IRQDMAControl); /* Set DMA address */
  634. outb((mix_image | 0x40), u_Mixer); /* Select IRQ control */
  635. outb((irq_image), u_IRQDMAControl); /* Set IRQ address */
  636. gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
  637. mix_image &= ~0x02; /* Enable line out */
  638. mix_image |= 0x08; /* Enable IRQ */
  639. outb((mix_image), u_Mixer); /*
  640. * Turn mixer channels on
  641. * Note! Mic in is left off.
  642. */
  643. gus_select_voice(0); /* This disables writes to IRQ/DMA reg */
  644. gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
  645. inb(u_Status); /* Touch the status register */
  646. gus_look8(0x41); /* Clear any pending DMA IRQs */
  647. gus_look8(0x49); /* Clear any pending sample IRQs */
  648. gus_read8(0x0f); /* Clear pending IRQs */
  649. if (iw_mode)
  650. gus_write8(0x19, gus_read8(0x19) | 0x01);
  651. spin_unlock_irqrestore(&gus_lock,flags);
  652. }
  653. static void __init pnp_mem_init(void)
  654. {
  655. #include "iwmem.h"
  656. #define CHUNK_SIZE (256*1024)
  657. #define BANK_SIZE (4*1024*1024)
  658. #define CHUNKS_PER_BANK (BANK_SIZE/CHUNK_SIZE)
  659. int bank, chunk, addr, total = 0;
  660. int bank_sizes[4];
  661. int i, j, bits = -1, testbits = -1, nbanks = 0;
  662. /*
  663. * This routine determines what kind of RAM is installed in each of the four
  664. * SIMM banks and configures the DRAM address decode logic accordingly.
  665. */
  666. /*
  667. * Place the chip into enhanced mode
  668. */
  669. gus_write8(0x19, gus_read8(0x19) | 0x01);
  670. gus_write8(0x53, gus_look8(0x53) & ~0x02); /* Select DRAM I/O access */
  671. /*
  672. * Set memory configuration to 4 DRAM banks of 4M in each (16M total).
  673. */
  674. gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | 0x000c);
  675. /*
  676. * Perform the DRAM size detection for each bank individually.
  677. */
  678. for (bank = 0; bank < 4; bank++)
  679. {
  680. int size = 0;
  681. addr = bank * BANK_SIZE;
  682. /* Clean check points of each chunk */
  683. for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
  684. {
  685. gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
  686. gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
  687. }
  688. /* Write a value to each chunk point and verify the result */
  689. for (chunk = 0; chunk < CHUNKS_PER_BANK; chunk++)
  690. {
  691. gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x55);
  692. gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0xAA);
  693. if (gus_peek(addr + chunk * CHUNK_SIZE + 0L) == 0x55 &&
  694. gus_peek(addr + chunk * CHUNK_SIZE + 1L) == 0xAA)
  695. {
  696. /* OK. There is RAM. Now check for possible shadows */
  697. int ok = 1, chunk2;
  698. for (chunk2 = 0; ok && chunk2 < chunk; chunk2++)
  699. if (gus_peek(addr + chunk2 * CHUNK_SIZE + 0L) ||
  700. gus_peek(addr + chunk2 * CHUNK_SIZE + 1L))
  701. ok = 0; /* Addressing wraps */
  702. if (ok)
  703. size = (chunk + 1) * CHUNK_SIZE;
  704. }
  705. gus_poke(addr + chunk * CHUNK_SIZE + 0L, 0x00);
  706. gus_poke(addr + chunk * CHUNK_SIZE + 1L, 0x00);
  707. }
  708. bank_sizes[bank] = size;
  709. if (size)
  710. nbanks = bank + 1;
  711. DDB(printk("Interwave: Bank %d, size=%dk\n", bank, size / 1024));
  712. }
  713. if (nbanks == 0) /* No RAM - Give up */
  714. {
  715. printk(KERN_ERR "Sound: An Interwave audio chip detected but no DRAM\n");
  716. printk(KERN_ERR "Sound: Unable to work with this card.\n");
  717. gus_write8(0x19, gus_read8(0x19) & ~0x01);
  718. gus_mem_size = 0;
  719. return;
  720. }
  721. /*
  722. * Now we know how much DRAM there is in each bank. The next step is
  723. * to find a DRAM size encoding (0 to 12) which is best for the combination
  724. * we have.
  725. *
  726. * First try if any of the possible alternatives matches exactly the amount
  727. * of memory we have.
  728. */
  729. for (i = 0; bits == -1 && i < 13; i++)
  730. {
  731. bits = i;
  732. for (j = 0; bits != -1 && j < 4; j++)
  733. if (mem_decode[i][j] != bank_sizes[j])
  734. bits = -1; /* No hit */
  735. }
  736. /*
  737. * If necessary, try to find a combination where other than the last
  738. * bank matches our configuration and the last bank is left oversized.
  739. * In this way we don't leave holes in the middle of memory.
  740. */
  741. if (bits == -1) /* No luck yet */
  742. {
  743. for (i = 0; bits == -1 && i < 13; i++)
  744. {
  745. bits = i;
  746. for (j = 0; bits != -1 && j < nbanks - 1; j++)
  747. if (mem_decode[i][j] != bank_sizes[j])
  748. bits = -1; /* No hit */
  749. if (mem_decode[i][nbanks - 1] < bank_sizes[nbanks - 1])
  750. bits = -1; /* The last bank is too small */
  751. }
  752. }
  753. /*
  754. * The last resort is to search for a combination where the banks are
  755. * smaller than the actual SIMMs. This leaves some memory in the banks
  756. * unused but doesn't leave holes in the DRAM address space.
  757. */
  758. if (bits == -1) /* No luck yet */
  759. {
  760. for (i = 0; i < 13; i++)
  761. {
  762. testbits = i;
  763. for (j = 0; testbits != -1 && j < nbanks - 1; j++)
  764. if (mem_decode[i][j] > bank_sizes[j]) {
  765. testbits = -1;
  766. }
  767. if(testbits > bits) bits = testbits;
  768. }
  769. if (bits != -1)
  770. {
  771. printk(KERN_INFO "Interwave: Can't use all installed RAM.\n");
  772. printk(KERN_INFO "Interwave: Try reordering SIMMS.\n");
  773. }
  774. printk(KERN_INFO "Interwave: Can't find working DRAM encoding.\n");
  775. printk(KERN_INFO "Interwave: Defaulting to 256k. Try reordering SIMMS.\n");
  776. bits = 0;
  777. }
  778. DDB(printk("Interwave: Selecting DRAM addressing mode %d\n", bits));
  779. for (bank = 0; bank < 4; bank++)
  780. {
  781. DDB(printk(" Bank %d, mem=%dk (limit %dk)\n", bank, bank_sizes[bank] / 1024, mem_decode[bits][bank] / 1024));
  782. if (bank_sizes[bank] > mem_decode[bits][bank])
  783. total += mem_decode[bits][bank];
  784. else
  785. total += bank_sizes[bank];
  786. }
  787. DDB(printk("Total %dk of DRAM (enhanced mode)\n", total / 1024));
  788. /*
  789. * Set the memory addressing mode.
  790. */
  791. gus_write16(0x52, (gus_look16(0x52) & 0xfff0) | bits);
  792. /* Leave the chip into enhanced mode. Disable LFO */
  793. gus_mem_size = total;
  794. iw_mode = 1;
  795. gus_write8(0x19, (gus_read8(0x19) | 0x01) & ~0x02);
  796. }
  797. int __init gus_wave_detect(int baseaddr)
  798. {
  799. unsigned long i, max_mem = 1024L;
  800. unsigned long loc;
  801. unsigned char val;
  802. if (!request_region(baseaddr, 16, "GUS"))
  803. return 0;
  804. if (!request_region(baseaddr + 0x100, 12, "GUS")) { /* 0x10c-> is MAX */
  805. release_region(baseaddr, 16);
  806. return 0;
  807. }
  808. gus_base = baseaddr;
  809. gus_write8(0x4c, 0); /* Reset GF1 */
  810. gus_delay();
  811. gus_delay();
  812. gus_write8(0x4c, 1); /* Release Reset */
  813. gus_delay();
  814. gus_delay();
  815. #ifdef GUSPNP_AUTODETECT
  816. val = gus_look8(0x5b); /* Version number register */
  817. gus_write8(0x5b, ~val); /* Invert all bits */
  818. if ((gus_look8(0x5b) & 0xf0) == (val & 0xf0)) /* No change */
  819. {
  820. if ((gus_look8(0x5b) & 0x0f) == ((~val) & 0x0f)) /* Change */
  821. {
  822. DDB(printk("Interwave chip version %d detected\n", (val & 0xf0) >> 4));
  823. gus_pnp_flag = 1;
  824. }
  825. else
  826. {
  827. DDB(printk("Not an Interwave chip (%x)\n", gus_look8(0x5b)));
  828. gus_pnp_flag = 0;
  829. }
  830. }
  831. gus_write8(0x5b, val); /* Restore all bits */
  832. #endif
  833. if (gus_pnp_flag)
  834. pnp_mem_init();
  835. if (iw_mode)
  836. return 1;
  837. /* See if there is first block there.... */
  838. gus_poke(0L, 0xaa);
  839. if (gus_peek(0L) != 0xaa) {
  840. release_region(baseaddr + 0x100, 12);
  841. release_region(baseaddr, 16);
  842. return 0;
  843. }
  844. /* Now zero it out so that I can check for mirroring .. */
  845. gus_poke(0L, 0x00);
  846. for (i = 1L; i < max_mem; i++)
  847. {
  848. int n, failed;
  849. /* check for mirroring ... */
  850. if (gus_peek(0L) != 0)
  851. break;
  852. loc = i << 10;
  853. for (n = loc - 1, failed = 0; n <= loc; n++)
  854. {
  855. gus_poke(loc, 0xaa);
  856. if (gus_peek(loc) != 0xaa)
  857. failed = 1;
  858. gus_poke(loc, 0x55);
  859. if (gus_peek(loc) != 0x55)
  860. failed = 1;
  861. }
  862. if (failed)
  863. break;
  864. }
  865. gus_mem_size = i << 10;
  866. return 1;
  867. }
  868. static int guswave_ioctl(int dev, unsigned int cmd, void __user *arg)
  869. {
  870. switch (cmd)
  871. {
  872. case SNDCTL_SYNTH_INFO:
  873. gus_info.nr_voices = nr_voices;
  874. if (copy_to_user(arg, &gus_info, sizeof(gus_info)))
  875. return -EFAULT;
  876. return 0;
  877. case SNDCTL_SEQ_RESETSAMPLES:
  878. reset_sample_memory();
  879. return 0;
  880. case SNDCTL_SEQ_PERCMODE:
  881. return 0;
  882. case SNDCTL_SYNTH_MEMAVL:
  883. return (gus_mem_size == 0) ? 0 : gus_mem_size - free_mem_ptr - 32;
  884. default:
  885. return -EINVAL;
  886. }
  887. }
  888. static int guswave_set_instr(int dev, int voice, int instr_no)
  889. {
  890. int sample_no;
  891. if (instr_no < 0 || instr_no > MAX_PATCH)
  892. instr_no = 0; /* Default to acoustic piano */
  893. if (voice < 0 || voice > 31)
  894. return -EINVAL;
  895. if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
  896. {
  897. voices[voice].sample_pending = instr_no;
  898. return 0;
  899. }
  900. sample_no = patch_table[instr_no];
  901. patch_map[voice] = -1;
  902. if (sample_no == NOT_SAMPLE)
  903. {
  904. /* printk("GUS: Undefined patch %d for voice %d\n", instr_no, voice);*/
  905. return -EINVAL; /* Patch not defined */
  906. }
  907. if (sample_ptrs[sample_no] == -1) /* Sample not loaded */
  908. {
  909. /* printk("GUS: Sample #%d not loaded for patch %d (voice %d)\n", sample_no, instr_no, voice);*/
  910. return -EINVAL;
  911. }
  912. sample_map[voice] = sample_no;
  913. patch_map[voice] = instr_no;
  914. return 0;
  915. }
  916. static int guswave_kill_note(int dev, int voice, int note, int velocity)
  917. {
  918. unsigned long flags;
  919. spin_lock_irqsave(&gus_lock,flags);
  920. /* voice_alloc->map[voice] = 0xffff; */
  921. if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
  922. {
  923. voices[voice].kill_pending = 1;
  924. spin_unlock_irqrestore(&gus_lock,flags);
  925. }
  926. else
  927. {
  928. spin_unlock_irqrestore(&gus_lock,flags);
  929. gus_voice_fade(voice);
  930. }
  931. return 0;
  932. }
  933. static void guswave_aftertouch(int dev, int voice, int pressure)
  934. {
  935. }
  936. static void guswave_panning(int dev, int voice, int value)
  937. {
  938. if (voice >= 0 || voice < 32)
  939. voices[voice].panning = value;
  940. }
  941. static void guswave_volume_method(int dev, int mode)
  942. {
  943. if (mode == VOL_METHOD_LINEAR || mode == VOL_METHOD_ADAGIO)
  944. volume_method = mode;
  945. }
  946. static void compute_volume(int voice, int volume)
  947. {
  948. if (volume < 128)
  949. voices[voice].midi_volume = volume;
  950. switch (volume_method)
  951. {
  952. case VOL_METHOD_ADAGIO:
  953. voices[voice].initial_volume =
  954. gus_adagio_vol(voices[voice].midi_volume, voices[voice].main_vol,
  955. voices[voice].expression_vol,
  956. voices[voice].patch_vol);
  957. break;
  958. case VOL_METHOD_LINEAR: /* Totally ignores patch-volume and expression */
  959. voices[voice].initial_volume = gus_linear_vol(volume, voices[voice].main_vol);
  960. break;
  961. default:
  962. voices[voice].initial_volume = volume_base +
  963. (voices[voice].midi_volume * volume_scale);
  964. }
  965. if (voices[voice].initial_volume > 4030)
  966. voices[voice].initial_volume = 4030;
  967. }
  968. static void compute_and_set_volume(int voice, int volume, int ramp_time)
  969. {
  970. int curr, target, rate;
  971. unsigned long flags;
  972. compute_volume(voice, volume);
  973. voices[voice].current_volume = voices[voice].initial_volume;
  974. spin_lock_irqsave(&gus_lock,flags);
  975. /*
  976. * CAUTION! Interrupts disabled. Enable them before returning
  977. */
  978. gus_select_voice(voice);
  979. curr = gus_read16(0x09) >> 4;
  980. target = voices[voice].initial_volume;
  981. if (ramp_time == INSTANT_RAMP)
  982. {
  983. gus_rampoff();
  984. gus_voice_volume(target);
  985. spin_unlock_irqrestore(&gus_lock,flags);
  986. return;
  987. }
  988. if (ramp_time == FAST_RAMP)
  989. rate = 63;
  990. else
  991. rate = 16;
  992. gus_ramp_rate(0, rate);
  993. if ((target - curr) / 64 == 0) /* Close enough to target. */
  994. {
  995. gus_rampoff();
  996. gus_voice_volume(target);
  997. spin_unlock_irqrestore(&gus_lock,flags);
  998. return;
  999. }
  1000. if (target > curr)
  1001. {
  1002. if (target > (4095 - 65))
  1003. target = 4095 - 65;
  1004. gus_ramp_range(curr, target);
  1005. gus_rampon(0x00); /* Ramp up, once, no IRQ */
  1006. }
  1007. else
  1008. {
  1009. if (target < 65)
  1010. target = 65;
  1011. gus_ramp_range(target, curr);
  1012. gus_rampon(0x40); /* Ramp down, once, no irq */
  1013. }
  1014. spin_unlock_irqrestore(&gus_lock,flags);
  1015. }
  1016. static void dynamic_volume_change(int voice)
  1017. {
  1018. unsigned char status;
  1019. unsigned long flags;
  1020. spin_lock_irqsave(&gus_lock,flags);
  1021. gus_select_voice(voice);
  1022. status = gus_read8(0x00); /* Get voice status */
  1023. spin_unlock_irqrestore(&gus_lock,flags);
  1024. if (status & 0x03)
  1025. return; /* Voice was not running */
  1026. if (!(voices[voice].mode & WAVE_ENVELOPES))
  1027. {
  1028. compute_and_set_volume(voice, voices[voice].midi_volume, 1);
  1029. return;
  1030. }
  1031. /*
  1032. * Voice is running and has envelopes.
  1033. */
  1034. spin_lock_irqsave(&gus_lock,flags);
  1035. gus_select_voice(voice);
  1036. status = gus_read8(0x0d); /* Ramping status */
  1037. spin_unlock_irqrestore(&gus_lock,flags);
  1038. if (status & 0x03) /* Sustain phase? */
  1039. {
  1040. compute_and_set_volume(voice, voices[voice].midi_volume, 1);
  1041. return;
  1042. }
  1043. if (voices[voice].env_phase < 0)
  1044. return;
  1045. compute_volume(voice, voices[voice].midi_volume);
  1046. }
  1047. static void guswave_controller(int dev, int voice, int ctrl_num, int value)
  1048. {
  1049. unsigned long flags;
  1050. unsigned long freq;
  1051. if (voice < 0 || voice > 31)
  1052. return;
  1053. switch (ctrl_num)
  1054. {
  1055. case CTRL_PITCH_BENDER:
  1056. voices[voice].bender = value;
  1057. if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
  1058. {
  1059. freq = compute_finetune(voices[voice].orig_freq, value, voices[voice].bender_range, 0);
  1060. voices[voice].current_freq = freq;
  1061. spin_lock_irqsave(&gus_lock,flags);
  1062. gus_select_voice(voice);
  1063. gus_voice_freq(freq);
  1064. spin_unlock_irqrestore(&gus_lock,flags);
  1065. }
  1066. break;
  1067. case CTRL_PITCH_BENDER_RANGE:
  1068. voices[voice].bender_range = value;
  1069. break;
  1070. case CTL_EXPRESSION:
  1071. value /= 128;
  1072. case CTRL_EXPRESSION:
  1073. if (volume_method == VOL_METHOD_ADAGIO)
  1074. {
  1075. voices[voice].expression_vol = value;
  1076. if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
  1077. dynamic_volume_change(voice);
  1078. }
  1079. break;
  1080. case CTL_PAN:
  1081. voices[voice].panning = (value * 2) - 128;
  1082. break;
  1083. case CTL_MAIN_VOLUME:
  1084. value = (value * 100) / 16383;
  1085. case CTRL_MAIN_VOLUME:
  1086. voices[voice].main_vol = value;
  1087. if (voices[voice].volume_irq_mode != VMODE_START_NOTE)
  1088. dynamic_volume_change(voice);
  1089. break;
  1090. default:
  1091. break;
  1092. }
  1093. }
  1094. static int guswave_start_note2(int dev, int voice, int note_num, int volume)
  1095. {
  1096. int sample, best_sample, best_delta, delta_freq;
  1097. int is16bits, samplep, patch, pan;
  1098. unsigned long note_freq, base_note, freq, flags;
  1099. unsigned char mode = 0;
  1100. if (voice < 0 || voice > 31)
  1101. {
  1102. /* printk("GUS: Invalid voice\n");*/
  1103. return -EINVAL;
  1104. }
  1105. if (note_num == 255)
  1106. {
  1107. if (voices[voice].mode & WAVE_ENVELOPES)
  1108. {
  1109. voices[voice].midi_volume = volume;
  1110. dynamic_volume_change(voice);
  1111. return 0;
  1112. }
  1113. compute_and_set_volume(voice, volume, 1);
  1114. return 0;
  1115. }
  1116. if ((patch = patch_map[voice]) == -1)
  1117. return -EINVAL;
  1118. if ((samplep = patch_table[patch]) == NOT_SAMPLE)
  1119. {
  1120. return -EINVAL;
  1121. }
  1122. note_freq = note_to_freq(note_num);
  1123. /*
  1124. * Find a sample within a patch so that the note_freq is between low_note
  1125. * and high_note.
  1126. */
  1127. sample = -1;
  1128. best_sample = samplep;
  1129. best_delta = 1000000;
  1130. while (samplep != 0 && samplep != NOT_SAMPLE && sample == -1)
  1131. {
  1132. delta_freq = note_freq - samples[samplep].base_note;
  1133. if (delta_freq < 0)
  1134. delta_freq = -delta_freq;
  1135. if (delta_freq < best_delta)
  1136. {
  1137. best_sample = samplep;
  1138. best_delta = delta_freq;
  1139. }
  1140. if (samples[samplep].low_note <= note_freq &&
  1141. note_freq <= samples[samplep].high_note)
  1142. {
  1143. sample = samplep;
  1144. }
  1145. else
  1146. samplep = samples[samplep].key; /* Link to next sample */
  1147. }
  1148. if (sample == -1)
  1149. sample = best_sample;
  1150. if (sample == -1)
  1151. {
  1152. /* printk("GUS: Patch %d not defined for note %d\n", patch, note_num);*/
  1153. return 0; /* Should play default patch ??? */
  1154. }
  1155. is16bits = (samples[sample].mode & WAVE_16_BITS) ? 1 : 0;
  1156. voices[voice].mode = samples[sample].mode;
  1157. voices[voice].patch_vol = samples[sample].volume;
  1158. if (iw_mode)
  1159. gus_write8(0x15, 0x00); /* RAM, Reset voice deactivate bit of SMSI */
  1160. if (voices[voice].mode & WAVE_ENVELOPES)
  1161. {
  1162. int i;
  1163. for (i = 0; i < 6; i++)
  1164. {
  1165. voices[voice].env_rate[i] = samples[sample].env_rate[i];
  1166. voices[voice].env_offset[i] = samples[sample].env_offset[i];
  1167. }
  1168. }
  1169. sample_map[voice] = sample;
  1170. if (voices[voice].fixed_pitch) /* Fixed pitch */
  1171. {
  1172. freq = samples[sample].base_freq;
  1173. }
  1174. else
  1175. {
  1176. base_note = samples[sample].base_note / 100;
  1177. note_freq /= 100;
  1178. freq = samples[sample].base_freq * note_freq / base_note;
  1179. }
  1180. voices[voice].orig_freq = freq;
  1181. /*
  1182. * Since the pitch bender may have been set before playing the note, we
  1183. * have to calculate the bending now.
  1184. */
  1185. freq = compute_finetune(voices[voice].orig_freq, voices[voice].bender,
  1186. voices[voice].bender_range, 0);
  1187. voices[voice].current_freq = freq;
  1188. pan = (samples[sample].panning + voices[voice].panning) / 32;
  1189. pan += 7;
  1190. if (pan < 0)
  1191. pan = 0;
  1192. if (pan > 15)
  1193. pan = 15;
  1194. if (samples[sample].mode & WAVE_16_BITS)
  1195. {
  1196. mode |= 0x04; /* 16 bits */
  1197. if ((sample_ptrs[sample] / GUS_BANK_SIZE) !=
  1198. ((sample_ptrs[sample] + samples[sample].len) / GUS_BANK_SIZE))
  1199. printk(KERN_ERR "GUS: Sample address error\n");
  1200. }
  1201. spin_lock_irqsave(&gus_lock,flags);
  1202. gus_select_voice(voice);
  1203. gus_voice_off();
  1204. gus_rampoff();
  1205. spin_unlock_irqrestore(&gus_lock,flags);
  1206. if (voices[voice].mode & WAVE_ENVELOPES)
  1207. {
  1208. compute_volume(voice, volume);
  1209. init_envelope(voice);
  1210. }
  1211. else
  1212. {
  1213. compute_and_set_volume(voice, volume, 0);
  1214. }
  1215. spin_lock_irqsave(&gus_lock,flags);
  1216. gus_select_voice(voice);
  1217. if (samples[sample].mode & WAVE_LOOP_BACK)
  1218. gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].len -
  1219. voices[voice].offset_pending, 0, is16bits); /* start=end */
  1220. else
  1221. gus_write_addr(0x0a, sample_ptrs[sample] + voices[voice].offset_pending, 0, is16bits); /* Sample start=begin */
  1222. if (samples[sample].mode & WAVE_LOOPING)
  1223. {
  1224. mode |= 0x08;
  1225. if (samples[sample].mode & WAVE_BIDIR_LOOP)
  1226. mode |= 0x10;
  1227. if (samples[sample].mode & WAVE_LOOP_BACK)
  1228. {
  1229. gus_write_addr(0x0a, sample_ptrs[sample] + samples[sample].loop_end -
  1230. voices[voice].offset_pending,
  1231. (samples[sample].fractions >> 4) & 0x0f, is16bits);
  1232. mode |= 0x40;
  1233. }
  1234. gus_write_addr(0x02, sample_ptrs[sample] + samples[sample].loop_start,
  1235. samples[sample].fractions & 0x0f, is16bits); /* Loop start location */
  1236. gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].loop_end,
  1237. (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
  1238. }
  1239. else
  1240. {
  1241. mode |= 0x20; /* Loop IRQ at the end */
  1242. voices[voice].loop_irq_mode = LMODE_FINISH; /* Ramp down at the end */
  1243. voices[voice].loop_irq_parm = 1;
  1244. gus_write_addr(0x02, sample_ptrs[sample], 0, is16bits); /* Loop start location */
  1245. gus_write_addr(0x04, sample_ptrs[sample] + samples[sample].len - 1,
  1246. (samples[sample].fractions >> 4) & 0x0f, is16bits); /* Loop end location */
  1247. }
  1248. gus_voice_freq(freq);
  1249. gus_voice_balance(pan);
  1250. gus_voice_on(mode);
  1251. spin_unlock_irqrestore(&gus_lock,flags);
  1252. return 0;
  1253. }
  1254. /*
  1255. * New guswave_start_note by Andrew J. Robinson attempts to minimize clicking
  1256. * when the note playing on the voice is changed. It uses volume
  1257. * ramping.
  1258. */
  1259. static int guswave_start_note(int dev, int voice, int note_num, int volume)
  1260. {
  1261. unsigned long flags;
  1262. int mode;
  1263. int ret_val = 0;
  1264. spin_lock_irqsave(&gus_lock,flags);
  1265. if (note_num == 255)
  1266. {
  1267. if (voices[voice].volume_irq_mode == VMODE_START_NOTE)
  1268. {
  1269. voices[voice].volume_pending = volume;
  1270. }
  1271. else
  1272. {
  1273. ret_val = guswave_start_note2(dev, voice, note_num, volume);
  1274. }
  1275. }
  1276. else
  1277. {
  1278. gus_select_voice(voice);
  1279. mode = gus_read8(0x00);
  1280. if (mode & 0x20)
  1281. gus_write8(0x00, mode & 0xdf); /* No interrupt! */
  1282. voices[voice].offset_pending = 0;
  1283. voices[voice].kill_pending = 0;
  1284. voices[voice].volume_irq_mode = 0;
  1285. voices[voice].loop_irq_mode = 0;
  1286. if (voices[voice].sample_pending >= 0)
  1287. {
  1288. spin_unlock_irqrestore(&gus_lock,flags); /* Run temporarily with interrupts enabled */
  1289. guswave_set_instr(voices[voice].dev_pending, voice, voices[voice].sample_pending);
  1290. voices[voice].sample_pending = -1;
  1291. spin_lock_irqsave(&gus_lock,flags);
  1292. gus_select_voice(voice); /* Reselect the voice (just to be sure) */
  1293. }
  1294. if ((mode & 0x01) || (int) ((gus_read16(0x09) >> 4) < (unsigned) 2065))
  1295. {
  1296. ret_val = guswave_start_note2(dev, voice, note_num, volume);
  1297. }
  1298. else
  1299. {
  1300. voices[voice].dev_pending = dev;
  1301. voices[voice].note_pending = note_num;
  1302. voices[voice].volume_pending = volume;
  1303. voices[voice].volume_irq_mode = VMODE_START_NOTE;
  1304. gus_rampoff();
  1305. gus_ramp_range(2000, 4065);
  1306. gus_ramp_rate(0, 63); /* Fastest possible rate */
  1307. gus_rampon(0x20 | 0x40); /* Ramp down, once, irq */
  1308. }
  1309. }
  1310. spin_unlock_irqrestore(&gus_lock,flags);
  1311. return ret_val;
  1312. }
  1313. static void guswave_reset(int dev)
  1314. {
  1315. int i;
  1316. for (i = 0; i < 32; i++)
  1317. {
  1318. gus_voice_init(i);
  1319. gus_voice_init2(i);
  1320. }
  1321. }
  1322. static int guswave_open(int dev, int mode)
  1323. {
  1324. int err;
  1325. if (gus_busy)
  1326. return -EBUSY;
  1327. voice_alloc->timestamp = 0;
  1328. if (gus_no_wave_dma) {
  1329. gus_no_dma = 1;
  1330. } else {
  1331. if ((err = DMAbuf_open_dma(gus_devnum)) < 0)
  1332. {
  1333. /* printk( "GUS: Loading samples without DMA\n"); */
  1334. gus_no_dma = 1; /* Upload samples using PIO */
  1335. }
  1336. else
  1337. gus_no_dma = 0;
  1338. }
  1339. init_waitqueue_head(&dram_sleeper);
  1340. gus_busy = 1;
  1341. active_device = GUS_DEV_WAVE;
  1342. gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
  1343. gus_initialize();
  1344. gus_reset();
  1345. gusintr(gus_irq, (void *)gus_hw_config, NULL); /* Serve pending interrupts */
  1346. return 0;
  1347. }
  1348. static void guswave_close(int dev)
  1349. {
  1350. gus_busy = 0;
  1351. active_device = 0;
  1352. gus_reset();
  1353. if (!gus_no_dma)
  1354. DMAbuf_close_dma(gus_devnum);
  1355. }
  1356. static int guswave_load_patch(int dev, int format, const char __user *addr,
  1357. int offs, int count, int pmgr_flag)
  1358. {
  1359. struct patch_info patch;
  1360. int instr;
  1361. long sizeof_patch;
  1362. unsigned long blk_sz, blk_end, left, src_offs, target;
  1363. sizeof_patch = (long) &patch.data[0] - (long) &patch; /* Header size */
  1364. if (format != GUS_PATCH)
  1365. {
  1366. /* printk("GUS Error: Invalid patch format (key) 0x%x\n", format);*/
  1367. return -EINVAL;
  1368. }
  1369. if (count < sizeof_patch)
  1370. {
  1371. /* printk("GUS Error: Patch header too short\n");*/
  1372. return -EINVAL;
  1373. }
  1374. count -= sizeof_patch;
  1375. if (free_sample >= MAX_SAMPLE)
  1376. {
  1377. /* printk("GUS: Sample table full\n");*/
  1378. return -ENOSPC;
  1379. }
  1380. /*
  1381. * Copy the header from user space but ignore the first bytes which have
  1382. * been transferred already.
  1383. */
  1384. if (copy_from_user(&((char *) &patch)[offs], &(addr)[offs],
  1385. sizeof_patch - offs))
  1386. return -EFAULT;
  1387. if (patch.mode & WAVE_ROM)
  1388. return -EINVAL;
  1389. if (gus_mem_size == 0)
  1390. return -ENOSPC;
  1391. instr = patch.instr_no;
  1392. if (instr < 0 || instr > MAX_PATCH)
  1393. {
  1394. /* printk(KERN_ERR "GUS: Invalid patch number %d\n", instr);*/
  1395. return -EINVAL;
  1396. }
  1397. if (count < patch.len)
  1398. {
  1399. /* printk(KERN_ERR "GUS Warning: Patch record too short (%d<%d)\n", count, (int) patch.len);*/
  1400. patch.len = count;
  1401. }
  1402. if (patch.len <= 0 || patch.len > gus_mem_size)
  1403. {
  1404. /* printk(KERN_ERR "GUS: Invalid sample length %d\n", (int) patch.len);*/
  1405. return -EINVAL;
  1406. }
  1407. if (patch.mode & WAVE_LOOPING)
  1408. {
  1409. if (patch.loop_start < 0 || patch.loop_start >= patch.len)
  1410. {
  1411. /* printk(KERN_ERR "GUS: Invalid loop start\n");*/
  1412. return -EINVAL;
  1413. }
  1414. if (patch.loop_end < patch.loop_start || patch.loop_end > patch.len)
  1415. {
  1416. /* printk(KERN_ERR "GUS: Invalid loop end\n");*/
  1417. return -EINVAL;
  1418. }
  1419. }
  1420. free_mem_ptr = (free_mem_ptr + 31) & ~31; /* 32 byte alignment */
  1421. if (patch.mode & WAVE_16_BITS)
  1422. {
  1423. /*
  1424. * 16 bit samples must fit one 256k bank.
  1425. */
  1426. if (patch.len >= GUS_BANK_SIZE)
  1427. {
  1428. /* printk("GUS: Sample (16 bit) too long %d\n", (int) patch.len);*/
  1429. return -ENOSPC;
  1430. }
  1431. if ((free_mem_ptr / GUS_BANK_SIZE) !=
  1432. ((free_mem_ptr + patch.len) / GUS_BANK_SIZE))
  1433. {
  1434. unsigned long tmp_mem =
  1435. /* Align to 256K */
  1436. ((free_mem_ptr / GUS_BANK_SIZE) + 1) * GUS_BANK_SIZE;
  1437. if ((tmp_mem + patch.len) > gus_mem_size)
  1438. return -ENOSPC;
  1439. free_mem_ptr = tmp_mem; /* This leaves unusable memory */
  1440. }
  1441. }
  1442. if ((free_mem_ptr + patch.len) > gus_mem_size)
  1443. return -ENOSPC;
  1444. sample_ptrs[free_sample] = free_mem_ptr;
  1445. /*
  1446. * Tremolo is not possible with envelopes
  1447. */
  1448. if (patch.mode & WAVE_ENVELOPES)
  1449. patch.mode &= ~WAVE_TREMOLO;
  1450. if (!(patch.mode & WAVE_FRACTIONS))
  1451. {
  1452. patch.fractions = 0;
  1453. }
  1454. memcpy((char *) &samples[free_sample], &patch, sizeof_patch);
  1455. /*
  1456. * Link this_one sample to the list of samples for patch 'instr'.
  1457. */
  1458. samples[free_sample].key = patch_table[instr];
  1459. patch_table[instr] = free_sample;
  1460. /*
  1461. * Use DMA to transfer the wave data to the DRAM
  1462. */
  1463. left = patch.len;
  1464. src_offs = 0;
  1465. target = free_mem_ptr;
  1466. while (left) /* Not completely transferred yet */
  1467. {
  1468. blk_sz = audio_devs[gus_devnum]->dmap_out->bytes_in_use;
  1469. if (blk_sz > left)
  1470. blk_sz = left;
  1471. /*
  1472. * DMA cannot cross bank (256k) boundaries. Check for that.
  1473. */
  1474. blk_end = target + blk_sz;
  1475. if ((target / GUS_BANK_SIZE) != (blk_end / GUS_BANK_SIZE))
  1476. {
  1477. /* Split the block */
  1478. blk_end &= ~(GUS_BANK_SIZE - 1);
  1479. blk_sz = blk_end - target;
  1480. }
  1481. if (gus_no_dma)
  1482. {
  1483. /*
  1484. * For some reason the DMA is not possible. We have to use PIO.
  1485. */
  1486. long i;
  1487. unsigned char data;
  1488. for (i = 0; i < blk_sz; i++)
  1489. {
  1490. get_user(*(unsigned char *) &data, (unsigned char __user *) &((addr)[sizeof_patch + i]));
  1491. if (patch.mode & WAVE_UNSIGNED)
  1492. if (!(patch.mode & WAVE_16_BITS) || (i & 0x01))
  1493. data ^= 0x80; /* Convert to signed */
  1494. gus_poke(target + i, data);
  1495. }
  1496. }
  1497. else
  1498. {
  1499. unsigned long address, hold_address;
  1500. unsigned char dma_command;
  1501. unsigned long flags;
  1502. if (audio_devs[gus_devnum]->dmap_out->raw_buf == NULL)
  1503. {
  1504. printk(KERN_ERR "GUS: DMA buffer == NULL\n");
  1505. return -ENOSPC;
  1506. }
  1507. /*
  1508. * OK, move now. First in and then out.
  1509. */
  1510. if (copy_from_user(audio_devs[gus_devnum]->dmap_out->raw_buf,
  1511. &(addr)[sizeof_patch + src_offs],
  1512. blk_sz))
  1513. return -EFAULT;
  1514. spin_lock_irqsave(&gus_lock,flags);
  1515. gus_write8(0x41, 0); /* Disable GF1 DMA */
  1516. DMAbuf_start_dma(gus_devnum, audio_devs[gus_devnum]->dmap_out->raw_buf_phys,
  1517. blk_sz, DMA_MODE_WRITE);
  1518. /*
  1519. * Set the DRAM address for the wave data
  1520. */
  1521. if (iw_mode)
  1522. {
  1523. /* Different address translation in enhanced mode */
  1524. unsigned char hi;
  1525. if (gus_dma > 4)
  1526. address = target >> 1; /* Convert to 16 bit word address */
  1527. else
  1528. address = target;
  1529. hi = (unsigned char) ((address >> 16) & 0xf0);
  1530. hi += (unsigned char) (address & 0x0f);
  1531. gus_write16(0x42, (address >> 4) & 0xffff); /* DMA address (low) */
  1532. gus_write8(0x50, hi);
  1533. }
  1534. else
  1535. {
  1536. address = target;
  1537. if (audio_devs[gus_devnum]->dmap_out->dma > 3)
  1538. {
  1539. hold_address = address;
  1540. address = address >> 1;
  1541. address &= 0x0001ffffL;
  1542. address |= (hold_address & 0x000c0000L);
  1543. }
  1544. gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
  1545. }
  1546. /*
  1547. * Start the DMA transfer
  1548. */
  1549. dma_command = 0x21; /* IRQ enable, DMA start */
  1550. if (patch.mode & WAVE_UNSIGNED)
  1551. dma_command |= 0x80; /* Invert MSB */
  1552. if (patch.mode & WAVE_16_BITS)
  1553. dma_command |= 0x40; /* 16 bit _DATA_ */
  1554. if (audio_devs[gus_devnum]->dmap_out->dma > 3)
  1555. dma_command |= 0x04; /* 16 bit DMA _channel_ */
  1556. /*
  1557. * Sleep here until the DRAM DMA done interrupt is served
  1558. */
  1559. active_device = GUS_DEV_WAVE;
  1560. gus_write8(0x41, dma_command); /* Lets go luteet (=bugs) */
  1561. spin_unlock_irqrestore(&gus_lock,flags); /* opens a race */
  1562. if (!interruptible_sleep_on_timeout(&dram_sleeper, HZ))
  1563. printk("GUS: DMA Transfer timed out\n");
  1564. }
  1565. /*
  1566. * Now the next part
  1567. */
  1568. left -= blk_sz;
  1569. src_offs += blk_sz;
  1570. target += blk_sz;
  1571. gus_write8(0x41, 0); /* Stop DMA */
  1572. }
  1573. free_mem_ptr += patch.len;
  1574. free_sample++;
  1575. return 0;
  1576. }
  1577. static void guswave_hw_control(int dev, unsigned char *event_rec)
  1578. {
  1579. int voice, cmd;
  1580. unsigned short p1, p2;
  1581. unsigned int plong;
  1582. unsigned long flags;
  1583. cmd = event_rec[2];
  1584. voice = event_rec[3];
  1585. p1 = *(unsigned short *) &event_rec[4];
  1586. p2 = *(unsigned short *) &event_rec[6];
  1587. plong = *(unsigned int *) &event_rec[4];
  1588. if ((voices[voice].volume_irq_mode == VMODE_START_NOTE) &&
  1589. (cmd != _GUS_VOICESAMPLE) && (cmd != _GUS_VOICE_POS))
  1590. do_volume_irq(voice);
  1591. switch (cmd)
  1592. {
  1593. case _GUS_NUMVOICES:
  1594. spin_lock_irqsave(&gus_lock,flags);
  1595. gus_select_voice(voice);
  1596. gus_select_max_voices(p1);
  1597. spin_unlock_irqrestore(&gus_lock,flags);
  1598. break;
  1599. case _GUS_VOICESAMPLE:
  1600. guswave_set_instr(dev, voice, p1);
  1601. break;
  1602. case _GUS_VOICEON:
  1603. spin_lock_irqsave(&gus_lock,flags);
  1604. gus_select_voice(voice);
  1605. p1 &= ~0x20; /* Don't allow interrupts */
  1606. gus_voice_on(p1);
  1607. spin_unlock_irqrestore(&gus_lock,flags);
  1608. break;
  1609. case _GUS_VOICEOFF:
  1610. spin_lock_irqsave(&gus_lock,flags);
  1611. gus_select_voice(voice);
  1612. gus_voice_off();
  1613. spin_unlock_irqrestore(&gus_lock,flags);
  1614. break;
  1615. case _GUS_VOICEFADE:
  1616. gus_voice_fade(voice);
  1617. break;
  1618. case _GUS_VOICEMODE:
  1619. spin_lock_irqsave(&gus_lock,flags);
  1620. gus_select_voice(voice);
  1621. p1 &= ~0x20; /* Don't allow interrupts */
  1622. gus_voice_mode(p1);
  1623. spin_unlock_irqrestore(&gus_lock,flags);
  1624. break;
  1625. case _GUS_VOICEBALA:
  1626. spin_lock_irqsave(&gus_lock,flags);
  1627. gus_select_voice(voice);
  1628. gus_voice_balance(p1);
  1629. spin_unlock_irqrestore(&gus_lock,flags);
  1630. break;
  1631. case _GUS_VOICEFREQ:
  1632. spin_lock_irqsave(&gus_lock,flags);
  1633. gus_select_voice(voice);
  1634. gus_voice_freq(plong);
  1635. spin_unlock_irqrestore(&gus_lock,flags);
  1636. break;
  1637. case _GUS_VOICEVOL:
  1638. spin_lock_irqsave(&gus_lock,flags);
  1639. gus_select_voice(voice);
  1640. gus_voice_volume(p1);
  1641. spin_unlock_irqrestore(&gus_lock,flags);
  1642. break;
  1643. case _GUS_VOICEVOL2: /* Just update the software voice level */
  1644. voices[voice].initial_volume = voices[voice].current_volume = p1;
  1645. break;
  1646. case _GUS_RAMPRANGE:
  1647. if (voices[voice].mode & WAVE_ENVELOPES)
  1648. break; /* NO-NO */
  1649. spin_lock_irqsave(&gus_lock,flags);
  1650. gus_select_voice(voice);
  1651. gus_ramp_range(p1, p2);
  1652. spin_unlock_irqrestore(&gus_lock,flags);
  1653. break;
  1654. case _GUS_RAMPRATE:
  1655. if (voices[voice].mode & WAVE_ENVELOPES)
  1656. break; /* NJET-NJET */
  1657. spin_lock_irqsave(&gus_lock,flags);
  1658. gus_select_voice(voice);
  1659. gus_ramp_rate(p1, p2);
  1660. spin_unlock_irqrestore(&gus_lock,flags);
  1661. break;
  1662. case _GUS_RAMPMODE:
  1663. if (voices[voice].mode & WAVE_ENVELOPES)
  1664. break; /* NO-NO */
  1665. spin_lock_irqsave(&gus_lock,flags);
  1666. gus_select_voice(voice);
  1667. p1 &= ~0x20; /* Don't allow interrupts */
  1668. gus_ramp_mode(p1);
  1669. spin_unlock_irqrestore(&gus_lock,flags);
  1670. break;
  1671. case _GUS_RAMPON:
  1672. if (voices[voice].mode & WAVE_ENVELOPES)
  1673. break; /* EI-EI */
  1674. spin_lock_irqsave(&gus_lock,flags);
  1675. gus_select_voice(voice);
  1676. p1 &= ~0x20; /* Don't allow interrupts */
  1677. gus_rampon(p1);
  1678. spin_unlock_irqrestore(&gus_lock,flags);
  1679. break;
  1680. case _GUS_RAMPOFF:
  1681. if (voices[voice].mode & WAVE_ENVELOPES)
  1682. break; /* NEJ-NEJ */
  1683. spin_lock_irqsave(&gus_lock,flags);
  1684. gus_select_voice(voice);
  1685. gus_rampoff();
  1686. spin_unlock_irqrestore(&gus_lock,flags);
  1687. break;
  1688. case _GUS_VOLUME_SCALE:
  1689. volume_base = p1;
  1690. volume_scale = p2;
  1691. break;
  1692. case _GUS_VOICE_POS:
  1693. spin_lock_irqsave(&gus_lock,flags);
  1694. gus_select_voice(voice);
  1695. gus_set_voice_pos(voice, plong);
  1696. spin_unlock_irqrestore(&gus_lock,flags);
  1697. break;
  1698. default:
  1699. break;
  1700. }
  1701. }
  1702. static int gus_audio_set_speed(int speed)
  1703. {
  1704. if (speed <= 0)
  1705. speed = gus_audio_speed;
  1706. if (speed < 4000)
  1707. speed = 4000;
  1708. if (speed > 44100)
  1709. speed = 44100;
  1710. gus_audio_speed = speed;
  1711. if (only_read_access)
  1712. {
  1713. /* Compute nearest valid recording speed and return it */
  1714. /* speed = (9878400 / (gus_audio_speed + 2)) / 16; */
  1715. speed = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
  1716. speed = (9878400 / (speed * 16)) - 2;
  1717. }
  1718. return speed;
  1719. }
  1720. static int gus_audio_set_channels(int channels)
  1721. {
  1722. if (!channels)
  1723. return gus_audio_channels;
  1724. if (channels > 2)
  1725. channels = 2;
  1726. if (channels < 1)
  1727. channels = 1;
  1728. gus_audio_channels = channels;
  1729. return channels;
  1730. }
  1731. static int gus_audio_set_bits(int bits)
  1732. {
  1733. if (!bits)
  1734. return gus_audio_bits;
  1735. if (bits != 8 && bits != 16)
  1736. bits = 8;
  1737. if (only_8_bits)
  1738. bits = 8;
  1739. gus_audio_bits = bits;
  1740. return bits;
  1741. }
  1742. static int gus_audio_ioctl(int dev, unsigned int cmd, void __user *arg)
  1743. {
  1744. int val;
  1745. switch (cmd)
  1746. {
  1747. case SOUND_PCM_WRITE_RATE:
  1748. if (get_user(val, (int __user*)arg))
  1749. return -EFAULT;
  1750. val = gus_audio_set_speed(val);
  1751. break;
  1752. case SOUND_PCM_READ_RATE:
  1753. val = gus_audio_speed;
  1754. break;
  1755. case SNDCTL_DSP_STEREO:
  1756. if (get_user(val, (int __user *)arg))
  1757. return -EFAULT;
  1758. val = gus_audio_set_channels(val + 1) - 1;
  1759. break;
  1760. case SOUND_PCM_WRITE_CHANNELS:
  1761. if (get_user(val, (int __user *)arg))
  1762. return -EFAULT;
  1763. val = gus_audio_set_channels(val);
  1764. break;
  1765. case SOUND_PCM_READ_CHANNELS:
  1766. val = gus_audio_channels;
  1767. break;
  1768. case SNDCTL_DSP_SETFMT:
  1769. if (get_user(val, (int __user *)arg))
  1770. return -EFAULT;
  1771. val = gus_audio_set_bits(val);
  1772. break;
  1773. case SOUND_PCM_READ_BITS:
  1774. val = gus_audio_bits;
  1775. break;
  1776. case SOUND_PCM_WRITE_FILTER: /* NOT POSSIBLE */
  1777. case SOUND_PCM_READ_FILTER:
  1778. val = -EINVAL;
  1779. break;
  1780. default:
  1781. return -EINVAL;
  1782. }
  1783. return put_user(val, (int __user *)arg);
  1784. }
  1785. static void gus_audio_reset(int dev)
  1786. {
  1787. if (recording_active)
  1788. {
  1789. gus_write8(0x49, 0x00); /* Halt recording */
  1790. set_input_volumes();
  1791. }
  1792. }
  1793. static int saved_iw_mode; /* A hack hack hack */
  1794. static int gus_audio_open(int dev, int mode)
  1795. {
  1796. if (gus_busy)
  1797. return -EBUSY;
  1798. if (gus_pnp_flag && mode & OPEN_READ)
  1799. {
  1800. /* printk(KERN_ERR "GUS: Audio device #%d is playback only.\n", dev);*/
  1801. return -EIO;
  1802. }
  1803. gus_initialize();
  1804. gus_busy = 1;
  1805. active_device = 0;
  1806. saved_iw_mode = iw_mode;
  1807. if (iw_mode)
  1808. {
  1809. /* There are some problems with audio in enhanced mode so disable it */
  1810. gus_write8(0x19, gus_read8(0x19) & ~0x01); /* Disable enhanced mode */
  1811. iw_mode = 0;
  1812. }
  1813. gus_reset();
  1814. reset_sample_memory();
  1815. gus_select_max_voices(14);
  1816. pcm_active = 0;
  1817. dma_active = 0;
  1818. pcm_opened = 1;
  1819. if (mode & OPEN_READ)
  1820. {
  1821. recording_active = 1;
  1822. set_input_volumes();
  1823. }
  1824. only_read_access = !(mode & OPEN_WRITE);
  1825. only_8_bits = mode & OPEN_READ;
  1826. if (only_8_bits)
  1827. audio_devs[dev]->format_mask = AFMT_U8;
  1828. else
  1829. audio_devs[dev]->format_mask = AFMT_U8 | AFMT_S16_LE;
  1830. return 0;
  1831. }
  1832. static void gus_audio_close(int dev)
  1833. {
  1834. iw_mode = saved_iw_mode;
  1835. gus_reset();
  1836. gus_busy = 0;
  1837. pcm_opened = 0;
  1838. active_device = 0;
  1839. if (recording_active)
  1840. {
  1841. gus_write8(0x49, 0x00); /* Halt recording */
  1842. set_input_volumes();
  1843. }
  1844. recording_active = 0;
  1845. }
  1846. static void gus_audio_update_volume(void)
  1847. {
  1848. unsigned long flags;
  1849. int voice;
  1850. if (pcm_active && pcm_opened)
  1851. for (voice = 0; voice < gus_audio_channels; voice++)
  1852. {
  1853. spin_lock_irqsave(&gus_lock,flags);
  1854. gus_select_voice(voice);
  1855. gus_rampoff();
  1856. gus_voice_volume(1530 + (25 * gus_pcm_volume));
  1857. gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
  1858. spin_unlock_irqrestore(&gus_lock,flags);
  1859. }
  1860. }
  1861. static void play_next_pcm_block(void)
  1862. {
  1863. unsigned long flags;
  1864. int speed = gus_audio_speed;
  1865. int this_one, is16bits, chn;
  1866. unsigned long dram_loc;
  1867. unsigned char mode[2], ramp_mode[2];
  1868. if (!pcm_qlen)
  1869. return;
  1870. this_one = pcm_head;
  1871. for (chn = 0; chn < gus_audio_channels; chn++)
  1872. {
  1873. mode[chn] = 0x00;
  1874. ramp_mode[chn] = 0x03; /* Ramping and rollover off */
  1875. if (chn == 0)
  1876. {
  1877. mode[chn] |= 0x20; /* Loop IRQ */
  1878. voices[chn].loop_irq_mode = LMODE_PCM;
  1879. }
  1880. if (gus_audio_bits != 8)
  1881. {
  1882. is16bits = 1;
  1883. mode[chn] |= 0x04; /* 16 bit data */
  1884. }
  1885. else
  1886. is16bits = 0;
  1887. dram_loc = this_one * pcm_bsize;
  1888. dram_loc += chn * pcm_banksize;
  1889. if (this_one == (pcm_nblk - 1)) /* Last fragment of the DRAM buffer */
  1890. {
  1891. mode[chn] |= 0x08; /* Enable loop */
  1892. ramp_mode[chn] = 0x03; /* Disable rollover bit */
  1893. }
  1894. else
  1895. {
  1896. if (chn == 0)
  1897. ramp_mode[chn] = 0x04; /* Enable rollover bit */
  1898. }
  1899. spin_lock_irqsave(&gus_lock,flags);
  1900. gus_select_voice(chn);
  1901. gus_voice_freq(speed);
  1902. if (gus_audio_channels == 1)
  1903. gus_voice_balance(7); /* mono */
  1904. else if (chn == 0)
  1905. gus_voice_balance(0); /* left */
  1906. else
  1907. gus_voice_balance(15); /* right */
  1908. if (!pcm_active) /* Playback not already active */
  1909. {
  1910. /*
  1911. * The playback was not started yet (or there has been a pause).
  1912. * Start the voice (again) and ask for a rollover irq at the end of
  1913. * this_one block. If this_one one is last of the buffers, use just
  1914. * the normal loop with irq.
  1915. */
  1916. gus_voice_off();
  1917. gus_rampoff();
  1918. gus_voice_volume(1530 + (25 * gus_pcm_volume));
  1919. gus_ramp_range(65, 1530 + (25 * gus_pcm_volume));
  1920. gus_write_addr(0x0a, chn * pcm_banksize, 0, is16bits); /* Starting position */
  1921. gus_write_addr(0x02, chn * pcm_banksize, 0, is16bits); /* Loop start */
  1922. if (chn != 0)
  1923. gus_write_addr(0x04, pcm_banksize + (pcm_bsize * pcm_nblk) - 1,
  1924. 0, is16bits); /* Loop end location */
  1925. }
  1926. if (chn == 0)
  1927. gus_write_addr(0x04, dram_loc + pcm_bsize - 1,
  1928. 0, is16bits); /* Loop end location */
  1929. else
  1930. mode[chn] |= 0x08; /* Enable looping */
  1931. spin_unlock_irqrestore(&gus_lock,flags);
  1932. }
  1933. for (chn = 0; chn < gus_audio_channels; chn++)
  1934. {
  1935. spin_lock_irqsave(&gus_lock,flags);
  1936. gus_select_voice(chn);
  1937. gus_write8(0x0d, ramp_mode[chn]);
  1938. if (iw_mode)
  1939. gus_write8(0x15, 0x00); /* Reset voice deactivate bit of SMSI */
  1940. gus_voice_on(mode[chn]);
  1941. spin_unlock_irqrestore(&gus_lock,flags);
  1942. }
  1943. pcm_active = 1;
  1944. }
  1945. static void gus_transfer_output_block(int dev, unsigned long buf,
  1946. int total_count, int intrflag, int chn)
  1947. {
  1948. /*
  1949. * This routine transfers one block of audio data to the DRAM. In mono mode
  1950. * it's called just once. When in stereo mode, this_one routine is called
  1951. * once for both channels.
  1952. *
  1953. * The left/mono channel data is transferred to the beginning of dram and the
  1954. * right data to the area pointed by gus_page_size.
  1955. */
  1956. int this_one, count;
  1957. unsigned long flags;
  1958. unsigned char dma_command;
  1959. unsigned long address, hold_address;
  1960. spin_lock_irqsave(&gus_lock,flags);
  1961. count = total_count / gus_audio_channels;
  1962. if (chn == 0)
  1963. {
  1964. if (pcm_qlen >= pcm_nblk)
  1965. printk(KERN_WARNING "GUS Warning: PCM buffers out of sync\n");
  1966. this_one = pcm_current_block = pcm_tail;
  1967. pcm_qlen++;
  1968. pcm_tail = (pcm_tail + 1) % pcm_nblk;
  1969. pcm_datasize[this_one] = count;
  1970. }
  1971. else
  1972. this_one = pcm_current_block;
  1973. gus_write8(0x41, 0); /* Disable GF1 DMA */
  1974. DMAbuf_start_dma(dev, buf + (chn * count), count, DMA_MODE_WRITE);
  1975. address = this_one * pcm_bsize;
  1976. address += chn * pcm_banksize;
  1977. if (audio_devs[dev]->dmap_out->dma > 3)
  1978. {
  1979. hold_address = address;
  1980. address = address >> 1;
  1981. address &= 0x0001ffffL;
  1982. address |= (hold_address & 0x000c0000L);
  1983. }
  1984. gus_write16(0x42, (address >> 4) & 0xffff); /* DRAM DMA address */
  1985. dma_command = 0x21; /* IRQ enable, DMA start */
  1986. if (gus_audio_bits != 8)
  1987. dma_command |= 0x40; /* 16 bit _DATA_ */
  1988. else
  1989. dma_command |= 0x80; /* Invert MSB */
  1990. if (audio_devs[dev]->dmap_out->dma > 3)
  1991. dma_command |= 0x04; /* 16 bit DMA channel */
  1992. gus_write8(0x41, dma_command); /* Kick start */
  1993. if (chn == (gus_audio_channels - 1)) /* Last channel */
  1994. {
  1995. /*
  1996. * Last (right or mono) channel data
  1997. */
  1998. dma_active = 1; /* DMA started. There is a unacknowledged buffer */
  1999. active_device = GUS_DEV_PCM_DONE;
  2000. if (!pcm_active && (pcm_qlen > 1 || count < pcm_bsize))
  2001. {
  2002. play_next_pcm_block();
  2003. }
  2004. }
  2005. else
  2006. {
  2007. /*
  2008. * Left channel data. The right channel
  2009. * is transferred after DMA interrupt
  2010. */
  2011. active_device = GUS_DEV_PCM_CONTINUE;
  2012. }
  2013. spin_unlock_irqrestore(&gus_lock,flags);
  2014. }
  2015. static void gus_uninterleave8(char *buf, int l)
  2016. {
  2017. /* This routine uninterleaves 8 bit stereo output (LRLRLR->LLLRRR) */
  2018. int i, p = 0, halfsize = l / 2;
  2019. char *buf2 = buf + halfsize, *src = bounce_buf;
  2020. memcpy(bounce_buf, buf, l);
  2021. for (i = 0; i < halfsize; i++)
  2022. {
  2023. buf[i] = src[p++]; /* Left channel */
  2024. buf2[i] = src[p++]; /* Right channel */
  2025. }
  2026. }
  2027. static void gus_uninterleave16(short *buf, int l)
  2028. {
  2029. /* This routine uninterleaves 16 bit stereo output (LRLRLR->LLLRRR) */
  2030. int i, p = 0, halfsize = l / 2;
  2031. short *buf2 = buf + halfsize, *src = (short *) bounce_buf;
  2032. memcpy(bounce_buf, (char *) buf, l * 2);
  2033. for (i = 0; i < halfsize; i++)
  2034. {
  2035. buf[i] = src[p++]; /* Left channel */
  2036. buf2[i] = src[p++]; /* Right channel */
  2037. }
  2038. }
  2039. static void gus_audio_output_block(int dev, unsigned long buf, int total_count,
  2040. int intrflag)
  2041. {
  2042. struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
  2043. dmap->flags |= DMA_NODMA | DMA_NOTIMEOUT;
  2044. pcm_current_buf = buf;
  2045. pcm_current_count = total_count;
  2046. pcm_current_intrflag = intrflag;
  2047. pcm_current_dev = dev;
  2048. if (gus_audio_channels == 2)
  2049. {
  2050. char *b = dmap->raw_buf + (buf - dmap->raw_buf_phys);
  2051. if (gus_audio_bits == 8)
  2052. gus_uninterleave8(b, total_count);
  2053. else
  2054. gus_uninterleave16((short *) b, total_count / 2);
  2055. }
  2056. gus_transfer_output_block(dev, buf, total_count, intrflag, 0);
  2057. }
  2058. static void gus_audio_start_input(int dev, unsigned long buf, int count,
  2059. int intrflag)
  2060. {
  2061. unsigned long flags;
  2062. unsigned char mode;
  2063. spin_lock_irqsave(&gus_lock,flags);
  2064. DMAbuf_start_dma(dev, buf, count, DMA_MODE_READ);
  2065. mode = 0xa0; /* DMA IRQ enabled, invert MSB */
  2066. if (audio_devs[dev]->dmap_in->dma > 3)
  2067. mode |= 0x04; /* 16 bit DMA channel */
  2068. if (gus_audio_channels > 1)
  2069. mode |= 0x02; /* Stereo */
  2070. mode |= 0x01; /* DMA enable */
  2071. gus_write8(0x49, mode);
  2072. spin_unlock_irqrestore(&gus_lock,flags);
  2073. }
  2074. static int gus_audio_prepare_for_input(int dev, int bsize, int bcount)
  2075. {
  2076. unsigned int rate;
  2077. gus_audio_bsize = bsize;
  2078. audio_devs[dev]->dmap_in->flags |= DMA_NODMA;
  2079. rate = (((9878400 + gus_audio_speed / 2) / (gus_audio_speed + 2)) + 8) / 16;
  2080. gus_write8(0x48, rate & 0xff); /* Set sampling rate */
  2081. if (gus_audio_bits != 8)
  2082. {
  2083. /* printk("GUS Error: 16 bit recording not supported\n");*/
  2084. return -EINVAL;
  2085. }
  2086. return 0;
  2087. }
  2088. static int gus_audio_prepare_for_output(int dev, int bsize, int bcount)
  2089. {
  2090. int i;
  2091. long mem_ptr, mem_size;
  2092. audio_devs[dev]->dmap_out->flags |= DMA_NODMA | DMA_NOTIMEOUT;
  2093. mem_ptr = 0;
  2094. mem_size = gus_mem_size / gus_audio_channels;
  2095. if (mem_size > (256 * 1024))
  2096. mem_size = 256 * 1024;
  2097. pcm_bsize = bsize / gus_audio_channels;
  2098. pcm_head = pcm_tail = pcm_qlen = 0;
  2099. pcm_nblk = 2; /* MAX_PCM_BUFFERS; */
  2100. if ((pcm_bsize * pcm_nblk) > mem_size)
  2101. pcm_nblk = mem_size / pcm_bsize;
  2102. for (i = 0; i < pcm_nblk; i++)
  2103. pcm_datasize[i] = 0;
  2104. pcm_banksize = pcm_nblk * pcm_bsize;
  2105. if (gus_audio_bits != 8 && pcm_banksize == (256 * 1024))
  2106. pcm_nblk--;
  2107. gus_write8(0x41, 0); /* Disable GF1 DMA */
  2108. return 0;
  2109. }
  2110. static int gus_local_qlen(int dev)
  2111. {
  2112. return pcm_qlen;
  2113. }
  2114. static struct audio_driver gus_audio_driver =
  2115. {
  2116. .owner = THIS_MODULE,
  2117. .open = gus_audio_open,
  2118. .close = gus_audio_close,
  2119. .output_block = gus_audio_output_block,
  2120. .start_input = gus_audio_start_input,
  2121. .ioctl = gus_audio_ioctl,
  2122. .prepare_for_input = gus_audio_prepare_for_input,
  2123. .prepare_for_output = gus_audio_prepare_for_output,
  2124. .halt_io = gus_audio_reset,
  2125. .local_qlen = gus_local_qlen,
  2126. };
  2127. static void guswave_setup_voice(int dev, int voice, int chn)
  2128. {
  2129. struct channel_info *info = &synth_devs[dev]->chn_info[chn];
  2130. guswave_set_instr(dev, voice, info->pgm_num);
  2131. voices[voice].expression_vol = info->controllers[CTL_EXPRESSION]; /* Just MSB */
  2132. voices[voice].main_vol = (info->controllers[CTL_MAIN_VOLUME] * 100) / (unsigned) 128;
  2133. voices[voice].panning = (info->controllers[CTL_PAN] * 2) - 128;
  2134. voices[voice].bender = 0;
  2135. voices[voice].bender_range = info->bender_range;
  2136. if (chn == 9)
  2137. voices[voice].fixed_pitch = 1;
  2138. }
  2139. static void guswave_bender(int dev, int voice, int value)
  2140. {
  2141. int freq;
  2142. unsigned long flags;
  2143. voices[voice].bender = value - 8192;
  2144. freq = compute_finetune(voices[voice].orig_freq, value - 8192, voices[voice].bender_range, 0);
  2145. voices[voice].current_freq = freq;
  2146. spin_lock_irqsave(&gus_lock,flags);
  2147. gus_select_voice(voice);
  2148. gus_voice_freq(freq);
  2149. spin_unlock_irqrestore(&gus_lock,flags);
  2150. }
  2151. static int guswave_alloc(int dev, int chn, int note, struct voice_alloc_info *alloc)
  2152. {
  2153. int i, p, best = -1, best_time = 0x7fffffff;
  2154. p = alloc->ptr;
  2155. /*
  2156. * First look for a completely stopped voice
  2157. */
  2158. for (i = 0; i < alloc->max_voice; i++)
  2159. {
  2160. if (alloc->map[p] == 0)
  2161. {
  2162. alloc->ptr = p;
  2163. return p;
  2164. }
  2165. if (alloc->alloc_times[p] < best_time)
  2166. {
  2167. best = p;
  2168. best_time = alloc->alloc_times[p];
  2169. }
  2170. p = (p + 1) % alloc->max_voice;
  2171. }
  2172. /*
  2173. * Then look for a releasing voice
  2174. */
  2175. for (i = 0; i < alloc->max_voice; i++)
  2176. {
  2177. if (alloc->map[p] == 0xffff)
  2178. {
  2179. alloc->ptr = p;
  2180. return p;
  2181. }
  2182. p = (p + 1) % alloc->max_voice;
  2183. }
  2184. if (best >= 0)
  2185. p = best;
  2186. alloc->ptr = p;
  2187. return p;
  2188. }
  2189. static struct synth_operations guswave_operations =
  2190. {
  2191. .owner = THIS_MODULE,
  2192. .id = "GUS",
  2193. .info = &gus_info,
  2194. .midi_dev = 0,
  2195. .synth_type = SYNTH_TYPE_SAMPLE,
  2196. .synth_subtype = SAMPLE_TYPE_GUS,
  2197. .open = guswave_open,
  2198. .close = guswave_close,
  2199. .ioctl = guswave_ioctl,
  2200. .kill_note = guswave_kill_note,
  2201. .start_note = guswave_start_note,
  2202. .set_instr = guswave_set_instr,
  2203. .reset = guswave_reset,
  2204. .hw_control = guswave_hw_control,
  2205. .load_patch = guswave_load_patch,
  2206. .aftertouch = guswave_aftertouch,
  2207. .controller = guswave_controller,
  2208. .panning = guswave_panning,
  2209. .volume_method = guswave_volume_method,
  2210. .bender = guswave_bender,
  2211. .alloc_voice = guswave_alloc,
  2212. .setup_voice = guswave_setup_voice
  2213. };
  2214. static void set_input_volumes(void)
  2215. {
  2216. unsigned long flags;
  2217. unsigned char mask = 0xff & ~0x06; /* Just line out enabled */
  2218. if (have_gus_max) /* Don't disturb GUS MAX */
  2219. return;
  2220. spin_lock_irqsave(&gus_lock,flags);
  2221. /*
  2222. * Enable channels having vol > 10%
  2223. * Note! bit 0x01 means the line in DISABLED while 0x04 means
  2224. * the mic in ENABLED.
  2225. */
  2226. if (gus_line_vol > 10)
  2227. mask &= ~0x01;
  2228. if (gus_mic_vol > 10)
  2229. mask |= 0x04;
  2230. if (recording_active)
  2231. {
  2232. /*
  2233. * Disable channel, if not selected for recording
  2234. */
  2235. if (!(gus_recmask & SOUND_MASK_LINE))
  2236. mask |= 0x01;
  2237. if (!(gus_recmask & SOUND_MASK_MIC))
  2238. mask &= ~0x04;
  2239. }
  2240. mix_image &= ~0x07;
  2241. mix_image |= mask & 0x07;
  2242. outb((mix_image), u_Mixer);
  2243. spin_unlock_irqrestore(&gus_lock,flags);
  2244. }
  2245. #define MIX_DEVS (SOUND_MASK_MIC|SOUND_MASK_LINE| \
  2246. SOUND_MASK_SYNTH|SOUND_MASK_PCM)
  2247. int gus_default_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  2248. {
  2249. int vol, val;
  2250. if (((cmd >> 8) & 0xff) != 'M')
  2251. return -EINVAL;
  2252. if (!access_ok(VERIFY_WRITE, arg, sizeof(int)))
  2253. return -EFAULT;
  2254. if (_SIOC_DIR(cmd) & _SIOC_WRITE)
  2255. {
  2256. if (__get_user(val, (int __user *) arg))
  2257. return -EFAULT;
  2258. switch (cmd & 0xff)
  2259. {
  2260. case SOUND_MIXER_RECSRC:
  2261. gus_recmask = val & MIX_DEVS;
  2262. if (!(gus_recmask & (SOUND_MASK_MIC | SOUND_MASK_LINE)))
  2263. gus_recmask = SOUND_MASK_MIC;
  2264. /* Note! Input volumes are updated during next open for recording */
  2265. val = gus_recmask;
  2266. break;
  2267. case SOUND_MIXER_MIC:
  2268. vol = val & 0xff;
  2269. if (vol < 0)
  2270. vol = 0;
  2271. if (vol > 100)
  2272. vol = 100;
  2273. gus_mic_vol = vol;
  2274. set_input_volumes();
  2275. val = vol | (vol << 8);
  2276. break;
  2277. case SOUND_MIXER_LINE:
  2278. vol = val & 0xff;
  2279. if (vol < 0)
  2280. vol = 0;
  2281. if (vol > 100)
  2282. vol = 100;
  2283. gus_line_vol = vol;
  2284. set_input_volumes();
  2285. val = vol | (vol << 8);
  2286. break;
  2287. case SOUND_MIXER_PCM:
  2288. gus_pcm_volume = val & 0xff;
  2289. if (gus_pcm_volume < 0)
  2290. gus_pcm_volume = 0;
  2291. if (gus_pcm_volume > 100)
  2292. gus_pcm_volume = 100;
  2293. gus_audio_update_volume();
  2294. val = gus_pcm_volume | (gus_pcm_volume << 8);
  2295. break;
  2296. case SOUND_MIXER_SYNTH:
  2297. gus_wave_volume = val & 0xff;
  2298. if (gus_wave_volume < 0)
  2299. gus_wave_volume = 0;
  2300. if (gus_wave_volume > 100)
  2301. gus_wave_volume = 100;
  2302. if (active_device == GUS_DEV_WAVE)
  2303. {
  2304. int voice;
  2305. for (voice = 0; voice < nr_voices; voice++)
  2306. dynamic_volume_change(voice); /* Apply the new vol */
  2307. }
  2308. val = gus_wave_volume | (gus_wave_volume << 8);
  2309. break;
  2310. default:
  2311. return -EINVAL;
  2312. }
  2313. }
  2314. else
  2315. {
  2316. switch (cmd & 0xff)
  2317. {
  2318. /*
  2319. * Return parameters
  2320. */
  2321. case SOUND_MIXER_RECSRC:
  2322. val = gus_recmask;
  2323. break;
  2324. case SOUND_MIXER_DEVMASK:
  2325. val = MIX_DEVS;
  2326. break;
  2327. case SOUND_MIXER_STEREODEVS:
  2328. val = 0;
  2329. break;
  2330. case SOUND_MIXER_RECMASK:
  2331. val = SOUND_MASK_MIC | SOUND_MASK_LINE;
  2332. break;
  2333. case SOUND_MIXER_CAPS:
  2334. val = 0;
  2335. break;
  2336. case SOUND_MIXER_MIC:
  2337. val = gus_mic_vol | (gus_mic_vol << 8);
  2338. break;
  2339. case SOUND_MIXER_LINE:
  2340. val = gus_line_vol | (gus_line_vol << 8);
  2341. break;
  2342. case SOUND_MIXER_PCM:
  2343. val = gus_pcm_volume | (gus_pcm_volume << 8);
  2344. break;
  2345. case SOUND_MIXER_SYNTH:
  2346. val = gus_wave_volume | (gus_wave_volume << 8);
  2347. break;
  2348. default:
  2349. return -EINVAL;
  2350. }
  2351. }
  2352. return __put_user(val, (int __user *)arg);
  2353. }
  2354. static struct mixer_operations gus_mixer_operations =
  2355. {
  2356. .owner = THIS_MODULE,
  2357. .id = "GUS",
  2358. .name = "Gravis Ultrasound",
  2359. .ioctl = gus_default_mixer_ioctl
  2360. };
  2361. static int __init gus_default_mixer_init(void)
  2362. {
  2363. int n;
  2364. if ((n = sound_alloc_mixerdev()) != -1)
  2365. {
  2366. /*
  2367. * Don't install if there is another
  2368. * mixer
  2369. */
  2370. mixer_devs[n] = &gus_mixer_operations;
  2371. }
  2372. if (have_gus_max)
  2373. {
  2374. /*
  2375. * Enable all mixer channels on the GF1 side. Otherwise recording will
  2376. * not be possible using GUS MAX.
  2377. */
  2378. mix_image &= ~0x07;
  2379. mix_image |= 0x04; /* All channels enabled */
  2380. outb((mix_image), u_Mixer);
  2381. }
  2382. return n;
  2383. }
  2384. void __init gus_wave_init(struct address_info *hw_config)
  2385. {
  2386. unsigned long flags;
  2387. unsigned char val;
  2388. char *model_num = "2.4";
  2389. char tmp[64];
  2390. int gus_type = 0x24; /* 2.4 */
  2391. int irq = hw_config->irq, dma = hw_config->dma, dma2 = hw_config->dma2;
  2392. int sdev;
  2393. hw_config->slots[0] = -1; /* No wave */
  2394. hw_config->slots[1] = -1; /* No ad1848 */
  2395. hw_config->slots[4] = -1; /* No audio */
  2396. hw_config->slots[5] = -1; /* No mixer */
  2397. if (!gus_pnp_flag)
  2398. {
  2399. if (irq < 0 || irq > 15)
  2400. {
  2401. printk(KERN_ERR "ERROR! Invalid IRQ#%d. GUS Disabled", irq);
  2402. return;
  2403. }
  2404. }
  2405. if (dma < 0 || dma > 7 || dma == 4)
  2406. {
  2407. printk(KERN_ERR "ERROR! Invalid DMA#%d. GUS Disabled", dma);
  2408. return;
  2409. }
  2410. gus_irq = irq;
  2411. gus_dma = dma;
  2412. gus_dma2 = dma2;
  2413. gus_hw_config = hw_config;
  2414. if (gus_dma2 == -1)
  2415. gus_dma2 = dma;
  2416. /*
  2417. * Try to identify the GUS model.
  2418. *
  2419. * Versions < 3.6 don't have the digital ASIC. Try to probe it first.
  2420. */
  2421. spin_lock_irqsave(&gus_lock,flags);
  2422. outb((0x20), gus_base + 0x0f);
  2423. val = inb(gus_base + 0x0f);
  2424. spin_unlock_irqrestore(&gus_lock,flags);
  2425. if (gus_pnp_flag || (val != 0xff && (val & 0x06))) /* Should be 0x02?? */
  2426. {
  2427. int ad_flags = 0;
  2428. if (gus_pnp_flag)
  2429. ad_flags = 0x12345678; /* Interwave "magic" */
  2430. /*
  2431. * It has the digital ASIC so the card is at least v3.4.
  2432. * Next try to detect the true model.
  2433. */
  2434. if (gus_pnp_flag) /* Hack hack hack */
  2435. val = 10;
  2436. else
  2437. val = inb(u_MixSelect);
  2438. /*
  2439. * Value 255 means pre-3.7 which don't have mixer.
  2440. * Values 5 thru 9 mean v3.7 which has a ICS2101 mixer.
  2441. * 10 and above is GUS MAX which has the CS4231 codec/mixer.
  2442. *
  2443. */
  2444. if (val == 255 || val < 5)
  2445. {
  2446. model_num = "3.4";
  2447. gus_type = 0x34;
  2448. }
  2449. else if (val < 10)
  2450. {
  2451. model_num = "3.7";
  2452. gus_type = 0x37;
  2453. mixer_type = ICS2101;
  2454. request_region(u_MixSelect, 1, "GUS mixer");
  2455. }
  2456. else
  2457. {
  2458. struct resource *ports;
  2459. ports = request_region(gus_base + 0x10c, 4, "ad1848");
  2460. model_num = "MAX";
  2461. gus_type = 0x40;
  2462. mixer_type = CS4231;
  2463. #ifdef CONFIG_SOUND_GUSMAX
  2464. {
  2465. unsigned char max_config = 0x40; /* Codec enable */
  2466. if (gus_dma2 == -1)
  2467. gus_dma2 = gus_dma;
  2468. if (gus_dma > 3)
  2469. max_config |= 0x10; /* 16 bit capture DMA */
  2470. if (gus_dma2 > 3)
  2471. max_config |= 0x20; /* 16 bit playback DMA */
  2472. max_config |= (gus_base >> 4) & 0x0f; /* Extract the X from 2X0 */
  2473. outb((max_config), gus_base + 0x106); /* UltraMax control */
  2474. }
  2475. if (!ports)
  2476. goto no_cs4231;
  2477. if (ad1848_detect(ports, &ad_flags, hw_config->osp))
  2478. {
  2479. char *name = "GUS MAX";
  2480. int old_num_mixers = num_mixers;
  2481. if (gus_pnp_flag)
  2482. name = "GUS PnP";
  2483. gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
  2484. gus_wave_volume = 90;
  2485. have_gus_max = 1;
  2486. if (hw_config->name)
  2487. name = hw_config->name;
  2488. hw_config->slots[1] = ad1848_init(name, ports,
  2489. -irq, gus_dma2, /* Playback DMA */
  2490. gus_dma, /* Capture DMA */
  2491. 1, /* Share DMA channels with GF1 */
  2492. hw_config->osp,
  2493. THIS_MODULE);
  2494. if (num_mixers > old_num_mixers)
  2495. {
  2496. /* GUS has it's own mixer map */
  2497. AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_SYNTH);
  2498. AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_CD);
  2499. AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
  2500. }
  2501. }
  2502. else {
  2503. release_region(gus_base + 0x10c, 4);
  2504. no_cs4231:
  2505. printk(KERN_WARNING "GUS: No CS4231 ??");
  2506. }
  2507. #else
  2508. printk(KERN_ERR "GUS MAX found, but not compiled in\n");
  2509. #endif
  2510. }
  2511. }
  2512. else
  2513. {
  2514. /*
  2515. * ASIC not detected so the card must be 2.2 or 2.4.
  2516. * There could still be the 16-bit/mixer daughter card.
  2517. */
  2518. }
  2519. if (hw_config->name)
  2520. snprintf(tmp, sizeof(tmp), "%s (%dk)", hw_config->name,
  2521. (int) gus_mem_size / 1024);
  2522. else if (gus_pnp_flag)
  2523. snprintf(tmp, sizeof(tmp), "Gravis UltraSound PnP (%dk)",
  2524. (int) gus_mem_size / 1024);
  2525. else
  2526. snprintf(tmp, sizeof(tmp), "Gravis UltraSound %s (%dk)", model_num,
  2527. (int) gus_mem_size / 1024);
  2528. samples = (struct patch_info *)vmalloc((MAX_SAMPLE + 1) * sizeof(*samples));
  2529. if (samples == NULL)
  2530. {
  2531. printk(KERN_WARNING "gus_init: Cant allocate memory for instrument tables\n");
  2532. return;
  2533. }
  2534. conf_printf(tmp, hw_config);
  2535. strlcpy(gus_info.name, tmp, sizeof(gus_info.name));
  2536. if ((sdev = sound_alloc_synthdev()) == -1)
  2537. printk(KERN_WARNING "gus_init: Too many synthesizers\n");
  2538. else
  2539. {
  2540. voice_alloc = &guswave_operations.alloc;
  2541. if (iw_mode)
  2542. guswave_operations.id = "IWAVE";
  2543. hw_config->slots[0] = sdev;
  2544. synth_devs[sdev] = &guswave_operations;
  2545. sequencer_init();
  2546. gus_tmr_install(gus_base + 8);
  2547. }
  2548. reset_sample_memory();
  2549. gus_initialize();
  2550. if ((gus_mem_size > 0) && !gus_no_wave_dma)
  2551. {
  2552. hw_config->slots[4] = -1;
  2553. if ((gus_devnum = sound_install_audiodrv(AUDIO_DRIVER_VERSION,
  2554. "Ultrasound",
  2555. &gus_audio_driver,
  2556. sizeof(struct audio_driver),
  2557. NEEDS_RESTART |
  2558. ((!iw_mode && dma2 != dma && dma2 != -1) ?
  2559. DMA_DUPLEX : 0),
  2560. AFMT_U8 | AFMT_S16_LE,
  2561. NULL, dma, dma2)) < 0)
  2562. {
  2563. return;
  2564. }
  2565. hw_config->slots[4] = gus_devnum;
  2566. audio_devs[gus_devnum]->min_fragment = 9; /* 512k */
  2567. audio_devs[gus_devnum]->max_fragment = 11; /* 8k (must match size of bounce_buf */
  2568. audio_devs[gus_devnum]->mixer_dev = -1; /* Next mixer# */
  2569. audio_devs[gus_devnum]->flags |= DMA_HARDSTOP;
  2570. }
  2571. /*
  2572. * Mixer dependent initialization.
  2573. */
  2574. switch (mixer_type)
  2575. {
  2576. case ICS2101:
  2577. gus_mic_vol = gus_line_vol = gus_pcm_volume = 100;
  2578. gus_wave_volume = 90;
  2579. request_region(u_MixSelect, 1, "GUS mixer");
  2580. hw_config->slots[5] = ics2101_mixer_init();
  2581. audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
  2582. return;
  2583. case CS4231:
  2584. /* Initialized elsewhere (ad1848.c) */
  2585. default:
  2586. hw_config->slots[5] = gus_default_mixer_init();
  2587. audio_devs[gus_devnum]->mixer_dev = hw_config->slots[5]; /* Next mixer# */
  2588. return;
  2589. }
  2590. }
  2591. void __exit gus_wave_unload(struct address_info *hw_config)
  2592. {
  2593. #ifdef CONFIG_SOUND_GUSMAX
  2594. if (have_gus_max)
  2595. {
  2596. ad1848_unload(gus_base + 0x10c,
  2597. -gus_irq,
  2598. gus_dma2, /* Playback DMA */
  2599. gus_dma, /* Capture DMA */
  2600. 1); /* Share DMA channels with GF1 */
  2601. }
  2602. #endif
  2603. if (mixer_type == ICS2101)
  2604. {
  2605. release_region(u_MixSelect, 1);
  2606. }
  2607. if (hw_config->slots[0] != -1)
  2608. sound_unload_synthdev(hw_config->slots[0]);
  2609. if (hw_config->slots[1] != -1)
  2610. sound_unload_audiodev(hw_config->slots[1]);
  2611. if (hw_config->slots[2] != -1)
  2612. sound_unload_mididev(hw_config->slots[2]);
  2613. if (hw_config->slots[4] != -1)
  2614. sound_unload_audiodev(hw_config->slots[4]);
  2615. if (hw_config->slots[5] != -1)
  2616. sound_unload_mixerdev(hw_config->slots[5]);
  2617. vfree(samples);
  2618. samples=NULL;
  2619. }
  2620. /* called in interrupt context */
  2621. static void do_loop_irq(int voice)
  2622. {
  2623. unsigned char tmp;
  2624. int mode, parm;
  2625. spin_lock(&gus_lock);
  2626. gus_select_voice(voice);
  2627. tmp = gus_read8(0x00);
  2628. tmp &= ~0x20; /*
  2629. * Disable wave IRQ for this_one voice
  2630. */
  2631. gus_write8(0x00, tmp);
  2632. if (tmp & 0x03) /* Voice stopped */
  2633. voice_alloc->map[voice] = 0;
  2634. mode = voices[voice].loop_irq_mode;
  2635. voices[voice].loop_irq_mode = 0;
  2636. parm = voices[voice].loop_irq_parm;
  2637. switch (mode)
  2638. {
  2639. case LMODE_FINISH: /*
  2640. * Final loop finished, shoot volume down
  2641. */
  2642. if ((int) (gus_read16(0x09) >> 4) < 100) /*
  2643. * Get current volume
  2644. */
  2645. {
  2646. gus_voice_off();
  2647. gus_rampoff();
  2648. gus_voice_init(voice);
  2649. break;
  2650. }
  2651. gus_ramp_range(65, 4065);
  2652. gus_ramp_rate(0, 63); /*
  2653. * Fastest possible rate
  2654. */
  2655. gus_rampon(0x20 | 0x40); /*
  2656. * Ramp down, once, irq
  2657. */
  2658. voices[voice].volume_irq_mode = VMODE_HALT;
  2659. break;
  2660. case LMODE_PCM_STOP:
  2661. pcm_active = 0; /* Signal to the play_next_pcm_block routine */
  2662. case LMODE_PCM:
  2663. {
  2664. pcm_qlen--;
  2665. pcm_head = (pcm_head + 1) % pcm_nblk;
  2666. if (pcm_qlen && pcm_active)
  2667. {
  2668. play_next_pcm_block();
  2669. }
  2670. else
  2671. {
  2672. /* Underrun. Just stop the voice */
  2673. gus_select_voice(0); /* Left channel */
  2674. gus_voice_off();
  2675. gus_rampoff();
  2676. gus_select_voice(1); /* Right channel */
  2677. gus_voice_off();
  2678. gus_rampoff();
  2679. pcm_active = 0;
  2680. }
  2681. /*
  2682. * If the queue was full before this interrupt, the DMA transfer was
  2683. * suspended. Let it continue now.
  2684. */
  2685. if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
  2686. DMAbuf_outputintr(gus_devnum, 0);
  2687. }
  2688. break;
  2689. default:
  2690. break;
  2691. }
  2692. spin_unlock(&gus_lock);
  2693. }
  2694. static void do_volume_irq(int voice)
  2695. {
  2696. unsigned char tmp;
  2697. int mode, parm;
  2698. unsigned long flags;
  2699. spin_lock_irqsave(&gus_lock,flags);
  2700. gus_select_voice(voice);
  2701. tmp = gus_read8(0x0d);
  2702. tmp &= ~0x20; /*
  2703. * Disable volume ramp IRQ
  2704. */
  2705. gus_write8(0x0d, tmp);
  2706. mode = voices[voice].volume_irq_mode;
  2707. voices[voice].volume_irq_mode = 0;
  2708. parm = voices[voice].volume_irq_parm;
  2709. switch (mode)
  2710. {
  2711. case VMODE_HALT: /* Decay phase finished */
  2712. if (iw_mode)
  2713. gus_write8(0x15, 0x02); /* Set voice deactivate bit of SMSI */
  2714. spin_unlock_irqrestore(&gus_lock,flags);
  2715. gus_voice_init(voice);
  2716. break;
  2717. case VMODE_ENVELOPE:
  2718. gus_rampoff();
  2719. spin_unlock_irqrestore(&gus_lock,flags);
  2720. step_envelope(voice);
  2721. break;
  2722. case VMODE_START_NOTE:
  2723. spin_unlock_irqrestore(&gus_lock,flags);
  2724. guswave_start_note2(voices[voice].dev_pending, voice,
  2725. voices[voice].note_pending, voices[voice].volume_pending);
  2726. if (voices[voice].kill_pending)
  2727. guswave_kill_note(voices[voice].dev_pending, voice,
  2728. voices[voice].note_pending, 0);
  2729. if (voices[voice].sample_pending >= 0)
  2730. {
  2731. guswave_set_instr(voices[voice].dev_pending, voice,
  2732. voices[voice].sample_pending);
  2733. voices[voice].sample_pending = -1;
  2734. }
  2735. break;
  2736. default:
  2737. spin_unlock_irqrestore(&gus_lock,flags);
  2738. }
  2739. }
  2740. /* called in irq context */
  2741. void gus_voice_irq(void)
  2742. {
  2743. unsigned long wave_ignore = 0, volume_ignore = 0;
  2744. unsigned long voice_bit;
  2745. unsigned char src, voice;
  2746. while (1)
  2747. {
  2748. src = gus_read8(0x0f); /*
  2749. * Get source info
  2750. */
  2751. voice = src & 0x1f;
  2752. src &= 0xc0;
  2753. if (src == (0x80 | 0x40))
  2754. return; /*
  2755. * No interrupt
  2756. */
  2757. voice_bit = 1 << voice;
  2758. if (!(src & 0x80)) /*
  2759. * Wave IRQ pending
  2760. */
  2761. if (!(wave_ignore & voice_bit) && (int) voice < nr_voices) /*
  2762. * Not done
  2763. * yet
  2764. */
  2765. {
  2766. wave_ignore |= voice_bit;
  2767. do_loop_irq(voice);
  2768. }
  2769. if (!(src & 0x40)) /*
  2770. * Volume IRQ pending
  2771. */
  2772. if (!(volume_ignore & voice_bit) && (int) voice < nr_voices) /*
  2773. * Not done
  2774. * yet
  2775. */
  2776. {
  2777. volume_ignore |= voice_bit;
  2778. do_volume_irq(voice);
  2779. }
  2780. }
  2781. }
  2782. void guswave_dma_irq(void)
  2783. {
  2784. unsigned char status;
  2785. status = gus_look8(0x41); /* Get DMA IRQ Status */
  2786. if (status & 0x40) /* DMA interrupt pending */
  2787. switch (active_device)
  2788. {
  2789. case GUS_DEV_WAVE:
  2790. wake_up(&dram_sleeper);
  2791. break;
  2792. case GUS_DEV_PCM_CONTINUE: /* Left channel data transferred */
  2793. gus_write8(0x41, 0); /* Disable GF1 DMA */
  2794. gus_transfer_output_block(pcm_current_dev, pcm_current_buf,
  2795. pcm_current_count,
  2796. pcm_current_intrflag, 1);
  2797. break;
  2798. case GUS_DEV_PCM_DONE: /* Right or mono channel data transferred */
  2799. gus_write8(0x41, 0); /* Disable GF1 DMA */
  2800. if (pcm_qlen < pcm_nblk)
  2801. {
  2802. dma_active = 0;
  2803. if (gus_busy)
  2804. {
  2805. if (audio_devs[gus_devnum]->dmap_out->qlen > 0)
  2806. DMAbuf_outputintr(gus_devnum, 0);
  2807. }
  2808. }
  2809. break;
  2810. default:
  2811. break;
  2812. }
  2813. status = gus_look8(0x49); /*
  2814. * Get Sampling IRQ Status
  2815. */
  2816. if (status & 0x40) /*
  2817. * Sampling Irq pending
  2818. */
  2819. {
  2820. DMAbuf_inputintr(gus_devnum);
  2821. }
  2822. }
  2823. /*
  2824. * Timer stuff
  2825. */
  2826. static volatile int select_addr, data_addr;
  2827. static volatile int curr_timer;
  2828. void gus_timer_command(unsigned int addr, unsigned int val)
  2829. {
  2830. int i;
  2831. outb(((unsigned char) (addr & 0xff)), select_addr);
  2832. for (i = 0; i < 2; i++)
  2833. inb(select_addr);
  2834. outb(((unsigned char) (val & 0xff)), data_addr);
  2835. for (i = 0; i < 2; i++)
  2836. inb(select_addr);
  2837. }
  2838. static void arm_timer(int timer, unsigned int interval)
  2839. {
  2840. curr_timer = timer;
  2841. if (timer == 1)
  2842. {
  2843. gus_write8(0x46, 256 - interval); /* Set counter for timer 1 */
  2844. gus_write8(0x45, 0x04); /* Enable timer 1 IRQ */
  2845. gus_timer_command(0x04, 0x01); /* Start timer 1 */
  2846. }
  2847. else
  2848. {
  2849. gus_write8(0x47, 256 - interval); /* Set counter for timer 2 */
  2850. gus_write8(0x45, 0x08); /* Enable timer 2 IRQ */
  2851. gus_timer_command(0x04, 0x02); /* Start timer 2 */
  2852. }
  2853. gus_timer_enabled = 1;
  2854. }
  2855. static unsigned int gus_tmr_start(int dev, unsigned int usecs_per_tick)
  2856. {
  2857. int timer_no, resolution;
  2858. int divisor;
  2859. if (usecs_per_tick > (256 * 80))
  2860. {
  2861. timer_no = 2;
  2862. resolution = 320; /* usec */
  2863. }
  2864. else
  2865. {
  2866. timer_no = 1;
  2867. resolution = 80; /* usec */
  2868. }
  2869. divisor = (usecs_per_tick + (resolution / 2)) / resolution;
  2870. arm_timer(timer_no, divisor);
  2871. return divisor * resolution;
  2872. }
  2873. static void gus_tmr_disable(int dev)
  2874. {
  2875. gus_write8(0x45, 0); /* Disable both timers */
  2876. gus_timer_enabled = 0;
  2877. }
  2878. static void gus_tmr_restart(int dev)
  2879. {
  2880. if (curr_timer == 1)
  2881. gus_write8(0x45, 0x04); /* Start timer 1 again */
  2882. else
  2883. gus_write8(0x45, 0x08); /* Start timer 2 again */
  2884. gus_timer_enabled = 1;
  2885. }
  2886. static struct sound_lowlev_timer gus_tmr =
  2887. {
  2888. 0,
  2889. 1,
  2890. gus_tmr_start,
  2891. gus_tmr_disable,
  2892. gus_tmr_restart
  2893. };
  2894. static void gus_tmr_install(int io_base)
  2895. {
  2896. struct sound_lowlev_timer *tmr;
  2897. select_addr = io_base;
  2898. data_addr = io_base + 1;
  2899. tmr = &gus_tmr;
  2900. #ifdef THIS_GETS_FIXED
  2901. sound_timer_init(&gus_tmr, "GUS");
  2902. #endif
  2903. }