trident_synth.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * Routines for Trident 4DWave NX/DX soundcards - Synthesizer
  3. * Copyright (c) by Scott McNab <jedi@tartarus.uwa.edu.au>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <sound/core.h>
  27. #include <sound/trident.h>
  28. #include <sound/seq_device.h>
  29. MODULE_AUTHOR("Scott McNab <jedi@tartarus.uwa.edu.au>");
  30. MODULE_DESCRIPTION("Routines for Trident 4DWave NX/DX soundcards - Synthesizer");
  31. MODULE_LICENSE("GPL");
  32. /* linear to log pan conversion table (4.2 channel attenuation format) */
  33. static unsigned int pan_table[63] = {
  34. 7959, 7733, 7514, 7301, 7093, 6892, 6697, 6507,
  35. 6322, 6143, 5968, 5799, 5634, 5475, 5319, 5168,
  36. 5022, 4879, 4741, 4606, 4475, 4349, 4225, 4105,
  37. 3989, 3876, 3766, 3659, 3555, 3454, 3356, 3261,
  38. 3168, 3078, 2991, 2906, 2824, 2744, 2666, 2590,
  39. 2517, 2445, 2376, 2308, 2243, 2179, 2117, 2057,
  40. 1999, 1942, 1887, 1833, 1781, 1731, 1682, 1634,
  41. 1588, 1543, 1499, 1456, 1415, 1375, 1336
  42. };
  43. #define LOG_TABLE_SIZE 386
  44. /* Linear half-attenuation to log conversion table in the format:
  45. * {linear volume, logarithmic attenuation equivalent}, ...
  46. *
  47. * Provides conversion from a linear half-volume value in the range
  48. * [0,8192] to a logarithmic attenuation value in the range 0 to 6.02dB.
  49. * Halving the linear volume is equivalent to an additional 6dB of
  50. * logarithmic attenuation. The algorithm used in log_from_linear()
  51. * therefore uses this table as follows:
  52. *
  53. * - loop and for every time the volume is less than half the maximum
  54. * volume (16384), add another 6dB and halve the maximum value used
  55. * for this comparison.
  56. * - when the volume is greater than half the maximum volume, take
  57. * the difference of the volume to half volume (in the range [0,8192])
  58. * and look up the log_table[] to find the nearest entry.
  59. * - take the logarithic component of this entry and add it to the
  60. * resulting attenuation.
  61. *
  62. * Thus this routine provides a linear->log conversion for a range of
  63. * [0,16384] using only 386 table entries
  64. *
  65. * Note: although this table stores log attenuation in 8.8 format, values
  66. * were only calculated for 6 bits fractional precision, since that is
  67. * the most precision offered by the trident hardware.
  68. */
  69. static unsigned short log_table[LOG_TABLE_SIZE*2] =
  70. {
  71. 4, 0x0604, 19, 0x0600, 34, 0x05fc,
  72. 49, 0x05f8, 63, 0x05f4, 78, 0x05f0, 93, 0x05ec, 108, 0x05e8,
  73. 123, 0x05e4, 138, 0x05e0, 153, 0x05dc, 168, 0x05d8, 183, 0x05d4,
  74. 198, 0x05d0, 213, 0x05cc, 228, 0x05c8, 244, 0x05c4, 259, 0x05c0,
  75. 274, 0x05bc, 289, 0x05b8, 304, 0x05b4, 320, 0x05b0, 335, 0x05ac,
  76. 350, 0x05a8, 366, 0x05a4, 381, 0x05a0, 397, 0x059c, 412, 0x0598,
  77. 428, 0x0594, 443, 0x0590, 459, 0x058c, 474, 0x0588, 490, 0x0584,
  78. 506, 0x0580, 521, 0x057c, 537, 0x0578, 553, 0x0574, 568, 0x0570,
  79. 584, 0x056c, 600, 0x0568, 616, 0x0564, 632, 0x0560, 647, 0x055c,
  80. 663, 0x0558, 679, 0x0554, 695, 0x0550, 711, 0x054c, 727, 0x0548,
  81. 743, 0x0544, 759, 0x0540, 776, 0x053c, 792, 0x0538, 808, 0x0534,
  82. 824, 0x0530, 840, 0x052c, 857, 0x0528, 873, 0x0524, 889, 0x0520,
  83. 906, 0x051c, 922, 0x0518, 938, 0x0514, 955, 0x0510, 971, 0x050c,
  84. 988, 0x0508, 1004, 0x0504, 1021, 0x0500, 1037, 0x04fc, 1054, 0x04f8,
  85. 1071, 0x04f4, 1087, 0x04f0, 1104, 0x04ec, 1121, 0x04e8, 1138, 0x04e4,
  86. 1154, 0x04e0, 1171, 0x04dc, 1188, 0x04d8, 1205, 0x04d4, 1222, 0x04d0,
  87. 1239, 0x04cc, 1256, 0x04c8, 1273, 0x04c4, 1290, 0x04c0, 1307, 0x04bc,
  88. 1324, 0x04b8, 1341, 0x04b4, 1358, 0x04b0, 1376, 0x04ac, 1393, 0x04a8,
  89. 1410, 0x04a4, 1427, 0x04a0, 1445, 0x049c, 1462, 0x0498, 1479, 0x0494,
  90. 1497, 0x0490, 1514, 0x048c, 1532, 0x0488, 1549, 0x0484, 1567, 0x0480,
  91. 1584, 0x047c, 1602, 0x0478, 1620, 0x0474, 1637, 0x0470, 1655, 0x046c,
  92. 1673, 0x0468, 1690, 0x0464, 1708, 0x0460, 1726, 0x045c, 1744, 0x0458,
  93. 1762, 0x0454, 1780, 0x0450, 1798, 0x044c, 1816, 0x0448, 1834, 0x0444,
  94. 1852, 0x0440, 1870, 0x043c, 1888, 0x0438, 1906, 0x0434, 1924, 0x0430,
  95. 1943, 0x042c, 1961, 0x0428, 1979, 0x0424, 1997, 0x0420, 2016, 0x041c,
  96. 2034, 0x0418, 2053, 0x0414, 2071, 0x0410, 2089, 0x040c, 2108, 0x0408,
  97. 2127, 0x0404, 2145, 0x0400, 2164, 0x03fc, 2182, 0x03f8, 2201, 0x03f4,
  98. 2220, 0x03f0, 2239, 0x03ec, 2257, 0x03e8, 2276, 0x03e4, 2295, 0x03e0,
  99. 2314, 0x03dc, 2333, 0x03d8, 2352, 0x03d4, 2371, 0x03d0, 2390, 0x03cc,
  100. 2409, 0x03c8, 2428, 0x03c4, 2447, 0x03c0, 2466, 0x03bc, 2485, 0x03b8,
  101. 2505, 0x03b4, 2524, 0x03b0, 2543, 0x03ac, 2562, 0x03a8, 2582, 0x03a4,
  102. 2601, 0x03a0, 2621, 0x039c, 2640, 0x0398, 2660, 0x0394, 2679, 0x0390,
  103. 2699, 0x038c, 2718, 0x0388, 2738, 0x0384, 2758, 0x0380, 2777, 0x037c,
  104. 2797, 0x0378, 2817, 0x0374, 2837, 0x0370, 2857, 0x036c, 2876, 0x0368,
  105. 2896, 0x0364, 2916, 0x0360, 2936, 0x035c, 2956, 0x0358, 2976, 0x0354,
  106. 2997, 0x0350, 3017, 0x034c, 3037, 0x0348, 3057, 0x0344, 3077, 0x0340,
  107. 3098, 0x033c, 3118, 0x0338, 3138, 0x0334, 3159, 0x0330, 3179, 0x032c,
  108. 3200, 0x0328, 3220, 0x0324, 3241, 0x0320, 3261, 0x031c, 3282, 0x0318,
  109. 3303, 0x0314, 3323, 0x0310, 3344, 0x030c, 3365, 0x0308, 3386, 0x0304,
  110. 3406, 0x0300, 3427, 0x02fc, 3448, 0x02f8, 3469, 0x02f4, 3490, 0x02f0,
  111. 3511, 0x02ec, 3532, 0x02e8, 3553, 0x02e4, 3575, 0x02e0, 3596, 0x02dc,
  112. 3617, 0x02d8, 3638, 0x02d4, 3660, 0x02d0, 3681, 0x02cc, 3702, 0x02c8,
  113. 3724, 0x02c4, 3745, 0x02c0, 3767, 0x02bc, 3788, 0x02b8, 3810, 0x02b4,
  114. 3831, 0x02b0, 3853, 0x02ac, 3875, 0x02a8, 3896, 0x02a4, 3918, 0x02a0,
  115. 3940, 0x029c, 3962, 0x0298, 3984, 0x0294, 4006, 0x0290, 4028, 0x028c,
  116. 4050, 0x0288, 4072, 0x0284, 4094, 0x0280, 4116, 0x027c, 4138, 0x0278,
  117. 4160, 0x0274, 4182, 0x0270, 4205, 0x026c, 4227, 0x0268, 4249, 0x0264,
  118. 4272, 0x0260, 4294, 0x025c, 4317, 0x0258, 4339, 0x0254, 4362, 0x0250,
  119. 4384, 0x024c, 4407, 0x0248, 4430, 0x0244, 4453, 0x0240, 4475, 0x023c,
  120. 4498, 0x0238, 4521, 0x0234, 4544, 0x0230, 4567, 0x022c, 4590, 0x0228,
  121. 4613, 0x0224, 4636, 0x0220, 4659, 0x021c, 4682, 0x0218, 4705, 0x0214,
  122. 4728, 0x0210, 4752, 0x020c, 4775, 0x0208, 4798, 0x0204, 4822, 0x0200,
  123. 4845, 0x01fc, 4869, 0x01f8, 4892, 0x01f4, 4916, 0x01f0, 4939, 0x01ec,
  124. 4963, 0x01e8, 4987, 0x01e4, 5010, 0x01e0, 5034, 0x01dc, 5058, 0x01d8,
  125. 5082, 0x01d4, 5106, 0x01d0, 5130, 0x01cc, 5154, 0x01c8, 5178, 0x01c4,
  126. 5202, 0x01c0, 5226, 0x01bc, 5250, 0x01b8, 5274, 0x01b4, 5299, 0x01b0,
  127. 5323, 0x01ac, 5347, 0x01a8, 5372, 0x01a4, 5396, 0x01a0, 5420, 0x019c,
  128. 5445, 0x0198, 5469, 0x0194, 5494, 0x0190, 5519, 0x018c, 5543, 0x0188,
  129. 5568, 0x0184, 5593, 0x0180, 5618, 0x017c, 5643, 0x0178, 5668, 0x0174,
  130. 5692, 0x0170, 5717, 0x016c, 5743, 0x0168, 5768, 0x0164, 5793, 0x0160,
  131. 5818, 0x015c, 5843, 0x0158, 5868, 0x0154, 5894, 0x0150, 5919, 0x014c,
  132. 5945, 0x0148, 5970, 0x0144, 5995, 0x0140, 6021, 0x013c, 6047, 0x0138,
  133. 6072, 0x0134, 6098, 0x0130, 6124, 0x012c, 6149, 0x0128, 6175, 0x0124,
  134. 6201, 0x0120, 6227, 0x011c, 6253, 0x0118, 6279, 0x0114, 6305, 0x0110,
  135. 6331, 0x010c, 6357, 0x0108, 6384, 0x0104, 6410, 0x0100, 6436, 0x00fc,
  136. 6462, 0x00f8, 6489, 0x00f4, 6515, 0x00f0, 6542, 0x00ec, 6568, 0x00e8,
  137. 6595, 0x00e4, 6621, 0x00e0, 6648, 0x00dc, 6675, 0x00d8, 6702, 0x00d4,
  138. 6728, 0x00d0, 6755, 0x00cc, 6782, 0x00c8, 6809, 0x00c4, 6836, 0x00c0,
  139. 6863, 0x00bc, 6890, 0x00b8, 6917, 0x00b4, 6945, 0x00b0, 6972, 0x00ac,
  140. 6999, 0x00a8, 7027, 0x00a4, 7054, 0x00a0, 7081, 0x009c, 7109, 0x0098,
  141. 7136, 0x0094, 7164, 0x0090, 7192, 0x008c, 7219, 0x0088, 7247, 0x0084,
  142. 7275, 0x0080, 7303, 0x007c, 7331, 0x0078, 7359, 0x0074, 7387, 0x0070,
  143. 7415, 0x006c, 7443, 0x0068, 7471, 0x0064, 7499, 0x0060, 7527, 0x005c,
  144. 7556, 0x0058, 7584, 0x0054, 7613, 0x0050, 7641, 0x004c, 7669, 0x0048,
  145. 7698, 0x0044, 7727, 0x0040, 7755, 0x003c, 7784, 0x0038, 7813, 0x0034,
  146. 7842, 0x0030, 7870, 0x002c, 7899, 0x0028, 7928, 0x0024, 7957, 0x0020,
  147. 7986, 0x001c, 8016, 0x0018, 8045, 0x0014, 8074, 0x0010, 8103, 0x000c,
  148. 8133, 0x0008, 8162, 0x0004, 8192, 0x0000
  149. };
  150. static unsigned short lookup_volume_table( unsigned short value )
  151. {
  152. /* This code is an optimised version of:
  153. * int i = 0;
  154. * while( volume_table[i*2] < value )
  155. * i++;
  156. * return volume_table[i*2+1];
  157. */
  158. unsigned short *ptr = log_table;
  159. while( *ptr < value )
  160. ptr += 2;
  161. return *(ptr+1);
  162. }
  163. /* this function calculates a 8.8 fixed point logarithmic attenuation
  164. * value from a linear volume value in the range 0 to 16384 */
  165. static unsigned short log_from_linear( unsigned short value )
  166. {
  167. if (value >= 16384)
  168. return 0x0000;
  169. if (value) {
  170. unsigned short result = 0;
  171. int v, c;
  172. for( c = 0, v = 8192; c < 14; c++, v >>= 1 ) {
  173. if( value >= v ) {
  174. result += lookup_volume_table( (value - v) << c );
  175. return result;
  176. }
  177. result += 0x0605; /* 6.0205 (result of -20*log10(0.5)) */
  178. }
  179. }
  180. return 0xffff;
  181. }
  182. /*
  183. * Sample handling operations
  184. */
  185. static void sample_start(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_position_t position);
  186. static void sample_stop(struct snd_trident * trident, struct snd_trident_voice * voice, int mode);
  187. static void sample_freq(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_frequency_t freq);
  188. static void sample_volume(struct snd_trident * trident, struct snd_trident_voice * voice, struct snd_seq_ev_volume * volume);
  189. static void sample_loop(struct snd_trident * trident, struct snd_trident_voice * voice, struct snd_seq_ev_loop * loop);
  190. static void sample_pos(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_position_t position);
  191. static void sample_private1(struct snd_trident * trident, struct snd_trident_voice * voice, unsigned char *data);
  192. static struct snd_trident_sample_ops sample_ops =
  193. {
  194. sample_start,
  195. sample_stop,
  196. sample_freq,
  197. sample_volume,
  198. sample_loop,
  199. sample_pos,
  200. sample_private1
  201. };
  202. static void snd_trident_simple_init(struct snd_trident_voice * voice)
  203. {
  204. //voice->handler_wave = interrupt_wave;
  205. //voice->handler_volume = interrupt_volume;
  206. //voice->handler_effect = interrupt_effect;
  207. //voice->volume_change = NULL;
  208. voice->sample_ops = &sample_ops;
  209. }
  210. static void sample_start(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_position_t position)
  211. {
  212. struct simple_instrument *simple;
  213. struct snd_seq_kinstr *instr;
  214. unsigned long flags;
  215. unsigned int loop_start, loop_end, sample_start, sample_end, start_offset;
  216. unsigned int value;
  217. unsigned int shift = 0;
  218. instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
  219. if (instr == NULL)
  220. return;
  221. voice->instr = instr->instr; /* copy ID to speedup aliases */
  222. simple = KINSTR_DATA(instr);
  223. spin_lock_irqsave(&trident->reg_lock, flags);
  224. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  225. voice->GVSel = 1; /* route to Wave volume */
  226. voice->CTRL = 0;
  227. voice->Alpha = 0;
  228. voice->FMS = 0;
  229. loop_start = simple->loop_start >> 4;
  230. loop_end = simple->loop_end >> 4;
  231. sample_start = (simple->start + position) >> 4;
  232. if( sample_start >= simple->size )
  233. sample_start = simple->start >> 4;
  234. sample_end = simple->size;
  235. start_offset = position >> 4;
  236. if (simple->format & SIMPLE_WAVE_16BIT) {
  237. voice->CTRL |= 8;
  238. shift++;
  239. }
  240. if (simple->format & SIMPLE_WAVE_STEREO) {
  241. voice->CTRL |= 4;
  242. shift++;
  243. }
  244. if (!(simple->format & SIMPLE_WAVE_UNSIGNED))
  245. voice->CTRL |= 2;
  246. voice->LBA = simple->address.memory;
  247. if (simple->format & SIMPLE_WAVE_LOOP) {
  248. voice->CTRL |= 1;
  249. voice->LBA += loop_start << shift;
  250. if( start_offset >= loop_start ) {
  251. voice->CSO = start_offset - loop_start;
  252. voice->negCSO = 0;
  253. } else {
  254. voice->CSO = loop_start - start_offset;
  255. voice->negCSO = 1;
  256. }
  257. voice->ESO = loop_end - loop_start - 1;
  258. } else {
  259. voice->LBA += start_offset << shift;
  260. voice->CSO = sample_start;
  261. voice->ESO = sample_end - 1;
  262. voice->negCSO = 0;
  263. }
  264. if (voice->flags & SNDRV_TRIDENT_VFLG_RUNNING) {
  265. snd_trident_stop_voice(trident, voice->number);
  266. voice->flags &= ~SNDRV_TRIDENT_VFLG_RUNNING;
  267. }
  268. /* set CSO sign */
  269. value = inl(TRID_REG(trident, T4D_SIGN_CSO_A));
  270. if( voice->negCSO ) {
  271. value |= 1 << (voice->number&31);
  272. } else {
  273. value &= ~(1 << (voice->number&31));
  274. }
  275. outl(value,TRID_REG(trident, T4D_SIGN_CSO_A));
  276. voice->Attribute = 0;
  277. snd_trident_write_voice_regs(trident, voice);
  278. snd_trident_start_voice(trident, voice->number);
  279. voice->flags |= SNDRV_TRIDENT_VFLG_RUNNING;
  280. spin_unlock_irqrestore(&trident->reg_lock, flags);
  281. snd_seq_instr_free_use(trident->synth.ilist, instr);
  282. }
  283. static void sample_stop(struct snd_trident * trident, struct snd_trident_voice * voice, int mode)
  284. {
  285. unsigned long flags;
  286. if (!(voice->flags & SNDRV_TRIDENT_VFLG_RUNNING))
  287. return;
  288. switch (mode) {
  289. default:
  290. spin_lock_irqsave(&trident->reg_lock, flags);
  291. snd_trident_stop_voice(trident, voice->number);
  292. voice->flags &= ~SNDRV_TRIDENT_VFLG_RUNNING;
  293. spin_unlock_irqrestore(&trident->reg_lock, flags);
  294. break;
  295. case SAMPLE_STOP_LOOP: /* disable loop only */
  296. voice->CTRL &= ~1;
  297. spin_lock_irqsave(&trident->reg_lock, flags);
  298. outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  299. outw((((voice->CTRL << 12) | (voice->EC & 0x0fff)) & 0xffff), CH_GVSEL_PAN_VOL_CTRL_EC);
  300. spin_unlock_irqrestore(&trident->reg_lock, flags);
  301. break;
  302. }
  303. }
  304. static void sample_freq(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_frequency_t freq)
  305. {
  306. unsigned long flags;
  307. freq >>= 4;
  308. spin_lock_irqsave(&trident->reg_lock, flags);
  309. if (freq == 44100)
  310. voice->Delta = 0xeb3;
  311. else if (freq == 8000)
  312. voice->Delta = 0x2ab;
  313. else if (freq == 48000)
  314. voice->Delta = 0x1000;
  315. else
  316. voice->Delta = (((freq << 12) + freq) / 48000) & 0x0000ffff;
  317. outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  318. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  319. outb((unsigned char) voice->Delta, TRID_REG(trident, CH_NX_DELTA_CSO + 3));
  320. outb((unsigned char) (voice->Delta >> 8), TRID_REG(trident, CH_NX_DELTA_ESO + 3));
  321. } else {
  322. outw((unsigned short) voice->Delta, TRID_REG(trident, CH_DX_ESO_DELTA));
  323. }
  324. spin_unlock_irqrestore(&trident->reg_lock, flags);
  325. }
  326. static void sample_volume(struct snd_trident * trident, struct snd_trident_voice * voice, struct snd_seq_ev_volume * volume)
  327. {
  328. unsigned long flags;
  329. unsigned short value;
  330. spin_lock_irqsave(&trident->reg_lock, flags);
  331. voice->GVSel = 0; /* use global music volume */
  332. voice->FMC = 0x03; /* fixme: can we do something useful with FMC? */
  333. if (volume->volume >= 0) {
  334. volume->volume &= 0x3fff;
  335. /* linear volume -> logarithmic attenuation conversion
  336. * uses EC register for greater resolution (6.6 bits) than Vol register (5.3 bits)
  337. * Vol register used when additional attenuation is required */
  338. voice->RVol = 0;
  339. voice->CVol = 0;
  340. value = log_from_linear( volume->volume );
  341. voice->Vol = 0;
  342. voice->EC = (value & 0x3fff) >> 2;
  343. if (value > 0x3fff) {
  344. voice->EC |= 0xfc0;
  345. if (value < 0x5f00 )
  346. voice->Vol = ((value >> 8) - 0x3f) << 5;
  347. else {
  348. voice->Vol = 0x3ff;
  349. voice->EC = 0xfff;
  350. }
  351. }
  352. }
  353. if (volume->lr >= 0) {
  354. volume->lr &= 0x3fff;
  355. /* approximate linear pan by attenuating channels */
  356. if (volume->lr >= 0x2000) { /* attenuate left (pan right) */
  357. value = 0x3fff - volume->lr;
  358. for (voice->Pan = 0; voice->Pan < 63; voice->Pan++ )
  359. if (value >= pan_table[voice->Pan] )
  360. break;
  361. } else { /* attenuate right (pan left) */
  362. for (voice->Pan = 0; voice->Pan < 63; voice->Pan++ )
  363. if ((unsigned int)volume->lr >= pan_table[voice->Pan] )
  364. break;
  365. voice->Pan |= 0x40;
  366. }
  367. }
  368. outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  369. outl((voice->GVSel << 31) | ((voice->Pan & 0x0000007f) << 24) |
  370. ((voice->Vol & 0x000000ff) << 16) | ((voice->CTRL & 0x0000000f) << 12) |
  371. (voice->EC & 0x00000fff), TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  372. value = ((voice->FMC & 0x03) << 14) | ((voice->RVol & 0x7f) << 7) | (voice->CVol & 0x7f);
  373. outw(value, TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
  374. spin_unlock_irqrestore(&trident->reg_lock, flags);
  375. }
  376. static void sample_loop(struct snd_trident * trident, struct snd_trident_voice * voice, struct snd_seq_ev_loop * loop)
  377. {
  378. unsigned long flags;
  379. struct simple_instrument *simple;
  380. struct snd_seq_kinstr *instr;
  381. unsigned int loop_start, loop_end;
  382. instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
  383. if (instr == NULL)
  384. return;
  385. voice->instr = instr->instr; /* copy ID to speedup aliases */
  386. simple = KINSTR_DATA(instr);
  387. loop_start = loop->start >> 4;
  388. loop_end = loop->end >> 4;
  389. spin_lock_irqsave(&trident->reg_lock, flags);
  390. voice->LBA = simple->address.memory + loop_start;
  391. voice->CSO = 0;
  392. voice->ESO = loop_end - loop_start - 1;
  393. outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  394. outb((voice->LBA >> 16), TRID_REG(trident, CH_LBA + 2));
  395. outw((voice->LBA & 0xffff), TRID_REG(trident, CH_LBA));
  396. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  397. outb((voice->ESO >> 16), TRID_REG(trident, CH_NX_DELTA_ESO + 2));
  398. outw((voice->ESO & 0xffff), TRID_REG(trident, CH_NX_DELTA_ESO));
  399. outb((voice->CSO >> 16), TRID_REG(trident, CH_NX_DELTA_CSO + 2));
  400. outw((voice->CSO & 0xffff), TRID_REG(trident, CH_NX_DELTA_CSO));
  401. } else {
  402. outw((voice->ESO & 0xffff), TRID_REG(trident, CH_DX_ESO_DELTA + 2));
  403. outw((voice->CSO & 0xffff), TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  404. }
  405. spin_unlock_irqrestore(&trident->reg_lock, flags);
  406. snd_seq_instr_free_use(trident->synth.ilist, instr);
  407. }
  408. static void sample_pos(struct snd_trident * trident, struct snd_trident_voice * voice, snd_seq_position_t position)
  409. {
  410. unsigned long flags;
  411. struct simple_instrument *simple;
  412. struct snd_seq_kinstr *instr;
  413. unsigned int value;
  414. instr = snd_seq_instr_find(trident->synth.ilist, &voice->instr, 0, 1);
  415. if (instr == NULL)
  416. return;
  417. voice->instr = instr->instr; /* copy ID to speedup aliases */
  418. simple = KINSTR_DATA(instr);
  419. spin_lock_irqsave(&trident->reg_lock, flags);
  420. if (simple->format & SIMPLE_WAVE_LOOP) {
  421. if( position >= simple->loop_start ) {
  422. voice->CSO = (position - simple->loop_start) >> 4;
  423. voice->negCSO = 0;
  424. } else {
  425. voice->CSO = (simple->loop_start - position) >> 4;
  426. voice->negCSO = 1;
  427. }
  428. } else {
  429. voice->CSO = position >> 4;
  430. voice->negCSO = 0;
  431. }
  432. /* set CSO sign */
  433. value = inl(TRID_REG(trident, T4D_SIGN_CSO_A));
  434. if( voice->negCSO ) {
  435. value |= 1 << (voice->number&31);
  436. } else {
  437. value &= ~(1 << (voice->number&31));
  438. }
  439. outl(value,TRID_REG(trident, T4D_SIGN_CSO_A));
  440. outb((unsigned char) voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  441. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  442. outw((voice->CSO & 0xffff), TRID_REG(trident, CH_NX_DELTA_CSO));
  443. outb((voice->CSO >> 16), TRID_REG(trident, CH_NX_DELTA_CSO + 2));
  444. } else {
  445. outw((voice->CSO & 0xffff), TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
  446. }
  447. spin_unlock_irqrestore(&trident->reg_lock, flags);
  448. snd_seq_instr_free_use(trident->synth.ilist, instr);
  449. }
  450. static void sample_private1(struct snd_trident * trident, struct snd_trident_voice * voice, unsigned char *data)
  451. {
  452. }
  453. /*
  454. * Memory management / sample loading
  455. */
  456. static int snd_trident_simple_put_sample(void *private_data,
  457. struct simple_instrument * instr,
  458. char __user *data, long len, int atomic)
  459. {
  460. struct snd_trident *trident = private_data;
  461. int size = instr->size;
  462. int shift = 0;
  463. if (instr->format & SIMPLE_WAVE_BACKWARD ||
  464. instr->format & SIMPLE_WAVE_BIDIR ||
  465. instr->format & SIMPLE_WAVE_ULAW)
  466. return -EINVAL; /* not supported */
  467. if (instr->format & SIMPLE_WAVE_16BIT)
  468. shift++;
  469. if (instr->format & SIMPLE_WAVE_STEREO)
  470. shift++;
  471. size <<= shift;
  472. if (trident->synth.current_size + size > trident->synth.max_size)
  473. return -ENOMEM;
  474. if (!access_ok(VERIFY_READ, data, size))
  475. return -EFAULT;
  476. if (trident->tlb.entries) {
  477. struct snd_util_memblk *memblk;
  478. memblk = snd_trident_synth_alloc(trident, size);
  479. if (memblk == NULL)
  480. return -ENOMEM;
  481. if (snd_trident_synth_copy_from_user(trident, memblk, 0, data, size) ) {
  482. snd_trident_synth_free(trident, memblk);
  483. return -EFAULT;
  484. }
  485. instr->address.ptr = (unsigned char*)memblk;
  486. instr->address.memory = memblk->offset;
  487. } else {
  488. struct snd_dma_buffer dmab;
  489. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  490. size, &dmab) < 0)
  491. return -ENOMEM;
  492. if (copy_from_user(dmab.area, data, size)) {
  493. snd_dma_free_pages(&dmab);
  494. return -EFAULT;
  495. }
  496. instr->address.ptr = dmab.area;
  497. instr->address.memory = dmab.addr;
  498. }
  499. trident->synth.current_size += size;
  500. return 0;
  501. }
  502. static int snd_trident_simple_get_sample(void *private_data,
  503. struct simple_instrument * instr,
  504. char __user *data, long len, int atomic)
  505. {
  506. //struct snd_trident *trident = private_data;
  507. int size = instr->size;
  508. int shift = 0;
  509. if (instr->format & SIMPLE_WAVE_16BIT)
  510. shift++;
  511. if (instr->format & SIMPLE_WAVE_STEREO)
  512. shift++;
  513. size <<= shift;
  514. if (!access_ok(VERIFY_WRITE, data, size))
  515. return -EFAULT;
  516. /* FIXME: not implemented yet */
  517. return -EBUSY;
  518. }
  519. static int snd_trident_simple_remove_sample(void *private_data,
  520. struct simple_instrument * instr,
  521. int atomic)
  522. {
  523. struct snd_trident *trident = private_data;
  524. int size = instr->size;
  525. if (instr->format & SIMPLE_WAVE_16BIT)
  526. size <<= 1;
  527. if (instr->format & SIMPLE_WAVE_STEREO)
  528. size <<= 1;
  529. if (trident->tlb.entries) {
  530. struct snd_util_memblk *memblk = (struct snd_util_memblk *)instr->address.ptr;
  531. if (memblk)
  532. snd_trident_synth_free(trident, memblk);
  533. else
  534. return -EFAULT;
  535. } else {
  536. struct snd_dma_buffer dmab;
  537. dmab.dev.type = SNDRV_DMA_TYPE_DEV;
  538. dmab.dev.dev = snd_dma_pci_data(trident->pci);
  539. dmab.area = instr->address.ptr;
  540. dmab.addr = instr->address.memory;
  541. dmab.bytes = size;
  542. snd_dma_free_pages(&dmab);
  543. }
  544. trident->synth.current_size -= size;
  545. if (trident->synth.current_size < 0) /* shouldn't need this check... */
  546. trident->synth.current_size = 0;
  547. return 0;
  548. }
  549. static void select_instrument(struct snd_trident * trident, struct snd_trident_voice * v)
  550. {
  551. struct snd_seq_kinstr *instr;
  552. instr = snd_seq_instr_find(trident->synth.ilist, &v->instr, 0, 1);
  553. if (instr != NULL) {
  554. if (instr->ops) {
  555. if (!strcmp(instr->ops->instr_type, SNDRV_SEQ_INSTR_ID_SIMPLE))
  556. snd_trident_simple_init(v);
  557. }
  558. snd_seq_instr_free_use(trident->synth.ilist, instr);
  559. }
  560. }
  561. /*
  562. */
  563. static void event_sample(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  564. {
  565. if (v->sample_ops && v->sample_ops->sample_stop)
  566. v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
  567. v->instr.std = ev->data.sample.param.sample.std;
  568. if (v->instr.std & 0xff000000) { /* private instrument */
  569. v->instr.std &= 0x00ffffff;
  570. v->instr.std |= (unsigned int)ev->source.client << 24;
  571. }
  572. v->instr.bank = ev->data.sample.param.sample.bank;
  573. v->instr.prg = ev->data.sample.param.sample.prg;
  574. select_instrument(p->trident, v);
  575. }
  576. static void event_cluster(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  577. {
  578. if (v->sample_ops && v->sample_ops->sample_stop)
  579. v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
  580. v->instr.cluster = ev->data.sample.param.cluster.cluster;
  581. select_instrument(p->trident, v);
  582. }
  583. static void event_start(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  584. {
  585. if (v->sample_ops && v->sample_ops->sample_start)
  586. v->sample_ops->sample_start(p->trident, v, ev->data.sample.param.position);
  587. }
  588. static void event_stop(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  589. {
  590. if (v->sample_ops && v->sample_ops->sample_stop)
  591. v->sample_ops->sample_stop(p->trident, v, ev->data.sample.param.stop_mode);
  592. }
  593. static void event_freq(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  594. {
  595. if (v->sample_ops && v->sample_ops->sample_freq)
  596. v->sample_ops->sample_freq(p->trident, v, ev->data.sample.param.frequency);
  597. }
  598. static void event_volume(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  599. {
  600. if (v->sample_ops && v->sample_ops->sample_volume)
  601. v->sample_ops->sample_volume(p->trident, v, &ev->data.sample.param.volume);
  602. }
  603. static void event_loop(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  604. {
  605. if (v->sample_ops && v->sample_ops->sample_loop)
  606. v->sample_ops->sample_loop(p->trident, v, &ev->data.sample.param.loop);
  607. }
  608. static void event_position(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  609. {
  610. if (v->sample_ops && v->sample_ops->sample_pos)
  611. v->sample_ops->sample_pos(p->trident, v, ev->data.sample.param.position);
  612. }
  613. static void event_private1(struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v)
  614. {
  615. if (v->sample_ops && v->sample_ops->sample_private1)
  616. v->sample_ops->sample_private1(p->trident, v, (unsigned char *) &ev->data.sample.param.raw8);
  617. }
  618. typedef void (trident_sample_event_handler_t) (struct snd_seq_event * ev, struct snd_trident_port * p, struct snd_trident_voice * v);
  619. static trident_sample_event_handler_t *trident_sample_event_handlers[9] =
  620. {
  621. event_sample,
  622. event_cluster,
  623. event_start,
  624. event_stop,
  625. event_freq,
  626. event_volume,
  627. event_loop,
  628. event_position,
  629. event_private1
  630. };
  631. static void snd_trident_sample_event(struct snd_seq_event * ev, struct snd_trident_port * p)
  632. {
  633. int idx, voice;
  634. struct snd_trident *trident = p->trident;
  635. struct snd_trident_voice *v;
  636. unsigned long flags;
  637. idx = ev->type - SNDRV_SEQ_EVENT_SAMPLE;
  638. if (idx < 0 || idx > 8)
  639. return;
  640. for (voice = 0; voice < 64; voice++) {
  641. v = &trident->synth.voices[voice];
  642. if (v->use && v->client == ev->source.client &&
  643. v->port == ev->source.port &&
  644. v->index == ev->data.sample.channel) {
  645. spin_lock_irqsave(&trident->event_lock, flags);
  646. trident_sample_event_handlers[idx] (ev, p, v);
  647. spin_unlock_irqrestore(&trident->event_lock, flags);
  648. return;
  649. }
  650. }
  651. }
  652. /*
  653. */
  654. static void snd_trident_synth_free_voices(struct snd_trident * trident, int client, int port)
  655. {
  656. int idx;
  657. struct snd_trident_voice *voice;
  658. for (idx = 0; idx < 32; idx++) {
  659. voice = &trident->synth.voices[idx];
  660. if (voice->use && voice->client == client && voice->port == port)
  661. snd_trident_free_voice(trident, voice);
  662. }
  663. }
  664. static int snd_trident_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
  665. {
  666. struct snd_trident_port *port = private_data;
  667. struct snd_trident *trident = port->trident;
  668. struct snd_trident_voice *voice;
  669. unsigned int idx;
  670. unsigned long flags;
  671. if (info->voices > 32)
  672. return -EINVAL;
  673. spin_lock_irqsave(&trident->reg_lock, flags);
  674. for (idx = 0; idx < info->voices; idx++) {
  675. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_SYNTH, info->sender.client, info->sender.port);
  676. if (voice == NULL) {
  677. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  678. spin_unlock_irqrestore(&trident->reg_lock, flags);
  679. return -EBUSY;
  680. }
  681. voice->index = idx;
  682. voice->Vol = 0x3ff;
  683. voice->EC = 0x0fff;
  684. }
  685. #if 0
  686. for (idx = 0; idx < info->midi_voices; idx++) {
  687. port->midi_has_voices = 1;
  688. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_MIDI, info->sender.client, info->sender.port);
  689. if (voice == NULL) {
  690. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  691. spin_unlock_irqrestore(&trident->reg_lock, flags);
  692. return -EBUSY;
  693. }
  694. voice->Vol = 0x3ff;
  695. voice->EC = 0x0fff;
  696. }
  697. #endif
  698. spin_unlock_irqrestore(&trident->reg_lock, flags);
  699. return 0;
  700. }
  701. static int snd_trident_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
  702. {
  703. struct snd_trident_port *port = private_data;
  704. struct snd_trident *trident = port->trident;
  705. unsigned long flags;
  706. spin_lock_irqsave(&trident->reg_lock, flags);
  707. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  708. spin_unlock_irqrestore(&trident->reg_lock, flags);
  709. return 0;
  710. }
  711. /*
  712. */
  713. static void snd_trident_synth_free_private_instruments(struct snd_trident_port * p, int client)
  714. {
  715. struct snd_seq_instr_header ifree;
  716. memset(&ifree, 0, sizeof(ifree));
  717. ifree.cmd = SNDRV_SEQ_INSTR_FREE_CMD_PRIVATE;
  718. snd_seq_instr_list_free_cond(p->trident->synth.ilist, &ifree, client, 0);
  719. }
  720. static int snd_trident_synth_event_input(struct snd_seq_event * ev, int direct, void *private_data, int atomic, int hop)
  721. {
  722. struct snd_trident_port *p = (struct snd_trident_port *) private_data;
  723. if (p == NULL)
  724. return -EINVAL;
  725. if (ev->type >= SNDRV_SEQ_EVENT_SAMPLE &&
  726. ev->type <= SNDRV_SEQ_EVENT_SAMPLE_PRIVATE1) {
  727. snd_trident_sample_event(ev, p);
  728. return 0;
  729. }
  730. if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM &&
  731. ev->source.port == SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE) {
  732. if (ev->type == SNDRV_SEQ_EVENT_CLIENT_EXIT) {
  733. snd_trident_synth_free_private_instruments(p, ev->data.addr.client);
  734. return 0;
  735. }
  736. }
  737. if (direct) {
  738. if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN) {
  739. snd_seq_instr_event(&p->trident->synth.simple_ops.kops,
  740. p->trident->synth.ilist, ev,
  741. p->trident->synth.seq_client, atomic, hop);
  742. return 0;
  743. }
  744. }
  745. return 0;
  746. }
  747. static void snd_trident_synth_instr_notify(void *private_data,
  748. struct snd_seq_kinstr * instr,
  749. int what)
  750. {
  751. int idx;
  752. struct snd_trident *trident = private_data;
  753. struct snd_trident_voice *pvoice;
  754. unsigned long flags;
  755. spin_lock_irqsave(&trident->event_lock, flags);
  756. for (idx = 0; idx < 64; idx++) {
  757. pvoice = &trident->synth.voices[idx];
  758. if (pvoice->use && !memcmp(&pvoice->instr, &instr->instr, sizeof(pvoice->instr))) {
  759. if (pvoice->sample_ops && pvoice->sample_ops->sample_stop) {
  760. pvoice->sample_ops->sample_stop(trident, pvoice, SAMPLE_STOP_IMMEDIATELY);
  761. } else {
  762. snd_trident_stop_voice(trident, pvoice->number);
  763. pvoice->flags &= ~SNDRV_TRIDENT_VFLG_RUNNING;
  764. }
  765. }
  766. }
  767. spin_unlock_irqrestore(&trident->event_lock, flags);
  768. }
  769. /*
  770. */
  771. static void snd_trident_synth_free_port(void *private_data)
  772. {
  773. struct snd_trident_port *p = (struct snd_trident_port *) private_data;
  774. if (p)
  775. snd_midi_channel_free_set(p->chset);
  776. }
  777. static int snd_trident_synth_create_port(struct snd_trident * trident, int idx)
  778. {
  779. struct snd_trident_port *p;
  780. struct snd_seq_port_callback callbacks;
  781. char name[32];
  782. char *str;
  783. int result;
  784. p = &trident->synth.seq_ports[idx];
  785. p->chset = snd_midi_channel_alloc_set(16);
  786. if (p->chset == NULL)
  787. return -ENOMEM;
  788. p->chset->private_data = p;
  789. p->trident = trident;
  790. p->client = trident->synth.seq_client;
  791. memset(&callbacks, 0, sizeof(callbacks));
  792. callbacks.owner = THIS_MODULE;
  793. callbacks.use = snd_trident_synth_use;
  794. callbacks.unuse = snd_trident_synth_unuse;
  795. callbacks.event_input = snd_trident_synth_event_input;
  796. callbacks.private_free = snd_trident_synth_free_port;
  797. callbacks.private_data = p;
  798. str = "???";
  799. switch (trident->device) {
  800. case TRIDENT_DEVICE_ID_DX: str = "Trident 4DWave-DX"; break;
  801. case TRIDENT_DEVICE_ID_NX: str = "Trident 4DWave-NX"; break;
  802. case TRIDENT_DEVICE_ID_SI7018: str = "SiS 7018"; break;
  803. }
  804. sprintf(name, "%s port %i", str, idx);
  805. p->chset->port = snd_seq_event_port_attach(trident->synth.seq_client,
  806. &callbacks,
  807. SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  808. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
  809. SNDRV_SEQ_PORT_TYPE_SYNTH,
  810. 16, 0,
  811. name);
  812. if (p->chset->port < 0) {
  813. result = p->chset->port;
  814. snd_trident_synth_free_port(p);
  815. return result;
  816. }
  817. p->port = p->chset->port;
  818. return 0;
  819. }
  820. /*
  821. */
  822. static int snd_trident_synth_new_device(struct snd_seq_device *dev)
  823. {
  824. struct snd_trident *trident;
  825. int client, i;
  826. struct snd_seq_port_subscribe sub;
  827. struct snd_simple_ops *simpleops;
  828. char *str;
  829. trident = *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  830. if (trident == NULL)
  831. return -EINVAL;
  832. trident->synth.seq_client = -1;
  833. /* allocate new client */
  834. str = "???";
  835. switch (trident->device) {
  836. case TRIDENT_DEVICE_ID_DX: str = "Trident 4DWave-DX"; break;
  837. case TRIDENT_DEVICE_ID_NX: str = "Trident 4DWave-NX"; break;
  838. case TRIDENT_DEVICE_ID_SI7018: str = "SiS 7018"; break;
  839. }
  840. client = trident->synth.seq_client =
  841. snd_seq_create_kernel_client(trident->card, 1, str);
  842. if (client < 0)
  843. return client;
  844. for (i = 0; i < 4; i++)
  845. snd_trident_synth_create_port(trident, i);
  846. trident->synth.ilist = snd_seq_instr_list_new();
  847. if (trident->synth.ilist == NULL) {
  848. snd_seq_delete_kernel_client(client);
  849. trident->synth.seq_client = -1;
  850. return -ENOMEM;
  851. }
  852. trident->synth.ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
  853. simpleops = &trident->synth.simple_ops;
  854. snd_seq_simple_init(simpleops, trident, NULL);
  855. simpleops->put_sample = snd_trident_simple_put_sample;
  856. simpleops->get_sample = snd_trident_simple_get_sample;
  857. simpleops->remove_sample = snd_trident_simple_remove_sample;
  858. simpleops->notify = snd_trident_synth_instr_notify;
  859. memset(&sub, 0, sizeof(sub));
  860. sub.sender.client = SNDRV_SEQ_CLIENT_SYSTEM;
  861. sub.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
  862. sub.dest.client = client;
  863. sub.dest.port = 0;
  864. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &sub);
  865. return 0;
  866. }
  867. static int snd_trident_synth_delete_device(struct snd_seq_device *dev)
  868. {
  869. struct snd_trident *trident;
  870. trident = *(struct snd_trident **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  871. if (trident == NULL)
  872. return -EINVAL;
  873. if (trident->synth.seq_client >= 0) {
  874. snd_seq_delete_kernel_client(trident->synth.seq_client);
  875. trident->synth.seq_client = -1;
  876. }
  877. if (trident->synth.ilist)
  878. snd_seq_instr_list_free(&trident->synth.ilist);
  879. return 0;
  880. }
  881. static int __init alsa_trident_synth_init(void)
  882. {
  883. static struct snd_seq_dev_ops ops =
  884. {
  885. snd_trident_synth_new_device,
  886. snd_trident_synth_delete_device
  887. };
  888. return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_TRIDENT, &ops,
  889. sizeof(struct snd_trident *));
  890. }
  891. static void __exit alsa_trident_synth_exit(void)
  892. {
  893. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_TRIDENT);
  894. }
  895. module_init(alsa_trident_synth_init)
  896. module_exit(alsa_trident_synth_exit)