2 * sound/386bsd/soundcard.c
4 * Soundcard driver for 386BSD.
6 * Copyright by Hannu Savolainen 1993
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 2.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include "opt_devfs.h"
31 #include <i386/isa/sound/sound_config.h>
33 #include <sys/devfsext.h>
36 #if NSND > 0 /* from "snd.h" */
41 #include <i386/isa/isa_device.h>
45 ** Register definitions for DMA controller 1 (channels 0..3):
47 #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
48 #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
49 #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
50 #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
53 ** Register definitions for DMA controller 2 (channels 4..7):
55 #define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */
56 #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
57 #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
58 #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
61 #define FIX_RETURN(ret) {if ((ret)<0) return -(ret); else return 0;}
63 static int soundcards_installed = 0; /* Number of installed soundcards */
64 static int soundcard_configured = 0;
66 static struct fileinfo files[SND_NDEVS];
67 struct selinfo selinfo[SND_NDEVS >> 4];
70 MIDIbuf_poll (int dev, struct fileinfo *file, int events, select_table * wait);
73 audio_poll(int dev, struct fileinfo * file, int events, select_table * wait);
76 sequencer_poll (int dev, struct fileinfo *file, int events, select_table * wait);
78 static int sndprobe __P((struct isa_device *));
79 static int sndattach __P((struct isa_device *));
80 static int sndmmap __P((dev_t dev, int offset, int nprot ));
82 static d_open_t sndopen;
83 static d_close_t sndclose;
84 static d_ioctl_t sndioctl;
85 static d_read_t sndread;
86 static d_write_t sndwrite;
87 static d_poll_t sndpoll;
89 static char driver_name[] = "snd";
92 static struct cdevsw snd_cdevsw = {
93 sndopen, sndclose, sndread, sndwrite,
94 sndioctl, nostop, noreset, nodevtotty,
95 sndpoll, sndmmap, nostrategy, driver_name,
102 static void sound_mem_init(void);
105 * for each "device XXX" entry in the config file, we have
106 * a struct isa_driver which is linked into isa_devtab_null[]
108 * XXX It is a bit stupid to call the generic routine so many times and
109 * switch then to the specific one, but the alternative way would be
110 * to replicate some code in the probe/attach routines.
113 struct isa_driver opldriver = {sndprobe, sndattach, "opl"};
114 struct isa_driver trixdriver = {sndprobe, sndattach, "trix"};
115 struct isa_driver trixsbdriver = {sndprobe, sndattach, "trixsb"};
116 struct isa_driver sbdriver = {sndprobe, sndattach, "sb"};
117 struct isa_driver sbxvidriver = {sndprobe, sndattach, "sbxvi"};
118 struct isa_driver sbmididriver = {sndprobe, sndattach, "sbmidi"};
119 struct isa_driver awedriver = {sndprobe, sndattach, "awe"};
120 struct isa_driver pasdriver = {sndprobe, sndattach, "pas"};
121 struct isa_driver mpudriver = {sndprobe, sndattach, "mpu"};
122 struct isa_driver gusdriver = {sndprobe, sndattach, "gus"};
123 struct isa_driver gusxvidriver = {sndprobe, sndattach, "gusxvi"};
124 struct isa_driver gusmaxdriver = {sndprobe, sndattach, "gusmax"};
125 struct isa_driver uartdriver = {sndprobe, sndattach, "uart"};
126 struct isa_driver mssdriver = {sndprobe, sndattach, "mss"};
127 struct isa_driver cssdriver = {sndprobe, sndattach, "css"};
128 struct isa_driver sscapedriver = {sndprobe, sndattach, "sscape"};
129 struct isa_driver sscape_mssdriver = {sndprobe, sndattach, "sscape_mss"};
131 short ipri_to_irq(u_short ipri);
133 static ointhand2_t sndintr;
138 struct timeval timecopy;
140 getmicrotime(&timecopy);
141 return timecopy.tv_usec / (1000000 / hz) +
142 (u_long) timecopy.tv_sec * hz;
146 sndmmap( dev_t dev, int offset, int nprot )
149 struct dma_buffparms * dmap;
151 dev = minor(dev) >> 4;
152 if (dev > 0 ) return (-1);
154 dmap = audio_devs[dev]->dmap_out;
156 if (nprot & PROT_EXEC)
158 dmap->mapping_flags |= DMA_MAP_MAPPED ;
159 return( i386_btop(vtophys(dmap->raw_buf) + offset) );
164 sndread(dev_t dev, struct uio * buf, int flag)
166 int count = buf->uio_resid;
169 FIX_RETURN(sound_read_sw(dev, &files[dev], buf, count));
174 sndwrite(dev_t dev, struct uio * buf, int flag)
176 int count = buf->uio_resid;
179 FIX_RETURN(sound_write_sw(dev, &files[dev], buf, count));
183 sndopen(dev_t dev, int flags, int mode, struct proc * p)
186 struct fileinfo tmp_file;
189 if (!soundcard_configured && dev) {
190 printf("SoundCard Error: soundcard system has not been configured\n");
195 if (flags & FREAD && flags & FWRITE)
196 tmp_file.mode = OPEN_READWRITE;
197 else if (flags & FREAD)
198 tmp_file.mode = OPEN_READ;
199 else if (flags & FWRITE)
200 tmp_file.mode = OPEN_WRITE;
202 selinfo[dev >> 4].si_pid = 0;
203 selinfo[dev >> 4].si_flags = 0;
204 if ((retval = sound_open_sw(dev, &tmp_file)) < 0)
207 bcopy((char *) &tmp_file, (char *) &files[dev], sizeof(tmp_file));
214 sndclose(dev_t dev, int flags, int mode, struct proc * p)
217 sound_release_sw(dev, &files[dev]);
223 sndioctl(dev_t dev, u_long cmd, caddr_t arg, int mode, struct proc * p)
226 FIX_RETURN(sound_ioctl_sw(dev, &files[dev], cmd, arg));
230 sndpoll(dev_t dev, int events, struct proc * p)
235 /* printf ("snd_select(dev=%d, rw=%d, pid=%d)\n", dev, rw, p->p_pid); */
237 switch (dev & 0x0f) {
238 #ifdef CONFIG_SEQUENCER
241 return sequencer_poll(dev, &files[dev], events, p);
247 return MIDIbuf_poll(dev, &files[dev], events, p);
256 return audio_poll(dev, &files[dev], events, p);
264 #endif /* ALLOW_POLL */
265 DEB(printf("sound_ioctl(dev=%d, cmd=0x%x, arg=0x%x)\n", dev, cmd, arg));
270 /* XXX this should become ffs(ipri), perhaps -1 lr 970705 */
272 ipri_to_irq(u_short ipri)
275 * Converts the ipri (bitmask) to the corresponding irq number
279 for (irq = 0; irq < 16; irq++)
280 if (ipri == (1 << irq))
283 return -1; /* Invalid argument */
287 driver_to_voxunit(struct isa_driver * driver)
290 * converts a sound driver pointer into the equivalent VoxWare device
293 if (driver == &opldriver)
294 return (SNDCARD_ADLIB);
295 else if (driver == &sbdriver)
297 else if (driver == &pasdriver)
298 return (SNDCARD_PAS);
299 else if (driver == &gusdriver)
300 return (SNDCARD_GUS);
301 else if (driver == &mpudriver)
302 return (SNDCARD_MPU401);
303 else if (driver == &sbxvidriver)
304 return (SNDCARD_SB16);
305 else if (driver == &sbmididriver)
306 return (SNDCARD_SB16MIDI);
307 else if(driver == &awedriver)
308 return(SNDCARD_AWE32);
309 else if (driver == &uartdriver)
310 return (SNDCARD_UART6850);
311 else if (driver == &gusdriver)
312 return (SNDCARD_GUS16);
313 else if (driver == &mssdriver)
314 return (SNDCARD_MSS);
315 else if (driver == &cssdriver)
316 return (SNDCARD_CS4232);
317 else if (driver == &sscapedriver)
318 return(SNDCARD_SSCAPE);
319 else if (driver == &sscape_mssdriver)
320 return(SNDCARD_SSCAPE_MSS);
321 else if (driver == &trixdriver)
322 return (SNDCARD_TRXPRO);
323 else if (driver == &trixsbdriver)
324 return (SNDCARD_TRXPRO_SB);
330 * very dirty: tmp_osp is allocated in sndprobe, and used at the next
334 static sound_os_info *temp_osp;
337 * sndprobe is called for each isa_device. From here, a voxware unit
338 * number is determined, and the appropriate probe routine is selected.
339 * The parameters from the config line are passed to the hw_config struct.
343 sndprobe(struct isa_device * dev)
345 struct address_info hw_config;
348 temp_osp = (sound_os_info *)malloc(sizeof(sound_os_info),
351 panic("SOUND: Cannot allocate memory\n");
354 * get config info from the kernel config. These may be overridden
355 * by the local autoconfiguration routines though (e.g. pnp stuff).
358 hw_config.io_base = dev->id_iobase;
359 hw_config.irq = ipri_to_irq(dev->id_irq);
360 hw_config.dma = dev->id_drq;
363 * misuse the flags field for read dma. Note that, to use 0 as
364 * read dma channel, one of the high bits should be set. lr970705 XXX
367 if (dev->id_flags != 0)
368 hw_config.dma2 = dev->id_flags & 0x7;
372 hw_config.always_detect = 0;
373 hw_config.name = NULL;
374 hw_config.card_subtype = 0;
376 temp_osp->unit = dev->id_unit;
377 hw_config.osp = temp_osp;
378 unit = driver_to_voxunit(dev->id_driver);
380 if (sndtable_probe(unit, &hw_config)) {
381 dev->id_iobase = hw_config.io_base;
382 dev->id_irq = hw_config.irq == -1 ? 0 : (1 << hw_config.irq);
383 dev->id_drq = hw_config.dma;
385 if (hw_config.dma != hw_config.dma2 && ( hw_config.dma2 != -1))
386 dev->id_flags = hw_config.dma2 | 0x100; /* XXX lr */
395 sndattach(struct isa_device * dev)
398 static int midi_initialized = 0;
399 static int seq_initialized = 0;
400 struct address_info hw_config;
404 /* XXX this is probably incomplete. */
405 dname = dev->id_driver->name;
406 if (strcmp(dname, "css") == 0 || strcmp(dname, "gusxvi") == 0 ||
407 strcmp(dname, "mss") == 0)
408 dev->id_ointr = adintr;
409 if (strcmp(dname, "gus") == 0)
410 dev->id_ointr = gusintr;
411 if (strcmp(dname, "pas") == 0)
412 dev->id_ointr = pasintr;
413 if (strcmp(dname, "sb") == 0)
414 dev->id_ointr = sbintr;
415 if (strcmp(dname, "sscape_mss") == 0)
416 dev->id_ointr = sndintr;
417 if (strcmp(dname, "sscape") == 0 || strcmp(dname, "trix") == 0)
418 dev->id_ointr = sscapeintr;
419 if (strcmp(dname, "uart0") == 0)
420 dev->id_ointr = m6850intr;
422 unit = driver_to_voxunit(dev->id_driver);
423 hw_config.io_base = dev->id_iobase;
424 hw_config.irq = ipri_to_irq(dev->id_irq);
425 hw_config.dma = dev->id_drq;
427 /* misuse the flags field for read dma */
428 if (dev->id_flags != 0)
429 hw_config.dma2 = dev->id_flags & 0x7;
433 hw_config.card_subtype = 0;
434 hw_config.osp = temp_osp;
439 if (!(sndtable_init_card(unit, &hw_config))) { /* init card */
440 printf(" <Driver not configured>");
444 * Init the high level sound driver
447 if (!(soundcards_installed = sndtable_get_cardcount())) {
448 DDB(printf("No drivers actually installed\n"));
449 return FALSE; /* No cards detected */
454 if (num_audiodevs) { /* Audio devices present */
458 soundcard_configured = 1;
461 if (num_midis && !midi_initialized)
462 midi_initialized = 1;
464 if ((num_midis + num_synths) && !seq_initialized) {
472 dev = makedev(CDEV_MAJOR, 0);
473 cdevsw_add(&dev, &snd_cdevsw, NULL);
476 #define GID_SND GID_GAMES
477 #define UID_SND UID_ROOT
478 #define PERM_SND 0660
480 * make links to first successfully probed device, don't do it if
481 * duplicate creation of same node failed (ie. bad cookie returned)
483 if (dev->id_driver == &opldriver){
484 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_SEQ,
485 DV_CHR, UID_SND, GID_SND, PERM_SND,
486 "sequencer%r", dev->id_unit);
487 if (tmp) devfs_link(tmp, "sequencer");
488 } else if (dev->id_driver == &mpudriver ||
489 dev->id_driver == &sbmididriver ||
490 dev->id_driver == &uartdriver){
491 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_MIDIN,
492 DV_CHR, UID_SND, GID_SND, PERM_SND,
493 "midi%r", dev->id_unit);
494 if (tmp) devfs_link(tmp, "midi");
496 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_DSP,
497 DV_CHR, UID_SND, GID_SND, PERM_SND,
498 "dsp%r", dev->id_unit);
499 if (tmp) devfs_link(tmp, "dsp");
500 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_DSP16,
501 DV_CHR, UID_SND, GID_SND, PERM_SND,
502 "dspW%r", dev->id_unit);
503 if (tmp) devfs_link(tmp, "dspW");
504 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_AUDIO,
505 DV_CHR, UID_SND, GID_SND, PERM_SND,
506 "audio%r", dev->id_unit);
507 if (tmp) devfs_link(tmp, "audio");
508 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_CTL,
509 DV_CHR, UID_SND, GID_SND, PERM_SND,
510 "mixer%r", dev->id_unit);
511 if (tmp) devfs_link(tmp, "mixer");
512 tmp = devfs_add_devswf(&snd_cdevsw, (dev->id_unit << 4) | SND_DEV_STATUS,
513 DV_CHR, UID_SND, GID_SND, PERM_SND,
514 "sndstat%r", dev->id_unit);
515 if (tmp) devfs_link(tmp, "sndstat");
525 alloc_dmap(int dev, int chan, struct dma_buffparms * dmap)
530 tmpbuf = contigmalloc(audio_devs[dev]->buffsize, M_DEVBUF, M_NOWAIT,
531 0ul, 0xfffffful, 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
533 printf("soundcard buffer alloc failed \n");
535 if (tmpbuf == NULL) {
536 printf("snd: Unable to allocate %d bytes of buffer\n",
537 2 * (int) audio_devs[dev]->buffsize);
540 dmap->raw_buf = tmpbuf;
542 * Use virtual address as the physical address, since isa_dmastart
543 * performs the phys address computation.
546 dmap->raw_buf_phys = (uintptr_t) tmpbuf;
547 for (i = 0; i < audio_devs[dev]->buffsize; i++) *tmpbuf++ = 0x80;
555 static u_long dsp_init_mask = 0;
557 for (dev = 0; dev < num_audiodevs; dev++) /* Enumerate devices */
558 if (!(dsp_init_mask & (1 << dev))) /* Not already done */
559 if (audio_devs[dev]->dmachan1 >= 0) {
560 dsp_init_mask |= (1 << dev);
561 audio_devs[dev]->buffsize = DSP_BUFFSIZE;
562 /* Now allocate the buffers */
563 alloc_dmap(dev, audio_devs[dev]->dmachan1,
564 audio_devs[dev]->dmap_out);
565 if (audio_devs[dev]->flags & DMA_DUPLEX)
566 alloc_dmap(dev, audio_devs[dev]->dmachan2,
567 audio_devs[dev]->dmap_in);
575 snd_ioctl_return(int *addr, int value)
578 return value; /* Error */
584 typedef void (*irq_proc_t) (int irq);
585 static irq_proc_t irq_proc[MAX_UNIT] = {NULL};
586 static int irq_irq[MAX_UNIT] = {0};
589 snd_set_irq_handler(int int_lvl, void (*hndlr) (int), sound_os_info * osp)
591 if (osp->unit >= MAX_UNIT) {
592 printf("Sound error: Unit number too high (%d)\n", osp->unit);
595 irq_proc[osp->unit] = hndlr;
596 irq_irq[osp->unit] = int_lvl;
603 if ( (unit >= MAX_UNIT) || (irq_proc[unit] == NULL) )
606 irq_proc[unit] (irq_irq[unit]); /* Call the installed handler */
610 conf_printf(char *name, struct address_info * hw_config)
615 printf("snd0: <%s> ", name);
617 if (hw_config->io_base != -1 )
618 printf("at 0x%03x", hw_config->io_base);
620 if (hw_config->irq != -1 )
621 printf(" irq %d", hw_config->irq);
623 if (hw_config->dma != -1 || hw_config->dma2 != -1) {
624 printf(" dma %d", hw_config->dma);
625 if (hw_config->dma2 != -1)
626 printf(",%d", hw_config->dma2);
633 conf_printf2(char *name, int base, int irq, int dma, int dma2)
638 printf("snd0: <%s> ", name);
640 if (hw_config->io_base != -1 )
641 printf("at 0x%03x", hw_config->io_base);
644 printf(" irq %d", irq);
646 if (dma != -1 || dma2 != -1) {
647 printf(" dma %d", dma);
656 void tenmicrosec (int j)
659 for (k = 0; k < j/10 ; k++) {
660 for (i = 0; i < 16; i++)
665 #endif /* NSND > 0 */