1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  * audioixp Audio Driver
  28  *
  29  * This driver supports audio hardware integrated in ATI IXP400 chipset.
  30  *
  31  * The IXP400 audio core is an AC'97 controller, which has independent
  32  * channels for PCM in, PCM out. The AC'97 controller is a PCI bus master
  33  * with scatter/gather support. Each channel has a DMA engine. Currently,
  34  * we use only the PCM in and PCM out channels. Each DMA engine uses one
  35  * buffer descriptor list.  Each entry contains a pointer to a data buffer,
  36  * status, length of the buffer being pointed to and the pointer to the next
  37  * entry. Length of the buffer is in number of bytes. Interrupt will be
  38  * triggered each time a entry is processed by hardware.
  39  *
  40  * System power management is not yet supported by the driver.
  41  *
  42  *      NOTE:
  43  *      This driver depends on the misc/ac97 and drv/audio modules being
  44  *      loaded first.
  45  */
  46 #include <sys/types.h>
  47 #include <sys/modctl.h>
  48 #include <sys/kmem.h>
  49 #include <sys/conf.h>
  50 #include <sys/ddi.h>
  51 #include <sys/sunddi.h>
  52 #include <sys/pci.h>
  53 #include <sys/note.h>
  54 #include <sys/audio/audio_driver.h>
  55 #include <sys/audio/ac97.h>
  56 #include "audioixp.h"
  57 
  58 /*
  59  * Module linkage routines for the kernel
  60  */
  61 static int audioixp_ddi_attach(dev_info_t *, ddi_attach_cmd_t);
  62 static int audioixp_ddi_detach(dev_info_t *, ddi_detach_cmd_t);
  63 static int audioixp_quiesce(dev_info_t *);
  64 static int audioixp_resume(dev_info_t *);
  65 static int audioixp_suspend(dev_info_t *);
  66 
  67 /*
  68  * Entry point routine prototypes
  69  */
  70 static int audioixp_open(void *, int, unsigned *, caddr_t *);
  71 static void audioixp_close(void *);
  72 static int audioixp_start(void *);
  73 static void audioixp_stop(void *);
  74 static int audioixp_format(void *);
  75 static int audioixp_channels(void *);
  76 static int audioixp_rate(void *);
  77 static uint64_t audioixp_count(void *);
  78 static void audioixp_sync(void *, unsigned);
  79 
  80 static audio_engine_ops_t audioixp_engine_ops = {
  81         AUDIO_ENGINE_VERSION,
  82         audioixp_open,
  83         audioixp_close,
  84         audioixp_start,
  85         audioixp_stop,
  86         audioixp_count,
  87         audioixp_format,
  88         audioixp_channels,
  89         audioixp_rate,
  90         audioixp_sync,
  91         NULL,
  92         NULL,
  93         NULL
  94 };
  95 
  96 /*
  97  * We drive audioixp in stereo only, so we don't want to display controls
  98  * that are used for multichannel codecs.  Note that this multichannel
  99  * configuration limitation is a problem for audioixp devices.
 100  */
 101 const char *audioixp_remove_ac97[] = {
 102         AUDIO_CTRL_ID_CENTER,
 103         AUDIO_CTRL_ID_LFE,
 104         AUDIO_CTRL_ID_SURROUND,
 105         AUDIO_CTRL_ID_JACK1,
 106         AUDIO_CTRL_ID_JACK2,
 107 };
 108 
 109 /*
 110  * Local Routine Prototypes
 111  */
 112 static int audioixp_attach(dev_info_t *);
 113 static int audioixp_detach(dev_info_t *);
 114 static int audioixp_alloc_port(audioixp_state_t *, int);
 115 static void audioixp_update_port(audioixp_port_t *);
 116 
 117 static int audioixp_codec_sync(audioixp_state_t *);
 118 static void audioixp_wr97(void *, uint8_t, uint16_t);
 119 static uint16_t audioixp_rd97(void *, uint8_t);
 120 static int audioixp_reset_ac97(audioixp_state_t *);
 121 static int audioixp_map_regs(audioixp_state_t *);
 122 static void audioixp_unmap_regs(audioixp_state_t *);
 123 static int audioixp_chip_init(audioixp_state_t *);
 124 static void audioixp_destroy(audioixp_state_t *);
 125 
 126 /*
 127  * Global variables, but used only by this file.
 128  */
 129 
 130 /*
 131  * DDI Structures
 132  */
 133 
 134 /* Device operations structure */
 135 static struct dev_ops audioixp_dev_ops = {
 136         DEVO_REV,               /* devo_rev */
 137         0,                      /* devo_refcnt */
 138         NULL,                   /* devo_getinfo */
 139         nulldev,                /* devo_identify - obsolete */
 140         nulldev,                /* devo_probe */
 141         audioixp_ddi_attach,    /* devo_attach */
 142         audioixp_ddi_detach,    /* devo_detach */
 143         nodev,                  /* devo_reset */
 144         NULL,                   /* devi_cb_ops */
 145         NULL,                   /* devo_bus_ops */
 146         NULL,                   /* devo_power */
 147         audioixp_quiesce,       /* devo_quiesce */
 148 };
 149 
 150 /* Linkage structure for loadable drivers */
 151 static struct modldrv audioixp_modldrv = {
 152         &mod_driverops,             /* drv_modops */
 153         IXP_MOD_NAME,           /* drv_linkinfo */
 154         &audioixp_dev_ops,  /* drv_dev_ops */
 155 };
 156 
 157 /* Module linkage structure */
 158 static struct modlinkage audioixp_modlinkage = {
 159         MODREV_1,                               /* ml_rev */
 160         { (void *)&audioixp_modldrv, NULL } /* ml_linkage */
 161 };
 162 
 163 /*
 164  * device access attributes for register mapping
 165  */
 166 static struct ddi_device_acc_attr dev_attr = {
 167         DDI_DEVICE_ATTR_V0,
 168         DDI_STRUCTURE_LE_ACC,
 169         DDI_STRICTORDER_ACC
 170 };
 171 static struct ddi_device_acc_attr buf_attr = {
 172         DDI_DEVICE_ATTR_V0,
 173         DDI_NEVERSWAP_ACC,
 174         DDI_STRICTORDER_ACC
 175 };
 176 
 177 /*
 178  * DMA attributes of buffer descriptor list
 179  */
 180 static ddi_dma_attr_t   bdlist_dma_attr = {
 181         DMA_ATTR_V0,    /* version */
 182         0,              /* addr_lo */
 183         0xffffffff,     /* addr_hi */
 184         0x0000ffff,     /* count_max */
 185         8,              /* align, BDL must be aligned on a 8-byte boundary */
 186         0x3c,           /* burstsize */
 187         8,              /* minxfer, set to the size of a BDlist entry */
 188         0x0000ffff,     /* maxxfer */
 189         0x00000fff,     /* seg, set to the RAM pagesize of intel platform */
 190         1,              /* sgllen, there's no scatter-gather list */
 191         8,              /* granular, set to the value of minxfer */
 192         0               /* flags, use virtual address */
 193 };
 194 
 195 /*
 196  * DMA attributes of buffers to be used to receive/send audio data
 197  */
 198 static ddi_dma_attr_t   sample_buf_dma_attr = {
 199         DMA_ATTR_V0,
 200         0,              /* addr_lo */
 201         0xffffffff,     /* addr_hi */
 202         0x0001fffe,     /* count_max */
 203         4,              /* align, data buffer is aligned on a 2-byte boundary */
 204         0x3c,           /* burstsize */
 205         4,              /* minxfer, set to the size of a sample data */
 206         0x0001ffff,     /* maxxfer */
 207         0x0001ffff,     /* seg */
 208         1,              /* sgllen, no scatter-gather */
 209         4,              /* granular, set to the value of minxfer */
 210         0,              /* flags, use virtual address */
 211 };
 212 
 213 /*
 214  * _init()
 215  *
 216  * Description:
 217  *      Driver initialization, called when driver is first loaded.
 218  *      This is how access is initially given to all the static structures.
 219  *
 220  * Arguments:
 221  *      None
 222  *
 223  * Returns:
 224  *      ddi_soft_state_init() status, see ddi_soft_state_init(9f), or
 225  *      mod_install() status, see mod_install(9f)
 226  */
 227 int
 228 _init(void)
 229 {
 230         int     error;
 231 
 232         audio_init_ops(&audioixp_dev_ops, IXP_NAME);
 233 
 234         if ((error = mod_install(&audioixp_modlinkage)) != 0) {
 235                 audio_fini_ops(&audioixp_dev_ops);
 236         }
 237 
 238         return (error);
 239 }
 240 
 241 /*
 242  * _fini()
 243  *
 244  * Description:
 245  *      Module de-initialization, called when the driver is to be unloaded.
 246  *
 247  * Arguments:
 248  *      None
 249  *
 250  * Returns:
 251  *      mod_remove() status, see mod_remove(9f)
 252  */
 253 int
 254 _fini(void)
 255 {
 256         int             error;
 257 
 258         if ((error = mod_remove(&audioixp_modlinkage)) != 0) {
 259                 return (error);
 260         }
 261 
 262         audio_fini_ops(&audioixp_dev_ops);
 263 
 264         return (0);
 265 }
 266 
 267 /*
 268  * _info()
 269  *
 270  * Description:
 271  *      Module information, returns information about the driver.
 272  *
 273  * Arguments:
 274  *      modinfo         *modinfop       Pointer to the opaque modinfo structure
 275  *
 276  * Returns:
 277  *      mod_info() status, see mod_info(9f)
 278  */
 279 int
 280 _info(struct modinfo *modinfop)
 281 {
 282         return (mod_info(&audioixp_modlinkage, modinfop));
 283 }
 284 
 285 
 286 /* ******************* Driver Entry Points ********************************* */
 287 
 288 /*
 289  * audioixp_ddi_attach()
 290  *
 291  * Description:
 292  *      Attach an instance of the audioixp driver.
 293  *
 294  * Arguments:
 295  *      dev_info_t      *dip    Pointer to the device's dev_info struct
 296  *      ddi_attach_cmd_t cmd    Attach command
 297  *
 298  * Returns:
 299  *      DDI_SUCCESS             The driver was initialized properly
 300  *      DDI_FAILURE             The driver couldn't be initialized properly
 301  */
 302 static int
 303 audioixp_ddi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
 304 {
 305         switch (cmd) {
 306         case DDI_ATTACH:
 307                 return (audioixp_attach(dip));
 308 
 309         /*
 310          * now, no suspend/resume supported. we'll do it in the future.
 311          */
 312         case DDI_RESUME:
 313                 return (audioixp_resume(dip));
 314         default:
 315                 return (DDI_FAILURE);
 316         }
 317 }
 318 
 319 /*
 320  * audioixp_ddi_detach()
 321  *
 322  * Description:
 323  *      Detach an instance of the audioixp driver.
 324  *
 325  * Arguments:
 326  *      dev_info_t              *dip    Pointer to the device's dev_info struct
 327  *      ddi_detach_cmd_t        cmd     Detach command
 328  *
 329  * Returns:
 330  *      DDI_SUCCESS     The driver was detached
 331  *      DDI_FAILURE     The driver couldn't be detached
 332  */
 333 static int
 334 audioixp_ddi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
 335 {
 336         switch (cmd) {
 337         case DDI_DETACH:
 338                 return (audioixp_detach(dip));
 339 
 340         /*
 341          * now, no suspend/resume supported. we'll do it in the future.
 342          */
 343         case DDI_SUSPEND:
 344                 return (audioixp_suspend(dip));
 345 
 346         default:
 347                 return (DDI_FAILURE);
 348         }
 349 }
 350 
 351 /*
 352  * quiesce(9E) entry point.
 353  *
 354  * This function is called when the system is single-threaded at high
 355  * PIL with preemption disabled. Therefore, this function must not be blocked.
 356  *
 357  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
 358  * DDI_FAILURE indicates an error condition and should almost never happen.
 359  */
 360 static int
 361 audioixp_quiesce(dev_info_t *dip)
 362 {
 363         audioixp_state_t                *statep;
 364 
 365         statep = ddi_get_driver_private(dip);
 366         ASSERT(statep != NULL);
 367 
 368         /* stop DMA engines */
 369         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT);
 370         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA);
 371         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN);
 372         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA);
 373 
 374         return (DDI_SUCCESS);
 375 }
 376 
 377 static int
 378 audioixp_suspend(dev_info_t *dip)
 379 {
 380         audioixp_state_t                *statep;
 381 
 382         statep = ddi_get_driver_private(dip);
 383         ASSERT(statep != NULL);
 384 
 385         audio_dev_suspend(statep->adev);
 386 
 387         return (DDI_SUCCESS);
 388 }
 389 
 390 static int
 391 audioixp_resume(dev_info_t *dip)
 392 {
 393         audioixp_state_t                *statep;
 394 
 395         statep = ddi_get_driver_private(dip);
 396         ASSERT(statep != NULL);
 397 
 398         if (audioixp_chip_init(statep) != DDI_SUCCESS) {
 399                 audio_dev_warn(statep->adev, "DDI_RESUME failed to init chip");
 400                 return (DDI_SUCCESS);
 401         }
 402 
 403         ac97_reset(statep->ac97);
 404         audio_dev_resume(statep->adev);
 405 
 406         return (DDI_SUCCESS);
 407 }
 408 
 409 /*
 410  * audioixp_open()
 411  *
 412  * Description:
 413  *      Opens a DMA engine for use.
 414  *
 415  * Arguments:
 416  *      void            *arg            The DMA engine to set up
 417  *      int             flag            Open flags
 418  *      unsigned        *nframesp       Receives number of frames
 419  *      caddr_t         *bufp           Receives kernel data buffer
 420  *
 421  * Returns:
 422  *      0       on success
 423  *      errno   on failure
 424  */
 425 static int
 426 audioixp_open(void *arg, int flag, unsigned *nframesp, caddr_t *bufp)
 427 {
 428         audioixp_port_t *port = arg;
 429 
 430         _NOTE(ARGUNUSED(flag));
 431 
 432         port->started = B_FALSE;
 433         port->count = 0;
 434         port->offset = 0;
 435         *nframesp = port->nframes;
 436         *bufp = port->samp_kaddr;
 437 
 438         return (0);
 439 }
 440 
 441 /*
 442  * audioixp_close()
 443  *
 444  * Description:
 445  *      Closes an audio DMA engine that was previously opened.  Since
 446  *      nobody is using it, we take this opportunity to possibly power
 447  *      down the entire device.
 448  *
 449  * Arguments:
 450  *      void    *arg            The DMA engine to shut down
 451  */
 452 static void
 453 audioixp_close(void *arg)
 454 {
 455         _NOTE(ARGUNUSED(arg));
 456 }
 457 
 458 /*
 459  * audioixp_stop()
 460  *
 461  * Description:
 462  *      This is called by the framework to stop a port that is
 463  *      transferring data.
 464  *
 465  * Arguments:
 466  *      void    *arg            The DMA engine to stop
 467  */
 468 static void
 469 audioixp_stop(void *arg)
 470 {
 471         audioixp_port_t         *port = arg;
 472         audioixp_state_t        *statep = port->statep;
 473 
 474         mutex_enter(&statep->inst_lock);
 475         if (port->num == IXP_REC) {
 476                 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN);
 477                 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA);
 478         } else {
 479                 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT);
 480                 CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA);
 481         }
 482         mutex_exit(&statep->inst_lock);
 483 }
 484 
 485 /*
 486  * audioixp_start()
 487  *
 488  * Description:
 489  *      This is called by the framework to start a port transferring data.
 490  *
 491  * Arguments:
 492  *      void    *arg            The DMA engine to start
 493  *
 494  * Returns:
 495  *      0       on success (never fails, errno if it did)
 496  */
 497 static int
 498 audioixp_start(void *arg)
 499 {
 500         audioixp_port_t         *port = arg;
 501         audioixp_state_t        *statep = port->statep;
 502 
 503         mutex_enter(&statep->inst_lock);
 504 
 505         port->offset = 0;
 506 
 507         if (port->num == IXP_REC) {
 508                 PUT32(IXP_AUDIO_FIFO_FLUSH, IXP_AUDIO_FIFO_FLUSH_IN);
 509                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_INTER_IN);
 510 
 511                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA);
 512                 PUT32(IXP_AUDIO_IN_DMA_LINK_P,
 513                     port->bdl_paddr | IXP_AUDIO_IN_DMA_LINK_P_EN);
 514 
 515                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN);
 516         } else {
 517                 uint32_t slot = GET32(IXP_AUDIO_OUT_DMA_SLOT_EN_THRESHOLD);
 518                 PUT32(IXP_AUDIO_FIFO_FLUSH, IXP_AUDIO_FIFO_FLUSH_OUT);
 519                 /* clear all slots */
 520                 slot &= ~ (IXP_AUDIO_OUT_DMA_SLOT_3 |
 521                     IXP_AUDIO_OUT_DMA_SLOT_4 |
 522                     IXP_AUDIO_OUT_DMA_SLOT_5 |
 523                     IXP_AUDIO_OUT_DMA_SLOT_6 |
 524                     IXP_AUDIO_OUT_DMA_SLOT_7 |
 525                     IXP_AUDIO_OUT_DMA_SLOT_8 |
 526                     IXP_AUDIO_OUT_DMA_SLOT_9 |
 527                     IXP_AUDIO_OUT_DMA_SLOT_10 |
 528                     IXP_AUDIO_OUT_DMA_SLOT_11 |
 529                     IXP_AUDIO_OUT_DMA_SLOT_12);
 530                 /* enable AC'97 output slots (depending on output channels) */
 531                 slot |= IXP_AUDIO_OUT_DMA_SLOT_3 |
 532                     IXP_AUDIO_OUT_DMA_SLOT_4;
 533                 if (port->nchan >= 4) {
 534                         slot |= IXP_AUDIO_OUT_DMA_SLOT_6 |
 535                             IXP_AUDIO_OUT_DMA_SLOT_9;
 536                 }
 537                 if (port->nchan >= 6) {
 538                         slot |= IXP_AUDIO_OUT_DMA_SLOT_7 |
 539                             IXP_AUDIO_OUT_DMA_SLOT_8;
 540                 }
 541 
 542                 PUT32(IXP_AUDIO_OUT_DMA_SLOT_EN_THRESHOLD, slot);
 543 
 544                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_INTER_OUT);
 545 
 546                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA);
 547                 PUT32(IXP_AUDIO_OUT_DMA_LINK_P,
 548                     port->bdl_paddr | IXP_AUDIO_OUT_DMA_LINK_P_EN);
 549 
 550                 SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT);
 551         }
 552         mutex_exit(&statep->inst_lock);
 553         return (0);
 554 }
 555 
 556 /*
 557  * audioixp_format()
 558  *
 559  * Description:
 560  *      This is called by the framework to query the format for the device.
 561  *
 562  * Arguments:
 563  *      void    *arg            The DMA engine to query
 564  *
 565  * Returns:
 566  *      AUDIO_FORMAT_S16_LE
 567  */
 568 static int
 569 audioixp_format(void *arg)
 570 {
 571         _NOTE(ARGUNUSED(arg));
 572 
 573         return (AUDIO_FORMAT_S16_LE);
 574 }
 575 
 576 /*
 577  * audioixp_channels()
 578  *
 579  * Description:
 580  *      This is called by the framework to query the channels for the device.
 581  *
 582  * Arguments:
 583  *      void    *arg            The DMA engine to query
 584  *
 585  * Returns:
 586  *      Number of channels for the device.
 587  */
 588 static int
 589 audioixp_channels(void *arg)
 590 {
 591         audioixp_port_t *port = arg;
 592 
 593         return (port->nchan);
 594 }
 595 
 596 /*
 597  * audioixp_rate()
 598  *
 599  * Description:
 600  *      This is called by the framework to query the rate of the device.
 601  *
 602  * Arguments:
 603  *      void    *arg            The DMA engine to query
 604  *
 605  * Returns:
 606  *      48000
 607  */
 608 static int
 609 audioixp_rate(void *arg)
 610 {
 611         _NOTE(ARGUNUSED(arg));
 612 
 613         return (48000);
 614 }
 615 
 616 /*
 617  * audioixp_count()
 618  *
 619  * Description:
 620  *      This is called by the framework to get the engine's frame counter
 621  *
 622  * Arguments:
 623  *      void    *arg            The DMA engine to query
 624  *
 625  * Returns:
 626  *      frame count for current engine
 627  */
 628 static uint64_t
 629 audioixp_count(void *arg)
 630 {
 631         audioixp_port_t         *port = arg;
 632         audioixp_state_t        *statep = port->statep;
 633         uint64_t                val;
 634 
 635         mutex_enter(&statep->inst_lock);
 636         audioixp_update_port(port);
 637         val = port->count;
 638         mutex_exit(&statep->inst_lock);
 639 
 640         return (val);
 641 }
 642 
 643 /*
 644  * audioixp_sync()
 645  *
 646  * Description:
 647  *      This is called by the framework to synchronize DMA caches.
 648  *
 649  * Arguments:
 650  *      void    *arg            The DMA engine to sync
 651  */
 652 static void
 653 audioixp_sync(void *arg, unsigned nframes)
 654 {
 655         audioixp_port_t *port = arg;
 656         _NOTE(ARGUNUSED(nframes));
 657 
 658         (void) ddi_dma_sync(port->samp_dmah, 0, 0, port->sync_dir);
 659 }
 660 
 661 /* *********************** Local Routines *************************** */
 662 
 663 /*
 664  * audioixp_alloc_port()
 665  *
 666  * Description:
 667  *      This routine allocates the DMA handles and the memory for the
 668  *      DMA engines to use.  It also configures the BDL lists properly
 669  *      for use.
 670  *
 671  * Arguments:
 672  *      dev_info_t      *dip    Pointer to the device's devinfo
 673  *
 674  * Returns:
 675  *      DDI_SUCCESS             Registers successfully mapped
 676  *      DDI_FAILURE             Registers not successfully mapped
 677  */
 678 static int
 679 audioixp_alloc_port(audioixp_state_t *statep, int num)
 680 {
 681         ddi_dma_cookie_t        cookie;
 682         uint_t                  count;
 683         int                     dir;
 684         unsigned                caps;
 685         audio_dev_t             *adev;
 686         audioixp_port_t         *port;
 687         uint32_t                paddr;
 688         int                     rc;
 689         dev_info_t              *dip;
 690         audioixp_bd_entry_t     *bdentry;
 691 
 692         adev = statep->adev;
 693         dip = statep->dip;
 694 
 695         port = kmem_zalloc(sizeof (*port), KM_SLEEP);
 696         port->statep = statep;
 697         port->started = B_FALSE;
 698         port->num = num;
 699 
 700         switch (num) {
 701         case IXP_REC:
 702                 statep->rec_port = port;
 703                 dir = DDI_DMA_READ;
 704                 caps = ENGINE_INPUT_CAP;
 705                 port->sync_dir = DDI_DMA_SYNC_FORKERNEL;
 706                 port->nchan = 2;
 707                 break;
 708         case IXP_PLAY:
 709                 statep->play_port = port;
 710                 dir = DDI_DMA_WRITE;
 711                 caps = ENGINE_OUTPUT_CAP;
 712                 port->sync_dir = DDI_DMA_SYNC_FORDEV;
 713                 /*
 714                  * We allow for end users to configure more channels
 715                  * than just two, but we default to just two.  The
 716                  * default stereo configuration works well.  On the
 717                  * configurations we have tested, we've found that
 718                  * more than two channels (or rather 6 channels) can
 719                  * cause inexplicable noise.  The noise is more
 720                  * noticeable when the system is running under load.
 721                  * (Holding the space bar in "top" while playing an
 722                  * MP3 is an easy way to recreate it.)  End users who
 723                  * want to experiment, or have configurations that
 724                  * don't suffer from this, may increase the channels
 725                  * by setting this max-channels property.  We leave it
 726                  * undocumented for now.
 727                  */
 728                 port->nchan = ddi_prop_get_int(DDI_DEV_T_ANY, dip, 0,
 729                     "max-channels", 2);
 730                 port->nchan = min(ac97_num_channels(statep->ac97),
 731                     port->nchan);
 732                 port->nchan &= ~1;       /* make sure its an even number */
 733                 port->nchan = max(port->nchan, 2);
 734                 break;
 735         default:
 736                 audio_dev_warn(adev, "bad port number (%d)!", num);
 737                 return (DDI_FAILURE);
 738         }
 739 
 740         port->nframes = 4096;
 741         port->fragfr = port->nframes / IXP_BD_NUMS;
 742         port->fragsz = port->fragfr * port->nchan * 2;
 743         port->samp_size = port->nframes * port->nchan * 2;
 744 
 745         /* allocate dma handle */
 746         rc = ddi_dma_alloc_handle(dip, &sample_buf_dma_attr, DDI_DMA_SLEEP,
 747             NULL, &port->samp_dmah);
 748         if (rc != DDI_SUCCESS) {
 749                 audio_dev_warn(adev, "ddi_dma_alloc_handle failed: %d", rc);
 750                 return (DDI_FAILURE);
 751         }
 752         /* allocate DMA buffer */
 753         rc = ddi_dma_mem_alloc(port->samp_dmah, port->samp_size, &buf_attr,
 754             DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL, &port->samp_kaddr,
 755             &port->samp_size, &port->samp_acch);
 756         if (rc == DDI_FAILURE) {
 757                 audio_dev_warn(adev, "dma_mem_alloc failed");
 758                 return (DDI_FAILURE);
 759         }
 760 
 761         /* bind DMA buffer */
 762         rc = ddi_dma_addr_bind_handle(port->samp_dmah, NULL,
 763             port->samp_kaddr, port->samp_size, dir|DDI_DMA_CONSISTENT,
 764             DDI_DMA_SLEEP, NULL, &cookie, &count);
 765         if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
 766                 audio_dev_warn(adev,
 767                     "ddi_dma_addr_bind_handle failed: %d", rc);
 768                 return (DDI_FAILURE);
 769         }
 770         port->samp_paddr = cookie.dmac_address;
 771 
 772         /*
 773          * now, from here we allocate DMA memory for buffer descriptor list.
 774          * we allocate adjacent DMA memory for all DMA engines.
 775          */
 776         rc = ddi_dma_alloc_handle(dip, &bdlist_dma_attr, DDI_DMA_SLEEP,
 777             NULL, &port->bdl_dmah);
 778         if (rc != DDI_SUCCESS) {
 779                 audio_dev_warn(adev, "ddi_dma_alloc_handle(bdlist) failed");
 780                 return (DDI_FAILURE);
 781         }
 782 
 783         /*
 784          * we allocate all buffer descriptors lists in continuous dma memory.
 785          */
 786         port->bdl_size = sizeof (audioixp_bd_entry_t) * IXP_BD_NUMS;
 787         rc = ddi_dma_mem_alloc(port->bdl_dmah, port->bdl_size,
 788             &dev_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
 789             &port->bdl_kaddr, &port->bdl_size, &port->bdl_acch);
 790         if (rc != DDI_SUCCESS) {
 791                 audio_dev_warn(adev, "ddi_dma_mem_alloc(bdlist) failed");
 792                 return (DDI_FAILURE);
 793         }
 794 
 795         rc = ddi_dma_addr_bind_handle(port->bdl_dmah, NULL, port->bdl_kaddr,
 796             port->bdl_size, DDI_DMA_WRITE|DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
 797             NULL, &cookie, &count);
 798         if ((rc != DDI_DMA_MAPPED) || (count != 1)) {
 799                 audio_dev_warn(adev, "addr_bind_handle failed");
 800                 return (DDI_FAILURE);
 801         }
 802         port->bdl_paddr = cookie.dmac_address;
 803 
 804         /*
 805          * Wire up the BD list.
 806          */
 807         paddr = port->samp_paddr;
 808         bdentry = (void *)port->bdl_kaddr;
 809 
 810         for (int i = 0; i < IXP_BD_NUMS; i++) {
 811 
 812                 /* set base address of buffer */
 813                 ddi_put32(port->bdl_acch, &bdentry->buf_base, paddr);
 814                 ddi_put16(port->bdl_acch, &bdentry->status, 0);
 815                 ddi_put16(port->bdl_acch, &bdentry->buf_len, port->fragsz / 4);
 816                 ddi_put32(port->bdl_acch, &bdentry->next, port->bdl_paddr +
 817                     (((i + 1) % IXP_BD_NUMS) * sizeof (audioixp_bd_entry_t)));
 818                 paddr += port->fragsz;
 819                 bdentry++;
 820         }
 821         (void) ddi_dma_sync(port->bdl_dmah, 0, 0, DDI_DMA_SYNC_FORDEV);
 822 
 823         port->engine = audio_engine_alloc(&audioixp_engine_ops, caps);
 824         if (port->engine == NULL) {
 825                 audio_dev_warn(adev, "audio_engine_alloc failed");
 826                 return (DDI_FAILURE);
 827         }
 828 
 829         audio_engine_set_private(port->engine, port);
 830         audio_dev_add_engine(adev, port->engine);
 831 
 832         return (DDI_SUCCESS);
 833 }
 834 
 835 /*
 836  * audioixp_free_port()
 837  *
 838  * Description:
 839  *      This routine unbinds the DMA cookies, frees the DMA buffers,
 840  *      deallocates the DMA handles.
 841  *
 842  * Arguments:
 843  *      audioixp_port_t *port   The port structure for a DMA engine.
 844  */
 845 static void
 846 audioixp_free_port(audioixp_port_t *port)
 847 {
 848         if (port == NULL)
 849                 return;
 850 
 851         if (port->engine) {
 852                 audio_dev_remove_engine(port->statep->adev, port->engine);
 853                 audio_engine_free(port->engine);
 854         }
 855         if (port->bdl_paddr) {
 856                 (void) ddi_dma_unbind_handle(port->bdl_dmah);
 857         }
 858         if (port->bdl_acch) {
 859                 ddi_dma_mem_free(&port->bdl_acch);
 860         }
 861         if (port->bdl_dmah) {
 862                 ddi_dma_free_handle(&port->bdl_dmah);
 863         }
 864         if (port->samp_paddr) {
 865                 (void) ddi_dma_unbind_handle(port->samp_dmah);
 866         }
 867         if (port->samp_acch) {
 868                 ddi_dma_mem_free(&port->samp_acch);
 869         }
 870         if (port->samp_dmah) {
 871                 ddi_dma_free_handle(&port->samp_dmah);
 872         }
 873         kmem_free(port, sizeof (*port));
 874 }
 875 
 876 /*
 877  * audioixp_update_port()
 878  *
 879  * Description:
 880  *      This routine updates the ports frame counter from hardware, and
 881  *      gracefully handles wraps.
 882  *
 883  * Arguments:
 884  *      audioixp_port_t *port           The port to update.
 885  */
 886 static void
 887 audioixp_update_port(audioixp_port_t *port)
 888 {
 889         audioixp_state_t        *statep = port->statep;
 890         unsigned                regoff;
 891         unsigned                n;
 892         int                     loop;
 893         uint32_t                offset;
 894         uint32_t                paddr;
 895 
 896         if (port->num == IXP_REC) {
 897                 regoff = IXP_AUDIO_IN_DMA_DT_CUR;
 898         } else {
 899                 regoff = IXP_AUDIO_OUT_DMA_DT_CUR;
 900         }
 901 
 902         /*
 903          * Apparently it may take several tries to get an update on the
 904          * position.  Is this a hardware bug?
 905          */
 906         for (loop = 100; loop; loop--) {
 907                 paddr = GET32(regoff);
 908 
 909                 /* make sure address is reasonable */
 910                 if ((paddr < port->samp_paddr) ||
 911                     (paddr >= (port->samp_paddr + port->samp_size))) {
 912                         continue;
 913                 }
 914 
 915                 offset = paddr - port->samp_paddr;
 916 
 917                 if (offset >= port->offset) {
 918                         n = offset - port->offset;
 919                 } else {
 920                         n = offset + (port->samp_size - port->offset);
 921                 }
 922                 port->offset = offset;
 923                 port->count += (n / (port->nchan * sizeof (uint16_t)));
 924                 return;
 925         }
 926 
 927         audio_dev_warn(statep->adev, "Unable to update count (h/w bug?)");
 928 }
 929 
 930 
 931 /*
 932  * audioixp_map_regs()
 933  *
 934  * Description:
 935  *      The registers are mapped in.
 936  *
 937  * Arguments:
 938  *      audioixp_state_t        *state            The device's state structure
 939  *
 940  * Returns:
 941  *      DDI_SUCCESS             Registers successfully mapped
 942  *      DDI_FAILURE             Registers not successfully mapped
 943  */
 944 static int
 945 audioixp_map_regs(audioixp_state_t *statep)
 946 {
 947         dev_info_t              *dip = statep->dip;
 948 
 949         /* map PCI config space */
 950         if (pci_config_setup(statep->dip, &statep->pcih) == DDI_FAILURE) {
 951                 audio_dev_warn(statep->adev, "unable to map PCI config space");
 952                 return (DDI_FAILURE);
 953         }
 954 
 955         /* map audio mixer register */
 956         if ((ddi_regs_map_setup(dip, IXP_IO_AM_REGS, &statep->regsp, 0, 0,
 957             &dev_attr, &statep->regsh)) != DDI_SUCCESS) {
 958                 audio_dev_warn(statep->adev, "unable to map audio registers");
 959                 return (DDI_FAILURE);
 960         }
 961         return (DDI_SUCCESS);
 962 }
 963 
 964 /*
 965  * audioixp_unmap_regs()
 966  *
 967  * Description:
 968  *      This routine unmaps control registers.
 969  *
 970  * Arguments:
 971  *      audioixp_state_t        *state          The device's state structure
 972  */
 973 static void
 974 audioixp_unmap_regs(audioixp_state_t *statep)
 975 {
 976         if (statep->regsh) {
 977                 ddi_regs_map_free(&statep->regsh);
 978         }
 979 
 980         if (statep->pcih) {
 981                 pci_config_teardown(&statep->pcih);
 982         }
 983 }
 984 
 985 /*
 986  * audioixp_codec_ready()
 987  *
 988  * Description:
 989  *      This routine checks the state of codecs.  It checks the flag to confirm
 990  *      that primary codec is ready.
 991  *
 992  * Arguments:
 993  *      audioixp_state_t        *state          The device's state structure
 994  *
 995  * Returns:
 996  *      DDI_SUCCESS      codec is ready
 997  *      DDI_FAILURE      codec is not ready
 998  */
 999 static int
1000 audioixp_codec_ready(audioixp_state_t *statep)
1001 {
1002         uint32_t        sr;
1003 
1004         PUT32(IXP_AUDIO_INT, 0xffffffff);
1005         drv_usecwait(1000);
1006 
1007         sr = GET32(IXP_AUDIO_INT);
1008         if (sr & IXP_AUDIO_INT_CODEC0_NOT_READY) {
1009                 PUT32(IXP_AUDIO_INT, IXP_AUDIO_INT_CODEC0_NOT_READY);
1010                 audio_dev_warn(statep->adev, "primary codec not ready");
1011 
1012                 return (DDI_FAILURE);
1013         }
1014         return (DDI_SUCCESS);
1015 }
1016 
1017 /*
1018  * audioixp_codec_sync()
1019  *
1020  * Description:
1021  *      Serialize access to the AC97 audio mixer registers.
1022  *
1023  * Arguments:
1024  *      audioixp_state_t        *state          The device's state structure
1025  *
1026  * Returns:
1027  *      DDI_SUCCESS             Ready for an I/O access to the codec
1028  *      DDI_FAILURE             An I/O access is currently in progress, can't
1029  *                              perform another I/O access.
1030  */
1031 static int
1032 audioixp_codec_sync(audioixp_state_t *statep)
1033 {
1034         int             i;
1035         uint32_t        cmd;
1036 
1037         for (i = 0; i < 300; i++) {
1038                 cmd = GET32(IXP_AUDIO_OUT_PHY_ADDR_DATA);
1039                 if (!(cmd & IXP_AUDIO_OUT_PHY_EN)) {
1040                         return (DDI_SUCCESS);
1041                 }
1042                 drv_usecwait(10);
1043         }
1044 
1045         audio_dev_warn(statep->adev, "unable to synchronize codec");
1046         return (DDI_FAILURE);
1047 }
1048 
1049 /*
1050  * audioixp_rd97()
1051  *
1052  * Description:
1053  *      Get the specific AC97 Codec register.
1054  *
1055  * Arguments:
1056  *      void            *arg            The device's state structure
1057  *      uint8_t         reg             AC97 register number
1058  *
1059  * Returns:
1060  *      Register value.
1061  */
1062 static uint16_t
1063 audioixp_rd97(void *arg, uint8_t reg)
1064 {
1065         audioixp_state_t        *statep = arg;
1066         uint32_t                value;
1067         uint32_t                result;
1068 
1069         if (audioixp_codec_sync(statep) != DDI_SUCCESS)
1070                 return (0xffff);
1071 
1072         value = IXP_AUDIO_OUT_PHY_PRIMARY_CODEC |
1073             IXP_AUDIO_OUT_PHY_READ |
1074             IXP_AUDIO_OUT_PHY_EN |
1075             ((unsigned)reg << IXP_AUDIO_OUT_PHY_ADDR_SHIFT);
1076         PUT32(IXP_AUDIO_OUT_PHY_ADDR_DATA, value);
1077 
1078         if (audioixp_codec_sync(statep) != DDI_SUCCESS)
1079                 return (0xffff);
1080 
1081         for (int i = 0; i < 300; i++) {
1082                 result = GET32(IXP_AUDIO_IN_PHY_ADDR_DATA);
1083                 if (result & IXP_AUDIO_IN_PHY_READY)        {
1084                         return (result >> IXP_AUDIO_IN_PHY_DATA_SHIFT);
1085                 }
1086                 drv_usecwait(10);
1087         }
1088 
1089 done:
1090         audio_dev_warn(statep->adev, "time out reading codec reg %d", reg);
1091         return (0xffff);
1092 }
1093 
1094 /*
1095  * audioixp_wr97()
1096  *
1097  * Description:
1098  *      Set the specific AC97 Codec register.
1099  *
1100  * Arguments:
1101  *      void            *arg            The device's state structure
1102  *      uint8_t         reg             AC97 register number
1103  *      uint16_t        data            The data want to be set
1104  */
1105 static void
1106 audioixp_wr97(void *arg, uint8_t reg, uint16_t data)
1107 {
1108         audioixp_state_t        *statep = arg;
1109         uint32_t                value;
1110 
1111         if (audioixp_codec_sync(statep) != DDI_SUCCESS) {
1112                 return;
1113         }
1114 
1115         value = IXP_AUDIO_OUT_PHY_PRIMARY_CODEC |
1116             IXP_AUDIO_OUT_PHY_WRITE |
1117             IXP_AUDIO_OUT_PHY_EN |
1118             ((unsigned)reg << IXP_AUDIO_OUT_PHY_ADDR_SHIFT) |
1119             ((unsigned)data << IXP_AUDIO_OUT_PHY_DATA_SHIFT);
1120         PUT32(IXP_AUDIO_OUT_PHY_ADDR_DATA, value);
1121 
1122         (void) audioixp_rd97(statep, reg);
1123 }
1124 
1125 /*
1126  * audioixp_reset_ac97()
1127  *
1128  * Description:
1129  *      Reset AC97 Codec register.
1130  *
1131  * Arguments:
1132  *      audioixp_state_t        *state          The device's state structure
1133  *
1134  * Returns:
1135  *      DDI_SUCCESS             Reset the codec successfully
1136  *      DDI_FAILURE             Failed to reset the codec
1137  */
1138 static int
1139 audioixp_reset_ac97(audioixp_state_t *statep)
1140 {
1141         uint32_t        cmd;
1142         int i;
1143 
1144         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_POWER_DOWN);
1145         drv_usecwait(10);
1146 
1147         /* register reset */
1148         SET32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_AC_SOFT_RESET);
1149         /* force a read to flush caches */
1150         (void) GET32(IXP_AUDIO_CMD);
1151 
1152         drv_usecwait(10);
1153         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_AC_SOFT_RESET);
1154 
1155         /* cold reset */
1156         for (i = 0; i < 300; i++) {
1157                 cmd = GET32(IXP_AUDIO_CMD);
1158                 if (cmd & IXP_AUDIO_CMD_AC_ACTIVE) {
1159                         cmd |= IXP_AUDIO_CMD_AC_RESET | IXP_AUDIO_CMD_AC_SYNC;
1160                         PUT32(IXP_AUDIO_CMD, cmd);
1161                         return (DDI_SUCCESS);
1162                 }
1163                 cmd &= ~IXP_AUDIO_CMD_AC_RESET;
1164                 cmd |= IXP_AUDIO_CMD_AC_SYNC;
1165                 PUT32(IXP_AUDIO_CMD, cmd);
1166                 (void) GET32(IXP_AUDIO_CMD);
1167                 drv_usecwait(10);
1168                 cmd |= IXP_AUDIO_CMD_AC_RESET;
1169                 PUT32(IXP_AUDIO_CMD, cmd);
1170                 drv_usecwait(10);
1171         }
1172 
1173         audio_dev_warn(statep->adev, "AC'97 reset timed out");
1174         return (DDI_FAILURE);
1175 }
1176 
1177 /*
1178  * audioixp_chip_init()
1179  *
1180  * Description:
1181  *      This routine initializes ATI IXP audio controller and the AC97
1182  *      codec.
1183  *
1184  * Arguments:
1185  *      audioixp_state_t        *state          The device's state structure
1186  *
1187  * Returns:
1188  *      DDI_SUCCESS     The hardware was initialized properly
1189  *      DDI_FAILURE     The hardware couldn't be initialized properly
1190  */
1191 static int
1192 audioixp_chip_init(audioixp_state_t *statep)
1193 {
1194         /*
1195          * put the audio controller into quiet state, everything off
1196          */
1197         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA);
1198         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA);
1199 
1200         /* AC97 reset */
1201         if (audioixp_reset_ac97(statep) != DDI_SUCCESS) {
1202                 audio_dev_warn(statep->adev, "AC97 codec reset failed");
1203                 return (DDI_FAILURE);
1204         }
1205 
1206         if (audioixp_codec_ready(statep) != DDI_SUCCESS) {
1207                 audio_dev_warn(statep->adev, "AC97 codec not ready");
1208                 return (DDI_FAILURE);
1209         }
1210 
1211         return (DDI_SUCCESS);
1212 
1213 }       /* audioixp_chip_init() */
1214 
1215 /*
1216  * audioixp_attach()
1217  *
1218  * Description:
1219  *      Attach an instance of the audioixp driver. This routine does
1220  *      the device dependent attach tasks.
1221  *
1222  * Arguments:
1223  *      dev_info_t      *dip    Pointer to the device's dev_info struct
1224  *      ddi_attach_cmd_t cmd    Attach command
1225  *
1226  * Returns:
1227  *      DDI_SUCCESS             The driver was initialized properly
1228  *      DDI_FAILURE             The driver couldn't be initialized properly
1229  */
1230 static int
1231 audioixp_attach(dev_info_t *dip)
1232 {
1233         uint16_t                cmdeg;
1234         audioixp_state_t        *statep;
1235         audio_dev_t             *adev;
1236         uint32_t                devid;
1237         const char              *name;
1238         const char              *rev;
1239 
1240         /* allocate the soft state structure */
1241         statep = kmem_zalloc(sizeof (*statep), KM_SLEEP);
1242         statep->dip = dip;
1243         ddi_set_driver_private(dip, statep);
1244         mutex_init(&statep->inst_lock, NULL, MUTEX_DRIVER, NULL);
1245 
1246         /* allocate framework audio device */
1247         if ((adev = audio_dev_alloc(dip, 0)) == NULL) {
1248                 cmn_err(CE_WARN, "!%s%d: unable to allocate audio dev",
1249                     ddi_driver_name(dip), ddi_get_instance(dip));
1250                 goto error;
1251         }
1252         statep->adev = adev;
1253 
1254         /* map in the registers */
1255         if (audioixp_map_regs(statep) != DDI_SUCCESS) {
1256                 audio_dev_warn(adev, "couldn't map registers");
1257                 goto error;
1258         }
1259 
1260         /* set device information -- this could be smarter */
1261         devid = ((pci_config_get16(statep->pcih, PCI_CONF_VENID)) << 16) |
1262             pci_config_get16(statep->pcih, PCI_CONF_DEVID);
1263 
1264         name = "ATI AC'97";
1265         switch (devid) {
1266         case IXP_PCI_ID_200:
1267                 rev = "IXP150";
1268                 break;
1269         case IXP_PCI_ID_300:
1270                 rev = "SB300";
1271                 break;
1272         case IXP_PCI_ID_400:
1273                 if (pci_config_get8(statep->pcih, PCI_CONF_REVID) & 0x80) {
1274                         rev = "SB450";
1275                 } else {
1276                         rev = "SB400";
1277                 }
1278                 break;
1279         case IXP_PCI_ID_SB600:
1280                 rev = "SB600";
1281                 break;
1282         default:
1283                 rev = "Unknown";
1284                 break;
1285         }
1286         audio_dev_set_description(adev, name);
1287         audio_dev_set_version(adev, rev);
1288 
1289         /* set PCI command register */
1290         cmdeg = pci_config_get16(statep->pcih, PCI_CONF_COMM);
1291         pci_config_put16(statep->pcih, PCI_CONF_COMM,
1292             cmdeg | PCI_COMM_IO | PCI_COMM_MAE);
1293 
1294         statep->ac97 = ac97_alloc(dip, audioixp_rd97, audioixp_wr97, statep);
1295         if (statep->ac97 == NULL) {
1296                 audio_dev_warn(adev, "failed to allocate ac97 handle");
1297                 goto error;
1298         }
1299 
1300         /* allocate port structures */
1301         if ((audioixp_alloc_port(statep, IXP_PLAY) != DDI_SUCCESS) ||
1302             (audioixp_alloc_port(statep, IXP_REC) != DDI_SUCCESS)) {
1303                 goto error;
1304         }
1305 
1306         /*
1307          * If we have locked in a stereo configuration, then don't expose
1308          * multichannel-specific AC'97 codec controls.
1309          */
1310         if (statep->play_port->nchan == 2) {
1311                 int i;
1312                 ac97_ctrl_t *ctrl;
1313                 const char *name;
1314 
1315                 for (i = 0; (name = audioixp_remove_ac97[i]) != NULL; i++) {
1316                         ctrl = ac97_control_find(statep->ac97, name);
1317                         if (ctrl != NULL) {
1318                                 ac97_control_unregister(ctrl);
1319                         }
1320                 }
1321         }
1322 
1323         if (audioixp_chip_init(statep) != DDI_SUCCESS) {
1324                 audio_dev_warn(statep->adev, "failed to init chip");
1325                 goto error;
1326         }
1327 
1328         /* initialize the AC'97 part */
1329         if (ac97_init(statep->ac97, adev) != DDI_SUCCESS) {
1330                 audio_dev_warn(adev, "ac'97 initialization failed");
1331                 goto error;
1332         }
1333 
1334         if (audio_dev_register(adev) != DDI_SUCCESS) {
1335                 audio_dev_warn(adev, "unable to register with framework");
1336                 goto error;
1337         }
1338 
1339         ddi_report_dev(dip);
1340 
1341         return (DDI_SUCCESS);
1342 
1343 error:
1344         audioixp_destroy(statep);
1345         return (DDI_FAILURE);
1346 }
1347 
1348 /*
1349  * audioixp_detach()
1350  *
1351  * Description:
1352  *      Detach an instance of the audioixp driver.
1353  *
1354  * Arguments:
1355  *      dev_info_t      *dip    Pointer to the device's dev_info struct
1356  *
1357  * Returns:
1358  *      DDI_SUCCESS     The driver was detached
1359  *      DDI_FAILURE     The driver couldn't be detached
1360  */
1361 static int
1362 audioixp_detach(dev_info_t *dip)
1363 {
1364         audioixp_state_t        *statep;
1365 
1366         statep = ddi_get_driver_private(dip);
1367 
1368         if (audio_dev_unregister(statep->adev) != DDI_SUCCESS) {
1369                 return (DDI_FAILURE);
1370         }
1371 
1372         audioixp_destroy(statep);
1373         return (DDI_SUCCESS);
1374 }
1375 
1376 /*
1377  * audioixp_destroy()
1378  *
1379  * Description:
1380  *      This routine releases all resources held by the device instance,
1381  *      as part of either detach or a failure in attach.
1382  *
1383  * Arguments:
1384  *      audioixp_state_t        *state  The device soft state.
1385  */
1386 static void
1387 audioixp_destroy(audioixp_state_t *statep)
1388 {
1389         /*
1390          * put the audio controller into quiet state, everything off
1391          */
1392         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_OUT_DMA);
1393         CLR32(IXP_AUDIO_CMD, IXP_AUDIO_CMD_EN_IN_DMA);
1394 
1395         audioixp_free_port(statep->play_port);
1396         audioixp_free_port(statep->rec_port);
1397 
1398         audioixp_unmap_regs(statep);
1399 
1400         if (statep->ac97) {
1401                 ac97_free(statep->ac97);
1402         }
1403 
1404         if (statep->adev) {
1405                 audio_dev_free(statep->adev);
1406         }
1407 
1408         mutex_destroy(&statep->inst_lock);
1409         kmem_free(statep, sizeof (*statep));
1410 }