blob: 48e4053eda12429ad1f7d979d13b796065db92e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
2
3<book>
4<?dbhtml filename="index.html">
5
6<!-- ****************************************************** -->
7<!-- Header -->
8<!-- ****************************************************** -->
9 <bookinfo>
10 <title>Writing an ALSA Driver</title>
11 <author>
12 <firstname>Takashi</firstname>
13 <surname>Iwai</surname>
14 <affiliation>
15 <address>
16 <email>tiwai@suse.de</email>
17 </address>
18 </affiliation>
19 </author>
20
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +020021 <date>Oct 15, 2007</date>
Clemens Ladischd1761d12007-09-10 08:05:19 +020022 <edition>0.3.7</edition>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 <abstract>
25 <para>
26 This document describes how to write an ALSA (Advanced Linux
27 Sound Architecture) driver.
28 </para>
29 </abstract>
30
31 <legalnotice>
32 <para>
Takashi Iwai7c22f1a2005-10-10 11:46:31 +020033 Copyright (c) 2002-2005 Takashi Iwai <email>tiwai@suse.de</email>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 </para>
35
36 <para>
37 This document is free; you can redistribute it and/or modify it
38 under the terms of the GNU General Public License as published by
39 the Free Software Foundation; either version 2 of the License, or
40 (at your option) any later version.
41 </para>
42
43 <para>
44 This document is distributed in the hope that it will be useful,
45 but <emphasis>WITHOUT ANY WARRANTY</emphasis>; without even the
46 implied warranty of <emphasis>MERCHANTABILITY or FITNESS FOR A
47 PARTICULAR PURPOSE</emphasis>. See the GNU General Public License
48 for more details.
49 </para>
50
51 <para>
52 You should have received a copy of the GNU General Public
53 License along with this program; if not, write to the Free
54 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
55 MA 02111-1307 USA
56 </para>
57 </legalnotice>
58
59 </bookinfo>
60
61<!-- ****************************************************** -->
62<!-- Preface -->
63<!-- ****************************************************** -->
64 <preface id="preface">
65 <title>Preface</title>
66 <para>
67 This document describes how to write an
68 <ulink url="http://www.alsa-project.org/"><citetitle>
69 ALSA (Advanced Linux Sound Architecture)</citetitle></ulink>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +020070 driver. The document focuses mainly on PCI soundcards.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 In the case of other device types, the API might
72 be different, too. However, at least the ALSA kernel API is
73 consistent, and therefore it would be still a bit help for
74 writing them.
75 </para>
76
77 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +020078 This document targets people who already have enough
79 C language skills and have basic linux kernel programming
80 knowledge. This document doesn't explain the general
81 topic of linux kernel coding and doesn't cover low-level
82 driver implementation details. It only describes
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 the standard way to write a PCI sound driver on ALSA.
84 </para>
85
86 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +020087 If you are already familiar with the older ALSA ver.0.5.x API, you
88 can check the drivers such as <filename>sound/pci/es1938.c</filename> or
89 <filename>sound/pci/maestro3.c</filename> which have also almost the same
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 code-base in the ALSA 0.5.x tree, so you can compare the differences.
91 </para>
92
93 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +020094 This document is still a draft version. Any feedback and
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 corrections, please!!
96 </para>
97 </preface>
98
99
100<!-- ****************************************************** -->
101<!-- File Tree Structure -->
102<!-- ****************************************************** -->
103 <chapter id="file-tree">
104 <title>File Tree Structure</title>
105
106 <section id="file-tree-general">
107 <title>General</title>
108 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200109 The ALSA drivers are provided in two ways.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 </para>
111
112 <para>
113 One is the trees provided as a tarball or via cvs from the
114 ALSA's ftp site, and another is the 2.6 (or later) Linux kernel
115 tree. To synchronize both, the ALSA driver tree is split into
116 two different trees: alsa-kernel and alsa-driver. The former
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200117 contains purely the source code for the Linux 2.6 (or later)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 tree. This tree is designed only for compilation on 2.6 or
119 later environment. The latter, alsa-driver, contains many subtle
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200120 files for compiling ALSA drivers outside of the Linux kernel tree,
121 wrapper functions for older 2.2 and 2.4 kernels, to adapt the latest kernel API,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 and additional drivers which are still in development or in
123 tests. The drivers in alsa-driver tree will be moved to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200124 alsa-kernel (and eventually to the 2.6 kernel tree) when they are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 finished and confirmed to work fine.
126 </para>
127
128 <para>
129 The file tree structure of ALSA driver is depicted below. Both
130 alsa-kernel and alsa-driver have almost the same file
131 structure, except for <quote>core</quote> directory. It's
132 named as <quote>acore</quote> in alsa-driver tree.
133
134 <example>
135 <title>ALSA File Tree Structure</title>
136 <literallayout>
137 sound
138 /core
139 /oss
140 /seq
141 /oss
142 /instr
143 /ioctl32
144 /include
145 /drivers
146 /mpu401
147 /opl3
148 /i2c
149 /l3
150 /synth
151 /emux
152 /pci
153 /(cards)
154 /isa
155 /(cards)
156 /arm
157 /ppc
158 /sparc
159 /usb
160 /pcmcia /(cards)
161 /oss
162 </literallayout>
163 </example>
164 </para>
165 </section>
166
167 <section id="file-tree-core-directory">
168 <title>core directory</title>
169 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200170 This directory contains the middle layer which is the heart
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 of ALSA drivers. In this directory, the native ALSA modules are
172 stored. The sub-directories contain different modules and are
173 dependent upon the kernel config.
174 </para>
175
176 <section id="file-tree-core-directory-oss">
177 <title>core/oss</title>
178
179 <para>
180 The codes for PCM and mixer OSS emulation modules are stored
181 in this directory. The rawmidi OSS emulation is included in
182 the ALSA rawmidi code since it's quite small. The sequencer
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200183 code is stored in <filename>core/seq/oss</filename> directory (see
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 <link linkend="file-tree-core-directory-seq-oss"><citetitle>
185 below</citetitle></link>).
186 </para>
187 </section>
188
189 <section id="file-tree-core-directory-ioctl32">
190 <title>core/ioctl32</title>
191
192 <para>
193 This directory contains the 32bit-ioctl wrappers for 64bit
194 architectures such like x86-64, ppc64 and sparc64. For 32bit
195 and alpha architectures, these are not compiled.
196 </para>
197 </section>
198
199 <section id="file-tree-core-directory-seq">
200 <title>core/seq</title>
201 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200202 This directory and its sub-directories are for the ALSA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 sequencer. This directory contains the sequencer core and
204 primary sequencer modules such like snd-seq-midi,
205 snd-seq-virmidi, etc. They are compiled only when
206 <constant>CONFIG_SND_SEQUENCER</constant> is set in the kernel
207 config.
208 </para>
209 </section>
210
211 <section id="file-tree-core-directory-seq-oss">
212 <title>core/seq/oss</title>
213 <para>
214 This contains the OSS sequencer emulation codes.
215 </para>
216 </section>
217
218 <section id="file-tree-core-directory-deq-instr">
219 <title>core/seq/instr</title>
220 <para>
221 This directory contains the modules for the sequencer
222 instrument layer.
223 </para>
224 </section>
225 </section>
226
227 <section id="file-tree-include-directory">
228 <title>include directory</title>
229 <para>
230 This is the place for the public header files of ALSA drivers,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200231 which are to be exported to user-space, or included by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 several files at different directories. Basically, the private
233 header files should not be placed in this directory, but you may
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200234 still find files there, due to historical reasons :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 </para>
236 </section>
237
238 <section id="file-tree-drivers-directory">
239 <title>drivers directory</title>
240 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200241 This directory contains code shared among different drivers
242 on different architectures. They are hence supposed not to be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 architecture-specific.
244 For example, the dummy pcm driver and the serial MIDI
245 driver are found in this directory. In the sub-directories,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200246 there is code for components which are independent from
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 bus and cpu architectures.
248 </para>
249
250 <section id="file-tree-drivers-directory-mpu401">
251 <title>drivers/mpu401</title>
252 <para>
253 The MPU401 and MPU401-UART modules are stored here.
254 </para>
255 </section>
256
257 <section id="file-tree-drivers-directory-opl3">
258 <title>drivers/opl3 and opl4</title>
259 <para>
260 The OPL3 and OPL4 FM-synth stuff is found here.
261 </para>
262 </section>
263 </section>
264
265 <section id="file-tree-i2c-directory">
266 <title>i2c directory</title>
267 <para>
268 This contains the ALSA i2c components.
269 </para>
270
271 <para>
272 Although there is a standard i2c layer on Linux, ALSA has its
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200273 own i2c code for some cards, because the soundcard needs only a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 simple operation and the standard i2c API is too complicated for
275 such a purpose.
276 </para>
277
278 <section id="file-tree-i2c-directory-l3">
279 <title>i2c/l3</title>
280 <para>
281 This is a sub-directory for ARM L3 i2c.
282 </para>
283 </section>
284 </section>
285
286 <section id="file-tree-synth-directory">
287 <title>synth directory</title>
288 <para>
289 This contains the synth middle-level modules.
290 </para>
291
292 <para>
293 So far, there is only Emu8000/Emu10k1 synth driver under
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200294 the <filename>synth/emux</filename> sub-directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 </para>
296 </section>
297
298 <section id="file-tree-pci-directory">
299 <title>pci directory</title>
300 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200301 This directory and its sub-directories hold the top-level card modules
302 for PCI soundcards and the code specific to the PCI BUS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 </para>
304
305 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200306 The drivers compiled from a single file are stored directly
307 in the pci directory, while the drivers with several source files are
308 stored on their own sub-directory (e.g. emu10k1, ice1712).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 </para>
310 </section>
311
312 <section id="file-tree-isa-directory">
313 <title>isa directory</title>
314 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200315 This directory and its sub-directories hold the top-level card modules
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for ISA soundcards.
317 </para>
318 </section>
319
320 <section id="file-tree-arm-ppc-sparc-directories">
321 <title>arm, ppc, and sparc directories</title>
322 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200323 They are used for top-level card modules which are
324 specific to one of these architectures.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 </para>
326 </section>
327
328 <section id="file-tree-usb-directory">
329 <title>usb directory</title>
330 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200331 This directory contains the USB-audio driver. In the latest version, the
332 USB MIDI driver is integrated in the usb-audio driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 </para>
334 </section>
335
336 <section id="file-tree-pcmcia-directory">
337 <title>pcmcia directory</title>
338 <para>
339 The PCMCIA, especially PCCard drivers will go here. CardBus
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200340 drivers will be in the pci directory, because their API is identical
341 to that of standard PCI cards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 </para>
343 </section>
344
345 <section id="file-tree-oss-directory">
346 <title>oss directory</title>
347 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200348 The OSS/Lite source files are stored here in Linux 2.6 (or
349 later) tree. In the ALSA driver tarball, this directory is empty,
350 of course :)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 </para>
352 </section>
353 </chapter>
354
355
356<!-- ****************************************************** -->
357<!-- Basic Flow for PCI Drivers -->
358<!-- ****************************************************** -->
359 <chapter id="basic-flow">
360 <title>Basic Flow for PCI Drivers</title>
361
362 <section id="basic-flow-outline">
363 <title>Outline</title>
364 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200365 The minimum flow for PCI soundcards is as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 <itemizedlist>
368 <listitem><para>define the PCI ID table (see the section
369 <link linkend="pci-resource-entries"><citetitle>PCI Entries
370 </citetitle></link>).</para></listitem>
371 <listitem><para>create <function>probe()</function> callback.</para></listitem>
372 <listitem><para>create <function>remove()</function> callback.</para></listitem>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200373 <listitem><para>create a <structname>pci_driver</structname> structure
374 containing the three pointers above.</para></listitem>
375 <listitem><para>create an <function>init()</function> function just calling
376 the <function>pci_register_driver()</function> to register the pci_driver table
377 defined above.</para></listitem>
378 <listitem><para>create an <function>exit()</function> function to call
379 the <function>pci_unregister_driver()</function> function.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 </itemizedlist>
381 </para>
382 </section>
383
384 <section id="basic-flow-example">
385 <title>Full Code Example</title>
386 <para>
387 The code example is shown below. Some parts are kept
388 unimplemented at this moment but will be filled in the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200389 next sections. The numbers in the comment lines of the
390 <function>snd_mychip_probe()</function> function
391 refer to details explained in the following section.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 <example>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200394 <title>Basic Flow for PCI Drivers - Example</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 <programlisting>
396<![CDATA[
397 #include <sound/driver.h>
398 #include <linux/init.h>
399 #include <linux/pci.h>
400 #include <linux/slab.h>
401 #include <sound/core.h>
402 #include <sound/initval.h>
403
404 /* module parameters (see "Module Parameters") */
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200405 /* SNDRV_CARDS: maximum number of cards supported by this module */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
407 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
408 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
409
410 /* definition of the chip-specific record */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100411 struct mychip {
412 struct snd_card *card;
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200413 /* the rest of the implementation will be in section
414 * "PCI Resource Management"
Takashi Iwai95a5b082007-07-26 16:50:09 +0200415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 };
417
418 /* chip-specific destructor
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200419 * (see "PCI Resource Management")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100421 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 {
Takashi Iwai95a5b082007-07-26 16:50:09 +0200423 .... /* will be implemented later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 /* component-destructor
427 * (see "Management of Cards and Components")
428 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100429 static int snd_mychip_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100431 return snd_mychip_free(device->device_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /* chip-specific constructor
435 * (see "Management of Cards and Components")
436 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100437 static int __devinit snd_mychip_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct pci_dev *pci,
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100439 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100441 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100443 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .dev_free = snd_mychip_dev_free,
445 };
446
447 *rchip = NULL;
448
Takashi Iwai95a5b082007-07-26 16:50:09 +0200449 /* check PCI availability here
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200450 * (see "PCI Resource Management")
Takashi Iwai95a5b082007-07-26 16:50:09 +0200451 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 ....
453
454 /* allocate a chip-specific data with zero filled */
Takashi Iwai561b2202005-09-09 14:22:34 +0200455 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (chip == NULL)
457 return -ENOMEM;
458
459 chip->card = card;
460
Takashi Iwai95a5b082007-07-26 16:50:09 +0200461 /* rest of initialization here; will be implemented
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200462 * later, see "PCI Resource Management"
Takashi Iwai95a5b082007-07-26 16:50:09 +0200463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ....
465
Takashi Iwai95a5b082007-07-26 16:50:09 +0200466 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
467 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 snd_mychip_free(chip);
469 return err;
470 }
471
472 snd_card_set_dev(card, &pci->dev);
473
474 *rchip = chip;
475 return 0;
476 }
477
478 /* constructor -- see "Constructor" sub-section */
479 static int __devinit snd_mychip_probe(struct pci_dev *pci,
480 const struct pci_device_id *pci_id)
481 {
482 static int dev;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100483 struct snd_card *card;
484 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int err;
486
487 /* (1) */
488 if (dev >= SNDRV_CARDS)
489 return -ENODEV;
490 if (!enable[dev]) {
491 dev++;
492 return -ENOENT;
493 }
494
495 /* (2) */
496 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
497 if (card == NULL)
498 return -ENOMEM;
499
500 /* (3) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200501 err = snd_mychip_create(card, pci, &chip);
502 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 snd_card_free(card);
504 return err;
505 }
506
507 /* (4) */
508 strcpy(card->driver, "My Chip");
509 strcpy(card->shortname, "My Own Chip 123");
510 sprintf(card->longname, "%s at 0x%lx irq %i",
511 card->shortname, chip->ioport, chip->irq);
512
513 /* (5) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200514 .... /* implemented later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /* (6) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200517 err = snd_card_register(card);
518 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 snd_card_free(card);
520 return err;
521 }
522
523 /* (7) */
524 pci_set_drvdata(pci, card);
525 dev++;
526 return 0;
527 }
528
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200529 /* destructor -- see the "Destructor" sub-section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 static void __devexit snd_mychip_remove(struct pci_dev *pci)
531 {
532 snd_card_free(pci_get_drvdata(pci));
533 pci_set_drvdata(pci, NULL);
534 }
535]]>
536 </programlisting>
537 </example>
538 </para>
539 </section>
540
541 <section id="basic-flow-constructor">
542 <title>Constructor</title>
543 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200544 The real constructor of PCI drivers is the <function>probe</function> callback.
545 The <function>probe</function> callback and other component-constructors which are called
546 from the <function>probe</function> callback should be defined with
547 the <parameter>__devinit</parameter> prefix. You
548 cannot use the <parameter>__init</parameter> prefix for them,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 because any PCI device could be a hotplug device.
550 </para>
551
552 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200553 In the <function>probe</function> callback, the following scheme is often used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 </para>
555
556 <section id="basic-flow-constructor-device-index">
557 <title>1) Check and increment the device index.</title>
558 <para>
559 <informalexample>
560 <programlisting>
561<![CDATA[
562 static int dev;
563 ....
564 if (dev >= SNDRV_CARDS)
565 return -ENODEV;
566 if (!enable[dev]) {
567 dev++;
568 return -ENOENT;
569 }
570]]>
571 </programlisting>
572 </informalexample>
573
574 where enable[dev] is the module option.
575 </para>
576
577 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200578 Each time the <function>probe</function> callback is called, check the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 availability of the device. If not available, simply increment
580 the device index and returns. dev will be incremented also
581 later (<link
582 linkend="basic-flow-constructor-set-pci"><citetitle>step
583 7</citetitle></link>).
584 </para>
585 </section>
586
587 <section id="basic-flow-constructor-create-card">
588 <title>2) Create a card instance</title>
589 <para>
590 <informalexample>
591 <programlisting>
592<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100593 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ....
595 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
596]]>
597 </programlisting>
598 </informalexample>
599 </para>
600
601 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200602 The details will be explained in the section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 <link linkend="card-management-card-instance"><citetitle>
604 Management of Cards and Components</citetitle></link>.
605 </para>
606 </section>
607
608 <section id="basic-flow-constructor-create-main">
609 <title>3) Create a main component</title>
610 <para>
611 In this part, the PCI resources are allocated.
612
613 <informalexample>
614 <programlisting>
615<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100616 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +0200618 err = snd_mychip_create(card, pci, &chip);
619 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 snd_card_free(card);
621 return err;
622 }
623]]>
624 </programlisting>
625 </informalexample>
626
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200627 The details will be explained in the section <link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 linkend="pci-resource"><citetitle>PCI Resource
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200629 Management</citetitle></link>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 </para>
631 </section>
632
633 <section id="basic-flow-constructor-main-component">
634 <title>4) Set the driver ID and name strings.</title>
635 <para>
636 <informalexample>
637 <programlisting>
638<![CDATA[
639 strcpy(card->driver, "My Chip");
640 strcpy(card->shortname, "My Own Chip 123");
641 sprintf(card->longname, "%s at 0x%lx irq %i",
642 card->shortname, chip->ioport, chip->irq);
643]]>
644 </programlisting>
645 </informalexample>
646
647 The driver field holds the minimal ID string of the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200648 chip. This is used by alsa-lib's configurator, so keep it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 simple but unique.
650 Even the same driver can have different driver IDs to
651 distinguish the functionality of each chip type.
652 </para>
653
654 <para>
655 The shortname field is a string shown as more verbose
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200656 name. The longname field contains the information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 shown in <filename>/proc/asound/cards</filename>.
658 </para>
659 </section>
660
661 <section id="basic-flow-constructor-create-other">
662 <title>5) Create other components, such as mixer, MIDI, etc.</title>
663 <para>
664 Here you define the basic components such as
665 <link linkend="pcm-interface"><citetitle>PCM</citetitle></link>,
666 mixer (e.g. <link linkend="api-ac97"><citetitle>AC97</citetitle></link>),
667 MIDI (e.g. <link linkend="midi-interface"><citetitle>MPU-401</citetitle></link>),
668 and other interfaces.
669 Also, if you want a <link linkend="proc-interface"><citetitle>proc
670 file</citetitle></link>, define it here, too.
671 </para>
672 </section>
673
674 <section id="basic-flow-constructor-register-card">
675 <title>6) Register the card instance.</title>
676 <para>
677 <informalexample>
678 <programlisting>
679<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +0200680 err = snd_card_register(card);
681 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 snd_card_free(card);
683 return err;
684 }
685]]>
686 </programlisting>
687 </informalexample>
688 </para>
689
690 <para>
691 Will be explained in the section <link
692 linkend="card-management-registration"><citetitle>Management
693 of Cards and Components</citetitle></link>, too.
694 </para>
695 </section>
696
697 <section id="basic-flow-constructor-set-pci">
698 <title>7) Set the PCI driver data and return zero.</title>
699 <para>
700 <informalexample>
701 <programlisting>
702<![CDATA[
703 pci_set_drvdata(pci, card);
704 dev++;
705 return 0;
706]]>
707 </programlisting>
708 </informalexample>
709
710 In the above, the card record is stored. This pointer is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200711 used in the remove callback and power-management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 callbacks, too.
713 </para>
714 </section>
715 </section>
716
717 <section id="basic-flow-destructor">
718 <title>Destructor</title>
719 <para>
720 The destructor, remove callback, simply releases the card
721 instance. Then the ALSA middle layer will release all the
722 attached components automatically.
723 </para>
724
725 <para>
726 It would be typically like the following:
727
728 <informalexample>
729 <programlisting>
730<![CDATA[
731 static void __devexit snd_mychip_remove(struct pci_dev *pci)
732 {
733 snd_card_free(pci_get_drvdata(pci));
734 pci_set_drvdata(pci, NULL);
735 }
736]]>
737 </programlisting>
738 </informalexample>
739
740 The above code assumes that the card pointer is set to the PCI
741 driver data.
742 </para>
743 </section>
744
745 <section id="basic-flow-header-files">
746 <title>Header Files</title>
747 <para>
748 For the above example, at least the following include files
749 are necessary.
750
751 <informalexample>
752 <programlisting>
753<![CDATA[
754 #include <sound/driver.h>
755 #include <linux/init.h>
756 #include <linux/pci.h>
757 #include <linux/slab.h>
758 #include <sound/core.h>
759 #include <sound/initval.h>
760]]>
761 </programlisting>
762 </informalexample>
763
764 where the last one is necessary only when module options are
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200765 defined in the source file. If the code is split into several
766 files, the files without module options don't need them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 </para>
768
769 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200770 In addition to these headers, you'll need
771 <filename>&lt;linux/interrupt.h&gt;</filename> for interrupt
772 handling, and <filename>&lt;asm/io.h&gt;</filename> for I/O
773 access. If you use the <function>mdelay()</function> or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 <function>udelay()</function> functions, you'll need to include
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200775 <filename>&lt;linux/delay.h&gt;</filename> too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 </para>
777
778 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200779 The ALSA interfaces like the PCM and control APIs are defined in other
780 <filename>&lt;sound/xxx.h&gt;</filename> header files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 They have to be included after
782 <filename>&lt;sound/core.h&gt;</filename>.
783 </para>
784
785 </section>
786 </chapter>
787
788
789<!-- ****************************************************** -->
790<!-- Management of Cards and Components -->
791<!-- ****************************************************** -->
792 <chapter id="card-management">
793 <title>Management of Cards and Components</title>
794
795 <section id="card-management-card-instance">
796 <title>Card Instance</title>
797 <para>
798 For each soundcard, a <quote>card</quote> record must be allocated.
799 </para>
800
801 <para>
802 A card record is the headquarters of the soundcard. It manages
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200803 the whole list of devices (components) on the soundcard, such as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 PCM, mixers, MIDI, synthesizer, and so on. Also, the card
805 record holds the ID and the name strings of the card, manages
806 the root of proc files, and controls the power-management states
807 and hotplug disconnections. The component list on the card
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200808 record is used to manage the correct release of resources at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 destruction.
810 </para>
811
812 <para>
813 As mentioned above, to create a card instance, call
814 <function>snd_card_new()</function>.
815
816 <informalexample>
817 <programlisting>
818<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100819 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 card = snd_card_new(index, id, module, extra_size);
821]]>
822 </programlisting>
823 </informalexample>
824 </para>
825
826 <para>
827 The function takes four arguments, the card-index number, the
828 id string, the module pointer (usually
829 <constant>THIS_MODULE</constant>),
830 and the size of extra-data space. The last argument is used to
831 allocate card-&gt;private_data for the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200832 chip-specific data. Note that these data
833 are allocated by <function>snd_card_new()</function>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 </para>
835 </section>
836
837 <section id="card-management-component">
838 <title>Components</title>
839 <para>
840 After the card is created, you can attach the components
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200841 (devices) to the card instance. In an ALSA driver, a component is
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100842 represented as a struct <structname>snd_device</structname> object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 A component can be a PCM instance, a control interface, a raw
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200844 MIDI interface, etc. Each such instance has one component
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 entry.
846 </para>
847
848 <para>
849 A component can be created via
850 <function>snd_device_new()</function> function.
851
852 <informalexample>
853 <programlisting>
854<![CDATA[
855 snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);
856]]>
857 </programlisting>
858 </informalexample>
859 </para>
860
861 <para>
862 This takes the card pointer, the device-level
863 (<constant>SNDRV_DEV_XXX</constant>), the data pointer, and the
864 callback pointers (<parameter>&amp;ops</parameter>). The
865 device-level defines the type of components and the order of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200866 registration and de-registration. For most components, the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 device-level is already defined. For a user-defined component,
868 you can use <constant>SNDRV_DEV_LOWLEVEL</constant>.
869 </para>
870
871 <para>
872 This function itself doesn't allocate the data space. The data
873 must be allocated manually beforehand, and its pointer is passed
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200874 as the argument. This pointer is used as the
875 (<parameter>chip</parameter> identifier in the above example)
876 for the instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 </para>
878
879 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200880 Each pre-defined ALSA component such as ac97 and pcm calls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 <function>snd_device_new()</function> inside its
882 constructor. The destructor for each component is defined in the
883 callback pointers. Hence, you don't need to take care of
884 calling a destructor for such a component.
885 </para>
886
887 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200888 If you wish to create your own component, you need to
889 set the destructor function to the dev_free callback in
890 the <parameter>ops</parameter>, so that it can be released
891 automatically via <function>snd_card_free()</function>.
892 The next example will show an implementation of chip-specific
893 data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 </para>
895 </section>
896
897 <section id="card-management-chip-specific">
898 <title>Chip-Specific Data</title>
899 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200900 Chip-specific information, e.g. the I/O port address, its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 resource pointer, or the irq number, is stored in the
902 chip-specific record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 <informalexample>
905 <programlisting>
906<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100907 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ....
909 };
910]]>
911 </programlisting>
912 </informalexample>
913 </para>
914
915 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200916 In general, there are two ways of allocating the chip record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 </para>
918
919 <section id="card-management-chip-specific-snd-card-new">
920 <title>1. Allocating via <function>snd_card_new()</function>.</title>
921 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200922 As mentioned above, you can pass the extra-data-length
923 to the 4th argument of <function>snd_card_new()</function>, i.e.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 <informalexample>
926 <programlisting>
927<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100928 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct mychip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929]]>
930 </programlisting>
931 </informalexample>
932
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200933 struct <structname>mychip</structname> is the type of the chip record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 </para>
935
936 <para>
937 In return, the allocated record can be accessed as
938
939 <informalexample>
940 <programlisting>
941<![CDATA[
Takashi Iwai437a5a42006-11-21 12:14:23 +0100942 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943]]>
944 </programlisting>
945 </informalexample>
946
947 With this method, you don't have to allocate twice.
948 The record is released together with the card instance.
949 </para>
950 </section>
951
952 <section id="card-management-chip-specific-allocate-extra">
953 <title>2. Allocating an extra device.</title>
954
955 <para>
956 After allocating a card instance via
957 <function>snd_card_new()</function> (with
958 <constant>NULL</constant> on the 4th arg), call
Takashi Iwai561b2202005-09-09 14:22:34 +0200959 <function>kzalloc()</function>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 <informalexample>
962 <programlisting>
963<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100964 struct snd_card *card;
965 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 card = snd_card_new(index[dev], id[dev], THIS_MODULE, NULL);
967 .....
Takashi Iwai561b2202005-09-09 14:22:34 +0200968 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969]]>
970 </programlisting>
971 </informalexample>
972 </para>
973
974 <para>
975 The chip record should have the field to hold the card
976 pointer at least,
977
978 <informalexample>
979 <programlisting>
980<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100981 struct mychip {
982 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 ....
984 };
985]]>
986 </programlisting>
987 </informalexample>
988 </para>
989
990 <para>
991 Then, set the card pointer in the returned chip instance.
992
993 <informalexample>
994 <programlisting>
995<![CDATA[
996 chip->card = card;
997]]>
998 </programlisting>
999 </informalexample>
1000 </para>
1001
1002 <para>
1003 Next, initialize the fields, and register this chip
1004 record as a low-level device with a specified
1005 <parameter>ops</parameter>,
1006
1007 <informalexample>
1008 <programlisting>
1009<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001010 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 .dev_free = snd_mychip_dev_free,
1012 };
1013 ....
1014 snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1015]]>
1016 </programlisting>
1017 </informalexample>
1018
1019 <function>snd_mychip_dev_free()</function> is the
1020 device-destructor function, which will call the real
1021 destructor.
1022 </para>
1023
1024 <para>
1025 <informalexample>
1026 <programlisting>
1027<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001028 static int snd_mychip_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001030 return snd_mychip_free(device->device_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032]]>
1033 </programlisting>
1034 </informalexample>
1035
1036 where <function>snd_mychip_free()</function> is the real destructor.
1037 </para>
1038 </section>
1039 </section>
1040
1041 <section id="card-management-registration">
1042 <title>Registration and Release</title>
1043 <para>
1044 After all components are assigned, register the card instance
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001045 by calling <function>snd_card_register()</function>. Access
1046 to the device files is enabled at this point. That is, before
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 <function>snd_card_register()</function> is called, the
1048 components are safely inaccessible from external side. If this
1049 call fails, exit the probe function after releasing the card via
1050 <function>snd_card_free()</function>.
1051 </para>
1052
1053 <para>
1054 For releasing the card instance, you can call simply
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001055 <function>snd_card_free()</function>. As mentioned earlier, all
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 components are released automatically by this call.
1057 </para>
1058
1059 <para>
1060 As further notes, the destructors (both
1061 <function>snd_mychip_dev_free</function> and
1062 <function>snd_mychip_free</function>) cannot be defined with
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001063 the <parameter>__devexit</parameter> prefix, because they may be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 called from the constructor, too, at the false path.
1065 </para>
1066
1067 <para>
1068 For a device which allows hotplugging, you can use
Takashi Iwai2b29b132006-06-23 14:38:26 +02001069 <function>snd_card_free_when_closed</function>. This one will
1070 postpone the destruction until all devices are closed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 </para>
1072
1073 </section>
1074
1075 </chapter>
1076
1077
1078<!-- ****************************************************** -->
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001079<!-- PCI Resource Management -->
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080<!-- ****************************************************** -->
1081 <chapter id="pci-resource">
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001082 <title>PCI Resource Management</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 <section id="pci-resource-example">
1085 <title>Full Code Example</title>
1086 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001087 In this section, we'll complete the chip-specific constructor,
1088 destructor and PCI entries. Example code is shown first,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 below.
1090
1091 <example>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001092 <title>PCI Resource Management Example</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 <programlisting>
1094<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001095 struct mychip {
1096 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct pci_dev *pci;
1098
1099 unsigned long port;
1100 int irq;
1101 };
1102
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001103 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 {
1105 /* disable hardware here if any */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001106 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /* release the irq */
1109 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001110 free_irq(chip->irq, chip);
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001111 /* release the I/O ports & memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 pci_release_regions(chip->pci);
1113 /* disable the PCI entry */
1114 pci_disable_device(chip->pci);
1115 /* release the data */
1116 kfree(chip);
1117 return 0;
1118 }
1119
1120 /* chip-specific constructor */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001121 static int __devinit snd_mychip_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct pci_dev *pci,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001123 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001125 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001127 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 .dev_free = snd_mychip_dev_free,
1129 };
1130
1131 *rchip = NULL;
1132
1133 /* initialize the PCI entry */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001134 err = pci_enable_device(pci);
1135 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return err;
1137 /* check PCI availability (28bit DMA) */
Tobias Klauser56b146d2006-04-10 22:54:21 -07001138 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
1139 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 printk(KERN_ERR "error to set 28bit mask DMA\n");
1141 pci_disable_device(pci);
1142 return -ENXIO;
1143 }
1144
Takashi Iwai561b2202005-09-09 14:22:34 +02001145 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (chip == NULL) {
1147 pci_disable_device(pci);
1148 return -ENOMEM;
1149 }
1150
1151 /* initialize the stuff */
1152 chip->card = card;
1153 chip->pci = pci;
1154 chip->irq = -1;
1155
1156 /* (1) PCI resource allocation */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001157 err = pci_request_regions(pci, "My Chip");
1158 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 kfree(chip);
1160 pci_disable_device(pci);
1161 return err;
1162 }
1163 chip->port = pci_resource_start(pci, 0);
1164 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01001165 IRQF_SHARED, "My Chip", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1167 snd_mychip_free(chip);
1168 return -EBUSY;
1169 }
1170 chip->irq = pci->irq;
1171
1172 /* (2) initialization of the chip hardware */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001173 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Takashi Iwai95a5b082007-07-26 16:50:09 +02001175 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1176 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 snd_mychip_free(chip);
1178 return err;
1179 }
1180
1181 snd_card_set_dev(card, &pci->dev);
1182
1183 *rchip = chip;
1184 return 0;
1185 }
1186
1187 /* PCI IDs */
Takashi Iwaif40b6892006-07-05 16:51:05 +02001188 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1190 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1191 ....
1192 { 0, }
1193 };
1194 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1195
1196 /* pci_driver definition */
1197 static struct pci_driver driver = {
1198 .name = "My Own Chip",
1199 .id_table = snd_mychip_ids,
1200 .probe = snd_mychip_probe,
1201 .remove = __devexit_p(snd_mychip_remove),
1202 };
1203
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001204 /* module initialization */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 static int __init alsa_card_mychip_init(void)
1206 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001207 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001210 /* module clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 static void __exit alsa_card_mychip_exit(void)
1212 {
1213 pci_unregister_driver(&driver);
1214 }
1215
1216 module_init(alsa_card_mychip_init)
1217 module_exit(alsa_card_mychip_exit)
1218
1219 EXPORT_NO_SYMBOLS; /* for old kernels only */
1220]]>
1221 </programlisting>
1222 </example>
1223 </para>
1224 </section>
1225
1226 <section id="pci-resource-some-haftas">
1227 <title>Some Hafta's</title>
1228 <para>
1229 The allocation of PCI resources is done in the
1230 <function>probe()</function> function, and usually an extra
1231 <function>xxx_create()</function> function is written for this
Tobias Klauser56b146d2006-04-10 22:54:21 -07001232 purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 </para>
1234
1235 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001236 In the case of PCI devices, you first have to call
1237 the <function>pci_enable_device()</function> function before
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 allocating resources. Also, you need to set the proper PCI DMA
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001239 mask to limit the accessed I/O range. In some cases, you might
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 need to call <function>pci_set_master()</function> function,
Tobias Klauser56b146d2006-04-10 22:54:21 -07001241 too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 </para>
1243
1244 <para>
1245 Suppose the 28bit mask, and the code to be added would be like:
1246
1247 <informalexample>
1248 <programlisting>
1249<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001250 err = pci_enable_device(pci);
1251 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return err;
Tobias Klauser56b146d2006-04-10 22:54:21 -07001253 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
1254 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 printk(KERN_ERR "error to set 28bit mask DMA\n");
1256 pci_disable_device(pci);
1257 return -ENXIO;
1258 }
1259
1260]]>
1261 </programlisting>
1262 </informalexample>
1263 </para>
1264 </section>
1265
1266 <section id="pci-resource-resource-allocation">
1267 <title>Resource Allocation</title>
1268 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001269 The allocation of I/O ports and irqs is done via standard kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 functions. Unlike ALSA ver.0.5.x., there are no helpers for
1271 that. And these resources must be released in the destructor
1272 function (see below). Also, on ALSA 0.9.x, you don't need to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001273 allocate (pseudo-)DMA for PCI like in ALSA 0.5.x.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 </para>
1275
1276 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001277 Now assume that the PCI device has an I/O port with 8 bytes
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001278 and an interrupt. Then struct <structname>mychip</structname> will have the
Tobias Klauser56b146d2006-04-10 22:54:21 -07001279 following fields:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 <informalexample>
1282 <programlisting>
1283<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001284 struct mychip {
1285 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 unsigned long port;
1288 int irq;
1289 };
1290]]>
1291 </programlisting>
1292 </informalexample>
1293 </para>
1294
1295 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001296 For an I/O port (and also a memory region), you need to have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 the resource pointer for the standard resource management. For
1298 an irq, you have to keep only the irq number (integer). But you
1299 need to initialize this number as -1 before actual allocation,
1300 since irq 0 is valid. The port address and its resource pointer
1301 can be initialized as null by
Takashi Iwai561b2202005-09-09 14:22:34 +02001302 <function>kzalloc()</function> automatically, so you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 don't have to take care of resetting them.
1304 </para>
1305
1306 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001307 The allocation of an I/O port is done like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 <informalexample>
1310 <programlisting>
1311<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001312 err = pci_request_regions(pci, "My Chip");
1313 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 kfree(chip);
1315 pci_disable_device(pci);
1316 return err;
1317 }
1318 chip->port = pci_resource_start(pci, 0);
1319]]>
1320 </programlisting>
1321 </informalexample>
1322 </para>
1323
1324 <para>
1325 <!-- obsolete -->
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001326 It will reserve the I/O port region of 8 bytes of the given
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 PCI device. The returned value, chip-&gt;res_port, is allocated
1328 via <function>kmalloc()</function> by
1329 <function>request_region()</function>. The pointer must be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001330 released via <function>kfree()</function>, but there is a
1331 problem with this. This issue will be explained later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 </para>
1333
1334 <para>
1335 The allocation of an interrupt source is done like this:
1336
1337 <informalexample>
1338 <programlisting>
1339<![CDATA[
1340 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwaia1101332007-08-07 16:16:07 +02001341 IRQF_SHARED, "My Chip", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1343 snd_mychip_free(chip);
1344 return -EBUSY;
1345 }
1346 chip->irq = pci->irq;
1347]]>
1348 </programlisting>
1349 </informalexample>
1350
1351 where <function>snd_mychip_interrupt()</function> is the
1352 interrupt handler defined <link
1353 linkend="pcm-interface-interrupt-handler"><citetitle>later</citetitle></link>.
1354 Note that chip-&gt;irq should be defined
1355 only when <function>request_irq()</function> succeeded.
1356 </para>
1357
1358 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001359 On the PCI bus, interrupts can be shared. Thus,
1360 <constant>IRQF_SHARED</constant> is used as the interrupt flag of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 <function>request_irq()</function>.
1362 </para>
1363
1364 <para>
1365 The last argument of <function>request_irq()</function> is the
1366 data pointer passed to the interrupt handler. Usually, the
1367 chip-specific record is used for that, but you can use what you
1368 like, too.
1369 </para>
1370
1371 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001372 I won't give details about the interrupt handler at this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 point, but at least its appearance can be explained now. The
1374 interrupt handler looks usually like the following:
1375
1376 <informalexample>
1377 <programlisting>
1378<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01001379 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001381 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 ....
1383 return IRQ_HANDLED;
1384 }
1385]]>
1386 </programlisting>
1387 </informalexample>
1388 </para>
1389
1390 <para>
1391 Now let's write the corresponding destructor for the resources
1392 above. The role of destructor is simple: disable the hardware
1393 (if already activated) and release the resources. So far, we
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001394 have no hardware part, so the disabling code is not written here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 </para>
1396
1397 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001398 To release the resources, the <quote>check-and-release</quote>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 method is a safer way. For the interrupt, do like this:
1400
1401 <informalexample>
1402 <programlisting>
1403<![CDATA[
1404 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001405 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406]]>
1407 </programlisting>
1408 </informalexample>
1409
1410 Since the irq number can start from 0, you should initialize
1411 chip-&gt;irq with a negative value (e.g. -1), so that you can
1412 check the validity of the irq number as above.
1413 </para>
1414
1415 <para>
1416 When you requested I/O ports or memory regions via
1417 <function>pci_request_region()</function> or
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001418 <function>pci_request_regions()</function> like in this example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 release the resource(s) using the corresponding function,
1420 <function>pci_release_region()</function> or
1421 <function>pci_release_regions()</function>.
1422
1423 <informalexample>
1424 <programlisting>
1425<![CDATA[
1426 pci_release_regions(chip->pci);
1427]]>
1428 </programlisting>
1429 </informalexample>
1430 </para>
1431
1432 <para>
1433 When you requested manually via <function>request_region()</function>
1434 or <function>request_mem_region</function>, you can release it via
1435 <function>release_resource()</function>. Suppose that you keep
1436 the resource pointer returned from <function>request_region()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001437 in chip-&gt;res_port, the release procedure looks like:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 <informalexample>
1440 <programlisting>
1441<![CDATA[
Takashi Iwaib1d57762005-10-10 11:56:31 +02001442 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443]]>
1444 </programlisting>
1445 </informalexample>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 </para>
1447
1448 <para>
1449 Don't forget to call <function>pci_disable_device()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001450 before the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 </para>
1452
1453 <para>
1454 And finally, release the chip-specific record.
1455
1456 <informalexample>
1457 <programlisting>
1458<![CDATA[
1459 kfree(chip);
1460]]>
1461 </programlisting>
1462 </informalexample>
1463 </para>
1464
1465 <para>
1466 Again, remember that you cannot
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001467 use the <parameter>__devexit</parameter> prefix for this destructor.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 </para>
1469
1470 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001471 We didn't implement the hardware disabling part in the above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 If you need to do this, please note that the destructor may be
1473 called even before the initialization of the chip is completed.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001474 It would be better to have a flag to skip hardware disabling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 if the hardware was not initialized yet.
1476 </para>
1477
1478 <para>
1479 When the chip-data is assigned to the card using
1480 <function>snd_device_new()</function> with
1481 <constant>SNDRV_DEV_LOWLELVEL</constant> , its destructor is
1482 called at the last. That is, it is assured that all other
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001483 components like PCMs and controls have already been released.
1484 You don't have to stop PCMs, etc. explicitly, but just
1485 call low-level hardware stopping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 </para>
1487
1488 <para>
1489 The management of a memory-mapped region is almost as same as
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001490 the management of an I/O port. You'll need three fields like
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 the following:
1492
1493 <informalexample>
1494 <programlisting>
1495<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001496 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 ....
1498 unsigned long iobase_phys;
1499 void __iomem *iobase_virt;
1500 };
1501]]>
1502 </programlisting>
1503 </informalexample>
1504
1505 and the allocation would be like below:
1506
1507 <informalexample>
1508 <programlisting>
1509<![CDATA[
1510 if ((err = pci_request_regions(pci, "My Chip")) < 0) {
1511 kfree(chip);
1512 return err;
1513 }
1514 chip->iobase_phys = pci_resource_start(pci, 0);
1515 chip->iobase_virt = ioremap_nocache(chip->iobase_phys,
1516 pci_resource_len(pci, 0));
1517]]>
1518 </programlisting>
1519 </informalexample>
1520
1521 and the corresponding destructor would be:
1522
1523 <informalexample>
1524 <programlisting>
1525<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001526 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 {
1528 ....
1529 if (chip->iobase_virt)
1530 iounmap(chip->iobase_virt);
1531 ....
1532 pci_release_regions(chip->pci);
1533 ....
1534 }
1535]]>
1536 </programlisting>
1537 </informalexample>
1538 </para>
1539
1540 </section>
1541
1542 <section id="pci-resource-device-struct">
1543 <title>Registration of Device Struct</title>
1544 <para>
1545 At some point, typically after calling <function>snd_device_new()</function>,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001546 you need to register the struct <structname>device</structname> of the chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 you're handling for udev and co. ALSA provides a macro for compatibility with
1548 older kernels. Simply call like the following:
1549 <informalexample>
1550 <programlisting>
1551<![CDATA[
1552 snd_card_set_dev(card, &pci->dev);
1553]]>
1554 </programlisting>
1555 </informalexample>
1556 so that it stores the PCI's device pointer to the card. This will be
1557 referred by ALSA core functions later when the devices are registered.
1558 </para>
1559 <para>
1560 In the case of non-PCI, pass the proper device struct pointer of the BUS
1561 instead. (In the case of legacy ISA without PnP, you don't have to do
1562 anything.)
1563 </para>
1564 </section>
1565
1566 <section id="pci-resource-entries">
1567 <title>PCI Entries</title>
1568 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001569 So far, so good. Let's finish the missing PCI
1570 stuff. At first, we need a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 <structname>pci_device_id</structname> table for this
1572 chipset. It's a table of PCI vendor/device ID number, and some
1573 masks.
1574 </para>
1575
1576 <para>
1577 For example,
1578
1579 <informalexample>
1580 <programlisting>
1581<![CDATA[
Takashi Iwaif40b6892006-07-05 16:51:05 +02001582 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1584 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1585 ....
1586 { 0, }
1587 };
1588 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1589]]>
1590 </programlisting>
1591 </informalexample>
1592 </para>
1593
1594 <para>
1595 The first and second fields of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001596 the <structname>pci_device_id</structname> structure are the vendor and
1597 device IDs. If you have no reason to filter the matching
1598 devices, you can leave the remaining fields as above. The last
1599 field of the <structname>pci_device_id</structname> struct contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 private data for this entry. You can specify any value here, for
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001601 example, to define specific operations for supported device IDs.
1602 Such an example is found in the intel8x0 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 </para>
1604
1605 <para>
1606 The last entry of this list is the terminator. You must
1607 specify this all-zero entry.
1608 </para>
1609
1610 <para>
1611 Then, prepare the <structname>pci_driver</structname> record:
1612
1613 <informalexample>
1614 <programlisting>
1615<![CDATA[
1616 static struct pci_driver driver = {
1617 .name = "My Own Chip",
1618 .id_table = snd_mychip_ids,
1619 .probe = snd_mychip_probe,
1620 .remove = __devexit_p(snd_mychip_remove),
1621 };
1622]]>
1623 </programlisting>
1624 </informalexample>
1625 </para>
1626
1627 <para>
1628 The <structfield>probe</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001629 <structfield>remove</structfield> functions have already
1630 been defined in the previous sections.
1631 The <structfield>remove</structfield> function should
1632 be defined with the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 <function>__devexit_p()</function> macro, so that it's not
1634 defined for built-in (and non-hot-pluggable) case. The
1635 <structfield>name</structfield>
1636 field is the name string of this device. Note that you must not
1637 use a slash <quote>/</quote> in this string.
1638 </para>
1639
1640 <para>
1641 And at last, the module entries:
1642
1643 <informalexample>
1644 <programlisting>
1645<![CDATA[
1646 static int __init alsa_card_mychip_init(void)
1647 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001648 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650
1651 static void __exit alsa_card_mychip_exit(void)
1652 {
1653 pci_unregister_driver(&driver);
1654 }
1655
1656 module_init(alsa_card_mychip_init)
1657 module_exit(alsa_card_mychip_exit)
1658]]>
1659 </programlisting>
1660 </informalexample>
1661 </para>
1662
1663 <para>
1664 Note that these module entries are tagged with
1665 <parameter>__init</parameter> and
1666 <parameter>__exit</parameter> prefixes, not
1667 <parameter>__devinit</parameter> nor
1668 <parameter>__devexit</parameter>.
1669 </para>
1670
1671 <para>
1672 Oh, one thing was forgotten. If you have no exported symbols,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001673 you need to declare it in 2.2 or 2.4 kernels (it's not necessary in 2.6 kernels).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 <informalexample>
1676 <programlisting>
1677<![CDATA[
1678 EXPORT_NO_SYMBOLS;
1679]]>
1680 </programlisting>
1681 </informalexample>
1682
1683 That's all!
1684 </para>
1685 </section>
1686 </chapter>
1687
1688
1689<!-- ****************************************************** -->
1690<!-- PCM Interface -->
1691<!-- ****************************************************** -->
1692 <chapter id="pcm-interface">
1693 <title>PCM Interface</title>
1694
1695 <section id="pcm-interface-general">
1696 <title>General</title>
1697 <para>
1698 The PCM middle layer of ALSA is quite powerful and it is only
1699 necessary for each driver to implement the low-level functions
1700 to access its hardware.
1701 </para>
1702
1703 <para>
1704 For accessing to the PCM layer, you need to include
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001705 <filename>&lt;sound/pcm.h&gt;</filename> first. In addition,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 <filename>&lt;sound/pcm_params.h&gt;</filename> might be needed
1707 if you access to some functions related with hw_param.
1708 </para>
1709
1710 <para>
1711 Each card device can have up to four pcm instances. A pcm
1712 instance corresponds to a pcm device file. The limitation of
1713 number of instances comes only from the available bit size of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001714 the Linux's device numbers. Once when 64bit device number is
1715 used, we'll have more pcm instances available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 </para>
1717
1718 <para>
1719 A pcm instance consists of pcm playback and capture streams,
1720 and each pcm stream consists of one or more pcm substreams. Some
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001721 soundcards support multiple playback functions. For example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 emu10k1 has a PCM playback of 32 stereo substreams. In this case, at
1723 each open, a free substream is (usually) automatically chosen
1724 and opened. Meanwhile, when only one substream exists and it was
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001725 already opened, the successful open will either block
1726 or error with <constant>EAGAIN</constant> according to the
1727 file open mode. But you don't have to care about such details in your
1728 driver. The PCM middle layer will take care of such work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 </para>
1730 </section>
1731
1732 <section id="pcm-interface-example">
1733 <title>Full Code Example</title>
1734 <para>
1735 The example code below does not include any hardware access
1736 routines but shows only the skeleton, how to build up the PCM
1737 interfaces.
1738
1739 <example>
1740 <title>PCM Example Code</title>
1741 <programlisting>
1742<![CDATA[
1743 #include <sound/pcm.h>
1744 ....
1745
1746 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001747 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 .info = (SNDRV_PCM_INFO_MMAP |
1749 SNDRV_PCM_INFO_INTERLEAVED |
1750 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1751 SNDRV_PCM_INFO_MMAP_VALID),
1752 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1753 .rates = SNDRV_PCM_RATE_8000_48000,
1754 .rate_min = 8000,
1755 .rate_max = 48000,
1756 .channels_min = 2,
1757 .channels_max = 2,
1758 .buffer_bytes_max = 32768,
1759 .period_bytes_min = 4096,
1760 .period_bytes_max = 32768,
1761 .periods_min = 1,
1762 .periods_max = 1024,
1763 };
1764
1765 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001766 static struct snd_pcm_hardware snd_mychip_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 .info = (SNDRV_PCM_INFO_MMAP |
1768 SNDRV_PCM_INFO_INTERLEAVED |
1769 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1770 SNDRV_PCM_INFO_MMAP_VALID),
1771 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1772 .rates = SNDRV_PCM_RATE_8000_48000,
1773 .rate_min = 8000,
1774 .rate_max = 48000,
1775 .channels_min = 2,
1776 .channels_max = 2,
1777 .buffer_bytes_max = 32768,
1778 .period_bytes_min = 4096,
1779 .period_bytes_max = 32768,
1780 .periods_min = 1,
1781 .periods_max = 1024,
1782 };
1783
1784 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001785 static int snd_mychip_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001787 struct mychip *chip = snd_pcm_substream_chip(substream);
1788 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 runtime->hw = snd_mychip_playback_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001791 /* more hardware-initialization will be done here */
1792 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return 0;
1794 }
1795
1796 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001797 static int snd_mychip_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001799 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001800 /* the hardware-specific codes will be here */
1801 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
1803
1804 }
1805
1806 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001807 static int snd_mychip_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001809 struct mychip *chip = snd_pcm_substream_chip(substream);
1810 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 runtime->hw = snd_mychip_capture_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001813 /* more hardware-initialization will be done here */
1814 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return 0;
1816 }
1817
1818 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001819 static int snd_mychip_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001821 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001822 /* the hardware-specific codes will be here */
1823 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return 0;
1825
1826 }
1827
1828 /* hw_params callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001829 static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
1830 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 {
1832 return snd_pcm_lib_malloc_pages(substream,
1833 params_buffer_bytes(hw_params));
1834 }
1835
1836 /* hw_free callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001837 static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 {
1839 return snd_pcm_lib_free_pages(substream);
1840 }
1841
1842 /* prepare callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001843 static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001845 struct mychip *chip = snd_pcm_substream_chip(substream);
1846 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 /* set up the hardware with the current configuration
1849 * for example...
1850 */
1851 mychip_set_sample_format(chip, runtime->format);
1852 mychip_set_sample_rate(chip, runtime->rate);
1853 mychip_set_channels(chip, runtime->channels);
Takashi Iwai0b7bed42006-03-02 15:35:55 +01001854 mychip_set_dma_setup(chip, runtime->dma_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 chip->buffer_size,
1856 chip->period_size);
1857 return 0;
1858 }
1859
1860 /* trigger callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001861 static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int cmd)
1863 {
1864 switch (cmd) {
1865 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001866 /* do something to start the PCM engine */
1867 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001870 /* do something to stop the PCM engine */
1871 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
1873 default:
1874 return -EINVAL;
1875 }
1876 }
1877
1878 /* pointer callback */
1879 static snd_pcm_uframes_t
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001880 snd_mychip_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001882 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 unsigned int current_ptr;
1884
1885 /* get the current hardware pointer */
1886 current_ptr = mychip_get_hw_pointer(chip);
1887 return current_ptr;
1888 }
1889
1890 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001891 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .open = snd_mychip_playback_open,
1893 .close = snd_mychip_playback_close,
1894 .ioctl = snd_pcm_lib_ioctl,
1895 .hw_params = snd_mychip_pcm_hw_params,
1896 .hw_free = snd_mychip_pcm_hw_free,
1897 .prepare = snd_mychip_pcm_prepare,
1898 .trigger = snd_mychip_pcm_trigger,
1899 .pointer = snd_mychip_pcm_pointer,
1900 };
1901
1902 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001903 static struct snd_pcm_ops snd_mychip_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .open = snd_mychip_capture_open,
1905 .close = snd_mychip_capture_close,
1906 .ioctl = snd_pcm_lib_ioctl,
1907 .hw_params = snd_mychip_pcm_hw_params,
1908 .hw_free = snd_mychip_pcm_hw_free,
1909 .prepare = snd_mychip_pcm_prepare,
1910 .trigger = snd_mychip_pcm_trigger,
1911 .pointer = snd_mychip_pcm_pointer,
1912 };
1913
1914 /*
1915 * definitions of capture are omitted here...
1916 */
1917
1918 /* create a pcm device */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001919 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001921 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 int err;
1923
Takashi Iwai95a5b082007-07-26 16:50:09 +02001924 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1925 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return err;
1927 pcm->private_data = chip;
1928 strcpy(pcm->name, "My Chip");
1929 chip->pcm = pcm;
1930 /* set operators */
1931 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1932 &snd_mychip_playback_ops);
1933 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1934 &snd_mychip_capture_ops);
1935 /* pre-allocation of buffers */
1936 /* NOTE: this may fail */
1937 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1938 snd_dma_pci_data(chip->pci),
1939 64*1024, 64*1024);
1940 return 0;
1941 }
1942]]>
1943 </programlisting>
1944 </example>
1945 </para>
1946 </section>
1947
1948 <section id="pcm-interface-constructor">
1949 <title>Constructor</title>
1950 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001951 A pcm instance is allocated by the <function>snd_pcm_new()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 function. It would be better to create a constructor for pcm,
1953 namely,
1954
1955 <informalexample>
1956 <programlisting>
1957<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001958 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001960 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 int err;
1962
Takashi Iwai95a5b082007-07-26 16:50:09 +02001963 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1964 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return err;
1966 pcm->private_data = chip;
1967 strcpy(pcm->name, "My Chip");
1968 chip->pcm = pcm;
1969 ....
1970 return 0;
1971 }
1972]]>
1973 </programlisting>
1974 </informalexample>
1975 </para>
1976
1977 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001978 The <function>snd_pcm_new()</function> function takes four
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 arguments. The first argument is the card pointer to which this
1980 pcm is assigned, and the second is the ID string.
1981 </para>
1982
1983 <para>
1984 The third argument (<parameter>index</parameter>, 0 in the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001985 above) is the index of this new pcm. It begins from zero. If
1986 you create more than one pcm instances, specify the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 different numbers in this argument. For example,
1988 <parameter>index</parameter> = 1 for the second PCM device.
1989 </para>
1990
1991 <para>
1992 The fourth and fifth arguments are the number of substreams
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001993 for playback and capture, respectively. Here 1 is used for
1994 both arguments. When no playback or capture substreams are available,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 pass 0 to the corresponding argument.
1996 </para>
1997
1998 <para>
1999 If a chip supports multiple playbacks or captures, you can
2000 specify more numbers, but they must be handled properly in
2001 open/close, etc. callbacks. When you need to know which
2002 substream you are referring to, then it can be obtained from
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002003 struct <structname>snd_pcm_substream</structname> data passed to each callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 as follows:
2005
2006 <informalexample>
2007 <programlisting>
2008<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002009 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 int index = substream->number;
2011]]>
2012 </programlisting>
2013 </informalexample>
2014 </para>
2015
2016 <para>
2017 After the pcm is created, you need to set operators for each
2018 pcm stream.
2019
2020 <informalexample>
2021 <programlisting>
2022<![CDATA[
2023 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2024 &snd_mychip_playback_ops);
2025 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2026 &snd_mychip_capture_ops);
2027]]>
2028 </programlisting>
2029 </informalexample>
2030 </para>
2031
2032 <para>
2033 The operators are defined typically like this:
2034
2035 <informalexample>
2036 <programlisting>
2037<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002038 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 .open = snd_mychip_pcm_open,
2040 .close = snd_mychip_pcm_close,
2041 .ioctl = snd_pcm_lib_ioctl,
2042 .hw_params = snd_mychip_pcm_hw_params,
2043 .hw_free = snd_mychip_pcm_hw_free,
2044 .prepare = snd_mychip_pcm_prepare,
2045 .trigger = snd_mychip_pcm_trigger,
2046 .pointer = snd_mychip_pcm_pointer,
2047 };
2048]]>
2049 </programlisting>
2050 </informalexample>
2051
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002052 All the callbacks are described in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 <link linkend="pcm-interface-operators"><citetitle>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002054 Operators</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 </para>
2056
2057 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002058 After setting the operators, you probably will want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 pre-allocate the buffer. For the pre-allocation, simply call
2060 the following:
2061
2062 <informalexample>
2063 <programlisting>
2064<![CDATA[
2065 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2066 snd_dma_pci_data(chip->pci),
2067 64*1024, 64*1024);
2068]]>
2069 </programlisting>
2070 </informalexample>
2071
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002072 It will allocate a buffer up to 64kB as default.
2073 Buffer management details will be described in the later section <link
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 linkend="buffer-and-memory"><citetitle>Buffer and Memory
2075 Management</citetitle></link>.
2076 </para>
2077
2078 <para>
2079 Additionally, you can set some extra information for this pcm
2080 in pcm-&gt;info_flags.
2081 The available values are defined as
2082 <constant>SNDRV_PCM_INFO_XXX</constant> in
2083 <filename>&lt;sound/asound.h&gt;</filename>, which is used for
2084 the hardware definition (described later). When your soundchip
2085 supports only half-duplex, specify like this:
2086
2087 <informalexample>
2088 <programlisting>
2089<![CDATA[
2090 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
2091]]>
2092 </programlisting>
2093 </informalexample>
2094 </para>
2095 </section>
2096
2097 <section id="pcm-interface-destructor">
2098 <title>... And the Destructor?</title>
2099 <para>
2100 The destructor for a pcm instance is not always
2101 necessary. Since the pcm device will be released by the middle
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002102 layer code automatically, you don't have to call the destructor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 explicitly.
2104 </para>
2105
2106 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002107 The destructor would be necessary if you created
2108 special records internally and needed to release them. In such a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 case, set the destructor function to
2110 pcm-&gt;private_free:
2111
2112 <example>
2113 <title>PCM Instance with a Destructor</title>
2114 <programlisting>
2115<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002116 static void mychip_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002118 struct mychip *chip = snd_pcm_chip(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 /* free your own data */
2120 kfree(chip->my_private_pcm_data);
Takashi Iwai95a5b082007-07-26 16:50:09 +02002121 /* do what you like else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 ....
2123 }
2124
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002125 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002127 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 ....
2129 /* allocate your own data */
2130 chip->my_private_pcm_data = kmalloc(...);
2131 /* set the destructor */
2132 pcm->private_data = chip;
2133 pcm->private_free = mychip_pcm_free;
2134 ....
2135 }
2136]]>
2137 </programlisting>
2138 </example>
2139 </para>
2140 </section>
2141
2142 <section id="pcm-interface-runtime">
2143 <title>Runtime Pointer - The Chest of PCM Information</title>
2144 <para>
2145 When the PCM substream is opened, a PCM runtime instance is
2146 allocated and assigned to the substream. This pointer is
2147 accessible via <constant>substream-&gt;runtime</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002148 This runtime pointer holds most information you need
2149 to control the PCM: the copy of hw_params and sw_params configurations, the buffer
2150 pointers, mmap records, spinlocks, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 </para>
2152
2153 <para>
2154 The definition of runtime instance is found in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002155 <filename>&lt;sound/pcm.h&gt;</filename>. Here are
2156 the contents of this file:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 <informalexample>
2158 <programlisting>
2159<![CDATA[
2160struct _snd_pcm_runtime {
2161 /* -- Status -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002162 struct snd_pcm_substream *trigger_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 snd_timestamp_t trigger_tstamp; /* trigger timestamp */
2164 int overrange;
2165 snd_pcm_uframes_t avail_max;
2166 snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
2167 snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/
2168
2169 /* -- HW params -- */
2170 snd_pcm_access_t access; /* access mode */
2171 snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
2172 snd_pcm_subformat_t subformat; /* subformat */
2173 unsigned int rate; /* rate in Hz */
2174 unsigned int channels; /* channels */
2175 snd_pcm_uframes_t period_size; /* period size */
2176 unsigned int periods; /* periods */
2177 snd_pcm_uframes_t buffer_size; /* buffer size */
2178 unsigned int tick_time; /* tick time */
2179 snd_pcm_uframes_t min_align; /* Min alignment for the format */
2180 size_t byte_align;
2181 unsigned int frame_bits;
2182 unsigned int sample_bits;
2183 unsigned int info;
2184 unsigned int rate_num;
2185 unsigned int rate_den;
2186
2187 /* -- SW params -- */
Takashi Iwai07799e72005-10-10 11:49:49 +02002188 struct timespec tstamp_mode; /* mmap timestamp is updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 unsigned int period_step;
2190 unsigned int sleep_min; /* min ticks to sleep */
2191 snd_pcm_uframes_t xfer_align; /* xfer size need to be a multiple */
2192 snd_pcm_uframes_t start_threshold;
2193 snd_pcm_uframes_t stop_threshold;
2194 snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
2195 noise is nearest than this */
2196 snd_pcm_uframes_t silence_size; /* Silence filling size */
2197 snd_pcm_uframes_t boundary; /* pointers wrap point */
2198
2199 snd_pcm_uframes_t silenced_start;
2200 snd_pcm_uframes_t silenced_size;
2201
2202 snd_pcm_sync_id_t sync; /* hardware synchronization ID */
2203
2204 /* -- mmap -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002205 volatile struct snd_pcm_mmap_status *status;
2206 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 atomic_t mmap_count;
2208
2209 /* -- locking / scheduling -- */
2210 spinlock_t lock;
2211 wait_queue_head_t sleep;
2212 struct timer_list tick_timer;
2213 struct fasync_struct *fasync;
2214
2215 /* -- private section -- */
2216 void *private_data;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002217 void (*private_free)(struct snd_pcm_runtime *runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* -- hardware description -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002220 struct snd_pcm_hardware hw;
2221 struct snd_pcm_hw_constraints hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 /* -- interrupt callbacks -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002224 void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
2225 void (*transfer_ack_end)(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /* -- timer -- */
2228 unsigned int timer_resolution; /* timer resolution */
2229
2230 /* -- DMA -- */
2231 unsigned char *dma_area; /* DMA area */
2232 dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
2233 size_t dma_bytes; /* size of DMA area */
2234
2235 struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
2236
2237#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2238 /* -- OSS things -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002239 struct snd_pcm_oss_runtime oss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240#endif
2241};
2242]]>
2243 </programlisting>
2244 </informalexample>
2245 </para>
2246
2247 <para>
2248 For the operators (callbacks) of each sound driver, most of
2249 these records are supposed to be read-only. Only the PCM
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002250 middle-layer changes / updates them. The exceptions are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 the hardware description (hw), interrupt callbacks
2252 (transfer_ack_xxx), DMA buffer information, and the private
2253 data. Besides, if you use the standard buffer allocation
2254 method via <function>snd_pcm_lib_malloc_pages()</function>,
2255 you don't need to set the DMA buffer information by yourself.
2256 </para>
2257
2258 <para>
2259 In the sections below, important records are explained.
2260 </para>
2261
2262 <section id="pcm-interface-runtime-hw">
2263 <title>Hardware Description</title>
2264 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002265 The hardware descriptor (struct <structname>snd_pcm_hardware</structname>)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 contains the definitions of the fundamental hardware
2267 configuration. Above all, you'll need to define this in
2268 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2269 the open callback</citetitle></link>.
2270 Note that the runtime instance holds the copy of the
2271 descriptor, not the pointer to the existing descriptor. That
2272 is, in the open callback, you can modify the copied descriptor
2273 (<constant>runtime-&gt;hw</constant>) as you need. For example, if the maximum
2274 number of channels is 1 only on some chip models, you can
2275 still use the same hardware descriptor and change the
2276 channels_max later:
2277 <informalexample>
2278 <programlisting>
2279<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002280 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 ...
2282 runtime->hw = snd_mychip_playback_hw; /* common definition */
2283 if (chip->model == VERY_OLD_ONE)
2284 runtime->hw.channels_max = 1;
2285]]>
2286 </programlisting>
2287 </informalexample>
2288 </para>
2289
2290 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002291 Typically, you'll have a hardware descriptor as below:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 <informalexample>
2293 <programlisting>
2294<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002295 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 .info = (SNDRV_PCM_INFO_MMAP |
2297 SNDRV_PCM_INFO_INTERLEAVED |
2298 SNDRV_PCM_INFO_BLOCK_TRANSFER |
2299 SNDRV_PCM_INFO_MMAP_VALID),
2300 .formats = SNDRV_PCM_FMTBIT_S16_LE,
2301 .rates = SNDRV_PCM_RATE_8000_48000,
2302 .rate_min = 8000,
2303 .rate_max = 48000,
2304 .channels_min = 2,
2305 .channels_max = 2,
2306 .buffer_bytes_max = 32768,
2307 .period_bytes_min = 4096,
2308 .period_bytes_max = 32768,
2309 .periods_min = 1,
2310 .periods_max = 1024,
2311 };
2312]]>
2313 </programlisting>
2314 </informalexample>
2315 </para>
2316
2317 <para>
2318 <itemizedlist>
2319 <listitem><para>
2320 The <structfield>info</structfield> field contains the type and
2321 capabilities of this pcm. The bit flags are defined in
2322 <filename>&lt;sound/asound.h&gt;</filename> as
2323 <constant>SNDRV_PCM_INFO_XXX</constant>. Here, at least, you
2324 have to specify whether the mmap is supported and which
2325 interleaved format is supported.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002326 When the is supported, add the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 <constant>SNDRV_PCM_INFO_MMAP</constant> flag here. When the
2328 hardware supports the interleaved or the non-interleaved
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002329 formats, <constant>SNDRV_PCM_INFO_INTERLEAVED</constant> or
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 <constant>SNDRV_PCM_INFO_NONINTERLEAVED</constant> flag must
2331 be set, respectively. If both are supported, you can set both,
2332 too.
2333 </para>
2334
2335 <para>
2336 In the above example, <constant>MMAP_VALID</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002337 <constant>BLOCK_TRANSFER</constant> are specified for the OSS mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mode. Usually both are set. Of course,
2339 <constant>MMAP_VALID</constant> is set only if the mmap is
2340 really supported.
2341 </para>
2342
2343 <para>
2344 The other possible flags are
2345 <constant>SNDRV_PCM_INFO_PAUSE</constant> and
2346 <constant>SNDRV_PCM_INFO_RESUME</constant>. The
2347 <constant>PAUSE</constant> bit means that the pcm supports the
2348 <quote>pause</quote> operation, while the
2349 <constant>RESUME</constant> bit means that the pcm supports
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002350 the full <quote>suspend/resume</quote> operation.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002351 If the <constant>PAUSE</constant> flag is set,
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002352 the <structfield>trigger</structfield> callback below
2353 must handle the corresponding (pause push/release) commands.
2354 The suspend/resume trigger commands can be defined even without
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002355 the <constant>RESUME</constant> flag. See <link
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002356 linkend="power-management"><citetitle>
2357 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 </para>
2359
2360 <para>
2361 When the PCM substreams can be synchronized (typically,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01002362 synchronized start/stop of a playback and a capture streams),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 you can give <constant>SNDRV_PCM_INFO_SYNC_START</constant>,
2364 too. In this case, you'll need to check the linked-list of
2365 PCM substreams in the trigger callback. This will be
2366 described in the later section.
2367 </para>
2368 </listitem>
2369
2370 <listitem>
2371 <para>
2372 <structfield>formats</structfield> field contains the bit-flags
2373 of supported formats (<constant>SNDRV_PCM_FMTBIT_XXX</constant>).
2374 If the hardware supports more than one format, give all or'ed
2375 bits. In the example above, the signed 16bit little-endian
2376 format is specified.
2377 </para>
2378 </listitem>
2379
2380 <listitem>
2381 <para>
2382 <structfield>rates</structfield> field contains the bit-flags of
2383 supported rates (<constant>SNDRV_PCM_RATE_XXX</constant>).
2384 When the chip supports continuous rates, pass
2385 <constant>CONTINUOUS</constant> bit additionally.
2386 The pre-defined rate bits are provided only for typical
2387 rates. If your chip supports unconventional rates, you need to add
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002388 the <constant>KNOT</constant> bit and set up the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 constraint manually (explained later).
2390 </para>
2391 </listitem>
2392
2393 <listitem>
2394 <para>
2395 <structfield>rate_min</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002396 <structfield>rate_max</structfield> define the minimum and
2397 maximum sample rate. This should correspond somehow to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 <structfield>rates</structfield> bits.
2399 </para>
2400 </listitem>
2401
2402 <listitem>
2403 <para>
2404 <structfield>channel_min</structfield> and
2405 <structfield>channel_max</structfield>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002406 define, as you might already expected, the minimum and maximum
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 number of channels.
2408 </para>
2409 </listitem>
2410
2411 <listitem>
2412 <para>
2413 <structfield>buffer_bytes_max</structfield> defines the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002414 maximum buffer size in bytes. There is no
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 <structfield>buffer_bytes_min</structfield> field, since
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002416 it can be calculated from the minimum period size and the
2417 minimum number of periods.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 Meanwhile, <structfield>period_bytes_min</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002419 define the minimum and maximum size of the period in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 <structfield>periods_max</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002421 <structfield>periods_min</structfield> define the maximum and
2422 minimum number of periods in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 </para>
2424
2425 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002426 The <quote>period</quote> is a term that corresponds to
2427 a fragment in the OSS world. The period defines the size at
2428 which a PCM interrupt is generated. This size strongly
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 depends on the hardware.
2430 Generally, the smaller period size will give you more
2431 interrupts, that is, more controls.
2432 In the case of capture, this size defines the input latency.
2433 On the other hand, the whole buffer size defines the
2434 output latency for the playback direction.
2435 </para>
2436 </listitem>
2437
2438 <listitem>
2439 <para>
2440 There is also a field <structfield>fifo_size</structfield>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002441 This specifies the size of the hardware FIFO, but currently it
2442 is neither used in the driver nor in the alsa-lib. So, you
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 can ignore this field.
2444 </para>
2445 </listitem>
2446 </itemizedlist>
2447 </para>
2448 </section>
2449
2450 <section id="pcm-interface-runtime-config">
2451 <title>PCM Configurations</title>
2452 <para>
2453 Ok, let's go back again to the PCM runtime records.
2454 The most frequently referred records in the runtime instance are
2455 the PCM configurations.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002456 The PCM configurations are stored in the runtime instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 after the application sends <type>hw_params</type> data via
2458 alsa-lib. There are many fields copied from hw_params and
2459 sw_params structs. For example,
2460 <structfield>format</structfield> holds the format type
2461 chosen by the application. This field contains the enum value
2462 <constant>SNDRV_PCM_FORMAT_XXX</constant>.
2463 </para>
2464
2465 <para>
2466 One thing to be noted is that the configured buffer and period
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002467 sizes are stored in <quote>frames</quote> in the runtime.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 In the ALSA world, 1 frame = channels * samples-size.
2469 For conversion between frames and bytes, you can use the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002470 <function>frames_to_bytes()</function> and
2471 <function>bytes_to_frames()</function> helper functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 <informalexample>
2473 <programlisting>
2474<![CDATA[
2475 period_bytes = frames_to_bytes(runtime, runtime->period_size);
2476]]>
2477 </programlisting>
2478 </informalexample>
2479 </para>
2480
2481 <para>
2482 Also, many software parameters (sw_params) are
2483 stored in frames, too. Please check the type of the field.
2484 <type>snd_pcm_uframes_t</type> is for the frames as unsigned
2485 integer while <type>snd_pcm_sframes_t</type> is for the frames
2486 as signed integer.
2487 </para>
2488 </section>
2489
2490 <section id="pcm-interface-runtime-dma">
2491 <title>DMA Buffer Information</title>
2492 <para>
2493 The DMA buffer is defined by the following four fields,
2494 <structfield>dma_area</structfield>,
2495 <structfield>dma_addr</structfield>,
2496 <structfield>dma_bytes</structfield> and
2497 <structfield>dma_private</structfield>.
2498 The <structfield>dma_area</structfield> holds the buffer
2499 pointer (the logical address). You can call
2500 <function>memcpy</function> from/to
2501 this pointer. Meanwhile, <structfield>dma_addr</structfield>
2502 holds the physical address of the buffer. This field is
2503 specified only when the buffer is a linear buffer.
2504 <structfield>dma_bytes</structfield> holds the size of buffer
2505 in bytes. <structfield>dma_private</structfield> is used for
2506 the ALSA DMA allocator.
2507 </para>
2508
2509 <para>
2510 If you use a standard ALSA function,
2511 <function>snd_pcm_lib_malloc_pages()</function>, for
2512 allocating the buffer, these fields are set by the ALSA middle
2513 layer, and you should <emphasis>not</emphasis> change them by
2514 yourself. You can read them but not write them.
2515 On the other hand, if you want to allocate the buffer by
2516 yourself, you'll need to manage it in hw_params callback.
2517 At least, <structfield>dma_bytes</structfield> is mandatory.
2518 <structfield>dma_area</structfield> is necessary when the
2519 buffer is mmapped. If your driver doesn't support mmap, this
2520 field is not necessary. <structfield>dma_addr</structfield>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002521 is also optional. You can use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 <structfield>dma_private</structfield> as you like, too.
2523 </para>
2524 </section>
2525
2526 <section id="pcm-interface-runtime-status">
2527 <title>Running Status</title>
2528 <para>
2529 The running status can be referred via <constant>runtime-&gt;status</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002530 This is the pointer to the struct <structname>snd_pcm_mmap_status</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 record. For example, you can get the current DMA hardware
2532 pointer via <constant>runtime-&gt;status-&gt;hw_ptr</constant>.
2533 </para>
2534
2535 <para>
2536 The DMA application pointer can be referred via
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002537 <constant>runtime-&gt;control</constant>, which points to the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002538 struct <structname>snd_pcm_mmap_control</structname> record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 However, accessing directly to this value is not recommended.
2540 </para>
2541 </section>
2542
2543 <section id="pcm-interface-runtime-private">
2544 <title>Private Data</title>
2545 <para>
2546 You can allocate a record for the substream and store it in
2547 <constant>runtime-&gt;private_data</constant>. Usually, this
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002548 is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2550 the open callback</citetitle></link>.
2551 Don't mix this with <constant>pcm-&gt;private_data</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002552 The <constant>pcm-&gt;private_data</constant> usually points to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 chip instance assigned statically at the creation of PCM, while the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002554 <constant>runtime-&gt;private_data</constant> points to a dynamic
2555 data structure created at the PCM open callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 <informalexample>
2558 <programlisting>
2559<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002560 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002562 struct my_pcm_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 ....
2564 data = kmalloc(sizeof(*data), GFP_KERNEL);
2565 substream->runtime->private_data = data;
2566 ....
2567 }
2568]]>
2569 </programlisting>
2570 </informalexample>
2571 </para>
2572
2573 <para>
2574 The allocated object must be released in
2575 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2576 the close callback</citetitle></link>.
2577 </para>
2578 </section>
2579
2580 <section id="pcm-interface-runtime-intr">
2581 <title>Interrupt Callbacks</title>
2582 <para>
2583 The field <structfield>transfer_ack_begin</structfield> and
2584 <structfield>transfer_ack_end</structfield> are called at
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002585 the beginning and at the end of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 <function>snd_pcm_period_elapsed()</function>, respectively.
2587 </para>
2588 </section>
2589
2590 </section>
2591
2592 <section id="pcm-interface-operators">
2593 <title>Operators</title>
2594 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002595 OK, now let me give details about each pcm callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 (<parameter>ops</parameter>). In general, every callback must
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002597 return 0 if successful, or a negative error number
2598 such as <constant>-EINVAL</constant>. To choose an appropriate
2599 error number, it is advised to check what value other parts of
2600 the kernel return when the same kind of request fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 </para>
2602
2603 <para>
2604 The callback function takes at least the argument with
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002605 <structname>snd_pcm_substream</structname> pointer. To retrieve
2606 the chip record from the given substream instance, you can use the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 following macro.
2608
2609 <informalexample>
2610 <programlisting>
2611<![CDATA[
2612 int xxx() {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002613 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 ....
2615 }
2616]]>
2617 </programlisting>
2618 </informalexample>
2619
2620 The macro reads <constant>substream-&gt;private_data</constant>,
2621 which is a copy of <constant>pcm-&gt;private_data</constant>.
2622 You can override the former if you need to assign different data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002623 records per PCM substream. For example, the cmi8330 driver assigns
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 different private_data for playback and capture directions,
2625 because it uses two different codecs (SB- and AD-compatible) for
2626 different directions.
2627 </para>
2628
2629 <section id="pcm-interface-operators-open-callback">
2630 <title>open callback</title>
2631 <para>
2632 <informalexample>
2633 <programlisting>
2634<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002635 static int snd_xxx_open(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636]]>
2637 </programlisting>
2638 </informalexample>
2639
2640 This is called when a pcm substream is opened.
2641 </para>
2642
2643 <para>
2644 At least, here you have to initialize the runtime-&gt;hw
2645 record. Typically, this is done by like this:
2646
2647 <informalexample>
2648 <programlisting>
2649<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002650 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002652 struct mychip *chip = snd_pcm_substream_chip(substream);
2653 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 runtime->hw = snd_mychip_playback_hw;
2656 return 0;
2657 }
2658]]>
2659 </programlisting>
2660 </informalexample>
2661
2662 where <parameter>snd_mychip_playback_hw</parameter> is the
2663 pre-defined hardware description.
2664 </para>
2665
2666 <para>
2667 You can allocate a private data in this callback, as described
2668 in <link linkend="pcm-interface-runtime-private"><citetitle>
2669 Private Data</citetitle></link> section.
2670 </para>
2671
2672 <para>
2673 If the hardware configuration needs more constraints, set the
2674 hardware constraints here, too.
2675 See <link linkend="pcm-interface-constraints"><citetitle>
2676 Constraints</citetitle></link> for more details.
2677 </para>
2678 </section>
2679
2680 <section id="pcm-interface-operators-close-callback">
2681 <title>close callback</title>
2682 <para>
2683 <informalexample>
2684 <programlisting>
2685<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002686 static int snd_xxx_close(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687]]>
2688 </programlisting>
2689 </informalexample>
2690
2691 Obviously, this is called when a pcm substream is closed.
2692 </para>
2693
2694 <para>
2695 Any private instance for a pcm substream allocated in the
2696 open callback will be released here.
2697
2698 <informalexample>
2699 <programlisting>
2700<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002701 static int snd_xxx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 {
2703 ....
2704 kfree(substream->runtime->private_data);
2705 ....
2706 }
2707]]>
2708 </programlisting>
2709 </informalexample>
2710 </para>
2711 </section>
2712
2713 <section id="pcm-interface-operators-ioctl-callback">
2714 <title>ioctl callback</title>
2715 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002716 This is used for any special call to pcm ioctls. But
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 usually you can pass a generic ioctl callback,
2718 <function>snd_pcm_lib_ioctl</function>.
2719 </para>
2720 </section>
2721
2722 <section id="pcm-interface-operators-hw-params-callback">
2723 <title>hw_params callback</title>
2724 <para>
2725 <informalexample>
2726 <programlisting>
2727<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002728 static int snd_xxx_hw_params(struct snd_pcm_substream *substream,
2729 struct snd_pcm_hw_params *hw_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730]]>
2731 </programlisting>
2732 </informalexample>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 </para>
2734
2735 <para>
2736 This is called when the hardware parameter
2737 (<structfield>hw_params</structfield>) is set
2738 up by the application,
2739 that is, once when the buffer size, the period size, the
2740 format, etc. are defined for the pcm substream.
2741 </para>
2742
2743 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002744 Many hardware setups should be done in this callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 including the allocation of buffers.
2746 </para>
2747
2748 <para>
2749 Parameters to be initialized are retrieved by
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002750 <function>params_xxx()</function> macros. To allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 buffer, you can call a helper function,
2752
2753 <informalexample>
2754 <programlisting>
2755<![CDATA[
2756 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
2757]]>
2758 </programlisting>
2759 </informalexample>
2760
2761 <function>snd_pcm_lib_malloc_pages()</function> is available
2762 only when the DMA buffers have been pre-allocated.
2763 See the section <link
2764 linkend="buffer-and-memory-buffer-types"><citetitle>
2765 Buffer Types</citetitle></link> for more details.
2766 </para>
2767
2768 <para>
2769 Note that this and <structfield>prepare</structfield> callbacks
2770 may be called multiple times per initialization.
2771 For example, the OSS emulation may
2772 call these callbacks at each change via its ioctl.
2773 </para>
2774
2775 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002776 Thus, you need to be careful not to allocate the same buffers
2777 many times, which will lead to memory leaks! Calling the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 helper function above many times is OK. It will release the
2779 previous buffer automatically when it was already allocated.
2780 </para>
2781
2782 <para>
2783 Another note is that this callback is non-atomic
2784 (schedulable). This is important, because the
2785 <structfield>trigger</structfield> callback
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002786 is atomic (non-schedulable). That is, mutexes or any
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 schedule-related functions are not available in
2788 <structfield>trigger</structfield> callback.
2789 Please see the subsection
2790 <link linkend="pcm-interface-atomicity"><citetitle>
2791 Atomicity</citetitle></link> for details.
2792 </para>
2793 </section>
2794
2795 <section id="pcm-interface-operators-hw-free-callback">
2796 <title>hw_free callback</title>
2797 <para>
2798 <informalexample>
2799 <programlisting>
2800<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002801 static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802]]>
2803 </programlisting>
2804 </informalexample>
2805 </para>
2806
2807 <para>
2808 This is called to release the resources allocated via
2809 <structfield>hw_params</structfield>. For example, releasing the
2810 buffer via
2811 <function>snd_pcm_lib_malloc_pages()</function> is done by
2812 calling the following:
2813
2814 <informalexample>
2815 <programlisting>
2816<![CDATA[
2817 snd_pcm_lib_free_pages(substream);
2818]]>
2819 </programlisting>
2820 </informalexample>
2821 </para>
2822
2823 <para>
2824 This function is always called before the close callback is called.
2825 Also, the callback may be called multiple times, too.
2826 Keep track whether the resource was already released.
2827 </para>
2828 </section>
2829
2830 <section id="pcm-interface-operators-prepare-callback">
2831 <title>prepare callback</title>
2832 <para>
2833 <informalexample>
2834 <programlisting>
2835<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002836 static int snd_xxx_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837]]>
2838 </programlisting>
2839 </informalexample>
2840 </para>
2841
2842 <para>
2843 This callback is called when the pcm is
2844 <quote>prepared</quote>. You can set the format type, sample
2845 rate, etc. here. The difference from
2846 <structfield>hw_params</structfield> is that the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002847 <structfield>prepare</structfield> callback will be called each
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 time
2849 <function>snd_pcm_prepare()</function> is called, i.e. when
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002850 recovering after underruns, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 </para>
2852
2853 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002854 Note that this callback is now non-atomic.
2855 You can use schedule-related functions safely in this callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 </para>
2857
2858 <para>
2859 In this and the following callbacks, you can refer to the
2860 values via the runtime record,
2861 substream-&gt;runtime.
2862 For example, to get the current
2863 rate, format or channels, access to
2864 runtime-&gt;rate,
2865 runtime-&gt;format or
2866 runtime-&gt;channels, respectively.
2867 The physical address of the allocated buffer is set to
2868 runtime-&gt;dma_area. The buffer and period sizes are
2869 in runtime-&gt;buffer_size and runtime-&gt;period_size,
2870 respectively.
2871 </para>
2872
2873 <para>
2874 Be careful that this callback will be called many times at
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002875 each setup, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 </para>
2877 </section>
2878
2879 <section id="pcm-interface-operators-trigger-callback">
2880 <title>trigger callback</title>
2881 <para>
2882 <informalexample>
2883 <programlisting>
2884<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002885 static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886]]>
2887 </programlisting>
2888 </informalexample>
2889
2890 This is called when the pcm is started, stopped or paused.
2891 </para>
2892
2893 <para>
2894 Which action is specified in the second argument,
2895 <constant>SNDRV_PCM_TRIGGER_XXX</constant> in
2896 <filename>&lt;sound/pcm.h&gt;</filename>. At least,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002897 the <constant>START</constant> and <constant>STOP</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 commands must be defined in this callback.
2899
2900 <informalexample>
2901 <programlisting>
2902<![CDATA[
2903 switch (cmd) {
2904 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002905 /* do something to start the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 break;
2907 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002908 /* do something to stop the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 break;
2910 default:
2911 return -EINVAL;
2912 }
2913]]>
2914 </programlisting>
2915 </informalexample>
2916 </para>
2917
2918 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002919 When the pcm supports the pause operation (given in the info
2920 field of the hardware table), the <constant>PAUSE_PUSE</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 and <constant>PAUSE_RELEASE</constant> commands must be
2922 handled here, too. The former is the command to pause the pcm,
2923 and the latter to restart the pcm again.
2924 </para>
2925
2926 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002927 When the pcm supports the suspend/resume operation,
2928 regardless of full or partial suspend/resume support,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002929 the <constant>SUSPEND</constant> and <constant>RESUME</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 commands must be handled, too.
2931 These commands are issued when the power-management status is
2932 changed. Obviously, the <constant>SUSPEND</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002933 <constant>RESUME</constant> commands
2934 suspend and resume the pcm substream, and usually, they
2935 are identical to the <constant>STOP</constant> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 <constant>START</constant> commands, respectively.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002937 See the <link linkend="power-management"><citetitle>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002938 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 </para>
2940
2941 <para>
2942 As mentioned, this callback is atomic. You cannot call
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002943 functions which may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 The trigger callback should be as minimal as possible,
2945 just really triggering the DMA. The other stuff should be
2946 initialized hw_params and prepare callbacks properly
2947 beforehand.
2948 </para>
2949 </section>
2950
2951 <section id="pcm-interface-operators-pointer-callback">
2952 <title>pointer callback</title>
2953 <para>
2954 <informalexample>
2955 <programlisting>
2956<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002957 static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958]]>
2959 </programlisting>
2960 </informalexample>
2961
2962 This callback is called when the PCM middle layer inquires
2963 the current hardware position on the buffer. The position must
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002964 be returned in frames,
2965 ranging from 0 to buffer_size - 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 </para>
2967
2968 <para>
2969 This is called usually from the buffer-update routine in the
2970 pcm middle layer, which is invoked when
2971 <function>snd_pcm_period_elapsed()</function> is called in the
2972 interrupt routine. Then the pcm middle layer updates the
2973 position and calculates the available space, and wakes up the
2974 sleeping poll threads, etc.
2975 </para>
2976
2977 <para>
2978 This callback is also atomic.
2979 </para>
2980 </section>
2981
2982 <section id="pcm-interface-operators-copy-silence">
2983 <title>copy and silence callbacks</title>
2984 <para>
2985 These callbacks are not mandatory, and can be omitted in
2986 most cases. These callbacks are used when the hardware buffer
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002987 cannot be in the normal memory space. Some chips have their
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 own buffer on the hardware which is not mappable. In such a
2989 case, you have to transfer the data manually from the memory
2990 buffer to the hardware buffer. Or, if the buffer is
2991 non-contiguous on both physical and virtual memory spaces,
2992 these callbacks must be defined, too.
2993 </para>
2994
2995 <para>
2996 If these two callbacks are defined, copy and set-silence
2997 operations are done by them. The detailed will be described in
2998 the later section <link
2999 linkend="buffer-and-memory"><citetitle>Buffer and Memory
3000 Management</citetitle></link>.
3001 </para>
3002 </section>
3003
3004 <section id="pcm-interface-operators-ack">
3005 <title>ack callback</title>
3006 <para>
3007 This callback is also not mandatory. This callback is called
3008 when the appl_ptr is updated in read or write operations.
3009 Some drivers like emu10k1-fx and cs46xx need to track the
3010 current appl_ptr for the internal buffer, and this callback
3011 is useful only for such a purpose.
3012 </para>
3013 <para>
3014 This callback is atomic.
3015 </para>
3016 </section>
3017
3018 <section id="pcm-interface-operators-page-callback">
3019 <title>page callback</title>
3020
3021 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003022 This callback is optional too. This callback is used
3023 mainly for non-contiguous buffers. The mmap calls this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 callback to get the page address. Some examples will be
3025 explained in the later section <link
3026 linkend="buffer-and-memory"><citetitle>Buffer and Memory
3027 Management</citetitle></link>, too.
3028 </para>
3029 </section>
3030 </section>
3031
3032 <section id="pcm-interface-interrupt-handler">
3033 <title>Interrupt Handler</title>
3034 <para>
3035 The rest of pcm stuff is the PCM interrupt handler. The
3036 role of PCM interrupt handler in the sound driver is to update
3037 the buffer position and to tell the PCM middle layer when the
3038 buffer position goes across the prescribed period size. To
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003039 inform this, call the <function>snd_pcm_period_elapsed()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 function.
3041 </para>
3042
3043 <para>
3044 There are several types of sound chips to generate the interrupts.
3045 </para>
3046
3047 <section id="pcm-interface-interrupt-handler-boundary">
3048 <title>Interrupts at the period (fragment) boundary</title>
3049 <para>
3050 This is the most frequently found type: the hardware
3051 generates an interrupt at each period boundary.
3052 In this case, you can call
3053 <function>snd_pcm_period_elapsed()</function> at each
3054 interrupt.
3055 </para>
3056
3057 <para>
3058 <function>snd_pcm_period_elapsed()</function> takes the
3059 substream pointer as its argument. Thus, you need to keep the
3060 substream pointer accessible from the chip instance. For
3061 example, define substream field in the chip record to hold the
3062 current running substream pointer, and set the pointer value
3063 at open callback (and reset at close callback).
3064 </para>
3065
3066 <para>
Adrian Bunk04187262006-06-30 18:23:04 +02003067 If you acquire a spinlock in the interrupt handler, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 lock is used in other pcm callbacks, too, then you have to
3069 release the lock before calling
3070 <function>snd_pcm_period_elapsed()</function>, because
3071 <function>snd_pcm_period_elapsed()</function> calls other pcm
3072 callbacks inside.
3073 </para>
3074
3075 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003076 Typical code would be like:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 <example>
3079 <title>Interrupt Handler Case #1</title>
3080 <programlisting>
3081<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003082 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003084 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 spin_lock(&chip->lock);
3086 ....
3087 if (pcm_irq_invoked(chip)) {
3088 /* call updater, unlock before it */
3089 spin_unlock(&chip->lock);
3090 snd_pcm_period_elapsed(chip->substream);
3091 spin_lock(&chip->lock);
Takashi Iwai95a5b082007-07-26 16:50:09 +02003092 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
3094 ....
3095 spin_unlock(&chip->lock);
3096 return IRQ_HANDLED;
3097 }
3098]]>
3099 </programlisting>
3100 </example>
3101 </para>
3102 </section>
3103
3104 <section id="pcm-interface-interrupt-handler-timer">
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003105 <title>High frequency timer interrupts</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003107 This happense when the hardware doesn't generate interrupts
3108 at the period boundary but issues timer interrupts at a fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 timer rate (e.g. es1968 or ymfpci drivers).
3110 In this case, you need to check the current hardware
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003111 position and accumulate the processed sample length at each
3112 interrupt. When the accumulated size exceeds the period
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 size, call
3114 <function>snd_pcm_period_elapsed()</function> and reset the
3115 accumulator.
3116 </para>
3117
3118 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003119 Typical code would be like the following.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 <example>
3122 <title>Interrupt Handler Case #2</title>
3123 <programlisting>
3124<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003125 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003127 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 spin_lock(&chip->lock);
3129 ....
3130 if (pcm_irq_invoked(chip)) {
3131 unsigned int last_ptr, size;
3132 /* get the current hardware pointer (in frames) */
3133 last_ptr = get_hw_ptr(chip);
3134 /* calculate the processed frames since the
3135 * last update
3136 */
3137 if (last_ptr < chip->last_ptr)
3138 size = runtime->buffer_size + last_ptr
3139 - chip->last_ptr;
3140 else
3141 size = last_ptr - chip->last_ptr;
3142 /* remember the last updated point */
3143 chip->last_ptr = last_ptr;
3144 /* accumulate the size */
3145 chip->size += size;
3146 /* over the period boundary? */
3147 if (chip->size >= runtime->period_size) {
3148 /* reset the accumulator */
3149 chip->size %= runtime->period_size;
3150 /* call updater */
3151 spin_unlock(&chip->lock);
3152 snd_pcm_period_elapsed(substream);
3153 spin_lock(&chip->lock);
3154 }
Takashi Iwai95a5b082007-07-26 16:50:09 +02003155 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
3157 ....
3158 spin_unlock(&chip->lock);
3159 return IRQ_HANDLED;
3160 }
3161]]>
3162 </programlisting>
3163 </example>
3164 </para>
3165 </section>
3166
3167 <section id="pcm-interface-interrupt-handler-both">
3168 <title>On calling <function>snd_pcm_period_elapsed()</function></title>
3169 <para>
3170 In both cases, even if more than one period are elapsed, you
3171 don't have to call
3172 <function>snd_pcm_period_elapsed()</function> many times. Call
3173 only once. And the pcm layer will check the current hardware
3174 pointer and update to the latest status.
3175 </para>
3176 </section>
3177 </section>
3178
3179 <section id="pcm-interface-atomicity">
3180 <title>Atomicity</title>
3181 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003182 One of the most important (and thus difficult to debug) problems
3183 in kernel programming are race conditions.
3184 In the Linux kernel, they are usually avoided via spin-locks, mutexes
3185 or semaphores. In general, if a race condition can happen
3186 in an interrupt handler, it has to be managed atomically, and you
3187 have to use a spinlock to protect the critical session. If the
3188 critical section is not in interrupt handler code and
3189 if taking a relatively long time to execute is acceptable, you
3190 should use mutexes or semaphores instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 </para>
3192
3193 <para>
3194 As already seen, some pcm callbacks are atomic and some are
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003195 not. For example, the <parameter>hw_params</parameter> callback is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 non-atomic, while <parameter>trigger</parameter> callback is
3197 atomic. This means, the latter is called already in a spinlock
3198 held by the PCM middle layer. Please take this atomicity into
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003199 account when you choose a locking scheme in the callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 </para>
3201
3202 <para>
3203 In the atomic callbacks, you cannot use functions which may call
3204 <function>schedule</function> or go to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003205 <function>sleep</function>. Semaphores and mutexes can sleep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 and hence they cannot be used inside the atomic callbacks
3207 (e.g. <parameter>trigger</parameter> callback).
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003208 To implement some delay in such a callback, please use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 <function>udelay()</function> or <function>mdelay()</function>.
3210 </para>
3211
3212 <para>
3213 All three atomic callbacks (trigger, pointer, and ack) are
3214 called with local interrupts disabled.
3215 </para>
3216
3217 </section>
3218 <section id="pcm-interface-constraints">
3219 <title>Constraints</title>
3220 <para>
3221 If your chip supports unconventional sample rates, or only the
3222 limited samples, you need to set a constraint for the
3223 condition.
3224 </para>
3225
3226 <para>
3227 For example, in order to restrict the sample rates in the some
3228 supported values, use
3229 <function>snd_pcm_hw_constraint_list()</function>.
3230 You need to call this function in the open callback.
3231
3232 <example>
3233 <title>Example of Hardware Constraints</title>
3234 <programlisting>
3235<![CDATA[
3236 static unsigned int rates[] =
3237 {4000, 10000, 22050, 44100};
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003238 static struct snd_pcm_hw_constraint_list constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 .count = ARRAY_SIZE(rates),
3240 .list = rates,
3241 .mask = 0,
3242 };
3243
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003244 static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 {
3246 int err;
3247 ....
3248 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
3249 SNDRV_PCM_HW_PARAM_RATE,
3250 &constraints_rates);
3251 if (err < 0)
3252 return err;
3253 ....
3254 }
3255]]>
3256 </programlisting>
3257 </example>
3258 </para>
3259
3260 <para>
3261 There are many different constraints.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003262 Look at <filename>sound/pcm.h</filename> for a complete list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 You can even define your own constraint rules.
3264 For example, let's suppose my_chip can manage a substream of 1 channel
3265 if and only if the format is S16_LE, otherwise it supports any format
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003266 specified in the <structname>snd_pcm_hardware</structname> structure (or in any
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 other constraint_list). You can build a rule like this:
3268
3269 <example>
3270 <title>Example of Hardware Constraints for Channels</title>
3271 <programlisting>
3272<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003273 static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
3274 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003276 struct snd_interval *c = hw_param_interval(params,
3277 SNDRV_PCM_HW_PARAM_CHANNELS);
3278 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3279 struct snd_mask fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
3281 snd_mask_any(&fmt); /* Init the struct */
3282 if (c->min < 2) {
3283 fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE;
3284 return snd_mask_refine(f, &fmt);
3285 }
3286 return 0;
3287 }
3288]]>
3289 </programlisting>
3290 </example>
3291 </para>
3292
3293 <para>
3294 Then you need to call this function to add your rule:
3295
3296 <informalexample>
3297 <programlisting>
3298<![CDATA[
3299 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3300 hw_rule_channels_by_format, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3301 -1);
3302]]>
3303 </programlisting>
3304 </informalexample>
3305 </para>
3306
3307 <para>
3308 The rule function is called when an application sets the number of
3309 channels. But an application can set the format before the number of
3310 channels. Thus you also need to define the inverse rule:
3311
3312 <example>
3313 <title>Example of Hardware Constraints for Channels</title>
3314 <programlisting>
3315<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003316 static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
3317 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003319 struct snd_interval *c = hw_param_interval(params,
3320 SNDRV_PCM_HW_PARAM_CHANNELS);
3321 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3322 struct snd_interval ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 snd_interval_any(&ch);
3325 if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
3326 ch.min = ch.max = 1;
3327 ch.integer = 1;
3328 return snd_interval_refine(c, &ch);
3329 }
3330 return 0;
3331 }
3332]]>
3333 </programlisting>
3334 </example>
3335 </para>
3336
3337 <para>
3338 ...and in the open callback:
3339 <informalexample>
3340 <programlisting>
3341<![CDATA[
3342 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3343 hw_rule_format_by_channels, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3344 -1);
3345]]>
3346 </programlisting>
3347 </informalexample>
3348 </para>
3349
3350 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003351 I won't give more details here, rather I
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 would like to say, <quote>Luke, use the source.</quote>
3353 </para>
3354 </section>
3355
3356 </chapter>
3357
3358
3359<!-- ****************************************************** -->
3360<!-- Control Interface -->
3361<!-- ****************************************************** -->
3362 <chapter id="control-interface">
3363 <title>Control Interface</title>
3364
3365 <section id="control-interface-general">
3366 <title>General</title>
3367 <para>
3368 The control interface is used widely for many switches,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003369 sliders, etc. which are accessed from user-space. Its most
3370 important use is the mixer interface. In other words, since ALSA
3371 0.9.x, all the mixer stuff is implemented on the control kernel API.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 </para>
3373
3374 <para>
3375 ALSA has a well-defined AC97 control module. If your chip
3376 supports only the AC97 and nothing else, you can skip this
3377 section.
3378 </para>
3379
3380 <para>
3381 The control API is defined in
3382 <filename>&lt;sound/control.h&gt;</filename>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003383 Include this file if you want to add your own controls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 </para>
3385 </section>
3386
3387 <section id="control-interface-definition">
3388 <title>Definition of Controls</title>
3389 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003390 To create a new control, you need to define the
3391 following three
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 callbacks: <structfield>info</structfield>,
3393 <structfield>get</structfield> and
3394 <structfield>put</structfield>. Then, define a
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003395 struct <structname>snd_kcontrol_new</structname> record, such as:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 <example>
3398 <title>Definition of a Control</title>
3399 <programlisting>
3400<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003401 static struct snd_kcontrol_new my_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3403 .name = "PCM Playback Switch",
3404 .index = 0,
3405 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003406 .private_value = 0xffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 .info = my_control_info,
3408 .get = my_control_get,
3409 .put = my_control_put
3410 };
3411]]>
3412 </programlisting>
3413 </example>
3414 </para>
3415
3416 <para>
3417 Most likely the control is created via
3418 <function>snd_ctl_new1()</function>, and in such a case, you can
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003419 add the <parameter>__devinitdata</parameter> prefix to the
3420 definition as above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 </para>
3422
3423 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003424 The <structfield>iface</structfield> field specifies the control
3425 type, <constant>SNDRV_CTL_ELEM_IFACE_XXX</constant>, which
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003426 is usually <constant>MIXER</constant>.
3427 Use <constant>CARD</constant> for global controls that are not
3428 logically part of the mixer.
3429 If the control is closely associated with some specific device on
3430 the sound card, use <constant>HWDEP</constant>,
3431 <constant>PCM</constant>, <constant>RAWMIDI</constant>,
3432 <constant>TIMER</constant>, or <constant>SEQUENCER</constant>, and
3433 specify the device number with the
3434 <structfield>device</structfield> and
3435 <structfield>subdevice</structfield> fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 </para>
3437
3438 <para>
3439 The <structfield>name</structfield> is the name identifier
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003440 string. Since ALSA 0.9.x, the control name is very important,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 because its role is classified from its name. There are
3442 pre-defined standard control names. The details are described in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003443 the <link linkend="control-interface-control-names"><citetitle>
3444 Control Names</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 </para>
3446
3447 <para>
3448 The <structfield>index</structfield> field holds the index number
3449 of this control. If there are several different controls with
3450 the same name, they can be distinguished by the index
3451 number. This is the case when
3452 several codecs exist on the card. If the index is zero, you can
3453 omit the definition above.
3454 </para>
3455
3456 <para>
3457 The <structfield>access</structfield> field contains the access
3458 type of this control. Give the combination of bit masks,
3459 <constant>SNDRV_CTL_ELEM_ACCESS_XXX</constant>, there.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003460 The details will be explained in
3461 the <link linkend="control-interface-access-flags"><citetitle>
3462 Access Flags</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 </para>
3464
3465 <para>
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003466 The <structfield>private_value</structfield> field contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 an arbitrary long integer value for this record. When using
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003468 the generic <structfield>info</structfield>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 <structfield>get</structfield> and
3470 <structfield>put</structfield> callbacks, you can pass a value
3471 through this field. If several small numbers are necessary, you can
3472 combine them in bitwise. Or, it's possible to give a pointer
3473 (casted to unsigned long) of some record to this field, too.
3474 </para>
3475
3476 <para>
Clemens Ladischd1761d12007-09-10 08:05:19 +02003477 The <structfield>tlv</structfield> field can be used to provide
3478 metadata about the control; see the
3479 <link linkend="control-interface-tlv">
3480 <citetitle>Metadata</citetitle></link> subsection.
3481 </para>
3482
3483 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 The other three are
3485 <link linkend="control-interface-callbacks"><citetitle>
3486 callback functions</citetitle></link>.
3487 </para>
3488 </section>
3489
3490 <section id="control-interface-control-names">
3491 <title>Control Names</title>
3492 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003493 There are some standards to define the control names. A
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 control is usually defined from the three parts as
3495 <quote>SOURCE DIRECTION FUNCTION</quote>.
3496 </para>
3497
3498 <para>
3499 The first, <constant>SOURCE</constant>, specifies the source
3500 of the control, and is a string such as <quote>Master</quote>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003501 <quote>PCM</quote>, <quote>CD</quote> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 <quote>Line</quote>. There are many pre-defined sources.
3503 </para>
3504
3505 <para>
3506 The second, <constant>DIRECTION</constant>, is one of the
3507 following strings according to the direction of the control:
3508 <quote>Playback</quote>, <quote>Capture</quote>, <quote>Bypass
3509 Playback</quote> and <quote>Bypass Capture</quote>. Or, it can
3510 be omitted, meaning both playback and capture directions.
3511 </para>
3512
3513 <para>
3514 The third, <constant>FUNCTION</constant>, is one of the
3515 following strings according to the function of the control:
3516 <quote>Switch</quote>, <quote>Volume</quote> and
3517 <quote>Route</quote>.
3518 </para>
3519
3520 <para>
3521 The example of control names are, thus, <quote>Master Capture
3522 Switch</quote> or <quote>PCM Playback Volume</quote>.
3523 </para>
3524
3525 <para>
3526 There are some exceptions:
3527 </para>
3528
3529 <section id="control-interface-control-names-global">
3530 <title>Global capture and playback</title>
3531 <para>
3532 <quote>Capture Source</quote>, <quote>Capture Switch</quote>
3533 and <quote>Capture Volume</quote> are used for the global
3534 capture (input) source, switch and volume. Similarly,
3535 <quote>Playback Switch</quote> and <quote>Playback
3536 Volume</quote> are used for the global output gain switch and
3537 volume.
3538 </para>
3539 </section>
3540
3541 <section id="control-interface-control-names-tone">
3542 <title>Tone-controls</title>
3543 <para>
3544 tone-control switch and volumes are specified like
3545 <quote>Tone Control - XXX</quote>, e.g. <quote>Tone Control -
3546 Switch</quote>, <quote>Tone Control - Bass</quote>,
3547 <quote>Tone Control - Center</quote>.
3548 </para>
3549 </section>
3550
3551 <section id="control-interface-control-names-3d">
3552 <title>3D controls</title>
3553 <para>
3554 3D-control switches and volumes are specified like <quote>3D
3555 Control - XXX</quote>, e.g. <quote>3D Control -
3556 Switch</quote>, <quote>3D Control - Center</quote>, <quote>3D
3557 Control - Space</quote>.
3558 </para>
3559 </section>
3560
3561 <section id="control-interface-control-names-mic">
3562 <title>Mic boost</title>
3563 <para>
3564 Mic-boost switch is set as <quote>Mic Boost</quote> or
3565 <quote>Mic Boost (6dB)</quote>.
3566 </para>
3567
3568 <para>
3569 More precise information can be found in
3570 <filename>Documentation/sound/alsa/ControlNames.txt</filename>.
3571 </para>
3572 </section>
3573 </section>
3574
3575 <section id="control-interface-access-flags">
3576 <title>Access Flags</title>
3577
3578 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003579 The access flag is the bitmask which specifies the access type
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 of the given control. The default access type is
3581 <constant>SNDRV_CTL_ELEM_ACCESS_READWRITE</constant>,
3582 which means both read and write are allowed to this control.
3583 When the access flag is omitted (i.e. = 0), it is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003584 considered as <constant>READWRITE</constant> access as default.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 </para>
3586
3587 <para>
3588 When the control is read-only, pass
3589 <constant>SNDRV_CTL_ELEM_ACCESS_READ</constant> instead.
3590 In this case, you don't have to define
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003591 the <structfield>put</structfield> callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 Similarly, when the control is write-only (although it's a rare
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003593 case), you can use the <constant>WRITE</constant> flag instead, and
3594 you don't need the <structfield>get</structfield> callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 </para>
3596
3597 <para>
3598 If the control value changes frequently (e.g. the VU meter),
3599 <constant>VOLATILE</constant> flag should be given. This means
3600 that the control may be changed without
3601 <link linkend="control-interface-change-notification"><citetitle>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003602 notification</citetitle></link>. Applications should poll such
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 a control constantly.
3604 </para>
3605
3606 <para>
3607 When the control is inactive, set
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003608 the <constant>INACTIVE</constant> flag, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 There are <constant>LOCK</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003610 <constant>OWNER</constant> flags to change the write
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 permissions.
3612 </para>
3613
3614 </section>
3615
3616 <section id="control-interface-callbacks">
3617 <title>Callbacks</title>
3618
3619 <section id="control-interface-callbacks-info">
3620 <title>info callback</title>
3621 <para>
3622 The <structfield>info</structfield> callback is used to get
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003623 detailed information on this control. This must store the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003624 values of the given struct <structname>snd_ctl_elem_info</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 object. For example, for a boolean control with a single
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003626 element:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 <example>
3629 <title>Example of info callback</title>
3630 <programlisting>
3631<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003632 static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003633 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 {
3635 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3636 uinfo->count = 1;
3637 uinfo->value.integer.min = 0;
3638 uinfo->value.integer.max = 1;
3639 return 0;
3640 }
3641]]>
3642 </programlisting>
3643 </example>
3644 </para>
3645
3646 <para>
3647 The <structfield>type</structfield> field specifies the type
3648 of the control. There are <constant>BOOLEAN</constant>,
3649 <constant>INTEGER</constant>, <constant>ENUMERATED</constant>,
3650 <constant>BYTES</constant>, <constant>IEC958</constant> and
3651 <constant>INTEGER64</constant>. The
3652 <structfield>count</structfield> field specifies the
3653 number of elements in this control. For example, a stereo
3654 volume would have count = 2. The
3655 <structfield>value</structfield> field is a union, and
3656 the values stored are depending on the type. The boolean and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003657 integer types are identical.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 </para>
3659
3660 <para>
3661 The enumerated type is a bit different from others. You'll
3662 need to set the string for the currently given item index.
3663
3664 <informalexample>
3665 <programlisting>
3666<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003667 static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003668 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 {
3670 static char *texts[4] = {
3671 "First", "Second", "Third", "Fourth"
3672 };
3673 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3674 uinfo->count = 1;
3675 uinfo->value.enumerated.items = 4;
3676 if (uinfo->value.enumerated.item > 3)
3677 uinfo->value.enumerated.item = 3;
3678 strcpy(uinfo->value.enumerated.name,
3679 texts[uinfo->value.enumerated.item]);
3680 return 0;
3681 }
3682]]>
3683 </programlisting>
3684 </informalexample>
3685 </para>
Takashi Iwai95a5b082007-07-26 16:50:09 +02003686
3687 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003688 Some common info callbacks are available for your convenience:
Takashi Iwai95a5b082007-07-26 16:50:09 +02003689 <function>snd_ctl_boolean_mono_info()</function> and
3690 <function>snd_ctl_boolean_stereo_info()</function>.
3691 Obviously, the former is an info callback for a mono channel
3692 boolean item, just like <function>snd_myctl_mono_info</function>
3693 above, and the latter is for a stereo channel boolean item.
3694 </para>
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 </section>
3697
3698 <section id="control-interface-callbacks-get">
3699 <title>get callback</title>
3700
3701 <para>
3702 This callback is used to read the current value of the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003703 control and to return to user-space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 </para>
3705
3706 <para>
3707 For example,
3708
3709 <example>
3710 <title>Example of get callback</title>
3711 <programlisting>
3712<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003713 static int snd_myctl_get(struct snd_kcontrol *kcontrol,
3714 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003716 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 ucontrol->value.integer.value[0] = get_some_value(chip);
3718 return 0;
3719 }
3720]]>
3721 </programlisting>
3722 </example>
3723 </para>
3724
3725 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003726 The <structfield>value</structfield> field depends on
3727 the type of control as well as on the info callback. For example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 the sb driver uses this field to store the register offset,
3729 the bit-shift and the bit-mask. The
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003730 <structfield>private_value</structfield> field is set as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 <informalexample>
3732 <programlisting>
3733<![CDATA[
3734 .private_value = reg | (shift << 16) | (mask << 24)
3735]]>
3736 </programlisting>
3737 </informalexample>
3738 and is retrieved in callbacks like
3739 <informalexample>
3740 <programlisting>
3741<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003742 static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol,
3743 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 {
3745 int reg = kcontrol->private_value & 0xff;
3746 int shift = (kcontrol->private_value >> 16) & 0xff;
3747 int mask = (kcontrol->private_value >> 24) & 0xff;
3748 ....
3749 }
3750]]>
3751 </programlisting>
3752 </informalexample>
3753 </para>
3754
3755 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003756 In the <structfield>get</structfield> callback,
3757 you have to fill all the elements if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 control has more than one elements,
3759 i.e. <structfield>count</structfield> &gt; 1.
3760 In the example above, we filled only one element
3761 (<structfield>value.integer.value[0]</structfield>) since it's
3762 assumed as <structfield>count</structfield> = 1.
3763 </para>
3764 </section>
3765
3766 <section id="control-interface-callbacks-put">
3767 <title>put callback</title>
3768
3769 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003770 This callback is used to write a value from user-space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 </para>
3772
3773 <para>
3774 For example,
3775
3776 <example>
3777 <title>Example of put callback</title>
3778 <programlisting>
3779<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003780 static int snd_myctl_put(struct snd_kcontrol *kcontrol,
3781 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003783 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 int changed = 0;
3785 if (chip->current_value !=
3786 ucontrol->value.integer.value[0]) {
3787 change_current_value(chip,
3788 ucontrol->value.integer.value[0]);
3789 changed = 1;
3790 }
3791 return changed;
3792 }
3793]]>
3794 </programlisting>
3795 </example>
3796
3797 As seen above, you have to return 1 if the value is
3798 changed. If the value is not changed, return 0 instead.
3799 If any fatal error happens, return a negative error code as
3800 usual.
3801 </para>
3802
3803 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003804 As in the <structfield>get</structfield> callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 when the control has more than one elements,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003806 all elements must be evaluated in this callback, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 </para>
3808 </section>
3809
3810 <section id="control-interface-callbacks-all">
3811 <title>Callbacks are not atomic</title>
3812 <para>
3813 All these three callbacks are basically not atomic.
3814 </para>
3815 </section>
3816 </section>
3817
3818 <section id="control-interface-constructor">
3819 <title>Constructor</title>
3820 <para>
3821 When everything is ready, finally we can create a new
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003822 control. To create a control, there are two functions to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 called, <function>snd_ctl_new1()</function> and
3824 <function>snd_ctl_add()</function>.
3825 </para>
3826
3827 <para>
3828 In the simplest way, you can do like this:
3829
3830 <informalexample>
3831 <programlisting>
3832<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003833 err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
3834 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 return err;
3836]]>
3837 </programlisting>
3838 </informalexample>
3839
3840 where <parameter>my_control</parameter> is the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003841 struct <structname>snd_kcontrol_new</structname> object defined above, and chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 is the object pointer to be passed to
3843 kcontrol-&gt;private_data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003844 which can be referred to in callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 </para>
3846
3847 <para>
3848 <function>snd_ctl_new1()</function> allocates a new
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003849 <structname>snd_kcontrol</structname> instance (that's why the definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 of <parameter>my_control</parameter> can be with
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003851 the <parameter>__devinitdata</parameter>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 prefix), and <function>snd_ctl_add</function> assigns the given
3853 control component to the card.
3854 </para>
3855 </section>
3856
3857 <section id="control-interface-change-notification">
3858 <title>Change Notification</title>
3859 <para>
3860 If you need to change and update a control in the interrupt
3861 routine, you can call <function>snd_ctl_notify()</function>. For
3862 example,
3863
3864 <informalexample>
3865 <programlisting>
3866<![CDATA[
3867 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);
3868]]>
3869 </programlisting>
3870 </informalexample>
3871
3872 This function takes the card pointer, the event-mask, and the
3873 control id pointer for the notification. The event-mask
3874 specifies the types of notification, for example, in the above
3875 example, the change of control values is notified.
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003876 The id pointer is the pointer of struct <structname>snd_ctl_elem_id</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 to be notified.
3878 You can find some examples in <filename>es1938.c</filename> or
3879 <filename>es1968.c</filename> for hardware volume interrupts.
3880 </para>
3881 </section>
3882
Clemens Ladischd1761d12007-09-10 08:05:19 +02003883 <section id="control-interface-tlv">
3884 <title>Metadata</title>
3885 <para>
3886 To provide information about the dB values of a mixer control, use
3887 on of the <constant>DECLARE_TLV_xxx</constant> macros from
3888 <filename>&lt;sound/tlv.h&gt;</filename> to define a variable
3889 containing this information, set the<structfield>tlv.p
3890 </structfield> field to point to this variable, and include the
3891 <constant>SNDRV_CTL_ELEM_ACCESS_TLV_READ</constant> flag in the
3892 <structfield>access</structfield> field; like this:
3893 <informalexample>
3894 <programlisting>
3895<![CDATA[
3896 static DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0);
3897
3898 static struct snd_kcontrol_new my_control __devinitdata = {
3899 ...
3900 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3901 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3902 ...
3903 .tlv.p = db_scale_my_control,
3904 };
3905]]>
3906 </programlisting>
3907 </informalexample>
3908 </para>
3909
3910 <para>
3911 The <function>DECLARE_TLV_DB_SCALE</function> macro defines
3912 information about a mixer control where each step in the control's
3913 value changes the dB value by a constant dB amount.
3914 The first parameter is the name of the variable to be defined.
3915 The second parameter is the minimum value, in units of 0.01 dB.
3916 The third parameter is the step size, in units of 0.01 dB.
3917 Set the fourth parameter to 1 if the minimum value actually mutes
3918 the control.
3919 </para>
3920
3921 <para>
3922 The <function>DECLARE_TLV_DB_LINEAR</function> macro defines
3923 information about a mixer control where the control's value affects
3924 the output linearly.
3925 The first parameter is the name of the variable to be defined.
3926 The second parameter is the minimum value, in units of 0.01 dB.
3927 The third parameter is the maximum value, in units of 0.01 dB.
3928 If the minimum value mutes the control, set the second parameter to
3929 <constant>TLV_DB_GAIN_MUTE</constant>.
3930 </para>
3931 </section>
3932
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 </chapter>
3934
3935
3936<!-- ****************************************************** -->
3937<!-- API for AC97 Codec -->
3938<!-- ****************************************************** -->
3939 <chapter id="api-ac97">
3940 <title>API for AC97 Codec</title>
3941
3942 <section>
3943 <title>General</title>
3944 <para>
3945 The ALSA AC97 codec layer is a well-defined one, and you don't
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003946 have to write much code to control it. Only low-level control
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 routines are necessary. The AC97 codec API is defined in
3948 <filename>&lt;sound/ac97_codec.h&gt;</filename>.
3949 </para>
3950 </section>
3951
3952 <section id="api-ac97-example">
3953 <title>Full Code Example</title>
3954 <para>
3955 <example>
3956 <title>Example of AC97 Interface</title>
3957 <programlisting>
3958<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003959 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003961 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 ....
3963 };
3964
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003965 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 unsigned short reg)
3967 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003968 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003970 /* read a register value here from the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 return the_register_value;
3972 }
3973
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003974 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 unsigned short reg, unsigned short val)
3976 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003977 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003979 /* write the given register value to the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 }
3981
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003982 static int snd_mychip_ac97(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003984 struct snd_ac97_bus *bus;
3985 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003987 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 .write = snd_mychip_ac97_write,
3989 .read = snd_mychip_ac97_read,
3990 };
3991
Takashi Iwai95a5b082007-07-26 16:50:09 +02003992 err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
3993 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return err;
3995 memset(&ac97, 0, sizeof(ac97));
3996 ac97.private_data = chip;
3997 return snd_ac97_mixer(bus, &ac97, &chip->ac97);
3998 }
3999
4000]]>
4001 </programlisting>
4002 </example>
4003 </para>
4004 </section>
4005
4006 <section id="api-ac97-constructor">
4007 <title>Constructor</title>
4008 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004009 To create an ac97 instance, first call <function>snd_ac97_bus</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 with an <type>ac97_bus_ops_t</type> record with callback functions.
4011
4012 <informalexample>
4013 <programlisting>
4014<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004015 struct snd_ac97_bus *bus;
4016 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 .write = snd_mychip_ac97_write,
4018 .read = snd_mychip_ac97_read,
4019 };
4020
4021 snd_ac97_bus(card, 0, &ops, NULL, &pbus);
4022]]>
4023 </programlisting>
4024 </informalexample>
4025
4026 The bus record is shared among all belonging ac97 instances.
4027 </para>
4028
4029 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004030 And then call <function>snd_ac97_mixer()</function> with an
4031 struct <structname>snd_ac97_template</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 record together with the bus pointer created above.
4033
4034 <informalexample>
4035 <programlisting>
4036<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004037 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int err;
4039
4040 memset(&ac97, 0, sizeof(ac97));
4041 ac97.private_data = chip;
4042 snd_ac97_mixer(bus, &ac97, &chip->ac97);
4043]]>
4044 </programlisting>
4045 </informalexample>
4046
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004047 where chip-&gt;ac97 is a pointer to a newly created
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 <type>ac97_t</type> instance.
4049 In this case, the chip pointer is set as the private data, so that
4050 the read/write callback functions can refer to this chip instance.
4051 This instance is not necessarily stored in the chip
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004052 record. If you need to change the register values from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 driver, or need the suspend/resume of ac97 codecs, keep this
4054 pointer to pass to the corresponding functions.
4055 </para>
4056 </section>
4057
4058 <section id="api-ac97-callbacks">
4059 <title>Callbacks</title>
4060 <para>
4061 The standard callbacks are <structfield>read</structfield> and
4062 <structfield>write</structfield>. Obviously they
4063 correspond to the functions for read and write accesses to the
4064 hardware low-level codes.
4065 </para>
4066
4067 <para>
4068 The <structfield>read</structfield> callback returns the
4069 register value specified in the argument.
4070
4071 <informalexample>
4072 <programlisting>
4073<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004074 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 unsigned short reg)
4076 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004077 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 ....
4079 return the_register_value;
4080 }
4081]]>
4082 </programlisting>
4083 </informalexample>
4084
4085 Here, the chip can be cast from ac97-&gt;private_data.
4086 </para>
4087
4088 <para>
4089 Meanwhile, the <structfield>write</structfield> callback is
4090 used to set the register value.
4091
4092 <informalexample>
4093 <programlisting>
4094<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004095 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 unsigned short reg, unsigned short val)
4097]]>
4098 </programlisting>
4099 </informalexample>
4100 </para>
4101
4102 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004103 These callbacks are non-atomic like the control API callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 </para>
4105
4106 <para>
4107 There are also other callbacks:
4108 <structfield>reset</structfield>,
4109 <structfield>wait</structfield> and
4110 <structfield>init</structfield>.
4111 </para>
4112
4113 <para>
4114 The <structfield>reset</structfield> callback is used to reset
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004115 the codec. If the chip requires a special kind of reset, you can
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 define this callback.
4117 </para>
4118
4119 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004120 The <structfield>wait</structfield> callback is used to
4121 add some waiting time in the standard initialization of the codec. If the
4122 chip requires the extra waiting time, define this callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 </para>
4124
4125 <para>
4126 The <structfield>init</structfield> callback is used for
4127 additional initialization of the codec.
4128 </para>
4129 </section>
4130
4131 <section id="api-ac97-updating-registers">
4132 <title>Updating Registers in The Driver</title>
4133 <para>
4134 If you need to access to the codec from the driver, you can
4135 call the following functions:
4136 <function>snd_ac97_write()</function>,
4137 <function>snd_ac97_read()</function>,
4138 <function>snd_ac97_update()</function> and
4139 <function>snd_ac97_update_bits()</function>.
4140 </para>
4141
4142 <para>
4143 Both <function>snd_ac97_write()</function> and
4144 <function>snd_ac97_update()</function> functions are used to
4145 set a value to the given register
4146 (<constant>AC97_XXX</constant>). The difference between them is
4147 that <function>snd_ac97_update()</function> doesn't write a
4148 value if the given value has been already set, while
4149 <function>snd_ac97_write()</function> always rewrites the
4150 value.
4151
4152 <informalexample>
4153 <programlisting>
4154<![CDATA[
4155 snd_ac97_write(ac97, AC97_MASTER, 0x8080);
4156 snd_ac97_update(ac97, AC97_MASTER, 0x8080);
4157]]>
4158 </programlisting>
4159 </informalexample>
4160 </para>
4161
4162 <para>
4163 <function>snd_ac97_read()</function> is used to read the value
4164 of the given register. For example,
4165
4166 <informalexample>
4167 <programlisting>
4168<![CDATA[
4169 value = snd_ac97_read(ac97, AC97_MASTER);
4170]]>
4171 </programlisting>
4172 </informalexample>
4173 </para>
4174
4175 <para>
4176 <function>snd_ac97_update_bits()</function> is used to update
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004177 some bits in the given register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
4179 <informalexample>
4180 <programlisting>
4181<![CDATA[
4182 snd_ac97_update_bits(ac97, reg, mask, value);
4183]]>
4184 </programlisting>
4185 </informalexample>
4186 </para>
4187
4188 <para>
4189 Also, there is a function to change the sample rate (of a
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004190 given register such as
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 <constant>AC97_PCM_FRONT_DAC_RATE</constant>) when VRA or
4192 DRA is supported by the codec:
4193 <function>snd_ac97_set_rate()</function>.
4194
4195 <informalexample>
4196 <programlisting>
4197<![CDATA[
4198 snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);
4199]]>
4200 </programlisting>
4201 </informalexample>
4202 </para>
4203
4204 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004205 The following registers are available to set the rate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 <constant>AC97_PCM_MIC_ADC_RATE</constant>,
4207 <constant>AC97_PCM_FRONT_DAC_RATE</constant>,
4208 <constant>AC97_PCM_LR_ADC_RATE</constant>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004209 <constant>AC97_SPDIF</constant>. When
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 <constant>AC97_SPDIF</constant> is specified, the register is
4211 not really changed but the corresponding IEC958 status bits will
4212 be updated.
4213 </para>
4214 </section>
4215
4216 <section id="api-ac97-clock-adjustment">
4217 <title>Clock Adjustment</title>
4218 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004219 In some chips, the clock of the codec isn't 48000 but using a
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 PCI clock (to save a quartz!). In this case, change the field
4221 bus-&gt;clock to the corresponding
4222 value. For example, intel8x0
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004223 and es1968 drivers have their own function to read from the clock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 </para>
4225 </section>
4226
4227 <section id="api-ac97-proc-files">
4228 <title>Proc Files</title>
4229 <para>
4230 The ALSA AC97 interface will create a proc file such as
4231 <filename>/proc/asound/card0/codec97#0/ac97#0-0</filename> and
4232 <filename>ac97#0-0+regs</filename>. You can refer to these files to
4233 see the current status and registers of the codec.
4234 </para>
4235 </section>
4236
4237 <section id="api-ac97-multiple-codecs">
4238 <title>Multiple Codecs</title>
4239 <para>
4240 When there are several codecs on the same card, you need to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004241 call <function>snd_ac97_mixer()</function> multiple times with
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 ac97.num=1 or greater. The <structfield>num</structfield> field
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004243 specifies the codec number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 </para>
4245
4246 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004247 If you set up multiple codecs, you either need to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 different callbacks for each codec or check
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004249 ac97-&gt;num in the callback routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 </para>
4251 </section>
4252
4253 </chapter>
4254
4255
4256<!-- ****************************************************** -->
4257<!-- MIDI (MPU401-UART) Interface -->
4258<!-- ****************************************************** -->
4259 <chapter id="midi-interface">
4260 <title>MIDI (MPU401-UART) Interface</title>
4261
4262 <section id="midi-interface-general">
4263 <title>General</title>
4264 <para>
4265 Many soundcards have built-in MIDI (MPU401-UART)
4266 interfaces. When the soundcard supports the standard MPU401-UART
4267 interface, most likely you can use the ALSA MPU401-UART API. The
4268 MPU401-UART API is defined in
4269 <filename>&lt;sound/mpu401.h&gt;</filename>.
4270 </para>
4271
4272 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004273 Some soundchips have a similar but slightly different
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 implementation of mpu401 stuff. For example, emu10k1 has its own
4275 mpu401 routines.
4276 </para>
4277 </section>
4278
4279 <section id="midi-interface-constructor">
4280 <title>Constructor</title>
4281 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004282 To create a rawmidi object, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 <function>snd_mpu401_uart_new()</function>.
4284
4285 <informalexample>
4286 <programlisting>
4287<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004288 struct snd_rawmidi *rmidi;
Takashi Iwai302e4c22006-05-23 13:24:30 +02004289 snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 irq, irq_flags, &rmidi);
4291]]>
4292 </programlisting>
4293 </informalexample>
4294 </para>
4295
4296 <para>
4297 The first argument is the card pointer, and the second is the
4298 index of this component. You can create up to 8 rawmidi
4299 devices.
4300 </para>
4301
4302 <para>
4303 The third argument is the type of the hardware,
4304 <constant>MPU401_HW_XXX</constant>. If it's not a special one,
4305 you can use <constant>MPU401_HW_MPU401</constant>.
4306 </para>
4307
4308 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004309 The 4th argument is the I/O port address. Many
4310 backward-compatible MPU401 have an I/O port such as 0x330. Or, it
4311 might be a part of its own PCI I/O region. It depends on the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 chip design.
4313 </para>
4314
4315 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004316 The 5th argument is a bitflag for additional information.
4317 When the I/O port address above is part of the PCI I/O
4318 region, the MPU401 I/O port might have been already allocated
Takashi Iwai302e4c22006-05-23 13:24:30 +02004319 (reserved) by the driver itself. In such a case, pass a bit flag
4320 <constant>MPU401_INFO_INTEGRATED</constant>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004321 and the mpu401-uart layer will allocate the I/O ports by itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 </para>
4323
Takashi Iwai302e4c22006-05-23 13:24:30 +02004324 <para>
4325 When the controller supports only the input or output MIDI stream,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004326 pass the <constant>MPU401_INFO_INPUT</constant> or
Takashi Iwai302e4c22006-05-23 13:24:30 +02004327 <constant>MPU401_INFO_OUTPUT</constant> bitflag, respectively.
4328 Then the rawmidi instance is created as a single stream.
4329 </para>
4330
4331 <para>
4332 <constant>MPU401_INFO_MMIO</constant> bitflag is used to change
4333 the access method to MMIO (via readb and writeb) instead of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004334 iob and outb. In this case, you have to pass the iomapped address
Takashi Iwai302e4c22006-05-23 13:24:30 +02004335 to <function>snd_mpu401_uart_new()</function>.
4336 </para>
4337
4338 <para>
4339 When <constant>MPU401_INFO_TX_IRQ</constant> is set, the output
4340 stream isn't checked in the default interrupt handler. The driver
4341 needs to call <function>snd_mpu401_uart_interrupt_tx()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004342 by itself to start processing the output stream in the irq handler.
Takashi Iwai302e4c22006-05-23 13:24:30 +02004343 </para>
4344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 <para>
4346 Usually, the port address corresponds to the command port and
4347 port + 1 corresponds to the data port. If not, you may change
4348 the <structfield>cport</structfield> field of
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004349 struct <structname>snd_mpu401</structname> manually
4350 afterward. However, <structname>snd_mpu401</structname> pointer is not
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 returned explicitly by
4352 <function>snd_mpu401_uart_new()</function>. You need to cast
4353 rmidi-&gt;private_data to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004354 <structname>snd_mpu401</structname> explicitly,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
4356 <informalexample>
4357 <programlisting>
4358<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004359 struct snd_mpu401 *mpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 mpu = rmidi->private_data;
4361]]>
4362 </programlisting>
4363 </informalexample>
4364
4365 and reset the cport as you like:
4366
4367 <informalexample>
4368 <programlisting>
4369<![CDATA[
4370 mpu->cport = my_own_control_port;
4371]]>
4372 </programlisting>
4373 </informalexample>
4374 </para>
4375
4376 <para>
4377 The 6th argument specifies the irq number for UART. If the irq
4378 is already allocated, pass 0 to the 7th argument
4379 (<parameter>irq_flags</parameter>). Otherwise, pass the flags
4380 for irq allocation
4381 (<constant>SA_XXX</constant> bits) to it, and the irq will be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004382 reserved by the mpu401-uart layer. If the card doesn't generate
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 UART interrupts, pass -1 as the irq number. Then a timer
4384 interrupt will be invoked for polling.
4385 </para>
4386 </section>
4387
4388 <section id="midi-interface-interrupt-handler">
4389 <title>Interrupt Handler</title>
4390 <para>
4391 When the interrupt is allocated in
4392 <function>snd_mpu401_uart_new()</function>, the private
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004393 interrupt handler is used, hence you don't have anything else to do
4394 than creating the mpu401 stuff. Otherwise, you have to call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 <function>snd_mpu401_uart_interrupt()</function> explicitly when
4396 a UART interrupt is invoked and checked in your own interrupt
4397 handler.
4398 </para>
4399
4400 <para>
4401 In this case, you need to pass the private_data of the
4402 returned rawmidi object from
4403 <function>snd_mpu401_uart_new()</function> as the second
4404 argument of <function>snd_mpu401_uart_interrupt()</function>.
4405
4406 <informalexample>
4407 <programlisting>
4408<![CDATA[
4409 snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);
4410]]>
4411 </programlisting>
4412 </informalexample>
4413 </para>
4414 </section>
4415
4416 </chapter>
4417
4418
4419<!-- ****************************************************** -->
4420<!-- RawMIDI Interface -->
4421<!-- ****************************************************** -->
4422 <chapter id="rawmidi-interface">
4423 <title>RawMIDI Interface</title>
4424
4425 <section id="rawmidi-interface-overview">
4426 <title>Overview</title>
4427
4428 <para>
4429 The raw MIDI interface is used for hardware MIDI ports that can
4430 be accessed as a byte stream. It is not used for synthesizer
4431 chips that do not directly understand MIDI.
4432 </para>
4433
4434 <para>
4435 ALSA handles file and buffer management. All you have to do is
4436 to write some code to move data between the buffer and the
4437 hardware.
4438 </para>
4439
4440 <para>
4441 The rawmidi API is defined in
4442 <filename>&lt;sound/rawmidi.h&gt;</filename>.
4443 </para>
4444 </section>
4445
4446 <section id="rawmidi-interface-constructor">
4447 <title>Constructor</title>
4448
4449 <para>
4450 To create a rawmidi device, call the
4451 <function>snd_rawmidi_new</function> function:
4452 <informalexample>
4453 <programlisting>
4454<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004455 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi);
4457 if (err < 0)
4458 return err;
4459 rmidi->private_data = chip;
4460 strcpy(rmidi->name, "My MIDI");
4461 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
4462 SNDRV_RAWMIDI_INFO_INPUT |
4463 SNDRV_RAWMIDI_INFO_DUPLEX;
4464]]>
4465 </programlisting>
4466 </informalexample>
4467 </para>
4468
4469 <para>
4470 The first argument is the card pointer, the second argument is
4471 the ID string.
4472 </para>
4473
4474 <para>
4475 The third argument is the index of this component. You can
4476 create up to 8 rawmidi devices.
4477 </para>
4478
4479 <para>
4480 The fourth and fifth arguments are the number of output and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004481 input substreams, respectively, of this device (a substream is
4482 the equivalent of a MIDI port).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 </para>
4484
4485 <para>
4486 Set the <structfield>info_flags</structfield> field to specify
4487 the capabilities of the device.
4488 Set <constant>SNDRV_RAWMIDI_INFO_OUTPUT</constant> if there is
4489 at least one output port,
4490 <constant>SNDRV_RAWMIDI_INFO_INPUT</constant> if there is at
4491 least one input port,
4492 and <constant>SNDRV_RAWMIDI_INFO_DUPLEX</constant> if the device
4493 can handle output and input at the same time.
4494 </para>
4495
4496 <para>
4497 After the rawmidi device is created, you need to set the
4498 operators (callbacks) for each substream. There are helper
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004499 functions to set the operators for all the substreams of a device:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 <informalexample>
4501 <programlisting>
4502<![CDATA[
4503 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops);
4504 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);
4505]]>
4506 </programlisting>
4507 </informalexample>
4508 </para>
4509
4510 <para>
4511 The operators are usually defined like this:
4512 <informalexample>
4513 <programlisting>
4514<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004515 static struct snd_rawmidi_ops snd_mymidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 .open = snd_mymidi_output_open,
4517 .close = snd_mymidi_output_close,
4518 .trigger = snd_mymidi_output_trigger,
4519 };
4520]]>
4521 </programlisting>
4522 </informalexample>
4523 These callbacks are explained in the <link
4524 linkend="rawmidi-interface-callbacks"><citetitle>Callbacks</citetitle></link>
4525 section.
4526 </para>
4527
4528 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004529 If there are more than one substream, you should give a
4530 unique name to each of them:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 <informalexample>
4532 <programlisting>
4533<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004534 struct snd_rawmidi_substream *substream;
Takashi Iwai95a5b082007-07-26 16:50:09 +02004535 list_for_each_entry(substream,
4536 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
4537 list {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 sprintf(substream->name, "My MIDI Port %d", substream->number + 1);
4539 }
4540 /* same for SNDRV_RAWMIDI_STREAM_INPUT */
4541]]>
4542 </programlisting>
4543 </informalexample>
4544 </para>
4545 </section>
4546
4547 <section id="rawmidi-interface-callbacks">
4548 <title>Callbacks</title>
4549
4550 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004551 In all the callbacks, the private data that you've set for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 rawmidi device can be accessed as
4553 substream-&gt;rmidi-&gt;private_data.
4554 <!-- <code> isn't available before DocBook 4.3 -->
4555 </para>
4556
4557 <para>
4558 If there is more than one port, your callbacks can determine the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004559 port index from the struct snd_rawmidi_substream data passed to each
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 callback:
4561 <informalexample>
4562 <programlisting>
4563<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004564 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 int index = substream->number;
4566]]>
4567 </programlisting>
4568 </informalexample>
4569 </para>
4570
4571 <section id="rawmidi-interface-op-open">
4572 <title><function>open</function> callback</title>
4573
4574 <informalexample>
4575 <programlisting>
4576<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004577 static int snd_xxx_open(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578]]>
4579 </programlisting>
4580 </informalexample>
4581
4582 <para>
4583 This is called when a substream is opened.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004584 You can initialize the hardware here, but you shouldn't
4585 start transmitting/receiving data yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 </para>
4587 </section>
4588
4589 <section id="rawmidi-interface-op-close">
4590 <title><function>close</function> callback</title>
4591
4592 <informalexample>
4593 <programlisting>
4594<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004595 static int snd_xxx_close(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596]]>
4597 </programlisting>
4598 </informalexample>
4599
4600 <para>
4601 Guess what.
4602 </para>
4603
4604 <para>
4605 The <function>open</function> and <function>close</function>
4606 callbacks of a rawmidi device are serialized with a mutex,
4607 and can sleep.
4608 </para>
4609 </section>
4610
4611 <section id="rawmidi-interface-op-trigger-out">
4612 <title><function>trigger</function> callback for output
4613 substreams</title>
4614
4615 <informalexample>
4616 <programlisting>
4617<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004618 static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619]]>
4620 </programlisting>
4621 </informalexample>
4622
4623 <para>
4624 This is called with a nonzero <parameter>up</parameter>
4625 parameter when there is some data in the substream buffer that
4626 must be transmitted.
4627 </para>
4628
4629 <para>
4630 To read data from the buffer, call
4631 <function>snd_rawmidi_transmit_peek</function>. It will
4632 return the number of bytes that have been read; this will be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004633 less than the number of bytes requested when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 data in the buffer.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004635 After the data have been transmitted successfully, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 <function>snd_rawmidi_transmit_ack</function> to remove the
4637 data from the substream buffer:
4638 <informalexample>
4639 <programlisting>
4640<![CDATA[
4641 unsigned char data;
4642 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004643 if (snd_mychip_try_to_transmit(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 snd_rawmidi_transmit_ack(substream, 1);
4645 else
4646 break; /* hardware FIFO full */
4647 }
4648]]>
4649 </programlisting>
4650 </informalexample>
4651 </para>
4652
4653 <para>
4654 If you know beforehand that the hardware will accept data, you
4655 can use the <function>snd_rawmidi_transmit</function> function
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004656 which reads some data and removes them from the buffer at once:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 <informalexample>
4658 <programlisting>
4659<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004660 while (snd_mychip_transmit_possible()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 unsigned char data;
4662 if (snd_rawmidi_transmit(substream, &data, 1) != 1)
4663 break; /* no more data */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004664 snd_mychip_transmit(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
4666]]>
4667 </programlisting>
4668 </informalexample>
4669 </para>
4670
4671 <para>
4672 If you know beforehand how many bytes you can accept, you can
4673 use a buffer size greater than one with the
4674 <function>snd_rawmidi_transmit*</function> functions.
4675 </para>
4676
4677 <para>
4678 The <function>trigger</function> callback must not sleep. If
4679 the hardware FIFO is full before the substream buffer has been
4680 emptied, you have to continue transmitting data later, either
4681 in an interrupt handler, or with a timer if the hardware
4682 doesn't have a MIDI transmit interrupt.
4683 </para>
4684
4685 <para>
4686 The <function>trigger</function> callback is called with a
4687 zero <parameter>up</parameter> parameter when the transmission
4688 of data should be aborted.
4689 </para>
4690 </section>
4691
4692 <section id="rawmidi-interface-op-trigger-in">
4693 <title><function>trigger</function> callback for input
4694 substreams</title>
4695
4696 <informalexample>
4697 <programlisting>
4698<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004699 static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700]]>
4701 </programlisting>
4702 </informalexample>
4703
4704 <para>
4705 This is called with a nonzero <parameter>up</parameter>
4706 parameter to enable receiving data, or with a zero
4707 <parameter>up</parameter> parameter do disable receiving data.
4708 </para>
4709
4710 <para>
4711 The <function>trigger</function> callback must not sleep; the
4712 actual reading of data from the device is usually done in an
4713 interrupt handler.
4714 </para>
4715
4716 <para>
4717 When data reception is enabled, your interrupt handler should
4718 call <function>snd_rawmidi_receive</function> for all received
4719 data:
4720 <informalexample>
4721 <programlisting>
4722<![CDATA[
4723 void snd_mychip_midi_interrupt(...)
4724 {
4725 while (mychip_midi_available()) {
4726 unsigned char data;
4727 data = mychip_midi_read();
4728 snd_rawmidi_receive(substream, &data, 1);
4729 }
4730 }
4731]]>
4732 </programlisting>
4733 </informalexample>
4734 </para>
4735 </section>
4736
4737 <section id="rawmidi-interface-op-drain">
4738 <title><function>drain</function> callback</title>
4739
4740 <informalexample>
4741 <programlisting>
4742<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004743 static void snd_xxx_drain(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744]]>
4745 </programlisting>
4746 </informalexample>
4747
4748 <para>
4749 This is only used with output substreams. This function should wait
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004750 until all data read from the substream buffer have been transmitted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 This ensures that the device can be closed and the driver unloaded
4752 without losing data.
4753 </para>
4754
4755 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004756 This callback is optional. If you do not set
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004757 <structfield>drain</structfield> in the struct snd_rawmidi_ops
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 structure, ALSA will simply wait for 50&nbsp;milliseconds
4759 instead.
4760 </para>
4761 </section>
4762 </section>
4763
4764 </chapter>
4765
4766
4767<!-- ****************************************************** -->
4768<!-- Miscellaneous Devices -->
4769<!-- ****************************************************** -->
4770 <chapter id="misc-devices">
4771 <title>Miscellaneous Devices</title>
4772
4773 <section id="misc-devices-opl3">
4774 <title>FM OPL3</title>
4775 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004776 The FM OPL3 is still used in many chips (mainly for backward
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 compatibility). ALSA has a nice OPL3 FM control layer, too. The
4778 OPL3 API is defined in
4779 <filename>&lt;sound/opl3.h&gt;</filename>.
4780 </para>
4781
4782 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004783 FM registers can be directly accessed through the direct-FM API,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 defined in <filename>&lt;sound/asound_fm.h&gt;</filename>. In
4785 ALSA native mode, FM registers are accessed through
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004786 the Hardware-Dependant Device direct-FM extension API, whereas in
4787 OSS compatible mode, FM registers can be accessed with the OSS
4788 direct-FM compatible API in <filename>/dev/dmfmX</filename> device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 </para>
4790
4791 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004792 To create the OPL3 component, you have two functions to
4793 call. The first one is a constructor for the <type>opl3_t</type>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 instance.
4795
4796 <informalexample>
4797 <programlisting>
4798<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004799 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX,
4801 integrated, &opl3);
4802]]>
4803 </programlisting>
4804 </informalexample>
4805 </para>
4806
4807 <para>
4808 The first argument is the card pointer, the second one is the
4809 left port address, and the third is the right port address. In
4810 most cases, the right port is placed at the left port + 2.
4811 </para>
4812
4813 <para>
4814 The fourth argument is the hardware type.
4815 </para>
4816
4817 <para>
4818 When the left and right ports have been already allocated by
4819 the card driver, pass non-zero to the fifth argument
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004820 (<parameter>integrated</parameter>). Otherwise, the opl3 module will
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 allocate the specified ports by itself.
4822 </para>
4823
4824 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004825 When the accessing the hardware requires special method
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 instead of the standard I/O access, you can create opl3 instance
4827 separately with <function>snd_opl3_new()</function>.
4828
4829 <informalexample>
4830 <programlisting>
4831<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004832 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);
4834]]>
4835 </programlisting>
4836 </informalexample>
4837 </para>
4838
4839 <para>
4840 Then set <structfield>command</structfield>,
4841 <structfield>private_data</structfield> and
4842 <structfield>private_free</structfield> for the private
4843 access function, the private data and the destructor.
4844 The l_port and r_port are not necessarily set. Only the
4845 command must be set properly. You can retrieve the data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004846 from the opl3-&gt;private_data field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 </para>
4848
4849 <para>
4850 After creating the opl3 instance via <function>snd_opl3_new()</function>,
4851 call <function>snd_opl3_init()</function> to initialize the chip to the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004852 proper state. Note that <function>snd_opl3_create()</function> always
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 calls it internally.
4854 </para>
4855
4856 <para>
4857 If the opl3 instance is created successfully, then create a
4858 hwdep device for this opl3.
4859
4860 <informalexample>
4861 <programlisting>
4862<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004863 struct snd_hwdep *opl3hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);
4865]]>
4866 </programlisting>
4867 </informalexample>
4868 </para>
4869
4870 <para>
4871 The first argument is the <type>opl3_t</type> instance you
4872 created, and the second is the index number, usually 0.
4873 </para>
4874
4875 <para>
4876 The third argument is the index-offset for the sequencer
4877 client assigned to the OPL3 port. When there is an MPU401-UART,
4878 give 1 for here (UART always takes 0).
4879 </para>
4880 </section>
4881
4882 <section id="misc-devices-hardware-dependent">
4883 <title>Hardware-Dependent Devices</title>
4884 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004885 Some chips need user-space access for special
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 controls or for loading the micro code. In such a case, you can
4887 create a hwdep (hardware-dependent) device. The hwdep API is
4888 defined in <filename>&lt;sound/hwdep.h&gt;</filename>. You can
4889 find examples in opl3 driver or
4890 <filename>isa/sb/sb16_csp.c</filename>.
4891 </para>
4892
4893 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004894 The creation of the <type>hwdep</type> instance is done via
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 <function>snd_hwdep_new()</function>.
4896
4897 <informalexample>
4898 <programlisting>
4899<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004900 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 snd_hwdep_new(card, "My HWDEP", 0, &hw);
4902]]>
4903 </programlisting>
4904 </informalexample>
4905
4906 where the third argument is the index number.
4907 </para>
4908
4909 <para>
4910 You can then pass any pointer value to the
4911 <parameter>private_data</parameter>.
4912 If you assign a private data, you should define the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004913 destructor, too. The destructor function is set in
4914 the <structfield>private_free</structfield> field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915
4916 <informalexample>
4917 <programlisting>
4918<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004919 struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 hw->private_data = p;
4921 hw->private_free = mydata_free;
4922]]>
4923 </programlisting>
4924 </informalexample>
4925
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004926 and the implementation of the destructor would be:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927
4928 <informalexample>
4929 <programlisting>
4930<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004931 static void mydata_free(struct snd_hwdep *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004933 struct mydata *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 kfree(p);
4935 }
4936]]>
4937 </programlisting>
4938 </informalexample>
4939 </para>
4940
4941 <para>
4942 The arbitrary file operations can be defined for this
4943 instance. The file operators are defined in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004944 the <parameter>ops</parameter> table. For example, assume that
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 this chip needs an ioctl.
4946
4947 <informalexample>
4948 <programlisting>
4949<![CDATA[
4950 hw->ops.open = mydata_open;
4951 hw->ops.ioctl = mydata_ioctl;
4952 hw->ops.release = mydata_release;
4953]]>
4954 </programlisting>
4955 </informalexample>
4956
4957 And implement the callback functions as you like.
4958 </para>
4959 </section>
4960
4961 <section id="misc-devices-IEC958">
4962 <title>IEC958 (S/PDIF)</title>
4963 <para>
4964 Usually the controls for IEC958 devices are implemented via
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004965 the control interface. There is a macro to compose a name string for
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 IEC958 controls, <function>SNDRV_CTL_NAME_IEC958()</function>
4967 defined in <filename>&lt;include/asound.h&gt;</filename>.
4968 </para>
4969
4970 <para>
4971 There are some standard controls for IEC958 status bits. These
4972 controls use the type <type>SNDRV_CTL_ELEM_TYPE_IEC958</type>,
4973 and the size of element is fixed as 4 bytes array
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004974 (value.iec958.status[x]). For the <structfield>info</structfield>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 callback, you don't specify
4976 the value field for this type (the count field must be set,
4977 though).
4978 </para>
4979
4980 <para>
4981 <quote>IEC958 Playback Con Mask</quote> is used to return the
4982 bit-mask for the IEC958 status bits of consumer mode. Similarly,
4983 <quote>IEC958 Playback Pro Mask</quote> returns the bitmask for
4984 professional mode. They are read-only controls, and are defined
4985 as MIXER controls (iface =
4986 <constant>SNDRV_CTL_ELEM_IFACE_MIXER</constant>).
4987 </para>
4988
4989 <para>
4990 Meanwhile, <quote>IEC958 Playback Default</quote> control is
4991 defined for getting and setting the current default IEC958
4992 bits. Note that this one is usually defined as a PCM control
4993 (iface = <constant>SNDRV_CTL_ELEM_IFACE_PCM</constant>),
4994 although in some places it's defined as a MIXER control.
4995 </para>
4996
4997 <para>
4998 In addition, you can define the control switches to
4999 enable/disable or to set the raw bit mode. The implementation
5000 will depend on the chip, but the control should be named as
5001 <quote>IEC958 xxx</quote>, preferably using
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005002 the <function>SNDRV_CTL_NAME_IEC958()</function> macro.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 </para>
5004
5005 <para>
5006 You can find several cases, for example,
5007 <filename>pci/emu10k1</filename>,
5008 <filename>pci/ice1712</filename>, or
5009 <filename>pci/cmipci.c</filename>.
5010 </para>
5011 </section>
5012
5013 </chapter>
5014
5015
5016<!-- ****************************************************** -->
5017<!-- Buffer and Memory Management -->
5018<!-- ****************************************************** -->
5019 <chapter id="buffer-and-memory">
5020 <title>Buffer and Memory Management</title>
5021
5022 <section id="buffer-and-memory-buffer-types">
5023 <title>Buffer Types</title>
5024 <para>
5025 ALSA provides several different buffer allocation functions
5026 depending on the bus and the architecture. All these have a
5027 consistent API. The allocation of physically-contiguous pages is
5028 done via
5029 <function>snd_malloc_xxx_pages()</function> function, where xxx
5030 is the bus type.
5031 </para>
5032
5033 <para>
5034 The allocation of pages with fallback is
5035 <function>snd_malloc_xxx_pages_fallback()</function>. This
5036 function tries to allocate the specified pages but if the pages
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005037 are not available, it tries to reduce the page sizes until
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 enough space is found.
5039 </para>
5040
5041 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005042 The release the pages, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 <function>snd_free_xxx_pages()</function> function.
5044 </para>
5045
5046 <para>
5047 Usually, ALSA drivers try to allocate and reserve
5048 a large contiguous physical space
5049 at the time the module is loaded for the later use.
5050 This is called <quote>pre-allocation</quote>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005051 As already written, you can call the following function at
5052 pcm instance construction time (in the case of PCI bus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 <informalexample>
5055 <programlisting>
5056<![CDATA[
5057 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
5058 snd_dma_pci_data(pci), size, max);
5059]]>
5060 </programlisting>
5061 </informalexample>
5062
5063 where <parameter>size</parameter> is the byte size to be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005064 pre-allocated and the <parameter>max</parameter> is the maximum
5065 size to be changed via the <filename>prealloc</filename> proc file.
5066 The allocator will try to get an area as large as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 within the given size.
5068 </para>
5069
5070 <para>
5071 The second argument (type) and the third argument (device pointer)
5072 are dependent on the bus.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005073 In the case of the ISA bus, pass <function>snd_dma_isa_data()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 as the third argument with <constant>SNDRV_DMA_TYPE_DEV</constant> type.
5075 For the continuous buffer unrelated to the bus can be pre-allocated
5076 with <constant>SNDRV_DMA_TYPE_CONTINUOUS</constant> type and the
5077 <function>snd_dma_continuous_data(GFP_KERNEL)</function> device pointer,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005078 where <constant>GFP_KERNEL</constant> is the kernel allocation flag to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 use. For the SBUS, <constant>SNDRV_DMA_TYPE_SBUS</constant> and
5080 <function>snd_dma_sbus_data(sbus_dev)</function> are used instead.
5081 For the PCI scatter-gather buffers, use
5082 <constant>SNDRV_DMA_TYPE_DEV_SG</constant> with
5083 <function>snd_dma_pci_data(pci)</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005084 (see the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 <link linkend="buffer-and-memory-non-contiguous"><citetitle>Non-Contiguous Buffers
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005086 </citetitle></link> section).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 </para>
5088
5089 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005090 Once the buffer is pre-allocated, you can use the
5091 allocator in the <structfield>hw_params</structfield> callback:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
5093 <informalexample>
5094 <programlisting>
5095<![CDATA[
5096 snd_pcm_lib_malloc_pages(substream, size);
5097]]>
5098 </programlisting>
5099 </informalexample>
5100
5101 Note that you have to pre-allocate to use this function.
5102 </para>
5103 </section>
5104
5105 <section id="buffer-and-memory-external-hardware">
5106 <title>External Hardware Buffers</title>
5107 <para>
5108 Some chips have their own hardware buffers and the DMA
5109 transfer from the host memory is not available. In such a case,
5110 you need to either 1) copy/set the audio data directly to the
5111 external hardware buffer, or 2) make an intermediate buffer and
5112 copy/set the data from it to the external hardware buffer in
5113 interrupts (or in tasklets, preferably).
5114 </para>
5115
5116 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005117 The first case works fine if the external hardware buffer is large
5118 enough. This method doesn't need any extra buffers and thus is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 more effective. You need to define the
5120 <structfield>copy</structfield> and
5121 <structfield>silence</structfield> callbacks for
5122 the data transfer. However, there is a drawback: it cannot
5123 be mmapped. The examples are GUS's GF1 PCM or emu8000's
5124 wavetable PCM.
5125 </para>
5126
5127 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005128 The second case allows for mmap on the buffer, although you have
5129 to handle an interrupt or a tasklet to transfer the data
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 from the intermediate buffer to the hardware buffer. You can find an
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005131 example in the vxpocket driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 </para>
5133
5134 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005135 Another case is when the chip uses a PCI memory-map
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 region for the buffer instead of the host memory. In this case,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005137 mmap is available only on certain architectures like the Intel one.
5138 In non-mmap mode, the data cannot be transferred as in the normal
5139 way. Thus you need to define the <structfield>copy</structfield> and
5140 <structfield>silence</structfield> callbacks as well,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 as in the cases above. The examples are found in
5142 <filename>rme32.c</filename> and <filename>rme96.c</filename>.
5143 </para>
5144
5145 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005146 The implementation of the <structfield>copy</structfield> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 <structfield>silence</structfield> callbacks depends upon
5148 whether the hardware supports interleaved or non-interleaved
5149 samples. The <structfield>copy</structfield> callback is
5150 defined like below, a bit
5151 differently depending whether the direction is playback or
5152 capture:
5153
5154 <informalexample>
5155 <programlisting>
5156<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005157 static int playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count);
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005159 static int capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count);
5161]]>
5162 </programlisting>
5163 </informalexample>
5164 </para>
5165
5166 <para>
5167 In the case of interleaved samples, the second argument
5168 (<parameter>channel</parameter>) is not used. The third argument
5169 (<parameter>pos</parameter>) points the
5170 current position offset in frames.
5171 </para>
5172
5173 <para>
5174 The meaning of the fourth argument is different between
5175 playback and capture. For playback, it holds the source data
5176 pointer, and for capture, it's the destination data pointer.
5177 </para>
5178
5179 <para>
5180 The last argument is the number of frames to be copied.
5181 </para>
5182
5183 <para>
5184 What you have to do in this callback is again different
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005185 between playback and capture directions. In the
5186 playback case, you copy the given amount of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 (<parameter>count</parameter>) at the specified pointer
5188 (<parameter>src</parameter>) to the specified offset
5189 (<parameter>pos</parameter>) on the hardware buffer. When
5190 coded like memcpy-like way, the copy would be like:
5191
5192 <informalexample>
5193 <programlisting>
5194<![CDATA[
5195 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src,
5196 frames_to_bytes(runtime, count));
5197]]>
5198 </programlisting>
5199 </informalexample>
5200 </para>
5201
5202 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005203 For the capture direction, you copy the given amount of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 data (<parameter>count</parameter>) at the specified offset
5205 (<parameter>pos</parameter>) on the hardware buffer to the
5206 specified pointer (<parameter>dst</parameter>).
5207
5208 <informalexample>
5209 <programlisting>
5210<![CDATA[
5211 my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos),
5212 frames_to_bytes(runtime, count));
5213]]>
5214 </programlisting>
5215 </informalexample>
5216
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005217 Note that both the position and the amount of data are given
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 in frames.
5219 </para>
5220
5221 <para>
5222 In the case of non-interleaved samples, the implementation
5223 will be a bit more complicated.
5224 </para>
5225
5226 <para>
5227 You need to check the channel argument, and if it's -1, copy
5228 the whole channels. Otherwise, you have to copy only the
5229 specified channel. Please check
5230 <filename>isa/gus/gus_pcm.c</filename> as an example.
5231 </para>
5232
5233 <para>
5234 The <structfield>silence</structfield> callback is also
5235 implemented in a similar way.
5236
5237 <informalexample>
5238 <programlisting>
5239<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005240 static int silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
5242]]>
5243 </programlisting>
5244 </informalexample>
5245 </para>
5246
5247 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005248 The meanings of arguments are the same as in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 <structfield>copy</structfield>
5250 callback, although there is no <parameter>src/dst</parameter>
5251 argument. In the case of interleaved samples, the channel
5252 argument has no meaning, as well as on
5253 <structfield>copy</structfield> callback.
5254 </para>
5255
5256 <para>
5257 The role of <structfield>silence</structfield> callback is to
5258 set the given amount
5259 (<parameter>count</parameter>) of silence data at the
5260 specified offset (<parameter>pos</parameter>) on the hardware
5261 buffer. Suppose that the data format is signed (that is, the
5262 silent-data is 0), and the implementation using a memset-like
5263 function would be like:
5264
5265 <informalexample>
5266 <programlisting>
5267<![CDATA[
5268 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0,
5269 frames_to_bytes(runtime, count));
5270]]>
5271 </programlisting>
5272 </informalexample>
5273 </para>
5274
5275 <para>
5276 In the case of non-interleaved samples, again, the
5277 implementation becomes a bit more complicated. See, for example,
5278 <filename>isa/gus/gus_pcm.c</filename>.
5279 </para>
5280 </section>
5281
5282 <section id="buffer-and-memory-non-contiguous">
5283 <title>Non-Contiguous Buffers</title>
5284 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005285 If your hardware supports the page table as in emu10k1 or the
5286 buffer descriptors as in via82xx, you can use the scatter-gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 (SG) DMA. ALSA provides an interface for handling SG-buffers.
5288 The API is provided in <filename>&lt;sound/pcm.h&gt;</filename>.
5289 </para>
5290
5291 <para>
5292 For creating the SG-buffer handler, call
5293 <function>snd_pcm_lib_preallocate_pages()</function> or
5294 <function>snd_pcm_lib_preallocate_pages_for_all()</function>
5295 with <constant>SNDRV_DMA_TYPE_DEV_SG</constant>
5296 in the PCM constructor like other PCI pre-allocator.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005297 You need to pass <function>snd_dma_pci_data(pci)</function>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 where pci is the struct <structname>pci_dev</structname> pointer
5299 of the chip as well.
Giuliano Pochini44275f12006-01-27 12:02:05 +01005300 The <type>struct snd_sg_buf</type> instance is created as
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 substream-&gt;dma_private. You can cast
5302 the pointer like:
5303
5304 <informalexample>
5305 <programlisting>
5306<![CDATA[
Giuliano Pochini44275f12006-01-27 12:02:05 +01005307 struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308]]>
5309 </programlisting>
5310 </informalexample>
5311 </para>
5312
5313 <para>
5314 Then call <function>snd_pcm_lib_malloc_pages()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005315 in the <structfield>hw_params</structfield> callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 as well as in the case of normal PCI buffer.
5317 The SG-buffer handler will allocate the non-contiguous kernel
5318 pages of the given size and map them onto the virtually contiguous
5319 memory. The virtual pointer is addressed in runtime-&gt;dma_area.
5320 The physical address (runtime-&gt;dma_addr) is set to zero,
5321 because the buffer is physically non-contigous.
5322 The physical address table is set up in sgbuf-&gt;table.
5323 You can get the physical address at a certain offset via
5324 <function>snd_pcm_sgbuf_get_addr()</function>.
5325 </para>
5326
5327 <para>
5328 When a SG-handler is used, you need to set
5329 <function>snd_pcm_sgbuf_ops_page</function> as
5330 the <structfield>page</structfield> callback.
5331 (See <link linkend="pcm-interface-operators-page-callback">
5332 <citetitle>page callback section</citetitle></link>.)
5333 </para>
5334
5335 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005336 To release the data, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 <function>snd_pcm_lib_free_pages()</function> in the
5338 <structfield>hw_free</structfield> callback as usual.
5339 </para>
5340 </section>
5341
5342 <section id="buffer-and-memory-vmalloced">
5343 <title>Vmalloc'ed Buffers</title>
5344 <para>
5345 It's possible to use a buffer allocated via
5346 <function>vmalloc</function>, for example, for an intermediate
5347 buffer. Since the allocated pages are not contiguous, you need
5348 to set the <structfield>page</structfield> callback to obtain
5349 the physical address at every offset.
5350 </para>
5351
5352 <para>
5353 The implementation of <structfield>page</structfield> callback
5354 would be like this:
5355
5356 <informalexample>
5357 <programlisting>
5358<![CDATA[
5359 #include <linux/vmalloc.h>
5360
5361 /* get the physical page pointer on the given offset */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005362 static struct page *mychip_page(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 unsigned long offset)
5364 {
5365 void *pageptr = substream->runtime->dma_area + offset;
5366 return vmalloc_to_page(pageptr);
5367 }
5368]]>
5369 </programlisting>
5370 </informalexample>
5371 </para>
5372 </section>
5373
5374 </chapter>
5375
5376
5377<!-- ****************************************************** -->
5378<!-- Proc Interface -->
5379<!-- ****************************************************** -->
5380 <chapter id="proc-interface">
5381 <title>Proc Interface</title>
5382 <para>
5383 ALSA provides an easy interface for procfs. The proc files are
5384 very useful for debugging. I recommend you set up proc files if
5385 you write a driver and want to get a running status or register
5386 dumps. The API is found in
5387 <filename>&lt;sound/info.h&gt;</filename>.
5388 </para>
5389
5390 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005391 To create a proc file, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 <function>snd_card_proc_new()</function>.
5393
5394 <informalexample>
5395 <programlisting>
5396<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005397 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 int err = snd_card_proc_new(card, "my-file", &entry);
5399]]>
5400 </programlisting>
5401 </informalexample>
5402
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005403 where the second argument specifies the name of the proc file to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 created. The above example will create a file
5405 <filename>my-file</filename> under the card directory,
5406 e.g. <filename>/proc/asound/card0/my-file</filename>.
5407 </para>
5408
5409 <para>
5410 Like other components, the proc entry created via
5411 <function>snd_card_proc_new()</function> will be registered and
5412 released automatically in the card registration and release
5413 functions.
5414 </para>
5415
5416 <para>
5417 When the creation is successful, the function stores a new
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005418 instance in the pointer given in the third argument.
5419 It is initialized as a text proc file for read only. To use
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 this proc file as a read-only text file as it is, set the read
5421 callback with a private data via
5422 <function>snd_info_set_text_ops()</function>.
5423
5424 <informalexample>
5425 <programlisting>
5426<![CDATA[
Takashi Iwaibf850202006-04-28 15:13:41 +02005427 snd_info_set_text_ops(entry, chip, my_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428]]>
5429 </programlisting>
5430 </informalexample>
5431
5432 where the second argument (<parameter>chip</parameter>) is the
5433 private data to be used in the callbacks. The third parameter
5434 specifies the read buffer size and the fourth
5435 (<parameter>my_proc_read</parameter>) is the callback function, which
5436 is defined like
5437
5438 <informalexample>
5439 <programlisting>
5440<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005441 static void my_proc_read(struct snd_info_entry *entry,
5442 struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443]]>
5444 </programlisting>
5445 </informalexample>
5446
5447 </para>
5448
5449 <para>
5450 In the read callback, use <function>snd_iprintf()</function> for
5451 output strings, which works just like normal
5452 <function>printf()</function>. For example,
5453
5454 <informalexample>
5455 <programlisting>
5456<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005457 static void my_proc_read(struct snd_info_entry *entry,
5458 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005460 struct my_chip *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
5462 snd_iprintf(buffer, "This is my chip!\n");
5463 snd_iprintf(buffer, "Port = %ld\n", chip->port);
5464 }
5465]]>
5466 </programlisting>
5467 </informalexample>
5468 </para>
5469
5470 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005471 The file permissions can be changed afterwards. As default, it's
5472 set as read only for all users. If you want to add write
5473 permission for the user (root as default), do as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 <informalexample>
5476 <programlisting>
5477<![CDATA[
5478 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
5479]]>
5480 </programlisting>
5481 </informalexample>
5482
5483 and set the write buffer size and the callback
5484
5485 <informalexample>
5486 <programlisting>
5487<![CDATA[
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 entry->c.text.write = my_proc_write;
5489]]>
5490 </programlisting>
5491 </informalexample>
5492 </para>
5493
5494 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 For the write callback, you can use
5496 <function>snd_info_get_line()</function> to get a text line, and
5497 <function>snd_info_get_str()</function> to retrieve a string from
5498 the line. Some examples are found in
5499 <filename>core/oss/mixer_oss.c</filename>, core/oss/and
5500 <filename>pcm_oss.c</filename>.
5501 </para>
5502
5503 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005504 For a raw-data proc-file, set the attributes as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 <informalexample>
5507 <programlisting>
5508<![CDATA[
5509 static struct snd_info_entry_ops my_file_io_ops = {
5510 .read = my_file_io_read,
5511 };
5512
5513 entry->content = SNDRV_INFO_CONTENT_DATA;
5514 entry->private_data = chip;
5515 entry->c.ops = &my_file_io_ops;
5516 entry->size = 4096;
5517 entry->mode = S_IFREG | S_IRUGO;
5518]]>
5519 </programlisting>
5520 </informalexample>
5521 </para>
5522
5523 <para>
5524 The callback is much more complicated than the text-file
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005525 version. You need to use a low-level I/O functions such as
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 <function>copy_from/to_user()</function> to transfer the
5527 data.
5528
5529 <informalexample>
5530 <programlisting>
5531<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005532 static long my_file_io_read(struct snd_info_entry *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 void *file_private_data,
5534 struct file *file,
5535 char *buf,
5536 unsigned long count,
5537 unsigned long pos)
5538 {
5539 long size = count;
5540 if (pos + size > local_max_size)
5541 size = local_max_size - pos;
5542 if (copy_to_user(buf, local_data + pos, size))
5543 return -EFAULT;
5544 return size;
5545 }
5546]]>
5547 </programlisting>
5548 </informalexample>
5549 </para>
5550
5551 </chapter>
5552
5553
5554<!-- ****************************************************** -->
5555<!-- Power Management -->
5556<!-- ****************************************************** -->
5557 <chapter id="power-management">
5558 <title>Power Management</title>
5559 <para>
Paolo Ornati670e9f32006-10-03 22:57:56 +02005560 If the chip is supposed to work with suspend/resume
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005561 functions, you need to add power-management code to the
5562 driver. The additional code for power-management should be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 <function>ifdef</function>'ed with
5564 <constant>CONFIG_PM</constant>.
5565 </para>
5566
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005567 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005568 If the driver <emphasis>fully</emphasis> supports suspend/resume
5569 that is, the device can be
5570 properly resumed to its state when suspend was called,
5571 you can set the <constant>SNDRV_PCM_INFO_RESUME</constant> flag
5572 in the pcm info field. Usually, this is possible when the
5573 registers of the chip can be safely saved and restored to
5574 RAM. If this is set, the trigger callback is called with
5575 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> after the resume
5576 callback completes.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005577 </para>
5578
5579 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005580 Even if the driver doesn't support PM fully but
5581 partial suspend/resume is still possible, it's still worthy to
5582 implement suspend/resume callbacks. In such a case, applications
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005583 would reset the status by calling
5584 <function>snd_pcm_prepare()</function> and restart the stream
5585 appropriately. Hence, you can define suspend/resume callbacks
5586 below but don't set <constant>SNDRV_PCM_INFO_RESUME</constant>
5587 info flag to the PCM.
5588 </para>
5589
5590 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005591 Note that the trigger with SUSPEND can always be called when
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005592 <function>snd_pcm_suspend_all</function> is called,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005593 regardless of the <constant>SNDRV_PCM_INFO_RESUME</constant> flag.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005594 The <constant>RESUME</constant> flag affects only the behavior
5595 of <function>snd_pcm_resume()</function>.
5596 (Thus, in theory,
5597 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> isn't needed
5598 to be handled in the trigger callback when no
5599 <constant>SNDRV_PCM_INFO_RESUME</constant> flag is set. But,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005600 it's better to keep it for compatibility reasons.)
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005601 </para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005603 In the earlier version of ALSA drivers, a common
5604 power-management layer was provided, but it has been removed.
5605 The driver needs to define the suspend/resume hooks according to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005606 the bus the device is connected to. In the case of PCI drivers, the
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005607 callbacks look like below:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608
5609 <informalexample>
5610 <programlisting>
5611<![CDATA[
5612 #ifdef CONFIG_PM
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005613 static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005615 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 return 0;
5617 }
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005618 static int snd_my_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005620 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 return 0;
5622 }
5623 #endif
5624]]>
5625 </programlisting>
5626 </informalexample>
5627 </para>
5628
5629 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005630 The scheme of the real suspend job is as follows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
5632 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005633 <listitem><para>Retrieve the card and the chip data.</para></listitem>
5634 <listitem><para>Call <function>snd_power_change_state()</function> with
5635 <constant>SNDRV_CTL_POWER_D3hot</constant> to change the
5636 power status.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 <listitem><para>Call <function>snd_pcm_suspend_all()</function> to suspend the running PCM streams.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005638 <listitem><para>If AC97 codecs are used, call
Takashi Iwaia7306332006-05-04 11:58:43 +02005639 <function>snd_ac97_suspend()</function> for each codec.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 <listitem><para>Save the register values if necessary.</para></listitem>
5641 <listitem><para>Stop the hardware if necessary.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005642 <listitem><para>Disable the PCI device by calling
5643 <function>pci_disable_device()</function>. Then, call
5644 <function>pci_save_state()</function> at last.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 </orderedlist>
5646 </para>
5647
5648 <para>
5649 A typical code would be like:
5650
5651 <informalexample>
5652 <programlisting>
5653<![CDATA[
Alexey Dobriyan323579882006-01-15 02:12:54 +01005654 static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 {
5656 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005657 struct snd_card *card = pci_get_drvdata(pci);
5658 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005660 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 /* (3) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005662 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 /* (4) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005664 snd_ac97_suspend(chip->ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /* (5) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005666 snd_mychip_save_registers(chip);
5667 /* (6) */
5668 snd_mychip_stop_hardware(chip);
5669 /* (7) */
5670 pci_disable_device(pci);
5671 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 return 0;
5673 }
5674]]>
5675 </programlisting>
5676 </informalexample>
5677 </para>
5678
5679 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005680 The scheme of the real resume job is as follows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005683 <listitem><para>Retrieve the card and the chip data.</para></listitem>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005684 <listitem><para>Set up PCI. First, call <function>pci_restore_state()</function>.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005685 Then enable the pci device again by calling <function>pci_enable_device()</function>.
5686 Call <function>pci_set_master()</function> if necessary, too.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 <listitem><para>Re-initialize the chip.</para></listitem>
5688 <listitem><para>Restore the saved registers if necessary.</para></listitem>
5689 <listitem><para>Resume the mixer, e.g. calling
5690 <function>snd_ac97_resume()</function>.</para></listitem>
5691 <listitem><para>Restart the hardware (if any).</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005692 <listitem><para>Call <function>snd_power_change_state()</function> with
5693 <constant>SNDRV_CTL_POWER_D0</constant> to notify the processes.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 </orderedlist>
5695 </para>
5696
5697 <para>
5698 A typical code would be like:
5699
5700 <informalexample>
5701 <programlisting>
5702<![CDATA[
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005703 static int mychip_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 {
5705 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005706 struct snd_card *card = pci_get_drvdata(pci);
5707 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005709 pci_restore_state(pci);
5710 pci_enable_device(pci);
5711 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 /* (3) */
5713 snd_mychip_reinit_chip(chip);
5714 /* (4) */
5715 snd_mychip_restore_registers(chip);
5716 /* (5) */
5717 snd_ac97_resume(chip->ac97);
5718 /* (6) */
5719 snd_mychip_restart_chip(chip);
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005720 /* (7) */
5721 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 return 0;
5723 }
5724]]>
5725 </programlisting>
5726 </informalexample>
5727 </para>
5728
5729 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005730 As shown in the above, it's better to save registers after
5731 suspending the PCM operations via
5732 <function>snd_pcm_suspend_all()</function> or
5733 <function>snd_pcm_suspend()</function>. It means that the PCM
5734 streams are already stoppped when the register snapshot is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005735 taken. But, remember that you don't have to restart the PCM
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005736 stream in the resume callback. It'll be restarted via
5737 trigger call with <constant>SNDRV_PCM_TRIGGER_RESUME</constant>
5738 when necessary.
5739 </para>
5740
5741 <para>
5742 OK, we have all callbacks now. Let's set them up. In the
5743 initialization of the card, make sure that you can get the chip
5744 data from the card instance, typically via
5745 <structfield>private_data</structfield> field, in case you
5746 created the chip data individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 <informalexample>
5749 <programlisting>
5750<![CDATA[
5751 static int __devinit snd_mychip_probe(struct pci_dev *pci,
5752 const struct pci_device_id *pci_id)
5753 {
5754 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005755 struct snd_card *card;
5756 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 ....
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005758 card = snd_card_new(index[dev], id[dev], THIS_MODULE, NULL);
5759 ....
5760 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
5761 ....
5762 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 ....
5764 }
5765]]>
5766 </programlisting>
5767 </informalexample>
5768
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005769 When you created the chip data with
5770 <function>snd_card_new()</function>, it's anyway accessible
5771 via <structfield>private_data</structfield> field.
5772
5773 <informalexample>
5774 <programlisting>
5775<![CDATA[
5776 static int __devinit snd_mychip_probe(struct pci_dev *pci,
5777 const struct pci_device_id *pci_id)
5778 {
5779 ....
5780 struct snd_card *card;
5781 struct mychip *chip;
5782 ....
5783 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
5784 sizeof(struct mychip));
5785 ....
5786 chip = card->private_data;
5787 ....
5788 }
5789]]>
5790 </programlisting>
5791 </informalexample>
5792
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 </para>
5794
5795 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005796 If you need a space to save the registers, allocate the
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005797 buffer for it here, too, since it would be fatal
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 if you cannot allocate a memory in the suspend phase.
5799 The allocated buffer should be released in the corresponding
5800 destructor.
5801 </para>
5802
5803 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005804 And next, set suspend/resume callbacks to the pci_driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 <informalexample>
5807 <programlisting>
5808<![CDATA[
5809 static struct pci_driver driver = {
5810 .name = "My Chip",
5811 .id_table = snd_my_ids,
5812 .probe = snd_my_probe,
5813 .remove = __devexit_p(snd_my_remove),
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005814 #ifdef CONFIG_PM
5815 .suspend = snd_my_suspend,
5816 .resume = snd_my_resume,
5817 #endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 };
5819]]>
5820 </programlisting>
5821 </informalexample>
5822 </para>
5823
5824 </chapter>
5825
5826
5827<!-- ****************************************************** -->
5828<!-- Module Parameters -->
5829<!-- ****************************************************** -->
5830 <chapter id="module-parameters">
5831 <title>Module Parameters</title>
5832 <para>
5833 There are standard module options for ALSA. At least, each
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005834 module should have the <parameter>index</parameter>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 <parameter>id</parameter> and <parameter>enable</parameter>
5836 options.
5837 </para>
5838
5839 <para>
5840 If the module supports multiple cards (usually up to
5841 8 = <constant>SNDRV_CARDS</constant> cards), they should be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005842 arrays. The default initial values are defined already as
5843 constants for easier programming:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 <informalexample>
5846 <programlisting>
5847<![CDATA[
5848 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
5849 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
5850 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
5851]]>
5852 </programlisting>
5853 </informalexample>
5854 </para>
5855
5856 <para>
5857 If the module supports only a single card, they could be single
5858 variables, instead. <parameter>enable</parameter> option is not
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005859 always necessary in this case, but it would be better to have a
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860 dummy option for compatibility.
5861 </para>
5862
5863 <para>
5864 The module parameters must be declared with the standard
5865 <function>module_param()()</function>,
5866 <function>module_param_array()()</function> and
5867 <function>MODULE_PARM_DESC()</function> macros.
5868 </para>
5869
5870 <para>
5871 The typical coding would be like below:
5872
5873 <informalexample>
5874 <programlisting>
5875<![CDATA[
5876 #define CARD_NAME "My Chip"
5877
5878 module_param_array(index, int, NULL, 0444);
5879 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
5880 module_param_array(id, charp, NULL, 0444);
5881 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
5882 module_param_array(enable, bool, NULL, 0444);
5883 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
5884]]>
5885 </programlisting>
5886 </informalexample>
5887 </para>
5888
5889 <para>
5890 Also, don't forget to define the module description, classes,
5891 license and devices. Especially, the recent modprobe requires to
5892 define the module license as GPL, etc., otherwise the system is
5893 shown as <quote>tainted</quote>.
5894
5895 <informalexample>
5896 <programlisting>
5897<![CDATA[
5898 MODULE_DESCRIPTION("My Chip");
5899 MODULE_LICENSE("GPL");
5900 MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
5901]]>
5902 </programlisting>
5903 </informalexample>
5904 </para>
5905
5906 </chapter>
5907
5908
5909<!-- ****************************************************** -->
5910<!-- How To Put Your Driver -->
5911<!-- ****************************************************** -->
5912 <chapter id="how-to-put-your-driver">
5913 <title>How To Put Your Driver Into ALSA Tree</title>
5914 <section>
5915 <title>General</title>
5916 <para>
5917 So far, you've learned how to write the driver codes.
5918 And you might have a question now: how to put my own
5919 driver into the ALSA driver tree?
5920 Here (finally :) the standard procedure is described briefly.
5921 </para>
5922
5923 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005924 Suppose that you create a new PCI driver for the card
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 <quote>xyz</quote>. The card module name would be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005926 snd-xyz. The new driver is usually put into the alsa-driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 tree, <filename>alsa-driver/pci</filename> directory in
5928 the case of PCI cards.
5929 Then the driver is evaluated, audited and tested
5930 by developers and users. After a certain time, the driver
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005931 will go to the alsa-kernel tree (to the corresponding directory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 such as <filename>alsa-kernel/pci</filename>) and eventually
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005933 will be integrated into the Linux 2.6 tree (the directory would be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 <filename>linux/sound/pci</filename>).
5935 </para>
5936
5937 <para>
5938 In the following sections, the driver code is supposed
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005939 to be put into alsa-driver tree. The two cases are covered:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 a driver consisting of a single source file and one consisting
5941 of several source files.
5942 </para>
5943 </section>
5944
5945 <section>
5946 <title>Driver with A Single Source File</title>
5947 <para>
5948 <orderedlist>
5949 <listitem>
5950 <para>
5951 Modify alsa-driver/pci/Makefile
5952 </para>
5953
5954 <para>
5955 Suppose you have a file xyz.c. Add the following
5956 two lines
5957 <informalexample>
5958 <programlisting>
5959<![CDATA[
5960 snd-xyz-objs := xyz.o
5961 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
5962]]>
5963 </programlisting>
5964 </informalexample>
5965 </para>
5966 </listitem>
5967
5968 <listitem>
5969 <para>
5970 Create the Kconfig entry
5971 </para>
5972
5973 <para>
5974 Add the new entry of Kconfig for your xyz driver.
5975 <informalexample>
5976 <programlisting>
5977<![CDATA[
5978 config SND_XYZ
5979 tristate "Foobar XYZ"
5980 depends on SND
5981 select SND_PCM
5982 help
5983 Say Y here to include support for Foobar XYZ soundcard.
5984
5985 To compile this driver as a module, choose M here: the module
5986 will be called snd-xyz.
5987]]>
5988 </programlisting>
5989 </informalexample>
5990
5991 the line, select SND_PCM, specifies that the driver xyz supports
5992 PCM. In addition to SND_PCM, the following components are
5993 supported for select command:
5994 SND_RAWMIDI, SND_TIMER, SND_HWDEP, SND_MPU401_UART,
5995 SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, SND_AC97_CODEC.
5996 Add the select command for each supported component.
5997 </para>
5998
5999 <para>
6000 Note that some selections imply the lowlevel selections.
6001 For example, PCM includes TIMER, MPU401_UART includes RAWMIDI,
6002 AC97_CODEC includes PCM, and OPL3_LIB includes HWDEP.
6003 You don't need to give the lowlevel selections again.
6004 </para>
6005
6006 <para>
6007 For the details of Kconfig script, refer to the kbuild
6008 documentation.
6009 </para>
6010
6011 </listitem>
6012
6013 <listitem>
6014 <para>
6015 Run cvscompile script to re-generate the configure script and
6016 build the whole stuff again.
6017 </para>
6018 </listitem>
6019 </orderedlist>
6020 </para>
6021 </section>
6022
6023 <section>
6024 <title>Drivers with Several Source Files</title>
6025 <para>
6026 Suppose that the driver snd-xyz have several source files.
6027 They are located in the new subdirectory,
6028 pci/xyz.
6029
6030 <orderedlist>
6031 <listitem>
6032 <para>
6033 Add a new directory (<filename>xyz</filename>) in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006034 <filename>alsa-driver/pci/Makefile</filename> as below
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 <informalexample>
6037 <programlisting>
6038<![CDATA[
6039 obj-$(CONFIG_SND) += xyz/
6040]]>
6041 </programlisting>
6042 </informalexample>
6043 </para>
6044 </listitem>
6045
6046 <listitem>
6047 <para>
6048 Under the directory <filename>xyz</filename>, create a Makefile
6049
6050 <example>
6051 <title>Sample Makefile for a driver xyz</title>
6052 <programlisting>
6053<![CDATA[
6054 ifndef SND_TOPDIR
6055 SND_TOPDIR=../..
6056 endif
6057
6058 include $(SND_TOPDIR)/toplevel.config
6059 include $(SND_TOPDIR)/Makefile.conf
6060
6061 snd-xyz-objs := xyz.o abc.o def.o
6062
6063 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
6064
6065 include $(SND_TOPDIR)/Rules.make
6066]]>
6067 </programlisting>
6068 </example>
6069 </para>
6070 </listitem>
6071
6072 <listitem>
6073 <para>
6074 Create the Kconfig entry
6075 </para>
6076
6077 <para>
6078 This procedure is as same as in the last section.
6079 </para>
6080 </listitem>
6081
6082 <listitem>
6083 <para>
6084 Run cvscompile script to re-generate the configure script and
6085 build the whole stuff again.
6086 </para>
6087 </listitem>
6088 </orderedlist>
6089 </para>
6090 </section>
6091
6092 </chapter>
6093
6094<!-- ****************************************************** -->
6095<!-- Useful Functions -->
6096<!-- ****************************************************** -->
6097 <chapter id="useful-functions">
6098 <title>Useful Functions</title>
6099
6100 <section id="useful-functions-snd-printk">
6101 <title><function>snd_printk()</function> and friends</title>
6102 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006103 ALSA provides a verbose version of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 <function>printk()</function> function. If a kernel config
6105 <constant>CONFIG_SND_VERBOSE_PRINTK</constant> is set, this
6106 function prints the given message together with the file name
6107 and the line of the caller. The <constant>KERN_XXX</constant>
6108 prefix is processed as
6109 well as the original <function>printk()</function> does, so it's
6110 recommended to add this prefix, e.g.
6111
6112 <informalexample>
6113 <programlisting>
6114<![CDATA[
6115 snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\n");
6116]]>
6117 </programlisting>
6118 </informalexample>
6119 </para>
6120
6121 <para>
6122 There are also <function>printk()</function>'s for
6123 debugging. <function>snd_printd()</function> can be used for
6124 general debugging purposes. If
6125 <constant>CONFIG_SND_DEBUG</constant> is set, this function is
6126 compiled, and works just like
6127 <function>snd_printk()</function>. If the ALSA is compiled
6128 without the debugging flag, it's ignored.
6129 </para>
6130
6131 <para>
6132 <function>snd_printdd()</function> is compiled in only when
6133 <constant>CONFIG_SND_DEBUG_DETECT</constant> is set. Please note
6134 that <constant>DEBUG_DETECT</constant> is not set as default
6135 even if you configure the alsa-driver with
6136 <option>--with-debug=full</option> option. You need to give
6137 explicitly <option>--with-debug=detect</option> option instead.
6138 </para>
6139 </section>
6140
6141 <section id="useful-functions-snd-assert">
6142 <title><function>snd_assert()</function></title>
6143 <para>
6144 <function>snd_assert()</function> macro is similar with the
6145 normal <function>assert()</function> macro. For example,
6146
6147 <informalexample>
6148 <programlisting>
6149<![CDATA[
6150 snd_assert(pointer != NULL, return -EINVAL);
6151]]>
6152 </programlisting>
6153 </informalexample>
6154 </para>
6155
6156 <para>
6157 The first argument is the expression to evaluate, and the
6158 second argument is the action if it fails. When
6159 <constant>CONFIG_SND_DEBUG</constant>, is set, it will show an
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006160 error message such as <computeroutput>BUG? (xxx)</computeroutput>
6161 together with stack trace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 </para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 <para>
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006164 When no debug flag is set, this macro is ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 </para>
6166 </section>
6167
6168 <section id="useful-functions-snd-bug">
6169 <title><function>snd_BUG()</function></title>
6170 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006171 It shows the <computeroutput>BUG?</computeroutput> message and
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006172 stack trace as well as <function>snd_assert</function> at the point.
6173 It's useful to show that a fatal error happens there.
6174 </para>
6175 <para>
6176 When no debug flag is set, this macro is ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 </para>
6178 </section>
6179 </chapter>
6180
6181
6182<!-- ****************************************************** -->
6183<!-- Acknowledgments -->
6184<!-- ****************************************************** -->
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01006185 <chapter id="acknowledgments">
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 <title>Acknowledgments</title>
6187 <para>
6188 I would like to thank Phil Kerr for his help for improvement and
6189 corrections of this document.
6190 </para>
6191 <para>
6192 Kevin Conder reformatted the original plain-text to the
6193 DocBook format.
6194 </para>
6195 <para>
6196 Giuliano Pochini corrected typos and contributed the example codes
6197 in the hardware constraints section.
6198 </para>
6199 </chapter>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200</book>