xref: /freebsd/sys/dev/sound/pci/fm801.c (revision f0cfa1b168014f56c02b83e5f28412cc5f78d117)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2000 Dmitry Dicky diwil@dataart.com
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER 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
26  * SUCH DAMAGE.
27  */
28 
29 #ifdef HAVE_KERNEL_OPTION_HEADERS
30 #include "opt_snd.h"
31 #endif
32 
33 #include <dev/sound/pcm/sound.h>
34 #include <dev/sound/pcm/ac97.h>
35 #include <dev/pci/pcireg.h>
36 #include <dev/pci/pcivar.h>
37 
38 SND_DECLARE_FILE("$FreeBSD$");
39 
40 #define PCI_VENDOR_FORTEMEDIA	0x1319
41 #define PCI_DEVICE_FORTEMEDIA1	0x08011319	/* Audio controller */
42 #define PCI_DEVICE_FORTEMEDIA2	0x08021319	/* Joystick controller */
43 
44 #define FM_PCM_VOLUME           0x00
45 #define FM_FM_VOLUME            0x02
46 #define FM_I2S_VOLUME           0x04
47 #define FM_RECORD_SOURCE        0x06
48 
49 #define FM_PLAY_CTL             0x08
50 #define  FM_PLAY_RATE_MASK              0x0f00
51 #define  FM_PLAY_BUF1_LAST              0x0001
52 #define  FM_PLAY_BUF2_LAST              0x0002
53 #define  FM_PLAY_START                  0x0020
54 #define  FM_PLAY_PAUSE                  0x0040
55 #define  FM_PLAY_STOPNOW                0x0080
56 #define  FM_PLAY_16BIT                  0x4000
57 #define  FM_PLAY_STEREO                 0x8000
58 
59 #define FM_PLAY_DMALEN          0x0a
60 #define FM_PLAY_DMABUF1         0x0c
61 #define FM_PLAY_DMABUF2         0x10
62 
63 
64 #define FM_REC_CTL              0x14
65 #define  FM_REC_RATE_MASK               0x0f00
66 #define  FM_REC_BUF1_LAST               0x0001
67 #define  FM_REC_BUF2_LAST               0x0002
68 #define  FM_REC_START                   0x0020
69 #define  FM_REC_PAUSE                   0x0040
70 #define  FM_REC_STOPNOW                 0x0080
71 #define  FM_REC_16BIT                   0x4000
72 #define  FM_REC_STEREO                  0x8000
73 
74 
75 #define FM_REC_DMALEN           0x16
76 #define FM_REC_DMABUF1          0x18
77 #define FM_REC_DMABUF2          0x1c
78 
79 #define FM_CODEC_CTL            0x22
80 #define FM_VOLUME               0x26
81 #define  FM_VOLUME_MUTE                 0x8000
82 
83 #define FM_CODEC_CMD            0x2a
84 #define  FM_CODEC_CMD_READ              0x0080
85 #define  FM_CODEC_CMD_VALID             0x0100
86 #define  FM_CODEC_CMD_BUSY              0x0200
87 
88 #define FM_CODEC_DATA           0x2c
89 
90 #define FM_IO_CTL               0x52
91 #define FM_CARD_CTL             0x54
92 
93 #define FM_INTMASK              0x56
94 #define  FM_INTMASK_PLAY                0x0001
95 #define  FM_INTMASK_REC                 0x0002
96 #define  FM_INTMASK_VOL                 0x0040
97 #define  FM_INTMASK_MPU                 0x0080
98 
99 #define FM_INTSTATUS            0x5a
100 #define  FM_INTSTATUS_PLAY              0x0100
101 #define  FM_INTSTATUS_REC               0x0200
102 #define  FM_INTSTATUS_VOL               0x4000
103 #define  FM_INTSTATUS_MPU               0x8000
104 
105 #define FM801_DEFAULT_BUFSZ	4096	/* Other values do not work!!! */
106 
107 /* debug purposes */
108 #define DPRINT	 if(0) printf
109 
110 /*
111 static int fm801ch_setup(struct pcm_channel *c);
112 */
113 
114 static u_int32_t fmts[] = {
115 	SND_FORMAT(AFMT_U8, 1, 0),
116 	SND_FORMAT(AFMT_U8, 2, 0),
117 	SND_FORMAT(AFMT_S16_LE, 1, 0),
118 	SND_FORMAT(AFMT_S16_LE, 2, 0),
119 	0
120 };
121 
122 static struct pcmchan_caps fm801ch_caps = {
123 	5500, 48000,
124 	fmts, 0
125 };
126 
127 struct fm801_info;
128 
129 struct fm801_chinfo {
130 	struct fm801_info	*parent;
131 	struct pcm_channel	*channel;
132 	struct snd_dbuf		*buffer;
133 	u_int32_t		spd, dir, fmt;  /* speed, direction, format */
134 	u_int32_t		shift;
135 };
136 
137 struct fm801_info {
138 	int			type;
139 	bus_space_tag_t		st;
140 	bus_space_handle_t	sh;
141 	bus_dma_tag_t		parent_dmat;
142 
143 	device_t		dev;
144 	int			num;
145 	u_int32_t		unit;
146 
147 	struct resource		*reg, *irq;
148 	int			regtype, regid, irqid;
149 	void			*ih;
150 
151 	u_int32_t		play_flip,
152 				play_nextblk,
153 				play_start,
154 				play_blksize,
155 				play_fmt,
156 				play_shift,
157 				play_size;
158 
159 	u_int32_t		rec_flip,
160 				rec_nextblk,
161 				rec_start,
162 				rec_blksize,
163 				rec_fmt,
164 				rec_shift,
165 				rec_size;
166 
167 	unsigned int		bufsz;
168 
169 	struct fm801_chinfo	pch, rch;
170 
171 	device_t		radio;
172 };
173 
174 /* Bus Read / Write routines */
175 static u_int32_t
176 fm801_rd(struct fm801_info *fm801, int regno, int size)
177 {
178 	switch(size) {
179 	case 1:
180 		return (bus_space_read_1(fm801->st, fm801->sh, regno));
181 	case 2:
182 		return (bus_space_read_2(fm801->st, fm801->sh, regno));
183 	case 4:
184 		return (bus_space_read_4(fm801->st, fm801->sh, regno));
185 	default:
186 		return 0xffffffff;
187 	}
188 }
189 
190 static void
191 fm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size)
192 {
193 
194 	switch(size) {
195 	case 1:
196 		bus_space_write_1(fm801->st, fm801->sh, regno, data);
197 		break;
198 	case 2:
199 		bus_space_write_2(fm801->st, fm801->sh, regno, data);
200 		break;
201 	case 4:
202 		bus_space_write_4(fm801->st, fm801->sh, regno, data);
203 		break;
204 	}
205 }
206 
207 /* -------------------------------------------------------------------- */
208 /*
209  *  ac97 codec routines
210  */
211 #define TIMO 50
212 static int
213 fm801_rdcd(kobj_t obj, void *devinfo, int regno)
214 {
215 	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
216 	int i;
217 
218 	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
219 		DELAY(10000);
220 		DPRINT("fm801 rdcd: 1 - DELAY\n");
221 	}
222 	if (i >= TIMO) {
223 		printf("fm801 rdcd: codec busy\n");
224 		return 0;
225 	}
226 
227 	fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2);
228 
229 	for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++)
230 	{
231 		DELAY(10000);
232 		DPRINT("fm801 rdcd: 2 - DELAY\n");
233 	}
234 	if (i >= TIMO) {
235 		printf("fm801 rdcd: write codec invalid\n");
236 		return 0;
237 	}
238 
239 	return fm801_rd(fm801,FM_CODEC_DATA,2);
240 }
241 
242 static int
243 fm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
244 {
245 	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
246 	int i;
247 
248 	DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data);
249 /*
250 	if(regno == AC97_REG_RECSEL)	return;
251 */
252 	/* Poll until codec is ready */
253 	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
254 		DELAY(10000);
255 		DPRINT("fm801 rdcd: 1 - DELAY\n");
256 	}
257 	if (i >= TIMO) {
258 		printf("fm801 wrcd: read codec busy\n");
259 		return -1;
260 	}
261 
262 	fm801_wr(fm801,FM_CODEC_DATA,data, 2);
263 	fm801_wr(fm801,FM_CODEC_CMD, regno,2);
264 
265 	/* wait until codec is ready */
266 	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
267 		DELAY(10000);
268 		DPRINT("fm801 wrcd: 2 - DELAY\n");
269 	}
270 	if (i >= TIMO) {
271 		printf("fm801 wrcd: read codec busy\n");
272 		return -1;
273 	}
274 	DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data);
275 	return 0;
276 }
277 
278 static kobj_method_t fm801_ac97_methods[] = {
279     	KOBJMETHOD(ac97_read,		fm801_rdcd),
280     	KOBJMETHOD(ac97_write,		fm801_wrcd),
281 	DEVMETHOD_END
282 };
283 AC97_DECLARE(fm801_ac97);
284 
285 /* -------------------------------------------------------------------- */
286 
287 /*
288  * The interrupt handler
289  */
290 static void
291 fm801_intr(void *p)
292 {
293 	struct fm801_info 	*fm801 = (struct fm801_info *)p;
294 	u_int32_t       	intsrc = fm801_rd(fm801, FM_INTSTATUS, 2);
295 
296 	DPRINT("\nfm801_intr intsrc 0x%x ", intsrc);
297 
298 	if(intsrc & FM_INTSTATUS_PLAY) {
299 		fm801->play_flip++;
300 		if(fm801->play_flip & 1) {
301 			fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4);
302 		} else
303 			fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4);
304 		chn_intr(fm801->pch.channel);
305 	}
306 
307 	if(intsrc & FM_INTSTATUS_REC) {
308 		fm801->rec_flip++;
309 		if(fm801->rec_flip & 1) {
310 			fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4);
311 		} else
312 			fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4);
313 		chn_intr(fm801->rch.channel);
314 	}
315 
316 	if ( intsrc & FM_INTSTATUS_MPU ) {
317 		/* This is a TODOish thing... */
318 		fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2);
319 	}
320 
321 	if ( intsrc & FM_INTSTATUS_VOL ) {
322 		/* This is a TODOish thing... */
323 		fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2);
324 	}
325 
326 	DPRINT("fm801_intr clear\n\n");
327 	fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2);
328 }
329 
330 /* -------------------------------------------------------------------- */
331 /* channel interface */
332 static void *
333 fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
334 {
335 	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
336 	struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch;
337 
338 	DPRINT("fm801ch_init, direction = %d\n", dir);
339 	ch->parent = fm801;
340 	ch->channel = c;
341 	ch->buffer = b;
342 	ch->dir = dir;
343 	if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, 0, fm801->bufsz) != 0)
344 		return NULL;
345 	return (void *)ch;
346 }
347 
348 static int
349 fm801ch_setformat(kobj_t obj, void *data, u_int32_t format)
350 {
351 	struct fm801_chinfo *ch = data;
352 	struct fm801_info *fm801 = ch->parent;
353 
354 	DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format,
355 		(AFMT_CHANNEL(format) > 1)?"stereo":"mono",
356 		(format & AFMT_16BIT) ? "16bit":"8bit",
357 		(format & AFMT_SIGNED)? "signed":"unsigned",
358 		(format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" );
359 
360 	if(ch->dir == PCMDIR_PLAY) {
361 		fm801->play_fmt =
362 		    (AFMT_CHANNEL(format) > 1)? FM_PLAY_STEREO : 0;
363 		fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
364 		return 0;
365 	}
366 
367 	if(ch->dir == PCMDIR_REC ) {
368 		fm801->rec_fmt = (AFMT_CHANNEL(format) > 1)? FM_REC_STEREO:0;
369 		fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
370 		return 0;
371 	}
372 
373 	return 0;
374 }
375 
376 struct {
377 	u_int32_t limit;
378 	u_int32_t rate;
379 } fm801_rates[11] = {
380 	{  6600,  5500 },
381 	{  8750,  8000 },
382 	{ 10250,  9600 },
383 	{ 13200, 11025 },
384 	{ 17500, 16000 },
385 	{ 20500, 19200 },
386 	{ 26500, 22050 },
387 	{ 35000, 32000 },
388 	{ 41000, 38400 },
389 	{ 46000, 44100 },
390 	{ 48000, 48000 },
391 /* anything above -> 48000 */
392 };
393 
394 static u_int32_t
395 fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed)
396 {
397 	struct fm801_chinfo *ch = data;
398 	struct fm801_info *fm801 = ch->parent;
399 	int i;
400 
401 
402 	for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ;
403 
404 	if(ch->dir == PCMDIR_PLAY) {
405 		fm801->pch.spd = fm801_rates[i].rate;
406 		fm801->play_shift = (i<<8);
407 		fm801->play_shift &= FM_PLAY_RATE_MASK;
408 	}
409 
410 	if(ch->dir == PCMDIR_REC ) {
411 		fm801->rch.spd = fm801_rates[i].rate;
412 		fm801->rec_shift = (i<<8);
413 		fm801->rec_shift &= FM_REC_RATE_MASK;
414 	}
415 
416 	ch->spd = fm801_rates[i].rate;
417 
418 	return fm801_rates[i].rate;
419 }
420 
421 static u_int32_t
422 fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
423 {
424 	struct fm801_chinfo *ch = data;
425 	struct fm801_info *fm801 = ch->parent;
426 
427 	/*
428 	 * Don't mind for play_flip, set the blocksize to the
429 	 * desired values in any case - otherwise sound playback
430 	 * breaks here.
431 	 */
432 	if(ch->dir == PCMDIR_PLAY)
433 		fm801->play_blksize = blocksize;
434 
435 	if(ch->dir == PCMDIR_REC)
436 		fm801->rec_blksize = blocksize;
437 
438 	DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir);
439 
440 	return blocksize;
441 }
442 
443 static int
444 fm801ch_trigger(kobj_t obj, void *data, int go)
445 {
446 	struct fm801_chinfo *ch = data;
447 	struct fm801_info *fm801 = ch->parent;
448 	u_int32_t baseaddr = sndbuf_getbufaddr(ch->buffer);
449 	u_int32_t k1;
450 
451 	DPRINT("fm801ch_trigger go %d , ", go);
452 
453 	if (!PCMTRIG_COMMON(go))
454 		return 0;
455 
456 	if (ch->dir == PCMDIR_PLAY) {
457 		if (go == PCMTRIG_START) {
458 
459 			fm801->play_start = baseaddr;
460 			fm801->play_nextblk = fm801->play_start + fm801->play_blksize;
461 			fm801->play_flip = 0;
462 			fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2);
463 			fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4);
464 			fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4);
465 			fm801_wr(fm801, FM_PLAY_CTL,
466 					FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift,
467 					2 );
468 			} else {
469 			fm801->play_flip = 0;
470 			k1 = fm801_rd(fm801, FM_PLAY_CTL,2);
471 			fm801_wr(fm801, FM_PLAY_CTL,
472 				(k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
473 				FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 );
474 		}
475 	} else if(ch->dir == PCMDIR_REC) {
476 		if (go == PCMTRIG_START) {
477 			fm801->rec_start = baseaddr;
478 			fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize;
479 			fm801->rec_flip = 0;
480 			fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2);
481 			fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4);
482 			fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4);
483 			fm801_wr(fm801, FM_REC_CTL,
484 					FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift,
485 					2 );
486 			} else {
487 			fm801->rec_flip = 0;
488 			k1 = fm801_rd(fm801, FM_REC_CTL,2);
489 			fm801_wr(fm801, FM_REC_CTL,
490 				(k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
491 				FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2);
492 		}
493 	}
494 
495 	return 0;
496 }
497 
498 /* Almost ALSA copy */
499 static u_int32_t
500 fm801ch_getptr(kobj_t obj, void *data)
501 {
502 	struct fm801_chinfo *ch = data;
503 	struct fm801_info *fm801 = ch->parent;
504 	u_int32_t result = 0;
505 
506 	if (ch->dir == PCMDIR_PLAY) {
507 		result = fm801_rd(fm801,
508 			(fm801->play_flip&1) ?
509 			FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start;
510 	}
511 
512 	if (ch->dir == PCMDIR_REC) {
513 		result = fm801_rd(fm801,
514 			(fm801->rec_flip&1) ?
515 			FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start;
516 	}
517 
518 	return result;
519 }
520 
521 static struct pcmchan_caps *
522 fm801ch_getcaps(kobj_t obj, void *data)
523 {
524 	return &fm801ch_caps;
525 }
526 
527 static kobj_method_t fm801ch_methods[] = {
528     	KOBJMETHOD(channel_init,		fm801ch_init),
529     	KOBJMETHOD(channel_setformat,		fm801ch_setformat),
530     	KOBJMETHOD(channel_setspeed,		fm801ch_setspeed),
531     	KOBJMETHOD(channel_setblocksize,	fm801ch_setblocksize),
532     	KOBJMETHOD(channel_trigger,		fm801ch_trigger),
533     	KOBJMETHOD(channel_getptr,		fm801ch_getptr),
534     	KOBJMETHOD(channel_getcaps,		fm801ch_getcaps),
535 	DEVMETHOD_END
536 };
537 CHANNEL_DECLARE(fm801ch);
538 
539 /* -------------------------------------------------------------------- */
540 
541 /*
542  *  Init routine is taken from an original NetBSD driver
543  */
544 static int
545 fm801_init(struct fm801_info *fm801)
546 {
547 	u_int32_t k1;
548 
549 	/* reset codec */
550 	fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2);
551 	DELAY(100000);
552 	fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2);
553 	DELAY(100000);
554 
555 	fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2);
556 	fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2);
557 	fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2);
558 	fm801_wr(fm801, 0x40,0x107f,2);	/* enable legacy audio */
559 
560 	fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2);
561 
562 	/* Unmask playback, record and mpu interrupts, mask the rest */
563 	k1 = fm801_rd((void *)fm801, FM_INTMASK,2);
564 	fm801_wr(fm801, FM_INTMASK,
565 		(k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
566 		FM_INTMASK_VOL,2);
567 	fm801_wr(fm801, FM_INTSTATUS,
568 		FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
569 		FM_INTSTATUS_VOL,2);
570 
571 	DPRINT("FM801 init Ok\n");
572 	return 0;
573 }
574 
575 static int
576 fm801_pci_attach(device_t dev)
577 {
578 	struct ac97_info 	*codec = NULL;
579 	struct fm801_info 	*fm801;
580 	int 			i;
581 	int 			mapped = 0;
582 	char 			status[SND_STATUSLEN];
583 
584 	fm801 = malloc(sizeof(*fm801), M_DEVBUF, M_WAITOK | M_ZERO);
585 	fm801->type = pci_get_devid(dev);
586 
587 	pci_enable_busmaster(dev);
588 
589 	for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) {
590 		fm801->regid = PCIR_BAR(i);
591 		fm801->regtype = SYS_RES_MEMORY;
592 		fm801->reg = bus_alloc_resource_any(dev, fm801->regtype,
593 						    &fm801->regid, RF_ACTIVE);
594 		if(!fm801->reg)
595 		{
596 			fm801->regtype = SYS_RES_IOPORT;
597 			fm801->reg = bus_alloc_resource_any(dev,
598 							    fm801->regtype,
599 							    &fm801->regid,
600 							    RF_ACTIVE);
601 		}
602 
603 		if(fm801->reg) {
604 			fm801->st = rman_get_bustag(fm801->reg);
605 			fm801->sh = rman_get_bushandle(fm801->reg);
606 			mapped++;
607 		}
608 	}
609 
610 	if (mapped == 0) {
611 		device_printf(dev, "unable to map register space\n");
612 		goto oops;
613 	}
614 
615 	fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536);
616 
617 	fm801_init(fm801);
618 
619 	codec = AC97_CREATE(dev, fm801, fm801_ac97);
620 	if (codec == NULL) goto oops;
621 
622 	if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops;
623 
624 	fm801->irqid = 0;
625 	fm801->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fm801->irqid,
626 					    RF_ACTIVE | RF_SHAREABLE);
627 	if (!fm801->irq ||
628 	    snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) {
629 		device_printf(dev, "unable to map interrupt\n");
630 		goto oops;
631 	}
632 
633 	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
634 		/*boundary*/0,
635 		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
636 		/*highaddr*/BUS_SPACE_MAXADDR,
637 		/*filter*/NULL, /*filterarg*/NULL,
638 		/*maxsize*/fm801->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
639 		/*flags*/0, /*lockfunc*/busdma_lock_mutex,
640 		/*lockarg*/&Giant, &fm801->parent_dmat) != 0) {
641 		device_printf(dev, "unable to create dma tag\n");
642 		goto oops;
643 	}
644 
645 	snprintf(status, 64, "at %s 0x%jx irq %jd %s",
646 		(fm801->regtype == SYS_RES_IOPORT)? "io" : "memory",
647 		rman_get_start(fm801->reg), rman_get_start(fm801->irq),PCM_KLDSTRING(snd_fm801));
648 
649 #define FM801_MAXPLAYCH	1
650 	if (pcm_register(dev, fm801, FM801_MAXPLAYCH, 1)) goto oops;
651 	pcm_addchan(dev, PCMDIR_PLAY, &fm801ch_class, fm801);
652 	pcm_addchan(dev, PCMDIR_REC, &fm801ch_class, fm801);
653 	pcm_setstatus(dev, status);
654 
655 	fm801->radio = device_add_child(dev, "radio", -1);
656 	bus_generic_attach(dev);
657 
658 	return 0;
659 
660 oops:
661 	if (codec) ac97_destroy(codec);
662 	if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
663 	if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih);
664 	if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
665 	if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat);
666 	free(fm801, M_DEVBUF);
667 	return ENXIO;
668 }
669 
670 static int
671 fm801_pci_detach(device_t dev)
672 {
673 	int r;
674 	struct fm801_info *fm801;
675 
676 	DPRINT("Forte Media FM801 detach\n");
677 
678 	fm801 = pcm_getdevinfo(dev);
679 
680 	r = bus_generic_detach(dev);
681 	if (r)
682 		return r;
683 	if (fm801->radio != NULL) {
684 		r = device_delete_child(dev, fm801->radio);
685 		if (r)
686 			return r;
687 		fm801->radio = NULL;
688 	}
689 
690 	r = pcm_unregister(dev);
691 	if (r)
692 		return r;
693 
694 	bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
695 	bus_teardown_intr(dev, fm801->irq, fm801->ih);
696 	bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
697 	bus_dma_tag_destroy(fm801->parent_dmat);
698 	free(fm801, M_DEVBUF);
699 	return 0;
700 }
701 
702 static int
703 fm801_pci_probe( device_t dev )
704 {
705 	int id;
706 
707 	if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) {
708 		device_set_desc(dev, "Forte Media FM801 Audio Controller");
709 		return BUS_PROBE_DEFAULT;
710 	}
711 /*
712 	if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) {
713 		device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)");
714 		return ENXIO;
715 	}
716 */
717 	return ENXIO;
718 }
719 
720 static struct resource *
721 fm801_alloc_resource(device_t bus, device_t child, int type, int *rid,
722 		     rman_res_t start, rman_res_t end, rman_res_t count,
723 		     u_int flags)
724 {
725 	struct fm801_info *fm801;
726 
727 	fm801 = pcm_getdevinfo(bus);
728 
729 	if (type == SYS_RES_IOPORT && *rid == PCIR_BAR(0))
730 		return (fm801->reg);
731 
732 	return (NULL);
733 }
734 
735 static int
736 fm801_release_resource(device_t bus, device_t child, int type, int rid,
737 		       struct resource *r)
738 {
739 	return (0);
740 }
741 
742 static device_method_t fm801_methods[] = {
743 	/* Device interface */
744 	DEVMETHOD(device_probe,		fm801_pci_probe),
745 	DEVMETHOD(device_attach,	fm801_pci_attach),
746 	DEVMETHOD(device_detach,	fm801_pci_detach),
747 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
748 	DEVMETHOD(device_suspend,	bus_generic_suspend),
749 	DEVMETHOD(device_resume,	bus_generic_resume),
750 
751 	/* Bus interface */
752 	DEVMETHOD(bus_alloc_resource,	fm801_alloc_resource),
753 	DEVMETHOD(bus_release_resource,	fm801_release_resource),
754 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
755 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
756 
757 	DEVMETHOD_END
758 };
759 
760 static driver_t fm801_driver = {
761 	"pcm",
762 	fm801_methods,
763 	PCM_SOFTC_SIZE,
764 };
765 
766 DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, 0, 0);
767 MODULE_DEPEND(snd_fm801, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
768 MODULE_VERSION(snd_fm801, 1);
769