fm801.c revision 102620
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
27#include <dev/sound/pcm/sound.h>
28#include <dev/sound/pcm/ac97.h>
29#include <pci/pcireg.h>
30#include <pci/pcivar.h>
31
32SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/fm801.c 102620 2002-08-30 14:21:01Z sobomax $");
33
34#define PCI_VENDOR_FORTEMEDIA	0x1319
35#define PCI_DEVICE_FORTEMEDIA1	0x08011319
36#define PCI_DEVICE_FORTEMEDIA2	0x08021319	/* ??? have no idea what's this... */
37
38#define FM_PCM_VOLUME           0x00
39#define FM_FM_VOLUME            0x02
40#define FM_I2S_VOLUME           0x04
41#define FM_RECORD_SOURCE        0x06
42
43#define FM_PLAY_CTL             0x08
44#define  FM_PLAY_RATE_MASK              0x0f00
45#define  FM_PLAY_BUF1_LAST              0x0001
46#define  FM_PLAY_BUF2_LAST              0x0002
47#define  FM_PLAY_START                  0x0020
48#define  FM_PLAY_PAUSE                  0x0040
49#define  FM_PLAY_STOPNOW                0x0080
50#define  FM_PLAY_16BIT                  0x4000
51#define  FM_PLAY_STEREO                 0x8000
52
53#define FM_PLAY_DMALEN          0x0a
54#define FM_PLAY_DMABUF1         0x0c
55#define FM_PLAY_DMABUF2         0x10
56
57
58#define FM_REC_CTL              0x14
59#define  FM_REC_RATE_MASK               0x0f00
60#define  FM_REC_BUF1_LAST               0x0001
61#define  FM_REC_BUF2_LAST               0x0002
62#define  FM_REC_START                   0x0020
63#define  FM_REC_PAUSE                   0x0040
64#define  FM_REC_STOPNOW                 0x0080
65#define  FM_REC_16BIT                   0x4000
66#define  FM_REC_STEREO                  0x8000
67
68
69#define FM_REC_DMALEN           0x16
70#define FM_REC_DMABUF1          0x18
71#define FM_REC_DMABUF2          0x1c
72
73#define FM_CODEC_CTL            0x22
74#define FM_VOLUME               0x26
75#define  FM_VOLUME_MUTE                 0x8000
76
77#define FM_CODEC_CMD            0x2a
78#define  FM_CODEC_CMD_READ              0x0080
79#define  FM_CODEC_CMD_VALID             0x0100
80#define  FM_CODEC_CMD_BUSY              0x0200
81
82#define FM_CODEC_DATA           0x2c
83
84#define FM_IO_CTL               0x52
85#define FM_CARD_CTL             0x54
86
87#define FM_INTMASK              0x56
88#define  FM_INTMASK_PLAY                0x0001
89#define  FM_INTMASK_REC                 0x0002
90#define  FM_INTMASK_VOL                 0x0040
91#define  FM_INTMASK_MPU                 0x0080
92
93#define FM_INTSTATUS            0x5a
94#define  FM_INTSTATUS_PLAY              0x0100
95#define  FM_INTSTATUS_REC               0x0200
96#define  FM_INTSTATUS_VOL               0x4000
97#define  FM_INTSTATUS_MPU               0x8000
98
99#define FM801_DEFAULT_BUFSZ	4096	/* Other values do not work!!! */
100
101/* debug purposes */
102#define DPRINT	 if(0) printf
103
104/*
105static int fm801ch_setup(struct pcm_channel *c);
106*/
107
108static u_int32_t fmts[] = {
109	AFMT_U8,
110	AFMT_STEREO | AFMT_U8,
111	AFMT_S16_LE,
112	AFMT_STEREO | AFMT_S16_LE,
113	0
114};
115
116static struct pcmchan_caps fm801ch_caps = {
117	4000, 48000,
118	fmts, 0
119};
120
121struct fm801_info;
122
123struct fm801_chinfo {
124	struct fm801_info	*parent;
125	struct pcm_channel	*channel;
126	struct snd_dbuf		*buffer;
127	u_int32_t		spd, dir, fmt;  /* speed, direction, format */
128	u_int32_t		shift;
129};
130
131struct fm801_info {
132	int			type;
133	bus_space_tag_t		st;
134	bus_space_handle_t	sh;
135	bus_dma_tag_t		parent_dmat;
136
137	device_t		dev;
138	int			num;
139	u_int32_t		unit;
140
141	struct resource		*reg, *irq;
142	int			regtype, regid, irqid;
143	void			*ih;
144
145	u_int32_t		play_flip,
146				play_nextblk,
147				play_start,
148				play_blksize,
149				play_fmt,
150				play_shift,
151				play_size;
152
153	u_int32_t		rec_flip,
154				rec_nextblk,
155				rec_start,
156				rec_blksize,
157				rec_fmt,
158				rec_shift,
159				rec_size;
160
161	unsigned int		bufsz;
162
163	struct fm801_chinfo	pch, rch;
164};
165
166/* Bus Read / Write routines */
167static u_int32_t
168fm801_rd(struct fm801_info *fm801, int regno, int size)
169{
170	switch(size) {
171	case 1:
172		return (bus_space_read_1(fm801->st, fm801->sh, regno));
173	case 2:
174		return (bus_space_read_2(fm801->st, fm801->sh, regno));
175	case 4:
176		return (bus_space_read_4(fm801->st, fm801->sh, regno));
177	default:
178		return 0xffffffff;
179	}
180}
181
182static void
183fm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size)
184{
185	switch(size) {
186	case 1:
187		return bus_space_write_1(fm801->st, fm801->sh, regno, data);
188	case 2:
189		return bus_space_write_2(fm801->st, fm801->sh, regno, data);
190	case 4:
191		return bus_space_write_4(fm801->st, fm801->sh, regno, data);
192	default:
193		return;
194	}
195}
196
197/* -------------------------------------------------------------------- */
198/*
199 *  ac97 codec routines
200 */
201#define TIMO 50
202static int
203fm801_rdcd(kobj_t obj, void *devinfo, int regno)
204{
205	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
206	int i;
207
208	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
209		DELAY(10000);
210		DPRINT("fm801 rdcd: 1 - DELAY\n");
211	}
212	if (i >= TIMO) {
213		printf("fm801 rdcd: codec busy\n");
214		return 0;
215	}
216
217	fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2);
218
219	for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++)
220	{
221		DELAY(10000);
222		DPRINT("fm801 rdcd: 2 - DELAY\n");
223	}
224	if (i >= TIMO) {
225		printf("fm801 rdcd: write codec invalid\n");
226		return -1;
227	}
228
229	return fm801_rd(fm801,FM_CODEC_DATA,2);
230}
231
232static int
233fm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
234{
235	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
236	int i;
237
238	DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data);
239/*
240	if(regno == AC97_REG_RECSEL)	return;
241*/
242	/* Poll until codec is ready */
243	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
244		DELAY(10000);
245		DPRINT("fm801 rdcd: 1 - DELAY\n");
246	}
247	if (i >= TIMO) {
248		printf("fm801 wrcd: read codec busy\n");
249		return -1;
250	}
251
252	fm801_wr(fm801,FM_CODEC_DATA,data, 2);
253	fm801_wr(fm801,FM_CODEC_CMD, regno,2);
254
255	/* wait until codec is ready */
256	for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
257		DELAY(10000);
258		DPRINT("fm801 wrcd: 2 - DELAY\n");
259	}
260	if (i >= TIMO) {
261		printf("fm801 wrcd: read codec busy\n");
262		return -1;
263	}
264	DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data);
265	return 0;
266}
267
268static kobj_method_t fm801_ac97_methods[] = {
269    	KOBJMETHOD(ac97_read,		fm801_rdcd),
270    	KOBJMETHOD(ac97_write,		fm801_wrcd),
271	{ 0, 0 }
272};
273AC97_DECLARE(fm801_ac97);
274
275/* -------------------------------------------------------------------- */
276
277/*
278 * The interrupt handler
279 */
280static void
281fm801_intr(void *p)
282{
283	struct fm801_info 	*fm801 = (struct fm801_info *)p;
284	u_int32_t       	intsrc = fm801_rd(fm801, FM_INTSTATUS, 2);
285
286	DPRINT("\nfm801_intr intsrc 0x%x ", intsrc);
287
288	if(intsrc & FM_INTSTATUS_PLAY) {
289		fm801->play_flip++;
290		if(fm801->play_flip & 1) {
291			fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4);
292		} else
293			fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4);
294		chn_intr(fm801->pch.channel);
295	}
296
297	if(intsrc & FM_INTSTATUS_REC) {
298		fm801->rec_flip++;
299		if(fm801->rec_flip & 1) {
300			fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4);
301		} else
302			fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4);
303		chn_intr(fm801->rch.channel);
304	}
305
306	if ( intsrc & FM_INTSTATUS_MPU ) {
307		/* This is a TODOish thing... */
308		fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2);
309	}
310
311	if ( intsrc & FM_INTSTATUS_VOL ) {
312		/* This is a TODOish thing... */
313		fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2);
314	}
315
316	DPRINT("fm801_intr clear\n\n");
317	fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2);
318}
319
320/* -------------------------------------------------------------------- */
321/* channel interface */
322static void *
323fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
324{
325	struct fm801_info *fm801 = (struct fm801_info *)devinfo;
326	struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch;
327
328	DPRINT("fm801ch_init, direction = %d\n", dir);
329	ch->parent = fm801;
330	ch->channel = c;
331	ch->buffer = b;
332	ch->dir = dir;
333	if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, fm801->bufsz) == -1) return NULL;
334	return (void *)ch;
335}
336
337static int
338fm801ch_setformat(kobj_t obj, void *data, u_int32_t format)
339{
340	struct fm801_chinfo *ch = data;
341	struct fm801_info *fm801 = ch->parent;
342
343	DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format,
344		(format & AFMT_STEREO)?"stereo":"mono",
345		(format & (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)) ? "16bit":"8bit",
346		(format & AFMT_SIGNED)? "signed":"unsigned",
347		(format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" );
348
349	if(ch->dir == PCMDIR_PLAY) {
350		fm801->play_fmt =  (format & AFMT_STEREO)? FM_PLAY_STEREO : 0;
351		fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
352		return 0;
353	}
354
355	if(ch->dir == PCMDIR_REC ) {
356		fm801->rec_fmt = (format & AFMT_STEREO)? FM_REC_STEREO:0;
357		fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0;
358		return 0;
359	}
360
361	return 0;
362}
363
364struct {
365	int limit;
366	int rate;
367} fm801_rates[11] = {
368	{  6600,  5500 },
369	{  8750,  8000 },
370	{ 10250,  9600 },
371	{ 13200, 11025 },
372	{ 17500, 16000 },
373	{ 20500, 19200 },
374	{ 26500, 22050 },
375	{ 35000, 32000 },
376	{ 41000, 38400 },
377	{ 46000, 44100 },
378	{ 48000, 48000 },
379/* anything above -> 48000 */
380};
381
382static int
383fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed)
384{
385	struct fm801_chinfo *ch = data;
386	struct fm801_info *fm801 = ch->parent;
387	register int i;
388
389
390	for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ;
391
392	if(ch->dir == PCMDIR_PLAY) {
393		fm801->pch.spd = fm801_rates[i].rate;
394		fm801->play_shift = (i<<8);
395		fm801->play_shift &= FM_PLAY_RATE_MASK;
396	}
397
398	if(ch->dir == PCMDIR_REC ) {
399		fm801->rch.spd = fm801_rates[i].rate;
400		fm801->rec_shift = (i<<8);
401		fm801->rec_shift &= FM_REC_RATE_MASK;
402	}
403
404	ch->spd = fm801_rates[i].rate;
405
406	return fm801_rates[i].rate;
407}
408
409static int
410fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
411{
412	struct fm801_chinfo *ch = data;
413	struct fm801_info *fm801 = ch->parent;
414
415	if(ch->dir == PCMDIR_PLAY) {
416		if(fm801->play_flip) return fm801->play_blksize;
417		fm801->play_blksize = blocksize;
418	}
419
420	if(ch->dir == PCMDIR_REC) {
421		if(fm801->rec_flip) return fm801->rec_blksize;
422		fm801->rec_blksize = blocksize;
423	}
424
425	DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir);
426
427	return blocksize;
428}
429
430static int
431fm801ch_trigger(kobj_t obj, void *data, int go)
432{
433	struct fm801_chinfo *ch = data;
434	struct fm801_info *fm801 = ch->parent;
435	u_int32_t baseaddr = vtophys(sndbuf_getbuf(ch->buffer));
436	u_int32_t k1;
437
438	DPRINT("fm801ch_trigger go %d , ", go);
439
440	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) {
441		return 0;
442	}
443
444	if (ch->dir == PCMDIR_PLAY) {
445		if (go == PCMTRIG_START) {
446
447			fm801->play_start = baseaddr;
448			fm801->play_nextblk = fm801->play_start + fm801->play_blksize;
449			fm801->play_flip = 0;
450			fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2);
451			fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4);
452			fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4);
453			fm801_wr(fm801, FM_PLAY_CTL,
454					FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift,
455					2 );
456			} else {
457			fm801->play_flip = 0;
458			k1 = fm801_rd(fm801, FM_PLAY_CTL,2);
459			fm801_wr(fm801, FM_PLAY_CTL,
460				(k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) |
461				FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 );
462		}
463	} else if(ch->dir == PCMDIR_REC) {
464		if (go == PCMTRIG_START) {
465			fm801->rec_start = baseaddr;
466			fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize;
467			fm801->rec_flip = 0;
468			fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2);
469			fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4);
470			fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4);
471			fm801_wr(fm801, FM_REC_CTL,
472					FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift,
473					2 );
474			} else {
475			fm801->rec_flip = 0;
476			k1 = fm801_rd(fm801, FM_REC_CTL,2);
477			fm801_wr(fm801, FM_REC_CTL,
478				(k1 & ~(FM_REC_STOPNOW | FM_REC_START)) |
479				FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2);
480		}
481	}
482
483	return 0;
484}
485
486/* Almost ALSA copy */
487static int
488fm801ch_getptr(kobj_t obj, void *data)
489{
490	struct fm801_chinfo *ch = data;
491	struct fm801_info *fm801 = ch->parent;
492	int result = 0;
493
494	if (ch->dir == PCMDIR_PLAY) {
495		result = fm801_rd(fm801,
496			(fm801->play_flip&1) ?
497			FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start;
498	}
499
500	if (ch->dir == PCMDIR_REC) {
501		result = fm801_rd(fm801,
502			(fm801->rec_flip&1) ?
503			FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start;
504	}
505
506	return result;
507}
508
509static struct pcmchan_caps *
510fm801ch_getcaps(kobj_t obj, void *data)
511{
512	return &fm801ch_caps;
513}
514
515static kobj_method_t fm801ch_methods[] = {
516    	KOBJMETHOD(channel_init,		fm801ch_init),
517    	KOBJMETHOD(channel_setformat,		fm801ch_setformat),
518    	KOBJMETHOD(channel_setspeed,		fm801ch_setspeed),
519    	KOBJMETHOD(channel_setblocksize,	fm801ch_setblocksize),
520    	KOBJMETHOD(channel_trigger,		fm801ch_trigger),
521    	KOBJMETHOD(channel_getptr,		fm801ch_getptr),
522    	KOBJMETHOD(channel_getcaps,		fm801ch_getcaps),
523	{ 0, 0 }
524};
525CHANNEL_DECLARE(fm801ch);
526
527/* -------------------------------------------------------------------- */
528
529/*
530 *  Init routine is taken from an original NetBSD driver
531 */
532static int
533fm801_init(struct fm801_info *fm801)
534{
535	u_int32_t k1;
536
537	/* reset codec */
538	fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2);
539	DELAY(100000);
540	fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2);
541	DELAY(100000);
542
543	fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2);
544	fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2);
545	fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2);
546	fm801_wr(fm801, 0x40,0x107f,2);	/* enable legacy audio */
547
548	fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2);
549
550	/* Unmask playback, record and mpu interrupts, mask the rest */
551	k1 = fm801_rd((void *)fm801, FM_INTMASK,2);
552	fm801_wr(fm801, FM_INTMASK,
553		(k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) |
554		FM_INTMASK_VOL,2);
555	fm801_wr(fm801, FM_INTSTATUS,
556		FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU |
557		FM_INTSTATUS_VOL,2);
558
559	DPRINT("FM801 init Ok\n");
560	return 0;
561}
562
563static int
564fm801_pci_attach(device_t dev)
565{
566	u_int32_t 		data;
567	struct ac97_info 	*codec = 0;
568	struct fm801_info 	*fm801;
569	int 			i;
570	int 			mapped = 0;
571	char 			status[SND_STATUSLEN];
572
573	if ((fm801 = (struct fm801_info *)malloc(sizeof(*fm801), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
574		device_printf(dev, "cannot allocate softc\n");
575		return ENXIO;
576	}
577
578	fm801->type = pci_get_devid(dev);
579
580	data = pci_read_config(dev, PCIR_COMMAND, 2);
581	data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
582	pci_write_config(dev, PCIR_COMMAND, data, 2);
583	data = pci_read_config(dev, PCIR_COMMAND, 2);
584
585	for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) {
586		fm801->regid = PCIR_MAPS + i*4;
587		fm801->regtype = SYS_RES_MEMORY;
588		fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid,
589						0, ~0, 1, RF_ACTIVE);
590		if(!fm801->reg)
591		{
592			fm801->regtype = SYS_RES_IOPORT;
593			fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid,
594						0, ~0, 1, RF_ACTIVE);
595		}
596
597		if(fm801->reg) {
598			fm801->st = rman_get_bustag(fm801->reg);
599			fm801->sh = rman_get_bushandle(fm801->reg);
600			mapped++;
601		}
602	}
603
604	if (mapped == 0) {
605		device_printf(dev, "unable to map register space\n");
606		goto oops;
607	}
608
609	fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536);
610
611	fm801_init(fm801);
612
613	codec = AC97_CREATE(dev, fm801, fm801_ac97);
614	if (codec == NULL) goto oops;
615
616	/*
617	 * XXX: quick check that device actually has sound capabilities.
618	 * The problem is that some cards built around fm801 chip only
619	 * have radio tuner onboard, but no sound capabilities.
620	 */
621	if (fm801_rdcd(NULL, fm801, AC97_REG_POWER) == -1) 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	return 0;
654
655oops:
656	if (codec) ac97_destroy(codec);
657	if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
658	if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih);
659	if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
660	if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat);
661	free(fm801, M_DEVBUF);
662	return ENXIO;
663}
664
665static int
666fm801_pci_detach(device_t dev)
667{
668	int r;
669	struct fm801_info *fm801;
670
671	DPRINT("Forte Media FM801 detach\n");
672
673	r = pcm_unregister(dev);
674	if (r)
675		return r;
676
677	fm801 = pcm_getdevinfo(dev);
678	bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
679	bus_teardown_intr(dev, fm801->irq, fm801->ih);
680	bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
681	bus_dma_tag_destroy(fm801->parent_dmat);
682	free(fm801, M_DEVBUF);
683	return 0;
684}
685
686static int
687fm801_pci_probe( device_t dev )
688{
689	int id;
690	if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) {
691		device_set_desc(dev, "Forte Media FM801 Audio Controller");
692		return 0;
693	}
694/*
695	if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) {
696		device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)");
697		return ENXIO;
698	}
699*/
700	return ENXIO;
701}
702
703static device_method_t fm801_methods[] = {
704	/* Device interface */
705	DEVMETHOD(device_probe,		fm801_pci_probe),
706	DEVMETHOD(device_attach,	fm801_pci_attach),
707	DEVMETHOD(device_detach,	fm801_pci_detach),
708	{ 0, 0}
709};
710
711static driver_t fm801_driver = {
712	"pcm",
713	fm801_methods,
714	PCM_SOFTC_SIZE,
715};
716
717DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, 0, 0);
718MODULE_DEPEND(snd_fm801, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
719MODULE_VERSION(snd_fm801, 1);
720
721