1*4882a593Smuzhiyun====================== 2*4882a593SmuzhiyunWriting an ALSA Driver 3*4882a593Smuzhiyun====================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Takashi Iwai <tiwai@suse.de> 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunPreface 8*4882a593Smuzhiyun======= 9*4882a593Smuzhiyun 10*4882a593SmuzhiyunThis document describes how to write an `ALSA (Advanced Linux Sound 11*4882a593SmuzhiyunArchitecture) <http://www.alsa-project.org/>`__ driver. The document 12*4882a593Smuzhiyunfocuses mainly on PCI soundcards. In the case of other device types, the 13*4882a593SmuzhiyunAPI might be different, too. However, at least the ALSA kernel API is 14*4882a593Smuzhiyunconsistent, and therefore it would be still a bit help for writing them. 15*4882a593Smuzhiyun 16*4882a593SmuzhiyunThis document targets people who already have enough C language skills 17*4882a593Smuzhiyunand have basic linux kernel programming knowledge. This document doesn't 18*4882a593Smuzhiyunexplain the general topic of linux kernel coding and doesn't cover 19*4882a593Smuzhiyunlow-level driver implementation details. It only describes the standard 20*4882a593Smuzhiyunway to write a PCI sound driver on ALSA. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunThis document is still a draft version. Any feedback and corrections, 23*4882a593Smuzhiyunplease!! 24*4882a593Smuzhiyun 25*4882a593SmuzhiyunFile Tree Structure 26*4882a593Smuzhiyun=================== 27*4882a593Smuzhiyun 28*4882a593SmuzhiyunGeneral 29*4882a593Smuzhiyun------- 30*4882a593Smuzhiyun 31*4882a593SmuzhiyunThe file tree structure of ALSA driver is depicted below. 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun:: 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun sound 36*4882a593Smuzhiyun /core 37*4882a593Smuzhiyun /oss 38*4882a593Smuzhiyun /seq 39*4882a593Smuzhiyun /oss 40*4882a593Smuzhiyun /include 41*4882a593Smuzhiyun /drivers 42*4882a593Smuzhiyun /mpu401 43*4882a593Smuzhiyun /opl3 44*4882a593Smuzhiyun /i2c 45*4882a593Smuzhiyun /synth 46*4882a593Smuzhiyun /emux 47*4882a593Smuzhiyun /pci 48*4882a593Smuzhiyun /(cards) 49*4882a593Smuzhiyun /isa 50*4882a593Smuzhiyun /(cards) 51*4882a593Smuzhiyun /arm 52*4882a593Smuzhiyun /ppc 53*4882a593Smuzhiyun /sparc 54*4882a593Smuzhiyun /usb 55*4882a593Smuzhiyun /pcmcia /(cards) 56*4882a593Smuzhiyun /soc 57*4882a593Smuzhiyun /oss 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun 60*4882a593Smuzhiyuncore directory 61*4882a593Smuzhiyun-------------- 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunThis directory contains the middle layer which is the heart of ALSA 64*4882a593Smuzhiyundrivers. In this directory, the native ALSA modules are stored. The 65*4882a593Smuzhiyunsub-directories contain different modules and are dependent upon the 66*4882a593Smuzhiyunkernel config. 67*4882a593Smuzhiyun 68*4882a593Smuzhiyuncore/oss 69*4882a593Smuzhiyun~~~~~~~~ 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunThe codes for PCM and mixer OSS emulation modules are stored in this 72*4882a593Smuzhiyundirectory. The rawmidi OSS emulation is included in the ALSA rawmidi 73*4882a593Smuzhiyuncode since it's quite small. The sequencer code is stored in 74*4882a593Smuzhiyun``core/seq/oss`` directory (see `below <#core-seq-oss>`__). 75*4882a593Smuzhiyun 76*4882a593Smuzhiyuncore/seq 77*4882a593Smuzhiyun~~~~~~~~ 78*4882a593Smuzhiyun 79*4882a593SmuzhiyunThis directory and its sub-directories are for the ALSA sequencer. This 80*4882a593Smuzhiyundirectory contains the sequencer core and primary sequencer modules such 81*4882a593Smuzhiyunlike snd-seq-midi, snd-seq-virmidi, etc. They are compiled only when 82*4882a593Smuzhiyun``CONFIG_SND_SEQUENCER`` is set in the kernel config. 83*4882a593Smuzhiyun 84*4882a593Smuzhiyuncore/seq/oss 85*4882a593Smuzhiyun~~~~~~~~~~~~ 86*4882a593Smuzhiyun 87*4882a593SmuzhiyunThis contains the OSS sequencer emulation codes. 88*4882a593Smuzhiyun 89*4882a593Smuzhiyuninclude directory 90*4882a593Smuzhiyun----------------- 91*4882a593Smuzhiyun 92*4882a593SmuzhiyunThis is the place for the public header files of ALSA drivers, which are 93*4882a593Smuzhiyunto be exported to user-space, or included by several files at different 94*4882a593Smuzhiyundirectories. Basically, the private header files should not be placed in 95*4882a593Smuzhiyunthis directory, but you may still find files there, due to historical 96*4882a593Smuzhiyunreasons :) 97*4882a593Smuzhiyun 98*4882a593Smuzhiyundrivers directory 99*4882a593Smuzhiyun----------------- 100*4882a593Smuzhiyun 101*4882a593SmuzhiyunThis directory contains code shared among different drivers on different 102*4882a593Smuzhiyunarchitectures. They are hence supposed not to be architecture-specific. 103*4882a593SmuzhiyunFor example, the dummy pcm driver and the serial MIDI driver are found 104*4882a593Smuzhiyunin this directory. In the sub-directories, there is code for components 105*4882a593Smuzhiyunwhich are independent from bus and cpu architectures. 106*4882a593Smuzhiyun 107*4882a593Smuzhiyundrivers/mpu401 108*4882a593Smuzhiyun~~~~~~~~~~~~~~ 109*4882a593Smuzhiyun 110*4882a593SmuzhiyunThe MPU401 and MPU401-UART modules are stored here. 111*4882a593Smuzhiyun 112*4882a593Smuzhiyundrivers/opl3 and opl4 113*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 114*4882a593Smuzhiyun 115*4882a593SmuzhiyunThe OPL3 and OPL4 FM-synth stuff is found here. 116*4882a593Smuzhiyun 117*4882a593Smuzhiyuni2c directory 118*4882a593Smuzhiyun------------- 119*4882a593Smuzhiyun 120*4882a593SmuzhiyunThis contains the ALSA i2c components. 121*4882a593Smuzhiyun 122*4882a593SmuzhiyunAlthough there is a standard i2c layer on Linux, ALSA has its own i2c 123*4882a593Smuzhiyuncode for some cards, because the soundcard needs only a simple operation 124*4882a593Smuzhiyunand the standard i2c API is too complicated for such a purpose. 125*4882a593Smuzhiyun 126*4882a593Smuzhiyunsynth directory 127*4882a593Smuzhiyun--------------- 128*4882a593Smuzhiyun 129*4882a593SmuzhiyunThis contains the synth middle-level modules. 130*4882a593Smuzhiyun 131*4882a593SmuzhiyunSo far, there is only Emu8000/Emu10k1 synth driver under the 132*4882a593Smuzhiyun``synth/emux`` sub-directory. 133*4882a593Smuzhiyun 134*4882a593Smuzhiyunpci directory 135*4882a593Smuzhiyun------------- 136*4882a593Smuzhiyun 137*4882a593SmuzhiyunThis directory and its sub-directories hold the top-level card modules 138*4882a593Smuzhiyunfor PCI soundcards and the code specific to the PCI BUS. 139*4882a593Smuzhiyun 140*4882a593SmuzhiyunThe drivers compiled from a single file are stored directly in the pci 141*4882a593Smuzhiyundirectory, while the drivers with several source files are stored on 142*4882a593Smuzhiyuntheir own sub-directory (e.g. emu10k1, ice1712). 143*4882a593Smuzhiyun 144*4882a593Smuzhiyunisa directory 145*4882a593Smuzhiyun------------- 146*4882a593Smuzhiyun 147*4882a593SmuzhiyunThis directory and its sub-directories hold the top-level card modules 148*4882a593Smuzhiyunfor ISA soundcards. 149*4882a593Smuzhiyun 150*4882a593Smuzhiyunarm, ppc, and sparc directories 151*4882a593Smuzhiyun------------------------------- 152*4882a593Smuzhiyun 153*4882a593SmuzhiyunThey are used for top-level card modules which are specific to one of 154*4882a593Smuzhiyunthese architectures. 155*4882a593Smuzhiyun 156*4882a593Smuzhiyunusb directory 157*4882a593Smuzhiyun------------- 158*4882a593Smuzhiyun 159*4882a593SmuzhiyunThis directory contains the USB-audio driver. In the latest version, the 160*4882a593SmuzhiyunUSB MIDI driver is integrated in the usb-audio driver. 161*4882a593Smuzhiyun 162*4882a593Smuzhiyunpcmcia directory 163*4882a593Smuzhiyun---------------- 164*4882a593Smuzhiyun 165*4882a593SmuzhiyunThe PCMCIA, especially PCCard drivers will go here. CardBus drivers will 166*4882a593Smuzhiyunbe in the pci directory, because their API is identical to that of 167*4882a593Smuzhiyunstandard PCI cards. 168*4882a593Smuzhiyun 169*4882a593Smuzhiyunsoc directory 170*4882a593Smuzhiyun------------- 171*4882a593Smuzhiyun 172*4882a593SmuzhiyunThis directory contains the codes for ASoC (ALSA System on Chip) 173*4882a593Smuzhiyunlayer including ASoC core, codec and machine drivers. 174*4882a593Smuzhiyun 175*4882a593Smuzhiyunoss directory 176*4882a593Smuzhiyun------------- 177*4882a593Smuzhiyun 178*4882a593SmuzhiyunHere contains OSS/Lite codes. 179*4882a593SmuzhiyunAll codes have been deprecated except for dmasound on m68k as of 180*4882a593Smuzhiyunwriting this. 181*4882a593Smuzhiyun 182*4882a593Smuzhiyun 183*4882a593SmuzhiyunBasic Flow for PCI Drivers 184*4882a593Smuzhiyun========================== 185*4882a593Smuzhiyun 186*4882a593SmuzhiyunOutline 187*4882a593Smuzhiyun------- 188*4882a593Smuzhiyun 189*4882a593SmuzhiyunThe minimum flow for PCI soundcards is as follows: 190*4882a593Smuzhiyun 191*4882a593Smuzhiyun- define the PCI ID table (see the section `PCI Entries`_). 192*4882a593Smuzhiyun 193*4882a593Smuzhiyun- create ``probe`` callback. 194*4882a593Smuzhiyun 195*4882a593Smuzhiyun- create ``remove`` callback. 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun- create a struct pci_driver structure 198*4882a593Smuzhiyun containing the three pointers above. 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun- create an ``init`` function just calling the 201*4882a593Smuzhiyun :c:func:`pci_register_driver()` to register the pci_driver 202*4882a593Smuzhiyun table defined above. 203*4882a593Smuzhiyun 204*4882a593Smuzhiyun- create an ``exit`` function to call the 205*4882a593Smuzhiyun :c:func:`pci_unregister_driver()` function. 206*4882a593Smuzhiyun 207*4882a593SmuzhiyunFull Code Example 208*4882a593Smuzhiyun----------------- 209*4882a593Smuzhiyun 210*4882a593SmuzhiyunThe code example is shown below. Some parts are kept unimplemented at 211*4882a593Smuzhiyunthis moment but will be filled in the next sections. The numbers in the 212*4882a593Smuzhiyuncomment lines of the :c:func:`snd_mychip_probe()` function refer 213*4882a593Smuzhiyunto details explained in the following section. 214*4882a593Smuzhiyun 215*4882a593Smuzhiyun:: 216*4882a593Smuzhiyun 217*4882a593Smuzhiyun #include <linux/init.h> 218*4882a593Smuzhiyun #include <linux/pci.h> 219*4882a593Smuzhiyun #include <linux/slab.h> 220*4882a593Smuzhiyun #include <sound/core.h> 221*4882a593Smuzhiyun #include <sound/initval.h> 222*4882a593Smuzhiyun 223*4882a593Smuzhiyun /* module parameters (see "Module Parameters") */ 224*4882a593Smuzhiyun /* SNDRV_CARDS: maximum number of cards supported by this module */ 225*4882a593Smuzhiyun static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 226*4882a593Smuzhiyun static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 227*4882a593Smuzhiyun static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 228*4882a593Smuzhiyun 229*4882a593Smuzhiyun /* definition of the chip-specific record */ 230*4882a593Smuzhiyun struct mychip { 231*4882a593Smuzhiyun struct snd_card *card; 232*4882a593Smuzhiyun /* the rest of the implementation will be in section 233*4882a593Smuzhiyun * "PCI Resource Management" 234*4882a593Smuzhiyun */ 235*4882a593Smuzhiyun }; 236*4882a593Smuzhiyun 237*4882a593Smuzhiyun /* chip-specific destructor 238*4882a593Smuzhiyun * (see "PCI Resource Management") 239*4882a593Smuzhiyun */ 240*4882a593Smuzhiyun static int snd_mychip_free(struct mychip *chip) 241*4882a593Smuzhiyun { 242*4882a593Smuzhiyun .... /* will be implemented later... */ 243*4882a593Smuzhiyun } 244*4882a593Smuzhiyun 245*4882a593Smuzhiyun /* component-destructor 246*4882a593Smuzhiyun * (see "Management of Cards and Components") 247*4882a593Smuzhiyun */ 248*4882a593Smuzhiyun static int snd_mychip_dev_free(struct snd_device *device) 249*4882a593Smuzhiyun { 250*4882a593Smuzhiyun return snd_mychip_free(device->device_data); 251*4882a593Smuzhiyun } 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun /* chip-specific constructor 254*4882a593Smuzhiyun * (see "Management of Cards and Components") 255*4882a593Smuzhiyun */ 256*4882a593Smuzhiyun static int snd_mychip_create(struct snd_card *card, 257*4882a593Smuzhiyun struct pci_dev *pci, 258*4882a593Smuzhiyun struct mychip **rchip) 259*4882a593Smuzhiyun { 260*4882a593Smuzhiyun struct mychip *chip; 261*4882a593Smuzhiyun int err; 262*4882a593Smuzhiyun static const struct snd_device_ops ops = { 263*4882a593Smuzhiyun .dev_free = snd_mychip_dev_free, 264*4882a593Smuzhiyun }; 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun *rchip = NULL; 267*4882a593Smuzhiyun 268*4882a593Smuzhiyun /* check PCI availability here 269*4882a593Smuzhiyun * (see "PCI Resource Management") 270*4882a593Smuzhiyun */ 271*4882a593Smuzhiyun .... 272*4882a593Smuzhiyun 273*4882a593Smuzhiyun /* allocate a chip-specific data with zero filled */ 274*4882a593Smuzhiyun chip = kzalloc(sizeof(*chip), GFP_KERNEL); 275*4882a593Smuzhiyun if (chip == NULL) 276*4882a593Smuzhiyun return -ENOMEM; 277*4882a593Smuzhiyun 278*4882a593Smuzhiyun chip->card = card; 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun /* rest of initialization here; will be implemented 281*4882a593Smuzhiyun * later, see "PCI Resource Management" 282*4882a593Smuzhiyun */ 283*4882a593Smuzhiyun .... 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 286*4882a593Smuzhiyun if (err < 0) { 287*4882a593Smuzhiyun snd_mychip_free(chip); 288*4882a593Smuzhiyun return err; 289*4882a593Smuzhiyun } 290*4882a593Smuzhiyun 291*4882a593Smuzhiyun *rchip = chip; 292*4882a593Smuzhiyun return 0; 293*4882a593Smuzhiyun } 294*4882a593Smuzhiyun 295*4882a593Smuzhiyun /* constructor -- see "Driver Constructor" sub-section */ 296*4882a593Smuzhiyun static int snd_mychip_probe(struct pci_dev *pci, 297*4882a593Smuzhiyun const struct pci_device_id *pci_id) 298*4882a593Smuzhiyun { 299*4882a593Smuzhiyun static int dev; 300*4882a593Smuzhiyun struct snd_card *card; 301*4882a593Smuzhiyun struct mychip *chip; 302*4882a593Smuzhiyun int err; 303*4882a593Smuzhiyun 304*4882a593Smuzhiyun /* (1) */ 305*4882a593Smuzhiyun if (dev >= SNDRV_CARDS) 306*4882a593Smuzhiyun return -ENODEV; 307*4882a593Smuzhiyun if (!enable[dev]) { 308*4882a593Smuzhiyun dev++; 309*4882a593Smuzhiyun return -ENOENT; 310*4882a593Smuzhiyun } 311*4882a593Smuzhiyun 312*4882a593Smuzhiyun /* (2) */ 313*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 314*4882a593Smuzhiyun 0, &card); 315*4882a593Smuzhiyun if (err < 0) 316*4882a593Smuzhiyun return err; 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun /* (3) */ 319*4882a593Smuzhiyun err = snd_mychip_create(card, pci, &chip); 320*4882a593Smuzhiyun if (err < 0) 321*4882a593Smuzhiyun goto error; 322*4882a593Smuzhiyun 323*4882a593Smuzhiyun /* (4) */ 324*4882a593Smuzhiyun strcpy(card->driver, "My Chip"); 325*4882a593Smuzhiyun strcpy(card->shortname, "My Own Chip 123"); 326*4882a593Smuzhiyun sprintf(card->longname, "%s at 0x%lx irq %i", 327*4882a593Smuzhiyun card->shortname, chip->port, chip->irq); 328*4882a593Smuzhiyun 329*4882a593Smuzhiyun /* (5) */ 330*4882a593Smuzhiyun .... /* implemented later */ 331*4882a593Smuzhiyun 332*4882a593Smuzhiyun /* (6) */ 333*4882a593Smuzhiyun err = snd_card_register(card); 334*4882a593Smuzhiyun if (err < 0) 335*4882a593Smuzhiyun goto error; 336*4882a593Smuzhiyun 337*4882a593Smuzhiyun /* (7) */ 338*4882a593Smuzhiyun pci_set_drvdata(pci, card); 339*4882a593Smuzhiyun dev++; 340*4882a593Smuzhiyun return 0; 341*4882a593Smuzhiyun 342*4882a593Smuzhiyun error: 343*4882a593Smuzhiyun snd_card_free(card); 344*4882a593Smuzhiyun return err; 345*4882a593Smuzhiyun } 346*4882a593Smuzhiyun 347*4882a593Smuzhiyun /* destructor -- see the "Destructor" sub-section */ 348*4882a593Smuzhiyun static void snd_mychip_remove(struct pci_dev *pci) 349*4882a593Smuzhiyun { 350*4882a593Smuzhiyun snd_card_free(pci_get_drvdata(pci)); 351*4882a593Smuzhiyun } 352*4882a593Smuzhiyun 353*4882a593Smuzhiyun 354*4882a593Smuzhiyun 355*4882a593SmuzhiyunDriver Constructor 356*4882a593Smuzhiyun------------------ 357*4882a593Smuzhiyun 358*4882a593SmuzhiyunThe real constructor of PCI drivers is the ``probe`` callback. The 359*4882a593Smuzhiyun``probe`` callback and other component-constructors which are called 360*4882a593Smuzhiyunfrom the ``probe`` callback cannot be used with the ``__init`` prefix 361*4882a593Smuzhiyunbecause any PCI device could be a hotplug device. 362*4882a593Smuzhiyun 363*4882a593SmuzhiyunIn the ``probe`` callback, the following scheme is often used. 364*4882a593Smuzhiyun 365*4882a593Smuzhiyun1) Check and increment the device index. 366*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 367*4882a593Smuzhiyun 368*4882a593Smuzhiyun:: 369*4882a593Smuzhiyun 370*4882a593Smuzhiyun static int dev; 371*4882a593Smuzhiyun .... 372*4882a593Smuzhiyun if (dev >= SNDRV_CARDS) 373*4882a593Smuzhiyun return -ENODEV; 374*4882a593Smuzhiyun if (!enable[dev]) { 375*4882a593Smuzhiyun dev++; 376*4882a593Smuzhiyun return -ENOENT; 377*4882a593Smuzhiyun } 378*4882a593Smuzhiyun 379*4882a593Smuzhiyun 380*4882a593Smuzhiyunwhere ``enable[dev]`` is the module option. 381*4882a593Smuzhiyun 382*4882a593SmuzhiyunEach time the ``probe`` callback is called, check the availability of 383*4882a593Smuzhiyunthe device. If not available, simply increment the device index and 384*4882a593Smuzhiyunreturns. dev will be incremented also later (`step 7 385*4882a593Smuzhiyun<#set-the-pci-driver-data-and-return-zero>`__). 386*4882a593Smuzhiyun 387*4882a593Smuzhiyun2) Create a card instance 388*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~ 389*4882a593Smuzhiyun 390*4882a593Smuzhiyun:: 391*4882a593Smuzhiyun 392*4882a593Smuzhiyun struct snd_card *card; 393*4882a593Smuzhiyun int err; 394*4882a593Smuzhiyun .... 395*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 396*4882a593Smuzhiyun 0, &card); 397*4882a593Smuzhiyun 398*4882a593Smuzhiyun 399*4882a593SmuzhiyunThe details will be explained in the section `Management of Cards and 400*4882a593SmuzhiyunComponents`_. 401*4882a593Smuzhiyun 402*4882a593Smuzhiyun3) Create a main component 403*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~ 404*4882a593Smuzhiyun 405*4882a593SmuzhiyunIn this part, the PCI resources are allocated. 406*4882a593Smuzhiyun 407*4882a593Smuzhiyun:: 408*4882a593Smuzhiyun 409*4882a593Smuzhiyun struct mychip *chip; 410*4882a593Smuzhiyun .... 411*4882a593Smuzhiyun err = snd_mychip_create(card, pci, &chip); 412*4882a593Smuzhiyun if (err < 0) 413*4882a593Smuzhiyun goto error; 414*4882a593Smuzhiyun 415*4882a593SmuzhiyunThe details will be explained in the section `PCI Resource 416*4882a593SmuzhiyunManagement`_. 417*4882a593Smuzhiyun 418*4882a593SmuzhiyunWhen something goes wrong, the probe function needs to deal with the 419*4882a593Smuzhiyunerror. In this example, we have a single error handling path placed 420*4882a593Smuzhiyunat the end of the function. 421*4882a593Smuzhiyun 422*4882a593Smuzhiyun:: 423*4882a593Smuzhiyun 424*4882a593Smuzhiyun error: 425*4882a593Smuzhiyun snd_card_free(card); 426*4882a593Smuzhiyun return err; 427*4882a593Smuzhiyun 428*4882a593SmuzhiyunSince each component can be properly freed, the single 429*4882a593Smuzhiyun:c:func:`snd_card_free()` call should suffice in most cases. 430*4882a593Smuzhiyun 431*4882a593Smuzhiyun 432*4882a593Smuzhiyun4) Set the driver ID and name strings. 433*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 434*4882a593Smuzhiyun 435*4882a593Smuzhiyun:: 436*4882a593Smuzhiyun 437*4882a593Smuzhiyun strcpy(card->driver, "My Chip"); 438*4882a593Smuzhiyun strcpy(card->shortname, "My Own Chip 123"); 439*4882a593Smuzhiyun sprintf(card->longname, "%s at 0x%lx irq %i", 440*4882a593Smuzhiyun card->shortname, chip->port, chip->irq); 441*4882a593Smuzhiyun 442*4882a593SmuzhiyunThe driver field holds the minimal ID string of the chip. This is used 443*4882a593Smuzhiyunby alsa-lib's configurator, so keep it simple but unique. Even the 444*4882a593Smuzhiyunsame driver can have different driver IDs to distinguish the 445*4882a593Smuzhiyunfunctionality of each chip type. 446*4882a593Smuzhiyun 447*4882a593SmuzhiyunThe shortname field is a string shown as more verbose name. The longname 448*4882a593Smuzhiyunfield contains the information shown in ``/proc/asound/cards``. 449*4882a593Smuzhiyun 450*4882a593Smuzhiyun5) Create other components, such as mixer, MIDI, etc. 451*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 452*4882a593Smuzhiyun 453*4882a593SmuzhiyunHere you define the basic components such as `PCM <#PCM-Interface>`__, 454*4882a593Smuzhiyunmixer (e.g. `AC97 <#API-for-AC97-Codec>`__), MIDI (e.g. 455*4882a593Smuzhiyun`MPU-401 <#MIDI-MPU401-UART-Interface>`__), and other interfaces. 456*4882a593SmuzhiyunAlso, if you want a `proc file <#Proc-Interface>`__, define it here, 457*4882a593Smuzhiyuntoo. 458*4882a593Smuzhiyun 459*4882a593Smuzhiyun6) Register the card instance. 460*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 461*4882a593Smuzhiyun 462*4882a593Smuzhiyun:: 463*4882a593Smuzhiyun 464*4882a593Smuzhiyun err = snd_card_register(card); 465*4882a593Smuzhiyun if (err < 0) 466*4882a593Smuzhiyun goto error; 467*4882a593Smuzhiyun 468*4882a593SmuzhiyunWill be explained in the section `Management of Cards and 469*4882a593SmuzhiyunComponents`_, too. 470*4882a593Smuzhiyun 471*4882a593Smuzhiyun7) Set the PCI driver data and return zero. 472*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 473*4882a593Smuzhiyun 474*4882a593Smuzhiyun:: 475*4882a593Smuzhiyun 476*4882a593Smuzhiyun pci_set_drvdata(pci, card); 477*4882a593Smuzhiyun dev++; 478*4882a593Smuzhiyun return 0; 479*4882a593Smuzhiyun 480*4882a593SmuzhiyunIn the above, the card record is stored. This pointer is used in the 481*4882a593Smuzhiyunremove callback and power-management callbacks, too. 482*4882a593Smuzhiyun 483*4882a593SmuzhiyunDestructor 484*4882a593Smuzhiyun---------- 485*4882a593Smuzhiyun 486*4882a593SmuzhiyunThe destructor, remove callback, simply releases the card instance. Then 487*4882a593Smuzhiyunthe ALSA middle layer will release all the attached components 488*4882a593Smuzhiyunautomatically. 489*4882a593Smuzhiyun 490*4882a593SmuzhiyunIt would be typically just calling :c:func:`snd_card_free()`: 491*4882a593Smuzhiyun 492*4882a593Smuzhiyun:: 493*4882a593Smuzhiyun 494*4882a593Smuzhiyun static void snd_mychip_remove(struct pci_dev *pci) 495*4882a593Smuzhiyun { 496*4882a593Smuzhiyun snd_card_free(pci_get_drvdata(pci)); 497*4882a593Smuzhiyun } 498*4882a593Smuzhiyun 499*4882a593Smuzhiyun 500*4882a593SmuzhiyunThe above code assumes that the card pointer is set to the PCI driver 501*4882a593Smuzhiyundata. 502*4882a593Smuzhiyun 503*4882a593SmuzhiyunHeader Files 504*4882a593Smuzhiyun------------ 505*4882a593Smuzhiyun 506*4882a593SmuzhiyunFor the above example, at least the following include files are 507*4882a593Smuzhiyunnecessary. 508*4882a593Smuzhiyun 509*4882a593Smuzhiyun:: 510*4882a593Smuzhiyun 511*4882a593Smuzhiyun #include <linux/init.h> 512*4882a593Smuzhiyun #include <linux/pci.h> 513*4882a593Smuzhiyun #include <linux/slab.h> 514*4882a593Smuzhiyun #include <sound/core.h> 515*4882a593Smuzhiyun #include <sound/initval.h> 516*4882a593Smuzhiyun 517*4882a593Smuzhiyunwhere the last one is necessary only when module options are defined 518*4882a593Smuzhiyunin the source file. If the code is split into several files, the files 519*4882a593Smuzhiyunwithout module options don't need them. 520*4882a593Smuzhiyun 521*4882a593SmuzhiyunIn addition to these headers, you'll need ``<linux/interrupt.h>`` for 522*4882a593Smuzhiyuninterrupt handling, and ``<linux/io.h>`` for I/O access. If you use the 523*4882a593Smuzhiyun:c:func:`mdelay()` or :c:func:`udelay()` functions, you'll need 524*4882a593Smuzhiyunto include ``<linux/delay.h>`` too. 525*4882a593Smuzhiyun 526*4882a593SmuzhiyunThe ALSA interfaces like the PCM and control APIs are defined in other 527*4882a593Smuzhiyun``<sound/xxx.h>`` header files. They have to be included after 528*4882a593Smuzhiyun``<sound/core.h>``. 529*4882a593Smuzhiyun 530*4882a593SmuzhiyunManagement of Cards and Components 531*4882a593Smuzhiyun================================== 532*4882a593Smuzhiyun 533*4882a593SmuzhiyunCard Instance 534*4882a593Smuzhiyun------------- 535*4882a593Smuzhiyun 536*4882a593SmuzhiyunFor each soundcard, a “card” record must be allocated. 537*4882a593Smuzhiyun 538*4882a593SmuzhiyunA card record is the headquarters of the soundcard. It manages the whole 539*4882a593Smuzhiyunlist of devices (components) on the soundcard, such as PCM, mixers, 540*4882a593SmuzhiyunMIDI, synthesizer, and so on. Also, the card record holds the ID and the 541*4882a593Smuzhiyunname strings of the card, manages the root of proc files, and controls 542*4882a593Smuzhiyunthe power-management states and hotplug disconnections. The component 543*4882a593Smuzhiyunlist on the card record is used to manage the correct release of 544*4882a593Smuzhiyunresources at destruction. 545*4882a593Smuzhiyun 546*4882a593SmuzhiyunAs mentioned above, to create a card instance, call 547*4882a593Smuzhiyun:c:func:`snd_card_new()`. 548*4882a593Smuzhiyun 549*4882a593Smuzhiyun:: 550*4882a593Smuzhiyun 551*4882a593Smuzhiyun struct snd_card *card; 552*4882a593Smuzhiyun int err; 553*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index, id, module, extra_size, &card); 554*4882a593Smuzhiyun 555*4882a593Smuzhiyun 556*4882a593SmuzhiyunThe function takes six arguments: the parent device pointer, the 557*4882a593Smuzhiyuncard-index number, the id string, the module pointer (usually 558*4882a593Smuzhiyun``THIS_MODULE``), the size of extra-data space, and the pointer to 559*4882a593Smuzhiyunreturn the card instance. The extra_size argument is used to allocate 560*4882a593Smuzhiyuncard->private_data for the chip-specific data. Note that these data are 561*4882a593Smuzhiyunallocated by :c:func:`snd_card_new()`. 562*4882a593Smuzhiyun 563*4882a593SmuzhiyunThe first argument, the pointer of struct device, specifies the parent 564*4882a593Smuzhiyundevice. For PCI devices, typically ``&pci->`` is passed there. 565*4882a593Smuzhiyun 566*4882a593SmuzhiyunComponents 567*4882a593Smuzhiyun---------- 568*4882a593Smuzhiyun 569*4882a593SmuzhiyunAfter the card is created, you can attach the components (devices) to 570*4882a593Smuzhiyunthe card instance. In an ALSA driver, a component is represented as a 571*4882a593Smuzhiyunstruct snd_device object. A component 572*4882a593Smuzhiyuncan be a PCM instance, a control interface, a raw MIDI interface, etc. 573*4882a593SmuzhiyunEach such instance has one component entry. 574*4882a593Smuzhiyun 575*4882a593SmuzhiyunA component can be created via :c:func:`snd_device_new()` 576*4882a593Smuzhiyunfunction. 577*4882a593Smuzhiyun 578*4882a593Smuzhiyun:: 579*4882a593Smuzhiyun 580*4882a593Smuzhiyun snd_device_new(card, SNDRV_DEV_XXX, chip, &ops); 581*4882a593Smuzhiyun 582*4882a593SmuzhiyunThis takes the card pointer, the device-level (``SNDRV_DEV_XXX``), the 583*4882a593Smuzhiyundata pointer, and the callback pointers (``&ops``). The device-level 584*4882a593Smuzhiyundefines the type of components and the order of registration and 585*4882a593Smuzhiyunde-registration. For most components, the device-level is already 586*4882a593Smuzhiyundefined. For a user-defined component, you can use 587*4882a593Smuzhiyun``SNDRV_DEV_LOWLEVEL``. 588*4882a593Smuzhiyun 589*4882a593SmuzhiyunThis function itself doesn't allocate the data space. The data must be 590*4882a593Smuzhiyunallocated manually beforehand, and its pointer is passed as the 591*4882a593Smuzhiyunargument. This pointer (``chip`` in the above example) is used as the 592*4882a593Smuzhiyunidentifier for the instance. 593*4882a593Smuzhiyun 594*4882a593SmuzhiyunEach pre-defined ALSA component such as ac97 and pcm calls 595*4882a593Smuzhiyun:c:func:`snd_device_new()` inside its constructor. The destructor 596*4882a593Smuzhiyunfor each component is defined in the callback pointers. Hence, you don't 597*4882a593Smuzhiyunneed to take care of calling a destructor for such a component. 598*4882a593Smuzhiyun 599*4882a593SmuzhiyunIf you wish to create your own component, you need to set the destructor 600*4882a593Smuzhiyunfunction to the dev_free callback in the ``ops``, so that it can be 601*4882a593Smuzhiyunreleased automatically via :c:func:`snd_card_free()`. The next 602*4882a593Smuzhiyunexample will show an implementation of chip-specific data. 603*4882a593Smuzhiyun 604*4882a593SmuzhiyunChip-Specific Data 605*4882a593Smuzhiyun------------------ 606*4882a593Smuzhiyun 607*4882a593SmuzhiyunChip-specific information, e.g. the I/O port address, its resource 608*4882a593Smuzhiyunpointer, or the irq number, is stored in the chip-specific record. 609*4882a593Smuzhiyun 610*4882a593Smuzhiyun:: 611*4882a593Smuzhiyun 612*4882a593Smuzhiyun struct mychip { 613*4882a593Smuzhiyun .... 614*4882a593Smuzhiyun }; 615*4882a593Smuzhiyun 616*4882a593Smuzhiyun 617*4882a593SmuzhiyunIn general, there are two ways of allocating the chip record. 618*4882a593Smuzhiyun 619*4882a593Smuzhiyun1. Allocating via :c:func:`snd_card_new()`. 620*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 621*4882a593Smuzhiyun 622*4882a593SmuzhiyunAs mentioned above, you can pass the extra-data-length to the 5th 623*4882a593Smuzhiyunargument of :c:func:`snd_card_new()`, i.e. 624*4882a593Smuzhiyun 625*4882a593Smuzhiyun:: 626*4882a593Smuzhiyun 627*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 628*4882a593Smuzhiyun sizeof(struct mychip), &card); 629*4882a593Smuzhiyun 630*4882a593Smuzhiyunstruct mychip is the type of the chip record. 631*4882a593Smuzhiyun 632*4882a593SmuzhiyunIn return, the allocated record can be accessed as 633*4882a593Smuzhiyun 634*4882a593Smuzhiyun:: 635*4882a593Smuzhiyun 636*4882a593Smuzhiyun struct mychip *chip = card->private_data; 637*4882a593Smuzhiyun 638*4882a593SmuzhiyunWith this method, you don't have to allocate twice. The record is 639*4882a593Smuzhiyunreleased together with the card instance. 640*4882a593Smuzhiyun 641*4882a593Smuzhiyun2. Allocating an extra device. 642*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 643*4882a593Smuzhiyun 644*4882a593SmuzhiyunAfter allocating a card instance via :c:func:`snd_card_new()` 645*4882a593Smuzhiyun(with ``0`` on the 4th arg), call :c:func:`kzalloc()`. 646*4882a593Smuzhiyun 647*4882a593Smuzhiyun:: 648*4882a593Smuzhiyun 649*4882a593Smuzhiyun struct snd_card *card; 650*4882a593Smuzhiyun struct mychip *chip; 651*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 652*4882a593Smuzhiyun 0, &card); 653*4882a593Smuzhiyun ..... 654*4882a593Smuzhiyun chip = kzalloc(sizeof(*chip), GFP_KERNEL); 655*4882a593Smuzhiyun 656*4882a593SmuzhiyunThe chip record should have the field to hold the card pointer at least, 657*4882a593Smuzhiyun 658*4882a593Smuzhiyun:: 659*4882a593Smuzhiyun 660*4882a593Smuzhiyun struct mychip { 661*4882a593Smuzhiyun struct snd_card *card; 662*4882a593Smuzhiyun .... 663*4882a593Smuzhiyun }; 664*4882a593Smuzhiyun 665*4882a593Smuzhiyun 666*4882a593SmuzhiyunThen, set the card pointer in the returned chip instance. 667*4882a593Smuzhiyun 668*4882a593Smuzhiyun:: 669*4882a593Smuzhiyun 670*4882a593Smuzhiyun chip->card = card; 671*4882a593Smuzhiyun 672*4882a593SmuzhiyunNext, initialize the fields, and register this chip record as a 673*4882a593Smuzhiyunlow-level device with a specified ``ops``, 674*4882a593Smuzhiyun 675*4882a593Smuzhiyun:: 676*4882a593Smuzhiyun 677*4882a593Smuzhiyun static const struct snd_device_ops ops = { 678*4882a593Smuzhiyun .dev_free = snd_mychip_dev_free, 679*4882a593Smuzhiyun }; 680*4882a593Smuzhiyun .... 681*4882a593Smuzhiyun snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 682*4882a593Smuzhiyun 683*4882a593Smuzhiyun:c:func:`snd_mychip_dev_free()` is the device-destructor 684*4882a593Smuzhiyunfunction, which will call the real destructor. 685*4882a593Smuzhiyun 686*4882a593Smuzhiyun:: 687*4882a593Smuzhiyun 688*4882a593Smuzhiyun static int snd_mychip_dev_free(struct snd_device *device) 689*4882a593Smuzhiyun { 690*4882a593Smuzhiyun return snd_mychip_free(device->device_data); 691*4882a593Smuzhiyun } 692*4882a593Smuzhiyun 693*4882a593Smuzhiyunwhere :c:func:`snd_mychip_free()` is the real destructor. 694*4882a593Smuzhiyun 695*4882a593SmuzhiyunThe demerit of this method is the obviously more amount of codes. 696*4882a593SmuzhiyunThe merit is, however, you can trigger the own callback at registering 697*4882a593Smuzhiyunand disconnecting the card via setting in snd_device_ops. 698*4882a593SmuzhiyunAbout the registering and disconnecting the card, see the subsections 699*4882a593Smuzhiyunbelow. 700*4882a593Smuzhiyun 701*4882a593Smuzhiyun 702*4882a593SmuzhiyunRegistration and Release 703*4882a593Smuzhiyun------------------------ 704*4882a593Smuzhiyun 705*4882a593SmuzhiyunAfter all components are assigned, register the card instance by calling 706*4882a593Smuzhiyun:c:func:`snd_card_register()`. Access to the device files is 707*4882a593Smuzhiyunenabled at this point. That is, before 708*4882a593Smuzhiyun:c:func:`snd_card_register()` is called, the components are safely 709*4882a593Smuzhiyuninaccessible from external side. If this call fails, exit the probe 710*4882a593Smuzhiyunfunction after releasing the card via :c:func:`snd_card_free()`. 711*4882a593Smuzhiyun 712*4882a593SmuzhiyunFor releasing the card instance, you can call simply 713*4882a593Smuzhiyun:c:func:`snd_card_free()`. As mentioned earlier, all components 714*4882a593Smuzhiyunare released automatically by this call. 715*4882a593Smuzhiyun 716*4882a593SmuzhiyunFor a device which allows hotplugging, you can use 717*4882a593Smuzhiyun:c:func:`snd_card_free_when_closed()`. This one will postpone 718*4882a593Smuzhiyunthe destruction until all devices are closed. 719*4882a593Smuzhiyun 720*4882a593SmuzhiyunPCI Resource Management 721*4882a593Smuzhiyun======================= 722*4882a593Smuzhiyun 723*4882a593SmuzhiyunFull Code Example 724*4882a593Smuzhiyun----------------- 725*4882a593Smuzhiyun 726*4882a593SmuzhiyunIn this section, we'll complete the chip-specific constructor, 727*4882a593Smuzhiyundestructor and PCI entries. Example code is shown first, below. 728*4882a593Smuzhiyun 729*4882a593Smuzhiyun:: 730*4882a593Smuzhiyun 731*4882a593Smuzhiyun struct mychip { 732*4882a593Smuzhiyun struct snd_card *card; 733*4882a593Smuzhiyun struct pci_dev *pci; 734*4882a593Smuzhiyun 735*4882a593Smuzhiyun unsigned long port; 736*4882a593Smuzhiyun int irq; 737*4882a593Smuzhiyun }; 738*4882a593Smuzhiyun 739*4882a593Smuzhiyun static int snd_mychip_free(struct mychip *chip) 740*4882a593Smuzhiyun { 741*4882a593Smuzhiyun /* disable hardware here if any */ 742*4882a593Smuzhiyun .... /* (not implemented in this document) */ 743*4882a593Smuzhiyun 744*4882a593Smuzhiyun /* release the irq */ 745*4882a593Smuzhiyun if (chip->irq >= 0) 746*4882a593Smuzhiyun free_irq(chip->irq, chip); 747*4882a593Smuzhiyun /* release the I/O ports & memory */ 748*4882a593Smuzhiyun pci_release_regions(chip->pci); 749*4882a593Smuzhiyun /* disable the PCI entry */ 750*4882a593Smuzhiyun pci_disable_device(chip->pci); 751*4882a593Smuzhiyun /* release the data */ 752*4882a593Smuzhiyun kfree(chip); 753*4882a593Smuzhiyun return 0; 754*4882a593Smuzhiyun } 755*4882a593Smuzhiyun 756*4882a593Smuzhiyun /* chip-specific constructor */ 757*4882a593Smuzhiyun static int snd_mychip_create(struct snd_card *card, 758*4882a593Smuzhiyun struct pci_dev *pci, 759*4882a593Smuzhiyun struct mychip **rchip) 760*4882a593Smuzhiyun { 761*4882a593Smuzhiyun struct mychip *chip; 762*4882a593Smuzhiyun int err; 763*4882a593Smuzhiyun static const struct snd_device_ops ops = { 764*4882a593Smuzhiyun .dev_free = snd_mychip_dev_free, 765*4882a593Smuzhiyun }; 766*4882a593Smuzhiyun 767*4882a593Smuzhiyun *rchip = NULL; 768*4882a593Smuzhiyun 769*4882a593Smuzhiyun /* initialize the PCI entry */ 770*4882a593Smuzhiyun err = pci_enable_device(pci); 771*4882a593Smuzhiyun if (err < 0) 772*4882a593Smuzhiyun return err; 773*4882a593Smuzhiyun /* check PCI availability (28bit DMA) */ 774*4882a593Smuzhiyun if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 || 775*4882a593Smuzhiyun pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) { 776*4882a593Smuzhiyun printk(KERN_ERR "error to set 28bit mask DMA\n"); 777*4882a593Smuzhiyun pci_disable_device(pci); 778*4882a593Smuzhiyun return -ENXIO; 779*4882a593Smuzhiyun } 780*4882a593Smuzhiyun 781*4882a593Smuzhiyun chip = kzalloc(sizeof(*chip), GFP_KERNEL); 782*4882a593Smuzhiyun if (chip == NULL) { 783*4882a593Smuzhiyun pci_disable_device(pci); 784*4882a593Smuzhiyun return -ENOMEM; 785*4882a593Smuzhiyun } 786*4882a593Smuzhiyun 787*4882a593Smuzhiyun /* initialize the stuff */ 788*4882a593Smuzhiyun chip->card = card; 789*4882a593Smuzhiyun chip->pci = pci; 790*4882a593Smuzhiyun chip->irq = -1; 791*4882a593Smuzhiyun 792*4882a593Smuzhiyun /* (1) PCI resource allocation */ 793*4882a593Smuzhiyun err = pci_request_regions(pci, "My Chip"); 794*4882a593Smuzhiyun if (err < 0) { 795*4882a593Smuzhiyun kfree(chip); 796*4882a593Smuzhiyun pci_disable_device(pci); 797*4882a593Smuzhiyun return err; 798*4882a593Smuzhiyun } 799*4882a593Smuzhiyun chip->port = pci_resource_start(pci, 0); 800*4882a593Smuzhiyun if (request_irq(pci->irq, snd_mychip_interrupt, 801*4882a593Smuzhiyun IRQF_SHARED, KBUILD_MODNAME, chip)) { 802*4882a593Smuzhiyun printk(KERN_ERR "cannot grab irq %d\n", pci->irq); 803*4882a593Smuzhiyun snd_mychip_free(chip); 804*4882a593Smuzhiyun return -EBUSY; 805*4882a593Smuzhiyun } 806*4882a593Smuzhiyun chip->irq = pci->irq; 807*4882a593Smuzhiyun card->sync_irq = chip->irq; 808*4882a593Smuzhiyun 809*4882a593Smuzhiyun /* (2) initialization of the chip hardware */ 810*4882a593Smuzhiyun .... /* (not implemented in this document) */ 811*4882a593Smuzhiyun 812*4882a593Smuzhiyun err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); 813*4882a593Smuzhiyun if (err < 0) { 814*4882a593Smuzhiyun snd_mychip_free(chip); 815*4882a593Smuzhiyun return err; 816*4882a593Smuzhiyun } 817*4882a593Smuzhiyun 818*4882a593Smuzhiyun *rchip = chip; 819*4882a593Smuzhiyun return 0; 820*4882a593Smuzhiyun } 821*4882a593Smuzhiyun 822*4882a593Smuzhiyun /* PCI IDs */ 823*4882a593Smuzhiyun static struct pci_device_id snd_mychip_ids[] = { 824*4882a593Smuzhiyun { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, 825*4882a593Smuzhiyun PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, 826*4882a593Smuzhiyun .... 827*4882a593Smuzhiyun { 0, } 828*4882a593Smuzhiyun }; 829*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, snd_mychip_ids); 830*4882a593Smuzhiyun 831*4882a593Smuzhiyun /* pci_driver definition */ 832*4882a593Smuzhiyun static struct pci_driver driver = { 833*4882a593Smuzhiyun .name = KBUILD_MODNAME, 834*4882a593Smuzhiyun .id_table = snd_mychip_ids, 835*4882a593Smuzhiyun .probe = snd_mychip_probe, 836*4882a593Smuzhiyun .remove = snd_mychip_remove, 837*4882a593Smuzhiyun }; 838*4882a593Smuzhiyun 839*4882a593Smuzhiyun /* module initialization */ 840*4882a593Smuzhiyun static int __init alsa_card_mychip_init(void) 841*4882a593Smuzhiyun { 842*4882a593Smuzhiyun return pci_register_driver(&driver); 843*4882a593Smuzhiyun } 844*4882a593Smuzhiyun 845*4882a593Smuzhiyun /* module clean up */ 846*4882a593Smuzhiyun static void __exit alsa_card_mychip_exit(void) 847*4882a593Smuzhiyun { 848*4882a593Smuzhiyun pci_unregister_driver(&driver); 849*4882a593Smuzhiyun } 850*4882a593Smuzhiyun 851*4882a593Smuzhiyun module_init(alsa_card_mychip_init) 852*4882a593Smuzhiyun module_exit(alsa_card_mychip_exit) 853*4882a593Smuzhiyun 854*4882a593Smuzhiyun EXPORT_NO_SYMBOLS; /* for old kernels only */ 855*4882a593Smuzhiyun 856*4882a593SmuzhiyunSome Hafta's 857*4882a593Smuzhiyun------------ 858*4882a593Smuzhiyun 859*4882a593SmuzhiyunThe allocation of PCI resources is done in the ``probe`` function, and 860*4882a593Smuzhiyunusually an extra :c:func:`xxx_create()` function is written for this 861*4882a593Smuzhiyunpurpose. 862*4882a593Smuzhiyun 863*4882a593SmuzhiyunIn the case of PCI devices, you first have to call the 864*4882a593Smuzhiyun:c:func:`pci_enable_device()` function before allocating 865*4882a593Smuzhiyunresources. Also, you need to set the proper PCI DMA mask to limit the 866*4882a593Smuzhiyunaccessed I/O range. In some cases, you might need to call 867*4882a593Smuzhiyun:c:func:`pci_set_master()` function, too. 868*4882a593Smuzhiyun 869*4882a593SmuzhiyunSuppose the 28bit mask, and the code to be added would be like: 870*4882a593Smuzhiyun 871*4882a593Smuzhiyun:: 872*4882a593Smuzhiyun 873*4882a593Smuzhiyun err = pci_enable_device(pci); 874*4882a593Smuzhiyun if (err < 0) 875*4882a593Smuzhiyun return err; 876*4882a593Smuzhiyun if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 || 877*4882a593Smuzhiyun pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) { 878*4882a593Smuzhiyun printk(KERN_ERR "error to set 28bit mask DMA\n"); 879*4882a593Smuzhiyun pci_disable_device(pci); 880*4882a593Smuzhiyun return -ENXIO; 881*4882a593Smuzhiyun } 882*4882a593Smuzhiyun 883*4882a593Smuzhiyun 884*4882a593SmuzhiyunResource Allocation 885*4882a593Smuzhiyun------------------- 886*4882a593Smuzhiyun 887*4882a593SmuzhiyunThe allocation of I/O ports and irqs is done via standard kernel 888*4882a593Smuzhiyunfunctions. These resources must be released in the destructor 889*4882a593Smuzhiyunfunction (see below). 890*4882a593Smuzhiyun 891*4882a593SmuzhiyunNow assume that the PCI device has an I/O port with 8 bytes and an 892*4882a593Smuzhiyuninterrupt. Then struct mychip will have the 893*4882a593Smuzhiyunfollowing fields: 894*4882a593Smuzhiyun 895*4882a593Smuzhiyun:: 896*4882a593Smuzhiyun 897*4882a593Smuzhiyun struct mychip { 898*4882a593Smuzhiyun struct snd_card *card; 899*4882a593Smuzhiyun 900*4882a593Smuzhiyun unsigned long port; 901*4882a593Smuzhiyun int irq; 902*4882a593Smuzhiyun }; 903*4882a593Smuzhiyun 904*4882a593Smuzhiyun 905*4882a593SmuzhiyunFor an I/O port (and also a memory region), you need to have the 906*4882a593Smuzhiyunresource pointer for the standard resource management. For an irq, you 907*4882a593Smuzhiyunhave to keep only the irq number (integer). But you need to initialize 908*4882a593Smuzhiyunthis number as -1 before actual allocation, since irq 0 is valid. The 909*4882a593Smuzhiyunport address and its resource pointer can be initialized as null by 910*4882a593Smuzhiyun:c:func:`kzalloc()` automatically, so you don't have to take care of 911*4882a593Smuzhiyunresetting them. 912*4882a593Smuzhiyun 913*4882a593SmuzhiyunThe allocation of an I/O port is done like this: 914*4882a593Smuzhiyun 915*4882a593Smuzhiyun:: 916*4882a593Smuzhiyun 917*4882a593Smuzhiyun err = pci_request_regions(pci, "My Chip"); 918*4882a593Smuzhiyun if (err < 0) { 919*4882a593Smuzhiyun kfree(chip); 920*4882a593Smuzhiyun pci_disable_device(pci); 921*4882a593Smuzhiyun return err; 922*4882a593Smuzhiyun } 923*4882a593Smuzhiyun chip->port = pci_resource_start(pci, 0); 924*4882a593Smuzhiyun 925*4882a593SmuzhiyunIt will reserve the I/O port region of 8 bytes of the given PCI device. 926*4882a593SmuzhiyunThe returned value, ``chip->res_port``, is allocated via 927*4882a593Smuzhiyun:c:func:`kmalloc()` by :c:func:`request_region()`. The pointer 928*4882a593Smuzhiyunmust be released via :c:func:`kfree()`, but there is a problem with 929*4882a593Smuzhiyunthis. This issue will be explained later. 930*4882a593Smuzhiyun 931*4882a593SmuzhiyunThe allocation of an interrupt source is done like this: 932*4882a593Smuzhiyun 933*4882a593Smuzhiyun:: 934*4882a593Smuzhiyun 935*4882a593Smuzhiyun if (request_irq(pci->irq, snd_mychip_interrupt, 936*4882a593Smuzhiyun IRQF_SHARED, KBUILD_MODNAME, chip)) { 937*4882a593Smuzhiyun printk(KERN_ERR "cannot grab irq %d\n", pci->irq); 938*4882a593Smuzhiyun snd_mychip_free(chip); 939*4882a593Smuzhiyun return -EBUSY; 940*4882a593Smuzhiyun } 941*4882a593Smuzhiyun chip->irq = pci->irq; 942*4882a593Smuzhiyun 943*4882a593Smuzhiyunwhere :c:func:`snd_mychip_interrupt()` is the interrupt handler 944*4882a593Smuzhiyundefined `later <#pcm-interface-interrupt-handler>`__. Note that 945*4882a593Smuzhiyun``chip->irq`` should be defined only when :c:func:`request_irq()` 946*4882a593Smuzhiyunsucceeded. 947*4882a593Smuzhiyun 948*4882a593SmuzhiyunOn the PCI bus, interrupts can be shared. Thus, ``IRQF_SHARED`` is used 949*4882a593Smuzhiyunas the interrupt flag of :c:func:`request_irq()`. 950*4882a593Smuzhiyun 951*4882a593SmuzhiyunThe last argument of :c:func:`request_irq()` is the data pointer 952*4882a593Smuzhiyunpassed to the interrupt handler. Usually, the chip-specific record is 953*4882a593Smuzhiyunused for that, but you can use what you like, too. 954*4882a593Smuzhiyun 955*4882a593SmuzhiyunI won't give details about the interrupt handler at this point, but at 956*4882a593Smuzhiyunleast its appearance can be explained now. The interrupt handler looks 957*4882a593Smuzhiyunusually like the following: 958*4882a593Smuzhiyun 959*4882a593Smuzhiyun:: 960*4882a593Smuzhiyun 961*4882a593Smuzhiyun static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) 962*4882a593Smuzhiyun { 963*4882a593Smuzhiyun struct mychip *chip = dev_id; 964*4882a593Smuzhiyun .... 965*4882a593Smuzhiyun return IRQ_HANDLED; 966*4882a593Smuzhiyun } 967*4882a593Smuzhiyun 968*4882a593SmuzhiyunAfter requesting the IRQ, you can passed it to ``card->sync_irq`` 969*4882a593Smuzhiyunfield: 970*4882a593Smuzhiyun:: 971*4882a593Smuzhiyun 972*4882a593Smuzhiyun card->irq = chip->irq; 973*4882a593Smuzhiyun 974*4882a593SmuzhiyunThis allows PCM core automatically performing 975*4882a593Smuzhiyun:c:func:`synchronize_irq()` at the necessary timing like ``hw_free``. 976*4882a593SmuzhiyunSee the later section `sync_stop callback`_ for details. 977*4882a593Smuzhiyun 978*4882a593SmuzhiyunNow let's write the corresponding destructor for the resources above. 979*4882a593SmuzhiyunThe role of destructor is simple: disable the hardware (if already 980*4882a593Smuzhiyunactivated) and release the resources. So far, we have no hardware part, 981*4882a593Smuzhiyunso the disabling code is not written here. 982*4882a593Smuzhiyun 983*4882a593SmuzhiyunTo release the resources, the “check-and-release” method is a safer way. 984*4882a593SmuzhiyunFor the interrupt, do like this: 985*4882a593Smuzhiyun 986*4882a593Smuzhiyun:: 987*4882a593Smuzhiyun 988*4882a593Smuzhiyun if (chip->irq >= 0) 989*4882a593Smuzhiyun free_irq(chip->irq, chip); 990*4882a593Smuzhiyun 991*4882a593SmuzhiyunSince the irq number can start from 0, you should initialize 992*4882a593Smuzhiyun``chip->irq`` with a negative value (e.g. -1), so that you can check 993*4882a593Smuzhiyunthe validity of the irq number as above. 994*4882a593Smuzhiyun 995*4882a593SmuzhiyunWhen you requested I/O ports or memory regions via 996*4882a593Smuzhiyun:c:func:`pci_request_region()` or 997*4882a593Smuzhiyun:c:func:`pci_request_regions()` like in this example, release the 998*4882a593Smuzhiyunresource(s) using the corresponding function, 999*4882a593Smuzhiyun:c:func:`pci_release_region()` or 1000*4882a593Smuzhiyun:c:func:`pci_release_regions()`. 1001*4882a593Smuzhiyun 1002*4882a593Smuzhiyun:: 1003*4882a593Smuzhiyun 1004*4882a593Smuzhiyun pci_release_regions(chip->pci); 1005*4882a593Smuzhiyun 1006*4882a593SmuzhiyunWhen you requested manually via :c:func:`request_region()` or 1007*4882a593Smuzhiyun:c:func:`request_mem_region()`, you can release it via 1008*4882a593Smuzhiyun:c:func:`release_resource()`. Suppose that you keep the resource 1009*4882a593Smuzhiyunpointer returned from :c:func:`request_region()` in 1010*4882a593Smuzhiyunchip->res_port, the release procedure looks like: 1011*4882a593Smuzhiyun 1012*4882a593Smuzhiyun:: 1013*4882a593Smuzhiyun 1014*4882a593Smuzhiyun release_and_free_resource(chip->res_port); 1015*4882a593Smuzhiyun 1016*4882a593SmuzhiyunDon't forget to call :c:func:`pci_disable_device()` before the 1017*4882a593Smuzhiyunend. 1018*4882a593Smuzhiyun 1019*4882a593SmuzhiyunAnd finally, release the chip-specific record. 1020*4882a593Smuzhiyun 1021*4882a593Smuzhiyun:: 1022*4882a593Smuzhiyun 1023*4882a593Smuzhiyun kfree(chip); 1024*4882a593Smuzhiyun 1025*4882a593SmuzhiyunWe didn't implement the hardware disabling part in the above. If you 1026*4882a593Smuzhiyunneed to do this, please note that the destructor may be called even 1027*4882a593Smuzhiyunbefore the initialization of the chip is completed. It would be better 1028*4882a593Smuzhiyunto have a flag to skip hardware disabling if the hardware was not 1029*4882a593Smuzhiyuninitialized yet. 1030*4882a593Smuzhiyun 1031*4882a593SmuzhiyunWhen the chip-data is assigned to the card using 1032*4882a593Smuzhiyun:c:func:`snd_device_new()` with ``SNDRV_DEV_LOWLELVEL`` , its 1033*4882a593Smuzhiyundestructor is called at the last. That is, it is assured that all other 1034*4882a593Smuzhiyuncomponents like PCMs and controls have already been released. You don't 1035*4882a593Smuzhiyunhave to stop PCMs, etc. explicitly, but just call low-level hardware 1036*4882a593Smuzhiyunstopping. 1037*4882a593Smuzhiyun 1038*4882a593SmuzhiyunThe management of a memory-mapped region is almost as same as the 1039*4882a593Smuzhiyunmanagement of an I/O port. You'll need three fields like the 1040*4882a593Smuzhiyunfollowing: 1041*4882a593Smuzhiyun 1042*4882a593Smuzhiyun:: 1043*4882a593Smuzhiyun 1044*4882a593Smuzhiyun struct mychip { 1045*4882a593Smuzhiyun .... 1046*4882a593Smuzhiyun unsigned long iobase_phys; 1047*4882a593Smuzhiyun void __iomem *iobase_virt; 1048*4882a593Smuzhiyun }; 1049*4882a593Smuzhiyun 1050*4882a593Smuzhiyunand the allocation would be like below: 1051*4882a593Smuzhiyun 1052*4882a593Smuzhiyun:: 1053*4882a593Smuzhiyun 1054*4882a593Smuzhiyun err = pci_request_regions(pci, "My Chip"); 1055*4882a593Smuzhiyun if (err < 0) { 1056*4882a593Smuzhiyun kfree(chip); 1057*4882a593Smuzhiyun return err; 1058*4882a593Smuzhiyun } 1059*4882a593Smuzhiyun chip->iobase_phys = pci_resource_start(pci, 0); 1060*4882a593Smuzhiyun chip->iobase_virt = ioremap(chip->iobase_phys, 1061*4882a593Smuzhiyun pci_resource_len(pci, 0)); 1062*4882a593Smuzhiyun 1063*4882a593Smuzhiyunand the corresponding destructor would be: 1064*4882a593Smuzhiyun 1065*4882a593Smuzhiyun:: 1066*4882a593Smuzhiyun 1067*4882a593Smuzhiyun static int snd_mychip_free(struct mychip *chip) 1068*4882a593Smuzhiyun { 1069*4882a593Smuzhiyun .... 1070*4882a593Smuzhiyun if (chip->iobase_virt) 1071*4882a593Smuzhiyun iounmap(chip->iobase_virt); 1072*4882a593Smuzhiyun .... 1073*4882a593Smuzhiyun pci_release_regions(chip->pci); 1074*4882a593Smuzhiyun .... 1075*4882a593Smuzhiyun } 1076*4882a593Smuzhiyun 1077*4882a593SmuzhiyunOf course, a modern way with :c:func:`pci_iomap()` will make things a 1078*4882a593Smuzhiyunbit easier, too. 1079*4882a593Smuzhiyun 1080*4882a593Smuzhiyun:: 1081*4882a593Smuzhiyun 1082*4882a593Smuzhiyun err = pci_request_regions(pci, "My Chip"); 1083*4882a593Smuzhiyun if (err < 0) { 1084*4882a593Smuzhiyun kfree(chip); 1085*4882a593Smuzhiyun return err; 1086*4882a593Smuzhiyun } 1087*4882a593Smuzhiyun chip->iobase_virt = pci_iomap(pci, 0, 0); 1088*4882a593Smuzhiyun 1089*4882a593Smuzhiyunwhich is paired with :c:func:`pci_iounmap()` at destructor. 1090*4882a593Smuzhiyun 1091*4882a593Smuzhiyun 1092*4882a593SmuzhiyunPCI Entries 1093*4882a593Smuzhiyun----------- 1094*4882a593Smuzhiyun 1095*4882a593SmuzhiyunSo far, so good. Let's finish the missing PCI stuff. At first, we need a 1096*4882a593Smuzhiyunstruct pci_device_id table for 1097*4882a593Smuzhiyunthis chipset. It's a table of PCI vendor/device ID number, and some 1098*4882a593Smuzhiyunmasks. 1099*4882a593Smuzhiyun 1100*4882a593SmuzhiyunFor example, 1101*4882a593Smuzhiyun 1102*4882a593Smuzhiyun:: 1103*4882a593Smuzhiyun 1104*4882a593Smuzhiyun static struct pci_device_id snd_mychip_ids[] = { 1105*4882a593Smuzhiyun { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, 1106*4882a593Smuzhiyun PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, 1107*4882a593Smuzhiyun .... 1108*4882a593Smuzhiyun { 0, } 1109*4882a593Smuzhiyun }; 1110*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, snd_mychip_ids); 1111*4882a593Smuzhiyun 1112*4882a593SmuzhiyunThe first and second fields of the struct pci_device_id are the vendor 1113*4882a593Smuzhiyunand device IDs. If you have no reason to filter the matching devices, you can 1114*4882a593Smuzhiyunleave the remaining fields as above. The last field of the 1115*4882a593Smuzhiyunstruct pci_device_id contains private data for this entry. You can specify 1116*4882a593Smuzhiyunany value here, for example, to define specific operations for supported 1117*4882a593Smuzhiyundevice IDs. Such an example is found in the intel8x0 driver. 1118*4882a593Smuzhiyun 1119*4882a593SmuzhiyunThe last entry of this list is the terminator. You must specify this 1120*4882a593Smuzhiyunall-zero entry. 1121*4882a593Smuzhiyun 1122*4882a593SmuzhiyunThen, prepare the struct pci_driver 1123*4882a593Smuzhiyunrecord: 1124*4882a593Smuzhiyun 1125*4882a593Smuzhiyun:: 1126*4882a593Smuzhiyun 1127*4882a593Smuzhiyun static struct pci_driver driver = { 1128*4882a593Smuzhiyun .name = KBUILD_MODNAME, 1129*4882a593Smuzhiyun .id_table = snd_mychip_ids, 1130*4882a593Smuzhiyun .probe = snd_mychip_probe, 1131*4882a593Smuzhiyun .remove = snd_mychip_remove, 1132*4882a593Smuzhiyun }; 1133*4882a593Smuzhiyun 1134*4882a593SmuzhiyunThe ``probe`` and ``remove`` functions have already been defined in 1135*4882a593Smuzhiyunthe previous sections. The ``name`` field is the name string of this 1136*4882a593Smuzhiyundevice. Note that you must not use a slash “/” in this string. 1137*4882a593Smuzhiyun 1138*4882a593SmuzhiyunAnd at last, the module entries: 1139*4882a593Smuzhiyun 1140*4882a593Smuzhiyun:: 1141*4882a593Smuzhiyun 1142*4882a593Smuzhiyun static int __init alsa_card_mychip_init(void) 1143*4882a593Smuzhiyun { 1144*4882a593Smuzhiyun return pci_register_driver(&driver); 1145*4882a593Smuzhiyun } 1146*4882a593Smuzhiyun 1147*4882a593Smuzhiyun static void __exit alsa_card_mychip_exit(void) 1148*4882a593Smuzhiyun { 1149*4882a593Smuzhiyun pci_unregister_driver(&driver); 1150*4882a593Smuzhiyun } 1151*4882a593Smuzhiyun 1152*4882a593Smuzhiyun module_init(alsa_card_mychip_init) 1153*4882a593Smuzhiyun module_exit(alsa_card_mychip_exit) 1154*4882a593Smuzhiyun 1155*4882a593SmuzhiyunNote that these module entries are tagged with ``__init`` and ``__exit`` 1156*4882a593Smuzhiyunprefixes. 1157*4882a593Smuzhiyun 1158*4882a593SmuzhiyunThat's all! 1159*4882a593Smuzhiyun 1160*4882a593SmuzhiyunPCM Interface 1161*4882a593Smuzhiyun============= 1162*4882a593Smuzhiyun 1163*4882a593SmuzhiyunGeneral 1164*4882a593Smuzhiyun------- 1165*4882a593Smuzhiyun 1166*4882a593SmuzhiyunThe PCM middle layer of ALSA is quite powerful and it is only necessary 1167*4882a593Smuzhiyunfor each driver to implement the low-level functions to access its 1168*4882a593Smuzhiyunhardware. 1169*4882a593Smuzhiyun 1170*4882a593SmuzhiyunFor accessing to the PCM layer, you need to include ``<sound/pcm.h>`` 1171*4882a593Smuzhiyunfirst. In addition, ``<sound/pcm_params.h>`` might be needed if you 1172*4882a593Smuzhiyunaccess to some functions related with hw_param. 1173*4882a593Smuzhiyun 1174*4882a593SmuzhiyunEach card device can have up to four pcm instances. A pcm instance 1175*4882a593Smuzhiyuncorresponds to a pcm device file. The limitation of number of instances 1176*4882a593Smuzhiyuncomes only from the available bit size of the Linux's device numbers. 1177*4882a593SmuzhiyunOnce when 64bit device number is used, we'll have more pcm instances 1178*4882a593Smuzhiyunavailable. 1179*4882a593Smuzhiyun 1180*4882a593SmuzhiyunA pcm instance consists of pcm playback and capture streams, and each 1181*4882a593Smuzhiyunpcm stream consists of one or more pcm substreams. Some soundcards 1182*4882a593Smuzhiyunsupport multiple playback functions. For example, emu10k1 has a PCM 1183*4882a593Smuzhiyunplayback of 32 stereo substreams. In this case, at each open, a free 1184*4882a593Smuzhiyunsubstream is (usually) automatically chosen and opened. Meanwhile, when 1185*4882a593Smuzhiyunonly one substream exists and it was already opened, the successful open 1186*4882a593Smuzhiyunwill either block or error with ``EAGAIN`` according to the file open 1187*4882a593Smuzhiyunmode. But you don't have to care about such details in your driver. The 1188*4882a593SmuzhiyunPCM middle layer will take care of such work. 1189*4882a593Smuzhiyun 1190*4882a593SmuzhiyunFull Code Example 1191*4882a593Smuzhiyun----------------- 1192*4882a593Smuzhiyun 1193*4882a593SmuzhiyunThe example code below does not include any hardware access routines but 1194*4882a593Smuzhiyunshows only the skeleton, how to build up the PCM interfaces. 1195*4882a593Smuzhiyun 1196*4882a593Smuzhiyun:: 1197*4882a593Smuzhiyun 1198*4882a593Smuzhiyun #include <sound/pcm.h> 1199*4882a593Smuzhiyun .... 1200*4882a593Smuzhiyun 1201*4882a593Smuzhiyun /* hardware definition */ 1202*4882a593Smuzhiyun static struct snd_pcm_hardware snd_mychip_playback_hw = { 1203*4882a593Smuzhiyun .info = (SNDRV_PCM_INFO_MMAP | 1204*4882a593Smuzhiyun SNDRV_PCM_INFO_INTERLEAVED | 1205*4882a593Smuzhiyun SNDRV_PCM_INFO_BLOCK_TRANSFER | 1206*4882a593Smuzhiyun SNDRV_PCM_INFO_MMAP_VALID), 1207*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S16_LE, 1208*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_48000, 1209*4882a593Smuzhiyun .rate_min = 8000, 1210*4882a593Smuzhiyun .rate_max = 48000, 1211*4882a593Smuzhiyun .channels_min = 2, 1212*4882a593Smuzhiyun .channels_max = 2, 1213*4882a593Smuzhiyun .buffer_bytes_max = 32768, 1214*4882a593Smuzhiyun .period_bytes_min = 4096, 1215*4882a593Smuzhiyun .period_bytes_max = 32768, 1216*4882a593Smuzhiyun .periods_min = 1, 1217*4882a593Smuzhiyun .periods_max = 1024, 1218*4882a593Smuzhiyun }; 1219*4882a593Smuzhiyun 1220*4882a593Smuzhiyun /* hardware definition */ 1221*4882a593Smuzhiyun static struct snd_pcm_hardware snd_mychip_capture_hw = { 1222*4882a593Smuzhiyun .info = (SNDRV_PCM_INFO_MMAP | 1223*4882a593Smuzhiyun SNDRV_PCM_INFO_INTERLEAVED | 1224*4882a593Smuzhiyun SNDRV_PCM_INFO_BLOCK_TRANSFER | 1225*4882a593Smuzhiyun SNDRV_PCM_INFO_MMAP_VALID), 1226*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S16_LE, 1227*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_48000, 1228*4882a593Smuzhiyun .rate_min = 8000, 1229*4882a593Smuzhiyun .rate_max = 48000, 1230*4882a593Smuzhiyun .channels_min = 2, 1231*4882a593Smuzhiyun .channels_max = 2, 1232*4882a593Smuzhiyun .buffer_bytes_max = 32768, 1233*4882a593Smuzhiyun .period_bytes_min = 4096, 1234*4882a593Smuzhiyun .period_bytes_max = 32768, 1235*4882a593Smuzhiyun .periods_min = 1, 1236*4882a593Smuzhiyun .periods_max = 1024, 1237*4882a593Smuzhiyun }; 1238*4882a593Smuzhiyun 1239*4882a593Smuzhiyun /* open callback */ 1240*4882a593Smuzhiyun static int snd_mychip_playback_open(struct snd_pcm_substream *substream) 1241*4882a593Smuzhiyun { 1242*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1243*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime; 1244*4882a593Smuzhiyun 1245*4882a593Smuzhiyun runtime->hw = snd_mychip_playback_hw; 1246*4882a593Smuzhiyun /* more hardware-initialization will be done here */ 1247*4882a593Smuzhiyun .... 1248*4882a593Smuzhiyun return 0; 1249*4882a593Smuzhiyun } 1250*4882a593Smuzhiyun 1251*4882a593Smuzhiyun /* close callback */ 1252*4882a593Smuzhiyun static int snd_mychip_playback_close(struct snd_pcm_substream *substream) 1253*4882a593Smuzhiyun { 1254*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1255*4882a593Smuzhiyun /* the hardware-specific codes will be here */ 1256*4882a593Smuzhiyun .... 1257*4882a593Smuzhiyun return 0; 1258*4882a593Smuzhiyun 1259*4882a593Smuzhiyun } 1260*4882a593Smuzhiyun 1261*4882a593Smuzhiyun /* open callback */ 1262*4882a593Smuzhiyun static int snd_mychip_capture_open(struct snd_pcm_substream *substream) 1263*4882a593Smuzhiyun { 1264*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1265*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime; 1266*4882a593Smuzhiyun 1267*4882a593Smuzhiyun runtime->hw = snd_mychip_capture_hw; 1268*4882a593Smuzhiyun /* more hardware-initialization will be done here */ 1269*4882a593Smuzhiyun .... 1270*4882a593Smuzhiyun return 0; 1271*4882a593Smuzhiyun } 1272*4882a593Smuzhiyun 1273*4882a593Smuzhiyun /* close callback */ 1274*4882a593Smuzhiyun static int snd_mychip_capture_close(struct snd_pcm_substream *substream) 1275*4882a593Smuzhiyun { 1276*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1277*4882a593Smuzhiyun /* the hardware-specific codes will be here */ 1278*4882a593Smuzhiyun .... 1279*4882a593Smuzhiyun return 0; 1280*4882a593Smuzhiyun } 1281*4882a593Smuzhiyun 1282*4882a593Smuzhiyun /* hw_params callback */ 1283*4882a593Smuzhiyun static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream, 1284*4882a593Smuzhiyun struct snd_pcm_hw_params *hw_params) 1285*4882a593Smuzhiyun { 1286*4882a593Smuzhiyun /* the hardware-specific codes will be here */ 1287*4882a593Smuzhiyun .... 1288*4882a593Smuzhiyun return 0; 1289*4882a593Smuzhiyun } 1290*4882a593Smuzhiyun 1291*4882a593Smuzhiyun /* hw_free callback */ 1292*4882a593Smuzhiyun static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream) 1293*4882a593Smuzhiyun { 1294*4882a593Smuzhiyun /* the hardware-specific codes will be here */ 1295*4882a593Smuzhiyun .... 1296*4882a593Smuzhiyun return 0; 1297*4882a593Smuzhiyun } 1298*4882a593Smuzhiyun 1299*4882a593Smuzhiyun /* prepare callback */ 1300*4882a593Smuzhiyun static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream) 1301*4882a593Smuzhiyun { 1302*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1303*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime; 1304*4882a593Smuzhiyun 1305*4882a593Smuzhiyun /* set up the hardware with the current configuration 1306*4882a593Smuzhiyun * for example... 1307*4882a593Smuzhiyun */ 1308*4882a593Smuzhiyun mychip_set_sample_format(chip, runtime->format); 1309*4882a593Smuzhiyun mychip_set_sample_rate(chip, runtime->rate); 1310*4882a593Smuzhiyun mychip_set_channels(chip, runtime->channels); 1311*4882a593Smuzhiyun mychip_set_dma_setup(chip, runtime->dma_addr, 1312*4882a593Smuzhiyun chip->buffer_size, 1313*4882a593Smuzhiyun chip->period_size); 1314*4882a593Smuzhiyun return 0; 1315*4882a593Smuzhiyun } 1316*4882a593Smuzhiyun 1317*4882a593Smuzhiyun /* trigger callback */ 1318*4882a593Smuzhiyun static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream, 1319*4882a593Smuzhiyun int cmd) 1320*4882a593Smuzhiyun { 1321*4882a593Smuzhiyun switch (cmd) { 1322*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_START: 1323*4882a593Smuzhiyun /* do something to start the PCM engine */ 1324*4882a593Smuzhiyun .... 1325*4882a593Smuzhiyun break; 1326*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_STOP: 1327*4882a593Smuzhiyun /* do something to stop the PCM engine */ 1328*4882a593Smuzhiyun .... 1329*4882a593Smuzhiyun break; 1330*4882a593Smuzhiyun default: 1331*4882a593Smuzhiyun return -EINVAL; 1332*4882a593Smuzhiyun } 1333*4882a593Smuzhiyun } 1334*4882a593Smuzhiyun 1335*4882a593Smuzhiyun /* pointer callback */ 1336*4882a593Smuzhiyun static snd_pcm_uframes_t 1337*4882a593Smuzhiyun snd_mychip_pcm_pointer(struct snd_pcm_substream *substream) 1338*4882a593Smuzhiyun { 1339*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1340*4882a593Smuzhiyun unsigned int current_ptr; 1341*4882a593Smuzhiyun 1342*4882a593Smuzhiyun /* get the current hardware pointer */ 1343*4882a593Smuzhiyun current_ptr = mychip_get_hw_pointer(chip); 1344*4882a593Smuzhiyun return current_ptr; 1345*4882a593Smuzhiyun } 1346*4882a593Smuzhiyun 1347*4882a593Smuzhiyun /* operators */ 1348*4882a593Smuzhiyun static struct snd_pcm_ops snd_mychip_playback_ops = { 1349*4882a593Smuzhiyun .open = snd_mychip_playback_open, 1350*4882a593Smuzhiyun .close = snd_mychip_playback_close, 1351*4882a593Smuzhiyun .hw_params = snd_mychip_pcm_hw_params, 1352*4882a593Smuzhiyun .hw_free = snd_mychip_pcm_hw_free, 1353*4882a593Smuzhiyun .prepare = snd_mychip_pcm_prepare, 1354*4882a593Smuzhiyun .trigger = snd_mychip_pcm_trigger, 1355*4882a593Smuzhiyun .pointer = snd_mychip_pcm_pointer, 1356*4882a593Smuzhiyun }; 1357*4882a593Smuzhiyun 1358*4882a593Smuzhiyun /* operators */ 1359*4882a593Smuzhiyun static struct snd_pcm_ops snd_mychip_capture_ops = { 1360*4882a593Smuzhiyun .open = snd_mychip_capture_open, 1361*4882a593Smuzhiyun .close = snd_mychip_capture_close, 1362*4882a593Smuzhiyun .hw_params = snd_mychip_pcm_hw_params, 1363*4882a593Smuzhiyun .hw_free = snd_mychip_pcm_hw_free, 1364*4882a593Smuzhiyun .prepare = snd_mychip_pcm_prepare, 1365*4882a593Smuzhiyun .trigger = snd_mychip_pcm_trigger, 1366*4882a593Smuzhiyun .pointer = snd_mychip_pcm_pointer, 1367*4882a593Smuzhiyun }; 1368*4882a593Smuzhiyun 1369*4882a593Smuzhiyun /* 1370*4882a593Smuzhiyun * definitions of capture are omitted here... 1371*4882a593Smuzhiyun */ 1372*4882a593Smuzhiyun 1373*4882a593Smuzhiyun /* create a pcm device */ 1374*4882a593Smuzhiyun static int snd_mychip_new_pcm(struct mychip *chip) 1375*4882a593Smuzhiyun { 1376*4882a593Smuzhiyun struct snd_pcm *pcm; 1377*4882a593Smuzhiyun int err; 1378*4882a593Smuzhiyun 1379*4882a593Smuzhiyun err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); 1380*4882a593Smuzhiyun if (err < 0) 1381*4882a593Smuzhiyun return err; 1382*4882a593Smuzhiyun pcm->private_data = chip; 1383*4882a593Smuzhiyun strcpy(pcm->name, "My Chip"); 1384*4882a593Smuzhiyun chip->pcm = pcm; 1385*4882a593Smuzhiyun /* set operators */ 1386*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1387*4882a593Smuzhiyun &snd_mychip_playback_ops); 1388*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 1389*4882a593Smuzhiyun &snd_mychip_capture_ops); 1390*4882a593Smuzhiyun /* pre-allocation of buffers */ 1391*4882a593Smuzhiyun /* NOTE: this may fail */ 1392*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, 1393*4882a593Smuzhiyun &chip->pci->dev, 1394*4882a593Smuzhiyun 64*1024, 64*1024); 1395*4882a593Smuzhiyun return 0; 1396*4882a593Smuzhiyun } 1397*4882a593Smuzhiyun 1398*4882a593Smuzhiyun 1399*4882a593SmuzhiyunPCM Constructor 1400*4882a593Smuzhiyun--------------- 1401*4882a593Smuzhiyun 1402*4882a593SmuzhiyunA pcm instance is allocated by the :c:func:`snd_pcm_new()` 1403*4882a593Smuzhiyunfunction. It would be better to create a constructor for pcm, namely, 1404*4882a593Smuzhiyun 1405*4882a593Smuzhiyun:: 1406*4882a593Smuzhiyun 1407*4882a593Smuzhiyun static int snd_mychip_new_pcm(struct mychip *chip) 1408*4882a593Smuzhiyun { 1409*4882a593Smuzhiyun struct snd_pcm *pcm; 1410*4882a593Smuzhiyun int err; 1411*4882a593Smuzhiyun 1412*4882a593Smuzhiyun err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); 1413*4882a593Smuzhiyun if (err < 0) 1414*4882a593Smuzhiyun return err; 1415*4882a593Smuzhiyun pcm->private_data = chip; 1416*4882a593Smuzhiyun strcpy(pcm->name, "My Chip"); 1417*4882a593Smuzhiyun chip->pcm = pcm; 1418*4882a593Smuzhiyun .... 1419*4882a593Smuzhiyun return 0; 1420*4882a593Smuzhiyun } 1421*4882a593Smuzhiyun 1422*4882a593SmuzhiyunThe :c:func:`snd_pcm_new()` function takes four arguments. The 1423*4882a593Smuzhiyunfirst argument is the card pointer to which this pcm is assigned, and 1424*4882a593Smuzhiyunthe second is the ID string. 1425*4882a593Smuzhiyun 1426*4882a593SmuzhiyunThe third argument (``index``, 0 in the above) is the index of this new 1427*4882a593Smuzhiyunpcm. It begins from zero. If you create more than one pcm instances, 1428*4882a593Smuzhiyunspecify the different numbers in this argument. For example, ``index = 1429*4882a593Smuzhiyun1`` for the second PCM device. 1430*4882a593Smuzhiyun 1431*4882a593SmuzhiyunThe fourth and fifth arguments are the number of substreams for playback 1432*4882a593Smuzhiyunand capture, respectively. Here 1 is used for both arguments. When no 1433*4882a593Smuzhiyunplayback or capture substreams are available, pass 0 to the 1434*4882a593Smuzhiyuncorresponding argument. 1435*4882a593Smuzhiyun 1436*4882a593SmuzhiyunIf a chip supports multiple playbacks or captures, you can specify more 1437*4882a593Smuzhiyunnumbers, but they must be handled properly in open/close, etc. 1438*4882a593Smuzhiyuncallbacks. When you need to know which substream you are referring to, 1439*4882a593Smuzhiyunthen it can be obtained from struct snd_pcm_substream data passed to each 1440*4882a593Smuzhiyuncallback as follows: 1441*4882a593Smuzhiyun 1442*4882a593Smuzhiyun:: 1443*4882a593Smuzhiyun 1444*4882a593Smuzhiyun struct snd_pcm_substream *substream; 1445*4882a593Smuzhiyun int index = substream->number; 1446*4882a593Smuzhiyun 1447*4882a593Smuzhiyun 1448*4882a593SmuzhiyunAfter the pcm is created, you need to set operators for each pcm stream. 1449*4882a593Smuzhiyun 1450*4882a593Smuzhiyun:: 1451*4882a593Smuzhiyun 1452*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, 1453*4882a593Smuzhiyun &snd_mychip_playback_ops); 1454*4882a593Smuzhiyun snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, 1455*4882a593Smuzhiyun &snd_mychip_capture_ops); 1456*4882a593Smuzhiyun 1457*4882a593SmuzhiyunThe operators are defined typically like this: 1458*4882a593Smuzhiyun 1459*4882a593Smuzhiyun:: 1460*4882a593Smuzhiyun 1461*4882a593Smuzhiyun static struct snd_pcm_ops snd_mychip_playback_ops = { 1462*4882a593Smuzhiyun .open = snd_mychip_pcm_open, 1463*4882a593Smuzhiyun .close = snd_mychip_pcm_close, 1464*4882a593Smuzhiyun .hw_params = snd_mychip_pcm_hw_params, 1465*4882a593Smuzhiyun .hw_free = snd_mychip_pcm_hw_free, 1466*4882a593Smuzhiyun .prepare = snd_mychip_pcm_prepare, 1467*4882a593Smuzhiyun .trigger = snd_mychip_pcm_trigger, 1468*4882a593Smuzhiyun .pointer = snd_mychip_pcm_pointer, 1469*4882a593Smuzhiyun }; 1470*4882a593Smuzhiyun 1471*4882a593SmuzhiyunAll the callbacks are described in the Operators_ subsection. 1472*4882a593Smuzhiyun 1473*4882a593SmuzhiyunAfter setting the operators, you probably will want to pre-allocate the 1474*4882a593Smuzhiyunbuffer and set up the managed allocation mode. 1475*4882a593SmuzhiyunFor that, simply call the following: 1476*4882a593Smuzhiyun 1477*4882a593Smuzhiyun:: 1478*4882a593Smuzhiyun 1479*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, 1480*4882a593Smuzhiyun &chip->pci->dev, 1481*4882a593Smuzhiyun 64*1024, 64*1024); 1482*4882a593Smuzhiyun 1483*4882a593SmuzhiyunIt will allocate a buffer up to 64kB as default. Buffer management 1484*4882a593Smuzhiyundetails will be described in the later section `Buffer and Memory 1485*4882a593SmuzhiyunManagement`_. 1486*4882a593Smuzhiyun 1487*4882a593SmuzhiyunAdditionally, you can set some extra information for this pcm in 1488*4882a593Smuzhiyun``pcm->info_flags``. The available values are defined as 1489*4882a593Smuzhiyun``SNDRV_PCM_INFO_XXX`` in ``<sound/asound.h>``, which is used for the 1490*4882a593Smuzhiyunhardware definition (described later). When your soundchip supports only 1491*4882a593Smuzhiyunhalf-duplex, specify like this: 1492*4882a593Smuzhiyun 1493*4882a593Smuzhiyun:: 1494*4882a593Smuzhiyun 1495*4882a593Smuzhiyun pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; 1496*4882a593Smuzhiyun 1497*4882a593Smuzhiyun 1498*4882a593Smuzhiyun... And the Destructor? 1499*4882a593Smuzhiyun----------------------- 1500*4882a593Smuzhiyun 1501*4882a593SmuzhiyunThe destructor for a pcm instance is not always necessary. Since the pcm 1502*4882a593Smuzhiyundevice will be released by the middle layer code automatically, you 1503*4882a593Smuzhiyundon't have to call the destructor explicitly. 1504*4882a593Smuzhiyun 1505*4882a593SmuzhiyunThe destructor would be necessary if you created special records 1506*4882a593Smuzhiyuninternally and needed to release them. In such a case, set the 1507*4882a593Smuzhiyundestructor function to ``pcm->private_free``: 1508*4882a593Smuzhiyun 1509*4882a593Smuzhiyun:: 1510*4882a593Smuzhiyun 1511*4882a593Smuzhiyun static void mychip_pcm_free(struct snd_pcm *pcm) 1512*4882a593Smuzhiyun { 1513*4882a593Smuzhiyun struct mychip *chip = snd_pcm_chip(pcm); 1514*4882a593Smuzhiyun /* free your own data */ 1515*4882a593Smuzhiyun kfree(chip->my_private_pcm_data); 1516*4882a593Smuzhiyun /* do what you like else */ 1517*4882a593Smuzhiyun .... 1518*4882a593Smuzhiyun } 1519*4882a593Smuzhiyun 1520*4882a593Smuzhiyun static int snd_mychip_new_pcm(struct mychip *chip) 1521*4882a593Smuzhiyun { 1522*4882a593Smuzhiyun struct snd_pcm *pcm; 1523*4882a593Smuzhiyun .... 1524*4882a593Smuzhiyun /* allocate your own data */ 1525*4882a593Smuzhiyun chip->my_private_pcm_data = kmalloc(...); 1526*4882a593Smuzhiyun /* set the destructor */ 1527*4882a593Smuzhiyun pcm->private_data = chip; 1528*4882a593Smuzhiyun pcm->private_free = mychip_pcm_free; 1529*4882a593Smuzhiyun .... 1530*4882a593Smuzhiyun } 1531*4882a593Smuzhiyun 1532*4882a593Smuzhiyun 1533*4882a593Smuzhiyun 1534*4882a593SmuzhiyunRuntime Pointer - The Chest of PCM Information 1535*4882a593Smuzhiyun---------------------------------------------- 1536*4882a593Smuzhiyun 1537*4882a593SmuzhiyunWhen the PCM substream is opened, a PCM runtime instance is allocated 1538*4882a593Smuzhiyunand assigned to the substream. This pointer is accessible via 1539*4882a593Smuzhiyun``substream->runtime``. This runtime pointer holds most information you 1540*4882a593Smuzhiyunneed to control the PCM: the copy of hw_params and sw_params 1541*4882a593Smuzhiyunconfigurations, the buffer pointers, mmap records, spinlocks, etc. 1542*4882a593Smuzhiyun 1543*4882a593SmuzhiyunThe definition of runtime instance is found in ``<sound/pcm.h>``. Here 1544*4882a593Smuzhiyunare the contents of this file: 1545*4882a593Smuzhiyun 1546*4882a593Smuzhiyun:: 1547*4882a593Smuzhiyun 1548*4882a593Smuzhiyun struct _snd_pcm_runtime { 1549*4882a593Smuzhiyun /* -- Status -- */ 1550*4882a593Smuzhiyun struct snd_pcm_substream *trigger_master; 1551*4882a593Smuzhiyun snd_timestamp_t trigger_tstamp; /* trigger timestamp */ 1552*4882a593Smuzhiyun int overrange; 1553*4882a593Smuzhiyun snd_pcm_uframes_t avail_max; 1554*4882a593Smuzhiyun snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */ 1555*4882a593Smuzhiyun snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/ 1556*4882a593Smuzhiyun 1557*4882a593Smuzhiyun /* -- HW params -- */ 1558*4882a593Smuzhiyun snd_pcm_access_t access; /* access mode */ 1559*4882a593Smuzhiyun snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */ 1560*4882a593Smuzhiyun snd_pcm_subformat_t subformat; /* subformat */ 1561*4882a593Smuzhiyun unsigned int rate; /* rate in Hz */ 1562*4882a593Smuzhiyun unsigned int channels; /* channels */ 1563*4882a593Smuzhiyun snd_pcm_uframes_t period_size; /* period size */ 1564*4882a593Smuzhiyun unsigned int periods; /* periods */ 1565*4882a593Smuzhiyun snd_pcm_uframes_t buffer_size; /* buffer size */ 1566*4882a593Smuzhiyun unsigned int tick_time; /* tick time */ 1567*4882a593Smuzhiyun snd_pcm_uframes_t min_align; /* Min alignment for the format */ 1568*4882a593Smuzhiyun size_t byte_align; 1569*4882a593Smuzhiyun unsigned int frame_bits; 1570*4882a593Smuzhiyun unsigned int sample_bits; 1571*4882a593Smuzhiyun unsigned int info; 1572*4882a593Smuzhiyun unsigned int rate_num; 1573*4882a593Smuzhiyun unsigned int rate_den; 1574*4882a593Smuzhiyun 1575*4882a593Smuzhiyun /* -- SW params -- */ 1576*4882a593Smuzhiyun struct timespec tstamp_mode; /* mmap timestamp is updated */ 1577*4882a593Smuzhiyun unsigned int period_step; 1578*4882a593Smuzhiyun unsigned int sleep_min; /* min ticks to sleep */ 1579*4882a593Smuzhiyun snd_pcm_uframes_t start_threshold; 1580*4882a593Smuzhiyun snd_pcm_uframes_t stop_threshold; 1581*4882a593Smuzhiyun snd_pcm_uframes_t silence_threshold; /* Silence filling happens when 1582*4882a593Smuzhiyun noise is nearest than this */ 1583*4882a593Smuzhiyun snd_pcm_uframes_t silence_size; /* Silence filling size */ 1584*4882a593Smuzhiyun snd_pcm_uframes_t boundary; /* pointers wrap point */ 1585*4882a593Smuzhiyun 1586*4882a593Smuzhiyun snd_pcm_uframes_t silenced_start; 1587*4882a593Smuzhiyun snd_pcm_uframes_t silenced_size; 1588*4882a593Smuzhiyun 1589*4882a593Smuzhiyun snd_pcm_sync_id_t sync; /* hardware synchronization ID */ 1590*4882a593Smuzhiyun 1591*4882a593Smuzhiyun /* -- mmap -- */ 1592*4882a593Smuzhiyun volatile struct snd_pcm_mmap_status *status; 1593*4882a593Smuzhiyun volatile struct snd_pcm_mmap_control *control; 1594*4882a593Smuzhiyun atomic_t mmap_count; 1595*4882a593Smuzhiyun 1596*4882a593Smuzhiyun /* -- locking / scheduling -- */ 1597*4882a593Smuzhiyun spinlock_t lock; 1598*4882a593Smuzhiyun wait_queue_head_t sleep; 1599*4882a593Smuzhiyun struct timer_list tick_timer; 1600*4882a593Smuzhiyun struct fasync_struct *fasync; 1601*4882a593Smuzhiyun 1602*4882a593Smuzhiyun /* -- private section -- */ 1603*4882a593Smuzhiyun void *private_data; 1604*4882a593Smuzhiyun void (*private_free)(struct snd_pcm_runtime *runtime); 1605*4882a593Smuzhiyun 1606*4882a593Smuzhiyun /* -- hardware description -- */ 1607*4882a593Smuzhiyun struct snd_pcm_hardware hw; 1608*4882a593Smuzhiyun struct snd_pcm_hw_constraints hw_constraints; 1609*4882a593Smuzhiyun 1610*4882a593Smuzhiyun /* -- timer -- */ 1611*4882a593Smuzhiyun unsigned int timer_resolution; /* timer resolution */ 1612*4882a593Smuzhiyun 1613*4882a593Smuzhiyun /* -- DMA -- */ 1614*4882a593Smuzhiyun unsigned char *dma_area; /* DMA area */ 1615*4882a593Smuzhiyun dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */ 1616*4882a593Smuzhiyun size_t dma_bytes; /* size of DMA area */ 1617*4882a593Smuzhiyun 1618*4882a593Smuzhiyun struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */ 1619*4882a593Smuzhiyun 1620*4882a593Smuzhiyun #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) 1621*4882a593Smuzhiyun /* -- OSS things -- */ 1622*4882a593Smuzhiyun struct snd_pcm_oss_runtime oss; 1623*4882a593Smuzhiyun #endif 1624*4882a593Smuzhiyun }; 1625*4882a593Smuzhiyun 1626*4882a593Smuzhiyun 1627*4882a593SmuzhiyunFor the operators (callbacks) of each sound driver, most of these 1628*4882a593Smuzhiyunrecords are supposed to be read-only. Only the PCM middle-layer changes 1629*4882a593Smuzhiyun/ updates them. The exceptions are the hardware description (hw) DMA 1630*4882a593Smuzhiyunbuffer information and the private data. Besides, if you use the 1631*4882a593Smuzhiyunstandard managed buffer allocation mode, you don't need to set the 1632*4882a593SmuzhiyunDMA buffer information by yourself. 1633*4882a593Smuzhiyun 1634*4882a593SmuzhiyunIn the sections below, important records are explained. 1635*4882a593Smuzhiyun 1636*4882a593SmuzhiyunHardware Description 1637*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~ 1638*4882a593Smuzhiyun 1639*4882a593SmuzhiyunThe hardware descriptor (struct snd_pcm_hardware) contains the definitions of 1640*4882a593Smuzhiyunthe fundamental hardware configuration. Above all, you'll need to define this 1641*4882a593Smuzhiyunin the `PCM open callback`_. Note that the runtime instance holds the copy of 1642*4882a593Smuzhiyunthe descriptor, not the pointer to the existing descriptor. That is, 1643*4882a593Smuzhiyunin the open callback, you can modify the copied descriptor 1644*4882a593Smuzhiyun(``runtime->hw``) as you need. For example, if the maximum number of 1645*4882a593Smuzhiyunchannels is 1 only on some chip models, you can still use the same 1646*4882a593Smuzhiyunhardware descriptor and change the channels_max later: 1647*4882a593Smuzhiyun 1648*4882a593Smuzhiyun:: 1649*4882a593Smuzhiyun 1650*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime; 1651*4882a593Smuzhiyun ... 1652*4882a593Smuzhiyun runtime->hw = snd_mychip_playback_hw; /* common definition */ 1653*4882a593Smuzhiyun if (chip->model == VERY_OLD_ONE) 1654*4882a593Smuzhiyun runtime->hw.channels_max = 1; 1655*4882a593Smuzhiyun 1656*4882a593SmuzhiyunTypically, you'll have a hardware descriptor as below: 1657*4882a593Smuzhiyun 1658*4882a593Smuzhiyun:: 1659*4882a593Smuzhiyun 1660*4882a593Smuzhiyun static struct snd_pcm_hardware snd_mychip_playback_hw = { 1661*4882a593Smuzhiyun .info = (SNDRV_PCM_INFO_MMAP | 1662*4882a593Smuzhiyun SNDRV_PCM_INFO_INTERLEAVED | 1663*4882a593Smuzhiyun SNDRV_PCM_INFO_BLOCK_TRANSFER | 1664*4882a593Smuzhiyun SNDRV_PCM_INFO_MMAP_VALID), 1665*4882a593Smuzhiyun .formats = SNDRV_PCM_FMTBIT_S16_LE, 1666*4882a593Smuzhiyun .rates = SNDRV_PCM_RATE_8000_48000, 1667*4882a593Smuzhiyun .rate_min = 8000, 1668*4882a593Smuzhiyun .rate_max = 48000, 1669*4882a593Smuzhiyun .channels_min = 2, 1670*4882a593Smuzhiyun .channels_max = 2, 1671*4882a593Smuzhiyun .buffer_bytes_max = 32768, 1672*4882a593Smuzhiyun .period_bytes_min = 4096, 1673*4882a593Smuzhiyun .period_bytes_max = 32768, 1674*4882a593Smuzhiyun .periods_min = 1, 1675*4882a593Smuzhiyun .periods_max = 1024, 1676*4882a593Smuzhiyun }; 1677*4882a593Smuzhiyun 1678*4882a593Smuzhiyun- The ``info`` field contains the type and capabilities of this 1679*4882a593Smuzhiyun pcm. The bit flags are defined in ``<sound/asound.h>`` as 1680*4882a593Smuzhiyun ``SNDRV_PCM_INFO_XXX``. Here, at least, you have to specify whether 1681*4882a593Smuzhiyun the mmap is supported and which interleaved format is 1682*4882a593Smuzhiyun supported. When the hardware supports mmap, add the 1683*4882a593Smuzhiyun ``SNDRV_PCM_INFO_MMAP`` flag here. When the hardware supports the 1684*4882a593Smuzhiyun interleaved or the non-interleaved formats, 1685*4882a593Smuzhiyun ``SNDRV_PCM_INFO_INTERLEAVED`` or ``SNDRV_PCM_INFO_NONINTERLEAVED`` 1686*4882a593Smuzhiyun flag must be set, respectively. If both are supported, you can set 1687*4882a593Smuzhiyun both, too. 1688*4882a593Smuzhiyun 1689*4882a593Smuzhiyun In the above example, ``MMAP_VALID`` and ``BLOCK_TRANSFER`` are 1690*4882a593Smuzhiyun specified for the OSS mmap mode. Usually both are set. Of course, 1691*4882a593Smuzhiyun ``MMAP_VALID`` is set only if the mmap is really supported. 1692*4882a593Smuzhiyun 1693*4882a593Smuzhiyun The other possible flags are ``SNDRV_PCM_INFO_PAUSE`` and 1694*4882a593Smuzhiyun ``SNDRV_PCM_INFO_RESUME``. The ``PAUSE`` bit means that the pcm 1695*4882a593Smuzhiyun supports the “pause” operation, while the ``RESUME`` bit means that 1696*4882a593Smuzhiyun the pcm supports the full “suspend/resume” operation. If the 1697*4882a593Smuzhiyun ``PAUSE`` flag is set, the ``trigger`` callback below must handle 1698*4882a593Smuzhiyun the corresponding (pause push/release) commands. The suspend/resume 1699*4882a593Smuzhiyun trigger commands can be defined even without the ``RESUME`` 1700*4882a593Smuzhiyun flag. See `Power Management`_ section for details. 1701*4882a593Smuzhiyun 1702*4882a593Smuzhiyun When the PCM substreams can be synchronized (typically, 1703*4882a593Smuzhiyun synchronized start/stop of a playback and a capture streams), you 1704*4882a593Smuzhiyun can give ``SNDRV_PCM_INFO_SYNC_START``, too. In this case, you'll 1705*4882a593Smuzhiyun need to check the linked-list of PCM substreams in the trigger 1706*4882a593Smuzhiyun callback. This will be described in the later section. 1707*4882a593Smuzhiyun 1708*4882a593Smuzhiyun- ``formats`` field contains the bit-flags of supported formats 1709*4882a593Smuzhiyun (``SNDRV_PCM_FMTBIT_XXX``). If the hardware supports more than one 1710*4882a593Smuzhiyun format, give all or'ed bits. In the example above, the signed 16bit 1711*4882a593Smuzhiyun little-endian format is specified. 1712*4882a593Smuzhiyun 1713*4882a593Smuzhiyun- ``rates`` field contains the bit-flags of supported rates 1714*4882a593Smuzhiyun (``SNDRV_PCM_RATE_XXX``). When the chip supports continuous rates, 1715*4882a593Smuzhiyun pass ``CONTINUOUS`` bit additionally. The pre-defined rate bits are 1716*4882a593Smuzhiyun provided only for typical rates. If your chip supports 1717*4882a593Smuzhiyun unconventional rates, you need to add the ``KNOT`` bit and set up 1718*4882a593Smuzhiyun the hardware constraint manually (explained later). 1719*4882a593Smuzhiyun 1720*4882a593Smuzhiyun- ``rate_min`` and ``rate_max`` define the minimum and maximum sample 1721*4882a593Smuzhiyun rate. This should correspond somehow to ``rates`` bits. 1722*4882a593Smuzhiyun 1723*4882a593Smuzhiyun- ``channel_min`` and ``channel_max`` define, as you might already 1724*4882a593Smuzhiyun expected, the minimum and maximum number of channels. 1725*4882a593Smuzhiyun 1726*4882a593Smuzhiyun- ``buffer_bytes_max`` defines the maximum buffer size in 1727*4882a593Smuzhiyun bytes. There is no ``buffer_bytes_min`` field, since it can be 1728*4882a593Smuzhiyun calculated from the minimum period size and the minimum number of 1729*4882a593Smuzhiyun periods. Meanwhile, ``period_bytes_min`` and define the minimum and 1730*4882a593Smuzhiyun maximum size of the period in bytes. ``periods_max`` and 1731*4882a593Smuzhiyun ``periods_min`` define the maximum and minimum number of periods in 1732*4882a593Smuzhiyun the buffer. 1733*4882a593Smuzhiyun 1734*4882a593Smuzhiyun The “period” is a term that corresponds to a fragment in the OSS 1735*4882a593Smuzhiyun world. The period defines the size at which a PCM interrupt is 1736*4882a593Smuzhiyun generated. This size strongly depends on the hardware. Generally, 1737*4882a593Smuzhiyun the smaller period size will give you more interrupts, that is, 1738*4882a593Smuzhiyun more controls. In the case of capture, this size defines the input 1739*4882a593Smuzhiyun latency. On the other hand, the whole buffer size defines the 1740*4882a593Smuzhiyun output latency for the playback direction. 1741*4882a593Smuzhiyun 1742*4882a593Smuzhiyun- There is also a field ``fifo_size``. This specifies the size of the 1743*4882a593Smuzhiyun hardware FIFO, but currently it is neither used in the driver nor 1744*4882a593Smuzhiyun in the alsa-lib. So, you can ignore this field. 1745*4882a593Smuzhiyun 1746*4882a593SmuzhiyunPCM Configurations 1747*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 1748*4882a593Smuzhiyun 1749*4882a593SmuzhiyunOk, let's go back again to the PCM runtime records. The most 1750*4882a593Smuzhiyunfrequently referred records in the runtime instance are the PCM 1751*4882a593Smuzhiyunconfigurations. The PCM configurations are stored in the runtime 1752*4882a593Smuzhiyuninstance after the application sends ``hw_params`` data via 1753*4882a593Smuzhiyunalsa-lib. There are many fields copied from hw_params and sw_params 1754*4882a593Smuzhiyunstructs. For example, ``format`` holds the format type chosen by the 1755*4882a593Smuzhiyunapplication. This field contains the enum value 1756*4882a593Smuzhiyun``SNDRV_PCM_FORMAT_XXX``. 1757*4882a593Smuzhiyun 1758*4882a593SmuzhiyunOne thing to be noted is that the configured buffer and period sizes 1759*4882a593Smuzhiyunare stored in “frames” in the runtime. In the ALSA world, ``1 frame = 1760*4882a593Smuzhiyunchannels \* samples-size``. For conversion between frames and bytes, 1761*4882a593Smuzhiyunyou can use the :c:func:`frames_to_bytes()` and 1762*4882a593Smuzhiyun:c:func:`bytes_to_frames()` helper functions. 1763*4882a593Smuzhiyun 1764*4882a593Smuzhiyun:: 1765*4882a593Smuzhiyun 1766*4882a593Smuzhiyun period_bytes = frames_to_bytes(runtime, runtime->period_size); 1767*4882a593Smuzhiyun 1768*4882a593SmuzhiyunAlso, many software parameters (sw_params) are stored in frames, too. 1769*4882a593SmuzhiyunPlease check the type of the field. ``snd_pcm_uframes_t`` is for the 1770*4882a593Smuzhiyunframes as unsigned integer while ``snd_pcm_sframes_t`` is for the 1771*4882a593Smuzhiyunframes as signed integer. 1772*4882a593Smuzhiyun 1773*4882a593SmuzhiyunDMA Buffer Information 1774*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 1775*4882a593Smuzhiyun 1776*4882a593SmuzhiyunThe DMA buffer is defined by the following four fields, ``dma_area``, 1777*4882a593Smuzhiyun``dma_addr``, ``dma_bytes`` and ``dma_private``. The ``dma_area`` 1778*4882a593Smuzhiyunholds the buffer pointer (the logical address). You can call 1779*4882a593Smuzhiyun:c:func:`memcpy()` from/to this pointer. Meanwhile, ``dma_addr`` holds 1780*4882a593Smuzhiyunthe physical address of the buffer. This field is specified only when 1781*4882a593Smuzhiyunthe buffer is a linear buffer. ``dma_bytes`` holds the size of buffer 1782*4882a593Smuzhiyunin bytes. ``dma_private`` is used for the ALSA DMA allocator. 1783*4882a593Smuzhiyun 1784*4882a593SmuzhiyunIf you use either the managed buffer allocation mode or the standard 1785*4882a593SmuzhiyunAPI function :c:func:`snd_pcm_lib_malloc_pages()` for allocating the buffer, 1786*4882a593Smuzhiyunthese fields are set by the ALSA middle layer, and you should *not* 1787*4882a593Smuzhiyunchange them by yourself. You can read them but not write them. On the 1788*4882a593Smuzhiyunother hand, if you want to allocate the buffer by yourself, you'll 1789*4882a593Smuzhiyunneed to manage it in hw_params callback. At least, ``dma_bytes`` is 1790*4882a593Smuzhiyunmandatory. ``dma_area`` is necessary when the buffer is mmapped. If 1791*4882a593Smuzhiyunyour driver doesn't support mmap, this field is not 1792*4882a593Smuzhiyunnecessary. ``dma_addr`` is also optional. You can use dma_private as 1793*4882a593Smuzhiyunyou like, too. 1794*4882a593Smuzhiyun 1795*4882a593SmuzhiyunRunning Status 1796*4882a593Smuzhiyun~~~~~~~~~~~~~~ 1797*4882a593Smuzhiyun 1798*4882a593SmuzhiyunThe running status can be referred via ``runtime->status``. This is 1799*4882a593Smuzhiyunthe pointer to the struct snd_pcm_mmap_status record. 1800*4882a593SmuzhiyunFor example, you can get the current 1801*4882a593SmuzhiyunDMA hardware pointer via ``runtime->status->hw_ptr``. 1802*4882a593Smuzhiyun 1803*4882a593SmuzhiyunThe DMA application pointer can be referred via ``runtime->control``, 1804*4882a593Smuzhiyunwhich points to the struct snd_pcm_mmap_control record. 1805*4882a593SmuzhiyunHowever, accessing directly to this value is not recommended. 1806*4882a593Smuzhiyun 1807*4882a593SmuzhiyunPrivate Data 1808*4882a593Smuzhiyun~~~~~~~~~~~~ 1809*4882a593Smuzhiyun 1810*4882a593SmuzhiyunYou can allocate a record for the substream and store it in 1811*4882a593Smuzhiyun``runtime->private_data``. Usually, this is done in the `PCM open 1812*4882a593Smuzhiyuncallback`_. Don't mix this with ``pcm->private_data``. The 1813*4882a593Smuzhiyun``pcm->private_data`` usually points to the chip instance assigned 1814*4882a593Smuzhiyunstatically at the creation of PCM, while the ``runtime->private_data`` 1815*4882a593Smuzhiyunpoints to a dynamic data structure created at the PCM open 1816*4882a593Smuzhiyuncallback. 1817*4882a593Smuzhiyun 1818*4882a593Smuzhiyun:: 1819*4882a593Smuzhiyun 1820*4882a593Smuzhiyun static int snd_xxx_open(struct snd_pcm_substream *substream) 1821*4882a593Smuzhiyun { 1822*4882a593Smuzhiyun struct my_pcm_data *data; 1823*4882a593Smuzhiyun .... 1824*4882a593Smuzhiyun data = kmalloc(sizeof(*data), GFP_KERNEL); 1825*4882a593Smuzhiyun substream->runtime->private_data = data; 1826*4882a593Smuzhiyun .... 1827*4882a593Smuzhiyun } 1828*4882a593Smuzhiyun 1829*4882a593Smuzhiyun 1830*4882a593SmuzhiyunThe allocated object must be released in the `close callback`_. 1831*4882a593Smuzhiyun 1832*4882a593SmuzhiyunOperators 1833*4882a593Smuzhiyun--------- 1834*4882a593Smuzhiyun 1835*4882a593SmuzhiyunOK, now let me give details about each pcm callback (``ops``). In 1836*4882a593Smuzhiyungeneral, every callback must return 0 if successful, or a negative 1837*4882a593Smuzhiyunerror number such as ``-EINVAL``. To choose an appropriate error 1838*4882a593Smuzhiyunnumber, it is advised to check what value other parts of the kernel 1839*4882a593Smuzhiyunreturn when the same kind of request fails. 1840*4882a593Smuzhiyun 1841*4882a593SmuzhiyunThe callback function takes at least the argument with 1842*4882a593Smuzhiyunstruct snd_pcm_substream pointer. To retrieve the chip 1843*4882a593Smuzhiyunrecord from the given substream instance, you can use the following 1844*4882a593Smuzhiyunmacro. 1845*4882a593Smuzhiyun 1846*4882a593Smuzhiyun:: 1847*4882a593Smuzhiyun 1848*4882a593Smuzhiyun int xxx() { 1849*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1850*4882a593Smuzhiyun .... 1851*4882a593Smuzhiyun } 1852*4882a593Smuzhiyun 1853*4882a593SmuzhiyunThe macro reads ``substream->private_data``, which is a copy of 1854*4882a593Smuzhiyun``pcm->private_data``. You can override the former if you need to 1855*4882a593Smuzhiyunassign different data records per PCM substream. For example, the 1856*4882a593Smuzhiyuncmi8330 driver assigns different ``private_data`` for playback and 1857*4882a593Smuzhiyuncapture directions, because it uses two different codecs (SB- and 1858*4882a593SmuzhiyunAD-compatible) for different directions. 1859*4882a593Smuzhiyun 1860*4882a593SmuzhiyunPCM open callback 1861*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~ 1862*4882a593Smuzhiyun 1863*4882a593Smuzhiyun:: 1864*4882a593Smuzhiyun 1865*4882a593Smuzhiyun static int snd_xxx_open(struct snd_pcm_substream *substream); 1866*4882a593Smuzhiyun 1867*4882a593SmuzhiyunThis is called when a pcm substream is opened. 1868*4882a593Smuzhiyun 1869*4882a593SmuzhiyunAt least, here you have to initialize the ``runtime->hw`` 1870*4882a593Smuzhiyunrecord. Typically, this is done by like this: 1871*4882a593Smuzhiyun 1872*4882a593Smuzhiyun:: 1873*4882a593Smuzhiyun 1874*4882a593Smuzhiyun static int snd_xxx_open(struct snd_pcm_substream *substream) 1875*4882a593Smuzhiyun { 1876*4882a593Smuzhiyun struct mychip *chip = snd_pcm_substream_chip(substream); 1877*4882a593Smuzhiyun struct snd_pcm_runtime *runtime = substream->runtime; 1878*4882a593Smuzhiyun 1879*4882a593Smuzhiyun runtime->hw = snd_mychip_playback_hw; 1880*4882a593Smuzhiyun return 0; 1881*4882a593Smuzhiyun } 1882*4882a593Smuzhiyun 1883*4882a593Smuzhiyunwhere ``snd_mychip_playback_hw`` is the pre-defined hardware 1884*4882a593Smuzhiyundescription. 1885*4882a593Smuzhiyun 1886*4882a593SmuzhiyunYou can allocate a private data in this callback, as described in 1887*4882a593Smuzhiyun`Private Data`_ section. 1888*4882a593Smuzhiyun 1889*4882a593SmuzhiyunIf the hardware configuration needs more constraints, set the hardware 1890*4882a593Smuzhiyunconstraints here, too. See Constraints_ for more details. 1891*4882a593Smuzhiyun 1892*4882a593Smuzhiyunclose callback 1893*4882a593Smuzhiyun~~~~~~~~~~~~~~ 1894*4882a593Smuzhiyun 1895*4882a593Smuzhiyun:: 1896*4882a593Smuzhiyun 1897*4882a593Smuzhiyun static int snd_xxx_close(struct snd_pcm_substream *substream); 1898*4882a593Smuzhiyun 1899*4882a593Smuzhiyun 1900*4882a593SmuzhiyunObviously, this is called when a pcm substream is closed. 1901*4882a593Smuzhiyun 1902*4882a593SmuzhiyunAny private instance for a pcm substream allocated in the ``open`` 1903*4882a593Smuzhiyuncallback will be released here. 1904*4882a593Smuzhiyun 1905*4882a593Smuzhiyun:: 1906*4882a593Smuzhiyun 1907*4882a593Smuzhiyun static int snd_xxx_close(struct snd_pcm_substream *substream) 1908*4882a593Smuzhiyun { 1909*4882a593Smuzhiyun .... 1910*4882a593Smuzhiyun kfree(substream->runtime->private_data); 1911*4882a593Smuzhiyun .... 1912*4882a593Smuzhiyun } 1913*4882a593Smuzhiyun 1914*4882a593Smuzhiyunioctl callback 1915*4882a593Smuzhiyun~~~~~~~~~~~~~~ 1916*4882a593Smuzhiyun 1917*4882a593SmuzhiyunThis is used for any special call to pcm ioctls. But usually you can 1918*4882a593Smuzhiyunleave it as NULL, then PCM core calls the generic ioctl callback 1919*4882a593Smuzhiyunfunction :c:func:`snd_pcm_lib_ioctl()`. If you need to deal with the 1920*4882a593Smuzhiyununique setup of channel info or reset procedure, you can pass your own 1921*4882a593Smuzhiyuncallback function here. 1922*4882a593Smuzhiyun 1923*4882a593Smuzhiyunhw_params callback 1924*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~ 1925*4882a593Smuzhiyun 1926*4882a593Smuzhiyun:: 1927*4882a593Smuzhiyun 1928*4882a593Smuzhiyun static int snd_xxx_hw_params(struct snd_pcm_substream *substream, 1929*4882a593Smuzhiyun struct snd_pcm_hw_params *hw_params); 1930*4882a593Smuzhiyun 1931*4882a593SmuzhiyunThis is called when the hardware parameter (``hw_params``) is set up 1932*4882a593Smuzhiyunby the application, that is, once when the buffer size, the period 1933*4882a593Smuzhiyunsize, the format, etc. are defined for the pcm substream. 1934*4882a593Smuzhiyun 1935*4882a593SmuzhiyunMany hardware setups should be done in this callback, including the 1936*4882a593Smuzhiyunallocation of buffers. 1937*4882a593Smuzhiyun 1938*4882a593SmuzhiyunParameters to be initialized are retrieved by 1939*4882a593Smuzhiyun:c:func:`params_xxx()` macros. 1940*4882a593Smuzhiyun 1941*4882a593SmuzhiyunWhen you set up the managed buffer allocation mode for the substream, 1942*4882a593Smuzhiyuna buffer is already allocated before this callback gets 1943*4882a593Smuzhiyuncalled. Alternatively, you can call a helper function below for 1944*4882a593Smuzhiyunallocating the buffer, too. 1945*4882a593Smuzhiyun 1946*4882a593Smuzhiyun:: 1947*4882a593Smuzhiyun 1948*4882a593Smuzhiyun snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params)); 1949*4882a593Smuzhiyun 1950*4882a593Smuzhiyun:c:func:`snd_pcm_lib_malloc_pages()` is available only when the 1951*4882a593SmuzhiyunDMA buffers have been pre-allocated. See the section `Buffer Types`_ 1952*4882a593Smuzhiyunfor more details. 1953*4882a593Smuzhiyun 1954*4882a593SmuzhiyunNote that this and ``prepare`` callbacks may be called multiple times 1955*4882a593Smuzhiyunper initialization. For example, the OSS emulation may call these 1956*4882a593Smuzhiyuncallbacks at each change via its ioctl. 1957*4882a593Smuzhiyun 1958*4882a593SmuzhiyunThus, you need to be careful not to allocate the same buffers many 1959*4882a593Smuzhiyuntimes, which will lead to memory leaks! Calling the helper function 1960*4882a593Smuzhiyunabove many times is OK. It will release the previous buffer 1961*4882a593Smuzhiyunautomatically when it was already allocated. 1962*4882a593Smuzhiyun 1963*4882a593SmuzhiyunAnother note is that this callback is non-atomic (schedulable) as 1964*4882a593Smuzhiyundefault, i.e. when no ``nonatomic`` flag set. This is important, 1965*4882a593Smuzhiyunbecause the ``trigger`` callback is atomic (non-schedulable). That is, 1966*4882a593Smuzhiyunmutexes or any schedule-related functions are not available in 1967*4882a593Smuzhiyun``trigger`` callback. Please see the subsection Atomicity_ for 1968*4882a593Smuzhiyundetails. 1969*4882a593Smuzhiyun 1970*4882a593Smuzhiyunhw_free callback 1971*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~ 1972*4882a593Smuzhiyun 1973*4882a593Smuzhiyun:: 1974*4882a593Smuzhiyun 1975*4882a593Smuzhiyun static int snd_xxx_hw_free(struct snd_pcm_substream *substream); 1976*4882a593Smuzhiyun 1977*4882a593SmuzhiyunThis is called to release the resources allocated via 1978*4882a593Smuzhiyun``hw_params``. 1979*4882a593Smuzhiyun 1980*4882a593SmuzhiyunThis function is always called before the close callback is called. 1981*4882a593SmuzhiyunAlso, the callback may be called multiple times, too. Keep track 1982*4882a593Smuzhiyunwhether the resource was already released. 1983*4882a593Smuzhiyun 1984*4882a593SmuzhiyunWhen you have set up the managed buffer allocation mode for the PCM 1985*4882a593Smuzhiyunsubstream, the allocated PCM buffer will be automatically released 1986*4882a593Smuzhiyunafter this callback gets called. Otherwise you'll have to release the 1987*4882a593Smuzhiyunbuffer manually. Typically, when the buffer was allocated from the 1988*4882a593Smuzhiyunpre-allocated pool, you can use the standard API function 1989*4882a593Smuzhiyun:c:func:`snd_pcm_lib_malloc_pages()` like: 1990*4882a593Smuzhiyun 1991*4882a593Smuzhiyun:: 1992*4882a593Smuzhiyun 1993*4882a593Smuzhiyun snd_pcm_lib_free_pages(substream); 1994*4882a593Smuzhiyun 1995*4882a593Smuzhiyunprepare callback 1996*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 1997*4882a593Smuzhiyun 1998*4882a593Smuzhiyun:: 1999*4882a593Smuzhiyun 2000*4882a593Smuzhiyun static int snd_xxx_prepare(struct snd_pcm_substream *substream); 2001*4882a593Smuzhiyun 2002*4882a593SmuzhiyunThis callback is called when the pcm is “prepared”. You can set the 2003*4882a593Smuzhiyunformat type, sample rate, etc. here. The difference from ``hw_params`` 2004*4882a593Smuzhiyunis that the ``prepare`` callback will be called each time 2005*4882a593Smuzhiyun:c:func:`snd_pcm_prepare()` is called, i.e. when recovering after 2006*4882a593Smuzhiyununderruns, etc. 2007*4882a593Smuzhiyun 2008*4882a593SmuzhiyunNote that this callback is now non-atomic. You can use 2009*4882a593Smuzhiyunschedule-related functions safely in this callback. 2010*4882a593Smuzhiyun 2011*4882a593SmuzhiyunIn this and the following callbacks, you can refer to the values via 2012*4882a593Smuzhiyunthe runtime record, ``substream->runtime``. For example, to get the 2013*4882a593Smuzhiyuncurrent rate, format or channels, access to ``runtime->rate``, 2014*4882a593Smuzhiyun``runtime->format`` or ``runtime->channels``, respectively. The 2015*4882a593Smuzhiyunphysical address of the allocated buffer is set to 2016*4882a593Smuzhiyun``runtime->dma_area``. The buffer and period sizes are in 2017*4882a593Smuzhiyun``runtime->buffer_size`` and ``runtime->period_size``, respectively. 2018*4882a593Smuzhiyun 2019*4882a593SmuzhiyunBe careful that this callback will be called many times at each setup, 2020*4882a593Smuzhiyuntoo. 2021*4882a593Smuzhiyun 2022*4882a593Smuzhiyuntrigger callback 2023*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 2024*4882a593Smuzhiyun 2025*4882a593Smuzhiyun:: 2026*4882a593Smuzhiyun 2027*4882a593Smuzhiyun static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd); 2028*4882a593Smuzhiyun 2029*4882a593SmuzhiyunThis is called when the pcm is started, stopped or paused. 2030*4882a593Smuzhiyun 2031*4882a593SmuzhiyunWhich action is specified in the second argument, 2032*4882a593Smuzhiyun``SNDRV_PCM_TRIGGER_XXX`` in ``<sound/pcm.h>``. At least, the ``START`` 2033*4882a593Smuzhiyunand ``STOP`` commands must be defined in this callback. 2034*4882a593Smuzhiyun 2035*4882a593Smuzhiyun:: 2036*4882a593Smuzhiyun 2037*4882a593Smuzhiyun switch (cmd) { 2038*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_START: 2039*4882a593Smuzhiyun /* do something to start the PCM engine */ 2040*4882a593Smuzhiyun break; 2041*4882a593Smuzhiyun case SNDRV_PCM_TRIGGER_STOP: 2042*4882a593Smuzhiyun /* do something to stop the PCM engine */ 2043*4882a593Smuzhiyun break; 2044*4882a593Smuzhiyun default: 2045*4882a593Smuzhiyun return -EINVAL; 2046*4882a593Smuzhiyun } 2047*4882a593Smuzhiyun 2048*4882a593SmuzhiyunWhen the pcm supports the pause operation (given in the info field of 2049*4882a593Smuzhiyunthe hardware table), the ``PAUSE_PUSH`` and ``PAUSE_RELEASE`` commands 2050*4882a593Smuzhiyunmust be handled here, too. The former is the command to pause the pcm, 2051*4882a593Smuzhiyunand the latter to restart the pcm again. 2052*4882a593Smuzhiyun 2053*4882a593SmuzhiyunWhen the pcm supports the suspend/resume operation, regardless of full 2054*4882a593Smuzhiyunor partial suspend/resume support, the ``SUSPEND`` and ``RESUME`` 2055*4882a593Smuzhiyuncommands must be handled, too. These commands are issued when the 2056*4882a593Smuzhiyunpower-management status is changed. Obviously, the ``SUSPEND`` and 2057*4882a593Smuzhiyun``RESUME`` commands suspend and resume the pcm substream, and usually, 2058*4882a593Smuzhiyunthey are identical to the ``STOP`` and ``START`` commands, respectively. 2059*4882a593SmuzhiyunSee the `Power Management`_ section for details. 2060*4882a593Smuzhiyun 2061*4882a593SmuzhiyunAs mentioned, this callback is atomic as default unless ``nonatomic`` 2062*4882a593Smuzhiyunflag set, and you cannot call functions which may sleep. The 2063*4882a593Smuzhiyun``trigger`` callback should be as minimal as possible, just really 2064*4882a593Smuzhiyuntriggering the DMA. The other stuff should be initialized 2065*4882a593Smuzhiyun``hw_params`` and ``prepare`` callbacks properly beforehand. 2066*4882a593Smuzhiyun 2067*4882a593Smuzhiyunsync_stop callback 2068*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~ 2069*4882a593Smuzhiyun 2070*4882a593Smuzhiyun:: 2071*4882a593Smuzhiyun 2072*4882a593Smuzhiyun static int snd_xxx_sync_stop(struct snd_pcm_substream *substream); 2073*4882a593Smuzhiyun 2074*4882a593SmuzhiyunThis callback is optional, and NULL can be passed. It's called after 2075*4882a593Smuzhiyunthe PCM core stops the stream and changes the stream state 2076*4882a593Smuzhiyun``prepare``, ``hw_params`` or ``hw_free``. 2077*4882a593SmuzhiyunSince the IRQ handler might be still pending, we need to wait until 2078*4882a593Smuzhiyunthe pending task finishes before moving to the next step; otherwise it 2079*4882a593Smuzhiyunmight lead to a crash due to resource conflicts or access to the freed 2080*4882a593Smuzhiyunresources. A typical behavior is to call a synchronization function 2081*4882a593Smuzhiyunlike :c:func:`synchronize_irq()` here. 2082*4882a593Smuzhiyun 2083*4882a593SmuzhiyunFor majority of drivers that need only a call of 2084*4882a593Smuzhiyun:c:func:`synchronize_irq()`, there is a simpler setup, too. 2085*4882a593SmuzhiyunWhile keeping NULL to ``sync_stop`` PCM callback, the driver can set 2086*4882a593Smuzhiyun``card->sync_irq`` field to store the valid interrupt number after 2087*4882a593Smuzhiyunrequesting an IRQ, instead. Then PCM core will look call 2088*4882a593Smuzhiyun:c:func:`synchronize_irq()` with the given IRQ appropriately. 2089*4882a593Smuzhiyun 2090*4882a593SmuzhiyunIf the IRQ handler is released at the card destructor, you don't need 2091*4882a593Smuzhiyunto clear ``card->sync_irq``, as the card itself is being released. 2092*4882a593SmuzhiyunSo, usually you'll need to add just a single line for assigning 2093*4882a593Smuzhiyun``card->sync_irq`` in the driver code unless the driver re-acquires 2094*4882a593Smuzhiyunthe IRQ. When the driver frees and re-acquires the IRQ dynamically 2095*4882a593Smuzhiyun(e.g. for suspend/resume), it needs to clear and re-set 2096*4882a593Smuzhiyun``card->sync_irq`` again appropriately. 2097*4882a593Smuzhiyun 2098*4882a593Smuzhiyunpointer callback 2099*4882a593Smuzhiyun~~~~~~~~~~~~~~~~ 2100*4882a593Smuzhiyun 2101*4882a593Smuzhiyun:: 2102*4882a593Smuzhiyun 2103*4882a593Smuzhiyun static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream) 2104*4882a593Smuzhiyun 2105*4882a593SmuzhiyunThis callback is called when the PCM middle layer inquires the current 2106*4882a593Smuzhiyunhardware position on the buffer. The position must be returned in 2107*4882a593Smuzhiyunframes, ranging from 0 to ``buffer_size - 1``. 2108*4882a593Smuzhiyun 2109*4882a593SmuzhiyunThis is called usually from the buffer-update routine in the pcm 2110*4882a593Smuzhiyunmiddle layer, which is invoked when :c:func:`snd_pcm_period_elapsed()` 2111*4882a593Smuzhiyunis called in the interrupt routine. Then the pcm middle layer updates 2112*4882a593Smuzhiyunthe position and calculates the available space, and wakes up the 2113*4882a593Smuzhiyunsleeping poll threads, etc. 2114*4882a593Smuzhiyun 2115*4882a593SmuzhiyunThis callback is also atomic as default. 2116*4882a593Smuzhiyun 2117*4882a593Smuzhiyuncopy_user, copy_kernel and fill_silence ops 2118*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2119*4882a593Smuzhiyun 2120*4882a593SmuzhiyunThese callbacks are not mandatory, and can be omitted in most cases. 2121*4882a593SmuzhiyunThese callbacks are used when the hardware buffer cannot be in the 2122*4882a593Smuzhiyunnormal memory space. Some chips have their own buffer on the hardware 2123*4882a593Smuzhiyunwhich is not mappable. In such a case, you have to transfer the data 2124*4882a593Smuzhiyunmanually from the memory buffer to the hardware buffer. Or, if the 2125*4882a593Smuzhiyunbuffer is non-contiguous on both physical and virtual memory spaces, 2126*4882a593Smuzhiyunthese callbacks must be defined, too. 2127*4882a593Smuzhiyun 2128*4882a593SmuzhiyunIf these two callbacks are defined, copy and set-silence operations 2129*4882a593Smuzhiyunare done by them. The detailed will be described in the later section 2130*4882a593Smuzhiyun`Buffer and Memory Management`_. 2131*4882a593Smuzhiyun 2132*4882a593Smuzhiyunack callback 2133*4882a593Smuzhiyun~~~~~~~~~~~~ 2134*4882a593Smuzhiyun 2135*4882a593SmuzhiyunThis callback is also not mandatory. This callback is called when the 2136*4882a593Smuzhiyun``appl_ptr`` is updated in read or write operations. Some drivers like 2137*4882a593Smuzhiyunemu10k1-fx and cs46xx need to track the current ``appl_ptr`` for the 2138*4882a593Smuzhiyuninternal buffer, and this callback is useful only for such a purpose. 2139*4882a593Smuzhiyun 2140*4882a593SmuzhiyunThis callback is atomic as default. 2141*4882a593Smuzhiyun 2142*4882a593Smuzhiyunpage callback 2143*4882a593Smuzhiyun~~~~~~~~~~~~~ 2144*4882a593Smuzhiyun 2145*4882a593SmuzhiyunThis callback is optional too. The mmap calls this callback to get the 2146*4882a593Smuzhiyunpage fault address. 2147*4882a593Smuzhiyun 2148*4882a593SmuzhiyunSince the recent changes, you need no special callback any longer for 2149*4882a593Smuzhiyunthe standard SG-buffer or vmalloc-buffer. Hence this callback should 2150*4882a593Smuzhiyunbe rarely used. 2151*4882a593Smuzhiyun 2152*4882a593Smuzhiyunmmap calllback 2153*4882a593Smuzhiyun~~~~~~~~~~~~~~ 2154*4882a593Smuzhiyun 2155*4882a593SmuzhiyunThis is another optional callback for controlling mmap behavior. 2156*4882a593SmuzhiyunOnce when defined, PCM core calls this callback when a page is 2157*4882a593Smuzhiyunmemory-mapped instead of dealing via the standard helper. 2158*4882a593SmuzhiyunIf you need special handling (due to some architecture or 2159*4882a593Smuzhiyundevice-specific issues), implement everything here as you like. 2160*4882a593Smuzhiyun 2161*4882a593Smuzhiyun 2162*4882a593SmuzhiyunPCM Interrupt Handler 2163*4882a593Smuzhiyun--------------------- 2164*4882a593Smuzhiyun 2165*4882a593SmuzhiyunThe rest of pcm stuff is the PCM interrupt handler. The role of PCM 2166*4882a593Smuzhiyuninterrupt handler in the sound driver is to update the buffer position 2167*4882a593Smuzhiyunand to tell the PCM middle layer when the buffer position goes across 2168*4882a593Smuzhiyunthe prescribed period size. To inform this, call the 2169*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` function. 2170*4882a593Smuzhiyun 2171*4882a593SmuzhiyunThere are several types of sound chips to generate the interrupts. 2172*4882a593Smuzhiyun 2173*4882a593SmuzhiyunInterrupts at the period (fragment) boundary 2174*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2175*4882a593Smuzhiyun 2176*4882a593SmuzhiyunThis is the most frequently found type: the hardware generates an 2177*4882a593Smuzhiyuninterrupt at each period boundary. In this case, you can call 2178*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` at each interrupt. 2179*4882a593Smuzhiyun 2180*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` takes the substream pointer as 2181*4882a593Smuzhiyunits argument. Thus, you need to keep the substream pointer accessible 2182*4882a593Smuzhiyunfrom the chip instance. For example, define ``substream`` field in the 2183*4882a593Smuzhiyunchip record to hold the current running substream pointer, and set the 2184*4882a593Smuzhiyunpointer value at ``open`` callback (and reset at ``close`` callback). 2185*4882a593Smuzhiyun 2186*4882a593SmuzhiyunIf you acquire a spinlock in the interrupt handler, and the lock is used 2187*4882a593Smuzhiyunin other pcm callbacks, too, then you have to release the lock before 2188*4882a593Smuzhiyuncalling :c:func:`snd_pcm_period_elapsed()`, because 2189*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` calls other pcm callbacks 2190*4882a593Smuzhiyuninside. 2191*4882a593Smuzhiyun 2192*4882a593SmuzhiyunTypical code would be like: 2193*4882a593Smuzhiyun 2194*4882a593Smuzhiyun:: 2195*4882a593Smuzhiyun 2196*4882a593Smuzhiyun 2197*4882a593Smuzhiyun static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) 2198*4882a593Smuzhiyun { 2199*4882a593Smuzhiyun struct mychip *chip = dev_id; 2200*4882a593Smuzhiyun spin_lock(&chip->lock); 2201*4882a593Smuzhiyun .... 2202*4882a593Smuzhiyun if (pcm_irq_invoked(chip)) { 2203*4882a593Smuzhiyun /* call updater, unlock before it */ 2204*4882a593Smuzhiyun spin_unlock(&chip->lock); 2205*4882a593Smuzhiyun snd_pcm_period_elapsed(chip->substream); 2206*4882a593Smuzhiyun spin_lock(&chip->lock); 2207*4882a593Smuzhiyun /* acknowledge the interrupt if necessary */ 2208*4882a593Smuzhiyun } 2209*4882a593Smuzhiyun .... 2210*4882a593Smuzhiyun spin_unlock(&chip->lock); 2211*4882a593Smuzhiyun return IRQ_HANDLED; 2212*4882a593Smuzhiyun } 2213*4882a593Smuzhiyun 2214*4882a593Smuzhiyun 2215*4882a593Smuzhiyun 2216*4882a593SmuzhiyunHigh frequency timer interrupts 2217*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2218*4882a593Smuzhiyun 2219*4882a593SmuzhiyunThis happens when the hardware doesn't generate interrupts at the period 2220*4882a593Smuzhiyunboundary but issues timer interrupts at a fixed timer rate (e.g. es1968 2221*4882a593Smuzhiyunor ymfpci drivers). In this case, you need to check the current hardware 2222*4882a593Smuzhiyunposition and accumulate the processed sample length at each interrupt. 2223*4882a593SmuzhiyunWhen the accumulated size exceeds the period size, call 2224*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` and reset the accumulator. 2225*4882a593Smuzhiyun 2226*4882a593SmuzhiyunTypical code would be like the following. 2227*4882a593Smuzhiyun 2228*4882a593Smuzhiyun:: 2229*4882a593Smuzhiyun 2230*4882a593Smuzhiyun 2231*4882a593Smuzhiyun static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) 2232*4882a593Smuzhiyun { 2233*4882a593Smuzhiyun struct mychip *chip = dev_id; 2234*4882a593Smuzhiyun spin_lock(&chip->lock); 2235*4882a593Smuzhiyun .... 2236*4882a593Smuzhiyun if (pcm_irq_invoked(chip)) { 2237*4882a593Smuzhiyun unsigned int last_ptr, size; 2238*4882a593Smuzhiyun /* get the current hardware pointer (in frames) */ 2239*4882a593Smuzhiyun last_ptr = get_hw_ptr(chip); 2240*4882a593Smuzhiyun /* calculate the processed frames since the 2241*4882a593Smuzhiyun * last update 2242*4882a593Smuzhiyun */ 2243*4882a593Smuzhiyun if (last_ptr < chip->last_ptr) 2244*4882a593Smuzhiyun size = runtime->buffer_size + last_ptr 2245*4882a593Smuzhiyun - chip->last_ptr; 2246*4882a593Smuzhiyun else 2247*4882a593Smuzhiyun size = last_ptr - chip->last_ptr; 2248*4882a593Smuzhiyun /* remember the last updated point */ 2249*4882a593Smuzhiyun chip->last_ptr = last_ptr; 2250*4882a593Smuzhiyun /* accumulate the size */ 2251*4882a593Smuzhiyun chip->size += size; 2252*4882a593Smuzhiyun /* over the period boundary? */ 2253*4882a593Smuzhiyun if (chip->size >= runtime->period_size) { 2254*4882a593Smuzhiyun /* reset the accumulator */ 2255*4882a593Smuzhiyun chip->size %= runtime->period_size; 2256*4882a593Smuzhiyun /* call updater */ 2257*4882a593Smuzhiyun spin_unlock(&chip->lock); 2258*4882a593Smuzhiyun snd_pcm_period_elapsed(substream); 2259*4882a593Smuzhiyun spin_lock(&chip->lock); 2260*4882a593Smuzhiyun } 2261*4882a593Smuzhiyun /* acknowledge the interrupt if necessary */ 2262*4882a593Smuzhiyun } 2263*4882a593Smuzhiyun .... 2264*4882a593Smuzhiyun spin_unlock(&chip->lock); 2265*4882a593Smuzhiyun return IRQ_HANDLED; 2266*4882a593Smuzhiyun } 2267*4882a593Smuzhiyun 2268*4882a593Smuzhiyun 2269*4882a593Smuzhiyun 2270*4882a593SmuzhiyunOn calling :c:func:`snd_pcm_period_elapsed()` 2271*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2272*4882a593Smuzhiyun 2273*4882a593SmuzhiyunIn both cases, even if more than one period are elapsed, you don't have 2274*4882a593Smuzhiyunto call :c:func:`snd_pcm_period_elapsed()` many times. Call only 2275*4882a593Smuzhiyunonce. And the pcm layer will check the current hardware pointer and 2276*4882a593Smuzhiyunupdate to the latest status. 2277*4882a593Smuzhiyun 2278*4882a593SmuzhiyunAtomicity 2279*4882a593Smuzhiyun--------- 2280*4882a593Smuzhiyun 2281*4882a593SmuzhiyunOne of the most important (and thus difficult to debug) problems in 2282*4882a593Smuzhiyunkernel programming are race conditions. In the Linux kernel, they are 2283*4882a593Smuzhiyunusually avoided via spin-locks, mutexes or semaphores. In general, if a 2284*4882a593Smuzhiyunrace condition can happen in an interrupt handler, it has to be managed 2285*4882a593Smuzhiyunatomically, and you have to use a spinlock to protect the critical 2286*4882a593Smuzhiyunsession. If the critical section is not in interrupt handler code and if 2287*4882a593Smuzhiyuntaking a relatively long time to execute is acceptable, you should use 2288*4882a593Smuzhiyunmutexes or semaphores instead. 2289*4882a593Smuzhiyun 2290*4882a593SmuzhiyunAs already seen, some pcm callbacks are atomic and some are not. For 2291*4882a593Smuzhiyunexample, the ``hw_params`` callback is non-atomic, while ``trigger`` 2292*4882a593Smuzhiyuncallback is atomic. This means, the latter is called already in a 2293*4882a593Smuzhiyunspinlock held by the PCM middle layer. Please take this atomicity into 2294*4882a593Smuzhiyunaccount when you choose a locking scheme in the callbacks. 2295*4882a593Smuzhiyun 2296*4882a593SmuzhiyunIn the atomic callbacks, you cannot use functions which may call 2297*4882a593Smuzhiyun:c:func:`schedule()` or go to :c:func:`sleep()`. Semaphores and 2298*4882a593Smuzhiyunmutexes can sleep, and hence they cannot be used inside the atomic 2299*4882a593Smuzhiyuncallbacks (e.g. ``trigger`` callback). To implement some delay in such a 2300*4882a593Smuzhiyuncallback, please use :c:func:`udelay()` or :c:func:`mdelay()`. 2301*4882a593Smuzhiyun 2302*4882a593SmuzhiyunAll three atomic callbacks (trigger, pointer, and ack) are called with 2303*4882a593Smuzhiyunlocal interrupts disabled. 2304*4882a593Smuzhiyun 2305*4882a593SmuzhiyunThe recent changes in PCM core code, however, allow all PCM operations 2306*4882a593Smuzhiyunto be non-atomic. This assumes that the all caller sides are in 2307*4882a593Smuzhiyunnon-atomic contexts. For example, the function 2308*4882a593Smuzhiyun:c:func:`snd_pcm_period_elapsed()` is called typically from the 2309*4882a593Smuzhiyuninterrupt handler. But, if you set up the driver to use a threaded 2310*4882a593Smuzhiyuninterrupt handler, this call can be in non-atomic context, too. In such 2311*4882a593Smuzhiyuna case, you can set ``nonatomic`` filed of struct snd_pcm object 2312*4882a593Smuzhiyunafter creating it. When this flag is set, mutex and rwsem are used internally 2313*4882a593Smuzhiyunin the PCM core instead of spin and rwlocks, so that you can call all PCM 2314*4882a593Smuzhiyunfunctions safely in a non-atomic 2315*4882a593Smuzhiyuncontext. 2316*4882a593Smuzhiyun 2317*4882a593SmuzhiyunConstraints 2318*4882a593Smuzhiyun----------- 2319*4882a593Smuzhiyun 2320*4882a593SmuzhiyunIf your chip supports unconventional sample rates, or only the limited 2321*4882a593Smuzhiyunsamples, you need to set a constraint for the condition. 2322*4882a593Smuzhiyun 2323*4882a593SmuzhiyunFor example, in order to restrict the sample rates in the some supported 2324*4882a593Smuzhiyunvalues, use :c:func:`snd_pcm_hw_constraint_list()`. You need to 2325*4882a593Smuzhiyuncall this function in the open callback. 2326*4882a593Smuzhiyun 2327*4882a593Smuzhiyun:: 2328*4882a593Smuzhiyun 2329*4882a593Smuzhiyun static unsigned int rates[] = 2330*4882a593Smuzhiyun {4000, 10000, 22050, 44100}; 2331*4882a593Smuzhiyun static struct snd_pcm_hw_constraint_list constraints_rates = { 2332*4882a593Smuzhiyun .count = ARRAY_SIZE(rates), 2333*4882a593Smuzhiyun .list = rates, 2334*4882a593Smuzhiyun .mask = 0, 2335*4882a593Smuzhiyun }; 2336*4882a593Smuzhiyun 2337*4882a593Smuzhiyun static int snd_mychip_pcm_open(struct snd_pcm_substream *substream) 2338*4882a593Smuzhiyun { 2339*4882a593Smuzhiyun int err; 2340*4882a593Smuzhiyun .... 2341*4882a593Smuzhiyun err = snd_pcm_hw_constraint_list(substream->runtime, 0, 2342*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_RATE, 2343*4882a593Smuzhiyun &constraints_rates); 2344*4882a593Smuzhiyun if (err < 0) 2345*4882a593Smuzhiyun return err; 2346*4882a593Smuzhiyun .... 2347*4882a593Smuzhiyun } 2348*4882a593Smuzhiyun 2349*4882a593Smuzhiyun 2350*4882a593Smuzhiyun 2351*4882a593SmuzhiyunThere are many different constraints. Look at ``sound/pcm.h`` for a 2352*4882a593Smuzhiyuncomplete list. You can even define your own constraint rules. For 2353*4882a593Smuzhiyunexample, let's suppose my_chip can manage a substream of 1 channel if 2354*4882a593Smuzhiyunand only if the format is ``S16_LE``, otherwise it supports any format 2355*4882a593Smuzhiyunspecified in struct snd_pcm_hardware> (or in any other 2356*4882a593Smuzhiyunconstraint_list). You can build a rule like this: 2357*4882a593Smuzhiyun 2358*4882a593Smuzhiyun:: 2359*4882a593Smuzhiyun 2360*4882a593Smuzhiyun static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params, 2361*4882a593Smuzhiyun struct snd_pcm_hw_rule *rule) 2362*4882a593Smuzhiyun { 2363*4882a593Smuzhiyun struct snd_interval *c = hw_param_interval(params, 2364*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_CHANNELS); 2365*4882a593Smuzhiyun struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2366*4882a593Smuzhiyun struct snd_interval ch; 2367*4882a593Smuzhiyun 2368*4882a593Smuzhiyun snd_interval_any(&ch); 2369*4882a593Smuzhiyun if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) { 2370*4882a593Smuzhiyun ch.min = ch.max = 1; 2371*4882a593Smuzhiyun ch.integer = 1; 2372*4882a593Smuzhiyun return snd_interval_refine(c, &ch); 2373*4882a593Smuzhiyun } 2374*4882a593Smuzhiyun return 0; 2375*4882a593Smuzhiyun } 2376*4882a593Smuzhiyun 2377*4882a593Smuzhiyun 2378*4882a593SmuzhiyunThen you need to call this function to add your rule: 2379*4882a593Smuzhiyun 2380*4882a593Smuzhiyun:: 2381*4882a593Smuzhiyun 2382*4882a593Smuzhiyun snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, 2383*4882a593Smuzhiyun hw_rule_channels_by_format, NULL, 2384*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_FORMAT, -1); 2385*4882a593Smuzhiyun 2386*4882a593SmuzhiyunThe rule function is called when an application sets the PCM format, and 2387*4882a593Smuzhiyunit refines the number of channels accordingly. But an application may 2388*4882a593Smuzhiyunset the number of channels before setting the format. Thus you also need 2389*4882a593Smuzhiyunto define the inverse rule: 2390*4882a593Smuzhiyun 2391*4882a593Smuzhiyun:: 2392*4882a593Smuzhiyun 2393*4882a593Smuzhiyun static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params, 2394*4882a593Smuzhiyun struct snd_pcm_hw_rule *rule) 2395*4882a593Smuzhiyun { 2396*4882a593Smuzhiyun struct snd_interval *c = hw_param_interval(params, 2397*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_CHANNELS); 2398*4882a593Smuzhiyun struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 2399*4882a593Smuzhiyun struct snd_mask fmt; 2400*4882a593Smuzhiyun 2401*4882a593Smuzhiyun snd_mask_any(&fmt); /* Init the struct */ 2402*4882a593Smuzhiyun if (c->min < 2) { 2403*4882a593Smuzhiyun fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE; 2404*4882a593Smuzhiyun return snd_mask_refine(f, &fmt); 2405*4882a593Smuzhiyun } 2406*4882a593Smuzhiyun return 0; 2407*4882a593Smuzhiyun } 2408*4882a593Smuzhiyun 2409*4882a593Smuzhiyun 2410*4882a593Smuzhiyun... and in the open callback: 2411*4882a593Smuzhiyun 2412*4882a593Smuzhiyun:: 2413*4882a593Smuzhiyun 2414*4882a593Smuzhiyun snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, 2415*4882a593Smuzhiyun hw_rule_format_by_channels, NULL, 2416*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_CHANNELS, -1); 2417*4882a593Smuzhiyun 2418*4882a593SmuzhiyunOne typical usage of the hw constraints is to align the buffer size 2419*4882a593Smuzhiyunwith the period size. As default, ALSA PCM core doesn't enforce the 2420*4882a593Smuzhiyunbuffer size to be aligned with the period size. For example, it'd be 2421*4882a593Smuzhiyunpossible to have a combination like 256 period bytes with 999 buffer 2422*4882a593Smuzhiyunbytes. 2423*4882a593Smuzhiyun 2424*4882a593SmuzhiyunMany device chips, however, require the buffer to be a multiple of 2425*4882a593Smuzhiyunperiods. In such a case, call 2426*4882a593Smuzhiyun:c:func:`snd_pcm_hw_constraint_integer()` for 2427*4882a593Smuzhiyun``SNDRV_PCM_HW_PARAM_PERIODS``. 2428*4882a593Smuzhiyun 2429*4882a593Smuzhiyun:: 2430*4882a593Smuzhiyun 2431*4882a593Smuzhiyun snd_pcm_hw_constraint_integer(substream->runtime, 2432*4882a593Smuzhiyun SNDRV_PCM_HW_PARAM_PERIODS); 2433*4882a593Smuzhiyun 2434*4882a593SmuzhiyunThis assures that the number of periods is integer, hence the buffer 2435*4882a593Smuzhiyunsize is aligned with the period size. 2436*4882a593Smuzhiyun 2437*4882a593SmuzhiyunThe hw constraint is a very much powerful mechanism to define the 2438*4882a593Smuzhiyunpreferred PCM configuration, and there are relevant helpers. 2439*4882a593SmuzhiyunI won't give more details here, rather I would like to say, “Luke, use 2440*4882a593Smuzhiyunthe source.” 2441*4882a593Smuzhiyun 2442*4882a593SmuzhiyunControl Interface 2443*4882a593Smuzhiyun================= 2444*4882a593Smuzhiyun 2445*4882a593SmuzhiyunGeneral 2446*4882a593Smuzhiyun------- 2447*4882a593Smuzhiyun 2448*4882a593SmuzhiyunThe control interface is used widely for many switches, sliders, etc. 2449*4882a593Smuzhiyunwhich are accessed from user-space. Its most important use is the mixer 2450*4882a593Smuzhiyuninterface. In other words, since ALSA 0.9.x, all the mixer stuff is 2451*4882a593Smuzhiyunimplemented on the control kernel API. 2452*4882a593Smuzhiyun 2453*4882a593SmuzhiyunALSA has a well-defined AC97 control module. If your chip supports only 2454*4882a593Smuzhiyunthe AC97 and nothing else, you can skip this section. 2455*4882a593Smuzhiyun 2456*4882a593SmuzhiyunThe control API is defined in ``<sound/control.h>``. Include this file 2457*4882a593Smuzhiyunif you want to add your own controls. 2458*4882a593Smuzhiyun 2459*4882a593SmuzhiyunDefinition of Controls 2460*4882a593Smuzhiyun---------------------- 2461*4882a593Smuzhiyun 2462*4882a593SmuzhiyunTo create a new control, you need to define the following three 2463*4882a593Smuzhiyuncallbacks: ``info``, ``get`` and ``put``. Then, define a 2464*4882a593Smuzhiyunstruct snd_kcontrol_new record, such as: 2465*4882a593Smuzhiyun 2466*4882a593Smuzhiyun:: 2467*4882a593Smuzhiyun 2468*4882a593Smuzhiyun 2469*4882a593Smuzhiyun static struct snd_kcontrol_new my_control = { 2470*4882a593Smuzhiyun .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2471*4882a593Smuzhiyun .name = "PCM Playback Switch", 2472*4882a593Smuzhiyun .index = 0, 2473*4882a593Smuzhiyun .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 2474*4882a593Smuzhiyun .private_value = 0xffff, 2475*4882a593Smuzhiyun .info = my_control_info, 2476*4882a593Smuzhiyun .get = my_control_get, 2477*4882a593Smuzhiyun .put = my_control_put 2478*4882a593Smuzhiyun }; 2479*4882a593Smuzhiyun 2480*4882a593Smuzhiyun 2481*4882a593SmuzhiyunThe ``iface`` field specifies the control type, 2482*4882a593Smuzhiyun``SNDRV_CTL_ELEM_IFACE_XXX``, which is usually ``MIXER``. Use ``CARD`` 2483*4882a593Smuzhiyunfor global controls that are not logically part of the mixer. If the 2484*4882a593Smuzhiyuncontrol is closely associated with some specific device on the sound 2485*4882a593Smuzhiyuncard, use ``HWDEP``, ``PCM``, ``RAWMIDI``, ``TIMER``, or ``SEQUENCER``, 2486*4882a593Smuzhiyunand specify the device number with the ``device`` and ``subdevice`` 2487*4882a593Smuzhiyunfields. 2488*4882a593Smuzhiyun 2489*4882a593SmuzhiyunThe ``name`` is the name identifier string. Since ALSA 0.9.x, the 2490*4882a593Smuzhiyuncontrol name is very important, because its role is classified from 2491*4882a593Smuzhiyunits name. There are pre-defined standard control names. The details 2492*4882a593Smuzhiyunare described in the `Control Names`_ subsection. 2493*4882a593Smuzhiyun 2494*4882a593SmuzhiyunThe ``index`` field holds the index number of this control. If there 2495*4882a593Smuzhiyunare several different controls with the same name, they can be 2496*4882a593Smuzhiyundistinguished by the index number. This is the case when several 2497*4882a593Smuzhiyuncodecs exist on the card. If the index is zero, you can omit the 2498*4882a593Smuzhiyundefinition above. 2499*4882a593Smuzhiyun 2500*4882a593SmuzhiyunThe ``access`` field contains the access type of this control. Give 2501*4882a593Smuzhiyunthe combination of bit masks, ``SNDRV_CTL_ELEM_ACCESS_XXX``, 2502*4882a593Smuzhiyunthere. The details will be explained in the `Access Flags`_ 2503*4882a593Smuzhiyunsubsection. 2504*4882a593Smuzhiyun 2505*4882a593SmuzhiyunThe ``private_value`` field contains an arbitrary long integer value 2506*4882a593Smuzhiyunfor this record. When using the generic ``info``, ``get`` and ``put`` 2507*4882a593Smuzhiyuncallbacks, you can pass a value through this field. If several small 2508*4882a593Smuzhiyunnumbers are necessary, you can combine them in bitwise. Or, it's 2509*4882a593Smuzhiyunpossible to give a pointer (casted to unsigned long) of some record to 2510*4882a593Smuzhiyunthis field, too. 2511*4882a593Smuzhiyun 2512*4882a593SmuzhiyunThe ``tlv`` field can be used to provide metadata about the control; 2513*4882a593Smuzhiyunsee the `Metadata`_ subsection. 2514*4882a593Smuzhiyun 2515*4882a593SmuzhiyunThe other three are `Control Callbacks`_. 2516*4882a593Smuzhiyun 2517*4882a593SmuzhiyunControl Names 2518*4882a593Smuzhiyun------------- 2519*4882a593Smuzhiyun 2520*4882a593SmuzhiyunThere are some standards to define the control names. A control is 2521*4882a593Smuzhiyunusually defined from the three parts as “SOURCE DIRECTION FUNCTION”. 2522*4882a593Smuzhiyun 2523*4882a593SmuzhiyunThe first, ``SOURCE``, specifies the source of the control, and is a 2524*4882a593Smuzhiyunstring such as “Master”, “PCM”, “CD” and “Line”. There are many 2525*4882a593Smuzhiyunpre-defined sources. 2526*4882a593Smuzhiyun 2527*4882a593SmuzhiyunThe second, ``DIRECTION``, is one of the following strings according to 2528*4882a593Smuzhiyunthe direction of the control: “Playback”, “Capture”, “Bypass Playback” 2529*4882a593Smuzhiyunand “Bypass Capture”. Or, it can be omitted, meaning both playback and 2530*4882a593Smuzhiyuncapture directions. 2531*4882a593Smuzhiyun 2532*4882a593SmuzhiyunThe third, ``FUNCTION``, is one of the following strings according to 2533*4882a593Smuzhiyunthe function of the control: “Switch”, “Volume” and “Route”. 2534*4882a593Smuzhiyun 2535*4882a593SmuzhiyunThe example of control names are, thus, “Master Capture Switch” or “PCM 2536*4882a593SmuzhiyunPlayback Volume”. 2537*4882a593Smuzhiyun 2538*4882a593SmuzhiyunThere are some exceptions: 2539*4882a593Smuzhiyun 2540*4882a593SmuzhiyunGlobal capture and playback 2541*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2542*4882a593Smuzhiyun 2543*4882a593Smuzhiyun“Capture Source”, “Capture Switch” and “Capture Volume” are used for the 2544*4882a593Smuzhiyunglobal capture (input) source, switch and volume. Similarly, “Playback 2545*4882a593SmuzhiyunSwitch” and “Playback Volume” are used for the global output gain switch 2546*4882a593Smuzhiyunand volume. 2547*4882a593Smuzhiyun 2548*4882a593SmuzhiyunTone-controls 2549*4882a593Smuzhiyun~~~~~~~~~~~~~ 2550*4882a593Smuzhiyun 2551*4882a593Smuzhiyuntone-control switch and volumes are specified like “Tone Control - XXX”, 2552*4882a593Smuzhiyune.g. “Tone Control - Switch”, “Tone Control - Bass”, “Tone Control - 2553*4882a593SmuzhiyunCenter”. 2554*4882a593Smuzhiyun 2555*4882a593Smuzhiyun3D controls 2556*4882a593Smuzhiyun~~~~~~~~~~~ 2557*4882a593Smuzhiyun 2558*4882a593Smuzhiyun3D-control switches and volumes are specified like “3D Control - XXX”, 2559*4882a593Smuzhiyune.g. “3D Control - Switch”, “3D Control - Center”, “3D Control - Space”. 2560*4882a593Smuzhiyun 2561*4882a593SmuzhiyunMic boost 2562*4882a593Smuzhiyun~~~~~~~~~ 2563*4882a593Smuzhiyun 2564*4882a593SmuzhiyunMic-boost switch is set as “Mic Boost” or “Mic Boost (6dB)”. 2565*4882a593Smuzhiyun 2566*4882a593SmuzhiyunMore precise information can be found in 2567*4882a593Smuzhiyun``Documentation/sound/designs/control-names.rst``. 2568*4882a593Smuzhiyun 2569*4882a593SmuzhiyunAccess Flags 2570*4882a593Smuzhiyun------------ 2571*4882a593Smuzhiyun 2572*4882a593SmuzhiyunThe access flag is the bitmask which specifies the access type of the 2573*4882a593Smuzhiyungiven control. The default access type is 2574*4882a593Smuzhiyun``SNDRV_CTL_ELEM_ACCESS_READWRITE``, which means both read and write are 2575*4882a593Smuzhiyunallowed to this control. When the access flag is omitted (i.e. = 0), it 2576*4882a593Smuzhiyunis considered as ``READWRITE`` access as default. 2577*4882a593Smuzhiyun 2578*4882a593SmuzhiyunWhen the control is read-only, pass ``SNDRV_CTL_ELEM_ACCESS_READ`` 2579*4882a593Smuzhiyuninstead. In this case, you don't have to define the ``put`` callback. 2580*4882a593SmuzhiyunSimilarly, when the control is write-only (although it's a rare case), 2581*4882a593Smuzhiyunyou can use the ``WRITE`` flag instead, and you don't need the ``get`` 2582*4882a593Smuzhiyuncallback. 2583*4882a593Smuzhiyun 2584*4882a593SmuzhiyunIf the control value changes frequently (e.g. the VU meter), 2585*4882a593Smuzhiyun``VOLATILE`` flag should be given. This means that the control may be 2586*4882a593Smuzhiyunchanged without `Change notification`_. Applications should poll such 2587*4882a593Smuzhiyuna control constantly. 2588*4882a593Smuzhiyun 2589*4882a593SmuzhiyunWhen the control is inactive, set the ``INACTIVE`` flag, too. There are 2590*4882a593Smuzhiyun``LOCK`` and ``OWNER`` flags to change the write permissions. 2591*4882a593Smuzhiyun 2592*4882a593SmuzhiyunControl Callbacks 2593*4882a593Smuzhiyun----------------- 2594*4882a593Smuzhiyun 2595*4882a593Smuzhiyuninfo callback 2596*4882a593Smuzhiyun~~~~~~~~~~~~~ 2597*4882a593Smuzhiyun 2598*4882a593SmuzhiyunThe ``info`` callback is used to get detailed information on this 2599*4882a593Smuzhiyuncontrol. This must store the values of the given 2600*4882a593Smuzhiyunstruct snd_ctl_elem_info object. For example, 2601*4882a593Smuzhiyunfor a boolean control with a single element: 2602*4882a593Smuzhiyun 2603*4882a593Smuzhiyun:: 2604*4882a593Smuzhiyun 2605*4882a593Smuzhiyun 2606*4882a593Smuzhiyun static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol, 2607*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo) 2608*4882a593Smuzhiyun { 2609*4882a593Smuzhiyun uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 2610*4882a593Smuzhiyun uinfo->count = 1; 2611*4882a593Smuzhiyun uinfo->value.integer.min = 0; 2612*4882a593Smuzhiyun uinfo->value.integer.max = 1; 2613*4882a593Smuzhiyun return 0; 2614*4882a593Smuzhiyun } 2615*4882a593Smuzhiyun 2616*4882a593Smuzhiyun 2617*4882a593Smuzhiyun 2618*4882a593SmuzhiyunThe ``type`` field specifies the type of the control. There are 2619*4882a593Smuzhiyun``BOOLEAN``, ``INTEGER``, ``ENUMERATED``, ``BYTES``, ``IEC958`` and 2620*4882a593Smuzhiyun``INTEGER64``. The ``count`` field specifies the number of elements in 2621*4882a593Smuzhiyunthis control. For example, a stereo volume would have count = 2. The 2622*4882a593Smuzhiyun``value`` field is a union, and the values stored are depending on the 2623*4882a593Smuzhiyuntype. The boolean and integer types are identical. 2624*4882a593Smuzhiyun 2625*4882a593SmuzhiyunThe enumerated type is a bit different from others. You'll need to set 2626*4882a593Smuzhiyunthe string for the currently given item index. 2627*4882a593Smuzhiyun 2628*4882a593Smuzhiyun:: 2629*4882a593Smuzhiyun 2630*4882a593Smuzhiyun static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, 2631*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo) 2632*4882a593Smuzhiyun { 2633*4882a593Smuzhiyun static char *texts[4] = { 2634*4882a593Smuzhiyun "First", "Second", "Third", "Fourth" 2635*4882a593Smuzhiyun }; 2636*4882a593Smuzhiyun uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2637*4882a593Smuzhiyun uinfo->count = 1; 2638*4882a593Smuzhiyun uinfo->value.enumerated.items = 4; 2639*4882a593Smuzhiyun if (uinfo->value.enumerated.item > 3) 2640*4882a593Smuzhiyun uinfo->value.enumerated.item = 3; 2641*4882a593Smuzhiyun strcpy(uinfo->value.enumerated.name, 2642*4882a593Smuzhiyun texts[uinfo->value.enumerated.item]); 2643*4882a593Smuzhiyun return 0; 2644*4882a593Smuzhiyun } 2645*4882a593Smuzhiyun 2646*4882a593SmuzhiyunThe above callback can be simplified with a helper function, 2647*4882a593Smuzhiyun:c:func:`snd_ctl_enum_info()`. The final code looks like below. 2648*4882a593Smuzhiyun(You can pass ``ARRAY_SIZE(texts)`` instead of 4 in the third argument; 2649*4882a593Smuzhiyunit's a matter of taste.) 2650*4882a593Smuzhiyun 2651*4882a593Smuzhiyun:: 2652*4882a593Smuzhiyun 2653*4882a593Smuzhiyun static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, 2654*4882a593Smuzhiyun struct snd_ctl_elem_info *uinfo) 2655*4882a593Smuzhiyun { 2656*4882a593Smuzhiyun static char *texts[4] = { 2657*4882a593Smuzhiyun "First", "Second", "Third", "Fourth" 2658*4882a593Smuzhiyun }; 2659*4882a593Smuzhiyun return snd_ctl_enum_info(uinfo, 1, 4, texts); 2660*4882a593Smuzhiyun } 2661*4882a593Smuzhiyun 2662*4882a593Smuzhiyun 2663*4882a593SmuzhiyunSome common info callbacks are available for your convenience: 2664*4882a593Smuzhiyun:c:func:`snd_ctl_boolean_mono_info()` and 2665*4882a593Smuzhiyun:c:func:`snd_ctl_boolean_stereo_info()`. Obviously, the former 2666*4882a593Smuzhiyunis an info callback for a mono channel boolean item, just like 2667*4882a593Smuzhiyun:c:func:`snd_myctl_mono_info()` above, and the latter is for a 2668*4882a593Smuzhiyunstereo channel boolean item. 2669*4882a593Smuzhiyun 2670*4882a593Smuzhiyunget callback 2671*4882a593Smuzhiyun~~~~~~~~~~~~ 2672*4882a593Smuzhiyun 2673*4882a593SmuzhiyunThis callback is used to read the current value of the control and to 2674*4882a593Smuzhiyunreturn to user-space. 2675*4882a593Smuzhiyun 2676*4882a593SmuzhiyunFor example, 2677*4882a593Smuzhiyun 2678*4882a593Smuzhiyun:: 2679*4882a593Smuzhiyun 2680*4882a593Smuzhiyun 2681*4882a593Smuzhiyun static int snd_myctl_get(struct snd_kcontrol *kcontrol, 2682*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol) 2683*4882a593Smuzhiyun { 2684*4882a593Smuzhiyun struct mychip *chip = snd_kcontrol_chip(kcontrol); 2685*4882a593Smuzhiyun ucontrol->value.integer.value[0] = get_some_value(chip); 2686*4882a593Smuzhiyun return 0; 2687*4882a593Smuzhiyun } 2688*4882a593Smuzhiyun 2689*4882a593Smuzhiyun 2690*4882a593Smuzhiyun 2691*4882a593SmuzhiyunThe ``value`` field depends on the type of control as well as on the 2692*4882a593Smuzhiyuninfo callback. For example, the sb driver uses this field to store the 2693*4882a593Smuzhiyunregister offset, the bit-shift and the bit-mask. The ``private_value`` 2694*4882a593Smuzhiyunfield is set as follows: 2695*4882a593Smuzhiyun 2696*4882a593Smuzhiyun:: 2697*4882a593Smuzhiyun 2698*4882a593Smuzhiyun .private_value = reg | (shift << 16) | (mask << 24) 2699*4882a593Smuzhiyun 2700*4882a593Smuzhiyunand is retrieved in callbacks like 2701*4882a593Smuzhiyun 2702*4882a593Smuzhiyun:: 2703*4882a593Smuzhiyun 2704*4882a593Smuzhiyun static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol, 2705*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol) 2706*4882a593Smuzhiyun { 2707*4882a593Smuzhiyun int reg = kcontrol->private_value & 0xff; 2708*4882a593Smuzhiyun int shift = (kcontrol->private_value >> 16) & 0xff; 2709*4882a593Smuzhiyun int mask = (kcontrol->private_value >> 24) & 0xff; 2710*4882a593Smuzhiyun .... 2711*4882a593Smuzhiyun } 2712*4882a593Smuzhiyun 2713*4882a593SmuzhiyunIn the ``get`` callback, you have to fill all the elements if the 2714*4882a593Smuzhiyuncontrol has more than one elements, i.e. ``count > 1``. In the example 2715*4882a593Smuzhiyunabove, we filled only one element (``value.integer.value[0]``) since 2716*4882a593Smuzhiyunit's assumed as ``count = 1``. 2717*4882a593Smuzhiyun 2718*4882a593Smuzhiyunput callback 2719*4882a593Smuzhiyun~~~~~~~~~~~~ 2720*4882a593Smuzhiyun 2721*4882a593SmuzhiyunThis callback is used to write a value from user-space. 2722*4882a593Smuzhiyun 2723*4882a593SmuzhiyunFor example, 2724*4882a593Smuzhiyun 2725*4882a593Smuzhiyun:: 2726*4882a593Smuzhiyun 2727*4882a593Smuzhiyun 2728*4882a593Smuzhiyun static int snd_myctl_put(struct snd_kcontrol *kcontrol, 2729*4882a593Smuzhiyun struct snd_ctl_elem_value *ucontrol) 2730*4882a593Smuzhiyun { 2731*4882a593Smuzhiyun struct mychip *chip = snd_kcontrol_chip(kcontrol); 2732*4882a593Smuzhiyun int changed = 0; 2733*4882a593Smuzhiyun if (chip->current_value != 2734*4882a593Smuzhiyun ucontrol->value.integer.value[0]) { 2735*4882a593Smuzhiyun change_current_value(chip, 2736*4882a593Smuzhiyun ucontrol->value.integer.value[0]); 2737*4882a593Smuzhiyun changed = 1; 2738*4882a593Smuzhiyun } 2739*4882a593Smuzhiyun return changed; 2740*4882a593Smuzhiyun } 2741*4882a593Smuzhiyun 2742*4882a593Smuzhiyun 2743*4882a593Smuzhiyun 2744*4882a593SmuzhiyunAs seen above, you have to return 1 if the value is changed. If the 2745*4882a593Smuzhiyunvalue is not changed, return 0 instead. If any fatal error happens, 2746*4882a593Smuzhiyunreturn a negative error code as usual. 2747*4882a593Smuzhiyun 2748*4882a593SmuzhiyunAs in the ``get`` callback, when the control has more than one 2749*4882a593Smuzhiyunelements, all elements must be evaluated in this callback, too. 2750*4882a593Smuzhiyun 2751*4882a593SmuzhiyunCallbacks are not atomic 2752*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~ 2753*4882a593Smuzhiyun 2754*4882a593SmuzhiyunAll these three callbacks are basically not atomic. 2755*4882a593Smuzhiyun 2756*4882a593SmuzhiyunControl Constructor 2757*4882a593Smuzhiyun------------------- 2758*4882a593Smuzhiyun 2759*4882a593SmuzhiyunWhen everything is ready, finally we can create a new control. To create 2760*4882a593Smuzhiyuna control, there are two functions to be called, 2761*4882a593Smuzhiyun:c:func:`snd_ctl_new1()` and :c:func:`snd_ctl_add()`. 2762*4882a593Smuzhiyun 2763*4882a593SmuzhiyunIn the simplest way, you can do like this: 2764*4882a593Smuzhiyun 2765*4882a593Smuzhiyun:: 2766*4882a593Smuzhiyun 2767*4882a593Smuzhiyun err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip)); 2768*4882a593Smuzhiyun if (err < 0) 2769*4882a593Smuzhiyun return err; 2770*4882a593Smuzhiyun 2771*4882a593Smuzhiyunwhere ``my_control`` is the struct snd_kcontrol_new object defined above, 2772*4882a593Smuzhiyunand chip is the object pointer to be passed to kcontrol->private_data which 2773*4882a593Smuzhiyuncan be referred to in callbacks. 2774*4882a593Smuzhiyun 2775*4882a593Smuzhiyun:c:func:`snd_ctl_new1()` allocates a new struct snd_kcontrol instance, and 2776*4882a593Smuzhiyun:c:func:`snd_ctl_add()` assigns the given control component to the 2777*4882a593Smuzhiyuncard. 2778*4882a593Smuzhiyun 2779*4882a593SmuzhiyunChange Notification 2780*4882a593Smuzhiyun------------------- 2781*4882a593Smuzhiyun 2782*4882a593SmuzhiyunIf you need to change and update a control in the interrupt routine, you 2783*4882a593Smuzhiyuncan call :c:func:`snd_ctl_notify()`. For example, 2784*4882a593Smuzhiyun 2785*4882a593Smuzhiyun:: 2786*4882a593Smuzhiyun 2787*4882a593Smuzhiyun snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer); 2788*4882a593Smuzhiyun 2789*4882a593SmuzhiyunThis function takes the card pointer, the event-mask, and the control id 2790*4882a593Smuzhiyunpointer for the notification. The event-mask specifies the types of 2791*4882a593Smuzhiyunnotification, for example, in the above example, the change of control 2792*4882a593Smuzhiyunvalues is notified. The id pointer is the pointer of struct snd_ctl_elem_id 2793*4882a593Smuzhiyunto be notified. You can find some examples in ``es1938.c`` or ``es1968.c`` 2794*4882a593Smuzhiyunfor hardware volume interrupts. 2795*4882a593Smuzhiyun 2796*4882a593SmuzhiyunMetadata 2797*4882a593Smuzhiyun-------- 2798*4882a593Smuzhiyun 2799*4882a593SmuzhiyunTo provide information about the dB values of a mixer control, use on of 2800*4882a593Smuzhiyunthe ``DECLARE_TLV_xxx`` macros from ``<sound/tlv.h>`` to define a 2801*4882a593Smuzhiyunvariable containing this information, set the ``tlv.p`` field to point to 2802*4882a593Smuzhiyunthis variable, and include the ``SNDRV_CTL_ELEM_ACCESS_TLV_READ`` flag 2803*4882a593Smuzhiyunin the ``access`` field; like this: 2804*4882a593Smuzhiyun 2805*4882a593Smuzhiyun:: 2806*4882a593Smuzhiyun 2807*4882a593Smuzhiyun static DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0); 2808*4882a593Smuzhiyun 2809*4882a593Smuzhiyun static struct snd_kcontrol_new my_control = { 2810*4882a593Smuzhiyun ... 2811*4882a593Smuzhiyun .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | 2812*4882a593Smuzhiyun SNDRV_CTL_ELEM_ACCESS_TLV_READ, 2813*4882a593Smuzhiyun ... 2814*4882a593Smuzhiyun .tlv.p = db_scale_my_control, 2815*4882a593Smuzhiyun }; 2816*4882a593Smuzhiyun 2817*4882a593Smuzhiyun 2818*4882a593SmuzhiyunThe :c:func:`DECLARE_TLV_DB_SCALE()` macro defines information 2819*4882a593Smuzhiyunabout a mixer control where each step in the control's value changes the 2820*4882a593SmuzhiyundB value by a constant dB amount. The first parameter is the name of the 2821*4882a593Smuzhiyunvariable to be defined. The second parameter is the minimum value, in 2822*4882a593Smuzhiyununits of 0.01 dB. The third parameter is the step size, in units of 0.01 2823*4882a593SmuzhiyundB. Set the fourth parameter to 1 if the minimum value actually mutes 2824*4882a593Smuzhiyunthe control. 2825*4882a593Smuzhiyun 2826*4882a593SmuzhiyunThe :c:func:`DECLARE_TLV_DB_LINEAR()` macro defines information 2827*4882a593Smuzhiyunabout a mixer control where the control's value affects the output 2828*4882a593Smuzhiyunlinearly. The first parameter is the name of the variable to be defined. 2829*4882a593SmuzhiyunThe second parameter is the minimum value, in units of 0.01 dB. The 2830*4882a593Smuzhiyunthird parameter is the maximum value, in units of 0.01 dB. If the 2831*4882a593Smuzhiyunminimum value mutes the control, set the second parameter to 2832*4882a593Smuzhiyun``TLV_DB_GAIN_MUTE``. 2833*4882a593Smuzhiyun 2834*4882a593SmuzhiyunAPI for AC97 Codec 2835*4882a593Smuzhiyun================== 2836*4882a593Smuzhiyun 2837*4882a593SmuzhiyunGeneral 2838*4882a593Smuzhiyun------- 2839*4882a593Smuzhiyun 2840*4882a593SmuzhiyunThe ALSA AC97 codec layer is a well-defined one, and you don't have to 2841*4882a593Smuzhiyunwrite much code to control it. Only low-level control routines are 2842*4882a593Smuzhiyunnecessary. The AC97 codec API is defined in ``<sound/ac97_codec.h>``. 2843*4882a593Smuzhiyun 2844*4882a593SmuzhiyunFull Code Example 2845*4882a593Smuzhiyun----------------- 2846*4882a593Smuzhiyun 2847*4882a593Smuzhiyun:: 2848*4882a593Smuzhiyun 2849*4882a593Smuzhiyun struct mychip { 2850*4882a593Smuzhiyun .... 2851*4882a593Smuzhiyun struct snd_ac97 *ac97; 2852*4882a593Smuzhiyun .... 2853*4882a593Smuzhiyun }; 2854*4882a593Smuzhiyun 2855*4882a593Smuzhiyun static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, 2856*4882a593Smuzhiyun unsigned short reg) 2857*4882a593Smuzhiyun { 2858*4882a593Smuzhiyun struct mychip *chip = ac97->private_data; 2859*4882a593Smuzhiyun .... 2860*4882a593Smuzhiyun /* read a register value here from the codec */ 2861*4882a593Smuzhiyun return the_register_value; 2862*4882a593Smuzhiyun } 2863*4882a593Smuzhiyun 2864*4882a593Smuzhiyun static void snd_mychip_ac97_write(struct snd_ac97 *ac97, 2865*4882a593Smuzhiyun unsigned short reg, unsigned short val) 2866*4882a593Smuzhiyun { 2867*4882a593Smuzhiyun struct mychip *chip = ac97->private_data; 2868*4882a593Smuzhiyun .... 2869*4882a593Smuzhiyun /* write the given register value to the codec */ 2870*4882a593Smuzhiyun } 2871*4882a593Smuzhiyun 2872*4882a593Smuzhiyun static int snd_mychip_ac97(struct mychip *chip) 2873*4882a593Smuzhiyun { 2874*4882a593Smuzhiyun struct snd_ac97_bus *bus; 2875*4882a593Smuzhiyun struct snd_ac97_template ac97; 2876*4882a593Smuzhiyun int err; 2877*4882a593Smuzhiyun static struct snd_ac97_bus_ops ops = { 2878*4882a593Smuzhiyun .write = snd_mychip_ac97_write, 2879*4882a593Smuzhiyun .read = snd_mychip_ac97_read, 2880*4882a593Smuzhiyun }; 2881*4882a593Smuzhiyun 2882*4882a593Smuzhiyun err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus); 2883*4882a593Smuzhiyun if (err < 0) 2884*4882a593Smuzhiyun return err; 2885*4882a593Smuzhiyun memset(&ac97, 0, sizeof(ac97)); 2886*4882a593Smuzhiyun ac97.private_data = chip; 2887*4882a593Smuzhiyun return snd_ac97_mixer(bus, &ac97, &chip->ac97); 2888*4882a593Smuzhiyun } 2889*4882a593Smuzhiyun 2890*4882a593Smuzhiyun 2891*4882a593SmuzhiyunAC97 Constructor 2892*4882a593Smuzhiyun---------------- 2893*4882a593Smuzhiyun 2894*4882a593SmuzhiyunTo create an ac97 instance, first call :c:func:`snd_ac97_bus()` 2895*4882a593Smuzhiyunwith an ``ac97_bus_ops_t`` record with callback functions. 2896*4882a593Smuzhiyun 2897*4882a593Smuzhiyun:: 2898*4882a593Smuzhiyun 2899*4882a593Smuzhiyun struct snd_ac97_bus *bus; 2900*4882a593Smuzhiyun static struct snd_ac97_bus_ops ops = { 2901*4882a593Smuzhiyun .write = snd_mychip_ac97_write, 2902*4882a593Smuzhiyun .read = snd_mychip_ac97_read, 2903*4882a593Smuzhiyun }; 2904*4882a593Smuzhiyun 2905*4882a593Smuzhiyun snd_ac97_bus(card, 0, &ops, NULL, &pbus); 2906*4882a593Smuzhiyun 2907*4882a593SmuzhiyunThe bus record is shared among all belonging ac97 instances. 2908*4882a593Smuzhiyun 2909*4882a593SmuzhiyunAnd then call :c:func:`snd_ac97_mixer()` with an struct snd_ac97_template 2910*4882a593Smuzhiyunrecord together with the bus pointer created above. 2911*4882a593Smuzhiyun 2912*4882a593Smuzhiyun:: 2913*4882a593Smuzhiyun 2914*4882a593Smuzhiyun struct snd_ac97_template ac97; 2915*4882a593Smuzhiyun int err; 2916*4882a593Smuzhiyun 2917*4882a593Smuzhiyun memset(&ac97, 0, sizeof(ac97)); 2918*4882a593Smuzhiyun ac97.private_data = chip; 2919*4882a593Smuzhiyun snd_ac97_mixer(bus, &ac97, &chip->ac97); 2920*4882a593Smuzhiyun 2921*4882a593Smuzhiyunwhere chip->ac97 is a pointer to a newly created ``ac97_t`` 2922*4882a593Smuzhiyuninstance. In this case, the chip pointer is set as the private data, 2923*4882a593Smuzhiyunso that the read/write callback functions can refer to this chip 2924*4882a593Smuzhiyuninstance. This instance is not necessarily stored in the chip 2925*4882a593Smuzhiyunrecord. If you need to change the register values from the driver, or 2926*4882a593Smuzhiyunneed the suspend/resume of ac97 codecs, keep this pointer to pass to 2927*4882a593Smuzhiyunthe corresponding functions. 2928*4882a593Smuzhiyun 2929*4882a593SmuzhiyunAC97 Callbacks 2930*4882a593Smuzhiyun-------------- 2931*4882a593Smuzhiyun 2932*4882a593SmuzhiyunThe standard callbacks are ``read`` and ``write``. Obviously they 2933*4882a593Smuzhiyuncorrespond to the functions for read and write accesses to the 2934*4882a593Smuzhiyunhardware low-level codes. 2935*4882a593Smuzhiyun 2936*4882a593SmuzhiyunThe ``read`` callback returns the register value specified in the 2937*4882a593Smuzhiyunargument. 2938*4882a593Smuzhiyun 2939*4882a593Smuzhiyun:: 2940*4882a593Smuzhiyun 2941*4882a593Smuzhiyun static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, 2942*4882a593Smuzhiyun unsigned short reg) 2943*4882a593Smuzhiyun { 2944*4882a593Smuzhiyun struct mychip *chip = ac97->private_data; 2945*4882a593Smuzhiyun .... 2946*4882a593Smuzhiyun return the_register_value; 2947*4882a593Smuzhiyun } 2948*4882a593Smuzhiyun 2949*4882a593SmuzhiyunHere, the chip can be cast from ``ac97->private_data``. 2950*4882a593Smuzhiyun 2951*4882a593SmuzhiyunMeanwhile, the ``write`` callback is used to set the register 2952*4882a593Smuzhiyunvalue 2953*4882a593Smuzhiyun 2954*4882a593Smuzhiyun:: 2955*4882a593Smuzhiyun 2956*4882a593Smuzhiyun static void snd_mychip_ac97_write(struct snd_ac97 *ac97, 2957*4882a593Smuzhiyun unsigned short reg, unsigned short val) 2958*4882a593Smuzhiyun 2959*4882a593Smuzhiyun 2960*4882a593SmuzhiyunThese callbacks are non-atomic like the control API callbacks. 2961*4882a593Smuzhiyun 2962*4882a593SmuzhiyunThere are also other callbacks: ``reset``, ``wait`` and ``init``. 2963*4882a593Smuzhiyun 2964*4882a593SmuzhiyunThe ``reset`` callback is used to reset the codec. If the chip 2965*4882a593Smuzhiyunrequires a special kind of reset, you can define this callback. 2966*4882a593Smuzhiyun 2967*4882a593SmuzhiyunThe ``wait`` callback is used to add some waiting time in the standard 2968*4882a593Smuzhiyuninitialization of the codec. If the chip requires the extra waiting 2969*4882a593Smuzhiyuntime, define this callback. 2970*4882a593Smuzhiyun 2971*4882a593SmuzhiyunThe ``init`` callback is used for additional initialization of the 2972*4882a593Smuzhiyuncodec. 2973*4882a593Smuzhiyun 2974*4882a593SmuzhiyunUpdating Registers in The Driver 2975*4882a593Smuzhiyun-------------------------------- 2976*4882a593Smuzhiyun 2977*4882a593SmuzhiyunIf you need to access to the codec from the driver, you can call the 2978*4882a593Smuzhiyunfollowing functions: :c:func:`snd_ac97_write()`, 2979*4882a593Smuzhiyun:c:func:`snd_ac97_read()`, :c:func:`snd_ac97_update()` and 2980*4882a593Smuzhiyun:c:func:`snd_ac97_update_bits()`. 2981*4882a593Smuzhiyun 2982*4882a593SmuzhiyunBoth :c:func:`snd_ac97_write()` and 2983*4882a593Smuzhiyun:c:func:`snd_ac97_update()` functions are used to set a value to 2984*4882a593Smuzhiyunthe given register (``AC97_XXX``). The difference between them is that 2985*4882a593Smuzhiyun:c:func:`snd_ac97_update()` doesn't write a value if the given 2986*4882a593Smuzhiyunvalue has been already set, while :c:func:`snd_ac97_write()` 2987*4882a593Smuzhiyunalways rewrites the value. 2988*4882a593Smuzhiyun 2989*4882a593Smuzhiyun:: 2990*4882a593Smuzhiyun 2991*4882a593Smuzhiyun snd_ac97_write(ac97, AC97_MASTER, 0x8080); 2992*4882a593Smuzhiyun snd_ac97_update(ac97, AC97_MASTER, 0x8080); 2993*4882a593Smuzhiyun 2994*4882a593Smuzhiyun:c:func:`snd_ac97_read()` is used to read the value of the given 2995*4882a593Smuzhiyunregister. For example, 2996*4882a593Smuzhiyun 2997*4882a593Smuzhiyun:: 2998*4882a593Smuzhiyun 2999*4882a593Smuzhiyun value = snd_ac97_read(ac97, AC97_MASTER); 3000*4882a593Smuzhiyun 3001*4882a593Smuzhiyun:c:func:`snd_ac97_update_bits()` is used to update some bits in 3002*4882a593Smuzhiyunthe given register. 3003*4882a593Smuzhiyun 3004*4882a593Smuzhiyun:: 3005*4882a593Smuzhiyun 3006*4882a593Smuzhiyun snd_ac97_update_bits(ac97, reg, mask, value); 3007*4882a593Smuzhiyun 3008*4882a593SmuzhiyunAlso, there is a function to change the sample rate (of a given register 3009*4882a593Smuzhiyunsuch as ``AC97_PCM_FRONT_DAC_RATE``) when VRA or DRA is supported by the 3010*4882a593Smuzhiyuncodec: :c:func:`snd_ac97_set_rate()`. 3011*4882a593Smuzhiyun 3012*4882a593Smuzhiyun:: 3013*4882a593Smuzhiyun 3014*4882a593Smuzhiyun snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100); 3015*4882a593Smuzhiyun 3016*4882a593Smuzhiyun 3017*4882a593SmuzhiyunThe following registers are available to set the rate: 3018*4882a593Smuzhiyun``AC97_PCM_MIC_ADC_RATE``, ``AC97_PCM_FRONT_DAC_RATE``, 3019*4882a593Smuzhiyun``AC97_PCM_LR_ADC_RATE``, ``AC97_SPDIF``. When ``AC97_SPDIF`` is 3020*4882a593Smuzhiyunspecified, the register is not really changed but the corresponding 3021*4882a593SmuzhiyunIEC958 status bits will be updated. 3022*4882a593Smuzhiyun 3023*4882a593SmuzhiyunClock Adjustment 3024*4882a593Smuzhiyun---------------- 3025*4882a593Smuzhiyun 3026*4882a593SmuzhiyunIn some chips, the clock of the codec isn't 48000 but using a PCI clock 3027*4882a593Smuzhiyun(to save a quartz!). In this case, change the field ``bus->clock`` to 3028*4882a593Smuzhiyunthe corresponding value. For example, intel8x0 and es1968 drivers have 3029*4882a593Smuzhiyuntheir own function to read from the clock. 3030*4882a593Smuzhiyun 3031*4882a593SmuzhiyunProc Files 3032*4882a593Smuzhiyun---------- 3033*4882a593Smuzhiyun 3034*4882a593SmuzhiyunThe ALSA AC97 interface will create a proc file such as 3035*4882a593Smuzhiyun``/proc/asound/card0/codec97#0/ac97#0-0`` and ``ac97#0-0+regs``. You 3036*4882a593Smuzhiyuncan refer to these files to see the current status and registers of 3037*4882a593Smuzhiyunthe codec. 3038*4882a593Smuzhiyun 3039*4882a593SmuzhiyunMultiple Codecs 3040*4882a593Smuzhiyun--------------- 3041*4882a593Smuzhiyun 3042*4882a593SmuzhiyunWhen there are several codecs on the same card, you need to call 3043*4882a593Smuzhiyun:c:func:`snd_ac97_mixer()` multiple times with ``ac97.num=1`` or 3044*4882a593Smuzhiyungreater. The ``num`` field specifies the codec number. 3045*4882a593Smuzhiyun 3046*4882a593SmuzhiyunIf you set up multiple codecs, you either need to write different 3047*4882a593Smuzhiyuncallbacks for each codec or check ``ac97->num`` in the callback 3048*4882a593Smuzhiyunroutines. 3049*4882a593Smuzhiyun 3050*4882a593SmuzhiyunMIDI (MPU401-UART) Interface 3051*4882a593Smuzhiyun============================ 3052*4882a593Smuzhiyun 3053*4882a593SmuzhiyunGeneral 3054*4882a593Smuzhiyun------- 3055*4882a593Smuzhiyun 3056*4882a593SmuzhiyunMany soundcards have built-in MIDI (MPU401-UART) interfaces. When the 3057*4882a593Smuzhiyunsoundcard supports the standard MPU401-UART interface, most likely you 3058*4882a593Smuzhiyuncan use the ALSA MPU401-UART API. The MPU401-UART API is defined in 3059*4882a593Smuzhiyun``<sound/mpu401.h>``. 3060*4882a593Smuzhiyun 3061*4882a593SmuzhiyunSome soundchips have a similar but slightly different implementation of 3062*4882a593Smuzhiyunmpu401 stuff. For example, emu10k1 has its own mpu401 routines. 3063*4882a593Smuzhiyun 3064*4882a593SmuzhiyunMIDI Constructor 3065*4882a593Smuzhiyun---------------- 3066*4882a593Smuzhiyun 3067*4882a593SmuzhiyunTo create a rawmidi object, call :c:func:`snd_mpu401_uart_new()`. 3068*4882a593Smuzhiyun 3069*4882a593Smuzhiyun:: 3070*4882a593Smuzhiyun 3071*4882a593Smuzhiyun struct snd_rawmidi *rmidi; 3072*4882a593Smuzhiyun snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags, 3073*4882a593Smuzhiyun irq, &rmidi); 3074*4882a593Smuzhiyun 3075*4882a593Smuzhiyun 3076*4882a593SmuzhiyunThe first argument is the card pointer, and the second is the index of 3077*4882a593Smuzhiyunthis component. You can create up to 8 rawmidi devices. 3078*4882a593Smuzhiyun 3079*4882a593SmuzhiyunThe third argument is the type of the hardware, ``MPU401_HW_XXX``. If 3080*4882a593Smuzhiyunit's not a special one, you can use ``MPU401_HW_MPU401``. 3081*4882a593Smuzhiyun 3082*4882a593SmuzhiyunThe 4th argument is the I/O port address. Many backward-compatible 3083*4882a593SmuzhiyunMPU401 have an I/O port such as 0x330. Or, it might be a part of its own 3084*4882a593SmuzhiyunPCI I/O region. It depends on the chip design. 3085*4882a593Smuzhiyun 3086*4882a593SmuzhiyunThe 5th argument is a bitflag for additional information. When the I/O 3087*4882a593Smuzhiyunport address above is part of the PCI I/O region, the MPU401 I/O port 3088*4882a593Smuzhiyunmight have been already allocated (reserved) by the driver itself. In 3089*4882a593Smuzhiyunsuch a case, pass a bit flag ``MPU401_INFO_INTEGRATED``, and the 3090*4882a593Smuzhiyunmpu401-uart layer will allocate the I/O ports by itself. 3091*4882a593Smuzhiyun 3092*4882a593SmuzhiyunWhen the controller supports only the input or output MIDI stream, pass 3093*4882a593Smuzhiyunthe ``MPU401_INFO_INPUT`` or ``MPU401_INFO_OUTPUT`` bitflag, 3094*4882a593Smuzhiyunrespectively. Then the rawmidi instance is created as a single stream. 3095*4882a593Smuzhiyun 3096*4882a593Smuzhiyun``MPU401_INFO_MMIO`` bitflag is used to change the access method to MMIO 3097*4882a593Smuzhiyun(via readb and writeb) instead of iob and outb. In this case, you have 3098*4882a593Smuzhiyunto pass the iomapped address to :c:func:`snd_mpu401_uart_new()`. 3099*4882a593Smuzhiyun 3100*4882a593SmuzhiyunWhen ``MPU401_INFO_TX_IRQ`` is set, the output stream isn't checked in 3101*4882a593Smuzhiyunthe default interrupt handler. The driver needs to call 3102*4882a593Smuzhiyun:c:func:`snd_mpu401_uart_interrupt_tx()` by itself to start 3103*4882a593Smuzhiyunprocessing the output stream in the irq handler. 3104*4882a593Smuzhiyun 3105*4882a593SmuzhiyunIf the MPU-401 interface shares its interrupt with the other logical 3106*4882a593Smuzhiyundevices on the card, set ``MPU401_INFO_IRQ_HOOK`` (see 3107*4882a593Smuzhiyun`below <#MIDI-Interrupt-Handler>`__). 3108*4882a593Smuzhiyun 3109*4882a593SmuzhiyunUsually, the port address corresponds to the command port and port + 1 3110*4882a593Smuzhiyuncorresponds to the data port. If not, you may change the ``cport`` 3111*4882a593Smuzhiyunfield of struct snd_mpu401 manually afterward. 3112*4882a593SmuzhiyunHowever, struct snd_mpu401 pointer is 3113*4882a593Smuzhiyunnot returned explicitly by :c:func:`snd_mpu401_uart_new()`. You 3114*4882a593Smuzhiyunneed to cast ``rmidi->private_data`` to struct snd_mpu401 explicitly, 3115*4882a593Smuzhiyun 3116*4882a593Smuzhiyun:: 3117*4882a593Smuzhiyun 3118*4882a593Smuzhiyun struct snd_mpu401 *mpu; 3119*4882a593Smuzhiyun mpu = rmidi->private_data; 3120*4882a593Smuzhiyun 3121*4882a593Smuzhiyunand reset the ``cport`` as you like: 3122*4882a593Smuzhiyun 3123*4882a593Smuzhiyun:: 3124*4882a593Smuzhiyun 3125*4882a593Smuzhiyun mpu->cport = my_own_control_port; 3126*4882a593Smuzhiyun 3127*4882a593SmuzhiyunThe 6th argument specifies the ISA irq number that will be allocated. If 3128*4882a593Smuzhiyunno interrupt is to be allocated (because your code is already allocating 3129*4882a593Smuzhiyuna shared interrupt, or because the device does not use interrupts), pass 3130*4882a593Smuzhiyun-1 instead. For a MPU-401 device without an interrupt, a polling timer 3131*4882a593Smuzhiyunwill be used instead. 3132*4882a593Smuzhiyun 3133*4882a593SmuzhiyunMIDI Interrupt Handler 3134*4882a593Smuzhiyun---------------------- 3135*4882a593Smuzhiyun 3136*4882a593SmuzhiyunWhen the interrupt is allocated in 3137*4882a593Smuzhiyun:c:func:`snd_mpu401_uart_new()`, an exclusive ISA interrupt 3138*4882a593Smuzhiyunhandler is automatically used, hence you don't have anything else to do 3139*4882a593Smuzhiyunthan creating the mpu401 stuff. Otherwise, you have to set 3140*4882a593Smuzhiyun``MPU401_INFO_IRQ_HOOK``, and call 3141*4882a593Smuzhiyun:c:func:`snd_mpu401_uart_interrupt()` explicitly from your own 3142*4882a593Smuzhiyuninterrupt handler when it has determined that a UART interrupt has 3143*4882a593Smuzhiyunoccurred. 3144*4882a593Smuzhiyun 3145*4882a593SmuzhiyunIn this case, you need to pass the private_data of the returned rawmidi 3146*4882a593Smuzhiyunobject from :c:func:`snd_mpu401_uart_new()` as the second 3147*4882a593Smuzhiyunargument of :c:func:`snd_mpu401_uart_interrupt()`. 3148*4882a593Smuzhiyun 3149*4882a593Smuzhiyun:: 3150*4882a593Smuzhiyun 3151*4882a593Smuzhiyun snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs); 3152*4882a593Smuzhiyun 3153*4882a593Smuzhiyun 3154*4882a593SmuzhiyunRawMIDI Interface 3155*4882a593Smuzhiyun================= 3156*4882a593Smuzhiyun 3157*4882a593SmuzhiyunOverview 3158*4882a593Smuzhiyun-------- 3159*4882a593Smuzhiyun 3160*4882a593SmuzhiyunThe raw MIDI interface is used for hardware MIDI ports that can be 3161*4882a593Smuzhiyunaccessed as a byte stream. It is not used for synthesizer chips that do 3162*4882a593Smuzhiyunnot directly understand MIDI. 3163*4882a593Smuzhiyun 3164*4882a593SmuzhiyunALSA handles file and buffer management. All you have to do is to write 3165*4882a593Smuzhiyunsome code to move data between the buffer and the hardware. 3166*4882a593Smuzhiyun 3167*4882a593SmuzhiyunThe rawmidi API is defined in ``<sound/rawmidi.h>``. 3168*4882a593Smuzhiyun 3169*4882a593SmuzhiyunRawMIDI Constructor 3170*4882a593Smuzhiyun------------------- 3171*4882a593Smuzhiyun 3172*4882a593SmuzhiyunTo create a rawmidi device, call the :c:func:`snd_rawmidi_new()` 3173*4882a593Smuzhiyunfunction: 3174*4882a593Smuzhiyun 3175*4882a593Smuzhiyun:: 3176*4882a593Smuzhiyun 3177*4882a593Smuzhiyun struct snd_rawmidi *rmidi; 3178*4882a593Smuzhiyun err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi); 3179*4882a593Smuzhiyun if (err < 0) 3180*4882a593Smuzhiyun return err; 3181*4882a593Smuzhiyun rmidi->private_data = chip; 3182*4882a593Smuzhiyun strcpy(rmidi->name, "My MIDI"); 3183*4882a593Smuzhiyun rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | 3184*4882a593Smuzhiyun SNDRV_RAWMIDI_INFO_INPUT | 3185*4882a593Smuzhiyun SNDRV_RAWMIDI_INFO_DUPLEX; 3186*4882a593Smuzhiyun 3187*4882a593SmuzhiyunThe first argument is the card pointer, the second argument is the ID 3188*4882a593Smuzhiyunstring. 3189*4882a593Smuzhiyun 3190*4882a593SmuzhiyunThe third argument is the index of this component. You can create up to 3191*4882a593Smuzhiyun8 rawmidi devices. 3192*4882a593Smuzhiyun 3193*4882a593SmuzhiyunThe fourth and fifth arguments are the number of output and input 3194*4882a593Smuzhiyunsubstreams, respectively, of this device (a substream is the equivalent 3195*4882a593Smuzhiyunof a MIDI port). 3196*4882a593Smuzhiyun 3197*4882a593SmuzhiyunSet the ``info_flags`` field to specify the capabilities of the 3198*4882a593Smuzhiyundevice. Set ``SNDRV_RAWMIDI_INFO_OUTPUT`` if there is at least one 3199*4882a593Smuzhiyunoutput port, ``SNDRV_RAWMIDI_INFO_INPUT`` if there is at least one 3200*4882a593Smuzhiyuninput port, and ``SNDRV_RAWMIDI_INFO_DUPLEX`` if the device can handle 3201*4882a593Smuzhiyunoutput and input at the same time. 3202*4882a593Smuzhiyun 3203*4882a593SmuzhiyunAfter the rawmidi device is created, you need to set the operators 3204*4882a593Smuzhiyun(callbacks) for each substream. There are helper functions to set the 3205*4882a593Smuzhiyunoperators for all the substreams of a device: 3206*4882a593Smuzhiyun 3207*4882a593Smuzhiyun:: 3208*4882a593Smuzhiyun 3209*4882a593Smuzhiyun snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops); 3210*4882a593Smuzhiyun snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops); 3211*4882a593Smuzhiyun 3212*4882a593SmuzhiyunThe operators are usually defined like this: 3213*4882a593Smuzhiyun 3214*4882a593Smuzhiyun:: 3215*4882a593Smuzhiyun 3216*4882a593Smuzhiyun static struct snd_rawmidi_ops snd_mymidi_output_ops = { 3217*4882a593Smuzhiyun .open = snd_mymidi_output_open, 3218*4882a593Smuzhiyun .close = snd_mymidi_output_close, 3219*4882a593Smuzhiyun .trigger = snd_mymidi_output_trigger, 3220*4882a593Smuzhiyun }; 3221*4882a593Smuzhiyun 3222*4882a593SmuzhiyunThese callbacks are explained in the `RawMIDI Callbacks`_ section. 3223*4882a593Smuzhiyun 3224*4882a593SmuzhiyunIf there are more than one substream, you should give a unique name to 3225*4882a593Smuzhiyuneach of them: 3226*4882a593Smuzhiyun 3227*4882a593Smuzhiyun:: 3228*4882a593Smuzhiyun 3229*4882a593Smuzhiyun struct snd_rawmidi_substream *substream; 3230*4882a593Smuzhiyun list_for_each_entry(substream, 3231*4882a593Smuzhiyun &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, 3232*4882a593Smuzhiyun list { 3233*4882a593Smuzhiyun sprintf(substream->name, "My MIDI Port %d", substream->number + 1); 3234*4882a593Smuzhiyun } 3235*4882a593Smuzhiyun /* same for SNDRV_RAWMIDI_STREAM_INPUT */ 3236*4882a593Smuzhiyun 3237*4882a593SmuzhiyunRawMIDI Callbacks 3238*4882a593Smuzhiyun----------------- 3239*4882a593Smuzhiyun 3240*4882a593SmuzhiyunIn all the callbacks, the private data that you've set for the rawmidi 3241*4882a593Smuzhiyundevice can be accessed as ``substream->rmidi->private_data``. 3242*4882a593Smuzhiyun 3243*4882a593SmuzhiyunIf there is more than one port, your callbacks can determine the port 3244*4882a593Smuzhiyunindex from the struct snd_rawmidi_substream data passed to each 3245*4882a593Smuzhiyuncallback: 3246*4882a593Smuzhiyun 3247*4882a593Smuzhiyun:: 3248*4882a593Smuzhiyun 3249*4882a593Smuzhiyun struct snd_rawmidi_substream *substream; 3250*4882a593Smuzhiyun int index = substream->number; 3251*4882a593Smuzhiyun 3252*4882a593SmuzhiyunRawMIDI open callback 3253*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~ 3254*4882a593Smuzhiyun 3255*4882a593Smuzhiyun:: 3256*4882a593Smuzhiyun 3257*4882a593Smuzhiyun static int snd_xxx_open(struct snd_rawmidi_substream *substream); 3258*4882a593Smuzhiyun 3259*4882a593Smuzhiyun 3260*4882a593SmuzhiyunThis is called when a substream is opened. You can initialize the 3261*4882a593Smuzhiyunhardware here, but you shouldn't start transmitting/receiving data yet. 3262*4882a593Smuzhiyun 3263*4882a593SmuzhiyunRawMIDI close callback 3264*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~ 3265*4882a593Smuzhiyun 3266*4882a593Smuzhiyun:: 3267*4882a593Smuzhiyun 3268*4882a593Smuzhiyun static int snd_xxx_close(struct snd_rawmidi_substream *substream); 3269*4882a593Smuzhiyun 3270*4882a593SmuzhiyunGuess what. 3271*4882a593Smuzhiyun 3272*4882a593SmuzhiyunThe ``open`` and ``close`` callbacks of a rawmidi device are 3273*4882a593Smuzhiyunserialized with a mutex, and can sleep. 3274*4882a593Smuzhiyun 3275*4882a593SmuzhiyunRawmidi trigger callback for output substreams 3276*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3277*4882a593Smuzhiyun 3278*4882a593Smuzhiyun:: 3279*4882a593Smuzhiyun 3280*4882a593Smuzhiyun static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up); 3281*4882a593Smuzhiyun 3282*4882a593Smuzhiyun 3283*4882a593SmuzhiyunThis is called with a nonzero ``up`` parameter when there is some data 3284*4882a593Smuzhiyunin the substream buffer that must be transmitted. 3285*4882a593Smuzhiyun 3286*4882a593SmuzhiyunTo read data from the buffer, call 3287*4882a593Smuzhiyun:c:func:`snd_rawmidi_transmit_peek()`. It will return the number 3288*4882a593Smuzhiyunof bytes that have been read; this will be less than the number of bytes 3289*4882a593Smuzhiyunrequested when there are no more data in the buffer. After the data have 3290*4882a593Smuzhiyunbeen transmitted successfully, call 3291*4882a593Smuzhiyun:c:func:`snd_rawmidi_transmit_ack()` to remove the data from the 3292*4882a593Smuzhiyunsubstream buffer: 3293*4882a593Smuzhiyun 3294*4882a593Smuzhiyun:: 3295*4882a593Smuzhiyun 3296*4882a593Smuzhiyun unsigned char data; 3297*4882a593Smuzhiyun while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) { 3298*4882a593Smuzhiyun if (snd_mychip_try_to_transmit(data)) 3299*4882a593Smuzhiyun snd_rawmidi_transmit_ack(substream, 1); 3300*4882a593Smuzhiyun else 3301*4882a593Smuzhiyun break; /* hardware FIFO full */ 3302*4882a593Smuzhiyun } 3303*4882a593Smuzhiyun 3304*4882a593SmuzhiyunIf you know beforehand that the hardware will accept data, you can use 3305*4882a593Smuzhiyunthe :c:func:`snd_rawmidi_transmit()` function which reads some 3306*4882a593Smuzhiyundata and removes them from the buffer at once: 3307*4882a593Smuzhiyun 3308*4882a593Smuzhiyun:: 3309*4882a593Smuzhiyun 3310*4882a593Smuzhiyun while (snd_mychip_transmit_possible()) { 3311*4882a593Smuzhiyun unsigned char data; 3312*4882a593Smuzhiyun if (snd_rawmidi_transmit(substream, &data, 1) != 1) 3313*4882a593Smuzhiyun break; /* no more data */ 3314*4882a593Smuzhiyun snd_mychip_transmit(data); 3315*4882a593Smuzhiyun } 3316*4882a593Smuzhiyun 3317*4882a593SmuzhiyunIf you know beforehand how many bytes you can accept, you can use a 3318*4882a593Smuzhiyunbuffer size greater than one with the ``snd_rawmidi_transmit*()`` functions. 3319*4882a593Smuzhiyun 3320*4882a593SmuzhiyunThe ``trigger`` callback must not sleep. If the hardware FIFO is full 3321*4882a593Smuzhiyunbefore the substream buffer has been emptied, you have to continue 3322*4882a593Smuzhiyuntransmitting data later, either in an interrupt handler, or with a 3323*4882a593Smuzhiyuntimer if the hardware doesn't have a MIDI transmit interrupt. 3324*4882a593Smuzhiyun 3325*4882a593SmuzhiyunThe ``trigger`` callback is called with a zero ``up`` parameter when 3326*4882a593Smuzhiyunthe transmission of data should be aborted. 3327*4882a593Smuzhiyun 3328*4882a593SmuzhiyunRawMIDI trigger callback for input substreams 3329*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3330*4882a593Smuzhiyun 3331*4882a593Smuzhiyun:: 3332*4882a593Smuzhiyun 3333*4882a593Smuzhiyun static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up); 3334*4882a593Smuzhiyun 3335*4882a593Smuzhiyun 3336*4882a593SmuzhiyunThis is called with a nonzero ``up`` parameter to enable receiving data, 3337*4882a593Smuzhiyunor with a zero ``up`` parameter do disable receiving data. 3338*4882a593Smuzhiyun 3339*4882a593SmuzhiyunThe ``trigger`` callback must not sleep; the actual reading of data 3340*4882a593Smuzhiyunfrom the device is usually done in an interrupt handler. 3341*4882a593Smuzhiyun 3342*4882a593SmuzhiyunWhen data reception is enabled, your interrupt handler should call 3343*4882a593Smuzhiyun:c:func:`snd_rawmidi_receive()` for all received data: 3344*4882a593Smuzhiyun 3345*4882a593Smuzhiyun:: 3346*4882a593Smuzhiyun 3347*4882a593Smuzhiyun void snd_mychip_midi_interrupt(...) 3348*4882a593Smuzhiyun { 3349*4882a593Smuzhiyun while (mychip_midi_available()) { 3350*4882a593Smuzhiyun unsigned char data; 3351*4882a593Smuzhiyun data = mychip_midi_read(); 3352*4882a593Smuzhiyun snd_rawmidi_receive(substream, &data, 1); 3353*4882a593Smuzhiyun } 3354*4882a593Smuzhiyun } 3355*4882a593Smuzhiyun 3356*4882a593Smuzhiyun 3357*4882a593Smuzhiyundrain callback 3358*4882a593Smuzhiyun~~~~~~~~~~~~~~ 3359*4882a593Smuzhiyun 3360*4882a593Smuzhiyun:: 3361*4882a593Smuzhiyun 3362*4882a593Smuzhiyun static void snd_xxx_drain(struct snd_rawmidi_substream *substream); 3363*4882a593Smuzhiyun 3364*4882a593Smuzhiyun 3365*4882a593SmuzhiyunThis is only used with output substreams. This function should wait 3366*4882a593Smuzhiyununtil all data read from the substream buffer have been transmitted. 3367*4882a593SmuzhiyunThis ensures that the device can be closed and the driver unloaded 3368*4882a593Smuzhiyunwithout losing data. 3369*4882a593Smuzhiyun 3370*4882a593SmuzhiyunThis callback is optional. If you do not set ``drain`` in the struct 3371*4882a593Smuzhiyunsnd_rawmidi_ops structure, ALSA will simply wait for 50 milliseconds 3372*4882a593Smuzhiyuninstead. 3373*4882a593Smuzhiyun 3374*4882a593SmuzhiyunMiscellaneous Devices 3375*4882a593Smuzhiyun===================== 3376*4882a593Smuzhiyun 3377*4882a593SmuzhiyunFM OPL3 3378*4882a593Smuzhiyun------- 3379*4882a593Smuzhiyun 3380*4882a593SmuzhiyunThe FM OPL3 is still used in many chips (mainly for backward 3381*4882a593Smuzhiyuncompatibility). ALSA has a nice OPL3 FM control layer, too. The OPL3 API 3382*4882a593Smuzhiyunis defined in ``<sound/opl3.h>``. 3383*4882a593Smuzhiyun 3384*4882a593SmuzhiyunFM registers can be directly accessed through the direct-FM API, defined 3385*4882a593Smuzhiyunin ``<sound/asound_fm.h>``. In ALSA native mode, FM registers are 3386*4882a593Smuzhiyunaccessed through the Hardware-Dependent Device direct-FM extension API, 3387*4882a593Smuzhiyunwhereas in OSS compatible mode, FM registers can be accessed with the 3388*4882a593SmuzhiyunOSS direct-FM compatible API in ``/dev/dmfmX`` device. 3389*4882a593Smuzhiyun 3390*4882a593SmuzhiyunTo create the OPL3 component, you have two functions to call. The first 3391*4882a593Smuzhiyunone is a constructor for the ``opl3_t`` instance. 3392*4882a593Smuzhiyun 3393*4882a593Smuzhiyun:: 3394*4882a593Smuzhiyun 3395*4882a593Smuzhiyun struct snd_opl3 *opl3; 3396*4882a593Smuzhiyun snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX, 3397*4882a593Smuzhiyun integrated, &opl3); 3398*4882a593Smuzhiyun 3399*4882a593SmuzhiyunThe first argument is the card pointer, the second one is the left port 3400*4882a593Smuzhiyunaddress, and the third is the right port address. In most cases, the 3401*4882a593Smuzhiyunright port is placed at the left port + 2. 3402*4882a593Smuzhiyun 3403*4882a593SmuzhiyunThe fourth argument is the hardware type. 3404*4882a593Smuzhiyun 3405*4882a593SmuzhiyunWhen the left and right ports have been already allocated by the card 3406*4882a593Smuzhiyundriver, pass non-zero to the fifth argument (``integrated``). Otherwise, 3407*4882a593Smuzhiyunthe opl3 module will allocate the specified ports by itself. 3408*4882a593Smuzhiyun 3409*4882a593SmuzhiyunWhen the accessing the hardware requires special method instead of the 3410*4882a593Smuzhiyunstandard I/O access, you can create opl3 instance separately with 3411*4882a593Smuzhiyun:c:func:`snd_opl3_new()`. 3412*4882a593Smuzhiyun 3413*4882a593Smuzhiyun:: 3414*4882a593Smuzhiyun 3415*4882a593Smuzhiyun struct snd_opl3 *opl3; 3416*4882a593Smuzhiyun snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3); 3417*4882a593Smuzhiyun 3418*4882a593SmuzhiyunThen set ``command``, ``private_data`` and ``private_free`` for the 3419*4882a593Smuzhiyunprivate access function, the private data and the destructor. The 3420*4882a593Smuzhiyun``l_port`` and ``r_port`` are not necessarily set. Only the command 3421*4882a593Smuzhiyunmust be set properly. You can retrieve the data from the 3422*4882a593Smuzhiyun``opl3->private_data`` field. 3423*4882a593Smuzhiyun 3424*4882a593SmuzhiyunAfter creating the opl3 instance via :c:func:`snd_opl3_new()`, 3425*4882a593Smuzhiyuncall :c:func:`snd_opl3_init()` to initialize the chip to the 3426*4882a593Smuzhiyunproper state. Note that :c:func:`snd_opl3_create()` always calls 3427*4882a593Smuzhiyunit internally. 3428*4882a593Smuzhiyun 3429*4882a593SmuzhiyunIf the opl3 instance is created successfully, then create a hwdep device 3430*4882a593Smuzhiyunfor this opl3. 3431*4882a593Smuzhiyun 3432*4882a593Smuzhiyun:: 3433*4882a593Smuzhiyun 3434*4882a593Smuzhiyun struct snd_hwdep *opl3hwdep; 3435*4882a593Smuzhiyun snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep); 3436*4882a593Smuzhiyun 3437*4882a593SmuzhiyunThe first argument is the ``opl3_t`` instance you created, and the 3438*4882a593Smuzhiyunsecond is the index number, usually 0. 3439*4882a593Smuzhiyun 3440*4882a593SmuzhiyunThe third argument is the index-offset for the sequencer client assigned 3441*4882a593Smuzhiyunto the OPL3 port. When there is an MPU401-UART, give 1 for here (UART 3442*4882a593Smuzhiyunalways takes 0). 3443*4882a593Smuzhiyun 3444*4882a593SmuzhiyunHardware-Dependent Devices 3445*4882a593Smuzhiyun-------------------------- 3446*4882a593Smuzhiyun 3447*4882a593SmuzhiyunSome chips need user-space access for special controls or for loading 3448*4882a593Smuzhiyunthe micro code. In such a case, you can create a hwdep 3449*4882a593Smuzhiyun(hardware-dependent) device. The hwdep API is defined in 3450*4882a593Smuzhiyun``<sound/hwdep.h>``. You can find examples in opl3 driver or 3451*4882a593Smuzhiyun``isa/sb/sb16_csp.c``. 3452*4882a593Smuzhiyun 3453*4882a593SmuzhiyunThe creation of the ``hwdep`` instance is done via 3454*4882a593Smuzhiyun:c:func:`snd_hwdep_new()`. 3455*4882a593Smuzhiyun 3456*4882a593Smuzhiyun:: 3457*4882a593Smuzhiyun 3458*4882a593Smuzhiyun struct snd_hwdep *hw; 3459*4882a593Smuzhiyun snd_hwdep_new(card, "My HWDEP", 0, &hw); 3460*4882a593Smuzhiyun 3461*4882a593Smuzhiyunwhere the third argument is the index number. 3462*4882a593Smuzhiyun 3463*4882a593SmuzhiyunYou can then pass any pointer value to the ``private_data``. If you 3464*4882a593Smuzhiyunassign a private data, you should define the destructor, too. The 3465*4882a593Smuzhiyundestructor function is set in the ``private_free`` field. 3466*4882a593Smuzhiyun 3467*4882a593Smuzhiyun:: 3468*4882a593Smuzhiyun 3469*4882a593Smuzhiyun struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL); 3470*4882a593Smuzhiyun hw->private_data = p; 3471*4882a593Smuzhiyun hw->private_free = mydata_free; 3472*4882a593Smuzhiyun 3473*4882a593Smuzhiyunand the implementation of the destructor would be: 3474*4882a593Smuzhiyun 3475*4882a593Smuzhiyun:: 3476*4882a593Smuzhiyun 3477*4882a593Smuzhiyun static void mydata_free(struct snd_hwdep *hw) 3478*4882a593Smuzhiyun { 3479*4882a593Smuzhiyun struct mydata *p = hw->private_data; 3480*4882a593Smuzhiyun kfree(p); 3481*4882a593Smuzhiyun } 3482*4882a593Smuzhiyun 3483*4882a593SmuzhiyunThe arbitrary file operations can be defined for this instance. The file 3484*4882a593Smuzhiyunoperators are defined in the ``ops`` table. For example, assume that 3485*4882a593Smuzhiyunthis chip needs an ioctl. 3486*4882a593Smuzhiyun 3487*4882a593Smuzhiyun:: 3488*4882a593Smuzhiyun 3489*4882a593Smuzhiyun hw->ops.open = mydata_open; 3490*4882a593Smuzhiyun hw->ops.ioctl = mydata_ioctl; 3491*4882a593Smuzhiyun hw->ops.release = mydata_release; 3492*4882a593Smuzhiyun 3493*4882a593SmuzhiyunAnd implement the callback functions as you like. 3494*4882a593Smuzhiyun 3495*4882a593SmuzhiyunIEC958 (S/PDIF) 3496*4882a593Smuzhiyun--------------- 3497*4882a593Smuzhiyun 3498*4882a593SmuzhiyunUsually the controls for IEC958 devices are implemented via the control 3499*4882a593Smuzhiyuninterface. There is a macro to compose a name string for IEC958 3500*4882a593Smuzhiyuncontrols, :c:func:`SNDRV_CTL_NAME_IEC958()` defined in 3501*4882a593Smuzhiyun``<include/asound.h>``. 3502*4882a593Smuzhiyun 3503*4882a593SmuzhiyunThere are some standard controls for IEC958 status bits. These controls 3504*4882a593Smuzhiyunuse the type ``SNDRV_CTL_ELEM_TYPE_IEC958``, and the size of element is 3505*4882a593Smuzhiyunfixed as 4 bytes array (value.iec958.status[x]). For the ``info`` 3506*4882a593Smuzhiyuncallback, you don't specify the value field for this type (the count 3507*4882a593Smuzhiyunfield must be set, though). 3508*4882a593Smuzhiyun 3509*4882a593Smuzhiyun“IEC958 Playback Con Mask” is used to return the bit-mask for the IEC958 3510*4882a593Smuzhiyunstatus bits of consumer mode. Similarly, “IEC958 Playback Pro Mask” 3511*4882a593Smuzhiyunreturns the bitmask for professional mode. They are read-only controls. 3512*4882a593Smuzhiyun 3513*4882a593SmuzhiyunMeanwhile, “IEC958 Playback Default” control is defined for getting and 3514*4882a593Smuzhiyunsetting the current default IEC958 bits. 3515*4882a593Smuzhiyun 3516*4882a593SmuzhiyunDue to historical reasons, both variants of the Playback Mask and the 3517*4882a593SmuzhiyunPlayback Default controls can be implemented on either a 3518*4882a593Smuzhiyun``SNDRV_CTL_ELEM_IFACE_PCM`` or a ``SNDRV_CTL_ELEM_IFACE_MIXER`` iface. 3519*4882a593SmuzhiyunDrivers should expose the mask and default on the same iface though. 3520*4882a593Smuzhiyun 3521*4882a593SmuzhiyunIn addition, you can define the control switches to enable/disable or to 3522*4882a593Smuzhiyunset the raw bit mode. The implementation will depend on the chip, but 3523*4882a593Smuzhiyunthe control should be named as “IEC958 xxx”, preferably using the 3524*4882a593Smuzhiyun:c:func:`SNDRV_CTL_NAME_IEC958()` macro. 3525*4882a593Smuzhiyun 3526*4882a593SmuzhiyunYou can find several cases, for example, ``pci/emu10k1``, 3527*4882a593Smuzhiyun``pci/ice1712``, or ``pci/cmipci.c``. 3528*4882a593Smuzhiyun 3529*4882a593SmuzhiyunBuffer and Memory Management 3530*4882a593Smuzhiyun============================ 3531*4882a593Smuzhiyun 3532*4882a593SmuzhiyunBuffer Types 3533*4882a593Smuzhiyun------------ 3534*4882a593Smuzhiyun 3535*4882a593SmuzhiyunALSA provides several different buffer allocation functions depending on 3536*4882a593Smuzhiyunthe bus and the architecture. All these have a consistent API. The 3537*4882a593Smuzhiyunallocation of physically-contiguous pages is done via 3538*4882a593Smuzhiyun:c:func:`snd_malloc_xxx_pages()` function, where xxx is the bus 3539*4882a593Smuzhiyuntype. 3540*4882a593Smuzhiyun 3541*4882a593SmuzhiyunThe allocation of pages with fallback is 3542*4882a593Smuzhiyun:c:func:`snd_malloc_xxx_pages_fallback()`. This function tries 3543*4882a593Smuzhiyunto allocate the specified pages but if the pages are not available, it 3544*4882a593Smuzhiyuntries to reduce the page sizes until enough space is found. 3545*4882a593Smuzhiyun 3546*4882a593SmuzhiyunThe release the pages, call :c:func:`snd_free_xxx_pages()` 3547*4882a593Smuzhiyunfunction. 3548*4882a593Smuzhiyun 3549*4882a593SmuzhiyunUsually, ALSA drivers try to allocate and reserve a large contiguous 3550*4882a593Smuzhiyunphysical space at the time the module is loaded for the later use. This 3551*4882a593Smuzhiyunis called “pre-allocation”. As already written, you can call the 3552*4882a593Smuzhiyunfollowing function at pcm instance construction time (in the case of PCI 3553*4882a593Smuzhiyunbus). 3554*4882a593Smuzhiyun 3555*4882a593Smuzhiyun:: 3556*4882a593Smuzhiyun 3557*4882a593Smuzhiyun snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, 3558*4882a593Smuzhiyun &pci->dev, size, max); 3559*4882a593Smuzhiyun 3560*4882a593Smuzhiyunwhere ``size`` is the byte size to be pre-allocated and the ``max`` is 3561*4882a593Smuzhiyunthe maximum size to be changed via the ``prealloc`` proc file. The 3562*4882a593Smuzhiyunallocator will try to get an area as large as possible within the 3563*4882a593Smuzhiyungiven size. 3564*4882a593Smuzhiyun 3565*4882a593SmuzhiyunThe second argument (type) and the third argument (device pointer) are 3566*4882a593Smuzhiyundependent on the bus. For normal devices, pass the device pointer 3567*4882a593Smuzhiyun(typically identical as ``card->dev``) to the third argument with 3568*4882a593Smuzhiyun``SNDRV_DMA_TYPE_DEV`` type. For the continuous buffer unrelated to the 3569*4882a593Smuzhiyunbus can be pre-allocated with ``SNDRV_DMA_TYPE_CONTINUOUS`` type. 3570*4882a593SmuzhiyunYou can pass NULL to the device pointer in that case, which is the 3571*4882a593Smuzhiyundefault mode implying to allocate with ``GFP_KERNEL`` flag. 3572*4882a593SmuzhiyunIf you need a different GFP flag, you can pass it by encoding the flag 3573*4882a593Smuzhiyuninto the device pointer via a special macro 3574*4882a593Smuzhiyun:c:func:`snd_dma_continuous_data()`. 3575*4882a593SmuzhiyunFor the scatter-gather buffers, use ``SNDRV_DMA_TYPE_DEV_SG`` with the 3576*4882a593Smuzhiyundevice pointer (see the `Non-Contiguous Buffers`_ section). 3577*4882a593Smuzhiyun 3578*4882a593SmuzhiyunOnce the buffer is pre-allocated, you can use the allocator in the 3579*4882a593Smuzhiyun``hw_params`` callback: 3580*4882a593Smuzhiyun 3581*4882a593Smuzhiyun:: 3582*4882a593Smuzhiyun 3583*4882a593Smuzhiyun snd_pcm_lib_malloc_pages(substream, size); 3584*4882a593Smuzhiyun 3585*4882a593SmuzhiyunNote that you have to pre-allocate to use this function. 3586*4882a593Smuzhiyun 3587*4882a593SmuzhiyunMost of drivers use, though, rather the newly introduced "managed 3588*4882a593Smuzhiyunbuffer allocation mode" instead of the manual allocation or release. 3589*4882a593SmuzhiyunThis is done by calling :c:func:`snd_pcm_set_managed_buffer_all()` 3590*4882a593Smuzhiyuninstead of :c:func:`snd_pcm_lib_preallocate_pages_for_all()`. 3591*4882a593Smuzhiyun 3592*4882a593Smuzhiyun:: 3593*4882a593Smuzhiyun 3594*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, 3595*4882a593Smuzhiyun &pci->dev, size, max); 3596*4882a593Smuzhiyun 3597*4882a593Smuzhiyunwhere passed arguments are identical in both functions. 3598*4882a593SmuzhiyunThe difference in the managed mode is that PCM core will call 3599*4882a593Smuzhiyun:c:func:`snd_pcm_lib_malloc_pages()` internally already before calling 3600*4882a593Smuzhiyunthe PCM ``hw_params`` callback, and call :c:func:`snd_pcm_lib_free_pages()` 3601*4882a593Smuzhiyunafter the PCM ``hw_free`` callback automatically. So the driver 3602*4882a593Smuzhiyundoesn't have to call these functions explicitly in its callback any 3603*4882a593Smuzhiyunlonger. This made many driver code having NULL ``hw_params`` and 3604*4882a593Smuzhiyun``hw_free`` entries. 3605*4882a593Smuzhiyun 3606*4882a593SmuzhiyunExternal Hardware Buffers 3607*4882a593Smuzhiyun------------------------- 3608*4882a593Smuzhiyun 3609*4882a593SmuzhiyunSome chips have their own hardware buffers and the DMA transfer from the 3610*4882a593Smuzhiyunhost memory is not available. In such a case, you need to either 1) 3611*4882a593Smuzhiyuncopy/set the audio data directly to the external hardware buffer, or 2) 3612*4882a593Smuzhiyunmake an intermediate buffer and copy/set the data from it to the 3613*4882a593Smuzhiyunexternal hardware buffer in interrupts (or in tasklets, preferably). 3614*4882a593Smuzhiyun 3615*4882a593SmuzhiyunThe first case works fine if the external hardware buffer is large 3616*4882a593Smuzhiyunenough. This method doesn't need any extra buffers and thus is more 3617*4882a593Smuzhiyuneffective. You need to define the ``copy_user`` and ``copy_kernel`` 3618*4882a593Smuzhiyuncallbacks for the data transfer, in addition to ``fill_silence`` 3619*4882a593Smuzhiyuncallback for playback. However, there is a drawback: it cannot be 3620*4882a593Smuzhiyunmmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM. 3621*4882a593Smuzhiyun 3622*4882a593SmuzhiyunThe second case allows for mmap on the buffer, although you have to 3623*4882a593Smuzhiyunhandle an interrupt or a tasklet to transfer the data from the 3624*4882a593Smuzhiyunintermediate buffer to the hardware buffer. You can find an example in 3625*4882a593Smuzhiyunthe vxpocket driver. 3626*4882a593Smuzhiyun 3627*4882a593SmuzhiyunAnother case is when the chip uses a PCI memory-map region for the 3628*4882a593Smuzhiyunbuffer instead of the host memory. In this case, mmap is available only 3629*4882a593Smuzhiyunon certain architectures like the Intel one. In non-mmap mode, the data 3630*4882a593Smuzhiyuncannot be transferred as in the normal way. Thus you need to define the 3631*4882a593Smuzhiyun``copy_user``, ``copy_kernel`` and ``fill_silence`` callbacks as well, 3632*4882a593Smuzhiyunas in the cases above. The examples are found in ``rme32.c`` and 3633*4882a593Smuzhiyun``rme96.c``. 3634*4882a593Smuzhiyun 3635*4882a593SmuzhiyunThe implementation of the ``copy_user``, ``copy_kernel`` and 3636*4882a593Smuzhiyun``silence`` callbacks depends upon whether the hardware supports 3637*4882a593Smuzhiyuninterleaved or non-interleaved samples. The ``copy_user`` callback is 3638*4882a593Smuzhiyundefined like below, a bit differently depending whether the direction 3639*4882a593Smuzhiyunis playback or capture: 3640*4882a593Smuzhiyun 3641*4882a593Smuzhiyun:: 3642*4882a593Smuzhiyun 3643*4882a593Smuzhiyun static int playback_copy_user(struct snd_pcm_substream *substream, 3644*4882a593Smuzhiyun int channel, unsigned long pos, 3645*4882a593Smuzhiyun void __user *src, unsigned long count); 3646*4882a593Smuzhiyun static int capture_copy_user(struct snd_pcm_substream *substream, 3647*4882a593Smuzhiyun int channel, unsigned long pos, 3648*4882a593Smuzhiyun void __user *dst, unsigned long count); 3649*4882a593Smuzhiyun 3650*4882a593SmuzhiyunIn the case of interleaved samples, the second argument (``channel``) is 3651*4882a593Smuzhiyunnot used. The third argument (``pos``) points the current position 3652*4882a593Smuzhiyunoffset in bytes. 3653*4882a593Smuzhiyun 3654*4882a593SmuzhiyunThe meaning of the fourth argument is different between playback and 3655*4882a593Smuzhiyuncapture. For playback, it holds the source data pointer, and for 3656*4882a593Smuzhiyuncapture, it's the destination data pointer. 3657*4882a593Smuzhiyun 3658*4882a593SmuzhiyunThe last argument is the number of bytes to be copied. 3659*4882a593Smuzhiyun 3660*4882a593SmuzhiyunWhat you have to do in this callback is again different between playback 3661*4882a593Smuzhiyunand capture directions. In the playback case, you copy the given amount 3662*4882a593Smuzhiyunof data (``count``) at the specified pointer (``src``) to the specified 3663*4882a593Smuzhiyunoffset (``pos``) on the hardware buffer. When coded like memcpy-like 3664*4882a593Smuzhiyunway, the copy would be like: 3665*4882a593Smuzhiyun 3666*4882a593Smuzhiyun:: 3667*4882a593Smuzhiyun 3668*4882a593Smuzhiyun my_memcpy_from_user(my_buffer + pos, src, count); 3669*4882a593Smuzhiyun 3670*4882a593SmuzhiyunFor the capture direction, you copy the given amount of data (``count``) 3671*4882a593Smuzhiyunat the specified offset (``pos``) on the hardware buffer to the 3672*4882a593Smuzhiyunspecified pointer (``dst``). 3673*4882a593Smuzhiyun 3674*4882a593Smuzhiyun:: 3675*4882a593Smuzhiyun 3676*4882a593Smuzhiyun my_memcpy_to_user(dst, my_buffer + pos, count); 3677*4882a593Smuzhiyun 3678*4882a593SmuzhiyunHere the functions are named as ``from_user`` and ``to_user`` because 3679*4882a593Smuzhiyunit's the user-space buffer that is passed to these callbacks. That 3680*4882a593Smuzhiyunis, the callback is supposed to copy from/to the user-space data 3681*4882a593Smuzhiyundirectly to/from the hardware buffer. 3682*4882a593Smuzhiyun 3683*4882a593SmuzhiyunCareful readers might notice that these callbacks receive the 3684*4882a593Smuzhiyunarguments in bytes, not in frames like other callbacks. It's because 3685*4882a593Smuzhiyunit would make coding easier like the examples above, and also it makes 3686*4882a593Smuzhiyuneasier to unify both the interleaved and non-interleaved cases, as 3687*4882a593Smuzhiyunexplained in the following. 3688*4882a593Smuzhiyun 3689*4882a593SmuzhiyunIn the case of non-interleaved samples, the implementation will be a bit 3690*4882a593Smuzhiyunmore complicated. The callback is called for each channel, passed by 3691*4882a593Smuzhiyunthe second argument, so totally it's called for N-channels times per 3692*4882a593Smuzhiyuntransfer. 3693*4882a593Smuzhiyun 3694*4882a593SmuzhiyunThe meaning of other arguments are almost same as the interleaved 3695*4882a593Smuzhiyuncase. The callback is supposed to copy the data from/to the given 3696*4882a593Smuzhiyunuser-space buffer, but only for the given channel. For the detailed 3697*4882a593Smuzhiyunimplementations, please check ``isa/gus/gus_pcm.c`` or 3698*4882a593Smuzhiyun"pci/rme9652/rme9652.c" as examples. 3699*4882a593Smuzhiyun 3700*4882a593SmuzhiyunThe above callbacks are the copy from/to the user-space buffer. There 3701*4882a593Smuzhiyunare some cases where we want copy from/to the kernel-space buffer 3702*4882a593Smuzhiyuninstead. In such a case, ``copy_kernel`` callback is called. It'd 3703*4882a593Smuzhiyunlook like: 3704*4882a593Smuzhiyun 3705*4882a593Smuzhiyun:: 3706*4882a593Smuzhiyun 3707*4882a593Smuzhiyun static int playback_copy_kernel(struct snd_pcm_substream *substream, 3708*4882a593Smuzhiyun int channel, unsigned long pos, 3709*4882a593Smuzhiyun void *src, unsigned long count); 3710*4882a593Smuzhiyun static int capture_copy_kernel(struct snd_pcm_substream *substream, 3711*4882a593Smuzhiyun int channel, unsigned long pos, 3712*4882a593Smuzhiyun void *dst, unsigned long count); 3713*4882a593Smuzhiyun 3714*4882a593SmuzhiyunAs found easily, the only difference is that the buffer pointer is 3715*4882a593Smuzhiyunwithout ``__user`` prefix; that is, a kernel-buffer pointer is passed 3716*4882a593Smuzhiyunin the fourth argument. Correspondingly, the implementation would be 3717*4882a593Smuzhiyuna version without the user-copy, such as: 3718*4882a593Smuzhiyun 3719*4882a593Smuzhiyun:: 3720*4882a593Smuzhiyun 3721*4882a593Smuzhiyun my_memcpy(my_buffer + pos, src, count); 3722*4882a593Smuzhiyun 3723*4882a593SmuzhiyunUsually for the playback, another callback ``fill_silence`` is 3724*4882a593Smuzhiyundefined. It's implemented in a similar way as the copy callbacks 3725*4882a593Smuzhiyunabove: 3726*4882a593Smuzhiyun 3727*4882a593Smuzhiyun:: 3728*4882a593Smuzhiyun 3729*4882a593Smuzhiyun static int silence(struct snd_pcm_substream *substream, int channel, 3730*4882a593Smuzhiyun unsigned long pos, unsigned long count); 3731*4882a593Smuzhiyun 3732*4882a593SmuzhiyunThe meanings of arguments are the same as in the ``copy_user`` and 3733*4882a593Smuzhiyun``copy_kernel`` callbacks, although there is no buffer pointer 3734*4882a593Smuzhiyunargument. In the case of interleaved samples, the channel argument has 3735*4882a593Smuzhiyunno meaning, as well as on ``copy_*`` callbacks. 3736*4882a593Smuzhiyun 3737*4882a593SmuzhiyunThe role of ``fill_silence`` callback is to set the given amount 3738*4882a593Smuzhiyun(``count``) of silence data at the specified offset (``pos``) on the 3739*4882a593Smuzhiyunhardware buffer. Suppose that the data format is signed (that is, the 3740*4882a593Smuzhiyunsilent-data is 0), and the implementation using a memset-like function 3741*4882a593Smuzhiyunwould be like: 3742*4882a593Smuzhiyun 3743*4882a593Smuzhiyun:: 3744*4882a593Smuzhiyun 3745*4882a593Smuzhiyun my_memset(my_buffer + pos, 0, count); 3746*4882a593Smuzhiyun 3747*4882a593SmuzhiyunIn the case of non-interleaved samples, again, the implementation 3748*4882a593Smuzhiyunbecomes a bit more complicated, as it's called N-times per transfer 3749*4882a593Smuzhiyunfor each channel. See, for example, ``isa/gus/gus_pcm.c``. 3750*4882a593Smuzhiyun 3751*4882a593SmuzhiyunNon-Contiguous Buffers 3752*4882a593Smuzhiyun---------------------- 3753*4882a593Smuzhiyun 3754*4882a593SmuzhiyunIf your hardware supports the page table as in emu10k1 or the buffer 3755*4882a593Smuzhiyundescriptors as in via82xx, you can use the scatter-gather (SG) DMA. ALSA 3756*4882a593Smuzhiyunprovides an interface for handling SG-buffers. The API is provided in 3757*4882a593Smuzhiyun``<sound/pcm.h>``. 3758*4882a593Smuzhiyun 3759*4882a593SmuzhiyunFor creating the SG-buffer handler, call 3760*4882a593Smuzhiyun:c:func:`snd_pcm_set_managed_buffer()` or 3761*4882a593Smuzhiyun:c:func:`snd_pcm_set_managed_buffer_all()` with 3762*4882a593Smuzhiyun``SNDRV_DMA_TYPE_DEV_SG`` in the PCM constructor like other PCI 3763*4882a593Smuzhiyunpre-allocator. You need to pass ``&pci->dev``, where pci is 3764*4882a593Smuzhiyunthe struct pci_dev pointer of the chip as 3765*4882a593Smuzhiyunwell. 3766*4882a593Smuzhiyun 3767*4882a593Smuzhiyun:: 3768*4882a593Smuzhiyun 3769*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG, 3770*4882a593Smuzhiyun &pci->dev, size, max); 3771*4882a593Smuzhiyun 3772*4882a593SmuzhiyunThe ``struct snd_sg_buf`` instance is created as 3773*4882a593Smuzhiyun``substream->dma_private`` in turn. You can cast the pointer like: 3774*4882a593Smuzhiyun 3775*4882a593Smuzhiyun:: 3776*4882a593Smuzhiyun 3777*4882a593Smuzhiyun struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private; 3778*4882a593Smuzhiyun 3779*4882a593SmuzhiyunThen in :c:func:`snd_pcm_lib_malloc_pages()` call, the common SG-buffer 3780*4882a593Smuzhiyunhandler will allocate the non-contiguous kernel pages of the given size 3781*4882a593Smuzhiyunand map them onto the virtually contiguous memory. The virtual pointer 3782*4882a593Smuzhiyunis addressed in runtime->dma_area. The physical address 3783*4882a593Smuzhiyun(``runtime->dma_addr``) is set to zero, because the buffer is 3784*4882a593Smuzhiyunphysically non-contiguous. The physical address table is set up in 3785*4882a593Smuzhiyun``sgbuf->table``. You can get the physical address at a certain offset 3786*4882a593Smuzhiyunvia :c:func:`snd_pcm_sgbuf_get_addr()`. 3787*4882a593Smuzhiyun 3788*4882a593SmuzhiyunIf you need to release the SG-buffer data explicitly, call the 3789*4882a593Smuzhiyunstandard API function :c:func:`snd_pcm_lib_free_pages()` as usual. 3790*4882a593Smuzhiyun 3791*4882a593SmuzhiyunVmalloc'ed Buffers 3792*4882a593Smuzhiyun------------------ 3793*4882a593Smuzhiyun 3794*4882a593SmuzhiyunIt's possible to use a buffer allocated via :c:func:`vmalloc()`, for 3795*4882a593Smuzhiyunexample, for an intermediate buffer. In the recent version of kernel, 3796*4882a593Smuzhiyunyou can simply allocate it via standard 3797*4882a593Smuzhiyun:c:func:`snd_pcm_lib_malloc_pages()` and co after setting up the 3798*4882a593Smuzhiyunbuffer preallocation with ``SNDRV_DMA_TYPE_VMALLOC`` type. 3799*4882a593Smuzhiyun 3800*4882a593Smuzhiyun:: 3801*4882a593Smuzhiyun 3802*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, 3803*4882a593Smuzhiyun NULL, 0, 0); 3804*4882a593Smuzhiyun 3805*4882a593SmuzhiyunThe NULL is passed to the device pointer argument, which indicates 3806*4882a593Smuzhiyunthat the default pages (GFP_KERNEL and GFP_HIGHMEM) will be 3807*4882a593Smuzhiyunallocated. 3808*4882a593Smuzhiyun 3809*4882a593SmuzhiyunAlso, note that zero is passed to both the size and the max size 3810*4882a593Smuzhiyunarguments here. Since each vmalloc call should succeed at any time, 3811*4882a593Smuzhiyunwe don't need to pre-allocate the buffers like other continuous 3812*4882a593Smuzhiyunpages. 3813*4882a593Smuzhiyun 3814*4882a593SmuzhiyunIf you need the 32bit DMA allocation, pass the device pointer encoded 3815*4882a593Smuzhiyunby :c:func:`snd_dma_continuous_data()` with ``GFP_KERNEL|__GFP_DMA32`` 3816*4882a593Smuzhiyunargument. 3817*4882a593Smuzhiyun 3818*4882a593Smuzhiyun:: 3819*4882a593Smuzhiyun 3820*4882a593Smuzhiyun snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, 3821*4882a593Smuzhiyun snd_dma_continuous_data(GFP_KERNEL | __GFP_DMA32), 0, 0); 3822*4882a593Smuzhiyun 3823*4882a593SmuzhiyunProc Interface 3824*4882a593Smuzhiyun============== 3825*4882a593Smuzhiyun 3826*4882a593SmuzhiyunALSA provides an easy interface for procfs. The proc files are very 3827*4882a593Smuzhiyunuseful for debugging. I recommend you set up proc files if you write a 3828*4882a593Smuzhiyundriver and want to get a running status or register dumps. The API is 3829*4882a593Smuzhiyunfound in ``<sound/info.h>``. 3830*4882a593Smuzhiyun 3831*4882a593SmuzhiyunTo create a proc file, call :c:func:`snd_card_proc_new()`. 3832*4882a593Smuzhiyun 3833*4882a593Smuzhiyun:: 3834*4882a593Smuzhiyun 3835*4882a593Smuzhiyun struct snd_info_entry *entry; 3836*4882a593Smuzhiyun int err = snd_card_proc_new(card, "my-file", &entry); 3837*4882a593Smuzhiyun 3838*4882a593Smuzhiyunwhere the second argument specifies the name of the proc file to be 3839*4882a593Smuzhiyuncreated. The above example will create a file ``my-file`` under the 3840*4882a593Smuzhiyuncard directory, e.g. ``/proc/asound/card0/my-file``. 3841*4882a593Smuzhiyun 3842*4882a593SmuzhiyunLike other components, the proc entry created via 3843*4882a593Smuzhiyun:c:func:`snd_card_proc_new()` will be registered and released 3844*4882a593Smuzhiyunautomatically in the card registration and release functions. 3845*4882a593Smuzhiyun 3846*4882a593SmuzhiyunWhen the creation is successful, the function stores a new instance in 3847*4882a593Smuzhiyunthe pointer given in the third argument. It is initialized as a text 3848*4882a593Smuzhiyunproc file for read only. To use this proc file as a read-only text file 3849*4882a593Smuzhiyunas it is, set the read callback with a private data via 3850*4882a593Smuzhiyun:c:func:`snd_info_set_text_ops()`. 3851*4882a593Smuzhiyun 3852*4882a593Smuzhiyun:: 3853*4882a593Smuzhiyun 3854*4882a593Smuzhiyun snd_info_set_text_ops(entry, chip, my_proc_read); 3855*4882a593Smuzhiyun 3856*4882a593Smuzhiyunwhere the second argument (``chip``) is the private data to be used in 3857*4882a593Smuzhiyunthe callbacks. The third parameter specifies the read buffer size and 3858*4882a593Smuzhiyunthe fourth (``my_proc_read``) is the callback function, which is 3859*4882a593Smuzhiyundefined like 3860*4882a593Smuzhiyun 3861*4882a593Smuzhiyun:: 3862*4882a593Smuzhiyun 3863*4882a593Smuzhiyun static void my_proc_read(struct snd_info_entry *entry, 3864*4882a593Smuzhiyun struct snd_info_buffer *buffer); 3865*4882a593Smuzhiyun 3866*4882a593SmuzhiyunIn the read callback, use :c:func:`snd_iprintf()` for output 3867*4882a593Smuzhiyunstrings, which works just like normal :c:func:`printf()`. For 3868*4882a593Smuzhiyunexample, 3869*4882a593Smuzhiyun 3870*4882a593Smuzhiyun:: 3871*4882a593Smuzhiyun 3872*4882a593Smuzhiyun static void my_proc_read(struct snd_info_entry *entry, 3873*4882a593Smuzhiyun struct snd_info_buffer *buffer) 3874*4882a593Smuzhiyun { 3875*4882a593Smuzhiyun struct my_chip *chip = entry->private_data; 3876*4882a593Smuzhiyun 3877*4882a593Smuzhiyun snd_iprintf(buffer, "This is my chip!\n"); 3878*4882a593Smuzhiyun snd_iprintf(buffer, "Port = %ld\n", chip->port); 3879*4882a593Smuzhiyun } 3880*4882a593Smuzhiyun 3881*4882a593SmuzhiyunThe file permissions can be changed afterwards. As default, it's set as 3882*4882a593Smuzhiyunread only for all users. If you want to add write permission for the 3883*4882a593Smuzhiyunuser (root as default), do as follows: 3884*4882a593Smuzhiyun 3885*4882a593Smuzhiyun:: 3886*4882a593Smuzhiyun 3887*4882a593Smuzhiyun entry->mode = S_IFREG | S_IRUGO | S_IWUSR; 3888*4882a593Smuzhiyun 3889*4882a593Smuzhiyunand set the write buffer size and the callback 3890*4882a593Smuzhiyun 3891*4882a593Smuzhiyun:: 3892*4882a593Smuzhiyun 3893*4882a593Smuzhiyun entry->c.text.write = my_proc_write; 3894*4882a593Smuzhiyun 3895*4882a593SmuzhiyunFor the write callback, you can use :c:func:`snd_info_get_line()` 3896*4882a593Smuzhiyunto get a text line, and :c:func:`snd_info_get_str()` to retrieve 3897*4882a593Smuzhiyuna string from the line. Some examples are found in 3898*4882a593Smuzhiyun``core/oss/mixer_oss.c``, core/oss/and ``pcm_oss.c``. 3899*4882a593Smuzhiyun 3900*4882a593SmuzhiyunFor a raw-data proc-file, set the attributes as follows: 3901*4882a593Smuzhiyun 3902*4882a593Smuzhiyun:: 3903*4882a593Smuzhiyun 3904*4882a593Smuzhiyun static const struct snd_info_entry_ops my_file_io_ops = { 3905*4882a593Smuzhiyun .read = my_file_io_read, 3906*4882a593Smuzhiyun }; 3907*4882a593Smuzhiyun 3908*4882a593Smuzhiyun entry->content = SNDRV_INFO_CONTENT_DATA; 3909*4882a593Smuzhiyun entry->private_data = chip; 3910*4882a593Smuzhiyun entry->c.ops = &my_file_io_ops; 3911*4882a593Smuzhiyun entry->size = 4096; 3912*4882a593Smuzhiyun entry->mode = S_IFREG | S_IRUGO; 3913*4882a593Smuzhiyun 3914*4882a593SmuzhiyunFor the raw data, ``size`` field must be set properly. This specifies 3915*4882a593Smuzhiyunthe maximum size of the proc file access. 3916*4882a593Smuzhiyun 3917*4882a593SmuzhiyunThe read/write callbacks of raw mode are more direct than the text mode. 3918*4882a593SmuzhiyunYou need to use a low-level I/O functions such as 3919*4882a593Smuzhiyun:c:func:`copy_from_user()` and :c:func:`copy_to_user()` to transfer the data. 3920*4882a593Smuzhiyun 3921*4882a593Smuzhiyun:: 3922*4882a593Smuzhiyun 3923*4882a593Smuzhiyun static ssize_t my_file_io_read(struct snd_info_entry *entry, 3924*4882a593Smuzhiyun void *file_private_data, 3925*4882a593Smuzhiyun struct file *file, 3926*4882a593Smuzhiyun char *buf, 3927*4882a593Smuzhiyun size_t count, 3928*4882a593Smuzhiyun loff_t pos) 3929*4882a593Smuzhiyun { 3930*4882a593Smuzhiyun if (copy_to_user(buf, local_data + pos, count)) 3931*4882a593Smuzhiyun return -EFAULT; 3932*4882a593Smuzhiyun return count; 3933*4882a593Smuzhiyun } 3934*4882a593Smuzhiyun 3935*4882a593SmuzhiyunIf the size of the info entry has been set up properly, ``count`` and 3936*4882a593Smuzhiyun``pos`` are guaranteed to fit within 0 and the given size. You don't 3937*4882a593Smuzhiyunhave to check the range in the callbacks unless any other condition is 3938*4882a593Smuzhiyunrequired. 3939*4882a593Smuzhiyun 3940*4882a593SmuzhiyunPower Management 3941*4882a593Smuzhiyun================ 3942*4882a593Smuzhiyun 3943*4882a593SmuzhiyunIf the chip is supposed to work with suspend/resume functions, you need 3944*4882a593Smuzhiyunto add power-management code to the driver. The additional code for 3945*4882a593Smuzhiyunpower-management should be ifdef-ed with ``CONFIG_PM``, or annotated 3946*4882a593Smuzhiyunwith __maybe_unused attribute; otherwise the compiler will complain 3947*4882a593Smuzhiyunyou. 3948*4882a593Smuzhiyun 3949*4882a593SmuzhiyunIf the driver *fully* supports suspend/resume that is, the device can be 3950*4882a593Smuzhiyunproperly resumed to its state when suspend was called, you can set the 3951*4882a593Smuzhiyun``SNDRV_PCM_INFO_RESUME`` flag in the pcm info field. Usually, this is 3952*4882a593Smuzhiyunpossible when the registers of the chip can be safely saved and restored 3953*4882a593Smuzhiyunto RAM. If this is set, the trigger callback is called with 3954*4882a593Smuzhiyun``SNDRV_PCM_TRIGGER_RESUME`` after the resume callback completes. 3955*4882a593Smuzhiyun 3956*4882a593SmuzhiyunEven if the driver doesn't support PM fully but partial suspend/resume 3957*4882a593Smuzhiyunis still possible, it's still worthy to implement suspend/resume 3958*4882a593Smuzhiyuncallbacks. In such a case, applications would reset the status by 3959*4882a593Smuzhiyuncalling :c:func:`snd_pcm_prepare()` and restart the stream 3960*4882a593Smuzhiyunappropriately. Hence, you can define suspend/resume callbacks below but 3961*4882a593Smuzhiyundon't set ``SNDRV_PCM_INFO_RESUME`` info flag to the PCM. 3962*4882a593Smuzhiyun 3963*4882a593SmuzhiyunNote that the trigger with SUSPEND can always be called when 3964*4882a593Smuzhiyun:c:func:`snd_pcm_suspend_all()` is called, regardless of the 3965*4882a593Smuzhiyun``SNDRV_PCM_INFO_RESUME`` flag. The ``RESUME`` flag affects only the 3966*4882a593Smuzhiyunbehavior of :c:func:`snd_pcm_resume()`. (Thus, in theory, 3967*4882a593Smuzhiyun``SNDRV_PCM_TRIGGER_RESUME`` isn't needed to be handled in the trigger 3968*4882a593Smuzhiyuncallback when no ``SNDRV_PCM_INFO_RESUME`` flag is set. But, it's better 3969*4882a593Smuzhiyunto keep it for compatibility reasons.) 3970*4882a593Smuzhiyun 3971*4882a593SmuzhiyunIn the earlier version of ALSA drivers, a common power-management layer 3972*4882a593Smuzhiyunwas provided, but it has been removed. The driver needs to define the 3973*4882a593Smuzhiyunsuspend/resume hooks according to the bus the device is connected to. In 3974*4882a593Smuzhiyunthe case of PCI drivers, the callbacks look like below: 3975*4882a593Smuzhiyun 3976*4882a593Smuzhiyun:: 3977*4882a593Smuzhiyun 3978*4882a593Smuzhiyun static int __maybe_unused snd_my_suspend(struct device *dev) 3979*4882a593Smuzhiyun { 3980*4882a593Smuzhiyun .... /* do things for suspend */ 3981*4882a593Smuzhiyun return 0; 3982*4882a593Smuzhiyun } 3983*4882a593Smuzhiyun static int __maybe_unused snd_my_resume(struct device *dev) 3984*4882a593Smuzhiyun { 3985*4882a593Smuzhiyun .... /* do things for suspend */ 3986*4882a593Smuzhiyun return 0; 3987*4882a593Smuzhiyun } 3988*4882a593Smuzhiyun 3989*4882a593SmuzhiyunThe scheme of the real suspend job is as follows. 3990*4882a593Smuzhiyun 3991*4882a593Smuzhiyun1. Retrieve the card and the chip data. 3992*4882a593Smuzhiyun 3993*4882a593Smuzhiyun2. Call :c:func:`snd_power_change_state()` with 3994*4882a593Smuzhiyun ``SNDRV_CTL_POWER_D3hot`` to change the power status. 3995*4882a593Smuzhiyun 3996*4882a593Smuzhiyun3. If AC97 codecs are used, call :c:func:`snd_ac97_suspend()` for 3997*4882a593Smuzhiyun each codec. 3998*4882a593Smuzhiyun 3999*4882a593Smuzhiyun4. Save the register values if necessary. 4000*4882a593Smuzhiyun 4001*4882a593Smuzhiyun5. Stop the hardware if necessary. 4002*4882a593Smuzhiyun 4003*4882a593SmuzhiyunA typical code would be like: 4004*4882a593Smuzhiyun 4005*4882a593Smuzhiyun:: 4006*4882a593Smuzhiyun 4007*4882a593Smuzhiyun static int __maybe_unused mychip_suspend(struct device *dev) 4008*4882a593Smuzhiyun { 4009*4882a593Smuzhiyun /* (1) */ 4010*4882a593Smuzhiyun struct snd_card *card = dev_get_drvdata(dev); 4011*4882a593Smuzhiyun struct mychip *chip = card->private_data; 4012*4882a593Smuzhiyun /* (2) */ 4013*4882a593Smuzhiyun snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); 4014*4882a593Smuzhiyun /* (3) */ 4015*4882a593Smuzhiyun snd_ac97_suspend(chip->ac97); 4016*4882a593Smuzhiyun /* (4) */ 4017*4882a593Smuzhiyun snd_mychip_save_registers(chip); 4018*4882a593Smuzhiyun /* (5) */ 4019*4882a593Smuzhiyun snd_mychip_stop_hardware(chip); 4020*4882a593Smuzhiyun return 0; 4021*4882a593Smuzhiyun } 4022*4882a593Smuzhiyun 4023*4882a593Smuzhiyun 4024*4882a593SmuzhiyunThe scheme of the real resume job is as follows. 4025*4882a593Smuzhiyun 4026*4882a593Smuzhiyun1. Retrieve the card and the chip data. 4027*4882a593Smuzhiyun 4028*4882a593Smuzhiyun2. Re-initialize the chip. 4029*4882a593Smuzhiyun 4030*4882a593Smuzhiyun3. Restore the saved registers if necessary. 4031*4882a593Smuzhiyun 4032*4882a593Smuzhiyun4. Resume the mixer, e.g. calling :c:func:`snd_ac97_resume()`. 4033*4882a593Smuzhiyun 4034*4882a593Smuzhiyun5. Restart the hardware (if any). 4035*4882a593Smuzhiyun 4036*4882a593Smuzhiyun6. Call :c:func:`snd_power_change_state()` with 4037*4882a593Smuzhiyun ``SNDRV_CTL_POWER_D0`` to notify the processes. 4038*4882a593Smuzhiyun 4039*4882a593SmuzhiyunA typical code would be like: 4040*4882a593Smuzhiyun 4041*4882a593Smuzhiyun:: 4042*4882a593Smuzhiyun 4043*4882a593Smuzhiyun static int __maybe_unused mychip_resume(struct pci_dev *pci) 4044*4882a593Smuzhiyun { 4045*4882a593Smuzhiyun /* (1) */ 4046*4882a593Smuzhiyun struct snd_card *card = dev_get_drvdata(dev); 4047*4882a593Smuzhiyun struct mychip *chip = card->private_data; 4048*4882a593Smuzhiyun /* (2) */ 4049*4882a593Smuzhiyun snd_mychip_reinit_chip(chip); 4050*4882a593Smuzhiyun /* (3) */ 4051*4882a593Smuzhiyun snd_mychip_restore_registers(chip); 4052*4882a593Smuzhiyun /* (4) */ 4053*4882a593Smuzhiyun snd_ac97_resume(chip->ac97); 4054*4882a593Smuzhiyun /* (5) */ 4055*4882a593Smuzhiyun snd_mychip_restart_chip(chip); 4056*4882a593Smuzhiyun /* (6) */ 4057*4882a593Smuzhiyun snd_power_change_state(card, SNDRV_CTL_POWER_D0); 4058*4882a593Smuzhiyun return 0; 4059*4882a593Smuzhiyun } 4060*4882a593Smuzhiyun 4061*4882a593SmuzhiyunNote that, at the time this callback gets called, the PCM stream has 4062*4882a593Smuzhiyunbeen already suspended via its own PM ops calling 4063*4882a593Smuzhiyun:c:func:`snd_pcm_suspend_all()` internally. 4064*4882a593Smuzhiyun 4065*4882a593SmuzhiyunOK, we have all callbacks now. Let's set them up. In the initialization 4066*4882a593Smuzhiyunof the card, make sure that you can get the chip data from the card 4067*4882a593Smuzhiyuninstance, typically via ``private_data`` field, in case you created the 4068*4882a593Smuzhiyunchip data individually. 4069*4882a593Smuzhiyun 4070*4882a593Smuzhiyun:: 4071*4882a593Smuzhiyun 4072*4882a593Smuzhiyun static int snd_mychip_probe(struct pci_dev *pci, 4073*4882a593Smuzhiyun const struct pci_device_id *pci_id) 4074*4882a593Smuzhiyun { 4075*4882a593Smuzhiyun .... 4076*4882a593Smuzhiyun struct snd_card *card; 4077*4882a593Smuzhiyun struct mychip *chip; 4078*4882a593Smuzhiyun int err; 4079*4882a593Smuzhiyun .... 4080*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 4081*4882a593Smuzhiyun 0, &card); 4082*4882a593Smuzhiyun .... 4083*4882a593Smuzhiyun chip = kzalloc(sizeof(*chip), GFP_KERNEL); 4084*4882a593Smuzhiyun .... 4085*4882a593Smuzhiyun card->private_data = chip; 4086*4882a593Smuzhiyun .... 4087*4882a593Smuzhiyun } 4088*4882a593Smuzhiyun 4089*4882a593SmuzhiyunWhen you created the chip data with :c:func:`snd_card_new()`, it's 4090*4882a593Smuzhiyunanyway accessible via ``private_data`` field. 4091*4882a593Smuzhiyun 4092*4882a593Smuzhiyun:: 4093*4882a593Smuzhiyun 4094*4882a593Smuzhiyun static int snd_mychip_probe(struct pci_dev *pci, 4095*4882a593Smuzhiyun const struct pci_device_id *pci_id) 4096*4882a593Smuzhiyun { 4097*4882a593Smuzhiyun .... 4098*4882a593Smuzhiyun struct snd_card *card; 4099*4882a593Smuzhiyun struct mychip *chip; 4100*4882a593Smuzhiyun int err; 4101*4882a593Smuzhiyun .... 4102*4882a593Smuzhiyun err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 4103*4882a593Smuzhiyun sizeof(struct mychip), &card); 4104*4882a593Smuzhiyun .... 4105*4882a593Smuzhiyun chip = card->private_data; 4106*4882a593Smuzhiyun .... 4107*4882a593Smuzhiyun } 4108*4882a593Smuzhiyun 4109*4882a593SmuzhiyunIf you need a space to save the registers, allocate the buffer for it 4110*4882a593Smuzhiyunhere, too, since it would be fatal if you cannot allocate a memory in 4111*4882a593Smuzhiyunthe suspend phase. The allocated buffer should be released in the 4112*4882a593Smuzhiyuncorresponding destructor. 4113*4882a593Smuzhiyun 4114*4882a593SmuzhiyunAnd next, set suspend/resume callbacks to the pci_driver. 4115*4882a593Smuzhiyun 4116*4882a593Smuzhiyun:: 4117*4882a593Smuzhiyun 4118*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(snd_my_pm_ops, mychip_suspend, mychip_resume); 4119*4882a593Smuzhiyun 4120*4882a593Smuzhiyun static struct pci_driver driver = { 4121*4882a593Smuzhiyun .name = KBUILD_MODNAME, 4122*4882a593Smuzhiyun .id_table = snd_my_ids, 4123*4882a593Smuzhiyun .probe = snd_my_probe, 4124*4882a593Smuzhiyun .remove = snd_my_remove, 4125*4882a593Smuzhiyun .driver.pm = &snd_my_pm_ops, 4126*4882a593Smuzhiyun }; 4127*4882a593Smuzhiyun 4128*4882a593SmuzhiyunModule Parameters 4129*4882a593Smuzhiyun================= 4130*4882a593Smuzhiyun 4131*4882a593SmuzhiyunThere are standard module options for ALSA. At least, each module should 4132*4882a593Smuzhiyunhave the ``index``, ``id`` and ``enable`` options. 4133*4882a593Smuzhiyun 4134*4882a593SmuzhiyunIf the module supports multiple cards (usually up to 8 = ``SNDRV_CARDS`` 4135*4882a593Smuzhiyuncards), they should be arrays. The default initial values are defined 4136*4882a593Smuzhiyunalready as constants for easier programming: 4137*4882a593Smuzhiyun 4138*4882a593Smuzhiyun:: 4139*4882a593Smuzhiyun 4140*4882a593Smuzhiyun static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; 4141*4882a593Smuzhiyun static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; 4142*4882a593Smuzhiyun static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; 4143*4882a593Smuzhiyun 4144*4882a593SmuzhiyunIf the module supports only a single card, they could be single 4145*4882a593Smuzhiyunvariables, instead. ``enable`` option is not always necessary in this 4146*4882a593Smuzhiyuncase, but it would be better to have a dummy option for compatibility. 4147*4882a593Smuzhiyun 4148*4882a593SmuzhiyunThe module parameters must be declared with the standard 4149*4882a593Smuzhiyun``module_param()``, ``module_param_array()`` and 4150*4882a593Smuzhiyun:c:func:`MODULE_PARM_DESC()` macros. 4151*4882a593Smuzhiyun 4152*4882a593SmuzhiyunThe typical coding would be like below: 4153*4882a593Smuzhiyun 4154*4882a593Smuzhiyun:: 4155*4882a593Smuzhiyun 4156*4882a593Smuzhiyun #define CARD_NAME "My Chip" 4157*4882a593Smuzhiyun 4158*4882a593Smuzhiyun module_param_array(index, int, NULL, 0444); 4159*4882a593Smuzhiyun MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard."); 4160*4882a593Smuzhiyun module_param_array(id, charp, NULL, 0444); 4161*4882a593Smuzhiyun MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard."); 4162*4882a593Smuzhiyun module_param_array(enable, bool, NULL, 0444); 4163*4882a593Smuzhiyun MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard."); 4164*4882a593Smuzhiyun 4165*4882a593SmuzhiyunAlso, don't forget to define the module description and the license. 4166*4882a593SmuzhiyunEspecially, the recent modprobe requires to define the 4167*4882a593Smuzhiyunmodule license as GPL, etc., otherwise the system is shown as “tainted”. 4168*4882a593Smuzhiyun 4169*4882a593Smuzhiyun:: 4170*4882a593Smuzhiyun 4171*4882a593Smuzhiyun MODULE_DESCRIPTION("Sound driver for My Chip"); 4172*4882a593Smuzhiyun MODULE_LICENSE("GPL"); 4173*4882a593Smuzhiyun 4174*4882a593Smuzhiyun 4175*4882a593SmuzhiyunHow To Put Your Driver Into ALSA Tree 4176*4882a593Smuzhiyun===================================== 4177*4882a593Smuzhiyun 4178*4882a593SmuzhiyunGeneral 4179*4882a593Smuzhiyun------- 4180*4882a593Smuzhiyun 4181*4882a593SmuzhiyunSo far, you've learned how to write the driver codes. And you might have 4182*4882a593Smuzhiyuna question now: how to put my own driver into the ALSA driver tree? Here 4183*4882a593Smuzhiyun(finally :) the standard procedure is described briefly. 4184*4882a593Smuzhiyun 4185*4882a593SmuzhiyunSuppose that you create a new PCI driver for the card “xyz”. The card 4186*4882a593Smuzhiyunmodule name would be snd-xyz. The new driver is usually put into the 4187*4882a593Smuzhiyunalsa-driver tree, ``sound/pci`` directory in the case of PCI 4188*4882a593Smuzhiyuncards. 4189*4882a593Smuzhiyun 4190*4882a593SmuzhiyunIn the following sections, the driver code is supposed to be put into 4191*4882a593SmuzhiyunLinux kernel tree. The two cases are covered: a driver consisting of a 4192*4882a593Smuzhiyunsingle source file and one consisting of several source files. 4193*4882a593Smuzhiyun 4194*4882a593SmuzhiyunDriver with A Single Source File 4195*4882a593Smuzhiyun-------------------------------- 4196*4882a593Smuzhiyun 4197*4882a593Smuzhiyun1. Modify sound/pci/Makefile 4198*4882a593Smuzhiyun 4199*4882a593Smuzhiyun Suppose you have a file xyz.c. Add the following two lines 4200*4882a593Smuzhiyun 4201*4882a593Smuzhiyun:: 4202*4882a593Smuzhiyun 4203*4882a593Smuzhiyun snd-xyz-objs := xyz.o 4204*4882a593Smuzhiyun obj-$(CONFIG_SND_XYZ) += snd-xyz.o 4205*4882a593Smuzhiyun 4206*4882a593Smuzhiyun2. Create the Kconfig entry 4207*4882a593Smuzhiyun 4208*4882a593Smuzhiyun Add the new entry of Kconfig for your xyz driver. config SND_XYZ 4209*4882a593Smuzhiyun tristate "Foobar XYZ" depends on SND select SND_PCM help Say Y here 4210*4882a593Smuzhiyun to include support for Foobar XYZ soundcard. To compile this driver 4211*4882a593Smuzhiyun as a module, choose M here: the module will be called snd-xyz. the 4212*4882a593Smuzhiyun line, select SND_PCM, specifies that the driver xyz supports PCM. In 4213*4882a593Smuzhiyun addition to SND_PCM, the following components are supported for 4214*4882a593Smuzhiyun select command: SND_RAWMIDI, SND_TIMER, SND_HWDEP, 4215*4882a593Smuzhiyun SND_MPU401_UART, SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, 4216*4882a593Smuzhiyun SND_AC97_CODEC. Add the select command for each supported 4217*4882a593Smuzhiyun component. 4218*4882a593Smuzhiyun 4219*4882a593Smuzhiyun Note that some selections imply the lowlevel selections. For example, 4220*4882a593Smuzhiyun PCM includes TIMER, MPU401_UART includes RAWMIDI, AC97_CODEC 4221*4882a593Smuzhiyun includes PCM, and OPL3_LIB includes HWDEP. You don't need to give 4222*4882a593Smuzhiyun the lowlevel selections again. 4223*4882a593Smuzhiyun 4224*4882a593Smuzhiyun For the details of Kconfig script, refer to the kbuild documentation. 4225*4882a593Smuzhiyun 4226*4882a593SmuzhiyunDrivers with Several Source Files 4227*4882a593Smuzhiyun--------------------------------- 4228*4882a593Smuzhiyun 4229*4882a593SmuzhiyunSuppose that the driver snd-xyz have several source files. They are 4230*4882a593Smuzhiyunlocated in the new subdirectory, sound/pci/xyz. 4231*4882a593Smuzhiyun 4232*4882a593Smuzhiyun1. Add a new directory (``sound/pci/xyz``) in ``sound/pci/Makefile`` 4233*4882a593Smuzhiyun as below 4234*4882a593Smuzhiyun 4235*4882a593Smuzhiyun:: 4236*4882a593Smuzhiyun 4237*4882a593Smuzhiyun obj-$(CONFIG_SND) += sound/pci/xyz/ 4238*4882a593Smuzhiyun 4239*4882a593Smuzhiyun 4240*4882a593Smuzhiyun2. Under the directory ``sound/pci/xyz``, create a Makefile 4241*4882a593Smuzhiyun 4242*4882a593Smuzhiyun:: 4243*4882a593Smuzhiyun 4244*4882a593Smuzhiyun snd-xyz-objs := xyz.o abc.o def.o 4245*4882a593Smuzhiyun obj-$(CONFIG_SND_XYZ) += snd-xyz.o 4246*4882a593Smuzhiyun 4247*4882a593Smuzhiyun3. Create the Kconfig entry 4248*4882a593Smuzhiyun 4249*4882a593Smuzhiyun This procedure is as same as in the last section. 4250*4882a593Smuzhiyun 4251*4882a593Smuzhiyun 4252*4882a593SmuzhiyunUseful Functions 4253*4882a593Smuzhiyun================ 4254*4882a593Smuzhiyun 4255*4882a593Smuzhiyun:c:func:`snd_printk()` and friends 4256*4882a593Smuzhiyun---------------------------------- 4257*4882a593Smuzhiyun 4258*4882a593Smuzhiyun.. note:: This subsection describes a few helper functions for 4259*4882a593Smuzhiyun decorating a bit more on the standard :c:func:`printk()` & co. 4260*4882a593Smuzhiyun However, in general, the use of such helpers is no longer recommended. 4261*4882a593Smuzhiyun If possible, try to stick with the standard functions like 4262*4882a593Smuzhiyun :c:func:`dev_err()` or :c:func:`pr_err()`. 4263*4882a593Smuzhiyun 4264*4882a593SmuzhiyunALSA provides a verbose version of the :c:func:`printk()` function. 4265*4882a593SmuzhiyunIf a kernel config ``CONFIG_SND_VERBOSE_PRINTK`` is set, this function 4266*4882a593Smuzhiyunprints the given message together with the file name and the line of the 4267*4882a593Smuzhiyuncaller. The ``KERN_XXX`` prefix is processed as well as the original 4268*4882a593Smuzhiyun:c:func:`printk()` does, so it's recommended to add this prefix, 4269*4882a593Smuzhiyune.g. snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\\n"); 4270*4882a593Smuzhiyun 4271*4882a593SmuzhiyunThere are also :c:func:`printk()`'s for debugging. 4272*4882a593Smuzhiyun:c:func:`snd_printd()` can be used for general debugging purposes. 4273*4882a593SmuzhiyunIf ``CONFIG_SND_DEBUG`` is set, this function is compiled, and works 4274*4882a593Smuzhiyunjust like :c:func:`snd_printk()`. If the ALSA is compiled without 4275*4882a593Smuzhiyunthe debugging flag, it's ignored. 4276*4882a593Smuzhiyun 4277*4882a593Smuzhiyun:c:func:`snd_printdd()` is compiled in only when 4278*4882a593Smuzhiyun``CONFIG_SND_DEBUG_VERBOSE`` is set. 4279*4882a593Smuzhiyun 4280*4882a593Smuzhiyun:c:func:`snd_BUG()` 4281*4882a593Smuzhiyun------------------- 4282*4882a593Smuzhiyun 4283*4882a593SmuzhiyunIt shows the ``BUG?`` message and stack trace as well as 4284*4882a593Smuzhiyun:c:func:`snd_BUG_ON()` at the point. It's useful to show that a 4285*4882a593Smuzhiyunfatal error happens there. 4286*4882a593Smuzhiyun 4287*4882a593SmuzhiyunWhen no debug flag is set, this macro is ignored. 4288*4882a593Smuzhiyun 4289*4882a593Smuzhiyun:c:func:`snd_BUG_ON()` 4290*4882a593Smuzhiyun---------------------- 4291*4882a593Smuzhiyun 4292*4882a593Smuzhiyun:c:func:`snd_BUG_ON()` macro is similar with 4293*4882a593Smuzhiyun:c:func:`WARN_ON()` macro. For example, snd_BUG_ON(!pointer); or 4294*4882a593Smuzhiyunit can be used as the condition, if (snd_BUG_ON(non_zero_is_bug)) 4295*4882a593Smuzhiyunreturn -EINVAL; 4296*4882a593Smuzhiyun 4297*4882a593SmuzhiyunThe macro takes an conditional expression to evaluate. When 4298*4882a593Smuzhiyun``CONFIG_SND_DEBUG``, is set, if the expression is non-zero, it shows 4299*4882a593Smuzhiyunthe warning message such as ``BUG? (xxx)`` normally followed by stack 4300*4882a593Smuzhiyuntrace. In both cases it returns the evaluated value. 4301*4882a593Smuzhiyun 4302*4882a593SmuzhiyunAcknowledgments 4303*4882a593Smuzhiyun=============== 4304*4882a593Smuzhiyun 4305*4882a593SmuzhiyunI would like to thank Phil Kerr for his help for improvement and 4306*4882a593Smuzhiyuncorrections of this document. 4307*4882a593Smuzhiyun 4308*4882a593SmuzhiyunKevin Conder reformatted the original plain-text to the DocBook format. 4309*4882a593Smuzhiyun 4310*4882a593SmuzhiyunGiuliano Pochini corrected typos and contributed the example codes in 4311*4882a593Smuzhiyunthe hardware constraints section. 4312