wavfront.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  1. /* -*- linux-c -*-
  2. *
  3. * sound/wavfront.c
  4. *
  5. * A Linux driver for Turtle Beach WaveFront Series (Maui, Tropez, Tropez Plus)
  6. *
  7. * This driver supports the onboard wavetable synthesizer (an ICS2115),
  8. * including patch, sample and program loading and unloading, conversion
  9. * of GUS patches during loading, and full user-level access to all
  10. * WaveFront commands. It tries to provide semi-intelligent patch and
  11. * sample management as well.
  12. *
  13. * It also provides support for the ICS emulation of an MPU-401. Full
  14. * support for the ICS emulation's "virtual MIDI mode" is provided in
  15. * wf_midi.c.
  16. *
  17. * Support is also provided for the Tropez Plus' onboard FX processor,
  18. * a Yamaha YSS225. Currently, code exists to configure the YSS225,
  19. * and there is an interface allowing tweaking of any of its memory
  20. * addresses. However, I have been unable to decipher the logical
  21. * positioning of the configuration info for various effects, so for
  22. * now, you just get the YSS225 in the same state as Turtle Beach's
  23. * "SETUPSND.EXE" utility leaves it.
  24. *
  25. * The boards' DAC/ADC (a Crystal CS4232) is supported by cs4232.[co],
  26. * This chip also controls the configuration of the card: the wavefront
  27. * synth is logical unit 4.
  28. *
  29. *
  30. * Supported devices:
  31. *
  32. * /dev/dsp - using cs4232+ad1848 modules, OSS compatible
  33. * /dev/midiNN and /dev/midiNN+1 - using wf_midi code, OSS compatible
  34. * /dev/synth00 - raw synth interface
  35. *
  36. **********************************************************************
  37. *
  38. * Copyright (C) by Paul Barton-Davis 1998
  39. *
  40. * Some portions of this file are taken from work that is
  41. * copyright (C) by Hannu Savolainen 1993-1996
  42. *
  43. * Although the relevant code here is all new, the handling of
  44. * sample/alias/multi- samples is entirely based on a driver by Matt
  45. * Martin and Rutger Nijlunsing which demonstrated how to get things
  46. * to work correctly. The GUS patch loading code has been almost
  47. * unaltered by me, except to fit formatting and function names in the
  48. * rest of the file. Many thanks to them.
  49. *
  50. * Appreciation and thanks to Hannu Savolainen for his early work on the Maui
  51. * driver, and answering a few questions while this one was developed.
  52. *
  53. * Absolutely NO thanks to Turtle Beach/Voyetra and Yamaha for their
  54. * complete lack of help in developing this driver, and in particular
  55. * for their utter silence in response to questions about undocumented
  56. * aspects of configuring a WaveFront soundcard, particularly the
  57. * effects processor.
  58. *
  59. * $Id: wavfront.c,v 0.7 1998/09/09 15:47:36 pbd Exp $
  60. *
  61. * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  62. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  63. * for more info.
  64. *
  65. * Changes:
  66. * 11-10-2000 Bartlomiej Zolnierkiewicz <bkz@linux-ide.org>
  67. * Added some __init and __initdata to entries in yss225.c
  68. */
  69. #include <linux/module.h>
  70. #include <linux/kernel.h>
  71. #include <linux/init.h>
  72. #include <linux/sched.h>
  73. #include <linux/smp_lock.h>
  74. #include <linux/ptrace.h>
  75. #include <linux/fcntl.h>
  76. #include <linux/syscalls.h>
  77. #include <linux/ioport.h>
  78. #include <linux/spinlock.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/config.h>
  81. #include <linux/delay.h>
  82. #include "sound_config.h"
  83. #include <linux/wavefront.h>
  84. #define _MIDI_SYNTH_C_
  85. #define MIDI_SYNTH_NAME "WaveFront MIDI"
  86. #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT
  87. #include "midi_synth.h"
  88. /* Compile-time control of the extent to which OSS is supported.
  89. I consider /dev/sequencer to be an anachronism, but given its
  90. widespread usage by various Linux MIDI software, it seems worth
  91. offering support to it if it's not too painful. Instead of using
  92. /dev/sequencer, I recommend:
  93. for synth programming and patch loading: /dev/synthNN
  94. for kernel-synchronized MIDI sequencing: the ALSA sequencer
  95. for direct MIDI control: /dev/midiNN
  96. I have never tried static compilation into the kernel. The #if's
  97. for this are really just notes to myself about what the code is
  98. for.
  99. */
  100. #define OSS_SUPPORT_SEQ 0x1 /* use of /dev/sequencer */
  101. #define OSS_SUPPORT_STATIC_INSTALL 0x2 /* static compilation into kernel */
  102. #define OSS_SUPPORT_LEVEL 0x1 /* just /dev/sequencer for now */
  103. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  104. static int (*midi_load_patch) (int devno, int format, const char __user *addr,
  105. int offs, int count, int pmgr_flag) = NULL;
  106. #endif /* OSS_SUPPORT_SEQ */
  107. /* if WF_DEBUG not defined, no run-time debugging messages will
  108. be available via the debug flag setting. Given the current
  109. beta state of the driver, this will remain set until a future
  110. version.
  111. */
  112. #define WF_DEBUG 1
  113. #ifdef WF_DEBUG
  114. /* Thank goodness for gcc's preprocessor ... */
  115. #define DPRINT(cond, format, args...) \
  116. if ((dev.debug & (cond)) == (cond)) { \
  117. printk (KERN_DEBUG LOGNAME format, ## args); \
  118. }
  119. #else
  120. #define DPRINT(cond, format, args...)
  121. #endif
  122. #define LOGNAME "WaveFront: "
  123. /* bitmasks for WaveFront status port value */
  124. #define STAT_RINTR_ENABLED 0x01
  125. #define STAT_CAN_READ 0x02
  126. #define STAT_INTR_READ 0x04
  127. #define STAT_WINTR_ENABLED 0x10
  128. #define STAT_CAN_WRITE 0x20
  129. #define STAT_INTR_WRITE 0x40
  130. /*** Module-accessible parameters ***************************************/
  131. static int wf_raw; /* we normally check for "raw state" to firmware
  132. loading. if set, then during driver loading, the
  133. state of the board is ignored, and we reset the
  134. board and load the firmware anyway.
  135. */
  136. static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
  137. whatever state it is when the driver is loaded.
  138. The default is to download the microprogram and
  139. associated coefficients to set it up for "default"
  140. operation, whatever that means.
  141. */
  142. static int debug_default; /* you can set this to control debugging
  143. during driver loading. it takes any combination
  144. of the WF_DEBUG_* flags defined in
  145. wavefront.h
  146. */
  147. /* XXX this needs to be made firmware and hardware version dependent */
  148. static char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
  149. version of the WaveFront OS
  150. */
  151. static int wait_polls = 2000; /* This is a number of tries we poll the
  152. status register before resorting to sleeping.
  153. WaveFront being an ISA card each poll takes
  154. about 1.2us. So before going to
  155. sleep we wait up to 2.4ms in a loop.
  156. */
  157. static int sleep_length = HZ/100; /* This says how long we're going to
  158. sleep between polls.
  159. 10ms sounds reasonable for fast response.
  160. */
  161. static int sleep_tries = 50; /* Wait for status 0.5 seconds total. */
  162. static int reset_time = 2; /* hundreths of a second we wait after a HW reset for
  163. the expected interrupt.
  164. */
  165. static int ramcheck_time = 20; /* time in seconds to wait while ROM code
  166. checks on-board RAM.
  167. */
  168. static int osrun_time = 10; /* time in seconds we wait for the OS to
  169. start running.
  170. */
  171. module_param(wf_raw, int, 0);
  172. module_param(fx_raw, int, 0);
  173. module_param(debug_default, int, 0);
  174. module_param(wait_polls, int, 0);
  175. module_param(sleep_length, int, 0);
  176. module_param(sleep_tries, int, 0);
  177. module_param(ospath, charp, 0);
  178. module_param(reset_time, int, 0);
  179. module_param(ramcheck_time, int, 0);
  180. module_param(osrun_time, int, 0);
  181. /***************************************************************************/
  182. /* Note: because this module doesn't export any symbols, this really isn't
  183. a global variable, even if it looks like one. I was quite confused by
  184. this when I started writing this as a (newer) module -- pbd.
  185. */
  186. struct wf_config {
  187. int devno; /* device number from kernel */
  188. int irq; /* "you were one, one of the few ..." */
  189. int base; /* low i/o port address */
  190. #define mpu_data_port base
  191. #define mpu_command_port base + 1 /* write semantics */
  192. #define mpu_status_port base + 1 /* read semantics */
  193. #define data_port base + 2
  194. #define status_port base + 3 /* read semantics */
  195. #define control_port base + 3 /* write semantics */
  196. #define block_port base + 4 /* 16 bit, writeonly */
  197. #define last_block_port base + 6 /* 16 bit, writeonly */
  198. /* FX ports. These are mapped through the ICS2115 to the YS225.
  199. The ICS2115 takes care of flipping the relevant pins on the
  200. YS225 so that access to each of these ports does the right
  201. thing. Note: these are NOT documented by Turtle Beach.
  202. */
  203. #define fx_status base + 8
  204. #define fx_op base + 8
  205. #define fx_lcr base + 9
  206. #define fx_dsp_addr base + 0xa
  207. #define fx_dsp_page base + 0xb
  208. #define fx_dsp_lsb base + 0xc
  209. #define fx_dsp_msb base + 0xd
  210. #define fx_mod_addr base + 0xe
  211. #define fx_mod_data base + 0xf
  212. volatile int irq_ok; /* set by interrupt handler */
  213. volatile int irq_cnt; /* ditto */
  214. int opened; /* flag, holds open(2) mode */
  215. char debug; /* debugging flags */
  216. int freemem; /* installed RAM, in bytes */
  217. int synth_dev; /* devno for "raw" synth */
  218. int mididev; /* devno for internal MIDI */
  219. int ext_mididev; /* devno for external MIDI */
  220. int fx_mididev; /* devno for FX MIDI interface */
  221. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  222. int oss_dev; /* devno for OSS sequencer synth */
  223. #endif /* OSS_SUPPORT_SEQ */
  224. char fw_version[2]; /* major = [0], minor = [1] */
  225. char hw_version[2]; /* major = [0], minor = [1] */
  226. char israw; /* needs Motorola microcode */
  227. char has_fx; /* has FX processor (Tropez+) */
  228. char prog_status[WF_MAX_PROGRAM]; /* WF_SLOT_* */
  229. char patch_status[WF_MAX_PATCH]; /* WF_SLOT_* */
  230. char sample_status[WF_MAX_SAMPLE]; /* WF_ST_* | WF_SLOT_* */
  231. int samples_used; /* how many */
  232. char interrupts_on; /* h/w MPU interrupts enabled ? */
  233. char rom_samples_rdonly; /* can we write on ROM samples */
  234. wait_queue_head_t interrupt_sleeper;
  235. } dev;
  236. static DEFINE_SPINLOCK(lock);
  237. static int detect_wffx(void);
  238. static int wffx_ioctl (wavefront_fx_info *);
  239. static int wffx_init (void);
  240. static int wavefront_delete_sample (int sampnum);
  241. static int wavefront_find_free_sample (void);
  242. /* From wf_midi.c */
  243. extern int virtual_midi_enable (void);
  244. extern int virtual_midi_disable (void);
  245. extern int detect_wf_mpu (int, int);
  246. extern int install_wf_mpu (void);
  247. extern int uninstall_wf_mpu (void);
  248. typedef struct {
  249. int cmd;
  250. char *action;
  251. unsigned int read_cnt;
  252. unsigned int write_cnt;
  253. int need_ack;
  254. } wavefront_command;
  255. static struct {
  256. int errno;
  257. const char *errstr;
  258. } wavefront_errors[] = {
  259. { 0x01, "Bad sample number" },
  260. { 0x02, "Out of sample memory" },
  261. { 0x03, "Bad patch number" },
  262. { 0x04, "Error in number of voices" },
  263. { 0x06, "Sample load already in progress" },
  264. { 0x0B, "No sample load request pending" },
  265. { 0x0E, "Bad MIDI channel number" },
  266. { 0x10, "Download Record Error" },
  267. { 0x80, "Success" },
  268. { 0 }
  269. };
  270. #define NEEDS_ACK 1
  271. static wavefront_command wavefront_commands[] = {
  272. { WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
  273. { WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
  274. { WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
  275. { WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
  276. { WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
  277. { WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
  278. { WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
  279. { WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
  280. { WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
  281. { WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
  282. { WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
  283. { WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
  284. { WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
  285. { WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
  286. { WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
  287. { WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
  288. { WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
  289. { WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
  290. { WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
  291. { WFC_DOWNLOAD_SAMPLE, "download sample",
  292. 0, WF_SAMPLE_BYTES, NEEDS_ACK },
  293. { WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
  294. { WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
  295. 0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
  296. { WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
  297. /* This command requires a variable number of bytes to be written.
  298. There is a hack in wavefront_cmd() to support this. The actual
  299. count is passed in as the read buffer ptr, cast appropriately.
  300. Ugh.
  301. */
  302. { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
  303. /* This one is a hack as well. We just read the first byte of the
  304. response, don't fetch an ACK, and leave the rest to the
  305. calling function. Ugly, ugly, ugly.
  306. */
  307. { WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
  308. { WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
  309. 0, WF_ALIAS_BYTES, NEEDS_ACK },
  310. { WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
  311. { WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
  312. { WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
  313. { WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
  314. { WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
  315. { WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
  316. { WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
  317. { WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
  318. { WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
  319. { WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
  320. NEEDS_ACK},
  321. { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
  322. { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
  323. 0, 1, NEEDS_ACK },
  324. { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
  325. { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
  326. 32, 0, 0 },
  327. { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
  328. { 0x00 }
  329. };
  330. static const char *
  331. wavefront_errorstr (int errnum)
  332. {
  333. int i;
  334. for (i = 0; wavefront_errors[i].errstr; i++) {
  335. if (wavefront_errors[i].errno == errnum) {
  336. return wavefront_errors[i].errstr;
  337. }
  338. }
  339. return "Unknown WaveFront error";
  340. }
  341. static wavefront_command *
  342. wavefront_get_command (int cmd)
  343. {
  344. int i;
  345. for (i = 0; wavefront_commands[i].cmd != 0; i++) {
  346. if (cmd == wavefront_commands[i].cmd) {
  347. return &wavefront_commands[i];
  348. }
  349. }
  350. return (wavefront_command *) 0;
  351. }
  352. static inline int
  353. wavefront_status (void)
  354. {
  355. return inb (dev.status_port);
  356. }
  357. static int
  358. wavefront_wait (int mask)
  359. {
  360. int i;
  361. for (i = 0; i < wait_polls; i++)
  362. if (wavefront_status() & mask)
  363. return 1;
  364. for (i = 0; i < sleep_tries; i++) {
  365. if (wavefront_status() & mask) {
  366. set_current_state(TASK_RUNNING);
  367. return 1;
  368. }
  369. set_current_state(TASK_INTERRUPTIBLE);
  370. schedule_timeout(sleep_length);
  371. if (signal_pending(current))
  372. break;
  373. }
  374. set_current_state(TASK_RUNNING);
  375. return 0;
  376. }
  377. static int
  378. wavefront_read (void)
  379. {
  380. if (wavefront_wait (STAT_CAN_READ))
  381. return inb (dev.data_port);
  382. DPRINT (WF_DEBUG_DATA, "read timeout.\n");
  383. return -1;
  384. }
  385. static int
  386. wavefront_write (unsigned char data)
  387. {
  388. if (wavefront_wait (STAT_CAN_WRITE)) {
  389. outb (data, dev.data_port);
  390. return 0;
  391. }
  392. DPRINT (WF_DEBUG_DATA, "write timeout.\n");
  393. return -1;
  394. }
  395. static int
  396. wavefront_cmd (int cmd, unsigned char *rbuf, unsigned char *wbuf)
  397. {
  398. int ack;
  399. int i;
  400. int c;
  401. wavefront_command *wfcmd;
  402. if ((wfcmd = wavefront_get_command (cmd)) == (wavefront_command *) 0) {
  403. printk (KERN_WARNING LOGNAME "command 0x%x not supported.\n",
  404. cmd);
  405. return 1;
  406. }
  407. /* Hack to handle the one variable-size write command. See
  408. wavefront_send_multisample() for the other half of this
  409. gross and ugly strategy.
  410. */
  411. if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
  412. wfcmd->write_cnt = (unsigned int) rbuf;
  413. rbuf = NULL;
  414. }
  415. DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
  416. cmd, wfcmd->action, wfcmd->read_cnt,
  417. wfcmd->write_cnt, wfcmd->need_ack);
  418. if (wavefront_write (cmd)) {
  419. DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
  420. "0x%x [%s].\n",
  421. cmd, wfcmd->action);
  422. return 1;
  423. }
  424. if (wfcmd->write_cnt > 0) {
  425. DPRINT (WF_DEBUG_DATA, "writing %d bytes "
  426. "for 0x%x\n",
  427. wfcmd->write_cnt, cmd);
  428. for (i = 0; i < wfcmd->write_cnt; i++) {
  429. if (wavefront_write (wbuf[i])) {
  430. DPRINT (WF_DEBUG_IO, "bad write for byte "
  431. "%d of 0x%x [%s].\n",
  432. i, cmd, wfcmd->action);
  433. return 1;
  434. }
  435. DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
  436. i, wbuf[i]);
  437. }
  438. }
  439. if (wfcmd->read_cnt > 0) {
  440. DPRINT (WF_DEBUG_DATA, "reading %d ints "
  441. "for 0x%x\n",
  442. wfcmd->read_cnt, cmd);
  443. for (i = 0; i < wfcmd->read_cnt; i++) {
  444. if ((c = wavefront_read()) == -1) {
  445. DPRINT (WF_DEBUG_IO, "bad read for byte "
  446. "%d of 0x%x [%s].\n",
  447. i, cmd, wfcmd->action);
  448. return 1;
  449. }
  450. /* Now handle errors. Lots of special cases here */
  451. if (c == 0xff) {
  452. if ((c = wavefront_read ()) == -1) {
  453. DPRINT (WF_DEBUG_IO, "bad read for "
  454. "error byte at "
  455. "read byte %d "
  456. "of 0x%x [%s].\n",
  457. i, cmd,
  458. wfcmd->action);
  459. return 1;
  460. }
  461. /* Can you believe this madness ? */
  462. if (c == 1 &&
  463. wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
  464. rbuf[0] = WF_ST_EMPTY;
  465. return (0);
  466. } else if (c == 3 &&
  467. wfcmd->cmd == WFC_UPLOAD_PATCH) {
  468. return 3;
  469. } else if (c == 1 &&
  470. wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
  471. return 1;
  472. } else {
  473. DPRINT (WF_DEBUG_IO, "error %d (%s) "
  474. "during "
  475. "read for byte "
  476. "%d of 0x%x "
  477. "[%s].\n",
  478. c,
  479. wavefront_errorstr (c),
  480. i, cmd,
  481. wfcmd->action);
  482. return 1;
  483. }
  484. } else {
  485. rbuf[i] = c;
  486. }
  487. DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
  488. }
  489. }
  490. if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
  491. DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
  492. /* Some commands need an ACK, but return zero instead
  493. of the standard value.
  494. */
  495. if ((ack = wavefront_read()) == 0) {
  496. ack = WF_ACK;
  497. }
  498. if (ack != WF_ACK) {
  499. if (ack == -1) {
  500. DPRINT (WF_DEBUG_IO, "cannot read ack for "
  501. "0x%x [%s].\n",
  502. cmd, wfcmd->action);
  503. return 1;
  504. } else {
  505. int err = -1; /* something unknown */
  506. if (ack == 0xff) { /* explicit error */
  507. if ((err = wavefront_read ()) == -1) {
  508. DPRINT (WF_DEBUG_DATA,
  509. "cannot read err "
  510. "for 0x%x [%s].\n",
  511. cmd, wfcmd->action);
  512. }
  513. }
  514. DPRINT (WF_DEBUG_IO, "0x%x [%s] "
  515. "failed (0x%x, 0x%x, %s)\n",
  516. cmd, wfcmd->action, ack, err,
  517. wavefront_errorstr (err));
  518. return -err;
  519. }
  520. }
  521. DPRINT (WF_DEBUG_DATA, "ack received "
  522. "for 0x%x [%s]\n",
  523. cmd, wfcmd->action);
  524. } else {
  525. DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
  526. "ACK (%d,%d,%d)\n",
  527. cmd, wfcmd->action, wfcmd->read_cnt,
  528. wfcmd->write_cnt, wfcmd->need_ack);
  529. }
  530. return 0;
  531. }
  532. /***********************************************************************
  533. WaveFront: data munging
  534. Things here are weird. All data written to the board cannot
  535. have its most significant bit set. Any data item with values
  536. potentially > 0x7F (127) must be split across multiple bytes.
  537. Sometimes, we need to munge numeric values that are represented on
  538. the x86 side as 8-32 bit values. Sometimes, we need to munge data
  539. that is represented on the x86 side as an array of bytes. The most
  540. efficient approach to handling both cases seems to be to use 2
  541. different functions for munging and 2 for de-munging. This avoids
  542. weird casting and worrying about bit-level offsets.
  543. **********************************************************************/
  544. static
  545. unsigned char *
  546. munge_int32 (unsigned int src,
  547. unsigned char *dst,
  548. unsigned int dst_size)
  549. {
  550. int i;
  551. for (i = 0;i < dst_size; i++) {
  552. *dst = src & 0x7F; /* Mask high bit of LSB */
  553. src = src >> 7; /* Rotate Right 7 bits */
  554. /* Note: we leave the upper bits in place */
  555. dst++;
  556. };
  557. return dst;
  558. };
  559. static int
  560. demunge_int32 (unsigned char* src, int src_size)
  561. {
  562. int i;
  563. int outval = 0;
  564. for (i = src_size - 1; i >= 0; i--) {
  565. outval=(outval<<7)+src[i];
  566. }
  567. return outval;
  568. };
  569. static
  570. unsigned char *
  571. munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
  572. {
  573. int i;
  574. unsigned int last = dst_size / 2;
  575. for (i = 0; i < last; i++) {
  576. *dst++ = src[i] & 0x7f;
  577. *dst++ = src[i] >> 7;
  578. }
  579. return dst;
  580. }
  581. static
  582. unsigned char *
  583. demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
  584. {
  585. int i;
  586. unsigned char *end = src + src_bytes;
  587. end = src + src_bytes;
  588. /* NOTE: src and dst *CAN* point to the same address */
  589. for (i = 0; src != end; i++) {
  590. dst[i] = *src++;
  591. dst[i] |= (*src++)<<7;
  592. }
  593. return dst;
  594. }
  595. /***********************************************************************
  596. WaveFront: sample, patch and program management.
  597. ***********************************************************************/
  598. static int
  599. wavefront_delete_sample (int sample_num)
  600. {
  601. unsigned char wbuf[2];
  602. int x;
  603. wbuf[0] = sample_num & 0x7f;
  604. wbuf[1] = sample_num >> 7;
  605. if ((x = wavefront_cmd (WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {
  606. dev.sample_status[sample_num] = WF_ST_EMPTY;
  607. }
  608. return x;
  609. }
  610. static int
  611. wavefront_get_sample_status (int assume_rom)
  612. {
  613. int i;
  614. unsigned char rbuf[32], wbuf[32];
  615. unsigned int sc_real, sc_alias, sc_multi;
  616. /* check sample status */
  617. if (wavefront_cmd (WFC_GET_NSAMPLES, rbuf, wbuf)) {
  618. printk (KERN_WARNING LOGNAME "cannot request sample count.\n");
  619. return -1;
  620. }
  621. sc_real = sc_alias = sc_multi = dev.samples_used = 0;
  622. for (i = 0; i < WF_MAX_SAMPLE; i++) {
  623. wbuf[0] = i & 0x7f;
  624. wbuf[1] = i >> 7;
  625. if (wavefront_cmd (WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
  626. printk (KERN_WARNING LOGNAME
  627. "cannot identify sample "
  628. "type of slot %d\n", i);
  629. dev.sample_status[i] = WF_ST_EMPTY;
  630. continue;
  631. }
  632. dev.sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
  633. if (assume_rom) {
  634. dev.sample_status[i] |= WF_SLOT_ROM;
  635. }
  636. switch (rbuf[0] & WF_ST_MASK) {
  637. case WF_ST_SAMPLE:
  638. sc_real++;
  639. break;
  640. case WF_ST_MULTISAMPLE:
  641. sc_multi++;
  642. break;
  643. case WF_ST_ALIAS:
  644. sc_alias++;
  645. break;
  646. case WF_ST_EMPTY:
  647. break;
  648. default:
  649. printk (KERN_WARNING LOGNAME "unknown sample type for "
  650. "slot %d (0x%x)\n",
  651. i, rbuf[0]);
  652. }
  653. if (rbuf[0] != WF_ST_EMPTY) {
  654. dev.samples_used++;
  655. }
  656. }
  657. printk (KERN_INFO LOGNAME
  658. "%d samples used (%d real, %d aliases, %d multi), "
  659. "%d empty\n", dev.samples_used, sc_real, sc_alias, sc_multi,
  660. WF_MAX_SAMPLE - dev.samples_used);
  661. return (0);
  662. }
  663. static int
  664. wavefront_get_patch_status (void)
  665. {
  666. unsigned char patchbuf[WF_PATCH_BYTES];
  667. unsigned char patchnum[2];
  668. wavefront_patch *p;
  669. int i, x, cnt, cnt2;
  670. for (i = 0; i < WF_MAX_PATCH; i++) {
  671. patchnum[0] = i & 0x7f;
  672. patchnum[1] = i >> 7;
  673. if ((x = wavefront_cmd (WFC_UPLOAD_PATCH, patchbuf,
  674. patchnum)) == 0) {
  675. dev.patch_status[i] |= WF_SLOT_FILLED;
  676. p = (wavefront_patch *) patchbuf;
  677. dev.sample_status
  678. [p->sample_number|(p->sample_msb<<7)] |=
  679. WF_SLOT_USED;
  680. } else if (x == 3) { /* Bad patch number */
  681. dev.patch_status[i] = 0;
  682. } else {
  683. printk (KERN_ERR LOGNAME "upload patch "
  684. "error 0x%x\n", x);
  685. dev.patch_status[i] = 0;
  686. return 1;
  687. }
  688. }
  689. /* program status has already filled in slot_used bits */
  690. for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
  691. if (dev.patch_status[i] & WF_SLOT_FILLED) {
  692. cnt++;
  693. }
  694. if (dev.patch_status[i] & WF_SLOT_USED) {
  695. cnt2++;
  696. }
  697. }
  698. printk (KERN_INFO LOGNAME
  699. "%d patch slots filled, %d in use\n", cnt, cnt2);
  700. return (0);
  701. }
  702. static int
  703. wavefront_get_program_status (void)
  704. {
  705. unsigned char progbuf[WF_PROGRAM_BYTES];
  706. wavefront_program prog;
  707. unsigned char prognum;
  708. int i, x, l, cnt;
  709. for (i = 0; i < WF_MAX_PROGRAM; i++) {
  710. prognum = i;
  711. if ((x = wavefront_cmd (WFC_UPLOAD_PROGRAM, progbuf,
  712. &prognum)) == 0) {
  713. dev.prog_status[i] |= WF_SLOT_USED;
  714. demunge_buf (progbuf, (unsigned char *) &prog,
  715. WF_PROGRAM_BYTES);
  716. for (l = 0; l < WF_NUM_LAYERS; l++) {
  717. if (prog.layer[l].mute) {
  718. dev.patch_status
  719. [prog.layer[l].patch_number] |=
  720. WF_SLOT_USED;
  721. }
  722. }
  723. } else if (x == 1) { /* Bad program number */
  724. dev.prog_status[i] = 0;
  725. } else {
  726. printk (KERN_ERR LOGNAME "upload program "
  727. "error 0x%x\n", x);
  728. dev.prog_status[i] = 0;
  729. }
  730. }
  731. for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
  732. if (dev.prog_status[i]) {
  733. cnt++;
  734. }
  735. }
  736. printk (KERN_INFO LOGNAME "%d programs slots in use\n", cnt);
  737. return (0);
  738. }
  739. static int
  740. wavefront_send_patch (wavefront_patch_info *header)
  741. {
  742. unsigned char buf[WF_PATCH_BYTES+2];
  743. unsigned char *bptr;
  744. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
  745. header->number);
  746. dev.patch_status[header->number] |= WF_SLOT_FILLED;
  747. bptr = buf;
  748. bptr = munge_int32 (header->number, buf, 2);
  749. munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
  750. if (wavefront_cmd (WFC_DOWNLOAD_PATCH, NULL, buf)) {
  751. printk (KERN_ERR LOGNAME "download patch failed\n");
  752. return -(EIO);
  753. }
  754. return (0);
  755. }
  756. static int
  757. wavefront_send_program (wavefront_patch_info *header)
  758. {
  759. unsigned char buf[WF_PROGRAM_BYTES+1];
  760. int i;
  761. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
  762. header->number);
  763. dev.prog_status[header->number] = WF_SLOT_USED;
  764. /* XXX need to zero existing SLOT_USED bit for program_status[i]
  765. where `i' is the program that's being (potentially) overwritten.
  766. */
  767. for (i = 0; i < WF_NUM_LAYERS; i++) {
  768. if (header->hdr.pr.layer[i].mute) {
  769. dev.patch_status[header->hdr.pr.layer[i].patch_number] |=
  770. WF_SLOT_USED;
  771. /* XXX need to mark SLOT_USED for sample used by
  772. patch_number, but this means we have to load it. Ick.
  773. */
  774. }
  775. }
  776. buf[0] = header->number;
  777. munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
  778. if (wavefront_cmd (WFC_DOWNLOAD_PROGRAM, NULL, buf)) {
  779. printk (KERN_WARNING LOGNAME "download patch failed\n");
  780. return -(EIO);
  781. }
  782. return (0);
  783. }
  784. static int
  785. wavefront_freemem (void)
  786. {
  787. char rbuf[8];
  788. if (wavefront_cmd (WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {
  789. printk (KERN_WARNING LOGNAME "can't get memory stats.\n");
  790. return -1;
  791. } else {
  792. return demunge_int32 (rbuf, 4);
  793. }
  794. }
  795. static int
  796. wavefront_send_sample (wavefront_patch_info *header,
  797. UINT16 __user *dataptr,
  798. int data_is_unsigned)
  799. {
  800. /* samples are downloaded via a 16-bit wide i/o port
  801. (you could think of it as 2 adjacent 8-bit wide ports
  802. but its less efficient that way). therefore, all
  803. the blocksizes and so forth listed in the documentation,
  804. and used conventionally to refer to sample sizes,
  805. which are given in 8-bit units (bytes), need to be
  806. divided by 2.
  807. */
  808. UINT16 sample_short;
  809. UINT32 length;
  810. UINT16 __user *data_end = NULL;
  811. unsigned int i;
  812. const int max_blksize = 4096/2;
  813. unsigned int written;
  814. unsigned int blocksize;
  815. int dma_ack;
  816. int blocknum;
  817. unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
  818. unsigned char *shptr;
  819. int skip = 0;
  820. int initial_skip = 0;
  821. DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
  822. "type %d, %d bytes from %p\n",
  823. header->size ? "" : "header ",
  824. header->number, header->subkey,
  825. header->size,
  826. header->dataptr);
  827. if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
  828. int x;
  829. if ((x = wavefront_find_free_sample ()) < 0) {
  830. return -ENOMEM;
  831. }
  832. printk (KERN_DEBUG LOGNAME "unspecified sample => %d\n", x);
  833. header->number = x;
  834. }
  835. if (header->size) {
  836. /* XXX it's a debatable point whether or not RDONLY semantics
  837. on the ROM samples should cover just the sample data or
  838. the sample header. For now, it only covers the sample data,
  839. so anyone is free at all times to rewrite sample headers.
  840. My reason for this is that we have the sample headers
  841. available in the WFB file for General MIDI, and so these
  842. can always be reset if needed. The sample data, however,
  843. cannot be recovered without a complete reset and firmware
  844. reload of the ICS2115, which is a very expensive operation.
  845. So, doing things this way allows us to honor the notion of
  846. "RESETSAMPLES" reasonably cheaply. Note however, that this
  847. is done purely at user level: there is no WFB parser in
  848. this driver, and so a complete reset (back to General MIDI,
  849. or theoretically some other configuration) is the
  850. responsibility of the user level library.
  851. To try to do this in the kernel would be a little
  852. crazy: we'd need 158K of kernel space just to hold
  853. a copy of the patch/program/sample header data.
  854. */
  855. if (dev.rom_samples_rdonly) {
  856. if (dev.sample_status[header->number] & WF_SLOT_ROM) {
  857. printk (KERN_ERR LOGNAME "sample slot %d "
  858. "write protected\n",
  859. header->number);
  860. return -EACCES;
  861. }
  862. }
  863. wavefront_delete_sample (header->number);
  864. }
  865. if (header->size) {
  866. dev.freemem = wavefront_freemem ();
  867. if (dev.freemem < header->size) {
  868. printk (KERN_ERR LOGNAME
  869. "insufficient memory to "
  870. "load %d byte sample.\n",
  871. header->size);
  872. return -ENOMEM;
  873. }
  874. }
  875. skip = WF_GET_CHANNEL(&header->hdr.s);
  876. if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
  877. printk (KERN_ERR LOGNAME "channel selection only "
  878. "possible on 16-bit samples");
  879. return -(EINVAL);
  880. }
  881. switch (skip) {
  882. case 0:
  883. initial_skip = 0;
  884. skip = 1;
  885. break;
  886. case 1:
  887. initial_skip = 0;
  888. skip = 2;
  889. break;
  890. case 2:
  891. initial_skip = 1;
  892. skip = 2;
  893. break;
  894. case 3:
  895. initial_skip = 2;
  896. skip = 3;
  897. break;
  898. case 4:
  899. initial_skip = 3;
  900. skip = 4;
  901. break;
  902. case 5:
  903. initial_skip = 4;
  904. skip = 5;
  905. break;
  906. case 6:
  907. initial_skip = 5;
  908. skip = 6;
  909. break;
  910. }
  911. DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
  912. "initial skip = %d, skip = %d\n",
  913. WF_GET_CHANNEL (&header->hdr.s),
  914. initial_skip, skip);
  915. /* Be safe, and zero the "Unused" bits ... */
  916. WF_SET_CHANNEL(&header->hdr.s, 0);
  917. /* adjust size for 16 bit samples by dividing by two. We always
  918. send 16 bits per write, even for 8 bit samples, so the length
  919. is always half the size of the sample data in bytes.
  920. */
  921. length = header->size / 2;
  922. /* the data we're sent has not been munged, and in fact, the
  923. header we have to send isn't just a munged copy either.
  924. so, build the sample header right here.
  925. */
  926. shptr = &sample_hdr[0];
  927. shptr = munge_int32 (header->number, shptr, 2);
  928. if (header->size) {
  929. shptr = munge_int32 (length, shptr, 4);
  930. }
  931. /* Yes, a 4 byte result doesn't contain all of the offset bits,
  932. but the offset only uses 24 bits.
  933. */
  934. shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleStartOffset),
  935. shptr, 4);
  936. shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopStartOffset),
  937. shptr, 4);
  938. shptr = munge_int32 (*((UINT32 *) &header->hdr.s.loopEndOffset),
  939. shptr, 4);
  940. shptr = munge_int32 (*((UINT32 *) &header->hdr.s.sampleEndOffset),
  941. shptr, 4);
  942. /* This one is truly weird. What kind of weirdo decided that in
  943. a system dominated by 16 and 32 bit integers, they would use
  944. a just 12 bits ?
  945. */
  946. shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
  947. /* Why is this nybblified, when the MSB is *always* zero ?
  948. Anyway, we can't take address of bitfield, so make a
  949. good-faith guess at where it starts.
  950. */
  951. shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
  952. shptr, 2);
  953. if (wavefront_cmd (header->size ?
  954. WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
  955. NULL, sample_hdr)) {
  956. printk (KERN_WARNING LOGNAME "sample %sdownload refused.\n",
  957. header->size ? "" : "header ");
  958. return -(EIO);
  959. }
  960. if (header->size == 0) {
  961. goto sent; /* Sorry. Just had to have one somewhere */
  962. }
  963. data_end = dataptr + length;
  964. /* Do any initial skip over an unused channel's data */
  965. dataptr += initial_skip;
  966. for (written = 0, blocknum = 0;
  967. written < length; written += max_blksize, blocknum++) {
  968. if ((length - written) > max_blksize) {
  969. blocksize = max_blksize;
  970. } else {
  971. /* round to nearest 16-byte value */
  972. blocksize = ((length-written+7)&~0x7);
  973. }
  974. if (wavefront_cmd (WFC_DOWNLOAD_BLOCK, NULL, NULL)) {
  975. printk (KERN_WARNING LOGNAME "download block "
  976. "request refused.\n");
  977. return -(EIO);
  978. }
  979. for (i = 0; i < blocksize; i++) {
  980. if (dataptr < data_end) {
  981. __get_user (sample_short, dataptr);
  982. dataptr += skip;
  983. if (data_is_unsigned) { /* GUS ? */
  984. if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
  985. /* 8 bit sample
  986. resolution, sign
  987. extend both bytes.
  988. */
  989. ((unsigned char*)
  990. &sample_short)[0] += 0x7f;
  991. ((unsigned char*)
  992. &sample_short)[1] += 0x7f;
  993. } else {
  994. /* 16 bit sample
  995. resolution, sign
  996. extend the MSB.
  997. */
  998. sample_short += 0x7fff;
  999. }
  1000. }
  1001. } else {
  1002. /* In padding section of final block:
  1003. Don't fetch unsupplied data from
  1004. user space, just continue with
  1005. whatever the final value was.
  1006. */
  1007. }
  1008. if (i < blocksize - 1) {
  1009. outw (sample_short, dev.block_port);
  1010. } else {
  1011. outw (sample_short, dev.last_block_port);
  1012. }
  1013. }
  1014. /* Get "DMA page acknowledge", even though its really
  1015. nothing to do with DMA at all.
  1016. */
  1017. if ((dma_ack = wavefront_read ()) != WF_DMA_ACK) {
  1018. if (dma_ack == -1) {
  1019. printk (KERN_ERR LOGNAME "upload sample "
  1020. "DMA ack timeout\n");
  1021. return -(EIO);
  1022. } else {
  1023. printk (KERN_ERR LOGNAME "upload sample "
  1024. "DMA ack error 0x%x\n",
  1025. dma_ack);
  1026. return -(EIO);
  1027. }
  1028. }
  1029. }
  1030. dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
  1031. /* Note, label is here because sending the sample header shouldn't
  1032. alter the sample_status info at all.
  1033. */
  1034. sent:
  1035. return (0);
  1036. }
  1037. static int
  1038. wavefront_send_alias (wavefront_patch_info *header)
  1039. {
  1040. unsigned char alias_hdr[WF_ALIAS_BYTES];
  1041. DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
  1042. "alias for %d\n",
  1043. header->number,
  1044. header->hdr.a.OriginalSample);
  1045. munge_int32 (header->number, &alias_hdr[0], 2);
  1046. munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
  1047. munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
  1048. &alias_hdr[4], 4);
  1049. munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
  1050. &alias_hdr[8], 4);
  1051. munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
  1052. &alias_hdr[12], 4);
  1053. munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
  1054. &alias_hdr[16], 4);
  1055. munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
  1056. munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
  1057. if (wavefront_cmd (WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {
  1058. printk (KERN_ERR LOGNAME "download alias failed.\n");
  1059. return -(EIO);
  1060. }
  1061. dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
  1062. return (0);
  1063. }
  1064. static int
  1065. wavefront_send_multisample (wavefront_patch_info *header)
  1066. {
  1067. int i;
  1068. int num_samples;
  1069. unsigned char msample_hdr[WF_MSAMPLE_BYTES];
  1070. munge_int32 (header->number, &msample_hdr[0], 2);
  1071. /* You'll recall at this point that the "number of samples" value
  1072. in a wavefront_multisample struct is actually the log2 of the
  1073. real number of samples.
  1074. */
  1075. num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
  1076. msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
  1077. DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
  1078. header->number,
  1079. header->hdr.ms.NumberOfSamples,
  1080. num_samples);
  1081. for (i = 0; i < num_samples; i++) {
  1082. DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
  1083. i, header->hdr.ms.SampleNumber[i]);
  1084. munge_int32 (header->hdr.ms.SampleNumber[i],
  1085. &msample_hdr[3+(i*2)], 2);
  1086. }
  1087. /* Need a hack here to pass in the number of bytes
  1088. to be written to the synth. This is ugly, and perhaps
  1089. one day, I'll fix it.
  1090. */
  1091. if (wavefront_cmd (WFC_DOWNLOAD_MULTISAMPLE,
  1092. (unsigned char *) ((num_samples*2)+3),
  1093. msample_hdr)) {
  1094. printk (KERN_ERR LOGNAME "download of multisample failed.\n");
  1095. return -(EIO);
  1096. }
  1097. dev.sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
  1098. return (0);
  1099. }
  1100. static int
  1101. wavefront_fetch_multisample (wavefront_patch_info *header)
  1102. {
  1103. int i;
  1104. unsigned char log_ns[1];
  1105. unsigned char number[2];
  1106. int num_samples;
  1107. munge_int32 (header->number, number, 2);
  1108. if (wavefront_cmd (WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
  1109. printk (KERN_ERR LOGNAME "upload multisample failed.\n");
  1110. return -(EIO);
  1111. }
  1112. DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
  1113. header->number, log_ns[0]);
  1114. header->hdr.ms.NumberOfSamples = log_ns[0];
  1115. /* get the number of samples ... */
  1116. num_samples = (1 << log_ns[0]);
  1117. for (i = 0; i < num_samples; i++) {
  1118. s8 d[2];
  1119. if ((d[0] = wavefront_read ()) == -1) {
  1120. printk (KERN_ERR LOGNAME "upload multisample failed "
  1121. "during sample loop.\n");
  1122. return -(EIO);
  1123. }
  1124. if ((d[1] = wavefront_read ()) == -1) {
  1125. printk (KERN_ERR LOGNAME "upload multisample failed "
  1126. "during sample loop.\n");
  1127. return -(EIO);
  1128. }
  1129. header->hdr.ms.SampleNumber[i] =
  1130. demunge_int32 ((unsigned char *) d, 2);
  1131. DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
  1132. i, header->hdr.ms.SampleNumber[i]);
  1133. }
  1134. return (0);
  1135. }
  1136. static int
  1137. wavefront_send_drum (wavefront_patch_info *header)
  1138. {
  1139. unsigned char drumbuf[WF_DRUM_BYTES];
  1140. wavefront_drum *drum = &header->hdr.d;
  1141. int i;
  1142. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
  1143. "note %d, patch = %d\n",
  1144. header->number, drum->PatchNumber);
  1145. drumbuf[0] = header->number & 0x7f;
  1146. for (i = 0; i < 4; i++) {
  1147. munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
  1148. }
  1149. if (wavefront_cmd (WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {
  1150. printk (KERN_ERR LOGNAME "download drum failed.\n");
  1151. return -(EIO);
  1152. }
  1153. return (0);
  1154. }
  1155. static int
  1156. wavefront_find_free_sample (void)
  1157. {
  1158. int i;
  1159. for (i = 0; i < WF_MAX_SAMPLE; i++) {
  1160. if (!(dev.sample_status[i] & WF_SLOT_FILLED)) {
  1161. return i;
  1162. }
  1163. }
  1164. printk (KERN_WARNING LOGNAME "no free sample slots!\n");
  1165. return -1;
  1166. }
  1167. static int
  1168. wavefront_find_free_patch (void)
  1169. {
  1170. int i;
  1171. for (i = 0; i < WF_MAX_PATCH; i++) {
  1172. if (!(dev.patch_status[i] & WF_SLOT_FILLED)) {
  1173. return i;
  1174. }
  1175. }
  1176. printk (KERN_WARNING LOGNAME "no free patch slots!\n");
  1177. return -1;
  1178. }
  1179. static int
  1180. log2_2048(int n)
  1181. {
  1182. int tbl[]={0, 0, 2048, 3246, 4096, 4755, 5294, 5749, 6143,
  1183. 6492, 6803, 7084, 7342, 7578, 7797, 8001, 8192,
  1184. 8371, 8540, 8699, 8851, 8995, 9132, 9264, 9390,
  1185. 9510, 9626, 9738, 9845, 9949, 10049, 10146};
  1186. int i;
  1187. /* Returns 2048*log2(n) */
  1188. /* FIXME: this is like doing integer math
  1189. on quantum particles (RuN) */
  1190. i=0;
  1191. while(n>=32*256) {
  1192. n>>=8;
  1193. i+=2048*8;
  1194. }
  1195. while(n>=32) {
  1196. n>>=1;
  1197. i+=2048;
  1198. }
  1199. i+=tbl[n];
  1200. return(i);
  1201. }
  1202. static int
  1203. wavefront_load_gus_patch (int devno, int format, const char __user *addr,
  1204. int offs, int count, int pmgr_flag)
  1205. {
  1206. struct patch_info guspatch;
  1207. wavefront_patch_info *samp, *pat, *prog;
  1208. wavefront_patch *patp;
  1209. wavefront_sample *sampp;
  1210. wavefront_program *progp;
  1211. int i,base_note;
  1212. long sizeof_patch;
  1213. int rc = -ENOMEM;
  1214. samp = kmalloc(3 * sizeof(wavefront_patch_info), GFP_KERNEL);
  1215. if (!samp)
  1216. goto free_fail;
  1217. pat = samp + 1;
  1218. prog = pat + 1;
  1219. /* Copy in the header of the GUS patch */
  1220. sizeof_patch = (long) &guspatch.data[0] - (long) &guspatch;
  1221. if (copy_from_user(&((char *) &guspatch)[offs],
  1222. &(addr)[offs], sizeof_patch - offs)) {
  1223. rc = -EFAULT;
  1224. goto free_fail;
  1225. }
  1226. if ((i = wavefront_find_free_patch ()) == -1) {
  1227. rc = -EBUSY;
  1228. goto free_fail;
  1229. }
  1230. pat->number = i;
  1231. pat->subkey = WF_ST_PATCH;
  1232. patp = &pat->hdr.p;
  1233. if ((i = wavefront_find_free_sample ()) == -1) {
  1234. rc = -EBUSY;
  1235. goto free_fail;
  1236. }
  1237. samp->number = i;
  1238. samp->subkey = WF_ST_SAMPLE;
  1239. samp->size = guspatch.len;
  1240. sampp = &samp->hdr.s;
  1241. prog->number = guspatch.instr_no;
  1242. progp = &prog->hdr.pr;
  1243. /* Setup the patch structure */
  1244. patp->amplitude_bias=guspatch.volume;
  1245. patp->portamento=0;
  1246. patp->sample_number= samp->number & 0xff;
  1247. patp->sample_msb= samp->number >> 8;
  1248. patp->pitch_bend= /*12*/ 0;
  1249. patp->mono=1;
  1250. patp->retrigger=1;
  1251. patp->nohold=(guspatch.mode & WAVE_SUSTAIN_ON) ? 0:1;
  1252. patp->frequency_bias=0;
  1253. patp->restart=0;
  1254. patp->reuse=0;
  1255. patp->reset_lfo=1;
  1256. patp->fm_src2=0;
  1257. patp->fm_src1=WF_MOD_MOD_WHEEL;
  1258. patp->am_src=WF_MOD_PRESSURE;
  1259. patp->am_amount=127;
  1260. patp->fc1_mod_amount=0;
  1261. patp->fc2_mod_amount=0;
  1262. patp->fm_amount1=0;
  1263. patp->fm_amount2=0;
  1264. patp->envelope1.attack_level=127;
  1265. patp->envelope1.decay1_level=127;
  1266. patp->envelope1.decay2_level=127;
  1267. patp->envelope1.sustain_level=127;
  1268. patp->envelope1.release_level=0;
  1269. patp->envelope2.attack_velocity=127;
  1270. patp->envelope2.attack_level=127;
  1271. patp->envelope2.decay1_level=127;
  1272. patp->envelope2.decay2_level=127;
  1273. patp->envelope2.sustain_level=127;
  1274. patp->envelope2.release_level=0;
  1275. patp->envelope2.attack_velocity=127;
  1276. patp->randomizer=0;
  1277. /* Program for this patch */
  1278. progp->layer[0].patch_number= pat->number; /* XXX is this right ? */
  1279. progp->layer[0].mute=1;
  1280. progp->layer[0].pan_or_mod=1;
  1281. progp->layer[0].pan=7;
  1282. progp->layer[0].mix_level=127 /* guspatch.volume */;
  1283. progp->layer[0].split_type=0;
  1284. progp->layer[0].split_point=0;
  1285. progp->layer[0].play_below=0;
  1286. for (i = 1; i < 4; i++) {
  1287. progp->layer[i].mute=0;
  1288. }
  1289. /* Sample data */
  1290. sampp->SampleResolution=((~guspatch.mode & WAVE_16_BITS)<<1);
  1291. for (base_note=0;
  1292. note_to_freq (base_note) < guspatch.base_note;
  1293. base_note++);
  1294. if ((guspatch.base_note-note_to_freq(base_note))
  1295. >(note_to_freq(base_note)-guspatch.base_note))
  1296. base_note++;
  1297. printk(KERN_DEBUG "ref freq=%d,base note=%d\n",
  1298. guspatch.base_freq,
  1299. base_note);
  1300. sampp->FrequencyBias = (29550 - log2_2048(guspatch.base_freq)
  1301. + base_note*171);
  1302. printk(KERN_DEBUG "Freq Bias is %d\n", sampp->FrequencyBias);
  1303. sampp->Loop=(guspatch.mode & WAVE_LOOPING) ? 1:0;
  1304. sampp->sampleStartOffset.Fraction=0;
  1305. sampp->sampleStartOffset.Integer=0;
  1306. sampp->loopStartOffset.Fraction=0;
  1307. sampp->loopStartOffset.Integer=guspatch.loop_start
  1308. >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
  1309. sampp->loopEndOffset.Fraction=0;
  1310. sampp->loopEndOffset.Integer=guspatch.loop_end
  1311. >>((guspatch.mode&WAVE_16_BITS) ? 1:0);
  1312. sampp->sampleEndOffset.Fraction=0;
  1313. sampp->sampleEndOffset.Integer=guspatch.len >> (guspatch.mode&1);
  1314. sampp->Bidirectional=(guspatch.mode&WAVE_BIDIR_LOOP) ? 1:0;
  1315. sampp->Reverse=(guspatch.mode&WAVE_LOOP_BACK) ? 1:0;
  1316. /* Now ship it down */
  1317. wavefront_send_sample (samp,
  1318. (unsigned short __user *) &(addr)[sizeof_patch],
  1319. (guspatch.mode & WAVE_UNSIGNED) ? 1:0);
  1320. wavefront_send_patch (pat);
  1321. wavefront_send_program (prog);
  1322. /* Now pan as best we can ... use the slave/internal MIDI device
  1323. number if it exists (since it talks to the WaveFront), or the
  1324. master otherwise.
  1325. */
  1326. if (dev.mididev > 0) {
  1327. midi_synth_controller (dev.mididev, guspatch.instr_no, 10,
  1328. ((guspatch.panning << 4) > 127) ?
  1329. 127 : (guspatch.panning << 4));
  1330. }
  1331. rc = 0;
  1332. free_fail:
  1333. kfree(samp);
  1334. return rc;
  1335. }
  1336. static int
  1337. wavefront_load_patch (const char __user *addr)
  1338. {
  1339. wavefront_patch_info header;
  1340. if (copy_from_user (&header, addr, sizeof(wavefront_patch_info) -
  1341. sizeof(wavefront_any))) {
  1342. printk (KERN_WARNING LOGNAME "bad address for load patch.\n");
  1343. return -EFAULT;
  1344. }
  1345. DPRINT (WF_DEBUG_LOAD_PATCH, "download "
  1346. "Sample type: %d "
  1347. "Sample number: %d "
  1348. "Sample size: %d\n",
  1349. header.subkey,
  1350. header.number,
  1351. header.size);
  1352. switch (header.subkey) {
  1353. case WF_ST_SAMPLE: /* sample or sample_header, based on patch->size */
  1354. if (copy_from_user((unsigned char *) &header.hdr.s,
  1355. (unsigned char __user *) header.hdrptr,
  1356. sizeof (wavefront_sample)))
  1357. return -EFAULT;
  1358. return wavefront_send_sample (&header, header.dataptr, 0);
  1359. case WF_ST_MULTISAMPLE:
  1360. if (copy_from_user(&header.hdr.s, header.hdrptr,
  1361. sizeof(wavefront_multisample)))
  1362. return -EFAULT;
  1363. return wavefront_send_multisample (&header);
  1364. case WF_ST_ALIAS:
  1365. if (copy_from_user(&header.hdr.a, header.hdrptr,
  1366. sizeof (wavefront_alias)))
  1367. return -EFAULT;
  1368. return wavefront_send_alias (&header);
  1369. case WF_ST_DRUM:
  1370. if (copy_from_user(&header.hdr.d, header.hdrptr,
  1371. sizeof (wavefront_drum)))
  1372. return -EFAULT;
  1373. return wavefront_send_drum (&header);
  1374. case WF_ST_PATCH:
  1375. if (copy_from_user(&header.hdr.p, header.hdrptr,
  1376. sizeof (wavefront_patch)))
  1377. return -EFAULT;
  1378. return wavefront_send_patch (&header);
  1379. case WF_ST_PROGRAM:
  1380. if (copy_from_user(&header.hdr.pr, header.hdrptr,
  1381. sizeof (wavefront_program)))
  1382. return -EFAULT;
  1383. return wavefront_send_program (&header);
  1384. default:
  1385. printk (KERN_ERR LOGNAME "unknown patch type %d.\n",
  1386. header.subkey);
  1387. return -(EINVAL);
  1388. }
  1389. return 0;
  1390. }
  1391. /***********************************************************************
  1392. WaveFront: /dev/sequencer{,2} and other hardware-dependent interfaces
  1393. ***********************************************************************/
  1394. static void
  1395. process_sample_hdr (UCHAR8 *buf)
  1396. {
  1397. wavefront_sample s;
  1398. UCHAR8 *ptr;
  1399. ptr = buf;
  1400. /* The board doesn't send us an exact copy of a "wavefront_sample"
  1401. in response to an Upload Sample Header command. Instead, we
  1402. have to convert the data format back into our data structure,
  1403. just as in the Download Sample command, where we have to do
  1404. something very similar in the reverse direction.
  1405. */
  1406. *((UINT32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1407. *((UINT32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1408. *((UINT32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1409. *((UINT32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1410. *((UINT32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
  1411. s.SampleResolution = *ptr & 0x3;
  1412. s.Loop = *ptr & 0x8;
  1413. s.Bidirectional = *ptr & 0x10;
  1414. s.Reverse = *ptr & 0x40;
  1415. /* Now copy it back to where it came from */
  1416. memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
  1417. }
  1418. static int
  1419. wavefront_synth_control (int cmd, wavefront_control *wc)
  1420. {
  1421. unsigned char patchnumbuf[2];
  1422. int i;
  1423. DPRINT (WF_DEBUG_CMD, "synth control with "
  1424. "cmd 0x%x\n", wc->cmd);
  1425. /* Pre-handling of or for various commands */
  1426. switch (wc->cmd) {
  1427. case WFC_DISABLE_INTERRUPTS:
  1428. printk (KERN_INFO LOGNAME "interrupts disabled.\n");
  1429. outb (0x80|0x20, dev.control_port);
  1430. dev.interrupts_on = 0;
  1431. return 0;
  1432. case WFC_ENABLE_INTERRUPTS:
  1433. printk (KERN_INFO LOGNAME "interrupts enabled.\n");
  1434. outb (0x80|0x40|0x20, dev.control_port);
  1435. dev.interrupts_on = 1;
  1436. return 0;
  1437. case WFC_INTERRUPT_STATUS:
  1438. wc->rbuf[0] = dev.interrupts_on;
  1439. return 0;
  1440. case WFC_ROMSAMPLES_RDONLY:
  1441. dev.rom_samples_rdonly = wc->wbuf[0];
  1442. wc->status = 0;
  1443. return 0;
  1444. case WFC_IDENTIFY_SLOT_TYPE:
  1445. i = wc->wbuf[0] | (wc->wbuf[1] << 7);
  1446. if (i <0 || i >= WF_MAX_SAMPLE) {
  1447. printk (KERN_WARNING LOGNAME "invalid slot ID %d\n",
  1448. i);
  1449. wc->status = EINVAL;
  1450. return 0;
  1451. }
  1452. wc->rbuf[0] = dev.sample_status[i];
  1453. wc->status = 0;
  1454. return 0;
  1455. case WFC_DEBUG_DRIVER:
  1456. dev.debug = wc->wbuf[0];
  1457. printk (KERN_INFO LOGNAME "debug = 0x%x\n", dev.debug);
  1458. return 0;
  1459. case WFC_FX_IOCTL:
  1460. wffx_ioctl ((wavefront_fx_info *) &wc->wbuf[0]);
  1461. return 0;
  1462. case WFC_UPLOAD_PATCH:
  1463. munge_int32 (*((UINT32 *) wc->wbuf), patchnumbuf, 2);
  1464. memcpy (wc->wbuf, patchnumbuf, 2);
  1465. break;
  1466. case WFC_UPLOAD_MULTISAMPLE:
  1467. /* multisamples have to be handled differently, and
  1468. cannot be dealt with properly by wavefront_cmd() alone.
  1469. */
  1470. wc->status = wavefront_fetch_multisample
  1471. ((wavefront_patch_info *) wc->rbuf);
  1472. return 0;
  1473. case WFC_UPLOAD_SAMPLE_ALIAS:
  1474. printk (KERN_INFO LOGNAME "support for sample alias upload "
  1475. "being considered.\n");
  1476. wc->status = EINVAL;
  1477. return -EINVAL;
  1478. }
  1479. wc->status = wavefront_cmd (wc->cmd, wc->rbuf, wc->wbuf);
  1480. /* Post-handling of certain commands.
  1481. In particular, if the command was an upload, demunge the data
  1482. so that the user-level doesn't have to think about it.
  1483. */
  1484. if (wc->status == 0) {
  1485. switch (wc->cmd) {
  1486. /* intercept any freemem requests so that we know
  1487. we are always current with the user-level view
  1488. of things.
  1489. */
  1490. case WFC_REPORT_FREE_MEMORY:
  1491. dev.freemem = demunge_int32 (wc->rbuf, 4);
  1492. break;
  1493. case WFC_UPLOAD_PATCH:
  1494. demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
  1495. break;
  1496. case WFC_UPLOAD_PROGRAM:
  1497. demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
  1498. break;
  1499. case WFC_UPLOAD_EDRUM_PROGRAM:
  1500. demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
  1501. break;
  1502. case WFC_UPLOAD_SAMPLE_HEADER:
  1503. process_sample_hdr (wc->rbuf);
  1504. break;
  1505. case WFC_UPLOAD_SAMPLE_ALIAS:
  1506. printk (KERN_INFO LOGNAME "support for "
  1507. "sample aliases still "
  1508. "being considered.\n");
  1509. break;
  1510. case WFC_VMIDI_OFF:
  1511. if (virtual_midi_disable () < 0) {
  1512. return -(EIO);
  1513. }
  1514. break;
  1515. case WFC_VMIDI_ON:
  1516. if (virtual_midi_enable () < 0) {
  1517. return -(EIO);
  1518. }
  1519. break;
  1520. }
  1521. }
  1522. return 0;
  1523. }
  1524. /***********************************************************************/
  1525. /* WaveFront: Linux file system interface (for access via raw synth) */
  1526. /***********************************************************************/
  1527. static int
  1528. wavefront_open (struct inode *inode, struct file *file)
  1529. {
  1530. /* XXX fix me */
  1531. dev.opened = file->f_flags;
  1532. return 0;
  1533. }
  1534. static int
  1535. wavefront_release(struct inode *inode, struct file *file)
  1536. {
  1537. lock_kernel();
  1538. dev.opened = 0;
  1539. dev.debug = 0;
  1540. unlock_kernel();
  1541. return 0;
  1542. }
  1543. static int
  1544. wavefront_ioctl(struct inode *inode, struct file *file,
  1545. unsigned int cmd, unsigned long arg)
  1546. {
  1547. wavefront_control wc;
  1548. int err;
  1549. switch (cmd) {
  1550. case WFCTL_WFCMD:
  1551. if (copy_from_user(&wc, (void __user *) arg, sizeof (wc)))
  1552. return -EFAULT;
  1553. if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
  1554. if (copy_to_user ((void __user *) arg, &wc, sizeof (wc)))
  1555. return -EFAULT;
  1556. }
  1557. return err;
  1558. case WFCTL_LOAD_SPP:
  1559. return wavefront_load_patch ((const char __user *) arg);
  1560. default:
  1561. printk (KERN_WARNING LOGNAME "invalid ioctl %#x\n", cmd);
  1562. return -(EINVAL);
  1563. }
  1564. return 0;
  1565. }
  1566. static /*const*/ struct file_operations wavefront_fops = {
  1567. .owner = THIS_MODULE,
  1568. .llseek = no_llseek,
  1569. .ioctl = wavefront_ioctl,
  1570. .open = wavefront_open,
  1571. .release = wavefront_release,
  1572. };
  1573. /***********************************************************************/
  1574. /* WaveFront: OSS installation and support interface */
  1575. /***********************************************************************/
  1576. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  1577. static struct synth_info wavefront_info =
  1578. {"Turtle Beach WaveFront", 0, SYNTH_TYPE_SAMPLE, SAMPLE_TYPE_WAVEFRONT,
  1579. 0, 32, 0, 0, SYNTH_CAP_INPUT};
  1580. static int
  1581. wavefront_oss_open (int devno, int mode)
  1582. {
  1583. dev.opened = mode;
  1584. return 0;
  1585. }
  1586. static void
  1587. wavefront_oss_close (int devno)
  1588. {
  1589. dev.opened = 0;
  1590. dev.debug = 0;
  1591. return;
  1592. }
  1593. static int
  1594. wavefront_oss_ioctl (int devno, unsigned int cmd, void __user * arg)
  1595. {
  1596. wavefront_control wc;
  1597. int err;
  1598. switch (cmd) {
  1599. case SNDCTL_SYNTH_INFO:
  1600. if(copy_to_user(arg, &wavefront_info, sizeof (wavefront_info)))
  1601. return -EFAULT;
  1602. return 0;
  1603. case SNDCTL_SEQ_RESETSAMPLES:
  1604. // printk (KERN_WARNING LOGNAME "driver cannot reset samples.\n");
  1605. return 0; /* don't force an error */
  1606. case SNDCTL_SEQ_PERCMODE:
  1607. return 0; /* don't force an error */
  1608. case SNDCTL_SYNTH_MEMAVL:
  1609. if ((dev.freemem = wavefront_freemem ()) < 0) {
  1610. printk (KERN_ERR LOGNAME "cannot get memory size\n");
  1611. return -EIO;
  1612. } else {
  1613. return dev.freemem;
  1614. }
  1615. break;
  1616. case SNDCTL_SYNTH_CONTROL:
  1617. if(copy_from_user (&wc, arg, sizeof (wc)))
  1618. err = -EFAULT;
  1619. else if ((err = wavefront_synth_control (cmd, &wc)) == 0) {
  1620. if(copy_to_user (arg, &wc, sizeof (wc)))
  1621. err = -EFAULT;
  1622. }
  1623. return err;
  1624. default:
  1625. return -(EINVAL);
  1626. }
  1627. }
  1628. static int
  1629. wavefront_oss_load_patch (int devno, int format, const char __user *addr,
  1630. int offs, int count, int pmgr_flag)
  1631. {
  1632. if (format == SYSEX_PATCH) { /* Handled by midi_synth.c */
  1633. if (midi_load_patch == NULL) {
  1634. printk (KERN_ERR LOGNAME
  1635. "SYSEX not loadable: "
  1636. "no midi patch loader!\n");
  1637. return -(EINVAL);
  1638. }
  1639. return midi_load_patch (devno, format, addr,
  1640. offs, count, pmgr_flag);
  1641. } else if (format == GUS_PATCH) {
  1642. return wavefront_load_gus_patch (devno, format,
  1643. addr, offs, count, pmgr_flag);
  1644. } else if (format != WAVEFRONT_PATCH) {
  1645. printk (KERN_ERR LOGNAME "unknown patch format %d\n", format);
  1646. return -(EINVAL);
  1647. }
  1648. if (count < sizeof (wavefront_patch_info)) {
  1649. printk (KERN_ERR LOGNAME "sample header too short\n");
  1650. return -(EINVAL);
  1651. }
  1652. /* "addr" points to a user-space wavefront_patch_info */
  1653. return wavefront_load_patch (addr);
  1654. }
  1655. static struct synth_operations wavefront_operations =
  1656. {
  1657. .owner = THIS_MODULE,
  1658. .id = "WaveFront",
  1659. .info = &wavefront_info,
  1660. .midi_dev = 0,
  1661. .synth_type = SYNTH_TYPE_SAMPLE,
  1662. .synth_subtype = SAMPLE_TYPE_WAVEFRONT,
  1663. .open = wavefront_oss_open,
  1664. .close = wavefront_oss_close,
  1665. .ioctl = wavefront_oss_ioctl,
  1666. .kill_note = midi_synth_kill_note,
  1667. .start_note = midi_synth_start_note,
  1668. .set_instr = midi_synth_set_instr,
  1669. .reset = midi_synth_reset,
  1670. .load_patch = midi_synth_load_patch,
  1671. .aftertouch = midi_synth_aftertouch,
  1672. .controller = midi_synth_controller,
  1673. .panning = midi_synth_panning,
  1674. .bender = midi_synth_bender,
  1675. .setup_voice = midi_synth_setup_voice
  1676. };
  1677. #endif /* OSS_SUPPORT_SEQ */
  1678. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_STATIC_INSTALL
  1679. static void __init attach_wavefront (struct address_info *hw_config)
  1680. {
  1681. (void) install_wavefront ();
  1682. }
  1683. static int __init probe_wavefront (struct address_info *hw_config)
  1684. {
  1685. return !detect_wavefront (hw_config->irq, hw_config->io_base);
  1686. }
  1687. static void __exit unload_wavefront (struct address_info *hw_config)
  1688. {
  1689. (void) uninstall_wavefront ();
  1690. }
  1691. #endif /* OSS_SUPPORT_STATIC_INSTALL */
  1692. /***********************************************************************/
  1693. /* WaveFront: Linux modular sound kernel installation interface */
  1694. /***********************************************************************/
  1695. static irqreturn_t
  1696. wavefrontintr(int irq, void *dev_id, struct pt_regs *dummy)
  1697. {
  1698. struct wf_config *hw = dev_id;
  1699. /*
  1700. Some comments on interrupts. I attempted a version of this
  1701. driver that used interrupts throughout the code instead of
  1702. doing busy and/or sleep-waiting. Alas, it appears that once
  1703. the Motorola firmware is downloaded, the card *never*
  1704. generates an RX interrupt. These are successfully generated
  1705. during firmware loading, and after that wavefront_status()
  1706. reports that an interrupt is pending on the card from time
  1707. to time, but it never seems to be delivered to this
  1708. driver. Note also that wavefront_status() continues to
  1709. report that RX interrupts are enabled, suggesting that I
  1710. didn't goof up and disable them by mistake.
  1711. Thus, I stepped back to a prior version of
  1712. wavefront_wait(), the only place where this really
  1713. matters. Its sad, but I've looked through the code to check
  1714. on things, and I really feel certain that the Motorola
  1715. firmware prevents RX-ready interrupts.
  1716. */
  1717. if ((wavefront_status() & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
  1718. return IRQ_NONE;
  1719. }
  1720. hw->irq_ok = 1;
  1721. hw->irq_cnt++;
  1722. wake_up_interruptible (&hw->interrupt_sleeper);
  1723. return IRQ_HANDLED;
  1724. }
  1725. /* STATUS REGISTER
  1726. 0 Host Rx Interrupt Enable (1=Enabled)
  1727. 1 Host Rx Register Full (1=Full)
  1728. 2 Host Rx Interrupt Pending (1=Interrupt)
  1729. 3 Unused
  1730. 4 Host Tx Interrupt (1=Enabled)
  1731. 5 Host Tx Register empty (1=Empty)
  1732. 6 Host Tx Interrupt Pending (1=Interrupt)
  1733. 7 Unused
  1734. */
  1735. static int
  1736. wavefront_interrupt_bits (int irq)
  1737. {
  1738. int bits;
  1739. switch (irq) {
  1740. case 9:
  1741. bits = 0x00;
  1742. break;
  1743. case 5:
  1744. bits = 0x08;
  1745. break;
  1746. case 12:
  1747. bits = 0x10;
  1748. break;
  1749. case 15:
  1750. bits = 0x18;
  1751. break;
  1752. default:
  1753. printk (KERN_WARNING LOGNAME "invalid IRQ %d\n", irq);
  1754. bits = -1;
  1755. }
  1756. return bits;
  1757. }
  1758. static void
  1759. wavefront_should_cause_interrupt (int val, int port, int timeout)
  1760. {
  1761. unsigned long flags;
  1762. /* this will not help on SMP - but at least it compiles */
  1763. spin_lock_irqsave(&lock, flags);
  1764. dev.irq_ok = 0;
  1765. outb (val,port);
  1766. interruptible_sleep_on_timeout (&dev.interrupt_sleeper, timeout);
  1767. spin_unlock_irqrestore(&lock,flags);
  1768. }
  1769. static int __init wavefront_hw_reset (void)
  1770. {
  1771. int bits;
  1772. int hwv[2];
  1773. unsigned long irq_mask;
  1774. short reported_irq;
  1775. /* IRQ already checked in init_module() */
  1776. bits = wavefront_interrupt_bits (dev.irq);
  1777. printk (KERN_DEBUG LOGNAME "autodetecting WaveFront IRQ\n");
  1778. irq_mask = probe_irq_on ();
  1779. outb (0x0, dev.control_port);
  1780. outb (0x80 | 0x40 | bits, dev.data_port);
  1781. wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
  1782. dev.control_port,
  1783. (reset_time*HZ)/100);
  1784. reported_irq = probe_irq_off (irq_mask);
  1785. if (reported_irq != dev.irq) {
  1786. if (reported_irq == 0) {
  1787. printk (KERN_ERR LOGNAME
  1788. "No unassigned interrupts detected "
  1789. "after h/w reset\n");
  1790. } else if (reported_irq < 0) {
  1791. printk (KERN_ERR LOGNAME
  1792. "Multiple unassigned interrupts detected "
  1793. "after h/w reset\n");
  1794. } else {
  1795. printk (KERN_ERR LOGNAME "autodetected IRQ %d not the "
  1796. "value provided (%d)\n", reported_irq,
  1797. dev.irq);
  1798. }
  1799. dev.irq = -1;
  1800. return 1;
  1801. } else {
  1802. printk (KERN_INFO LOGNAME "autodetected IRQ at %d\n",
  1803. reported_irq);
  1804. }
  1805. if (request_irq (dev.irq, wavefrontintr,
  1806. IRQF_DISABLED|IRQF_SHARED,
  1807. "wavefront synth", &dev) < 0) {
  1808. printk (KERN_WARNING LOGNAME "IRQ %d not available!\n",
  1809. dev.irq);
  1810. return 1;
  1811. }
  1812. /* try reset of port */
  1813. outb (0x0, dev.control_port);
  1814. /* At this point, the board is in reset, and the H/W initialization
  1815. register is accessed at the same address as the data port.
  1816. Bit 7 - Enable IRQ Driver
  1817. 0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
  1818. 1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
  1819. Bit 6 - MIDI Interface Select
  1820. 0 - Use the MIDI Input from the 26-pin WaveBlaster
  1821. compatible header as the serial MIDI source
  1822. 1 - Use the MIDI Input from the 9-pin D connector as the
  1823. serial MIDI source.
  1824. Bits 5:3 - IRQ Selection
  1825. 0 0 0 - IRQ 2/9
  1826. 0 0 1 - IRQ 5
  1827. 0 1 0 - IRQ 12
  1828. 0 1 1 - IRQ 15
  1829. 1 0 0 - Reserved
  1830. 1 0 1 - Reserved
  1831. 1 1 0 - Reserved
  1832. 1 1 1 - Reserved
  1833. Bits 2:1 - Reserved
  1834. Bit 0 - Disable Boot ROM
  1835. 0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
  1836. 1 - memory accesses to 03FC30-03FFFFH are directed to external
  1837. storage.
  1838. */
  1839. /* configure hardware: IRQ, enable interrupts,
  1840. plus external 9-pin MIDI interface selected
  1841. */
  1842. outb (0x80 | 0x40 | bits, dev.data_port);
  1843. /* CONTROL REGISTER
  1844. 0 Host Rx Interrupt Enable (1=Enabled) 0x1
  1845. 1 Unused 0x2
  1846. 2 Unused 0x4
  1847. 3 Unused 0x8
  1848. 4 Host Tx Interrupt Enable 0x10
  1849. 5 Mute (0=Mute; 1=Play) 0x20
  1850. 6 Master Interrupt Enable (1=Enabled) 0x40
  1851. 7 Master Reset (0=Reset; 1=Run) 0x80
  1852. Take us out of reset, mute output, master + TX + RX interrupts on.
  1853. We'll get an interrupt presumably to tell us that the TX
  1854. register is clear.
  1855. */
  1856. wavefront_should_cause_interrupt(0x80|0x40|0x10|0x1,
  1857. dev.control_port,
  1858. (reset_time*HZ)/100);
  1859. /* Note: data port is now the data port, not the h/w initialization
  1860. port.
  1861. */
  1862. if (!dev.irq_ok) {
  1863. printk (KERN_WARNING LOGNAME
  1864. "intr not received after h/w un-reset.\n");
  1865. goto gone_bad;
  1866. }
  1867. dev.interrupts_on = 1;
  1868. /* Note: data port is now the data port, not the h/w initialization
  1869. port.
  1870. At this point, only "HW VERSION" or "DOWNLOAD OS" commands
  1871. will work. So, issue one of them, and wait for TX
  1872. interrupt. This can take a *long* time after a cold boot,
  1873. while the ISC ROM does its RAM test. The SDK says up to 4
  1874. seconds - with 12MB of RAM on a Tropez+, it takes a lot
  1875. longer than that (~16secs). Note that the card understands
  1876. the difference between a warm and a cold boot, so
  1877. subsequent ISC2115 reboots (say, caused by module
  1878. reloading) will get through this much faster.
  1879. XXX Interesting question: why is no RX interrupt received first ?
  1880. */
  1881. wavefront_should_cause_interrupt(WFC_HARDWARE_VERSION,
  1882. dev.data_port, ramcheck_time*HZ);
  1883. if (!dev.irq_ok) {
  1884. printk (KERN_WARNING LOGNAME
  1885. "post-RAM-check interrupt not received.\n");
  1886. goto gone_bad;
  1887. }
  1888. if (!wavefront_wait (STAT_CAN_READ)) {
  1889. printk (KERN_WARNING LOGNAME
  1890. "no response to HW version cmd.\n");
  1891. goto gone_bad;
  1892. }
  1893. if ((hwv[0] = wavefront_read ()) == -1) {
  1894. printk (KERN_WARNING LOGNAME
  1895. "board not responding correctly.\n");
  1896. goto gone_bad;
  1897. }
  1898. if (hwv[0] == 0xFF) { /* NAK */
  1899. /* Board's RAM test failed. Try to read error code,
  1900. and tell us about it either way.
  1901. */
  1902. if ((hwv[0] = wavefront_read ()) == -1) {
  1903. printk (KERN_WARNING LOGNAME "on-board RAM test failed "
  1904. "(bad error code).\n");
  1905. } else {
  1906. printk (KERN_WARNING LOGNAME "on-board RAM test failed "
  1907. "(error code: 0x%x).\n",
  1908. hwv[0]);
  1909. }
  1910. goto gone_bad;
  1911. }
  1912. /* We're OK, just get the next byte of the HW version response */
  1913. if ((hwv[1] = wavefront_read ()) == -1) {
  1914. printk (KERN_WARNING LOGNAME "incorrect h/w response.\n");
  1915. goto gone_bad;
  1916. }
  1917. printk (KERN_INFO LOGNAME "hardware version %d.%d\n",
  1918. hwv[0], hwv[1]);
  1919. return 0;
  1920. gone_bad:
  1921. if (dev.irq >= 0) {
  1922. free_irq (dev.irq, &dev);
  1923. dev.irq = -1;
  1924. }
  1925. return (1);
  1926. }
  1927. static int __init detect_wavefront (int irq, int io_base)
  1928. {
  1929. unsigned char rbuf[4], wbuf[4];
  1930. /* TB docs say the device takes up 8 ports, but we know that
  1931. if there is an FX device present (i.e. a Tropez+) it really
  1932. consumes 16.
  1933. */
  1934. if (!request_region (io_base, 16, "wavfront")) {
  1935. printk (KERN_ERR LOGNAME "IO address range 0x%x - 0x%x "
  1936. "already in use - ignored\n", dev.base,
  1937. dev.base+15);
  1938. return -1;
  1939. }
  1940. dev.irq = irq;
  1941. dev.base = io_base;
  1942. dev.israw = 0;
  1943. dev.debug = debug_default;
  1944. dev.interrupts_on = 0;
  1945. dev.irq_cnt = 0;
  1946. dev.rom_samples_rdonly = 1; /* XXX default lock on ROM sample slots */
  1947. if (wavefront_cmd (WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
  1948. dev.fw_version[0] = rbuf[0];
  1949. dev.fw_version[1] = rbuf[1];
  1950. printk (KERN_INFO LOGNAME
  1951. "firmware %d.%d already loaded.\n",
  1952. rbuf[0], rbuf[1]);
  1953. /* check that a command actually works */
  1954. if (wavefront_cmd (WFC_HARDWARE_VERSION,
  1955. rbuf, wbuf) == 0) {
  1956. dev.hw_version[0] = rbuf[0];
  1957. dev.hw_version[1] = rbuf[1];
  1958. } else {
  1959. printk (KERN_WARNING LOGNAME "not raw, but no "
  1960. "hardware version!\n");
  1961. release_region (io_base, 16);
  1962. return 0;
  1963. }
  1964. if (!wf_raw) {
  1965. /* will re-acquire region in install_wavefront() */
  1966. release_region (io_base, 16);
  1967. return 1;
  1968. } else {
  1969. printk (KERN_INFO LOGNAME
  1970. "reloading firmware anyway.\n");
  1971. dev.israw = 1;
  1972. }
  1973. } else {
  1974. dev.israw = 1;
  1975. printk (KERN_INFO LOGNAME
  1976. "no response to firmware probe, assume raw.\n");
  1977. }
  1978. init_waitqueue_head (&dev.interrupt_sleeper);
  1979. if (wavefront_hw_reset ()) {
  1980. printk (KERN_WARNING LOGNAME "hardware reset failed\n");
  1981. release_region (io_base, 16);
  1982. return 0;
  1983. }
  1984. /* Check for FX device, present only on Tropez+ */
  1985. dev.has_fx = (detect_wffx () == 0);
  1986. /* will re-acquire region in install_wavefront() */
  1987. release_region (io_base, 16);
  1988. return 1;
  1989. }
  1990. #include "os.h"
  1991. #include <linux/fs.h>
  1992. #include <linux/mm.h>
  1993. #include <linux/slab.h>
  1994. #include <asm/uaccess.h>
  1995. static int
  1996. wavefront_download_firmware (char *path)
  1997. {
  1998. unsigned char section[WF_SECTION_MAX];
  1999. char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
  2000. int section_cnt_downloaded = 0;
  2001. int fd;
  2002. int c;
  2003. int i;
  2004. mm_segment_t fs;
  2005. /* This tries to be a bit cleverer than the stuff Alan Cox did for
  2006. the generic sound firmware, in that it actually knows
  2007. something about the structure of the Motorola firmware. In
  2008. particular, it uses a version that has been stripped of the
  2009. 20K of useless header information, and had section lengths
  2010. added, making it possible to load the entire OS without any
  2011. [kv]malloc() activity, since the longest entity we ever read is
  2012. 42 bytes (well, WF_SECTION_MAX) long.
  2013. */
  2014. fs = get_fs();
  2015. set_fs (get_ds());
  2016. if ((fd = sys_open (path, 0, 0)) < 0) {
  2017. printk (KERN_WARNING LOGNAME "Unable to load \"%s\".\n",
  2018. path);
  2019. return 1;
  2020. }
  2021. while (1) {
  2022. int x;
  2023. if ((x = sys_read (fd, &section_length, sizeof (section_length))) !=
  2024. sizeof (section_length)) {
  2025. printk (KERN_ERR LOGNAME "firmware read error.\n");
  2026. goto failure;
  2027. }
  2028. if (section_length == 0) {
  2029. break;
  2030. }
  2031. if (sys_read (fd, section, section_length) != section_length) {
  2032. printk (KERN_ERR LOGNAME "firmware section "
  2033. "read error.\n");
  2034. goto failure;
  2035. }
  2036. /* Send command */
  2037. if (wavefront_write (WFC_DOWNLOAD_OS)) {
  2038. goto failure;
  2039. }
  2040. for (i = 0; i < section_length; i++) {
  2041. if (wavefront_write (section[i])) {
  2042. goto failure;
  2043. }
  2044. }
  2045. /* get ACK */
  2046. if (wavefront_wait (STAT_CAN_READ)) {
  2047. if ((c = inb (dev.data_port)) != WF_ACK) {
  2048. printk (KERN_ERR LOGNAME "download "
  2049. "of section #%d not "
  2050. "acknowledged, ack = 0x%x\n",
  2051. section_cnt_downloaded + 1, c);
  2052. goto failure;
  2053. }
  2054. } else {
  2055. printk (KERN_ERR LOGNAME "time out for firmware ACK.\n");
  2056. goto failure;
  2057. }
  2058. }
  2059. sys_close (fd);
  2060. set_fs (fs);
  2061. return 0;
  2062. failure:
  2063. sys_close (fd);
  2064. set_fs (fs);
  2065. printk (KERN_ERR "\nWaveFront: firmware download failed!!!\n");
  2066. return 1;
  2067. }
  2068. static int __init wavefront_config_midi (void)
  2069. {
  2070. unsigned char rbuf[4], wbuf[4];
  2071. if (detect_wf_mpu (dev.irq, dev.base) < 0) {
  2072. printk (KERN_WARNING LOGNAME
  2073. "could not find working MIDI device\n");
  2074. return -1;
  2075. }
  2076. if ((dev.mididev = install_wf_mpu ()) < 0) {
  2077. printk (KERN_WARNING LOGNAME
  2078. "MIDI interfaces not configured\n");
  2079. return -1;
  2080. }
  2081. /* Route external MIDI to WaveFront synth (by default) */
  2082. if (wavefront_cmd (WFC_MISYNTH_ON, rbuf, wbuf)) {
  2083. printk (KERN_WARNING LOGNAME
  2084. "cannot enable MIDI-IN to synth routing.\n");
  2085. /* XXX error ? */
  2086. }
  2087. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  2088. /* Get the regular MIDI patch loading function, so we can
  2089. use it if we ever get handed a SYSEX patch. This is
  2090. unlikely, because its so damn slow, but we may as well
  2091. leave this functionality from maui.c behind, since it
  2092. could be useful for sequencer applications that can
  2093. only use MIDI to do patch loading.
  2094. */
  2095. if (midi_devs[dev.mididev]->converter != NULL) {
  2096. midi_load_patch = midi_devs[dev.mididev]->converter->load_patch;
  2097. midi_devs[dev.mididev]->converter->load_patch =
  2098. &wavefront_oss_load_patch;
  2099. }
  2100. #endif /* OSS_SUPPORT_SEQ */
  2101. /* Turn on Virtual MIDI, but first *always* turn it off,
  2102. since otherwise consectutive reloads of the driver will
  2103. never cause the hardware to generate the initial "internal" or
  2104. "external" source bytes in the MIDI data stream. This
  2105. is pretty important, since the internal hardware generally will
  2106. be used to generate none or very little MIDI output, and
  2107. thus the only source of MIDI data is actually external. Without
  2108. the switch bytes, the driver will think it all comes from
  2109. the internal interface. Duh.
  2110. */
  2111. if (wavefront_cmd (WFC_VMIDI_OFF, rbuf, wbuf)) {
  2112. printk (KERN_WARNING LOGNAME
  2113. "virtual MIDI mode not disabled\n");
  2114. return 0; /* We're OK, but missing the external MIDI dev */
  2115. }
  2116. if ((dev.ext_mididev = virtual_midi_enable ()) < 0) {
  2117. printk (KERN_WARNING LOGNAME "no virtual MIDI access.\n");
  2118. } else {
  2119. if (wavefront_cmd (WFC_VMIDI_ON, rbuf, wbuf)) {
  2120. printk (KERN_WARNING LOGNAME
  2121. "cannot enable virtual MIDI mode.\n");
  2122. virtual_midi_disable ();
  2123. }
  2124. }
  2125. return 0;
  2126. }
  2127. static int __init wavefront_do_reset (int atboot)
  2128. {
  2129. char voices[1];
  2130. if (!atboot && wavefront_hw_reset ()) {
  2131. printk (KERN_WARNING LOGNAME "hw reset failed.\n");
  2132. goto gone_bad;
  2133. }
  2134. if (dev.israw) {
  2135. if (wavefront_download_firmware (ospath)) {
  2136. goto gone_bad;
  2137. }
  2138. dev.israw = 0;
  2139. /* Wait for the OS to get running. The protocol for
  2140. this is non-obvious, and was determined by
  2141. using port-IO tracing in DOSemu and some
  2142. experimentation here.
  2143. Rather than using timed waits, use interrupts creatively.
  2144. */
  2145. wavefront_should_cause_interrupt (WFC_NOOP,
  2146. dev.data_port,
  2147. (osrun_time*HZ));
  2148. if (!dev.irq_ok) {
  2149. printk (KERN_WARNING LOGNAME
  2150. "no post-OS interrupt.\n");
  2151. goto gone_bad;
  2152. }
  2153. /* Now, do it again ! */
  2154. wavefront_should_cause_interrupt (WFC_NOOP,
  2155. dev.data_port, (10*HZ));
  2156. if (!dev.irq_ok) {
  2157. printk (KERN_WARNING LOGNAME
  2158. "no post-OS interrupt(2).\n");
  2159. goto gone_bad;
  2160. }
  2161. /* OK, no (RX/TX) interrupts any more, but leave mute
  2162. in effect.
  2163. */
  2164. outb (0x80|0x40, dev.control_port);
  2165. /* No need for the IRQ anymore */
  2166. free_irq (dev.irq, &dev);
  2167. }
  2168. if (dev.has_fx && fx_raw) {
  2169. wffx_init ();
  2170. }
  2171. /* SETUPSND.EXE asks for sample memory config here, but since i
  2172. have no idea how to interpret the result, we'll forget
  2173. about it.
  2174. */
  2175. if ((dev.freemem = wavefront_freemem ()) < 0) {
  2176. goto gone_bad;
  2177. }
  2178. printk (KERN_INFO LOGNAME "available DRAM %dk\n", dev.freemem / 1024);
  2179. if (wavefront_write (0xf0) ||
  2180. wavefront_write (1) ||
  2181. (wavefront_read () < 0)) {
  2182. dev.debug = 0;
  2183. printk (KERN_WARNING LOGNAME "MPU emulation mode not set.\n");
  2184. goto gone_bad;
  2185. }
  2186. voices[0] = 32;
  2187. if (wavefront_cmd (WFC_SET_NVOICES, NULL, voices)) {
  2188. printk (KERN_WARNING LOGNAME
  2189. "cannot set number of voices to 32.\n");
  2190. goto gone_bad;
  2191. }
  2192. return 0;
  2193. gone_bad:
  2194. /* reset that sucker so that it doesn't bother us. */
  2195. outb (0x0, dev.control_port);
  2196. dev.interrupts_on = 0;
  2197. if (dev.irq >= 0) {
  2198. free_irq (dev.irq, &dev);
  2199. }
  2200. return 1;
  2201. }
  2202. static int __init wavefront_init (int atboot)
  2203. {
  2204. int samples_are_from_rom;
  2205. if (dev.israw) {
  2206. samples_are_from_rom = 1;
  2207. } else {
  2208. /* XXX is this always true ? */
  2209. samples_are_from_rom = 0;
  2210. }
  2211. if (dev.israw || fx_raw) {
  2212. if (wavefront_do_reset (atboot)) {
  2213. return -1;
  2214. }
  2215. }
  2216. wavefront_get_sample_status (samples_are_from_rom);
  2217. wavefront_get_program_status ();
  2218. wavefront_get_patch_status ();
  2219. /* Start normal operation: unreset, master interrupt enabled, no mute
  2220. */
  2221. outb (0x80|0x40|0x20, dev.control_port);
  2222. return (0);
  2223. }
  2224. static int __init install_wavefront (void)
  2225. {
  2226. if (!request_region (dev.base+2, 6, "wavefront synth"))
  2227. return -1;
  2228. if (dev.has_fx) {
  2229. if (!request_region (dev.base+8, 8, "wavefront fx")) {
  2230. release_region (dev.base+2, 6);
  2231. return -1;
  2232. }
  2233. }
  2234. if ((dev.synth_dev = register_sound_synth (&wavefront_fops, -1)) < 0) {
  2235. printk (KERN_ERR LOGNAME "cannot register raw synth\n");
  2236. goto err_out;
  2237. }
  2238. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  2239. if ((dev.oss_dev = sound_alloc_synthdev()) == -1) {
  2240. printk (KERN_ERR LOGNAME "Too many sequencers\n");
  2241. /* FIXME: leak: should unregister sound synth */
  2242. goto err_out;
  2243. } else {
  2244. synth_devs[dev.oss_dev] = &wavefront_operations;
  2245. }
  2246. #endif /* OSS_SUPPORT_SEQ */
  2247. if (wavefront_init (1) < 0) {
  2248. printk (KERN_WARNING LOGNAME "initialization failed.\n");
  2249. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  2250. sound_unload_synthdev (dev.oss_dev);
  2251. #endif /* OSS_SUPPORT_SEQ */
  2252. goto err_out;
  2253. }
  2254. if (wavefront_config_midi ()) {
  2255. printk (KERN_WARNING LOGNAME "could not initialize MIDI.\n");
  2256. }
  2257. return dev.oss_dev;
  2258. err_out:
  2259. release_region (dev.base+2, 6);
  2260. if (dev.has_fx)
  2261. release_region (dev.base+8, 8);
  2262. return -1;
  2263. }
  2264. static void __exit uninstall_wavefront (void)
  2265. {
  2266. /* the first two i/o addresses are freed by the wf_mpu code */
  2267. release_region (dev.base+2, 6);
  2268. if (dev.has_fx) {
  2269. release_region (dev.base+8, 8);
  2270. }
  2271. unregister_sound_synth (dev.synth_dev);
  2272. #if OSS_SUPPORT_LEVEL & OSS_SUPPORT_SEQ
  2273. sound_unload_synthdev (dev.oss_dev);
  2274. #endif /* OSS_SUPPORT_SEQ */
  2275. uninstall_wf_mpu ();
  2276. }
  2277. /***********************************************************************/
  2278. /* WaveFront FX control */
  2279. /***********************************************************************/
  2280. #include "yss225.h"
  2281. /* Control bits for the Load Control Register
  2282. */
  2283. #define FX_LSB_TRANSFER 0x01 /* transfer after DSP LSB byte written */
  2284. #define FX_MSB_TRANSFER 0x02 /* transfer after DSP MSB byte written */
  2285. #define FX_AUTO_INCR 0x04 /* auto-increment DSP address after transfer */
  2286. static int
  2287. wffx_idle (void)
  2288. {
  2289. int i;
  2290. unsigned int x = 0x80;
  2291. for (i = 0; i < 1000; i++) {
  2292. x = inb (dev.fx_status);
  2293. if ((x & 0x80) == 0) {
  2294. break;
  2295. }
  2296. }
  2297. if (x & 0x80) {
  2298. printk (KERN_ERR LOGNAME "FX device never idle.\n");
  2299. return 0;
  2300. }
  2301. return (1);
  2302. }
  2303. int __init detect_wffx (void)
  2304. {
  2305. /* This is a crude check, but its the best one I have for now.
  2306. Certainly on the Maui and the Tropez, wffx_idle() will
  2307. report "never idle", which suggests that this test should
  2308. work OK.
  2309. */
  2310. if (inb (dev.fx_status) & 0x80) {
  2311. printk (KERN_INFO LOGNAME "Hmm, probably a Maui or Tropez.\n");
  2312. return -1;
  2313. }
  2314. return 0;
  2315. }
  2316. static void
  2317. wffx_mute (int onoff)
  2318. {
  2319. if (!wffx_idle()) {
  2320. return;
  2321. }
  2322. outb (onoff ? 0x02 : 0x00, dev.fx_op);
  2323. }
  2324. static int
  2325. wffx_memset (int page,
  2326. int addr, int cnt, unsigned short *data)
  2327. {
  2328. if (page < 0 || page > 7) {
  2329. printk (KERN_ERR LOGNAME "FX memset: "
  2330. "page must be >= 0 and <= 7\n");
  2331. return -(EINVAL);
  2332. }
  2333. if (addr < 0 || addr > 0x7f) {
  2334. printk (KERN_ERR LOGNAME "FX memset: "
  2335. "addr must be >= 0 and <= 7f\n");
  2336. return -(EINVAL);
  2337. }
  2338. if (cnt == 1) {
  2339. outb (FX_LSB_TRANSFER, dev.fx_lcr);
  2340. outb (page, dev.fx_dsp_page);
  2341. outb (addr, dev.fx_dsp_addr);
  2342. outb ((data[0] >> 8), dev.fx_dsp_msb);
  2343. outb ((data[0] & 0xff), dev.fx_dsp_lsb);
  2344. printk (KERN_INFO LOGNAME "FX: addr %d:%x set to 0x%x\n",
  2345. page, addr, data[0]);
  2346. } else {
  2347. int i;
  2348. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2349. outb (page, dev.fx_dsp_page);
  2350. outb (addr, dev.fx_dsp_addr);
  2351. for (i = 0; i < cnt; i++) {
  2352. outb ((data[i] >> 8), dev.fx_dsp_msb);
  2353. outb ((data[i] & 0xff), dev.fx_dsp_lsb);
  2354. if (!wffx_idle ()) {
  2355. break;
  2356. }
  2357. }
  2358. if (i != cnt) {
  2359. printk (KERN_WARNING LOGNAME
  2360. "FX memset "
  2361. "(0x%x, 0x%x, %p, %d) incomplete\n",
  2362. page, addr, data, cnt);
  2363. return -(EIO);
  2364. }
  2365. }
  2366. return 0;
  2367. }
  2368. static int
  2369. wffx_ioctl (wavefront_fx_info *r)
  2370. {
  2371. unsigned short page_data[256];
  2372. unsigned short *pd;
  2373. switch (r->request) {
  2374. case WFFX_MUTE:
  2375. wffx_mute (r->data[0]);
  2376. return 0;
  2377. case WFFX_MEMSET:
  2378. if (r->data[2] <= 0) {
  2379. printk (KERN_ERR LOGNAME "cannot write "
  2380. "<= 0 bytes to FX\n");
  2381. return -(EINVAL);
  2382. } else if (r->data[2] == 1) {
  2383. pd = (unsigned short *) &r->data[3];
  2384. } else {
  2385. if (r->data[2] > sizeof (page_data)) {
  2386. printk (KERN_ERR LOGNAME "cannot write "
  2387. "> 255 bytes to FX\n");
  2388. return -(EINVAL);
  2389. }
  2390. if (copy_from_user(page_data,
  2391. (unsigned char __user *)r->data[3],
  2392. r->data[2]))
  2393. return -EFAULT;
  2394. pd = page_data;
  2395. }
  2396. return wffx_memset (r->data[0], /* page */
  2397. r->data[1], /* addr */
  2398. r->data[2], /* cnt */
  2399. pd);
  2400. default:
  2401. printk (KERN_WARNING LOGNAME
  2402. "FX: ioctl %d not yet supported\n",
  2403. r->request);
  2404. return -(EINVAL);
  2405. }
  2406. }
  2407. /* YSS225 initialization.
  2408. This code was developed using DOSEMU. The Turtle Beach SETUPSND
  2409. utility was run with I/O tracing in DOSEMU enabled, and a reconstruction
  2410. of the port I/O done, using the Yamaha faxback document as a guide
  2411. to add more logic to the code. Its really pretty weird.
  2412. There was an alternative approach of just dumping the whole I/O
  2413. sequence as a series of port/value pairs and a simple loop
  2414. that output it. However, I hope that eventually I'll get more
  2415. control over what this code does, and so I tried to stick with
  2416. a somewhat "algorithmic" approach.
  2417. */
  2418. static int __init wffx_init (void)
  2419. {
  2420. int i;
  2421. int j;
  2422. /* Set all bits for all channels on the MOD unit to zero */
  2423. /* XXX But why do this twice ? */
  2424. for (j = 0; j < 2; j++) {
  2425. for (i = 0x10; i <= 0xff; i++) {
  2426. if (!wffx_idle ()) {
  2427. return (-1);
  2428. }
  2429. outb (i, dev.fx_mod_addr);
  2430. outb (0x0, dev.fx_mod_data);
  2431. }
  2432. }
  2433. if (!wffx_idle()) return (-1);
  2434. outb (0x02, dev.fx_op); /* mute on */
  2435. if (!wffx_idle()) return (-1);
  2436. outb (0x07, dev.fx_dsp_page);
  2437. outb (0x44, dev.fx_dsp_addr);
  2438. outb (0x00, dev.fx_dsp_msb);
  2439. outb (0x00, dev.fx_dsp_lsb);
  2440. if (!wffx_idle()) return (-1);
  2441. outb (0x07, dev.fx_dsp_page);
  2442. outb (0x42, dev.fx_dsp_addr);
  2443. outb (0x00, dev.fx_dsp_msb);
  2444. outb (0x00, dev.fx_dsp_lsb);
  2445. if (!wffx_idle()) return (-1);
  2446. outb (0x07, dev.fx_dsp_page);
  2447. outb (0x43, dev.fx_dsp_addr);
  2448. outb (0x00, dev.fx_dsp_msb);
  2449. outb (0x00, dev.fx_dsp_lsb);
  2450. if (!wffx_idle()) return (-1);
  2451. outb (0x07, dev.fx_dsp_page);
  2452. outb (0x7c, dev.fx_dsp_addr);
  2453. outb (0x00, dev.fx_dsp_msb);
  2454. outb (0x00, dev.fx_dsp_lsb);
  2455. if (!wffx_idle()) return (-1);
  2456. outb (0x07, dev.fx_dsp_page);
  2457. outb (0x7e, dev.fx_dsp_addr);
  2458. outb (0x00, dev.fx_dsp_msb);
  2459. outb (0x00, dev.fx_dsp_lsb);
  2460. if (!wffx_idle()) return (-1);
  2461. outb (0x07, dev.fx_dsp_page);
  2462. outb (0x46, dev.fx_dsp_addr);
  2463. outb (0x00, dev.fx_dsp_msb);
  2464. outb (0x00, dev.fx_dsp_lsb);
  2465. if (!wffx_idle()) return (-1);
  2466. outb (0x07, dev.fx_dsp_page);
  2467. outb (0x49, dev.fx_dsp_addr);
  2468. outb (0x00, dev.fx_dsp_msb);
  2469. outb (0x00, dev.fx_dsp_lsb);
  2470. if (!wffx_idle()) return (-1);
  2471. outb (0x07, dev.fx_dsp_page);
  2472. outb (0x47, dev.fx_dsp_addr);
  2473. outb (0x00, dev.fx_dsp_msb);
  2474. outb (0x00, dev.fx_dsp_lsb);
  2475. if (!wffx_idle()) return (-1);
  2476. outb (0x07, dev.fx_dsp_page);
  2477. outb (0x4a, dev.fx_dsp_addr);
  2478. outb (0x00, dev.fx_dsp_msb);
  2479. outb (0x00, dev.fx_dsp_lsb);
  2480. /* either because of stupidity by TB's programmers, or because it
  2481. actually does something, rezero the MOD page.
  2482. */
  2483. for (i = 0x10; i <= 0xff; i++) {
  2484. if (!wffx_idle ()) {
  2485. return (-1);
  2486. }
  2487. outb (i, dev.fx_mod_addr);
  2488. outb (0x0, dev.fx_mod_data);
  2489. }
  2490. /* load page zero */
  2491. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2492. outb (0x00, dev.fx_dsp_page);
  2493. outb (0x00, dev.fx_dsp_addr);
  2494. for (i = 0; i < sizeof (page_zero); i += 2) {
  2495. outb (page_zero[i], dev.fx_dsp_msb);
  2496. outb (page_zero[i+1], dev.fx_dsp_lsb);
  2497. if (!wffx_idle()) return (-1);
  2498. }
  2499. /* Now load page one */
  2500. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2501. outb (0x01, dev.fx_dsp_page);
  2502. outb (0x00, dev.fx_dsp_addr);
  2503. for (i = 0; i < sizeof (page_one); i += 2) {
  2504. outb (page_one[i], dev.fx_dsp_msb);
  2505. outb (page_one[i+1], dev.fx_dsp_lsb);
  2506. if (!wffx_idle()) return (-1);
  2507. }
  2508. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2509. outb (0x02, dev.fx_dsp_page);
  2510. outb (0x00, dev.fx_dsp_addr);
  2511. for (i = 0; i < sizeof (page_two); i++) {
  2512. outb (page_two[i], dev.fx_dsp_lsb);
  2513. if (!wffx_idle()) return (-1);
  2514. }
  2515. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2516. outb (0x03, dev.fx_dsp_page);
  2517. outb (0x00, dev.fx_dsp_addr);
  2518. for (i = 0; i < sizeof (page_three); i++) {
  2519. outb (page_three[i], dev.fx_dsp_lsb);
  2520. if (!wffx_idle()) return (-1);
  2521. }
  2522. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2523. outb (0x04, dev.fx_dsp_page);
  2524. outb (0x00, dev.fx_dsp_addr);
  2525. for (i = 0; i < sizeof (page_four); i++) {
  2526. outb (page_four[i], dev.fx_dsp_lsb);
  2527. if (!wffx_idle()) return (-1);
  2528. }
  2529. /* Load memory area (page six) */
  2530. outb (FX_LSB_TRANSFER, dev.fx_lcr);
  2531. outb (0x06, dev.fx_dsp_page);
  2532. for (i = 0; i < sizeof (page_six); i += 3) {
  2533. outb (page_six[i], dev.fx_dsp_addr);
  2534. outb (page_six[i+1], dev.fx_dsp_msb);
  2535. outb (page_six[i+2], dev.fx_dsp_lsb);
  2536. if (!wffx_idle()) return (-1);
  2537. }
  2538. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2539. outb (0x07, dev.fx_dsp_page);
  2540. outb (0x00, dev.fx_dsp_addr);
  2541. for (i = 0; i < sizeof (page_seven); i += 2) {
  2542. outb (page_seven[i], dev.fx_dsp_msb);
  2543. outb (page_seven[i+1], dev.fx_dsp_lsb);
  2544. if (!wffx_idle()) return (-1);
  2545. }
  2546. /* Now setup the MOD area. We do this algorithmically in order to
  2547. save a little data space. It could be done in the same fashion
  2548. as the "pages".
  2549. */
  2550. for (i = 0x00; i <= 0x0f; i++) {
  2551. outb (0x01, dev.fx_mod_addr);
  2552. outb (i, dev.fx_mod_data);
  2553. if (!wffx_idle()) return (-1);
  2554. outb (0x02, dev.fx_mod_addr);
  2555. outb (0x00, dev.fx_mod_data);
  2556. if (!wffx_idle()) return (-1);
  2557. }
  2558. for (i = 0xb0; i <= 0xbf; i++) {
  2559. outb (i, dev.fx_mod_addr);
  2560. outb (0x20, dev.fx_mod_data);
  2561. if (!wffx_idle()) return (-1);
  2562. }
  2563. for (i = 0xf0; i <= 0xff; i++) {
  2564. outb (i, dev.fx_mod_addr);
  2565. outb (0x20, dev.fx_mod_data);
  2566. if (!wffx_idle()) return (-1);
  2567. }
  2568. for (i = 0x10; i <= 0x1d; i++) {
  2569. outb (i, dev.fx_mod_addr);
  2570. outb (0xff, dev.fx_mod_data);
  2571. if (!wffx_idle()) return (-1);
  2572. }
  2573. outb (0x1e, dev.fx_mod_addr);
  2574. outb (0x40, dev.fx_mod_data);
  2575. if (!wffx_idle()) return (-1);
  2576. for (i = 0x1f; i <= 0x2d; i++) {
  2577. outb (i, dev.fx_mod_addr);
  2578. outb (0xff, dev.fx_mod_data);
  2579. if (!wffx_idle()) return (-1);
  2580. }
  2581. outb (0x2e, dev.fx_mod_addr);
  2582. outb (0x00, dev.fx_mod_data);
  2583. if (!wffx_idle()) return (-1);
  2584. for (i = 0x2f; i <= 0x3e; i++) {
  2585. outb (i, dev.fx_mod_addr);
  2586. outb (0x00, dev.fx_mod_data);
  2587. if (!wffx_idle()) return (-1);
  2588. }
  2589. outb (0x3f, dev.fx_mod_addr);
  2590. outb (0x20, dev.fx_mod_data);
  2591. if (!wffx_idle()) return (-1);
  2592. for (i = 0x40; i <= 0x4d; i++) {
  2593. outb (i, dev.fx_mod_addr);
  2594. outb (0x00, dev.fx_mod_data);
  2595. if (!wffx_idle()) return (-1);
  2596. }
  2597. outb (0x4e, dev.fx_mod_addr);
  2598. outb (0x0e, dev.fx_mod_data);
  2599. if (!wffx_idle()) return (-1);
  2600. outb (0x4f, dev.fx_mod_addr);
  2601. outb (0x0e, dev.fx_mod_data);
  2602. if (!wffx_idle()) return (-1);
  2603. for (i = 0x50; i <= 0x6b; i++) {
  2604. outb (i, dev.fx_mod_addr);
  2605. outb (0x00, dev.fx_mod_data);
  2606. if (!wffx_idle()) return (-1);
  2607. }
  2608. outb (0x6c, dev.fx_mod_addr);
  2609. outb (0x40, dev.fx_mod_data);
  2610. if (!wffx_idle()) return (-1);
  2611. outb (0x6d, dev.fx_mod_addr);
  2612. outb (0x00, dev.fx_mod_data);
  2613. if (!wffx_idle()) return (-1);
  2614. outb (0x6e, dev.fx_mod_addr);
  2615. outb (0x40, dev.fx_mod_data);
  2616. if (!wffx_idle()) return (-1);
  2617. outb (0x6f, dev.fx_mod_addr);
  2618. outb (0x40, dev.fx_mod_data);
  2619. if (!wffx_idle()) return (-1);
  2620. for (i = 0x70; i <= 0x7f; i++) {
  2621. outb (i, dev.fx_mod_addr);
  2622. outb (0xc0, dev.fx_mod_data);
  2623. if (!wffx_idle()) return (-1);
  2624. }
  2625. for (i = 0x80; i <= 0xaf; i++) {
  2626. outb (i, dev.fx_mod_addr);
  2627. outb (0x00, dev.fx_mod_data);
  2628. if (!wffx_idle()) return (-1);
  2629. }
  2630. for (i = 0xc0; i <= 0xdd; i++) {
  2631. outb (i, dev.fx_mod_addr);
  2632. outb (0x00, dev.fx_mod_data);
  2633. if (!wffx_idle()) return (-1);
  2634. }
  2635. outb (0xde, dev.fx_mod_addr);
  2636. outb (0x10, dev.fx_mod_data);
  2637. if (!wffx_idle()) return (-1);
  2638. outb (0xdf, dev.fx_mod_addr);
  2639. outb (0x10, dev.fx_mod_data);
  2640. if (!wffx_idle()) return (-1);
  2641. for (i = 0xe0; i <= 0xef; i++) {
  2642. outb (i, dev.fx_mod_addr);
  2643. outb (0x00, dev.fx_mod_data);
  2644. if (!wffx_idle()) return (-1);
  2645. }
  2646. for (i = 0x00; i <= 0x0f; i++) {
  2647. outb (0x01, dev.fx_mod_addr);
  2648. outb (i, dev.fx_mod_data);
  2649. outb (0x02, dev.fx_mod_addr);
  2650. outb (0x01, dev.fx_mod_data);
  2651. if (!wffx_idle()) return (-1);
  2652. }
  2653. outb (0x02, dev.fx_op); /* mute on */
  2654. /* Now set the coefficients and so forth for the programs above */
  2655. for (i = 0; i < sizeof (coefficients); i += 4) {
  2656. outb (coefficients[i], dev.fx_dsp_page);
  2657. outb (coefficients[i+1], dev.fx_dsp_addr);
  2658. outb (coefficients[i+2], dev.fx_dsp_msb);
  2659. outb (coefficients[i+3], dev.fx_dsp_lsb);
  2660. if (!wffx_idle()) return (-1);
  2661. }
  2662. /* Some settings (?) that are too small to bundle into loops */
  2663. if (!wffx_idle()) return (-1);
  2664. outb (0x1e, dev.fx_mod_addr);
  2665. outb (0x14, dev.fx_mod_data);
  2666. if (!wffx_idle()) return (-1);
  2667. outb (0xde, dev.fx_mod_addr);
  2668. outb (0x20, dev.fx_mod_data);
  2669. if (!wffx_idle()) return (-1);
  2670. outb (0xdf, dev.fx_mod_addr);
  2671. outb (0x20, dev.fx_mod_data);
  2672. /* some more coefficients */
  2673. if (!wffx_idle()) return (-1);
  2674. outb (0x06, dev.fx_dsp_page);
  2675. outb (0x78, dev.fx_dsp_addr);
  2676. outb (0x00, dev.fx_dsp_msb);
  2677. outb (0x40, dev.fx_dsp_lsb);
  2678. if (!wffx_idle()) return (-1);
  2679. outb (0x07, dev.fx_dsp_page);
  2680. outb (0x03, dev.fx_dsp_addr);
  2681. outb (0x0f, dev.fx_dsp_msb);
  2682. outb (0xff, dev.fx_dsp_lsb);
  2683. if (!wffx_idle()) return (-1);
  2684. outb (0x07, dev.fx_dsp_page);
  2685. outb (0x0b, dev.fx_dsp_addr);
  2686. outb (0x0f, dev.fx_dsp_msb);
  2687. outb (0xff, dev.fx_dsp_lsb);
  2688. if (!wffx_idle()) return (-1);
  2689. outb (0x07, dev.fx_dsp_page);
  2690. outb (0x02, dev.fx_dsp_addr);
  2691. outb (0x00, dev.fx_dsp_msb);
  2692. outb (0x00, dev.fx_dsp_lsb);
  2693. if (!wffx_idle()) return (-1);
  2694. outb (0x07, dev.fx_dsp_page);
  2695. outb (0x0a, dev.fx_dsp_addr);
  2696. outb (0x00, dev.fx_dsp_msb);
  2697. outb (0x00, dev.fx_dsp_lsb);
  2698. if (!wffx_idle()) return (-1);
  2699. outb (0x07, dev.fx_dsp_page);
  2700. outb (0x46, dev.fx_dsp_addr);
  2701. outb (0x00, dev.fx_dsp_msb);
  2702. outb (0x00, dev.fx_dsp_lsb);
  2703. if (!wffx_idle()) return (-1);
  2704. outb (0x07, dev.fx_dsp_page);
  2705. outb (0x49, dev.fx_dsp_addr);
  2706. outb (0x00, dev.fx_dsp_msb);
  2707. outb (0x00, dev.fx_dsp_lsb);
  2708. /* Now, for some strange reason, lets reload every page
  2709. and all the coefficients over again. I have *NO* idea
  2710. why this is done. I do know that no sound is produced
  2711. is this phase is omitted.
  2712. */
  2713. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2714. outb (0x00, dev.fx_dsp_page);
  2715. outb (0x10, dev.fx_dsp_addr);
  2716. for (i = 0; i < sizeof (page_zero_v2); i += 2) {
  2717. outb (page_zero_v2[i], dev.fx_dsp_msb);
  2718. outb (page_zero_v2[i+1], dev.fx_dsp_lsb);
  2719. if (!wffx_idle()) return (-1);
  2720. }
  2721. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2722. outb (0x01, dev.fx_dsp_page);
  2723. outb (0x10, dev.fx_dsp_addr);
  2724. for (i = 0; i < sizeof (page_one_v2); i += 2) {
  2725. outb (page_one_v2[i], dev.fx_dsp_msb);
  2726. outb (page_one_v2[i+1], dev.fx_dsp_lsb);
  2727. if (!wffx_idle()) return (-1);
  2728. }
  2729. if (!wffx_idle()) return (-1);
  2730. if (!wffx_idle()) return (-1);
  2731. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2732. outb (0x02, dev.fx_dsp_page);
  2733. outb (0x10, dev.fx_dsp_addr);
  2734. for (i = 0; i < sizeof (page_two_v2); i++) {
  2735. outb (page_two_v2[i], dev.fx_dsp_lsb);
  2736. if (!wffx_idle()) return (-1);
  2737. }
  2738. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2739. outb (0x03, dev.fx_dsp_page);
  2740. outb (0x10, dev.fx_dsp_addr);
  2741. for (i = 0; i < sizeof (page_three_v2); i++) {
  2742. outb (page_three_v2[i], dev.fx_dsp_lsb);
  2743. if (!wffx_idle()) return (-1);
  2744. }
  2745. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2746. outb (0x04, dev.fx_dsp_page);
  2747. outb (0x10, dev.fx_dsp_addr);
  2748. for (i = 0; i < sizeof (page_four_v2); i++) {
  2749. outb (page_four_v2[i], dev.fx_dsp_lsb);
  2750. if (!wffx_idle()) return (-1);
  2751. }
  2752. outb (FX_LSB_TRANSFER, dev.fx_lcr);
  2753. outb (0x06, dev.fx_dsp_page);
  2754. /* Page six v.2 is algorithmic */
  2755. for (i = 0x10; i <= 0x3e; i += 2) {
  2756. outb (i, dev.fx_dsp_addr);
  2757. outb (0x00, dev.fx_dsp_msb);
  2758. outb (0x00, dev.fx_dsp_lsb);
  2759. if (!wffx_idle()) return (-1);
  2760. }
  2761. outb (FX_AUTO_INCR|FX_LSB_TRANSFER, dev.fx_lcr);
  2762. outb (0x07, dev.fx_dsp_page);
  2763. outb (0x10, dev.fx_dsp_addr);
  2764. for (i = 0; i < sizeof (page_seven_v2); i += 2) {
  2765. outb (page_seven_v2[i], dev.fx_dsp_msb);
  2766. outb (page_seven_v2[i+1], dev.fx_dsp_lsb);
  2767. if (!wffx_idle()) return (-1);
  2768. }
  2769. for (i = 0x00; i < sizeof(mod_v2); i += 2) {
  2770. outb (mod_v2[i], dev.fx_mod_addr);
  2771. outb (mod_v2[i+1], dev.fx_mod_data);
  2772. if (!wffx_idle()) return (-1);
  2773. }
  2774. for (i = 0; i < sizeof (coefficients2); i += 4) {
  2775. outb (coefficients2[i], dev.fx_dsp_page);
  2776. outb (coefficients2[i+1], dev.fx_dsp_addr);
  2777. outb (coefficients2[i+2], dev.fx_dsp_msb);
  2778. outb (coefficients2[i+3], dev.fx_dsp_lsb);
  2779. if (!wffx_idle()) return (-1);
  2780. }
  2781. for (i = 0; i < sizeof (coefficients3); i += 2) {
  2782. int x;
  2783. outb (0x07, dev.fx_dsp_page);
  2784. x = (i % 4) ? 0x4e : 0x4c;
  2785. outb (x, dev.fx_dsp_addr);
  2786. outb (coefficients3[i], dev.fx_dsp_msb);
  2787. outb (coefficients3[i+1], dev.fx_dsp_lsb);
  2788. }
  2789. outb (0x00, dev.fx_op); /* mute off */
  2790. if (!wffx_idle()) return (-1);
  2791. return (0);
  2792. }
  2793. static int io = -1;
  2794. static int irq = -1;
  2795. MODULE_AUTHOR ("Paul Barton-Davis <pbd@op.net>");
  2796. MODULE_DESCRIPTION ("Turtle Beach WaveFront Linux Driver");
  2797. MODULE_LICENSE("GPL");
  2798. module_param (io, int, 0);
  2799. module_param (irq, int, 0);
  2800. static int __init init_wavfront (void)
  2801. {
  2802. printk ("Turtle Beach WaveFront Driver\n"
  2803. "Copyright (C) by Hannu Solvainen, "
  2804. "Paul Barton-Davis 1993-1998.\n");
  2805. /* XXX t'would be lovely to ask the CS4232 for these values, eh ? */
  2806. if (io == -1 || irq == -1) {
  2807. printk (KERN_INFO LOGNAME "irq and io options must be set.\n");
  2808. return -EINVAL;
  2809. }
  2810. if (wavefront_interrupt_bits (irq) < 0) {
  2811. printk (KERN_INFO LOGNAME
  2812. "IRQ must be 9, 5, 12 or 15 (not %d)\n", irq);
  2813. return -ENODEV;
  2814. }
  2815. if (detect_wavefront (irq, io) < 0) {
  2816. return -ENODEV;
  2817. }
  2818. if (install_wavefront () < 0) {
  2819. return -EIO;
  2820. }
  2821. return 0;
  2822. }
  2823. static void __exit cleanup_wavfront (void)
  2824. {
  2825. uninstall_wavefront ();
  2826. }
  2827. module_init(init_wavfront);
  2828. module_exit(cleanup_wavfront);