xref: /freebsd/sys/dev/sound/pcm/sound.c (revision 39004e693d9164594ded4ed8ef48c6bcb25ebf1c)
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 <dev/sound/pcm/sound.h>
31 
32 static int 	status_isopen = 0;
33 static int 	status_init(char *buf, int size);
34 static int 	status_read(struct uio *buf);
35 
36 static d_open_t sndopen;
37 static d_close_t sndclose;
38 static d_ioctl_t sndioctl;
39 static d_read_t sndread;
40 static d_write_t sndwrite;
41 static d_mmap_t sndmmap;
42 static d_poll_t sndpoll;
43 
44 #define CDEV_MAJOR 30
45 static struct cdevsw snd_cdevsw = {
46 	/* open */	sndopen,
47 	/* close */	sndclose,
48 	/* read */	sndread,
49 	/* write */	sndwrite,
50 	/* ioctl */	sndioctl,
51 	/* poll */	sndpoll,
52 	/* mmap */	sndmmap,
53 	/* strategy */	nostrategy,
54 	/* name */	"snd",
55 	/* maj */	CDEV_MAJOR,
56 	/* dump */	nodump,
57 	/* psize */	nopsize,
58 	/* flags */	0,
59 	/* bmaj */	-1
60 };
61 
62 /* PROPOSAL:
63 each unit needs:
64 status, mixer, dsp, dspW, audio, sequencer, midi-in, seq2, sndproc = 9 devices
65 dspW and audio are deprecated.
66 dsp needs min 64 channels, will give it 256
67 
68 minor = (unit << 12) + (dev << 8) + channel
69 currently minor = (channel << 8) + (unit << 4) + dev
70 
71 nomenclature:
72 	/dev/pcmX/dsp.(0..255)
73 	/dev/pcmX/dspW
74 	/dev/pcmX/audio
75 	/dev/pcmX/status
76 	/dev/pcmX/mixer
77 	[etc.]
78 
79 currently:
80 minor = (channel << 8) + (unit << 4) + dev
81 */
82 
83 #define PCMMINOR(x) (minor(x))
84 #define PCMCHAN(x) ((PCMMINOR(x) & 0x0000ff00) >> 8)
85 #define PCMUNIT(x) ((PCMMINOR(x) & 0x000000f0) >> 4)
86 #define PCMDEV(x)   (PCMMINOR(x) & 0x0000000f)
87 #define PCMMKMINOR(u, d, c) ((((c) & 0xff) << 8) | (((u) & 0x0f) << 4) | ((d) & 0x0f))
88 
89 static devclass_t pcm_devclass;
90 
91 static snddev_info *
92 gsd(int unit)
93 {
94 	return devclass_get_softc(pcm_devclass, unit);
95 }
96 
97 int
98 pcm_addchan(device_t dev, int dir, pcm_channel *templ, void *devinfo)
99 {
100     	int unit = device_get_unit(dev);
101     	snddev_info *d = device_get_softc(dev);
102 	pcm_channel *ch;
103 
104 	ch = (dir == PCMDIR_PLAY)? &d->play[d->playcount] : &d->rec[d->reccount];
105 	*ch = *templ;
106 	if (chn_init(ch, devinfo, dir)) {
107 		device_printf(dev, "chn_init() for %s:%d failed\n",
108 		              (dir == PCMDIR_PLAY)? "play" : "record",
109 			      (dir == PCMDIR_PLAY)? d->playcount : d->reccount);
110 		return 1;
111 	}
112 	if (dir == PCMDIR_PLAY) d->playcount++; else d->reccount++;
113 	make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP, d->chancount),
114 		 UID_ROOT, GID_WHEEL, 0666, "dsp%d.%d", unit, d->chancount);
115 	make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_AUDIO, d->chancount),
116 		 UID_ROOT, GID_WHEEL, 0666, "audio%d.%d", unit, d->chancount);
117 	make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP16, d->chancount),
118 		 UID_ROOT, GID_WHEEL, 0666, "dspW%d.%d", unit, d->chancount);
119 	/* XXX SND_DEV_NORESET? */
120 	d->chancount++;
121 	return 0;
122 }
123 
124 int
125 pcm_setstatus(device_t dev, char *str)
126 {
127     	snddev_info *d = device_get_softc(dev);
128 	strncpy(d->status, str, SND_STATUSLEN);
129 	return 0;
130 }
131 
132 u_int32_t
133 pcm_getflags(device_t dev)
134 {
135     	snddev_info *d = device_get_softc(dev);
136 	return d->flags;
137 }
138 
139 void
140 pcm_setflags(device_t dev, u_int32_t val)
141 {
142     	snddev_info *d = device_get_softc(dev);
143 	d->flags = val;
144 }
145 
146 void *
147 pcm_getdevinfo(device_t dev)
148 {
149     	snddev_info *d = device_get_softc(dev);
150 	return d->devinfo;
151 }
152 
153 void
154 pcm_setswap(device_t dev, pcm_swap_t *swap)
155 {
156     	snddev_info *d = device_get_softc(dev);
157 	d->swap = swap;
158 }
159 /* This is the generic init routine */
160 int
161 pcm_register(device_t dev, void *devinfo, int numplay, int numrec)
162 {
163     	int sz, unit = device_get_unit(dev);
164     	snddev_info *d = device_get_softc(dev);
165 
166     	if (!pcm_devclass) {
167     		pcm_devclass = device_get_devclass(dev);
168 		make_dev(&snd_cdevsw, PCMMKMINOR(0, SND_DEV_STATUS, 0),
169 			 UID_ROOT, GID_WHEEL, 0444, "sndstat");
170 	}
171 	make_dev(&snd_cdevsw, PCMMKMINOR(unit, SND_DEV_CTL, 0),
172 		 UID_ROOT, GID_WHEEL, 0666, "mixer%d", unit);
173 
174 	d->devinfo = devinfo;
175 	d->chancount = d->playcount = d->reccount = 0;
176     	sz = (numplay + numrec) * sizeof(pcm_channel *);
177 
178 	if (sz > 0) {
179 		d->aplay = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT);
180     		if (!d->aplay) goto no;
181     		bzero(d->aplay, sz);
182 
183     		d->arec = (pcm_channel **)malloc(sz, M_DEVBUF, M_NOWAIT);
184     		if (!d->arec) goto no;
185     		bzero(d->arec, sz);
186 	}
187 
188 	if (numplay > 0) {
189     		d->play = (pcm_channel *)malloc(numplay * sizeof(pcm_channel),
190 						M_DEVBUF, M_NOWAIT);
191     		if (!d->play) goto no;
192     		bzero(d->play, numplay * sizeof(pcm_channel));
193 	}
194 
195 	if (numrec > 0) {
196 	  	d->rec = (pcm_channel *)malloc(numrec * sizeof(pcm_channel),
197 				       	M_DEVBUF, M_NOWAIT);
198     		if (!d->rec) goto no;
199     		bzero(d->rec, numrec * sizeof(pcm_channel));
200 	}
201 
202 	fkchan_setup(&d->fakechan);
203 	chn_init(&d->fakechan, NULL, 0);
204 	d->magic = MAGIC(unit); /* debugging... */
205 	d->swap = NULL;
206 
207     	return 0;
208 no:
209 	if (d->aplay) free(d->aplay, M_DEVBUF);
210 	if (d->play) free(d->play, M_DEVBUF);
211 	if (d->arec) free(d->arec, M_DEVBUF);
212 	if (d->rec) free(d->rec, M_DEVBUF);
213 	return ENXIO;
214 }
215 
216 /*
217  * a small utility function which, given a device number, returns
218  * a pointer to the associated snddev_info struct, and sets the unit
219  * number.
220  */
221 static snddev_info *
222 get_snddev_info(dev_t i_dev, int *unit, int *dev, int *chan)
223 {
224     	int u, d, c;
225 
226     	u = PCMUNIT(i_dev);
227     	d = PCMDEV(i_dev);
228     	c = PCMCHAN(i_dev);
229     	if (u > devclass_get_maxunit(pcm_devclass)) u = -1;
230     	if (unit) *unit = u;
231     	if (dev) *dev = d;
232     	if (chan) *chan = c;
233     	if (u < 0) return NULL;
234 
235     	switch(d) {
236     	case SND_DEV_CTL:	/* /dev/mixer handled by pcm */
237     	case SND_DEV_STATUS: /* /dev/sndstat handled by pcm */
238     	case SND_DEV_DSP:
239     	case SND_DEV_DSP16:
240     	case SND_DEV_AUDIO:
241 		return gsd(u);
242 
243     	case SND_DEV_SEQ: /* XXX when enabled... */
244     	case SND_DEV_SEQ2:
245     	case SND_DEV_MIDIN:
246     	case SND_DEV_SNDPROC:	/* /dev/sndproc handled by pcm */
247     	default:
248 		printf("unsupported subdevice %d\n", d);
249 		return NULL;
250     	}
251 }
252 
253 static int
254 sndopen(dev_t i_dev, int flags, int mode, struct proc *p)
255 {
256     	int dev, unit, chan;
257     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
258 
259     	DEB(printf("open snd%d subdev %d flags 0x%08x mode 0x%08x\n",
260 		unit, dev, flags, mode));
261 
262     	switch(dev) {
263     	case SND_DEV_STATUS:
264 		if (status_isopen) return EBUSY;
265 		status_isopen = 1;
266 		return 0;
267 
268     	case SND_DEV_CTL:
269 		return d? 0 : ENXIO;
270 
271     	case SND_DEV_AUDIO:
272     	case SND_DEV_DSP:
273     	case SND_DEV_DSP16:
274 	case SND_DEV_NORESET:
275 		return d? dsp_open(d, chan, flags, dev) : ENXIO;
276 
277     	default:
278     		return ENXIO;
279     	}
280 }
281 
282 static int
283 sndclose(dev_t i_dev, int flags, int mode, struct proc *p)
284 {
285     	int dev, unit, chan;
286     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
287 
288     	DEB(printf("close snd%d subdev %d\n", unit, dev));
289 
290     	switch(dev) { /* only those for which close makes sense */
291     	case SND_DEV_STATUS:
292 		if (!status_isopen) return EBADF;
293 		status_isopen = 0;
294 		return 0;
295 
296     	case SND_DEV_CTL:
297 		return d? 0 : ENXIO;
298 
299     	case SND_DEV_AUDIO:
300     	case SND_DEV_DSP:
301     	case SND_DEV_DSP16:
302 		return d? dsp_close(d, chan, dev) : ENXIO;
303 
304     	default:
305 		return ENXIO;
306     	}
307 }
308 
309 static int
310 sndread(dev_t i_dev, struct uio *buf, int flag)
311 {
312     	int dev, unit, chan;
313     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
314     	DEB(printf("read snd%d subdev %d flag 0x%08x\n", unit, dev, flag));
315 
316     	switch(dev) {
317     	case SND_DEV_STATUS:
318 		return status_isopen? status_read(buf) : EBADF;
319 
320     	case SND_DEV_AUDIO:
321     	case SND_DEV_DSP:
322     	case SND_DEV_DSP16:
323         	return d? dsp_read(d, chan, buf, flag) : EBADF;
324 
325     	default:
326     		return ENXIO;
327     	}
328 }
329 
330 static int
331 sndwrite(dev_t i_dev, struct uio *buf, int flag)
332 {
333     	int dev, unit, chan;
334     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
335 
336     	DEB(printf("write snd%d subdev %d flag 0x%08x\n", unit, dev & 0xf, flag));
337 
338     	switch(dev) {	/* only writeable devices */
339     	case SND_DEV_DSP:
340     	case SND_DEV_DSP16:
341     	case SND_DEV_AUDIO:
342 		return d? dsp_write(d, chan, buf, flag) : EBADF;
343 
344     	default:
345 		return EPERM; /* for non-writeable devices ; */
346     	}
347 }
348 
349 static int
350 sndioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc * p)
351 {
352     	int dev, chan;
353     	snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan);
354 
355     	if (d == NULL) return ENXIO;
356 
357     	switch(dev) {
358     	case SND_DEV_CTL:
359 		return mixer_ioctl(d, cmd, arg);
360 
361     	case SND_DEV_AUDIO:
362     	case SND_DEV_DSP:
363     	case SND_DEV_DSP16:
364 		if (IOCGROUP(cmd) == 'M')
365 			return mixer_ioctl(d, cmd, arg);
366 		else
367 			return dsp_ioctl(d, chan, cmd, arg);
368 
369     	default:
370     		return ENXIO;
371     	}
372 }
373 
374 static int
375 sndpoll(dev_t i_dev, int events, struct proc *p)
376 {
377     	int dev, chan;
378     	snddev_info *d = get_snddev_info(i_dev, NULL, &dev, &chan);
379 
380 	DEB(printf("sndpoll d 0x%p dev 0x%04x events 0x%08x\n", d, dev, events));
381 
382     	if (d == NULL) return ENXIO;
383 
384     	switch(dev) {
385     	case SND_DEV_AUDIO:
386     	case SND_DEV_DSP:
387     	case SND_DEV_DSP16:
388 		return dsp_poll(d, chan, events, p);
389 
390     	default:
391     		return (events &
392        		       (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM)) | POLLHUP;
393     	}
394 }
395 
396 /*
397  * The mmap interface allows access to the play and read buffer,
398  * plus the device descriptor.
399  * The various blocks are accessible at the following offsets:
400  *
401  * 0x00000000 ( 0   ) : write buffer ;
402  * 0x01000000 (16 MB) : read buffer ;
403  * 0x02000000 (32 MB) : device descriptor (dangerous!)
404  *
405  * WARNING: the mmap routines assume memory areas are aligned. This
406  * is true (probably) for the dma buffers, but likely false for the
407  * device descriptor. As a consequence, we do not know where it is
408  * located in the requested area.
409  */
410 static int
411 sndmmap(dev_t i_dev, vm_offset_t offset, int nprot)
412 {
413     	int unit, dev, chan;
414     	snddev_info *d = get_snddev_info(i_dev, &unit, &dev, &chan);
415 
416     	DEB(printf("sndmmap d 0x%p dev 0x%04x ofs 0x%08x nprot 0x%08x\n",
417 		   d, dev, offset, nprot));
418 
419     	if (d == NULL || nprot & PROT_EXEC)	return -1; /* forbidden */
420 
421     	switch(dev) {
422     	case SND_DEV_AUDIO:
423     	case SND_DEV_DSP:
424     	case SND_DEV_DSP16:
425 		return dsp_mmap(d, chan, offset, nprot);
426 
427     	default:
428     		return -1;
429     	}
430 }
431 
432 static int
433 status_init(char *buf, int size)
434 {
435     	int             i;
436     	device_t	    dev;
437     	snddev_info     *d;
438 
439     	snprintf(buf, size, "FreeBSD Audio Driver (newpcm) %s %s\n"
440 		 "Installed devices:\n", __DATE__, __TIME__);
441 
442     	for (i = 0; i <= devclass_get_maxunit(pcm_devclass); i++) {
443 		d = gsd(i);
444 		if (!d) continue;
445 		dev = devclass_get_device(pcm_devclass, i);
446         	if (1) {
447 			snprintf(buf + strlen(buf), size - strlen(buf),
448 		            	"pcm%d: <%s> %s",
449 		            	i, device_get_desc(dev), d->status);
450 			if (d->chancount > 0)
451 				snprintf(buf + strlen(buf), size - strlen(buf),
452 				" (%dp/%dr channels%s)\n",
453 			    	d->playcount, d->reccount,
454 			    	(!(d->flags & SD_F_SIMPLEX))? " duplex" : "");
455 			else
456 				snprintf(buf + strlen(buf), size - strlen(buf),
457 				" (mixer only)\n");
458 		}
459     	}
460     	return strlen(buf);
461 }
462 
463 static int
464 status_read(struct uio *buf)
465 {
466     	static char	status_buf[4096];
467     	static int 	bufptr = 0, buflen = 0;
468     	int l;
469 
470     	if (status_isopen == 1) {
471 		status_isopen++;
472 		bufptr = 0;
473 		buflen = status_init(status_buf, sizeof status_buf);
474     	}
475 
476     	l = min(buf->uio_resid, buflen - bufptr);
477     	bufptr += l;
478     	return (l > 0)? uiomove(status_buf + bufptr - l, l, buf) : 0;
479 }
480