wavefront_synth.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /* Copyright (C) by Paul Barton-Davis 1998-1999
  2. *
  3. * Some portions of this file are taken from work that is
  4. * copyright (C) by Hannu Savolainen 1993-1996
  5. *
  6. * This program is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  7. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  8. * for more info.
  9. */
  10. /*
  11. * An ALSA lowlevel driver for Turtle Beach ICS2115 wavetable synth
  12. * (Maui, Tropez, Tropez Plus)
  13. *
  14. * This driver supports the onboard wavetable synthesizer (an ICS2115),
  15. * including patch, sample and program loading and unloading, conversion
  16. * of GUS patches during loading, and full user-level access to all
  17. * WaveFront commands. It tries to provide semi-intelligent patch and
  18. * sample management as well.
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/time.h>
  27. #include <linux/wait.h>
  28. #include <linux/moduleparam.h>
  29. #include <sound/core.h>
  30. #include <sound/snd_wavefront.h>
  31. #include <sound/initval.h>
  32. static int wf_raw = 0; /* we normally check for "raw state" to firmware
  33. loading. if non-zero, then during driver loading, the
  34. state of the board is ignored, and we reset the
  35. board and load the firmware anyway.
  36. */
  37. static int fx_raw = 1; /* if this is zero, we'll leave the FX processor in
  38. whatever state it is when the driver is loaded.
  39. The default is to download the microprogram and
  40. associated coefficients to set it up for "default"
  41. operation, whatever that means.
  42. */
  43. static int debug_default = 0; /* you can set this to control debugging
  44. during driver loading. it takes any combination
  45. of the WF_DEBUG_* flags defined in
  46. wavefront.h
  47. */
  48. /* XXX this needs to be made firmware and hardware version dependent */
  49. static char *ospath = "/etc/sound/wavefront.os"; /* where to find a processed
  50. version of the WaveFront OS
  51. */
  52. static int wait_usecs = 150; /* This magic number seems to give pretty optimal
  53. throughput based on my limited experimentation.
  54. If you want to play around with it and find a better
  55. value, be my guest. Remember, the idea is to
  56. get a number that causes us to just busy wait
  57. for as many WaveFront commands as possible, without
  58. coming up with a number so large that we hog the
  59. whole CPU.
  60. Specifically, with this number, out of about 134,000
  61. status waits, only about 250 result in a sleep.
  62. */
  63. static int sleep_interval = 100; /* HZ/sleep_interval seconds per sleep */
  64. static int sleep_tries = 50; /* number of times we'll try to sleep */
  65. static int reset_time = 2; /* hundreths of a second we wait after a HW
  66. reset for the expected interrupt.
  67. */
  68. static int ramcheck_time = 20; /* time in seconds to wait while ROM code
  69. checks on-board RAM.
  70. */
  71. static int osrun_time = 10; /* time in seconds we wait for the OS to
  72. start running.
  73. */
  74. module_param(wf_raw, int, 0444);
  75. MODULE_PARM_DESC(wf_raw, "if non-zero, assume that we need to boot the OS");
  76. module_param(fx_raw, int, 0444);
  77. MODULE_PARM_DESC(fx_raw, "if non-zero, assume that the FX process needs help");
  78. module_param(debug_default, int, 0444);
  79. MODULE_PARM_DESC(debug_default, "debug parameters for card initialization");
  80. module_param(wait_usecs, int, 0444);
  81. MODULE_PARM_DESC(wait_usecs, "how long to wait without sleeping, usecs");
  82. module_param(sleep_interval, int, 0444);
  83. MODULE_PARM_DESC(sleep_interval, "how long to sleep when waiting for reply");
  84. module_param(sleep_tries, int, 0444);
  85. MODULE_PARM_DESC(sleep_tries, "how many times to try sleeping during a wait");
  86. module_param(ospath, charp, 0444);
  87. MODULE_PARM_DESC(ospath, "full pathname to processed ICS2115 OS firmware");
  88. module_param(reset_time, int, 0444);
  89. MODULE_PARM_DESC(reset_time, "how long to wait for a reset to take effect");
  90. module_param(ramcheck_time, int, 0444);
  91. MODULE_PARM_DESC(ramcheck_time, "how many seconds to wait for the RAM test");
  92. module_param(osrun_time, int, 0444);
  93. MODULE_PARM_DESC(osrun_time, "how many seconds to wait for the ICS2115 OS");
  94. /* if WF_DEBUG not defined, no run-time debugging messages will
  95. be available via the debug flag setting. Given the current
  96. beta state of the driver, this will remain set until a future
  97. version.
  98. */
  99. #define WF_DEBUG 1
  100. #ifdef WF_DEBUG
  101. #define DPRINT(cond, ...) \
  102. if ((dev->debug & (cond)) == (cond)) { \
  103. snd_printk (__VA_ARGS__); \
  104. }
  105. #else
  106. #define DPRINT(cond, args...)
  107. #endif /* WF_DEBUG */
  108. #define LOGNAME "WaveFront: "
  109. /* bitmasks for WaveFront status port value */
  110. #define STAT_RINTR_ENABLED 0x01
  111. #define STAT_CAN_READ 0x02
  112. #define STAT_INTR_READ 0x04
  113. #define STAT_WINTR_ENABLED 0x10
  114. #define STAT_CAN_WRITE 0x20
  115. #define STAT_INTR_WRITE 0x40
  116. static int wavefront_delete_sample (snd_wavefront_t *, int sampnum);
  117. static int wavefront_find_free_sample (snd_wavefront_t *);
  118. struct wavefront_command {
  119. int cmd;
  120. char *action;
  121. unsigned int read_cnt;
  122. unsigned int write_cnt;
  123. int need_ack;
  124. };
  125. static struct {
  126. int errno;
  127. const char *errstr;
  128. } wavefront_errors[] = {
  129. { 0x01, "Bad sample number" },
  130. { 0x02, "Out of sample memory" },
  131. { 0x03, "Bad patch number" },
  132. { 0x04, "Error in number of voices" },
  133. { 0x06, "Sample load already in progress" },
  134. { 0x0B, "No sample load request pending" },
  135. { 0x0E, "Bad MIDI channel number" },
  136. { 0x10, "Download Record Error" },
  137. { 0x80, "Success" },
  138. { 0x0 }
  139. };
  140. #define NEEDS_ACK 1
  141. static struct wavefront_command wavefront_commands[] = {
  142. { WFC_SET_SYNTHVOL, "set synthesizer volume", 0, 1, NEEDS_ACK },
  143. { WFC_GET_SYNTHVOL, "get synthesizer volume", 1, 0, 0},
  144. { WFC_SET_NVOICES, "set number of voices", 0, 1, NEEDS_ACK },
  145. { WFC_GET_NVOICES, "get number of voices", 1, 0, 0 },
  146. { WFC_SET_TUNING, "set synthesizer tuning", 0, 2, NEEDS_ACK },
  147. { WFC_GET_TUNING, "get synthesizer tuning", 2, 0, 0 },
  148. { WFC_DISABLE_CHANNEL, "disable synth channel", 0, 1, NEEDS_ACK },
  149. { WFC_ENABLE_CHANNEL, "enable synth channel", 0, 1, NEEDS_ACK },
  150. { WFC_GET_CHANNEL_STATUS, "get synth channel status", 3, 0, 0 },
  151. { WFC_MISYNTH_OFF, "disable midi-in to synth", 0, 0, NEEDS_ACK },
  152. { WFC_MISYNTH_ON, "enable midi-in to synth", 0, 0, NEEDS_ACK },
  153. { WFC_VMIDI_ON, "enable virtual midi mode", 0, 0, NEEDS_ACK },
  154. { WFC_VMIDI_OFF, "disable virtual midi mode", 0, 0, NEEDS_ACK },
  155. { WFC_MIDI_STATUS, "report midi status", 1, 0, 0 },
  156. { WFC_FIRMWARE_VERSION, "report firmware version", 2, 0, 0 },
  157. { WFC_HARDWARE_VERSION, "report hardware version", 2, 0, 0 },
  158. { WFC_GET_NSAMPLES, "report number of samples", 2, 0, 0 },
  159. { WFC_INSTOUT_LEVELS, "report instantaneous output levels", 7, 0, 0 },
  160. { WFC_PEAKOUT_LEVELS, "report peak output levels", 7, 0, 0 },
  161. { WFC_DOWNLOAD_SAMPLE, "download sample",
  162. 0, WF_SAMPLE_BYTES, NEEDS_ACK },
  163. { WFC_DOWNLOAD_BLOCK, "download block", 0, 0, NEEDS_ACK},
  164. { WFC_DOWNLOAD_SAMPLE_HEADER, "download sample header",
  165. 0, WF_SAMPLE_HDR_BYTES, NEEDS_ACK },
  166. { WFC_UPLOAD_SAMPLE_HEADER, "upload sample header", 13, 2, 0 },
  167. /* This command requires a variable number of bytes to be written.
  168. There is a hack in snd_wavefront_cmd() to support this. The actual
  169. count is passed in as the read buffer ptr, cast appropriately.
  170. Ugh.
  171. */
  172. { WFC_DOWNLOAD_MULTISAMPLE, "download multisample", 0, 0, NEEDS_ACK },
  173. /* This one is a hack as well. We just read the first byte of the
  174. response, don't fetch an ACK, and leave the rest to the
  175. calling function. Ugly, ugly, ugly.
  176. */
  177. { WFC_UPLOAD_MULTISAMPLE, "upload multisample", 2, 1, 0 },
  178. { WFC_DOWNLOAD_SAMPLE_ALIAS, "download sample alias",
  179. 0, WF_ALIAS_BYTES, NEEDS_ACK },
  180. { WFC_UPLOAD_SAMPLE_ALIAS, "upload sample alias", WF_ALIAS_BYTES, 2, 0},
  181. { WFC_DELETE_SAMPLE, "delete sample", 0, 2, NEEDS_ACK },
  182. { WFC_IDENTIFY_SAMPLE_TYPE, "identify sample type", 5, 2, 0 },
  183. { WFC_UPLOAD_SAMPLE_PARAMS, "upload sample parameters" },
  184. { WFC_REPORT_FREE_MEMORY, "report free memory", 4, 0, 0 },
  185. { WFC_DOWNLOAD_PATCH, "download patch", 0, 134, NEEDS_ACK },
  186. { WFC_UPLOAD_PATCH, "upload patch", 132, 2, 0 },
  187. { WFC_DOWNLOAD_PROGRAM, "download program", 0, 33, NEEDS_ACK },
  188. { WFC_UPLOAD_PROGRAM, "upload program", 32, 1, 0 },
  189. { WFC_DOWNLOAD_EDRUM_PROGRAM, "download enhanced drum program", 0, 9,
  190. NEEDS_ACK},
  191. { WFC_UPLOAD_EDRUM_PROGRAM, "upload enhanced drum program", 8, 1, 0},
  192. { WFC_SET_EDRUM_CHANNEL, "set enhanced drum program channel",
  193. 0, 1, NEEDS_ACK },
  194. { WFC_DISABLE_DRUM_PROGRAM, "disable drum program", 0, 1, NEEDS_ACK },
  195. { WFC_REPORT_CHANNEL_PROGRAMS, "report channel program numbers",
  196. 32, 0, 0 },
  197. { WFC_NOOP, "the no-op command", 0, 0, NEEDS_ACK },
  198. { 0x00 }
  199. };
  200. static const char *
  201. wavefront_errorstr (int errnum)
  202. {
  203. int i;
  204. for (i = 0; wavefront_errors[i].errstr; i++) {
  205. if (wavefront_errors[i].errno == errnum) {
  206. return wavefront_errors[i].errstr;
  207. }
  208. }
  209. return "Unknown WaveFront error";
  210. }
  211. static struct wavefront_command *
  212. wavefront_get_command (int cmd)
  213. {
  214. int i;
  215. for (i = 0; wavefront_commands[i].cmd != 0; i++) {
  216. if (cmd == wavefront_commands[i].cmd) {
  217. return &wavefront_commands[i];
  218. }
  219. }
  220. return NULL;
  221. }
  222. static inline int
  223. wavefront_status (snd_wavefront_t *dev)
  224. {
  225. return inb (dev->status_port);
  226. }
  227. static int
  228. wavefront_sleep (int limit)
  229. {
  230. schedule_timeout_interruptible(limit);
  231. return signal_pending(current);
  232. }
  233. static int
  234. wavefront_wait (snd_wavefront_t *dev, int mask)
  235. {
  236. int i;
  237. /* Spin for a short period of time, because >99% of all
  238. requests to the WaveFront can be serviced inline like this.
  239. */
  240. for (i = 0; i < wait_usecs; i += 5) {
  241. if (wavefront_status (dev) & mask) {
  242. return 1;
  243. }
  244. udelay(5);
  245. }
  246. for (i = 0; i < sleep_tries; i++) {
  247. if (wavefront_status (dev) & mask) {
  248. return 1;
  249. }
  250. if (wavefront_sleep (HZ/sleep_interval)) {
  251. return (0);
  252. }
  253. }
  254. return (0);
  255. }
  256. static int
  257. wavefront_read (snd_wavefront_t *dev)
  258. {
  259. if (wavefront_wait (dev, STAT_CAN_READ))
  260. return inb (dev->data_port);
  261. DPRINT (WF_DEBUG_DATA, "read timeout.\n");
  262. return -1;
  263. }
  264. static int
  265. wavefront_write (snd_wavefront_t *dev, unsigned char data)
  266. {
  267. if (wavefront_wait (dev, STAT_CAN_WRITE)) {
  268. outb (data, dev->data_port);
  269. return 0;
  270. }
  271. DPRINT (WF_DEBUG_DATA, "write timeout.\n");
  272. return -1;
  273. }
  274. int
  275. snd_wavefront_cmd (snd_wavefront_t *dev,
  276. int cmd, unsigned char *rbuf, unsigned char *wbuf)
  277. {
  278. int ack;
  279. unsigned int i;
  280. int c;
  281. struct wavefront_command *wfcmd;
  282. if ((wfcmd = wavefront_get_command (cmd)) == NULL) {
  283. snd_printk ("command 0x%x not supported.\n",
  284. cmd);
  285. return 1;
  286. }
  287. /* Hack to handle the one variable-size write command. See
  288. wavefront_send_multisample() for the other half of this
  289. gross and ugly strategy.
  290. */
  291. if (cmd == WFC_DOWNLOAD_MULTISAMPLE) {
  292. wfcmd->write_cnt = (unsigned long) rbuf;
  293. rbuf = NULL;
  294. }
  295. DPRINT (WF_DEBUG_CMD, "0x%x [%s] (%d,%d,%d)\n",
  296. cmd, wfcmd->action, wfcmd->read_cnt,
  297. wfcmd->write_cnt, wfcmd->need_ack);
  298. if (wavefront_write (dev, cmd)) {
  299. DPRINT ((WF_DEBUG_IO|WF_DEBUG_CMD), "cannot request "
  300. "0x%x [%s].\n",
  301. cmd, wfcmd->action);
  302. return 1;
  303. }
  304. if (wfcmd->write_cnt > 0) {
  305. DPRINT (WF_DEBUG_DATA, "writing %d bytes "
  306. "for 0x%x\n",
  307. wfcmd->write_cnt, cmd);
  308. for (i = 0; i < wfcmd->write_cnt; i++) {
  309. if (wavefront_write (dev, wbuf[i])) {
  310. DPRINT (WF_DEBUG_IO, "bad write for byte "
  311. "%d of 0x%x [%s].\n",
  312. i, cmd, wfcmd->action);
  313. return 1;
  314. }
  315. DPRINT (WF_DEBUG_DATA, "write[%d] = 0x%x\n",
  316. i, wbuf[i]);
  317. }
  318. }
  319. if (wfcmd->read_cnt > 0) {
  320. DPRINT (WF_DEBUG_DATA, "reading %d ints "
  321. "for 0x%x\n",
  322. wfcmd->read_cnt, cmd);
  323. for (i = 0; i < wfcmd->read_cnt; i++) {
  324. if ((c = wavefront_read (dev)) == -1) {
  325. DPRINT (WF_DEBUG_IO, "bad read for byte "
  326. "%d of 0x%x [%s].\n",
  327. i, cmd, wfcmd->action);
  328. return 1;
  329. }
  330. /* Now handle errors. Lots of special cases here */
  331. if (c == 0xff) {
  332. if ((c = wavefront_read (dev)) == -1) {
  333. DPRINT (WF_DEBUG_IO, "bad read for "
  334. "error byte at "
  335. "read byte %d "
  336. "of 0x%x [%s].\n",
  337. i, cmd,
  338. wfcmd->action);
  339. return 1;
  340. }
  341. /* Can you believe this madness ? */
  342. if (c == 1 &&
  343. wfcmd->cmd == WFC_IDENTIFY_SAMPLE_TYPE) {
  344. rbuf[0] = WF_ST_EMPTY;
  345. return (0);
  346. } else if (c == 3 &&
  347. wfcmd->cmd == WFC_UPLOAD_PATCH) {
  348. return 3;
  349. } else if (c == 1 &&
  350. wfcmd->cmd == WFC_UPLOAD_PROGRAM) {
  351. return 1;
  352. } else {
  353. DPRINT (WF_DEBUG_IO, "error %d (%s) "
  354. "during "
  355. "read for byte "
  356. "%d of 0x%x "
  357. "[%s].\n",
  358. c,
  359. wavefront_errorstr (c),
  360. i, cmd,
  361. wfcmd->action);
  362. return 1;
  363. }
  364. } else {
  365. rbuf[i] = c;
  366. }
  367. DPRINT (WF_DEBUG_DATA, "read[%d] = 0x%x\n",i, rbuf[i]);
  368. }
  369. }
  370. if ((wfcmd->read_cnt == 0 && wfcmd->write_cnt == 0) || wfcmd->need_ack) {
  371. DPRINT (WF_DEBUG_CMD, "reading ACK for 0x%x\n", cmd);
  372. /* Some commands need an ACK, but return zero instead
  373. of the standard value.
  374. */
  375. if ((ack = wavefront_read (dev)) == 0) {
  376. ack = WF_ACK;
  377. }
  378. if (ack != WF_ACK) {
  379. if (ack == -1) {
  380. DPRINT (WF_DEBUG_IO, "cannot read ack for "
  381. "0x%x [%s].\n",
  382. cmd, wfcmd->action);
  383. return 1;
  384. } else {
  385. int err = -1; /* something unknown */
  386. if (ack == 0xff) { /* explicit error */
  387. if ((err = wavefront_read (dev)) == -1) {
  388. DPRINT (WF_DEBUG_DATA,
  389. "cannot read err "
  390. "for 0x%x [%s].\n",
  391. cmd, wfcmd->action);
  392. }
  393. }
  394. DPRINT (WF_DEBUG_IO, "0x%x [%s] "
  395. "failed (0x%x, 0x%x, %s)\n",
  396. cmd, wfcmd->action, ack, err,
  397. wavefront_errorstr (err));
  398. return -err;
  399. }
  400. }
  401. DPRINT (WF_DEBUG_DATA, "ack received "
  402. "for 0x%x [%s]\n",
  403. cmd, wfcmd->action);
  404. } else {
  405. DPRINT (WF_DEBUG_CMD, "0x%x [%s] does not need "
  406. "ACK (%d,%d,%d)\n",
  407. cmd, wfcmd->action, wfcmd->read_cnt,
  408. wfcmd->write_cnt, wfcmd->need_ack);
  409. }
  410. return 0;
  411. }
  412. /***********************************************************************
  413. WaveFront data munging
  414. Things here are weird. All data written to the board cannot
  415. have its most significant bit set. Any data item with values
  416. potentially > 0x7F (127) must be split across multiple bytes.
  417. Sometimes, we need to munge numeric values that are represented on
  418. the x86 side as 8-32 bit values. Sometimes, we need to munge data
  419. that is represented on the x86 side as an array of bytes. The most
  420. efficient approach to handling both cases seems to be to use 2
  421. different functions for munging and 2 for de-munging. This avoids
  422. weird casting and worrying about bit-level offsets.
  423. **********************************************************************/
  424. static unsigned char *
  425. munge_int32 (unsigned int src,
  426. unsigned char *dst,
  427. unsigned int dst_size)
  428. {
  429. unsigned int i;
  430. for (i = 0; i < dst_size; i++) {
  431. *dst = src & 0x7F; /* Mask high bit of LSB */
  432. src = src >> 7; /* Rotate Right 7 bits */
  433. /* Note: we leave the upper bits in place */
  434. dst++;
  435. };
  436. return dst;
  437. };
  438. static int
  439. demunge_int32 (unsigned char* src, int src_size)
  440. {
  441. int i;
  442. int outval = 0;
  443. for (i = src_size - 1; i >= 0; i--) {
  444. outval=(outval<<7)+src[i];
  445. }
  446. return outval;
  447. };
  448. static
  449. unsigned char *
  450. munge_buf (unsigned char *src, unsigned char *dst, unsigned int dst_size)
  451. {
  452. unsigned int i;
  453. unsigned int last = dst_size / 2;
  454. for (i = 0; i < last; i++) {
  455. *dst++ = src[i] & 0x7f;
  456. *dst++ = src[i] >> 7;
  457. }
  458. return dst;
  459. }
  460. static
  461. unsigned char *
  462. demunge_buf (unsigned char *src, unsigned char *dst, unsigned int src_bytes)
  463. {
  464. int i;
  465. unsigned char *end = src + src_bytes;
  466. end = src + src_bytes;
  467. /* NOTE: src and dst *CAN* point to the same address */
  468. for (i = 0; src != end; i++) {
  469. dst[i] = *src++;
  470. dst[i] |= (*src++)<<7;
  471. }
  472. return dst;
  473. }
  474. /***********************************************************************
  475. WaveFront: sample, patch and program management.
  476. ***********************************************************************/
  477. static int
  478. wavefront_delete_sample (snd_wavefront_t *dev, int sample_num)
  479. {
  480. unsigned char wbuf[2];
  481. int x;
  482. wbuf[0] = sample_num & 0x7f;
  483. wbuf[1] = sample_num >> 7;
  484. if ((x = snd_wavefront_cmd (dev, WFC_DELETE_SAMPLE, NULL, wbuf)) == 0) {
  485. dev->sample_status[sample_num] = WF_ST_EMPTY;
  486. }
  487. return x;
  488. }
  489. static int
  490. wavefront_get_sample_status (snd_wavefront_t *dev, int assume_rom)
  491. {
  492. int i;
  493. unsigned char rbuf[32], wbuf[32];
  494. unsigned int sc_real, sc_alias, sc_multi;
  495. /* check sample status */
  496. if (snd_wavefront_cmd (dev, WFC_GET_NSAMPLES, rbuf, wbuf)) {
  497. snd_printk ("cannot request sample count.\n");
  498. return -1;
  499. }
  500. sc_real = sc_alias = sc_multi = dev->samples_used = 0;
  501. for (i = 0; i < WF_MAX_SAMPLE; i++) {
  502. wbuf[0] = i & 0x7f;
  503. wbuf[1] = i >> 7;
  504. if (snd_wavefront_cmd (dev, WFC_IDENTIFY_SAMPLE_TYPE, rbuf, wbuf)) {
  505. snd_printk("cannot identify sample "
  506. "type of slot %d\n", i);
  507. dev->sample_status[i] = WF_ST_EMPTY;
  508. continue;
  509. }
  510. dev->sample_status[i] = (WF_SLOT_FILLED|rbuf[0]);
  511. if (assume_rom) {
  512. dev->sample_status[i] |= WF_SLOT_ROM;
  513. }
  514. switch (rbuf[0] & WF_ST_MASK) {
  515. case WF_ST_SAMPLE:
  516. sc_real++;
  517. break;
  518. case WF_ST_MULTISAMPLE:
  519. sc_multi++;
  520. break;
  521. case WF_ST_ALIAS:
  522. sc_alias++;
  523. break;
  524. case WF_ST_EMPTY:
  525. break;
  526. default:
  527. snd_printk ("unknown sample type for "
  528. "slot %d (0x%x)\n",
  529. i, rbuf[0]);
  530. }
  531. if (rbuf[0] != WF_ST_EMPTY) {
  532. dev->samples_used++;
  533. }
  534. }
  535. snd_printk ("%d samples used (%d real, %d aliases, %d multi), "
  536. "%d empty\n", dev->samples_used, sc_real, sc_alias, sc_multi,
  537. WF_MAX_SAMPLE - dev->samples_used);
  538. return (0);
  539. }
  540. static int
  541. wavefront_get_patch_status (snd_wavefront_t *dev)
  542. {
  543. unsigned char patchbuf[WF_PATCH_BYTES];
  544. unsigned char patchnum[2];
  545. wavefront_patch *p;
  546. int i, x, cnt, cnt2;
  547. for (i = 0; i < WF_MAX_PATCH; i++) {
  548. patchnum[0] = i & 0x7f;
  549. patchnum[1] = i >> 7;
  550. if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PATCH, patchbuf,
  551. patchnum)) == 0) {
  552. dev->patch_status[i] |= WF_SLOT_FILLED;
  553. p = (wavefront_patch *) patchbuf;
  554. dev->sample_status
  555. [p->sample_number|(p->sample_msb<<7)] |=
  556. WF_SLOT_USED;
  557. } else if (x == 3) { /* Bad patch number */
  558. dev->patch_status[i] = 0;
  559. } else {
  560. snd_printk ("upload patch "
  561. "error 0x%x\n", x);
  562. dev->patch_status[i] = 0;
  563. return 1;
  564. }
  565. }
  566. /* program status has already filled in slot_used bits */
  567. for (i = 0, cnt = 0, cnt2 = 0; i < WF_MAX_PATCH; i++) {
  568. if (dev->patch_status[i] & WF_SLOT_FILLED) {
  569. cnt++;
  570. }
  571. if (dev->patch_status[i] & WF_SLOT_USED) {
  572. cnt2++;
  573. }
  574. }
  575. snd_printk ("%d patch slots filled, %d in use\n", cnt, cnt2);
  576. return (0);
  577. }
  578. static int
  579. wavefront_get_program_status (snd_wavefront_t *dev)
  580. {
  581. unsigned char progbuf[WF_PROGRAM_BYTES];
  582. wavefront_program prog;
  583. unsigned char prognum;
  584. int i, x, l, cnt;
  585. for (i = 0; i < WF_MAX_PROGRAM; i++) {
  586. prognum = i;
  587. if ((x = snd_wavefront_cmd (dev, WFC_UPLOAD_PROGRAM, progbuf,
  588. &prognum)) == 0) {
  589. dev->prog_status[i] |= WF_SLOT_USED;
  590. demunge_buf (progbuf, (unsigned char *) &prog,
  591. WF_PROGRAM_BYTES);
  592. for (l = 0; l < WF_NUM_LAYERS; l++) {
  593. if (prog.layer[l].mute) {
  594. dev->patch_status
  595. [prog.layer[l].patch_number] |=
  596. WF_SLOT_USED;
  597. }
  598. }
  599. } else if (x == 1) { /* Bad program number */
  600. dev->prog_status[i] = 0;
  601. } else {
  602. snd_printk ("upload program "
  603. "error 0x%x\n", x);
  604. dev->prog_status[i] = 0;
  605. }
  606. }
  607. for (i = 0, cnt = 0; i < WF_MAX_PROGRAM; i++) {
  608. if (dev->prog_status[i]) {
  609. cnt++;
  610. }
  611. }
  612. snd_printk ("%d programs slots in use\n", cnt);
  613. return (0);
  614. }
  615. static int
  616. wavefront_send_patch (snd_wavefront_t *dev, wavefront_patch_info *header)
  617. {
  618. unsigned char buf[WF_PATCH_BYTES+2];
  619. unsigned char *bptr;
  620. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading patch %d\n",
  621. header->number);
  622. dev->patch_status[header->number] |= WF_SLOT_FILLED;
  623. bptr = buf;
  624. bptr = munge_int32 (header->number, buf, 2);
  625. munge_buf ((unsigned char *)&header->hdr.p, bptr, WF_PATCH_BYTES);
  626. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PATCH, NULL, buf)) {
  627. snd_printk ("download patch failed\n");
  628. return -(EIO);
  629. }
  630. return (0);
  631. }
  632. static int
  633. wavefront_send_program (snd_wavefront_t *dev, wavefront_patch_info *header)
  634. {
  635. unsigned char buf[WF_PROGRAM_BYTES+1];
  636. int i;
  637. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading program %d\n",
  638. header->number);
  639. dev->prog_status[header->number] = WF_SLOT_USED;
  640. /* XXX need to zero existing SLOT_USED bit for program_status[i]
  641. where `i' is the program that's being (potentially) overwritten.
  642. */
  643. for (i = 0; i < WF_NUM_LAYERS; i++) {
  644. if (header->hdr.pr.layer[i].mute) {
  645. dev->patch_status[header->hdr.pr.layer[i].patch_number] |=
  646. WF_SLOT_USED;
  647. /* XXX need to mark SLOT_USED for sample used by
  648. patch_number, but this means we have to load it. Ick.
  649. */
  650. }
  651. }
  652. buf[0] = header->number;
  653. munge_buf ((unsigned char *)&header->hdr.pr, &buf[1], WF_PROGRAM_BYTES);
  654. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_PROGRAM, NULL, buf)) {
  655. snd_printk ("download patch failed\n");
  656. return -(EIO);
  657. }
  658. return (0);
  659. }
  660. static int
  661. wavefront_freemem (snd_wavefront_t *dev)
  662. {
  663. char rbuf[8];
  664. if (snd_wavefront_cmd (dev, WFC_REPORT_FREE_MEMORY, rbuf, NULL)) {
  665. snd_printk ("can't get memory stats.\n");
  666. return -1;
  667. } else {
  668. return demunge_int32 (rbuf, 4);
  669. }
  670. }
  671. static int
  672. wavefront_send_sample (snd_wavefront_t *dev,
  673. wavefront_patch_info *header,
  674. u16 __user *dataptr,
  675. int data_is_unsigned)
  676. {
  677. /* samples are downloaded via a 16-bit wide i/o port
  678. (you could think of it as 2 adjacent 8-bit wide ports
  679. but its less efficient that way). therefore, all
  680. the blocksizes and so forth listed in the documentation,
  681. and used conventionally to refer to sample sizes,
  682. which are given in 8-bit units (bytes), need to be
  683. divided by 2.
  684. */
  685. u16 sample_short = 0;
  686. u32 length;
  687. u16 __user *data_end = NULL;
  688. unsigned int i;
  689. const unsigned int max_blksize = 4096/2;
  690. unsigned int written;
  691. unsigned int blocksize;
  692. int dma_ack;
  693. int blocknum;
  694. unsigned char sample_hdr[WF_SAMPLE_HDR_BYTES];
  695. unsigned char *shptr;
  696. int skip = 0;
  697. int initial_skip = 0;
  698. DPRINT (WF_DEBUG_LOAD_PATCH, "sample %sdownload for slot %d, "
  699. "type %d, %d bytes from 0x%lx\n",
  700. header->size ? "" : "header ",
  701. header->number, header->subkey,
  702. header->size,
  703. (unsigned long) header->dataptr);
  704. if (header->number == WAVEFRONT_FIND_FREE_SAMPLE_SLOT) {
  705. int x;
  706. if ((x = wavefront_find_free_sample (dev)) < 0) {
  707. return -ENOMEM;
  708. }
  709. snd_printk ("unspecified sample => %d\n", x);
  710. header->number = x;
  711. }
  712. if (header->size) {
  713. /* XXX it's a debatable point whether or not RDONLY semantics
  714. on the ROM samples should cover just the sample data or
  715. the sample header. For now, it only covers the sample data,
  716. so anyone is free at all times to rewrite sample headers.
  717. My reason for this is that we have the sample headers
  718. available in the WFB file for General MIDI, and so these
  719. can always be reset if needed. The sample data, however,
  720. cannot be recovered without a complete reset and firmware
  721. reload of the ICS2115, which is a very expensive operation.
  722. So, doing things this way allows us to honor the notion of
  723. "RESETSAMPLES" reasonably cheaply. Note however, that this
  724. is done purely at user level: there is no WFB parser in
  725. this driver, and so a complete reset (back to General MIDI,
  726. or theoretically some other configuration) is the
  727. responsibility of the user level library.
  728. To try to do this in the kernel would be a little
  729. crazy: we'd need 158K of kernel space just to hold
  730. a copy of the patch/program/sample header data.
  731. */
  732. if (dev->rom_samples_rdonly) {
  733. if (dev->sample_status[header->number] & WF_SLOT_ROM) {
  734. snd_printk ("sample slot %d "
  735. "write protected\n",
  736. header->number);
  737. return -EACCES;
  738. }
  739. }
  740. wavefront_delete_sample (dev, header->number);
  741. }
  742. if (header->size) {
  743. dev->freemem = wavefront_freemem (dev);
  744. if (dev->freemem < (int)header->size) {
  745. snd_printk ("insufficient memory to "
  746. "load %d byte sample.\n",
  747. header->size);
  748. return -ENOMEM;
  749. }
  750. }
  751. skip = WF_GET_CHANNEL(&header->hdr.s);
  752. if (skip > 0 && header->hdr.s.SampleResolution != LINEAR_16BIT) {
  753. snd_printk ("channel selection only "
  754. "possible on 16-bit samples");
  755. return -(EINVAL);
  756. }
  757. switch (skip) {
  758. case 0:
  759. initial_skip = 0;
  760. skip = 1;
  761. break;
  762. case 1:
  763. initial_skip = 0;
  764. skip = 2;
  765. break;
  766. case 2:
  767. initial_skip = 1;
  768. skip = 2;
  769. break;
  770. case 3:
  771. initial_skip = 2;
  772. skip = 3;
  773. break;
  774. case 4:
  775. initial_skip = 3;
  776. skip = 4;
  777. break;
  778. case 5:
  779. initial_skip = 4;
  780. skip = 5;
  781. break;
  782. case 6:
  783. initial_skip = 5;
  784. skip = 6;
  785. break;
  786. }
  787. DPRINT (WF_DEBUG_LOAD_PATCH, "channel selection: %d => "
  788. "initial skip = %d, skip = %d\n",
  789. WF_GET_CHANNEL (&header->hdr.s),
  790. initial_skip, skip);
  791. /* Be safe, and zero the "Unused" bits ... */
  792. WF_SET_CHANNEL(&header->hdr.s, 0);
  793. /* adjust size for 16 bit samples by dividing by two. We always
  794. send 16 bits per write, even for 8 bit samples, so the length
  795. is always half the size of the sample data in bytes.
  796. */
  797. length = header->size / 2;
  798. /* the data we're sent has not been munged, and in fact, the
  799. header we have to send isn't just a munged copy either.
  800. so, build the sample header right here.
  801. */
  802. shptr = &sample_hdr[0];
  803. shptr = munge_int32 (header->number, shptr, 2);
  804. if (header->size) {
  805. shptr = munge_int32 (length, shptr, 4);
  806. }
  807. /* Yes, a 4 byte result doesn't contain all of the offset bits,
  808. but the offset only uses 24 bits.
  809. */
  810. shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleStartOffset),
  811. shptr, 4);
  812. shptr = munge_int32 (*((u32 *) &header->hdr.s.loopStartOffset),
  813. shptr, 4);
  814. shptr = munge_int32 (*((u32 *) &header->hdr.s.loopEndOffset),
  815. shptr, 4);
  816. shptr = munge_int32 (*((u32 *) &header->hdr.s.sampleEndOffset),
  817. shptr, 4);
  818. /* This one is truly weird. What kind of weirdo decided that in
  819. a system dominated by 16 and 32 bit integers, they would use
  820. a just 12 bits ?
  821. */
  822. shptr = munge_int32 (header->hdr.s.FrequencyBias, shptr, 3);
  823. /* Why is this nybblified, when the MSB is *always* zero ?
  824. Anyway, we can't take address of bitfield, so make a
  825. good-faith guess at where it starts.
  826. */
  827. shptr = munge_int32 (*(&header->hdr.s.FrequencyBias+1),
  828. shptr, 2);
  829. if (snd_wavefront_cmd (dev,
  830. header->size ?
  831. WFC_DOWNLOAD_SAMPLE : WFC_DOWNLOAD_SAMPLE_HEADER,
  832. NULL, sample_hdr)) {
  833. snd_printk ("sample %sdownload refused.\n",
  834. header->size ? "" : "header ");
  835. return -(EIO);
  836. }
  837. if (header->size == 0) {
  838. goto sent; /* Sorry. Just had to have one somewhere */
  839. }
  840. data_end = dataptr + length;
  841. /* Do any initial skip over an unused channel's data */
  842. dataptr += initial_skip;
  843. for (written = 0, blocknum = 0;
  844. written < length; written += max_blksize, blocknum++) {
  845. if ((length - written) > max_blksize) {
  846. blocksize = max_blksize;
  847. } else {
  848. /* round to nearest 16-byte value */
  849. blocksize = ALIGN(length - written, 8);
  850. }
  851. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_BLOCK, NULL, NULL)) {
  852. snd_printk ("download block "
  853. "request refused.\n");
  854. return -(EIO);
  855. }
  856. for (i = 0; i < blocksize; i++) {
  857. if (dataptr < data_end) {
  858. __get_user (sample_short, dataptr);
  859. dataptr += skip;
  860. if (data_is_unsigned) { /* GUS ? */
  861. if (WF_SAMPLE_IS_8BIT(&header->hdr.s)) {
  862. /* 8 bit sample
  863. resolution, sign
  864. extend both bytes.
  865. */
  866. ((unsigned char*)
  867. &sample_short)[0] += 0x7f;
  868. ((unsigned char*)
  869. &sample_short)[1] += 0x7f;
  870. } else {
  871. /* 16 bit sample
  872. resolution, sign
  873. extend the MSB.
  874. */
  875. sample_short += 0x7fff;
  876. }
  877. }
  878. } else {
  879. /* In padding section of final block:
  880. Don't fetch unsupplied data from
  881. user space, just continue with
  882. whatever the final value was.
  883. */
  884. }
  885. if (i < blocksize - 1) {
  886. outw (sample_short, dev->block_port);
  887. } else {
  888. outw (sample_short, dev->last_block_port);
  889. }
  890. }
  891. /* Get "DMA page acknowledge", even though its really
  892. nothing to do with DMA at all.
  893. */
  894. if ((dma_ack = wavefront_read (dev)) != WF_DMA_ACK) {
  895. if (dma_ack == -1) {
  896. snd_printk ("upload sample "
  897. "DMA ack timeout\n");
  898. return -(EIO);
  899. } else {
  900. snd_printk ("upload sample "
  901. "DMA ack error 0x%x\n",
  902. dma_ack);
  903. return -(EIO);
  904. }
  905. }
  906. }
  907. dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_SAMPLE);
  908. /* Note, label is here because sending the sample header shouldn't
  909. alter the sample_status info at all.
  910. */
  911. sent:
  912. return (0);
  913. }
  914. static int
  915. wavefront_send_alias (snd_wavefront_t *dev, wavefront_patch_info *header)
  916. {
  917. unsigned char alias_hdr[WF_ALIAS_BYTES];
  918. DPRINT (WF_DEBUG_LOAD_PATCH, "download alias, %d is "
  919. "alias for %d\n",
  920. header->number,
  921. header->hdr.a.OriginalSample);
  922. munge_int32 (header->number, &alias_hdr[0], 2);
  923. munge_int32 (header->hdr.a.OriginalSample, &alias_hdr[2], 2);
  924. munge_int32 (*((unsigned int *)&header->hdr.a.sampleStartOffset),
  925. &alias_hdr[4], 4);
  926. munge_int32 (*((unsigned int *)&header->hdr.a.loopStartOffset),
  927. &alias_hdr[8], 4);
  928. munge_int32 (*((unsigned int *)&header->hdr.a.loopEndOffset),
  929. &alias_hdr[12], 4);
  930. munge_int32 (*((unsigned int *)&header->hdr.a.sampleEndOffset),
  931. &alias_hdr[16], 4);
  932. munge_int32 (header->hdr.a.FrequencyBias, &alias_hdr[20], 3);
  933. munge_int32 (*(&header->hdr.a.FrequencyBias+1), &alias_hdr[23], 2);
  934. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_SAMPLE_ALIAS, NULL, alias_hdr)) {
  935. snd_printk ("download alias failed.\n");
  936. return -(EIO);
  937. }
  938. dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_ALIAS);
  939. return (0);
  940. }
  941. static int
  942. wavefront_send_multisample (snd_wavefront_t *dev, wavefront_patch_info *header)
  943. {
  944. int i;
  945. int num_samples;
  946. unsigned char *msample_hdr;
  947. msample_hdr = kmalloc(sizeof(WF_MSAMPLE_BYTES), GFP_KERNEL);
  948. if (! msample_hdr)
  949. return -ENOMEM;
  950. munge_int32 (header->number, &msample_hdr[0], 2);
  951. /* You'll recall at this point that the "number of samples" value
  952. in a wavefront_multisample struct is actually the log2 of the
  953. real number of samples.
  954. */
  955. num_samples = (1<<(header->hdr.ms.NumberOfSamples&7));
  956. msample_hdr[2] = (unsigned char) header->hdr.ms.NumberOfSamples;
  957. DPRINT (WF_DEBUG_LOAD_PATCH, "multi %d with %d=%d samples\n",
  958. header->number,
  959. header->hdr.ms.NumberOfSamples,
  960. num_samples);
  961. for (i = 0; i < num_samples; i++) {
  962. DPRINT(WF_DEBUG_LOAD_PATCH|WF_DEBUG_DATA, "sample[%d] = %d\n",
  963. i, header->hdr.ms.SampleNumber[i]);
  964. munge_int32 (header->hdr.ms.SampleNumber[i],
  965. &msample_hdr[3+(i*2)], 2);
  966. }
  967. /* Need a hack here to pass in the number of bytes
  968. to be written to the synth. This is ugly, and perhaps
  969. one day, I'll fix it.
  970. */
  971. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_MULTISAMPLE,
  972. (unsigned char *) (long) ((num_samples*2)+3),
  973. msample_hdr)) {
  974. snd_printk ("download of multisample failed.\n");
  975. kfree(msample_hdr);
  976. return -(EIO);
  977. }
  978. dev->sample_status[header->number] = (WF_SLOT_FILLED|WF_ST_MULTISAMPLE);
  979. kfree(msample_hdr);
  980. return (0);
  981. }
  982. static int
  983. wavefront_fetch_multisample (snd_wavefront_t *dev,
  984. wavefront_patch_info *header)
  985. {
  986. int i;
  987. unsigned char log_ns[1];
  988. unsigned char number[2];
  989. int num_samples;
  990. munge_int32 (header->number, number, 2);
  991. if (snd_wavefront_cmd (dev, WFC_UPLOAD_MULTISAMPLE, log_ns, number)) {
  992. snd_printk ("upload multisample failed.\n");
  993. return -(EIO);
  994. }
  995. DPRINT (WF_DEBUG_DATA, "msample %d has %d samples\n",
  996. header->number, log_ns[0]);
  997. header->hdr.ms.NumberOfSamples = log_ns[0];
  998. /* get the number of samples ... */
  999. num_samples = (1 << log_ns[0]);
  1000. for (i = 0; i < num_samples; i++) {
  1001. char d[2];
  1002. int val;
  1003. if ((val = wavefront_read (dev)) == -1) {
  1004. snd_printk ("upload multisample failed "
  1005. "during sample loop.\n");
  1006. return -(EIO);
  1007. }
  1008. d[0] = val;
  1009. if ((val = wavefront_read (dev)) == -1) {
  1010. snd_printk ("upload multisample failed "
  1011. "during sample loop.\n");
  1012. return -(EIO);
  1013. }
  1014. d[1] = val;
  1015. header->hdr.ms.SampleNumber[i] =
  1016. demunge_int32 ((unsigned char *) d, 2);
  1017. DPRINT (WF_DEBUG_DATA, "msample sample[%d] = %d\n",
  1018. i, header->hdr.ms.SampleNumber[i]);
  1019. }
  1020. return (0);
  1021. }
  1022. static int
  1023. wavefront_send_drum (snd_wavefront_t *dev, wavefront_patch_info *header)
  1024. {
  1025. unsigned char drumbuf[WF_DRUM_BYTES];
  1026. wavefront_drum *drum = &header->hdr.d;
  1027. int i;
  1028. DPRINT (WF_DEBUG_LOAD_PATCH, "downloading edrum for MIDI "
  1029. "note %d, patch = %d\n",
  1030. header->number, drum->PatchNumber);
  1031. drumbuf[0] = header->number & 0x7f;
  1032. for (i = 0; i < 4; i++) {
  1033. munge_int32 (((unsigned char *)drum)[i], &drumbuf[1+(i*2)], 2);
  1034. }
  1035. if (snd_wavefront_cmd (dev, WFC_DOWNLOAD_EDRUM_PROGRAM, NULL, drumbuf)) {
  1036. snd_printk ("download drum failed.\n");
  1037. return -(EIO);
  1038. }
  1039. return (0);
  1040. }
  1041. static int
  1042. wavefront_find_free_sample (snd_wavefront_t *dev)
  1043. {
  1044. int i;
  1045. for (i = 0; i < WF_MAX_SAMPLE; i++) {
  1046. if (!(dev->sample_status[i] & WF_SLOT_FILLED)) {
  1047. return i;
  1048. }
  1049. }
  1050. snd_printk ("no free sample slots!\n");
  1051. return -1;
  1052. }
  1053. #if 0
  1054. static int
  1055. wavefront_find_free_patch (snd_wavefront_t *dev)
  1056. {
  1057. int i;
  1058. for (i = 0; i < WF_MAX_PATCH; i++) {
  1059. if (!(dev->patch_status[i] & WF_SLOT_FILLED)) {
  1060. return i;
  1061. }
  1062. }
  1063. snd_printk ("no free patch slots!\n");
  1064. return -1;
  1065. }
  1066. #endif
  1067. static int
  1068. wavefront_load_patch (snd_wavefront_t *dev, const char __user *addr)
  1069. {
  1070. wavefront_patch_info *header;
  1071. int err;
  1072. header = kmalloc(sizeof(*header), GFP_KERNEL);
  1073. if (! header)
  1074. return -ENOMEM;
  1075. if (copy_from_user (header, addr, sizeof(wavefront_patch_info) -
  1076. sizeof(wavefront_any))) {
  1077. snd_printk ("bad address for load patch.\n");
  1078. err = -EFAULT;
  1079. goto __error;
  1080. }
  1081. DPRINT (WF_DEBUG_LOAD_PATCH, "download "
  1082. "Sample type: %d "
  1083. "Sample number: %d "
  1084. "Sample size: %d\n",
  1085. header->subkey,
  1086. header->number,
  1087. header->size);
  1088. switch (header->subkey) {
  1089. case WF_ST_SAMPLE: /* sample or sample_header, based on patch->size */
  1090. if (copy_from_user (&header->hdr.s, header->hdrptr,
  1091. sizeof (wavefront_sample))) {
  1092. err = -EFAULT;
  1093. break;
  1094. }
  1095. err = wavefront_send_sample (dev, header, header->dataptr, 0);
  1096. break;
  1097. case WF_ST_MULTISAMPLE:
  1098. if (copy_from_user (&header->hdr.s, header->hdrptr,
  1099. sizeof (wavefront_multisample))) {
  1100. err = -EFAULT;
  1101. break;
  1102. }
  1103. err = wavefront_send_multisample (dev, header);
  1104. break;
  1105. case WF_ST_ALIAS:
  1106. if (copy_from_user (&header->hdr.a, header->hdrptr,
  1107. sizeof (wavefront_alias))) {
  1108. err = -EFAULT;
  1109. break;
  1110. }
  1111. err = wavefront_send_alias (dev, header);
  1112. break;
  1113. case WF_ST_DRUM:
  1114. if (copy_from_user (&header->hdr.d, header->hdrptr,
  1115. sizeof (wavefront_drum))) {
  1116. err = -EFAULT;
  1117. break;
  1118. }
  1119. err = wavefront_send_drum (dev, header);
  1120. break;
  1121. case WF_ST_PATCH:
  1122. if (copy_from_user (&header->hdr.p, header->hdrptr,
  1123. sizeof (wavefront_patch))) {
  1124. err = -EFAULT;
  1125. break;
  1126. }
  1127. err = wavefront_send_patch (dev, header);
  1128. break;
  1129. case WF_ST_PROGRAM:
  1130. if (copy_from_user (&header->hdr.pr, header->hdrptr,
  1131. sizeof (wavefront_program))) {
  1132. err = -EFAULT;
  1133. break;
  1134. }
  1135. err = wavefront_send_program (dev, header);
  1136. break;
  1137. default:
  1138. snd_printk ("unknown patch type %d.\n",
  1139. header->subkey);
  1140. err = -EINVAL;
  1141. break;
  1142. }
  1143. __error:
  1144. kfree(header);
  1145. return err;
  1146. }
  1147. /***********************************************************************
  1148. WaveFront: hardware-dependent interface
  1149. ***********************************************************************/
  1150. static void
  1151. process_sample_hdr (u8 *buf)
  1152. {
  1153. wavefront_sample s;
  1154. u8 *ptr;
  1155. ptr = buf;
  1156. /* The board doesn't send us an exact copy of a "wavefront_sample"
  1157. in response to an Upload Sample Header command. Instead, we
  1158. have to convert the data format back into our data structure,
  1159. just as in the Download Sample command, where we have to do
  1160. something very similar in the reverse direction.
  1161. */
  1162. *((u32 *) &s.sampleStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1163. *((u32 *) &s.loopStartOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1164. *((u32 *) &s.loopEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1165. *((u32 *) &s.sampleEndOffset) = demunge_int32 (ptr, 4); ptr += 4;
  1166. *((u32 *) &s.FrequencyBias) = demunge_int32 (ptr, 3); ptr += 3;
  1167. s.SampleResolution = *ptr & 0x3;
  1168. s.Loop = *ptr & 0x8;
  1169. s.Bidirectional = *ptr & 0x10;
  1170. s.Reverse = *ptr & 0x40;
  1171. /* Now copy it back to where it came from */
  1172. memcpy (buf, (unsigned char *) &s, sizeof (wavefront_sample));
  1173. }
  1174. static int
  1175. wavefront_synth_control (snd_wavefront_card_t *acard,
  1176. wavefront_control *wc)
  1177. {
  1178. snd_wavefront_t *dev = &acard->wavefront;
  1179. unsigned char patchnumbuf[2];
  1180. int i;
  1181. DPRINT (WF_DEBUG_CMD, "synth control with "
  1182. "cmd 0x%x\n", wc->cmd);
  1183. /* Pre-handling of or for various commands */
  1184. switch (wc->cmd) {
  1185. case WFC_DISABLE_INTERRUPTS:
  1186. snd_printk ("interrupts disabled.\n");
  1187. outb (0x80|0x20, dev->control_port);
  1188. dev->interrupts_are_midi = 1;
  1189. return 0;
  1190. case WFC_ENABLE_INTERRUPTS:
  1191. snd_printk ("interrupts enabled.\n");
  1192. outb (0x80|0x40|0x20, dev->control_port);
  1193. dev->interrupts_are_midi = 1;
  1194. return 0;
  1195. case WFC_INTERRUPT_STATUS:
  1196. wc->rbuf[0] = dev->interrupts_are_midi;
  1197. return 0;
  1198. case WFC_ROMSAMPLES_RDONLY:
  1199. dev->rom_samples_rdonly = wc->wbuf[0];
  1200. wc->status = 0;
  1201. return 0;
  1202. case WFC_IDENTIFY_SLOT_TYPE:
  1203. i = wc->wbuf[0] | (wc->wbuf[1] << 7);
  1204. if (i <0 || i >= WF_MAX_SAMPLE) {
  1205. snd_printk ("invalid slot ID %d\n",
  1206. i);
  1207. wc->status = EINVAL;
  1208. return -EINVAL;
  1209. }
  1210. wc->rbuf[0] = dev->sample_status[i];
  1211. wc->status = 0;
  1212. return 0;
  1213. case WFC_DEBUG_DRIVER:
  1214. dev->debug = wc->wbuf[0];
  1215. snd_printk ("debug = 0x%x\n", dev->debug);
  1216. return 0;
  1217. case WFC_UPLOAD_PATCH:
  1218. munge_int32 (*((u32 *) wc->wbuf), patchnumbuf, 2);
  1219. memcpy (wc->wbuf, patchnumbuf, 2);
  1220. break;
  1221. case WFC_UPLOAD_MULTISAMPLE:
  1222. /* multisamples have to be handled differently, and
  1223. cannot be dealt with properly by snd_wavefront_cmd() alone.
  1224. */
  1225. wc->status = wavefront_fetch_multisample
  1226. (dev, (wavefront_patch_info *) wc->rbuf);
  1227. return 0;
  1228. case WFC_UPLOAD_SAMPLE_ALIAS:
  1229. snd_printk ("support for sample alias upload "
  1230. "being considered.\n");
  1231. wc->status = EINVAL;
  1232. return -EINVAL;
  1233. }
  1234. wc->status = snd_wavefront_cmd (dev, wc->cmd, wc->rbuf, wc->wbuf);
  1235. /* Post-handling of certain commands.
  1236. In particular, if the command was an upload, demunge the data
  1237. so that the user-level doesn't have to think about it.
  1238. */
  1239. if (wc->status == 0) {
  1240. switch (wc->cmd) {
  1241. /* intercept any freemem requests so that we know
  1242. we are always current with the user-level view
  1243. of things.
  1244. */
  1245. case WFC_REPORT_FREE_MEMORY:
  1246. dev->freemem = demunge_int32 (wc->rbuf, 4);
  1247. break;
  1248. case WFC_UPLOAD_PATCH:
  1249. demunge_buf (wc->rbuf, wc->rbuf, WF_PATCH_BYTES);
  1250. break;
  1251. case WFC_UPLOAD_PROGRAM:
  1252. demunge_buf (wc->rbuf, wc->rbuf, WF_PROGRAM_BYTES);
  1253. break;
  1254. case WFC_UPLOAD_EDRUM_PROGRAM:
  1255. demunge_buf (wc->rbuf, wc->rbuf, WF_DRUM_BYTES - 1);
  1256. break;
  1257. case WFC_UPLOAD_SAMPLE_HEADER:
  1258. process_sample_hdr (wc->rbuf);
  1259. break;
  1260. case WFC_UPLOAD_SAMPLE_ALIAS:
  1261. snd_printk ("support for "
  1262. "sample aliases still "
  1263. "being considered.\n");
  1264. break;
  1265. case WFC_VMIDI_OFF:
  1266. snd_wavefront_midi_disable_virtual (acard);
  1267. break;
  1268. case WFC_VMIDI_ON:
  1269. snd_wavefront_midi_enable_virtual (acard);
  1270. break;
  1271. }
  1272. }
  1273. return 0;
  1274. }
  1275. int
  1276. snd_wavefront_synth_open (struct snd_hwdep *hw, struct file *file)
  1277. {
  1278. if (!try_module_get(hw->card->module))
  1279. return -EFAULT;
  1280. file->private_data = hw;
  1281. return 0;
  1282. }
  1283. int
  1284. snd_wavefront_synth_release (struct snd_hwdep *hw, struct file *file)
  1285. {
  1286. module_put(hw->card->module);
  1287. return 0;
  1288. }
  1289. int
  1290. snd_wavefront_synth_ioctl (struct snd_hwdep *hw, struct file *file,
  1291. unsigned int cmd, unsigned long arg)
  1292. {
  1293. struct snd_card *card;
  1294. snd_wavefront_t *dev;
  1295. snd_wavefront_card_t *acard;
  1296. wavefront_control *wc;
  1297. void __user *argp = (void __user *)arg;
  1298. int err;
  1299. card = (struct snd_card *) hw->card;
  1300. snd_assert(card != NULL, return -ENODEV);
  1301. snd_assert(card->private_data != NULL, return -ENODEV);
  1302. acard = card->private_data;
  1303. dev = &acard->wavefront;
  1304. switch (cmd) {
  1305. case WFCTL_LOAD_SPP:
  1306. if (wavefront_load_patch (dev, argp) != 0) {
  1307. return -EIO;
  1308. }
  1309. break;
  1310. case WFCTL_WFCMD:
  1311. wc = kmalloc(sizeof(*wc), GFP_KERNEL);
  1312. if (! wc)
  1313. return -ENOMEM;
  1314. if (copy_from_user (wc, argp, sizeof (*wc)))
  1315. err = -EFAULT;
  1316. else if (wavefront_synth_control (acard, wc) < 0)
  1317. err = -EIO;
  1318. else if (copy_to_user (argp, wc, sizeof (*wc)))
  1319. err = -EFAULT;
  1320. else
  1321. err = 0;
  1322. kfree(wc);
  1323. return err;
  1324. default:
  1325. return -EINVAL;
  1326. }
  1327. return 0;
  1328. }
  1329. /***********************************************************************/
  1330. /* WaveFront: interface for card-level wavefront module */
  1331. /***********************************************************************/
  1332. void
  1333. snd_wavefront_internal_interrupt (snd_wavefront_card_t *card)
  1334. {
  1335. snd_wavefront_t *dev = &card->wavefront;
  1336. /*
  1337. Some comments on interrupts. I attempted a version of this
  1338. driver that used interrupts throughout the code instead of
  1339. doing busy and/or sleep-waiting. Alas, it appears that once
  1340. the Motorola firmware is downloaded, the card *never*
  1341. generates an RX interrupt. These are successfully generated
  1342. during firmware loading, and after that wavefront_status()
  1343. reports that an interrupt is pending on the card from time
  1344. to time, but it never seems to be delivered to this
  1345. driver. Note also that wavefront_status() continues to
  1346. report that RX interrupts are enabled, suggesting that I
  1347. didn't goof up and disable them by mistake.
  1348. Thus, I stepped back to a prior version of
  1349. wavefront_wait(), the only place where this really
  1350. matters. Its sad, but I've looked through the code to check
  1351. on things, and I really feel certain that the Motorola
  1352. firmware prevents RX-ready interrupts.
  1353. */
  1354. if ((wavefront_status(dev) & (STAT_INTR_READ|STAT_INTR_WRITE)) == 0) {
  1355. return;
  1356. }
  1357. spin_lock(&dev->irq_lock);
  1358. dev->irq_ok = 1;
  1359. dev->irq_cnt++;
  1360. spin_unlock(&dev->irq_lock);
  1361. wake_up(&dev->interrupt_sleeper);
  1362. }
  1363. /* STATUS REGISTER
  1364. 0 Host Rx Interrupt Enable (1=Enabled)
  1365. 1 Host Rx Register Full (1=Full)
  1366. 2 Host Rx Interrupt Pending (1=Interrupt)
  1367. 3 Unused
  1368. 4 Host Tx Interrupt (1=Enabled)
  1369. 5 Host Tx Register empty (1=Empty)
  1370. 6 Host Tx Interrupt Pending (1=Interrupt)
  1371. 7 Unused
  1372. */
  1373. static int __devinit
  1374. snd_wavefront_interrupt_bits (int irq)
  1375. {
  1376. int bits;
  1377. switch (irq) {
  1378. case 9:
  1379. bits = 0x00;
  1380. break;
  1381. case 5:
  1382. bits = 0x08;
  1383. break;
  1384. case 12:
  1385. bits = 0x10;
  1386. break;
  1387. case 15:
  1388. bits = 0x18;
  1389. break;
  1390. default:
  1391. snd_printk ("invalid IRQ %d\n", irq);
  1392. bits = -1;
  1393. }
  1394. return bits;
  1395. }
  1396. static void __devinit
  1397. wavefront_should_cause_interrupt (snd_wavefront_t *dev,
  1398. int val, int port, int timeout)
  1399. {
  1400. wait_queue_t wait;
  1401. init_waitqueue_entry(&wait, current);
  1402. spin_lock_irq(&dev->irq_lock);
  1403. add_wait_queue(&dev->interrupt_sleeper, &wait);
  1404. dev->irq_ok = 0;
  1405. outb (val,port);
  1406. spin_unlock_irq(&dev->irq_lock);
  1407. while (1) {
  1408. if ((timeout = schedule_timeout_interruptible(timeout)) == 0)
  1409. return;
  1410. if (dev->irq_ok)
  1411. return;
  1412. }
  1413. }
  1414. static int __devinit
  1415. wavefront_reset_to_cleanliness (snd_wavefront_t *dev)
  1416. {
  1417. int bits;
  1418. int hwv[2];
  1419. /* IRQ already checked */
  1420. bits = snd_wavefront_interrupt_bits (dev->irq);
  1421. /* try reset of port */
  1422. outb (0x0, dev->control_port);
  1423. /* At this point, the board is in reset, and the H/W initialization
  1424. register is accessed at the same address as the data port.
  1425. Bit 7 - Enable IRQ Driver
  1426. 0 - Tri-state the Wave-Board drivers for the PC Bus IRQs
  1427. 1 - Enable IRQ selected by bits 5:3 to be driven onto the PC Bus.
  1428. Bit 6 - MIDI Interface Select
  1429. 0 - Use the MIDI Input from the 26-pin WaveBlaster
  1430. compatible header as the serial MIDI source
  1431. 1 - Use the MIDI Input from the 9-pin D connector as the
  1432. serial MIDI source.
  1433. Bits 5:3 - IRQ Selection
  1434. 0 0 0 - IRQ 2/9
  1435. 0 0 1 - IRQ 5
  1436. 0 1 0 - IRQ 12
  1437. 0 1 1 - IRQ 15
  1438. 1 0 0 - Reserved
  1439. 1 0 1 - Reserved
  1440. 1 1 0 - Reserved
  1441. 1 1 1 - Reserved
  1442. Bits 2:1 - Reserved
  1443. Bit 0 - Disable Boot ROM
  1444. 0 - memory accesses to 03FC30-03FFFFH utilize the internal Boot ROM
  1445. 1 - memory accesses to 03FC30-03FFFFH are directed to external
  1446. storage.
  1447. */
  1448. /* configure hardware: IRQ, enable interrupts,
  1449. plus external 9-pin MIDI interface selected
  1450. */
  1451. outb (0x80 | 0x40 | bits, dev->data_port);
  1452. /* CONTROL REGISTER
  1453. 0 Host Rx Interrupt Enable (1=Enabled) 0x1
  1454. 1 Unused 0x2
  1455. 2 Unused 0x4
  1456. 3 Unused 0x8
  1457. 4 Host Tx Interrupt Enable 0x10
  1458. 5 Mute (0=Mute; 1=Play) 0x20
  1459. 6 Master Interrupt Enable (1=Enabled) 0x40
  1460. 7 Master Reset (0=Reset; 1=Run) 0x80
  1461. Take us out of reset, mute output, master + TX + RX interrupts on.
  1462. We'll get an interrupt presumably to tell us that the TX
  1463. register is clear.
  1464. */
  1465. wavefront_should_cause_interrupt(dev, 0x80|0x40|0x10|0x1,
  1466. dev->control_port,
  1467. (reset_time*HZ)/100);
  1468. /* Note: data port is now the data port, not the h/w initialization
  1469. port.
  1470. */
  1471. if (!dev->irq_ok) {
  1472. snd_printk ("intr not received after h/w un-reset.\n");
  1473. goto gone_bad;
  1474. }
  1475. /* Note: data port is now the data port, not the h/w initialization
  1476. port.
  1477. At this point, only "HW VERSION" or "DOWNLOAD OS" commands
  1478. will work. So, issue one of them, and wait for TX
  1479. interrupt. This can take a *long* time after a cold boot,
  1480. while the ISC ROM does its RAM test. The SDK says up to 4
  1481. seconds - with 12MB of RAM on a Tropez+, it takes a lot
  1482. longer than that (~16secs). Note that the card understands
  1483. the difference between a warm and a cold boot, so
  1484. subsequent ISC2115 reboots (say, caused by module
  1485. reloading) will get through this much faster.
  1486. XXX Interesting question: why is no RX interrupt received first ?
  1487. */
  1488. wavefront_should_cause_interrupt(dev, WFC_HARDWARE_VERSION,
  1489. dev->data_port, ramcheck_time*HZ);
  1490. if (!dev->irq_ok) {
  1491. snd_printk ("post-RAM-check interrupt not received.\n");
  1492. goto gone_bad;
  1493. }
  1494. if (!wavefront_wait (dev, STAT_CAN_READ)) {
  1495. snd_printk ("no response to HW version cmd.\n");
  1496. goto gone_bad;
  1497. }
  1498. if ((hwv[0] = wavefront_read (dev)) == -1) {
  1499. snd_printk ("board not responding correctly.\n");
  1500. goto gone_bad;
  1501. }
  1502. if (hwv[0] == 0xFF) { /* NAK */
  1503. /* Board's RAM test failed. Try to read error code,
  1504. and tell us about it either way.
  1505. */
  1506. if ((hwv[0] = wavefront_read (dev)) == -1) {
  1507. snd_printk ("on-board RAM test failed "
  1508. "(bad error code).\n");
  1509. } else {
  1510. snd_printk ("on-board RAM test failed "
  1511. "(error code: 0x%x).\n",
  1512. hwv[0]);
  1513. }
  1514. goto gone_bad;
  1515. }
  1516. /* We're OK, just get the next byte of the HW version response */
  1517. if ((hwv[1] = wavefront_read (dev)) == -1) {
  1518. snd_printk ("incorrect h/w response.\n");
  1519. goto gone_bad;
  1520. }
  1521. snd_printk ("hardware version %d.%d\n",
  1522. hwv[0], hwv[1]);
  1523. return 0;
  1524. gone_bad:
  1525. return (1);
  1526. }
  1527. #include <linux/fs.h>
  1528. #include <linux/mm.h>
  1529. #include <linux/slab.h>
  1530. #include <linux/unistd.h>
  1531. #include <linux/syscalls.h>
  1532. #include <asm/uaccess.h>
  1533. static int __devinit
  1534. wavefront_download_firmware (snd_wavefront_t *dev, char *path)
  1535. {
  1536. unsigned char section[WF_SECTION_MAX];
  1537. signed char section_length; /* yes, just a char; max value is WF_SECTION_MAX */
  1538. int section_cnt_downloaded = 0;
  1539. int fd;
  1540. int c;
  1541. int i;
  1542. mm_segment_t fs;
  1543. /* This tries to be a bit cleverer than the stuff Alan Cox did for
  1544. the generic sound firmware, in that it actually knows
  1545. something about the structure of the Motorola firmware. In
  1546. particular, it uses a version that has been stripped of the
  1547. 20K of useless header information, and had section lengths
  1548. added, making it possible to load the entire OS without any
  1549. [kv]malloc() activity, since the longest entity we ever read is
  1550. 42 bytes (well, WF_SECTION_MAX) long.
  1551. */
  1552. fs = get_fs();
  1553. set_fs (get_ds());
  1554. if ((fd = sys_open ((char __user *) path, 0, 0)) < 0) {
  1555. snd_printk ("Unable to load \"%s\".\n",
  1556. path);
  1557. return 1;
  1558. }
  1559. while (1) {
  1560. int x;
  1561. if ((x = sys_read (fd, (char __user *) &section_length, sizeof (section_length))) !=
  1562. sizeof (section_length)) {
  1563. snd_printk ("firmware read error.\n");
  1564. goto failure;
  1565. }
  1566. if (section_length == 0) {
  1567. break;
  1568. }
  1569. if (section_length < 0 || section_length > WF_SECTION_MAX) {
  1570. snd_printk ("invalid firmware section length %d\n",
  1571. section_length);
  1572. goto failure;
  1573. }
  1574. if (sys_read (fd, (char __user *) section, section_length) != section_length) {
  1575. snd_printk ("firmware section "
  1576. "read error.\n");
  1577. goto failure;
  1578. }
  1579. /* Send command */
  1580. if (wavefront_write (dev, WFC_DOWNLOAD_OS)) {
  1581. goto failure;
  1582. }
  1583. for (i = 0; i < section_length; i++) {
  1584. if (wavefront_write (dev, section[i])) {
  1585. goto failure;
  1586. }
  1587. }
  1588. /* get ACK */
  1589. if (wavefront_wait (dev, STAT_CAN_READ)) {
  1590. if ((c = inb (dev->data_port)) != WF_ACK) {
  1591. snd_printk ("download "
  1592. "of section #%d not "
  1593. "acknowledged, ack = 0x%x\n",
  1594. section_cnt_downloaded + 1, c);
  1595. goto failure;
  1596. }
  1597. } else {
  1598. snd_printk ("time out for firmware ACK.\n");
  1599. goto failure;
  1600. }
  1601. }
  1602. sys_close (fd);
  1603. set_fs (fs);
  1604. return 0;
  1605. failure:
  1606. sys_close (fd);
  1607. set_fs (fs);
  1608. snd_printk ("firmware download failed!!!\n");
  1609. return 1;
  1610. }
  1611. static int __devinit
  1612. wavefront_do_reset (snd_wavefront_t *dev)
  1613. {
  1614. char voices[1];
  1615. if (wavefront_reset_to_cleanliness (dev)) {
  1616. snd_printk ("hw reset failed.\n");
  1617. goto gone_bad;
  1618. }
  1619. if (dev->israw) {
  1620. if (wavefront_download_firmware (dev, ospath)) {
  1621. goto gone_bad;
  1622. }
  1623. dev->israw = 0;
  1624. /* Wait for the OS to get running. The protocol for
  1625. this is non-obvious, and was determined by
  1626. using port-IO tracing in DOSemu and some
  1627. experimentation here.
  1628. Rather than using timed waits, use interrupts creatively.
  1629. */
  1630. wavefront_should_cause_interrupt (dev, WFC_NOOP,
  1631. dev->data_port,
  1632. (osrun_time*HZ));
  1633. if (!dev->irq_ok) {
  1634. snd_printk ("no post-OS interrupt.\n");
  1635. goto gone_bad;
  1636. }
  1637. /* Now, do it again ! */
  1638. wavefront_should_cause_interrupt (dev, WFC_NOOP,
  1639. dev->data_port, (10*HZ));
  1640. if (!dev->irq_ok) {
  1641. snd_printk ("no post-OS interrupt(2).\n");
  1642. goto gone_bad;
  1643. }
  1644. /* OK, no (RX/TX) interrupts any more, but leave mute
  1645. in effect.
  1646. */
  1647. outb (0x80|0x40, dev->control_port);
  1648. }
  1649. /* SETUPSND.EXE asks for sample memory config here, but since i
  1650. have no idea how to interpret the result, we'll forget
  1651. about it.
  1652. */
  1653. if ((dev->freemem = wavefront_freemem (dev)) < 0) {
  1654. goto gone_bad;
  1655. }
  1656. snd_printk ("available DRAM %dk\n", dev->freemem / 1024);
  1657. if (wavefront_write (dev, 0xf0) ||
  1658. wavefront_write (dev, 1) ||
  1659. (wavefront_read (dev) < 0)) {
  1660. dev->debug = 0;
  1661. snd_printk ("MPU emulation mode not set.\n");
  1662. goto gone_bad;
  1663. }
  1664. voices[0] = 32;
  1665. if (snd_wavefront_cmd (dev, WFC_SET_NVOICES, NULL, voices)) {
  1666. snd_printk ("cannot set number of voices to 32.\n");
  1667. goto gone_bad;
  1668. }
  1669. return 0;
  1670. gone_bad:
  1671. /* reset that sucker so that it doesn't bother us. */
  1672. outb (0x0, dev->control_port);
  1673. dev->interrupts_are_midi = 0;
  1674. return 1;
  1675. }
  1676. int __devinit
  1677. snd_wavefront_start (snd_wavefront_t *dev)
  1678. {
  1679. int samples_are_from_rom;
  1680. /* IMPORTANT: assumes that snd_wavefront_detect() and/or
  1681. wavefront_reset_to_cleanliness() has already been called
  1682. */
  1683. if (dev->israw) {
  1684. samples_are_from_rom = 1;
  1685. } else {
  1686. /* XXX is this always true ? */
  1687. samples_are_from_rom = 0;
  1688. }
  1689. if (dev->israw || fx_raw) {
  1690. if (wavefront_do_reset (dev)) {
  1691. return -1;
  1692. }
  1693. }
  1694. /* Check for FX device, present only on Tropez+ */
  1695. dev->has_fx = (snd_wavefront_fx_detect (dev) == 0);
  1696. if (dev->has_fx && fx_raw) {
  1697. snd_wavefront_fx_start (dev);
  1698. }
  1699. wavefront_get_sample_status (dev, samples_are_from_rom);
  1700. wavefront_get_program_status (dev);
  1701. wavefront_get_patch_status (dev);
  1702. /* Start normal operation: unreset, master interrupt enabled, no mute
  1703. */
  1704. outb (0x80|0x40|0x20, dev->control_port);
  1705. return (0);
  1706. }
  1707. int __devinit
  1708. snd_wavefront_detect (snd_wavefront_card_t *card)
  1709. {
  1710. unsigned char rbuf[4], wbuf[4];
  1711. snd_wavefront_t *dev = &card->wavefront;
  1712. /* returns zero if a WaveFront card is successfully detected.
  1713. negative otherwise.
  1714. */
  1715. dev->israw = 0;
  1716. dev->has_fx = 0;
  1717. dev->debug = debug_default;
  1718. dev->interrupts_are_midi = 0;
  1719. dev->irq_cnt = 0;
  1720. dev->rom_samples_rdonly = 1;
  1721. if (snd_wavefront_cmd (dev, WFC_FIRMWARE_VERSION, rbuf, wbuf) == 0) {
  1722. dev->fw_version[0] = rbuf[0];
  1723. dev->fw_version[1] = rbuf[1];
  1724. snd_printk ("firmware %d.%d already loaded.\n",
  1725. rbuf[0], rbuf[1]);
  1726. /* check that a command actually works */
  1727. if (snd_wavefront_cmd (dev, WFC_HARDWARE_VERSION,
  1728. rbuf, wbuf) == 0) {
  1729. dev->hw_version[0] = rbuf[0];
  1730. dev->hw_version[1] = rbuf[1];
  1731. } else {
  1732. snd_printk ("not raw, but no "
  1733. "hardware version!\n");
  1734. return -1;
  1735. }
  1736. if (!wf_raw) {
  1737. return 0;
  1738. } else {
  1739. snd_printk ("reloading firmware as you requested.\n");
  1740. dev->israw = 1;
  1741. }
  1742. } else {
  1743. dev->israw = 1;
  1744. snd_printk ("no response to firmware probe, assume raw.\n");
  1745. }
  1746. return 0;
  1747. }