emux_oss.c 12 KB

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