trident_synth.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position);
  186. static void sample_stop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_stop_mode_t mode);
  187. static void sample_freq(trident_t * trident, snd_trident_voice_t * voice, snd_seq_frequency_t freq);
  188. static void sample_volume(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_volume_t * volume);
  189. static void sample_loop(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_loop_t * loop);
  190. static void sample_pos(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position);
  191. static void sample_private1(trident_t * trident, snd_trident_voice_t * voice, unsigned char *data);
  192. static snd_trident_sample_ops_t 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(snd_trident_voice_t * 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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position)
  211. {
  212. simple_instrument_t *simple;
  213. snd_seq_kinstr_t *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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_stop_mode_t 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(trident_t * trident, snd_trident_voice_t * 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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_volume_t * 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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_ev_loop_t * loop)
  377. {
  378. unsigned long flags;
  379. simple_instrument_t *simple;
  380. snd_seq_kinstr_t *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(trident_t * trident, snd_trident_voice_t * voice, snd_seq_position_t position)
  409. {
  410. unsigned long flags;
  411. simple_instrument_t *simple;
  412. snd_seq_kinstr_t *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(trident_t * trident, snd_trident_voice_t * voice, unsigned char *data)
  451. {
  452. }
  453. /*
  454. * Memory management / sample loading
  455. */
  456. static int snd_trident_simple_put_sample(void *private_data, simple_instrument_t * instr,
  457. char __user *data, long len, int atomic)
  458. {
  459. trident_t *trident = private_data;
  460. int size = instr->size;
  461. int shift = 0;
  462. if (instr->format & SIMPLE_WAVE_BACKWARD ||
  463. instr->format & SIMPLE_WAVE_BIDIR ||
  464. instr->format & SIMPLE_WAVE_ULAW)
  465. return -EINVAL; /* not supported */
  466. if (instr->format & SIMPLE_WAVE_16BIT)
  467. shift++;
  468. if (instr->format & SIMPLE_WAVE_STEREO)
  469. shift++;
  470. size <<= shift;
  471. if (trident->synth.current_size + size > trident->synth.max_size)
  472. return -ENOMEM;
  473. if (!access_ok(VERIFY_READ, data, size))
  474. return -EFAULT;
  475. if (trident->tlb.entries) {
  476. snd_util_memblk_t *memblk;
  477. memblk = snd_trident_synth_alloc(trident, size);
  478. if (memblk == NULL)
  479. return -ENOMEM;
  480. if (snd_trident_synth_copy_from_user(trident, memblk, 0, data, size) ) {
  481. snd_trident_synth_free(trident, memblk);
  482. return -EFAULT;
  483. }
  484. instr->address.ptr = (unsigned char*)memblk;
  485. instr->address.memory = memblk->offset;
  486. } else {
  487. struct snd_dma_buffer dmab;
  488. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(trident->pci),
  489. size, &dmab) < 0)
  490. return -ENOMEM;
  491. if (copy_from_user(dmab.area, data, size)) {
  492. snd_dma_free_pages(&dmab);
  493. return -EFAULT;
  494. }
  495. instr->address.ptr = dmab.area;
  496. instr->address.memory = dmab.addr;
  497. }
  498. trident->synth.current_size += size;
  499. return 0;
  500. }
  501. static int snd_trident_simple_get_sample(void *private_data, simple_instrument_t * instr,
  502. char __user *data, long len, int atomic)
  503. {
  504. //trident_t *trident = private_data;
  505. int size = instr->size;
  506. int shift = 0;
  507. if (instr->format & SIMPLE_WAVE_16BIT)
  508. shift++;
  509. if (instr->format & SIMPLE_WAVE_STEREO)
  510. shift++;
  511. size <<= shift;
  512. if (!access_ok(VERIFY_WRITE, data, size))
  513. return -EFAULT;
  514. /* FIXME: not implemented yet */
  515. return -EBUSY;
  516. }
  517. static int snd_trident_simple_remove_sample(void *private_data, simple_instrument_t * instr,
  518. int atomic)
  519. {
  520. trident_t *trident = private_data;
  521. int size = instr->size;
  522. if (instr->format & SIMPLE_WAVE_16BIT)
  523. size <<= 1;
  524. if (instr->format & SIMPLE_WAVE_STEREO)
  525. size <<= 1;
  526. if (trident->tlb.entries) {
  527. snd_util_memblk_t *memblk = (snd_util_memblk_t*)instr->address.ptr;
  528. if (memblk)
  529. snd_trident_synth_free(trident, memblk);
  530. else
  531. return -EFAULT;
  532. } else {
  533. struct snd_dma_buffer dmab;
  534. dmab.dev.type = SNDRV_DMA_TYPE_DEV;
  535. dmab.dev.dev = snd_dma_pci_data(trident->pci);
  536. dmab.area = instr->address.ptr;
  537. dmab.addr = instr->address.memory;
  538. dmab.bytes = size;
  539. snd_dma_free_pages(&dmab);
  540. }
  541. trident->synth.current_size -= size;
  542. if (trident->synth.current_size < 0) /* shouldn't need this check... */
  543. trident->synth.current_size = 0;
  544. return 0;
  545. }
  546. static void select_instrument(trident_t * trident, snd_trident_voice_t * v)
  547. {
  548. snd_seq_kinstr_t *instr;
  549. instr = snd_seq_instr_find(trident->synth.ilist, &v->instr, 0, 1);
  550. if (instr != NULL) {
  551. if (instr->ops) {
  552. if (!strcmp(instr->ops->instr_type, SNDRV_SEQ_INSTR_ID_SIMPLE))
  553. snd_trident_simple_init(v);
  554. }
  555. snd_seq_instr_free_use(trident->synth.ilist, instr);
  556. }
  557. }
  558. /*
  559. */
  560. static void event_sample(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  561. {
  562. if (v->sample_ops && v->sample_ops->sample_stop)
  563. v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
  564. v->instr.std = ev->data.sample.param.sample.std;
  565. if (v->instr.std & 0xff000000) { /* private instrument */
  566. v->instr.std &= 0x00ffffff;
  567. v->instr.std |= (unsigned int)ev->source.client << 24;
  568. }
  569. v->instr.bank = ev->data.sample.param.sample.bank;
  570. v->instr.prg = ev->data.sample.param.sample.prg;
  571. select_instrument(p->trident, v);
  572. }
  573. static void event_cluster(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  574. {
  575. if (v->sample_ops && v->sample_ops->sample_stop)
  576. v->sample_ops->sample_stop(p->trident, v, SAMPLE_STOP_IMMEDIATELY);
  577. v->instr.cluster = ev->data.sample.param.cluster.cluster;
  578. select_instrument(p->trident, v);
  579. }
  580. static void event_start(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  581. {
  582. if (v->sample_ops && v->sample_ops->sample_start)
  583. v->sample_ops->sample_start(p->trident, v, ev->data.sample.param.position);
  584. }
  585. static void event_stop(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  586. {
  587. if (v->sample_ops && v->sample_ops->sample_stop)
  588. v->sample_ops->sample_stop(p->trident, v, ev->data.sample.param.stop_mode);
  589. }
  590. static void event_freq(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  591. {
  592. if (v->sample_ops && v->sample_ops->sample_freq)
  593. v->sample_ops->sample_freq(p->trident, v, ev->data.sample.param.frequency);
  594. }
  595. static void event_volume(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  596. {
  597. if (v->sample_ops && v->sample_ops->sample_volume)
  598. v->sample_ops->sample_volume(p->trident, v, &ev->data.sample.param.volume);
  599. }
  600. static void event_loop(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  601. {
  602. if (v->sample_ops && v->sample_ops->sample_loop)
  603. v->sample_ops->sample_loop(p->trident, v, &ev->data.sample.param.loop);
  604. }
  605. static void event_position(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  606. {
  607. if (v->sample_ops && v->sample_ops->sample_pos)
  608. v->sample_ops->sample_pos(p->trident, v, ev->data.sample.param.position);
  609. }
  610. static void event_private1(snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v)
  611. {
  612. if (v->sample_ops && v->sample_ops->sample_private1)
  613. v->sample_ops->sample_private1(p->trident, v, (unsigned char *) &ev->data.sample.param.raw8);
  614. }
  615. typedef void (trident_sample_event_handler_t) (snd_seq_event_t * ev, snd_trident_port_t * p, snd_trident_voice_t * v);
  616. static trident_sample_event_handler_t *trident_sample_event_handlers[9] =
  617. {
  618. event_sample,
  619. event_cluster,
  620. event_start,
  621. event_stop,
  622. event_freq,
  623. event_volume,
  624. event_loop,
  625. event_position,
  626. event_private1
  627. };
  628. static void snd_trident_sample_event(snd_seq_event_t * ev, snd_trident_port_t * p)
  629. {
  630. int idx, voice;
  631. trident_t *trident = p->trident;
  632. snd_trident_voice_t *v;
  633. unsigned long flags;
  634. idx = ev->type - SNDRV_SEQ_EVENT_SAMPLE;
  635. if (idx < 0 || idx > 8)
  636. return;
  637. for (voice = 0; voice < 64; voice++) {
  638. v = &trident->synth.voices[voice];
  639. if (v->use && v->client == ev->source.client &&
  640. v->port == ev->source.port &&
  641. v->index == ev->data.sample.channel) {
  642. spin_lock_irqsave(&trident->event_lock, flags);
  643. trident_sample_event_handlers[idx] (ev, p, v);
  644. spin_unlock_irqrestore(&trident->event_lock, flags);
  645. return;
  646. }
  647. }
  648. }
  649. /*
  650. */
  651. static void snd_trident_synth_free_voices(trident_t * trident, int client, int port)
  652. {
  653. int idx;
  654. snd_trident_voice_t *voice;
  655. for (idx = 0; idx < 32; idx++) {
  656. voice = &trident->synth.voices[idx];
  657. if (voice->use && voice->client == client && voice->port == port)
  658. snd_trident_free_voice(trident, voice);
  659. }
  660. }
  661. static int snd_trident_synth_use(void *private_data, snd_seq_port_subscribe_t * info)
  662. {
  663. snd_trident_port_t *port = (snd_trident_port_t *) private_data;
  664. trident_t *trident = port->trident;
  665. snd_trident_voice_t *voice;
  666. unsigned int idx;
  667. unsigned long flags;
  668. if (info->voices > 32)
  669. return -EINVAL;
  670. spin_lock_irqsave(&trident->reg_lock, flags);
  671. for (idx = 0; idx < info->voices; idx++) {
  672. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_SYNTH, info->sender.client, info->sender.port);
  673. if (voice == NULL) {
  674. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  675. spin_unlock_irqrestore(&trident->reg_lock, flags);
  676. return -EBUSY;
  677. }
  678. voice->index = idx;
  679. voice->Vol = 0x3ff;
  680. voice->EC = 0x0fff;
  681. }
  682. #if 0
  683. for (idx = 0; idx < info->midi_voices; idx++) {
  684. port->midi_has_voices = 1;
  685. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_MIDI, info->sender.client, info->sender.port);
  686. if (voice == NULL) {
  687. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  688. spin_unlock_irqrestore(&trident->reg_lock, flags);
  689. return -EBUSY;
  690. }
  691. voice->Vol = 0x3ff;
  692. voice->EC = 0x0fff;
  693. }
  694. #endif
  695. spin_unlock_irqrestore(&trident->reg_lock, flags);
  696. return 0;
  697. }
  698. static int snd_trident_synth_unuse(void *private_data, snd_seq_port_subscribe_t * info)
  699. {
  700. snd_trident_port_t *port = (snd_trident_port_t *) private_data;
  701. trident_t *trident = port->trident;
  702. unsigned long flags;
  703. spin_lock_irqsave(&trident->reg_lock, flags);
  704. snd_trident_synth_free_voices(trident, info->sender.client, info->sender.port);
  705. spin_unlock_irqrestore(&trident->reg_lock, flags);
  706. return 0;
  707. }
  708. /*
  709. */
  710. static void snd_trident_synth_free_private_instruments(snd_trident_port_t * p, int client)
  711. {
  712. snd_seq_instr_header_t ifree;
  713. memset(&ifree, 0, sizeof(ifree));
  714. ifree.cmd = SNDRV_SEQ_INSTR_FREE_CMD_PRIVATE;
  715. snd_seq_instr_list_free_cond(p->trident->synth.ilist, &ifree, client, 0);
  716. }
  717. static int snd_trident_synth_event_input(snd_seq_event_t * ev, int direct, void *private_data, int atomic, int hop)
  718. {
  719. snd_trident_port_t *p = (snd_trident_port_t *) private_data;
  720. if (p == NULL)
  721. return -EINVAL;
  722. if (ev->type >= SNDRV_SEQ_EVENT_SAMPLE &&
  723. ev->type <= SNDRV_SEQ_EVENT_SAMPLE_PRIVATE1) {
  724. snd_trident_sample_event(ev, p);
  725. return 0;
  726. }
  727. if (ev->source.client == SNDRV_SEQ_CLIENT_SYSTEM &&
  728. ev->source.port == SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE) {
  729. if (ev->type == SNDRV_SEQ_EVENT_CLIENT_EXIT) {
  730. snd_trident_synth_free_private_instruments(p, ev->data.addr.client);
  731. return 0;
  732. }
  733. }
  734. if (direct) {
  735. if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN) {
  736. snd_seq_instr_event(&p->trident->synth.simple_ops.kops,
  737. p->trident->synth.ilist, ev,
  738. p->trident->synth.seq_client, atomic, hop);
  739. return 0;
  740. }
  741. }
  742. return 0;
  743. }
  744. static void snd_trident_synth_instr_notify(void *private_data,
  745. snd_seq_kinstr_t * instr,
  746. int what)
  747. {
  748. int idx;
  749. trident_t *trident = private_data;
  750. snd_trident_voice_t *pvoice;
  751. unsigned long flags;
  752. spin_lock_irqsave(&trident->event_lock, flags);
  753. for (idx = 0; idx < 64; idx++) {
  754. pvoice = &trident->synth.voices[idx];
  755. if (pvoice->use && !memcmp(&pvoice->instr, &instr->instr, sizeof(pvoice->instr))) {
  756. if (pvoice->sample_ops && pvoice->sample_ops->sample_stop) {
  757. pvoice->sample_ops->sample_stop(trident, pvoice, SAMPLE_STOP_IMMEDIATELY);
  758. } else {
  759. snd_trident_stop_voice(trident, pvoice->number);
  760. pvoice->flags &= ~SNDRV_TRIDENT_VFLG_RUNNING;
  761. }
  762. }
  763. }
  764. spin_unlock_irqrestore(&trident->event_lock, flags);
  765. }
  766. /*
  767. */
  768. static void snd_trident_synth_free_port(void *private_data)
  769. {
  770. snd_trident_port_t *p = (snd_trident_port_t *) private_data;
  771. if (p)
  772. snd_midi_channel_free_set(p->chset);
  773. }
  774. static int snd_trident_synth_create_port(trident_t * trident, int idx)
  775. {
  776. snd_trident_port_t *p;
  777. snd_seq_port_callback_t callbacks;
  778. char name[32];
  779. char *str;
  780. int result;
  781. p = &trident->synth.seq_ports[idx];
  782. p->chset = snd_midi_channel_alloc_set(16);
  783. if (p->chset == NULL)
  784. return -ENOMEM;
  785. p->chset->private_data = p;
  786. p->trident = trident;
  787. p->client = trident->synth.seq_client;
  788. memset(&callbacks, 0, sizeof(callbacks));
  789. callbacks.owner = THIS_MODULE;
  790. callbacks.use = snd_trident_synth_use;
  791. callbacks.unuse = snd_trident_synth_unuse;
  792. callbacks.event_input = snd_trident_synth_event_input;
  793. callbacks.private_free = snd_trident_synth_free_port;
  794. callbacks.private_data = p;
  795. str = "???";
  796. switch (trident->device) {
  797. case TRIDENT_DEVICE_ID_DX: str = "Trident 4DWave-DX"; break;
  798. case TRIDENT_DEVICE_ID_NX: str = "Trident 4DWave-NX"; break;
  799. case TRIDENT_DEVICE_ID_SI7018: str = "SiS 7018"; break;
  800. }
  801. sprintf(name, "%s port %i", str, idx);
  802. p->chset->port = snd_seq_event_port_attach(trident->synth.seq_client,
  803. &callbacks,
  804. SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
  805. SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
  806. SNDRV_SEQ_PORT_TYPE_SYNTH,
  807. 16, 0,
  808. name);
  809. if (p->chset->port < 0) {
  810. result = p->chset->port;
  811. snd_trident_synth_free_port(p);
  812. return result;
  813. }
  814. p->port = p->chset->port;
  815. return 0;
  816. }
  817. /*
  818. */
  819. static int snd_trident_synth_new_device(snd_seq_device_t *dev)
  820. {
  821. trident_t *trident;
  822. int client, i;
  823. snd_seq_client_callback_t callbacks;
  824. snd_seq_client_info_t cinfo;
  825. snd_seq_port_subscribe_t sub;
  826. snd_simple_ops_t *simpleops;
  827. char *str;
  828. trident = *(trident_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  829. if (trident == NULL)
  830. return -EINVAL;
  831. trident->synth.seq_client = -1;
  832. /* allocate new client */
  833. memset(&callbacks, 0, sizeof(callbacks));
  834. callbacks.private_data = trident;
  835. callbacks.allow_output = callbacks.allow_input = 1;
  836. client = trident->synth.seq_client =
  837. snd_seq_create_kernel_client(trident->card, 1, &callbacks);
  838. if (client < 0)
  839. return client;
  840. /* change name of client */
  841. memset(&cinfo, 0, sizeof(cinfo));
  842. cinfo.client = client;
  843. cinfo.type = KERNEL_CLIENT;
  844. str = "???";
  845. switch (trident->device) {
  846. case TRIDENT_DEVICE_ID_DX: str = "Trident 4DWave-DX"; break;
  847. case TRIDENT_DEVICE_ID_NX: str = "Trident 4DWave-NX"; break;
  848. case TRIDENT_DEVICE_ID_SI7018: str = "SiS 7018"; break;
  849. }
  850. sprintf(cinfo.name, str);
  851. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
  852. for (i = 0; i < 4; i++)
  853. snd_trident_synth_create_port(trident, i);
  854. trident->synth.ilist = snd_seq_instr_list_new();
  855. if (trident->synth.ilist == NULL) {
  856. snd_seq_delete_kernel_client(client);
  857. trident->synth.seq_client = -1;
  858. return -ENOMEM;
  859. }
  860. trident->synth.ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
  861. simpleops = &trident->synth.simple_ops;
  862. snd_seq_simple_init(simpleops, trident, NULL);
  863. simpleops->put_sample = snd_trident_simple_put_sample;
  864. simpleops->get_sample = snd_trident_simple_get_sample;
  865. simpleops->remove_sample = snd_trident_simple_remove_sample;
  866. simpleops->notify = snd_trident_synth_instr_notify;
  867. memset(&sub, 0, sizeof(sub));
  868. sub.sender.client = SNDRV_SEQ_CLIENT_SYSTEM;
  869. sub.sender.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
  870. sub.dest.client = client;
  871. sub.dest.port = 0;
  872. snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &sub);
  873. return 0;
  874. }
  875. static int snd_trident_synth_delete_device(snd_seq_device_t *dev)
  876. {
  877. trident_t *trident;
  878. trident = *(trident_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
  879. if (trident == NULL)
  880. return -EINVAL;
  881. if (trident->synth.seq_client >= 0) {
  882. snd_seq_delete_kernel_client(trident->synth.seq_client);
  883. trident->synth.seq_client = -1;
  884. }
  885. if (trident->synth.ilist)
  886. snd_seq_instr_list_free(&trident->synth.ilist);
  887. return 0;
  888. }
  889. static int __init alsa_trident_synth_init(void)
  890. {
  891. static snd_seq_dev_ops_t ops =
  892. {
  893. snd_trident_synth_new_device,
  894. snd_trident_synth_delete_device
  895. };
  896. return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_TRIDENT, &ops,
  897. sizeof(trident_t*));
  898. }
  899. static void __exit alsa_trident_synth_exit(void)
  900. {
  901. snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_TRIDENT);
  902. }
  903. module_init(alsa_trident_synth_init)
  904. module_exit(alsa_trident_synth_exit)