fm801.c revision 294883
1155131Srwatson/*- 2162621Srwatson * Copyright (c) 2000 Dmitry Dicky diwil@dataart.com 3155131Srwatson * All rights reserved. 4155131Srwatson * 5155131Srwatson * Redistribution and use in source and binary forms, with or without 6155131Srwatson * modification, are permitted provided that the following conditions 7155131Srwatson * are met: 8155131Srwatson * 1. Redistributions of source code must retain the above copyright 9155131Srwatson * notice, this list of conditions and the following disclaimer. 10155131Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11155131Srwatson * notice, this list of conditions and the following disclaimer in the 12155131Srwatson * documentation and/or other materials provided with the distribution. 13168777Srwatson * 14155131Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND 15155131Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16155131Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17155131Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18155131Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19155131Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20155131Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21155131Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22155131Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23155131Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24155131Srwatson * SUCH DAMAGE. 25155131Srwatson */ 26155131Srwatson 27155131Srwatson#ifdef HAVE_KERNEL_OPTION_HEADERS 28155131Srwatson#include "opt_snd.h" 29155131Srwatson#endif 30155131Srwatson 31155131Srwatson#include <dev/sound/pcm/sound.h> 32155131Srwatson#include <dev/sound/pcm/ac97.h> 33243750Srwatson#include <dev/pci/pcireg.h> 34189279Srwatson#include <dev/pci/pcivar.h> 35162621Srwatson 36155131SrwatsonSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/fm801.c 294883 2016-01-27 02:23:54Z jhibbits $"); 37243750Srwatson 38243750Srwatson#define PCI_VENDOR_FORTEMEDIA 0x1319 39162503Srwatson#define PCI_DEVICE_FORTEMEDIA1 0x08011319 /* Audio controller */ 40162503Srwatson#define PCI_DEVICE_FORTEMEDIA2 0x08021319 /* Joystick controller */ 41168777Srwatson 42162503Srwatson#define FM_PCM_VOLUME 0x00 43168777Srwatson#define FM_FM_VOLUME 0x02 44155131Srwatson#define FM_I2S_VOLUME 0x04 45155131Srwatson#define FM_RECORD_SOURCE 0x06 46155131Srwatson 47168777Srwatson#define FM_PLAY_CTL 0x08 48155131Srwatson#define FM_PLAY_RATE_MASK 0x0f00 49168777Srwatson#define FM_PLAY_BUF1_LAST 0x0001 50155131Srwatson#define FM_PLAY_BUF2_LAST 0x0002 51168777Srwatson#define FM_PLAY_START 0x0020 52155131Srwatson#define FM_PLAY_PAUSE 0x0040 53155131Srwatson#define FM_PLAY_STOPNOW 0x0080 54155131Srwatson#define FM_PLAY_16BIT 0x4000 55243750Srwatson#define FM_PLAY_STEREO 0x8000 56155131Srwatson 57293161Sbrueffer#define FM_PLAY_DMALEN 0x0a 58189279Srwatson#define FM_PLAY_DMABUF1 0x0c 59162621Srwatson#define FM_PLAY_DMABUF2 0x10 60162621Srwatson 61155131Srwatson 62155131Srwatson#define FM_REC_CTL 0x14 63243750Srwatson#define FM_REC_RATE_MASK 0x0f00 64243750Srwatson#define FM_REC_BUF1_LAST 0x0001 65243750Srwatson#define FM_REC_BUF2_LAST 0x0002 66243750Srwatson#define FM_REC_START 0x0020 67155131Srwatson#define FM_REC_PAUSE 0x0040 68162503Srwatson#define FM_REC_STOPNOW 0x0080 69162503Srwatson#define FM_REC_16BIT 0x4000 70162503Srwatson#define FM_REC_STEREO 0x8000 71191273Srwatson 72162503Srwatson 73191273Srwatson#define FM_REC_DMALEN 0x16 74155131Srwatson#define FM_REC_DMABUF1 0x18 75155131Srwatson#define FM_REC_DMABUF2 0x1c 76155131Srwatson 77155131Srwatson#define FM_CODEC_CTL 0x22 78155131Srwatson#define FM_VOLUME 0x26 79168777Srwatson#define FM_VOLUME_MUTE 0x8000 80155131Srwatson 81168777Srwatson#define FM_CODEC_CMD 0x2a 82155131Srwatson#define FM_CODEC_CMD_READ 0x0080 83168777Srwatson#define FM_CODEC_CMD_VALID 0x0100 84168777Srwatson#define FM_CODEC_CMD_BUSY 0x0200 85155131Srwatson 86168777Srwatson#define FM_CODEC_DATA 0x2c 87171537Srwatson 88168777Srwatson#define FM_IO_CTL 0x52 89155131Srwatson#define FM_CARD_CTL 0x54 90155131Srwatson 91155131Srwatson#define FM_INTMASK 0x56 92155131Srwatson#define FM_INTMASK_PLAY 0x0001 93168777Srwatson#define FM_INTMASK_REC 0x0002 94155131Srwatson#define FM_INTMASK_VOL 0x0040 95168777Srwatson#define FM_INTMASK_MPU 0x0080 96155364Srwatson 97155131Srwatson#define FM_INTSTATUS 0x5a 98168777Srwatson#define FM_INTSTATUS_PLAY 0x0100 99155131Srwatson#define FM_INTSTATUS_REC 0x0200 100168777Srwatson#define FM_INTSTATUS_VOL 0x4000 101155131Srwatson#define FM_INTSTATUS_MPU 0x8000 102168777Srwatson 103243750Srwatson#define FM801_DEFAULT_BUFSZ 4096 /* Other values do not work!!! */ 104243750Srwatson 105243750Srwatson/* debug purposes */ 106155131Srwatson#define DPRINT if(0) printf 107168777Srwatson 108189279Srwatson/* 109243750Srwatsonstatic int fm801ch_setup(struct pcm_channel *c); 110189279Srwatson*/ 111189279Srwatson 112189279Srwatsonstatic u_int32_t fmts[] = { 113189279Srwatson SND_FORMAT(AFMT_U8, 1, 0), 114189279Srwatson SND_FORMAT(AFMT_U8, 2, 0), 115189279Srwatson SND_FORMAT(AFMT_S16_LE, 1, 0), 116189279Srwatson SND_FORMAT(AFMT_S16_LE, 2, 0), 117243750Srwatson 0 118189279Srwatson}; 119189279Srwatson 120189279Srwatsonstatic struct pcmchan_caps fm801ch_caps = { 121189279Srwatson 5500, 48000, 122189279Srwatson fmts, 0 123189279Srwatson}; 124189279Srwatson 125189279Srwatsonstruct fm801_info; 126162621Srwatson 127168777Srwatsonstruct fm801_chinfo { 128168777Srwatson struct fm801_info *parent; 129168777Srwatson struct pcm_channel *channel; 130168777Srwatson struct snd_dbuf *buffer; 131162621Srwatson u_int32_t spd, dir, fmt; /* speed, direction, format */ 132162621Srwatson u_int32_t shift; 133168777Srwatson}; 134155131Srwatson 135168777Srwatsonstruct fm801_info { 136155131Srwatson int type; 137168777Srwatson bus_space_tag_t st; 138155131Srwatson bus_space_handle_t sh; 139168777Srwatson bus_dma_tag_t parent_dmat; 140155131Srwatson 141168777Srwatson device_t dev; 142243750Srwatson int num; 143243750Srwatson u_int32_t unit; 144243750Srwatson 145243750Srwatson struct resource *reg, *irq; 146243750Srwatson int regtype, regid, irqid; 147243750Srwatson void *ih; 148243750Srwatson 149243750Srwatson u_int32_t play_flip, 150243750Srwatson play_nextblk, 151243750Srwatson play_start, 152243750Srwatson play_blksize, 153243750Srwatson play_fmt, 154243750Srwatson play_shift, 155243750Srwatson play_size; 156243750Srwatson 157243750Srwatson u_int32_t rec_flip, 158243750Srwatson rec_nextblk, 159155131Srwatson rec_start, 160168777Srwatson rec_blksize, 161155131Srwatson rec_fmt, 162168777Srwatson rec_shift, 163155131Srwatson rec_size; 164168777Srwatson 165162503Srwatson unsigned int bufsz; 166168777Srwatson 167162503Srwatson struct fm801_chinfo pch, rch; 168168777Srwatson 169162503Srwatson device_t radio; 170168777Srwatson}; 171162503Srwatson 172168777Srwatson/* Bus Read / Write routines */ 173162503Srwatsonstatic u_int32_t 174168777Srwatsonfm801_rd(struct fm801_info *fm801, int regno, int size) 175162503Srwatson{ 176168777Srwatson switch(size) { 177162503Srwatson case 1: 178168777Srwatson return (bus_space_read_1(fm801->st, fm801->sh, regno)); 179168777Srwatson case 2: 180168777Srwatson return (bus_space_read_2(fm801->st, fm801->sh, regno)); 181162503Srwatson case 4: 182168777Srwatson return (bus_space_read_4(fm801->st, fm801->sh, regno)); 183162503Srwatson default: 184168777Srwatson return 0xffffffff; 185162503Srwatson } 186168777Srwatson} 187162503Srwatson 188168777Srwatsonstatic void 189162503Srwatsonfm801_wr(struct fm801_info *fm801, int regno, u_int32_t data, int size) 190168777Srwatson{ 191155131Srwatson 192168777Srwatson switch(size) { 193243750Srwatson case 1: 194155131Srwatson bus_space_write_1(fm801->st, fm801->sh, regno, data); 195189279Srwatson break; 196155131Srwatson case 2: 197243750Srwatson bus_space_write_2(fm801->st, fm801->sh, regno, data); 198243750Srwatson break; 199162503Srwatson case 4: 200162503Srwatson bus_space_write_4(fm801->st, fm801->sh, regno, data); 201155131Srwatson break; 202162503Srwatson } 203168777Srwatson} 204155131Srwatson 205155131Srwatson/* -------------------------------------------------------------------- */ 206155131Srwatson/* 207162503Srwatson * ac97 codec routines 208168777Srwatson */ 209162503Srwatson#define TIMO 50 210168777Srwatsonstatic int 211162503Srwatsonfm801_rdcd(kobj_t obj, void *devinfo, int regno) 212162503Srwatson{ 213162503Srwatson struct fm801_info *fm801 = (struct fm801_info *)devinfo; 214243750Srwatson int i; 215243750Srwatson 216243750Srwatson for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 217243750Srwatson DELAY(10000); 218243750Srwatson DPRINT("fm801 rdcd: 1 - DELAY\n"); 219155131Srwatson } 220155131Srwatson if (i >= TIMO) { 221155131Srwatson printf("fm801 rdcd: codec busy\n"); 222155131Srwatson return 0; 223155131Srwatson } 224168777Srwatson 225168777Srwatson fm801_wr(fm801,FM_CODEC_CMD, regno|FM_CODEC_CMD_READ,2); 226168777Srwatson 227168777Srwatson for (i = 0; i < TIMO && !(fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_VALID); i++) 228168777Srwatson { 229155131Srwatson DELAY(10000); 230168777Srwatson DPRINT("fm801 rdcd: 2 - DELAY\n"); 231168777Srwatson } 232168777Srwatson if (i >= TIMO) { 233168777Srwatson printf("fm801 rdcd: write codec invalid\n"); 234168777Srwatson return 0; 235168777Srwatson } 236168777Srwatson 237155131Srwatson return fm801_rd(fm801,FM_CODEC_DATA,2); 238155131Srwatson} 239155131Srwatson 240155131Srwatsonstatic int 241155131Srwatsonfm801_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data) 242155131Srwatson{ 243155131Srwatson struct fm801_info *fm801 = (struct fm801_info *)devinfo; 244155131Srwatson int i; 245155131Srwatson 246155131Srwatson DPRINT("fm801_wrcd reg 0x%x val 0x%x\n",regno, data); 247155131Srwatson/* 248155131Srwatson if(regno == AC97_REG_RECSEL) return; 249243750Srwatson*/ 250155131Srwatson /* Poll until codec is ready */ 251155131Srwatson for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 252155131Srwatson DELAY(10000); 253155131Srwatson DPRINT("fm801 rdcd: 1 - DELAY\n"); 254155131Srwatson } 255155131Srwatson if (i >= TIMO) { 256155131Srwatson printf("fm801 wrcd: read codec busy\n"); 257155131Srwatson return -1; 258155131Srwatson } 259 260 fm801_wr(fm801,FM_CODEC_DATA,data, 2); 261 fm801_wr(fm801,FM_CODEC_CMD, regno,2); 262 263 /* wait until codec is ready */ 264 for (i = 0; i < TIMO && fm801_rd(fm801,FM_CODEC_CMD,2) & FM_CODEC_CMD_BUSY; i++) { 265 DELAY(10000); 266 DPRINT("fm801 wrcd: 2 - DELAY\n"); 267 } 268 if (i >= TIMO) { 269 printf("fm801 wrcd: read codec busy\n"); 270 return -1; 271 } 272 DPRINT("fm801 wrcd release reg 0x%x val 0x%x\n",regno, data); 273 return 0; 274} 275 276static kobj_method_t fm801_ac97_methods[] = { 277 KOBJMETHOD(ac97_read, fm801_rdcd), 278 KOBJMETHOD(ac97_write, fm801_wrcd), 279 DEVMETHOD_END 280}; 281AC97_DECLARE(fm801_ac97); 282 283/* -------------------------------------------------------------------- */ 284 285/* 286 * The interrupt handler 287 */ 288static void 289fm801_intr(void *p) 290{ 291 struct fm801_info *fm801 = (struct fm801_info *)p; 292 u_int32_t intsrc = fm801_rd(fm801, FM_INTSTATUS, 2); 293 294 DPRINT("\nfm801_intr intsrc 0x%x ", intsrc); 295 296 if(intsrc & FM_INTSTATUS_PLAY) { 297 fm801->play_flip++; 298 if(fm801->play_flip & 1) { 299 fm801_wr(fm801, FM_PLAY_DMABUF1, fm801->play_start,4); 300 } else 301 fm801_wr(fm801, FM_PLAY_DMABUF2, fm801->play_nextblk,4); 302 chn_intr(fm801->pch.channel); 303 } 304 305 if(intsrc & FM_INTSTATUS_REC) { 306 fm801->rec_flip++; 307 if(fm801->rec_flip & 1) { 308 fm801_wr(fm801, FM_REC_DMABUF1, fm801->rec_start,4); 309 } else 310 fm801_wr(fm801, FM_REC_DMABUF2, fm801->rec_nextblk,4); 311 chn_intr(fm801->rch.channel); 312 } 313 314 if ( intsrc & FM_INTSTATUS_MPU ) { 315 /* This is a TODOish thing... */ 316 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_MPU,2); 317 } 318 319 if ( intsrc & FM_INTSTATUS_VOL ) { 320 /* This is a TODOish thing... */ 321 fm801_wr(fm801, FM_INTSTATUS, intsrc & FM_INTSTATUS_VOL,2); 322 } 323 324 DPRINT("fm801_intr clear\n\n"); 325 fm801_wr(fm801, FM_INTSTATUS, intsrc & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC), 2); 326} 327 328/* -------------------------------------------------------------------- */ 329/* channel interface */ 330static void * 331fm801ch_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir) 332{ 333 struct fm801_info *fm801 = (struct fm801_info *)devinfo; 334 struct fm801_chinfo *ch = (dir == PCMDIR_PLAY)? &fm801->pch : &fm801->rch; 335 336 DPRINT("fm801ch_init, direction = %d\n", dir); 337 ch->parent = fm801; 338 ch->channel = c; 339 ch->buffer = b; 340 ch->dir = dir; 341 if (sndbuf_alloc(ch->buffer, fm801->parent_dmat, 0, fm801->bufsz) != 0) 342 return NULL; 343 return (void *)ch; 344} 345 346static int 347fm801ch_setformat(kobj_t obj, void *data, u_int32_t format) 348{ 349 struct fm801_chinfo *ch = data; 350 struct fm801_info *fm801 = ch->parent; 351 352 DPRINT("fm801ch_setformat 0x%x : %s, %s, %s, %s\n", format, 353 (AFMT_CHANNEL(format) > 1)?"stereo":"mono", 354 (format & AFMT_16BIT) ? "16bit":"8bit", 355 (format & AFMT_SIGNED)? "signed":"unsigned", 356 (format & AFMT_BIGENDIAN)?"bigendiah":"littleendian" ); 357 358 if(ch->dir == PCMDIR_PLAY) { 359 fm801->play_fmt = 360 (AFMT_CHANNEL(format) > 1)? FM_PLAY_STEREO : 0; 361 fm801->play_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 362 return 0; 363 } 364 365 if(ch->dir == PCMDIR_REC ) { 366 fm801->rec_fmt = (AFMT_CHANNEL(format) > 1)? FM_REC_STEREO:0; 367 fm801->rec_fmt |= (format & AFMT_16BIT) ? FM_PLAY_16BIT : 0; 368 return 0; 369 } 370 371 return 0; 372} 373 374struct { 375 u_int32_t limit; 376 u_int32_t rate; 377} fm801_rates[11] = { 378 { 6600, 5500 }, 379 { 8750, 8000 }, 380 { 10250, 9600 }, 381 { 13200, 11025 }, 382 { 17500, 16000 }, 383 { 20500, 19200 }, 384 { 26500, 22050 }, 385 { 35000, 32000 }, 386 { 41000, 38400 }, 387 { 46000, 44100 }, 388 { 48000, 48000 }, 389/* anything above -> 48000 */ 390}; 391 392static u_int32_t 393fm801ch_setspeed(kobj_t obj, void *data, u_int32_t speed) 394{ 395 struct fm801_chinfo *ch = data; 396 struct fm801_info *fm801 = ch->parent; 397 register int i; 398 399 400 for (i = 0; i < 10 && fm801_rates[i].limit <= speed; i++) ; 401 402 if(ch->dir == PCMDIR_PLAY) { 403 fm801->pch.spd = fm801_rates[i].rate; 404 fm801->play_shift = (i<<8); 405 fm801->play_shift &= FM_PLAY_RATE_MASK; 406 } 407 408 if(ch->dir == PCMDIR_REC ) { 409 fm801->rch.spd = fm801_rates[i].rate; 410 fm801->rec_shift = (i<<8); 411 fm801->rec_shift &= FM_REC_RATE_MASK; 412 } 413 414 ch->spd = fm801_rates[i].rate; 415 416 return fm801_rates[i].rate; 417} 418 419static u_int32_t 420fm801ch_setblocksize(kobj_t obj, void *data, u_int32_t blocksize) 421{ 422 struct fm801_chinfo *ch = data; 423 struct fm801_info *fm801 = ch->parent; 424 425 /* 426 * Don't mind for play_flip, set the blocksize to the 427 * desired values in any case - otherwise sound playback 428 * breaks here. 429 */ 430 if(ch->dir == PCMDIR_PLAY) 431 fm801->play_blksize = blocksize; 432 433 if(ch->dir == PCMDIR_REC) 434 fm801->rec_blksize = blocksize; 435 436 DPRINT("fm801ch_setblocksize %d (dir %d)\n",blocksize, ch->dir); 437 438 return blocksize; 439} 440 441static int 442fm801ch_trigger(kobj_t obj, void *data, int go) 443{ 444 struct fm801_chinfo *ch = data; 445 struct fm801_info *fm801 = ch->parent; 446 u_int32_t baseaddr = sndbuf_getbufaddr(ch->buffer); 447 u_int32_t k1; 448 449 DPRINT("fm801ch_trigger go %d , ", go); 450 451 if (!PCMTRIG_COMMON(go)) 452 return 0; 453 454 if (ch->dir == PCMDIR_PLAY) { 455 if (go == PCMTRIG_START) { 456 457 fm801->play_start = baseaddr; 458 fm801->play_nextblk = fm801->play_start + fm801->play_blksize; 459 fm801->play_flip = 0; 460 fm801_wr(fm801, FM_PLAY_DMALEN, fm801->play_blksize - 1, 2); 461 fm801_wr(fm801, FM_PLAY_DMABUF1,fm801->play_start,4); 462 fm801_wr(fm801, FM_PLAY_DMABUF2,fm801->play_nextblk,4); 463 fm801_wr(fm801, FM_PLAY_CTL, 464 FM_PLAY_START | FM_PLAY_STOPNOW | fm801->play_fmt | fm801->play_shift, 465 2 ); 466 } else { 467 fm801->play_flip = 0; 468 k1 = fm801_rd(fm801, FM_PLAY_CTL,2); 469 fm801_wr(fm801, FM_PLAY_CTL, 470 (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | 471 FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST, 2 ); 472 } 473 } else if(ch->dir == PCMDIR_REC) { 474 if (go == PCMTRIG_START) { 475 fm801->rec_start = baseaddr; 476 fm801->rec_nextblk = fm801->rec_start + fm801->rec_blksize; 477 fm801->rec_flip = 0; 478 fm801_wr(fm801, FM_REC_DMALEN, fm801->rec_blksize - 1, 2); 479 fm801_wr(fm801, FM_REC_DMABUF1,fm801->rec_start,4); 480 fm801_wr(fm801, FM_REC_DMABUF2,fm801->rec_nextblk,4); 481 fm801_wr(fm801, FM_REC_CTL, 482 FM_REC_START | FM_REC_STOPNOW | fm801->rec_fmt | fm801->rec_shift, 483 2 ); 484 } else { 485 fm801->rec_flip = 0; 486 k1 = fm801_rd(fm801, FM_REC_CTL,2); 487 fm801_wr(fm801, FM_REC_CTL, 488 (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | 489 FM_REC_BUF1_LAST | FM_REC_BUF2_LAST, 2); 490 } 491 } 492 493 return 0; 494} 495 496/* Almost ALSA copy */ 497static u_int32_t 498fm801ch_getptr(kobj_t obj, void *data) 499{ 500 struct fm801_chinfo *ch = data; 501 struct fm801_info *fm801 = ch->parent; 502 u_int32_t result = 0; 503 504 if (ch->dir == PCMDIR_PLAY) { 505 result = fm801_rd(fm801, 506 (fm801->play_flip&1) ? 507 FM_PLAY_DMABUF2:FM_PLAY_DMABUF1, 4) - fm801->play_start; 508 } 509 510 if (ch->dir == PCMDIR_REC) { 511 result = fm801_rd(fm801, 512 (fm801->rec_flip&1) ? 513 FM_REC_DMABUF2:FM_REC_DMABUF1, 4) - fm801->rec_start; 514 } 515 516 return result; 517} 518 519static struct pcmchan_caps * 520fm801ch_getcaps(kobj_t obj, void *data) 521{ 522 return &fm801ch_caps; 523} 524 525static kobj_method_t fm801ch_methods[] = { 526 KOBJMETHOD(channel_init, fm801ch_init), 527 KOBJMETHOD(channel_setformat, fm801ch_setformat), 528 KOBJMETHOD(channel_setspeed, fm801ch_setspeed), 529 KOBJMETHOD(channel_setblocksize, fm801ch_setblocksize), 530 KOBJMETHOD(channel_trigger, fm801ch_trigger), 531 KOBJMETHOD(channel_getptr, fm801ch_getptr), 532 KOBJMETHOD(channel_getcaps, fm801ch_getcaps), 533 DEVMETHOD_END 534}; 535CHANNEL_DECLARE(fm801ch); 536 537/* -------------------------------------------------------------------- */ 538 539/* 540 * Init routine is taken from an original NetBSD driver 541 */ 542static int 543fm801_init(struct fm801_info *fm801) 544{ 545 u_int32_t k1; 546 547 /* reset codec */ 548 fm801_wr(fm801, FM_CODEC_CTL, 0x0020,2); 549 DELAY(100000); 550 fm801_wr(fm801, FM_CODEC_CTL, 0x0000,2); 551 DELAY(100000); 552 553 fm801_wr(fm801, FM_PCM_VOLUME, 0x0808,2); 554 fm801_wr(fm801, FM_FM_VOLUME, 0x0808,2); 555 fm801_wr(fm801, FM_I2S_VOLUME, 0x0808,2); 556 fm801_wr(fm801, 0x40,0x107f,2); /* enable legacy audio */ 557 558 fm801_wr((void *)fm801, FM_RECORD_SOURCE, 0x0000,2); 559 560 /* Unmask playback, record and mpu interrupts, mask the rest */ 561 k1 = fm801_rd((void *)fm801, FM_INTMASK,2); 562 fm801_wr(fm801, FM_INTMASK, 563 (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) | 564 FM_INTMASK_VOL,2); 565 fm801_wr(fm801, FM_INTSTATUS, 566 FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU | 567 FM_INTSTATUS_VOL,2); 568 569 DPRINT("FM801 init Ok\n"); 570 return 0; 571} 572 573static int 574fm801_pci_attach(device_t dev) 575{ 576 struct ac97_info *codec = 0; 577 struct fm801_info *fm801; 578 int i; 579 int mapped = 0; 580 char status[SND_STATUSLEN]; 581 582 fm801 = malloc(sizeof(*fm801), M_DEVBUF, M_WAITOK | M_ZERO); 583 fm801->type = pci_get_devid(dev); 584 585 pci_enable_busmaster(dev); 586 587 for (i = 0; (mapped == 0) && (i < PCI_MAXMAPS_0); i++) { 588 fm801->regid = PCIR_BAR(i); 589 fm801->regtype = SYS_RES_MEMORY; 590 fm801->reg = bus_alloc_resource_any(dev, fm801->regtype, 591 &fm801->regid, RF_ACTIVE); 592 if(!fm801->reg) 593 { 594 fm801->regtype = SYS_RES_IOPORT; 595 fm801->reg = bus_alloc_resource_any(dev, 596 fm801->regtype, 597 &fm801->regid, 598 RF_ACTIVE); 599 } 600 601 if(fm801->reg) { 602 fm801->st = rman_get_bustag(fm801->reg); 603 fm801->sh = rman_get_bushandle(fm801->reg); 604 mapped++; 605 } 606 } 607 608 if (mapped == 0) { 609 device_printf(dev, "unable to map register space\n"); 610 goto oops; 611 } 612 613 fm801->bufsz = pcm_getbuffersize(dev, 4096, FM801_DEFAULT_BUFSZ, 65536); 614 615 fm801_init(fm801); 616 617 codec = AC97_CREATE(dev, fm801, fm801_ac97); 618 if (codec == NULL) goto oops; 619 620 if (mixer_init(dev, ac97_getmixerclass(), codec) == -1) goto oops; 621 622 fm801->irqid = 0; 623 fm801->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &fm801->irqid, 624 RF_ACTIVE | RF_SHAREABLE); 625 if (!fm801->irq || snd_setup_intr(dev, fm801->irq, 0, fm801_intr, fm801, &fm801->ih)) { 626 device_printf(dev, "unable to map interrupt\n"); 627 goto oops; 628 } 629 630 if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2, 631 /*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, 636 /*flags*/0, /*lockfunc*/busdma_lock_mutex, 637 /*lockarg*/&Giant, &fm801->parent_dmat) != 0) { 638 device_printf(dev, "unable to create dma tag\n"); 639 goto oops; 640 } 641 642 snprintf(status, 64, "at %s 0x%lx irq %ld %s", 643 (fm801->regtype == SYS_RES_IOPORT)? "io" : "memory", 644 rman_get_start(fm801->reg), rman_get_start(fm801->irq),PCM_KLDSTRING(snd_fm801)); 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); 663 free(fm801, M_DEVBUF); 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); 695 free(fm801, M_DEVBUF); 696 return 0; 697} 698 699static int 700fm801_pci_probe( device_t dev ) 701{ 702 int id; 703 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; 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*/ 714 return ENXIO; 715} 716 717static struct resource * 718fm801_alloc_resource(device_t bus, device_t child, int type, int *rid, 719 rman_res_t start, rman_res_t end, rman_res_t count, 720 u_int flags) 721{ 722 struct fm801_info *fm801; 723 724 fm801 = pcm_getdevinfo(bus); 725 726 if (type == SYS_RES_IOPORT && *rid == PCIR_BAR(0)) 727 return (fm801->reg); 728 729 return (NULL); 730} 731 732static int 733fm801_release_resource(device_t bus, device_t child, int type, int rid, 734 struct resource *r) 735{ 736 return (0); 737} 738 739static device_method_t fm801_methods[] = { 740 /* Device interface */ 741 DEVMETHOD(device_probe, fm801_pci_probe), 742 DEVMETHOD(device_attach, fm801_pci_attach), 743 DEVMETHOD(device_detach, fm801_pci_detach), 744 DEVMETHOD(device_shutdown, bus_generic_shutdown), 745 DEVMETHOD(device_suspend, bus_generic_suspend), 746 DEVMETHOD(device_resume, bus_generic_resume), 747 748 /* Bus interface */ 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 754 DEVMETHOD_END 755}; 756 757static driver_t fm801_driver = { 758 "pcm", 759 fm801_methods, 760 PCM_SOFTC_SIZE, 761}; 762 763DRIVER_MODULE(snd_fm801, pci, fm801_driver, pcm_devclass, 0, 0); 764MODULE_DEPEND(snd_fm801, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER); 765MODULE_VERSION(snd_fm801, 1); 766