ALSA Architecture Analysis

Source: Internet
Author: User
Tags naming convention

Statement: This Bo content reproduced from Http://blog.csdn.net/droidphone
I. Overview

ALSA is advanced Linux Sound architecture abbreviation, has become the mainstream of Linux audio architecture, to learn more about ALSA This open source project information and knowledge, please check the following Web site: http:// www.alsa-project.org/.

In the kernel device-driven layer, ALSA provides alsa-driver, while at the application level, ALSA provides us with alsa-lib, which can control the underlying audio hardware by simply invoking the API provided by Alsa-lib.

Figure 1.1 Alsa Software Architecture

As can be seen from Figure 1.1, the alsa-lib of user space provides a uniform API interface to the application, which hides the implementation details of the drive layer and simplifies the implementation of the application. In kernel space, ALSA-SOC is actually a further encapsulation of alsa-driver, and he provides some column-enhanced functionality for embedded devices.  This series of posts only discusses Alsa-driver and ALSA-SOC in embedded systems. Two. ALSA Device file structure

We started our Alsa journey from Alsa's device file structure in Linux. Look at the ALSA-driven device file structure on my computer:

$ cd/dev/snd
$ ls-l

CRW-RW----+ 1 root audio 116, 8 2011-02-23 21:38 controlC0
CRW-RW----+ 1 root audio 116, 4 2011-02-23 21:38 midic0d0
CRW-RW----+ 1 root audio 116, 7 2011-02-23 21:39 pcmc0d0c
CRW-RW----+ 1 root audio 116, 6 2011-02-23 21:56 pcmc0d0p
CRW-RW----+ 1 root audio 116, 5 2011-02-23 21:38 pcmc0d1p
CRW-RW----+ 1 root audio 116, 3 2011-02-23 21:38 seq
CRW-RW----+ 1 root audio 116, 2 2011-02-23 21:38 timer
$


    We can see the following device files: controlC0-->                  for sound card control, such as channel selection, mixing, microphone control, etc. midic0d0 -->                  for playing MIDI audio pcmc0d0c--〉                PCM equipment for recording pcmc0d0p--〉                PCM device seq --〉  for playback                         Sequencer Timer--〉                        Timer

Where c0d0 represents the device in sound card 0 0,pcmc0d0c The last C represents the capture,pcmc0d0p last p representing playback, which is the naming convention in Alsa-driver. As you can see from the list above, my sound card hangs 6 devices, depending on the actual ability of the sound card, the driver can actually hang more kinds of devices, in include/sound/core.h, the following device types are defined:

[C-sharp] View Plain copy print? #define  SNDRV_DEV_TOPLEVEL   ((__force snd_device_type_t)  0)   #define  sndrv_ dev_control    ((__force snd_device_type_t)  1)   #define  sndrv_dev_lowlevel_ pre   ((__force snd_device_type_t)  2)   #define  SNDRV_DEV_LOWLEVEL_NORMAL  (__ force snd_device_type_t)  0x1000   #define  SNDRV_DEV_PCM         ((__force snd_device_type_t)  0x1001)   #define  SNDRV_DEV_RAWMIDI    (__ force snd_device_type_t)  0x1002   #define  SNDRV_DEV_TIMER      (__ force snd_device_type_t)  0x1003   #define  SNDRV_DEV_SEQUENCER  (__force snd_ device_type_t)  0x1004   #define  SNDRV_DEV_HWDEP      (__force snd_ device_type_t)  0x1005   #define  sndrv_dev_info       ((__force snd_device_type_t)  0x1006)   #define  SNDRV_DEV_BUS         ((__force snd_device_type_t)  0x1007)   #define  SNDRV_DEV_CODEC       ((__force snd_device_type_t)  0x1008)   #define  SNDRV_DEV_JACK            ((__force snd_device_type_t)  0x1009)   # define sndrv_dev_lowlevel   ((__force snd_device_type_t)  0x2000)   

Generally, we are more concerned with the PCM and control devices.

Three. Driver code file structure

In the Linux2.6 code tree, the code file structure of ALSA is as follows:

Sound
/core
/oss
/seq
/ioctl32
/include
/drivers
/i2c
/synth
/emux
/pci
/(Cards)
/isa
/(Cards)
/arm
/ppc
/sparc
/usb
/pcmcia/(cards)
/oss
/soc
                   / Codecs core               This directory contains a ALSA-driven middle tier , it is the core part of the entire ALSA drive core/oss        contains PCM and mixer modules that simulate the old OSS architecture core/seq         related Sequencer Code include          ALSA drives the common header file directory, the header file of the directory needs to be exported to user space applications used, typically, the driver module private header files should not be placed here  drivers            Place Some common code i2c            that are not related to CPU and bus architectures       alsa own I2C control code pci                  Top level directory for PCI sound cards, subdirectories contain code for various PCI sound cards isa                  ISA sound card top-level directory with subdirectories containing code for various ISA sound cards soc                 the middle-tier code for the system-on-chip system soc/codecs    For the SOC system of various codec code, and platform-independent Linux ALSA sound card driver Two: the creation of sound card

1. struct Snd_card

What's 1.1 Snd_card?

Snd_card can be said to be the entire ALSA audio-driven top-level of a structure, the entire sound card software logic structure began in the structure, almost all the sound-related logic devices are under the management of Snd_card, the sound card driver's first action is usually to create a snd_card structure. Because of this, in this section, we also start from struct Cnd_card.

Definition of 1.2. Snd_card

The definition of Snd_card is in the header file: include/sound/core.h

[C-sharp] View Plain copy print? /* main structure for soundcard */      struct snd_card {        int number;         /*  number of soundcard  (index to                                    snd_cards)  */          char  id[16];            /* id string  of this card */       char driver[16];         /* driver name */       char  shortname[32];     /* short name of this soundcard */       char longname[80];      /*  name of this soundcard */       char mixername[80] ;     /* mixer name */       char  components[128];       /* card components delimited  with                                   space */        struct module *module;      /*  top-level module */          void *private_data;      /* private data for soundcard */        void  (*private_free)   (struct snd_card *card);  /* callback for freeing of                                    private data  */       struct list_head devices;   /*  devices */          unsigned int last_numid;     /* last used numeric ID */       struct  rw_semaphore controls_rwsem; /* controls list lock */        rwlock_t ctl_files_rwlock;  /* ctl_files list lock */        int controls_count;     /* count  of all controls */       int user_ctl_count;     /* count  of all user controls */       struct list_head  controls;  /* all controls for this card */        struct list_head ctl_files; /* active control files */           struct snd_info_entry *proc_root;   / * root for soundcard specific files */       struct  snd_info_entry *proc_id; /* the card id */        struct proc_dir_entry *proc_root_link;  /* number link to real  id */          struct list_head files_list;     /* all files associated to this card */        Struct snd_shutdown_f_ops *s_f_ops; /* file operations in the shutdown                                    state */        spinlock_t files_lock;      /* lock  the files for this card */       int shutdown ;            /* this card is going  down */       int free_on_last_close;      /* free in context of file_release */        wait_queue_head_t shutdown_sleep;       struct device *dev;      /* device assigned to this card */  #ifndef  config_sysfs_deprecated        STRUCT DEVICE *CARD_DEV;    /* CARDX  object for sysfs */  #endif   #ifdef  config_pm         unsigned int power_state;   /* power state */        struct mutex power_lock;    /* power lock */        wait_queue_head_t power_sleep;   #endif   #if  defined (config_snd_mixer_oss)  | |  defined (config_snd_mixer_oss_module)        struct snd_mixer_oss * mixer_oss;       int mixer_oss_change_count;   #enDIF   };  

struct List_head devices records the list of all logical devices under the sound card struct List_head controls record the list void *private_data Sound of all control units under the sound card Card's private data, you can specify the size of the data by 2 when creating a sound card. The process of establishing sound card

2.1.1. The first step is to create an instance of Snd_card

[C-sharp] struct Snd_card *card;   int err; .... err = Snd_card_create (index, ID, this_module, 0, &card);

Index An integer value, the number ID string for the sound card, and the identifier for the sound card The fourth parameter determines the size of the private data that needs to be allocated at the same time when the Snd_card instance is created, and the pointer to the data is eventually assigned to the Snd_ca RD's Private_data data member card returns a pointer to the Snd_card instance that was created

2.1.2. Step two, create a chip-specific data for your sound card

The private data for the sound card is used primarily for storing some resource information about the sound card, such as interrupting resources, IO resources, DMA resources, and so on. There are two ways to create: By using the fourth argument in Snd_card_create () in the previous step, let Snd_card_create create

[C-sharp] view plain copy print? struct mychip is used to save private data Err = Snd_card_create (index, ID, this_module, sizeof (struct mychip), &c   ARD); Remove struct mychip from private_data *chip = card->private_data;

Create yourself:

[C-sharp] view plain copy print?       struct Mychip {struct Snd_card *card;   ....   };   struct Snd_card *card;   struct Mychip *chip;   Err = Snd_card_create (Index[dev], Id[dev], this_module, 0, &card);   Special data record Snd_card instance Chip->card = Card; ... chip = kzalloc (sizeof (*chip), Gfp_kernel);

Then, register the chip's proprietary data as a lower-order device for the sound card:

[C-sharp] view plain copy print?   static int Snd_mychip_dev_free (struct snd_device *device) {return snd_mychip_free (device->device_data);   static struct Snd_device_ops Ops = {. Dev_free = Snd_mychip_dev_free,}; .... snd_device_new (Card, sndrv_dev_lowlevel, Chip, &ops);

2.1.3. Step three, set the ID and name of the driver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.