blob: c0781bb1f9b59d37f2c25536cc0465e6cf675c48 [file] [log] [blame]
Takashi Iwai5f8206c2009-02-09 08:50:43 +01001<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5<!-- ****************************************************** -->
6<!-- Header -->
7<!-- ****************************************************** -->
Takashi Iwai5f8206c2009-02-09 08:50:43 +01008<book id="Writing-an-ALSA-Driver">
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 <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[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 #include <linux/init.h>
398 #include <linux/pci.h>
399 #include <linux/slab.h>
400 #include <sound/core.h>
401 #include <sound/initval.h>
402
403 /* module parameters (see "Module Parameters") */
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200404 /* SNDRV_CARDS: maximum number of cards supported by this module */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
406 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +1030407 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* definition of the chip-specific record */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100410 struct mychip {
411 struct snd_card *card;
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200412 /* the rest of the implementation will be in section
413 * "PCI Resource Management"
Takashi Iwai95a5b082007-07-26 16:50:09 +0200414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 };
416
417 /* chip-specific destructor
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200418 * (see "PCI Resource Management")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100420 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 {
Takashi Iwai95a5b082007-07-26 16:50:09 +0200422 .... /* will be implemented later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
425 /* component-destructor
426 * (see "Management of Cards and Components")
427 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100428 static int snd_mychip_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100430 return snd_mychip_free(device->device_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 /* chip-specific constructor
434 * (see "Management of Cards and Components")
435 */
Takashi Iwai090015a2012-12-07 07:52:28 +0100436 static int snd_mychip_create(struct snd_card *card,
437 struct pci_dev *pci,
438 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100440 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100442 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .dev_free = snd_mychip_dev_free,
444 };
445
446 *rchip = NULL;
447
Takashi Iwai95a5b082007-07-26 16:50:09 +0200448 /* check PCI availability here
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200449 * (see "PCI Resource Management")
Takashi Iwai95a5b082007-07-26 16:50:09 +0200450 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 ....
452
453 /* allocate a chip-specific data with zero filled */
Takashi Iwai561b2202005-09-09 14:22:34 +0200454 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (chip == NULL)
456 return -ENOMEM;
457
458 chip->card = card;
459
Takashi Iwai95a5b082007-07-26 16:50:09 +0200460 /* rest of initialization here; will be implemented
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200461 * later, see "PCI Resource Management"
Takashi Iwai95a5b082007-07-26 16:50:09 +0200462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 ....
464
Takashi Iwai95a5b082007-07-26 16:50:09 +0200465 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
466 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 snd_mychip_free(chip);
468 return err;
469 }
470
471 snd_card_set_dev(card, &pci->dev);
472
473 *rchip = chip;
474 return 0;
475 }
476
477 /* constructor -- see "Constructor" sub-section */
Takashi Iwai090015a2012-12-07 07:52:28 +0100478 static int snd_mychip_probe(struct pci_dev *pci,
479 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 {
481 static int dev;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100482 struct snd_card *card;
483 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int err;
485
486 /* (1) */
487 if (dev >= SNDRV_CARDS)
488 return -ENODEV;
489 if (!enable[dev]) {
490 dev++;
491 return -ENOENT;
492 }
493
494 /* (2) */
Takashi Iwaid4533792008-12-28 16:45:34 +0100495 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
496 if (err < 0)
497 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 /* (3) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200500 err = snd_mychip_create(card, pci, &chip);
501 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 snd_card_free(card);
503 return err;
504 }
505
506 /* (4) */
507 strcpy(card->driver, "My Chip");
508 strcpy(card->shortname, "My Own Chip 123");
509 sprintf(card->longname, "%s at 0x%lx irq %i",
510 card->shortname, chip->ioport, chip->irq);
511
512 /* (5) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200513 .... /* implemented later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 /* (6) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200516 err = snd_card_register(card);
517 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 snd_card_free(card);
519 return err;
520 }
521
522 /* (7) */
523 pci_set_drvdata(pci, card);
524 dev++;
525 return 0;
526 }
527
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200528 /* destructor -- see the "Destructor" sub-section */
Takashi Iwai090015a2012-12-07 07:52:28 +0100529 static void snd_mychip_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 {
531 snd_card_free(pci_get_drvdata(pci));
532 pci_set_drvdata(pci, NULL);
533 }
534]]>
535 </programlisting>
536 </example>
537 </para>
538 </section>
539
540 <section id="basic-flow-constructor">
541 <title>Constructor</title>
542 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200543 The real constructor of PCI drivers is the <function>probe</function> callback.
544 The <function>probe</function> callback and other component-constructors which are called
Takashi Iwai090015a2012-12-07 07:52:28 +0100545 from the <function>probe</function> callback cannot be used with
546 the <parameter>__init</parameter> prefix
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 because any PCI device could be a hotplug device.
548 </para>
549
550 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200551 In the <function>probe</function> callback, the following scheme is often used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 </para>
553
554 <section id="basic-flow-constructor-device-index">
555 <title>1) Check and increment the device index.</title>
556 <para>
557 <informalexample>
558 <programlisting>
559<![CDATA[
560 static int dev;
561 ....
562 if (dev >= SNDRV_CARDS)
563 return -ENODEV;
564 if (!enable[dev]) {
565 dev++;
566 return -ENOENT;
567 }
568]]>
569 </programlisting>
570 </informalexample>
571
572 where enable[dev] is the module option.
573 </para>
574
575 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200576 Each time the <function>probe</function> callback is called, check the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 availability of the device. If not available, simply increment
578 the device index and returns. dev will be incremented also
579 later (<link
580 linkend="basic-flow-constructor-set-pci"><citetitle>step
581 7</citetitle></link>).
582 </para>
583 </section>
584
585 <section id="basic-flow-constructor-create-card">
586 <title>2) Create a card instance</title>
587 <para>
588 <informalexample>
589 <programlisting>
590<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100591 struct snd_card *card;
Takashi Iwaid4533792008-12-28 16:45:34 +0100592 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ....
Takashi Iwaid4533792008-12-28 16:45:34 +0100594 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595]]>
596 </programlisting>
597 </informalexample>
598 </para>
599
600 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200601 The details will be explained in the section
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 <link linkend="card-management-card-instance"><citetitle>
603 Management of Cards and Components</citetitle></link>.
604 </para>
605 </section>
606
607 <section id="basic-flow-constructor-create-main">
608 <title>3) Create a main component</title>
609 <para>
610 In this part, the PCI resources are allocated.
611
612 <informalexample>
613 <programlisting>
614<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100615 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +0200617 err = snd_mychip_create(card, pci, &chip);
618 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 snd_card_free(card);
620 return err;
621 }
622]]>
623 </programlisting>
624 </informalexample>
625
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200626 The details will be explained in the section <link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 linkend="pci-resource"><citetitle>PCI Resource
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200628 Management</citetitle></link>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 </para>
630 </section>
631
632 <section id="basic-flow-constructor-main-component">
633 <title>4) Set the driver ID and name strings.</title>
634 <para>
635 <informalexample>
636 <programlisting>
637<![CDATA[
638 strcpy(card->driver, "My Chip");
639 strcpy(card->shortname, "My Own Chip 123");
640 sprintf(card->longname, "%s at 0x%lx irq %i",
641 card->shortname, chip->ioport, chip->irq);
642]]>
643 </programlisting>
644 </informalexample>
645
646 The driver field holds the minimal ID string of the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200647 chip. This is used by alsa-lib's configurator, so keep it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 simple but unique.
649 Even the same driver can have different driver IDs to
650 distinguish the functionality of each chip type.
651 </para>
652
653 <para>
654 The shortname field is a string shown as more verbose
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200655 name. The longname field contains the information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 shown in <filename>/proc/asound/cards</filename>.
657 </para>
658 </section>
659
660 <section id="basic-flow-constructor-create-other">
661 <title>5) Create other components, such as mixer, MIDI, etc.</title>
662 <para>
663 Here you define the basic components such as
664 <link linkend="pcm-interface"><citetitle>PCM</citetitle></link>,
665 mixer (e.g. <link linkend="api-ac97"><citetitle>AC97</citetitle></link>),
666 MIDI (e.g. <link linkend="midi-interface"><citetitle>MPU-401</citetitle></link>),
667 and other interfaces.
668 Also, if you want a <link linkend="proc-interface"><citetitle>proc
669 file</citetitle></link>, define it here, too.
670 </para>
671 </section>
672
673 <section id="basic-flow-constructor-register-card">
674 <title>6) Register the card instance.</title>
675 <para>
676 <informalexample>
677 <programlisting>
678<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +0200679 err = snd_card_register(card);
680 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 snd_card_free(card);
682 return err;
683 }
684]]>
685 </programlisting>
686 </informalexample>
687 </para>
688
689 <para>
690 Will be explained in the section <link
691 linkend="card-management-registration"><citetitle>Management
692 of Cards and Components</citetitle></link>, too.
693 </para>
694 </section>
695
696 <section id="basic-flow-constructor-set-pci">
697 <title>7) Set the PCI driver data and return zero.</title>
698 <para>
699 <informalexample>
700 <programlisting>
701<![CDATA[
702 pci_set_drvdata(pci, card);
703 dev++;
704 return 0;
705]]>
706 </programlisting>
707 </informalexample>
708
709 In the above, the card record is stored. This pointer is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200710 used in the remove callback and power-management
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 callbacks, too.
712 </para>
713 </section>
714 </section>
715
716 <section id="basic-flow-destructor">
717 <title>Destructor</title>
718 <para>
719 The destructor, remove callback, simply releases the card
720 instance. Then the ALSA middle layer will release all the
721 attached components automatically.
722 </para>
723
724 <para>
725 It would be typically like the following:
726
727 <informalexample>
728 <programlisting>
729<![CDATA[
Takashi Iwai090015a2012-12-07 07:52:28 +0100730 static void snd_mychip_remove(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 {
732 snd_card_free(pci_get_drvdata(pci));
733 pci_set_drvdata(pci, NULL);
734 }
735]]>
736 </programlisting>
737 </informalexample>
738
739 The above code assumes that the card pointer is set to the PCI
740 driver data.
741 </para>
742 </section>
743
744 <section id="basic-flow-header-files">
745 <title>Header Files</title>
746 <para>
747 For the above example, at least the following include files
748 are necessary.
749
750 <informalexample>
751 <programlisting>
752<![CDATA[
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 #include <linux/init.h>
754 #include <linux/pci.h>
755 #include <linux/slab.h>
756 #include <sound/core.h>
757 #include <sound/initval.h>
758]]>
759 </programlisting>
760 </informalexample>
761
762 where the last one is necessary only when module options are
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200763 defined in the source file. If the code is split into several
764 files, the files without module options don't need them.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 </para>
766
767 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200768 In addition to these headers, you'll need
769 <filename>&lt;linux/interrupt.h&gt;</filename> for interrupt
770 handling, and <filename>&lt;asm/io.h&gt;</filename> for I/O
771 access. If you use the <function>mdelay()</function> or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 <function>udelay()</function> functions, you'll need to include
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200773 <filename>&lt;linux/delay.h&gt;</filename> too.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 </para>
775
776 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200777 The ALSA interfaces like the PCM and control APIs are defined in other
778 <filename>&lt;sound/xxx.h&gt;</filename> header files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 They have to be included after
780 <filename>&lt;sound/core.h&gt;</filename>.
781 </para>
782
783 </section>
784 </chapter>
785
786
787<!-- ****************************************************** -->
788<!-- Management of Cards and Components -->
789<!-- ****************************************************** -->
790 <chapter id="card-management">
791 <title>Management of Cards and Components</title>
792
793 <section id="card-management-card-instance">
794 <title>Card Instance</title>
795 <para>
796 For each soundcard, a <quote>card</quote> record must be allocated.
797 </para>
798
799 <para>
800 A card record is the headquarters of the soundcard. It manages
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200801 the whole list of devices (components) on the soundcard, such as
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 PCM, mixers, MIDI, synthesizer, and so on. Also, the card
803 record holds the ID and the name strings of the card, manages
804 the root of proc files, and controls the power-management states
805 and hotplug disconnections. The component list on the card
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200806 record is used to manage the correct release of resources at
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 destruction.
808 </para>
809
810 <para>
811 As mentioned above, to create a card instance, call
Takashi Iwaid4533792008-12-28 16:45:34 +0100812 <function>snd_card_create()</function>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 <informalexample>
815 <programlisting>
816<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100817 struct snd_card *card;
Takashi Iwaid4533792008-12-28 16:45:34 +0100818 int err;
819 err = snd_card_create(index, id, module, extra_size, &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820]]>
821 </programlisting>
822 </informalexample>
823 </para>
824
825 <para>
Takashi Iwaid4533792008-12-28 16:45:34 +0100826 The function takes five arguments, the card-index number, the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 id string, the module pointer (usually
828 <constant>THIS_MODULE</constant>),
Takashi Iwaid4533792008-12-28 16:45:34 +0100829 the size of extra-data space, and the pointer to return the
830 card instance. The extra_size argument is used to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 allocate card-&gt;private_data for the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200832 chip-specific data. Note that these data
Takashi Iwaid4533792008-12-28 16:45:34 +0100833 are allocated by <function>snd_card_create()</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
Antonio Ospite04044b82013-01-29 12:56:27 +0100874 as the argument. This pointer (<parameter>chip</parameter> in the
875 above example) is used as the identifier for the instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 </para>
877
878 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200879 Each pre-defined ALSA component such as ac97 and pcm calls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 <function>snd_device_new()</function> inside its
881 constructor. The destructor for each component is defined in the
882 callback pointers. Hence, you don't need to take care of
883 calling a destructor for such a component.
884 </para>
885
886 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200887 If you wish to create your own component, you need to
888 set the destructor function to the dev_free callback in
889 the <parameter>ops</parameter>, so that it can be released
890 automatically via <function>snd_card_free()</function>.
891 The next example will show an implementation of chip-specific
892 data.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 </para>
894 </section>
895
896 <section id="card-management-chip-specific">
897 <title>Chip-Specific Data</title>
898 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200899 Chip-specific information, e.g. the I/O port address, its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 resource pointer, or the irq number, is stored in the
901 chip-specific record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 <informalexample>
904 <programlisting>
905<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100906 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 ....
908 };
909]]>
910 </programlisting>
911 </informalexample>
912 </para>
913
914 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200915 In general, there are two ways of allocating the chip record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 </para>
917
918 <section id="card-management-chip-specific-snd-card-new">
Takashi Iwaid4533792008-12-28 16:45:34 +0100919 <title>1. Allocating via <function>snd_card_create()</function>.</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +0200921 As mentioned above, you can pass the extra-data-length
Takashi Iwaid4533792008-12-28 16:45:34 +0100922 to the 4th argument of <function>snd_card_create()</function>, i.e.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 <informalexample>
925 <programlisting>
926<![CDATA[
Takashi Iwaid4533792008-12-28 16:45:34 +0100927 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
928 sizeof(struct mychip), &card);
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
Takashi Iwaid4533792008-12-28 16:45:34 +0100957 <function>snd_card_create()</function> (with
958 <constant>0</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;
Takashi Iwaid4533792008-12-28 16:45:34 +0100966 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 .....
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 For a device which allows hotplugging, you can use
Takashi Iwai2b29b132006-06-23 14:38:26 +02001061 <function>snd_card_free_when_closed</function>. This one will
1062 postpone the destruction until all devices are closed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 </para>
1064
1065 </section>
1066
1067 </chapter>
1068
1069
1070<!-- ****************************************************** -->
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001071<!-- PCI Resource Management -->
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072<!-- ****************************************************** -->
1073 <chapter id="pci-resource">
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001074 <title>PCI Resource Management</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 <section id="pci-resource-example">
1077 <title>Full Code Example</title>
1078 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001079 In this section, we'll complete the chip-specific constructor,
1080 destructor and PCI entries. Example code is shown first,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 below.
1082
1083 <example>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001084 <title>PCI Resource Management Example</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 <programlisting>
1086<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001087 struct mychip {
1088 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct pci_dev *pci;
1090
1091 unsigned long port;
1092 int irq;
1093 };
1094
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001095 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 {
1097 /* disable hardware here if any */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001098 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 /* release the irq */
1101 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001102 free_irq(chip->irq, chip);
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001103 /* release the I/O ports & memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 pci_release_regions(chip->pci);
1105 /* disable the PCI entry */
1106 pci_disable_device(chip->pci);
1107 /* release the data */
1108 kfree(chip);
1109 return 0;
1110 }
1111
1112 /* chip-specific constructor */
Takashi Iwai090015a2012-12-07 07:52:28 +01001113 static int snd_mychip_create(struct snd_card *card,
1114 struct pci_dev *pci,
1115 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001117 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001119 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 .dev_free = snd_mychip_dev_free,
1121 };
1122
1123 *rchip = NULL;
1124
1125 /* initialize the PCI entry */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001126 err = pci_enable_device(pci);
1127 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return err;
1129 /* check PCI availability (28bit DMA) */
Yang Hongyang2c5510d2009-04-06 19:01:19 -07001130 if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
1131 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 printk(KERN_ERR "error to set 28bit mask DMA\n");
1133 pci_disable_device(pci);
1134 return -ENXIO;
1135 }
1136
Takashi Iwai561b2202005-09-09 14:22:34 +02001137 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (chip == NULL) {
1139 pci_disable_device(pci);
1140 return -ENOMEM;
1141 }
1142
1143 /* initialize the stuff */
1144 chip->card = card;
1145 chip->pci = pci;
1146 chip->irq = -1;
1147
1148 /* (1) PCI resource allocation */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001149 err = pci_request_regions(pci, "My Chip");
1150 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 kfree(chip);
1152 pci_disable_device(pci);
1153 return err;
1154 }
1155 chip->port = pci_resource_start(pci, 0);
1156 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwaice1fd362011-06-14 08:54:14 +02001157 IRQF_SHARED, KBUILD_MODNAME, chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1159 snd_mychip_free(chip);
1160 return -EBUSY;
1161 }
1162 chip->irq = pci->irq;
1163
1164 /* (2) initialization of the chip hardware */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001165 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Takashi Iwai95a5b082007-07-26 16:50:09 +02001167 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1168 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 snd_mychip_free(chip);
1170 return err;
1171 }
1172
1173 snd_card_set_dev(card, &pci->dev);
1174
1175 *rchip = chip;
1176 return 0;
1177 }
1178
1179 /* PCI IDs */
Takashi Iwaif40b6892006-07-05 16:51:05 +02001180 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1182 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1183 ....
1184 { 0, }
1185 };
1186 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1187
1188 /* pci_driver definition */
1189 static struct pci_driver driver = {
Takashi Iwaice1fd362011-06-14 08:54:14 +02001190 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 .id_table = snd_mychip_ids,
1192 .probe = snd_mychip_probe,
Takashi Iwai090015a2012-12-07 07:52:28 +01001193 .remove = snd_mychip_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 };
1195
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001196 /* module initialization */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 static int __init alsa_card_mychip_init(void)
1198 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001199 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001202 /* module clean up */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 static void __exit alsa_card_mychip_exit(void)
1204 {
1205 pci_unregister_driver(&driver);
1206 }
1207
1208 module_init(alsa_card_mychip_init)
1209 module_exit(alsa_card_mychip_exit)
1210
1211 EXPORT_NO_SYMBOLS; /* for old kernels only */
1212]]>
1213 </programlisting>
1214 </example>
1215 </para>
1216 </section>
1217
1218 <section id="pci-resource-some-haftas">
1219 <title>Some Hafta's</title>
1220 <para>
1221 The allocation of PCI resources is done in the
1222 <function>probe()</function> function, and usually an extra
1223 <function>xxx_create()</function> function is written for this
Tobias Klauser56b146d2006-04-10 22:54:21 -07001224 purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 </para>
1226
1227 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001228 In the case of PCI devices, you first have to call
1229 the <function>pci_enable_device()</function> function before
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 allocating resources. Also, you need to set the proper PCI DMA
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001231 mask to limit the accessed I/O range. In some cases, you might
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 need to call <function>pci_set_master()</function> function,
Tobias Klauser56b146d2006-04-10 22:54:21 -07001233 too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 </para>
1235
1236 <para>
1237 Suppose the 28bit mask, and the code to be added would be like:
1238
1239 <informalexample>
1240 <programlisting>
1241<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001242 err = pci_enable_device(pci);
1243 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return err;
Yang Hongyang2c5510d2009-04-06 19:01:19 -07001245 if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
1246 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 printk(KERN_ERR "error to set 28bit mask DMA\n");
1248 pci_disable_device(pci);
1249 return -ENXIO;
1250 }
1251
1252]]>
1253 </programlisting>
1254 </informalexample>
1255 </para>
1256 </section>
1257
1258 <section id="pci-resource-resource-allocation">
1259 <title>Resource Allocation</title>
1260 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001261 The allocation of I/O ports and irqs is done via standard kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 functions. Unlike ALSA ver.0.5.x., there are no helpers for
1263 that. And these resources must be released in the destructor
1264 function (see below). Also, on ALSA 0.9.x, you don't need to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001265 allocate (pseudo-)DMA for PCI like in ALSA 0.5.x.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 </para>
1267
1268 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001269 Now assume that the PCI device has an I/O port with 8 bytes
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001270 and an interrupt. Then struct <structname>mychip</structname> will have the
Tobias Klauser56b146d2006-04-10 22:54:21 -07001271 following fields:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 <informalexample>
1274 <programlisting>
1275<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001276 struct mychip {
1277 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 unsigned long port;
1280 int irq;
1281 };
1282]]>
1283 </programlisting>
1284 </informalexample>
1285 </para>
1286
1287 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001288 For an I/O port (and also a memory region), you need to have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 the resource pointer for the standard resource management. For
1290 an irq, you have to keep only the irq number (integer). But you
1291 need to initialize this number as -1 before actual allocation,
1292 since irq 0 is valid. The port address and its resource pointer
1293 can be initialized as null by
Takashi Iwai561b2202005-09-09 14:22:34 +02001294 <function>kzalloc()</function> automatically, so you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 don't have to take care of resetting them.
1296 </para>
1297
1298 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001299 The allocation of an I/O port is done like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 <informalexample>
1302 <programlisting>
1303<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001304 err = pci_request_regions(pci, "My Chip");
1305 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(chip);
1307 pci_disable_device(pci);
1308 return err;
1309 }
1310 chip->port = pci_resource_start(pci, 0);
1311]]>
1312 </programlisting>
1313 </informalexample>
1314 </para>
1315
1316 <para>
1317 <!-- obsolete -->
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001318 It will reserve the I/O port region of 8 bytes of the given
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 PCI device. The returned value, chip-&gt;res_port, is allocated
1320 via <function>kmalloc()</function> by
1321 <function>request_region()</function>. The pointer must be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001322 released via <function>kfree()</function>, but there is a
1323 problem with this. This issue will be explained later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 </para>
1325
1326 <para>
1327 The allocation of an interrupt source is done like this:
1328
1329 <informalexample>
1330 <programlisting>
1331<![CDATA[
1332 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwaice1fd362011-06-14 08:54:14 +02001333 IRQF_SHARED, KBUILD_MODNAME, chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1335 snd_mychip_free(chip);
1336 return -EBUSY;
1337 }
1338 chip->irq = pci->irq;
1339]]>
1340 </programlisting>
1341 </informalexample>
1342
1343 where <function>snd_mychip_interrupt()</function> is the
1344 interrupt handler defined <link
1345 linkend="pcm-interface-interrupt-handler"><citetitle>later</citetitle></link>.
1346 Note that chip-&gt;irq should be defined
1347 only when <function>request_irq()</function> succeeded.
1348 </para>
1349
1350 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001351 On the PCI bus, interrupts can be shared. Thus,
1352 <constant>IRQF_SHARED</constant> is used as the interrupt flag of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 <function>request_irq()</function>.
1354 </para>
1355
1356 <para>
1357 The last argument of <function>request_irq()</function> is the
1358 data pointer passed to the interrupt handler. Usually, the
1359 chip-specific record is used for that, but you can use what you
1360 like, too.
1361 </para>
1362
1363 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001364 I won't give details about the interrupt handler at this
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 point, but at least its appearance can be explained now. The
1366 interrupt handler looks usually like the following:
1367
1368 <informalexample>
1369 <programlisting>
1370<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01001371 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001373 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 ....
1375 return IRQ_HANDLED;
1376 }
1377]]>
1378 </programlisting>
1379 </informalexample>
1380 </para>
1381
1382 <para>
1383 Now let's write the corresponding destructor for the resources
1384 above. The role of destructor is simple: disable the hardware
1385 (if already activated) and release the resources. So far, we
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001386 have no hardware part, so the disabling code is not written here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 </para>
1388
1389 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001390 To release the resources, the <quote>check-and-release</quote>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 method is a safer way. For the interrupt, do like this:
1392
1393 <informalexample>
1394 <programlisting>
1395<![CDATA[
1396 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001397 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398]]>
1399 </programlisting>
1400 </informalexample>
1401
1402 Since the irq number can start from 0, you should initialize
1403 chip-&gt;irq with a negative value (e.g. -1), so that you can
1404 check the validity of the irq number as above.
1405 </para>
1406
1407 <para>
1408 When you requested I/O ports or memory regions via
1409 <function>pci_request_region()</function> or
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001410 <function>pci_request_regions()</function> like in this example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 release the resource(s) using the corresponding function,
1412 <function>pci_release_region()</function> or
1413 <function>pci_release_regions()</function>.
1414
1415 <informalexample>
1416 <programlisting>
1417<![CDATA[
1418 pci_release_regions(chip->pci);
1419]]>
1420 </programlisting>
1421 </informalexample>
1422 </para>
1423
1424 <para>
1425 When you requested manually via <function>request_region()</function>
1426 or <function>request_mem_region</function>, you can release it via
1427 <function>release_resource()</function>. Suppose that you keep
1428 the resource pointer returned from <function>request_region()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001429 in chip-&gt;res_port, the release procedure looks like:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 <informalexample>
1432 <programlisting>
1433<![CDATA[
Takashi Iwaib1d57762005-10-10 11:56:31 +02001434 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435]]>
1436 </programlisting>
1437 </informalexample>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 </para>
1439
1440 <para>
1441 Don't forget to call <function>pci_disable_device()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001442 before the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 </para>
1444
1445 <para>
1446 And finally, release the chip-specific record.
1447
1448 <informalexample>
1449 <programlisting>
1450<![CDATA[
1451 kfree(chip);
1452]]>
1453 </programlisting>
1454 </informalexample>
1455 </para>
1456
1457 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001458 We didn't implement the hardware disabling part in the above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 If you need to do this, please note that the destructor may be
1460 called even before the initialization of the chip is completed.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001461 It would be better to have a flag to skip hardware disabling
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if the hardware was not initialized yet.
1463 </para>
1464
1465 <para>
1466 When the chip-data is assigned to the card using
1467 <function>snd_device_new()</function> with
1468 <constant>SNDRV_DEV_LOWLELVEL</constant> , its destructor is
1469 called at the last. That is, it is assured that all other
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001470 components like PCMs and controls have already been released.
1471 You don't have to stop PCMs, etc. explicitly, but just
1472 call low-level hardware stopping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 </para>
1474
1475 <para>
1476 The management of a memory-mapped region is almost as same as
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001477 the management of an I/O port. You'll need three fields like
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 the following:
1479
1480 <informalexample>
1481 <programlisting>
1482<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001483 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 ....
1485 unsigned long iobase_phys;
1486 void __iomem *iobase_virt;
1487 };
1488]]>
1489 </programlisting>
1490 </informalexample>
1491
1492 and the allocation would be like below:
1493
1494 <informalexample>
1495 <programlisting>
1496<![CDATA[
1497 if ((err = pci_request_regions(pci, "My Chip")) < 0) {
1498 kfree(chip);
1499 return err;
1500 }
1501 chip->iobase_phys = pci_resource_start(pci, 0);
1502 chip->iobase_virt = ioremap_nocache(chip->iobase_phys,
1503 pci_resource_len(pci, 0));
1504]]>
1505 </programlisting>
1506 </informalexample>
1507
1508 and the corresponding destructor would be:
1509
1510 <informalexample>
1511 <programlisting>
1512<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001513 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 {
1515 ....
1516 if (chip->iobase_virt)
1517 iounmap(chip->iobase_virt);
1518 ....
1519 pci_release_regions(chip->pci);
1520 ....
1521 }
1522]]>
1523 </programlisting>
1524 </informalexample>
1525 </para>
1526
1527 </section>
1528
1529 <section id="pci-resource-device-struct">
1530 <title>Registration of Device Struct</title>
1531 <para>
1532 At some point, typically after calling <function>snd_device_new()</function>,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001533 you need to register the struct <structname>device</structname> of the chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 you're handling for udev and co. ALSA provides a macro for compatibility with
1535 older kernels. Simply call like the following:
1536 <informalexample>
1537 <programlisting>
1538<![CDATA[
1539 snd_card_set_dev(card, &pci->dev);
1540]]>
1541 </programlisting>
1542 </informalexample>
1543 so that it stores the PCI's device pointer to the card. This will be
1544 referred by ALSA core functions later when the devices are registered.
1545 </para>
1546 <para>
1547 In the case of non-PCI, pass the proper device struct pointer of the BUS
1548 instead. (In the case of legacy ISA without PnP, you don't have to do
1549 anything.)
1550 </para>
1551 </section>
1552
1553 <section id="pci-resource-entries">
1554 <title>PCI Entries</title>
1555 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001556 So far, so good. Let's finish the missing PCI
1557 stuff. At first, we need a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 <structname>pci_device_id</structname> table for this
1559 chipset. It's a table of PCI vendor/device ID number, and some
1560 masks.
1561 </para>
1562
1563 <para>
1564 For example,
1565
1566 <informalexample>
1567 <programlisting>
1568<![CDATA[
Takashi Iwaif40b6892006-07-05 16:51:05 +02001569 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1571 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1572 ....
1573 { 0, }
1574 };
1575 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1576]]>
1577 </programlisting>
1578 </informalexample>
1579 </para>
1580
1581 <para>
1582 The first and second fields of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001583 the <structname>pci_device_id</structname> structure are the vendor and
1584 device IDs. If you have no reason to filter the matching
1585 devices, you can leave the remaining fields as above. The last
1586 field of the <structname>pci_device_id</structname> struct contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 private data for this entry. You can specify any value here, for
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001588 example, to define specific operations for supported device IDs.
1589 Such an example is found in the intel8x0 driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 </para>
1591
1592 <para>
1593 The last entry of this list is the terminator. You must
1594 specify this all-zero entry.
1595 </para>
1596
1597 <para>
1598 Then, prepare the <structname>pci_driver</structname> record:
1599
1600 <informalexample>
1601 <programlisting>
1602<![CDATA[
1603 static struct pci_driver driver = {
Takashi Iwaice1fd362011-06-14 08:54:14 +02001604 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .id_table = snd_mychip_ids,
1606 .probe = snd_mychip_probe,
Takashi Iwai090015a2012-12-07 07:52:28 +01001607 .remove = snd_mychip_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 };
1609]]>
1610 </programlisting>
1611 </informalexample>
1612 </para>
1613
1614 <para>
1615 The <structfield>probe</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001616 <structfield>remove</structfield> functions have already
1617 been defined in the previous sections.
Takashi Iwai090015a2012-12-07 07:52:28 +01001618 The <structfield>name</structfield>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 field is the name string of this device. Note that you must not
1620 use a slash <quote>/</quote> in this string.
1621 </para>
1622
1623 <para>
1624 And at last, the module entries:
1625
1626 <informalexample>
1627 <programlisting>
1628<![CDATA[
1629 static int __init alsa_card_mychip_init(void)
1630 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001631 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
1634 static void __exit alsa_card_mychip_exit(void)
1635 {
1636 pci_unregister_driver(&driver);
1637 }
1638
1639 module_init(alsa_card_mychip_init)
1640 module_exit(alsa_card_mychip_exit)
1641]]>
1642 </programlisting>
1643 </informalexample>
1644 </para>
1645
1646 <para>
1647 Note that these module entries are tagged with
1648 <parameter>__init</parameter> and
Takashi Iwai090015a2012-12-07 07:52:28 +01001649 <parameter>__exit</parameter> prefixes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 </para>
1651
1652 <para>
1653 Oh, one thing was forgotten. If you have no exported symbols,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001654 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 -07001655
1656 <informalexample>
1657 <programlisting>
1658<![CDATA[
1659 EXPORT_NO_SYMBOLS;
1660]]>
1661 </programlisting>
1662 </informalexample>
1663
1664 That's all!
1665 </para>
1666 </section>
1667 </chapter>
1668
1669
1670<!-- ****************************************************** -->
1671<!-- PCM Interface -->
1672<!-- ****************************************************** -->
1673 <chapter id="pcm-interface">
1674 <title>PCM Interface</title>
1675
1676 <section id="pcm-interface-general">
1677 <title>General</title>
1678 <para>
1679 The PCM middle layer of ALSA is quite powerful and it is only
1680 necessary for each driver to implement the low-level functions
1681 to access its hardware.
1682 </para>
1683
1684 <para>
1685 For accessing to the PCM layer, you need to include
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001686 <filename>&lt;sound/pcm.h&gt;</filename> first. In addition,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 <filename>&lt;sound/pcm_params.h&gt;</filename> might be needed
1688 if you access to some functions related with hw_param.
1689 </para>
1690
1691 <para>
1692 Each card device can have up to four pcm instances. A pcm
1693 instance corresponds to a pcm device file. The limitation of
1694 number of instances comes only from the available bit size of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001695 the Linux's device numbers. Once when 64bit device number is
1696 used, we'll have more pcm instances available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 </para>
1698
1699 <para>
1700 A pcm instance consists of pcm playback and capture streams,
1701 and each pcm stream consists of one or more pcm substreams. Some
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001702 soundcards support multiple playback functions. For example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 emu10k1 has a PCM playback of 32 stereo substreams. In this case, at
1704 each open, a free substream is (usually) automatically chosen
1705 and opened. Meanwhile, when only one substream exists and it was
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001706 already opened, the successful open will either block
1707 or error with <constant>EAGAIN</constant> according to the
1708 file open mode. But you don't have to care about such details in your
1709 driver. The PCM middle layer will take care of such work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 </para>
1711 </section>
1712
1713 <section id="pcm-interface-example">
1714 <title>Full Code Example</title>
1715 <para>
1716 The example code below does not include any hardware access
1717 routines but shows only the skeleton, how to build up the PCM
1718 interfaces.
1719
1720 <example>
1721 <title>PCM Example Code</title>
1722 <programlisting>
1723<![CDATA[
1724 #include <sound/pcm.h>
1725 ....
1726
1727 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001728 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 .info = (SNDRV_PCM_INFO_MMAP |
1730 SNDRV_PCM_INFO_INTERLEAVED |
1731 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1732 SNDRV_PCM_INFO_MMAP_VALID),
1733 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1734 .rates = SNDRV_PCM_RATE_8000_48000,
1735 .rate_min = 8000,
1736 .rate_max = 48000,
1737 .channels_min = 2,
1738 .channels_max = 2,
1739 .buffer_bytes_max = 32768,
1740 .period_bytes_min = 4096,
1741 .period_bytes_max = 32768,
1742 .periods_min = 1,
1743 .periods_max = 1024,
1744 };
1745
1746 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001747 static struct snd_pcm_hardware snd_mychip_capture_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 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001766 static int snd_mychip_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001768 struct mychip *chip = snd_pcm_substream_chip(substream);
1769 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 runtime->hw = snd_mychip_playback_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001772 /* more hardware-initialization will be done here */
1773 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
1775 }
1776
1777 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001778 static int snd_mychip_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001780 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001781 /* the hardware-specific codes will be here */
1782 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784
1785 }
1786
1787 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001788 static int snd_mychip_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001790 struct mychip *chip = snd_pcm_substream_chip(substream);
1791 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 runtime->hw = snd_mychip_capture_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001794 /* more hardware-initialization will be done here */
1795 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return 0;
1797 }
1798
1799 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001800 static int snd_mychip_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001802 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001803 /* the hardware-specific codes will be here */
1804 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 0;
1806
1807 }
1808
1809 /* hw_params callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001810 static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
1811 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 {
1813 return snd_pcm_lib_malloc_pages(substream,
1814 params_buffer_bytes(hw_params));
1815 }
1816
1817 /* hw_free callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001818 static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 {
1820 return snd_pcm_lib_free_pages(substream);
1821 }
1822
1823 /* prepare callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001824 static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001826 struct mychip *chip = snd_pcm_substream_chip(substream);
1827 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 /* set up the hardware with the current configuration
1830 * for example...
1831 */
1832 mychip_set_sample_format(chip, runtime->format);
1833 mychip_set_sample_rate(chip, runtime->rate);
1834 mychip_set_channels(chip, runtime->channels);
Takashi Iwai0b7bed42006-03-02 15:35:55 +01001835 mychip_set_dma_setup(chip, runtime->dma_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 chip->buffer_size,
1837 chip->period_size);
1838 return 0;
1839 }
1840
1841 /* trigger callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001842 static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 int cmd)
1844 {
1845 switch (cmd) {
1846 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001847 /* do something to start the PCM engine */
1848 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001851 /* do something to stop the PCM engine */
1852 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 break;
1854 default:
1855 return -EINVAL;
1856 }
1857 }
1858
1859 /* pointer callback */
1860 static snd_pcm_uframes_t
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001861 snd_mychip_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001863 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 unsigned int current_ptr;
1865
1866 /* get the current hardware pointer */
1867 current_ptr = mychip_get_hw_pointer(chip);
1868 return current_ptr;
1869 }
1870
1871 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001872 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 .open = snd_mychip_playback_open,
1874 .close = snd_mychip_playback_close,
1875 .ioctl = snd_pcm_lib_ioctl,
1876 .hw_params = snd_mychip_pcm_hw_params,
1877 .hw_free = snd_mychip_pcm_hw_free,
1878 .prepare = snd_mychip_pcm_prepare,
1879 .trigger = snd_mychip_pcm_trigger,
1880 .pointer = snd_mychip_pcm_pointer,
1881 };
1882
1883 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001884 static struct snd_pcm_ops snd_mychip_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .open = snd_mychip_capture_open,
1886 .close = snd_mychip_capture_close,
1887 .ioctl = snd_pcm_lib_ioctl,
1888 .hw_params = snd_mychip_pcm_hw_params,
1889 .hw_free = snd_mychip_pcm_hw_free,
1890 .prepare = snd_mychip_pcm_prepare,
1891 .trigger = snd_mychip_pcm_trigger,
1892 .pointer = snd_mychip_pcm_pointer,
1893 };
1894
1895 /*
1896 * definitions of capture are omitted here...
1897 */
1898
1899 /* create a pcm device */
Takashi Iwai090015a2012-12-07 07:52:28 +01001900 static int snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001902 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 int err;
1904
Takashi Iwai95a5b082007-07-26 16:50:09 +02001905 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1906 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return err;
1908 pcm->private_data = chip;
1909 strcpy(pcm->name, "My Chip");
1910 chip->pcm = pcm;
1911 /* set operators */
1912 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1913 &snd_mychip_playback_ops);
1914 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1915 &snd_mychip_capture_ops);
1916 /* pre-allocation of buffers */
1917 /* NOTE: this may fail */
1918 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1919 snd_dma_pci_data(chip->pci),
1920 64*1024, 64*1024);
1921 return 0;
1922 }
1923]]>
1924 </programlisting>
1925 </example>
1926 </para>
1927 </section>
1928
1929 <section id="pcm-interface-constructor">
1930 <title>Constructor</title>
1931 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001932 A pcm instance is allocated by the <function>snd_pcm_new()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 function. It would be better to create a constructor for pcm,
1934 namely,
1935
1936 <informalexample>
1937 <programlisting>
1938<![CDATA[
Takashi Iwai090015a2012-12-07 07:52:28 +01001939 static int snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001941 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int err;
1943
Takashi Iwai95a5b082007-07-26 16:50:09 +02001944 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1945 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 return err;
1947 pcm->private_data = chip;
1948 strcpy(pcm->name, "My Chip");
1949 chip->pcm = pcm;
1950 ....
1951 return 0;
1952 }
1953]]>
1954 </programlisting>
1955 </informalexample>
1956 </para>
1957
1958 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001959 The <function>snd_pcm_new()</function> function takes four
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 arguments. The first argument is the card pointer to which this
1961 pcm is assigned, and the second is the ID string.
1962 </para>
1963
1964 <para>
1965 The third argument (<parameter>index</parameter>, 0 in the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001966 above) is the index of this new pcm. It begins from zero. If
1967 you create more than one pcm instances, specify the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 different numbers in this argument. For example,
1969 <parameter>index</parameter> = 1 for the second PCM device.
1970 </para>
1971
1972 <para>
1973 The fourth and fifth arguments are the number of substreams
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02001974 for playback and capture, respectively. Here 1 is used for
1975 both arguments. When no playback or capture substreams are available,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 pass 0 to the corresponding argument.
1977 </para>
1978
1979 <para>
1980 If a chip supports multiple playbacks or captures, you can
1981 specify more numbers, but they must be handled properly in
1982 open/close, etc. callbacks. When you need to know which
1983 substream you are referring to, then it can be obtained from
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001984 struct <structname>snd_pcm_substream</structname> data passed to each callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 as follows:
1986
1987 <informalexample>
1988 <programlisting>
1989<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001990 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 int index = substream->number;
1992]]>
1993 </programlisting>
1994 </informalexample>
1995 </para>
1996
1997 <para>
1998 After the pcm is created, you need to set operators for each
1999 pcm stream.
2000
2001 <informalexample>
2002 <programlisting>
2003<![CDATA[
2004 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2005 &snd_mychip_playback_ops);
2006 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2007 &snd_mychip_capture_ops);
2008]]>
2009 </programlisting>
2010 </informalexample>
2011 </para>
2012
2013 <para>
2014 The operators are defined typically like this:
2015
2016 <informalexample>
2017 <programlisting>
2018<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002019 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 .open = snd_mychip_pcm_open,
2021 .close = snd_mychip_pcm_close,
2022 .ioctl = snd_pcm_lib_ioctl,
2023 .hw_params = snd_mychip_pcm_hw_params,
2024 .hw_free = snd_mychip_pcm_hw_free,
2025 .prepare = snd_mychip_pcm_prepare,
2026 .trigger = snd_mychip_pcm_trigger,
2027 .pointer = snd_mychip_pcm_pointer,
2028 };
2029]]>
2030 </programlisting>
2031 </informalexample>
2032
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002033 All the callbacks are described in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 <link linkend="pcm-interface-operators"><citetitle>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002035 Operators</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 </para>
2037
2038 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002039 After setting the operators, you probably will want to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 pre-allocate the buffer. For the pre-allocation, simply call
2041 the following:
2042
2043 <informalexample>
2044 <programlisting>
2045<![CDATA[
2046 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2047 snd_dma_pci_data(chip->pci),
2048 64*1024, 64*1024);
2049]]>
2050 </programlisting>
2051 </informalexample>
2052
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002053 It will allocate a buffer up to 64kB as default.
2054 Buffer management details will be described in the later section <link
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 linkend="buffer-and-memory"><citetitle>Buffer and Memory
2056 Management</citetitle></link>.
2057 </para>
2058
2059 <para>
2060 Additionally, you can set some extra information for this pcm
2061 in pcm-&gt;info_flags.
2062 The available values are defined as
2063 <constant>SNDRV_PCM_INFO_XXX</constant> in
2064 <filename>&lt;sound/asound.h&gt;</filename>, which is used for
2065 the hardware definition (described later). When your soundchip
2066 supports only half-duplex, specify like this:
2067
2068 <informalexample>
2069 <programlisting>
2070<![CDATA[
2071 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
2072]]>
2073 </programlisting>
2074 </informalexample>
2075 </para>
2076 </section>
2077
2078 <section id="pcm-interface-destructor">
2079 <title>... And the Destructor?</title>
2080 <para>
2081 The destructor for a pcm instance is not always
2082 necessary. Since the pcm device will be released by the middle
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002083 layer code automatically, you don't have to call the destructor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 explicitly.
2085 </para>
2086
2087 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002088 The destructor would be necessary if you created
2089 special records internally and needed to release them. In such a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 case, set the destructor function to
2091 pcm-&gt;private_free:
2092
2093 <example>
2094 <title>PCM Instance with a Destructor</title>
2095 <programlisting>
2096<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002097 static void mychip_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002099 struct mychip *chip = snd_pcm_chip(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /* free your own data */
2101 kfree(chip->my_private_pcm_data);
Takashi Iwai95a5b082007-07-26 16:50:09 +02002102 /* do what you like else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 ....
2104 }
2105
Takashi Iwai090015a2012-12-07 07:52:28 +01002106 static int snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002108 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 ....
2110 /* allocate your own data */
2111 chip->my_private_pcm_data = kmalloc(...);
2112 /* set the destructor */
2113 pcm->private_data = chip;
2114 pcm->private_free = mychip_pcm_free;
2115 ....
2116 }
2117]]>
2118 </programlisting>
2119 </example>
2120 </para>
2121 </section>
2122
2123 <section id="pcm-interface-runtime">
2124 <title>Runtime Pointer - The Chest of PCM Information</title>
2125 <para>
2126 When the PCM substream is opened, a PCM runtime instance is
2127 allocated and assigned to the substream. This pointer is
2128 accessible via <constant>substream-&gt;runtime</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002129 This runtime pointer holds most information you need
2130 to control the PCM: the copy of hw_params and sw_params configurations, the buffer
2131 pointers, mmap records, spinlocks, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 </para>
2133
2134 <para>
2135 The definition of runtime instance is found in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002136 <filename>&lt;sound/pcm.h&gt;</filename>. Here are
2137 the contents of this file:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 <informalexample>
2139 <programlisting>
2140<![CDATA[
2141struct _snd_pcm_runtime {
2142 /* -- Status -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002143 struct snd_pcm_substream *trigger_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 snd_timestamp_t trigger_tstamp; /* trigger timestamp */
2145 int overrange;
2146 snd_pcm_uframes_t avail_max;
2147 snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
2148 snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/
2149
2150 /* -- HW params -- */
2151 snd_pcm_access_t access; /* access mode */
2152 snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
2153 snd_pcm_subformat_t subformat; /* subformat */
2154 unsigned int rate; /* rate in Hz */
2155 unsigned int channels; /* channels */
2156 snd_pcm_uframes_t period_size; /* period size */
2157 unsigned int periods; /* periods */
2158 snd_pcm_uframes_t buffer_size; /* buffer size */
2159 unsigned int tick_time; /* tick time */
2160 snd_pcm_uframes_t min_align; /* Min alignment for the format */
2161 size_t byte_align;
2162 unsigned int frame_bits;
2163 unsigned int sample_bits;
2164 unsigned int info;
2165 unsigned int rate_num;
2166 unsigned int rate_den;
2167
2168 /* -- SW params -- */
Takashi Iwai07799e72005-10-10 11:49:49 +02002169 struct timespec tstamp_mode; /* mmap timestamp is updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 unsigned int period_step;
2171 unsigned int sleep_min; /* min ticks to sleep */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 snd_pcm_uframes_t start_threshold;
2173 snd_pcm_uframes_t stop_threshold;
2174 snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
2175 noise is nearest than this */
2176 snd_pcm_uframes_t silence_size; /* Silence filling size */
2177 snd_pcm_uframes_t boundary; /* pointers wrap point */
2178
2179 snd_pcm_uframes_t silenced_start;
2180 snd_pcm_uframes_t silenced_size;
2181
2182 snd_pcm_sync_id_t sync; /* hardware synchronization ID */
2183
2184 /* -- mmap -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002185 volatile struct snd_pcm_mmap_status *status;
2186 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 atomic_t mmap_count;
2188
2189 /* -- locking / scheduling -- */
2190 spinlock_t lock;
2191 wait_queue_head_t sleep;
2192 struct timer_list tick_timer;
2193 struct fasync_struct *fasync;
2194
2195 /* -- private section -- */
2196 void *private_data;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002197 void (*private_free)(struct snd_pcm_runtime *runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
2199 /* -- hardware description -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002200 struct snd_pcm_hardware hw;
2201 struct snd_pcm_hw_constraints hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* -- interrupt callbacks -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002204 void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
2205 void (*transfer_ack_end)(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /* -- timer -- */
2208 unsigned int timer_resolution; /* timer resolution */
2209
2210 /* -- DMA -- */
2211 unsigned char *dma_area; /* DMA area */
2212 dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
2213 size_t dma_bytes; /* size of DMA area */
2214
2215 struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
2216
2217#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2218 /* -- OSS things -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002219 struct snd_pcm_oss_runtime oss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220#endif
2221};
2222]]>
2223 </programlisting>
2224 </informalexample>
2225 </para>
2226
2227 <para>
2228 For the operators (callbacks) of each sound driver, most of
2229 these records are supposed to be read-only. Only the PCM
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002230 middle-layer changes / updates them. The exceptions are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 the hardware description (hw), interrupt callbacks
2232 (transfer_ack_xxx), DMA buffer information, and the private
2233 data. Besides, if you use the standard buffer allocation
2234 method via <function>snd_pcm_lib_malloc_pages()</function>,
2235 you don't need to set the DMA buffer information by yourself.
2236 </para>
2237
2238 <para>
2239 In the sections below, important records are explained.
2240 </para>
2241
2242 <section id="pcm-interface-runtime-hw">
2243 <title>Hardware Description</title>
2244 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002245 The hardware descriptor (struct <structname>snd_pcm_hardware</structname>)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 contains the definitions of the fundamental hardware
2247 configuration. Above all, you'll need to define this in
2248 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2249 the open callback</citetitle></link>.
2250 Note that the runtime instance holds the copy of the
2251 descriptor, not the pointer to the existing descriptor. That
2252 is, in the open callback, you can modify the copied descriptor
2253 (<constant>runtime-&gt;hw</constant>) as you need. For example, if the maximum
2254 number of channels is 1 only on some chip models, you can
2255 still use the same hardware descriptor and change the
2256 channels_max later:
2257 <informalexample>
2258 <programlisting>
2259<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002260 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 ...
2262 runtime->hw = snd_mychip_playback_hw; /* common definition */
2263 if (chip->model == VERY_OLD_ONE)
2264 runtime->hw.channels_max = 1;
2265]]>
2266 </programlisting>
2267 </informalexample>
2268 </para>
2269
2270 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002271 Typically, you'll have a hardware descriptor as below:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 <informalexample>
2273 <programlisting>
2274<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002275 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 .info = (SNDRV_PCM_INFO_MMAP |
2277 SNDRV_PCM_INFO_INTERLEAVED |
2278 SNDRV_PCM_INFO_BLOCK_TRANSFER |
2279 SNDRV_PCM_INFO_MMAP_VALID),
2280 .formats = SNDRV_PCM_FMTBIT_S16_LE,
2281 .rates = SNDRV_PCM_RATE_8000_48000,
2282 .rate_min = 8000,
2283 .rate_max = 48000,
2284 .channels_min = 2,
2285 .channels_max = 2,
2286 .buffer_bytes_max = 32768,
2287 .period_bytes_min = 4096,
2288 .period_bytes_max = 32768,
2289 .periods_min = 1,
2290 .periods_max = 1024,
2291 };
2292]]>
2293 </programlisting>
2294 </informalexample>
2295 </para>
2296
2297 <para>
2298 <itemizedlist>
2299 <listitem><para>
2300 The <structfield>info</structfield> field contains the type and
2301 capabilities of this pcm. The bit flags are defined in
2302 <filename>&lt;sound/asound.h&gt;</filename> as
2303 <constant>SNDRV_PCM_INFO_XXX</constant>. Here, at least, you
2304 have to specify whether the mmap is supported and which
2305 interleaved format is supported.
Antonio Ospite04044b82013-01-29 12:56:27 +01002306 When the hardware supports mmap, add the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 <constant>SNDRV_PCM_INFO_MMAP</constant> flag here. When the
2308 hardware supports the interleaved or the non-interleaved
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002309 formats, <constant>SNDRV_PCM_INFO_INTERLEAVED</constant> or
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 <constant>SNDRV_PCM_INFO_NONINTERLEAVED</constant> flag must
2311 be set, respectively. If both are supported, you can set both,
2312 too.
2313 </para>
2314
2315 <para>
2316 In the above example, <constant>MMAP_VALID</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002317 <constant>BLOCK_TRANSFER</constant> are specified for the OSS mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 mode. Usually both are set. Of course,
2319 <constant>MMAP_VALID</constant> is set only if the mmap is
2320 really supported.
2321 </para>
2322
2323 <para>
2324 The other possible flags are
2325 <constant>SNDRV_PCM_INFO_PAUSE</constant> and
2326 <constant>SNDRV_PCM_INFO_RESUME</constant>. The
2327 <constant>PAUSE</constant> bit means that the pcm supports the
2328 <quote>pause</quote> operation, while the
2329 <constant>RESUME</constant> bit means that the pcm supports
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002330 the full <quote>suspend/resume</quote> operation.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002331 If the <constant>PAUSE</constant> flag is set,
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002332 the <structfield>trigger</structfield> callback below
2333 must handle the corresponding (pause push/release) commands.
2334 The suspend/resume trigger commands can be defined even without
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002335 the <constant>RESUME</constant> flag. See <link
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002336 linkend="power-management"><citetitle>
2337 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 </para>
2339
2340 <para>
2341 When the PCM substreams can be synchronized (typically,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01002342 synchronized start/stop of a playback and a capture streams),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 you can give <constant>SNDRV_PCM_INFO_SYNC_START</constant>,
2344 too. In this case, you'll need to check the linked-list of
2345 PCM substreams in the trigger callback. This will be
2346 described in the later section.
2347 </para>
2348 </listitem>
2349
2350 <listitem>
2351 <para>
2352 <structfield>formats</structfield> field contains the bit-flags
2353 of supported formats (<constant>SNDRV_PCM_FMTBIT_XXX</constant>).
2354 If the hardware supports more than one format, give all or'ed
2355 bits. In the example above, the signed 16bit little-endian
2356 format is specified.
2357 </para>
2358 </listitem>
2359
2360 <listitem>
2361 <para>
2362 <structfield>rates</structfield> field contains the bit-flags of
2363 supported rates (<constant>SNDRV_PCM_RATE_XXX</constant>).
2364 When the chip supports continuous rates, pass
2365 <constant>CONTINUOUS</constant> bit additionally.
2366 The pre-defined rate bits are provided only for typical
2367 rates. If your chip supports unconventional rates, you need to add
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002368 the <constant>KNOT</constant> bit and set up the hardware
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 constraint manually (explained later).
2370 </para>
2371 </listitem>
2372
2373 <listitem>
2374 <para>
2375 <structfield>rate_min</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002376 <structfield>rate_max</structfield> define the minimum and
2377 maximum sample rate. This should correspond somehow to
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 <structfield>rates</structfield> bits.
2379 </para>
2380 </listitem>
2381
2382 <listitem>
2383 <para>
2384 <structfield>channel_min</structfield> and
2385 <structfield>channel_max</structfield>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002386 define, as you might already expected, the minimum and maximum
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 number of channels.
2388 </para>
2389 </listitem>
2390
2391 <listitem>
2392 <para>
2393 <structfield>buffer_bytes_max</structfield> defines the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002394 maximum buffer size in bytes. There is no
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 <structfield>buffer_bytes_min</structfield> field, since
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002396 it can be calculated from the minimum period size and the
2397 minimum number of periods.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 Meanwhile, <structfield>period_bytes_min</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002399 define the minimum and maximum size of the period in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 <structfield>periods_max</structfield> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002401 <structfield>periods_min</structfield> define the maximum and
2402 minimum number of periods in the buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 </para>
2404
2405 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002406 The <quote>period</quote> is a term that corresponds to
2407 a fragment in the OSS world. The period defines the size at
2408 which a PCM interrupt is generated. This size strongly
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 depends on the hardware.
2410 Generally, the smaller period size will give you more
2411 interrupts, that is, more controls.
2412 In the case of capture, this size defines the input latency.
2413 On the other hand, the whole buffer size defines the
2414 output latency for the playback direction.
2415 </para>
2416 </listitem>
2417
2418 <listitem>
2419 <para>
2420 There is also a field <structfield>fifo_size</structfield>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002421 This specifies the size of the hardware FIFO, but currently it
2422 is neither used in the driver nor in the alsa-lib. So, you
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 can ignore this field.
2424 </para>
2425 </listitem>
2426 </itemizedlist>
2427 </para>
2428 </section>
2429
2430 <section id="pcm-interface-runtime-config">
2431 <title>PCM Configurations</title>
2432 <para>
2433 Ok, let's go back again to the PCM runtime records.
2434 The most frequently referred records in the runtime instance are
2435 the PCM configurations.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002436 The PCM configurations are stored in the runtime instance
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 after the application sends <type>hw_params</type> data via
2438 alsa-lib. There are many fields copied from hw_params and
2439 sw_params structs. For example,
2440 <structfield>format</structfield> holds the format type
2441 chosen by the application. This field contains the enum value
2442 <constant>SNDRV_PCM_FORMAT_XXX</constant>.
2443 </para>
2444
2445 <para>
2446 One thing to be noted is that the configured buffer and period
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002447 sizes are stored in <quote>frames</quote> in the runtime.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 In the ALSA world, 1 frame = channels * samples-size.
2449 For conversion between frames and bytes, you can use the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002450 <function>frames_to_bytes()</function> and
2451 <function>bytes_to_frames()</function> helper functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 <informalexample>
2453 <programlisting>
2454<![CDATA[
2455 period_bytes = frames_to_bytes(runtime, runtime->period_size);
2456]]>
2457 </programlisting>
2458 </informalexample>
2459 </para>
2460
2461 <para>
2462 Also, many software parameters (sw_params) are
2463 stored in frames, too. Please check the type of the field.
2464 <type>snd_pcm_uframes_t</type> is for the frames as unsigned
2465 integer while <type>snd_pcm_sframes_t</type> is for the frames
2466 as signed integer.
2467 </para>
2468 </section>
2469
2470 <section id="pcm-interface-runtime-dma">
2471 <title>DMA Buffer Information</title>
2472 <para>
2473 The DMA buffer is defined by the following four fields,
2474 <structfield>dma_area</structfield>,
2475 <structfield>dma_addr</structfield>,
2476 <structfield>dma_bytes</structfield> and
2477 <structfield>dma_private</structfield>.
2478 The <structfield>dma_area</structfield> holds the buffer
2479 pointer (the logical address). You can call
2480 <function>memcpy</function> from/to
2481 this pointer. Meanwhile, <structfield>dma_addr</structfield>
2482 holds the physical address of the buffer. This field is
2483 specified only when the buffer is a linear buffer.
2484 <structfield>dma_bytes</structfield> holds the size of buffer
2485 in bytes. <structfield>dma_private</structfield> is used for
2486 the ALSA DMA allocator.
2487 </para>
2488
2489 <para>
2490 If you use a standard ALSA function,
2491 <function>snd_pcm_lib_malloc_pages()</function>, for
2492 allocating the buffer, these fields are set by the ALSA middle
2493 layer, and you should <emphasis>not</emphasis> change them by
2494 yourself. You can read them but not write them.
2495 On the other hand, if you want to allocate the buffer by
2496 yourself, you'll need to manage it in hw_params callback.
2497 At least, <structfield>dma_bytes</structfield> is mandatory.
2498 <structfield>dma_area</structfield> is necessary when the
2499 buffer is mmapped. If your driver doesn't support mmap, this
2500 field is not necessary. <structfield>dma_addr</structfield>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002501 is also optional. You can use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 <structfield>dma_private</structfield> as you like, too.
2503 </para>
2504 </section>
2505
2506 <section id="pcm-interface-runtime-status">
2507 <title>Running Status</title>
2508 <para>
2509 The running status can be referred via <constant>runtime-&gt;status</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002510 This is the pointer to the struct <structname>snd_pcm_mmap_status</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 record. For example, you can get the current DMA hardware
2512 pointer via <constant>runtime-&gt;status-&gt;hw_ptr</constant>.
2513 </para>
2514
2515 <para>
2516 The DMA application pointer can be referred via
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002517 <constant>runtime-&gt;control</constant>, which points to the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002518 struct <structname>snd_pcm_mmap_control</structname> record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 However, accessing directly to this value is not recommended.
2520 </para>
2521 </section>
2522
2523 <section id="pcm-interface-runtime-private">
2524 <title>Private Data</title>
2525 <para>
2526 You can allocate a record for the substream and store it in
2527 <constant>runtime-&gt;private_data</constant>. Usually, this
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002528 is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2530 the open callback</citetitle></link>.
2531 Don't mix this with <constant>pcm-&gt;private_data</constant>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002532 The <constant>pcm-&gt;private_data</constant> usually points to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 chip instance assigned statically at the creation of PCM, while the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002534 <constant>runtime-&gt;private_data</constant> points to a dynamic
2535 data structure created at the PCM open callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 <informalexample>
2538 <programlisting>
2539<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002540 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002542 struct my_pcm_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 ....
2544 data = kmalloc(sizeof(*data), GFP_KERNEL);
2545 substream->runtime->private_data = data;
2546 ....
2547 }
2548]]>
2549 </programlisting>
2550 </informalexample>
2551 </para>
2552
2553 <para>
2554 The allocated object must be released in
2555 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2556 the close callback</citetitle></link>.
2557 </para>
2558 </section>
2559
2560 <section id="pcm-interface-runtime-intr">
2561 <title>Interrupt Callbacks</title>
2562 <para>
2563 The field <structfield>transfer_ack_begin</structfield> and
2564 <structfield>transfer_ack_end</structfield> are called at
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002565 the beginning and at the end of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 <function>snd_pcm_period_elapsed()</function>, respectively.
2567 </para>
2568 </section>
2569
2570 </section>
2571
2572 <section id="pcm-interface-operators">
2573 <title>Operators</title>
2574 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002575 OK, now let me give details about each pcm callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 (<parameter>ops</parameter>). In general, every callback must
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002577 return 0 if successful, or a negative error number
2578 such as <constant>-EINVAL</constant>. To choose an appropriate
2579 error number, it is advised to check what value other parts of
2580 the kernel return when the same kind of request fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 </para>
2582
2583 <para>
2584 The callback function takes at least the argument with
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002585 <structname>snd_pcm_substream</structname> pointer. To retrieve
2586 the chip record from the given substream instance, you can use the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 following macro.
2588
2589 <informalexample>
2590 <programlisting>
2591<![CDATA[
2592 int xxx() {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002593 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 ....
2595 }
2596]]>
2597 </programlisting>
2598 </informalexample>
2599
2600 The macro reads <constant>substream-&gt;private_data</constant>,
2601 which is a copy of <constant>pcm-&gt;private_data</constant>.
2602 You can override the former if you need to assign different data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002603 records per PCM substream. For example, the cmi8330 driver assigns
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 different private_data for playback and capture directions,
2605 because it uses two different codecs (SB- and AD-compatible) for
2606 different directions.
2607 </para>
2608
2609 <section id="pcm-interface-operators-open-callback">
2610 <title>open callback</title>
2611 <para>
2612 <informalexample>
2613 <programlisting>
2614<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002615 static int snd_xxx_open(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616]]>
2617 </programlisting>
2618 </informalexample>
2619
2620 This is called when a pcm substream is opened.
2621 </para>
2622
2623 <para>
2624 At least, here you have to initialize the runtime-&gt;hw
2625 record. Typically, this is done by like this:
2626
2627 <informalexample>
2628 <programlisting>
2629<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002630 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002632 struct mychip *chip = snd_pcm_substream_chip(substream);
2633 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 runtime->hw = snd_mychip_playback_hw;
2636 return 0;
2637 }
2638]]>
2639 </programlisting>
2640 </informalexample>
2641
2642 where <parameter>snd_mychip_playback_hw</parameter> is the
2643 pre-defined hardware description.
2644 </para>
2645
2646 <para>
2647 You can allocate a private data in this callback, as described
2648 in <link linkend="pcm-interface-runtime-private"><citetitle>
2649 Private Data</citetitle></link> section.
2650 </para>
2651
2652 <para>
2653 If the hardware configuration needs more constraints, set the
2654 hardware constraints here, too.
2655 See <link linkend="pcm-interface-constraints"><citetitle>
2656 Constraints</citetitle></link> for more details.
2657 </para>
2658 </section>
2659
2660 <section id="pcm-interface-operators-close-callback">
2661 <title>close callback</title>
2662 <para>
2663 <informalexample>
2664 <programlisting>
2665<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002666 static int snd_xxx_close(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667]]>
2668 </programlisting>
2669 </informalexample>
2670
2671 Obviously, this is called when a pcm substream is closed.
2672 </para>
2673
2674 <para>
2675 Any private instance for a pcm substream allocated in the
2676 open callback will be released here.
2677
2678 <informalexample>
2679 <programlisting>
2680<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002681 static int snd_xxx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 {
2683 ....
2684 kfree(substream->runtime->private_data);
2685 ....
2686 }
2687]]>
2688 </programlisting>
2689 </informalexample>
2690 </para>
2691 </section>
2692
2693 <section id="pcm-interface-operators-ioctl-callback">
2694 <title>ioctl callback</title>
2695 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002696 This is used for any special call to pcm ioctls. But
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 usually you can pass a generic ioctl callback,
2698 <function>snd_pcm_lib_ioctl</function>.
2699 </para>
2700 </section>
2701
2702 <section id="pcm-interface-operators-hw-params-callback">
2703 <title>hw_params callback</title>
2704 <para>
2705 <informalexample>
2706 <programlisting>
2707<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002708 static int snd_xxx_hw_params(struct snd_pcm_substream *substream,
2709 struct snd_pcm_hw_params *hw_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710]]>
2711 </programlisting>
2712 </informalexample>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 </para>
2714
2715 <para>
2716 This is called when the hardware parameter
2717 (<structfield>hw_params</structfield>) is set
2718 up by the application,
2719 that is, once when the buffer size, the period size, the
2720 format, etc. are defined for the pcm substream.
2721 </para>
2722
2723 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002724 Many hardware setups should be done in this callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 including the allocation of buffers.
2726 </para>
2727
2728 <para>
2729 Parameters to be initialized are retrieved by
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002730 <function>params_xxx()</function> macros. To allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 buffer, you can call a helper function,
2732
2733 <informalexample>
2734 <programlisting>
2735<![CDATA[
2736 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
2737]]>
2738 </programlisting>
2739 </informalexample>
2740
2741 <function>snd_pcm_lib_malloc_pages()</function> is available
2742 only when the DMA buffers have been pre-allocated.
2743 See the section <link
2744 linkend="buffer-and-memory-buffer-types"><citetitle>
2745 Buffer Types</citetitle></link> for more details.
2746 </para>
2747
2748 <para>
2749 Note that this and <structfield>prepare</structfield> callbacks
2750 may be called multiple times per initialization.
2751 For example, the OSS emulation may
2752 call these callbacks at each change via its ioctl.
2753 </para>
2754
2755 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002756 Thus, you need to be careful not to allocate the same buffers
2757 many times, which will lead to memory leaks! Calling the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 helper function above many times is OK. It will release the
2759 previous buffer automatically when it was already allocated.
2760 </para>
2761
2762 <para>
2763 Another note is that this callback is non-atomic
2764 (schedulable). This is important, because the
2765 <structfield>trigger</structfield> callback
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002766 is atomic (non-schedulable). That is, mutexes or any
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 schedule-related functions are not available in
2768 <structfield>trigger</structfield> callback.
2769 Please see the subsection
2770 <link linkend="pcm-interface-atomicity"><citetitle>
2771 Atomicity</citetitle></link> for details.
2772 </para>
2773 </section>
2774
2775 <section id="pcm-interface-operators-hw-free-callback">
2776 <title>hw_free callback</title>
2777 <para>
2778 <informalexample>
2779 <programlisting>
2780<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002781 static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782]]>
2783 </programlisting>
2784 </informalexample>
2785 </para>
2786
2787 <para>
2788 This is called to release the resources allocated via
2789 <structfield>hw_params</structfield>. For example, releasing the
2790 buffer via
2791 <function>snd_pcm_lib_malloc_pages()</function> is done by
2792 calling the following:
2793
2794 <informalexample>
2795 <programlisting>
2796<![CDATA[
2797 snd_pcm_lib_free_pages(substream);
2798]]>
2799 </programlisting>
2800 </informalexample>
2801 </para>
2802
2803 <para>
2804 This function is always called before the close callback is called.
2805 Also, the callback may be called multiple times, too.
2806 Keep track whether the resource was already released.
2807 </para>
2808 </section>
2809
2810 <section id="pcm-interface-operators-prepare-callback">
2811 <title>prepare callback</title>
2812 <para>
2813 <informalexample>
2814 <programlisting>
2815<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002816 static int snd_xxx_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817]]>
2818 </programlisting>
2819 </informalexample>
2820 </para>
2821
2822 <para>
2823 This callback is called when the pcm is
2824 <quote>prepared</quote>. You can set the format type, sample
2825 rate, etc. here. The difference from
2826 <structfield>hw_params</structfield> is that the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002827 <structfield>prepare</structfield> callback will be called each
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 time
2829 <function>snd_pcm_prepare()</function> is called, i.e. when
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002830 recovering after underruns, etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 </para>
2832
2833 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002834 Note that this callback is now non-atomic.
2835 You can use schedule-related functions safely in this callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 </para>
2837
2838 <para>
2839 In this and the following callbacks, you can refer to the
2840 values via the runtime record,
2841 substream-&gt;runtime.
2842 For example, to get the current
2843 rate, format or channels, access to
2844 runtime-&gt;rate,
2845 runtime-&gt;format or
2846 runtime-&gt;channels, respectively.
2847 The physical address of the allocated buffer is set to
2848 runtime-&gt;dma_area. The buffer and period sizes are
2849 in runtime-&gt;buffer_size and runtime-&gt;period_size,
2850 respectively.
2851 </para>
2852
2853 <para>
2854 Be careful that this callback will be called many times at
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002855 each setup, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 </para>
2857 </section>
2858
2859 <section id="pcm-interface-operators-trigger-callback">
2860 <title>trigger callback</title>
2861 <para>
2862 <informalexample>
2863 <programlisting>
2864<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002865 static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866]]>
2867 </programlisting>
2868 </informalexample>
2869
2870 This is called when the pcm is started, stopped or paused.
2871 </para>
2872
2873 <para>
2874 Which action is specified in the second argument,
2875 <constant>SNDRV_PCM_TRIGGER_XXX</constant> in
2876 <filename>&lt;sound/pcm.h&gt;</filename>. At least,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002877 the <constant>START</constant> and <constant>STOP</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 commands must be defined in this callback.
2879
2880 <informalexample>
2881 <programlisting>
2882<![CDATA[
2883 switch (cmd) {
2884 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002885 /* do something to start the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 break;
2887 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002888 /* do something to stop the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 break;
2890 default:
2891 return -EINVAL;
2892 }
2893]]>
2894 </programlisting>
2895 </informalexample>
2896 </para>
2897
2898 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002899 When the pcm supports the pause operation (given in the info
Antonio Ospited4dab5a2013-01-29 12:56:28 +01002900 field of the hardware table), the <constant>PAUSE_PUSH</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 and <constant>PAUSE_RELEASE</constant> commands must be
2902 handled here, too. The former is the command to pause the pcm,
2903 and the latter to restart the pcm again.
2904 </para>
2905
2906 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002907 When the pcm supports the suspend/resume operation,
2908 regardless of full or partial suspend/resume support,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002909 the <constant>SUSPEND</constant> and <constant>RESUME</constant>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 commands must be handled, too.
2911 These commands are issued when the power-management status is
2912 changed. Obviously, the <constant>SUSPEND</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002913 <constant>RESUME</constant> commands
2914 suspend and resume the pcm substream, and usually, they
2915 are identical to the <constant>STOP</constant> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 <constant>START</constant> commands, respectively.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002917 See the <link linkend="power-management"><citetitle>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002918 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 </para>
2920
2921 <para>
2922 As mentioned, this callback is atomic. You cannot call
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002923 functions which may sleep.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 The trigger callback should be as minimal as possible,
2925 just really triggering the DMA. The other stuff should be
2926 initialized hw_params and prepare callbacks properly
2927 beforehand.
2928 </para>
2929 </section>
2930
2931 <section id="pcm-interface-operators-pointer-callback">
2932 <title>pointer callback</title>
2933 <para>
2934 <informalexample>
2935 <programlisting>
2936<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002937 static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938]]>
2939 </programlisting>
2940 </informalexample>
2941
2942 This callback is called when the PCM middle layer inquires
2943 the current hardware position on the buffer. The position must
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002944 be returned in frames,
2945 ranging from 0 to buffer_size - 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 </para>
2947
2948 <para>
2949 This is called usually from the buffer-update routine in the
2950 pcm middle layer, which is invoked when
2951 <function>snd_pcm_period_elapsed()</function> is called in the
2952 interrupt routine. Then the pcm middle layer updates the
2953 position and calculates the available space, and wakes up the
2954 sleeping poll threads, etc.
2955 </para>
2956
2957 <para>
2958 This callback is also atomic.
2959 </para>
2960 </section>
2961
2962 <section id="pcm-interface-operators-copy-silence">
2963 <title>copy and silence callbacks</title>
2964 <para>
2965 These callbacks are not mandatory, and can be omitted in
2966 most cases. These callbacks are used when the hardware buffer
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02002967 cannot be in the normal memory space. Some chips have their
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 own buffer on the hardware which is not mappable. In such a
2969 case, you have to transfer the data manually from the memory
2970 buffer to the hardware buffer. Or, if the buffer is
2971 non-contiguous on both physical and virtual memory spaces,
2972 these callbacks must be defined, too.
2973 </para>
2974
2975 <para>
2976 If these two callbacks are defined, copy and set-silence
2977 operations are done by them. The detailed will be described in
2978 the later section <link
2979 linkend="buffer-and-memory"><citetitle>Buffer and Memory
2980 Management</citetitle></link>.
2981 </para>
2982 </section>
2983
2984 <section id="pcm-interface-operators-ack">
2985 <title>ack callback</title>
2986 <para>
2987 This callback is also not mandatory. This callback is called
2988 when the appl_ptr is updated in read or write operations.
2989 Some drivers like emu10k1-fx and cs46xx need to track the
2990 current appl_ptr for the internal buffer, and this callback
2991 is useful only for such a purpose.
2992 </para>
2993 <para>
2994 This callback is atomic.
2995 </para>
2996 </section>
2997
2998 <section id="pcm-interface-operators-page-callback">
2999 <title>page callback</title>
3000
3001 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003002 This callback is optional too. This callback is used
3003 mainly for non-contiguous buffers. The mmap calls this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 callback to get the page address. Some examples will be
3005 explained in the later section <link
3006 linkend="buffer-and-memory"><citetitle>Buffer and Memory
3007 Management</citetitle></link>, too.
3008 </para>
3009 </section>
3010 </section>
3011
3012 <section id="pcm-interface-interrupt-handler">
3013 <title>Interrupt Handler</title>
3014 <para>
3015 The rest of pcm stuff is the PCM interrupt handler. The
3016 role of PCM interrupt handler in the sound driver is to update
3017 the buffer position and to tell the PCM middle layer when the
3018 buffer position goes across the prescribed period size. To
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003019 inform this, call the <function>snd_pcm_period_elapsed()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 function.
3021 </para>
3022
3023 <para>
3024 There are several types of sound chips to generate the interrupts.
3025 </para>
3026
3027 <section id="pcm-interface-interrupt-handler-boundary">
3028 <title>Interrupts at the period (fragment) boundary</title>
3029 <para>
3030 This is the most frequently found type: the hardware
3031 generates an interrupt at each period boundary.
3032 In this case, you can call
3033 <function>snd_pcm_period_elapsed()</function> at each
3034 interrupt.
3035 </para>
3036
3037 <para>
3038 <function>snd_pcm_period_elapsed()</function> takes the
3039 substream pointer as its argument. Thus, you need to keep the
3040 substream pointer accessible from the chip instance. For
3041 example, define substream field in the chip record to hold the
3042 current running substream pointer, and set the pointer value
3043 at open callback (and reset at close callback).
3044 </para>
3045
3046 <para>
Adrian Bunk04187262006-06-30 18:23:04 +02003047 If you acquire a spinlock in the interrupt handler, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 lock is used in other pcm callbacks, too, then you have to
3049 release the lock before calling
3050 <function>snd_pcm_period_elapsed()</function>, because
3051 <function>snd_pcm_period_elapsed()</function> calls other pcm
3052 callbacks inside.
3053 </para>
3054
3055 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003056 Typical code would be like:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 <example>
3059 <title>Interrupt Handler Case #1</title>
3060 <programlisting>
3061<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003062 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003064 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 spin_lock(&chip->lock);
3066 ....
3067 if (pcm_irq_invoked(chip)) {
3068 /* call updater, unlock before it */
3069 spin_unlock(&chip->lock);
3070 snd_pcm_period_elapsed(chip->substream);
3071 spin_lock(&chip->lock);
Takashi Iwai95a5b082007-07-26 16:50:09 +02003072 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 ....
3075 spin_unlock(&chip->lock);
3076 return IRQ_HANDLED;
3077 }
3078]]>
3079 </programlisting>
3080 </example>
3081 </para>
3082 </section>
3083
3084 <section id="pcm-interface-interrupt-handler-timer">
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003085 <title>High frequency timer interrupts</title>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 <para>
Antonio Ospited4dab5a2013-01-29 12:56:28 +01003087 This happens when the hardware doesn't generate interrupts
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003088 at the period boundary but issues timer interrupts at a fixed
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 timer rate (e.g. es1968 or ymfpci drivers).
3090 In this case, you need to check the current hardware
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003091 position and accumulate the processed sample length at each
3092 interrupt. When the accumulated size exceeds the period
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 size, call
3094 <function>snd_pcm_period_elapsed()</function> and reset the
3095 accumulator.
3096 </para>
3097
3098 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003099 Typical code would be like the following.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
3101 <example>
3102 <title>Interrupt Handler Case #2</title>
3103 <programlisting>
3104<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003105 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003107 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 spin_lock(&chip->lock);
3109 ....
3110 if (pcm_irq_invoked(chip)) {
3111 unsigned int last_ptr, size;
3112 /* get the current hardware pointer (in frames) */
3113 last_ptr = get_hw_ptr(chip);
3114 /* calculate the processed frames since the
3115 * last update
3116 */
3117 if (last_ptr < chip->last_ptr)
3118 size = runtime->buffer_size + last_ptr
3119 - chip->last_ptr;
3120 else
3121 size = last_ptr - chip->last_ptr;
3122 /* remember the last updated point */
3123 chip->last_ptr = last_ptr;
3124 /* accumulate the size */
3125 chip->size += size;
3126 /* over the period boundary? */
3127 if (chip->size >= runtime->period_size) {
3128 /* reset the accumulator */
3129 chip->size %= runtime->period_size;
3130 /* call updater */
3131 spin_unlock(&chip->lock);
3132 snd_pcm_period_elapsed(substream);
3133 spin_lock(&chip->lock);
3134 }
Takashi Iwai95a5b082007-07-26 16:50:09 +02003135 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
3137 ....
3138 spin_unlock(&chip->lock);
3139 return IRQ_HANDLED;
3140 }
3141]]>
3142 </programlisting>
3143 </example>
3144 </para>
3145 </section>
3146
3147 <section id="pcm-interface-interrupt-handler-both">
3148 <title>On calling <function>snd_pcm_period_elapsed()</function></title>
3149 <para>
3150 In both cases, even if more than one period are elapsed, you
3151 don't have to call
3152 <function>snd_pcm_period_elapsed()</function> many times. Call
3153 only once. And the pcm layer will check the current hardware
3154 pointer and update to the latest status.
3155 </para>
3156 </section>
3157 </section>
3158
3159 <section id="pcm-interface-atomicity">
3160 <title>Atomicity</title>
3161 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003162 One of the most important (and thus difficult to debug) problems
3163 in kernel programming are race conditions.
3164 In the Linux kernel, they are usually avoided via spin-locks, mutexes
3165 or semaphores. In general, if a race condition can happen
3166 in an interrupt handler, it has to be managed atomically, and you
3167 have to use a spinlock to protect the critical session. If the
3168 critical section is not in interrupt handler code and
3169 if taking a relatively long time to execute is acceptable, you
3170 should use mutexes or semaphores instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 </para>
3172
3173 <para>
3174 As already seen, some pcm callbacks are atomic and some are
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003175 not. For example, the <parameter>hw_params</parameter> callback is
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 non-atomic, while <parameter>trigger</parameter> callback is
3177 atomic. This means, the latter is called already in a spinlock
3178 held by the PCM middle layer. Please take this atomicity into
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003179 account when you choose a locking scheme in the callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 </para>
3181
3182 <para>
3183 In the atomic callbacks, you cannot use functions which may call
3184 <function>schedule</function> or go to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003185 <function>sleep</function>. Semaphores and mutexes can sleep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 and hence they cannot be used inside the atomic callbacks
3187 (e.g. <parameter>trigger</parameter> callback).
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003188 To implement some delay in such a callback, please use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 <function>udelay()</function> or <function>mdelay()</function>.
3190 </para>
3191
3192 <para>
3193 All three atomic callbacks (trigger, pointer, and ack) are
3194 called with local interrupts disabled.
3195 </para>
3196
3197 </section>
3198 <section id="pcm-interface-constraints">
3199 <title>Constraints</title>
3200 <para>
3201 If your chip supports unconventional sample rates, or only the
3202 limited samples, you need to set a constraint for the
3203 condition.
3204 </para>
3205
3206 <para>
3207 For example, in order to restrict the sample rates in the some
3208 supported values, use
3209 <function>snd_pcm_hw_constraint_list()</function>.
3210 You need to call this function in the open callback.
3211
3212 <example>
3213 <title>Example of Hardware Constraints</title>
3214 <programlisting>
3215<![CDATA[
3216 static unsigned int rates[] =
3217 {4000, 10000, 22050, 44100};
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003218 static struct snd_pcm_hw_constraint_list constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 .count = ARRAY_SIZE(rates),
3220 .list = rates,
3221 .mask = 0,
3222 };
3223
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003224 static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 {
3226 int err;
3227 ....
3228 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
3229 SNDRV_PCM_HW_PARAM_RATE,
3230 &constraints_rates);
3231 if (err < 0)
3232 return err;
3233 ....
3234 }
3235]]>
3236 </programlisting>
3237 </example>
3238 </para>
3239
3240 <para>
3241 There are many different constraints.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003242 Look at <filename>sound/pcm.h</filename> for a complete list.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 You can even define your own constraint rules.
3244 For example, let's suppose my_chip can manage a substream of 1 channel
3245 if and only if the format is S16_LE, otherwise it supports any format
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003246 specified in the <structname>snd_pcm_hardware</structname> structure (or in any
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 other constraint_list). You can build a rule like this:
3248
3249 <example>
3250 <title>Example of Hardware Constraints for Channels</title>
3251 <programlisting>
3252<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003253 static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
3254 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003256 struct snd_interval *c = hw_param_interval(params,
3257 SNDRV_PCM_HW_PARAM_CHANNELS);
3258 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3259 struct snd_mask fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
3261 snd_mask_any(&fmt); /* Init the struct */
3262 if (c->min < 2) {
3263 fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE;
3264 return snd_mask_refine(f, &fmt);
3265 }
3266 return 0;
3267 }
3268]]>
3269 </programlisting>
3270 </example>
3271 </para>
3272
3273 <para>
3274 Then you need to call this function to add your rule:
3275
3276 <informalexample>
3277 <programlisting>
3278<![CDATA[
3279 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3280 hw_rule_channels_by_format, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3281 -1);
3282]]>
3283 </programlisting>
3284 </informalexample>
3285 </para>
3286
3287 <para>
3288 The rule function is called when an application sets the number of
3289 channels. But an application can set the format before the number of
3290 channels. Thus you also need to define the inverse rule:
3291
3292 <example>
3293 <title>Example of Hardware Constraints for Channels</title>
3294 <programlisting>
3295<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003296 static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
3297 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003299 struct snd_interval *c = hw_param_interval(params,
3300 SNDRV_PCM_HW_PARAM_CHANNELS);
3301 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3302 struct snd_interval ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 snd_interval_any(&ch);
3305 if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
3306 ch.min = ch.max = 1;
3307 ch.integer = 1;
3308 return snd_interval_refine(c, &ch);
3309 }
3310 return 0;
3311 }
3312]]>
3313 </programlisting>
3314 </example>
3315 </para>
3316
3317 <para>
3318 ...and in the open callback:
3319 <informalexample>
3320 <programlisting>
3321<![CDATA[
3322 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3323 hw_rule_format_by_channels, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3324 -1);
3325]]>
3326 </programlisting>
3327 </informalexample>
3328 </para>
3329
3330 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003331 I won't give more details here, rather I
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 would like to say, <quote>Luke, use the source.</quote>
3333 </para>
3334 </section>
3335
3336 </chapter>
3337
3338
3339<!-- ****************************************************** -->
3340<!-- Control Interface -->
3341<!-- ****************************************************** -->
3342 <chapter id="control-interface">
3343 <title>Control Interface</title>
3344
3345 <section id="control-interface-general">
3346 <title>General</title>
3347 <para>
3348 The control interface is used widely for many switches,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003349 sliders, etc. which are accessed from user-space. Its most
3350 important use is the mixer interface. In other words, since ALSA
3351 0.9.x, all the mixer stuff is implemented on the control kernel API.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 </para>
3353
3354 <para>
3355 ALSA has a well-defined AC97 control module. If your chip
3356 supports only the AC97 and nothing else, you can skip this
3357 section.
3358 </para>
3359
3360 <para>
3361 The control API is defined in
3362 <filename>&lt;sound/control.h&gt;</filename>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003363 Include this file if you want to add your own controls.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 </para>
3365 </section>
3366
3367 <section id="control-interface-definition">
3368 <title>Definition of Controls</title>
3369 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003370 To create a new control, you need to define the
3371 following three
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 callbacks: <structfield>info</structfield>,
3373 <structfield>get</structfield> and
3374 <structfield>put</structfield>. Then, define a
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003375 struct <structname>snd_kcontrol_new</structname> record, such as:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
3377 <example>
3378 <title>Definition of a Control</title>
3379 <programlisting>
3380<![CDATA[
Takashi Iwai090015a2012-12-07 07:52:28 +01003381 static struct snd_kcontrol_new my_control = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3383 .name = "PCM Playback Switch",
3384 .index = 0,
3385 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003386 .private_value = 0xffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 .info = my_control_info,
3388 .get = my_control_get,
3389 .put = my_control_put
3390 };
3391]]>
3392 </programlisting>
3393 </example>
3394 </para>
3395
3396 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003397 The <structfield>iface</structfield> field specifies the control
3398 type, <constant>SNDRV_CTL_ELEM_IFACE_XXX</constant>, which
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003399 is usually <constant>MIXER</constant>.
3400 Use <constant>CARD</constant> for global controls that are not
3401 logically part of the mixer.
3402 If the control is closely associated with some specific device on
3403 the sound card, use <constant>HWDEP</constant>,
3404 <constant>PCM</constant>, <constant>RAWMIDI</constant>,
3405 <constant>TIMER</constant>, or <constant>SEQUENCER</constant>, and
3406 specify the device number with the
3407 <structfield>device</structfield> and
3408 <structfield>subdevice</structfield> fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 </para>
3410
3411 <para>
3412 The <structfield>name</structfield> is the name identifier
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003413 string. Since ALSA 0.9.x, the control name is very important,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 because its role is classified from its name. There are
3415 pre-defined standard control names. The details are described in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003416 the <link linkend="control-interface-control-names"><citetitle>
3417 Control Names</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 </para>
3419
3420 <para>
3421 The <structfield>index</structfield> field holds the index number
3422 of this control. If there are several different controls with
3423 the same name, they can be distinguished by the index
3424 number. This is the case when
3425 several codecs exist on the card. If the index is zero, you can
3426 omit the definition above.
3427 </para>
3428
3429 <para>
3430 The <structfield>access</structfield> field contains the access
3431 type of this control. Give the combination of bit masks,
3432 <constant>SNDRV_CTL_ELEM_ACCESS_XXX</constant>, there.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003433 The details will be explained in
3434 the <link linkend="control-interface-access-flags"><citetitle>
3435 Access Flags</citetitle></link> subsection.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 </para>
3437
3438 <para>
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003439 The <structfield>private_value</structfield> field contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 an arbitrary long integer value for this record. When using
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003441 the generic <structfield>info</structfield>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 <structfield>get</structfield> and
3443 <structfield>put</structfield> callbacks, you can pass a value
3444 through this field. If several small numbers are necessary, you can
3445 combine them in bitwise. Or, it's possible to give a pointer
3446 (casted to unsigned long) of some record to this field, too.
3447 </para>
3448
3449 <para>
Clemens Ladischd1761d12007-09-10 08:05:19 +02003450 The <structfield>tlv</structfield> field can be used to provide
3451 metadata about the control; see the
3452 <link linkend="control-interface-tlv">
3453 <citetitle>Metadata</citetitle></link> subsection.
3454 </para>
3455
3456 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 The other three are
3458 <link linkend="control-interface-callbacks"><citetitle>
3459 callback functions</citetitle></link>.
3460 </para>
3461 </section>
3462
3463 <section id="control-interface-control-names">
3464 <title>Control Names</title>
3465 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003466 There are some standards to define the control names. A
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 control is usually defined from the three parts as
3468 <quote>SOURCE DIRECTION FUNCTION</quote>.
3469 </para>
3470
3471 <para>
3472 The first, <constant>SOURCE</constant>, specifies the source
3473 of the control, and is a string such as <quote>Master</quote>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003474 <quote>PCM</quote>, <quote>CD</quote> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 <quote>Line</quote>. There are many pre-defined sources.
3476 </para>
3477
3478 <para>
3479 The second, <constant>DIRECTION</constant>, is one of the
3480 following strings according to the direction of the control:
3481 <quote>Playback</quote>, <quote>Capture</quote>, <quote>Bypass
3482 Playback</quote> and <quote>Bypass Capture</quote>. Or, it can
3483 be omitted, meaning both playback and capture directions.
3484 </para>
3485
3486 <para>
3487 The third, <constant>FUNCTION</constant>, is one of the
3488 following strings according to the function of the control:
3489 <quote>Switch</quote>, <quote>Volume</quote> and
3490 <quote>Route</quote>.
3491 </para>
3492
3493 <para>
3494 The example of control names are, thus, <quote>Master Capture
3495 Switch</quote> or <quote>PCM Playback Volume</quote>.
3496 </para>
3497
3498 <para>
3499 There are some exceptions:
3500 </para>
3501
3502 <section id="control-interface-control-names-global">
3503 <title>Global capture and playback</title>
3504 <para>
3505 <quote>Capture Source</quote>, <quote>Capture Switch</quote>
3506 and <quote>Capture Volume</quote> are used for the global
3507 capture (input) source, switch and volume. Similarly,
3508 <quote>Playback Switch</quote> and <quote>Playback
3509 Volume</quote> are used for the global output gain switch and
3510 volume.
3511 </para>
3512 </section>
3513
3514 <section id="control-interface-control-names-tone">
3515 <title>Tone-controls</title>
3516 <para>
3517 tone-control switch and volumes are specified like
3518 <quote>Tone Control - XXX</quote>, e.g. <quote>Tone Control -
3519 Switch</quote>, <quote>Tone Control - Bass</quote>,
3520 <quote>Tone Control - Center</quote>.
3521 </para>
3522 </section>
3523
3524 <section id="control-interface-control-names-3d">
3525 <title>3D controls</title>
3526 <para>
3527 3D-control switches and volumes are specified like <quote>3D
3528 Control - XXX</quote>, e.g. <quote>3D Control -
3529 Switch</quote>, <quote>3D Control - Center</quote>, <quote>3D
3530 Control - Space</quote>.
3531 </para>
3532 </section>
3533
3534 <section id="control-interface-control-names-mic">
3535 <title>Mic boost</title>
3536 <para>
3537 Mic-boost switch is set as <quote>Mic Boost</quote> or
3538 <quote>Mic Boost (6dB)</quote>.
3539 </para>
3540
3541 <para>
3542 More precise information can be found in
3543 <filename>Documentation/sound/alsa/ControlNames.txt</filename>.
3544 </para>
3545 </section>
3546 </section>
3547
3548 <section id="control-interface-access-flags">
3549 <title>Access Flags</title>
3550
3551 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003552 The access flag is the bitmask which specifies the access type
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 of the given control. The default access type is
3554 <constant>SNDRV_CTL_ELEM_ACCESS_READWRITE</constant>,
3555 which means both read and write are allowed to this control.
3556 When the access flag is omitted (i.e. = 0), it is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003557 considered as <constant>READWRITE</constant> access as default.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 </para>
3559
3560 <para>
3561 When the control is read-only, pass
3562 <constant>SNDRV_CTL_ELEM_ACCESS_READ</constant> instead.
3563 In this case, you don't have to define
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003564 the <structfield>put</structfield> callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 Similarly, when the control is write-only (although it's a rare
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003566 case), you can use the <constant>WRITE</constant> flag instead, and
3567 you don't need the <structfield>get</structfield> callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 </para>
3569
3570 <para>
3571 If the control value changes frequently (e.g. the VU meter),
3572 <constant>VOLATILE</constant> flag should be given. This means
3573 that the control may be changed without
3574 <link linkend="control-interface-change-notification"><citetitle>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003575 notification</citetitle></link>. Applications should poll such
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 a control constantly.
3577 </para>
3578
3579 <para>
3580 When the control is inactive, set
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003581 the <constant>INACTIVE</constant> flag, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 There are <constant>LOCK</constant> and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003583 <constant>OWNER</constant> flags to change the write
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 permissions.
3585 </para>
3586
3587 </section>
3588
3589 <section id="control-interface-callbacks">
3590 <title>Callbacks</title>
3591
3592 <section id="control-interface-callbacks-info">
3593 <title>info callback</title>
3594 <para>
3595 The <structfield>info</structfield> callback is used to get
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003596 detailed information on this control. This must store the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003597 values of the given struct <structname>snd_ctl_elem_info</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 object. For example, for a boolean control with a single
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003599 element:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600
3601 <example>
3602 <title>Example of info callback</title>
3603 <programlisting>
3604<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003605 static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003606 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 {
3608 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3609 uinfo->count = 1;
3610 uinfo->value.integer.min = 0;
3611 uinfo->value.integer.max = 1;
3612 return 0;
3613 }
3614]]>
3615 </programlisting>
3616 </example>
3617 </para>
3618
3619 <para>
3620 The <structfield>type</structfield> field specifies the type
3621 of the control. There are <constant>BOOLEAN</constant>,
3622 <constant>INTEGER</constant>, <constant>ENUMERATED</constant>,
3623 <constant>BYTES</constant>, <constant>IEC958</constant> and
3624 <constant>INTEGER64</constant>. The
3625 <structfield>count</structfield> field specifies the
3626 number of elements in this control. For example, a stereo
3627 volume would have count = 2. The
3628 <structfield>value</structfield> field is a union, and
3629 the values stored are depending on the type. The boolean and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003630 integer types are identical.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 </para>
3632
3633 <para>
3634 The enumerated type is a bit different from others. You'll
3635 need to set the string for the currently given item index.
3636
3637 <informalexample>
3638 <programlisting>
3639<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003640 static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003641 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 {
3643 static char *texts[4] = {
3644 "First", "Second", "Third", "Fourth"
3645 };
3646 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3647 uinfo->count = 1;
3648 uinfo->value.enumerated.items = 4;
3649 if (uinfo->value.enumerated.item > 3)
3650 uinfo->value.enumerated.item = 3;
3651 strcpy(uinfo->value.enumerated.name,
3652 texts[uinfo->value.enumerated.item]);
3653 return 0;
3654 }
3655]]>
3656 </programlisting>
3657 </informalexample>
3658 </para>
Takashi Iwai95a5b082007-07-26 16:50:09 +02003659
3660 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003661 Some common info callbacks are available for your convenience:
Takashi Iwai95a5b082007-07-26 16:50:09 +02003662 <function>snd_ctl_boolean_mono_info()</function> and
3663 <function>snd_ctl_boolean_stereo_info()</function>.
3664 Obviously, the former is an info callback for a mono channel
3665 boolean item, just like <function>snd_myctl_mono_info</function>
3666 above, and the latter is for a stereo channel boolean item.
3667 </para>
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 </section>
3670
3671 <section id="control-interface-callbacks-get">
3672 <title>get callback</title>
3673
3674 <para>
3675 This callback is used to read the current value of the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003676 control and to return to user-space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 </para>
3678
3679 <para>
3680 For example,
3681
3682 <example>
3683 <title>Example of get callback</title>
3684 <programlisting>
3685<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003686 static int snd_myctl_get(struct snd_kcontrol *kcontrol,
3687 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003689 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 ucontrol->value.integer.value[0] = get_some_value(chip);
3691 return 0;
3692 }
3693]]>
3694 </programlisting>
3695 </example>
3696 </para>
3697
3698 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003699 The <structfield>value</structfield> field depends on
3700 the type of control as well as on the info callback. For example,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 the sb driver uses this field to store the register offset,
3702 the bit-shift and the bit-mask. The
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003703 <structfield>private_value</structfield> field is set as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 <informalexample>
3705 <programlisting>
3706<![CDATA[
3707 .private_value = reg | (shift << 16) | (mask << 24)
3708]]>
3709 </programlisting>
3710 </informalexample>
3711 and is retrieved in callbacks like
3712 <informalexample>
3713 <programlisting>
3714<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003715 static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol,
3716 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 {
3718 int reg = kcontrol->private_value & 0xff;
3719 int shift = (kcontrol->private_value >> 16) & 0xff;
3720 int mask = (kcontrol->private_value >> 24) & 0xff;
3721 ....
3722 }
3723]]>
3724 </programlisting>
3725 </informalexample>
3726 </para>
3727
3728 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003729 In the <structfield>get</structfield> callback,
3730 you have to fill all the elements if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 control has more than one elements,
3732 i.e. <structfield>count</structfield> &gt; 1.
3733 In the example above, we filled only one element
3734 (<structfield>value.integer.value[0]</structfield>) since it's
3735 assumed as <structfield>count</structfield> = 1.
3736 </para>
3737 </section>
3738
3739 <section id="control-interface-callbacks-put">
3740 <title>put callback</title>
3741
3742 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003743 This callback is used to write a value from user-space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 </para>
3745
3746 <para>
3747 For example,
3748
3749 <example>
3750 <title>Example of put callback</title>
3751 <programlisting>
3752<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003753 static int snd_myctl_put(struct snd_kcontrol *kcontrol,
3754 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003756 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 int changed = 0;
3758 if (chip->current_value !=
3759 ucontrol->value.integer.value[0]) {
3760 change_current_value(chip,
3761 ucontrol->value.integer.value[0]);
3762 changed = 1;
3763 }
3764 return changed;
3765 }
3766]]>
3767 </programlisting>
3768 </example>
3769
3770 As seen above, you have to return 1 if the value is
3771 changed. If the value is not changed, return 0 instead.
3772 If any fatal error happens, return a negative error code as
3773 usual.
3774 </para>
3775
3776 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003777 As in the <structfield>get</structfield> callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 when the control has more than one elements,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003779 all elements must be evaluated in this callback, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 </para>
3781 </section>
3782
3783 <section id="control-interface-callbacks-all">
3784 <title>Callbacks are not atomic</title>
3785 <para>
3786 All these three callbacks are basically not atomic.
3787 </para>
3788 </section>
3789 </section>
3790
3791 <section id="control-interface-constructor">
3792 <title>Constructor</title>
3793 <para>
3794 When everything is ready, finally we can create a new
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003795 control. To create a control, there are two functions to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 called, <function>snd_ctl_new1()</function> and
3797 <function>snd_ctl_add()</function>.
3798 </para>
3799
3800 <para>
3801 In the simplest way, you can do like this:
3802
3803 <informalexample>
3804 <programlisting>
3805<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003806 err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
3807 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return err;
3809]]>
3810 </programlisting>
3811 </informalexample>
3812
3813 where <parameter>my_control</parameter> is the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003814 struct <structname>snd_kcontrol_new</structname> object defined above, and chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 is the object pointer to be passed to
3816 kcontrol-&gt;private_data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003817 which can be referred to in callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 </para>
3819
3820 <para>
3821 <function>snd_ctl_new1()</function> allocates a new
Takashi Iwai090015a2012-12-07 07:52:28 +01003822 <structname>snd_kcontrol</structname> instance,
3823 and <function>snd_ctl_add</function> assigns the given
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 control component to the card.
3825 </para>
3826 </section>
3827
3828 <section id="control-interface-change-notification">
3829 <title>Change Notification</title>
3830 <para>
3831 If you need to change and update a control in the interrupt
3832 routine, you can call <function>snd_ctl_notify()</function>. For
3833 example,
3834
3835 <informalexample>
3836 <programlisting>
3837<![CDATA[
3838 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);
3839]]>
3840 </programlisting>
3841 </informalexample>
3842
3843 This function takes the card pointer, the event-mask, and the
3844 control id pointer for the notification. The event-mask
3845 specifies the types of notification, for example, in the above
3846 example, the change of control values is notified.
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003847 The id pointer is the pointer of struct <structname>snd_ctl_elem_id</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 to be notified.
3849 You can find some examples in <filename>es1938.c</filename> or
3850 <filename>es1968.c</filename> for hardware volume interrupts.
3851 </para>
3852 </section>
3853
Clemens Ladischd1761d12007-09-10 08:05:19 +02003854 <section id="control-interface-tlv">
3855 <title>Metadata</title>
3856 <para>
3857 To provide information about the dB values of a mixer control, use
3858 on of the <constant>DECLARE_TLV_xxx</constant> macros from
3859 <filename>&lt;sound/tlv.h&gt;</filename> to define a variable
3860 containing this information, set the<structfield>tlv.p
3861 </structfield> field to point to this variable, and include the
3862 <constant>SNDRV_CTL_ELEM_ACCESS_TLV_READ</constant> flag in the
3863 <structfield>access</structfield> field; like this:
3864 <informalexample>
3865 <programlisting>
3866<![CDATA[
3867 static DECLARE_TLV_DB_SCALE(db_scale_my_control, -4050, 150, 0);
3868
Takashi Iwai090015a2012-12-07 07:52:28 +01003869 static struct snd_kcontrol_new my_control = {
Clemens Ladischd1761d12007-09-10 08:05:19 +02003870 ...
3871 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
3872 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
3873 ...
3874 .tlv.p = db_scale_my_control,
3875 };
3876]]>
3877 </programlisting>
3878 </informalexample>
3879 </para>
3880
3881 <para>
3882 The <function>DECLARE_TLV_DB_SCALE</function> macro defines
3883 information about a mixer control where each step in the control's
3884 value changes the dB value by a constant dB amount.
3885 The first parameter is the name of the variable to be defined.
3886 The second parameter is the minimum value, in units of 0.01 dB.
3887 The third parameter is the step size, in units of 0.01 dB.
3888 Set the fourth parameter to 1 if the minimum value actually mutes
3889 the control.
3890 </para>
3891
3892 <para>
3893 The <function>DECLARE_TLV_DB_LINEAR</function> macro defines
3894 information about a mixer control where the control's value affects
3895 the output linearly.
3896 The first parameter is the name of the variable to be defined.
3897 The second parameter is the minimum value, in units of 0.01 dB.
3898 The third parameter is the maximum value, in units of 0.01 dB.
3899 If the minimum value mutes the control, set the second parameter to
3900 <constant>TLV_DB_GAIN_MUTE</constant>.
3901 </para>
3902 </section>
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 </chapter>
3905
3906
3907<!-- ****************************************************** -->
3908<!-- API for AC97 Codec -->
3909<!-- ****************************************************** -->
3910 <chapter id="api-ac97">
3911 <title>API for AC97 Codec</title>
3912
3913 <section>
3914 <title>General</title>
3915 <para>
3916 The ALSA AC97 codec layer is a well-defined one, and you don't
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003917 have to write much code to control it. Only low-level control
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 routines are necessary. The AC97 codec API is defined in
3919 <filename>&lt;sound/ac97_codec.h&gt;</filename>.
3920 </para>
3921 </section>
3922
3923 <section id="api-ac97-example">
3924 <title>Full Code Example</title>
3925 <para>
3926 <example>
3927 <title>Example of AC97 Interface</title>
3928 <programlisting>
3929<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003930 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003932 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 ....
3934 };
3935
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003936 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 unsigned short reg)
3938 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003939 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003941 /* read a register value here from the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return the_register_value;
3943 }
3944
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003945 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 unsigned short reg, unsigned short val)
3947 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003948 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003950 /* write the given register value to the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
3952
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003953 static int snd_mychip_ac97(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003955 struct snd_ac97_bus *bus;
3956 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003958 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 .write = snd_mychip_ac97_write,
3960 .read = snd_mychip_ac97_read,
3961 };
3962
Takashi Iwai95a5b082007-07-26 16:50:09 +02003963 err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
3964 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return err;
3966 memset(&ac97, 0, sizeof(ac97));
3967 ac97.private_data = chip;
3968 return snd_ac97_mixer(bus, &ac97, &chip->ac97);
3969 }
3970
3971]]>
3972 </programlisting>
3973 </example>
3974 </para>
3975 </section>
3976
3977 <section id="api-ac97-constructor">
3978 <title>Constructor</title>
3979 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02003980 To create an ac97 instance, first call <function>snd_ac97_bus</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 with an <type>ac97_bus_ops_t</type> record with callback functions.
3982
3983 <informalexample>
3984 <programlisting>
3985<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003986 struct snd_ac97_bus *bus;
3987 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
3992 snd_ac97_bus(card, 0, &ops, NULL, &pbus);
3993]]>
3994 </programlisting>
3995 </informalexample>
3996
3997 The bus record is shared among all belonging ac97 instances.
3998 </para>
3999
4000 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004001 And then call <function>snd_ac97_mixer()</function> with an
4002 struct <structname>snd_ac97_template</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 record together with the bus pointer created above.
4004
4005 <informalexample>
4006 <programlisting>
4007<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004008 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 int err;
4010
4011 memset(&ac97, 0, sizeof(ac97));
4012 ac97.private_data = chip;
4013 snd_ac97_mixer(bus, &ac97, &chip->ac97);
4014]]>
4015 </programlisting>
4016 </informalexample>
4017
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004018 where chip-&gt;ac97 is a pointer to a newly created
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 <type>ac97_t</type> instance.
4020 In this case, the chip pointer is set as the private data, so that
4021 the read/write callback functions can refer to this chip instance.
4022 This instance is not necessarily stored in the chip
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004023 record. If you need to change the register values from the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 driver, or need the suspend/resume of ac97 codecs, keep this
4025 pointer to pass to the corresponding functions.
4026 </para>
4027 </section>
4028
4029 <section id="api-ac97-callbacks">
4030 <title>Callbacks</title>
4031 <para>
4032 The standard callbacks are <structfield>read</structfield> and
4033 <structfield>write</structfield>. Obviously they
4034 correspond to the functions for read and write accesses to the
4035 hardware low-level codes.
4036 </para>
4037
4038 <para>
4039 The <structfield>read</structfield> callback returns the
4040 register value specified in the argument.
4041
4042 <informalexample>
4043 <programlisting>
4044<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004045 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 unsigned short reg)
4047 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004048 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 ....
4050 return the_register_value;
4051 }
4052]]>
4053 </programlisting>
4054 </informalexample>
4055
4056 Here, the chip can be cast from ac97-&gt;private_data.
4057 </para>
4058
4059 <para>
4060 Meanwhile, the <structfield>write</structfield> callback is
4061 used to set the register value.
4062
4063 <informalexample>
4064 <programlisting>
4065<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004066 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 unsigned short reg, unsigned short val)
4068]]>
4069 </programlisting>
4070 </informalexample>
4071 </para>
4072
4073 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004074 These callbacks are non-atomic like the control API callbacks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 </para>
4076
4077 <para>
4078 There are also other callbacks:
4079 <structfield>reset</structfield>,
4080 <structfield>wait</structfield> and
4081 <structfield>init</structfield>.
4082 </para>
4083
4084 <para>
4085 The <structfield>reset</structfield> callback is used to reset
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004086 the codec. If the chip requires a special kind of reset, you can
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 define this callback.
4088 </para>
4089
4090 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004091 The <structfield>wait</structfield> callback is used to
4092 add some waiting time in the standard initialization of the codec. If the
4093 chip requires the extra waiting time, define this callback.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 </para>
4095
4096 <para>
4097 The <structfield>init</structfield> callback is used for
4098 additional initialization of the codec.
4099 </para>
4100 </section>
4101
4102 <section id="api-ac97-updating-registers">
4103 <title>Updating Registers in The Driver</title>
4104 <para>
4105 If you need to access to the codec from the driver, you can
4106 call the following functions:
4107 <function>snd_ac97_write()</function>,
4108 <function>snd_ac97_read()</function>,
4109 <function>snd_ac97_update()</function> and
4110 <function>snd_ac97_update_bits()</function>.
4111 </para>
4112
4113 <para>
4114 Both <function>snd_ac97_write()</function> and
4115 <function>snd_ac97_update()</function> functions are used to
4116 set a value to the given register
4117 (<constant>AC97_XXX</constant>). The difference between them is
4118 that <function>snd_ac97_update()</function> doesn't write a
4119 value if the given value has been already set, while
4120 <function>snd_ac97_write()</function> always rewrites the
4121 value.
4122
4123 <informalexample>
4124 <programlisting>
4125<![CDATA[
4126 snd_ac97_write(ac97, AC97_MASTER, 0x8080);
4127 snd_ac97_update(ac97, AC97_MASTER, 0x8080);
4128]]>
4129 </programlisting>
4130 </informalexample>
4131 </para>
4132
4133 <para>
4134 <function>snd_ac97_read()</function> is used to read the value
4135 of the given register. For example,
4136
4137 <informalexample>
4138 <programlisting>
4139<![CDATA[
4140 value = snd_ac97_read(ac97, AC97_MASTER);
4141]]>
4142 </programlisting>
4143 </informalexample>
4144 </para>
4145
4146 <para>
4147 <function>snd_ac97_update_bits()</function> is used to update
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004148 some bits in the given register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150 <informalexample>
4151 <programlisting>
4152<![CDATA[
4153 snd_ac97_update_bits(ac97, reg, mask, value);
4154]]>
4155 </programlisting>
4156 </informalexample>
4157 </para>
4158
4159 <para>
4160 Also, there is a function to change the sample rate (of a
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004161 given register such as
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 <constant>AC97_PCM_FRONT_DAC_RATE</constant>) when VRA or
4163 DRA is supported by the codec:
4164 <function>snd_ac97_set_rate()</function>.
4165
4166 <informalexample>
4167 <programlisting>
4168<![CDATA[
4169 snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);
4170]]>
4171 </programlisting>
4172 </informalexample>
4173 </para>
4174
4175 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004176 The following registers are available to set the rate:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 <constant>AC97_PCM_MIC_ADC_RATE</constant>,
4178 <constant>AC97_PCM_FRONT_DAC_RATE</constant>,
4179 <constant>AC97_PCM_LR_ADC_RATE</constant>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004180 <constant>AC97_SPDIF</constant>. When
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 <constant>AC97_SPDIF</constant> is specified, the register is
4182 not really changed but the corresponding IEC958 status bits will
4183 be updated.
4184 </para>
4185 </section>
4186
4187 <section id="api-ac97-clock-adjustment">
4188 <title>Clock Adjustment</title>
4189 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004190 In some chips, the clock of the codec isn't 48000 but using a
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 PCI clock (to save a quartz!). In this case, change the field
4192 bus-&gt;clock to the corresponding
4193 value. For example, intel8x0
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004194 and es1968 drivers have their own function to read from the clock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 </para>
4196 </section>
4197
4198 <section id="api-ac97-proc-files">
4199 <title>Proc Files</title>
4200 <para>
4201 The ALSA AC97 interface will create a proc file such as
4202 <filename>/proc/asound/card0/codec97#0/ac97#0-0</filename> and
4203 <filename>ac97#0-0+regs</filename>. You can refer to these files to
4204 see the current status and registers of the codec.
4205 </para>
4206 </section>
4207
4208 <section id="api-ac97-multiple-codecs">
4209 <title>Multiple Codecs</title>
4210 <para>
4211 When there are several codecs on the same card, you need to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004212 call <function>snd_ac97_mixer()</function> multiple times with
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 ac97.num=1 or greater. The <structfield>num</structfield> field
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004214 specifies the codec number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 </para>
4216
4217 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004218 If you set up multiple codecs, you either need to write
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 different callbacks for each codec or check
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004220 ac97-&gt;num in the callback routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 </para>
4222 </section>
4223
4224 </chapter>
4225
4226
4227<!-- ****************************************************** -->
4228<!-- MIDI (MPU401-UART) Interface -->
4229<!-- ****************************************************** -->
4230 <chapter id="midi-interface">
4231 <title>MIDI (MPU401-UART) Interface</title>
4232
4233 <section id="midi-interface-general">
4234 <title>General</title>
4235 <para>
4236 Many soundcards have built-in MIDI (MPU401-UART)
4237 interfaces. When the soundcard supports the standard MPU401-UART
4238 interface, most likely you can use the ALSA MPU401-UART API. The
4239 MPU401-UART API is defined in
4240 <filename>&lt;sound/mpu401.h&gt;</filename>.
4241 </para>
4242
4243 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004244 Some soundchips have a similar but slightly different
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 implementation of mpu401 stuff. For example, emu10k1 has its own
4246 mpu401 routines.
4247 </para>
4248 </section>
4249
4250 <section id="midi-interface-constructor">
4251 <title>Constructor</title>
4252 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004253 To create a rawmidi object, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 <function>snd_mpu401_uart_new()</function>.
4255
4256 <informalexample>
4257 <programlisting>
4258<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004259 struct snd_rawmidi *rmidi;
Takashi Iwai302e4c22006-05-23 13:24:30 +02004260 snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags,
Clemens Ladischdba8b462011-09-13 11:24:41 +02004261 irq, &rmidi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262]]>
4263 </programlisting>
4264 </informalexample>
4265 </para>
4266
4267 <para>
4268 The first argument is the card pointer, and the second is the
4269 index of this component. You can create up to 8 rawmidi
4270 devices.
4271 </para>
4272
4273 <para>
4274 The third argument is the type of the hardware,
4275 <constant>MPU401_HW_XXX</constant>. If it's not a special one,
4276 you can use <constant>MPU401_HW_MPU401</constant>.
4277 </para>
4278
4279 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004280 The 4th argument is the I/O port address. Many
4281 backward-compatible MPU401 have an I/O port such as 0x330. Or, it
4282 might be a part of its own PCI I/O region. It depends on the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 chip design.
4284 </para>
4285
4286 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004287 The 5th argument is a bitflag for additional information.
4288 When the I/O port address above is part of the PCI I/O
4289 region, the MPU401 I/O port might have been already allocated
Takashi Iwai302e4c22006-05-23 13:24:30 +02004290 (reserved) by the driver itself. In such a case, pass a bit flag
4291 <constant>MPU401_INFO_INTEGRATED</constant>,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004292 and the mpu401-uart layer will allocate the I/O ports by itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 </para>
4294
Takashi Iwai302e4c22006-05-23 13:24:30 +02004295 <para>
4296 When the controller supports only the input or output MIDI stream,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004297 pass the <constant>MPU401_INFO_INPUT</constant> or
Takashi Iwai302e4c22006-05-23 13:24:30 +02004298 <constant>MPU401_INFO_OUTPUT</constant> bitflag, respectively.
4299 Then the rawmidi instance is created as a single stream.
4300 </para>
4301
4302 <para>
4303 <constant>MPU401_INFO_MMIO</constant> bitflag is used to change
4304 the access method to MMIO (via readb and writeb) instead of
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004305 iob and outb. In this case, you have to pass the iomapped address
Takashi Iwai302e4c22006-05-23 13:24:30 +02004306 to <function>snd_mpu401_uart_new()</function>.
4307 </para>
4308
4309 <para>
4310 When <constant>MPU401_INFO_TX_IRQ</constant> is set, the output
4311 stream isn't checked in the default interrupt handler. The driver
4312 needs to call <function>snd_mpu401_uart_interrupt_tx()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004313 by itself to start processing the output stream in the irq handler.
Takashi Iwai302e4c22006-05-23 13:24:30 +02004314 </para>
4315
Clemens Ladischdba8b462011-09-13 11:24:41 +02004316 <para>
4317 If the MPU-401 interface shares its interrupt with the other logical
4318 devices on the card, set <constant>MPU401_INFO_IRQ_HOOK</constant>
4319 (see <link linkend="midi-interface-interrupt-handler"><citetitle>
4320 below</citetitle></link>).
4321 </para>
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 <para>
4324 Usually, the port address corresponds to the command port and
4325 port + 1 corresponds to the data port. If not, you may change
4326 the <structfield>cport</structfield> field of
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004327 struct <structname>snd_mpu401</structname> manually
4328 afterward. However, <structname>snd_mpu401</structname> pointer is not
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 returned explicitly by
4330 <function>snd_mpu401_uart_new()</function>. You need to cast
4331 rmidi-&gt;private_data to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004332 <structname>snd_mpu401</structname> explicitly,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
4334 <informalexample>
4335 <programlisting>
4336<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004337 struct snd_mpu401 *mpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 mpu = rmidi->private_data;
4339]]>
4340 </programlisting>
4341 </informalexample>
4342
4343 and reset the cport as you like:
4344
4345 <informalexample>
4346 <programlisting>
4347<![CDATA[
4348 mpu->cport = my_own_control_port;
4349]]>
4350 </programlisting>
4351 </informalexample>
4352 </para>
4353
4354 <para>
Clemens Ladischdba8b462011-09-13 11:24:41 +02004355 The 6th argument specifies the ISA irq number that will be
4356 allocated. If no interrupt is to be allocated (because your
4357 code is already allocating a shared interrupt, or because the
4358 device does not use interrupts), pass -1 instead.
4359 For a MPU-401 device without an interrupt, a polling timer
4360 will be used instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 </para>
4362 </section>
4363
4364 <section id="midi-interface-interrupt-handler">
4365 <title>Interrupt Handler</title>
4366 <para>
4367 When the interrupt is allocated in
Clemens Ladischdba8b462011-09-13 11:24:41 +02004368 <function>snd_mpu401_uart_new()</function>, an exclusive ISA
4369 interrupt handler is automatically used, hence you don't have
4370 anything else to do than creating the mpu401 stuff. Otherwise, you
4371 have to set <constant>MPU401_INFO_IRQ_HOOK</constant>, and call
4372 <function>snd_mpu401_uart_interrupt()</function> explicitly from your
4373 own interrupt handler when it has determined that a UART interrupt
4374 has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 </para>
4376
4377 <para>
4378 In this case, you need to pass the private_data of the
4379 returned rawmidi object from
4380 <function>snd_mpu401_uart_new()</function> as the second
4381 argument of <function>snd_mpu401_uart_interrupt()</function>.
4382
4383 <informalexample>
4384 <programlisting>
4385<![CDATA[
4386 snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);
4387]]>
4388 </programlisting>
4389 </informalexample>
4390 </para>
4391 </section>
4392
4393 </chapter>
4394
4395
4396<!-- ****************************************************** -->
4397<!-- RawMIDI Interface -->
4398<!-- ****************************************************** -->
4399 <chapter id="rawmidi-interface">
4400 <title>RawMIDI Interface</title>
4401
4402 <section id="rawmidi-interface-overview">
4403 <title>Overview</title>
4404
4405 <para>
4406 The raw MIDI interface is used for hardware MIDI ports that can
4407 be accessed as a byte stream. It is not used for synthesizer
4408 chips that do not directly understand MIDI.
4409 </para>
4410
4411 <para>
4412 ALSA handles file and buffer management. All you have to do is
4413 to write some code to move data between the buffer and the
4414 hardware.
4415 </para>
4416
4417 <para>
4418 The rawmidi API is defined in
4419 <filename>&lt;sound/rawmidi.h&gt;</filename>.
4420 </para>
4421 </section>
4422
4423 <section id="rawmidi-interface-constructor">
4424 <title>Constructor</title>
4425
4426 <para>
4427 To create a rawmidi device, call the
4428 <function>snd_rawmidi_new</function> function:
4429 <informalexample>
4430 <programlisting>
4431<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004432 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi);
4434 if (err < 0)
4435 return err;
4436 rmidi->private_data = chip;
4437 strcpy(rmidi->name, "My MIDI");
4438 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
4439 SNDRV_RAWMIDI_INFO_INPUT |
4440 SNDRV_RAWMIDI_INFO_DUPLEX;
4441]]>
4442 </programlisting>
4443 </informalexample>
4444 </para>
4445
4446 <para>
4447 The first argument is the card pointer, the second argument is
4448 the ID string.
4449 </para>
4450
4451 <para>
4452 The third argument is the index of this component. You can
4453 create up to 8 rawmidi devices.
4454 </para>
4455
4456 <para>
4457 The fourth and fifth arguments are the number of output and
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004458 input substreams, respectively, of this device (a substream is
4459 the equivalent of a MIDI port).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 </para>
4461
4462 <para>
4463 Set the <structfield>info_flags</structfield> field to specify
4464 the capabilities of the device.
4465 Set <constant>SNDRV_RAWMIDI_INFO_OUTPUT</constant> if there is
4466 at least one output port,
4467 <constant>SNDRV_RAWMIDI_INFO_INPUT</constant> if there is at
4468 least one input port,
4469 and <constant>SNDRV_RAWMIDI_INFO_DUPLEX</constant> if the device
4470 can handle output and input at the same time.
4471 </para>
4472
4473 <para>
4474 After the rawmidi device is created, you need to set the
4475 operators (callbacks) for each substream. There are helper
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004476 functions to set the operators for all the substreams of a device:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 <informalexample>
4478 <programlisting>
4479<![CDATA[
4480 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops);
4481 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);
4482]]>
4483 </programlisting>
4484 </informalexample>
4485 </para>
4486
4487 <para>
4488 The operators are usually defined like this:
4489 <informalexample>
4490 <programlisting>
4491<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004492 static struct snd_rawmidi_ops snd_mymidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 .open = snd_mymidi_output_open,
4494 .close = snd_mymidi_output_close,
4495 .trigger = snd_mymidi_output_trigger,
4496 };
4497]]>
4498 </programlisting>
4499 </informalexample>
4500 These callbacks are explained in the <link
4501 linkend="rawmidi-interface-callbacks"><citetitle>Callbacks</citetitle></link>
4502 section.
4503 </para>
4504
4505 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004506 If there are more than one substream, you should give a
4507 unique name to each of them:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 <informalexample>
4509 <programlisting>
4510<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004511 struct snd_rawmidi_substream *substream;
Takashi Iwai95a5b082007-07-26 16:50:09 +02004512 list_for_each_entry(substream,
4513 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
4514 list {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 sprintf(substream->name, "My MIDI Port %d", substream->number + 1);
4516 }
4517 /* same for SNDRV_RAWMIDI_STREAM_INPUT */
4518]]>
4519 </programlisting>
4520 </informalexample>
4521 </para>
4522 </section>
4523
4524 <section id="rawmidi-interface-callbacks">
4525 <title>Callbacks</title>
4526
4527 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004528 In all the callbacks, the private data that you've set for the
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 rawmidi device can be accessed as
4530 substream-&gt;rmidi-&gt;private_data.
4531 <!-- <code> isn't available before DocBook 4.3 -->
4532 </para>
4533
4534 <para>
4535 If there is more than one port, your callbacks can determine the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004536 port index from the struct snd_rawmidi_substream data passed to each
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 callback:
4538 <informalexample>
4539 <programlisting>
4540<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004541 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 int index = substream->number;
4543]]>
4544 </programlisting>
4545 </informalexample>
4546 </para>
4547
4548 <section id="rawmidi-interface-op-open">
4549 <title><function>open</function> callback</title>
4550
4551 <informalexample>
4552 <programlisting>
4553<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004554 static int snd_xxx_open(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555]]>
4556 </programlisting>
4557 </informalexample>
4558
4559 <para>
4560 This is called when a substream is opened.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004561 You can initialize the hardware here, but you shouldn't
4562 start transmitting/receiving data yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 </para>
4564 </section>
4565
4566 <section id="rawmidi-interface-op-close">
4567 <title><function>close</function> callback</title>
4568
4569 <informalexample>
4570 <programlisting>
4571<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004572 static int snd_xxx_close(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573]]>
4574 </programlisting>
4575 </informalexample>
4576
4577 <para>
4578 Guess what.
4579 </para>
4580
4581 <para>
4582 The <function>open</function> and <function>close</function>
4583 callbacks of a rawmidi device are serialized with a mutex,
4584 and can sleep.
4585 </para>
4586 </section>
4587
4588 <section id="rawmidi-interface-op-trigger-out">
4589 <title><function>trigger</function> callback for output
4590 substreams</title>
4591
4592 <informalexample>
4593 <programlisting>
4594<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004595 static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596]]>
4597 </programlisting>
4598 </informalexample>
4599
4600 <para>
4601 This is called with a nonzero <parameter>up</parameter>
4602 parameter when there is some data in the substream buffer that
4603 must be transmitted.
4604 </para>
4605
4606 <para>
4607 To read data from the buffer, call
4608 <function>snd_rawmidi_transmit_peek</function>. It will
4609 return the number of bytes that have been read; this will be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004610 less than the number of bytes requested when there are no more
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 data in the buffer.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004612 After the data have been transmitted successfully, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 <function>snd_rawmidi_transmit_ack</function> to remove the
4614 data from the substream buffer:
4615 <informalexample>
4616 <programlisting>
4617<![CDATA[
4618 unsigned char data;
4619 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004620 if (snd_mychip_try_to_transmit(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 snd_rawmidi_transmit_ack(substream, 1);
4622 else
4623 break; /* hardware FIFO full */
4624 }
4625]]>
4626 </programlisting>
4627 </informalexample>
4628 </para>
4629
4630 <para>
4631 If you know beforehand that the hardware will accept data, you
4632 can use the <function>snd_rawmidi_transmit</function> function
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004633 which reads some data and removes them from the buffer at once:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 <informalexample>
4635 <programlisting>
4636<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004637 while (snd_mychip_transmit_possible()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 unsigned char data;
4639 if (snd_rawmidi_transmit(substream, &data, 1) != 1)
4640 break; /* no more data */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004641 snd_mychip_transmit(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
4643]]>
4644 </programlisting>
4645 </informalexample>
4646 </para>
4647
4648 <para>
4649 If you know beforehand how many bytes you can accept, you can
4650 use a buffer size greater than one with the
4651 <function>snd_rawmidi_transmit*</function> functions.
4652 </para>
4653
4654 <para>
4655 The <function>trigger</function> callback must not sleep. If
4656 the hardware FIFO is full before the substream buffer has been
4657 emptied, you have to continue transmitting data later, either
4658 in an interrupt handler, or with a timer if the hardware
4659 doesn't have a MIDI transmit interrupt.
4660 </para>
4661
4662 <para>
4663 The <function>trigger</function> callback is called with a
4664 zero <parameter>up</parameter> parameter when the transmission
4665 of data should be aborted.
4666 </para>
4667 </section>
4668
4669 <section id="rawmidi-interface-op-trigger-in">
4670 <title><function>trigger</function> callback for input
4671 substreams</title>
4672
4673 <informalexample>
4674 <programlisting>
4675<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004676 static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677]]>
4678 </programlisting>
4679 </informalexample>
4680
4681 <para>
4682 This is called with a nonzero <parameter>up</parameter>
4683 parameter to enable receiving data, or with a zero
4684 <parameter>up</parameter> parameter do disable receiving data.
4685 </para>
4686
4687 <para>
4688 The <function>trigger</function> callback must not sleep; the
4689 actual reading of data from the device is usually done in an
4690 interrupt handler.
4691 </para>
4692
4693 <para>
4694 When data reception is enabled, your interrupt handler should
4695 call <function>snd_rawmidi_receive</function> for all received
4696 data:
4697 <informalexample>
4698 <programlisting>
4699<![CDATA[
4700 void snd_mychip_midi_interrupt(...)
4701 {
4702 while (mychip_midi_available()) {
4703 unsigned char data;
4704 data = mychip_midi_read();
4705 snd_rawmidi_receive(substream, &data, 1);
4706 }
4707 }
4708]]>
4709 </programlisting>
4710 </informalexample>
4711 </para>
4712 </section>
4713
4714 <section id="rawmidi-interface-op-drain">
4715 <title><function>drain</function> callback</title>
4716
4717 <informalexample>
4718 <programlisting>
4719<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004720 static void snd_xxx_drain(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721]]>
4722 </programlisting>
4723 </informalexample>
4724
4725 <para>
4726 This is only used with output substreams. This function should wait
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004727 until all data read from the substream buffer have been transmitted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 This ensures that the device can be closed and the driver unloaded
4729 without losing data.
4730 </para>
4731
4732 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004733 This callback is optional. If you do not set
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004734 <structfield>drain</structfield> in the struct snd_rawmidi_ops
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 structure, ALSA will simply wait for 50&nbsp;milliseconds
4736 instead.
4737 </para>
4738 </section>
4739 </section>
4740
4741 </chapter>
4742
4743
4744<!-- ****************************************************** -->
4745<!-- Miscellaneous Devices -->
4746<!-- ****************************************************** -->
4747 <chapter id="misc-devices">
4748 <title>Miscellaneous Devices</title>
4749
4750 <section id="misc-devices-opl3">
4751 <title>FM OPL3</title>
4752 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004753 The FM OPL3 is still used in many chips (mainly for backward
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 compatibility). ALSA has a nice OPL3 FM control layer, too. The
4755 OPL3 API is defined in
4756 <filename>&lt;sound/opl3.h&gt;</filename>.
4757 </para>
4758
4759 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004760 FM registers can be directly accessed through the direct-FM API,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 defined in <filename>&lt;sound/asound_fm.h&gt;</filename>. In
4762 ALSA native mode, FM registers are accessed through
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004763 the Hardware-Dependent Device direct-FM extension API, whereas in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004764 OSS compatible mode, FM registers can be accessed with the OSS
4765 direct-FM compatible API in <filename>/dev/dmfmX</filename> device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 </para>
4767
4768 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004769 To create the OPL3 component, you have two functions to
4770 call. The first one is a constructor for the <type>opl3_t</type>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 instance.
4772
4773 <informalexample>
4774 <programlisting>
4775<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004776 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX,
4778 integrated, &opl3);
4779]]>
4780 </programlisting>
4781 </informalexample>
4782 </para>
4783
4784 <para>
4785 The first argument is the card pointer, the second one is the
4786 left port address, and the third is the right port address. In
4787 most cases, the right port is placed at the left port + 2.
4788 </para>
4789
4790 <para>
4791 The fourth argument is the hardware type.
4792 </para>
4793
4794 <para>
4795 When the left and right ports have been already allocated by
4796 the card driver, pass non-zero to the fifth argument
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004797 (<parameter>integrated</parameter>). Otherwise, the opl3 module will
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 allocate the specified ports by itself.
4799 </para>
4800
4801 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004802 When the accessing the hardware requires special method
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 instead of the standard I/O access, you can create opl3 instance
4804 separately with <function>snd_opl3_new()</function>.
4805
4806 <informalexample>
4807 <programlisting>
4808<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004809 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);
4811]]>
4812 </programlisting>
4813 </informalexample>
4814 </para>
4815
4816 <para>
4817 Then set <structfield>command</structfield>,
4818 <structfield>private_data</structfield> and
4819 <structfield>private_free</structfield> for the private
4820 access function, the private data and the destructor.
4821 The l_port and r_port are not necessarily set. Only the
4822 command must be set properly. You can retrieve the data
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004823 from the opl3-&gt;private_data field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 </para>
4825
4826 <para>
4827 After creating the opl3 instance via <function>snd_opl3_new()</function>,
4828 call <function>snd_opl3_init()</function> to initialize the chip to the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004829 proper state. Note that <function>snd_opl3_create()</function> always
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 calls it internally.
4831 </para>
4832
4833 <para>
4834 If the opl3 instance is created successfully, then create a
4835 hwdep device for this opl3.
4836
4837 <informalexample>
4838 <programlisting>
4839<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004840 struct snd_hwdep *opl3hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);
4842]]>
4843 </programlisting>
4844 </informalexample>
4845 </para>
4846
4847 <para>
4848 The first argument is the <type>opl3_t</type> instance you
4849 created, and the second is the index number, usually 0.
4850 </para>
4851
4852 <para>
4853 The third argument is the index-offset for the sequencer
4854 client assigned to the OPL3 port. When there is an MPU401-UART,
4855 give 1 for here (UART always takes 0).
4856 </para>
4857 </section>
4858
4859 <section id="misc-devices-hardware-dependent">
4860 <title>Hardware-Dependent Devices</title>
4861 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004862 Some chips need user-space access for special
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 controls or for loading the micro code. In such a case, you can
4864 create a hwdep (hardware-dependent) device. The hwdep API is
4865 defined in <filename>&lt;sound/hwdep.h&gt;</filename>. You can
4866 find examples in opl3 driver or
4867 <filename>isa/sb/sb16_csp.c</filename>.
4868 </para>
4869
4870 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004871 The creation of the <type>hwdep</type> instance is done via
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 <function>snd_hwdep_new()</function>.
4873
4874 <informalexample>
4875 <programlisting>
4876<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004877 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 snd_hwdep_new(card, "My HWDEP", 0, &hw);
4879]]>
4880 </programlisting>
4881 </informalexample>
4882
4883 where the third argument is the index number.
4884 </para>
4885
4886 <para>
4887 You can then pass any pointer value to the
4888 <parameter>private_data</parameter>.
4889 If you assign a private data, you should define the
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004890 destructor, too. The destructor function is set in
4891 the <structfield>private_free</structfield> field.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 <informalexample>
4894 <programlisting>
4895<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004896 struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 hw->private_data = p;
4898 hw->private_free = mydata_free;
4899]]>
4900 </programlisting>
4901 </informalexample>
4902
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004903 and the implementation of the destructor would be:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 <informalexample>
4906 <programlisting>
4907<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004908 static void mydata_free(struct snd_hwdep *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004910 struct mydata *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 kfree(p);
4912 }
4913]]>
4914 </programlisting>
4915 </informalexample>
4916 </para>
4917
4918 <para>
4919 The arbitrary file operations can be defined for this
4920 instance. The file operators are defined in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004921 the <parameter>ops</parameter> table. For example, assume that
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 this chip needs an ioctl.
4923
4924 <informalexample>
4925 <programlisting>
4926<![CDATA[
4927 hw->ops.open = mydata_open;
4928 hw->ops.ioctl = mydata_ioctl;
4929 hw->ops.release = mydata_release;
4930]]>
4931 </programlisting>
4932 </informalexample>
4933
4934 And implement the callback functions as you like.
4935 </para>
4936 </section>
4937
4938 <section id="misc-devices-IEC958">
4939 <title>IEC958 (S/PDIF)</title>
4940 <para>
4941 Usually the controls for IEC958 devices are implemented via
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004942 the control interface. There is a macro to compose a name string for
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 IEC958 controls, <function>SNDRV_CTL_NAME_IEC958()</function>
4944 defined in <filename>&lt;include/asound.h&gt;</filename>.
4945 </para>
4946
4947 <para>
4948 There are some standard controls for IEC958 status bits. These
4949 controls use the type <type>SNDRV_CTL_ELEM_TYPE_IEC958</type>,
4950 and the size of element is fixed as 4 bytes array
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004951 (value.iec958.status[x]). For the <structfield>info</structfield>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 callback, you don't specify
4953 the value field for this type (the count field must be set,
4954 though).
4955 </para>
4956
4957 <para>
4958 <quote>IEC958 Playback Con Mask</quote> is used to return the
4959 bit-mask for the IEC958 status bits of consumer mode. Similarly,
4960 <quote>IEC958 Playback Pro Mask</quote> returns the bitmask for
4961 professional mode. They are read-only controls, and are defined
4962 as MIXER controls (iface =
4963 <constant>SNDRV_CTL_ELEM_IFACE_MIXER</constant>).
4964 </para>
4965
4966 <para>
4967 Meanwhile, <quote>IEC958 Playback Default</quote> control is
4968 defined for getting and setting the current default IEC958
4969 bits. Note that this one is usually defined as a PCM control
4970 (iface = <constant>SNDRV_CTL_ELEM_IFACE_PCM</constant>),
4971 although in some places it's defined as a MIXER control.
4972 </para>
4973
4974 <para>
4975 In addition, you can define the control switches to
4976 enable/disable or to set the raw bit mode. The implementation
4977 will depend on the chip, but the control should be named as
4978 <quote>IEC958 xxx</quote>, preferably using
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02004979 the <function>SNDRV_CTL_NAME_IEC958()</function> macro.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 </para>
4981
4982 <para>
4983 You can find several cases, for example,
4984 <filename>pci/emu10k1</filename>,
4985 <filename>pci/ice1712</filename>, or
4986 <filename>pci/cmipci.c</filename>.
4987 </para>
4988 </section>
4989
4990 </chapter>
4991
4992
4993<!-- ****************************************************** -->
4994<!-- Buffer and Memory Management -->
4995<!-- ****************************************************** -->
4996 <chapter id="buffer-and-memory">
4997 <title>Buffer and Memory Management</title>
4998
4999 <section id="buffer-and-memory-buffer-types">
5000 <title>Buffer Types</title>
5001 <para>
5002 ALSA provides several different buffer allocation functions
5003 depending on the bus and the architecture. All these have a
5004 consistent API. The allocation of physically-contiguous pages is
5005 done via
5006 <function>snd_malloc_xxx_pages()</function> function, where xxx
5007 is the bus type.
5008 </para>
5009
5010 <para>
5011 The allocation of pages with fallback is
5012 <function>snd_malloc_xxx_pages_fallback()</function>. This
5013 function tries to allocate the specified pages but if the pages
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005014 are not available, it tries to reduce the page sizes until
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 enough space is found.
5016 </para>
5017
5018 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005019 The release the pages, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 <function>snd_free_xxx_pages()</function> function.
5021 </para>
5022
5023 <para>
5024 Usually, ALSA drivers try to allocate and reserve
5025 a large contiguous physical space
5026 at the time the module is loaded for the later use.
5027 This is called <quote>pre-allocation</quote>.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005028 As already written, you can call the following function at
5029 pcm instance construction time (in the case of PCI bus).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
5031 <informalexample>
5032 <programlisting>
5033<![CDATA[
5034 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
5035 snd_dma_pci_data(pci), size, max);
5036]]>
5037 </programlisting>
5038 </informalexample>
5039
5040 where <parameter>size</parameter> is the byte size to be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005041 pre-allocated and the <parameter>max</parameter> is the maximum
5042 size to be changed via the <filename>prealloc</filename> proc file.
5043 The allocator will try to get an area as large as possible
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 within the given size.
5045 </para>
5046
5047 <para>
5048 The second argument (type) and the third argument (device pointer)
5049 are dependent on the bus.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005050 In the case of the ISA bus, pass <function>snd_dma_isa_data()</function>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 as the third argument with <constant>SNDRV_DMA_TYPE_DEV</constant> type.
5052 For the continuous buffer unrelated to the bus can be pre-allocated
5053 with <constant>SNDRV_DMA_TYPE_CONTINUOUS</constant> type and the
5054 <function>snd_dma_continuous_data(GFP_KERNEL)</function> device pointer,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005055 where <constant>GFP_KERNEL</constant> is the kernel allocation flag to
David S. Miller759ee812008-08-27 00:33:26 -07005056 use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 For the PCI scatter-gather buffers, use
5058 <constant>SNDRV_DMA_TYPE_DEV_SG</constant> with
5059 <function>snd_dma_pci_data(pci)</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005060 (see the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 <link linkend="buffer-and-memory-non-contiguous"><citetitle>Non-Contiguous Buffers
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005062 </citetitle></link> section).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 </para>
5064
5065 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005066 Once the buffer is pre-allocated, you can use the
5067 allocator in the <structfield>hw_params</structfield> callback:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 <informalexample>
5070 <programlisting>
5071<![CDATA[
5072 snd_pcm_lib_malloc_pages(substream, size);
5073]]>
5074 </programlisting>
5075 </informalexample>
5076
5077 Note that you have to pre-allocate to use this function.
5078 </para>
5079 </section>
5080
5081 <section id="buffer-and-memory-external-hardware">
5082 <title>External Hardware Buffers</title>
5083 <para>
5084 Some chips have their own hardware buffers and the DMA
5085 transfer from the host memory is not available. In such a case,
5086 you need to either 1) copy/set the audio data directly to the
5087 external hardware buffer, or 2) make an intermediate buffer and
5088 copy/set the data from it to the external hardware buffer in
5089 interrupts (or in tasklets, preferably).
5090 </para>
5091
5092 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005093 The first case works fine if the external hardware buffer is large
5094 enough. This method doesn't need any extra buffers and thus is
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 more effective. You need to define the
5096 <structfield>copy</structfield> and
5097 <structfield>silence</structfield> callbacks for
5098 the data transfer. However, there is a drawback: it cannot
5099 be mmapped. The examples are GUS's GF1 PCM or emu8000's
5100 wavetable PCM.
5101 </para>
5102
5103 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005104 The second case allows for mmap on the buffer, although you have
5105 to handle an interrupt or a tasklet to transfer the data
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 from the intermediate buffer to the hardware buffer. You can find an
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005107 example in the vxpocket driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 </para>
5109
5110 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005111 Another case is when the chip uses a PCI memory-map
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 region for the buffer instead of the host memory. In this case,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005113 mmap is available only on certain architectures like the Intel one.
5114 In non-mmap mode, the data cannot be transferred as in the normal
5115 way. Thus you need to define the <structfield>copy</structfield> and
5116 <structfield>silence</structfield> callbacks as well,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 as in the cases above. The examples are found in
5118 <filename>rme32.c</filename> and <filename>rme96.c</filename>.
5119 </para>
5120
5121 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005122 The implementation of the <structfield>copy</structfield> and
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 <structfield>silence</structfield> callbacks depends upon
5124 whether the hardware supports interleaved or non-interleaved
5125 samples. The <structfield>copy</structfield> callback is
5126 defined like below, a bit
5127 differently depending whether the direction is playback or
5128 capture:
5129
5130 <informalexample>
5131 <programlisting>
5132<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005133 static int playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count);
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005135 static int capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count);
5137]]>
5138 </programlisting>
5139 </informalexample>
5140 </para>
5141
5142 <para>
5143 In the case of interleaved samples, the second argument
5144 (<parameter>channel</parameter>) is not used. The third argument
5145 (<parameter>pos</parameter>) points the
5146 current position offset in frames.
5147 </para>
5148
5149 <para>
5150 The meaning of the fourth argument is different between
5151 playback and capture. For playback, it holds the source data
5152 pointer, and for capture, it's the destination data pointer.
5153 </para>
5154
5155 <para>
5156 The last argument is the number of frames to be copied.
5157 </para>
5158
5159 <para>
5160 What you have to do in this callback is again different
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005161 between playback and capture directions. In the
5162 playback case, you copy the given amount of data
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 (<parameter>count</parameter>) at the specified pointer
5164 (<parameter>src</parameter>) to the specified offset
5165 (<parameter>pos</parameter>) on the hardware buffer. When
5166 coded like memcpy-like way, the copy would be like:
5167
5168 <informalexample>
5169 <programlisting>
5170<![CDATA[
5171 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src,
5172 frames_to_bytes(runtime, count));
5173]]>
5174 </programlisting>
5175 </informalexample>
5176 </para>
5177
5178 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005179 For the capture direction, you copy the given amount of
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 data (<parameter>count</parameter>) at the specified offset
5181 (<parameter>pos</parameter>) on the hardware buffer to the
5182 specified pointer (<parameter>dst</parameter>).
5183
5184 <informalexample>
5185 <programlisting>
5186<![CDATA[
5187 my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos),
5188 frames_to_bytes(runtime, count));
5189]]>
5190 </programlisting>
5191 </informalexample>
5192
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005193 Note that both the position and the amount of data are given
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 in frames.
5195 </para>
5196
5197 <para>
5198 In the case of non-interleaved samples, the implementation
5199 will be a bit more complicated.
5200 </para>
5201
5202 <para>
5203 You need to check the channel argument, and if it's -1, copy
5204 the whole channels. Otherwise, you have to copy only the
5205 specified channel. Please check
5206 <filename>isa/gus/gus_pcm.c</filename> as an example.
5207 </para>
5208
5209 <para>
5210 The <structfield>silence</structfield> callback is also
5211 implemented in a similar way.
5212
5213 <informalexample>
5214 <programlisting>
5215<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005216 static int silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
5218]]>
5219 </programlisting>
5220 </informalexample>
5221 </para>
5222
5223 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005224 The meanings of arguments are the same as in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 <structfield>copy</structfield>
5226 callback, although there is no <parameter>src/dst</parameter>
5227 argument. In the case of interleaved samples, the channel
5228 argument has no meaning, as well as on
5229 <structfield>copy</structfield> callback.
5230 </para>
5231
5232 <para>
5233 The role of <structfield>silence</structfield> callback is to
5234 set the given amount
5235 (<parameter>count</parameter>) of silence data at the
5236 specified offset (<parameter>pos</parameter>) on the hardware
5237 buffer. Suppose that the data format is signed (that is, the
5238 silent-data is 0), and the implementation using a memset-like
5239 function would be like:
5240
5241 <informalexample>
5242 <programlisting>
5243<![CDATA[
5244 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0,
5245 frames_to_bytes(runtime, count));
5246]]>
5247 </programlisting>
5248 </informalexample>
5249 </para>
5250
5251 <para>
5252 In the case of non-interleaved samples, again, the
5253 implementation becomes a bit more complicated. See, for example,
5254 <filename>isa/gus/gus_pcm.c</filename>.
5255 </para>
5256 </section>
5257
5258 <section id="buffer-and-memory-non-contiguous">
5259 <title>Non-Contiguous Buffers</title>
5260 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005261 If your hardware supports the page table as in emu10k1 or the
5262 buffer descriptors as in via82xx, you can use the scatter-gather
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 (SG) DMA. ALSA provides an interface for handling SG-buffers.
5264 The API is provided in <filename>&lt;sound/pcm.h&gt;</filename>.
5265 </para>
5266
5267 <para>
5268 For creating the SG-buffer handler, call
5269 <function>snd_pcm_lib_preallocate_pages()</function> or
5270 <function>snd_pcm_lib_preallocate_pages_for_all()</function>
5271 with <constant>SNDRV_DMA_TYPE_DEV_SG</constant>
5272 in the PCM constructor like other PCI pre-allocator.
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005273 You need to pass <function>snd_dma_pci_data(pci)</function>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 where pci is the struct <structname>pci_dev</structname> pointer
5275 of the chip as well.
Giuliano Pochini44275f12006-01-27 12:02:05 +01005276 The <type>struct snd_sg_buf</type> instance is created as
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 substream-&gt;dma_private. You can cast
5278 the pointer like:
5279
5280 <informalexample>
5281 <programlisting>
5282<![CDATA[
Giuliano Pochini44275f12006-01-27 12:02:05 +01005283 struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284]]>
5285 </programlisting>
5286 </informalexample>
5287 </para>
5288
5289 <para>
5290 Then call <function>snd_pcm_lib_malloc_pages()</function>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005291 in the <structfield>hw_params</structfield> callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 as well as in the case of normal PCI buffer.
5293 The SG-buffer handler will allocate the non-contiguous kernel
5294 pages of the given size and map them onto the virtually contiguous
5295 memory. The virtual pointer is addressed in runtime-&gt;dma_area.
5296 The physical address (runtime-&gt;dma_addr) is set to zero,
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02005297 because the buffer is physically non-contiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 The physical address table is set up in sgbuf-&gt;table.
5299 You can get the physical address at a certain offset via
5300 <function>snd_pcm_sgbuf_get_addr()</function>.
5301 </para>
5302
5303 <para>
5304 When a SG-handler is used, you need to set
5305 <function>snd_pcm_sgbuf_ops_page</function> as
5306 the <structfield>page</structfield> callback.
5307 (See <link linkend="pcm-interface-operators-page-callback">
5308 <citetitle>page callback section</citetitle></link>.)
5309 </para>
5310
5311 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005312 To release the data, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 <function>snd_pcm_lib_free_pages()</function> in the
5314 <structfield>hw_free</structfield> callback as usual.
5315 </para>
5316 </section>
5317
5318 <section id="buffer-and-memory-vmalloced">
5319 <title>Vmalloc'ed Buffers</title>
5320 <para>
5321 It's possible to use a buffer allocated via
5322 <function>vmalloc</function>, for example, for an intermediate
5323 buffer. Since the allocated pages are not contiguous, you need
5324 to set the <structfield>page</structfield> callback to obtain
5325 the physical address at every offset.
5326 </para>
5327
5328 <para>
5329 The implementation of <structfield>page</structfield> callback
5330 would be like this:
5331
5332 <informalexample>
5333 <programlisting>
5334<![CDATA[
5335 #include <linux/vmalloc.h>
5336
5337 /* get the physical page pointer on the given offset */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005338 static struct page *mychip_page(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 unsigned long offset)
5340 {
5341 void *pageptr = substream->runtime->dma_area + offset;
5342 return vmalloc_to_page(pageptr);
5343 }
5344]]>
5345 </programlisting>
5346 </informalexample>
5347 </para>
5348 </section>
5349
5350 </chapter>
5351
5352
5353<!-- ****************************************************** -->
5354<!-- Proc Interface -->
5355<!-- ****************************************************** -->
5356 <chapter id="proc-interface">
5357 <title>Proc Interface</title>
5358 <para>
5359 ALSA provides an easy interface for procfs. The proc files are
5360 very useful for debugging. I recommend you set up proc files if
5361 you write a driver and want to get a running status or register
5362 dumps. The API is found in
5363 <filename>&lt;sound/info.h&gt;</filename>.
5364 </para>
5365
5366 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005367 To create a proc file, call
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 <function>snd_card_proc_new()</function>.
5369
5370 <informalexample>
5371 <programlisting>
5372<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005373 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 int err = snd_card_proc_new(card, "my-file", &entry);
5375]]>
5376 </programlisting>
5377 </informalexample>
5378
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005379 where the second argument specifies the name of the proc file to be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 created. The above example will create a file
5381 <filename>my-file</filename> under the card directory,
5382 e.g. <filename>/proc/asound/card0/my-file</filename>.
5383 </para>
5384
5385 <para>
5386 Like other components, the proc entry created via
5387 <function>snd_card_proc_new()</function> will be registered and
5388 released automatically in the card registration and release
5389 functions.
5390 </para>
5391
5392 <para>
5393 When the creation is successful, the function stores a new
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005394 instance in the pointer given in the third argument.
5395 It is initialized as a text proc file for read only. To use
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 this proc file as a read-only text file as it is, set the read
5397 callback with a private data via
5398 <function>snd_info_set_text_ops()</function>.
5399
5400 <informalexample>
5401 <programlisting>
5402<![CDATA[
Takashi Iwaibf850202006-04-28 15:13:41 +02005403 snd_info_set_text_ops(entry, chip, my_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404]]>
5405 </programlisting>
5406 </informalexample>
5407
5408 where the second argument (<parameter>chip</parameter>) is the
5409 private data to be used in the callbacks. The third parameter
5410 specifies the read buffer size and the fourth
5411 (<parameter>my_proc_read</parameter>) is the callback function, which
5412 is defined like
5413
5414 <informalexample>
5415 <programlisting>
5416<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005417 static void my_proc_read(struct snd_info_entry *entry,
5418 struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419]]>
5420 </programlisting>
5421 </informalexample>
5422
5423 </para>
5424
5425 <para>
5426 In the read callback, use <function>snd_iprintf()</function> for
5427 output strings, which works just like normal
5428 <function>printf()</function>. For example,
5429
5430 <informalexample>
5431 <programlisting>
5432<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005433 static void my_proc_read(struct snd_info_entry *entry,
5434 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005436 struct my_chip *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
5438 snd_iprintf(buffer, "This is my chip!\n");
5439 snd_iprintf(buffer, "Port = %ld\n", chip->port);
5440 }
5441]]>
5442 </programlisting>
5443 </informalexample>
5444 </para>
5445
5446 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005447 The file permissions can be changed afterwards. As default, it's
5448 set as read only for all users. If you want to add write
5449 permission for the user (root as default), do as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 <informalexample>
5452 <programlisting>
5453<![CDATA[
5454 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
5455]]>
5456 </programlisting>
5457 </informalexample>
5458
5459 and set the write buffer size and the callback
5460
5461 <informalexample>
5462 <programlisting>
5463<![CDATA[
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 entry->c.text.write = my_proc_write;
5465]]>
5466 </programlisting>
5467 </informalexample>
5468 </para>
5469
5470 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 For the write callback, you can use
5472 <function>snd_info_get_line()</function> to get a text line, and
5473 <function>snd_info_get_str()</function> to retrieve a string from
5474 the line. Some examples are found in
5475 <filename>core/oss/mixer_oss.c</filename>, core/oss/and
5476 <filename>pcm_oss.c</filename>.
5477 </para>
5478
5479 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005480 For a raw-data proc-file, set the attributes as follows:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 <informalexample>
5483 <programlisting>
5484<![CDATA[
5485 static struct snd_info_entry_ops my_file_io_ops = {
5486 .read = my_file_io_read,
5487 };
5488
5489 entry->content = SNDRV_INFO_CONTENT_DATA;
5490 entry->private_data = chip;
5491 entry->c.ops = &my_file_io_ops;
5492 entry->size = 4096;
5493 entry->mode = S_IFREG | S_IRUGO;
5494]]>
5495 </programlisting>
5496 </informalexample>
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005497
5498 For the raw data, <structfield>size</structfield> field must be
5499 set properly. This specifies the maximum size of the proc file access.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 </para>
5501
5502 <para>
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005503 The read/write callbacks of raw mode are more direct than the text mode.
5504 You need to use a low-level I/O functions such as
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 <function>copy_from/to_user()</function> to transfer the
5506 data.
5507
5508 <informalexample>
5509 <programlisting>
5510<![CDATA[
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005511 static ssize_t my_file_io_read(struct snd_info_entry *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 void *file_private_data,
5513 struct file *file,
5514 char *buf,
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005515 size_t count,
5516 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 {
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005518 if (copy_to_user(buf, local_data + pos, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return -EFAULT;
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005520 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 }
5522]]>
5523 </programlisting>
5524 </informalexample>
Takashi Iwaic56a3b12010-04-13 12:55:09 +02005525
5526 If the size of the info entry has been set up properly,
5527 <structfield>count</structfield> and <structfield>pos</structfield> are
5528 guaranteed to fit within 0 and the given size.
5529 You don't have to check the range in the callbacks unless any
5530 other condition is required.
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 </para>
5533
5534 </chapter>
5535
5536
5537<!-- ****************************************************** -->
5538<!-- Power Management -->
5539<!-- ****************************************************** -->
5540 <chapter id="power-management">
5541 <title>Power Management</title>
5542 <para>
Paolo Ornati670e9f32006-10-03 22:57:56 +02005543 If the chip is supposed to work with suspend/resume
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005544 functions, you need to add power-management code to the
5545 driver. The additional code for power-management should be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 <function>ifdef</function>'ed with
5547 <constant>CONFIG_PM</constant>.
5548 </para>
5549
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005550 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005551 If the driver <emphasis>fully</emphasis> supports suspend/resume
5552 that is, the device can be
5553 properly resumed to its state when suspend was called,
5554 you can set the <constant>SNDRV_PCM_INFO_RESUME</constant> flag
5555 in the pcm info field. Usually, this is possible when the
5556 registers of the chip can be safely saved and restored to
5557 RAM. If this is set, the trigger callback is called with
5558 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> after the resume
5559 callback completes.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005560 </para>
5561
5562 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005563 Even if the driver doesn't support PM fully but
5564 partial suspend/resume is still possible, it's still worthy to
5565 implement suspend/resume callbacks. In such a case, applications
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005566 would reset the status by calling
5567 <function>snd_pcm_prepare()</function> and restart the stream
5568 appropriately. Hence, you can define suspend/resume callbacks
5569 below but don't set <constant>SNDRV_PCM_INFO_RESUME</constant>
5570 info flag to the PCM.
5571 </para>
5572
5573 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005574 Note that the trigger with SUSPEND can always be called when
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005575 <function>snd_pcm_suspend_all</function> is called,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005576 regardless of the <constant>SNDRV_PCM_INFO_RESUME</constant> flag.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005577 The <constant>RESUME</constant> flag affects only the behavior
5578 of <function>snd_pcm_resume()</function>.
5579 (Thus, in theory,
5580 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> isn't needed
5581 to be handled in the trigger callback when no
5582 <constant>SNDRV_PCM_INFO_RESUME</constant> flag is set. But,
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005583 it's better to keep it for compatibility reasons.)
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005584 </para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005586 In the earlier version of ALSA drivers, a common
5587 power-management layer was provided, but it has been removed.
5588 The driver needs to define the suspend/resume hooks according to
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005589 the bus the device is connected to. In the case of PCI drivers, the
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005590 callbacks look like below:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
5592 <informalexample>
5593 <programlisting>
5594<![CDATA[
5595 #ifdef CONFIG_PM
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005596 static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005598 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 return 0;
5600 }
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005601 static int snd_my_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005603 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return 0;
5605 }
5606 #endif
5607]]>
5608 </programlisting>
5609 </informalexample>
5610 </para>
5611
5612 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005613 The scheme of the real suspend job is as follows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005616 <listitem><para>Retrieve the card and the chip data.</para></listitem>
5617 <listitem><para>Call <function>snd_power_change_state()</function> with
5618 <constant>SNDRV_CTL_POWER_D3hot</constant> to change the
5619 power status.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 <listitem><para>Call <function>snd_pcm_suspend_all()</function> to suspend the running PCM streams.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005621 <listitem><para>If AC97 codecs are used, call
Takashi Iwaia7306332006-05-04 11:58:43 +02005622 <function>snd_ac97_suspend()</function> for each codec.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 <listitem><para>Save the register values if necessary.</para></listitem>
5624 <listitem><para>Stop the hardware if necessary.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005625 <listitem><para>Disable the PCI device by calling
5626 <function>pci_disable_device()</function>. Then, call
5627 <function>pci_save_state()</function> at last.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 </orderedlist>
5629 </para>
5630
5631 <para>
5632 A typical code would be like:
5633
5634 <informalexample>
5635 <programlisting>
5636<![CDATA[
Alexey Dobriyan323579882006-01-15 02:12:54 +01005637 static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 {
5639 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005640 struct snd_card *card = pci_get_drvdata(pci);
5641 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005643 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 /* (3) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005645 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 /* (4) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005647 snd_ac97_suspend(chip->ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 /* (5) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005649 snd_mychip_save_registers(chip);
5650 /* (6) */
5651 snd_mychip_stop_hardware(chip);
5652 /* (7) */
5653 pci_disable_device(pci);
5654 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 return 0;
5656 }
5657]]>
5658 </programlisting>
5659 </informalexample>
5660 </para>
5661
5662 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005663 The scheme of the real resume job is as follows.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
5665 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005666 <listitem><para>Retrieve the card and the chip data.</para></listitem>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005667 <listitem><para>Set up PCI. First, call <function>pci_restore_state()</function>.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005668 Then enable the pci device again by calling <function>pci_enable_device()</function>.
5669 Call <function>pci_set_master()</function> if necessary, too.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 <listitem><para>Re-initialize the chip.</para></listitem>
5671 <listitem><para>Restore the saved registers if necessary.</para></listitem>
5672 <listitem><para>Resume the mixer, e.g. calling
5673 <function>snd_ac97_resume()</function>.</para></listitem>
5674 <listitem><para>Restart the hardware (if any).</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005675 <listitem><para>Call <function>snd_power_change_state()</function> with
5676 <constant>SNDRV_CTL_POWER_D0</constant> to notify the processes.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 </orderedlist>
5678 </para>
5679
5680 <para>
5681 A typical code would be like:
5682
5683 <informalexample>
5684 <programlisting>
5685<![CDATA[
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005686 static int mychip_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 {
5688 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005689 struct snd_card *card = pci_get_drvdata(pci);
5690 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005692 pci_restore_state(pci);
5693 pci_enable_device(pci);
5694 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 /* (3) */
5696 snd_mychip_reinit_chip(chip);
5697 /* (4) */
5698 snd_mychip_restore_registers(chip);
5699 /* (5) */
5700 snd_ac97_resume(chip->ac97);
5701 /* (6) */
5702 snd_mychip_restart_chip(chip);
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005703 /* (7) */
5704 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 return 0;
5706 }
5707]]>
5708 </programlisting>
5709 </informalexample>
5710 </para>
5711
5712 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005713 As shown in the above, it's better to save registers after
5714 suspending the PCM operations via
5715 <function>snd_pcm_suspend_all()</function> or
5716 <function>snd_pcm_suspend()</function>. It means that the PCM
5717 streams are already stoppped when the register snapshot is
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005718 taken. But, remember that you don't have to restart the PCM
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005719 stream in the resume callback. It'll be restarted via
5720 trigger call with <constant>SNDRV_PCM_TRIGGER_RESUME</constant>
5721 when necessary.
5722 </para>
5723
5724 <para>
5725 OK, we have all callbacks now. Let's set them up. In the
5726 initialization of the card, make sure that you can get the chip
5727 data from the card instance, typically via
5728 <structfield>private_data</structfield> field, in case you
5729 created the chip data individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
5731 <informalexample>
5732 <programlisting>
5733<![CDATA[
Takashi Iwai090015a2012-12-07 07:52:28 +01005734 static int snd_mychip_probe(struct pci_dev *pci,
5735 const struct pci_device_id *pci_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 {
5737 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005738 struct snd_card *card;
5739 struct mychip *chip;
Takashi Iwaid4533792008-12-28 16:45:34 +01005740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 ....
Takashi Iwaid4533792008-12-28 16:45:34 +01005742 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005743 ....
5744 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
5745 ....
5746 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 ....
5748 }
5749]]>
5750 </programlisting>
5751 </informalexample>
5752
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005753 When you created the chip data with
Takashi Iwaid4533792008-12-28 16:45:34 +01005754 <function>snd_card_create()</function>, it's anyway accessible
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005755 via <structfield>private_data</structfield> field.
5756
5757 <informalexample>
5758 <programlisting>
5759<![CDATA[
Takashi Iwai090015a2012-12-07 07:52:28 +01005760 static int snd_mychip_probe(struct pci_dev *pci,
5761 const struct pci_device_id *pci_id)
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005762 {
5763 ....
5764 struct snd_card *card;
5765 struct mychip *chip;
Takashi Iwaid4533792008-12-28 16:45:34 +01005766 int err;
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005767 ....
Takashi Iwaid4533792008-12-28 16:45:34 +01005768 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
5769 sizeof(struct mychip), &card);
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005770 ....
5771 chip = card->private_data;
5772 ....
5773 }
5774]]>
5775 </programlisting>
5776 </informalexample>
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 </para>
5779
5780 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005781 If you need a space to save the registers, allocate the
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005782 buffer for it here, too, since it would be fatal
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 if you cannot allocate a memory in the suspend phase.
5784 The allocated buffer should be released in the corresponding
5785 destructor.
5786 </para>
5787
5788 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005789 And next, set suspend/resume callbacks to the pci_driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 <informalexample>
5792 <programlisting>
5793<![CDATA[
5794 static struct pci_driver driver = {
Takashi Iwaice1fd362011-06-14 08:54:14 +02005795 .name = KBUILD_MODNAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 .id_table = snd_my_ids,
5797 .probe = snd_my_probe,
Takashi Iwai090015a2012-12-07 07:52:28 +01005798 .remove = snd_my_remove,
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005799 #ifdef CONFIG_PM
5800 .suspend = snd_my_suspend,
5801 .resume = snd_my_resume,
5802 #endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 };
5804]]>
5805 </programlisting>
5806 </informalexample>
5807 </para>
5808
5809 </chapter>
5810
5811
5812<!-- ****************************************************** -->
5813<!-- Module Parameters -->
5814<!-- ****************************************************** -->
5815 <chapter id="module-parameters">
5816 <title>Module Parameters</title>
5817 <para>
5818 There are standard module options for ALSA. At least, each
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005819 module should have the <parameter>index</parameter>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 <parameter>id</parameter> and <parameter>enable</parameter>
5821 options.
5822 </para>
5823
5824 <para>
5825 If the module supports multiple cards (usually up to
5826 8 = <constant>SNDRV_CARDS</constant> cards), they should be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005827 arrays. The default initial values are defined already as
5828 constants for easier programming:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829
5830 <informalexample>
5831 <programlisting>
5832<![CDATA[
5833 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
5834 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
5835 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
5836]]>
5837 </programlisting>
5838 </informalexample>
5839 </para>
5840
5841 <para>
5842 If the module supports only a single card, they could be single
5843 variables, instead. <parameter>enable</parameter> option is not
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005844 always necessary in this case, but it would be better to have a
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 dummy option for compatibility.
5846 </para>
5847
5848 <para>
5849 The module parameters must be declared with the standard
5850 <function>module_param()()</function>,
5851 <function>module_param_array()()</function> and
5852 <function>MODULE_PARM_DESC()</function> macros.
5853 </para>
5854
5855 <para>
5856 The typical coding would be like below:
5857
5858 <informalexample>
5859 <programlisting>
5860<![CDATA[
5861 #define CARD_NAME "My Chip"
5862
5863 module_param_array(index, int, NULL, 0444);
5864 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
5865 module_param_array(id, charp, NULL, 0444);
5866 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
5867 module_param_array(enable, bool, NULL, 0444);
5868 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
5869]]>
5870 </programlisting>
5871 </informalexample>
5872 </para>
5873
5874 <para>
5875 Also, don't forget to define the module description, classes,
5876 license and devices. Especially, the recent modprobe requires to
5877 define the module license as GPL, etc., otherwise the system is
5878 shown as <quote>tainted</quote>.
5879
5880 <informalexample>
5881 <programlisting>
5882<![CDATA[
5883 MODULE_DESCRIPTION("My Chip");
5884 MODULE_LICENSE("GPL");
5885 MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
5886]]>
5887 </programlisting>
5888 </informalexample>
5889 </para>
5890
5891 </chapter>
5892
5893
5894<!-- ****************************************************** -->
5895<!-- How To Put Your Driver -->
5896<!-- ****************************************************** -->
5897 <chapter id="how-to-put-your-driver">
5898 <title>How To Put Your Driver Into ALSA Tree</title>
5899 <section>
5900 <title>General</title>
5901 <para>
5902 So far, you've learned how to write the driver codes.
5903 And you might have a question now: how to put my own
5904 driver into the ALSA driver tree?
5905 Here (finally :) the standard procedure is described briefly.
5906 </para>
5907
5908 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005909 Suppose that you create a new PCI driver for the card
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 <quote>xyz</quote>. The card module name would be
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005911 snd-xyz. The new driver is usually put into the alsa-driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 tree, <filename>alsa-driver/pci</filename> directory in
5913 the case of PCI cards.
5914 Then the driver is evaluated, audited and tested
5915 by developers and users. After a certain time, the driver
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005916 will go to the alsa-kernel tree (to the corresponding directory,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 such as <filename>alsa-kernel/pci</filename>) and eventually
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005918 will be integrated into the Linux 2.6 tree (the directory would be
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 <filename>linux/sound/pci</filename>).
5920 </para>
5921
5922 <para>
5923 In the following sections, the driver code is supposed
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02005924 to be put into alsa-driver tree. The two cases are covered:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 a driver consisting of a single source file and one consisting
5926 of several source files.
5927 </para>
5928 </section>
5929
5930 <section>
5931 <title>Driver with A Single Source File</title>
5932 <para>
5933 <orderedlist>
5934 <listitem>
5935 <para>
5936 Modify alsa-driver/pci/Makefile
5937 </para>
5938
5939 <para>
5940 Suppose you have a file xyz.c. Add the following
5941 two lines
5942 <informalexample>
5943 <programlisting>
5944<![CDATA[
5945 snd-xyz-objs := xyz.o
5946 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
5947]]>
5948 </programlisting>
5949 </informalexample>
5950 </para>
5951 </listitem>
5952
5953 <listitem>
5954 <para>
5955 Create the Kconfig entry
5956 </para>
5957
5958 <para>
5959 Add the new entry of Kconfig for your xyz driver.
5960 <informalexample>
5961 <programlisting>
5962<![CDATA[
5963 config SND_XYZ
5964 tristate "Foobar XYZ"
5965 depends on SND
5966 select SND_PCM
5967 help
5968 Say Y here to include support for Foobar XYZ soundcard.
5969
5970 To compile this driver as a module, choose M here: the module
5971 will be called snd-xyz.
5972]]>
5973 </programlisting>
5974 </informalexample>
5975
5976 the line, select SND_PCM, specifies that the driver xyz supports
5977 PCM. In addition to SND_PCM, the following components are
5978 supported for select command:
5979 SND_RAWMIDI, SND_TIMER, SND_HWDEP, SND_MPU401_UART,
5980 SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, SND_AC97_CODEC.
5981 Add the select command for each supported component.
5982 </para>
5983
5984 <para>
5985 Note that some selections imply the lowlevel selections.
5986 For example, PCM includes TIMER, MPU401_UART includes RAWMIDI,
5987 AC97_CODEC includes PCM, and OPL3_LIB includes HWDEP.
5988 You don't need to give the lowlevel selections again.
5989 </para>
5990
5991 <para>
5992 For the details of Kconfig script, refer to the kbuild
5993 documentation.
5994 </para>
5995
5996 </listitem>
5997
5998 <listitem>
5999 <para>
6000 Run cvscompile script to re-generate the configure script and
6001 build the whole stuff again.
6002 </para>
6003 </listitem>
6004 </orderedlist>
6005 </para>
6006 </section>
6007
6008 <section>
6009 <title>Drivers with Several Source Files</title>
6010 <para>
6011 Suppose that the driver snd-xyz have several source files.
6012 They are located in the new subdirectory,
6013 pci/xyz.
6014
6015 <orderedlist>
6016 <listitem>
6017 <para>
6018 Add a new directory (<filename>xyz</filename>) in
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006019 <filename>alsa-driver/pci/Makefile</filename> as below
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
6021 <informalexample>
6022 <programlisting>
6023<![CDATA[
6024 obj-$(CONFIG_SND) += xyz/
6025]]>
6026 </programlisting>
6027 </informalexample>
6028 </para>
6029 </listitem>
6030
6031 <listitem>
6032 <para>
6033 Under the directory <filename>xyz</filename>, create a Makefile
6034
6035 <example>
6036 <title>Sample Makefile for a driver xyz</title>
6037 <programlisting>
6038<![CDATA[
6039 ifndef SND_TOPDIR
6040 SND_TOPDIR=../..
6041 endif
6042
6043 include $(SND_TOPDIR)/toplevel.config
6044 include $(SND_TOPDIR)/Makefile.conf
6045
6046 snd-xyz-objs := xyz.o abc.o def.o
6047
6048 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
6049
6050 include $(SND_TOPDIR)/Rules.make
6051]]>
6052 </programlisting>
6053 </example>
6054 </para>
6055 </listitem>
6056
6057 <listitem>
6058 <para>
6059 Create the Kconfig entry
6060 </para>
6061
6062 <para>
6063 This procedure is as same as in the last section.
6064 </para>
6065 </listitem>
6066
6067 <listitem>
6068 <para>
6069 Run cvscompile script to re-generate the configure script and
6070 build the whole stuff again.
6071 </para>
6072 </listitem>
6073 </orderedlist>
6074 </para>
6075 </section>
6076
6077 </chapter>
6078
6079<!-- ****************************************************** -->
6080<!-- Useful Functions -->
6081<!-- ****************************************************** -->
6082 <chapter id="useful-functions">
6083 <title>Useful Functions</title>
6084
6085 <section id="useful-functions-snd-printk">
6086 <title><function>snd_printk()</function> and friends</title>
6087 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006088 ALSA provides a verbose version of the
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 <function>printk()</function> function. If a kernel config
6090 <constant>CONFIG_SND_VERBOSE_PRINTK</constant> is set, this
6091 function prints the given message together with the file name
6092 and the line of the caller. The <constant>KERN_XXX</constant>
6093 prefix is processed as
6094 well as the original <function>printk()</function> does, so it's
6095 recommended to add this prefix, e.g.
6096
6097 <informalexample>
6098 <programlisting>
6099<![CDATA[
6100 snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\n");
6101]]>
6102 </programlisting>
6103 </informalexample>
6104 </para>
6105
6106 <para>
6107 There are also <function>printk()</function>'s for
6108 debugging. <function>snd_printd()</function> can be used for
6109 general debugging purposes. If
6110 <constant>CONFIG_SND_DEBUG</constant> is set, this function is
6111 compiled, and works just like
6112 <function>snd_printk()</function>. If the ALSA is compiled
6113 without the debugging flag, it's ignored.
6114 </para>
6115
6116 <para>
6117 <function>snd_printdd()</function> is compiled in only when
Takashi Iwai62cf8722008-05-20 12:15:15 +02006118 <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is set. Please note
6119 that <constant>CONFIG_SND_DEBUG_VERBOSE</constant> is not set as default
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 even if you configure the alsa-driver with
6121 <option>--with-debug=full</option> option. You need to give
6122 explicitly <option>--with-debug=detect</option> option instead.
6123 </para>
6124 </section>
6125
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 <section id="useful-functions-snd-bug">
6127 <title><function>snd_BUG()</function></title>
6128 <para>
Michael Opdenacker3f03f7c2007-10-24 10:59:44 +02006129 It shows the <computeroutput>BUG?</computeroutput> message and
Takashi Iwai7cc6dff2008-08-08 17:14:55 +02006130 stack trace as well as <function>snd_BUG_ON</function> at the point.
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006131 It's useful to show that a fatal error happens there.
6132 </para>
6133 <para>
6134 When no debug flag is set, this macro is ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 </para>
6136 </section>
Takashi Iwai5ef03462008-08-08 17:06:01 +02006137
6138 <section id="useful-functions-snd-bug-on">
6139 <title><function>snd_BUG_ON()</function></title>
6140 <para>
6141 <function>snd_BUG_ON()</function> macro is similar with
6142 <function>WARN_ON()</function> macro. For example,
6143
6144 <informalexample>
6145 <programlisting>
6146<![CDATA[
6147 snd_BUG_ON(!pointer);
6148]]>
6149 </programlisting>
6150 </informalexample>
6151
6152 or it can be used as the condition,
6153 <informalexample>
6154 <programlisting>
6155<![CDATA[
6156 if (snd_BUG_ON(non_zero_is_bug))
6157 return -EINVAL;
6158]]>
6159 </programlisting>
6160 </informalexample>
6161
6162 </para>
6163
6164 <para>
6165 The macro takes an conditional expression to evaluate.
6166 When <constant>CONFIG_SND_DEBUG</constant>, is set, the
6167 expression is actually evaluated. If it's non-zero, it shows
6168 the warning message such as
6169 <computeroutput>BUG? (xxx)</computeroutput>
6170 normally followed by stack trace. It returns the evaluated
6171 value.
6172 When no <constant>CONFIG_SND_DEBUG</constant> is set, this
6173 macro always returns zero.
6174 </para>
6175
6176 </section>
6177
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 </chapter>
6179
6180
6181<!-- ****************************************************** -->
6182<!-- Acknowledgments -->
6183<!-- ****************************************************** -->
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01006184 <chapter id="acknowledgments">
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 <title>Acknowledgments</title>
6186 <para>
6187 I would like to thank Phil Kerr for his help for improvement and
6188 corrections of this document.
6189 </para>
6190 <para>
6191 Kevin Conder reformatted the original plain-text to the
6192 DocBook format.
6193 </para>
6194 <para>
6195 Giuliano Pochini corrected typos and contributed the example codes
6196 in the hardware constraints section.
6197 </para>
6198 </chapter>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199</book>