forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writing-an-alsa-driver.tmpl
6221 lines (5466 loc) · 200 KB
/
writing-an-alsa-driver.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
<!-- ****************************************************** -->
<!-- Header -->
<!-- ****************************************************** -->
<book id="Writing-an-ALSA-Driver">
<bookinfo>
<title>Writing an ALSA Driver</title>
<author>
<firstname>Takashi</firstname>
<surname>Iwai</surname>
<affiliation>
<address>
<email>[email protected]</email>
</address>
</affiliation>
</author>
<date>Oct 15, 2007</date>
<edition>0.3.7</edition>
<abstract>
<para>
This document describes how to write an ALSA (Advanced Linux
Sound Architecture) driver.
</para>
</abstract>
<legalnotice>
<para>
Copyright (c) 2002-2005 Takashi Iwai <email>[email protected]</email>
</para>
<para>
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.
</para>
<para>
This document is distributed in the hope that it will be useful,
but <emphasis>WITHOUT ANY WARRANTY</emphasis>; without even the
implied warranty of <emphasis>MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE</emphasis>. See the GNU General Public License
for more details.
</para>
<para>
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
</para>
</legalnotice>
</bookinfo>
<!-- ****************************************************** -->
<!-- Preface -->
<!-- ****************************************************** -->
<preface id="preface">
<title>Preface</title>
<para>
This document describes how to write an
<ulink url="http://www.alsa-project.org/"><citetitle>
ALSA (Advanced Linux Sound Architecture)</citetitle></ulink>
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.
</para>
<para>
This 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.
</para>
<para>
If you are already familiar with the older ALSA ver.0.5.x API, you
can check the drivers such as <filename>sound/pci/es1938.c</filename> or
<filename>sound/pci/maestro3.c</filename> which have also almost the same
code-base in the ALSA 0.5.x tree, so you can compare the differences.
</para>
<para>
This document is still a draft version. Any feedback and
corrections, please!!
</para>
</preface>
<!-- ****************************************************** -->
<!-- File Tree Structure -->
<!-- ****************************************************** -->
<chapter id="file-tree">
<title>File Tree Structure</title>
<section id="file-tree-general">
<title>General</title>
<para>
The ALSA drivers are provided in two ways.
</para>
<para>
One is 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 into
two different trees: alsa-kernel and alsa-driver. The former
contains purely the source code 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 ALSA drivers outside of the Linux kernel tree,
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 (and eventually to the 2.6 kernel tree) when they are
finished and confirmed to work fine.
</para>
<para>
The file tree structure of ALSA driver is depicted below. Both
alsa-kernel and alsa-driver have almost the same file
structure, except for <quote>core</quote> directory. It's
named as <quote>acore</quote> in alsa-driver tree.
<example>
<title>ALSA File Tree Structure</title>
<literallayout>
sound
/core
/oss
/seq
/oss
/instr
/ioctl32
/include
/drivers
/mpu401
/opl3
/i2c
/l3
/synth
/emux
/pci
/(cards)
/isa
/(cards)
/arm
/ppc
/sparc
/usb
/pcmcia /(cards)
/oss
</literallayout>
</example>
</para>
</section>
<section id="file-tree-core-directory">
<title>core directory</title>
<para>
This 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.
</para>
<section id="file-tree-core-directory-oss">
<title>core/oss</title>
<para>
The codes for PCM and mixer OSS emulation modules are stored
in this directory. The rawmidi OSS emulation is included in
the ALSA rawmidi code since it's quite small. The sequencer
code is stored in <filename>core/seq/oss</filename> directory (see
<link linkend="file-tree-core-directory-seq-oss"><citetitle>
below</citetitle></link>).
</para>
</section>
<section id="file-tree-core-directory-ioctl32">
<title>core/ioctl32</title>
<para>
This directory contains the 32bit-ioctl wrappers for 64bit
architectures such like x86-64, ppc64 and sparc64. For 32bit
and alpha architectures, these are not compiled.
</para>
</section>
<section id="file-tree-core-directory-seq">
<title>core/seq</title>
<para>
This directory and its sub-directories are for the ALSA
sequencer. This directory contains the sequencer core and
primary sequencer modules such like snd-seq-midi,
snd-seq-virmidi, etc. They are compiled only when
<constant>CONFIG_SND_SEQUENCER</constant> is set in the kernel
config.
</para>
</section>
<section id="file-tree-core-directory-seq-oss">
<title>core/seq/oss</title>
<para>
This contains the OSS sequencer emulation codes.
</para>
</section>
<section id="file-tree-core-directory-deq-instr">
<title>core/seq/instr</title>
<para>
This directory contains the modules for the sequencer
instrument layer.
</para>
</section>
</section>
<section id="file-tree-include-directory">
<title>include directory</title>
<para>
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 at 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 :)
</para>
</section>
<section id="file-tree-drivers-directory">
<title>drivers directory</title>
<para>
This 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.
</para>
<section id="file-tree-drivers-directory-mpu401">
<title>drivers/mpu401</title>
<para>
The MPU401 and MPU401-UART modules are stored here.
</para>
</section>
<section id="file-tree-drivers-directory-opl3">
<title>drivers/opl3 and opl4</title>
<para>
The OPL3 and OPL4 FM-synth stuff is found here.
</para>
</section>
</section>
<section id="file-tree-i2c-directory">
<title>i2c directory</title>
<para>
This contains the ALSA i2c components.
</para>
<para>
Although 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.
</para>
<section id="file-tree-i2c-directory-l3">
<title>i2c/l3</title>
<para>
This is a sub-directory for ARM L3 i2c.
</para>
</section>
</section>
<section id="file-tree-synth-directory">
<title>synth directory</title>
<para>
This contains the synth middle-level modules.
</para>
<para>
So far, there is only Emu8000/Emu10k1 synth driver under
the <filename>synth/emux</filename> sub-directory.
</para>
</section>
<section id="file-tree-pci-directory">
<title>pci directory</title>
<para>
This directory and its sub-directories hold the top-level card modules
for PCI soundcards and the code specific to the PCI BUS.
</para>
<para>
The 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).
</para>
</section>
<section id="file-tree-isa-directory">
<title>isa directory</title>
<para>
This directory and its sub-directories hold the top-level card modules
for ISA soundcards.
</para>
</section>
<section id="file-tree-arm-ppc-sparc-directories">
<title>arm, ppc, and sparc directories</title>
<para>
They are used for top-level card modules which are
specific to one of these architectures.
</para>
</section>
<section id="file-tree-usb-directory">
<title>usb directory</title>
<para>
This directory contains the USB-audio driver. In the latest version, the
USB MIDI driver is integrated in the usb-audio driver.
</para>
</section>
<section id="file-tree-pcmcia-directory">
<title>pcmcia directory</title>
<para>
The 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.
</para>
</section>
<section id="file-tree-oss-directory">
<title>oss directory</title>
<para>
The OSS/Lite source files are stored here in Linux 2.6 (or
later) tree. In the ALSA driver tarball, this directory is empty,
of course :)
</para>
</section>
</chapter>
<!-- ****************************************************** -->
<!-- Basic Flow for PCI Drivers -->
<!-- ****************************************************** -->
<chapter id="basic-flow">
<title>Basic Flow for PCI Drivers</title>
<section id="basic-flow-outline">
<title>Outline</title>
<para>
The minimum flow for PCI soundcards is as follows:
<itemizedlist>
<listitem><para>define the PCI ID table (see the section
<link linkend="pci-resource-entries"><citetitle>PCI Entries
</citetitle></link>).</para></listitem>
<listitem><para>create <function>probe()</function> callback.</para></listitem>
<listitem><para>create <function>remove()</function> callback.</para></listitem>
<listitem><para>create a <structname>pci_driver</structname> structure
containing the three pointers above.</para></listitem>
<listitem><para>create an <function>init()</function> function just calling
the <function>pci_register_driver()</function> to register the pci_driver table
defined above.</para></listitem>
<listitem><para>create an <function>exit()</function> function to call
the <function>pci_unregister_driver()</function> function.</para></listitem>
</itemizedlist>
</para>
</section>
<section id="basic-flow-example">
<title>Full Code Example</title>
<para>
The 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
<function>snd_mychip_probe()</function> function
refer to details explained in the following section.
<example>
<title>Basic Flow for PCI Drivers - Example</title>
<programlisting>
<![CDATA[
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/initval.h>
/* 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 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 "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) {
snd_card_free(card);
return err;
}
/* (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->ioport, chip->irq);
/* (5) */
.... /* implemented later */
/* (6) */
err = snd_card_register(card);
if (err < 0) {
snd_card_free(card);
return err;
}
/* (7) */
pci_set_drvdata(pci, card);
dev++;
return 0;
}
/* destructor -- see the "Destructor" sub-section */
static void snd_mychip_remove(struct pci_dev *pci)
{
snd_card_free(pci_get_drvdata(pci));
pci_set_drvdata(pci, NULL);
}
]]>
</programlisting>
</example>
</para>
</section>
<section id="basic-flow-constructor">
<title>Constructor</title>
<para>
The real constructor of PCI drivers is the <function>probe</function> callback.
The <function>probe</function> callback and other component-constructors which are called
from the <function>probe</function> callback cannot be used with
the <parameter>__init</parameter> prefix
because any PCI device could be a hotplug device.
</para>
<para>
In the <function>probe</function> callback, the following scheme is often used.
</para>
<section id="basic-flow-constructor-device-index">
<title>1) Check and increment the device index.</title>
<para>
<informalexample>
<programlisting>
<![CDATA[
static int dev;
....
if (dev >= SNDRV_CARDS)
return -ENODEV;
if (!enable[dev]) {
dev++;
return -ENOENT;
}
]]>
</programlisting>
</informalexample>
where enable[dev] is the module option.
</para>
<para>
Each time the <function>probe</function> callback is called, check the
availability of the device. If not available, simply increment
the device index and returns. dev will be incremented also
later (<link
linkend="basic-flow-constructor-set-pci"><citetitle>step
7</citetitle></link>).
</para>
</section>
<section id="basic-flow-constructor-create-card">
<title>2) Create a card instance</title>
<para>
<informalexample>
<programlisting>
<![CDATA[
struct snd_card *card;
int err;
....
err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
0, &card);
]]>
</programlisting>
</informalexample>
</para>
<para>
The details will be explained in the section
<link linkend="card-management-card-instance"><citetitle>
Management of Cards and Components</citetitle></link>.
</para>
</section>
<section id="basic-flow-constructor-create-main">
<title>3) Create a main component</title>
<para>
In this part, the PCI resources are allocated.
<informalexample>
<programlisting>
<![CDATA[
struct mychip *chip;
....
err = snd_mychip_create(card, pci, &chip);
if (err < 0) {
snd_card_free(card);
return err;
}
]]>
</programlisting>
</informalexample>
The details will be explained in the section <link
linkend="pci-resource"><citetitle>PCI Resource
Management</citetitle></link>.
</para>
</section>
<section id="basic-flow-constructor-main-component">
<title>4) Set the driver ID and name strings.</title>
<para>
<informalexample>
<programlisting>
<![CDATA[
strcpy(card->driver, "My Chip");
strcpy(card->shortname, "My Own Chip 123");
sprintf(card->longname, "%s at 0x%lx irq %i",
card->shortname, chip->ioport, chip->irq);
]]>
</programlisting>
</informalexample>
The 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.
</para>
<para>
The shortname field is a string shown as more verbose
name. The longname field contains the information
shown in <filename>/proc/asound/cards</filename>.
</para>
</section>
<section id="basic-flow-constructor-create-other">
<title>5) Create other components, such as mixer, MIDI, etc.</title>
<para>
Here you define the basic components such as
<link linkend="pcm-interface"><citetitle>PCM</citetitle></link>,
mixer (e.g. <link linkend="api-ac97"><citetitle>AC97</citetitle></link>),
MIDI (e.g. <link linkend="midi-interface"><citetitle>MPU-401</citetitle></link>),
and other interfaces.
Also, if you want a <link linkend="proc-interface"><citetitle>proc
file</citetitle></link>, define it here, too.
</para>
</section>
<section id="basic-flow-constructor-register-card">
<title>6) Register the card instance.</title>
<para>
<informalexample>
<programlisting>
<![CDATA[
err = snd_card_register(card);
if (err < 0) {
snd_card_free(card);
return err;
}
]]>
</programlisting>
</informalexample>
</para>
<para>
Will be explained in the section <link
linkend="card-management-registration"><citetitle>Management
of Cards and Components</citetitle></link>, too.
</para>
</section>
<section id="basic-flow-constructor-set-pci">
<title>7) Set the PCI driver data and return zero.</title>
<para>
<informalexample>
<programlisting>
<![CDATA[
pci_set_drvdata(pci, card);
dev++;
return 0;
]]>
</programlisting>
</informalexample>
In the above, the card record is stored. This pointer is
used in the remove callback and power-management
callbacks, too.
</para>
</section>
</section>
<section id="basic-flow-destructor">
<title>Destructor</title>
<para>
The destructor, remove callback, simply releases the card
instance. Then the ALSA middle layer will release all the
attached components automatically.
</para>
<para>
It would be typically like the following:
<informalexample>
<programlisting>
<![CDATA[
static void snd_mychip_remove(struct pci_dev *pci)
{
snd_card_free(pci_get_drvdata(pci));
pci_set_drvdata(pci, NULL);
}
]]>
</programlisting>
</informalexample>
The above code assumes that the card pointer is set to the PCI
driver data.
</para>
</section>
<section id="basic-flow-header-files">
<title>Header Files</title>
<para>
For the above example, at least the following include files
are necessary.
<informalexample>
<programlisting>
<![CDATA[
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <sound/core.h>
#include <sound/initval.h>
]]>
</programlisting>
</informalexample>
where 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.
</para>
<para>
In addition to these headers, you'll need
<filename><linux/interrupt.h></filename> for interrupt
handling, and <filename><asm/io.h></filename> for I/O
access. If you use the <function>mdelay()</function> or
<function>udelay()</function> functions, you'll need to include
<filename><linux/delay.h></filename> too.
</para>
<para>
The ALSA interfaces like the PCM and control APIs are defined in other
<filename><sound/xxx.h></filename> header files.
They have to be included after
<filename><sound/core.h></filename>.
</para>
</section>
</chapter>
<!-- ****************************************************** -->
<!-- Management of Cards and Components -->
<!-- ****************************************************** -->
<chapter id="card-management">
<title>Management of Cards and Components</title>
<section id="card-management-card-instance">
<title>Card Instance</title>
<para>
For each soundcard, a <quote>card</quote> record must be allocated.
</para>
<para>
A 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.
</para>
<para>
As mentioned above, to create a card instance, call
<function>snd_card_new()</function>.
<informalexample>
<programlisting>
<![CDATA[
struct snd_card *card;
int err;
err = snd_card_new(&pci->dev, index, id, module, extra_size, &card);
]]>
</programlisting>
</informalexample>
</para>
<para>
The function takes six arguments: the parent device pointer,
the card-index number, the id string, the module pointer (usually
<constant>THIS_MODULE</constant>),
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 <function>snd_card_new()</function>.
</para>
<para>
The first argument, the pointer of struct
<structname>device</structname>, specifies the parent device.
For PCI devices, typically &pci-> is passed there.
</para>
</section>
<section id="card-management-component">
<title>Components</title>
<para>
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 <structname>snd_device</structname> object.
A component can be a PCM instance, a control interface, a raw
MIDI interface, etc. Each such instance has one component
entry.
</para>
<para>
A component can be created via
<function>snd_device_new()</function> function.
<informalexample>
<programlisting>
<![CDATA[
snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);
]]>
</programlisting>
</informalexample>
</para>
<para>
This takes the card pointer, the device-level
(<constant>SNDRV_DEV_XXX</constant>), the data pointer, and the
callback pointers (<parameter>&ops</parameter>). 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 <constant>SNDRV_DEV_LOWLEVEL</constant>.
</para>
<para>
This 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 (<parameter>chip</parameter> in the
above example) is used as the identifier for the instance.
</para>
<para>
Each pre-defined ALSA component such as ac97 and pcm calls
<function>snd_device_new()</function> 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.
</para>
<para>
If you wish to create your own component, you need to
set the destructor function to the dev_free callback in
the <parameter>ops</parameter>, so that it can be released
automatically via <function>snd_card_free()</function>.
The next example will show an implementation of chip-specific
data.
</para>
</section>
<section id="card-management-chip-specific">
<title>Chip-Specific Data</title>
<para>
Chip-specific information, e.g. the I/O port address, its
resource pointer, or the irq number, is stored in the
chip-specific record.
<informalexample>
<programlisting>
<![CDATA[
struct mychip {
....
};
]]>
</programlisting>
</informalexample>
</para>
<para>
In general, there are two ways of allocating the chip record.
</para>
<section id="card-management-chip-specific-snd-card-new">
<title>1. Allocating via <function>snd_card_new()</function>.</title>
<para>
As mentioned above, you can pass the extra-data-length
to the 5th argument of <function>snd_card_new()</function>, i.e.
<informalexample>
<programlisting>
<![CDATA[
err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
sizeof(struct mychip), &card);
]]>
</programlisting>
</informalexample>
struct <structname>mychip</structname> is the type of the chip record.
</para>
<para>
In return, the allocated record can be accessed as
<informalexample>
<programlisting>
<![CDATA[
struct mychip *chip = card->private_data;
]]>
</programlisting>
</informalexample>
With this method, you don't have to allocate twice.
The record is released together with the card instance.
</para>
</section>
<section id="card-management-chip-specific-allocate-extra">
<title>2. Allocating an extra device.</title>
<para>
After allocating a card instance via
<function>snd_card_new()</function> (with
<constant>0</constant> on the 4th arg), call
<function>kzalloc()</function>.
<informalexample>
<programlisting>
<![CDATA[
struct 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);
]]>
</programlisting>
</informalexample>
</para>
<para>
The chip record should have the field to hold the card
pointer at least,
<informalexample>
<programlisting>
<![CDATA[
struct mychip {
struct snd_card *card;
....
};
]]>
</programlisting>
</informalexample>
</para>
<para>
Then, set the card pointer in the returned chip instance.
<informalexample>