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