xref: /freebsd/sys/dev/sound/pcm/sound.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 /*
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * (C) 1997 Luigi Rizzo (luigi@iet.unipi.it)
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include "opt_devfs.h"
31 
32 #include <dev/pcm/sound.h>
33 #ifdef DEVFS
34 #include <sys/devfsext.h>
35 #endif /* DEVFS */
36 
37 #if NPCM > 0	/* from "pcm.h" via disgusting #include in snd/sound.h */
38 
39 extern struct isa_driver pcmdriver;
40 
41 static int 	status_isopen = 0;
42 static int 	status_init(char *buf, int size);
43 static int 	status_read(struct uio *buf);
44 
45 static d_open_t sndopen;
46 static d_close_t sndclose;
47 static d_ioctl_t sndioctl;
48 static d_read_t sndread;
49 static d_write_t sndwrite;
50 static d_mmap_t sndmmap;
51 static d_poll_t sndpoll;
52 
53 #define CDEV_MAJOR 30
54 static struct cdevsw snd_cdevsw = {
55 	/* open */	sndopen,
56 	/* close */	sndclose,
57 	/* read */	sndread,
58 	/* write */	sndwrite,
59 	/* ioctl */	sndioctl,
60 	/* poll */	sndpoll,
61 	/* mmap */	sndmmap,
62 	/* strategy */	nostrategy,
63 	/* name */	"snd",
64 	/* maj */	CDEV_MAJOR,
65 	/* dump */	nodump,
66 	/* psize */	nopsize,
67 	/* flags */	0,
68 	/* bmaj */	-1
69 };
70 
71 /* PROPOSAL:
72 each unit needs:
73 status, mixer, dsp, dspW, audio, sequencer, midi-in, seq2, sndproc = 9 devices
74 dspW and audio are deprecated.
75 dsp needs min 64 channels, will give it 256
76 
77 minor = (unit << 12) + (dev << 8) + channel
78 currently minor = (channel << 8) + (unit << 4) + dev
79 
80 nomenclature:
81 	/dev/pcmX/dsp.(0..255)
82 	/dev/pcmX/dspW
83 	/dev/pcmX/audio
84 	/dev/pcmX/status
85 	/dev/pcmX/mixer
86 	[etc.]
87 
88 currently:
89 minor = (channel << 8) + (unit << 4) + dev
90 */
91 
92 #define PCMMINOR(x) (minor(x))
93 #define PCMCHAN(x) ((PCMMINOR(x) & 0x0000ff00) >> 8)
94 #define PCMUNIT(x) ((PCMMINOR(x) & 0x000000f0) >> 4)
95 #define PCMDEV(x)   (PCMMINOR(x) & 0x0000000f)
96 
97 static devclass_t pcm_devclass;
98 
99 static snddev_info *
100 gsd(int unit)
101 {
102 	return devclass_get_softc(pcm_devclass, unit);
103 }
104 
105 int
106 pcm_addchan(device_t dev, int dir, pcm_channel *templ, void *devinfo)
107 {
108     	snddev_info *d = device_get_softc(dev);
109 	pcm_channel *ch;
110 
111 	ch = (dir == PCMDIR_PLAY)? &d->play[d->playcount++] : &d->rec[d->reccount++];
112 	*ch = *templ;
113 	chn_init(ch, devinfo, dir);
114 	d->chancount++;
115 	return 0;
116 }
117 
118 int
119 pcm_setstatus(device_t dev, char *str)
120 {
121     	snddev_info *d = device_get_softc(dev);
122 	strncpy(d->status, str, SND_STATUSLEN);
123 	return 0;
124 }
125 
126 u_int32_t
127 pcm_getflags(device_t dev)
128 {
129     	snddev_info *d = device_get_softc(dev);
130 	return d->flags;
131 }
132 
133 void
134 pcm_setflags(device_t dev, u_int32_t val)
135 {
136     	snddev_info *d = device_get_softc(dev);
137 	d->flags = val;
138 }
139 
140 /* This is the generic init routine */
141 int
142 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
143 {
144     	int sz, unit = device_get_unit(dev);
145     	snddev_info *d = device_get_softc(dev);
146 
147     	if (!pcm_devclass) {
148     		pcm_devclass = device_get_devclass(dev);
149 		cdevsw_add(&snd_cdevsw);
150     	}
151 	d->devinfo = devinfo;
152 	d->chancount = d->playcount = d->reccount = 0;
153     	sz = (numplay + numrec) * sizeof(pcm_channel *);
154     	d->aplay = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT);
155     	if (!d->aplay) goto no;
156     	d->arec = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT);
157     	if (!d->arec) goto no;
158     	bzero(d->aplay, sz);
159     	bzero(d->arec, sz);
160 
161     	d->play = (pcm_channel *)malloc(numplay * sizeof(pcm_channel),
162 					M_DEVBUF, M_NOWAIT);
163     	if (!d->play) goto no;
164     	d->rec = (pcm_channel *)malloc(numrec * sizeof(pcm_channel),
165 				       M_DEVBUF, M_NOWAIT);
166     	if (!d->rec) goto no;
167     	bzero(d->play, numplay * sizeof(pcm_channel));
168     	bzero(d->rec, numrec * sizeof(pcm_channel));
169 
170 	fkchan_setup(&d->fakechan);
171 	chn_init(&d->fakechan, NULL, 0);
172 	d->magic = MAGIC(unit); /* debugging... */
173 
174     	return 0;
175 no:
176 	if (d->aplay) free(d->aplay, M_DEVBUF);
177 	if (d->play) free(d->play, M_DEVBUF);
178 	if (d->arec) free(d->arec, M_DEVBUF);
179 	if (d->rec) free(d->rec, M_DEVBUF);
180 	return ENXIO;
181 }
182 
183 /*
184  * a small utility function which, given a device number, returns
185  * a pointer to the associated snddev_info struct, and sets the unit
186  * number.
187  */
188 static snddev_info *
189 get_snddev_info(dev_t i_dev, int *unit, int *dev, int *chan)
190 {
191     	int u, d, c;
192 
193     	u = PCMUNIT(i_dev);
194     	d = PCMDEV(i_dev);
195     	c = PCMCHAN(i_dev);
196     	if (u > devclass_get_maxunit(pcm_devclass)) u = -1;
197     	if (unit) *unit = u;
198     	if (dev) *dev = d;
199     	if (chan) *chan = c;
200     	if (u < 0) return NULL;
201 
202     	switch(d) {
203     	case SND_DEV_CTL:	/* /dev/mixer handled by pcm */
204     	case SND_DEV_STATUS: /* /dev/sndstat handled by pcm */
205     	case SND_DEV_DSP:
206     	case SND_DEV_DSP16:
207     	case SND_DEV_AUDIO:
208 		return gsd(u);
209 
210     	case SND_DEV_SEQ: /* XXX when enabled... */
211     	case SND_DEV_SEQ2:
212     	case SND_DEV_MIDIN:
213     	case SND_DEV_SNDPROC:	/* /dev/sndproc handled by pcm */
214     	default:
215 		printf("unsupported subdevice %d\n", d);
216 		return NULL;
217     	}
218 }
219 
220 static int
221 sndopen(dev_t i_dev, int flags, int mode, struct proc *p)
222 {
223     	int dev, unit, chan;
224     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
225 
226     	DEB(printf("open snd%d subdev %d flags 0x%08x mode 0x%08x\n",
227 		unit, dev, flags, mode));
228 
229     	switch(dev) {
230     	case SND_DEV_STATUS:
231 		if (status_isopen) return EBUSY;
232 		status_isopen = 1;
233 		return 0;
234 
235     	case SND_DEV_CTL:
236 		return d? 0 : ENXIO;
237 
238     	case SND_DEV_AUDIO:
239     	case SND_DEV_DSP:
240     	case SND_DEV_DSP16:
241 	case SND_DEV_NORESET:
242 		return d? dsp_open(d, chan, flags, dev) : ENXIO;
243 
244     	default:
245     		return ENXIO;
246     	}
247 }
248 
249 static int
250 sndclose(dev_t i_dev, int flags, int mode, struct proc *p)
251 {
252     	int dev, unit, chan;
253     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
254 
255     	DEB(printf("close snd%d subdev %d\n", unit, dev));
256 
257     	switch(dev) { /* only those for which close makes sense */
258     	case SND_DEV_STATUS:
259 		if (!status_isopen) return EBADF;
260 		status_isopen = 0;
261 		return 0;
262 
263     	case SND_DEV_CTL:
264 		return d? 0 : ENXIO;
265 
266     	case SND_DEV_AUDIO:
267     	case SND_DEV_DSP:
268     	case SND_DEV_DSP16:
269 		return d? dsp_close(d, chan, dev) : ENXIO;
270 
271     	default:
272 		return ENXIO;
273     	}
274 }
275 
276 static int
277 sndread(dev_t i_dev, struct uio *buf, int flag)
278 {
279     	int dev, unit, chan;
280     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
281     	DEB(printf("read snd%d subdev %d flag 0x%08x\n", unit, dev, flag));
282 
283     	switch(dev) {
284     	case SND_DEV_STATUS:
285 		return status_isopen? status_read(buf) : EBADF;
286 
287     	case SND_DEV_AUDIO:
288     	case SND_DEV_DSP:
289     	case SND_DEV_DSP16:
290         	return d? dsp_read(d, chan, buf, flag) : EBADF;
291 
292     	default:
293     		return ENXIO;
294     	}
295 }
296 
297 static int
298 sndwrite(dev_t i_dev, struct uio *buf, int flag)
299 {
300     	int dev, unit, chan;
301     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
302 
303     	DEB(printf("write snd%d subdev %d flag 0x%08x\n", unit, dev & 0xf, flag));
304 
305     	switch(dev) {	/* only writeable devices */
306     	case SND_DEV_DSP:
307     	case SND_DEV_DSP16:
308     	case SND_DEV_AUDIO:
309 		return d? dsp_write(d, chan, buf, flag) : EBADF;
310 
311     	default:
312 		return EPERM; /* for non-writeable devices ; */
313     	}
314 }
315 
316 static int
317 sndioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc * p)
318 {
319     	int dev, chan;
320     	snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan);
321 
322     	if (d == NULL) return ENXIO;
323 
324     	switch(dev) {
325     	case SND_DEV_CTL:
326 		return mixer_ioctl(d, cmd, arg);
327 
328     	case SND_DEV_AUDIO:
329     	case SND_DEV_DSP:
330     	case SND_DEV_DSP16:
331 		return dsp_ioctl(d, chan, cmd, arg);
332 
333     	default:
334     		return ENXIO;
335     	}
336 }
337 
338 static int
339 sndpoll(dev_t i_dev, int events, struct proc *p)
340 {
341     	int dev, chan;
342     	snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan);
343 
344     	DEB(printf("sndpoll dev 0x%04x events 0x%08x\n", i_dev, events));
345 
346     	if (d == NULL) return ENXIO;
347 
348     	switch(dev) {
349     	case SND_DEV_AUDIO:
350     	case SND_DEV_DSP:
351     	case SND_DEV_DSP16:
352 		return dsp_poll(d, chan, events, p);
353 
354     	default:
355     		return (events &
356        		       (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) | POLLHUP;
357     	}
358 }
359 
360 /*
361  * The mmap interface allows access to the play and read buffer,
362  * plus the device descriptor.
363  * The various blocks are accessible at the following offsets:
364  *
365  * 0x00000000 ( 0   ) : write buffer ;
366  * 0x01000000 (16 MB) : read buffer ;
367  * 0x02000000 (32 MB) : device descriptor (dangerous!)
368  *
369  * WARNING: the mmap routines assume memory areas are aligned. This
370  * is true (probably) for the dma buffers, but likely false for the
371  * device descriptor. As a consequence, we do not know where it is
372  * located in the requested area.
373  */
374 static int
375 sndmmap(dev_t i_dev, vm_offset_t offset, int nprot)
376 {
377     	int unit, dev, chan;
378     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
379 
380     	DEB(printf("sndmmap d 0x%p dev 0x%04x ofs 0x%08x nprot 0x%08x\n",
381 		   d, dev, offset, nprot));
382 
383     	if (d == NULL || nprot & PROT_EXEC)	return -1; /* forbidden */
384 
385     	switch(dev) {
386     	case SND_DEV_AUDIO:
387     	case SND_DEV_DSP:
388     	case SND_DEV_DSP16:
389 		return dsp_mmap(d, chan, offset, nprot);
390 
391     	default:
392     		return -1;
393     	}
394 }
395 
396 static int
397 status_init(char *buf, int size)
398 {
399     	int             i;
400     	device_t	    dev;
401     	snddev_info     *d;
402 
403     	snprintf(buf, size, "FreeBSD Audio Driver (newpcm) %s %s\n"
404 		 "Installed devices:\n", __DATE__, __TIME__);
405 
406     	for (i = 0; i <= devclass_get_maxunit(pcm_devclass); i++) {
407 		d = gsd(i);
408 		if (!d) continue;
409 		dev = devclass_get_device(pcm_devclass, i);
410         	if (1) snprintf(buf + strlen(buf), size - strlen(buf),
411 		            	"pcm%d: <%s> %s (%d/%d channels%s)\n",
412 		            	i, device_get_desc(dev), d->status,
413 		            	d->playcount, d->reccount,
414 			    	(!(d->flags & SD_F_SIMPLEX))? " duplex" : "");
415     	}
416     	return strlen(buf);
417 }
418 
419 static int
420 status_read(struct uio *buf)
421 {
422     	static char	status_buf[4096];
423     	static int 	bufptr = 0, buflen = 0;
424     	int l;
425 
426     	if (status_isopen == 1) {
427 		status_isopen++;
428 		bufptr = 0;
429 		buflen = status_init(status_buf, sizeof status_buf);
430     	}
431 
432     	l = min(buf->uio_resid, buflen - bufptr);
433     	bufptr += l;
434     	return (l > 0)? uiomove(status_buf + bufptr - l, l, buf) : 0;
435 }
436 
437 #endif	/* NPCM > 0 */
438