emux_oss.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Interface for OSS sequencer emulation
  3. *
  4. * Copyright (C) 1999 Takashi Iwai <tiwai@suse.de>
  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. * Changes
  21. * 19990227 Steve Ratcliffe Made separate file and merged in latest
  22. * midi emulation.
  23. */
  24. #include <sound/driver.h>
  25. #ifdef CONFIG_SND_SEQUENCER_OSS
  26. #include <asm/uaccess.h>
  27. #include <sound/core.h>
  28. #include "emux_voice.h"
  29. #include <sound/asoundef.h>
  30. static int snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure);
  31. static int snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg);
  32. static int snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd,
  33. unsigned long ioarg);
  34. static int snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  35. const char __user *buf, int offs, int count);
  36. static int snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg);
  37. static int snd_emux_event_oss_input(struct snd_seq_event *ev, int direct,
  38. void *private, int atomic, int hop);
  39. static void reset_port_mode(struct snd_emux_port *port, int midi_mode);
  40. static void emuspec_control(struct snd_emux *emu, struct snd_emux_port *port,
  41. int cmd, unsigned char *event, int atomic, int hop);
  42. static void gusspec_control(struct snd_emux *emu, struct snd_emux_port *port,
  43. int cmd, unsigned char *event, int atomic, int hop);
  44. static void fake_event(struct snd_emux *emu, struct snd_emux_port *port,
  45. int ch, int param, int val, int atomic, int hop);
  46. /* operators */
  47. static struct snd_seq_oss_callback oss_callback = {
  48. .owner = THIS_MODULE,
  49. .open = snd_emux_open_seq_oss,
  50. .close = snd_emux_close_seq_oss,
  51. .ioctl = snd_emux_ioctl_seq_oss,
  52. .load_patch = snd_emux_load_patch_seq_oss,
  53. .reset = snd_emux_reset_seq_oss,
  54. };
  55. /*
  56. * register OSS synth
  57. */
  58. void
  59. snd_emux_init_seq_oss(struct snd_emux *emu)
  60. {
  61. struct snd_seq_oss_reg *arg;
  62. struct snd_seq_device *dev;
  63. if (snd_seq_device_new(emu->card, 0, SNDRV_SEQ_DEV_ID_OSS,
  64. sizeof(struct snd_seq_oss_reg), &dev) < 0)
  65. return;
  66. emu->oss_synth = dev;
  67. strcpy(dev->name, emu->name);
  68. arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
  69. arg->type = SYNTH_TYPE_SAMPLE;
  70. arg->subtype = SAMPLE_TYPE_AWE32;
  71. arg->nvoices = emu->max_voices;
  72. arg->oper = oss_callback;
  73. arg->private_data = emu;
  74. /* register to OSS synth table */
  75. snd_device_register(emu->card, dev);
  76. }
  77. /*
  78. * unregister
  79. */
  80. void
  81. snd_emux_detach_seq_oss(struct snd_emux *emu)
  82. {
  83. if (emu->oss_synth) {
  84. snd_device_free(emu->card, emu->oss_synth);
  85. emu->oss_synth = NULL;
  86. }
  87. }
  88. /* use port number as a unique soundfont client number */
  89. #define SF_CLIENT_NO(p) ((p) + 0x1000)
  90. /*
  91. * open port for OSS sequencer
  92. */
  93. static int
  94. snd_emux_open_seq_oss(struct snd_seq_oss_arg *arg, void *closure)
  95. {
  96. struct snd_emux *emu;
  97. struct snd_emux_port *p;
  98. struct snd_seq_port_callback callback;
  99. char tmpname[64];
  100. emu = closure;
  101. snd_assert(arg != NULL && emu != NULL, return -ENXIO);
  102. mutex_lock(&emu->register_mutex);
  103. if (!snd_emux_inc_count(emu)) {
  104. mutex_unlock(&emu->register_mutex);
  105. return -EFAULT;
  106. }
  107. memset(&callback, 0, sizeof(callback));
  108. callback.owner = THIS_MODULE;
  109. callback.event_input = snd_emux_event_oss_input;
  110. sprintf(tmpname, "%s OSS Port", emu->name);
  111. p = snd_emux_create_port(emu, tmpname, 32,
  112. 1, &callback);
  113. if (p == NULL) {
  114. snd_printk("can't create port\n");
  115. snd_emux_dec_count(emu);
  116. mutex_unlock(&emu->register_mutex);
  117. return -ENOMEM;
  118. }
  119. /* fill the argument data */
  120. arg->private_data = p;
  121. arg->addr.client = p->chset.client;
  122. arg->addr.port = p->chset.port;
  123. p->oss_arg = arg;
  124. reset_port_mode(p, arg->seq_mode);
  125. snd_emux_reset_port(p);
  126. mutex_unlock(&emu->register_mutex);
  127. return 0;
  128. }
  129. #define DEFAULT_DRUM_FLAGS ((1<<9) | (1<<25))
  130. /*
  131. * reset port mode
  132. */
  133. static void
  134. reset_port_mode(struct snd_emux_port *port, int midi_mode)
  135. {
  136. if (midi_mode) {
  137. port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_MIDI;
  138. port->drum_flags = DEFAULT_DRUM_FLAGS;
  139. port->volume_atten = 0;
  140. port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_KEYPRESS;
  141. } else {
  142. port->port_mode = SNDRV_EMUX_PORT_MODE_OSS_SYNTH;
  143. port->drum_flags = 0;
  144. port->volume_atten = 32;
  145. port->oss_arg->event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
  146. }
  147. }
  148. /*
  149. * close port
  150. */
  151. static int
  152. snd_emux_close_seq_oss(struct snd_seq_oss_arg *arg)
  153. {
  154. struct snd_emux *emu;
  155. struct snd_emux_port *p;
  156. snd_assert(arg != NULL, return -ENXIO);
  157. p = arg->private_data;
  158. snd_assert(p != NULL, return -ENXIO);
  159. emu = p->emu;
  160. snd_assert(emu != NULL, return -ENXIO);
  161. mutex_lock(&emu->register_mutex);
  162. snd_emux_sounds_off_all(p);
  163. snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port));
  164. snd_seq_event_port_detach(p->chset.client, p->chset.port);
  165. snd_emux_dec_count(emu);
  166. mutex_unlock(&emu->register_mutex);
  167. return 0;
  168. }
  169. /*
  170. * load patch
  171. */
  172. static int
  173. snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  174. const char __user *buf, int offs, int count)
  175. {
  176. struct snd_emux *emu;
  177. struct snd_emux_port *p;
  178. int rc;
  179. snd_assert(arg != NULL, return -ENXIO);
  180. p = arg->private_data;
  181. snd_assert(p != NULL, return -ENXIO);
  182. emu = p->emu;
  183. snd_assert(emu != NULL, return -ENXIO);
  184. if (format == GUS_PATCH)
  185. rc = snd_soundfont_load_guspatch(emu->sflist, buf, count,
  186. SF_CLIENT_NO(p->chset.port));
  187. else if (format == SNDRV_OSS_SOUNDFONT_PATCH) {
  188. struct soundfont_patch_info patch;
  189. if (count < (int)sizeof(patch))
  190. rc = -EINVAL;
  191. if (copy_from_user(&patch, buf, sizeof(patch)))
  192. rc = -EFAULT;
  193. if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
  194. patch.type <= SNDRV_SFNT_PROBE_DATA)
  195. rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port));
  196. else {
  197. if (emu->ops.load_fx)
  198. rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count);
  199. else
  200. rc = -EINVAL;
  201. }
  202. } else
  203. rc = 0;
  204. return rc;
  205. }
  206. /*
  207. * ioctl
  208. */
  209. static int
  210. snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg)
  211. {
  212. struct snd_emux_port *p;
  213. struct snd_emux *emu;
  214. snd_assert(arg != NULL, return -ENXIO);
  215. p = arg->private_data;
  216. snd_assert(p != NULL, return -ENXIO);
  217. emu = p->emu;
  218. snd_assert(emu != NULL, return -ENXIO);
  219. switch (cmd) {
  220. case SNDCTL_SEQ_RESETSAMPLES:
  221. snd_soundfont_remove_samples(emu->sflist);
  222. return 0;
  223. case SNDCTL_SYNTH_MEMAVL:
  224. if (emu->memhdr)
  225. return snd_util_mem_avail(emu->memhdr);
  226. return 0;
  227. }
  228. return 0;
  229. }
  230. /*
  231. * reset device
  232. */
  233. static int
  234. snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg)
  235. {
  236. struct snd_emux_port *p;
  237. snd_assert(arg != NULL, return -ENXIO);
  238. p = arg->private_data;
  239. snd_assert(p != NULL, return -ENXIO);
  240. snd_emux_reset_port(p);
  241. return 0;
  242. }
  243. /*
  244. * receive raw events: only SEQ_PRIVATE is accepted.
  245. */
  246. static int
  247. snd_emux_event_oss_input(struct snd_seq_event *ev, int direct, void *private_data,
  248. int atomic, int hop)
  249. {
  250. struct snd_emux *emu;
  251. struct snd_emux_port *p;
  252. unsigned char cmd, *data;
  253. p = private_data;
  254. snd_assert(p != NULL, return -EINVAL);
  255. emu = p->emu;
  256. snd_assert(emu != NULL, return -EINVAL);
  257. if (ev->type != SNDRV_SEQ_EVENT_OSS)
  258. return snd_emux_event_input(ev, direct, private_data, atomic, hop);
  259. data = ev->data.raw8.d;
  260. /* only SEQ_PRIVATE is accepted */
  261. if (data[0] != 0xfe)
  262. return 0;
  263. cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK;
  264. if (data[2] & _EMUX_OSS_MODE_FLAG)
  265. emuspec_control(emu, p, cmd, data, atomic, hop);
  266. else
  267. gusspec_control(emu, p, cmd, data, atomic, hop);
  268. return 0;
  269. }
  270. /*
  271. * OSS/AWE driver specific h/w controls
  272. */
  273. static void
  274. emuspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
  275. unsigned char *event, int atomic, int hop)
  276. {
  277. int voice;
  278. unsigned short p1;
  279. short p2;
  280. int i;
  281. struct snd_midi_channel *chan;
  282. voice = event[3];
  283. if (voice < 0 || voice >= port->chset.max_channels)
  284. chan = NULL;
  285. else
  286. chan = &port->chset.channels[voice];
  287. p1 = *(unsigned short *) &event[4];
  288. p2 = *(short *) &event[6];
  289. switch (cmd) {
  290. #if 0 /* don't do this atomically */
  291. case _EMUX_OSS_REMOVE_LAST_SAMPLES:
  292. snd_soundfont_remove_unlocked(emu->sflist);
  293. break;
  294. #endif
  295. case _EMUX_OSS_SEND_EFFECT:
  296. if (chan)
  297. snd_emux_send_effect_oss(port, chan, p1, p2);
  298. break;
  299. case _EMUX_OSS_TERMINATE_ALL:
  300. snd_emux_terminate_all(emu);
  301. break;
  302. case _EMUX_OSS_TERMINATE_CHANNEL:
  303. /*snd_emux_mute_channel(emu, chan);*/
  304. break;
  305. case _EMUX_OSS_RESET_CHANNEL:
  306. /*snd_emux_channel_init(chset, chan);*/
  307. break;
  308. case _EMUX_OSS_RELEASE_ALL:
  309. fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop);
  310. break;
  311. case _EMUX_OSS_NOTEOFF_ALL:
  312. fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop);
  313. break;
  314. case _EMUX_OSS_INITIAL_VOLUME:
  315. if (p2) {
  316. port->volume_atten = (short)p1;
  317. snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
  318. }
  319. break;
  320. case _EMUX_OSS_CHN_PRESSURE:
  321. if (chan) {
  322. chan->midi_pressure = p1;
  323. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2);
  324. }
  325. break;
  326. case _EMUX_OSS_CHANNEL_MODE:
  327. reset_port_mode(port, p1);
  328. snd_emux_reset_port(port);
  329. break;
  330. case _EMUX_OSS_DRUM_CHANNELS:
  331. port->drum_flags = *(unsigned int*)&event[4];
  332. for (i = 0; i < port->chset.max_channels; i++) {
  333. chan = &port->chset.channels[i];
  334. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  335. }
  336. break;
  337. case _EMUX_OSS_MISC_MODE:
  338. if (p1 < EMUX_MD_END)
  339. port->ctrls[p1] = p2;
  340. break;
  341. case _EMUX_OSS_DEBUG_MODE:
  342. break;
  343. default:
  344. if (emu->ops.oss_ioctl)
  345. emu->ops.oss_ioctl(emu, cmd, p1, p2);
  346. break;
  347. }
  348. }
  349. /*
  350. * GUS specific h/w controls
  351. */
  352. #include <linux/ultrasound.h>
  353. static void
  354. gusspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
  355. unsigned char *event, int atomic, int hop)
  356. {
  357. int voice;
  358. unsigned short p1;
  359. short p2;
  360. int plong;
  361. struct snd_midi_channel *chan;
  362. if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH)
  363. return;
  364. if (cmd == _GUS_NUMVOICES)
  365. return;
  366. voice = event[3];
  367. if (voice < 0 || voice >= port->chset.max_channels)
  368. return;
  369. chan = &port->chset.channels[voice];
  370. p1 = *(unsigned short *) &event[4];
  371. p2 = *(short *) &event[6];
  372. plong = *(int*) &event[4];
  373. switch (cmd) {
  374. case _GUS_VOICESAMPLE:
  375. chan->midi_program = p1;
  376. return;
  377. case _GUS_VOICEBALA:
  378. /* 0 to 15 --> 0 to 127 */
  379. chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3;
  380. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
  381. return;
  382. case _GUS_VOICEVOL:
  383. case _GUS_VOICEVOL2:
  384. /* not supported yet */
  385. return;
  386. case _GUS_RAMPRANGE:
  387. case _GUS_RAMPRATE:
  388. case _GUS_RAMPMODE:
  389. case _GUS_RAMPON:
  390. case _GUS_RAMPOFF:
  391. /* volume ramping not supported */
  392. return;
  393. case _GUS_VOLUME_SCALE:
  394. return;
  395. case _GUS_VOICE_POS:
  396. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  397. snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START,
  398. (short)(plong & 0x7fff),
  399. EMUX_FX_FLAG_SET);
  400. snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START,
  401. (plong >> 15) & 0xffff,
  402. EMUX_FX_FLAG_SET);
  403. #endif
  404. return;
  405. }
  406. }
  407. /*
  408. * send an event to midi emulation
  409. */
  410. static void
  411. fake_event(struct snd_emux *emu, struct snd_emux_port *port, int ch, int param, int val, int atomic, int hop)
  412. {
  413. struct snd_seq_event ev;
  414. memset(&ev, 0, sizeof(ev));
  415. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  416. ev.data.control.channel = ch;
  417. ev.data.control.param = param;
  418. ev.data.control.value = val;
  419. snd_emux_event_input(&ev, 0, port, atomic, hop);
  420. }
  421. #endif /* CONFIG_SND_SEQUENCER_OSS */