emux_oss.c 11 KB

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