opl3_synth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
  3. *
  4. * Routines for OPL2/OPL3/OPL4 control
  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 <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <linux/nospec.h>
  24. #include <sound/opl3.h>
  25. #include <sound/asound_fm.h>
  26. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  27. #define OPL3_SUPPORT_SYNTH
  28. #endif
  29. /*
  30. * There is 18 possible 2 OP voices
  31. * (9 in the left and 9 in the right).
  32. * The first OP is the modulator and 2nd is the carrier.
  33. *
  34. * The first three voices in the both sides may be connected
  35. * with another voice to a 4 OP voice. For example voice 0
  36. * can be connected with voice 3. The operators of voice 3 are
  37. * used as operators 3 and 4 of the new 4 OP voice.
  38. * In this case the 2 OP voice number 0 is the 'first half' and
  39. * voice 3 is the second.
  40. */
  41. /*
  42. * Register offset table for OPL2/3 voices,
  43. * OPL2 / one OPL3 register array side only
  44. */
  45. char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
  46. {
  47. /* OP1 OP2 OP3 OP4 */
  48. /* ------------------------ */
  49. { 0x00, 0x03, 0x08, 0x0b },
  50. { 0x01, 0x04, 0x09, 0x0c },
  51. { 0x02, 0x05, 0x0a, 0x0d },
  52. { 0x08, 0x0b, 0x00, 0x00 },
  53. { 0x09, 0x0c, 0x00, 0x00 },
  54. { 0x0a, 0x0d, 0x00, 0x00 },
  55. { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
  56. { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
  57. { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
  58. };
  59. EXPORT_SYMBOL(snd_opl3_regmap);
  60. /*
  61. * prototypes
  62. */
  63. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note);
  64. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice);
  65. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params);
  66. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode);
  67. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection);
  68. /* ------------------------------ */
  69. /*
  70. * open the device exclusively
  71. */
  72. int snd_opl3_open(struct snd_hwdep * hw, struct file *file)
  73. {
  74. return 0;
  75. }
  76. /*
  77. * ioctl for hwdep device:
  78. */
  79. int snd_opl3_ioctl(struct snd_hwdep * hw, struct file *file,
  80. unsigned int cmd, unsigned long arg)
  81. {
  82. struct snd_opl3 *opl3 = hw->private_data;
  83. void __user *argp = (void __user *)arg;
  84. if (snd_BUG_ON(!opl3))
  85. return -EINVAL;
  86. switch (cmd) {
  87. /* get information */
  88. case SNDRV_DM_FM_IOCTL_INFO:
  89. {
  90. struct snd_dm_fm_info info;
  91. memset(&info, 0, sizeof(info));
  92. info.fm_mode = opl3->fm_mode;
  93. info.rhythm = opl3->rhythm;
  94. if (copy_to_user(argp, &info, sizeof(struct snd_dm_fm_info)))
  95. return -EFAULT;
  96. return 0;
  97. }
  98. case SNDRV_DM_FM_IOCTL_RESET:
  99. #ifdef CONFIG_SND_OSSEMUL
  100. case SNDRV_DM_FM_OSS_IOCTL_RESET:
  101. #endif
  102. snd_opl3_reset(opl3);
  103. return 0;
  104. case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
  105. #ifdef CONFIG_SND_OSSEMUL
  106. case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
  107. #endif
  108. {
  109. struct snd_dm_fm_note note;
  110. if (copy_from_user(&note, argp, sizeof(struct snd_dm_fm_note)))
  111. return -EFAULT;
  112. return snd_opl3_play_note(opl3, &note);
  113. }
  114. case SNDRV_DM_FM_IOCTL_SET_VOICE:
  115. #ifdef CONFIG_SND_OSSEMUL
  116. case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
  117. #endif
  118. {
  119. struct snd_dm_fm_voice voice;
  120. if (copy_from_user(&voice, argp, sizeof(struct snd_dm_fm_voice)))
  121. return -EFAULT;
  122. return snd_opl3_set_voice(opl3, &voice);
  123. }
  124. case SNDRV_DM_FM_IOCTL_SET_PARAMS:
  125. #ifdef CONFIG_SND_OSSEMUL
  126. case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
  127. #endif
  128. {
  129. struct snd_dm_fm_params params;
  130. if (copy_from_user(&params, argp, sizeof(struct snd_dm_fm_params)))
  131. return -EFAULT;
  132. return snd_opl3_set_params(opl3, &params);
  133. }
  134. case SNDRV_DM_FM_IOCTL_SET_MODE:
  135. #ifdef CONFIG_SND_OSSEMUL
  136. case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
  137. #endif
  138. return snd_opl3_set_mode(opl3, (int) arg);
  139. case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
  140. #ifdef CONFIG_SND_OSSEMUL
  141. case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
  142. #endif
  143. return snd_opl3_set_connection(opl3, (int) arg);
  144. #ifdef OPL3_SUPPORT_SYNTH
  145. case SNDRV_DM_FM_IOCTL_CLEAR_PATCHES:
  146. snd_opl3_clear_patches(opl3);
  147. return 0;
  148. #endif
  149. #ifdef CONFIG_SND_DEBUG
  150. default:
  151. snd_printk(KERN_WARNING "unknown IOCTL: 0x%x\n", cmd);
  152. #endif
  153. }
  154. return -ENOTTY;
  155. }
  156. /*
  157. * close the device
  158. */
  159. int snd_opl3_release(struct snd_hwdep * hw, struct file *file)
  160. {
  161. struct snd_opl3 *opl3 = hw->private_data;
  162. snd_opl3_reset(opl3);
  163. return 0;
  164. }
  165. #ifdef OPL3_SUPPORT_SYNTH
  166. /*
  167. * write the device - load patches
  168. */
  169. long snd_opl3_write(struct snd_hwdep *hw, const char __user *buf, long count,
  170. loff_t *offset)
  171. {
  172. struct snd_opl3 *opl3 = hw->private_data;
  173. long result = 0;
  174. int err = 0;
  175. struct sbi_patch inst;
  176. while (count >= sizeof(inst)) {
  177. unsigned char type;
  178. if (copy_from_user(&inst, buf, sizeof(inst)))
  179. return -EFAULT;
  180. if (!memcmp(inst.key, FM_KEY_SBI, 4) ||
  181. !memcmp(inst.key, FM_KEY_2OP, 4))
  182. type = FM_PATCH_OPL2;
  183. else if (!memcmp(inst.key, FM_KEY_4OP, 4))
  184. type = FM_PATCH_OPL3;
  185. else /* invalid type */
  186. break;
  187. err = snd_opl3_load_patch(opl3, inst.prog, inst.bank, type,
  188. inst.name, inst.extension,
  189. inst.data);
  190. if (err < 0)
  191. break;
  192. result += sizeof(inst);
  193. count -= sizeof(inst);
  194. }
  195. return result > 0 ? result : err;
  196. }
  197. /*
  198. * Patch management
  199. */
  200. /* offsets for SBI params */
  201. #define AM_VIB 0
  202. #define KSL_LEVEL 2
  203. #define ATTACK_DECAY 4
  204. #define SUSTAIN_RELEASE 6
  205. #define WAVE_SELECT 8
  206. /* offset for SBI instrument */
  207. #define CONNECTION 10
  208. #define OFFSET_4OP 11
  209. /*
  210. * load a patch, obviously.
  211. *
  212. * loaded on the given program and bank numbers with the given type
  213. * (FM_PATCH_OPLx).
  214. * data is the pointer of SBI record _without_ header (key and name).
  215. * name is the name string of the patch.
  216. * ext is the extension data of 7 bytes long (stored in name of SBI
  217. * data up to offset 25), or NULL to skip.
  218. * return 0 if successful or a negative error code.
  219. */
  220. int snd_opl3_load_patch(struct snd_opl3 *opl3,
  221. int prog, int bank, int type,
  222. const char *name,
  223. const unsigned char *ext,
  224. const unsigned char *data)
  225. {
  226. struct fm_patch *patch;
  227. int i;
  228. patch = snd_opl3_find_patch(opl3, prog, bank, 1);
  229. if (!patch)
  230. return -ENOMEM;
  231. patch->type = type;
  232. for (i = 0; i < 2; i++) {
  233. patch->inst.op[i].am_vib = data[AM_VIB + i];
  234. patch->inst.op[i].ksl_level = data[KSL_LEVEL + i];
  235. patch->inst.op[i].attack_decay = data[ATTACK_DECAY + i];
  236. patch->inst.op[i].sustain_release = data[SUSTAIN_RELEASE + i];
  237. patch->inst.op[i].wave_select = data[WAVE_SELECT + i];
  238. }
  239. patch->inst.feedback_connection[0] = data[CONNECTION];
  240. if (type == FM_PATCH_OPL3) {
  241. for (i = 0; i < 2; i++) {
  242. patch->inst.op[i+2].am_vib =
  243. data[OFFSET_4OP + AM_VIB + i];
  244. patch->inst.op[i+2].ksl_level =
  245. data[OFFSET_4OP + KSL_LEVEL + i];
  246. patch->inst.op[i+2].attack_decay =
  247. data[OFFSET_4OP + ATTACK_DECAY + i];
  248. patch->inst.op[i+2].sustain_release =
  249. data[OFFSET_4OP + SUSTAIN_RELEASE + i];
  250. patch->inst.op[i+2].wave_select =
  251. data[OFFSET_4OP + WAVE_SELECT + i];
  252. }
  253. patch->inst.feedback_connection[1] =
  254. data[OFFSET_4OP + CONNECTION];
  255. }
  256. if (ext) {
  257. patch->inst.echo_delay = ext[0];
  258. patch->inst.echo_atten = ext[1];
  259. patch->inst.chorus_spread = ext[2];
  260. patch->inst.trnsps = ext[3];
  261. patch->inst.fix_dur = ext[4];
  262. patch->inst.modes = ext[5];
  263. patch->inst.fix_key = ext[6];
  264. }
  265. if (name)
  266. strlcpy(patch->name, name, sizeof(patch->name));
  267. return 0;
  268. }
  269. EXPORT_SYMBOL(snd_opl3_load_patch);
  270. /*
  271. * find a patch with the given program and bank numbers, returns its pointer
  272. * if no matching patch is found and create_patch is set, it creates a
  273. * new patch object.
  274. */
  275. struct fm_patch *snd_opl3_find_patch(struct snd_opl3 *opl3, int prog, int bank,
  276. int create_patch)
  277. {
  278. /* pretty dumb hash key */
  279. unsigned int key = (prog + bank) % OPL3_PATCH_HASH_SIZE;
  280. struct fm_patch *patch;
  281. for (patch = opl3->patch_table[key]; patch; patch = patch->next) {
  282. if (patch->prog == prog && patch->bank == bank)
  283. return patch;
  284. }
  285. if (!create_patch)
  286. return NULL;
  287. patch = kzalloc(sizeof(*patch), GFP_KERNEL);
  288. if (!patch)
  289. return NULL;
  290. patch->prog = prog;
  291. patch->bank = bank;
  292. patch->next = opl3->patch_table[key];
  293. opl3->patch_table[key] = patch;
  294. return patch;
  295. }
  296. EXPORT_SYMBOL(snd_opl3_find_patch);
  297. /*
  298. * Clear all patches of the given OPL3 instance
  299. */
  300. void snd_opl3_clear_patches(struct snd_opl3 *opl3)
  301. {
  302. int i;
  303. for (i = 0; i < OPL3_PATCH_HASH_SIZE; i++) {
  304. struct fm_patch *patch, *next;
  305. for (patch = opl3->patch_table[i]; patch; patch = next) {
  306. next = patch->next;
  307. kfree(patch);
  308. }
  309. }
  310. memset(opl3->patch_table, 0, sizeof(opl3->patch_table));
  311. }
  312. #endif /* OPL3_SUPPORT_SYNTH */
  313. /* ------------------------------ */
  314. void snd_opl3_reset(struct snd_opl3 * opl3)
  315. {
  316. unsigned short opl3_reg;
  317. unsigned short reg_side;
  318. unsigned char voice_offset;
  319. int max_voices, i;
  320. max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
  321. MAX_OPL2_VOICES : MAX_OPL3_VOICES;
  322. for (i = 0; i < max_voices; i++) {
  323. /* Get register array side and offset of voice */
  324. if (i < MAX_OPL2_VOICES) {
  325. /* Left register block for voices 0 .. 8 */
  326. reg_side = OPL3_LEFT;
  327. voice_offset = i;
  328. } else {
  329. /* Right register block for voices 9 .. 17 */
  330. reg_side = OPL3_RIGHT;
  331. voice_offset = i - MAX_OPL2_VOICES;
  332. }
  333. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
  334. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
  335. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
  336. opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
  337. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  338. opl3->command(opl3, opl3_reg, 0x00); /* Note off */
  339. }
  340. opl3->max_voices = MAX_OPL2_VOICES;
  341. opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
  342. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
  343. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
  344. opl3->rhythm = 0;
  345. }
  346. EXPORT_SYMBOL(snd_opl3_reset);
  347. static int snd_opl3_play_note(struct snd_opl3 * opl3, struct snd_dm_fm_note * note)
  348. {
  349. unsigned short reg_side;
  350. unsigned char voice_offset;
  351. unsigned short opl3_reg;
  352. unsigned char reg_val;
  353. /* Voices 0 - 8 in OPL2 mode */
  354. /* Voices 0 - 17 in OPL3 mode */
  355. if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  356. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  357. return -EINVAL;
  358. /* Get register array side and offset of voice */
  359. if (note->voice < MAX_OPL2_VOICES) {
  360. /* Left register block for voices 0 .. 8 */
  361. reg_side = OPL3_LEFT;
  362. voice_offset = note->voice;
  363. } else {
  364. /* Right register block for voices 9 .. 17 */
  365. reg_side = OPL3_RIGHT;
  366. voice_offset = note->voice - MAX_OPL2_VOICES;
  367. }
  368. /* Set lower 8 bits of note frequency */
  369. reg_val = (unsigned char) note->fnum;
  370. opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
  371. opl3->command(opl3, opl3_reg, reg_val);
  372. reg_val = 0x00;
  373. /* Set output sound flag */
  374. if (note->key_on)
  375. reg_val |= OPL3_KEYON_BIT;
  376. /* Set octave */
  377. reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
  378. /* Set higher 2 bits of note frequency */
  379. reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
  380. /* Set OPL3 KEYON_BLOCK register of requested voice */
  381. opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
  382. opl3->command(opl3, opl3_reg, reg_val);
  383. return 0;
  384. }
  385. static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * voice)
  386. {
  387. unsigned short reg_side;
  388. unsigned char op_offset;
  389. unsigned char voice_offset, voice_op;
  390. unsigned short opl3_reg;
  391. unsigned char reg_val;
  392. /* Only operators 1 and 2 */
  393. if (voice->op > 1)
  394. return -EINVAL;
  395. /* Voices 0 - 8 in OPL2 mode */
  396. /* Voices 0 - 17 in OPL3 mode */
  397. if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
  398. MAX_OPL3_VOICES : MAX_OPL2_VOICES))
  399. return -EINVAL;
  400. /* Get register array side and offset of voice */
  401. if (voice->voice < MAX_OPL2_VOICES) {
  402. /* Left register block for voices 0 .. 8 */
  403. reg_side = OPL3_LEFT;
  404. voice_offset = voice->voice;
  405. } else {
  406. /* Right register block for voices 9 .. 17 */
  407. reg_side = OPL3_RIGHT;
  408. voice_offset = voice->voice - MAX_OPL2_VOICES;
  409. }
  410. /* Get register offset of operator */
  411. voice_offset = array_index_nospec(voice_offset, MAX_OPL2_VOICES);
  412. voice_op = array_index_nospec(voice->op, 4);
  413. op_offset = snd_opl3_regmap[voice_offset][voice_op];
  414. reg_val = 0x00;
  415. /* Set amplitude modulation (tremolo) effect */
  416. if (voice->am)
  417. reg_val |= OPL3_TREMOLO_ON;
  418. /* Set vibrato effect */
  419. if (voice->vibrato)
  420. reg_val |= OPL3_VIBRATO_ON;
  421. /* Set sustaining sound phase */
  422. if (voice->do_sustain)
  423. reg_val |= OPL3_SUSTAIN_ON;
  424. /* Set keyboard scaling bit */
  425. if (voice->kbd_scale)
  426. reg_val |= OPL3_KSR;
  427. /* Set harmonic or frequency multiplier */
  428. reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
  429. /* Set OPL3 AM_VIB register of requested voice/operator */
  430. opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
  431. opl3->command(opl3, opl3_reg, reg_val);
  432. /* Set decreasing volume of higher notes */
  433. reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
  434. /* Set output volume */
  435. reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
  436. /* Set OPL3 KSL_LEVEL register of requested voice/operator */
  437. opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
  438. opl3->command(opl3, opl3_reg, reg_val);
  439. /* Set attack phase level */
  440. reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
  441. /* Set decay phase level */
  442. reg_val |= voice->decay & OPL3_DECAY_MASK;
  443. /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
  444. opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
  445. opl3->command(opl3, opl3_reg, reg_val);
  446. /* Set sustain phase level */
  447. reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
  448. /* Set release phase level */
  449. reg_val |= voice->release & OPL3_RELEASE_MASK;
  450. /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
  451. opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
  452. opl3->command(opl3, opl3_reg, reg_val);
  453. /* Set inter-operator feedback */
  454. reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
  455. /* Set inter-operator connection */
  456. if (voice->connection)
  457. reg_val |= OPL3_CONNECTION_BIT;
  458. /* OPL-3 only */
  459. if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
  460. if (voice->left)
  461. reg_val |= OPL3_VOICE_TO_LEFT;
  462. if (voice->right)
  463. reg_val |= OPL3_VOICE_TO_RIGHT;
  464. }
  465. /* Feedback/connection bits are applicable to voice */
  466. opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
  467. opl3->command(opl3, opl3_reg, reg_val);
  468. /* Select waveform */
  469. reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
  470. opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
  471. opl3->command(opl3, opl3_reg, reg_val);
  472. return 0;
  473. }
  474. static int snd_opl3_set_params(struct snd_opl3 * opl3, struct snd_dm_fm_params * params)
  475. {
  476. unsigned char reg_val;
  477. reg_val = 0x00;
  478. /* Set keyboard split method */
  479. if (params->kbd_split)
  480. reg_val |= OPL3_KEYBOARD_SPLIT;
  481. opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
  482. reg_val = 0x00;
  483. /* Set amplitude modulation (tremolo) depth */
  484. if (params->am_depth)
  485. reg_val |= OPL3_TREMOLO_DEPTH;
  486. /* Set vibrato depth */
  487. if (params->vib_depth)
  488. reg_val |= OPL3_VIBRATO_DEPTH;
  489. /* Set percussion mode */
  490. if (params->rhythm) {
  491. reg_val |= OPL3_PERCUSSION_ENABLE;
  492. opl3->rhythm = 1;
  493. } else {
  494. opl3->rhythm = 0;
  495. }
  496. /* Play percussion instruments */
  497. if (params->bass)
  498. reg_val |= OPL3_BASSDRUM_ON;
  499. if (params->snare)
  500. reg_val |= OPL3_SNAREDRUM_ON;
  501. if (params->tomtom)
  502. reg_val |= OPL3_TOMTOM_ON;
  503. if (params->cymbal)
  504. reg_val |= OPL3_CYMBAL_ON;
  505. if (params->hihat)
  506. reg_val |= OPL3_HIHAT_ON;
  507. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
  508. return 0;
  509. }
  510. static int snd_opl3_set_mode(struct snd_opl3 * opl3, int mode)
  511. {
  512. if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
  513. return -EINVAL;
  514. opl3->fm_mode = mode;
  515. if (opl3->hardware >= OPL3_HW_OPL3)
  516. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
  517. return 0;
  518. }
  519. static int snd_opl3_set_connection(struct snd_opl3 * opl3, int connection)
  520. {
  521. unsigned char reg_val;
  522. /* OPL-3 only */
  523. if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
  524. return -EINVAL;
  525. reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
  526. OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
  527. /* Set 4-op connections */
  528. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
  529. return 0;
  530. }