opl3_synth.c 16 KB

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