logo资料库

writing-an-alsa-driver.pdf

第1页 / 共82页
第2页 / 共82页
第3页 / 共82页
第4页 / 共82页
第5页 / 共82页
第6页 / 共82页
第7页 / 共82页
第8页 / 共82页
资料共82页,剩余部分请下载后查看
Writing an ALSA Driver
Table of Contents
List of Examples
Preface
Chapter 1. File Tree Structure
General
core directory
core/oss
core/ioctl32
core/seq
core/seq/oss
core/seq/instr
include directory
drivers directory
drivers/mpu401
drivers/opl3 and opl4
i2c directory
i2c/l3
synth directory
pci directory
isa directory
arm, ppc, and sparc directories
usb directory
pcmcia directory
oss directory
Chapter 2. Basic Flow for PCI Drivers
Outline
Full Code Example
Constructor
1) Check and increment the device index.
2) Create a card instance
3) Create a main component
4) Set the driver ID and name strings.
5) Create other components, such as mixer, MIDI, etc.
6) Register the card instance.
7) Set the PCI driver data and return zero.
Destructor
Header Files
Chapter 3. Management of Cards and Components
Card Instance
Components
ChipSpecific Data
1. Allocating via sndcardnew().
2. Allocating an extra device.
Registration and Release
Chapter 4. PCI Resource Managements
Full Code Example
Some Hafta's
Resource Allocation
PCI Entries
Chapter 5. PCM Interface
General
Full Code Example
Constructor
... And the Destructor?
Runtime Pointer The Chest of PCM Information
Hardware Description
PCM Configurations
DMA Buffer Information
Running Status
Private Data
Interrupt Callbacks
Operators
open callback
close callback
ioctl callback
hwparams callback
hwfree callback
prepare callback
trigger callback
pointer callback
copy and silence callbacks
ack callback
page callback
Interrupt Handler
Interrupts at the period (fragment) boundary
Highfrequent timer interrupts
On calling sndpcmperiodelapsed()
Atomicity
Constraints
Chapter 6. Control Interface
General
Definition of Controls
Control Names
Global capture and playback
Tonecontrols
3D controls
Mic boost
Access Flags
Callbacks
info callback
get callback
put callback
Callbacks are not atomic
Constructor
Change Notification
Chapter 7. API for AC97 Codec
General
Full Code Example
Constructor
Callbacks
Updating Registers in The Driver
Clock Adjustment
Proc Files
Multiple Codecs
Chapter 8. MIDI (MPU401UART) Interface
General
Constructor
Interrupt Handler
Chapter 9. Miscellaneous Devices
FM OPL3
HardwareDependent Devices
IEC958 (S/PDIF)
Chapter 10. Buffer and Memory Management
Buffer Types
External Hardware Buffers
NonContiguous Buffers
Vmalloc'ed Buffers
Chapter 11. Proc Interface
Chapter 12. Power Management
Chapter 13. Module Parameters
Chapter 14. How To Put Your Driver Into ALSA Tree
General
Driver with A Single Source File
Drivers with Several Source Files
Chapter 15. Useful Functions
sndprintk() and friends
sndassert()
sndruntimecheck()
sndBUG()
Chapter 16. Acknowledgments
Writing an ALSA Driver Takashi Iwai tiwai@suse.de
Writing an ALSA Driver by Takashi Iwai 0.3.3 Edition This document describes how to write an ALSA (Advanced Linux Sound Architecture) driver. Copyright (c) 2002-2004 Takashi Iwai This document is free; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This document is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Table of Contents Preface .........................................................................................................................................................i 1. File Tree Structure.................................................................................................................................1 General ...............................................................................................................................................1 core directory .....................................................................................................................................2 core/oss .....................................................................................................................................2 core/ioctl32 ...............................................................................................................................2 core/seq.....................................................................................................................................2 core/seq/oss...............................................................................................................................2 core/seq/instr.............................................................................................................................2 include directory ................................................................................................................................2 drivers directory .................................................................................................................................2 drivers/mpu401.........................................................................................................................3 drivers/opl3 and opl4 ................................................................................................................3 i2c directory .......................................................................................................................................3 i2c/l3 .........................................................................................................................................3 synth directory....................................................................................................................................3 pci directory .......................................................................................................................................3 isa directory........................................................................................................................................3 arm, ppc, and sparc directories...........................................................................................................4 usb directory.......................................................................................................................................4 pcmcia directory.................................................................................................................................4 oss directory .......................................................................................................................................4 2. Basic Flow for PCI Drivers ...................................................................................................................5 Outline................................................................................................................................................5 Full Code Example.............................................................................................................................5 Constructor.........................................................................................................................................8 1) Check and increment the device index.................................................................................8 2) Create a card instance...........................................................................................................8 3) Create a main component.....................................................................................................8 4) Set the driver ID and name strings. ......................................................................................9 5) Create other components, such as mixer, MIDI, etc. ...........................................................9 6) Register the card instance.....................................................................................................9 7) Set the PCI driver data and return zero. ...............................................................................9 Destructor.........................................................................................................................................10 Header Files .....................................................................................................................................10 3. Management of Cards and Components ...........................................................................................11 Card Instance....................................................................................................................................11 Components .....................................................................................................................................11 Chip-Specific Data ...........................................................................................................................12 1. Allocating via snd_card_new(). .....................................................................................12 2. Allocating an extra device. .................................................................................................12 Registration and Release ..................................................................................................................13 iii
4. PCI Resource Managements...............................................................................................................15 Full Code Example...........................................................................................................................15 Some Hafta’s ....................................................................................................................................17 Resource Allocation.........................................................................................................................17 PCI Entries .......................................................................................................................................21 5. PCM Interface......................................................................................................................................23 General .............................................................................................................................................23 Full Code Example...........................................................................................................................23 Constructor.......................................................................................................................................27 ... And the Destructor? .....................................................................................................................29 Runtime Pointer - The Chest of PCM Information..........................................................................29 Hardware Description.............................................................................................................31 PCM Configurations...............................................................................................................33 DMA Buffer Information .......................................................................................................33 Running Status........................................................................................................................34 Private Data ............................................................................................................................34 Interrupt Callbacks .................................................................................................................34 Operators..........................................................................................................................................35 open callback ..........................................................................................................................35 close callback..........................................................................................................................36 ioctl callback...........................................................................................................................36 hw_params callback ...............................................................................................................36 hw_free callback.....................................................................................................................37 prepare callback......................................................................................................................37 trigger callback .......................................................................................................................38 pointer callback ......................................................................................................................38 copy and silence callbacks......................................................................................................39 ack callback ............................................................................................................................39 page callback ..........................................................................................................................39 Interrupt Handler..............................................................................................................................39 Interrupts at the period (fragment) boundary .........................................................................39 High-frequent timer interrupts................................................................................................40 On calling snd_pcm_period_elapsed() ..........................................................................41 Atomicity .........................................................................................................................................41 Constraints .......................................................................................................................................42 6. Control Interface .................................................................................................................................45 General .............................................................................................................................................45 Definition of Controls ......................................................................................................................45 Control Names .................................................................................................................................46 Global capture and playback ..................................................................................................46 Tone-controls ..........................................................................................................................46 3D controls .............................................................................................................................46 Mic boost ................................................................................................................................46 Access Flags.....................................................................................................................................47 Callbacks..........................................................................................................................................47 info callback ...........................................................................................................................47 get callback.............................................................................................................................48 iv
put callback.............................................................................................................................49 Callbacks are not atomic ........................................................................................................49 Constructor.......................................................................................................................................50 Change Notification .........................................................................................................................50 7. API for AC97 Codec ............................................................................................................................51 General .............................................................................................................................................51 Full Code Example...........................................................................................................................51 Constructor.......................................................................................................................................52 Callbacks..........................................................................................................................................52 Updating Registers in The Driver ....................................................................................................53 Clock Adjustment ............................................................................................................................54 Proc Files..........................................................................................................................................54 Multiple Codecs ...............................................................................................................................54 8. MIDI (MPU401-UART) Interface......................................................................................................55 General .............................................................................................................................................55 Constructor.......................................................................................................................................55 Interrupt Handler..............................................................................................................................56 9. Miscellaneous Devices .........................................................................................................................57 FM OPL3 .........................................................................................................................................57 Hardware-Dependent Devices..........................................................................................................57 IEC958 (S/PDIF)..............................................................................................................................58 10. Buffer and Memory Management....................................................................................................60 Buffer Types.....................................................................................................................................60 External Hardware Buffers...............................................................................................................60 Non-Contiguous Buffers ..................................................................................................................62 Vmalloc’ed Buffers..........................................................................................................................63 11. Proc Interface.....................................................................................................................................64 12. Power Management ...........................................................................................................................67 13. Module Parameters ...........................................................................................................................70 14. How To Put Your Driver Into ALSA Tree.......................................................................................71 General .............................................................................................................................................71 Driver with A Single Source File.....................................................................................................71 Drivers with Several Source Files ....................................................................................................72 15. Useful Functions.................................................................................................................................73 snd_printk() and friends.............................................................................................................73 snd_assert()................................................................................................................................73 snd_runtime_check().................................................................................................................73 snd_BUG() ......................................................................................................................................73 16. Acknowledgments ..............................................................................................................................75 v
List of Examples 1-1. ALSA File Tree Structure ....................................................................................................................1 2-1. Basic Flow for PCI Drivers Example ...................................................................................................5 4-1. PCI Resource Managements Example ...............................................................................................15 5-1. PCM Example Code...........................................................................................................................23 5-2. PCM Instance with a Destructor ........................................................................................................29 5-3. Interrupt Handler Case #1 ..................................................................................................................40 5-4. Interrupt Handler Case #2 ..................................................................................................................40 5-5. Example of Hardware Constraints .....................................................................................................42 5-6. Example of Hardware Constraints for Channels................................................................................43 5-7. Example of Hardware Constraints for Channels................................................................................43 6-1. Definition of a Control .......................................................................................................................45 6-2. Example of info callback....................................................................................................................47 6-3. Example of get callback .....................................................................................................................48 6-4. Example of put callback .....................................................................................................................49 7-1. Example of AC97 Interface................................................................................................................51 14-1. Sample Makefile for a driver xyz .....................................................................................................72 vi
Preface This document describes how to write an ALSA (Advanced Linux Sound Architecture) (http://www.alsa-project.org/) driver. The document focuses mainly on the PCI soundcard. 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. The target of this document is ones who already have enough skill of C language and have the basic knowledge of linux kernel programming. This document doesn’t explain the general topics of linux kernel codes and doesn’t cover the detail of implementation of each low-level driver. It describes only how is the standard way to write a PCI sound driver on ALSA. If you are already familiar with the older ALSA ver.0.5.x, you can check the drivers such as es1938.c or maestro3.c which have also almost the same code-base in the ALSA 0.5.x tree, so you can compare the differences. This document is still a draft version. Any feedbacks and corrections, please!! i
Chapter 1. File Tree Structure General The ALSA drivers are provided in the two ways. One is the the trees provided as a tarball or via cvs from the ALSA’s ftp site, and another is the 2.6 (or later) Linux kernel tree. To synchronize both, the ALSA driver tree is split to two different trees: alsa-kernel and alsa-driver. The former contains purely the source codes for the Linux 2.6 (or later) tree. This tree is designed only for compilation on 2.6 or later environment. The latter, alsa-driver, contains many subtle files for compiling the ALSA driver on the outside of Linux kernel like configure script, the wrapper functions for older, 2.2 and 2.4 kernels, to adapt the latest kernel API, and additional drivers which are still in development or in tests. The drivers in alsa-driver tree will be moved to alsa-kernel (eventually 2.6 kernel tree) once when they are finished and confirmed to work fine. The file tree structure of ALSA driver is depicted below. Both alsa-kernel and alsa-driver have almost the same file structure, except for “core” directory. It’s named as “acore” in alsa-driver tree. Example 1-1. ALSA File Tree Structure sound /core /oss /seq /oss /instr /ioctl32 /include /drivers /mpu401 /opl3 /i2c /l3 /synth /pci /isa /emux /(cards) /(cards) /arm /ppc /sparc /usb /pcmcia /(cards) /oss 1
分享到:
收藏