Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Interface for OSS sequencer emulation |
| 3 | * |
| 4 | * Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si> |
| 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 "opl3_voice.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 23 | static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure); |
| 24 | static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg); |
| 25 | static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg); |
| 26 | static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, const char __user *buf, int offs, int count); |
| 27 | static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | /* */ |
| 30 | |
| 31 | static inline mm_segment_t snd_enter_user(void) |
| 32 | { |
| 33 | mm_segment_t fs = get_fs(); |
| 34 | set_fs(get_ds()); |
| 35 | return fs; |
| 36 | } |
| 37 | |
| 38 | static inline void snd_leave_user(mm_segment_t fs) |
| 39 | { |
| 40 | set_fs(fs); |
| 41 | } |
| 42 | |
| 43 | /* operators */ |
| 44 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 45 | extern struct snd_midi_op opl3_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 47 | static struct snd_seq_oss_callback oss_callback = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | .owner = THIS_MODULE, |
| 49 | .open = snd_opl3_open_seq_oss, |
| 50 | .close = snd_opl3_close_seq_oss, |
| 51 | .ioctl = snd_opl3_ioctl_seq_oss, |
| 52 | .load_patch = snd_opl3_load_patch_seq_oss, |
| 53 | .reset = snd_opl3_reset_seq_oss, |
| 54 | }; |
| 55 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 56 | static int snd_opl3_oss_event_input(struct snd_seq_event *ev, int direct, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | void *private_data, int atomic, int hop) |
| 58 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 59 | struct snd_opl3 *opl3 = private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | if (ev->type != SNDRV_SEQ_EVENT_OSS) |
| 62 | snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset); |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* ------------------------------ */ |
| 67 | |
| 68 | static void snd_opl3_oss_free_port(void *private_data) |
| 69 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 70 | struct snd_opl3 *opl3 = private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | snd_midi_channel_free_set(opl3->oss_chset); |
| 73 | } |
| 74 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 75 | static int snd_opl3_oss_create_port(struct snd_opl3 * opl3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 77 | struct snd_seq_port_callback callbacks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | char name[32]; |
| 79 | int voices, opl_ver; |
| 80 | |
| 81 | voices = (opl3->hardware < OPL3_HW_OPL3) ? |
| 82 | MAX_OPL2_VOICES : MAX_OPL3_VOICES; |
| 83 | opl3->oss_chset = snd_midi_channel_alloc_set(voices); |
| 84 | if (opl3->oss_chset == NULL) |
| 85 | return -ENOMEM; |
| 86 | opl3->oss_chset->private_data = opl3; |
| 87 | |
| 88 | memset(&callbacks, 0, sizeof(callbacks)); |
| 89 | callbacks.owner = THIS_MODULE; |
| 90 | callbacks.event_input = snd_opl3_oss_event_input; |
| 91 | callbacks.private_free = snd_opl3_oss_free_port; |
| 92 | callbacks.private_data = opl3; |
| 93 | |
| 94 | opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8; |
| 95 | sprintf(name, "OPL%i OSS Port", opl_ver); |
| 96 | |
| 97 | opl3->oss_chset->client = opl3->seq_client; |
| 98 | opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks, |
| 99 | SNDRV_SEQ_PORT_CAP_WRITE, |
| 100 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | |
Clemens Ladisch | 450047a | 2006-05-02 16:08:41 +0200 | [diff] [blame] | 101 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | |
| 102 | SNDRV_SEQ_PORT_TYPE_HARDWARE | |
| 103 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | voices, voices, |
| 105 | name); |
| 106 | if (opl3->oss_chset->port < 0) { |
Dave Jones | 5e315e9 | 2006-03-06 14:03:37 +0100 | [diff] [blame] | 107 | int port; |
| 108 | port = opl3->oss_chset->port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | snd_midi_channel_free_set(opl3->oss_chset); |
Dave Jones | 5e315e9 | 2006-03-06 14:03:37 +0100 | [diff] [blame] | 110 | return port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* ------------------------------ */ |
| 116 | |
| 117 | /* register OSS synth */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 118 | void snd_opl3_init_seq_oss(struct snd_opl3 *opl3, char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 120 | struct snd_seq_oss_reg *arg; |
| 121 | struct snd_seq_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS, |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 124 | sizeof(struct snd_seq_oss_reg), &dev) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return; |
| 126 | |
| 127 | opl3->oss_seq_dev = dev; |
| 128 | strlcpy(dev->name, name, sizeof(dev->name)); |
| 129 | arg = SNDRV_SEQ_DEVICE_ARGPTR(dev); |
| 130 | arg->type = SYNTH_TYPE_FM; |
| 131 | if (opl3->hardware < OPL3_HW_OPL3) { |
| 132 | arg->subtype = FM_TYPE_ADLIB; |
| 133 | arg->nvoices = MAX_OPL2_VOICES; |
| 134 | } else { |
| 135 | arg->subtype = FM_TYPE_OPL3; |
| 136 | arg->nvoices = MAX_OPL3_VOICES; |
| 137 | } |
| 138 | arg->oper = oss_callback; |
| 139 | arg->private_data = opl3; |
| 140 | |
Dave Jones | 5e315e9 | 2006-03-06 14:03:37 +0100 | [diff] [blame] | 141 | if (snd_opl3_oss_create_port(opl3)) { |
| 142 | /* register to OSS synth table */ |
| 143 | snd_device_register(opl3->card, dev); |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* unregister */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 148 | void snd_opl3_free_seq_oss(struct snd_opl3 *opl3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
| 150 | if (opl3->oss_seq_dev) { |
Takashi Iwai | b1a3aa2 | 2006-02-24 13:03:52 -0800 | [diff] [blame] | 151 | /* The instance should have been released in prior */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | opl3->oss_seq_dev = NULL; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* ------------------------------ */ |
| 157 | |
| 158 | /* open OSS sequencer */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 159 | static int snd_opl3_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 161 | struct snd_opl3 *opl3 = closure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | int err; |
| 163 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 164 | if (snd_BUG_ON(!arg)) |
| 165 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | if ((err = snd_opl3_synth_setup(opl3)) < 0) |
| 168 | return err; |
| 169 | |
| 170 | /* fill the argument data */ |
| 171 | arg->private_data = opl3; |
| 172 | arg->addr.client = opl3->oss_chset->client; |
| 173 | arg->addr.port = opl3->oss_chset->port; |
| 174 | |
| 175 | if ((err = snd_opl3_synth_use_inc(opl3)) < 0) |
| 176 | return err; |
| 177 | |
| 178 | opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH; |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | /* close OSS sequencer */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 183 | static int snd_opl3_close_seq_oss(struct snd_seq_oss_arg *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 185 | struct snd_opl3 *opl3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 187 | if (snd_BUG_ON(!arg)) |
| 188 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | opl3 = arg->private_data; |
| 190 | |
| 191 | snd_opl3_synth_cleanup(opl3); |
| 192 | |
| 193 | snd_opl3_synth_use_dec(opl3); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /* load patch */ |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | /* from sound_config.h */ |
| 200 | #define SBFM_MAXINSTR 256 |
| 201 | |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 202 | static int snd_opl3_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | const char __user *buf, int offs, int count) |
| 204 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 205 | struct snd_opl3 *opl3; |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 206 | struct sbi_instrument sbi; |
| 207 | char name[32]; |
| 208 | int err, type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 210 | if (snd_BUG_ON(!arg)) |
| 211 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | opl3 = arg->private_data; |
| 213 | |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 214 | if (format == FM_PATCH) |
| 215 | type = FM_PATCH_OPL2; |
| 216 | else if (format == OPL3_PATCH) |
| 217 | type = FM_PATCH_OPL3; |
| 218 | else |
| 219 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 221 | if (count < (int)sizeof(sbi)) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 222 | snd_printk(KERN_ERR "FM Error: Patch record too short\n"); |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 223 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 225 | if (copy_from_user(&sbi, buf, sizeof(sbi))) |
| 226 | return -EFAULT; |
| 227 | |
| 228 | if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) { |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 229 | snd_printk(KERN_ERR "FM Error: Invalid instrument number %d\n", |
Takashi Iwai | 224a033 | 2007-10-30 11:49:22 +0100 | [diff] [blame] | 230 | sbi.channel); |
| 231 | return -EINVAL; |
| 232 | } |
| 233 | |
| 234 | memset(name, 0, sizeof(name)); |
| 235 | sprintf(name, "Chan%d", sbi.channel); |
| 236 | |
| 237 | err = snd_opl3_load_patch(opl3, sbi.channel, 127, type, name, NULL, |
| 238 | sbi.operators); |
| 239 | if (err < 0) |
| 240 | return err; |
| 241 | |
| 242 | return sizeof(sbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* ioctl */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 246 | static int snd_opl3_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | unsigned long ioarg) |
| 248 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 249 | struct snd_opl3 *opl3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 251 | if (snd_BUG_ON(!arg)) |
| 252 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | opl3 = arg->private_data; |
| 254 | switch (cmd) { |
| 255 | case SNDCTL_FM_LOAD_INSTR: |
Takashi Iwai | 4520383 | 2009-02-05 15:51:50 +0100 | [diff] [blame] | 256 | snd_printk(KERN_ERR "OPL3: " |
| 257 | "Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. " |
| 258 | "Fix the program.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -EINVAL; |
| 260 | |
| 261 | case SNDCTL_SYNTH_MEMAVL: |
| 262 | return 0x7fffffff; |
| 263 | |
| 264 | case SNDCTL_FM_4OP_ENABLE: |
| 265 | // handled automatically by OPL instrument type |
| 266 | return 0; |
| 267 | |
| 268 | default: |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | /* reset device */ |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 275 | static int snd_opl3_reset_seq_oss(struct snd_seq_oss_arg *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 277 | struct snd_opl3 *opl3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 279 | if (snd_BUG_ON(!arg)) |
| 280 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | opl3 = arg->private_data; |
| 282 | |
| 283 | return 0; |
| 284 | } |