emux_oss.c 12 KB

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