emux_oss.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. if (snd_BUG_ON(!arg || !emu))
  101. 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(KERN_ERR "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. if (snd_BUG_ON(!arg))
  157. return -ENXIO;
  158. p = arg->private_data;
  159. if (snd_BUG_ON(!p))
  160. return -ENXIO;
  161. emu = p->emu;
  162. if (snd_BUG_ON(!emu))
  163. return -ENXIO;
  164. mutex_lock(&emu->register_mutex);
  165. snd_emux_sounds_off_all(p);
  166. snd_soundfont_close_check(emu->sflist, SF_CLIENT_NO(p->chset.port));
  167. snd_seq_event_port_detach(p->chset.client, p->chset.port);
  168. snd_emux_dec_count(emu);
  169. mutex_unlock(&emu->register_mutex);
  170. return 0;
  171. }
  172. /*
  173. * load patch
  174. */
  175. static int
  176. snd_emux_load_patch_seq_oss(struct snd_seq_oss_arg *arg, int format,
  177. const char __user *buf, int offs, int count)
  178. {
  179. struct snd_emux *emu;
  180. struct snd_emux_port *p;
  181. int rc;
  182. if (snd_BUG_ON(!arg))
  183. return -ENXIO;
  184. p = arg->private_data;
  185. if (snd_BUG_ON(!p))
  186. return -ENXIO;
  187. emu = p->emu;
  188. if (snd_BUG_ON(!emu))
  189. return -ENXIO;
  190. if (format == GUS_PATCH)
  191. rc = snd_soundfont_load_guspatch(emu->sflist, buf, count,
  192. SF_CLIENT_NO(p->chset.port));
  193. else if (format == SNDRV_OSS_SOUNDFONT_PATCH) {
  194. struct soundfont_patch_info patch;
  195. if (count < (int)sizeof(patch))
  196. rc = -EINVAL;
  197. if (copy_from_user(&patch, buf, sizeof(patch)))
  198. rc = -EFAULT;
  199. if (patch.type >= SNDRV_SFNT_LOAD_INFO &&
  200. patch.type <= SNDRV_SFNT_PROBE_DATA)
  201. rc = snd_soundfont_load(emu->sflist, buf, count, SF_CLIENT_NO(p->chset.port));
  202. else {
  203. if (emu->ops.load_fx)
  204. rc = emu->ops.load_fx(emu, patch.type, patch.optarg, buf, count);
  205. else
  206. rc = -EINVAL;
  207. }
  208. } else
  209. rc = 0;
  210. return rc;
  211. }
  212. /*
  213. * ioctl
  214. */
  215. static int
  216. snd_emux_ioctl_seq_oss(struct snd_seq_oss_arg *arg, unsigned int cmd, unsigned long ioarg)
  217. {
  218. struct snd_emux_port *p;
  219. struct snd_emux *emu;
  220. if (snd_BUG_ON(!arg))
  221. return -ENXIO;
  222. p = arg->private_data;
  223. if (snd_BUG_ON(!p))
  224. return -ENXIO;
  225. emu = p->emu;
  226. if (snd_BUG_ON(!emu))
  227. return -ENXIO;
  228. switch (cmd) {
  229. case SNDCTL_SEQ_RESETSAMPLES:
  230. snd_soundfont_remove_samples(emu->sflist);
  231. return 0;
  232. case SNDCTL_SYNTH_MEMAVL:
  233. if (emu->memhdr)
  234. return snd_util_mem_avail(emu->memhdr);
  235. return 0;
  236. }
  237. return 0;
  238. }
  239. /*
  240. * reset device
  241. */
  242. static int
  243. snd_emux_reset_seq_oss(struct snd_seq_oss_arg *arg)
  244. {
  245. struct snd_emux_port *p;
  246. if (snd_BUG_ON(!arg))
  247. return -ENXIO;
  248. p = arg->private_data;
  249. if (snd_BUG_ON(!p))
  250. return -ENXIO;
  251. snd_emux_reset_port(p);
  252. return 0;
  253. }
  254. /*
  255. * receive raw events: only SEQ_PRIVATE is accepted.
  256. */
  257. static int
  258. snd_emux_event_oss_input(struct snd_seq_event *ev, int direct, void *private_data,
  259. int atomic, int hop)
  260. {
  261. struct snd_emux *emu;
  262. struct snd_emux_port *p;
  263. unsigned char cmd, *data;
  264. p = private_data;
  265. if (snd_BUG_ON(!p))
  266. return -EINVAL;
  267. emu = p->emu;
  268. if (snd_BUG_ON(!emu))
  269. return -EINVAL;
  270. if (ev->type != SNDRV_SEQ_EVENT_OSS)
  271. return snd_emux_event_input(ev, direct, private_data, atomic, hop);
  272. data = ev->data.raw8.d;
  273. /* only SEQ_PRIVATE is accepted */
  274. if (data[0] != 0xfe)
  275. return 0;
  276. cmd = data[2] & _EMUX_OSS_MODE_VALUE_MASK;
  277. if (data[2] & _EMUX_OSS_MODE_FLAG)
  278. emuspec_control(emu, p, cmd, data, atomic, hop);
  279. else
  280. gusspec_control(emu, p, cmd, data, atomic, hop);
  281. return 0;
  282. }
  283. /*
  284. * OSS/AWE driver specific h/w controls
  285. */
  286. static void
  287. emuspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
  288. unsigned char *event, int atomic, int hop)
  289. {
  290. int voice;
  291. unsigned short p1;
  292. short p2;
  293. int i;
  294. struct snd_midi_channel *chan;
  295. voice = event[3];
  296. if (voice < 0 || voice >= port->chset.max_channels)
  297. chan = NULL;
  298. else
  299. chan = &port->chset.channels[voice];
  300. p1 = *(unsigned short *) &event[4];
  301. p2 = *(short *) &event[6];
  302. switch (cmd) {
  303. #if 0 /* don't do this atomically */
  304. case _EMUX_OSS_REMOVE_LAST_SAMPLES:
  305. snd_soundfont_remove_unlocked(emu->sflist);
  306. break;
  307. #endif
  308. case _EMUX_OSS_SEND_EFFECT:
  309. if (chan)
  310. snd_emux_send_effect_oss(port, chan, p1, p2);
  311. break;
  312. case _EMUX_OSS_TERMINATE_ALL:
  313. snd_emux_terminate_all(emu);
  314. break;
  315. case _EMUX_OSS_TERMINATE_CHANNEL:
  316. /*snd_emux_mute_channel(emu, chan);*/
  317. break;
  318. case _EMUX_OSS_RESET_CHANNEL:
  319. /*snd_emux_channel_init(chset, chan);*/
  320. break;
  321. case _EMUX_OSS_RELEASE_ALL:
  322. fake_event(emu, port, voice, MIDI_CTL_ALL_NOTES_OFF, 0, atomic, hop);
  323. break;
  324. case _EMUX_OSS_NOTEOFF_ALL:
  325. fake_event(emu, port, voice, MIDI_CTL_ALL_SOUNDS_OFF, 0, atomic, hop);
  326. break;
  327. case _EMUX_OSS_INITIAL_VOLUME:
  328. if (p2) {
  329. port->volume_atten = (short)p1;
  330. snd_emux_update_port(port, SNDRV_EMUX_UPDATE_VOLUME);
  331. }
  332. break;
  333. case _EMUX_OSS_CHN_PRESSURE:
  334. if (chan) {
  335. chan->midi_pressure = p1;
  336. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_FMMOD|SNDRV_EMUX_UPDATE_FM2FRQ2);
  337. }
  338. break;
  339. case _EMUX_OSS_CHANNEL_MODE:
  340. reset_port_mode(port, p1);
  341. snd_emux_reset_port(port);
  342. break;
  343. case _EMUX_OSS_DRUM_CHANNELS:
  344. port->drum_flags = *(unsigned int*)&event[4];
  345. for (i = 0; i < port->chset.max_channels; i++) {
  346. chan = &port->chset.channels[i];
  347. chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
  348. }
  349. break;
  350. case _EMUX_OSS_MISC_MODE:
  351. if (p1 < EMUX_MD_END)
  352. port->ctrls[p1] = p2;
  353. break;
  354. case _EMUX_OSS_DEBUG_MODE:
  355. break;
  356. default:
  357. if (emu->ops.oss_ioctl)
  358. emu->ops.oss_ioctl(emu, cmd, p1, p2);
  359. break;
  360. }
  361. }
  362. /*
  363. * GUS specific h/w controls
  364. */
  365. #include <linux/ultrasound.h>
  366. static void
  367. gusspec_control(struct snd_emux *emu, struct snd_emux_port *port, int cmd,
  368. unsigned char *event, int atomic, int hop)
  369. {
  370. int voice;
  371. unsigned short p1;
  372. short p2;
  373. int plong;
  374. struct snd_midi_channel *chan;
  375. if (port->port_mode != SNDRV_EMUX_PORT_MODE_OSS_SYNTH)
  376. return;
  377. if (cmd == _GUS_NUMVOICES)
  378. return;
  379. voice = event[3];
  380. if (voice < 0 || voice >= port->chset.max_channels)
  381. return;
  382. chan = &port->chset.channels[voice];
  383. p1 = *(unsigned short *) &event[4];
  384. p2 = *(short *) &event[6];
  385. plong = *(int*) &event[4];
  386. switch (cmd) {
  387. case _GUS_VOICESAMPLE:
  388. chan->midi_program = p1;
  389. return;
  390. case _GUS_VOICEBALA:
  391. /* 0 to 15 --> 0 to 127 */
  392. chan->control[MIDI_CTL_MSB_PAN] = (int)p1 << 3;
  393. snd_emux_update_channel(port, chan, SNDRV_EMUX_UPDATE_PAN);
  394. return;
  395. case _GUS_VOICEVOL:
  396. case _GUS_VOICEVOL2:
  397. /* not supported yet */
  398. return;
  399. case _GUS_RAMPRANGE:
  400. case _GUS_RAMPRATE:
  401. case _GUS_RAMPMODE:
  402. case _GUS_RAMPON:
  403. case _GUS_RAMPOFF:
  404. /* volume ramping not supported */
  405. return;
  406. case _GUS_VOLUME_SCALE:
  407. return;
  408. case _GUS_VOICE_POS:
  409. #ifdef SNDRV_EMUX_USE_RAW_EFFECT
  410. snd_emux_send_effect(port, chan, EMUX_FX_SAMPLE_START,
  411. (short)(plong & 0x7fff),
  412. EMUX_FX_FLAG_SET);
  413. snd_emux_send_effect(port, chan, EMUX_FX_COARSE_SAMPLE_START,
  414. (plong >> 15) & 0xffff,
  415. EMUX_FX_FLAG_SET);
  416. #endif
  417. return;
  418. }
  419. }
  420. /*
  421. * send an event to midi emulation
  422. */
  423. static void
  424. fake_event(struct snd_emux *emu, struct snd_emux_port *port, int ch, int param, int val, int atomic, int hop)
  425. {
  426. struct snd_seq_event ev;
  427. memset(&ev, 0, sizeof(ev));
  428. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  429. ev.data.control.channel = ch;
  430. ev.data.control.param = param;
  431. ev.data.control.value = val;
  432. snd_emux_event_input(&ev, 0, port, atomic, hop);
  433. }
  434. #endif /* CONFIG_SND_SEQUENCER_OSS */