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