kernel: Use NULL for DRIVER_MODULE()'s evh & arg (which are pointers).
[dragonfly.git] / sys / dev / sound / pci / fm801.c
CommitLineData
558a398b 1/*-
984263bc
MD
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.
1de703da 25 *
558a398b 26 * $FreeBSD: src/sys/dev/sound/pci/fm801.c,v 1.27.2.1 2006/01/10 01:01:24 ariff Exp $
984263bc
MD
27 */
28
29#include <dev/sound/pcm/sound.h>
30#include <dev/sound/pcm/ac97.h>
1f2de5d4
MD
31#include <bus/pci/pcireg.h>
32#include <bus/pci/pcivar.h>
984263bc 33
558a398b 34SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/fm801.c,v 1.10 2007/01/04 21:47:02 corecode Exp $");
984263bc
MD
35
36#define PCI_VENDOR_FORTEMEDIA 0x1319
37#define PCI_DEVICE_FORTEMEDIA1 0x08011319
38#define PCI_DEVICE_FORTEMEDIA2 0x08021319 /* ??? have no idea what's this... */
39
40#define FM_PCM_VOLUME 0x00
41#define FM_FM_VOLUME 0x02
42#define FM_I2S_VOLUME 0x04
43#define FM_RECORD_SOURCE 0x06
44
45#define FM_PLAY_CTL 0x08
46#define FM_PLAY_RATE_MASK 0x0f00
47#define FM_PLAY_BUF1_LAST 0x0001
48#define FM_PLAY_BUF2_LAST 0x0002
49#define FM_PLAY_START 0x0020
50#define FM_PLAY_PAUSE 0x0040
51#define FM_PLAY_STOPNOW 0x0080
52#define FM_PLAY_16BIT 0x4000
53#define FM_PLAY_STEREO 0x8000
54
55#define FM_PLAY_DMALEN 0x0a
56#define FM_PLAY_DMABUF1 0x0c
57#define FM_PLAY_DMABUF2 0x10
58
59
60#define FM_REC_CTL 0x14
61#define FM_REC_RATE_MASK 0x0f00
62#define FM_REC_BUF1_LAST 0x0001
63#define FM_REC_BUF2_LAST 0x0002
64#define FM_REC_START 0x0020
65#define FM_REC_PAUSE 0x0040
66#define FM_REC_STOPNOW 0x0080
67#define FM_REC_16BIT 0x4000
68#define FM_REC_STEREO 0x8000
69
70
71#define FM_REC_DMALEN 0x16
72#define FM_REC_DMABUF1 0x18
73#define FM_REC_DMABUF2 0x1c
74
75#define FM_CODEC_CTL 0x22
76#define FM_VOLUME 0x26
77#define FM_VOLUME_MUTE 0x8000
78
79#define FM_CODEC_CMD 0x2a
80#define FM_CODEC_CMD_READ 0x0080
81#define FM_CODEC_CMD_VALID 0x0100
82#define FM_CODEC_CMD_BUSY 0x0200
83
84#define FM_CODEC_DATA 0x2c
85
86#define FM_IO_CTL 0x52
87#define FM_CARD_CTL 0x54
88
89#define FM_INTMASK 0x56
90#define FM_INTMASK_PLAY 0x0001
91#define FM_INTMASK_REC 0x0002
92#define FM_INTMASK_VOL 0x0040
93#define FM_INTMASK_MPU 0x0080
94
95#define FM_INTSTATUS 0x5a
96#define FM_INTSTATUS_PLAY 0x0100
97#define FM_INTSTATUS_REC 0x0200
98#define FM_INTSTATUS_VOL 0x4000
99#define FM_INTSTATUS_MPU 0x8000
100
101#define FM801_DEFAULT_BUFSZ 4096 /* Other values do not work!!! */
102
103/* debug purposes */
e3869ec7 104#define DPRINT if(0) kprintf
984263bc
MD
105
106/*
107static int fm801ch_setup(struct pcm_channel *c);
108*/
109
110static u_int32_t fmts[] = {
111 AFMT_U8,
112 AFMT_STEREO | AFMT_U8,
113 AFMT_S16_LE,
114 AFMT_STEREO | AFMT_S16_LE,
115 0
116};
117
118static struct pcmchan_caps fm801ch_caps = {
558a398b 119 5500, 48000,
984263bc
MD
120 fmts, 0
121};
122
123struct fm801_info;
124
125struct fm801_chinfo {
558a398b
SS
126 struct fm801_info *parent;
127 struct pcm_channel *channel;
128 struct snd_dbuf *buffer;
129 u_int32_t spd, dir, fmt; /* speed, direction, format */
984263bc
MD
130 u_int32_t shift;
131};
132
133struct fm801_info {
558a398b
SS
134 int type;
135 bus_space_tag_t st;
136 bus_space_handle_t sh;
137 bus_dma_tag_t parent_dmat;
984263bc 138
558a398b
SS
139 device_t dev;
140 int num;
141 u_int32_t unit;
984263bc 142
558a398b
SS
143 struct resource *reg, *irq;
144 int regtype, regid, irqid;
145 void *ih;
984263bc
MD
146
147 u_int32_t play_flip,
148 play_nextblk,
149 play_start,
150 play_blksize,
151 play_fmt,
152 play_shift,
153 play_size;
154
155 u_int32_t rec_flip,
156 rec_nextblk,
157 rec_start,
158 rec_blksize,
159 rec_fmt,
160 rec_shift,
161 rec_size;
162
558a398b 163 unsigned int bufsz;
984263bc
MD
164
165 struct fm801_chinfo pch, rch;
166
167 device_t radio;
168};
169
170/* Bus Read / Write routines */
171static u_int32_t
172fm801_rd(struct fm801_info *fm801, int regno, int size)
173{
174 switch(size) {
175 case 1:
176 return (bus_space_read_1(fm801->st, fm801->sh, regno));
177 case 2:
178 return (bus_space_read_2(fm801->st, fm801->sh, regno));
179 case 4:
180 return (bus_space_read_4(fm801->st, fm801->sh, regno));
181 default:
182 return 0xffffffff;
183 }
184}
185
186static void
187fm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size)
188{
189
190 switch(size) {
191 case 1:
192 bus_space_write_1(fm801->st, fm801->sh, regno, data);
193 break;
194 case 2:
195 bus_space_write_2(fm801->st, fm801->sh, regno, data);
196 break;
197 case 4:
198 bus_space_write_4(fm801->st, fm801->sh, regno, data);
199 break;
200 }
201}
202
203/* -------------------------------------------------------------------- */
204/*
205 * ac97 codec routines
206 */
207#define TIMO 50
208static int
209fm801_rdcd(kobj_t obj, void *devinfo, int regno)
210{
211 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
212 int i;
213
214 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
215 DELAY(10000);
216 DPRINT("fm801 rdcd: 1 - DELAY\n");
217 }
218 if (i >= TIMO) {
e3869ec7 219 kprintf("fm801 rdcd: codec busy\n");
984263bc
MD
220 return 0;
221 }
222
223 fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2);
224
225 for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++)
226 {
227 DELAY(10000);
228 DPRINT("fm801 rdcd: 2 - DELAY\n");
229 }
230 if (i >= TIMO) {
e3869ec7 231 kprintf("fm801 rdcd: write codec invalid\n");
984263bc
MD
232 return 0;
233 }
234
235 return fm801_rd(fm801,FM_CODEC_DATA,2);
236}
237
238static int
239fm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
240{
241 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
242 int i;
243
244 DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data);
245/*
246 if(regno == AC97_REG_RECSEL) return;
247*/
248 /* Poll until codec is ready */
249 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
250 DELAY(10000);
251 DPRINT("fm801 rdcd: 1 - DELAY\n");
252 }
253 if (i >= TIMO) {
e3869ec7 254 kprintf("fm801 wrcd: read codec busy\n");
984263bc
MD
255 return -1;
256 }
257
258 fm801_wr(fm801,FM_CODEC_DATA,data, 2);
259 fm801_wr(fm801,FM_CODEC_CMD, regno,2);
260
261 /* wait until codec is ready */
262 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) {
263 DELAY(10000);
264 DPRINT("fm801 wrcd: 2 - DELAY\n");
265 }
266 if (i >= TIMO) {
e3869ec7 267 kprintf("fm801 wrcd: read codec busy\n");
984263bc
MD
268 return -1;
269 }
270 DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data);
271 return 0;
272}
273
274static kobj_method_t fm801_ac97_methods[] = {
275 KOBJMETHOD(ac97_read, fm801_rdcd),
276 KOBJMETHOD(ac97_write, fm801_wrcd),
277 { 0, 0 }
278};
279AC97_DECLARE(fm801_ac97);
280
281/* -------------------------------------------------------------------- */
282
283/*
284 * The interrupt handler
285 */
286static void
287fm801_intr(void *p)
288{
289 struct fm801_info *fm801 = (struct fm801_info *)p;
290 u_int32_t intsrc = fm801_rd(fm801, FM_INTSTATUS, 2);
291
292 DPRINT("\nfm801_intr intsrc 0x%x ", intsrc);
293
294 if(intsrc & FM_INTSTATUS_PLAY) {
295 fm801->play_flip++;
296 if(fm801->play_flip & 1) {
297 fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4);
298 } else
299 fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4);
300 chn_intr(fm801->pch.channel);
301 }
302
303 if(intsrc & FM_INTSTATUS_REC) {
304 fm801->rec_flip++;
305 if(fm801->rec_flip & 1) {
306 fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4);
307 } else
308 fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4);
309 chn_intr(fm801->rch.channel);
310 }
311
312 if ( intsrc & FM_INTSTATUS_MPU ) {
313 /* This is a TODOish thing... */
314 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2);
315 }
316
317 if ( intsrc & FM_INTSTATUS_VOL ) {
318 /* This is a TODOish thing... */
319 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2);
320 }
321
322 DPRINT("fm801_intr clear\n\n");
323 fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2);
324}
325
326/* -------------------------------------------------------------------- */
327/* channel interface */
328static void *
329fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
330{
331 struct fm801_info *fm801 = (struct fm801_info *)devinfo;
332 struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch;
333
334 DPRINT("fm801ch_init, direction = %d\n", dir);
335 ch->parent = fm801;
336 ch->channel = c;
337 ch->buffer = b;
338 ch->dir = dir;
558a398b
SS
339 if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, fm801->bufsz) != 0)
340 return NULL;
984263bc
MD
341 return (void *)ch;
342}
343
344static int
345fm801ch_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
371struct {
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
389static int
390fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed)
391{
392 struct fm801_chinfo *ch = data;
393 struct fm801_info *fm801 = ch->parent;
558a398b 394 register int i;
984263bc
MD
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
416static int
417fm801ch_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
437static int
438fm801ch_trigger(kobj_t obj, void *data, int go)
439{
440 struct fm801_chinfo *ch = data;
441 struct fm801_info *fm801 = ch->parent;
558a398b 442 u_int32_t baseaddr = sndbuf_getbufaddr(ch->buffer);
984263bc
MD
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 */
494static int
495fm801ch_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
516static struct pcmchan_caps *
517fm801ch_getcaps(kobj_t obj, void *data)
518{
519 return &fm801ch_caps;
520}
521
522static 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};
532CHANNEL_DECLARE(fm801ch);
533
534/* -------------------------------------------------------------------- */
535
536/*
537 * Init routine is taken from an original NetBSD driver
538 */
539static int
540fm801_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
570static int
571fm801_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
7522187f 580 fm801 = kmalloc(sizeof(*fm801), M_DEVBUF, M_WAITOK | M_ZERO);
984263bc
MD
581 fm801->type = pci_get_devid(dev);
582
583 data = pci_read_config(dev, PCIR_COMMAND, 2);
584 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
585 pci_write_config(dev, PCIR_COMMAND, data, 2);
586 data = pci_read_config(dev, PCIR_COMMAND, 2);
587
588 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) {
558a398b 589 fm801->regid = PCIR_BAR(i);
984263bc 590 fm801->regtype = SYS_RES_MEMORY;
558a398b
SS
591 fm801->reg = bus_alloc_resource_any(dev, fm801->regtype,
592 &fm801->regid, RF_ACTIVE);
984263bc
MD
593 if(!fm801->reg)
594 {
595 fm801->regtype = SYS_RES_IOPORT;
558a398b
SS
596 fm801->reg = bus_alloc_resource_any(dev,
597 fm801->regtype,
598 &fm801->regid,
599 RF_ACTIVE);
984263bc
MD
600 }
601
602 if(fm801->reg) {
603 fm801->st = rman_get_bustag(fm801->reg);
604 fm801->sh = rman_get_bushandle(fm801->reg);
605 mapped++;
606 }
607 }
608
609 if (mapped == 0) {
610 device_printf(dev, "unable to map register space\n");
611 goto oops;
612 }
613
614 fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536);
615
616 fm801_init(fm801);
617
618 codec = AC97_CREATE(dev, fm801, fm801_ac97);
619 if (codec == NULL) goto oops;
620
621 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops;
622
623 fm801->irqid = 0;
558a398b
SS
624 fm801->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fm801->irqid,
625 RF_ACTIVE | RF_SHAREABLE);
626 if (!fm801->irq || snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) {
984263bc
MD
627 device_printf(dev, "unable to map interrupt\n");
628 goto oops;
629 }
630
631 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
632 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
633 /*highaddr*/BUS_SPACE_MAXADDR,
634 /*filter*/NULL, /*filterarg*/NULL,
635 /*maxsize*/fm801->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
558a398b
SS
636 /*flags*/0,
637 &fm801->parent_dmat) != 0) {
984263bc
MD
638 device_printf(dev, "unable to create dma tag\n");
639 goto oops;
640 }
641
558a398b 642 ksnprintf(status, 64, "at %s 0x%lx irq %ld %s",
984263bc 643 (fm801->regtype == SYS_RES_IOPORT)? "io" : "memory",
558a398b 644 rman_get_start(fm801->reg), rman_get_start(fm801->irq),PCM_KLDSTRING(snd_fm801));
984263bc
MD
645
646#define FM801_MAXPLAYCH 1
647 if (pcm_register(dev, fm801, FM801_MAXPLAYCH, 1)) goto oops;
648 pcm_addchan(dev, PCMDIR_PLAY, &fm801ch_class, fm801);
649 pcm_addchan(dev, PCMDIR_REC, &fm801ch_class, fm801);
650 pcm_setstatus(dev, status);
651
652 fm801->radio = device_add_child(dev, "radio", -1);
653 bus_generic_attach(dev);
654
655 return 0;
656
657oops:
658 if (codec) ac97_destroy(codec);
659 if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
660 if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih);
661 if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
662 if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat);
efda3bd0 663 kfree(fm801, M_DEVBUF);
984263bc
MD
664 return ENXIO;
665}
666
667static int
668fm801_pci_detach(device_t dev)
669{
670 int r;
671 struct fm801_info *fm801;
672
673 DPRINT("Forte Media FM801 detach\n");
674
675 fm801 = pcm_getdevinfo(dev);
676
677 r = bus_generic_detach(dev);
678 if (r)
679 return r;
680 if (fm801->radio != NULL) {
681 r = device_delete_child(dev, fm801->radio);
682 if (r)
683 return r;
684 fm801->radio = NULL;
685 }
686
687 r = pcm_unregister(dev);
688 if (r)
689 return r;
690
691 bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg);
692 bus_teardown_intr(dev, fm801->irq, fm801->ih);
693 bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq);
694 bus_dma_tag_destroy(fm801->parent_dmat);
efda3bd0 695 kfree(fm801, M_DEVBUF);
984263bc
MD
696 return 0;
697}
698
699static int
700fm801_pci_probe( device_t dev )
701{
558a398b 702 int id;
984263bc 703
558a398b
SS
704 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) {
705 device_set_desc(dev, "Forte Media FM801 Audio Controller");
706 return BUS_PROBE_DEFAULT;
984263bc
MD
707 }
708/*
709 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) {
710 device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)");
711 return ENXIO;
712 }
713*/
558a398b 714 return ENXIO;
984263bc
MD
715}
716
717static struct resource *
718fm801_alloc_resource(device_t bus, device_t child, int type, int *rid,
719 u_long start, u_long end, u_long count, u_int flags)
720{
721 struct fm801_info *fm801;
722
723 fm801 = pcm_getdevinfo(bus);
724
558a398b 725 if (type == SYS_RES_IOPORT && *rid == PCIR_BAR(0))
984263bc
MD
726 return (fm801->reg);
727
728 return (NULL);
729}
730
731static int
732fm801_release_resource(device_t bus, device_t child, int type, int rid,
733 struct resource *r)
734{
735 return (0);
736}
737
738static device_method_t fm801_methods[] = {
739 /* Device interface */
740 DEVMETHOD(device_probe, fm801_pci_probe),
741 DEVMETHOD(device_attach, fm801_pci_attach),
742 DEVMETHOD(device_detach, fm801_pci_detach),
743 DEVMETHOD(device_shutdown, bus_generic_shutdown),
744 DEVMETHOD(device_suspend, bus_generic_suspend),
745 DEVMETHOD(device_resume, bus_generic_resume),
746
747 /* Bus interface */
748 DEVMETHOD(bus_print_child, bus_generic_print_child),
749 DEVMETHOD(bus_alloc_resource, fm801_alloc_resource),
750 DEVMETHOD(bus_release_resource, fm801_release_resource),
751 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
752 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
753 { 0, 0}
754};
755
756static driver_t fm801_driver = {
757 "pcm",
758 fm801_methods,
759 PCM_SOFTC_SIZE,
760};
761
aa2b9d05 762DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, NULL, NULL);
558a398b 763MODULE_DEPEND(snd_fm801, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
984263bc 764MODULE_VERSION(snd_fm801, 1);