`sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget;/translations/zh_CN/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget;/translations/zh_TW/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget;/translations/it_IT/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget;/translations/ja_JP/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget;/translations/ko_KR/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget;/translations/sp_SP/sound/kernel-api/writing-an-alsa-drivermodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(hWriting an ALSA Driverh]hWriting an ALSA Driver}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhU/var/lib/git/docbuild/linux/Documentation/sound/kernel-api/writing-an-alsa-driver.rsthKubh field_list)}(hhh]hfield)}(hhh](h field_name)}(hAuthorh]hAuthor}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhKubh field_body)}(hTakashi Iwai h]h paragraph)}(hTakashi Iwai h](hTakashi Iwai <}(hhhhhNhNubh reference)}(h tiwai@suse.deh]h tiwai@suse.de}(hhhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:tiwai@suse.deuh1hhhubh>}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hPrefaceh]hPreface}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hX`This document describes how to write an `ALSA (Advanced Linux Sound Architecture) `__ driver. The document focuses mainly on PCI soundcards. In the case of other device types, the API might be different, too. However, at least the ALSA kernel API is consistent, and therefore it would be still a bit help for writing them.h](h(This document describes how to write an }(hj hhhNhNubh)}(hK`ALSA (Advanced Linux Sound Architecture) `__h]h(ALSA (Advanced Linux Sound Architecture)}(hj(hhhNhNubah}(h]h ]h"]h$]h&]name(ALSA (Advanced Linux Sound Architecture)refurihttp://www.alsa-project.org/uh1hhj ubh driver. The document focuses mainly on PCI soundcards. In the case of other device types, the API might be different, too. However, at least the ALSA kernel API is consistent, and therefore it would be still a bit help for writing them.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hjhhubh)}(hXCThis document targets people who already have enough C language skills and have basic linux kernel programming knowledge. This document doesn't explain the general topic of linux kernel coding and doesn't cover low-level driver implementation details. It only describes the standard way to write a PCI sound driver on ALSA.h]hXGThis document targets people who already have enough C language skills and have basic linux kernel programming knowledge. This document doesn’t explain the general topic of linux kernel coding and doesn’t cover low-level driver implementation details. It only describes the standard way to write a PCI sound driver on ALSA.}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]prefaceah ]h"]prefaceah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hFile Tree Structureh]hFile Tree Structure}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(hGeneralh]hGeneral}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjkhhhhhKubh)}(h:The file tree structure of ALSA driver is depicted below::h]h9The file tree structure of ALSA driver is depicted below:}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjkhhubh literal_block)}(hXsound /core /oss /seq /oss /include /drivers /mpu401 /opl3 /i2c /synth /emux /pci /(cards) /isa /(cards) /arm /ppc /sparc /usb /pcmcia /(cards) /soc /ossh]hXsound /core /oss /seq /oss /include /drivers /mpu401 /opl3 /i2c /synth /emux /pci /(cards) /isa /(cards) /arm /ppc /sparc /usb /pcmcia /(cards) /soc /oss}hjsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jhhhKhjkhhubeh}(h]generalah ]h"]h$]generalah&]uh1hhjZhhhhhK referencedKubh)}(hhh](h)}(hcore directoryh]hcore directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK8ubh)}(hThis directory contains the middle layer which is the heart of ALSA drivers. In this directory, the native ALSA modules are stored. The sub-directories contain different modules and are dependent upon the kernel config.h]hThis directory contains the middle layer which is the heart of ALSA drivers. In this directory, the native ALSA modules are stored. The sub-directories contain different modules and are dependent upon the kernel config.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK:hjhhubh)}(hhh](h)}(hcore/ossh]hcore/oss}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK@ubh)}(hXThe code for OSS PCM and mixer emulation modules is stored in this directory. The OSS rawmidi emulation is included in the ALSA rawmidi code since it's quite small. The sequencer code is stored in ``core/seq/oss`` directory (see `below `__).h](hThe code for OSS PCM and mixer emulation modules is stored in this directory. The OSS rawmidi emulation is included in the ALSA rawmidi code since it’s quite small. The sequencer code is stored in }(hjhhhNhNubhliteral)}(h``core/seq/oss``h]h core/seq/oss}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh directory (see }(hjhhhNhNubh)}(h`below `__h]hbelow}(hjhhhNhNubah}(h]h ]h"]h$]h&]namebelowrefid core-seq-ossuh1hhjresolvedKubh).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKBhjhhubeh}(h]core-ossah ]h"]core/ossah$]h&]uh1hhjhhhhhK@ubh)}(hhh](h)}(hcore/seqh]hcore/seq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKHubh)}(hX This directory and its sub-directories are for the ALSA sequencer. This directory contains the sequencer core and primary sequencer modules such as snd-seq-midi, snd-seq-virmidi, etc. They are compiled only when ``CONFIG_SND_SEQUENCER`` is set in the kernel config.h](hThis directory and its sub-directories are for the ALSA sequencer. This directory contains the sequencer core and primary sequencer modules such as snd-seq-midi, snd-seq-virmidi, etc. They are compiled only when }(hj'hhhNhNubj)}(h``CONFIG_SND_SEQUENCER``h]hCONFIG_SND_SEQUENCER}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'ubh is set in the kernel config.}(hj'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKJhjhhubeh}(h]core-seqah ]h"]core/seqah$]h&]uh1hhjhhhhhKHubh)}(hhh](h)}(h core/seq/ossh]h core/seq/oss}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjOhhhhhKPubh)}(h/This contains the OSS sequencer emulation code.h]h/This contains the OSS sequencer emulation code.}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKRhjOhhubeh}(h]jah ]h"] core/seq/ossah$]h&]uh1hhjhhhhhKPjKubeh}(h]core-directoryah ]h"]core directoryah$]h&]uh1hhjZhhhhhK8ubh)}(hhh](h)}(hinclude directoryh]hinclude directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj}hhhhhKUubh)}(hX*This is the place for the public header files of ALSA drivers, which are to be exported to user-space, or included by several files in different directories. Basically, the private header files should not be placed in this directory, but you may still find files there, due to historical reasons :)h]hX*This is the place for the public header files of ALSA drivers, which are to be exported to user-space, or included by several files in different directories. Basically, the private header files should not be placed in this directory, but you may still find files there, due to historical reasons :)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKWhj}hhubeh}(h]include-directoryah ]h"]include directoryah$]h&]uh1hhjZhhhhhKUubh)}(hhh](h)}(hdrivers directoryh]hdrivers directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK^ubh)}(hXUThis directory contains code shared among different drivers on different architectures. They are hence supposed not to be architecture-specific. For example, the dummy PCM driver and the serial MIDI driver are found in this directory. In the sub-directories, there is code for components which are independent from bus and cpu architectures.h]hXUThis directory contains code shared among different drivers on different architectures. They are hence supposed not to be architecture-specific. For example, the dummy PCM driver and the serial MIDI driver are found in this directory. In the sub-directories, there is code for components which are independent from bus and cpu architectures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK`hjhhubh)}(hhh](h)}(hdrivers/mpu401h]hdrivers/mpu401}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKgubh)}(h3The MPU401 and MPU401-UART modules are stored here.h]h3The MPU401 and MPU401-UART modules are stored here.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKihjhhubeh}(h]drivers-mpu401ah ]h"]drivers/mpu401ah$]h&]uh1hhjhhhhhKgubh)}(hhh](h)}(hdrivers/opl3 and opl4h]hdrivers/opl3 and opl4}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKlubh)}(h/The OPL3 and OPL4 FM-synth stuff is found here.h]h/The OPL3 and OPL4 FM-synth stuff is found here.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKnhjhhubeh}(h]drivers-opl3-and-opl4ah ]h"]drivers/opl3 and opl4ah$]h&]uh1hhjhhhhhKlubeh}(h]drivers-directoryah ]h"]drivers directoryah$]h&]uh1hhjZhhhhhK^ubh)}(hhh](h)}(h i2c directoryh]h i2c directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKqubh)}(h&This contains the ALSA i2c components.h]h&This contains the ALSA i2c components.}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKshjhhubh)}(hAlthough there is a standard i2c layer on Linux, ALSA has its own i2c code for some cards, because the soundcard needs only a simple operation and the standard i2c API is too complicated for such a purpose.h]hAlthough there is a standard i2c layer on Linux, ALSA has its own i2c code for some cards, because the soundcard needs only a simple operation and the standard i2c API is too complicated for such a purpose.}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKuhjhhubeh}(h] i2c-directoryah ]h"] i2c directoryah$]h&]uh1hhjZhhhhhKqubh)}(hhh](h)}(hsynth directoryh]hsynth directory}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjNhhhhhKzubh)}(h-This contains the synth middle-level modules.h]h-This contains the synth middle-level modules.}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK|hjNhhubh)}(hZSo far, there is only Emu8000/Emu10k1 synth driver under the ``synth/emux`` sub-directory.h](h=So far, there is only Emu8000/Emu10k1 synth driver under the }(hjmhhhNhNubj)}(h``synth/emux``h]h synth/emux}(hjuhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjmubh sub-directory.}(hjmhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK~hjNhhubeh}(h]synth-directoryah ]h"]synth directoryah$]h&]uh1hhjZhhhhhKzubh)}(hhh](h)}(h pci directoryh]h pci directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hThis directory and its sub-directories hold the top-level card modules for PCI soundcards and the code specific to the PCI BUS.h]hThis directory and its sub-directories hold the top-level card modules for PCI soundcards and the code specific to the PCI BUS.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hThe drivers compiled from a single file are stored directly in the pci directory, while the drivers with several source files are stored on their own sub-directory (e.g. emu10k1, ice1712).h]hThe drivers compiled from a single file are stored directly in the pci directory, while the drivers with several source files are stored on their own sub-directory (e.g. emu10k1, ice1712).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] pci-directoryah ]h"] pci directoryah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(h isa directoryh]h isa directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hZThis directory and its sub-directories hold the top-level card modules for ISA soundcards.h]hZThis directory and its sub-directories hold the top-level card modules for ISA soundcards.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] isa-directoryah ]h"] isa directoryah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(harm, ppc, and sparc directoriesh]harm, ppc, and sparc directories}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hZThey are used for top-level card modules which are specific to one of these architectures.h]hZThey are used for top-level card modules which are specific to one of these architectures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]arm-ppc-and-sparc-directoriesah ]h"]arm, ppc, and sparc directoriesah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(h usb directoryh]h usb directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hhThis directory contains the USB-audio driver. The USB MIDI driver is integrated in the usb-audio driver.h]hhThis directory contains the USB-audio driver. The USB MIDI driver is integrated in the usb-audio driver.}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] usb-directoryah ]h"] usb directoryah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(hpcmcia directoryh]hpcmcia directory}(hjBhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj?hhhhhKubh)}(hThe PCMCIA, especially PCCard drivers will go here. CardBus drivers will be in the pci directory, because their API is identical to that of standard PCI cards.h]hThe PCMCIA, especially PCCard drivers will go here. CardBus drivers will be in the pci directory, because their API is identical to that of standard PCI cards.}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj?hhubeh}(h]pcmcia-directoryah ]h"]pcmcia directoryah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(h soc directoryh]h soc directory}(hjihhhNhNubah}(h]h ]h"]h$]h&]uh1hhjfhhhhhKubh)}(hvThis directory contains the codes for ASoC (ALSA System on Chip) layer including ASoC core, codec and machine drivers.h]hvThis directory contains the codes for ASoC (ALSA System on Chip) layer including ASoC core, codec and machine drivers.}(hjwhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjfhhubeh}(h] soc-directoryah ]h"] soc directoryah$]h&]uh1hhjZhhhhhKubh)}(hhh](h)}(h oss directoryh]h oss directory}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hkThis contains OSS/Lite code. At the time of writing, all code has been removed except for dmasound on m68k.h]hkThis contains OSS/Lite code. At the time of writing, all code has been removed except for dmasound on m68k.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h] oss-directoryah ]h"] oss directoryah$]h&]uh1hhjZhhhhhKubeh}(h]file-tree-structureah ]h"]file tree structureah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hBasic Flow for PCI Driversh]hBasic Flow for PCI Drivers}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hOutlineh]hOutline}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(h2The minimum flow for PCI soundcards is as follows:h]h2The minimum flow for PCI soundcards is as follows:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh bullet_list)}(hhh](h list_item)}(h:define the PCI ID table (see the section `PCI Entries`_). h]h)}(h9define the PCI ID table (see the section `PCI Entries`_).h](h)define the PCI ID table (see the section }(hjhhhNhNubh)}(h`PCI Entries`_h]h PCI Entries}(hjhhhNhNubah}(h]h ]h"]h$]h&]name PCI Entriesj pci-entriesuh1hhjjKubh).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hcreate ``probe`` callback. h]h)}(hcreate ``probe`` callback.h](hcreate }(hj$hhhNhNubj)}(h ``probe``h]hprobe}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj$ubh callback.}(hj$hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hcreate ``remove`` callback. h]h)}(hcreate ``remove`` callback.h](hcreate }(hjNhhhNhNubj)}(h ``remove``h]hremove}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh callback.}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjJubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hJcreate a struct pci_driver structure containing the three pointers above. h]h)}(hIcreate a struct pci_driver structure containing the three pointers above.h]hIcreate a struct pci_driver structure containing the three pointers above.}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjtubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h}create an ``init`` function just calling the :c:func:`pci_register_driver()` to register the pci_driver table defined above. h]h)}(h|create an ``init`` function just calling the :c:func:`pci_register_driver()` to register the pci_driver table defined above.h](h create an }(hjhhhNhNubj)}(h``init``h]hinit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh function just calling the }(hjhhhNhNubh)}(h:c:func:`pci_register_driver()`h]j)}(hjh]hpci_register_driver()}(hjhhhNhNubah}(h]h ](xrefcc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdoc'sound/kernel-api/writing-an-alsa-driver refdomainjreftypefunc refexplicitrefwarn reftargetpci_register_driveruh1hhhhKhjubh0 to register the pci_driver table defined above.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hTcreate an ``exit`` function to call the :c:func:`pci_unregister_driver()` function. h]h)}(hScreate an ``exit`` function to call the :c:func:`pci_unregister_driver()` function.h](h create an }(hjhhhNhNubj)}(h``exit``h]hexit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh function to call the }(hjhhhNhNubh)}(h!:c:func:`pci_unregister_driver()`h]j)}(hjh]hpci_unregister_driver()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_unregister_driveruh1hhhhKhjubh function.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]bullet-uh1jhhhKhjhhubeh}(h]outlineah ]h"]outlineah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hFull Code Exampleh]hFull Code Example}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj:hhhhhKubh)}(hThe code example is shown below. Some parts are kept unimplemented at this moment but will be filled in the next sections. The numbers in the comment lines of the :c:func:`snd_mychip_probe()` function refer to details explained in the following section.h](hThe code example is shown below. Some parts are kept unimplemented at this moment but will be filled in the next sections. The numbers in the comment lines of the }(hjKhhhNhNubh)}(h:c:func:`snd_mychip_probe()`h]j)}(hjUh]hsnd_mychip_probe()}(hjWhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjSubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mychip_probeuh1hhhhKhjKubh> function refer to details explained in the following section.}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj:hhubj)}(hX #include #include #include #include #include /* module parameters (see "Module Parameters") */ /* SNDRV_CARDS: maximum number of cards supported by this module */ static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* definition of the chip-specific record */ struct mychip { struct snd_card *card; /* the rest of the implementation will be in section * "PCI Resource Management" */ }; /* chip-specific destructor * (see "PCI Resource Management") */ static int snd_mychip_free(struct mychip *chip) { .... /* will be implemented later... */ } /* component-destructor * (see "Management of Cards and Components") */ static int snd_mychip_dev_free(struct snd_device *device) { return snd_mychip_free(device->device_data); } /* chip-specific constructor * (see "Management of Cards and Components") */ static int snd_mychip_create(struct snd_card *card, struct pci_dev *pci, struct mychip **rchip) { struct mychip *chip; int err; static const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; *rchip = NULL; /* check PCI availability here * (see "PCI Resource Management") */ .... /* allocate a chip-specific data with zero filled */ chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) return -ENOMEM; chip->card = card; /* rest of initialization here; will be implemented * later, see "PCI Resource Management" */ .... err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); if (err < 0) { snd_mychip_free(chip); return err; } *rchip = chip; return 0; } /* constructor -- see "Driver Constructor" sub-section */ static int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { static int dev; struct snd_card *card; struct mychip *chip; int err; /* (1) */ if (dev >= SNDRV_CARDS) return -ENODEV; if (!enable[dev]) { dev++; return -ENOENT; } /* (2) */ err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); if (err < 0) return err; /* (3) */ err = snd_mychip_create(card, pci, &chip); if (err < 0) goto error; /* (4) */ strcpy(card->driver, "My Chip"); strcpy(card->shortname, "My Own Chip 123"); sprintf(card->longname, "%s at 0x%lx irq %i", card->shortname, chip->port, chip->irq); /* (5) */ .... /* implemented later */ /* (6) */ err = snd_card_register(card); if (err < 0) goto error; /* (7) */ pci_set_drvdata(pci, card); dev++; return 0; error: snd_card_free(card); return err; } /* destructor -- see the "Destructor" sub-section */ static void snd_mychip_remove(struct pci_dev *pci) { snd_card_free(pci_get_drvdata(pci)); }h]hX #include #include #include #include #include /* module parameters (see "Module Parameters") */ /* SNDRV_CARDS: maximum number of cards supported by this module */ static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* definition of the chip-specific record */ struct mychip { struct snd_card *card; /* the rest of the implementation will be in section * "PCI Resource Management" */ }; /* chip-specific destructor * (see "PCI Resource Management") */ static int snd_mychip_free(struct mychip *chip) { .... /* will be implemented later... */ } /* component-destructor * (see "Management of Cards and Components") */ static int snd_mychip_dev_free(struct snd_device *device) { return snd_mychip_free(device->device_data); } /* chip-specific constructor * (see "Management of Cards and Components") */ static int snd_mychip_create(struct snd_card *card, struct pci_dev *pci, struct mychip **rchip) { struct mychip *chip; int err; static const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; *rchip = NULL; /* check PCI availability here * (see "PCI Resource Management") */ .... /* allocate a chip-specific data with zero filled */ chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) return -ENOMEM; chip->card = card; /* rest of initialization here; will be implemented * later, see "PCI Resource Management" */ .... err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); if (err < 0) { snd_mychip_free(chip); return err; } *rchip = chip; return 0; } /* constructor -- see "Driver Constructor" sub-section */ static int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { static int dev; struct snd_card *card; struct mychip *chip; int err; /* (1) */ if (dev >= SNDRV_CARDS) return -ENODEV; if (!enable[dev]) { dev++; return -ENOENT; } /* (2) */ err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); if (err < 0) return err; /* (3) */ err = snd_mychip_create(card, pci, &chip); if (err < 0) goto error; /* (4) */ strcpy(card->driver, "My Chip"); strcpy(card->shortname, "My Own Chip 123"); sprintf(card->longname, "%s at 0x%lx irq %i", card->shortname, chip->port, chip->irq); /* (5) */ .... /* implemented later */ /* (6) */ err = snd_card_register(card); if (err < 0) goto error; /* (7) */ pci_set_drvdata(pci, card); dev++; return 0; error: snd_card_free(card); return err; } /* destructor -- see the "Destructor" sub-section */ static void snd_mychip_remove(struct pci_dev *pci) { snd_card_free(pci_get_drvdata(pci)); }}hj|sbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj:hhubeh}(h]full-code-exampleah ]h"]h$]full code exampleah&]uh1hhjhhhhhKjKubh)}(hhh](h)}(hDriver Constructorh]hDriver Constructor}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM_ubh)}(hThe real constructor of PCI drivers is the ``probe`` callback. The ``probe`` callback and other component-constructors which are called from the ``probe`` callback cannot be used with the ``__init`` prefix because any PCI device could be a hotplug device.h](h+The real constructor of PCI drivers is the }(hjhhhNhNubj)}(h ``probe``h]hprobe}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh callback. The }(hjhhhNhNubj)}(h ``probe``h]hprobe}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhE callback and other component-constructors which are called from the }(hjhhhNhNubj)}(h ``probe``h]hprobe}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh" callback cannot be used with the }(hjhhhNhNubj)}(h ``__init``h]h__init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh9 prefix because any PCI device could be a hotplug device.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMahjhhubh)}(h>In the ``probe`` callback, the following scheme is often used.h](hIn the }(hjhhhNhNubj)}(h ``probe``h]hprobe}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. callback, the following scheme is often used.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMfhjhhubh)}(hhh](h)}(h(1) Check and increment the device index.h]h(1) Check and increment the device index.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMiubj)}(hstatic int dev; .... if (dev >= SNDRV_CARDS) return -ENODEV; if (!enable[dev]) { dev++; return -ENOENT; }h]hstatic int dev; .... if (dev >= SNDRV_CARDS) return -ENODEV; if (!enable[dev]) { dev++; return -ENOENT; }}hj*sbah}(h]h ]h"]h$]h&]jjuh1jhhhMmhjhhubh)}(h+where ``enable[dev]`` is the module option.h](hwhere }(hj8hhhNhNubj)}(h``enable[dev]``h]h enable[dev]}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj8ubh is the module option.}(hj8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMwhjhhubh)}(hEach time the ``probe`` callback is called, check the availability of the device. If not available, simply increment the device index and return. dev will be incremented also later (`step 7 <7) Set the PCI driver data and return zero._>`__).h](hEach time the }(hjXhhhNhNubj)}(h ``probe``h]hprobe}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh callback is called, check the availability of the device. If not available, simply increment the device index and return. dev will be incremented also later (}(hjXhhhNhNubh)}(h9`step 7 <7) Set the PCI driver data and return zero._>`__h]hstep 7}(hjrhhhNhNubah}(h]h ]h"]h$]h&]namestep 7j'set-the-pci-driver-data-and-return-zerouh1hhjXjKubh).}(hjXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMyhjhhubeh}(h]$check-and-increment-the-device-indexah ]h"](1) check and increment the device index.ah$]h&]uh1hhjhhhhhMiubh)}(hhh](h)}(h2) Create a card instanceh]h2) Create a card instance}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(hstruct snd_card *card; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card);h]hstruct snd_card *card; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hSThe details will be explained in the section `Management of Cards and Components`_.h](h-The details will be explained in the section }(hjhhhNhNubh)}(h%`Management of Cards and Components`_h]h"Management of Cards and Components}(hjhhhNhNubah}(h]h ]h"]h$]h&]name"Management of Cards and Componentsj"management-of-cards-and-componentsuh1hhjjKubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]create-a-card-instanceah ]h"]2) create a card instanceah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h3) Create a main componenth]h3) Create a main component}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h/In this part, the PCI resources are allocated::h]h.In this part, the PCI resources are allocated:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hestruct mychip *chip; .... err = snd_mychip_create(card, pci, &chip); if (err < 0) goto error;h]hestruct mychip *chip; .... err = snd_mychip_create(card, pci, &chip); if (err < 0) goto error;}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hHThe details will be explained in the section `PCI Resource Management`_.h](h-The details will be explained in the section }(hj hhhNhNubh)}(h`PCI Resource Management`_h]hPCI Resource Management}(hjhhhNhNubah}(h]h ]h"]h$]h&]namePCI Resource Managementjpci-resource-managementuh1hhj jKubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hWhen something goes wrong, the probe function needs to deal with the error. In this example, we have a single error handling path placed at the end of the function::h]hWhen something goes wrong, the probe function needs to deal with the error. In this example, we have a single error handling path placed at the end of the function:}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h7error: snd_card_free(card); return err;h]h7error: snd_card_free(card); return err;}hj=sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hsSince each component can be properly freed, the single :c:func:`snd_card_free()` call should suffice in most cases.h](h7Since each component can be properly freed, the single }(hjKhhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hjUh]hsnd_card_free()}(hjWhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjSubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMhjKubh# call should suffice in most cases.}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]create-a-main-componentah ]h"]3) create a main componentah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h&4) Set the driver ID and name strings.h]h&4) Set the driver ID and name strings.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(hstrcpy(card->driver, "My Chip"); strcpy(card->shortname, "My Own Chip 123"); sprintf(card->longname, "%s at 0x%lx irq %i", card->shortname, chip->port, chip->irq);h]hstrcpy(card->driver, "My Chip"); strcpy(card->shortname, "My Own Chip 123"); sprintf(card->longname, "%s at 0x%lx irq %i", card->shortname, chip->port, chip->irq);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hThe driver field holds the minimal ID string of the chip. This is used by alsa-lib's configurator, so keep it simple but unique. Even the same driver can have different driver IDs to distinguish the functionality of each chip type.h]hThe driver field holds the minimal ID string of the chip. This is used by alsa-lib’s configurator, so keep it simple but unique. Even the same driver can have different driver IDs to distinguish the functionality of each chip type.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe shortname field is a string shown as more verbose name. The longname field contains the information shown in ``/proc/asound/cards``.h](hqThe shortname field is a string shown as more verbose name. The longname field contains the information shown in }(hjhhhNhNubj)}(h``/proc/asound/cards``h]h/proc/asound/cards}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]"set-the-driver-id-and-name-stringsah ]h"]&4) set the driver id and name strings.ah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h55) Create other components, such as mixer, MIDI, etc.h]h55) Create other components, such as mixer, MIDI, etc.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hX Here you define the basic components such as `PCM `__, mixer (e.g. `AC97 `__), MIDI (e.g. `MPU-401 `__), and other interfaces. Also, if you want a `proc file `__, define it here, too.h](h-Here you define the basic components such as }(hjhhhNhNubh)}(h`PCM `__h]hPCM}(hjhhhNhNubah}(h]h ]h"]h$]h&]namePCMj pcm-interfaceuh1hhjjKubh, mixer (e.g. }(hjhhhNhNubh)}(h`AC97 `__h]hAC97}(hj hhhNhNubah}(h]h ]h"]h$]h&]nameAC97japi-for-ac97-codecuh1hhjjKubh), MIDI (e.g. }(hjhhhNhNubh)}(h+`MPU-401 `__h]hMPU-401}(hj hhhNhNubah}(h]h ]h"]h$]h&]nameMPU-401jmidi-mpu401-uart-interfaceuh1hhjjKubh-), and other interfaces. Also, if you want a }(hjhhhNhNubh)}(h`proc file `__h]h proc file}(hj1 hhhNhNubah}(h]h ]h"]h$]h&]name proc filejproc-interfaceuh1hhjjKubh, define it here, too.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h].create-other-components-such-as-mixer-midi-etcah ]h"]55) create other components, such as mixer, midi, etc.ah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h6) Register the card instance.h]h6) Register the card instance.}(hjW hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjT hhhhhMubj)}(h?err = snd_card_register(card); if (err < 0) goto error;h]h?err = snd_card_register(card); if (err < 0) goto error;}hje sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjT hhubh)}(hLWill be explained in the section `Management of Cards and Components`_, too.h](h!Will be explained in the section }(hjs hhhNhNubh)}(h%`Management of Cards and Components`_h]h"Management of Cards and Components}(hj{ hhhNhNubah}(h]h ]h"]h$]h&]name"Management of Cards and Componentsjjuh1hhjs jKubh, too.}(hjs hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjT hhubeh}(h]register-the-card-instanceah ]h"]6) register the card instance.ah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h+7) Set the PCI driver data and return zero.h]h+7) Set the PCI driver data and return zero.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubj)}(h,pci_set_drvdata(pci, card); dev++; return 0;h]h,pci_set_drvdata(pci, card); dev++; return 0;}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hyIn the above, the card record is stored. This pointer is used in the remove callback and power-management callbacks, too.h]hyIn the above, the card record is stored. This pointer is used in the remove callback and power-management callbacks, too.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]jah ]h"]+7) set the pci driver data and return zero.ah$]h&]uh1hhjhhhhhMjKubeh}(h]driver-constructorah ]h"]driver constructorah$]h&]uh1hhjhhhhhM_ubh)}(hhh](h)}(h Destructorh]h Destructor}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hThe destructor, the remove callback, simply releases the card instance. Then the ALSA middle layer will release all the attached components automatically.h]hThe destructor, the remove callback, simply releases the card instance. Then the ALSA middle layer will release all the attached components automatically.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h>It would be typically just calling :c:func:`snd_card_free()`::h](h#It would be typically just calling }(hj hhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hj h]hsnd_card_free()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMhj ubh:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hcstatic void snd_mychip_remove(struct pci_dev *pci) { snd_card_free(pci_get_drvdata(pci)); }h]hcstatic void snd_mychip_remove(struct pci_dev *pci) { snd_card_free(pci_get_drvdata(pci)); }}hj) sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hKThe above code assumes that the card pointer is set to the PCI driver data.h]hKThe above code assumes that the card pointer is set to the PCI driver data.}(hj7 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h] destructorah ]h"] destructorah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h Header Filesh]h Header Files}(hjP hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjM hhhhhMubh)}(hKFor the above example, at least the following include files are necessary::h]hJFor the above example, at least the following include files are necessary:}(hj^ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjM hhubj)}(hy#include #include #include #include #include h]hy#include #include #include #include #include }hjl sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjM hhubh)}(hwhere the last one is necessary only when module options are defined in the source file. If the code is split into several files, the files without module options don't need them.h]hwhere the last one is necessary only when module options are defined in the source file. If the code is split into several files, the files without module options don’t need them.}(hjz hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjM hhubh)}(hIn addition to these headers, you'll need ```` for interrupt handling, and ```` for I/O access. If you use the :c:func:`mdelay()` or :c:func:`udelay()` functions, you'll need to include ```` too.h](h,In addition to these headers, you’ll need }(hj hhhNhNubj)}(h````h]h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh for interrupt handling, and }(hj hhhNhNubj)}(h````h]h }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh for I/O access. If you use the }(hj hhhNhNubh)}(h:c:func:`mdelay()`h]j)}(hj h]hmdelay()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmdelayuh1hhhhMhj ubh or }(hj hhhNhNubh)}(h:c:func:`udelay()`h]j)}(hj h]hudelay()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjudelayuh1hhhhMhj ubh% functions, you’ll need to include }(hj hhhNhNubj)}(h````h]h}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh too.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjM hhubh)}(hThe ALSA interfaces like the PCM and control APIs are defined in other ```` header files. They have to be included after ````.h](hGThe ALSA interfaces like the PCM and control APIs are defined in other }(hj hhhNhNubj)}(h````h]h }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh. header files. They have to be included after }(hj hhhNhNubj)}(h````h]h}(hj, hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjM hhubeh}(h] header-filesah ]h"] header filesah$]h&]uh1hhjhhhhhMubeh}(h]basic-flow-for-pci-driversah ]h"]basic flow for pci driversah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h"Management of Cards and Componentsh]h"Management of Cards and Components}(hjW hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjT hhhhhMubh)}(hhh](h)}(h Card Instanceh]h Card Instance}(hjh hhhNhNubah}(h]h ]h"]h$]h&]uh1hhje hhhhhM ubh)}(h:For each soundcard, a “card” record must be allocated.h]h:For each soundcard, a “card” record must be allocated.}(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hje hhubh)}(hXA card record is the headquarters of the soundcard. It manages the whole list of devices (components) on the soundcard, such as PCM, mixers, MIDI, synthesizer, and so on. Also, the card record holds the ID and the name strings of the card, manages the root of proc files, and controls the power-management states and hotplug disconnections. The component list on the card record is used to manage the correct release of resources at destruction.h]hXA card record is the headquarters of the soundcard. It manages the whole list of devices (components) on the soundcard, such as PCM, mixers, MIDI, synthesizer, and so on. Also, the card record holds the ID and the name strings of the card, manages the root of proc files, and controls the power-management states and hotplug disconnections. The component list on the card record is used to manage the correct release of resources at destruction.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hje hhubh)}(hNAs mentioned above, to create a card instance, call :c:func:`snd_card_new()`::h](h4As mentioned above, to create a card instance, call }(hj hhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hj h]hsnd_card_new()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMhj ubh:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhje hhubj)}(hdstruct snd_card *card; int err; err = snd_card_new(&pci->dev, index, id, module, extra_size, &card);h]hdstruct snd_card *card; int err; err = snd_card_new(&pci->dev, index, id, module, extra_size, &card);}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhje hhubh)}(hXvThe function takes six arguments: the parent device pointer, the card-index number, the id string, the module pointer (usually ``THIS_MODULE``), the size of extra-data space, and the pointer to return the card instance. The extra_size argument is used to allocate card->private_data for the chip-specific data. Note that these data are allocated by :c:func:`snd_card_new()`.h](hThe function takes six arguments: the parent device pointer, the card-index number, the id string, the module pointer (usually }(hj hhhNhNubj)}(h``THIS_MODULE``h]h THIS_MODULE}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh), the size of extra-data space, and the pointer to return the card instance. The extra_size argument is used to allocate card->private_data for the chip-specific data. Note that these data are allocated by }(hj hhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hj h]hsnd_card_new()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhje hhubh)}(hThe first argument, the pointer of struct device, specifies the parent device. For PCI devices, typically ``&pci->`` is passed there.h](hjThe first argument, the pointer of struct device, specifies the parent device. For PCI devices, typically }(hj hhhNhNubj)}(h ``&pci->``h]h&pci->}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh is passed there.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM$hje hhubeh}(h] card-instanceah ]h"] card instanceah$]h&]uh1hhjT hhhhhM ubh)}(hhh](h)}(h Componentsh]h Components}(hj? hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj< hhhhhM(ubh)}(hX$After the card is created, you can attach the components (devices) to the card instance. In an ALSA driver, a component is represented as a struct snd_device object. A component can be a PCM instance, a control interface, a raw MIDI interface, etc. Each such instance has one component entry.h]hX$After the card is created, you can attach the components (devices) to the card instance. In an ALSA driver, a component is represented as a struct snd_device object. A component can be a PCM instance, a control interface, a raw MIDI interface, etc. Each such instance has one component entry.}(hjM hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM*hj< hhubh)}(hHA component can be created via the :c:func:`snd_device_new()` function::h](h#A component can be created via the }(hj[ hhhNhNubh)}(h:c:func:`snd_device_new()`h]j)}(hje h]hsnd_device_new()}(hjg hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjc ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_device_newuh1hhhhM0hj[ ubh function:}(hj[ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM0hj< hhubj)}(h0snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);h]h0snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhM3hj< hhubh)}(hXYThis takes the card pointer, the device-level (``SNDRV_DEV_XXX``), the data pointer, and the callback pointers (``&ops``). The device-level defines the type of components and the order of registration and de-registration. For most components, the device-level is already defined. For a user-defined component, you can use ``SNDRV_DEV_LOWLEVEL``.h](h/This takes the card pointer, the device-level (}(hj hhhNhNubj)}(h``SNDRV_DEV_XXX``h]h SNDRV_DEV_XXX}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh0), the data pointer, and the callback pointers (}(hj hhhNhNubj)}(h``&ops``h]h&ops}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh). The device-level defines the type of components and the order of registration and de-registration. For most components, the device-level is already defined. For a user-defined component, you can use }(hj hhhNhNubj)}(h``SNDRV_DEV_LOWLEVEL``h]hSNDRV_DEV_LOWLEVEL}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM5hj< hhubh)}(hThis function itself doesn't allocate the data space. The data must be allocated manually beforehand, and its pointer is passed as the argument. This pointer (``chip`` in the above example) is used as the identifier for the instance.h](hThis function itself doesn’t allocate the data space. The data must be allocated manually beforehand, and its pointer is passed as the argument. This pointer (}(hj hhhNhNubj)}(h``chip``h]hchip}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubhB in the above example) is used as the identifier for the instance.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM<hj< hhubh)}(hXEach pre-defined ALSA component such as AC97 and PCM calls :c:func:`snd_device_new()` inside its constructor. The destructor for each component is defined in the callback pointers. Hence, you don't need to take care of calling a destructor for such a component.h](h;Each pre-defined ALSA component such as AC97 and PCM calls }(hj hhhNhNubh)}(h:c:func:`snd_device_new()`h]j)}(hj h]hsnd_device_new()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_device_newuh1hhhhMAhj ubh inside its constructor. The destructor for each component is defined in the callback pointers. Hence, you don’t need to take care of calling a destructor for such a component.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMAhj< hhubh)}(hXIf you wish to create your own component, you need to set the destructor function to the dev_free callback in the ``ops``, so that it can be released automatically via :c:func:`snd_card_free()`. The next example will show an implementation of chip-specific data.h](hrIf you wish to create your own component, you need to set the destructor function to the dev_free callback in the }(hj/ hhhNhNubj)}(h``ops``h]hops}(hj7 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj/ ubh/, so that it can be released automatically via }(hj/ hhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hjK h]hsnd_card_free()}(hjM hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjI ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMFhj/ ubhE. The next example will show an implementation of chip-specific data.}(hj/ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMFhj< hhubeh}(h] componentsah ]h"] componentsah$]h&]uh1hhjT hhhhhM(ubh)}(hhh](h)}(hChip-Specific Datah]hChip-Specific Data}(hj} hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjz hhhhhMLubh)}(hChip-specific information, e.g. the I/O port address, its resource pointer, or the irq number, is stored in the chip-specific record::h]hChip-specific information, e.g. the I/O port address, its resource pointer, or the irq number, is stored in the chip-specific record:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMNhjz hhubj)}(hstruct mychip { .... };h]hstruct mychip { .... };}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMQhjz hhubh)}(h=In general, there are two ways of allocating the chip record.h]h=In general, there are two ways of allocating the chip record.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMVhjz hhubh)}(hhh](h)}(h+1. Allocating via :c:func:`snd_card_new()`.h](h1. Allocating via }(hj hhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hj h]hsnd_card_new()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMXhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhj hhhhhMYubh)}(hnAs mentioned above, you can pass the extra-data-length to the 5th argument of :c:func:`snd_card_new()`, e.g.::h](hNAs mentioned above, you can pass the extra-data-length to the 5th argument of }(hj hhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hj h]hsnd_card_new()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhM[hj ubh, e.g.:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM[hj hhubj)}(hqerr = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct mychip), &card);h]hqerr = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct mychip), &card);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM^hj hhubh)}(h-struct mychip is the type of the chip record.h]h-struct mychip is the type of the chip record.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMahj hhubh)}(h2In return, the allocated record can be accessed ash]h2In return, the allocated record can be accessed as}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMchj hhubj)}(h)struct mychip *chip = card->private_data;h]h)struct mychip *chip = card->private_data;}hjDsbah}(h]h ]h"]h$]h&]jjuh1jhhhMghj hhubh)}(hkWith this method, you don't have to allocate twice. The record is released together with the card instance.h]hmWith this method, you don’t have to allocate twice. The record is released together with the card instance.}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMihj hhubeh}(h]allocating-via-snd-card-newah ]h"]!1. allocating via snd_card_new().ah$]h&]uh1hhjz hhhhhMYubh)}(hhh](h)}(h2. Allocating an extra device.h]h2. Allocating an extra device.}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhhMmubh)}(huAfter allocating a card instance via :c:func:`snd_card_new()` (with ``0`` on the 4th arg), call :c:func:`kzalloc()`::h](h%After allocating a card instance via }(hjyhhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hjh]hsnd_card_new()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMohjyubh (with }(hjyhhhNhNubj)}(h``0``h]h0}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjyubh on the 4th arg), call }(hjyhhhNhNubh)}(h:c:func:`kzalloc()`h]j)}(hjh]h kzalloc()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjkzallocuh1hhhhMohjyubh:}(hjyhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMohjhhhubj)}(hstruct snd_card *card; struct mychip *chip; err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); ..... chip = kzalloc(sizeof(*chip), GFP_KERNEL);h]hstruct snd_card *card; struct mychip *chip; err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); ..... chip = kzalloc(sizeof(*chip), GFP_KERNEL);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMrhjhhhubh)}(hHThe chip record should have the field to hold the card pointer at least,h]hHThe chip record should have the field to hold the card pointer at least,}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMyhjhhhubj)}(h>struct mychip { struct snd_card *card; .... };h]h>struct mychip { struct snd_card *card; .... };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM}hjhhhubh)}(h:Then, set the card pointer in the returned chip instance::h]h9Then, set the card pointer in the returned chip instance:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubj)}(hchip->card = card;h]hchip->card = card;}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhhubh)}(hkNext, initialize the fields, and register this chip record as a low-level device with a specified ``ops``::h](hbNext, initialize the fields, and register this chip record as a low-level device with a specified p}(hj%hhhNhNubj)}(h``ops``h]hops}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubh:}(hj%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubj)}(hstatic const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; .... snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);h]hstatic const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; .... snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);}hjEsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhhubh)}(hh:c:func:`snd_mychip_dev_free()` is the device-destructor function, which will call the real destructor::h](h)}(h:c:func:`snd_mychip_dev_free()`h]j)}(hjYh]hsnd_mychip_dev_free()}(hj[hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjWubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mychip_dev_freeuh1hhhhMhjSubhH is the device-destructor function, which will call the real destructor:}(hjShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubj)}(hrstatic int snd_mychip_dev_free(struct snd_device *device) { return snd_mychip_free(device->device_data); }h]hrstatic int snd_mychip_dev_free(struct snd_device *device) { return snd_mychip_free(device->device_data); }}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhhubh)}(h9where :c:func:`snd_mychip_free()` is the real destructor.h](hwhere }(hjhhhNhNubh)}(h:c:func:`snd_mychip_free()`h]j)}(hjh]hsnd_mychip_free()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mychip_freeuh1hhhhMhjubh is the real destructor.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubh)}(hXThe demerit of this method is the obviously larger amount of code. The merit is, however, that you can trigger your own callback at registering and disconnecting the card via a setting in snd_device_ops. About registering and disconnecting the card, see the subsections below.h]hXThe demerit of this method is the obviously larger amount of code. The merit is, however, that you can trigger your own callback at registering and disconnecting the card via a setting in snd_device_ops. About registering and disconnecting the card, see the subsections below.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubeh}(h]allocating-an-extra-deviceah ]h"]2. allocating an extra device.ah$]h&]uh1hhjz hhhhhMmubeh}(h]chip-specific-dataah ]h"]chip-specific dataah$]h&]uh1hhjT hhhhhMLubh)}(hhh](h)}(hRegistration and Releaseh]hRegistration and Release}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXtAfter all components are assigned, register the card instance by calling :c:func:`snd_card_register()`. Access to the device files is enabled at this point. That is, before :c:func:`snd_card_register()` is called, the components are safely inaccessible from external side. If this call fails, exit the probe function after releasing the card via :c:func:`snd_card_free()`.h](hIAfter all components are assigned, register the card instance by calling }(hjhhhNhNubh)}(h:c:func:`snd_card_register()`h]j)}(hjh]hsnd_card_register()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_registeruh1hhhhMhjubhG. Access to the device files is enabled at this point. That is, before }(hjhhhNhNubh)}(h:c:func:`snd_card_register()`h]j)}(hjh]hsnd_card_register()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_registeruh1hhhhMhjubh is called, the components are safely inaccessible from external side. If this call fails, exit the probe function after releasing the card via }(hjhhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hj>h]hsnd_card_free()}(hj@hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hFor releasing the card instance, you can call simply :c:func:`snd_card_free()`. As mentioned earlier, all components are released automatically by this call.h](h5For releasing the card instance, you can call simply }(hjehhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hjoh]hsnd_card_free()}(hjqhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjmubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMhjeubhO. As mentioned earlier, all components are released automatically by this call.}(hjehhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hFor a device which allows hotplugging, you can use :c:func:`snd_card_free_when_closed()`. This one will postpone the destruction until all devices are closed.h](h3For a device which allows hotplugging, you can use }(hjhhhNhNubh)}(h%:c:func:`snd_card_free_when_closed()`h]j)}(hjh]hsnd_card_free_when_closed()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_free_when_closeduh1hhhhMhjubhF. This one will postpone the destruction until all devices are closed.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]registration-and-releaseah ]h"]registration and releaseah$]h&]uh1hhjT hhhhhMubeh}(h]jah ]h"]"management of cards and componentsah$]h&]uh1hhhhhhhhMjKubh)}(hhh](h)}(hPCI Resource Managementh]hPCI Resource Management}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hFull Code Exampleh]hFull Code Example}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hIn this section, we'll complete the chip-specific constructor, destructor and PCI entries. Example code is shown first, below::h]hIn this section, we’ll complete the chip-specific constructor, destructor and PCI entries. Example code is shown first, below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hXc struct mychip { struct snd_card *card; struct pci_dev *pci; unsigned long port; int irq; }; static int snd_mychip_free(struct mychip *chip) { /* disable hardware here if any */ .... /* (not implemented in this document) */ /* release the irq */ if (chip->irq >= 0) free_irq(chip->irq, chip); /* release the I/O ports & memory */ pci_release_regions(chip->pci); /* disable the PCI entry */ pci_disable_device(chip->pci); /* release the data */ kfree(chip); return 0; } /* chip-specific constructor */ static int snd_mychip_create(struct snd_card *card, struct pci_dev *pci, struct mychip **rchip) { struct mychip *chip; int err; static const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; *rchip = NULL; /* initialize the PCI entry */ err = pci_enable_device(pci); if (err < 0) return err; /* check PCI availability (28bit DMA) */ if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 || pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) { printk(KERN_ERR "error to set 28bit mask DMA\n"); pci_disable_device(pci); return -ENXIO; } chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) { pci_disable_device(pci); return -ENOMEM; } /* initialize the stuff */ chip->card = card; chip->pci = pci; chip->irq = -1; /* (1) PCI resource allocation */ err = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); pci_disable_device(pci); return err; } chip->port = pci_resource_start(pci, 0); if (request_irq(pci->irq, snd_mychip_interrupt, IRQF_SHARED, KBUILD_MODNAME, chip)) { printk(KERN_ERR "cannot grab irq %d\n", pci->irq); snd_mychip_free(chip); return -EBUSY; } chip->irq = pci->irq; card->sync_irq = chip->irq; /* (2) initialization of the chip hardware */ .... /* (not implemented in this document) */ err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); if (err < 0) { snd_mychip_free(chip); return err; } *rchip = chip; return 0; } /* PCI IDs */ static struct pci_device_id snd_mychip_ids[] = { { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, .... { 0, } }; MODULE_DEVICE_TABLE(pci, snd_mychip_ids); /* pci_driver definition */ static struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_mychip_ids, .probe = snd_mychip_probe, .remove = snd_mychip_remove, }; /* module initialization */ static int __init alsa_card_mychip_init(void) { return pci_register_driver(&driver); } /* module clean up */ static void __exit alsa_card_mychip_exit(void) { pci_unregister_driver(&driver); } module_init(alsa_card_mychip_init) module_exit(alsa_card_mychip_exit) EXPORT_NO_SYMBOLS; /* for old kernels only */h]hXc struct mychip { struct snd_card *card; struct pci_dev *pci; unsigned long port; int irq; }; static int snd_mychip_free(struct mychip *chip) { /* disable hardware here if any */ .... /* (not implemented in this document) */ /* release the irq */ if (chip->irq >= 0) free_irq(chip->irq, chip); /* release the I/O ports & memory */ pci_release_regions(chip->pci); /* disable the PCI entry */ pci_disable_device(chip->pci); /* release the data */ kfree(chip); return 0; } /* chip-specific constructor */ static int snd_mychip_create(struct snd_card *card, struct pci_dev *pci, struct mychip **rchip) { struct mychip *chip; int err; static const struct snd_device_ops ops = { .dev_free = snd_mychip_dev_free, }; *rchip = NULL; /* initialize the PCI entry */ err = pci_enable_device(pci); if (err < 0) return err; /* check PCI availability (28bit DMA) */ if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 || pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) { printk(KERN_ERR "error to set 28bit mask DMA\n"); pci_disable_device(pci); return -ENXIO; } chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) { pci_disable_device(pci); return -ENOMEM; } /* initialize the stuff */ chip->card = card; chip->pci = pci; chip->irq = -1; /* (1) PCI resource allocation */ err = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); pci_disable_device(pci); return err; } chip->port = pci_resource_start(pci, 0); if (request_irq(pci->irq, snd_mychip_interrupt, IRQF_SHARED, KBUILD_MODNAME, chip)) { printk(KERN_ERR "cannot grab irq %d\n", pci->irq); snd_mychip_free(chip); return -EBUSY; } chip->irq = pci->irq; card->sync_irq = chip->irq; /* (2) initialization of the chip hardware */ .... /* (not implemented in this document) */ err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops); if (err < 0) { snd_mychip_free(chip); return err; } *rchip = chip; return 0; } /* PCI IDs */ static struct pci_device_id snd_mychip_ids[] = { { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, .... { 0, } }; MODULE_DEVICE_TABLE(pci, snd_mychip_ids); /* pci_driver definition */ static struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_mychip_ids, .probe = snd_mychip_probe, .remove = snd_mychip_remove, }; /* module initialization */ static int __init alsa_card_mychip_init(void) { return pci_register_driver(&driver); } /* module clean up */ static void __exit alsa_card_mychip_exit(void) { pci_unregister_driver(&driver); } module_init(alsa_card_mychip_init) module_exit(alsa_card_mychip_exit) EXPORT_NO_SYMBOLS; /* for old kernels only */}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubeh}(h]id1ah ]h"]h$]jah&]uh1hhjhhhhhMjKubh)}(hhh](h)}(h Some Hafta'sh]hSome Hafta’s}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM:ubh)}(hThe allocation of PCI resources is done in the ``probe`` function, and usually an extra :c:func:`xxx_create()` function is written for this purpose.h](h/The allocation of PCI resources is done in the }(hj,hhhNhNubj)}(h ``probe``h]hprobe}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubh function, and usually an extra }(hj,hhhNhNubh)}(h:c:func:`xxx_create()`h]j)}(hjHh]h xxx_create()}(hjJhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjFubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj xxx_createuh1hhhhM<hj,ubh& function is written for this purpose.}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM<hjhhubh)}(hXIn the case of PCI devices, you first have to call the :c:func:`pci_enable_device()` function before allocating resources. Also, you need to set the proper PCI DMA mask to limit the accessed I/O range. In some cases, you might need to call :c:func:`pci_set_master()` function, too.h](h7In the case of PCI devices, you first have to call the }(hjohhhNhNubh)}(h:c:func:`pci_enable_device()`h]j)}(hjyh]hpci_enable_device()}(hj{hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjwubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_enable_deviceuh1hhhhM@hjoubh function before allocating resources. Also, you need to set the proper PCI DMA mask to limit the accessed I/O range. In some cases, you might need to call }(hjohhhNhNubh)}(h:c:func:`pci_set_master()`h]j)}(hjh]hpci_set_master()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_set_masteruh1hhhhM@hjoubh function, too.}(hjohhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM@hjhhubh)}(hport = pci_resource_start(pci, 0);h]herr = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); pci_disable_device(pci); return err; } chip->port = pci_resource_start(pci, 0);}hjasbah}(h]h ]h"]h$]h&]jjuh1jhhhMphjhhubh)}(hX-It will reserve the I/O port region of 8 bytes of the given PCI device. The returned value, ``chip->res_port``, is allocated via :c:func:`kmalloc()` by :c:func:`request_region()`. The pointer must be released via :c:func:`kfree()`, but there is a problem with this. This issue will be explained later.h](h\It will reserve the I/O port region of 8 bytes of the given PCI device. The returned value, }(hjohhhNhNubj)}(h``chip->res_port``h]hchip->res_port}(hjwhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh, is allocated via }(hjohhhNhNubh)}(h:c:func:`kmalloc()`h]j)}(hjh]h kmalloc()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjkmallocuh1hhhhMxhjoubh by }(hjohhhNhNubh)}(h:c:func:`request_region()`h]j)}(hjh]hrequest_region()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjrequest_regionuh1hhhhMxhjoubh#. The pointer must be released via }(hjohhhNhNubh)}(h:c:func:`kfree()`h]j)}(hjh]hkfree()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjkfreeuh1hhhhMxhjoubhG, but there is a problem with this. This issue will be explained later.}(hjohhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMxhjhhubh)}(h9The allocation of an interrupt source is done like this::h]h8The allocation of an interrupt source is done like this:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM~hjhhubj)}(hif (request_irq(pci->irq, snd_mychip_interrupt, IRQF_SHARED, KBUILD_MODNAME, chip)) { printk(KERN_ERR "cannot grab irq %d\n", pci->irq); snd_mychip_free(chip); return -EBUSY; } chip->irq = pci->irq;h]hif (request_irq(pci->irq, snd_mychip_interrupt, IRQF_SHARED, KBUILD_MODNAME, chip)) { printk(KERN_ERR "cannot grab irq %d\n", pci->irq); snd_mychip_free(chip); return -EBUSY; } chip->irq = pci->irq;}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hwhere :c:func:`snd_mychip_interrupt()` is the interrupt handler defined `later `__. Note that ``chip->irq`` should be defined only when :c:func:`request_irq()` succeeded.h](hwhere }(hjhhhNhNubh)}(h :c:func:`snd_mychip_interrupt()`h]j)}(hjh]hsnd_mychip_interrupt()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mychip_interruptuh1hhhhMhjubh" is the interrupt handler defined }(hjhhhNhNubh)}(h"`later `__h]hlater}(hj?hhhNhNubah}(h]h ]h"]h$]h&]namelaterjpcm-interrupt-handleruh1hhjjKubh . Note that }(hjhhhNhNubj)}(h ``chip->irq``h]h chip->irq}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh should be defined only when }(hjhhhNhNubh)}(h:c:func:`request_irq()`h]j)}(hjhh]h request_irq()}(hjjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjfubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj request_irquh1hhhhMhjubh succeeded.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hyOn the PCI bus, interrupts can be shared. Thus, ``IRQF_SHARED`` is used as the interrupt flag of :c:func:`request_irq()`.h](h0On the PCI bus, interrupts can be shared. Thus, }(hjhhhNhNubj)}(h``IRQF_SHARED``h]h IRQF_SHARED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh" is used as the interrupt flag of }(hjhhhNhNubh)}(h:c:func:`request_irq()`h]j)}(hjh]h request_irq()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj request_irquh1hhhhMhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe last argument of :c:func:`request_irq()` is the data pointer passed to the interrupt handler. Usually, the chip-specific record is used for that, but you can use what you like, too.h](hThe last argument of }(hjhhhNhNubh)}(h:c:func:`request_irq()`h]j)}(hjh]h request_irq()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj request_irquh1hhhhMhjubh is the data pointer passed to the interrupt handler. Usually, the chip-specific record is used for that, but you can use what you like, too.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hI won't give details about the interrupt handler at this point, but at least its appearance can be explained now. The interrupt handler looks usually as follows::h]hI won’t give details about the interrupt handler at this point, but at least its appearance can be explained now. The interrupt handler looks usually as follows:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hstatic irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; .... return IRQ_HANDLED; }h]hstatic irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; .... return IRQ_HANDLED; }}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hIAfter requesting the IRQ, you can passed it to ``card->sync_irq`` field::h](h/After requesting the IRQ, you can passed it to }(hjhhhNhNubj)}(h``card->sync_irq``h]hcard->sync_irq}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh field:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hcard->irq = chip->irq;h]hcard->irq = chip->irq;}hj?sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hThis allows the PCM core to automatically call :c:func:`synchronize_irq()` at the right time, like before ``hw_free``. See the later section `sync_stop callback`_ for details.h](h/This allows the PCM core to automatically call }(hjMhhhNhNubh)}(h:c:func:`synchronize_irq()`h]j)}(hjWh]hsynchronize_irq()}(hjYhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjUubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsynchronize_irquh1hhhhMhjMubh at the right time, like before }(hjMhhhNhNubj)}(h ``hw_free``h]hhw_free}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh. See the later section }(hjMhhhNhNubh)}(h`sync_stop callback`_h]hsync_stop callback}(hjhhhNhNubah}(h]h ]h"]h$]h&]namesync_stop callbackjsync-stop-callbackuh1hhjMjKubh for details.}(hjMhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hNow let's write the corresponding destructor for the resources above. The role of destructor is simple: disable the hardware (if already activated) and release the resources. So far, we have no hardware part, so the disabling code is not written here.h]hNow let’s write the corresponding destructor for the resources above. The role of destructor is simple: disable the hardware (if already activated) and release the resources. So far, we have no hardware part, so the disabling code is not written here.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hnTo release the resources, the “check-and-release” method is a safer way. For the interrupt, do like this::h]hmTo release the resources, the “check-and-release” method is a safer way. For the interrupt, do like this:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h6if (chip->irq >= 0) free_irq(chip->irq, chip);h]h6if (chip->irq >= 0) free_irq(chip->irq, chip);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hSince the irq number can start from 0, you should initialize ``chip->irq`` with a negative value (e.g. -1), so that you can check the validity of the irq number as above.h](h=Since the irq number can start from 0, you should initialize }(hjhhhNhNubj)}(h ``chip->irq``h]h chip->irq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh` with a negative value (e.g. -1), so that you can check the validity of the irq number as above.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXWhen you requested I/O ports or memory regions via :c:func:`pci_request_region()` or :c:func:`pci_request_regions()` like in this example, release the resource(s) using the corresponding function, :c:func:`pci_release_region()` or :c:func:`pci_release_regions()`::h](h3When you requested I/O ports or memory regions via }(hjhhhNhNubh)}(h:c:func:`pci_request_region()`h]j)}(hjh]hpci_request_region()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_request_regionuh1hhhhMhjubh or }(hjhhhNhNubh)}(h:c:func:`pci_request_regions()`h]j)}(hjh]hpci_request_regions()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_request_regionsuh1hhhhMhjubhQ like in this example, release the resource(s) using the corresponding function, }(hjhhhNhNubh)}(h:c:func:`pci_release_region()`h]j)}(hj?h]hpci_release_region()}(hjAhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj=ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_release_regionuh1hhhhMhjubh or }hjsbh)}(h:c:func:`pci_release_regions()`h]j)}(hjbh]hpci_release_regions()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj`ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_release_regionsuh1hhhhMhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hpci_release_regions(chip->pci);h]hpci_release_regions(chip->pci);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hXWhen you requested manually via :c:func:`request_region()` or :c:func:`request_mem_region()`, you can release it via :c:func:`release_resource()`. Suppose that you keep the resource pointer returned from :c:func:`request_region()` in chip->res_port, the release procedure looks like::h](h When you requested manually via }(hjhhhNhNubh)}(h:c:func:`request_region()`h]j)}(hjh]hrequest_region()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjrequest_regionuh1hhhhMhjubh or }(hjhhhNhNubh)}(h:c:func:`request_mem_region()`h]j)}(hjh]hrequest_mem_region()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjrequest_mem_regionuh1hhhhMhjubh, you can release it via }(hjhhhNhNubh)}(h:c:func:`release_resource()`h]j)}(hjh]hrelease_resource()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjrelease_resourceuh1hhhhMhjubh;. Suppose that you keep the resource pointer returned from }(hjhhhNhNubh)}(h:c:func:`request_region()`h]j)}(hj h]hrequest_region()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjrequest_regionuh1hhhhMhjubh5 in chip->res_port, the release procedure looks like:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h*release_and_free_resource(chip->res_port);h]h*release_and_free_resource(chip->res_port);}hj1sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hCDon't forget to call :c:func:`pci_disable_device()` before the end.h](hDon’t forget to call }(hj?hhhNhNubh)}(h:c:func:`pci_disable_device()`h]j)}(hjIh]hpci_disable_device()}(hjKhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjGubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjpci_disable_deviceuh1hhhhMhj?ubh before the end.}(hj?hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h/And finally, release the chip-specific record::h]h.And finally, release the chip-specific record:}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h kfree(chip);h]h kfree(chip);}hj~sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hXWe didn't implement the hardware disabling part above. If you need to do this, please note that the destructor may be called even before the initialization of the chip is completed. It would be better to have a flag to skip hardware disabling if the hardware was not initialized yet.h]hXWe didn’t implement the hardware disabling part above. If you need to do this, please note that the destructor may be called even before the initialization of the chip is completed. It would be better to have a flag to skip hardware disabling if the hardware was not initialized yet.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hXEWhen the chip-data is assigned to the card using :c:func:`snd_device_new()` with ``SNDRV_DEV_LOWLELVEL``, its destructor is called last. That is, it is assured that all other components like PCMs and controls have already been released. You don't have to stop PCMs, etc. explicitly, but just call low-level hardware stopping.h](h1When the chip-data is assigned to the card using }(hjhhhNhNubh)}(h:c:func:`snd_device_new()`h]j)}(hjh]hsnd_device_new()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_device_newuh1hhhhMhjubh with }(hjhhhNhNubj)}(h``SNDRV_DEV_LOWLELVEL``h]hSNDRV_DEV_LOWLELVEL}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, its destructor is called last. That is, it is assured that all other components like PCMs and controls have already been released. You don’t have to stop PCMs, etc. explicitly, but just call low-level hardware stopping.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hThe management of a memory-mapped region is almost as same as the management of an I/O port. You'll need two fields as follows::h]hThe management of a memory-mapped region is almost as same as the management of an I/O port. You’ll need two fields as follows:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hestruct mychip { .... unsigned long iobase_phys; void __iomem *iobase_virt; };h]hestruct mychip { .... unsigned long iobase_phys; void __iomem *iobase_virt; };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(h*and the allocation would look like below::h]h)and the allocation would look like below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hXerr = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); return err; } chip->iobase_phys = pci_resource_start(pci, 0); chip->iobase_virt = ioremap(chip->iobase_phys, pci_resource_len(pci, 0));h]hXerr = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); return err; } chip->iobase_phys = pci_resource_start(pci, 0); chip->iobase_virt = ioremap(chip->iobase_phys, pci_resource_len(pci, 0));}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(h+and the corresponding destructor would be::h]h*and the corresponding destructor would be:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hstatic int snd_mychip_free(struct mychip *chip) { .... if (chip->iobase_virt) iounmap(chip->iobase_virt); .... pci_release_regions(chip->pci); .... }h]hstatic int snd_mychip_free(struct mychip *chip) { .... if (chip->iobase_virt) iounmap(chip->iobase_virt); .... pci_release_regions(chip->pci); .... }}hj#sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hWOf course, a modern way with :c:func:`pci_iomap()` will make things a bit easier, too::h](hOf course, a modern way with }(hj1hhhNhNubh)}(h:c:func:`pci_iomap()`h]j)}(hj;h]h pci_iomap()}(hj=hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj9ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj pci_iomapuh1hhhhMhj1ubh$ will make things a bit easier, too:}(hj1hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(herr = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); return err; } chip->iobase_virt = pci_iomap(pci, 0, 0);h]herr = pci_request_regions(pci, "My Chip"); if (err < 0) { kfree(chip); return err; } chip->iobase_virt = pci_iomap(pci, 0, 0);}hjbsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(h;which is paired with :c:func:`pci_iounmap()` at destructor.h](hwhich is paired with }(hjphhhNhNubh)}(h:c:func:`pci_iounmap()`h]j)}(hjzh]h pci_iounmap()}(hj|hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjxubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj pci_iounmapuh1hhhhMhjpubh at destructor.}(hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]resource-allocationah ]h"]resource allocationah$]h&]uh1hhjhhhhhMTubh)}(hhh](h)}(h PCI Entriesh]h PCI Entries}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM ubh)}(hSo far, so good. Let's finish the missing PCI stuff. At first, we need a struct pci_device_id table for this chipset. It's a table of PCI vendor/device ID number, and some masks.h]hSo far, so good. Let’s finish the missing PCI stuff. At first, we need a struct pci_device_id table for this chipset. It’s a table of PCI vendor/device ID number, and some masks.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubh)}(h For example::h]h For example:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hstatic struct pci_device_id snd_mychip_ids[] = { { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, .... { 0, } }; MODULE_DEVICE_TABLE(pci, snd_mychip_ids);h]hstatic struct pci_device_id snd_mychip_ids[] = { { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, .... { 0, } }; MODULE_DEVICE_TABLE(pci, snd_mychip_ids);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hXThe first and second fields of the struct pci_device_id are the vendor and device IDs. If you have no reason to filter the matching devices, you can leave the remaining fields as above. The last field of the struct pci_device_id contains private data for this entry. You can specify any value here, for example, to define specific operations for supported device IDs. Such an example is found in the intel8x0 driver.h]hXThe first and second fields of the struct pci_device_id are the vendor and device IDs. If you have no reason to filter the matching devices, you can leave the remaining fields as above. The last field of the struct pci_device_id contains private data for this entry. You can specify any value here, for example, to define specific operations for supported device IDs. Such an example is found in the intel8x0 driver.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hTThe last entry of this list is the terminator. You must specify this all-zero entry.h]hTThe last entry of this list is the terminator. You must specify this all-zero entry.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM"hjhhubh)}(h,Then, prepare the struct pci_driver record::h]h+Then, prepare the struct pci_driver record:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hjhhubj)}(hstatic struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_mychip_ids, .probe = snd_mychip_probe, .remove = snd_mychip_remove, };h]hstatic struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_mychip_ids, .probe = snd_mychip_probe, .remove = snd_mychip_remove, };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM(hjhhubh)}(hThe ``probe`` and ``remove`` functions have already been defined in the previous sections. The ``name`` field is the name string of this device. Note that you must not use slashes (“/”) in this string.h](hThe }(hjhhhNhNubj)}(h ``probe``h]hprobe}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h ``remove``h]hremove}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhC functions have already been defined in the previous sections. The }(hjhhhNhNubj)}(h``name``h]hname}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhf field is the name string of this device. Note that you must not use slashes (“/”) in this string.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM/hjhhubh)}(h!And at last, the module entries::h]h And at last, the module entries:}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM3hjhhubj)}(hXstatic int __init alsa_card_mychip_init(void) { return pci_register_driver(&driver); } static void __exit alsa_card_mychip_exit(void) { pci_unregister_driver(&driver); } module_init(alsa_card_mychip_init) module_exit(alsa_card_mychip_exit)h]hXstatic int __init alsa_card_mychip_init(void) { return pci_register_driver(&driver); } static void __exit alsa_card_mychip_exit(void) { pci_unregister_driver(&driver); } module_init(alsa_card_mychip_init) module_exit(alsa_card_mychip_exit)}hjnsbah}(h]h ]h"]h$]h&]jjuh1jhhhM5hjhhubh)}(hRNote that these module entries are tagged with ``__init`` and ``__exit`` prefixes.h](h/Note that these module entries are tagged with }(hj|hhhNhNubj)}(h ``__init``h]h__init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|ubh and }(hj|hhhNhNubj)}(h ``__exit``h]h__exit}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|ubh prefixes.}(hj|hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMBhjhhubh)}(h That's all!h]h That’s all!}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMEhjhhubeh}(h]jah ]h"] pci entriesah$]h&]uh1hhjhhhhhM jKubeh}(h]j$ah ]h"]pci resource managementah$]h&]uh1hhhhhhhhMjKubh)}(hhh](h)}(h PCM Interfaceh]h PCM Interface}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMHubh)}(hhh](h)}(hGeneralh]hGeneral}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMKubh)}(hThe PCM middle layer of ALSA is quite powerful and it is only necessary for each driver to implement the low-level functions to access its hardware.h]hThe PCM middle layer of ALSA is quite powerful and it is only necessary for each driver to implement the low-level functions to access its hardware.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubh)}(hTo access the PCM layer, you need to include ```` first. In addition, ```` might be needed if you access some functions related with hw_param.h](h-To access the PCM layer, you need to include }(hjhhhNhNubj)}(h````h]h }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh first. In addition, }(hjhhhNhNubj)}(h````h]h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhD might be needed if you access some functions related with hw_param.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMQhjhhubh)}(hXEach card device can have up to four PCM instances. A PCM instance corresponds to a PCM device file. The limitation of number of instances comes only from the available bit size of Linux' device numbers. Once 64bit device numbers are used, we'll have more PCM instances available.h]hXEach card device can have up to four PCM instances. A PCM instance corresponds to a PCM device file. The limitation of number of instances comes only from the available bit size of Linux’ device numbers. Once 64bit device numbers are used, we’ll have more PCM instances available.}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMUhjhhubh)}(hX\A PCM instance consists of PCM playback and capture streams, and each PCM stream consists of one or more PCM substreams. Some soundcards support multiple playback functions. For example, emu10k1 has a PCM playback of 32 stereo substreams. In this case, at each open, a free substream is (usually) automatically chosen and opened. Meanwhile, when only one substream exists and it was already opened, a subsequent open will either block or error with ``EAGAIN`` according to the file open mode. But you don't have to care about such details in your driver. The PCM middle layer will take care of such work.h](hXA PCM instance consists of PCM playback and capture streams, and each PCM stream consists of one or more PCM substreams. Some soundcards support multiple playback functions. For example, emu10k1 has a PCM playback of 32 stereo substreams. In this case, at each open, a free substream is (usually) automatically chosen and opened. Meanwhile, when only one substream exists and it was already opened, a subsequent open will either block or error with }(hj:hhhNhNubj)}(h ``EAGAIN``h]hEAGAIN}(hjBhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj:ubh according to the file open mode. But you don’t have to care about such details in your driver. The PCM middle layer will take care of such work.}(hj:hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM[hjhhubeh}(h]id2ah ]h"]h$]jah&]uh1hhjhhhhhMKjKubh)}(hhh](h)}(hFull Code Exampleh]hFull Code Example}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjahhhhhMfubh)}(hThe example code below does not include any hardware access routines but shows only the skeleton, how to build up the PCM interfaces::h]hThe example code below does not include any hardware access routines but shows only the skeleton, how to build up the PCM interfaces:}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhhjahhubj)}(hX}#include .... /* hardware definition */ static struct snd_pcm_hardware snd_mychip_playback_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, }; /* hardware definition */ static struct snd_pcm_hardware snd_mychip_capture_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, }; /* open callback */ static int snd_mychip_playback_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_playback_hw; /* more hardware-initialization will be done here */ .... return 0; } /* close callback */ static int snd_mychip_playback_close(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); /* the hardware-specific codes will be here */ .... return 0; } /* open callback */ static int snd_mychip_capture_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_capture_hw; /* more hardware-initialization will be done here */ .... return 0; } /* close callback */ static int snd_mychip_capture_close(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); /* the hardware-specific codes will be here */ .... return 0; } /* hw_params callback */ static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { /* the hardware-specific codes will be here */ .... return 0; } /* hw_free callback */ static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream) { /* the hardware-specific codes will be here */ .... return 0; } /* prepare callback */ static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; /* set up the hardware with the current configuration * for example... */ mychip_set_sample_format(chip, runtime->format); mychip_set_sample_rate(chip, runtime->rate); mychip_set_channels(chip, runtime->channels); mychip_set_dma_setup(chip, runtime->dma_addr, chip->buffer_size, chip->period_size); return 0; } /* trigger callback */ static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream, int cmd) { switch (cmd) { case SNDRV_PCM_TRIGGER_START: /* do something to start the PCM engine */ .... break; case SNDRV_PCM_TRIGGER_STOP: /* do something to stop the PCM engine */ .... break; default: return -EINVAL; } } /* pointer callback */ static snd_pcm_uframes_t snd_mychip_pcm_pointer(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); unsigned int current_ptr; /* get the current hardware pointer */ current_ptr = mychip_get_hw_pointer(chip); return current_ptr; } /* operators */ static struct snd_pcm_ops snd_mychip_playback_ops = { .open = snd_mychip_playback_open, .close = snd_mychip_playback_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, }; /* operators */ static struct snd_pcm_ops snd_mychip_capture_ops = { .open = snd_mychip_capture_open, .close = snd_mychip_capture_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, }; /* * definitions of capture are omitted here... */ /* create a pcm device */ static int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; int err; err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); if (err < 0) return err; pcm->private_data = chip; strcpy(pcm->name, "My Chip"); chip->pcm = pcm; /* set operators */ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mychip_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mychip_capture_ops); /* pre-allocation of buffers */ /* NOTE: this may fail */ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 64*1024, 64*1024); return 0; }h]hX}#include .... /* hardware definition */ static struct snd_pcm_hardware snd_mychip_playback_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, }; /* hardware definition */ static struct snd_pcm_hardware snd_mychip_capture_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, }; /* open callback */ static int snd_mychip_playback_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_playback_hw; /* more hardware-initialization will be done here */ .... return 0; } /* close callback */ static int snd_mychip_playback_close(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); /* the hardware-specific codes will be here */ .... return 0; } /* open callback */ static int snd_mychip_capture_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_capture_hw; /* more hardware-initialization will be done here */ .... return 0; } /* close callback */ static int snd_mychip_capture_close(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); /* the hardware-specific codes will be here */ .... return 0; } /* hw_params callback */ static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params) { /* the hardware-specific codes will be here */ .... return 0; } /* hw_free callback */ static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream) { /* the hardware-specific codes will be here */ .... return 0; } /* prepare callback */ static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; /* set up the hardware with the current configuration * for example... */ mychip_set_sample_format(chip, runtime->format); mychip_set_sample_rate(chip, runtime->rate); mychip_set_channels(chip, runtime->channels); mychip_set_dma_setup(chip, runtime->dma_addr, chip->buffer_size, chip->period_size); return 0; } /* trigger callback */ static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream, int cmd) { switch (cmd) { case SNDRV_PCM_TRIGGER_START: /* do something to start the PCM engine */ .... break; case SNDRV_PCM_TRIGGER_STOP: /* do something to stop the PCM engine */ .... break; default: return -EINVAL; } } /* pointer callback */ static snd_pcm_uframes_t snd_mychip_pcm_pointer(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); unsigned int current_ptr; /* get the current hardware pointer */ current_ptr = mychip_get_hw_pointer(chip); return current_ptr; } /* operators */ static struct snd_pcm_ops snd_mychip_playback_ops = { .open = snd_mychip_playback_open, .close = snd_mychip_playback_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, }; /* operators */ static struct snd_pcm_ops snd_mychip_capture_ops = { .open = snd_mychip_capture_open, .close = snd_mychip_capture_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, }; /* * definitions of capture are omitted here... */ /* create a pcm device */ static int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; int err; err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); if (err < 0) return err; pcm->private_data = chip; strcpy(pcm->name, "My Chip"); chip->pcm = pcm; /* set operators */ snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mychip_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mychip_capture_ops); /* pre-allocation of buffers */ /* NOTE: this may fail */ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 64*1024, 64*1024); return 0; }}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMkhjahhubeh}(h]id3ah ]h"]h$]full code exampleah&]uh1hhjhhhhhMfjKubh)}(hhh](h)}(hPCM Constructorh]hPCM Constructor}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhM5ubh)}(hA PCM instance is allocated by the :c:func:`snd_pcm_new()` function. It would be better to create a constructor for the PCM, namely::h](h#A PCM instance is allocated by the }(hjhhhNhNubh)}(h:c:func:`snd_pcm_new()`h]j)}(hjh]h snd_pcm_new()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_pcm_newuh1hhhhM7hjubhJ function. It would be better to create a constructor for the PCM, namely:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM7hjhhubj)}(hXVstatic int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; int err; err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); if (err < 0) return err; pcm->private_data = chip; strcpy(pcm->name, "My Chip"); chip->pcm = pcm; ... return 0; }h]hXVstatic int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; int err; err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm); if (err < 0) return err; pcm->private_data = chip; strcpy(pcm->name, "My Chip"); chip->pcm = pcm; ... return 0; }}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM:hjhhubh)}(hThe :c:func:`snd_pcm_new()` function takes six arguments. The first argument is the card pointer to which this PCM is assigned, and the second is the ID string.h](hThe }(hjhhhNhNubh)}(h:c:func:`snd_pcm_new()`h]j)}(hjh]h snd_pcm_new()}(hjhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_pcm_newuh1hhhhMIhjubh function takes six arguments. The first argument is the card pointer to which this PCM is assigned, and the second is the ID string.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMIhjhhubh)}(hThe third argument (``index``, 0 in the above) is the index of this new PCM. It begins from zero. If you create more than one PCM instances, specify the different numbers in this argument. For example, ``index = 1`` for the second PCM device.h](hThe third argument (}(hjhhhNhNubj)}(h ``index``h]hindex}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh, 0 in the above) is the index of this new PCM. It begins from zero. If you create more than one PCM instances, specify the different numbers in this argument. For example, }(hjhhhNhNubj)}(h ``index = 1``h]h index = 1}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh for the second PCM device.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubh)}(hThe fourth and fifth arguments are the number of substreams for playback and capture, respectively. Here 1 is used for both arguments. When no playback or capture substreams are available, pass 0 to the corresponding argument.h]hThe fourth and fifth arguments are the number of substreams for playback and capture, respectively. Here 1 is used for both arguments. When no playback or capture substreams are available, pass 0 to the corresponding argument.}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMRhjhhubh)}(hX-If a chip supports multiple playbacks or captures, you can specify more numbers, but they must be handled properly in open/close, etc. callbacks. When you need to know which substream you are referring to, then it can be obtained from struct snd_pcm_substream data passed to each callback as follows::h]hX,If a chip supports multiple playbacks or captures, you can specify more numbers, but they must be handled properly in open/close, etc. callbacks. When you need to know which substream you are referring to, then it can be obtained from struct snd_pcm_substream data passed to each callback as follows:}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMWhjhhubj)}(hCstruct snd_pcm_substream *substream; int index = substream->number;h]hCstruct snd_pcm_substream *substream; int index = substream->number;}hjesbah}(h]h ]h"]h$]h&]jjuh1jhhhM]hjhhubh)}(hIAfter the PCM is created, you need to set operators for each PCM stream::h]hHAfter the PCM is created, you need to set operators for each PCM stream:}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMahjhhubj)}(hsnd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mychip_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mychip_capture_ops);h]hsnd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_mychip_playback_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_mychip_capture_ops);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMchjhhubh)}(h/The operators are defined typically like this::h]h.The operators are defined typically like this:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhhjhhubj)}(hX~static struct snd_pcm_ops snd_mychip_playback_ops = { .open = snd_mychip_pcm_open, .close = snd_mychip_pcm_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, };h]hX~static struct snd_pcm_ops snd_mychip_playback_ops = { .open = snd_mychip_pcm_open, .close = snd_mychip_pcm_close, .hw_params = snd_mychip_pcm_hw_params, .hw_free = snd_mychip_pcm_hw_free, .prepare = snd_mychip_pcm_prepare, .trigger = snd_mychip_pcm_trigger, .pointer = snd_mychip_pcm_pointer, };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMjhjhhubh)}(h=All the callbacks are described in the Operators_ subsection.h](h'All the callbacks are described in the }(hjhhhNhNubh)}(h Operators_h]h Operators}(hjhhhNhNubah}(h]h ]h"]h$]h&]name Operatorsj operatorsuh1hhjjKubh subsection.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMthjhhubh)}(hAfter setting the operators, you probably will want to pre-allocate the buffer and set up the managed allocation mode. For that, simply call the following::h]hAfter setting the operators, you probably will want to pre-allocate the buffer and set up the managed allocation mode. For that, simply call the following:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMvhjhhubj)}(hsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 64*1024, 64*1024);h]hsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &chip->pci->dev, 64*1024, 64*1024);}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMzhjhhubh)}(hIt will allocate a buffer up to 64kB by default. Buffer management details will be described in the later section `Buffer and Memory Management`_.h](hrIt will allocate a buffer up to 64kB by default. Buffer management details will be described in the later section }(hjhhhNhNubh)}(h`Buffer and Memory Management`_h]hBuffer and Memory Management}(hjhhhNhNubah}(h]h ]h"]h$]h&]nameBuffer and Memory Managementjbuffer-and-memory-managementuh1hhjjKubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM~hjhhubh)}(hX,Additionally, you can set some extra information for this PCM in ``pcm->info_flags``. The available values are defined as ``SNDRV_PCM_INFO_XXX`` in ````, which is used for the hardware definition (described later). When your soundchip supports only half-duplex, specify it like this::h](hAAdditionally, you can set some extra information for this PCM in }(hj hhhNhNubj)}(h``pcm->info_flags``h]hpcm->info_flags}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh&. The available values are defined as }(hj hhhNhNubj)}(h``SNDRV_PCM_INFO_XXX``h]hSNDRV_PCM_INFO_XXX}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh in }(hj hhhNhNubj)}(h````h]h}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh, which is used for the hardware definition (described later). When your soundchip supports only half-duplex, specify it like this:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h-pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;h]h-pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;}hjQsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubeh}(h]pcm-constructorah ]h"]pcm constructorah$]h&]uh1hhjhhhhhM5ubh)}(hhh](h)}(h... And the Destructor?h]h... And the Destructor?}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjghhhhhMubh)}(hThe destructor for a PCM instance is not always necessary. Since the PCM device will be released by the middle layer code automatically, you don't have to call the destructor explicitly.h]hThe destructor for a PCM instance is not always necessary. Since the PCM device will be released by the middle layer code automatically, you don’t have to call the destructor explicitly.}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjghhubh)}(hThe destructor would be necessary if you created special records internally and needed to release them. In such a case, set the destructor function to ``pcm->private_free``::h](hThe destructor would be necessary if you created special records internally and needed to release them. In such a case, set the destructor function to }(hjhhhNhNubj)}(h``pcm->private_free``h]hpcm->private_free}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjghhubj)}(hXstatic void mychip_pcm_free(struct snd_pcm *pcm) { struct mychip *chip = snd_pcm_chip(pcm); /* free your own data */ kfree(chip->my_private_pcm_data); /* do what you like else */ .... } static int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; .... /* allocate your own data */ chip->my_private_pcm_data = kmalloc(...); /* set the destructor */ pcm->private_data = chip; pcm->private_free = mychip_pcm_free; .... }h]hXstatic void mychip_pcm_free(struct snd_pcm *pcm) { struct mychip *chip = snd_pcm_chip(pcm); /* free your own data */ kfree(chip->my_private_pcm_data); /* do what you like else */ .... } static int snd_mychip_new_pcm(struct mychip *chip) { struct snd_pcm *pcm; .... /* allocate your own data */ chip->my_private_pcm_data = kmalloc(...); /* set the destructor */ pcm->private_data = chip; pcm->private_free = mychip_pcm_free; .... }}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjghhubeh}(h]and-the-destructorah ]h"]... and the destructor?ah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h.Runtime Pointer - The Chest of PCM Informationh]h.Runtime Pointer - The Chest of PCM Information}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hXIWhen the PCM substream is opened, a PCM runtime instance is allocated and assigned to the substream. This pointer is accessible via ``substream->runtime``. This runtime pointer holds most information you need to control the PCM: a copy of hw_params and sw_params configurations, the buffer pointers, mmap records, spinlocks, etc.h](hWhen the PCM substream is opened, a PCM runtime instance is allocated and assigned to the substream. This pointer is accessible via }(hjhhhNhNubj)}(h``substream->runtime``h]hsubstream->runtime}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. This runtime pointer holds most information you need to control the PCM: a copy of hw_params and sw_params configurations, the buffer pointers, mmap records, spinlocks, etc.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hjThe definition of runtime instance is found in ````. Here is the relevant part of this file::h](h/The definition of runtime instance is found in }(hjhhhNhNubj)}(h````h]h }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh). Here is the relevant part of this file:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hX struct _snd_pcm_runtime { /* -- Status -- */ struct snd_pcm_substream *trigger_master; snd_timestamp_t trigger_tstamp; /* trigger timestamp */ int overrange; snd_pcm_uframes_t avail_max; snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */ snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/ /* -- HW params -- */ snd_pcm_access_t access; /* access mode */ snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */ snd_pcm_subformat_t subformat; /* subformat */ unsigned int rate; /* rate in Hz */ unsigned int channels; /* channels */ snd_pcm_uframes_t period_size; /* period size */ unsigned int periods; /* periods */ snd_pcm_uframes_t buffer_size; /* buffer size */ unsigned int tick_time; /* tick time */ snd_pcm_uframes_t min_align; /* Min alignment for the format */ size_t byte_align; unsigned int frame_bits; unsigned int sample_bits; unsigned int info; unsigned int rate_num; unsigned int rate_den; /* -- SW params -- */ struct timespec tstamp_mode; /* mmap timestamp is updated */ unsigned int period_step; unsigned int sleep_min; /* min ticks to sleep */ snd_pcm_uframes_t start_threshold; /* * The following two thresholds alleviate playback buffer underruns; when * hw_avail drops below the threshold, the respective action is triggered: */ snd_pcm_uframes_t stop_threshold; /* - stop playback */ snd_pcm_uframes_t silence_threshold; /* - pre-fill buffer with silence */ snd_pcm_uframes_t silence_size; /* max size of silence pre-fill; when >= boundary, * fill played area with silence immediately */ snd_pcm_uframes_t boundary; /* pointers wrap point */ /* internal data of auto-silencer */ snd_pcm_uframes_t silence_start; /* starting pointer to silence area */ snd_pcm_uframes_t silence_filled; /* size filled with silence */ snd_pcm_sync_id_t sync; /* hardware synchronization ID */ /* -- mmap -- */ volatile struct snd_pcm_mmap_status *status; volatile struct snd_pcm_mmap_control *control; atomic_t mmap_count; /* -- locking / scheduling -- */ spinlock_t lock; wait_queue_head_t sleep; struct timer_list tick_timer; struct fasync_struct *fasync; /* -- private section -- */ void *private_data; void (*private_free)(struct snd_pcm_runtime *runtime); /* -- hardware description -- */ struct snd_pcm_hardware hw; struct snd_pcm_hw_constraints hw_constraints; /* -- timer -- */ unsigned int timer_resolution; /* timer resolution */ /* -- DMA -- */ unsigned char *dma_area; /* DMA area */ dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */ size_t dma_bytes; /* size of DMA area */ struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */ #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) /* -- OSS things -- */ struct snd_pcm_oss_runtime oss; #endif };h]hX struct _snd_pcm_runtime { /* -- Status -- */ struct snd_pcm_substream *trigger_master; snd_timestamp_t trigger_tstamp; /* trigger timestamp */ int overrange; snd_pcm_uframes_t avail_max; snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */ snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/ /* -- HW params -- */ snd_pcm_access_t access; /* access mode */ snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */ snd_pcm_subformat_t subformat; /* subformat */ unsigned int rate; /* rate in Hz */ unsigned int channels; /* channels */ snd_pcm_uframes_t period_size; /* period size */ unsigned int periods; /* periods */ snd_pcm_uframes_t buffer_size; /* buffer size */ unsigned int tick_time; /* tick time */ snd_pcm_uframes_t min_align; /* Min alignment for the format */ size_t byte_align; unsigned int frame_bits; unsigned int sample_bits; unsigned int info; unsigned int rate_num; unsigned int rate_den; /* -- SW params -- */ struct timespec tstamp_mode; /* mmap timestamp is updated */ unsigned int period_step; unsigned int sleep_min; /* min ticks to sleep */ snd_pcm_uframes_t start_threshold; /* * The following two thresholds alleviate playback buffer underruns; when * hw_avail drops below the threshold, the respective action is triggered: */ snd_pcm_uframes_t stop_threshold; /* - stop playback */ snd_pcm_uframes_t silence_threshold; /* - pre-fill buffer with silence */ snd_pcm_uframes_t silence_size; /* max size of silence pre-fill; when >= boundary, * fill played area with silence immediately */ snd_pcm_uframes_t boundary; /* pointers wrap point */ /* internal data of auto-silencer */ snd_pcm_uframes_t silence_start; /* starting pointer to silence area */ snd_pcm_uframes_t silence_filled; /* size filled with silence */ snd_pcm_sync_id_t sync; /* hardware synchronization ID */ /* -- mmap -- */ volatile struct snd_pcm_mmap_status *status; volatile struct snd_pcm_mmap_control *control; atomic_t mmap_count; /* -- locking / scheduling -- */ spinlock_t lock; wait_queue_head_t sleep; struct timer_list tick_timer; struct fasync_struct *fasync; /* -- private section -- */ void *private_data; void (*private_free)(struct snd_pcm_runtime *runtime); /* -- hardware description -- */ struct snd_pcm_hardware hw; struct snd_pcm_hw_constraints hw_constraints; /* -- timer -- */ unsigned int timer_resolution; /* timer resolution */ /* -- DMA -- */ unsigned char *dma_area; /* DMA area */ dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */ size_t dma_bytes; /* size of DMA area */ struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */ #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE) /* -- OSS things -- */ struct snd_pcm_oss_runtime oss; #endif };}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjhhubh)}(hXvFor the operators (callbacks) of each sound driver, most of these records are supposed to be read-only. Only the PCM middle-layer changes / updates them. The exceptions are the hardware description (hw) DMA buffer information and the private data. Besides, if you use the standard managed buffer allocation mode, you don't need to set the DMA buffer information by yourself.h]hXxFor the operators (callbacks) of each sound driver, most of these records are supposed to be read-only. Only the PCM middle-layer changes / updates them. The exceptions are the hardware description (hw) DMA buffer information and the private data. Besides, if you use the standard managed buffer allocation mode, you don’t need to set the DMA buffer information by yourself.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubh)}(h7In the sections below, important records are explained.h]h7In the sections below, important records are explained.}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(hHardware Descriptionh]hHardware Description}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj7hhhhhMubh)}(hX%The hardware descriptor (struct snd_pcm_hardware) contains the definitions of the fundamental hardware configuration. Above all, you'll need to define this in the `PCM open callback`_. Note that the runtime instance holds a copy of the descriptor, not a pointer to the existing descriptor. That is, in the open callback, you can modify the copied descriptor (``runtime->hw``) as you need. For example, if the maximum number of channels is 1 only on some chip models, you can still use the same hardware descriptor and change the channels_max later::h](hThe hardware descriptor (struct snd_pcm_hardware) contains the definitions of the fundamental hardware configuration. Above all, you’ll need to define this in the }(hjHhhhNhNubh)}(h`PCM open callback`_h]hPCM open callback}(hjPhhhNhNubah}(h]h ]h"]h$]h&]namePCM open callbackjpcm-open-callbackuh1hhjHjKubh. Note that the runtime instance holds a copy of the descriptor, not a pointer to the existing descriptor. That is, in the open callback, you can modify the copied descriptor (}(hjHhhhNhNubj)}(h``runtime->hw``h]h runtime->hw}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHubh) as you need. For example, if the maximum number of channels is 1 only on some chip models, you can still use the same hardware descriptor and change the channels_max later:}(hjHhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj7hhubj)}(hstruct snd_pcm_runtime *runtime = substream->runtime; ... runtime->hw = snd_mychip_playback_hw; /* common definition */ if (chip->model == VERY_OLD_ONE) runtime->hw.channels_max = 1;h]hstruct snd_pcm_runtime *runtime = substream->runtime; ... runtime->hw = snd_mychip_playback_hw; /* common definition */ if (chip->model == VERY_OLD_ONE) runtime->hw.channels_max = 1;}hj}sbah}(h]h ]h"]h$]h&]jjuh1jhhhM"hj7hhubh)}(h7Typically, you'll have a hardware descriptor as below::h]h8Typically, you’ll have a hardware descriptor as below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM(hj7hhubj)}(hXstatic struct snd_pcm_hardware snd_mychip_playback_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, };h]hXstatic struct snd_pcm_hardware snd_mychip_playback_hw = { .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID), .formats = SNDRV_PCM_FMTBIT_S16_LE, .rates = SNDRV_PCM_RATE_8000_48000, .rate_min = 8000, .rate_max = 48000, .channels_min = 2, .channels_max = 2, .buffer_bytes_max = 32768, .period_bytes_min = 4096, .period_bytes_max = 32768, .periods_min = 1, .periods_max = 1024, };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhM*hj7hhubj)}(hhh](j)}(hXThe ``info`` field contains the type and capabilities of this PCM. The bit flags are defined in ```` as ``SNDRV_PCM_INFO_XXX``. Here, at least, you have to specify whether mmap is supported and which interleaving formats are supported. When the hardware supports mmap, add the ``SNDRV_PCM_INFO_MMAP`` flag here. When the hardware supports the interleaved or the non-interleaved formats, the ``SNDRV_PCM_INFO_INTERLEAVED`` or ``SNDRV_PCM_INFO_NONINTERLEAVED`` flag must be set, respectively. If both are supported, you can set both, too. In the above example, ``MMAP_VALID`` and ``BLOCK_TRANSFER`` are specified for the OSS mmap mode. Usually both are set. Of course, ``MMAP_VALID`` is set only if mmap is really supported. The other possible flags are ``SNDRV_PCM_INFO_PAUSE`` and ``SNDRV_PCM_INFO_RESUME``. The ``PAUSE`` bit means that the PCM supports the “pause” operation, while the ``RESUME`` bit means that the PCM supports the full “suspend/resume” operation. If the ``PAUSE`` flag is set, the ``trigger`` callback below must handle the corresponding (pause push/release) commands. The suspend/resume trigger commands can be defined even without the ``RESUME`` flag. See the `Power Management`_ section for details. When the PCM substreams can be synchronized (typically, synchronized start/stop of a playback and a capture stream), you can give ``SNDRV_PCM_INFO_SYNC_START``, too. In this case, you'll need to check the linked-list of PCM substreams in the trigger callback. This will be described in a later section. h](h)}(hX(The ``info`` field contains the type and capabilities of this PCM. The bit flags are defined in ```` as ``SNDRV_PCM_INFO_XXX``. Here, at least, you have to specify whether mmap is supported and which interleaving formats are supported. When the hardware supports mmap, add the ``SNDRV_PCM_INFO_MMAP`` flag here. When the hardware supports the interleaved or the non-interleaved formats, the ``SNDRV_PCM_INFO_INTERLEAVED`` or ``SNDRV_PCM_INFO_NONINTERLEAVED`` flag must be set, respectively. If both are supported, you can set both, too.h](hThe }(hjhhhNhNubj)}(h``info``h]hinfo}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhT field contains the type and capabilities of this PCM. The bit flags are defined in }(hjhhhNhNubj)}(h````h]h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh as }(hjhhhNhNubj)}(h``SNDRV_PCM_INFO_XXX``h]hSNDRV_PCM_INFO_XXX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. Here, at least, you have to specify whether mmap is supported and which interleaving formats are supported. When the hardware supports mmap, add the }(hjhhhNhNubj)}(h``SNDRV_PCM_INFO_MMAP``h]hSNDRV_PCM_INFO_MMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh[ flag here. When the hardware supports the interleaved or the non-interleaved formats, the }(hjhhhNhNubj)}(h``SNDRV_PCM_INFO_INTERLEAVED``h]hSNDRV_PCM_INFO_INTERLEAVED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh or }(hjhhhNhNubj)}(h!``SNDRV_PCM_INFO_NONINTERLEAVED``h]hSNDRV_PCM_INFO_NONINTERLEAVED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhN flag must be set, respectively. If both are supported, you can set both, too.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM<hjubh)}(hIn the above example, ``MMAP_VALID`` and ``BLOCK_TRANSFER`` are specified for the OSS mmap mode. Usually both are set. Of course, ``MMAP_VALID`` is set only if mmap is really supported.h](hIn the above example, }(hj(hhhNhNubj)}(h``MMAP_VALID``h]h MMAP_VALID}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh and }(hj(hhhNhNubj)}(h``BLOCK_TRANSFER``h]hBLOCK_TRANSFER}(hjBhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubhG are specified for the OSS mmap mode. Usually both are set. Of course, }(hj(hhhNhNubj)}(h``MMAP_VALID``h]h MMAP_VALID}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh) is set only if mmap is really supported.}(hj(hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhjubh)}(hXThe other possible flags are ``SNDRV_PCM_INFO_PAUSE`` and ``SNDRV_PCM_INFO_RESUME``. The ``PAUSE`` bit means that the PCM supports the “pause” operation, while the ``RESUME`` bit means that the PCM supports the full “suspend/resume” operation. If the ``PAUSE`` flag is set, the ``trigger`` callback below must handle the corresponding (pause push/release) commands. The suspend/resume trigger commands can be defined even without the ``RESUME`` flag. See the `Power Management`_ section for details.h](hThe other possible flags are }(hjlhhhNhNubj)}(h``SNDRV_PCM_INFO_PAUSE``h]hSNDRV_PCM_INFO_PAUSE}(hjthhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh and }(hjlhhhNhNubj)}(h``SNDRV_PCM_INFO_RESUME``h]hSNDRV_PCM_INFO_RESUME}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh. The }(hjlhhhNhNubj)}(h ``PAUSE``h]hPAUSE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubhF bit means that the PCM supports the “pause” operation, while the }(hjlhhhNhNubj)}(h ``RESUME``h]hRESUME}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubhQ bit means that the PCM supports the full “suspend/resume” operation. If the }(hjlhhhNhNubj)}(h ``PAUSE``h]hPAUSE}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh flag is set, the }(hjlhhhNhNubj)}(h ``trigger``h]htrigger}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh callback below must handle the corresponding (pause push/release) commands. The suspend/resume trigger commands can be defined even without the }(hjlhhhNhNubj)}(h ``RESUME``h]hRESUME}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubh flag. See the }(hjlhhhNhNubh)}(h`Power Management`_h]hPower Management}(hjhhhNhNubah}(h]h ]h"]h$]h&]namePower Managementjpower-managementuh1hhjljKubh section for details.}(hjlhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMKhjubh)}(hX.When the PCM substreams can be synchronized (typically, synchronized start/stop of a playback and a capture stream), you can give ``SNDRV_PCM_INFO_SYNC_START``, too. In this case, you'll need to check the linked-list of PCM substreams in the trigger callback. This will be described in a later section.h](hWhen the PCM substreams can be synchronized (typically, synchronized start/stop of a playback and a capture stream), you can give }(hj hhhNhNubj)}(h``SNDRV_PCM_INFO_SYNC_START``h]hSNDRV_PCM_INFO_SYNC_START}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh, too. In this case, you’ll need to check the linked-list of PCM substreams in the trigger callback. This will be described in a later section.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMThjubeh}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hThe ``formats`` field contains the bit-flags of supported formats (``SNDRV_PCM_FMTBIT_XXX``). If the hardware supports more than one format, give all or'ed bits. In the example above, the signed 16bit little-endian format is specified. h]h)}(hThe ``formats`` field contains the bit-flags of supported formats (``SNDRV_PCM_FMTBIT_XXX``). If the hardware supports more than one format, give all or'ed bits. In the example above, the signed 16bit little-endian format is specified.h](hThe }(hj7hhhNhNubj)}(h ``formats``h]hformats}(hj?hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh4 field contains the bit-flags of supported formats (}(hj7hhhNhNubj)}(h``SNDRV_PCM_FMTBIT_XXX``h]hSNDRV_PCM_FMTBIT_XXX}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh). If the hardware supports more than one format, give all or’ed bits. In the example above, the signed 16bit little-endian format is specified.}(hj7hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMZhj3ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hXvThe ``rates`` field contains the bit-flags of supported rates (``SNDRV_PCM_RATE_XXX``). When the chip supports continuous rates, pass the ``CONTINUOUS`` bit additionally. The pre-defined rate bits are provided only for typical rates. If your chip supports unconventional rates, you need to add the ``KNOT`` bit and set up the hardware constraint manually (explained later). h]h)}(hXuThe ``rates`` field contains the bit-flags of supported rates (``SNDRV_PCM_RATE_XXX``). When the chip supports continuous rates, pass the ``CONTINUOUS`` bit additionally. The pre-defined rate bits are provided only for typical rates. If your chip supports unconventional rates, you need to add the ``KNOT`` bit and set up the hardware constraint manually (explained later).h](hThe }(hjshhhNhNubj)}(h ``rates``h]hrates}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjsubh2 field contains the bit-flags of supported rates (}(hjshhhNhNubj)}(h``SNDRV_PCM_RATE_XXX``h]hSNDRV_PCM_RATE_XXX}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjsubh5). When the chip supports continuous rates, pass the }(hjshhhNhNubj)}(h``CONTINUOUS``h]h CONTINUOUS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjsubh bit additionally. The pre-defined rate bits are provided only for typical rates. If your chip supports unconventional rates, you need to add the }(hjshhhNhNubj)}(h``KNOT``h]hKNOT}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjsubhC bit and set up the hardware constraint manually (explained later).}(hjshhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM_hjoubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h|``rate_min`` and ``rate_max`` define the minimum and maximum sample rate. This should correspond somehow to ``rates`` bits. h]h)}(h{``rate_min`` and ``rate_max`` define the minimum and maximum sample rate. This should correspond somehow to ``rates`` bits.h](j)}(h ``rate_min``h]hrate_min}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h ``rate_max``h]hrate_max}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhO define the minimum and maximum sample rate. This should correspond somehow to }(hjhhhNhNubj)}(h ``rates``h]hrates}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh bits.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMfhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h~``channels_min`` and ``channels_max`` define, as you might have already expected, the minimum and maximum number of channels. h]h)}(h}``channels_min`` and ``channels_max`` define, as you might have already expected, the minimum and maximum number of channels.h](j)}(h``channels_min``h]h channels_min}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h``channels_max``h]h channels_max}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhX define, as you might have already expected, the minimum and maximum number of channels.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMihjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hXx``buffer_bytes_max`` defines the maximum buffer size in bytes. There is no ``buffer_bytes_min`` field, since it can be calculated from the minimum period size and the minimum number of periods. Meanwhile, ``period_bytes_min`` and ``period_bytes_max`` define the minimum and maximum size of the period in bytes. ``periods_max`` and ``periods_min`` define the maximum and minimum number of periods in the buffer. The “period” is a term that corresponds to a fragment in the OSS world. The period defines the point at which a PCM interrupt is generated. This point strongly depends on the hardware. Generally, a smaller period size will give you more interrupts, which results in being able to fill/drain the buffer more timely. In the case of capture, this size defines the input latency. On the other hand, the whole buffer size defines the output latency for the playback direction. h](h)}(hX``buffer_bytes_max`` defines the maximum buffer size in bytes. There is no ``buffer_bytes_min`` field, since it can be calculated from the minimum period size and the minimum number of periods. Meanwhile, ``period_bytes_min`` and ``period_bytes_max`` define the minimum and maximum size of the period in bytes. ``periods_max`` and ``periods_min`` define the maximum and minimum number of periods in the buffer.h](j)}(h``buffer_bytes_max``h]hbuffer_bytes_max}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh7 defines the maximum buffer size in bytes. There is no }(hjUhhhNhNubj)}(h``buffer_bytes_min``h]hbuffer_bytes_min}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubhn field, since it can be calculated from the minimum period size and the minimum number of periods. Meanwhile, }(hjUhhhNhNubj)}(h``period_bytes_min``h]hperiod_bytes_min}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh and }(hjUhhhNhNubj)}(h``period_bytes_max``h]hperiod_bytes_max}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh= define the minimum and maximum size of the period in bytes. }(hjUhhhNhNubj)}(h``periods_max``h]h periods_max}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh and }hjUsbj)}(h``periods_min``h]h periods_min}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh@ define the maximum and minimum number of periods in the buffer.}(hjUhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMlhjQubh)}(hXThe “period” is a term that corresponds to a fragment in the OSS world. The period defines the point at which a PCM interrupt is generated. This point strongly depends on the hardware. Generally, a smaller period size will give you more interrupts, which results in being able to fill/drain the buffer more timely. In the case of capture, this size defines the input latency. On the other hand, the whole buffer size defines the output latency for the playback direction.h]hXThe “period” is a term that corresponds to a fragment in the OSS world. The period defines the point at which a PCM interrupt is generated. This point strongly depends on the hardware. Generally, a smaller period size will give you more interrupts, which results in being able to fill/drain the buffer more timely. In the case of capture, this size defines the input latency. On the other hand, the whole buffer size defines the output latency for the playback direction.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMthjQubeh}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(hThere is also a field ``fifo_size``. This specifies the size of the hardware FIFO, but currently it is neither used by the drivers nor in the alsa-lib. So, you can ignore this field. h]h)}(hThere is also a field ``fifo_size``. This specifies the size of the hardware FIFO, but currently it is neither used by the drivers nor in the alsa-lib. So, you can ignore this field.h](hThere is also a field }(hjhhhNhNubj)}(h ``fifo_size``h]h fifo_size}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. This specifies the size of the hardware FIFO, but currently it is neither used by the drivers nor in the alsa-lib. So, you can ignore this field.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM}hjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]j0j1uh1jhhhM<hj7hhubeh}(h]hardware-descriptionah ]h"]hardware descriptionah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hPCM Configurationsh]hPCM Configurations}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXOk, let's go back again to the PCM runtime records. The most frequently referred records in the runtime instance are the PCM configurations. The PCM configurations are stored in the runtime instance after the application sends ``hw_params`` data via alsa-lib. There are many fields copied from hw_params and sw_params structs. For example, ``format`` holds the format type chosen by the application. This field contains the enum value ``SNDRV_PCM_FORMAT_XXX``.h](hOk, let’s go back again to the PCM runtime records. The most frequently referred records in the runtime instance are the PCM configurations. The PCM configurations are stored in the runtime instance after the application sends }(hj( hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj0 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj( ubhd data via alsa-lib. There are many fields copied from hw_params and sw_params structs. For example, }(hj( hhhNhNubj)}(h ``format``h]hformat}(hjB hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj( ubhU holds the format type chosen by the application. This field contains the enum value }(hj( hhhNhNubj)}(h``SNDRV_PCM_FORMAT_XXX``h]hSNDRV_PCM_FORMAT_XXX}(hjT hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj( ubh.}(hj( hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hX2One thing to be noted is that the configured buffer and period sizes are stored in “frames” in the runtime. In the ALSA world, ``1 frame = channels \* samples-size``. For conversion between frames and bytes, you can use the :c:func:`frames_to_bytes()` and :c:func:`bytes_to_frames()` helper functions::h](hOne thing to be noted is that the configured buffer and period sizes are stored in “frames” in the runtime. In the ALSA world, }(hjl hhhNhNubj)}(h&``1 frame = channels \* samples-size``h]h"1 frame = channels \* samples-size}(hjt hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjl ubh;. For conversion between frames and bytes, you can use the }(hjl hhhNhNubh)}(h:c:func:`frames_to_bytes()`h]j)}(hj h]hframes_to_bytes()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjframes_to_bytesuh1hhhhMhjl ubh and }(hjl hhhNhNubh)}(h:c:func:`bytes_to_frames()`h]j)}(hj h]hbytes_to_frames()}(hj hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjbytes_to_framesuh1hhhhMhjl ubh helper functions:}(hjl hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(h>period_bytes = frames_to_bytes(runtime, runtime->period_size);h]h>period_bytes = frames_to_bytes(runtime, runtime->period_size);}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubh)}(hAlso, many software parameters (sw_params) are stored in frames, too. Please check the type of the field. ``snd_pcm_uframes_t`` is for frames as unsigned integer while ``snd_pcm_sframes_t`` is for frames as signed integer.h](hjAlso, many software parameters (sw_params) are stored in frames, too. Please check the type of the field. }(hj hhhNhNubj)}(h``snd_pcm_uframes_t``h]hsnd_pcm_uframes_t}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh) is for frames as unsigned integer while }(hj hhhNhNubj)}(h``snd_pcm_sframes_t``h]hsnd_pcm_sframes_t}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh! is for frames as signed integer.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]pcm-configurationsah ]h"]pcm configurationsah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hDMA Buffer Informationh]hDMA Buffer Information}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj!hhhhhMubh)}(hXThe DMA buffer is defined by the following four fields: ``dma_area``, ``dma_addr``, ``dma_bytes`` and ``dma_private``. ``dma_area`` holds the buffer pointer (the logical address). You can call :c:func:`memcpy()` from/to this pointer. Meanwhile, ``dma_addr`` holds the physical address of the buffer. This field is specified only when the buffer is a linear buffer. ``dma_bytes`` holds the size of the buffer in bytes. ``dma_private`` is used for the ALSA DMA allocator.h](h8The DMA buffer is defined by the following four fields: }(hj+!hhhNhNubj)}(h ``dma_area``h]hdma_area}(hj3!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh, }(hj+!hhhNhNubj)}(h ``dma_addr``h]hdma_addr}(hjE!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh, }(hj+!hhhNhNubj)}(h ``dma_bytes``h]h dma_bytes}(hjW!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh and }(hj+!hhhNhNubj)}(h``dma_private``h]h dma_private}(hji!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh. }(hj+!hhhNhNubj)}(h ``dma_area``h]hdma_area}(hj{!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh> holds the buffer pointer (the logical address). You can call }(hj+!hhhNhNubh)}(h:c:func:`memcpy()`h]j)}(hj!h]hmemcpy()}(hj!hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj!ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmemcpyuh1hhhhMhj+!ubh" from/to this pointer. Meanwhile, }(hj+!hhhNhNubj)}(h ``dma_addr``h]hdma_addr}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubhl holds the physical address of the buffer. This field is specified only when the buffer is a linear buffer. }(hj+!hhhNhNubj)}(h ``dma_bytes``h]h dma_bytes}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh( holds the size of the buffer in bytes. }(hj+!hhhNhNubj)}(h``dma_private``h]h dma_private}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+!ubh$ is used for the ALSA DMA allocator.}(hj+!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!hhubh)}(hXrIf you use either the managed buffer allocation mode or the standard API function :c:func:`snd_pcm_lib_malloc_pages()` for allocating the buffer, these fields are set by the ALSA middle layer, and you should *not* change them by yourself. You can read them but not write them. On the other hand, if you want to allocate the buffer by yourself, you'll need to manage it in the hw_params callback. At least, ``dma_bytes`` is mandatory. ``dma_area`` is necessary when the buffer is mmapped. If your driver doesn't support mmap, this field is not necessary. ``dma_addr`` is also optional. You can use dma_private as you like, too.h](hRIf you use either the managed buffer allocation mode or the standard API function }(hj!hhhNhNubh)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hj!h]hsnd_pcm_lib_malloc_pages()}(hj!hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj!ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhMhj!ubhZ for allocating the buffer, these fields are set by the ALSA middle layer, and you should }(hj!hhhNhNubhemphasis)}(h*not*h]hnot}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1j"hj!ubh change them by yourself. You can read them but not write them. On the other hand, if you want to allocate the buffer by yourself, you’ll need to manage it in the hw_params callback. At least, }(hj!hhhNhNubj)}(h ``dma_bytes``h]h dma_bytes}(hj+"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh is mandatory. }(hj!hhhNhNubj)}(h ``dma_area``h]hdma_area}(hj="hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubhn is necessary when the buffer is mmapped. If your driver doesn’t support mmap, this field is not necessary. }(hj!hhhNhNubj)}(h ``dma_addr``h]hdma_addr}(hjO"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj!ubh< is also optional. You can use dma_private as you like, too.}(hj!hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj!hhubeh}(h]dma-buffer-informationah ]h"]dma buffer informationah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(hRunning Statush]hRunning Status}(hjr"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo"hhhhhMubh)}(hThe running status can be referred via ``runtime->status``. This is a pointer to a struct snd_pcm_mmap_status record. For example, you can get the current DMA hardware pointer via ``runtime->status->hw_ptr``.h](h'The running status can be referred via }(hj"hhhNhNubj)}(h``runtime->status``h]hruntime->status}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubhz. This is a pointer to a struct snd_pcm_mmap_status record. For example, you can get the current DMA hardware pointer via }(hj"hhhNhNubj)}(h``runtime->status->hw_ptr``h]hruntime->status->hw_ptr}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh.}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjo"hhubh)}(hThe DMA application pointer can be referred via ``runtime->control``, which points to a struct snd_pcm_mmap_control record. However, accessing this value directly is not recommended.h](h0The DMA application pointer can be referred via }(hj"hhhNhNubj)}(h``runtime->control``h]hruntime->control}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubhr, which points to a struct snd_pcm_mmap_control record. However, accessing this value directly is not recommended.}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjo"hhubeh}(h]running-statusah ]h"]running statusah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h Private Datah]h Private Data}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"hhhhhMubh)}(hXYou can allocate a record for the substream and store it in ``runtime->private_data``. Usually, this is done in the `PCM open callback`_. Don't mix this with ``pcm->private_data``. The ``pcm->private_data`` usually points to the chip instance assigned statically at creation time of the PCM device, while ``runtime->private_data`` points to a dynamic data structure created in the PCM open callback::h](hprivate_data``h]hruntime->private_data}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh. Usually, this is done in the }(hj"hhhNhNubh)}(h`PCM open callback`_h]hPCM open callback}(hj#hhhNhNubah}(h]h ]h"]h$]h&]namePCM open callbackjj`uh1hhj"jKubh. Don’t mix this with }(hj"hhhNhNubj)}(h``pcm->private_data``h]hpcm->private_data}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubh. The }(hj"hhhNhNubj)}(h``pcm->private_data``h]hpcm->private_data}(hj+#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubhc usually points to the chip instance assigned statically at creation time of the PCM device, while }(hj"hhhNhNubj)}(h``runtime->private_data``h]hruntime->private_data}(hj=#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubhE points to a dynamic data structure created in the PCM open callback:}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubj)}(hstatic int snd_xxx_open(struct snd_pcm_substream *substream) { struct my_pcm_data *data; .... data = kmalloc(sizeof(*data), GFP_KERNEL); substream->runtime->private_data = data; .... }h]hstatic int snd_xxx_open(struct snd_pcm_substream *substream) { struct my_pcm_data *data; .... data = kmalloc(sizeof(*data), GFP_KERNEL); substream->runtime->private_data = data; .... }}hjU#sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj"hhubh)}(h?The allocated object must be released in the `close callback`_.h](h-The allocated object must be released in the }(hjc#hhhNhNubh)}(h`close callback`_h]hclose callback}(hjk#hhhNhNubah}(h]h ]h"]h$]h&]nameclose callbackjclose-callbackuh1hhjc#jKubh.}(hjc#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj"hhubeh}(h] private-dataah ]h"] private dataah$]h&]uh1hhjhhhhhMjKubeh}(h],runtime-pointer-the-chest-of-pcm-informationah ]h"].runtime pointer - the chest of pcm informationah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h Operatorsh]h Operators}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj#hhhhhMubh)}(hX5OK, now let me give details about each PCM callback (``ops``). In general, every callback must return 0 if successful, or a negative error number such as ``-EINVAL``. To choose an appropriate error number, it is advised to check what value other parts of the kernel return when the same kind of request fails.h](h5OK, now let me give details about each PCM callback (}(hj#hhhNhNubj)}(h``ops``h]hops}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubh^). In general, every callback must return 0 if successful, or a negative error number such as }(hj#hhhNhNubj)}(h ``-EINVAL``h]h-EINVAL}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubh. To choose an appropriate error number, it is advised to check what value other parts of the kernel return when the same kind of request fails.}(hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj#hhubh)}(hEach callback function takes at least one argument containing a struct snd_pcm_substream pointer. To retrieve the chip record from the given substream instance, you can use the following macro::h]hEach callback function takes at least one argument containing a struct snd_pcm_substream pointer. To retrieve the chip record from the given substream instance, you can use the following macro:}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj#hhubj)}(h^int xxx(...) { struct mychip *chip = snd_pcm_substream_chip(substream); .... }h]h^int xxx(...) { struct mychip *chip = snd_pcm_substream_chip(substream); .... }}hj#sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj#hhubh)}(hXqThe macro reads ``substream->private_data``, which is a copy of ``pcm->private_data``. You can override the former if you need to assign different data records per PCM substream. For example, the cmi8330 driver assigns different ``private_data`` for playback and capture directions, because it uses two different codecs (SB- and AD-compatible) for different directions.h](hThe macro reads }(hj#hhhNhNubj)}(h``substream->private_data``h]hsubstream->private_data}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubh, which is a copy of }(hj#hhhNhNubj)}(h``pcm->private_data``h]hpcm->private_data}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubh. You can override the former if you need to assign different data records per PCM substream. For example, the cmi8330 driver assigns different }(hj#hhhNhNubj)}(h``private_data``h]h private_data}(hj!$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#ubh| for playback and capture directions, because it uses two different codecs (SB- and AD-compatible) for different directions.}(hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj#hhubh)}(hhh](h)}(hPCM open callbackh]hPCM open callback}(hj<$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj9$hhhhhMubj)}(h=static int snd_xxx_open(struct snd_pcm_substream *substream);h]h=static int snd_xxx_open(struct snd_pcm_substream *substream);}hjJ$sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj9$hhubh)}(h.This is called when a PCM substream is opened.h]h.This is called when a PCM substream is opened.}(hjX$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj9$hhubh)}(heAt least, here you have to initialize the ``runtime->hw`` record. Typically, this is done like this::h](h*At least, here you have to initialize the }(hjf$hhhNhNubj)}(h``runtime->hw``h]h runtime->hw}(hjn$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjf$ubh+ record. Typically, this is done like this:}(hjf$hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj9$hhubj)}(hXstatic int snd_xxx_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_playback_hw; return 0; }h]hXstatic int snd_xxx_open(struct snd_pcm_substream *substream) { struct mychip *chip = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; runtime->hw = snd_mychip_playback_hw; return 0; }}hj$sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj9$hhubh)}(hIwhere ``snd_mychip_playback_hw`` is the pre-defined hardware description.h](hwhere }(hj$hhhNhNubj)}(h``snd_mychip_playback_hw``h]hsnd_mychip_playback_hw}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj$ubh) is the pre-defined hardware description.}(hj$hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj9$hhubh)}(h\You can allocate private data in this callback, as described in the `Private Data`_ section.h](hDYou can allocate private data in this callback, as described in the }(hj$hhhNhNubh)}(h`Private Data`_h]h Private Data}(hj$hhhNhNubah}(h]h ]h"]h$]h&]name Private Datajj#uh1hhj$jKubh section.}(hj$hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj9$hhubh)}(hIf the hardware configuration needs more constraints, set the hardware constraints here, too. See Constraints_ for more details.h](hbIf the hardware configuration needs more constraints, set the hardware constraints here, too. See }(hj$hhhNhNubh)}(h Constraints_h]h Constraints}(hj$hhhNhNubah}(h]h ]h"]h$]h&]name Constraintsj constraintsuh1hhj$jKubh for more details.}(hj$hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj9$hhubeh}(h]j`ah ]h"]pcm open callbackah$]h&]uh1hhj#hhhhhMjKubh)}(hhh](h)}(hclose callbackh]hclose callback}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj%hhhhhM ubj)}(h>static int snd_xxx_close(struct snd_pcm_substream *substream);h]h>static int snd_xxx_close(struct snd_pcm_substream *substream);}hj%sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj%hhubh)}(h9Obviously, this is called when a PCM substream is closed.h]h9Obviously, this is called when a PCM substream is closed.}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj%hhubh)}(hcAny private instance for a PCM substream allocated in the ``open`` callback will be released here::h](h:Any private instance for a PCM substream allocated in the }(hj-%hhhNhNubj)}(h``open``h]hopen}(hj5%hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-%ubh callback will be released here:}(hj-%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj%hhubj)}(hstatic int snd_xxx_close(struct snd_pcm_substream *substream) { .... kfree(substream->runtime->private_data); .... }h]hstatic int snd_xxx_close(struct snd_pcm_substream *substream) { .... kfree(substream->runtime->private_data); .... }}hjM%sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj%hhubeh}(h]j{#ah ]h"]close callbackah$]h&]uh1hhj#hhhhhM jKubh)}(hhh](h)}(hioctl callbackh]hioctl callback}(hje%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjb%hhhhhM!ubh)}(hX)This is used for any special call to PCM ioctls. But usually you can leave it NULL, then the PCM core calls the generic ioctl callback function :c:func:`snd_pcm_lib_ioctl()`. If you need to deal with a unique setup of channel info or reset procedure, you can pass your own callback function here.h](hThis is used for any special call to PCM ioctls. But usually you can leave it NULL, then the PCM core calls the generic ioctl callback function }(hjs%hhhNhNubh)}(h:c:func:`snd_pcm_lib_ioctl()`h]j)}(hj}%h]hsnd_pcm_lib_ioctl()}(hj%hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj{%ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_ioctluh1hhhhM#hjs%ubh|. If you need to deal with a unique setup of channel info or reset procedure, you can pass your own callback function here.}(hjs%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM#hjb%hhubeh}(h]ioctl-callbackah ]h"]ioctl callbackah$]h&]uh1hhj#hhhhhM!ubh)}(hhh](h)}(hhw_params callbackh]hhw_params callback}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj%hhhhhM*ubj)}(hstatic int snd_xxx_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params);h]hstatic int snd_xxx_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *hw_params);}hj%sbah}(h]h ]h"]h$]h&]jjuh1jhhhM.hj%hhubh)}(hThis is called when the hardware parameters (``hw_params``) are set up by the application, that is, once when the buffer size, the period size, the format, etc. are defined for the PCM substream.h](h-This is called when the hardware parameters (}(hj%hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubh) are set up by the application, that is, once when the buffer size, the period size, the format, etc. are defined for the PCM substream.}(hj%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM1hj%hhubh)}(hZMany hardware setups should be done in this callback, including the allocation of buffers.h]hZMany hardware setups should be done in this callback, including the allocation of buffers.}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM5hj%hhubh)}(hPParameters to be initialized are retrieved by the :c:func:`params_xxx()` macros.h](h2Parameters to be initialized are retrieved by the }(hj%hhhNhNubh)}(h:c:func:`params_xxx()`h]j)}(hj&h]h params_xxx()}(hj&hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj&ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj params_xxxuh1hhhhM8hj%ubh macros.}(hj%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM8hj%hhubh)}(hWhen you choose managed buffer allocation mode for the substream, a buffer is already allocated before this callback gets called. Alternatively, you can call a helper function below for allocating the buffer::h]hWhen you choose managed buffer allocation mode for the substream, a buffer is already allocated before this callback gets called. Alternatively, you can call a helper function below for allocating the buffer:}(hj*&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM;hj%hhubj)}(hDsnd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));h]hDsnd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));}hj8&sbah}(h]h ]h"]h$]h&]jjuh1jhhhM@hj%hhubh)}(h:c:func:`snd_pcm_lib_malloc_pages()` is available only when the DMA buffers have been pre-allocated. See the section `Buffer Types`_ for more details.h](h)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hjL&h]hsnd_pcm_lib_malloc_pages()}(hjN&hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjJ&ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhMBhjF&ubhQ is available only when the DMA buffers have been pre-allocated. See the section }(hjF&hhhNhNubh)}(h`Buffer Types`_h]h Buffer Types}(hjm&hhhNhNubah}(h]h ]h"]h$]h&]name Buffer Typesj buffer-typesuh1hhjF&jKubh for more details.}(hjF&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMBhj%hhubh)}(hNote that this one and the ``prepare`` callback may be called multiple times per initialization. For example, the OSS emulation may call these callbacks at each change via its ioctl.h](hNote that this one and the }(hj&hhhNhNubj)}(h ``prepare``h]hprepare}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh callback may be called multiple times per initialization. For example, the OSS emulation may call these callbacks at each change via its ioctl.}(hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMFhj%hhubh)}(hThus, you need to be careful not to allocate the same buffers many times, which will lead to memory leaks! Calling the helper function above many times is OK. It will release the previous buffer automatically when it was already allocated.h]hThus, you need to be careful not to allocate the same buffers many times, which will lead to memory leaks! Calling the helper function above many times is OK. It will release the previous buffer automatically when it was already allocated.}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMJhj%hhubh)}(hXTAnother note is that this callback is non-atomic (schedulable) by default, i.e. when no ``nonatomic`` flag set. This is important, because the ``trigger`` callback is atomic (non-schedulable). That is, mutexes or any schedule-related functions are not available in the ``trigger`` callback. Please see the subsection Atomicity_ for details.h](hXAnother note is that this callback is non-atomic (schedulable) by default, i.e. when no }(hj&hhhNhNubj)}(h ``nonatomic``h]h nonatomic}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh* flag set. This is important, because the }(hj&hhhNhNubj)}(h ``trigger``h]htrigger}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubhs callback is atomic (non-schedulable). That is, mutexes or any schedule-related functions are not available in the }(hj&hhhNhNubj)}(h ``trigger``h]htrigger}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh% callback. Please see the subsection }(hj&hhhNhNubh)}(h Atomicity_h]h Atomicity}(hj&hhhNhNubah}(h]h ]h"]h$]h&]name Atomicityj atomicityuh1hhj&jKubh for details.}(hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMOhj%hhubeh}(h]hw-params-callbackah ]h"]hw_params callbackah$]h&]uh1hhj#hhhhhM*ubh)}(hhh](h)}(hhw_free callbackh]hhw_free callback}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj'hhhhhMWubj)}(h@static int snd_xxx_hw_free(struct snd_pcm_substream *substream);h]h@static int snd_xxx_hw_free(struct snd_pcm_substream *substream);}hj('sbah}(h]h ]h"]h$]h&]jjuh1jhhhM[hj'hhubh)}(hDThis is called to release the resources allocated via ``hw_params``.h](h6This is called to release the resources allocated via }(hj6'hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj>'hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj6'ubh.}(hj6'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM]hj'hhubh)}(hThis function is always called before the close callback is called. Also, the callback may be called multiple times, too. Keep track whether each resource was already released.h]hThis function is always called before the close callback is called. Also, the callback may be called multiple times, too. Keep track whether each resource was already released.}(hjV'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM`hj'hhubh)}(hXsWhen you have chosen managed buffer allocation mode for the PCM substream, the allocated PCM buffer will be automatically released after this callback gets called. Otherwise you'll have to release the buffer manually. Typically, when the buffer was allocated from the pre-allocated pool, you can use the standard API function :c:func:`snd_pcm_lib_malloc_pages()` like::h](hXJWhen you have chosen managed buffer allocation mode for the PCM substream, the allocated PCM buffer will be automatically released after this callback gets called. Otherwise you’ll have to release the buffer manually. Typically, when the buffer was allocated from the pre-allocated pool, you can use the standard API function }(hjd'hhhNhNubh)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hjn'h]hsnd_pcm_lib_malloc_pages()}(hjp'hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjl'ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhMdhjd'ubh like:}(hjd'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMdhj'hhubj)}(h"snd_pcm_lib_free_pages(substream);h]h"snd_pcm_lib_free_pages(substream);}hj'sbah}(h]h ]h"]h$]h&]jjuh1jhhhMkhj'hhubeh}(h]hw-free-callbackah ]h"]hw_free callbackah$]h&]uh1hhj#hhhhhMWubh)}(hhh](h)}(hprepare callbackh]hprepare callback}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj'hhhhhMnubj)}(h@static int snd_xxx_prepare(struct snd_pcm_substream *substream);h]h@static int snd_xxx_prepare(struct snd_pcm_substream *substream);}hj'sbah}(h]h ]h"]h$]h&]jjuh1jhhhMrhj'hhubh)}(hXThis callback is called when the PCM is “prepared”. You can set the format type, sample rate, etc. here. The difference from ``hw_params`` is that the ``prepare`` callback will be called each time :c:func:`snd_pcm_prepare()` is called, i.e. when recovering after underruns, etc.h](hThis callback is called when the PCM is “prepared”. You can set the format type, sample rate, etc. here. The difference from }(hj'hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'ubh is that the }(hj'hhhNhNubj)}(h ``prepare``h]hprepare}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'ubh# callback will be called each time }(hj'hhhNhNubh)}(h:c:func:`snd_pcm_prepare()`h]j)}(hj'h]hsnd_pcm_prepare()}(hj'hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_prepareuh1hhhhMthj'ubh6 is called, i.e. when recovering after underruns, etc.}(hj'hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMthj'hhubh)}(hfNote that this callback is non-atomic. You can use schedule-related functions safely in this callback.h]hfNote that this callback is non-atomic. You can use schedule-related functions safely in this callback.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMzhj'hhubh)}(hXIn this and the following callbacks, you can refer to the values via the runtime record, ``substream->runtime``. For example, to get the current rate, format or channels, access to ``runtime->rate``, ``runtime->format`` or ``runtime->channels``, respectively. The physical address of the allocated buffer is set to ``runtime->dma_area``. The buffer and period sizes are in ``runtime->buffer_size`` and ``runtime->period_size``, respectively.h](hYIn this and the following callbacks, you can refer to the values via the runtime record, }(hj-(hhhNhNubj)}(h``substream->runtime``h]hsubstream->runtime}(hj5(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubhF. For example, to get the current rate, format or channels, access to }(hj-(hhhNhNubj)}(h``runtime->rate``h]h runtime->rate}(hjG(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubh, }(hj-(hhhNhNubj)}(h``runtime->format``h]hruntime->format}(hjY(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubh or }(hj-(hhhNhNubj)}(h``runtime->channels``h]hruntime->channels}(hjk(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubhG, respectively. The physical address of the allocated buffer is set to }(hj-(hhhNhNubj)}(h``runtime->dma_area``h]hruntime->dma_area}(hj}(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubh%. The buffer and period sizes are in }(hj-(hhhNhNubj)}(h``runtime->buffer_size``h]hruntime->buffer_size}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubh and }(hj-(hhhNhNubj)}(h``runtime->period_size``h]hruntime->period_size}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-(ubh, respectively.}(hj-(hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM}hj'hhubh)}(hKBe careful that this callback will be called many times at each setup, too.h]hKBe careful that this callback will be called many times at each setup, too.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj'hhubeh}(h]prepare-callbackah ]h"]prepare callbackah$]h&]uh1hhj#hhhhhMnubh)}(hhh](h)}(htrigger callbackh]htrigger callback}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj(hhhhhMubj)}(hIstatic int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);h]hIstatic int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);}hj(sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj(hhubh)}(h:This is called when the PCM is started, stopped or paused.h]h:This is called when the PCM is started, stopped or paused.}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj(hhubh)}(hThe action is specified in the second argument, ``SNDRV_PCM_TRIGGER_XXX`` defined in ````. At least, the ``START`` and ``STOP`` commands must be defined in this callback::h](h0The action is specified in the second argument, }(hj(hhhNhNubj)}(h``SNDRV_PCM_TRIGGER_XXX``h]hSNDRV_PCM_TRIGGER_XXX}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh defined in }(hj(hhhNhNubj)}(h````h]h }(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh. At least, the }(hj(hhhNhNubj)}(h ``START``h]hSTART}(hj()hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh and }(hj(hhhNhNubj)}(h``STOP``h]hSTOP}(hj:)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj(ubh+ commands must be defined in this callback:}(hj(hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj(hhubj)}(hswitch (cmd) { case SNDRV_PCM_TRIGGER_START: /* do something to start the PCM engine */ break; case SNDRV_PCM_TRIGGER_STOP: /* do something to stop the PCM engine */ break; default: return -EINVAL; }h]hswitch (cmd) { case SNDRV_PCM_TRIGGER_START: /* do something to start the PCM engine */ break; case SNDRV_PCM_TRIGGER_STOP: /* do something to stop the PCM engine */ break; default: return -EINVAL; }}hjR)sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj(hhubh)}(hWhen the PCM supports the pause operation (given in the info field of the hardware table), the ``PAUSE_PUSH`` and ``PAUSE_RELEASE`` commands must be handled here, too. The former is the command to pause the PCM, and the latter to restart the PCM again.h](h_When the PCM supports the pause operation (given in the info field of the hardware table), the }(hj`)hhhNhNubj)}(h``PAUSE_PUSH``h]h PAUSE_PUSH}(hjh)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj`)ubh and }(hj`)hhhNhNubj)}(h``PAUSE_RELEASE``h]h PAUSE_RELEASE}(hjz)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj`)ubhy commands must be handled here, too. The former is the command to pause the PCM, and the latter to restart the PCM again.}(hj`)hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj(hhubh)}(hXWhen the PCM supports the suspend/resume operation, regardless of full or partial suspend/resume support, the ``SUSPEND`` and ``RESUME`` commands must be handled, too. These commands are issued when the power-management status is changed. Obviously, the ``SUSPEND`` and ``RESUME`` commands suspend and resume the PCM substream, and usually, they are identical to the ``STOP`` and ``START`` commands, respectively. See the `Power Management`_ section for details.h](hnWhen the PCM supports the suspend/resume operation, regardless of full or partial suspend/resume support, the }(hj)hhhNhNubj)}(h ``SUSPEND``h]hSUSPEND}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubh and }(hj)hhhNhNubj)}(h ``RESUME``h]hRESUME}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubhv commands must be handled, too. These commands are issued when the power-management status is changed. Obviously, the }(hj)hhhNhNubj)}(h ``SUSPEND``h]hSUSPEND}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubh and }(hj)hhhNhNubj)}(h ``RESUME``h]hRESUME}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubhW commands suspend and resume the PCM substream, and usually, they are identical to the }(hj)hhhNhNubj)}(h``STOP``h]hSTOP}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubh and }hj)sbj)}(h ``START``h]hSTART}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubh! commands, respectively. See the }(hj)hhhNhNubh)}(h`Power Management`_h]hPower Management}(hj*hhhNhNubah}(h]h ]h"]h$]h&]namePower Managementjjuh1hhj)jKubh section for details.}(hj)hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj(hhubh)}(hXCAs mentioned, this callback is atomic by default unless the ``nonatomic`` flag set, and you cannot call functions which may sleep. The ``trigger`` callback should be as minimal as possible, just really triggering the DMA. The other stuff should be initialized in ``hw_params`` and ``prepare`` callbacks properly beforehand.h](h flag set, and you cannot call functions which may sleep. The }(hj *hhhNhNubj)}(h ``trigger``h]htrigger}(hj:*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj *ubhu callback should be as minimal as possible, just really triggering the DMA. The other stuff should be initialized in }(hj *hhhNhNubj)}(h ``hw_params``h]h hw_params}(hjL*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj *ubh and }(hj *hhhNhNubj)}(h ``prepare``h]hprepare}(hj^*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj *ubh callbacks properly beforehand.}(hj *hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj(hhubeh}(h]trigger-callbackah ]h"]trigger callbackah$]h&]uh1hhj#hhhhhMubh)}(hhh](h)}(hsync_stop callbackh]hsync_stop callback}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~*hhhhhMubj)}(hBstatic int snd_xxx_sync_stop(struct snd_pcm_substream *substream);h]hBstatic int snd_xxx_sync_stop(struct snd_pcm_substream *substream);}hj*sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj~*hhubh)}(hXThis callback is optional, and NULL can be passed. It's called after the PCM core stops the stream, before it changes the stream state via ``prepare``, ``hw_params`` or ``hw_free``. Since the IRQ handler might be still pending, we need to wait until the pending task finishes before moving to the next step; otherwise it might lead to a crash due to resource conflicts or access to freed resources. A typical behavior is to call a synchronization function like :c:func:`synchronize_irq()` here.h](hThis callback is optional, and NULL can be passed. It’s called after the PCM core stops the stream, before it changes the stream state via }(hj*hhhNhNubj)}(h ``prepare``h]hprepare}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj*ubh, }(hj*hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj*ubh or }(hj*hhhNhNubj)}(h ``hw_free``h]hhw_free}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj*ubhX. Since the IRQ handler might be still pending, we need to wait until the pending task finishes before moving to the next step; otherwise it might lead to a crash due to resource conflicts or access to freed resources. A typical behavior is to call a synchronization function like }(hj*hhhNhNubh)}(h:c:func:`synchronize_irq()`h]j)}(hj*h]hsynchronize_irq()}(hj*hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj*ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsynchronize_irquh1hhhhMhj*ubh here.}(hj*hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj~*hhubh)}(hXnFor the majority of drivers that need only a call of :c:func:`synchronize_irq()`, there is a simpler setup, too. While keeping the ``sync_stop`` PCM callback NULL, the driver can set the ``card->sync_irq`` field to the returned interrupt number after requesting an IRQ, instead. Then PCM core will call :c:func:`synchronize_irq()` with the given IRQ appropriately.h](h5For the majority of drivers that need only a call of }(hj+hhhNhNubh)}(h:c:func:`synchronize_irq()`h]j)}(hj+h]hsynchronize_irq()}(hj+hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj +ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsynchronize_irquh1hhhhMhj+ubh3, there is a simpler setup, too. While keeping the }(hj+hhhNhNubj)}(h ``sync_stop``h]h sync_stop}(hj/+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ubh+ PCM callback NULL, the driver can set the }(hj+hhhNhNubj)}(h``card->sync_irq``h]hcard->sync_irq}(hjA+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ubhd field to the returned interrupt number after requesting an IRQ, instead. Then PCM core will call }(hj+hhhNhNubh)}(h:c:func:`synchronize_irq()`h]j)}(hjU+h]hsynchronize_irq()}(hjW+hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjS+ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsynchronize_irquh1hhhhMhj+ubh" with the given IRQ appropriately.}(hj+hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj~*hhubh)}(hXIf the IRQ handler is released by the card destructor, you don't need to clear ``card->sync_irq``, as the card itself is being released. So, usually you'll need to add just a single line for assigning ``card->sync_irq`` in the driver code unless the driver re-acquires the IRQ. When the driver frees and re-acquires the IRQ dynamically (e.g. for suspend/resume), it needs to clear and re-set ``card->sync_irq`` again appropriately.h](hQIf the IRQ handler is released by the card destructor, you don’t need to clear }(hj|+hhhNhNubj)}(h``card->sync_irq``h]hcard->sync_irq}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|+ubhj, as the card itself is being released. So, usually you’ll need to add just a single line for assigning }(hj|+hhhNhNubj)}(h``card->sync_irq``h]hcard->sync_irq}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|+ubh in the driver code unless the driver re-acquires the IRQ. When the driver frees and re-acquires the IRQ dynamically (e.g. for suspend/resume), it needs to clear and re-set }(hj|+hhhNhNubj)}(h``card->sync_irq``h]hcard->sync_irq}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj|+ubh again appropriately.}(hj|+hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj~*hhubeh}(h]jah ]h"]sync_stop callbackah$]h&]uh1hhj#hhhhhMjKubh)}(hhh](h)}(hpointer callbackh]hpointer callback}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj+hhhhhMubj)}(hMstatic snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)h]hMstatic snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)}hj+sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj+hhubh)}(hThis callback is called when the PCM middle layer inquires the current hardware position in the buffer. The position must be returned in frames, ranging from 0 to ``buffer_size - 1``.h](hThis callback is called when the PCM middle layer inquires the current hardware position in the buffer. The position must be returned in frames, ranging from 0 to }(hj+hhhNhNubj)}(h``buffer_size - 1``h]hbuffer_size - 1}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ubh.}(hj+hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(hX+This is usually called from the buffer-update routine in the PCM middle layer, which is invoked when :c:func:`snd_pcm_period_elapsed()` is called by the interrupt routine. Then the PCM middle layer updates the position and calculates the available space, and wakes up the sleeping poll threads, etc.h](heThis is usually called from the buffer-update routine in the PCM middle layer, which is invoked when }(hj,hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj,h]hsnd_pcm_period_elapsed()}(hj,hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj,ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhj,ubh is called by the interrupt routine. Then the PCM middle layer updates the position and calculates the available space, and wakes up the sleeping poll threads, etc.}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubh)}(h(This callback is also atomic by default.h]h(This callback is also atomic by default.}(hj7,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+hhubeh}(h]pointer-callbackah ]h"]pointer callbackah$]h&]uh1hhj#hhhhhMubh)}(hhh](h)}(hcopy and fill_silence opsh]hcopy and fill_silence ops}(hjP,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjM,hhhhhMubh)}(hXThese callbacks are not mandatory, and can be omitted in most cases. These callbacks are used when the hardware buffer cannot be in the normal memory space. Some chips have their own buffer in the hardware which is not mappable. In such a case, you have to transfer the data manually from the memory buffer to the hardware buffer. Or, if the buffer is non-contiguous on both physical and virtual memory spaces, these callbacks must be defined, too.h]hXThese callbacks are not mandatory, and can be omitted in most cases. These callbacks are used when the hardware buffer cannot be in the normal memory space. Some chips have their own buffer in the hardware which is not mappable. In such a case, you have to transfer the data manually from the memory buffer to the hardware buffer. Or, if the buffer is non-contiguous on both physical and virtual memory spaces, these callbacks must be defined, too.}(hj^,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjM,hhubh)}(hIf these two callbacks are defined, copy and set-silence operations are done by them. The details will be described in the later section `Buffer and Memory Management`_.h](hIf these two callbacks are defined, copy and set-silence operations are done by them. The details will be described in the later section }(hjl,hhhNhNubh)}(h`Buffer and Memory Management`_h]hBuffer and Memory Management}(hjt,hhhNhNubah}(h]h ]h"]h$]h&]nameBuffer and Memory Managementjjuh1hhjl,jKubh.}(hjl,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjM,hhubeh}(h]copy-and-fill-silence-opsah ]h"]copy and fill_silence opsah$]h&]uh1hhj#hhhhhMubh)}(hhh](h)}(h ack callbackh]h ack callback}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj,hhhhhMubh)}(hXThis callback is also not mandatory. This callback is called when the ``appl_ptr`` is updated in read or write operations. Some drivers like emu10k1-fx and cs46xx need to track the current ``appl_ptr`` for the internal buffer, and this callback is useful only for such a purpose.h](hFThis callback is also not mandatory. This callback is called when the }(hj,hhhNhNubj)}(h ``appl_ptr``h]happl_ptr}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubhk is updated in read or write operations. Some drivers like emu10k1-fx and cs46xx need to track the current }(hj,hhhNhNubj)}(h ``appl_ptr``h]happl_ptr}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubhN for the internal buffer, and this callback is useful only for such a purpose.}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj,hhubh)}(hThe callback function may return 0 or a negative error. When the return value is ``-EPIPE``, PCM core treats that as a buffer XRUN, and changes the state to ``SNDRV_PCM_STATE_XRUN`` automatically.h](hQThe callback function may return 0 or a negative error. When the return value is }(hj,hhhNhNubj)}(h ``-EPIPE``h]h-EPIPE}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubhB, PCM core treats that as a buffer XRUN, and changes the state to }(hj,hhhNhNubj)}(h``SNDRV_PCM_STATE_XRUN``h]hSNDRV_PCM_STATE_XRUN}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubh automatically.}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj,hhubh)}(h#This callback is atomic by default.h]h#This callback is atomic by default.}(hj -hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj,hhubeh}(h] ack-callbackah ]h"] ack callbackah$]h&]uh1hhj#hhhhhMubh)}(hhh](h)}(h page callbackh]h page callback}(hj$-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj!-hhhhhMubh)}(hZThis callback is optional too. The mmap calls this callback to get the page fault address.h]hZThis callback is optional too. The mmap calls this callback to get the page fault address.}(hj2-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj!-hhubh)}(hvYou need no special callback for the standard SG-buffer or vmalloc- buffer. Hence this callback should be rarely used.h]hvYou need no special callback for the standard SG-buffer or vmalloc- buffer. Hence this callback should be rarely used.}(hj@-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj!-hhubeh}(h] page-callbackah ]h"] page callbackah$]h&]uh1hhj#hhhhhMubh)}(hhh](h)}(h mmap callbackh]h mmap callback}(hjY-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjV-hhhhhM ubh)}(hX-This is another optional callback for controlling mmap behavior. When defined, the PCM core calls this callback when a page is memory-mapped, instead of using the standard helper. If you need special handling (due to some architecture or device-specific issues), implement everything here as you like.h]hX-This is another optional callback for controlling mmap behavior. When defined, the PCM core calls this callback when a page is memory-mapped, instead of using the standard helper. If you need special handling (due to some architecture or device-specific issues), implement everything here as you like.}(hjg-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjV-hhubeh}(h] mmap-callbackah ]h"] mmap callbackah$]h&]uh1hhj#hhhhhM ubeh}(h]jah ]h"] operatorsah$]h&]uh1hhjhhhhhMjKubh)}(hhh](h)}(hPCM Interrupt Handlerh]hPCM Interrupt Handler}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj-hhhhhMubh)}(hXHThe remainder of the PCM stuff is the PCM interrupt handler. The role of the PCM interrupt handler in the sound driver is to update the buffer position and to tell the PCM middle layer when the buffer position goes across the specified period boundary. To inform about this, call the :c:func:`snd_pcm_period_elapsed()` function.h](hXThe remainder of the PCM stuff is the PCM interrupt handler. The role of the PCM interrupt handler in the sound driver is to update the buffer position and to tell the PCM middle layer when the buffer position goes across the specified period boundary. To inform about this, call the }(hj-hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj-h]hsnd_pcm_period_elapsed()}(hj-hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj-ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhj-ubh function.}(hj-hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj-hhubh)}(h;There are several ways sound chips can generate interrupts.h]h;There are several ways sound chips can generate interrupts.}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj-hhubh)}(hhh](h)}(h,Interrupts at the period (fragment) boundaryh]h,Interrupts at the period (fragment) boundary}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj-hhhhhM"ubh)}(hThis is the most frequently found type: the hardware generates an interrupt at each period boundary. In this case, you can call :c:func:`snd_pcm_period_elapsed()` at each interrupt.h](hThis is the most frequently found type: the hardware generates an interrupt at each period boundary. In this case, you can call }(hj-hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj-h]hsnd_pcm_period_elapsed()}(hj-hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj-ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhM$hj-ubh at each interrupt.}(hj-hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM$hj-hhubh)}(hX[:c:func:`snd_pcm_period_elapsed()` takes the substream pointer as its argument. Thus, you need to keep the substream pointer accessible from the chip instance. For example, define ``substream`` field in the chip record to hold the current running substream pointer, and set the pointer value at ``open`` callback (and reset at ``close`` callback).h](h)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj.h]hsnd_pcm_period_elapsed()}(hj.hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj.ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhM(hj.ubh takes the substream pointer as its argument. Thus, you need to keep the substream pointer accessible from the chip instance. For example, define }(hj.hhhNhNubj)}(h ``substream``h]h substream}(hj=.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj.ubhf field in the chip record to hold the current running substream pointer, and set the pointer value at }(hj.hhhNhNubj)}(h``open``h]hopen}(hjO.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj.ubh callback (and reset at }(hj.hhhNhNubj)}(h ``close``h]hclose}(hja.hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj.ubh callback).}(hj.hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM(hj-hhubh)}(hXIf you acquire a spinlock in the interrupt handler, and the lock is used in other PCM callbacks, too, then you have to release the lock before calling :c:func:`snd_pcm_period_elapsed()`, because :c:func:`snd_pcm_period_elapsed()` calls other PCM callbacks inside.h](hIf you acquire a spinlock in the interrupt handler, and the lock is used in other PCM callbacks, too, then you have to release the lock before calling }(hjy.hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj.h]hsnd_pcm_period_elapsed()}(hj.hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj.ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhM.hjy.ubh , because }(hjy.hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj.h]hsnd_pcm_period_elapsed()}(hj.hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj.ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhM.hjy.ubh" calls other PCM callbacks inside.}(hjy.hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM.hj-hhubh)}(hTypical code would look like::h]hTypical code would look like:}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hj-hhubj)}(hX static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; spin_lock(&chip->lock); .... if (pcm_irq_invoked(chip)) { /* call updater, unlock before it */ spin_unlock(&chip->lock); snd_pcm_period_elapsed(chip->substream); spin_lock(&chip->lock); /* acknowledge the interrupt if necessary */ } .... spin_unlock(&chip->lock); return IRQ_HANDLED; }h]hX static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; spin_lock(&chip->lock); .... if (pcm_irq_invoked(chip)) { /* call updater, unlock before it */ spin_unlock(&chip->lock); snd_pcm_period_elapsed(chip->substream); spin_lock(&chip->lock); /* acknowledge the interrupt if necessary */ } .... spin_unlock(&chip->lock); return IRQ_HANDLED; }}hj.sbah}(h]h ]h"]h$]h&]jjuh1jhhhM7hj-hhubh)}(hXZAlso, when the device can detect a buffer underrun/overrun, the driver can notify the XRUN status to the PCM core by calling :c:func:`snd_pcm_stop_xrun()`. This function stops the stream and sets the PCM state to ``SNDRV_PCM_STATE_XRUN``. Note that it must be called outside the PCM stream lock, hence it can't be called from the atomic callback.h](h}Also, when the device can detect a buffer underrun/overrun, the driver can notify the XRUN status to the PCM core by calling }(hj.hhhNhNubh)}(h:c:func:`snd_pcm_stop_xrun()`h]j)}(hj.h]hsnd_pcm_stop_xrun()}(hj.hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj.ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_stop_xrunuh1hhhhMHhj.ubh;. This function stops the stream and sets the PCM state to }(hj.hhhNhNubj)}(h``SNDRV_PCM_STATE_XRUN``h]hSNDRV_PCM_STATE_XRUN}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj.ubho. Note that it must be called outside the PCM stream lock, hence it can’t be called from the atomic callback.}(hj.hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMHhj-hhubeh}(h]*interrupts-at-the-period-fragment-boundaryah ]h"],interrupts at the period (fragment) boundaryah$]h&]uh1hhj-hhhhhM"ubh)}(hhh](h)}(hHigh frequency timer interruptsh]hHigh frequency timer interrupts}(hj7/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4/hhhhhMQubh)}(hXThis happens when the hardware doesn't generate interrupts at the period boundary but issues timer interrupts at a fixed timer rate (e.g. es1968 or ymfpci drivers). In this case, you need to check the current hardware position and accumulate the processed sample length at each interrupt. When the accumulated size exceeds the period size, call :c:func:`snd_pcm_period_elapsed()` and reset the accumulator.h](hX[This happens when the hardware doesn’t generate interrupts at the period boundary but issues timer interrupts at a fixed timer rate (e.g. es1968 or ymfpci drivers). In this case, you need to check the current hardware position and accumulate the processed sample length at each interrupt. When the accumulated size exceeds the period size, call }(hjE/hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hjO/h]hsnd_pcm_period_elapsed()}(hjQ/hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjM/ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMShjE/ubh and reset the accumulator.}(hjE/hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMShj4/hhubh)}(h$Typical code would look as follows::h]h#Typical code would look as follows:}(hjv/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMZhj4/hhubj)}(hXstatic irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; spin_lock(&chip->lock); .... if (pcm_irq_invoked(chip)) { unsigned int last_ptr, size; /* get the current hardware pointer (in frames) */ last_ptr = get_hw_ptr(chip); /* calculate the processed frames since the * last update */ if (last_ptr < chip->last_ptr) size = runtime->buffer_size + last_ptr - chip->last_ptr; else size = last_ptr - chip->last_ptr; /* remember the last updated point */ chip->last_ptr = last_ptr; /* accumulate the size */ chip->size += size; /* over the period boundary? */ if (chip->size >= runtime->period_size) { /* reset the accumulator */ chip->size %= runtime->period_size; /* call updater */ spin_unlock(&chip->lock); snd_pcm_period_elapsed(substream); spin_lock(&chip->lock); } /* acknowledge the interrupt if necessary */ } .... spin_unlock(&chip->lock); return IRQ_HANDLED; }h]hXstatic irqreturn_t snd_mychip_interrupt(int irq, void *dev_id) { struct mychip *chip = dev_id; spin_lock(&chip->lock); .... if (pcm_irq_invoked(chip)) { unsigned int last_ptr, size; /* get the current hardware pointer (in frames) */ last_ptr = get_hw_ptr(chip); /* calculate the processed frames since the * last update */ if (last_ptr < chip->last_ptr) size = runtime->buffer_size + last_ptr - chip->last_ptr; else size = last_ptr - chip->last_ptr; /* remember the last updated point */ chip->last_ptr = last_ptr; /* accumulate the size */ chip->size += size; /* over the period boundary? */ if (chip->size >= runtime->period_size) { /* reset the accumulator */ chip->size %= runtime->period_size; /* call updater */ spin_unlock(&chip->lock); snd_pcm_period_elapsed(substream); spin_lock(&chip->lock); } /* acknowledge the interrupt if necessary */ } .... spin_unlock(&chip->lock); return IRQ_HANDLED; }}hj/sbah}(h]h ]h"]h$]h&]jjuh1jhhhM]hj4/hhubeh}(h]high-frequency-timer-interruptsah ]h"]high frequency timer interruptsah$]h&]uh1hhj-hhhhhMQubh)}(hhh](h)}(h-On calling :c:func:`snd_pcm_period_elapsed()`h](h On calling }(hj/hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj/h]hsnd_pcm_period_elapsed()}(hj/hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj/ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhj/ubeh}(h]h ]h"]h$]h&]uh1hhj/hhhhhMubh)}(hIn both cases, even if more than one period has elapsed, you don't have to call :c:func:`snd_pcm_period_elapsed()` many times. Call only once. And the PCM layer will check the current hardware pointer and update to the latest status.h](hRIn both cases, even if more than one period has elapsed, you don’t have to call }(hj/hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj/h]hsnd_pcm_period_elapsed()}(hj/hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj/ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhj/ubhw many times. Call only once. And the PCM layer will check the current hardware pointer and update to the latest status.}(hj/hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj/hhubeh}(h]!on-calling-snd-pcm-period-elapsedah ]h"]#on calling snd_pcm_period_elapsed()ah$]h&]uh1hhj-hhhhhMubeh}(h]jOah ]h"]pcm interrupt handlerah$]h&]uh1hhjhhhhhMjKubh)}(hhh](h)}(h Atomicityh]h Atomicity}(hj 0hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 0hhhhhMubh)}(hX One of the most important (and thus difficult to debug) problems in kernel programming are race conditions. In the Linux kernel, they are usually avoided via spin-locks, mutexes or semaphores. In general, if a race condition can happen in an interrupt handler, it has to be managed atomically, and you have to use a spinlock to protect the critical section. If the critical section is not in interrupt handler code and if taking a relatively long time to execute is acceptable, you should use mutexes or semaphores instead.h]hX One of the most important (and thus difficult to debug) problems in kernel programming are race conditions. In the Linux kernel, they are usually avoided via spin-locks, mutexes or semaphores. In general, if a race condition can happen in an interrupt handler, it has to be managed atomically, and you have to use a spinlock to protect the critical section. If the critical section is not in interrupt handler code and if taking a relatively long time to execute is acceptable, you should use mutexes or semaphores instead.}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubh)}(hXfAs already seen, some PCM callbacks are atomic and some are not. For example, the ``hw_params`` callback is non-atomic, while the ``trigger`` callback is atomic. This means, the latter is called already in a spinlock held by the PCM middle layer, the PCM stream lock. Please take this atomicity into account when you choose a locking scheme in the callbacks.h](hRAs already seen, some PCM callbacks are atomic and some are not. For example, the }(hj)0hhhNhNubj)}(h ``hw_params``h]h hw_params}(hj10hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)0ubh# callback is non-atomic, while the }(hj)0hhhNhNubj)}(h ``trigger``h]htrigger}(hjC0hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)0ubh callback is atomic. This means, the latter is called already in a spinlock held by the PCM middle layer, the PCM stream lock. Please take this atomicity into account when you choose a locking scheme in the callbacks.}(hj)0hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubh)}(hXKIn the atomic callbacks, you cannot use functions which may call :c:func:`schedule()` or go to :c:func:`sleep()`. Semaphores and mutexes can sleep, and hence they cannot be used inside the atomic callbacks (e.g. ``trigger`` callback). To implement some delay in such a callback, please use :c:func:`udelay()` or :c:func:`mdelay()`.h](hAIn the atomic callbacks, you cannot use functions which may call }(hj[0hhhNhNubh)}(h:c:func:`schedule()`h]j)}(hje0h]h schedule()}(hjg0hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjc0ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjscheduleuh1hhhhMhj[0ubh or go to }(hj[0hhhNhNubh)}(h:c:func:`sleep()`h]j)}(hj0h]hsleep()}(hj0hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj0ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsleepuh1hhhhMhj[0ubhd. Semaphores and mutexes can sleep, and hence they cannot be used inside the atomic callbacks (e.g. }(hj[0hhhNhNubj)}(h ``trigger``h]htrigger}(hj0hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj[0ubhC callback). To implement some delay in such a callback, please use }(hj[0hhhNhNubh)}(h:c:func:`udelay()`h]j)}(hj0h]hudelay()}(hj0hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj0ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjudelayuh1hhhhMhj[0ubh or }(hj[0hhhNhNubh)}(h:c:func:`mdelay()`h]j)}(hj0h]hmdelay()}(hj0hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj0ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjmdelayuh1hhhhMhj[0ubh.}(hj[0hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubh)}(haAll three atomic callbacks (trigger, pointer, and ack) are called with local interrupts disabled.h]haAll three atomic callbacks (trigger, pointer, and ack) are called with local interrupts disabled.}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubh)}(hXtHowever, it is possible to request all PCM operations to be non-atomic. This assumes that all call sites are in non-atomic contexts. For example, the function :c:func:`snd_pcm_period_elapsed()` is called typically from the interrupt handler. But, if you set up the driver to use a threaded interrupt handler, this call can be in non-atomic context, too. In such a case, you can set the ``nonatomic`` field of the struct snd_pcm object after creating it. When this flag is set, mutex and rwsem are used internally in the PCM core instead of spin and rwlocks, so that you can call all PCM functions safely in a non-atomic context.h](hHowever, it is possible to request all PCM operations to be non-atomic. This assumes that all call sites are in non-atomic contexts. For example, the function }(hj1hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hj1h]hsnd_pcm_period_elapsed()}(hj!1hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj1ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhj1ubh is called typically from the interrupt handler. But, if you set up the driver to use a threaded interrupt handler, this call can be in non-atomic context, too. In such a case, you can set the }(hj1hhhNhNubj)}(h ``nonatomic``h]h nonatomic}(hj@1hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj1ubh field of the struct snd_pcm object after creating it. When this flag is set, mutex and rwsem are used internally in the PCM core instead of spin and rwlocks, so that you can call all PCM functions safely in a non-atomic context.}(hj1hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubh)}(hX2Also, in some cases, you might need to call :c:func:`snd_pcm_period_elapsed()` in the atomic context (e.g. the period gets elapsed during ``ack`` or other callback). There is a variant that can be called inside the PCM stream lock :c:func:`snd_pcm_period_elapsed_under_stream_lock()` for that purpose, too.h](h,Also, in some cases, you might need to call }(hjX1hhhNhNubh)}(h":c:func:`snd_pcm_period_elapsed()`h]j)}(hjb1h]hsnd_pcm_period_elapsed()}(hjd1hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj`1ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_period_elapseduh1hhhhMhjX1ubh< in the atomic context (e.g. the period gets elapsed during }(hjX1hhhNhNubj)}(h``ack``h]hack}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjX1ubhV or other callback). There is a variant that can be called inside the PCM stream lock }(hjX1hhhNhNubh)}(h4:c:func:`snd_pcm_period_elapsed_under_stream_lock()`h]j)}(hj1h]h*snd_pcm_period_elapsed_under_stream_lock()}(hj1hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj1ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj(snd_pcm_period_elapsed_under_stream_lockuh1hhhhMhjX1ubh for that purpose, too.}(hjX1hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj 0hhubeh}(h]j'ah ]h"] atomicityah$]h&]uh1hhjhhhhhMjKubh)}(hhh](h)}(h Constraintsh]h Constraints}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj1hhhhhMubh)}(h}Due to physical limitations, hardware is not infinitely configurable. These limitations are expressed by setting constraints.h]h}Due to physical limitations, hardware is not infinitely configurable. These limitations are expressed by setting constraints.}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj1hhubh)}(hFor example, in order to restrict the sample rates to some supported values, use :c:func:`snd_pcm_hw_constraint_list()`. You need to call this function in the open callback::h](hQFor example, in order to restrict the sample rates to some supported values, use }(hj1hhhNhNubh)}(h&:c:func:`snd_pcm_hw_constraint_list()`h]j)}(hj1h]hsnd_pcm_hw_constraint_list()}(hj1hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj1ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_hw_constraint_listuh1hhhhMhj1ubh6. You need to call this function in the open callback:}(hj1hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj1hhubj)}(hX7static unsigned int rates[] = {4000, 10000, 22050, 44100}; static struct snd_pcm_hw_constraint_list constraints_rates = { .count = ARRAY_SIZE(rates), .list = rates, .mask = 0, }; static int snd_mychip_pcm_open(struct snd_pcm_substream *substream) { int err; .... err = snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &constraints_rates); if (err < 0) return err; .... }h]hX7static unsigned int rates[] = {4000, 10000, 22050, 44100}; static struct snd_pcm_hw_constraint_list constraints_rates = { .count = ARRAY_SIZE(rates), .list = rates, .mask = 0, }; static int snd_mychip_pcm_open(struct snd_pcm_substream *substream) { int err; .... err = snd_pcm_hw_constraint_list(substream->runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &constraints_rates); if (err < 0) return err; .... }}hj2sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj1hhubh)}(hX{There are many different constraints. Look at ``sound/pcm.h`` for a complete list. You can even define your own constraint rules. For example, let's suppose my_chip can manage a substream of 1 channel if and only if the format is ``S16_LE``, otherwise it supports any format specified in struct snd_pcm_hardware (or in any other constraint_list). You can build a rule like this::h](h.There are many different constraints. Look at }(hj#2hhhNhNubj)}(h``sound/pcm.h``h]h sound/pcm.h}(hj+2hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#2ubh for a complete list. You can even define your own constraint rules. For example, let’s suppose my_chip can manage a substream of 1 channel if and only if the format is }(hj#2hhhNhNubj)}(h ``S16_LE``h]hS16_LE}(hj=2hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj#2ubh, otherwise it supports any format specified in struct snd_pcm_hardware (or in any other constraint_list). You can build a rule like this:}(hj#2hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj1hhubj)}(hXWstatic int hw_rule_channels_by_format(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); struct snd_interval ch; snd_interval_any(&ch); if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) { ch.min = ch.max = 1; ch.integer = 1; return snd_interval_refine(c, &ch); } return 0; }h]hXWstatic int hw_rule_channels_by_format(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); struct snd_interval ch; snd_interval_any(&ch); if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) { ch.min = ch.max = 1; ch.integer = 1; return snd_interval_refine(c, &ch); } return 0; }}hjU2sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj1hhubh)}(h6Then you need to call this function to add your rule::h]h5Then you need to call this function to add your rule:}(hjc2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj1hhubj)}(hsnd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, hw_rule_channels_by_format, NULL, SNDRV_PCM_HW_PARAM_FORMAT, -1);h]hsnd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, hw_rule_channels_by_format, NULL, SNDRV_PCM_HW_PARAM_FORMAT, -1);}hjq2sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj1hhubh)}(hThe rule function is called when an application sets the PCM format, and it refines the number of channels accordingly. But an application may set the number of channels before setting the format. Thus you also need to define the inverse rule::h]hThe rule function is called when an application sets the PCM format, and it refines the number of channels accordingly. But an application may set the number of channels before setting the format. Thus you also need to define the inverse rule:}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj1hhubj)}(hX7static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); struct snd_mask fmt; snd_mask_any(&fmt); /* Init the struct */ if (c->min < 2) { fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE; return snd_mask_refine(f, &fmt); } return 0; }h]hX7static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) { struct snd_interval *c = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); struct snd_mask fmt; snd_mask_any(&fmt); /* Init the struct */ if (c->min < 2) { fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE; return snd_mask_refine(f, &fmt); } return 0; }}hj2sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj1hhubh)}(h... and in the open callback::h]h... and in the open callback:}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj1hhubj)}(hsnd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, hw_rule_format_by_channels, NULL, SNDRV_PCM_HW_PARAM_CHANNELS, -1);h]hsnd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, hw_rule_format_by_channels, NULL, SNDRV_PCM_HW_PARAM_CHANNELS, -1);}hj2sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj1hhubh)}(hXOne typical usage of the hw constraints is to align the buffer size with the period size. By default, ALSA PCM core doesn't enforce the buffer size to be aligned with the period size. For example, it'd be possible to have a combination like 256 period bytes with 999 buffer bytes.h]hXOne typical usage of the hw constraints is to align the buffer size with the period size. By default, ALSA PCM core doesn’t enforce the buffer size to be aligned with the period size. For example, it’d be possible to have a combination like 256 period bytes with 999 buffer bytes.}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj1hhubh)}(hMany device chips, however, require the buffer to be a multiple of periods. In such a case, call :c:func:`snd_pcm_hw_constraint_integer()` for ``SNDRV_PCM_HW_PARAM_PERIODS``::h](hbMany device chips, however, require the buffer to be a multiple of periods. In such a case, call }(hj2hhhNhNubh)}(h):c:func:`snd_pcm_hw_constraint_integer()`h]j)}(hj2h]hsnd_pcm_hw_constraint_integer()}(hj2hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj2ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_hw_constraint_integeruh1hhhhM hj2ubh for }(hj2hhhNhNubj)}(h``SNDRV_PCM_HW_PARAM_PERIODS``h]hSNDRV_PCM_HW_PARAM_PERIODS}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj2ubh:}(hj2hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj1hhubj)}(hlsnd_pcm_hw_constraint_integer(substream->runtime, SNDRV_PCM_HW_PARAM_PERIODS);h]hlsnd_pcm_hw_constraint_integer(substream->runtime, SNDRV_PCM_HW_PARAM_PERIODS);}hj3sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj1hhubh)}(hjThis assures that the number of periods is integer, hence the buffer size is aligned with the period size.h]hjThis assures that the number of periods is integer, hence the buffer size is aligned with the period size.}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM" hj1hhubh)}(hThe hw constraint is a very powerful mechanism to define the preferred PCM configuration, and there are relevant helpers. I won't give more details here, rather I would like to say, “Luke, use the source.”h]hThe hw constraint is a very powerful mechanism to define the preferred PCM configuration, and there are relevant helpers. I won’t give more details here, rather I would like to say, “Luke, use the source.” }(hj$3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM% hj1hhubeh}(h]j$ah ]h"] constraintsah$]h&]uh1hhjhhhhhMjKubeh}(h]j ah ]h"] pcm interfaceah$]h&]uh1hhhhhhhhMHjKubh)}(hhh](h)}(hControl Interfaceh]hControl Interface}(hjC3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@3hhhhhM+ ubh)}(hhh](h)}(hGeneralh]hGeneral}(hjT3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQ3hhhhhM. ubh)}(hThe control interface is used widely for many switches, sliders, etc. which are accessed from user-space. Its most important use is the mixer interface. In other words, since ALSA 0.9.x, all the mixer stuff is implemented on the control kernel API.h]hThe control interface is used widely for many switches, sliders, etc. which are accessed from user-space. Its most important use is the mixer interface. In other words, since ALSA 0.9.x, all the mixer stuff is implemented on the control kernel API.}(hjb3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM0 hjQ3hhubh)}(h}ALSA has a well-defined AC97 control module. If your chip supports only the AC97 and nothing else, you can skip this section.h]h}ALSA has a well-defined AC97 control module. If your chip supports only the AC97 and nothing else, you can skip this section.}(hjp3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM5 hjQ3hhubh)}(hlThe control API is defined in ````. Include this file if you want to add your own controls.h](hThe control API is defined in }(hj~3hhhNhNubj)}(h````h]h}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~3ubh9. Include this file if you want to add your own controls.}(hj~3hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM8 hjQ3hhubeh}(h]id4ah ]h"]h$]generalah&]uh1hhj@3hhhhhM. jKubh)}(hhh](h)}(hDefinition of Controlsh]hDefinition of Controls}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj3hhhhhM< ubh)}(hTo create a new control, you need to define the following three callbacks: ``info``, ``get`` and ``put``. Then, define a struct snd_kcontrol_new record, such as::h](hKTo create a new control, you need to define the following three callbacks: }(hj3hhhNhNubj)}(h``info``h]hinfo}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj3ubh, }(hj3hhhNhNubj)}(h``get``h]hget}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj3ubh and }(hj3hhhNhNubj)}(h``put``h]hput}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj3ubh9. Then, define a struct snd_kcontrol_new record, such as:}(hj3hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM> hj3hhubj)}(hXJstatic struct snd_kcontrol_new my_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "PCM Playback Switch", .index = 0, .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, .private_value = 0xffff, .info = my_control_info, .get = my_control_get, .put = my_control_put };h]hXJstatic struct snd_kcontrol_new my_control = { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = "PCM Playback Switch", .index = 0, .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, .private_value = 0xffff, .info = my_control_info, .get = my_control_get, .put = my_control_put };}hj3sbah}(h]h ]h"]h$]h&]jjuh1jhhhMC hj3hhubh)}(hXThe ``iface`` field specifies the control type, ``SNDRV_CTL_ELEM_IFACE_XXX``, which is usually ``MIXER``. Use ``CARD`` for global controls that are not logically part of the mixer. If the control is closely associated with some specific device on the sound card, use ``HWDEP``, ``PCM``, ``RAWMIDI``, ``TIMER``, or ``SEQUENCER``, and specify the device number with the ``device`` and ``subdevice`` fields.h](hThe }(hj 4hhhNhNubj)}(h ``iface``h]hiface}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh# field specifies the control type, }(hj 4hhhNhNubj)}(h``SNDRV_CTL_ELEM_IFACE_XXX``h]hSNDRV_CTL_ELEM_IFACE_XXX}(hj#4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh, which is usually }(hj 4hhhNhNubj)}(h ``MIXER``h]hMIXER}(hj54hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh. Use }(hj 4hhhNhNubj)}(h``CARD``h]hCARD}(hjG4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh for global controls that are not logically part of the mixer. If the control is closely associated with some specific device on the sound card, use }(hj 4hhhNhNubj)}(h ``HWDEP``h]hHWDEP}(hjY4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh, }(hj 4hhhNhNubj)}(h``PCM``h]hPCM}(hjk4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh, }hj 4sbj)}(h ``RAWMIDI``h]hRAWMIDI}(hj}4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh, }hj 4sbj)}(h ``TIMER``h]hTIMER}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh, or }(hj 4hhhNhNubj)}(h ``SEQUENCER``h]h SEQUENCER}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh), and specify the device number with the }(hj 4hhhNhNubj)}(h ``device``h]hdevice}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh and }(hj 4hhhNhNubj)}(h ``subdevice``h]h subdevice}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj 4ubh fields.}(hj 4hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMO hj3hhubh)}(hThe ``name`` is the name identifier string. Since ALSA 0.9.x, the control name is very important, because its role is classified from its name. There are pre-defined standard control names. The details are described in the `Control Names`_ subsection.h](hThe }(hj4hhhNhNubj)}(h``name``h]hname}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubh is the name identifier string. Since ALSA 0.9.x, the control name is very important, because its role is classified from its name. There are pre-defined standard control names. The details are described in the }(hj4hhhNhNubh)}(h`Control Names`_h]h Control Names}(hj4hhhNhNubah}(h]h ]h"]h$]h&]name Control Namesj control-namesuh1hhj4jKubh subsection.}(hj4hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMW hj3hhubh)}(hXThe ``index`` field holds the index number of this control. If there are several different controls with the same name, they can be distinguished by the index number. This is the case when several codecs exist on the card. If the index is zero, you can omit the definition above.h](hThe }(hj5hhhNhNubj)}(h ``index``h]hindex}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5ubhX  field holds the index number of this control. If there are several different controls with the same name, they can be distinguished by the index number. This is the case when several codecs exist on the card. If the index is zero, you can omit the definition above.}(hj5hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM\ hj3hhubh)}(hThe ``access`` field contains the access type of this control. Give the combination of bit masks, ``SNDRV_CTL_ELEM_ACCESS_XXX``, there. The details will be explained in the `Access Flags`_ subsection.h](hThe }(hj25hhhNhNubj)}(h ``access``h]haccess}(hj:5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj25ubhT field contains the access type of this control. Give the combination of bit masks, }(hj25hhhNhNubj)}(h``SNDRV_CTL_ELEM_ACCESS_XXX``h]hSNDRV_CTL_ELEM_ACCESS_XXX}(hjL5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj25ubh., there. The details will be explained in the }(hj25hhhNhNubh)}(h`Access Flags`_h]h Access Flags}(hj^5hhhNhNubah}(h]h ]h"]h$]h&]name Access Flagsj access-flagsuh1hhj25jKubh subsection.}(hj25hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMb hj3hhubh)}(hXiThe ``private_value`` field contains an arbitrary long integer value for this record. When using the generic ``info``, ``get`` and ``put`` callbacks, you can pass a value through this field. If several small numbers are necessary, you can combine them in bitwise. Or, it's possible to store a pointer (casted to unsigned long) of some record in this field, too.h](hThe }(hjy5hhhNhNubj)}(h``private_value``h]h private_value}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy5ubhX field contains an arbitrary long integer value for this record. When using the generic }(hjy5hhhNhNubj)}(h``info``h]hinfo}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy5ubh, }(hjy5hhhNhNubj)}(h``get``h]hget}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy5ubh and }(hjy5hhhNhNubj)}(h``put``h]hput}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy5ubh callbacks, you can pass a value through this field. If several small numbers are necessary, you can combine them in bitwise. Or, it’s possible to store a pointer (casted to unsigned long) of some record in this field, too.}(hjy5hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMg hj3hhubh)}(hdThe ``tlv`` field can be used to provide metadata about the control; see the `Metadata`_ subsection.h](hThe }(hj5hhhNhNubj)}(h``tlv``h]htlv}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5ubhB field can be used to provide metadata about the control; see the }(hj5hhhNhNubh)}(h `Metadata`_h]hMetadata}(hj5hhhNhNubah}(h]h ]h"]h$]h&]nameMetadatajmetadatauh1hhj5jKubh subsection.}(hj5hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMn hj3hhubh)}(h)The other three are `Control Callbacks`_.h](hThe other three are }(hj6hhhNhNubh)}(h`Control Callbacks`_h]hControl Callbacks}(hj 6hhhNhNubah}(h]h ]h"]h$]h&]nameControl Callbacksjcontrol-callbacksuh1hhj6jKubh.}(hj6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMq hj3hhubeh}(h]definition-of-controlsah ]h"]definition of controlsah$]h&]uh1hhj@3hhhhhM< ubh)}(hhh](h)}(h Control Namesh]h Control Names}(hj26hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj/6hhhhhMt ubh)}(hThere are some standards to define the control names. A control is usually defined from the three parts as “SOURCE DIRECTION FUNCTION”.h]hThere are some standards to define the control names. A control is usually defined from the three parts as “SOURCE DIRECTION FUNCTION”.}(hj@6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMv hj/6hhubh)}(hThe first, ``SOURCE``, specifies the source of the control, and is a string such as “Master”, “PCM”, “CD” and “Line”. There are many pre-defined sources.h](h The first, }(hjN6hhhNhNubj)}(h ``SOURCE``h]hSOURCE}(hjV6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjN6ubh, specifies the source of the control, and is a string such as “Master”, “PCM”, “CD” and “Line”. There are many pre-defined sources.}(hjN6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMy hj/6hhubh)}(hThe second, ``DIRECTION``, is one of the following strings according to the direction of the control: “Playback”, “Capture”, “Bypass Playback” and “Bypass Capture”. Or, it can be omitted, meaning both playback and capture directions.h](h The second, }(hjn6hhhNhNubj)}(h ``DIRECTION``h]h DIRECTION}(hjv6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjn6ubh, is one of the following strings according to the direction of the control: “Playback”, “Capture”, “Bypass Playback” and “Bypass Capture”. Or, it can be omitted, meaning both playback and capture directions.}(hjn6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM} hj/6hhubh)}(hThe third, ``FUNCTION``, is one of the following strings according to the function of the control: “Switch”, “Volume” and “Route”.h](h The third, }(hj6hhhNhNubj)}(h ``FUNCTION``h]hFUNCTION}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj6ubhw, is one of the following strings according to the function of the control: “Switch”, “Volume” and “Route”.}(hj6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj/6hhubh)}(haThe example of control names are, thus, “Master Capture Switch” or “PCM Playback Volume”.h]haThe example of control names are, thus, “Master Capture Switch” or “PCM Playback Volume”.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj/6hhubh)}(hThere are some exceptions:h]hThere are some exceptions:}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj/6hhubh)}(hhh](h)}(hGlobal capture and playbackh]hGlobal capture and playback}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6hhhhhM ubh)}(h“Capture Source”, “Capture Switch” and “Capture Volume” are used for the global capture (input) source, switch and volume. Similarly, “Playback Switch” and “Playback Volume” are used for the global output gain switch and volume.h]h“Capture Source”, “Capture Switch” and “Capture Volume” are used for the global capture (input) source, switch and volume. Similarly, “Playback Switch” and “Playback Volume” are used for the global output gain switch and volume.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj6hhubeh}(h]global-capture-and-playbackah ]h"]global capture and playbackah$]h&]uh1hhj/6hhhhhM ubh)}(hhh](h)}(h Tone-controlsh]h Tone-controls}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6hhhhhM ubh)}(htone-control switch and volumes are specified like “Tone Control - XXX”, e.g. “Tone Control - Switch”, “Tone Control - Bass”, “Tone Control - Center”.h]htone-control switch and volumes are specified like “Tone Control - XXX”, e.g. “Tone Control - Switch”, “Tone Control - Bass”, “Tone Control - Center”.}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj6hhubeh}(h] tone-controlsah ]h"] tone-controlsah$]h&]uh1hhj/6hhhhhM ubh)}(hhh](h)}(h 3D controlsh]h 3D controls}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj7hhhhhM ubh)}(h3D-control switches and volumes are specified like “3D Control - XXX”, e.g. “3D Control - Switch”, “3D Control - Center”, “3D Control - Space”.h]h3D-control switches and volumes are specified like “3D Control - XXX”, e.g. “3D Control - Switch”, “3D Control - Center”, “3D Control - Space”.}(hj)7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubeh}(h] d-controlsah ]h"] 3d controlsah$]h&]uh1hhj/6hhhhhM ubh)}(hhh](h)}(h Mic boosth]h Mic boost}(hjB7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj?7hhhhhM ubh)}(hDMic-boost switch is set as “Mic Boost” or “Mic Boost (6dB)”.h]hDMic-boost switch is set as “Mic Boost” or “Mic Boost (6dB)”.}(hjP7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj?7hhubh)}(h[More precise information can be found in ``Documentation/sound/designs/control-names.rst``.h](h)More precise information can be found in }(hj^7hhhNhNubj)}(h1``Documentation/sound/designs/control-names.rst``h]h-Documentation/sound/designs/control-names.rst}(hjf7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^7ubh.}(hj^7hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj?7hhubeh}(h] mic-boostah ]h"] mic boostah$]h&]uh1hhj/6hhhhhM ubeh}(h]j5ah ]h"] control namesah$]h&]uh1hhj@3hhhhhMt jKubh)}(hhh](h)}(h Access Flagsh]h Access Flags}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj7hhhhhM ubh)}(hX2The access flag is the bitmask which specifies the access type of the given control. The default access type is ``SNDRV_CTL_ELEM_ACCESS_READWRITE``, which means both read and write are allowed to this control. When the access flag is omitted (i.e. = 0), it is considered as ``READWRITE`` access by default.h](hpThe access flag is the bitmask which specifies the access type of the given control. The default access type is }(hj7hhhNhNubj)}(h#``SNDRV_CTL_ELEM_ACCESS_READWRITE``h]hSNDRV_CTL_ELEM_ACCESS_READWRITE}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh, which means both read and write are allowed to this control. When the access flag is omitted (i.e. = 0), it is considered as }(hj7hhhNhNubj)}(h ``READWRITE``h]h READWRITE}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh access by default.}(hj7hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubh)}(hX When the control is read-only, pass ``SNDRV_CTL_ELEM_ACCESS_READ`` instead. In this case, you don't have to define the ``put`` callback. Similarly, when the control is write-only (although it's a rare case), you can use the ``WRITE`` flag instead, and you don't need the ``get`` callback.h](h$When the control is read-only, pass }(hj7hhhNhNubj)}(h``SNDRV_CTL_ELEM_ACCESS_READ``h]hSNDRV_CTL_ELEM_ACCESS_READ}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh7 instead. In this case, you don’t have to define the }(hj7hhhNhNubj)}(h``put``h]hput}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubhd callback. Similarly, when the control is write-only (although it’s a rare case), you can use the }(hj7hhhNhNubj)}(h ``WRITE``h]hWRITE}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh( flag instead, and you don’t need the }(hj7hhhNhNubj)}(h``get``h]hget}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubh callback.}(hj7hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubh)}(hIf the control value changes frequently (e.g. the VU meter), ``VOLATILE`` flag should be given. This means that the control may be changed without `Change notification`_. Applications should poll such a control constantly.h](h=If the control value changes frequently (e.g. the VU meter), }(hj&8hhhNhNubj)}(h ``VOLATILE``h]hVOLATILE}(hj.8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&8ubhJ flag should be given. This means that the control may be changed without }(hj&8hhhNhNubh)}(h`Change notification`_h]hChange notification}(hj@8hhhNhNubah}(h]h ]h"]h$]h&]nameChange notificationjchange-notificationuh1hhj&8jKubh5. Applications should poll such a control constantly.}(hj&8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubh)}(hWhen the control may be updated, but currently has no effect on anything, setting the ``INACTIVE`` flag may be appropriate. For example, PCM controls should be inactive while no PCM device is open.h](hVWhen the control may be updated, but currently has no effect on anything, setting the }(hj[8hhhNhNubj)}(h ``INACTIVE``h]hINACTIVE}(hjc8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj[8ubhc flag may be appropriate. For example, PCM controls should be inactive while no PCM device is open.}(hj[8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubh)}(hGThere are ``LOCK`` and ``OWNER`` flags to change the write permissions.h](h There are }(hj{8hhhNhNubj)}(h``LOCK``h]hLOCK}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj{8ubh and }(hj{8hhhNhNubj)}(h ``OWNER``h]hOWNER}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj{8ubh' flags to change the write permissions.}(hj{8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7hhubeh}(h]jn5ah ]h"] access flagsah$]h&]uh1hhj@3hhhhhM jKubh)}(hhh](h)}(hControl Callbacksh]hControl Callbacks}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8hhhhhM ubh)}(hhh](h)}(h info callbackh]h info callback}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8hhhhhM ubh)}(hThe ``info`` callback is used to get detailed information on this control. This must store the values of the given struct snd_ctl_elem_info object. For example, for a boolean control with a single element::h](hThe }(hj8hhhNhNubj)}(h``info``h]hinfo}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj8ubh callback is used to get detailed information on this control. This must store the values of the given struct snd_ctl_elem_info object. For example, for a boolean control with a single element:}(hj8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj8hhubj)}(hX%static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 1; return 0; }h]hX%static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 1; return 0; }}hj8sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj8hhubh)}(hXThe ``type`` field specifies the type of the control. There are ``BOOLEAN``, ``INTEGER``, ``ENUMERATED``, ``BYTES``, ``IEC958`` and ``INTEGER64``. The ``count`` field specifies the number of elements in this control. For example, a stereo volume would have count = 2. The ``value`` field is a union, and the values stored depend on the type. The boolean and integer types are identical.h](hThe }(hj9hhhNhNubj)}(h``type``h]htype}(hj 9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh4 field specifies the type of the control. There are }(hj9hhhNhNubj)}(h ``BOOLEAN``h]hBOOLEAN}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh, }(hj9hhhNhNubj)}(h ``INTEGER``h]hINTEGER}(hj09hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh, }hj9sbj)}(h``ENUMERATED``h]h ENUMERATED}(hjB9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh, }hj9sbj)}(h ``BYTES``h]hBYTES}(hjT9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh, }hj9sbj)}(h ``IEC958``h]hIEC958}(hjf9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh and }(hj9hhhNhNubj)}(h ``INTEGER64``h]h INTEGER64}(hjx9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh. The }(hj9hhhNhNubj)}(h ``count``h]hcount}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubhp field specifies the number of elements in this control. For example, a stereo volume would have count = 2. The }(hj9hhhNhNubj)}(h ``value``h]hvalue}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubhi field is a union, and the values stored depend on the type. The boolean and integer types are identical.}(hj9hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj8hhubh)}(hsThe enumerated type is a bit different from the others. You'll need to set the string for the selectec item index::h]htThe enumerated type is a bit different from the others. You’ll need to set the string for the selectec item index:}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj8hhubj)}(hX+static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static char *texts[4] = { "First", "Second", "Third", "Fourth" }; uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; uinfo->count = 1; uinfo->value.enumerated.items = 4; if (uinfo->value.enumerated.item > 3) uinfo->value.enumerated.item = 3; strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); return 0; }h]hX+static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static char *texts[4] = { "First", "Second", "Third", "Fourth" }; uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; uinfo->count = 1; uinfo->value.enumerated.items = 4; if (uinfo->value.enumerated.item > 3) uinfo->value.enumerated.item = 3; strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); return 0; }}hj9sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj8hhubh)}(hThe above callback can be simplified with a helper function, :c:func:`snd_ctl_enum_info()`. The final code looks like below. (You can pass ``ARRAY_SIZE(texts)`` instead of 4 in the third argument; it's a matter of taste.)h](h=The above callback can be simplified with a helper function, }(hj9hhhNhNubh)}(h:c:func:`snd_ctl_enum_info()`h]j)}(hj9h]hsnd_ctl_enum_info()}(hj9hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj9ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ctl_enum_infouh1hhhhM hj9ubh1. The final code looks like below. (You can pass }(hj9hhhNhNubj)}(h``ARRAY_SIZE(texts)``h]hARRAY_SIZE(texts)}(hj9hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9ubh? instead of 4 in the third argument; it’s a matter of taste.)}(hj9hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj8hhubj)}(hXstatic int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static char *texts[4] = { "First", "Second", "Third", "Fourth" }; return snd_ctl_enum_info(uinfo, 1, 4, texts); }h]hXstatic int snd_myctl_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) { static char *texts[4] = { "First", "Second", "Third", "Fourth" }; return snd_ctl_enum_info(uinfo, 1, 4, texts); }}hj:sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj8hhubh)}(hXBSome common info callbacks are available for your convenience: :c:func:`snd_ctl_boolean_mono_info()` and :c:func:`snd_ctl_boolean_stereo_info()`. Obviously, the former is an info callback for a mono channel boolean item, just like :c:func:`snd_myctl_mono_info()` above, and the latter is for a stereo channel boolean item.h](h?Some common info callbacks are available for your convenience: }(hj!:hhhNhNubh)}(h%:c:func:`snd_ctl_boolean_mono_info()`h]j)}(hj+:h]hsnd_ctl_boolean_mono_info()}(hj-:hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj):ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ctl_boolean_mono_infouh1hhhhM hj!:ubh and }(hj!:hhhNhNubh)}(h':c:func:`snd_ctl_boolean_stereo_info()`h]j)}(hjN:h]hsnd_ctl_boolean_stereo_info()}(hjP:hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjL:ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ctl_boolean_stereo_infouh1hhhhM hj!:ubhW. Obviously, the former is an info callback for a mono channel boolean item, just like }(hj!:hhhNhNubh)}(h:c:func:`snd_myctl_mono_info()`h]j)}(hjq:h]hsnd_myctl_mono_info()}(hjs:hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjo:ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_myctl_mono_infouh1hhhhM hj!:ubh< above, and the latter is for a stereo channel boolean item.}(hj!:hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj8hhubeh}(h] info-callbackah ]h"] info callbackah$]h&]uh1hhj8hhhhhM ubh)}(hhh](h)}(h get callbackh]h get callback}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj:hhhhhM ubh)}(hdThis callback is used to read the current value of the control, so it can be returned to user-space.h]hdThis callback is used to read the current value of the control, so it can be returned to user-space.}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj:hhubh)}(h For example::h]h For example:}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj:hhubj)}(hXstatic int snd_myctl_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct mychip *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = get_some_value(chip); return 0; }h]hXstatic int snd_myctl_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct mychip *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = get_some_value(chip); return 0; }}hj:sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj:hhubh)}(hThe ``value`` field depends on the type of control as well as on the info callback. For example, the sb driver uses this field to store the register offset, the bit-shift and the bit-mask. The ``private_value`` field is set as follows::h](hThe }(hj:hhhNhNubj)}(h ``value``h]hvalue}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj:ubh field depends on the type of control as well as on the info callback. For example, the sb driver uses this field to store the register offset, the bit-shift and the bit-mask. The }(hj:hhhNhNubj)}(h``private_value``h]h private_value}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj:ubh field is set as follows:}(hj:hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj:hhubj)}(h3.private_value = reg | (shift << 16) | (mask << 24)h]h3.private_value = reg | (shift << 16) | (mask << 24)}hj ;sbah}(h]h ]h"]h$]h&]jjuh1jhhhM" hj:hhubh)}(h$and is retrieved in callbacks like::h]h#and is retrieved in callbacks like:}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM$ hj:hhubj)}(hXAstatic int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 16) & 0xff; int mask = (kcontrol->private_value >> 24) & 0xff; .... }h]hXAstatic int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { int reg = kcontrol->private_value & 0xff; int shift = (kcontrol->private_value >> 16) & 0xff; int mask = (kcontrol->private_value >> 24) & 0xff; .... }}hj);sbah}(h]h ]h"]h$]h&]jjuh1jhhhM& hj:hhubh)}(hIn the ``get`` callback, you have to fill all the elements if the control has more than one element, i.e. ``count > 1``. In the example above, we filled only one element (``value.integer.value[0]``) since ``count = 1`` is assumed.h](hIn the }(hj7;hhhNhNubj)}(h``get``h]hget}(hj?;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7;ubh\ callback, you have to fill all the elements if the control has more than one element, i.e. }(hj7;hhhNhNubj)}(h ``count > 1``h]h count > 1}(hjQ;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7;ubh4. In the example above, we filled only one element (}(hj7;hhhNhNubj)}(h``value.integer.value[0]``h]hvalue.integer.value[0]}(hjc;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7;ubh) since }(hj7;hhhNhNubj)}(h ``count = 1``h]h count = 1}(hju;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7;ubh is assumed.}(hj7;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM/ hj:hhubeh}(h] get-callbackah ]h"] get callbackah$]h&]uh1hhj8hhhhhM ubh)}(hhh](h)}(h put callbackh]h put callback}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj;hhhhhM5 ubh)}(h>This callback is used to write a value coming from user-space.h]h>This callback is used to write a value coming from user-space.}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM7 hj;hhubh)}(h For example::h]h For example:}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM9 hj;hhubj)}(hXstatic int snd_myctl_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct mychip *chip = snd_kcontrol_chip(kcontrol); int changed = 0; if (chip->current_value != ucontrol->value.integer.value[0]) { change_current_value(chip, ucontrol->value.integer.value[0]); changed = 1; } return changed; }h]hXstatic int snd_myctl_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) { struct mychip *chip = snd_kcontrol_chip(kcontrol); int changed = 0; if (chip->current_value != ucontrol->value.integer.value[0]) { change_current_value(chip, ucontrol->value.integer.value[0]); changed = 1; } return changed; }}hj;sbah}(h]h ]h"]h$]h&]jjuh1jhhhM; hj;hhubh)}(hAs seen above, you have to return 1 if the value is changed. If the value is not changed, return 0 instead. If any fatal error happens, return a negative error code as usual.h]hAs seen above, you have to return 1 if the value is changed. If the value is not changed, return 0 instead. If any fatal error happens, return a negative error code as usual.}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMK hj;hhubh)}(h}As in the ``get`` callback, when the control has more than one element, all elements must be evaluated in this callback, too.h](h As in the }(hj;hhhNhNubj)}(h``get``h]hget}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj;ubhl callback, when the control has more than one element, all elements must be evaluated in this callback, too.}(hj;hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMO hj;hhubeh}(h] put-callbackah ]h"] put callbackah$]h&]uh1hhj8hhhhhM5 ubh)}(hhh](h)}(hCallbacks are not atomich]hCallbacks are not atomic}(hj <hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj<hhhhhMS ubh)}(h)All these three callbacks are not-atomic.h]h)All these three callbacks are not-atomic.}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMU hj<hhubeh}(h]callbacks-are-not-atomicah ]h"]callbacks are not atomicah$]h&]uh1hhj8hhhhhMS ubeh}(h]j6ah ]h"]control callbacksah$]h&]uh1hhj@3hhhhhM jKubh)}(hhh](h)}(hControl Constructorh]hControl Constructor}(hj7<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4<hhhhhMX ubh)}(hWhen everything is ready, finally we can create a new control. To create a control, there are two functions to be called, :c:func:`snd_ctl_new1()` and :c:func:`snd_ctl_add()`.h](hzWhen everything is ready, finally we can create a new control. To create a control, there are two functions to be called, }(hjE<hhhNhNubh)}(h:c:func:`snd_ctl_new1()`h]j)}(hjO<h]hsnd_ctl_new1()}(hjQ<hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjM<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ctl_new1uh1hhhhMZ hjE<ubh and }(hjE<hhhNhNubh)}(h:c:func:`snd_ctl_add()`h]j)}(hjr<h]h snd_ctl_add()}(hjt<hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjp<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ctl_adduh1hhhhMZ hjE<ubh.}(hjE<hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMZ hj4<hhubh)}(h.In the simplest way, you can do it like this::h]h-In the simplest way, you can do it like this:}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM^ hj4<hhubj)}(hZerr = snd_ctl_add(card, snd_ctl_new1(&my_control, chip)); if (err < 0) return err;h]hZerr = snd_ctl_add(card, snd_ctl_new1(&my_control, chip)); if (err < 0) return err;}hj<sbah}(h]h ]h"]h$]h&]jjuh1jhhhM` hj4<hhubh)}(hwhere ``my_control`` is the struct snd_kcontrol_new object defined above, and chip is the object pointer to be passed to kcontrol->private_data which can be referred to in callbacks.h](hwhere }(hj<hhhNhNubj)}(h``my_control``h]h my_control}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj<ubh is the struct snd_kcontrol_new object defined above, and chip is the object pointer to be passed to kcontrol->private_data which can be referred to in callbacks.}(hj<hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMd hj4<hhubh)}(h:c:func:`snd_ctl_new1()` allocates a new struct snd_kcontrol instance, and :c:func:`snd_ctl_add()` assigns the given control component to the card.h](h)}(h:c:func:`snd_ctl_new1()`h]j)}(hj<h]hsnd_ctl_new1()}(hj<hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ctl_new1uh1hhhhMh hj<ubh3 allocates a new struct snd_kcontrol instance, and }(hj<hhhNhNubh)}(h:c:func:`snd_ctl_add()`h]j)}(hj<h]h snd_ctl_add()}(hj=hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj<ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ctl_adduh1hhhhMh hj<ubh1 assigns the given control component to the card.}(hj<hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMh hj4<hhubeh}(h]control-constructorah ]h"]control constructorah$]h&]uh1hhj@3hhhhhMX ubh)}(hhh](h)}(hChange Notificationh]hChange Notification}(hj0=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj-=hhhhhMm ubh)}(h{If you need to change and update a control in the interrupt routine, you can call :c:func:`snd_ctl_notify()`. For example::h](hRIf you need to change and update a control in the interrupt routine, you can call }(hj>=hhhNhNubh)}(h:c:func:`snd_ctl_notify()`h]j)}(hjH=h]hsnd_ctl_notify()}(hjJ=hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjF=ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ctl_notifyuh1hhhhMo hj>=ubh. For example:}(hj>=hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMo hj-=hhubj)}(h=snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);h]h=snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);}hjo=sbah}(h]h ]h"]h$]h&]jjuh1jhhhMr hj-=hhubh)}(hXThis function takes the card pointer, the event-mask, and the control id pointer for the notification. The event-mask specifies the types of notification, for example, in the above example, the change of control values is notified. The id pointer is the pointer of struct snd_ctl_elem_id to be notified. You can find some examples in ``es1938.c`` or ``es1968.c`` for hardware volume interrupts.h](hXNThis function takes the card pointer, the event-mask, and the control id pointer for the notification. The event-mask specifies the types of notification, for example, in the above example, the change of control values is notified. The id pointer is the pointer of struct snd_ctl_elem_id to be notified. You can find some examples in }(hj}=hhhNhNubj)}(h ``es1938.c``h]hes1938.c}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj}=ubh or }(hj}=hhhNhNubj)}(h ``es1968.c``h]hes1968.c}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj}=ubh for hardware volume interrupts.}(hj}=hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMt hj-=hhubeh}(h]jP8ah ]h"]change notificationah$]h&]uh1hhj@3hhhhhMm jKubh)}(hhh](h)}(hMetadatah]hMetadata}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj=hhhhhM| ubh)}(hXATo provide information about the dB values of a mixer control, use one of the ``DECLARE_TLV_xxx`` macros from ```` to define a variable containing this information, set the ``tlv.p`` field to point to this variable, and include the ``SNDRV_CTL_ELEM_ACCESS_TLV_READ`` flag in the ``access`` field; like this::h](hNTo provide information about the dB values of a mixer control, use one of the }(hj=hhhNhNubj)}(h``DECLARE_TLV_xxx``h]hDECLARE_TLV_xxx}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh macros from }(hj=hhhNhNubj)}(h````h]h }(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh; to define a variable containing this information, set the }(hj=hhhNhNubj)}(h ``tlv.p``h]htlv.p}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh2 field to point to this variable, and include the }(hj=hhhNhNubj)}(h"``SNDRV_CTL_ELEM_ACCESS_TLV_READ``h]hSNDRV_CTL_ELEM_ACCESS_TLV_READ}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh flag in the }(hj=hhhNhNubj)}(h ``access``h]haccess}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh field; like this:}(hj=hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM~ hj=hhubj)}(hXstatic DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0); static struct snd_kcontrol_new my_control = { ... .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, ... .tlv.p = db_scale_my_control, };h]hXstatic DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0); static struct snd_kcontrol_new my_control = { ... .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, ... .tlv.p = db_scale_my_control, };}hj/>sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj=hhubh)}(hXThe :c:func:`DECLARE_TLV_DB_SCALE()` macro defines information about a mixer control where each step in the control's value changes the dB value by a constant dB amount. The first parameter is the name of the variable to be defined. The second parameter is the minimum value, in units of 0.01 dB. The third parameter is the step size, in units of 0.01 dB. Set the fourth parameter to 1 if the minimum value actually mutes the control.h](hThe }(hj=>hhhNhNubh)}(h :c:func:`DECLARE_TLV_DB_SCALE()`h]j)}(hjG>h]hDECLARE_TLV_DB_SCALE()}(hjI>hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjE>ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjDECLARE_TLV_DB_SCALEuh1hhhhM hj=>ubhX macro defines information about a mixer control where each step in the control’s value changes the dB value by a constant dB amount. The first parameter is the name of the variable to be defined. The second parameter is the minimum value, in units of 0.01 dB. The third parameter is the step size, in units of 0.01 dB. Set the fourth parameter to 1 if the minimum value actually mutes the control.}(hj=>hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj=hhubh)}(hXThe :c:func:`DECLARE_TLV_DB_LINEAR()` macro defines information about a mixer control where the control's value affects the output linearly. The first parameter is the name of the variable to be defined. The second parameter is the minimum value, in units of 0.01 dB. The third parameter is the maximum value, in units of 0.01 dB. If the minimum value mutes the control, set the second parameter to ``TLV_DB_GAIN_MUTE``.h](hThe }(hjn>hhhNhNubh)}(h!:c:func:`DECLARE_TLV_DB_LINEAR()`h]j)}(hjx>h]hDECLARE_TLV_DB_LINEAR()}(hjz>hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjv>ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjDECLARE_TLV_DB_LINEARuh1hhhhM hjn>ubhXl macro defines information about a mixer control where the control’s value affects the output linearly. The first parameter is the name of the variable to be defined. The second parameter is the minimum value, in units of 0.01 dB. The third parameter is the maximum value, in units of 0.01 dB. If the minimum value mutes the control, set the second parameter to }(hjn>hhhNhNubj)}(h``TLV_DB_GAIN_MUTE``h]hTLV_DB_GAIN_MUTE}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjn>ubh.}(hjn>hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj=hhubeh}(h]j5ah ]h"]metadataah$]h&]uh1hhj@3hhhhhM| jKubeh}(h]control-interfaceah ]h"]control interfaceah$]h&]uh1hhhhhhhhM+ ubh)}(hhh](h)}(hAPI for AC97 Codech]hAPI for AC97 Codec}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj>hhhhhM ubh)}(hhh](h)}(hGeneralh]hGeneral}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj>hhhhhM ubh)}(hThe ALSA AC97 codec layer is a well-defined one, and you don't have to write much code to control it. Only low-level control routines are necessary. The AC97 codec API is defined in ````.h](hThe ALSA AC97 codec layer is a well-defined one, and you don’t have to write much code to control it. Only low-level control routines are necessary. The AC97 codec API is defined in }(hj>hhhNhNubj)}(h````h]h}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj>ubh.}(hj>hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj>hhubeh}(h]id5ah ]h"]h$]generalah&]uh1hhj>hhhhhM jKubh)}(hhh](h)}(hFull Code Exampleh]hFull Code Example}(hj ?hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ?hhhhhM ubj)}(hXstruct mychip { .... struct snd_ac97 *ac97; .... }; static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, unsigned short reg) { struct mychip *chip = ac97->private_data; .... /* read a register value here from the codec */ return the_register_value; } static void snd_mychip_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val) { struct mychip *chip = ac97->private_data; .... /* write the given register value to the codec */ } static int snd_mychip_ac97(struct mychip *chip) { struct snd_ac97_bus *bus; struct snd_ac97_template ac97; int err; static struct snd_ac97_bus_ops ops = { .write = snd_mychip_ac97_write, .read = snd_mychip_ac97_read, }; err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus); if (err < 0) return err; memset(&ac97, 0, sizeof(ac97)); ac97.private_data = chip; return snd_ac97_mixer(bus, &ac97, &chip->ac97); }h]hXstruct mychip { .... struct snd_ac97 *ac97; .... }; static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, unsigned short reg) { struct mychip *chip = ac97->private_data; .... /* read a register value here from the codec */ return the_register_value; } static void snd_mychip_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val) { struct mychip *chip = ac97->private_data; .... /* write the given register value to the codec */ } static int snd_mychip_ac97(struct mychip *chip) { struct snd_ac97_bus *bus; struct snd_ac97_template ac97; int err; static struct snd_ac97_bus_ops ops = { .write = snd_mychip_ac97_write, .read = snd_mychip_ac97_read, }; err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus); if (err < 0) return err; memset(&ac97, 0, sizeof(ac97)); ac97.private_data = chip; return snd_ac97_mixer(bus, &ac97, &chip->ac97); }}hj?sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj ?hhubeh}(h]id6ah ]h"]h$]full code exampleah&]uh1hhj>hhhhhM jKubh)}(hhh](h)}(hAC97 Constructorh]hAC97 Constructor}(hj4?hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj1?hhhhhM ubh)}(h{To create an ac97 instance, first call :c:func:`snd_ac97_bus()` with an ``ac97_bus_ops_t`` record with callback functions::h](h'To create an ac97 instance, first call }(hjB?hhhNhNubh)}(h:c:func:`snd_ac97_bus()`h]j)}(hjL?h]hsnd_ac97_bus()}(hjN?hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjJ?ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ac97_busuh1hhhhM hjB?ubh with an }(hjB?hhhNhNubj)}(h``ac97_bus_ops_t``h]hac97_bus_ops_t}(hjm?hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjB?ubh record with callback functions:}(hjB?hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj1?hhubj)}(hstruct snd_ac97_bus *bus; static struct snd_ac97_bus_ops ops = { .write = snd_mychip_ac97_write, .read = snd_mychip_ac97_read, }; snd_ac97_bus(card, 0, &ops, NULL, &pbus);h]hstruct snd_ac97_bus *bus; static struct snd_ac97_bus_ops ops = { .write = snd_mychip_ac97_write, .read = snd_mychip_ac97_read, }; snd_ac97_bus(card, 0, &ops, NULL, &pbus);}hj?sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj1?hhubh)}(hac97);h]hstruct snd_ac97_template ac97; int err; memset(&ac97, 0, sizeof(ac97)); ac97.private_data = chip; snd_ac97_mixer(bus, &ac97, &chip->ac97);}hj?sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj1?hhubh)}(hXwhere chip->ac97 is a pointer to a newly created ``ac97_t`` instance. In this case, the chip pointer is set as the private data, so that the read/write callback functions can refer to this chip instance. This instance is not necessarily stored in the chip record. If you need to change the register values from the driver, or need the suspend/resume of ac97 codecs, keep this pointer to pass to the corresponding functions.h](h1where chip->ac97 is a pointer to a newly created }(hj?hhhNhNubj)}(h ``ac97_t``h]hac97_t}(hj?hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj?ubhXl instance. In this case, the chip pointer is set as the private data, so that the read/write callback functions can refer to this chip instance. This instance is not necessarily stored in the chip record. If you need to change the register values from the driver, or need the suspend/resume of ac97 codecs, keep this pointer to pass to the corresponding functions.}(hj?hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj1?hhubeh}(h]ac97-constructorah ]h"]ac97 constructorah$]h&]uh1hhj>hhhhhM ubh)}(hhh](h)}(hAC97 Callbacksh]hAC97 Callbacks}(hj @hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@hhhhhM ubh)}(hThe standard callbacks are ``read`` and ``write``. Obviously they correspond to the functions for read and write accesses to the hardware low-level codes.h](hThe standard callbacks are }(hj@hhhNhNubj)}(h``read``h]hread}(hj!@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh and }(hj@hhhNhNubj)}(h ``write``h]hwrite}(hj3@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubhi. Obviously they correspond to the functions for read and write accesses to the hardware low-level codes.}(hj@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(hLThe ``read`` callback returns the register value specified in the argument::h](hThe }(hjK@hhhNhNubj)}(h``read``h]hread}(hjS@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjK@ubh? callback returns the register value specified in the argument:}(hjK@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubj)}(hstatic unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, unsigned short reg) { struct mychip *chip = ac97->private_data; .... return the_register_value; }h]hstatic unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97, unsigned short reg) { struct mychip *chip = ac97->private_data; .... return the_register_value; }}hjk@sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj@hhubh)}(h7Here, the chip can be cast from ``ac97->private_data``.h](h Here, the chip can be cast from }(hjy@hhhNhNubj)}(h``ac97->private_data``h]hac97->private_data}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjy@ubh.}(hjy@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(hEMeanwhile, the ``write`` callback is used to set the register value::h](hMeanwhile, the }(hj@hhhNhNubj)}(h ``write``h]hwrite}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh, callback is used to set the register value:}(hj@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubj)}(hustatic void snd_mychip_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)h]hustatic void snd_mychip_ac97_write(struct snd_ac97 *ac97, unsigned short reg, unsigned short val)}hj@sbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj@hhubh)}(h>These callbacks are non-atomic like the control API callbacks.h]h>These callbacks are non-atomic like the control API callbacks.}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(hAThere are also other callbacks: ``reset``, ``wait`` and ``init``.h](h There are also other callbacks: }(hj@hhhNhNubj)}(h ``reset``h]hreset}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh, }(hj@hhhNhNubj)}(h``wait``h]hwait}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh and }(hj@hhhNhNubj)}(h``init``h]hinit}(hjAhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ubh.}(hj@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(h~The ``reset`` callback is used to reset the codec. If the chip requires a special kind of reset, you can define this callback.h](hThe }(hjAhhhNhNubj)}(h ``reset``h]hreset}(hj!AhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjAubhq callback is used to reset the codec. If the chip requires a special kind of reset, you can define this callback.}(hjAhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(hThe ``wait`` callback is used to add some waiting time in the standard initialization of the codec. If the chip requires the extra waiting time, define this callback.h](hThe }(hj9AhhhNhNubj)}(h``wait``h]hwait}(hjAAhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj9Aubh callback is used to add some waiting time in the standard initialization of the codec. If the chip requires the extra waiting time, define this callback.}(hj9AhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubh)}(hIThe ``init`` callback is used for additional initialization of the codec.h](hThe }(hjYAhhhNhNubj)}(h``init``h]hinit}(hjaAhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjYAubh= callback is used for additional initialization of the codec.}(hjYAhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj@hhubeh}(h]ac97-callbacksah ]h"]ac97 callbacksah$]h&]uh1hhj>hhhhhM ubh)}(hhh](h)}(h Updating Registers in The Driverh]h Updating Registers in The Driver}(hjAhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjAhhhhhM$ ubh)}(hIf you need to access to the codec from the driver, you can call the following functions: :c:func:`snd_ac97_write()`, :c:func:`snd_ac97_read()`, :c:func:`snd_ac97_update()` and :c:func:`snd_ac97_update_bits()`.h](hZIf you need to access to the codec from the driver, you can call the following functions: }(hjAhhhNhNubh)}(h:c:func:`snd_ac97_write()`h]j)}(hjAh]hsnd_ac97_write()}(hjAhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjAubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_writeuh1hhhhM& hjAubh, }(hjAhhhNhNubh)}(h:c:func:`snd_ac97_read()`h]j)}(hjAh]hsnd_ac97_read()}(hjAhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjAubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ac97_readuh1hhhhM& hjAubh, }(hjAhhhNhNubh)}(h:c:func:`snd_ac97_update()`h]j)}(hjAh]hsnd_ac97_update()}(hjAhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjAubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_updateuh1hhhhM& hjAubh and }(hjAhhhNhNubh)}(h :c:func:`snd_ac97_update_bits()`h]j)}(hjBh]hsnd_ac97_update_bits()}(hjBhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_update_bitsuh1hhhhM& hjAubh.}(hjAhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM& hjAhhubh)}(hXCBoth :c:func:`snd_ac97_write()` and :c:func:`snd_ac97_update()` functions are used to set a value to the given register (``AC97_XXX``). The difference between them is that :c:func:`snd_ac97_update()` doesn't write a value if the given value has been already set, while :c:func:`snd_ac97_write()` always rewrites the value::h](hBoth }(hj,BhhhNhNubh)}(h:c:func:`snd_ac97_write()`h]j)}(hj6Bh]hsnd_ac97_write()}(hj8BhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj4Bubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_writeuh1hhhhM+ hj,Bubh and }(hj,BhhhNhNubh)}(h:c:func:`snd_ac97_update()`h]j)}(hjYBh]hsnd_ac97_update()}(hj[BhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjWBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_updateuh1hhhhM+ hj,Bubh: functions are used to set a value to the given register (}(hj,BhhhNhNubj)}(h ``AC97_XXX``h]hAC97_XXX}(hjzBhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,Bubh'). The difference between them is that }(hj,BhhhNhNubh)}(h:c:func:`snd_ac97_update()`h]j)}(hjBh]hsnd_ac97_update()}(hjBhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_updateuh1hhhhM+ hj,BubhH doesn’t write a value if the given value has been already set, while }(hj,BhhhNhNubh)}(h:c:func:`snd_ac97_write()`h]j)}(hjBh]hsnd_ac97_write()}(hjBhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_writeuh1hhhhM+ hj,Bubh always rewrites the value:}(hj,BhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM+ hjAhhubj)}(hVsnd_ac97_write(ac97, AC97_MASTER, 0x8080); snd_ac97_update(ac97, AC97_MASTER, 0x8080);h]hVsnd_ac97_write(ac97, AC97_MASTER, 0x8080); snd_ac97_update(ac97, AC97_MASTER, 0x8080);}hjBsbah}(h]h ]h"]h$]h&]jjuh1jhhhM2 hjAhhubh)}(hX:c:func:`snd_ac97_read()` is used to read the value of the given register. For example::h](h)}(h:c:func:`snd_ac97_read()`h]j)}(hjBh]hsnd_ac97_read()}(hjBhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_ac97_readuh1hhhhM5 hjBubh> is used to read the value of the given register. For example:}(hjBhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM5 hjAhhubj)}(h)value = snd_ac97_read(ac97, AC97_MASTER);h]h)value = snd_ac97_read(ac97, AC97_MASTER);}hjCsbah}(h]h ]h"]h$]h&]jjuh1jhhhM8 hjAhhubh)}(hT:c:func:`snd_ac97_update_bits()` is used to update some bits in the given register::h](h)}(h :c:func:`snd_ac97_update_bits()`h]j)}(hj'Ch]hsnd_ac97_update_bits()}(hj)ChhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj%Cubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_update_bitsuh1hhhhM: hj!Cubh3 is used to update some bits in the given register:}(hj!ChhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM: hjAhhubj)}(h-snd_ac97_update_bits(ac97, reg, mask, value);h]h-snd_ac97_update_bits(ac97, reg, mask, value);}hjNCsbah}(h]h ]h"]h$]h&]jjuh1jhhhM= hjAhhubh)}(hAlso, there is a function to change the sample rate (of a given register such as ``AC97_PCM_FRONT_DAC_RATE``) when VRA or DRA is supported by the codec: :c:func:`snd_ac97_set_rate()`::h](hQAlso, there is a function to change the sample rate (of a given register such as }(hj\ChhhNhNubj)}(h``AC97_PCM_FRONT_DAC_RATE``h]hAC97_PCM_FRONT_DAC_RATE}(hjdChhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\Cubh-) when VRA or DRA is supported by the codec: }(hj\ChhhNhNubh)}(h:c:func:`snd_ac97_set_rate()`h]j)}(hjxCh]hsnd_ac97_set_rate()}(hjzChhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjvCubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_set_rateuh1hhhhM? hj\Cubh:}(hj\ChhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM? hjAhhubj)}(h8snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);h]h8snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);}hjCsbah}(h]h ]h"]h$]h&]jjuh1jhhhMC hjAhhubh)}(hXThe following registers are available to set the rate: ``AC97_PCM_MIC_ADC_RATE``, ``AC97_PCM_FRONT_DAC_RATE``, ``AC97_PCM_LR_ADC_RATE``, ``AC97_SPDIF``. When ``AC97_SPDIF`` is specified, the register is not really changed but the corresponding IEC958 status bits will be updated.h](h7The following registers are available to set the rate: }(hjChhhNhNubj)}(h``AC97_PCM_MIC_ADC_RATE``h]hAC97_PCM_MIC_ADC_RATE}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubh, }(hjChhhNhNubj)}(h``AC97_PCM_FRONT_DAC_RATE``h]hAC97_PCM_FRONT_DAC_RATE}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubh, }(hjChhhNhNubj)}(h``AC97_PCM_LR_ADC_RATE``h]hAC97_PCM_LR_ADC_RATE}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubh, }hjCsbj)}(h``AC97_SPDIF``h]h AC97_SPDIF}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubh. When }(hjChhhNhNubj)}(h``AC97_SPDIF``h]h AC97_SPDIF}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1jhjCubhk is specified, the register is not really changed but the corresponding IEC958 status bits will be updated.}(hjChhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMF hjAhhubeh}(h] updating-registers-in-the-driverah ]h"] updating registers in the driverah$]h&]uh1hhj>hhhhhM$ ubh)}(hhh](h)}(hClock Adjustmenth]hClock Adjustment}(hj DhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjDhhhhhMM ubh)}(hIn some chips, the clock of the codec isn't 48000 but using a PCI clock (to save a quartz!). In this case, change the field ``bus->clock`` to the corresponding value. For example, intel8x0 and es1968 drivers have their own function to read from the clock.h](h~In some chips, the clock of the codec isn’t 48000 but using a PCI clock (to save a quartz!). In this case, change the field }(hj.DhhhNhNubj)}(h``bus->clock``h]h bus->clock}(hj6DhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj.Dubhu to the corresponding value. For example, intel8x0 and es1968 drivers have their own function to read from the clock.}(hj.DhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMO hjDhhubeh}(h]clock-adjustmentah ]h"]clock adjustmentah$]h&]uh1hhj>hhhhhMM ubh)}(hhh](h)}(h Proc Filesh]h Proc Files}(hjYDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjVDhhhhhMU ubh)}(hThe ALSA AC97 interface will create a proc file such as ``/proc/asound/card0/codec97#0/ac97#0-0`` and ``ac97#0-0+regs``. You can refer to these files to see the current status and registers of the codec.h](h8The ALSA AC97 interface will create a proc file such as }(hjgDhhhNhNubj)}(h)``/proc/asound/card0/codec97#0/ac97#0-0``h]h%/proc/asound/card0/codec97#0/ac97#0-0}(hjoDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjgDubh and }(hjgDhhhNhNubj)}(h``ac97#0-0+regs``h]h ac97#0-0+regs}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjgDubhT. You can refer to these files to see the current status and registers of the codec.}(hjgDhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMW hjVDhhubeh}(h] proc-filesah ]h"] proc filesah$]h&]uh1hhj>hhhhhMU ubh)}(hhh](h)}(hMultiple Codecsh]hMultiple Codecs}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjDhhhhhM] ubh)}(hWhen there are several codecs on the same card, you need to call :c:func:`snd_ac97_mixer()` multiple times with ``ac97.num=1`` or greater. The ``num`` field specifies the codec number.h](hAWhen there are several codecs on the same card, you need to call }(hjDhhhNhNubh)}(h:c:func:`snd_ac97_mixer()`h]j)}(hjDh]hsnd_ac97_mixer()}(hjDhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjDubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_mixeruh1hhhhM_ hjDubh multiple times with }(hjDhhhNhNubj)}(h``ac97.num=1``h]h ac97.num=1}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjDubh or greater. The }(hjDhhhNhNubj)}(h``num``h]hnum}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjDubh" field specifies the codec number.}(hjDhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM_ hjDhhubh)}(hIf you set up multiple codecs, you either need to write different callbacks for each codec or check ``ac97->num`` in the callback routines.h](hdIf you set up multiple codecs, you either need to write different callbacks for each codec or check }(hjEhhhNhNubj)}(h ``ac97->num``h]h ac97->num}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjEubh in the callback routines.}(hjEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMc hjDhhubeh}(h]multiple-codecsah ]h"]multiple codecsah$]h&]uh1hhj>hhhhhM] ubeh}(h]j ah ]h"]api for ac97 codecah$]h&]uh1hhhhhhhhM jKubh)}(hhh](h)}(hMIDI (MPU401-UART) Interfaceh]hMIDI (MPU401-UART) Interface}(hj9EhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6EhhhhhMh ubh)}(hhh](h)}(hGeneralh]hGeneral}(hjJEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjGEhhhhhMk ubh)}(hMany soundcards have built-in MIDI (MPU401-UART) interfaces. When the soundcard supports the standard MPU401-UART interface, most likely you can use the ALSA MPU401-UART API. The MPU401-UART API is defined in ````.h](hMany soundcards have built-in MIDI (MPU401-UART) interfaces. When the soundcard supports the standard MPU401-UART interface, most likely you can use the ALSA MPU401-UART API. The MPU401-UART API is defined in }(hjXEhhhNhNubj)}(h````h]h}(hj`EhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXEubh.}(hjXEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMm hjGEhhubh)}(hSome soundchips have a similar but slightly different implementation of mpu401 stuff. For example, emu10k1 has its own mpu401 routines.h]hSome soundchips have a similar but slightly different implementation of mpu401 stuff. For example, emu10k1 has its own mpu401 routines.}(hjxEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMr hjGEhhubeh}(h]id7ah ]h"]h$]generalah&]uh1hhj6EhhhhhMk jKubh)}(hhh](h)}(hMIDI Constructorh]hMIDI Constructor}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjEhhhhhMv ubh)}(hBTo create a rawmidi object, call :c:func:`snd_mpu401_uart_new()`::h](h!To create a rawmidi object, call }(hjEhhhNhNubh)}(h:c:func:`snd_mpu401_uart_new()`h]j)}(hjEh]hsnd_mpu401_uart_new()}(hjEhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjEubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_newuh1hhhhMx hjEubh:}(hjEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMx hjEhhubj)}(h}struct snd_rawmidi *rmidi; snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags, irq, &rmidi);h]h}struct snd_rawmidi *rmidi; snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags, irq, &rmidi);}hjEsbah}(h]h ]h"]h$]h&]jjuh1jhhhMz hjEhhubh)}(h~The first argument is the card pointer, and the second is the index of this component. You can create up to 8 rawmidi devices.h]h~The first argument is the card pointer, and the second is the index of this component. You can create up to 8 rawmidi devices.}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hThe third argument is the type of the hardware, ``MPU401_HW_XXX``. If it's not a special one, you can use ``MPU401_HW_MPU401``.h](h0The third argument is the type of the hardware, }(hjEhhhNhNubj)}(h``MPU401_HW_XXX``h]h MPU401_HW_XXX}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjEubh+. If it’s not a special one, you can use }(hjEhhhNhNubj)}(h``MPU401_HW_MPU401``h]hMPU401_HW_MPU401}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjEubh.}(hjEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hThe 4th argument is the I/O port address. Many backward-compatible MPU401 have an I/O port such as 0x330. Or, it might be a part of its own PCI I/O region. It depends on the chip design.h]hThe 4th argument is the I/O port address. Many backward-compatible MPU401 have an I/O port such as 0x330. Or, it might be a part of its own PCI I/O region. It depends on the chip design.}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hXLThe 5th argument is a bitflag for additional information. When the I/O port address above is part of the PCI I/O region, the MPU401 I/O port might have been already allocated (reserved) by the driver itself. In such a case, pass a bit flag ``MPU401_INFO_INTEGRATED``, and the mpu401-uart layer will allocate the I/O ports by itself.h](hThe 5th argument is a bitflag for additional information. When the I/O port address above is part of the PCI I/O region, the MPU401 I/O port might have been already allocated (reserved) by the driver itself. In such a case, pass a bit flag }(hj,FhhhNhNubj)}(h``MPU401_INFO_INTEGRATED``h]hMPU401_INFO_INTEGRATED}(hj4FhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,FubhB, and the mpu401-uart layer will allocate the I/O ports by itself.}(hj,FhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hWhen the controller supports only the input or output MIDI stream, pass the ``MPU401_INFO_INPUT`` or ``MPU401_INFO_OUTPUT`` bitflag, respectively. Then the rawmidi instance is created as a single stream.h](hLWhen the controller supports only the input or output MIDI stream, pass the }(hjLFhhhNhNubj)}(h``MPU401_INFO_INPUT``h]hMPU401_INFO_INPUT}(hjTFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLFubh or }(hjLFhhhNhNubj)}(h``MPU401_INFO_OUTPUT``h]hMPU401_INFO_OUTPUT}(hjfFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLFubhP bitflag, respectively. Then the rawmidi instance is created as a single stream.}(hjLFhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(h``MPU401_INFO_MMIO`` bitflag is used to change the access method to MMIO (via readb and writeb) instead of iob and outb. In this case, you have to pass the iomapped address to :c:func:`snd_mpu401_uart_new()`.h](j)}(h``MPU401_INFO_MMIO``h]hMPU401_INFO_MMIO}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~Fubh bitflag is used to change the access method to MMIO (via readb and writeb) instead of iob and outb. In this case, you have to pass the iomapped address to }(hj~FhhhNhNubh)}(h:c:func:`snd_mpu401_uart_new()`h]j)}(hjFh]hsnd_mpu401_uart_new()}(hjFhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjFubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_newuh1hhhhM hj~Fubh.}(hj~FhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hWhen ``MPU401_INFO_TX_IRQ`` is set, the output stream isn't checked in the default interrupt handler. The driver needs to call :c:func:`snd_mpu401_uart_interrupt_tx()` by itself to start processing the output stream in the irq handler.h](hWhen }(hjFhhhNhNubj)}(h``MPU401_INFO_TX_IRQ``h]hMPU401_INFO_TX_IRQ}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjFubhf is set, the output stream isn’t checked in the default interrupt handler. The driver needs to call }(hjFhhhNhNubh)}(h(:c:func:`snd_mpu401_uart_interrupt_tx()`h]j)}(hjFh]hsnd_mpu401_uart_interrupt_tx()}(hjFhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjFubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_interrupt_txuh1hhhhM hjFubhD by itself to start processing the output stream in the irq handler.}(hjFhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hIf the MPU-401 interface shares its interrupt with the other logical devices on the card, set ``MPU401_INFO_IRQ_HOOK`` (see `below `__).h](h^If the MPU-401 interface shares its interrupt with the other logical devices on the card, set }(hjGhhhNhNubj)}(h``MPU401_INFO_IRQ_HOOK``h]hMPU401_INFO_IRQ_HOOK}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh (see }(hjGhhhNhNubh)}(h#`below `__h]hbelow}(hjGhhhNhNubah}(h]h ]h"]h$]h&]namebelowjmidi-interrupt-handleruh1hhjGjKubh).}(hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubh)}(hXeUsually, the port address corresponds to the command port and port + 1 corresponds to the data port. If not, you may change the ``cport`` field of struct snd_mpu401 manually afterward. However, struct snd_mpu401 pointer is not returned explicitly by :c:func:`snd_mpu401_uart_new()`. You need to cast ``rmidi->private_data`` to struct snd_mpu401 explicitly::h](hUsually, the port address corresponds to the command port and port + 1 corresponds to the data port. If not, you may change the }(hj5GhhhNhNubj)}(h ``cport``h]hcport}(hj=GhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5Gubhq field of struct snd_mpu401 manually afterward. However, struct snd_mpu401 pointer is not returned explicitly by }(hj5GhhhNhNubh)}(h:c:func:`snd_mpu401_uart_new()`h]j)}(hjQGh]hsnd_mpu401_uart_new()}(hjSGhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjOGubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_newuh1hhhhM hj5Gubh. You need to cast }(hj5GhhhNhNubj)}(h``rmidi->private_data``h]hrmidi->private_data}(hjrGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5Gubh! to struct snd_mpu401 explicitly:}(hj5GhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubj)}(h2struct snd_mpu401 *mpu; mpu = rmidi->private_data;h]h2struct snd_mpu401 *mpu; mpu = rmidi->private_data;}hjGsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjEhhubh)}(h%and reset the ``cport`` as you like::h](hand reset the }(hjGhhhNhNubj)}(h ``cport``h]hcport}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh as you like:}(hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubj)}(h!mpu->cport = my_own_control_port;h]h!mpu->cport = my_own_control_port;}hjGsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjEhhubh)}(hX7The 6th argument specifies the ISA irq number that will be allocated. If no interrupt is to be allocated (because your code is already allocating a shared interrupt, or because the device does not use interrupts), pass -1 instead. For a MPU-401 device without an interrupt, a polling timer will be used instead.h]hX7The 6th argument specifies the ISA irq number that will be allocated. If no interrupt is to be allocated (because your code is already allocating a shared interrupt, or because the device does not use interrupts), pass -1 instead. For a MPU-401 device without an interrupt, a polling timer will be used instead.}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjEhhubeh}(h]midi-constructorah ]h"]midi constructorah$]h&]uh1hhj6EhhhhhMv ubh)}(hhh](h)}(hMIDI Interrupt Handlerh]hMIDI Interrupt Handler}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjGhhhhhM ubh)}(hXWhen the interrupt is allocated in :c:func:`snd_mpu401_uart_new()`, an exclusive ISA interrupt handler is automatically used, hence you don't have anything else to do than creating the mpu401 stuff. Otherwise, you have to set ``MPU401_INFO_IRQ_HOOK``, and call :c:func:`snd_mpu401_uart_interrupt()` explicitly from your own interrupt handler when it has determined that a UART interrupt has occurred.h](h#When the interrupt is allocated in }(hjGhhhNhNubh)}(h:c:func:`snd_mpu401_uart_new()`h]j)}(hjGh]hsnd_mpu401_uart_new()}(hjGhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjGubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_newuh1hhhhM hjGubh, an exclusive ISA interrupt handler is automatically used, hence you don’t have anything else to do than creating the mpu401 stuff. Otherwise, you have to set }(hjGhhhNhNubj)}(h``MPU401_INFO_IRQ_HOOK``h]hMPU401_INFO_IRQ_HOOK}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjGubh , and call }(hjGhhhNhNubh)}(h%:c:func:`snd_mpu401_uart_interrupt()`h]j)}(hj,Hh]hsnd_mpu401_uart_interrupt()}(hj.HhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj*Hubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_interruptuh1hhhhM hjGubhf explicitly from your own interrupt handler when it has determined that a UART interrupt has occurred.}(hjGhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjGhhubh)}(hIn this case, you need to pass the private_data of the returned rawmidi object from :c:func:`snd_mpu401_uart_new()` as the second argument of :c:func:`snd_mpu401_uart_interrupt()`::h](hTIn this case, you need to pass the private_data of the returned rawmidi object from }(hjSHhhhNhNubh)}(h:c:func:`snd_mpu401_uart_new()`h]j)}(hj]Hh]hsnd_mpu401_uart_new()}(hj_HhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj[Hubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_newuh1hhhhM hjSHubh as the second argument of }(hjSHhhhNhNubh)}(h%:c:func:`snd_mpu401_uart_interrupt()`h]j)}(hjHh]hsnd_mpu401_uart_interrupt()}(hjHhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj~Hubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_mpu401_uart_interruptuh1hhhhM hjSHubh:}(hjSHhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjGhhubj)}(h:snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);h]h:snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);}hjHsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjGhhubeh}(h]j*Gah ]h"]midi interrupt handlerah$]h&]uh1hhj6EhhhhhM jKubeh}(h]j, ah ]h"]midi (mpu401-uart) interfaceah$]h&]uh1hhhhhhhhMh jKubh)}(hhh](h)}(hRawMIDI Interfaceh]hRawMIDI Interface}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjHhhhhhM ubh)}(hhh](h)}(hOverviewh]hOverview}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjHhhhhhM ubh)}(hThe raw MIDI interface is used for hardware MIDI ports that can be accessed as a byte stream. It is not used for synthesizer chips that do not directly understand MIDI.h]hThe raw MIDI interface is used for hardware MIDI ports that can be accessed as a byte stream. It is not used for synthesizer chips that do not directly understand MIDI.}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjHhhubh)}(hALSA handles file and buffer management. All you have to do is to write some code to move data between the buffer and the hardware.h]hALSA handles file and buffer management. All you have to do is to write some code to move data between the buffer and the hardware.}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjHhhubh)}(h4The rawmidi API is defined in ````.h](hThe rawmidi API is defined in }(hjIhhhNhNubj)}(h````h]h}(hj IhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjIubh.}(hjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjHhhubeh}(h]overviewah ]h"]overviewah$]h&]uh1hhjHhhhhhM ubh)}(hhh](h)}(hRawMIDI Constructorh]hRawMIDI Constructor}(hj,IhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj)IhhhhhM ubh)}(hKTo create a rawmidi device, call the :c:func:`snd_rawmidi_new()` function::h](h%To create a rawmidi device, call the }(hj:IhhhNhNubh)}(h:c:func:`snd_rawmidi_new()`h]j)}(hjDIh]hsnd_rawmidi_new()}(hjFIhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjBIubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_rawmidi_newuh1hhhhM hj:Iubh function:}(hj:IhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubj)}(hXHstruct snd_rawmidi *rmidi; err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi); if (err < 0) return err; rmidi->private_data = chip; strcpy(rmidi->name, "My MIDI"); rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;h]hXHstruct snd_rawmidi *rmidi; err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi); if (err < 0) return err; rmidi->private_data = chip; strcpy(rmidi->name, "My MIDI"); rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;}hjkIsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj)Ihhubh)}(hMThe first argument is the card pointer, the second argument is the ID string.h]hMThe first argument is the card pointer, the second argument is the ID string.}(hjyIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubh)}(hZThe third argument is the index of this component. You can create up to 8 rawmidi devices.h]hZThe third argument is the index of this component. You can create up to 8 rawmidi devices.}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubh)}(hThe fourth and fifth arguments are the number of output and input substreams, respectively, of this device (a substream is the equivalent of a MIDI port).h]hThe fourth and fifth arguments are the number of output and input substreams, respectively, of this device (a substream is the equivalent of a MIDI port).}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubh)}(hX/Set the ``info_flags`` field to specify the capabilities of the device. Set ``SNDRV_RAWMIDI_INFO_OUTPUT`` if there is at least one output port, ``SNDRV_RAWMIDI_INFO_INPUT`` if there is at least one input port, and ``SNDRV_RAWMIDI_INFO_DUPLEX`` if the device can handle output and input at the same time.h](hSet the }(hjIhhhNhNubj)}(h``info_flags``h]h info_flags}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjIubh6 field to specify the capabilities of the device. Set }(hjIhhhNhNubj)}(h``SNDRV_RAWMIDI_INFO_OUTPUT``h]hSNDRV_RAWMIDI_INFO_OUTPUT}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjIubh' if there is at least one output port, }(hjIhhhNhNubj)}(h``SNDRV_RAWMIDI_INFO_INPUT``h]hSNDRV_RAWMIDI_INFO_INPUT}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjIubh* if there is at least one input port, and }(hjIhhhNhNubj)}(h``SNDRV_RAWMIDI_INFO_DUPLEX``h]hSNDRV_RAWMIDI_INFO_DUPLEX}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjIubh< if the device can handle output and input at the same time.}(hjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubh)}(hAfter the rawmidi device is created, you need to set the operators (callbacks) for each substream. There are helper functions to set the operators for all the substreams of a device::h]hAfter the rawmidi device is created, you need to set the operators (callbacks) for each substream. There are helper functions to set the operators for all the substreams of a device:}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubj)}(hsnd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);h]hsnd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops); snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);}hjJsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj)Ihhubh)}(h-The operators are usually defined like this::h]h,The operators are usually defined like this:}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubj)}(hstatic struct snd_rawmidi_ops snd_mymidi_output_ops = { .open = snd_mymidi_output_open, .close = snd_mymidi_output_close, .trigger = snd_mymidi_output_trigger, };h]hstatic struct snd_rawmidi_ops snd_mymidi_output_ops = { .open = snd_mymidi_output_open, .close = snd_mymidi_output_close, .trigger = snd_mymidi_output_trigger, };}hj#Jsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj)Ihhubh)}(hBThese callbacks are explained in the `RawMIDI Callbacks`_ section.h](h%These callbacks are explained in the }(hj1JhhhNhNubh)}(h`RawMIDI Callbacks`_h]hRawMIDI Callbacks}(hj9JhhhNhNubah}(h]h ]h"]h$]h&]nameRawMIDI Callbacksjrawmidi-callbacksuh1hhj1JjKubh section.}(hj1JhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubh)}(hUIf there are more than one substream, you should give a unique name to each of them::h]hTIf there are more than one substream, you should give a unique name to each of them:}(hjTJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj)Ihhubj)}(hX'struct snd_rawmidi_substream *substream; list_for_each_entry(substream, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, list { sprintf(substream->name, "My MIDI Port %d", substream->number + 1); } /* same for SNDRV_RAWMIDI_STREAM_INPUT */h]hX'struct snd_rawmidi_substream *substream; list_for_each_entry(substream, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, list { sprintf(substream->name, "My MIDI Port %d", substream->number + 1); } /* same for SNDRV_RAWMIDI_STREAM_INPUT */}hjbJsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj)Ihhubeh}(h]rawmidi-constructorah ]h"]rawmidi constructorah$]h&]uh1hhjHhhhhhM ubh)}(hhh](h)}(hRawMIDI Callbacksh]hRawMIDI Callbacks}(hj{JhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjxJhhhhhM ubh)}(hIn all the callbacks, the private data that you've set for the rawmidi device can be accessed as ``substream->rmidi->private_data``.h](hcIn all the callbacks, the private data that you’ve set for the rawmidi device can be accessed as }(hjJhhhNhNubj)}(h"``substream->rmidi->private_data``h]hsubstream->rmidi->private_data}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjJubh.}(hjJhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjxJhhubh)}(hIf there is more than one port, your callbacks can determine the port index from the struct snd_rawmidi_substream data passed to each callback::h]hIf there is more than one port, your callbacks can determine the port index from the struct snd_rawmidi_substream data passed to each callback:}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjxJhhubj)}(hGstruct snd_rawmidi_substream *substream; int index = substream->number;h]hGstruct snd_rawmidi_substream *substream; int index = substream->number;}hjJsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjxJhhubh)}(hhh](h)}(hRawMIDI open callbackh]hRawMIDI open callback}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJhhhhhM ubj)}(hAstatic int snd_xxx_open(struct snd_rawmidi_substream *substream);h]hAstatic int snd_xxx_open(struct snd_rawmidi_substream *substream);}hjJsbah}(h]h ]h"]h$]h&]jjuh1jhhhM$ hjJhhubh)}(hThis is called when a substream is opened. You can initialize the hardware here, but you shouldn't start transmitting/receiving data yet.h]hThis is called when a substream is opened. You can initialize the hardware here, but you shouldn’t start transmitting/receiving data yet.}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM' hjJhhubeh}(h]rawmidi-open-callbackah ]h"]rawmidi open callbackah$]h&]uh1hhjxJhhhhhM ubh)}(hhh](h)}(hRawMIDI close callbackh]hRawMIDI close callback}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJhhhhhM+ ubj)}(hBstatic int snd_xxx_close(struct snd_rawmidi_substream *substream);h]hBstatic int snd_xxx_close(struct snd_rawmidi_substream *substream);}hj Ksbah}(h]h ]h"]h$]h&]jjuh1jhhhM/ hjJhhubh)}(h Guess what.h]h Guess what.}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1 hjJhhubh)}(hdThe ``open`` and ``close`` callbacks of a rawmidi device are serialized with a mutex, and can sleep.h](hThe }(hj'KhhhNhNubj)}(h``open``h]hopen}(hj/KhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'Kubh and }(hj'KhhhNhNubj)}(h ``close``h]hclose}(hjAKhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj'KubhJ callbacks of a rawmidi device are serialized with a mutex, and can sleep.}(hj'KhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM3 hjJhhubeh}(h]rawmidi-close-callbackah ]h"]rawmidi close callbackah$]h&]uh1hhjxJhhhhhM+ ubh)}(hhh](h)}(h.Rawmidi trigger callback for output substreamsh]h.Rawmidi trigger callback for output substreams}(hjdKhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjaKhhhhhM7 ubj)}(hTstatic void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);h]hTstatic void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);}hjrKsbah}(h]h ]h"]h$]h&]jjuh1jhhhM; hjaKhhubh)}(hxThis is called with a nonzero ``up`` parameter when there is some data in the substream buffer that must be transmitted.h](hThis is called with a nonzero }(hjKhhhNhNubj)}(h``up``h]hup}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjKubhT parameter when there is some data in the substream buffer that must be transmitted.}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM> hjaKhhubh)}(hXmTo read data from the buffer, call :c:func:`snd_rawmidi_transmit_peek()`. It will return the number of bytes that have been read; this will be less than the number of bytes requested when there are no more data in the buffer. After the data have been transmitted successfully, call :c:func:`snd_rawmidi_transmit_ack()` to remove the data from the substream buffer::h](h#To read data from the buffer, call }(hjKhhhNhNubh)}(h%:c:func:`snd_rawmidi_transmit_peek()`h]j)}(hjKh]hsnd_rawmidi_transmit_peek()}(hjKhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjKubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_rawmidi_transmit_peekuh1hhhhMA hjKubh. It will return the number of bytes that have been read; this will be less than the number of bytes requested when there are no more data in the buffer. After the data have been transmitted successfully, call }(hjKhhhNhNubh)}(h$:c:func:`snd_rawmidi_transmit_ack()`h]j)}(hjKh]hsnd_rawmidi_transmit_ack()}(hjKhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjKubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_rawmidi_transmit_ackuh1hhhhMA hjKubh. to remove the data from the substream buffer:}(hjKhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMA hjaKhhubj)}(hunsigned char data; while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) { if (snd_mychip_try_to_transmit(data)) snd_rawmidi_transmit_ack(substream, 1); else break; /* hardware FIFO full */ }h]hunsigned char data; while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) { if (snd_mychip_try_to_transmit(data)) snd_rawmidi_transmit_ack(substream, 1); else break; /* hardware FIFO full */ }}hjKsbah}(h]h ]h"]h$]h&]jjuh1jhhhMI hjaKhhubh)}(hIf you know beforehand that the hardware will accept data, you can use the :c:func:`snd_rawmidi_transmit()` function which reads some data and removes them from the buffer at once::h](hKIf you know beforehand that the hardware will accept data, you can use the }(hjLhhhNhNubh)}(h :c:func:`snd_rawmidi_transmit()`h]j)}(hj Lh]hsnd_rawmidi_transmit()}(hjLhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj Lubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_rawmidi_transmituh1hhhhMQ hjLubhI function which reads some data and removes them from the buffer at once:}(hjLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMQ hjaKhhubj)}(hwhile (snd_mychip_transmit_possible()) { unsigned char data; if (snd_rawmidi_transmit(substream, &data, 1) != 1) break; /* no more data */ snd_mychip_transmit(data); }h]hwhile (snd_mychip_transmit_possible()) { unsigned char data; if (snd_rawmidi_transmit(substream, &data, 1) != 1) break; /* no more data */ snd_mychip_transmit(data); }}hj3Lsbah}(h]h ]h"]h$]h&]jjuh1jhhhMU hjaKhhubh)}(hIf you know beforehand how many bytes you can accept, you can use a buffer size greater than one with the ``snd_rawmidi_transmit*()`` functions.h](hjIf you know beforehand how many bytes you can accept, you can use a buffer size greater than one with the }(hjALhhhNhNubj)}(h``snd_rawmidi_transmit*()``h]hsnd_rawmidi_transmit*()}(hjILhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjALubh functions.}(hjALhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM\ hjaKhhubh)}(hX The ``trigger`` callback must not sleep. If the hardware FIFO is full before the substream buffer has been emptied, you have to continue transmitting data later, either in an interrupt handler, or with a timer if the hardware doesn't have a MIDI transmit interrupt.h](hThe }(hjaLhhhNhNubj)}(h ``trigger``h]htrigger}(hjiLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaLubh callback must not sleep. If the hardware FIFO is full before the substream buffer has been emptied, you have to continue transmitting data later, either in an interrupt handler, or with a timer if the hardware doesn’t have a MIDI transmit interrupt.}(hjaLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM_ hjaKhhubh)}(hpThe ``trigger`` callback is called with a zero ``up`` parameter when the transmission of data should be aborted.h](hThe }(hjLhhhNhNubj)}(h ``trigger``h]htrigger}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh callback is called with a zero }(hjLhhhNhNubj)}(h``up``h]hup}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh; parameter when the transmission of data should be aborted.}(hjLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMd hjaKhhubeh}(h].rawmidi-trigger-callback-for-output-substreamsah ]h"].rawmidi trigger callback for output substreamsah$]h&]uh1hhjxJhhhhhM7 ubh)}(hhh](h)}(h-RawMIDI trigger callback for input substreamsh]h-RawMIDI trigger callback for input substreams}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjLhhhhhMh ubj)}(hSstatic void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);h]hSstatic void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);}hjLsbah}(h]h ]h"]h$]h&]jjuh1jhhhMl hjLhhubh)}(hThis is called with a nonzero ``up`` parameter to enable receiving data, or with a zero ``up`` parameter do disable receiving data.h](hThis is called with a nonzero }(hjLhhhNhNubj)}(h``up``h]hup}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh4 parameter to enable receiving data, or with a zero }(hjLhhhNhNubj)}(h``up``h]hup}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLubh% parameter do disable receiving data.}(hjLhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMo hjLhhubh)}(h|The ``trigger`` callback must not sleep; the actual reading of data from the device is usually done in an interrupt handler.h](hThe }(hj MhhhNhNubj)}(h ``trigger``h]htrigger}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj Mubhm callback must not sleep; the actual reading of data from the device is usually done in an interrupt handler.}(hj MhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMr hjLhhubh)}(hzWhen data reception is enabled, your interrupt handler should call :c:func:`snd_rawmidi_receive()` for all received data::h](hCWhen data reception is enabled, your interrupt handler should call }(hj,MhhhNhNubh)}(h:c:func:`snd_rawmidi_receive()`h]j)}(hj6Mh]hsnd_rawmidi_receive()}(hj8MhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj4Mubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_rawmidi_receiveuh1hhhhMu hj,Mubh for all received data:}(hj,MhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMu hjLhhubj)}(hvoid snd_mychip_midi_interrupt(...) { while (mychip_midi_available()) { unsigned char data; data = mychip_midi_read(); snd_rawmidi_receive(substream, &data, 1); } }h]hvoid snd_mychip_midi_interrupt(...) { while (mychip_midi_available()) { unsigned char data; data = mychip_midi_read(); snd_rawmidi_receive(substream, &data, 1); } }}hj]Msbah}(h]h ]h"]h$]h&]jjuh1jhhhMx hjLhhubeh}(h]-rawmidi-trigger-callback-for-input-substreamsah ]h"]-rawmidi trigger callback for input substreamsah$]h&]uh1hhjxJhhhhhMh ubh)}(hhh](h)}(hdrain callbackh]hdrain callback}(hjvMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjsMhhhhhM ubj)}(hCstatic void snd_xxx_drain(struct snd_rawmidi_substream *substream);h]hCstatic void snd_xxx_drain(struct snd_rawmidi_substream *substream);}hjMsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjsMhhubh)}(hThis is only used with output substreams. This function should wait until all data read from the substream buffer have been transmitted. This ensures that the device can be closed and the driver unloaded without losing data.h]hThis is only used with output substreams. This function should wait until all data read from the substream buffer have been transmitted. This ensures that the device can be closed and the driver unloaded without losing data.}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjsMhhubh)}(hThis callback is optional. If you do not set ``drain`` in the struct snd_rawmidi_ops structure, ALSA will simply wait for 50 milliseconds instead.h](h-This callback is optional. If you do not set }(hjMhhhNhNubj)}(h ``drain``h]hdrain}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh\ in the struct snd_rawmidi_ops structure, ALSA will simply wait for 50 milliseconds instead.}(hjMhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjsMhhubeh}(h]drain-callbackah ]h"]drain callbackah$]h&]uh1hhjxJhhhhhM ubeh}(h]jIJah ]h"]rawmidi callbacksah$]h&]uh1hhjHhhhhhM jKubeh}(h]rawmidi-interfaceah ]h"]rawmidi interfaceah$]h&]uh1hhhhhhhhM ubh)}(hhh](h)}(hMiscellaneous Devicesh]hMiscellaneous Devices}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMhhhhhM ubh)}(hhh](h)}(hFM OPL3h]hFM OPL3}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMhhhhhM ubh)}(hThe FM OPL3 is still used in many chips (mainly for backward compatibility). ALSA has a nice OPL3 FM control layer, too. The OPL3 API is defined in ````.h](hThe FM OPL3 is still used in many chips (mainly for backward compatibility). ALSA has a nice OPL3 FM control layer, too. The OPL3 API is defined in }(hjMhhhNhNubj)}(h````h]h}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh.}(hjMhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hXOFM registers can be directly accessed through the direct-FM API, defined in ````. In ALSA native mode, FM registers are accessed through the Hardware-Dependent Device direct-FM extension API, whereas in OSS compatible mode, FM registers can be accessed with the OSS direct-FM compatible API in ``/dev/dmfmX`` device.h](hLFM registers can be directly accessed through the direct-FM API, defined in }(hjNhhhNhNubj)}(h````h]h}(hj!NhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh. In ALSA native mode, FM registers are accessed through the Hardware-Dependent Device direct-FM extension API, whereas in OSS compatible mode, FM registers can be accessed with the OSS direct-FM compatible API in }(hjNhhhNhNubj)}(h``/dev/dmfmX``h]h /dev/dmfmX}(hj3NhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh device.}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hzTo create the OPL3 component, you have two functions to call. The first one is a constructor for the ``opl3_t`` instance::h](heTo create the OPL3 component, you have two functions to call. The first one is a constructor for the }(hjKNhhhNhNubj)}(h ``opl3_t``h]hopl3_t}(hjSNhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjKNubh instance:}(hjKNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubj)}(hpstruct snd_opl3 *opl3; snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX, integrated, &opl3);h]hpstruct snd_opl3 *opl3; snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX, integrated, &opl3);}hjkNsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjMhhubh)}(hThe first argument is the card pointer, the second one is the left port address, and the third is the right port address. In most cases, the right port is placed at the left port + 2.h]hThe first argument is the card pointer, the second one is the left port address, and the third is the right port address. In most cases, the right port is placed at the left port + 2.}(hjyNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(h)The fourth argument is the hardware type.h]h)The fourth argument is the hardware type.}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hWhen the left and right ports have been already allocated by the card driver, pass non-zero to the fifth argument (``integrated``). Otherwise, the opl3 module will allocate the specified ports by itself.h](hsWhen the left and right ports have been already allocated by the card driver, pass non-zero to the fifth argument (}(hjNhhhNhNubj)}(h``integrated``h]h integrated}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubhJ). Otherwise, the opl3 module will allocate the specified ports by itself.}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hWhen the accessing the hardware requires special method instead of the standard I/O access, you can create opl3 instance separately with :c:func:`snd_opl3_new()`::h](hWhen the accessing the hardware requires special method instead of the standard I/O access, you can create opl3 instance separately with }(hjNhhhNhNubh)}(h:c:func:`snd_opl3_new()`h]j)}(hjNh]hsnd_opl3_new()}(hjNhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjNubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_opl3_newuh1hhhhM hjNubh:}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubj)}(hCstruct snd_opl3 *opl3; snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);h]hCstruct snd_opl3 *opl3; snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);}hjNsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjMhhubh)}(hX Then set ``command``, ``private_data`` and ``private_free`` for the private access function, the private data and the destructor. The ``l_port`` and ``r_port`` are not necessarily set. Only the command must be set properly. You can retrieve the data from the ``opl3->private_data`` field.h](h Then set }(hjNhhhNhNubj)}(h ``command``h]hcommand}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh, }(hjNhhhNhNubj)}(h``private_data``h]h private_data}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh and }(hjNhhhNhNubj)}(h``private_free``h]h private_free}(hj OhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubhK for the private access function, the private data and the destructor. The }(hjNhhhNhNubj)}(h ``l_port``h]hl_port}(hj2OhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh and }hjNsbj)}(h ``r_port``h]hr_port}(hjDOhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubhd are not necessarily set. Only the command must be set properly. You can retrieve the data from the }(hjNhhhNhNubj)}(h``opl3->private_data``h]hopl3->private_data}(hjVOhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjNubh field.}(hjNhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hAfter creating the opl3 instance via :c:func:`snd_opl3_new()`, call :c:func:`snd_opl3_init()` to initialize the chip to the proper state. Note that :c:func:`snd_opl3_create()` always calls it internally.h](h%After creating the opl3 instance via }(hjnOhhhNhNubh)}(h:c:func:`snd_opl3_new()`h]j)}(hjxOh]hsnd_opl3_new()}(hjzOhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjvOubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_opl3_newuh1hhhhM hjnOubh, call }(hjnOhhhNhNubh)}(h:c:func:`snd_opl3_init()`h]j)}(hjOh]hsnd_opl3_init()}(hjOhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjOubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_opl3_inituh1hhhhM hjnOubh7 to initialize the chip to the proper state. Note that }(hjnOhhhNhNubh)}(h:c:func:`snd_opl3_create()`h]j)}(hjOh]hsnd_opl3_create()}(hjOhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjOubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_opl3_createuh1hhhhM hjnOubh always calls it internally.}(hjnOhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hXIf the opl3 instance is created successfully, then create a hwdep device for this opl3::h]hWIf the opl3 instance is created successfully, then create a hwdep device for this opl3:}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubj)}(hHstruct snd_hwdep *opl3hwdep; snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);h]hHstruct snd_hwdep *opl3hwdep; snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);}hjOsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjMhhubh)}(hiThe first argument is the ``opl3_t`` instance you created, and the second is the index number, usually 0.h](hThe first argument is the }(hjPhhhNhNubj)}(h ``opl3_t``h]hopl3_t}(hj PhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjPubhE instance you created, and the second is the index number, usually 0.}(hjPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubh)}(hThe third argument is the index-offset for the sequencer client assigned to the OPL3 port. When there is an MPU401-UART, give 1 for here (UART always takes 0).h]hThe third argument is the index-offset for the sequencer client assigned to the OPL3 port. When there is an MPU401-UART, give 1 for here (UART always takes 0).}(hj!PhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjMhhubeh}(h]fm-opl3ah ]h"]fm opl3ah$]h&]uh1hhjMhhhhhM ubh)}(hhh](h)}(hHardware-Dependent Devicesh]hHardware-Dependent Devices}(hj:PhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj7PhhhhhM ubh)}(hX Some chips need user-space access for special controls or for loading the micro code. In such a case, you can create a hwdep (hardware-dependent) device. The hwdep API is defined in ````. You can find examples in opl3 driver or ``isa/sb/sb16_csp.c``.h](hSome chips need user-space access for special controls or for loading the micro code. In such a case, you can create a hwdep (hardware-dependent) device. The hwdep API is defined in }(hjHPhhhNhNubj)}(h````h]h}(hjPPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHPubh*. You can find examples in opl3 driver or }(hjHPhhhNhNubj)}(h``isa/sb/sb16_csp.c``h]hisa/sb/sb16_csp.c}(hjbPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjHPubh.}(hjHPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubh)}(hNThe creation of the ``hwdep`` instance is done via :c:func:`snd_hwdep_new()`::h](hThe creation of the }(hjzPhhhNhNubj)}(h ``hwdep``h]hhwdep}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjzPubh instance is done via }(hjzPhhhNhNubh)}(h:c:func:`snd_hwdep_new()`h]j)}(hjPh]hsnd_hwdep_new()}(hjPhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjPubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_hwdep_newuh1hhhhM hjzPubh:}(hjzPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubj)}(h>struct snd_hwdep *hw; snd_hwdep_new(card, "My HWDEP", 0, &hw);h]h>struct snd_hwdep *hw; snd_hwdep_new(card, "My HWDEP", 0, &hw);}hjPsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj7Phhubh)}(h-where the third argument is the index number.h]h-where the third argument is the index number.}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubh)}(hYou can then pass any pointer value to the ``private_data``. If you assign private data, you should define a destructor, too. The destructor function is set in the ``private_free`` field::h](h+You can then pass any pointer value to the }(hjPhhhNhNubj)}(h``private_data``h]h private_data}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjPubhi. If you assign private data, you should define a destructor, too. The destructor function is set in the }(hjPhhhNhNubj)}(h``private_free``h]h private_free}(hjPhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjPubh field:}(hjPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubj)}(histruct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL); hw->private_data = p; hw->private_free = mydata_free;h]histruct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL); hw->private_data = p; hw->private_free = mydata_free;}hj Qsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj7Phhubh)}(h3and the implementation of the destructor would be::h]h2and the implementation of the destructor would be:}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubj)}(hpstatic void mydata_free(struct snd_hwdep *hw) { struct mydata *p = hw->private_data; kfree(p); }h]hpstatic void mydata_free(struct snd_hwdep *hw) { struct mydata *p = hw->private_data; kfree(p); }}hj'Qsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj7Phhubh)}(hThe arbitrary file operations can be defined for this instance. The file operators are defined in the ``ops`` table. For example, assume that this chip needs an ioctl::h](hfThe arbitrary file operations can be defined for this instance. The file operators are defined in the }(hj5QhhhNhNubj)}(h``ops``h]hops}(hj=QhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5Qubh: table. For example, assume that this chip needs an ioctl:}(hj5QhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubj)}(h[hw->ops.open = mydata_open; hw->ops.ioctl = mydata_ioctl; hw->ops.release = mydata_release;h]h[hw->ops.open = mydata_open; hw->ops.ioctl = mydata_ioctl; hw->ops.release = mydata_release;}hjUQsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj7Phhubh)}(h1And implement the callback functions as you like.h]h1And implement the callback functions as you like.}(hjcQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj7Phhubeh}(h]hardware-dependent-devicesah ]h"]hardware-dependent devicesah$]h&]uh1hhjMhhhhhM ubh)}(hhh](h)}(hIEC958 (S/PDIF)h]hIEC958 (S/PDIF)}(hj|QhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjyQhhhhhM ubh)}(hUsually the controls for IEC958 devices are implemented via the control interface. There is a macro to compose a name string for IEC958 controls, :c:func:`SNDRV_CTL_NAME_IEC958()` defined in ````.h](hUsually the controls for IEC958 devices are implemented via the control interface. There is a macro to compose a name string for IEC958 controls, }(hjQhhhNhNubh)}(h!:c:func:`SNDRV_CTL_NAME_IEC958()`h]j)}(hjQh]hSNDRV_CTL_NAME_IEC958()}(hjQhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjQubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjSNDRV_CTL_NAME_IEC958uh1hhhhM hjQubh defined in }(hjQhhhNhNubj)}(h````h]h}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjQubh.}(hjQhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(hX2There are some standard controls for IEC958 status bits. These controls use the type ``SNDRV_CTL_ELEM_TYPE_IEC958``, and the size of element is fixed as 4 bytes array (value.iec958.status[x]). For the ``info`` callback, you don't specify the value field for this type (the count field must be set, though).h](hUThere are some standard controls for IEC958 status bits. These controls use the type }(hjQhhhNhNubj)}(h``SNDRV_CTL_ELEM_TYPE_IEC958``h]hSNDRV_CTL_ELEM_TYPE_IEC958}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjQubhV, and the size of element is fixed as 4 bytes array (value.iec958.status[x]). For the }(hjQhhhNhNubj)}(h``info``h]hinfo}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjQubhc callback, you don’t specify the value field for this type (the count field must be set, though).}(hjQhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(h“IEC958 Playback Con Mask” is used to return the bit-mask for the IEC958 status bits of consumer mode. Similarly, “IEC958 Playback Pro Mask” returns the bitmask for professional mode. They are read-only controls.h]h“IEC958 Playback Con Mask” is used to return the bit-mask for the IEC958 status bits of consumer mode. Similarly, “IEC958 Playback Pro Mask” returns the bitmask for professional mode. They are read-only controls.}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(htMeanwhile, “IEC958 Playback Default” control is defined for getting and setting the current default IEC958 bits.h]htMeanwhile, “IEC958 Playback Default” control is defined for getting and setting the current default IEC958 bits.}(hj RhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(hX Due to historical reasons, both variants of the Playback Mask and the Playback Default controls can be implemented on either a ``SNDRV_CTL_ELEM_IFACE_PCM`` or a ``SNDRV_CTL_ELEM_IFACE_MIXER`` iface. Drivers should expose the mask and default on the same iface though.h](hDue to historical reasons, both variants of the Playback Mask and the Playback Default controls can be implemented on either a }(hjRhhhNhNubj)}(h``SNDRV_CTL_ELEM_IFACE_PCM``h]hSNDRV_CTL_ELEM_IFACE_PCM}(hj#RhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjRubh or a }(hjRhhhNhNubj)}(h``SNDRV_CTL_ELEM_IFACE_MIXER``h]hSNDRV_CTL_ELEM_IFACE_MIXER}(hj5RhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjRubhL iface. Drivers should expose the mask and default on the same iface though.}(hjRhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(hIn addition, you can define the control switches to enable/disable or to set the raw bit mode. The implementation will depend on the chip, but the control should be named as “IEC958 xxx”, preferably using the :c:func:`SNDRV_CTL_NAME_IEC958()` macro.h](hIn addition, you can define the control switches to enable/disable or to set the raw bit mode. The implementation will depend on the chip, but the control should be named as “IEC958 xxx”, preferably using the }(hjMRhhhNhNubh)}(h!:c:func:`SNDRV_CTL_NAME_IEC958()`h]j)}(hjWRh]hSNDRV_CTL_NAME_IEC958()}(hjYRhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjURubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjSNDRV_CTL_NAME_IEC958uh1hhhhM hjMRubh macro.}(hjMRhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubh)}(h_You can find several cases, for example, ``pci/emu10k1``, ``pci/ice1712``, or ``pci/cmipci.c``.h](h)You can find several cases, for example, }(hj~RhhhNhNubj)}(h``pci/emu10k1``h]h pci/emu10k1}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~Rubh, }(hj~RhhhNhNubj)}(h``pci/ice1712``h]h pci/ice1712}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~Rubh, or }(hj~RhhhNhNubj)}(h``pci/cmipci.c``h]h pci/cmipci.c}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~Rubh.}(hj~RhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjyQhhubeh}(h] iec958-s-pdifah ]h"]iec958 (s/pdif)ah$]h&]uh1hhjMhhhhhM ubeh}(h]miscellaneous-devicesah ]h"]miscellaneous devicesah$]h&]uh1hhhhhhhhM ubh)}(hhh](h)}(hBuffer and Memory Managementh]hBuffer and Memory Management}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjRhhhhhM! ubh)}(hhh](h)}(h Buffer Typesh]h Buffer Types}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjRhhhhhM$ ubh)}(hX ALSA provides several different buffer allocation functions depending on the bus and the architecture. All these have a consistent API. The allocation of physically-contiguous pages is done via the :c:func:`snd_malloc_xxx_pages()` function, where xxx is the bus type.h](hALSA provides several different buffer allocation functions depending on the bus and the architecture. All these have a consistent API. The allocation of physically-contiguous pages is done via the }(hjRhhhNhNubh)}(h :c:func:`snd_malloc_xxx_pages()`h]j)}(hjRh]hsnd_malloc_xxx_pages()}(hjShhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjRubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_malloc_xxx_pagesuh1hhhhM& hjRubh% function, where xxx is the bus type.}(hjRhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM& hjRhhubh)}(hXThe allocation of pages with fallback is done via :c:func:`snd_dma_alloc_pages_fallback()`. This function tries to allocate the specified number of pages, but if not enough pages are available, it tries to reduce the request size until enough space is found, down to one page.h](h2The allocation of pages with fallback is done via }(hj%ShhhNhNubh)}(h(:c:func:`snd_dma_alloc_pages_fallback()`h]j)}(hj/Sh]hsnd_dma_alloc_pages_fallback()}(hj1ShhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj-Subah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_dma_alloc_pages_fallbackuh1hhhhM, hj%Subh. This function tries to allocate the specified number of pages, but if not enough pages are available, it tries to reduce the request size until enough space is found, down to one page.}(hj%ShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM, hjRhhubh)}(hGTo release the pages, call the :c:func:`snd_dma_free_pages()` function.h](hTo release the pages, call the }(hjVShhhNhNubh)}(h:c:func:`snd_dma_free_pages()`h]j)}(hj`Sh]hsnd_dma_free_pages()}(hjbShhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj^Subah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_dma_free_pagesuh1hhhhM2 hjVSubh function.}(hjVShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2 hjRhhubh)}(hXUsually, ALSA drivers try to allocate and reserve a large contiguous physical space at the time the module is loaded for later use. This is called “pre-allocation”. As already written, you can call the following function at PCM instance construction time (in the case of PCI bus)::h]hXUsually, ALSA drivers try to allocate and reserve a large contiguous physical space at the time the module is loaded for later use. This is called “pre-allocation”. As already written, you can call the following function at PCM instance construction time (in the case of PCI bus):}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM5 hjRhhubj)}(h{snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, &pci->dev, size, max);h]h{snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, &pci->dev, size, max);}hjSsbah}(h]h ]h"]h$]h&]jjuh1jhhhM; hjRhhubh)}(hwhere ``size`` is the byte size to be pre-allocated and ``max`` is the maximum size settable via the ``prealloc`` proc file. The allocator will try to get an area as large as possible within the given size.h](hwhere }(hjShhhNhNubj)}(h``size``h]hsize}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh* is the byte size to be pre-allocated and }(hjShhhNhNubj)}(h``max``h]hmax}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh& is the maximum size settable via the }(hjShhhNhNubj)}(h ``prealloc``h]hprealloc}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh] proc file. The allocator will try to get an area as large as possible within the given size.}(hjShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM> hjRhhubh)}(hThe second argument (type) and the third argument (device pointer) are dependent on the bus. For normal devices, pass the device pointer (typically identical as ``card->dev``) to the third argument with ``SNDRV_DMA_TYPE_DEV`` type.h](hThe second argument (type) and the third argument (device pointer) are dependent on the bus. For normal devices, pass the device pointer (typically identical as }(hjShhhNhNubj)}(h ``card->dev``h]h card->dev}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh) to the third argument with }(hjShhhNhNubj)}(h``SNDRV_DMA_TYPE_DEV``h]hSNDRV_DMA_TYPE_DEV}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjSubh type.}(hjShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMC hjRhhubh)}(hXA continuous buffer unrelated to the bus can be pre-allocated with ``SNDRV_DMA_TYPE_CONTINUOUS`` type. You can pass NULL to the device pointer in that case, which is the default mode implying to allocate with the ``GFP_KERNEL`` flag. If you need a restricted (lower) address, set up the coherent DMA mask bits for the device, and pass the device pointer, like the normal device memory allocations. For this type, it's still allowed to pass NULL to the device pointer, too, if no address restriction is needed.h](hCA continuous buffer unrelated to the bus can be pre-allocated with }(hjThhhNhNubj)}(h``SNDRV_DMA_TYPE_CONTINUOUS``h]hSNDRV_DMA_TYPE_CONTINUOUS}(hj!ThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubhu type. You can pass NULL to the device pointer in that case, which is the default mode implying to allocate with the }(hjThhhNhNubj)}(h``GFP_KERNEL``h]h GFP_KERNEL}(hj3ThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubhX flag. If you need a restricted (lower) address, set up the coherent DMA mask bits for the device, and pass the device pointer, like the normal device memory allocations. For this type, it’s still allowed to pass NULL to the device pointer, too, if no address restriction is needed.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMH hjRhhubh)}(hFor the scatter-gather buffers, use ``SNDRV_DMA_TYPE_DEV_SG`` with the device pointer (see the `Non-Contiguous Buffers`_ section).h](h$For the scatter-gather buffers, use }(hjKThhhNhNubj)}(h``SNDRV_DMA_TYPE_DEV_SG``h]hSNDRV_DMA_TYPE_DEV_SG}(hjSThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjKTubh" with the device pointer (see the }(hjKThhhNhNubh)}(h`Non-Contiguous Buffers`_h]hNon-Contiguous Buffers}(hjeThhhNhNubah}(h]h ]h"]h$]h&]nameNon-Contiguous Buffersjnon-contiguous-buffersuh1hhjKTjKubh section).}(hjKThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMQ hjRhhubh)}(h[Once the buffer is pre-allocated, you can use the allocator in the ``hw_params`` callback::h](hCOnce the buffer is pre-allocated, you can use the allocator in the }(hjThhhNhNubj)}(h ``hw_params``h]h hw_params}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh callback:}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMT hjRhhubj)}(h*snd_pcm_lib_malloc_pages(substream, size);h]h*snd_pcm_lib_malloc_pages(substream, size);}hjTsbah}(h]h ]h"]h$]h&]jjuh1jhhhMW hjRhhubh)}(h8Note that you have to pre-allocate to use this function.h]h8Note that you have to pre-allocate to use this function.}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMY hjRhhubh)}(hBut most drivers use the "managed buffer allocation mode" instead of manual allocation and release. This is done by calling :c:func:`snd_pcm_set_managed_buffer_all()` instead of :c:func:`snd_pcm_lib_preallocate_pages_for_all()`::h](hBut most drivers use the “managed buffer allocation mode” instead of manual allocation and release. This is done by calling }(hjThhhNhNubh)}(h*:c:func:`snd_pcm_set_managed_buffer_all()`h]j)}(hjTh]h snd_pcm_set_managed_buffer_all()}(hjThhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjTubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_set_managed_buffer_alluh1hhhhM[ hjTubh instead of }(hjThhhNhNubh)}(h1:c:func:`snd_pcm_lib_preallocate_pages_for_all()`h]j)}(hjTh]h'snd_pcm_lib_preallocate_pages_for_all()}(hjThhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjTubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj%snd_pcm_lib_preallocate_pages_for_alluh1hhhhM[ hjTubh:}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM[ hjRhhubj)}(hmsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pci->dev, size, max);h]hmsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &pci->dev, size, max);}hjUsbah}(h]h ]h"]h$]h&]jjuh1jhhhM` hjRhhubh)}(hXwhere the passed arguments are identical for both functions. The difference in the managed mode is that PCM core will call :c:func:`snd_pcm_lib_malloc_pages()` internally already before calling the PCM ``hw_params`` callback, and call :c:func:`snd_pcm_lib_free_pages()` after the PCM ``hw_free`` callback automatically. So the driver doesn't have to call these functions explicitly in its callback any longer. This allows many drivers to have NULL ``hw_params`` and ``hw_free`` entries.h](h{where the passed arguments are identical for both functions. The difference in the managed mode is that PCM core will call }(hjUhhhNhNubh)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hj(Uh]hsnd_pcm_lib_malloc_pages()}(hj*UhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj&Uubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhMc hjUubh+ internally already before calling the PCM }(hjUhhhNhNubj)}(h ``hw_params``h]h hw_params}(hjIUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh callback, and call }(hjUhhhNhNubh)}(h":c:func:`snd_pcm_lib_free_pages()`h]j)}(hj]Uh]hsnd_pcm_lib_free_pages()}(hj_UhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj[Uubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_free_pagesuh1hhhhMc hjUubh after the PCM }(hjUhhhNhNubj)}(h ``hw_free``h]hhw_free}(hj~UhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh callback automatically. So the driver doesn’t have to call these functions explicitly in its callback any longer. This allows many drivers to have NULL }(hjUhhhNhNubj)}(h ``hw_params``h]h hw_params}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh and }(hjUhhhNhNubj)}(h ``hw_free``h]hhw_free}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh entries.}(hjUhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMc hjRhhubeh}(h]j}&ah ]h"] buffer typesah$]h&]uh1hhjRhhhhhM$ jKubh)}(hhh](h)}(hExternal Hardware Buffersh]hExternal Hardware Buffers}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjUhhhhhMm ubh)}(hXVSome chips have their own hardware buffers and DMA transfer from the host memory is not available. In such a case, you need to either 1) copy/set the audio data directly to the external hardware buffer, or 2) make an intermediate buffer and copy/set the data from it to the external hardware buffer in interrupts (or in tasklets, preferably).h]hXVSome chips have their own hardware buffers and DMA transfer from the host memory is not available. In such a case, you need to either 1) copy/set the audio data directly to the external hardware buffer, or 2) make an intermediate buffer and copy/set the data from it to the external hardware buffer in interrupts (or in tasklets, preferably).}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMo hjUhhubh)}(hX{The first case works fine if the external hardware buffer is large enough. This method doesn't need any extra buffers and thus is more efficient. You need to define the ``copy`` callback for the data transfer, in addition to the ``fill_silence`` callback for playback. However, there is a drawback: it cannot be mmapped. The examples are GUS's GF1 PCM or emu8000's wavetable PCM.h](hThe first case works fine if the external hardware buffer is large enough. This method doesn’t need any extra buffers and thus is more efficient. You need to define the }(hjUhhhNhNubj)}(h``copy``h]hcopy}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh4 callback for the data transfer, in addition to the }(hjUhhhNhNubj)}(h``fill_silence``h]h fill_silence}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh callback for playback. However, there is a drawback: it cannot be mmapped. The examples are GUS’s GF1 PCM or emu8000’s wavetable PCM.}(hjUhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMu hjUhhubh)}(hThe second case allows for mmap on the buffer, although you have to handle an interrupt or a tasklet to transfer the data from the intermediate buffer to the hardware buffer. You can find an example in the vxpocket driver.h]hThe second case allows for mmap on the buffer, although you have to handle an interrupt or a tasklet to transfer the data from the intermediate buffer to the hardware buffer. You can find an example in the vxpocket driver.}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM| hjUhhubh)}(hXAnother case is when the chip uses a PCI memory-map region for the buffer instead of the host memory. In this case, mmap is available only on certain architectures like the Intel one. In non-mmap mode, the data cannot be transferred as in the normal way. Thus you need to define the ``copy`` and ``fill_silence`` callbacks as well, as in the cases above. Examples are found in ``rme32.c`` and ``rme96.c``.h](hXAnother case is when the chip uses a PCI memory-map region for the buffer instead of the host memory. In this case, mmap is available only on certain architectures like the Intel one. In non-mmap mode, the data cannot be transferred as in the normal way. Thus you need to define the }(hj VhhhNhNubj)}(h``copy``h]hcopy}(hj(VhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj Vubh and }(hj VhhhNhNubj)}(h``fill_silence``h]h fill_silence}(hj:VhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj VubhA callbacks as well, as in the cases above. Examples are found in }(hj VhhhNhNubj)}(h ``rme32.c``h]hrme32.c}(hjLVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj Vubh and }(hj VhhhNhNubj)}(h ``rme96.c``h]hrme96.c}(hj^VhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj Vubh.}(hj VhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hX The implementation of the ``copy`` and ``silence`` callbacks depends upon whether the hardware supports interleaved or non-interleaved samples. The ``copy`` callback is defined like below, a bit differently depending on whether the direction is playback or capture::h](hThe implementation of the }(hjvVhhhNhNubj)}(h``copy``h]hcopy}(hj~VhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjvVubh and }(hjvVhhhNhNubj)}(h ``silence``h]hsilence}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjvVubhb callbacks depends upon whether the hardware supports interleaved or non-interleaved samples. The }(hjvVhhhNhNubj)}(h``copy``h]hcopy}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjvVubhm callback is defined like below, a bit differently depending on whether the direction is playback or capture:}(hjvVhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubj)}(hXFstatic int playback_copy(struct snd_pcm_substream *substream, int channel, unsigned long pos, struct iov_iter *src, unsigned long count); static int capture_copy(struct snd_pcm_substream *substream, int channel, unsigned long pos, struct iov_iter *dst, unsigned long count);h]hXFstatic int playback_copy(struct snd_pcm_substream *substream, int channel, unsigned long pos, struct iov_iter *src, unsigned long count); static int capture_copy(struct snd_pcm_substream *substream, int channel, unsigned long pos, struct iov_iter *dst, unsigned long count);}hjVsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjUhhubh)}(hIn the case of interleaved samples, the second argument (``channel``) is not used. The third argument (``pos``) specifies the position in bytes.h](h9In the case of interleaved samples, the second argument (}(hjVhhhNhNubj)}(h ``channel``h]hchannel}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjVubh#) is not used. The third argument (}(hjVhhhNhNubj)}(h``pos``h]hpos}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjVubh") specifies the position in bytes.}(hjVhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hThe meaning of the fourth argument is different between playback and capture. For playback, it holds the source data pointer, and for capture, it's the destination data pointer.h]hThe meaning of the fourth argument is different between playback and capture. For playback, it holds the source data pointer, and for capture, it’s the destination data pointer.}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(h6The last argument is the number of bytes to be copied.h]h6The last argument is the number of bytes to be copied.}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hX=What you have to do in this callback is again different between playback and capture directions. In the playback case, you copy the given amount of data (``count``) at the specified pointer (``src``) to the specified offset (``pos``) in the hardware buffer. When coded like memcpy-like way, the copy would look like::h](hWhat you have to do in this callback is again different between playback and capture directions. In the playback case, you copy the given amount of data (}(hjWhhhNhNubj)}(h ``count``h]hcount}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh) at the specified pointer (}(hjWhhhNhNubj)}(h``src``h]hsrc}(hj0WhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh) to the specified offset (}(hjWhhhNhNubj)}(h``pos``h]hpos}(hjBWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubhT) in the hardware buffer. When coded like memcpy-like way, the copy would look like:}(hjWhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubj)}(h1my_memcpy_from_iter(my_buffer + pos, src, count);h]h1my_memcpy_from_iter(my_buffer + pos, src, count);}hjZWsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjUhhubh)}(hFor the capture direction, you copy the given amount of data (``count``) at the specified offset (``pos``) in the hardware buffer to the specified pointer (``dst``)::h](h>For the capture direction, you copy the given amount of data (}(hjhWhhhNhNubj)}(h ``count``h]hcount}(hjpWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjhWubh) at the specified offset (}(hjhWhhhNhNubj)}(h``pos``h]hpos}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjhWubh3) in the hardware buffer to the specified pointer (}(hjhWhhhNhNubj)}(h``dst``h]hdst}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjhWubh):}(hjhWhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubj)}(h/my_memcpy_to_iter(dst, my_buffer + pos, count);h]h/my_memcpy_to_iter(dst, my_buffer + pos, count);}hjWsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjUhhubh)}(hThe given ``src`` or ``dst`` a struct iov_iter pointer containing the pointer and the size. Use the existing helpers to copy or access the data as defined in ``linux/uio.h``.h](h The given }(hjWhhhNhNubj)}(h``src``h]hsrc}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh or }(hjWhhhNhNubj)}(h``dst``h]hdst}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh a struct iov_iter pointer containing the pointer and the size. Use the existing helpers to copy or access the data as defined in }(hjWhhhNhNubj)}(h``linux/uio.h``h]h linux/uio.h}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWubh.}(hjWhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hX!Careful readers might notice that these callbacks receive the arguments in bytes, not in frames like other callbacks. It's because this makes coding easier like in the examples above, and also it makes it easier to unify both the interleaved and non-interleaved cases, as explained below.h]hX#Careful readers might notice that these callbacks receive the arguments in bytes, not in frames like other callbacks. It’s because this makes coding easier like in the examples above, and also it makes it easier to unify both the interleaved and non-interleaved cases, as explained below.}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hIn the case of non-interleaved samples, the implementation will be a bit more complicated. The callback is called for each channel, passed in the second argument, so in total it's called N times per transfer.h]hIn the case of non-interleaved samples, the implementation will be a bit more complicated. The callback is called for each channel, passed in the second argument, so in total it’s called N times per transfer.}(hj XhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hXThe meaning of the other arguments are almost the same as in the interleaved case. The callback is supposed to copy the data from/to the given user-space buffer, but only for the given channel. For details, please check ``isa/gus/gus_pcm.c`` or ``pci/rme9652/rme9652.c`` as examples.h](hThe meaning of the other arguments are almost the same as in the interleaved case. The callback is supposed to copy the data from/to the given user-space buffer, but only for the given channel. For details, please check }(hjXhhhNhNubj)}(h``isa/gus/gus_pcm.c``h]hisa/gus/gus_pcm.c}(hj"XhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh or }(hjXhhhNhNubj)}(h``pci/rme9652/rme9652.c``h]hpci/rme9652/rme9652.c}(hj4XhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh as examples.}(hjXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hUsually for the playback, another callback ``fill_silence`` is defined. It's implemented in a similar way as the copy callbacks above::h](h+Usually for the playback, another callback }(hjLXhhhNhNubj)}(h``fill_silence``h]h fill_silence}(hjTXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjLXubhN is defined. It’s implemented in a similar way as the copy callbacks above:}(hjLXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubj)}(hstatic int silence(struct snd_pcm_substream *substream, int channel, unsigned long pos, unsigned long count);h]hstatic int silence(struct snd_pcm_substream *substream, int channel, unsigned long pos, unsigned long count);}hjlXsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjUhhubh)}(hThe meanings of arguments are the same as in the ``copy`` callback, although there is no buffer pointer argument. In the case of interleaved samples, the channel argument has no meaning, as for the ``copy`` callback.h](h1The meanings of arguments are the same as in the }(hjzXhhhNhNubj)}(h``copy``h]hcopy}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjzXubh callback, although there is no buffer pointer argument. In the case of interleaved samples, the channel argument has no meaning, as for the }(hjzXhhhNhNubj)}(h``copy``h]hcopy}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjzXubh callback.}(hjzXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubh)}(hX(The role of the ``fill_silence`` callback is to set the given amount (``count``) of silence data at the specified offset (``pos``) in the hardware buffer. Suppose that the data format is signed (that is, the silent-data is 0), and the implementation using a memset-like function would look like::h](hThe role of the }(hjXhhhNhNubj)}(h``fill_silence``h]h fill_silence}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh& callback is to set the given amount (}(hjXhhhNhNubj)}(h ``count``h]hcount}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh+) of silence data at the specified offset (}(hjXhhhNhNubj)}(h``pos``h]hpos}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh) in the hardware buffer. Suppose that the data format is signed (that is, the silent-data is 0), and the implementation using a memset-like function would look like:}(hjXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubj)}(h%my_memset(my_buffer + pos, 0, count);h]h%my_memset(my_buffer + pos, 0, count);}hjXsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hjUhhubh)}(hIn the case of non-interleaved samples, again, the implementation becomes a bit more complicated, as it's called N times per transfer for each channel. See, for example, ``isa/gus/gus_pcm.c``.h](hIn the case of non-interleaved samples, again, the implementation becomes a bit more complicated, as it’s called N times per transfer for each channel. See, for example, }(hjXhhhNhNubj)}(h``isa/gus/gus_pcm.c``h]hisa/gus/gus_pcm.c}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjXubh.}(hjXhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjUhhubeh}(h]external-hardware-buffersah ]h"]external hardware buffersah$]h&]uh1hhjRhhhhhMm ubh)}(hhh](h)}(hNon-Contiguous Buffersh]hNon-Contiguous Buffers}(hj)YhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj&YhhhhhM ubh)}(hIf your hardware supports a page table as in emu10k1 or buffer descriptors as in via82xx, you can use scatter-gather (SG) DMA. ALSA provides an interface for handling SG-buffers. The API is provided in ````.h](hIf your hardware supports a page table as in emu10k1 or buffer descriptors as in via82xx, you can use scatter-gather (SG) DMA. ALSA provides an interface for handling SG-buffers. The API is provided in }(hj7YhhhNhNubj)}(h````h]h }(hj?YhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7Yubh.}(hj7YhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj&Yhhubh)}(hX5For creating the SG-buffer handler, call :c:func:`snd_pcm_set_managed_buffer()` or :c:func:`snd_pcm_set_managed_buffer_all()` with ``SNDRV_DMA_TYPE_DEV_SG`` in the PCM constructor like for other PCI pre-allocations. You need to pass ``&pci->dev``, where pci is the struct pci_dev pointer of the chip as well::h](h)For creating the SG-buffer handler, call }(hjWYhhhNhNubh)}(h&:c:func:`snd_pcm_set_managed_buffer()`h]j)}(hjaYh]hsnd_pcm_set_managed_buffer()}(hjcYhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj_Yubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_set_managed_bufferuh1hhhhM hjWYubh or }(hjWYhhhNhNubh)}(h*:c:func:`snd_pcm_set_managed_buffer_all()`h]j)}(hjYh]h snd_pcm_set_managed_buffer_all()}(hjYhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjYubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_set_managed_buffer_alluh1hhhhM hjWYubh with }(hjWYhhhNhNubj)}(h``SNDRV_DMA_TYPE_DEV_SG``h]hSNDRV_DMA_TYPE_DEV_SG}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWYubhM in the PCM constructor like for other PCI pre-allocations. You need to pass }(hjWYhhhNhNubj)}(h ``&pci->dev``h]h &pci->dev}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjWYubh>, where pci is the struct pci_dev pointer of the chip as well:}(hjWYhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj&Yhhubj)}(hpsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, max);h]hpsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, max);}hjYsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj&Yhhubh)}(htThe ``struct snd_sg_buf`` instance is created as ``substream->dma_private`` in turn. You can cast the pointer like::h](hThe }(hjYhhhNhNubj)}(h``struct snd_sg_buf``h]hstruct snd_sg_buf}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjYubh instance is created as }(hjYhhhNhNubj)}(h``substream->dma_private``h]hsubstream->dma_private}(hjYhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjYubh( in turn. You can cast the pointer like:}(hjYhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj&Yhhubj)}(hGstruct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;h]hGstruct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;}hjZsbah}(h]h ]h"]h$]h&]jjuh1jhhhM hj&Yhhubh)}(hXThen in the :c:func:`snd_pcm_lib_malloc_pages()` call, the common SG-buffer handler will allocate the non-contiguous kernel pages of the given size and map them as virtually contiguous memory. The virtual pointer is addressed via runtime->dma_area. The physical address (``runtime->dma_addr``) is set to zero, because the buffer is physically non-contiguous. The physical address table is set up in ``sgbuf->table``. You can get the physical address at a certain offset via :c:func:`snd_pcm_sgbuf_get_addr()`.h](h Then in the }(hjZhhhNhNubh)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hj'Zh]hsnd_pcm_lib_malloc_pages()}(hj)ZhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj%Zubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhM hjZubh call, the common SG-buffer handler will allocate the non-contiguous kernel pages of the given size and map them as virtually contiguous memory. The virtual pointer is addressed via runtime->dma_area. The physical address (}(hjZhhhNhNubj)}(h``runtime->dma_addr``h]hruntime->dma_addr}(hjHZhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubhk) is set to zero, because the buffer is physically non-contiguous. The physical address table is set up in }(hjZhhhNhNubj)}(h``sgbuf->table``h]h sgbuf->table}(hjZZhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh;. You can get the physical address at a certain offset via }(hjZhhhNhNubh)}(h":c:func:`snd_pcm_sgbuf_get_addr()`h]j)}(hjnZh]hsnd_pcm_sgbuf_get_addr()}(hjpZhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjlZubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_sgbuf_get_addruh1hhhhM hjZubh.}(hjZhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj&Yhhubh)}(hIf you need to release the SG-buffer data explicitly, call the standard API function :c:func:`snd_pcm_lib_free_pages()` as usual.h](hUIf you need to release the SG-buffer data explicitly, call the standard API function }(hjZhhhNhNubh)}(h":c:func:`snd_pcm_lib_free_pages()`h]j)}(hjZh]hsnd_pcm_lib_free_pages()}(hjZhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjZubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_free_pagesuh1hhhhM hjZubh as usual.}(hjZhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hj&Yhhubeh}(h]juTah ]h"]non-contiguous buffersah$]h&]uh1hhjRhhhhhM jKubh)}(hhh](h)}(hVmalloc'ed Buffersh]hVmalloc’ed Buffers}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjZhhhhhM ubh)}(hXIt's possible to use a buffer allocated via :c:func:`vmalloc()`, for example, for an intermediate buffer. You can simply allocate it via the standard :c:func:`snd_pcm_lib_malloc_pages()` and co. after setting up the buffer preallocation with ``SNDRV_DMA_TYPE_VMALLOC`` type::h](h.It’s possible to use a buffer allocated via }(hjZhhhNhNubh)}(h:c:func:`vmalloc()`h]j)}(hjZh]h vmalloc()}(hjZhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjZubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjvmallocuh1hhhhM hjZubhW, for example, for an intermediate buffer. You can simply allocate it via the standard }(hjZhhhNhNubh)}(h$:c:func:`snd_pcm_lib_malloc_pages()`h]j)}(hj [h]hsnd_pcm_lib_malloc_pages()}(hj [hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj [ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_lib_malloc_pagesuh1hhhhM hjZubh8 and co. after setting up the buffer preallocation with }(hjZhhhNhNubj)}(h``SNDRV_DMA_TYPE_VMALLOC``h]hSNDRV_DMA_TYPE_VMALLOC}(hj,[hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjZubh type:}(hjZhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM hjZhhubj)}(hgsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);h]hgsnd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC, NULL, 0, 0);}hjD[sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjZhhubh)}(hNULL is passed as the device pointer argument, which indicates that default pages (GFP_KERNEL and GFP_HIGHMEM) will be allocated.h]hNULL is passed as the device pointer argument, which indicates that default pages (GFP_KERNEL and GFP_HIGHMEM) will be allocated.}(hjR[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjZhhubh)}(hAlso, note that zero is passed as both the size and the max size argument here. Since each vmalloc call should succeed at any time, we don't need to pre-allocate the buffers like other continuous pages.h]hAlso, note that zero is passed as both the size and the max size argument here. Since each vmalloc call should succeed at any time, we don’t need to pre-allocate the buffers like other continuous pages.}(hj`[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjZhhubeh}(h]vmalloc-ed-buffersah ]h"]vmalloc'ed buffersah$]h&]uh1hhjRhhhhhM ubeh}(h]jah ]h"]buffer and memory managementah$]h&]uh1hhhhhhhhM! jKubh)}(hhh](h)}(hProc Interfaceh]hProc Interface}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj}[hhhhhMubh)}(hALSA provides an easy interface for procfs. The proc files are very useful for debugging. I recommend you set up proc files if you write a driver and want to get a running status or register dumps. The API is found in ````.h](hALSA provides an easy interface for procfs. The proc files are very useful for debugging. I recommend you set up proc files if you write a driver and want to get a running status or register dumps. The API is found in }(hj[hhhNhNubj)}(h````h]h}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj[ubh.}(hj[hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj}[hhubh)}(h;To create a proc file, call :c:func:`snd_card_proc_new()`::h](hTo create a proc file, call }(hj[hhhNhNubh)}(h:c:func:`snd_card_proc_new()`h]j)}(hj[h]hsnd_card_proc_new()}(hj[hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj[ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_proc_newuh1hhhhMhj[ubh:}(hj[hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj}[hhubj)}(hSstruct snd_info_entry *entry; int err = snd_card_proc_new(card, "my-file", &entry);h]hSstruct snd_info_entry *entry; int err = snd_card_proc_new(card, "my-file", &entry);}hj[sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj}[hhubh)}(hwhere the second argument specifies the name of the proc file to be created. The above example will create a file ``my-file`` under the card directory, e.g. ``/proc/asound/card0/my-file``.h](hrwhere the second argument specifies the name of the proc file to be created. The above example will create a file }(hj[hhhNhNubj)}(h ``my-file``h]hmy-file}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj[ubh under the card directory, e.g. }(hj[hhhNhNubj)}(h``/proc/asound/card0/my-file``h]h/proc/asound/card0/my-file}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj[ubh.}(hj[hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj}[hhubh)}(hLike other components, the proc entry created via :c:func:`snd_card_proc_new()` will be registered and released automatically in the card registration and release functions.h](h2Like other components, the proc entry created via }(hj\hhhNhNubh)}(h:c:func:`snd_card_proc_new()`h]j)}(hj)\h]hsnd_card_proc_new()}(hj+\hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'\ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_proc_newuh1hhhhM"hj\ubh^ will be registered and released automatically in the card registration and release functions.}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM"hj}[hhubh)}(hX*When the creation is successful, the function stores a new instance in the pointer given in the third argument. It is initialized as a text proc file for read only. To use this proc file as a read-only text file as-is, set the read callback with private data via :c:func:`snd_info_set_text_ops()`::h](hXWhen the creation is successful, the function stores a new instance in the pointer given in the third argument. It is initialized as a text proc file for read only. To use this proc file as a read-only text file as-is, set the read callback with private data via }(hjP\hhhNhNubh)}(h!:c:func:`snd_info_set_text_ops()`h]j)}(hjZ\h]hsnd_info_set_text_ops()}(hj\\hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjX\ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_info_set_text_opsuh1hhhhM&hjP\ubh:}(hjP\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM&hj}[hhubj)}(h1snd_info_set_text_ops(entry, chip, my_proc_read);h]h1snd_info_set_text_ops(entry, chip, my_proc_read);}hj\sbah}(h]h ]h"]h$]h&]jjuh1jhhhM,hj}[hhubh)}(hwhere the second argument (``chip``) is the private data to be used in the callback. The third parameter specifies the read buffer size and the fourth (``my_proc_read``) is the callback function, which is defined like::h](hwhere the second argument (}(hj\hhhNhNubj)}(h``chip``h]hchip}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubhu) is the private data to be used in the callback. The third parameter specifies the read buffer size and the fourth (}(hj\hhhNhNubj)}(h``my_proc_read``h]h my_proc_read}(hj\hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj\ubh2) is the callback function, which is defined like:}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM.hj}[hhubj)}(hpstatic void my_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);h]hpstatic void my_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer);}hj\sbah}(h]h ]h"]h$]h&]jjuh1jhhhM3hj}[hhubh)}(hIn the read callback, use :c:func:`snd_iprintf()` for output strings, which works just like normal :c:func:`printf()`. For example::h](hIn the read callback, use }(hj\hhhNhNubh)}(h:c:func:`snd_iprintf()`h]j)}(hj\h]h snd_iprintf()}(hj\hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj\ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_iprintfuh1hhhhM6hj\ubh2 for output strings, which works just like normal }(hj\hhhNhNubh)}(h:c:func:`printf()`h]j)}(hj\h]hprintf()}(hj\hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj\ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjprintfuh1hhhhM6hj\ubh. For example:}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM6hj}[hhubj)}(hXstatic void my_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct my_chip *chip = entry->private_data; snd_iprintf(buffer, "This is my chip!\n"); snd_iprintf(buffer, "Port = %ld\n", chip->port); }h]hXstatic void my_proc_read(struct snd_info_entry *entry, struct snd_info_buffer *buffer) { struct my_chip *chip = entry->private_data; snd_iprintf(buffer, "This is my chip!\n"); snd_iprintf(buffer, "Port = %ld\n", chip->port); }}hj#]sbah}(h]h ]h"]h$]h&]jjuh1jhhhM:hj}[hhubh)}(hThe file permissions can be changed afterwards. By default, they are read only for all users. If you want to add write permission for the user (root by default), do as follows::h]hThe file permissions can be changed afterwards. By default, they are read only for all users. If you want to add write permission for the user (root by default), do as follows:}(hj1]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMChj}[hhubj)}(h*entry->mode = S_IFREG | S_IRUGO | S_IWUSR;h]h*entry->mode = S_IFREG | S_IRUGO | S_IWUSR;}hj?]sbah}(h]h ]h"]h$]h&]jjuh1jhhhMGhj}[hhubh)}(h0and set the write buffer size and the callback::h]h/and set the write buffer size and the callback:}(hjM]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMIhj}[hhubj)}(h$entry->c.text.write = my_proc_write;h]h$entry->c.text.write = my_proc_write;}hj[]sbah}(h]h ]h"]h$]h&]jjuh1jhhhMKhj}[hhubh)}(hIn the write callback, you can use :c:func:`snd_info_get_line()` to get a text line, and :c:func:`snd_info_get_str()` to retrieve a string from the line. Some examples are found in ``core/oss/mixer_oss.c``, core/oss/and ``pcm_oss.c``.h](h#In the write callback, you can use }(hji]hhhNhNubh)}(h:c:func:`snd_info_get_line()`h]j)}(hjs]h]hsnd_info_get_line()}(hju]hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjq]ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_info_get_lineuh1hhhhMMhji]ubh to get a text line, and }(hji]hhhNhNubh)}(h:c:func:`snd_info_get_str()`h]j)}(hj]h]hsnd_info_get_str()}(hj]hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj]ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_info_get_struh1hhhhMMhji]ubh@ to retrieve a string from the line. Some examples are found in }(hji]hhhNhNubj)}(h``core/oss/mixer_oss.c``h]hcore/oss/mixer_oss.c}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1jhji]ubh, core/oss/and }(hji]hhhNhNubj)}(h ``pcm_oss.c``h]h pcm_oss.c}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1jhji]ubh.}(hji]hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhj}[hhubh)}(h9For a raw-data proc-file, set the attributes as follows::h]h8For a raw-data proc-file, set the attributes as follows:}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMRhj}[hhubj)}(hstatic const struct snd_info_entry_ops my_file_io_ops = { .read = my_file_io_read, }; entry->content = SNDRV_INFO_CONTENT_DATA; entry->private_data = chip; entry->c.ops = &my_file_io_ops; entry->size = 4096; entry->mode = S_IFREG | S_IRUGO;h]hstatic const struct snd_info_entry_ops my_file_io_ops = { .read = my_file_io_read, }; entry->content = SNDRV_INFO_CONTENT_DATA; entry->private_data = chip; entry->c.ops = &my_file_io_ops; entry->size = 4096; entry->mode = S_IFREG | S_IRUGO;}hj]sbah}(h]h ]h"]h$]h&]jjuh1jhhhMThj}[hhubh)}(hkFor raw data, ``size`` field must be set properly. This specifies the maximum size of the proc file access.h](hFor raw data, }(hj]hhhNhNubj)}(h``size``h]hsize}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj]ubhU field must be set properly. This specifies the maximum size of the proc file access.}(hj]hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM^hj}[hhubh)}(hThe read/write callbacks of raw mode are more direct than the text mode. You need to use a low-level I/O functions such as :c:func:`copy_from_user()` and :c:func:`copy_to_user()` to transfer the data::h](h{The read/write callbacks of raw mode are more direct than the text mode. You need to use a low-level I/O functions such as }(hj^hhhNhNubh)}(h:c:func:`copy_from_user()`h]j)}(hj'^h]hcopy_from_user()}(hj)^hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj%^ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjcopy_from_useruh1hhhhMahj^ubh and }(hj^hhhNhNubh)}(h:c:func:`copy_to_user()`h]j)}(hjJ^h]hcopy_to_user()}(hjL^hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjH^ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj copy_to_useruh1hhhhMahj^ubh to transfer the data:}(hj^hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMahj}[hhubj)}(hXstatic ssize_t my_file_io_read(struct snd_info_entry *entry, void *file_private_data, struct file *file, char *buf, size_t count, loff_t pos) { if (copy_to_user(buf, local_data + pos, count)) return -EFAULT; return count; }h]hXstatic ssize_t my_file_io_read(struct snd_info_entry *entry, void *file_private_data, struct file *file, char *buf, size_t count, loff_t pos) { if (copy_to_user(buf, local_data + pos, count)) return -EFAULT; return count; }}hjq^sbah}(h]h ]h"]h$]h&]jjuh1jhhhMfhj}[hhubh)}(hIf the size of the info entry has been set up properly, ``count`` and ``pos`` are guaranteed to fit within 0 and the given size. You don't have to check the range in the callbacks unless any other condition is required.h](h8If the size of the info entry has been set up properly, }(hj^hhhNhNubj)}(h ``count``h]hcount}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubh and }(hj^hhhNhNubj)}(h``pos``h]hpos}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubh are guaranteed to fit within 0 and the given size. You don’t have to check the range in the callbacks unless any other condition is required.}(hj^hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMrhj}[hhubeh}(h]jA ah ]h"]proc interfaceah$]h&]uh1hhhhhhhhMjKubh)}(hhh](h)}(hPower Managementh]hPower Management}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj^hhhhhMxubh)}(hXIf the chip is supposed to work with suspend/resume functions, you need to add power-management code to the driver. The additional code for power-management should be ifdef-ed with ``CONFIG_PM``, or annotated with __maybe_unused attribute; otherwise the compiler will complain.h](hIf the chip is supposed to work with suspend/resume functions, you need to add power-management code to the driver. The additional code for power-management should be ifdef-ed with }(hj^hhhNhNubj)}(h ``CONFIG_PM``h]h CONFIG_PM}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubhS, or annotated with __maybe_unused attribute; otherwise the compiler will complain.}(hj^hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMzhj^hhubh)}(hXIf the driver *fully* supports suspend/resume that is, the device can be properly resumed to its state when suspend was called, you can set the ``SNDRV_PCM_INFO_RESUME`` flag in the PCM info field. Usually, this is possible when the registers of the chip can be safely saved and restored to RAM. If this is set, the trigger callback is called with ``SNDRV_PCM_TRIGGER_RESUME`` after the resume callback completes.h](hIf the driver }(hj^hhhNhNubj")}(h*fully*h]hfully}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1j"hj^ubh{ supports suspend/resume that is, the device can be properly resumed to its state when suspend was called, you can set the }(hj^hhhNhNubj)}(h``SNDRV_PCM_INFO_RESUME``h]hSNDRV_PCM_INFO_RESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubh flag in the PCM info field. Usually, this is possible when the registers of the chip can be safely saved and restored to RAM. If this is set, the trigger callback is called with }(hj^hhhNhNubj)}(h``SNDRV_PCM_TRIGGER_RESUME``h]hSNDRV_PCM_TRIGGER_RESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj^ubh% after the resume callback completes.}(hj^hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubh)}(hXEven if the driver doesn't support PM fully but partial suspend/resume is still possible, it's still worthy to implement suspend/resume callbacks. In such a case, applications would reset the status by calling :c:func:`snd_pcm_prepare()` and restart the stream appropriately. Hence, you can define suspend/resume callbacks below but don't set the ``SNDRV_PCM_INFO_RESUME`` info flag to the PCM.h](hEven if the driver doesn’t support PM fully but partial suspend/resume is still possible, it’s still worthy to implement suspend/resume callbacks. In such a case, applications would reset the status by calling }(hj-_hhhNhNubh)}(h:c:func:`snd_pcm_prepare()`h]j)}(hj7_h]hsnd_pcm_prepare()}(hj9_hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj5_ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_prepareuh1hhhhMhj-_ubhp and restart the stream appropriately. Hence, you can define suspend/resume callbacks below but don’t set the }(hj-_hhhNhNubj)}(h``SNDRV_PCM_INFO_RESUME``h]hSNDRV_PCM_INFO_RESUME}(hjX_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-_ubh info flag to the PCM.}(hj-_hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubh)}(hXNote that the trigger with SUSPEND can always be called when :c:func:`snd_pcm_suspend_all()` is called, regardless of the ``SNDRV_PCM_INFO_RESUME`` flag. The ``RESUME`` flag affects only the behavior of :c:func:`snd_pcm_resume()`. (Thus, in theory, ``SNDRV_PCM_TRIGGER_RESUME`` isn't needed to be handled in the trigger callback when no ``SNDRV_PCM_INFO_RESUME`` flag is set. But, it's better to keep it for compatibility reasons.)h](h=Note that the trigger with SUSPEND can always be called when }(hjp_hhhNhNubh)}(h:c:func:`snd_pcm_suspend_all()`h]j)}(hjz_h]hsnd_pcm_suspend_all()}(hj|_hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjx_ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_suspend_alluh1hhhhMhjp_ubh is called, regardless of the }(hjp_hhhNhNubj)}(h``SNDRV_PCM_INFO_RESUME``h]hSNDRV_PCM_INFO_RESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjp_ubh flag. The }(hjp_hhhNhNubj)}(h ``RESUME``h]hRESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjp_ubh# flag affects only the behavior of }(hjp_hhhNhNubh)}(h:c:func:`snd_pcm_resume()`h]j)}(hj_h]hsnd_pcm_resume()}(hj_hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj_ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_resumeuh1hhhhMhjp_ubh. (Thus, in theory, }(hjp_hhhNhNubj)}(h``SNDRV_PCM_TRIGGER_RESUME``h]hSNDRV_PCM_TRIGGER_RESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjp_ubh> isn’t needed to be handled in the trigger callback when no }(hjp_hhhNhNubj)}(h``SNDRV_PCM_INFO_RESUME``h]hSNDRV_PCM_INFO_RESUME}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjp_ubhG flag is set. But, it’s better to keep it for compatibility reasons.)}(hjp_hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubh)}(hThe driver needs to define the suspend/resume hooks according to the bus the device is connected to. In the case of PCI drivers, the callbacks look like below::h]hThe driver needs to define the suspend/resume hooks according to the bus the device is connected to. In the case of PCI drivers, the callbacks look like below:}(hj `hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hstatic int __maybe_unused snd_my_suspend(struct device *dev) { .... /* do things for suspend */ return 0; } static int __maybe_unused snd_my_resume(struct device *dev) { .... /* do things for suspend */ return 0; }h]hstatic int __maybe_unused snd_my_suspend(struct device *dev) { .... /* do things for suspend */ return 0; } static int __maybe_unused snd_my_resume(struct device *dev) { .... /* do things for suspend */ return 0; }}hj`sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubh)}(h1The scheme of the real suspend job is as follows:h]h1The scheme of the real suspend job is as follows:}(hj(`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubhenumerated_list)}(hhh](j)}(h%Retrieve the card and the chip data. h]h)}(h$Retrieve the card and the chip data.h]h$Retrieve the card and the chip data.}(hj?`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj;`ubah}(h]h ]h"]h$]h&]uh1jhj8`hhhhhNubj)}(hcCall :c:func:`snd_power_change_state()` with ``SNDRV_CTL_POWER_D3hot`` to change the power status. h]h)}(hbCall :c:func:`snd_power_change_state()` with ``SNDRV_CTL_POWER_D3hot`` to change the power status.h](hCall }(hjW`hhhNhNubh)}(h":c:func:`snd_power_change_state()`h]j)}(hja`h]hsnd_power_change_state()}(hjc`hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj_`ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_power_change_stateuh1hhhhMhjW`ubh with }(hjW`hhhNhNubj)}(h``SNDRV_CTL_POWER_D3hot``h]hSNDRV_CTL_POWER_D3hot}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjW`ubh to change the power status.}(hjW`hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjS`ubah}(h]h ]h"]h$]h&]uh1jhj8`hhhhhNubj)}(hKIf AC97 codecs are used, call :c:func:`snd_ac97_suspend()` for each codec. h]h)}(hJIf AC97 codecs are used, call :c:func:`snd_ac97_suspend()` for each codec.h](hIf AC97 codecs are used, call }(hj`hhhNhNubh)}(h:c:func:`snd_ac97_suspend()`h]j)}(hj`h]hsnd_ac97_suspend()}(hj`hhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj`ubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_suspenduh1hhhhMhj`ubh for each codec.}(hj`hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj`ubah}(h]h ]h"]h$]h&]uh1jhj8`hhhhhNubj)}(h'Save the register values if necessary. h]h)}(h&Save the register values if necessary.h]h&Save the register values if necessary.}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj`ubah}(h]h ]h"]h$]h&]uh1jhj8`hhhhhNubj)}(h Stop the hardware if necessary. h]h)}(hStop the hardware if necessary.h]hStop the hardware if necessary.}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj`ubah}(h]h ]h"]h$]h&]uh1jhj8`hhhhhNubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix.uh1j6`hj^hhhhhMubh)}(hTypical code would look like::h]hTypical code would look like:}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hXstatic int __maybe_unused mychip_suspend(struct device *dev) { /* (1) */ struct snd_card *card = dev_get_drvdata(dev); struct mychip *chip = card->private_data; /* (2) */ snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); /* (3) */ snd_ac97_suspend(chip->ac97); /* (4) */ snd_mychip_save_registers(chip); /* (5) */ snd_mychip_stop_hardware(chip); return 0; }h]hXstatic int __maybe_unused mychip_suspend(struct device *dev) { /* (1) */ struct snd_card *card = dev_get_drvdata(dev); struct mychip *chip = card->private_data; /* (2) */ snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); /* (3) */ snd_ac97_suspend(chip->ac97); /* (4) */ snd_mychip_save_registers(chip); /* (5) */ snd_mychip_stop_hardware(chip); return 0; }}hj$asbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubh)}(h0The scheme of the real resume job is as follows:h]h0The scheme of the real resume job is as follows:}(hj2ahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj7`)}(hhh](j)}(h%Retrieve the card and the chip data. h]h)}(h$Retrieve the card and the chip data.h]h$Retrieve the card and the chip data.}(hjGahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjCaubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubj)}(hRe-initialize the chip. h]h)}(hRe-initialize the chip.h]hRe-initialize the chip.}(hj_ahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj[aubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubj)}(h*Restore the saved registers if necessary. h]h)}(h)Restore the saved registers if necessary.h]h)Restore the saved registers if necessary.}(hjwahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjsaubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubj)}(h?Resume the mixer, e.g. by calling :c:func:`snd_ac97_resume()`. h]h)}(h>Resume the mixer, e.g. by calling :c:func:`snd_ac97_resume()`.Eh](h"Resume the mixer, e.g. by calling }(hjahhhNhNubh)}(h:c:func:`snd_ac97_resume()`h]j)}(hjah]hsnd_ac97_resume()}(hjahhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjaubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_ac97_resumeuh1hhhhMhjaubh.}(hjahhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjaubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubj)}(hRestart the hardware (if any). h]h)}(hRestart the hardware (if any).h]hRestart the hardware (if any).}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjaubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubj)}(h]Call :c:func:`snd_power_change_state()` with ``SNDRV_CTL_POWER_D0`` to notify the processes. h]h)}(h\Call :c:func:`snd_power_change_state()` with ``SNDRV_CTL_POWER_D0`` to notify the processes.h](hCall }(hjahhhNhNubh)}(h":c:func:`snd_power_change_state()`h]j)}(hjah]hsnd_power_change_state()}(hjahhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjaubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_power_change_stateuh1hhhhMhjaubh with }(hjahhhNhNubj)}(h``SNDRV_CTL_POWER_D0``h]hSNDRV_CTL_POWER_D0}(hj bhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubh to notify the processes.}(hjahhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjaubah}(h]h ]h"]h$]h&]uh1jhj@ahhhhhNubeh}(h]h ]h"]h$]h&]jajajahjajauh1j6`hj^hhhhhMubh)}(hTypical code would look like::h]hTypical code would look like:}(hj1bhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hXstatic int __maybe_unused mychip_resume(struct pci_dev *pci) { /* (1) */ struct snd_card *card = dev_get_drvdata(dev); struct mychip *chip = card->private_data; /* (2) */ snd_mychip_reinit_chip(chip); /* (3) */ snd_mychip_restore_registers(chip); /* (4) */ snd_ac97_resume(chip->ac97); /* (5) */ snd_mychip_restart_chip(chip); /* (6) */ snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; }h]hXstatic int __maybe_unused mychip_resume(struct pci_dev *pci) { /* (1) */ struct snd_card *card = dev_get_drvdata(dev); struct mychip *chip = card->private_data; /* (2) */ snd_mychip_reinit_chip(chip); /* (3) */ snd_mychip_restore_registers(chip); /* (4) */ snd_ac97_resume(chip->ac97); /* (5) */ snd_mychip_restart_chip(chip); /* (6) */ snd_power_change_state(card, SNDRV_CTL_POWER_D0); return 0; }}hj?bsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubh)}(hNote that, at the time this callback gets called, the PCM stream has been already suspended via its own PM ops calling :c:func:`snd_pcm_suspend_all()` internally.h](hwNote that, at the time this callback gets called, the PCM stream has been already suspended via its own PM ops calling }(hjMbhhhNhNubh)}(h:c:func:`snd_pcm_suspend_all()`h]j)}(hjWbh]hsnd_pcm_suspend_all()}(hjYbhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjUbubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_pcm_suspend_alluh1hhhhMhjMbubh internally.}(hjMbhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubh)}(hOK, we have all callbacks now. Let's set them up. In the initialization of the card, make sure that you can get the chip data from the card instance, typically via ``private_data`` field, in case you created the chip data individually::h](hOK, we have all callbacks now. Let’s set them up. In the initialization of the card, make sure that you can get the chip data from the card instance, typically via }(hj~bhhhNhNubj)}(h``private_data``h]h private_data}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj~bubh7 field, in case you created the chip data individually:}(hj~bhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hXstatic int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { .... struct snd_card *card; struct mychip *chip; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); .... chip = kzalloc(sizeof(*chip), GFP_KERNEL); .... card->private_data = chip; .... }h]hXstatic int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { .... struct snd_card *card; struct mychip *chip; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, 0, &card); .... chip = kzalloc(sizeof(*chip), GFP_KERNEL); .... card->private_data = chip; .... }}hjbsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubh)}(hqWhen you created the chip data with :c:func:`snd_card_new()`, it's anyway accessible via ``private_data`` field::h](h$When you created the chip data with }(hjbhhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hjbh]hsnd_card_new()}(hjbhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjbubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMhjbubh, it’s anyway accessible via }(hjbhhhNhNubj)}(h``private_data``h]h private_data}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjbubh field:}(hjbhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hXstatic int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { .... struct snd_card *card; struct mychip *chip; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct mychip), &card); .... chip = card->private_data; .... }h]hXstatic int snd_mychip_probe(struct pci_dev *pci, const struct pci_device_id *pci_id) { .... struct snd_card *card; struct mychip *chip; int err; .... err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE, sizeof(struct mychip), &card); .... chip = card->private_data; .... }}hjbsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubh)}(hIf you need space to save the registers, allocate the buffer for it here, too, since it would be fatal if you cannot allocate a memory in the suspend phase. The allocated buffer should be released in the corresponding destructor.h]hIf you need space to save the registers, allocate the buffer for it here, too, since it would be fatal if you cannot allocate a memory in the suspend phase. The allocated buffer should be released in the corresponding destructor.}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubh)}(h:And next, set suspend/resume callbacks to the pci_driver::h]h9And next, set suspend/resume callbacks to the pci_driver:}(hj chhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj^hhubj)}(hX;static DEFINE_SIMPLE_DEV_PM_OPS(snd_my_pm_ops, mychip_suspend, mychip_resume); static struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_my_ids, .probe = snd_my_probe, .remove = snd_my_remove, .driver = { .pm = &snd_my_pm_ops, }, };h]hX;static DEFINE_SIMPLE_DEV_PM_OPS(snd_my_pm_ops, mychip_suspend, mychip_resume); static struct pci_driver driver = { .name = KBUILD_MODNAME, .id_table = snd_my_ids, .probe = snd_my_probe, .remove = snd_my_remove, .driver = { .pm = &snd_my_pm_ops, }, };}hjcsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj^hhubeh}(h]jah ]h"]power managementah$]h&]uh1hhhhhhhhMxjKubh)}(hhh](h)}(hModule Parametersh]hModule Parameters}(hj1chhhNhNubah}(h]h ]h"]h$]h&]uh1hhj.chhhhhM(ubh)}(h{There are standard module options for ALSA. At least, each module should have the ``index``, ``id`` and ``enable`` options.h](hRThere are standard module options for ALSA. At least, each module should have the }(hj?chhhNhNubj)}(h ``index``h]hindex}(hjGchhhNhNubah}(h]h ]h"]h$]h&]uh1jhj?cubh, }(hj?chhhNhNubj)}(h``id``h]hid}(hjYchhhNhNubah}(h]h ]h"]h$]h&]uh1jhj?cubh and }(hj?chhhNhNubj)}(h ``enable``h]henable}(hjkchhhNhNubah}(h]h ]h"]h$]h&]uh1jhj?cubh options.}(hj?chhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM*hj.chhubh)}(hIf the module supports multiple cards (usually up to 8 = ``SNDRV_CARDS`` cards), they should be arrays. The default initial values are defined already as constants for easier programming::h](h9If the module supports multiple cards (usually up to 8 = }(hjchhhNhNubj)}(h``SNDRV_CARDS``h]h SNDRV_CARDS}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubhs cards), they should be arrays. The default initial values are defined already as constants for easier programming:}(hjchhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM-hj.chhubj)}(hstatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;h]hstatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;}hjcsbah}(h]h ]h"]h$]h&]jjuh1jhhhM1hj.chhubh)}(hIf the module supports only a single card, they could be single variables, instead. ``enable`` option is not always necessary in this case, but it would be better to have a dummy option for compatibility.h](hTIf the module supports only a single card, they could be single variables, instead. }(hjchhhNhNubj)}(h ``enable``h]henable}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubhn option is not always necessary in this case, but it would be better to have a dummy option for compatibility.}(hjchhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM5hj.chhubh)}(hThe module parameters must be declared with the standard ``module_param()``, ``module_param_array()`` and :c:func:`MODULE_PARM_DESC()` macros.h](h9The module parameters must be declared with the standard }(hjchhhNhNubj)}(h``module_param()``h]hmodule_param()}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubh, }(hjchhhNhNubj)}(h``module_param_array()``h]hmodule_param_array()}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubh and }(hjchhhNhNubh)}(h:c:func:`MODULE_PARM_DESC()`h]j)}(hjch]hMODULE_PARM_DESC()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjcubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjMODULE_PARM_DESCuh1hhhhM9hjcubh macros.}(hjchhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM9hj.chhubh)}(h"Typical code would look as below::h]h!Typical code would look as below:}(hj&dhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM=hj.chhubj)}(hXc#define CARD_NAME "My Chip" module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");h]hXc#define CARD_NAME "My Chip" module_param_array(index, int, NULL, 0444); MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard."); module_param_array(id, charp, NULL, 0444); MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard."); module_param_array(enable, bool, NULL, 0444); MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");}hj4dsbah}(h]h ]h"]h$]h&]jjuh1jhhhM?hj.chhubh)}(hAlso, don't forget to define the module description and the license. Especially, the recent modprobe requires to define the module license as GPL, etc., otherwise the system is shown as “tainted”::h]hAlso, don’t forget to define the module description and the license. Especially, the recent modprobe requires to define the module license as GPL, etc., otherwise the system is shown as “tainted”:}(hjBdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMHhj.chhubj)}(hFMODULE_DESCRIPTION("Sound driver for My Chip"); MODULE_LICENSE("GPL");h]hFMODULE_DESCRIPTION("Sound driver for My Chip"); MODULE_LICENSE("GPL");}hjPdsbah}(h]h ]h"]h$]h&]jjuh1jhhhMLhj.chhubeh}(h]module-parametersah ]h"]module parametersah$]h&]uh1hhhhhhhhM(ubh)}(hhh](h)}(hDevice-Managed Resourcesh]hDevice-Managed Resources}(hjidhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjfdhhhhhMQubh)}(hXzIn the examples above, all resources are allocated and released manually. But human beings are lazy in nature, especially developers are lazier. So there are some ways to automate the release part; it's the (device-)managed resources aka devres or devm family. For example, an object allocated via :c:func:`devm_kmalloc()` will be freed automatically at unbinding the device.h](hX/In the examples above, all resources are allocated and released manually. But human beings are lazy in nature, especially developers are lazier. So there are some ways to automate the release part; it’s the (device-)managed resources aka devres or devm family. For example, an object allocated via }(hjwdhhhNhNubh)}(h:c:func:`devm_kmalloc()`h]j)}(hjdh]hdevm_kmalloc()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjdubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj devm_kmallocuh1hhhhMShjwdubh5 will be freed automatically at unbinding the device.}(hjwdhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMShjfdhhubh)}(hX1ALSA core provides also the device-managed helper, namely, :c:func:`snd_devm_card_new()` for creating a card object. Call this functions instead of the normal :c:func:`snd_card_new()`, and you can forget the explicit :c:func:`snd_card_free()` call, as it's called automagically at error and removal paths.h](h;ALSA core provides also the device-managed helper, namely, }(hjdhhhNhNubh)}(h:c:func:`snd_devm_card_new()`h]j)}(hjdh]hsnd_devm_card_new()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjdubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_devm_card_newuh1hhhhMZhjdubhG for creating a card object. Call this functions instead of the normal }(hjdhhhNhNubh)}(h:c:func:`snd_card_new()`h]j)}(hjdh]hsnd_card_new()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjdubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_newuh1hhhhMZhjdubh", and you can forget the explicit }(hjdhhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hjdh]hsnd_card_free()}(hjdhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjdubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhMZhjdubhA call, as it’s called automagically at error and removal paths.}(hjdhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMZhjfdhhubh)}(hOne caveat is that the call of :c:func:`snd_card_free()` would be put at the beginning of the call chain only after you call :c:func:`snd_card_register()`.h](hOne caveat is that the call of }(hjehhhNhNubh)}(h:c:func:`snd_card_free()`h]j)}(hj)eh]hsnd_card_free()}(hj+ehhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhj'eubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_card_freeuh1hhhhM`hjeubhE would be put at the beginning of the call chain only after you call }(hjehhhNhNubh)}(h:c:func:`snd_card_register()`h]j)}(hjLeh]hsnd_card_register()}(hjNehhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjJeubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_registeruh1hhhhM`hjeubh.}(hjehhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM`hjfdhhubh)}(hXnAlso, the ``private_free`` callback is always called at the card free, so be careful to put the hardware clean-up procedure in ``private_free`` callback. It might be called even before you actually set up at an earlier error path. For avoiding such an invalid initialization, you can set ``private_free`` callback after :c:func:`snd_card_register()` call succeeds.h](h Also, the }(hjsehhhNhNubj)}(h``private_free``h]h private_free}(hj{ehhhNhNubah}(h]h ]h"]h$]h&]uh1jhjseubhe callback is always called at the card free, so be careful to put the hardware clean-up procedure in }(hjsehhhNhNubj)}(h``private_free``h]h private_free}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1jhjseubh callback. It might be called even before you actually set up at an earlier error path. For avoiding such an invalid initialization, you can set }(hjsehhhNhNubj)}(h``private_free``h]h private_free}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1jhjseubh callback after }(hjsehhhNhNubh)}(h:c:func:`snd_card_register()`h]j)}(hjeh]hsnd_card_register()}(hjehhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjeubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_card_registeruh1hhhhMdhjseubh call succeeds.}(hjsehhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMdhjfdhhubh)}(hAnother thing to be remarked is that you should use device-managed helpers for each component as much as possible once when you manage the card in that way. Mixing up with the normal and the managed resources may screw up the release order.h]hAnother thing to be remarked is that you should use device-managed helpers for each component as much as possible once when you manage the card in that way. Mixing up with the normal and the managed resources may screw up the release order.}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMkhjfdhhubeh}(h]device-managed-resourcesah ]h"]device-managed resourcesah$]h&]uh1hhhhhhhhMQubh)}(hhh](h)}(h%How To Put Your Driver Into ALSA Treeh]h%How To Put Your Driver Into ALSA Tree}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhjehhhhhMrubh)}(hhh](h)}(hGeneralh]hGeneral}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjfhhhhhMuubh)}(hSo far, you've learned how to write the driver codes. And you might have a question now: how to put my own driver into the ALSA driver tree? Here (finally :) the standard procedure is described briefly.h]hSo far, you’ve learned how to write the driver codes. And you might have a question now: how to put my own driver into the ALSA driver tree? Here (finally :) the standard procedure is described briefly.}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMwhjfhhubh)}(hSuppose that you create a new PCI driver for the card “xyz”. The card module name would be snd-xyz. The new driver is usually put into the alsa-driver tree, ``sound/pci`` directory in the case of PCI cards.h](hSuppose that you create a new PCI driver for the card “xyz”. The card module name would be snd-xyz. The new driver is usually put into the alsa-driver tree, }(hj fhhhNhNubj)}(h ``sound/pci``h]h sound/pci}(hj(fhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj fubh$ directory in the case of PCI cards.}(hj fhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM{hjfhhubh)}(hIn the following sections, the driver code is supposed to be put into Linux kernel tree. The two cases are covered: a driver consisting of a single source file and one consisting of several source files.h]hIn the following sections, the driver code is supposed to be put into Linux kernel tree. The two cases are covered: a driver consisting of a single source file and one consisting of several source files.}(hj@fhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjfhhubeh}(h]id8ah ]h"]h$]generalah&]uh1hhjehhhhhMujKubh)}(hhh](h)}(h Driver with A Single Source Fileh]h Driver with A Single Source File}(hjYfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjVfhhhhhMubj7`)}(hhh](j)}(hModify sound/pci/Makefile Suppose you have a file xyz.c. Add the following two lines:: snd-xyz-y := xyz.o obj-$(CONFIG_SND_XYZ) += snd-xyz.o h](h)}(hModify sound/pci/Makefileh]hModify sound/pci/Makefile}(hjnfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjjfubh)}(hgubj)}(h#obj-$(CONFIG_SND) += sound/pci/xyz/h]h#obj-$(CONFIG_SND) += sound/pci/xyz/}hjtgsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj>gubeh}(h]h ]h"]h$]h&]uh1jhj;ghhhhhNubj)}(hUnder the directory ``sound/pci/xyz``, create a Makefile:: snd-xyz-y := xyz.o abc.o def.o obj-$(CONFIG_SND_XYZ) += snd-xyz.o h](h)}(h:Under the directory ``sound/pci/xyz``, create a Makefile::h](hUnder the directory }(hjghhhNhNubj)}(h``sound/pci/xyz``h]h sound/pci/xyz}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1jhjgubh, create a Makefile:}(hjghhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjgubj)}(hAsnd-xyz-y := xyz.o abc.o def.o obj-$(CONFIG_SND_XYZ) += snd-xyz.oh]hAsnd-xyz-y := xyz.o abc.o def.o obj-$(CONFIG_SND_XYZ) += snd-xyz.o}hjgsbah}(h]h ]h"]h$]h&]jjuh1jhhhMhjgubeh}(h]h ]h"]h$]h&]uh1jhj;ghhhhhNubj)}(hMCreate the Kconfig entry This procedure is as same as in the last section. h](h)}(hCreate the Kconfig entryh]hCreate the Kconfig entry}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjgubh)}(h1This procedure is as same as in the last section.h]h1This procedure is as same as in the last section.}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjgubeh}(h]h ]h"]h$]h&]uh1jhj;ghhhhhNubeh}(h]h ]h"]h$]h&]jajajahjajauh1j6`hjghhhhhMubeh}(h]!drivers-with-several-source-filesah ]h"]!drivers with several source filesah$]h&]uh1hhjehhhhhMubeh}(h]%how-to-put-your-driver-into-alsa-treeah ]h"]%how to put your driver into alsa treeah$]h&]uh1hhhhhhhhMrubh)}(hhh](h)}(hUseful Functionsh]hUseful Functions}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhjghhhhhMubh)}(hhh](h)}(h:c:func:`snd_BUG()`h]h)}(hjhh]j)}(hjhh]h snd_BUG()}(hjhhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjhubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjsnd_BUGuh1hhhhMhjhubah}(h]h ]h"]h$]h&]uh1hhj hhhhhhMubh)}(hIt shows the ``BUG?`` message and stack trace as well as :c:func:`snd_BUG_ON()` at the point. It's useful to show that a fatal error happens there.h](h It shows the }(hj8hhhhNhNubj)}(h``BUG?``h]hBUG?}(hj@hhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj8hubh$ message and stack trace as well as }(hj8hhhhNhNubh)}(h:c:func:`snd_BUG_ON()`h]j)}(hjThh]h snd_BUG_ON()}(hjVhhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjRhubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_BUG_ONuh1hhhhMhj8hubhF at the point. It’s useful to show that a fatal error happens there.}(hj8hhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhhubh)}(h1When no debug flag is set, this macro is ignored.h]h1When no debug flag is set, this macro is ignored.}(hj{hhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhhubeh}(h]snd-bugah ]h"] snd_bug()ah$]h&]uh1hhjghhhhhMubh)}(hhh](h)}(h:c:func:`snd_BUG_ON()`h]h)}(hjhh]j)}(hjhh]h snd_BUG_ON()}(hjhhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjhubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_BUG_ONuh1hhhhMhjhubah}(h]h ]h"]h$]h&]uh1hhjhhhhhhMubh)}(h:c:func:`snd_BUG_ON()` macro is similar with :c:func:`WARN_ON()` macro. For example, snd_BUG_ON(!pointer); or it can be used as the condition, if (snd_BUG_ON(non_zero_is_bug)) return -EINVAL;h](h)}(h:c:func:`snd_BUG_ON()`h]j)}(hjhh]h snd_BUG_ON()}(hjhhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjhubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnj snd_BUG_ONuh1hhhhMhjhubh macro is similar with }(hjhhhhNhNubh)}(h:c:func:`WARN_ON()`h]j)}(hjhh]h WARN_ON()}(hjhhhhNhNubah}(h]h ](jjc-funceh"]h$]h&]uh1jhjhubah}(h]h ]h"]h$]h&]refdocj refdomainjreftypefunc refexplicitrefwarnjWARN_ONuh1hhhhMhjhubh macro. For example, snd_BUG_ON(!pointer); or it can be used as the condition, if (snd_BUG_ON(non_zero_is_bug)) return -EINVAL;}(hjhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhhubh)}(hThe macro takes an conditional expression to evaluate. When ``CONFIG_SND_DEBUG``, is set, if the expression is non-zero, it shows the warning message such as ``BUG? (xxx)`` normally followed by stack trace. In both cases it returns the evaluated value.h](hj>j,6j)6j7j5j6j6j7j7j<7j97j7j7j8jn5j1<j6j:j:j;j;j<j<j*<j'<j*=j'=j=jP8j>j5j3Ej j@j@j~Aj{AjDjDjSDjPDjDjDj,Ej)EjHj, jGjGjHj*GjMjMj&Ij#IjuJjrJjMjIJjJjJj^Kj[KjLjLjpMjmMjMjMjRjRj4Pj1PjvQjsQjRjRjz[jjUj}&j#Yj YjZjuTjs[jp[j^jA j+cjjcdj`djejejgjgjgjgjgjgjKijHijhjhjCij@ijijiu nametypes}(jijWjj8jjzjjLjrjjjjjKjjjjj<jcjjjQ j7j9jj jjjjjQ j j jJ jI jj9 jw jjejjjjjjj=3jdjj#j j!jl"j"j#j-j$j_%j%j'j'j(j{*j+jJ,j,j-jS-jz-j0j1/j/j0j1j63j>j,6j7j6j7j<7j7j8j1<j:j;j<j*<j*=j=j>j3Ej@j~AjDjSDjDj,EjHjGjHjMj&IjuJjMjJj^KjLjpMjMjRj4PjvQjRjz[jUj#YjZjs[j^j+cjcdjejgjgjgjKijhjCijiuh}(jihjTjjjZjjkjwjjjjIjjjOjj}jjjjj jjHjjjNjjjjjjj9jj`j?jjfjjjN jj4jjj:j jjjjjj~jjjjN jj jT jj jG j jF jM jjT j6 je jt j< jjz jbj jjhjjj$jjjjjjjjjj jj\jjjajajjjgj#jj j7j!j ji"j!j"jo"j#j"jj#j`j9$j{#j%j%jb%j'j%j'j'j(j'jx*j(jj~*jG,j+j,jM,j-j,jP-j!-jw-jV-jOj-j./j-j/j4/j/j/j'j 0j$j1j>j@3j3jQ3j)6j3j5j/6j6j6j7j6j97j7j7j?7jn5j7j6j8j:j8j;j:j<j;j'<j<j'=j4<jP8j-=j5j=j j>j?j>j+?j ?j@j1?j{Aj@jDjAjPDjDjDjVDj)EjDj, j6EjEjGEjGjEj*GjGjMjHj#IjHjrJj)IjIJjxJjJjJj[KjJjLjaKjmMjLjMjsMjRjMj1PjMjsQj7PjRjyQjjRj}&jRj YjUjuTj&Yjp[jZjA j}[jj^j`dj.cjejfdjgjejPfjfjgjVfjgjgjHijgjhj hj@ijhjijNiu footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jiKsRparse_messages](hsystem_message)}(hhh]h)}(h4Duplicate implicit target name: "full code example".h]h8Duplicate implicit target name: “full code example”.}(hj]jhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjZjubah}(h]h ]h"]h$]h&]jalevelKtypeINFOsourcehlineMuh1jXjhjhhhhhMubjYj)}(hhh]h)}(h*Duplicate implicit target name: "general".h]h.Duplicate implicit target name: “general”.}(hjyjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjvjubah}(h]h ]h"]h$]h&]j\alevelKtypejsjsourcehlineMKuh1jXjhjhhhhhMKubjYj)}(hhh]h)}(h4Duplicate implicit target name: "full code example".h]h8Duplicate implicit target name: “full code example”.}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]jalevelKtypejsjsourcehlineMfuh1jXjhjahhhhhMfubjYj)}(hhh]h)}(h*Duplicate implicit target name: "general".h]h.Duplicate implicit target name: “general”.}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]j3alevelKtypejsjsourcehlineM. uh1jXjhjQ3hhhhhM. ubjYj)}(hhh]h)}(h*Duplicate implicit target name: "general".h]h.Duplicate implicit target name: “general”.}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]j?alevelKtypejsjsourcehlineM uh1jXjhj>hhhhhM ubjYj)}(hhh]h)}(h4Duplicate implicit target name: "full code example".h]h8Duplicate implicit target name: “full code example”.}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]j+?alevelKtypejsjsourcehlineM uh1jXjhj ?hhhhhM ubjYj)}(hhh]h)}(h*Duplicate implicit target name: "general".h]h.Duplicate implicit target name: “general”.}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]jEalevelKtypejsjsourcehlineMk uh1jXjhjGEhhhhhMk ubjYj)}(hhh]h)}(h*Duplicate implicit target name: "general".h]h.Duplicate implicit target name: “general”.}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjkubah}(h]h ]h"]h$]h&]jPfalevelKtypejsjsourcehlineMuuh1jXjhjfhhhhhMuubetransform_messages] transformerN include_log] decorationNhhub.