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 32 SND_DECLARE_FILE("$FreeBSD$"); 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 /* 105 static int fm801ch_setup(struct pcm_channel *c); 106 */ 107 108 static 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 116 static struct pcmchan_caps fm801ch_caps = { 117 4000, 48000, 118 fmts, 0 119 }; 120 121 struct fm801_info; 122 123 struct 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 131 struct 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 device_t radio; 166 }; 167 168 /* Bus Read / Write routines */ 169 static u_int32_t 170 fm801_rd(struct fm801_info *fm801, int regno, int size) 171 { 172 switch(size) { 173 case 1: 174 return (bus_space_read_1(fm801->st, fm801->sh, regno)); 175 case 2: 176 return (bus_space_read_2(fm801->st, fm801->sh, regno)); 177 case 4: 178 return (bus_space_read_4(fm801->st, fm801->sh, regno)); 179 default: 180 return 0xffffffff; 181 } 182 } 183 184 static void 185 fm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size) 186 { 187 switch(size) { 188 case 1: 189 return bus_space_write_1(fm801->st, fm801->sh, regno, data); 190 case 2: 191 return bus_space_write_2(fm801->st, fm801->sh, regno, data); 192 case 4: 193 return bus_space_write_4(fm801->st, fm801->sh, regno, data); 194 default: 195 return; 196 } 197 } 198 199 /* -------------------------------------------------------------------- */ 200 /* 201 * ac97 codec routines 202 */ 203 #define TIMO 50 204 static int 205 fm801_rdcd(kobj_t obj, void *devinfo, int regno) 206 { 207 struct fm801_info *fm801 = (struct fm801_info *)devinfo; 208 int i; 209 210 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 211 DELAY(10000); 212 DPRINT("fm801 rdcd: 1 - DELAY\n"); 213 } 214 if (i >= TIMO) { 215 printf("fm801 rdcd: codec busy\n"); 216 return 0; 217 } 218 219 fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2); 220 221 for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++) 222 { 223 DELAY(10000); 224 DPRINT("fm801 rdcd: 2 - DELAY\n"); 225 } 226 if (i >= TIMO) { 227 printf("fm801 rdcd: write codec invalid\n"); 228 return 0; 229 } 230 231 return fm801_rd(fm801,FM_CODEC_DATA,2); 232 } 233 234 static int 235 fm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data) 236 { 237 struct fm801_info *fm801 = (struct fm801_info *)devinfo; 238 int i; 239 240 DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data); 241 /* 242 if(regno == AC97_REG_RECSEL) return; 243 */ 244 /* Poll until codec is ready */ 245 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 246 DELAY(10000); 247 DPRINT("fm801 rdcd: 1 - DELAY\n"); 248 } 249 if (i >= TIMO) { 250 printf("fm801 wrcd: read codec busy\n"); 251 return -1; 252 } 253 254 fm801_wr(fm801,FM_CODEC_DATA,data, 2); 255 fm801_wr(fm801,FM_CODEC_CMD, regno,2); 256 257 /* wait until codec is ready */ 258 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 259 DELAY(10000); 260 DPRINT("fm801 wrcd: 2 - DELAY\n"); 261 } 262 if (i >= TIMO) { 263 printf("fm801 wrcd: read codec busy\n"); 264 return -1; 265 } 266 DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data); 267 return 0; 268 } 269 270 static kobj_method_t fm801_ac97_methods[] = { 271 KOBJMETHOD(ac97_read, fm801_rdcd), 272 KOBJMETHOD(ac97_write, fm801_wrcd), 273 { 0, 0 } 274 }; 275 AC97_DECLARE(fm801_ac97); 276 277 /* -------------------------------------------------------------------- */ 278 279 /* 280 * The interrupt handler 281 */ 282 static void 283 fm801_intr(void *p) 284 { 285 struct fm801_info *fm801 = (struct fm801_info *)p; 286 u_int32_t intsrc = fm801_rd(fm801, FM_INTSTATUS, 2); 287 288 DPRINT("\nfm801_intr intsrc 0x%x ", intsrc); 289 290 if(intsrc & FM_INTSTATUS_PLAY) { 291 fm801->play_flip++; 292 if(fm801->play_flip & 1) { 293 fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4); 294 } else 295 fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4); 296 chn_intr(fm801->pch.channel); 297 } 298 299 if(intsrc & FM_INTSTATUS_REC) { 300 fm801->rec_flip++; 301 if(fm801->rec_flip & 1) { 302 fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4); 303 } else 304 fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4); 305 chn_intr(fm801->rch.channel); 306 } 307 308 if ( intsrc & FM_INTSTATUS_MPU ) { 309 /* This is a TODOish thing... */ 310 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2); 311 } 312 313 if ( intsrc & FM_INTSTATUS_VOL ) { 314 /* This is a TODOish thing... */ 315 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2); 316 } 317 318 DPRINT("fm801_intr clear\n\n"); 319 fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2); 320 } 321 322 /* -------------------------------------------------------------------- */ 323 /* channel interface */ 324 static void * 325 fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 326 { 327 struct fm801_info *fm801 = (struct fm801_info *)devinfo; 328 struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch; 329 330 DPRINT("fm801ch_init, direction = %d\n", dir); 331 ch->parent = fm801; 332 ch->channel = c; 333 ch->buffer = b; 334 ch->dir = dir; 335 if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, fm801->bufsz) == -1) return NULL; 336 return (void *)ch; 337 } 338 339 static int 340 fm801ch_setformat(kobj_t obj, void *data, u_int32_t format) 341 { 342 struct fm801_chinfo *ch = data; 343 struct fm801_info *fm801 = ch->parent; 344 345 DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format, 346 (format & AFMT_STEREO)?"stereo":"mono", 347 (format & (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)) ? "16bit":"8bit", 348 (format & AFMT_SIGNED)? "signed":"unsigned", 349 (format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" ); 350 351 if(ch->dir == PCMDIR_PLAY) { 352 fm801->play_fmt = (format & AFMT_STEREO)? FM_PLAY_STEREO : 0; 353 fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 354 return 0; 355 } 356 357 if(ch->dir == PCMDIR_REC ) { 358 fm801->rec_fmt = (format & AFMT_STEREO)? FM_REC_STEREO:0; 359 fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 360 return 0; 361 } 362 363 return 0; 364 } 365 366 struct { 367 int limit; 368 int rate; 369 } fm801_rates[11] = { 370 { 6600, 5500 }, 371 { 8750, 8000 }, 372 { 10250, 9600 }, 373 { 13200, 11025 }, 374 { 17500, 16000 }, 375 { 20500, 19200 }, 376 { 26500, 22050 }, 377 { 35000, 32000 }, 378 { 41000, 38400 }, 379 { 46000, 44100 }, 380 { 48000, 48000 }, 381 /* anything above -> 48000 */ 382 }; 383 384 static int 385 fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed) 386 { 387 struct fm801_chinfo *ch = data; 388 struct fm801_info *fm801 = ch->parent; 389 register int i; 390 391 392 for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ; 393 394 if(ch->dir == PCMDIR_PLAY) { 395 fm801->pch.spd = fm801_rates[i].rate; 396 fm801->play_shift = (i<<8); 397 fm801->play_shift &= FM_PLAY_RATE_MASK; 398 } 399 400 if(ch->dir == PCMDIR_REC ) { 401 fm801->rch.spd = fm801_rates[i].rate; 402 fm801->rec_shift = (i<<8); 403 fm801->rec_shift &= FM_REC_RATE_MASK; 404 } 405 406 ch->spd = fm801_rates[i].rate; 407 408 return fm801_rates[i].rate; 409 } 410 411 static int 412 fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 413 { 414 struct fm801_chinfo *ch = data; 415 struct fm801_info *fm801 = ch->parent; 416 417 if(ch->dir == PCMDIR_PLAY) { 418 if(fm801->play_flip) return fm801->play_blksize; 419 fm801->play_blksize = blocksize; 420 } 421 422 if(ch->dir == PCMDIR_REC) { 423 if(fm801->rec_flip) return fm801->rec_blksize; 424 fm801->rec_blksize = blocksize; 425 } 426 427 DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir); 428 429 return blocksize; 430 } 431 432 static int 433 fm801ch_trigger(kobj_t obj, void *data, int go) 434 { 435 struct fm801_chinfo *ch = data; 436 struct fm801_info *fm801 = ch->parent; 437 u_int32_t baseaddr = vtophys(sndbuf_getbuf(ch->buffer)); 438 u_int32_t k1; 439 440 DPRINT("fm801ch_trigger go %d , ", go); 441 442 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD) { 443 return 0; 444 } 445 446 if (ch->dir == PCMDIR_PLAY) { 447 if (go == PCMTRIG_START) { 448 449 fm801->play_start = baseaddr; 450 fm801->play_nextblk = fm801->play_start + fm801->play_blksize; 451 fm801->play_flip = 0; 452 fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2); 453 fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4); 454 fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4); 455 fm801_wr(fm801, FM_PLAY_CTL, 456 FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift, 457 2 ); 458 } else { 459 fm801->play_flip = 0; 460 k1 = fm801_rd(fm801, FM_PLAY_CTL,2); 461 fm801_wr(fm801, FM_PLAY_CTL, 462 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | 463 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 ); 464 } 465 } else if(ch->dir == PCMDIR_REC) { 466 if (go == PCMTRIG_START) { 467 fm801->rec_start = baseaddr; 468 fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize; 469 fm801->rec_flip = 0; 470 fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2); 471 fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4); 472 fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4); 473 fm801_wr(fm801, FM_REC_CTL, 474 FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift, 475 2 ); 476 } else { 477 fm801->rec_flip = 0; 478 k1 = fm801_rd(fm801, FM_REC_CTL,2); 479 fm801_wr(fm801, FM_REC_CTL, 480 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | 481 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2); 482 } 483 } 484 485 return 0; 486 } 487 488 /* Almost ALSA copy */ 489 static int 490 fm801ch_getptr(kobj_t obj, void *data) 491 { 492 struct fm801_chinfo *ch = data; 493 struct fm801_info *fm801 = ch->parent; 494 int result = 0; 495 496 if (ch->dir == PCMDIR_PLAY) { 497 result = fm801_rd(fm801, 498 (fm801->play_flip&1) ? 499 FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start; 500 } 501 502 if (ch->dir == PCMDIR_REC) { 503 result = fm801_rd(fm801, 504 (fm801->rec_flip&1) ? 505 FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start; 506 } 507 508 return result; 509 } 510 511 static struct pcmchan_caps * 512 fm801ch_getcaps(kobj_t obj, void *data) 513 { 514 return &fm801ch_caps; 515 } 516 517 static kobj_method_t fm801ch_methods[] = { 518 KOBJMETHOD(channel_init, fm801ch_init), 519 KOBJMETHOD(channel_setformat, fm801ch_setformat), 520 KOBJMETHOD(channel_setspeed, fm801ch_setspeed), 521 KOBJMETHOD(channel_setblocksize, fm801ch_setblocksize), 522 KOBJMETHOD(channel_trigger, fm801ch_trigger), 523 KOBJMETHOD(channel_getptr, fm801ch_getptr), 524 KOBJMETHOD(channel_getcaps, fm801ch_getcaps), 525 { 0, 0 } 526 }; 527 CHANNEL_DECLARE(fm801ch); 528 529 /* -------------------------------------------------------------------- */ 530 531 /* 532 * Init routine is taken from an original NetBSD driver 533 */ 534 static int 535 fm801_init(struct fm801_info *fm801) 536 { 537 u_int32_t k1; 538 539 /* reset codec */ 540 fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2); 541 DELAY(100000); 542 fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2); 543 DELAY(100000); 544 545 fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2); 546 fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2); 547 fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2); 548 fm801_wr(fm801, 0x40,0x107f,2); /* enable legacy audio */ 549 550 fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2); 551 552 /* Unmask playback, record and mpu interrupts, mask the rest */ 553 k1 = fm801_rd((void *)fm801, FM_INTMASK,2); 554 fm801_wr(fm801, FM_INTMASK, 555 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) | 556 FM_INTMASK_VOL,2); 557 fm801_wr(fm801, FM_INTSTATUS, 558 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU | 559 FM_INTSTATUS_VOL,2); 560 561 DPRINT("FM801 init Ok\n"); 562 return 0; 563 } 564 565 static int 566 fm801_pci_attach(device_t dev) 567 { 568 u_int32_t data; 569 struct ac97_info *codec = 0; 570 struct fm801_info *fm801; 571 int i; 572 int mapped = 0; 573 char status[SND_STATUSLEN]; 574 575 if ((fm801 = (struct fm801_info *)malloc(sizeof(*fm801), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) { 576 device_printf(dev, "cannot allocate softc\n"); 577 return ENXIO; 578 } 579 580 fm801->type = pci_get_devid(dev); 581 582 data = pci_read_config(dev, PCIR_COMMAND, 2); 583 data |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN); 584 pci_write_config(dev, PCIR_COMMAND, data, 2); 585 data = pci_read_config(dev, PCIR_COMMAND, 2); 586 587 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) { 588 fm801->regid = PCIR_MAPS + i*4; 589 fm801->regtype = SYS_RES_MEMORY; 590 fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid, 591 0, ~0, 1, RF_ACTIVE); 592 if(!fm801->reg) 593 { 594 fm801->regtype = SYS_RES_IOPORT; 595 fm801->reg = bus_alloc_resource(dev, fm801->regtype, &fm801->regid, 596 0, ~0, 1, RF_ACTIVE); 597 } 598 599 if(fm801->reg) { 600 fm801->st = rman_get_bustag(fm801->reg); 601 fm801->sh = rman_get_bushandle(fm801->reg); 602 mapped++; 603 } 604 } 605 606 if (mapped == 0) { 607 device_printf(dev, "unable to map register space\n"); 608 goto oops; 609 } 610 611 fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536); 612 613 fm801_init(fm801); 614 615 codec = AC97_CREATE(dev, fm801, fm801_ac97); 616 if (codec == NULL) goto oops; 617 618 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops; 619 620 fm801->irqid = 0; 621 fm801->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &fm801->irqid, 622 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE); 623 if (!fm801->irq || snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) { 624 device_printf(dev, "unable to map interrupt\n"); 625 goto oops; 626 } 627 628 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0, 629 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT, 630 /*highaddr*/BUS_SPACE_MAXADDR, 631 /*filter*/NULL, /*filterarg*/NULL, 632 /*maxsize*/fm801->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff, 633 /*flags*/0, &fm801->parent_dmat) != 0) { 634 device_printf(dev, "unable to create dma tag\n"); 635 goto oops; 636 } 637 638 snprintf(status, 64, "at %s 0x%lx irq %ld", 639 (fm801->regtype == SYS_RES_IOPORT)? "io" : "memory", 640 rman_get_start(fm801->reg), rman_get_start(fm801->irq)); 641 642 #define FM801_MAXPLAYCH 1 643 if (pcm_register(dev, fm801, FM801_MAXPLAYCH, 1)) goto oops; 644 pcm_addchan(dev, PCMDIR_PLAY, &fm801ch_class, fm801); 645 pcm_addchan(dev, PCMDIR_REC, &fm801ch_class, fm801); 646 pcm_setstatus(dev, status); 647 648 fm801->radio = device_add_child(dev, "radio", -1); 649 bus_generic_attach(dev); 650 651 return 0; 652 653 oops: 654 if (codec) ac97_destroy(codec); 655 if (fm801->reg) bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg); 656 if (fm801->ih) bus_teardown_intr(dev, fm801->irq, fm801->ih); 657 if (fm801->irq) bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq); 658 if (fm801->parent_dmat) bus_dma_tag_destroy(fm801->parent_dmat); 659 free(fm801, M_DEVBUF); 660 return ENXIO; 661 } 662 663 static int 664 fm801_pci_detach(device_t dev) 665 { 666 int r; 667 struct fm801_info *fm801; 668 669 DPRINT("Forte Media FM801 detach\n"); 670 671 fm801 = pcm_getdevinfo(dev); 672 673 r = bus_generic_detach(dev); 674 if (r) 675 return r; 676 if (fm801->radio != NULL) { 677 r = device_delete_child(dev, fm801->radio); 678 if (r) 679 return r; 680 fm801->radio = NULL; 681 } 682 683 r = pcm_unregister(dev); 684 if (r) 685 return r; 686 687 bus_release_resource(dev, fm801->regtype, fm801->regid, fm801->reg); 688 bus_teardown_intr(dev, fm801->irq, fm801->ih); 689 bus_release_resource(dev, SYS_RES_IRQ, fm801->irqid, fm801->irq); 690 bus_dma_tag_destroy(fm801->parent_dmat); 691 free(fm801, M_DEVBUF); 692 return 0; 693 } 694 695 static int 696 fm801_pci_probe( device_t dev ) 697 { 698 u_int32_t data; 699 int id, regtype, regid, result; 700 struct resource *reg; 701 bus_space_tag_t st; 702 bus_space_handle_t sh; 703 704 result = ENXIO; 705 706 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA1 ) { 707 data = pci_read_config(dev, PCIR_COMMAND, 2); 708 data |= (PCIM_CMD_PORTEN|PCIM_CMD_BUSMASTEREN); 709 pci_write_config(dev, PCIR_COMMAND, data, 2); 710 data = pci_read_config(dev, PCIR_COMMAND, 2); 711 712 regid = PCIR_MAPS; 713 regtype = SYS_RES_IOPORT; 714 reg = bus_alloc_resource(dev, regtype, ®id, 0, ~0, 1, 715 RF_ACTIVE); 716 717 if (reg == NULL) 718 return ENXIO; 719 720 st = rman_get_bustag(reg); 721 sh = rman_get_bushandle(reg); 722 /* 723 * XXX: quick check that device actually has sound capabilities. 724 * The problem is that some cards built around FM801 chip only 725 * have radio tuner onboard, but no sound capabilities. There 726 * is no "official" way to quickly check this, because all 727 * IDs are exactly the same. The only difference is 0x28 728 * device control register, described in FM801 specification 729 * as "SRC/Mixer Test Control/DFC Status", but without 730 * any more detailed explanation. According to specs, and 731 * available sample cards (SF256-PCP-R and SF256-PCS-R) its 732 * power-on value should be `0', while on AC97-less tuner 733 * card (SF64-PCR) it was 0x80. 734 */ 735 if (bus_space_read_1(st, sh, 0x28) == 0) { 736 device_set_desc(dev, 737 "Forte Media FM801 Audio Controller"); 738 result = 0; 739 } 740 741 bus_release_resource(dev, regtype, regid, reg); 742 } 743 /* 744 if ((id = pci_get_devid(dev)) == PCI_DEVICE_FORTEMEDIA2 ) { 745 device_set_desc(dev, "Forte Media FM801 Joystick (Not Supported)"); 746 return ENXIO; 747 } 748 */ 749 return (result); 750 } 751 752 static struct resource * 753 fm801_alloc_resource(device_t bus, device_t child, int type, int *rid, 754 u_long start, u_long end, u_long count, u_int flags) 755 { 756 struct fm801_info *fm801; 757 758 fm801 = pcm_getdevinfo(bus); 759 760 if (type == SYS_RES_IOPORT && *rid == PCIR_MAPS) 761 return (fm801->reg); 762 763 return (NULL); 764 } 765 766 static int 767 fm801_release_resource(device_t bus, device_t child, int type, int rid, 768 struct resource *r) 769 { 770 return (0); 771 } 772 773 static device_method_t fm801_methods[] = { 774 /* Device interface */ 775 DEVMETHOD(device_probe, fm801_pci_probe), 776 DEVMETHOD(device_attach, fm801_pci_attach), 777 DEVMETHOD(device_detach, fm801_pci_detach), 778 DEVMETHOD(device_shutdown, bus_generic_shutdown), 779 DEVMETHOD(device_suspend, bus_generic_suspend), 780 DEVMETHOD(device_resume, bus_generic_resume), 781 782 /* Bus interface */ 783 DEVMETHOD(bus_print_child, bus_generic_print_child), 784 DEVMETHOD(bus_alloc_resource, fm801_alloc_resource), 785 DEVMETHOD(bus_release_resource, fm801_release_resource), 786 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 787 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 788 { 0, 0} 789 }; 790 791 static driver_t fm801_driver = { 792 "pcm", 793 fm801_methods, 794 PCM_SOFTC_SIZE, 795 }; 796 797 DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, 0, 0); 798 MODULE_DEPEND(snd_fm801, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER); 799 MODULE_VERSION(snd_fm801, 1); 800