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