blob: b9d2dbee9bc37ce1d418f55c37d918326b578425 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
2
3<book>
4<?dbhtml filename="index.html">
5
6<!-- ****************************************************** -->
7<!-- Header -->
8<!-- ****************************************************** -->
9 <bookinfo>
10 <title>Writing an ALSA Driver</title>
11 <author>
12 <firstname>Takashi</firstname>
13 <surname>Iwai</surname>
14 <affiliation>
15 <address>
16 <email>tiwai@suse.de</email>
17 </address>
18 </affiliation>
19 </author>
20
Takashi Iwai95a5b082007-07-26 16:50:09 +020021 <date>July 26, 2007</date>
22 <edition>0.3.6.1</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>
70 driver. The document focuses mainly on the PCI soundcard.
71 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>
78 The target of this document is ones who already have enough
79 skill of C language and have the basic knowledge of linux
80 kernel programming. This document doesn't explain the general
81 topics of linux kernel codes and doesn't cover the detail of
82 implementation of each low-level driver. It describes only how is
83 the standard way to write a PCI sound driver on ALSA.
84 </para>
85
86 <para>
87 If you are already familiar with the older ALSA ver.0.5.x, you
88 can check the drivers such as <filename>es1938.c</filename> or
89 <filename>maestro3.c</filename> which have also almost the same
90 code-base in the ALSA 0.5.x tree, so you can compare the differences.
91 </para>
92
93 <para>
94 This document is still a draft version. Any feedbacks and
95 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>
109 The ALSA drivers are provided in the two ways.
110 </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
117 contains purely the source codes for the Linux 2.6 (or later)
118 tree. This tree is designed only for compilation on 2.6 or
119 later environment. The latter, alsa-driver, contains many subtle
120 files for compiling the ALSA driver on the outside of Linux
121 kernel like configure script, the wrapper functions for older,
122 2.2 and 2.4 kernels, to adapt the latest kernel API,
123 and additional drivers which are still in development or in
124 tests. The drivers in alsa-driver tree will be moved to
125 alsa-kernel (eventually 2.6 kernel tree) once when they are
126 finished and confirmed to work fine.
127 </para>
128
129 <para>
130 The file tree structure of ALSA driver is depicted below. Both
131 alsa-kernel and alsa-driver have almost the same file
132 structure, except for <quote>core</quote> directory. It's
133 named as <quote>acore</quote> in alsa-driver tree.
134
135 <example>
136 <title>ALSA File Tree Structure</title>
137 <literallayout>
138 sound
139 /core
140 /oss
141 /seq
142 /oss
143 /instr
144 /ioctl32
145 /include
146 /drivers
147 /mpu401
148 /opl3
149 /i2c
150 /l3
151 /synth
152 /emux
153 /pci
154 /(cards)
155 /isa
156 /(cards)
157 /arm
158 /ppc
159 /sparc
160 /usb
161 /pcmcia /(cards)
162 /oss
163 </literallayout>
164 </example>
165 </para>
166 </section>
167
168 <section id="file-tree-core-directory">
169 <title>core directory</title>
170 <para>
171 This directory contains the middle layer, that is, the heart
172 of ALSA drivers. In this directory, the native ALSA modules are
173 stored. The sub-directories contain different modules and are
174 dependent upon the kernel config.
175 </para>
176
177 <section id="file-tree-core-directory-oss">
178 <title>core/oss</title>
179
180 <para>
181 The codes for PCM and mixer OSS emulation modules are stored
182 in this directory. The rawmidi OSS emulation is included in
183 the ALSA rawmidi code since it's quite small. The sequencer
184 code is stored in core/seq/oss directory (see
185 <link linkend="file-tree-core-directory-seq-oss"><citetitle>
186 below</citetitle></link>).
187 </para>
188 </section>
189
190 <section id="file-tree-core-directory-ioctl32">
191 <title>core/ioctl32</title>
192
193 <para>
194 This directory contains the 32bit-ioctl wrappers for 64bit
195 architectures such like x86-64, ppc64 and sparc64. For 32bit
196 and alpha architectures, these are not compiled.
197 </para>
198 </section>
199
200 <section id="file-tree-core-directory-seq">
201 <title>core/seq</title>
202 <para>
203 This and its sub-directories are for the ALSA
204 sequencer. This directory contains the sequencer core and
205 primary sequencer modules such like snd-seq-midi,
206 snd-seq-virmidi, etc. They are compiled only when
207 <constant>CONFIG_SND_SEQUENCER</constant> is set in the kernel
208 config.
209 </para>
210 </section>
211
212 <section id="file-tree-core-directory-seq-oss">
213 <title>core/seq/oss</title>
214 <para>
215 This contains the OSS sequencer emulation codes.
216 </para>
217 </section>
218
219 <section id="file-tree-core-directory-deq-instr">
220 <title>core/seq/instr</title>
221 <para>
222 This directory contains the modules for the sequencer
223 instrument layer.
224 </para>
225 </section>
226 </section>
227
228 <section id="file-tree-include-directory">
229 <title>include directory</title>
230 <para>
231 This is the place for the public header files of ALSA drivers,
232 which are to be exported to the user-space, or included by
233 several files at different directories. Basically, the private
234 header files should not be placed in this directory, but you may
235 still find files there, due to historical reason :)
236 </para>
237 </section>
238
239 <section id="file-tree-drivers-directory">
240 <title>drivers directory</title>
241 <para>
242 This directory contains the codes shared among different drivers
243 on the different architectures. They are hence supposed not to be
244 architecture-specific.
245 For example, the dummy pcm driver and the serial MIDI
246 driver are found in this directory. In the sub-directories,
247 there are the codes for components which are independent from
248 bus and cpu architectures.
249 </para>
250
251 <section id="file-tree-drivers-directory-mpu401">
252 <title>drivers/mpu401</title>
253 <para>
254 The MPU401 and MPU401-UART modules are stored here.
255 </para>
256 </section>
257
258 <section id="file-tree-drivers-directory-opl3">
259 <title>drivers/opl3 and opl4</title>
260 <para>
261 The OPL3 and OPL4 FM-synth stuff is found here.
262 </para>
263 </section>
264 </section>
265
266 <section id="file-tree-i2c-directory">
267 <title>i2c directory</title>
268 <para>
269 This contains the ALSA i2c components.
270 </para>
271
272 <para>
273 Although there is a standard i2c layer on Linux, ALSA has its
274 own i2c codes for some cards, because the soundcard needs only a
275 simple operation and the standard i2c API is too complicated for
276 such a purpose.
277 </para>
278
279 <section id="file-tree-i2c-directory-l3">
280 <title>i2c/l3</title>
281 <para>
282 This is a sub-directory for ARM L3 i2c.
283 </para>
284 </section>
285 </section>
286
287 <section id="file-tree-synth-directory">
288 <title>synth directory</title>
289 <para>
290 This contains the synth middle-level modules.
291 </para>
292
293 <para>
294 So far, there is only Emu8000/Emu10k1 synth driver under
295 synth/emux sub-directory.
296 </para>
297 </section>
298
299 <section id="file-tree-pci-directory">
300 <title>pci directory</title>
301 <para>
302 This and its sub-directories hold the top-level card modules
303 for PCI soundcards and the codes specific to the PCI BUS.
304 </para>
305
306 <para>
307 The drivers compiled from a single file is stored directly on
308 pci directory, while the drivers with several source files are
309 stored on its own sub-directory (e.g. emu10k1, ice1712).
310 </para>
311 </section>
312
313 <section id="file-tree-isa-directory">
314 <title>isa directory</title>
315 <para>
316 This and its sub-directories hold the top-level card modules
317 for ISA soundcards.
318 </para>
319 </section>
320
321 <section id="file-tree-arm-ppc-sparc-directories">
322 <title>arm, ppc, and sparc directories</title>
323 <para>
324 These are for the top-level card modules which are
325 specific to each given architecture.
326 </para>
327 </section>
328
329 <section id="file-tree-usb-directory">
330 <title>usb directory</title>
331 <para>
332 This contains the USB-audio driver. On the latest version, the
333 USB MIDI driver is integrated together with usb-audio driver.
334 </para>
335 </section>
336
337 <section id="file-tree-pcmcia-directory">
338 <title>pcmcia directory</title>
339 <para>
340 The PCMCIA, especially PCCard drivers will go here. CardBus
341 drivers will be on pci directory, because its API is identical
342 with the standard PCI cards.
343 </para>
344 </section>
345
346 <section id="file-tree-oss-directory">
347 <title>oss directory</title>
348 <para>
349 The OSS/Lite source files are stored here on Linux 2.6 (or
350 later) tree. (In the ALSA driver tarball, it's empty, of course :)
351 </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>
365 The minimum flow of PCI soundcard is like the following:
366
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>
373 <listitem><para>create pci_driver table which contains the three pointers above.</para></listitem>
Takashi Iwai01d25d42005-04-11 16:58:24 +0200374 <listitem><para>create <function>init()</function> function just calling <function>pci_register_driver()</function> to register the pci_driver table defined above.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 <listitem><para>create <function>exit()</function> function to call <function>pci_unregister_driver()</function> function.</para></listitem>
376 </itemizedlist>
377 </para>
378 </section>
379
380 <section id="basic-flow-example">
381 <title>Full Code Example</title>
382 <para>
383 The code example is shown below. Some parts are kept
384 unimplemented at this moment but will be filled in the
385 succeeding sections. The numbers in comment lines of
386 <function>snd_mychip_probe()</function> function are the
387 markers.
388
389 <example>
390 <title>Basic Flow for PCI Drivers Example</title>
391 <programlisting>
392<![CDATA[
393 #include <sound/driver.h>
394 #include <linux/init.h>
395 #include <linux/pci.h>
396 #include <linux/slab.h>
397 #include <sound/core.h>
398 #include <sound/initval.h>
399
400 /* module parameters (see "Module Parameters") */
401 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
402 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
403 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
404
405 /* definition of the chip-specific record */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100406 struct mychip {
407 struct snd_card *card;
Takashi Iwai95a5b082007-07-26 16:50:09 +0200408 /* rest of implementation will be in the section
409 * "PCI Resource Managements"
410 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 };
412
413 /* chip-specific destructor
414 * (see "PCI Resource Managements")
415 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100416 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 {
Takashi Iwai95a5b082007-07-26 16:50:09 +0200418 .... /* will be implemented later... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 /* component-destructor
422 * (see "Management of Cards and Components")
423 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100424 static int snd_mychip_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100426 return snd_mychip_free(device->device_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 /* chip-specific constructor
430 * (see "Management of Cards and Components")
431 */
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100432 static int __devinit snd_mychip_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 struct pci_dev *pci,
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100434 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100436 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100438 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 .dev_free = snd_mychip_dev_free,
440 };
441
442 *rchip = NULL;
443
Takashi Iwai95a5b082007-07-26 16:50:09 +0200444 /* check PCI availability here
445 * (see "PCI Resource Managements")
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 ....
448
449 /* allocate a chip-specific data with zero filled */
Takashi Iwai561b2202005-09-09 14:22:34 +0200450 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (chip == NULL)
452 return -ENOMEM;
453
454 chip->card = card;
455
Takashi Iwai95a5b082007-07-26 16:50:09 +0200456 /* rest of initialization here; will be implemented
457 * later, see "PCI Resource Managements"
458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 ....
460
Takashi Iwai95a5b082007-07-26 16:50:09 +0200461 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
462 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 snd_mychip_free(chip);
464 return err;
465 }
466
467 snd_card_set_dev(card, &pci->dev);
468
469 *rchip = chip;
470 return 0;
471 }
472
473 /* constructor -- see "Constructor" sub-section */
474 static int __devinit snd_mychip_probe(struct pci_dev *pci,
475 const struct pci_device_id *pci_id)
476 {
477 static int dev;
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100478 struct snd_card *card;
479 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int err;
481
482 /* (1) */
483 if (dev >= SNDRV_CARDS)
484 return -ENODEV;
485 if (!enable[dev]) {
486 dev++;
487 return -ENOENT;
488 }
489
490 /* (2) */
491 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
492 if (card == NULL)
493 return -ENOMEM;
494
495 /* (3) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200496 err = snd_mychip_create(card, pci, &chip);
497 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 snd_card_free(card);
499 return err;
500 }
501
502 /* (4) */
503 strcpy(card->driver, "My Chip");
504 strcpy(card->shortname, "My Own Chip 123");
505 sprintf(card->longname, "%s at 0x%lx irq %i",
506 card->shortname, chip->ioport, chip->irq);
507
508 /* (5) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200509 .... /* implemented later */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 /* (6) */
Takashi Iwai95a5b082007-07-26 16:50:09 +0200512 err = snd_card_register(card);
513 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 snd_card_free(card);
515 return err;
516 }
517
518 /* (7) */
519 pci_set_drvdata(pci, card);
520 dev++;
521 return 0;
522 }
523
524 /* destructor -- see "Destructor" sub-section */
525 static void __devexit snd_mychip_remove(struct pci_dev *pci)
526 {
527 snd_card_free(pci_get_drvdata(pci));
528 pci_set_drvdata(pci, NULL);
529 }
530]]>
531 </programlisting>
532 </example>
533 </para>
534 </section>
535
536 <section id="basic-flow-constructor">
537 <title>Constructor</title>
538 <para>
539 The real constructor of PCI drivers is probe callback. The
540 probe callback and other component-constructors which are called
541 from probe callback should be defined with
542 <parameter>__devinit</parameter> prefix. You
543 cannot use <parameter>__init</parameter> prefix for them,
544 because any PCI device could be a hotplug device.
545 </para>
546
547 <para>
548 In the probe callback, the following scheme is often used.
549 </para>
550
551 <section id="basic-flow-constructor-device-index">
552 <title>1) Check and increment the device index.</title>
553 <para>
554 <informalexample>
555 <programlisting>
556<![CDATA[
557 static int dev;
558 ....
559 if (dev >= SNDRV_CARDS)
560 return -ENODEV;
561 if (!enable[dev]) {
562 dev++;
563 return -ENOENT;
564 }
565]]>
566 </programlisting>
567 </informalexample>
568
569 where enable[dev] is the module option.
570 </para>
571
572 <para>
573 At each time probe callback is called, check the
574 availability of the device. If not available, simply increment
575 the device index and returns. dev will be incremented also
576 later (<link
577 linkend="basic-flow-constructor-set-pci"><citetitle>step
578 7</citetitle></link>).
579 </para>
580 </section>
581
582 <section id="basic-flow-constructor-create-card">
583 <title>2) Create a card instance</title>
584 <para>
585 <informalexample>
586 <programlisting>
587<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100588 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 ....
590 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
591]]>
592 </programlisting>
593 </informalexample>
594 </para>
595
596 <para>
597 The detail will be explained in the section
598 <link linkend="card-management-card-instance"><citetitle>
599 Management of Cards and Components</citetitle></link>.
600 </para>
601 </section>
602
603 <section id="basic-flow-constructor-create-main">
604 <title>3) Create a main component</title>
605 <para>
606 In this part, the PCI resources are allocated.
607
608 <informalexample>
609 <programlisting>
610<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100611 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +0200613 err = snd_mychip_create(card, pci, &chip);
614 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 snd_card_free(card);
616 return err;
617 }
618]]>
619 </programlisting>
620 </informalexample>
621
622 The detail will be explained in the section <link
623 linkend="pci-resource"><citetitle>PCI Resource
624 Managements</citetitle></link>.
625 </para>
626 </section>
627
628 <section id="basic-flow-constructor-main-component">
629 <title>4) Set the driver ID and name strings.</title>
630 <para>
631 <informalexample>
632 <programlisting>
633<![CDATA[
634 strcpy(card->driver, "My Chip");
635 strcpy(card->shortname, "My Own Chip 123");
636 sprintf(card->longname, "%s at 0x%lx irq %i",
637 card->shortname, chip->ioport, chip->irq);
638]]>
639 </programlisting>
640 </informalexample>
641
642 The driver field holds the minimal ID string of the
643 chip. This is referred by alsa-lib's configurator, so keep it
644 simple but unique.
645 Even the same driver can have different driver IDs to
646 distinguish the functionality of each chip type.
647 </para>
648
649 <para>
650 The shortname field is a string shown as more verbose
651 name. The longname field contains the information which is
652 shown in <filename>/proc/asound/cards</filename>.
653 </para>
654 </section>
655
656 <section id="basic-flow-constructor-create-other">
657 <title>5) Create other components, such as mixer, MIDI, etc.</title>
658 <para>
659 Here you define the basic components such as
660 <link linkend="pcm-interface"><citetitle>PCM</citetitle></link>,
661 mixer (e.g. <link linkend="api-ac97"><citetitle>AC97</citetitle></link>),
662 MIDI (e.g. <link linkend="midi-interface"><citetitle>MPU-401</citetitle></link>),
663 and other interfaces.
664 Also, if you want a <link linkend="proc-interface"><citetitle>proc
665 file</citetitle></link>, define it here, too.
666 </para>
667 </section>
668
669 <section id="basic-flow-constructor-register-card">
670 <title>6) Register the card instance.</title>
671 <para>
672 <informalexample>
673 <programlisting>
674<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +0200675 err = snd_card_register(card);
676 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 snd_card_free(card);
678 return err;
679 }
680]]>
681 </programlisting>
682 </informalexample>
683 </para>
684
685 <para>
686 Will be explained in the section <link
687 linkend="card-management-registration"><citetitle>Management
688 of Cards and Components</citetitle></link>, too.
689 </para>
690 </section>
691
692 <section id="basic-flow-constructor-set-pci">
693 <title>7) Set the PCI driver data and return zero.</title>
694 <para>
695 <informalexample>
696 <programlisting>
697<![CDATA[
698 pci_set_drvdata(pci, card);
699 dev++;
700 return 0;
701]]>
702 </programlisting>
703 </informalexample>
704
705 In the above, the card record is stored. This pointer is
706 referred in the remove callback and power-management
707 callbacks, too.
708 </para>
709 </section>
710 </section>
711
712 <section id="basic-flow-destructor">
713 <title>Destructor</title>
714 <para>
715 The destructor, remove callback, simply releases the card
716 instance. Then the ALSA middle layer will release all the
717 attached components automatically.
718 </para>
719
720 <para>
721 It would be typically like the following:
722
723 <informalexample>
724 <programlisting>
725<![CDATA[
726 static void __devexit snd_mychip_remove(struct pci_dev *pci)
727 {
728 snd_card_free(pci_get_drvdata(pci));
729 pci_set_drvdata(pci, NULL);
730 }
731]]>
732 </programlisting>
733 </informalexample>
734
735 The above code assumes that the card pointer is set to the PCI
736 driver data.
737 </para>
738 </section>
739
740 <section id="basic-flow-header-files">
741 <title>Header Files</title>
742 <para>
743 For the above example, at least the following include files
744 are necessary.
745
746 <informalexample>
747 <programlisting>
748<![CDATA[
749 #include <sound/driver.h>
750 #include <linux/init.h>
751 #include <linux/pci.h>
752 #include <linux/slab.h>
753 #include <sound/core.h>
754 #include <sound/initval.h>
755]]>
756 </programlisting>
757 </informalexample>
758
759 where the last one is necessary only when module options are
760 defined in the source file. If the codes are split to several
761 files, the file without module options don't need them.
762 </para>
763
764 <para>
765 In addition to them, you'll need
766 <filename>&lt;linux/interrupt.h&gt;</filename> for the interrupt
767 handling, and <filename>&lt;asm/io.h&gt;</filename> for the i/o
768 access. If you use <function>mdelay()</function> or
769 <function>udelay()</function> functions, you'll need to include
770 <filename>&lt;linux/delay.h&gt;</filename>, too.
771 </para>
772
773 <para>
774 The ALSA interfaces like PCM or control API are defined in other
775 header files as <filename>&lt;sound/xxx.h&gt;</filename>.
776 They have to be included after
777 <filename>&lt;sound/core.h&gt;</filename>.
778 </para>
779
780 </section>
781 </chapter>
782
783
784<!-- ****************************************************** -->
785<!-- Management of Cards and Components -->
786<!-- ****************************************************** -->
787 <chapter id="card-management">
788 <title>Management of Cards and Components</title>
789
790 <section id="card-management-card-instance">
791 <title>Card Instance</title>
792 <para>
793 For each soundcard, a <quote>card</quote> record must be allocated.
794 </para>
795
796 <para>
797 A card record is the headquarters of the soundcard. It manages
798 the list of whole devices (components) on the soundcard, such as
799 PCM, mixers, MIDI, synthesizer, and so on. Also, the card
800 record holds the ID and the name strings of the card, manages
801 the root of proc files, and controls the power-management states
802 and hotplug disconnections. The component list on the card
803 record is used to manage the proper releases of resources at
804 destruction.
805 </para>
806
807 <para>
808 As mentioned above, to create a card instance, call
809 <function>snd_card_new()</function>.
810
811 <informalexample>
812 <programlisting>
813<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100814 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 card = snd_card_new(index, id, module, extra_size);
816]]>
817 </programlisting>
818 </informalexample>
819 </para>
820
821 <para>
822 The function takes four arguments, the card-index number, the
823 id string, the module pointer (usually
824 <constant>THIS_MODULE</constant>),
825 and the size of extra-data space. The last argument is used to
826 allocate card-&gt;private_data for the
827 chip-specific data. Note that this data
828 <emphasis>is</emphasis> allocated by
829 <function>snd_card_new()</function>.
830 </para>
831 </section>
832
833 <section id="card-management-component">
834 <title>Components</title>
835 <para>
836 After the card is created, you can attach the components
837 (devices) to the card instance. On ALSA driver, a component is
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100838 represented as a struct <structname>snd_device</structname> object.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 A component can be a PCM instance, a control interface, a raw
840 MIDI interface, etc. Each of such instances has one component
841 entry.
842 </para>
843
844 <para>
845 A component can be created via
846 <function>snd_device_new()</function> function.
847
848 <informalexample>
849 <programlisting>
850<![CDATA[
851 snd_device_new(card, SNDRV_DEV_XXX, chip, &ops);
852]]>
853 </programlisting>
854 </informalexample>
855 </para>
856
857 <para>
858 This takes the card pointer, the device-level
859 (<constant>SNDRV_DEV_XXX</constant>), the data pointer, and the
860 callback pointers (<parameter>&amp;ops</parameter>). The
861 device-level defines the type of components and the order of
862 registration and de-registration. For most of components, the
863 device-level is already defined. For a user-defined component,
864 you can use <constant>SNDRV_DEV_LOWLEVEL</constant>.
865 </para>
866
867 <para>
868 This function itself doesn't allocate the data space. The data
869 must be allocated manually beforehand, and its pointer is passed
870 as the argument. This pointer is used as the identifier
871 (<parameter>chip</parameter> in the above example) for the
872 instance.
873 </para>
874
875 <para>
876 Each ALSA pre-defined component such as ac97 or pcm calls
877 <function>snd_device_new()</function> inside its
878 constructor. The destructor for each component is defined in the
879 callback pointers. Hence, you don't need to take care of
880 calling a destructor for such a component.
881 </para>
882
883 <para>
884 If you would like to create your own component, you need to
885 set the destructor function to dev_free callback in
886 <parameter>ops</parameter>, so that it can be released
887 automatically via <function>snd_card_free()</function>. The
888 example will be shown later as an implementation of a
889 chip-specific data.
890 </para>
891 </section>
892
893 <section id="card-management-chip-specific">
894 <title>Chip-Specific Data</title>
895 <para>
896 The chip-specific information, e.g. the i/o port address, its
897 resource pointer, or the irq number, is stored in the
898 chip-specific record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 <informalexample>
901 <programlisting>
902<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100903 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 ....
905 };
906]]>
907 </programlisting>
908 </informalexample>
909 </para>
910
911 <para>
912 In general, there are two ways to allocate the chip record.
913 </para>
914
915 <section id="card-management-chip-specific-snd-card-new">
916 <title>1. Allocating via <function>snd_card_new()</function>.</title>
917 <para>
918 As mentioned above, you can pass the extra-data-length to the 4th argument of <function>snd_card_new()</function>, i.e.
919
920 <informalexample>
921 <programlisting>
922<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100923 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(struct mychip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924]]>
925 </programlisting>
926 </informalexample>
927
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100928 whether struct <structname>mychip</structname> is the type of the chip record.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 </para>
930
931 <para>
932 In return, the allocated record can be accessed as
933
934 <informalexample>
935 <programlisting>
936<![CDATA[
Takashi Iwai437a5a42006-11-21 12:14:23 +0100937 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938]]>
939 </programlisting>
940 </informalexample>
941
942 With this method, you don't have to allocate twice.
943 The record is released together with the card instance.
944 </para>
945 </section>
946
947 <section id="card-management-chip-specific-allocate-extra">
948 <title>2. Allocating an extra device.</title>
949
950 <para>
951 After allocating a card instance via
952 <function>snd_card_new()</function> (with
953 <constant>NULL</constant> on the 4th arg), call
Takashi Iwai561b2202005-09-09 14:22:34 +0200954 <function>kzalloc()</function>.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 <informalexample>
957 <programlisting>
958<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100959 struct snd_card *card;
960 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 card = snd_card_new(index[dev], id[dev], THIS_MODULE, NULL);
962 .....
Takashi Iwai561b2202005-09-09 14:22:34 +0200963 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964]]>
965 </programlisting>
966 </informalexample>
967 </para>
968
969 <para>
970 The chip record should have the field to hold the card
971 pointer at least,
972
973 <informalexample>
974 <programlisting>
975<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +0100976 struct mychip {
977 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 ....
979 };
980]]>
981 </programlisting>
982 </informalexample>
983 </para>
984
985 <para>
986 Then, set the card pointer in the returned chip instance.
987
988 <informalexample>
989 <programlisting>
990<![CDATA[
991 chip->card = card;
992]]>
993 </programlisting>
994 </informalexample>
995 </para>
996
997 <para>
998 Next, initialize the fields, and register this chip
999 record as a low-level device with a specified
1000 <parameter>ops</parameter>,
1001
1002 <informalexample>
1003 <programlisting>
1004<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001005 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 .dev_free = snd_mychip_dev_free,
1007 };
1008 ....
1009 snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1010]]>
1011 </programlisting>
1012 </informalexample>
1013
1014 <function>snd_mychip_dev_free()</function> is the
1015 device-destructor function, which will call the real
1016 destructor.
1017 </para>
1018
1019 <para>
1020 <informalexample>
1021 <programlisting>
1022<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001023 static int snd_mychip_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001025 return snd_mychip_free(device->device_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027]]>
1028 </programlisting>
1029 </informalexample>
1030
1031 where <function>snd_mychip_free()</function> is the real destructor.
1032 </para>
1033 </section>
1034 </section>
1035
1036 <section id="card-management-registration">
1037 <title>Registration and Release</title>
1038 <para>
1039 After all components are assigned, register the card instance
1040 by calling <function>snd_card_register()</function>. The access
1041 to the device files are enabled at this point. That is, before
1042 <function>snd_card_register()</function> is called, the
1043 components are safely inaccessible from external side. If this
1044 call fails, exit the probe function after releasing the card via
1045 <function>snd_card_free()</function>.
1046 </para>
1047
1048 <para>
1049 For releasing the card instance, you can call simply
1050 <function>snd_card_free()</function>. As already mentioned, all
1051 components are released automatically by this call.
1052 </para>
1053
1054 <para>
1055 As further notes, the destructors (both
1056 <function>snd_mychip_dev_free</function> and
1057 <function>snd_mychip_free</function>) cannot be defined with
1058 <parameter>__devexit</parameter> prefix, because they may be
1059 called from the constructor, too, at the false path.
1060 </para>
1061
1062 <para>
1063 For a device which allows hotplugging, you can use
Takashi Iwai2b29b132006-06-23 14:38:26 +02001064 <function>snd_card_free_when_closed</function>. This one will
1065 postpone the destruction until all devices are closed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 </para>
1067
1068 </section>
1069
1070 </chapter>
1071
1072
1073<!-- ****************************************************** -->
1074<!-- PCI Resource Managements -->
1075<!-- ****************************************************** -->
1076 <chapter id="pci-resource">
1077 <title>PCI Resource Managements</title>
1078
1079 <section id="pci-resource-example">
1080 <title>Full Code Example</title>
1081 <para>
1082 In this section, we'll finish the chip-specific constructor,
1083 destructor and PCI entries. The example code is shown first,
1084 below.
1085
1086 <example>
1087 <title>PCI Resource Managements Example</title>
1088 <programlisting>
1089<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001090 struct mychip {
1091 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct pci_dev *pci;
1093
1094 unsigned long port;
1095 int irq;
1096 };
1097
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001098 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 {
1100 /* disable hardware here if any */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001101 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* release the irq */
1104 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001105 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /* release the i/o ports & memory */
1107 pci_release_regions(chip->pci);
1108 /* disable the PCI entry */
1109 pci_disable_device(chip->pci);
1110 /* release the data */
1111 kfree(chip);
1112 return 0;
1113 }
1114
1115 /* chip-specific constructor */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001116 static int __devinit snd_mychip_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct pci_dev *pci,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001118 struct mychip **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001120 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001122 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 .dev_free = snd_mychip_dev_free,
1124 };
1125
1126 *rchip = NULL;
1127
1128 /* initialize the PCI entry */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001129 err = pci_enable_device(pci);
1130 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return err;
1132 /* check PCI availability (28bit DMA) */
Tobias Klauser56b146d2006-04-10 22:54:21 -07001133 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
1134 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 printk(KERN_ERR "error to set 28bit mask DMA\n");
1136 pci_disable_device(pci);
1137 return -ENXIO;
1138 }
1139
Takashi Iwai561b2202005-09-09 14:22:34 +02001140 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (chip == NULL) {
1142 pci_disable_device(pci);
1143 return -ENOMEM;
1144 }
1145
1146 /* initialize the stuff */
1147 chip->card = card;
1148 chip->pci = pci;
1149 chip->irq = -1;
1150
1151 /* (1) PCI resource allocation */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001152 err = pci_request_regions(pci, "My Chip");
1153 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 kfree(chip);
1155 pci_disable_device(pci);
1156 return err;
1157 }
1158 chip->port = pci_resource_start(pci, 0);
1159 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwai437a5a42006-11-21 12:14:23 +01001160 IRQF_SHARED, "My Chip", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1162 snd_mychip_free(chip);
1163 return -EBUSY;
1164 }
1165 chip->irq = pci->irq;
1166
1167 /* (2) initialization of the chip hardware */
Takashi Iwai95a5b082007-07-26 16:50:09 +02001168 .... /* (not implemented in this document) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Takashi Iwai95a5b082007-07-26 16:50:09 +02001170 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1171 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 snd_mychip_free(chip);
1173 return err;
1174 }
1175
1176 snd_card_set_dev(card, &pci->dev);
1177
1178 *rchip = chip;
1179 return 0;
1180 }
1181
1182 /* PCI IDs */
Takashi Iwaif40b6892006-07-05 16:51:05 +02001183 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1185 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1186 ....
1187 { 0, }
1188 };
1189 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1190
1191 /* pci_driver definition */
1192 static struct pci_driver driver = {
1193 .name = "My Own Chip",
1194 .id_table = snd_mychip_ids,
1195 .probe = snd_mychip_probe,
1196 .remove = __devexit_p(snd_mychip_remove),
1197 };
1198
1199 /* initialization of the module */
1200 static int __init alsa_card_mychip_init(void)
1201 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001202 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
1204
1205 /* clean up the module */
1206 static void __exit alsa_card_mychip_exit(void)
1207 {
1208 pci_unregister_driver(&driver);
1209 }
1210
1211 module_init(alsa_card_mychip_init)
1212 module_exit(alsa_card_mychip_exit)
1213
1214 EXPORT_NO_SYMBOLS; /* for old kernels only */
1215]]>
1216 </programlisting>
1217 </example>
1218 </para>
1219 </section>
1220
1221 <section id="pci-resource-some-haftas">
1222 <title>Some Hafta's</title>
1223 <para>
1224 The allocation of PCI resources is done in the
1225 <function>probe()</function> function, and usually an extra
1226 <function>xxx_create()</function> function is written for this
Tobias Klauser56b146d2006-04-10 22:54:21 -07001227 purpose.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 </para>
1229
1230 <para>
1231 In the case of PCI devices, you have to call at first
1232 <function>pci_enable_device()</function> function before
1233 allocating resources. Also, you need to set the proper PCI DMA
1234 mask to limit the accessed i/o range. In some cases, you might
1235 need to call <function>pci_set_master()</function> function,
Tobias Klauser56b146d2006-04-10 22:54:21 -07001236 too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 </para>
1238
1239 <para>
1240 Suppose the 28bit mask, and the code to be added would be like:
1241
1242 <informalexample>
1243 <programlisting>
1244<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001245 err = pci_enable_device(pci);
1246 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return err;
Tobias Klauser56b146d2006-04-10 22:54:21 -07001248 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
1249 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 printk(KERN_ERR "error to set 28bit mask DMA\n");
1251 pci_disable_device(pci);
1252 return -ENXIO;
1253 }
1254
1255]]>
1256 </programlisting>
1257 </informalexample>
1258 </para>
1259 </section>
1260
1261 <section id="pci-resource-resource-allocation">
1262 <title>Resource Allocation</title>
1263 <para>
1264 The allocation of I/O ports and irqs are done via standard kernel
1265 functions. Unlike ALSA ver.0.5.x., there are no helpers for
1266 that. And these resources must be released in the destructor
1267 function (see below). Also, on ALSA 0.9.x, you don't need to
Tobias Klauser56b146d2006-04-10 22:54:21 -07001268 allocate (pseudo-)DMA for PCI like ALSA 0.5.x.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 </para>
1270
1271 <para>
1272 Now assume that this PCI device has an I/O port with 8 bytes
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001273 and an interrupt. Then struct <structname>mychip</structname> will have the
Tobias Klauser56b146d2006-04-10 22:54:21 -07001274 following fields:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 <informalexample>
1277 <programlisting>
1278<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001279 struct mychip {
1280 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 unsigned long port;
1283 int irq;
1284 };
1285]]>
1286 </programlisting>
1287 </informalexample>
1288 </para>
1289
1290 <para>
1291 For an i/o port (and also a memory region), you need to have
1292 the resource pointer for the standard resource management. For
1293 an irq, you have to keep only the irq number (integer). But you
1294 need to initialize this number as -1 before actual allocation,
1295 since irq 0 is valid. The port address and its resource pointer
1296 can be initialized as null by
Takashi Iwai561b2202005-09-09 14:22:34 +02001297 <function>kzalloc()</function> automatically, so you
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 don't have to take care of resetting them.
1299 </para>
1300
1301 <para>
1302 The allocation of an i/o port is done like this:
1303
1304 <informalexample>
1305 <programlisting>
1306<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02001307 err = pci_request_regions(pci, "My Chip");
1308 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 kfree(chip);
1310 pci_disable_device(pci);
1311 return err;
1312 }
1313 chip->port = pci_resource_start(pci, 0);
1314]]>
1315 </programlisting>
1316 </informalexample>
1317 </para>
1318
1319 <para>
1320 <!-- obsolete -->
1321 It will reserve the i/o port region of 8 bytes of the given
1322 PCI device. The returned value, chip-&gt;res_port, is allocated
1323 via <function>kmalloc()</function> by
1324 <function>request_region()</function>. The pointer must be
1325 released via <function>kfree()</function>, but there is some
1326 problem regarding this. This issue will be explained more below.
1327 </para>
1328
1329 <para>
1330 The allocation of an interrupt source is done like this:
1331
1332 <informalexample>
1333 <programlisting>
1334<![CDATA[
1335 if (request_irq(pci->irq, snd_mychip_interrupt,
Takashi Iwaia1101332007-08-07 16:16:07 +02001336 IRQF_SHARED, "My Chip", chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 printk(KERN_ERR "cannot grab irq %d\n", pci->irq);
1338 snd_mychip_free(chip);
1339 return -EBUSY;
1340 }
1341 chip->irq = pci->irq;
1342]]>
1343 </programlisting>
1344 </informalexample>
1345
1346 where <function>snd_mychip_interrupt()</function> is the
1347 interrupt handler defined <link
1348 linkend="pcm-interface-interrupt-handler"><citetitle>later</citetitle></link>.
1349 Note that chip-&gt;irq should be defined
1350 only when <function>request_irq()</function> succeeded.
1351 </para>
1352
1353 <para>
1354 On the PCI bus, the interrupts can be shared. Thus,
Thomas Gleixner6ce6c7f2006-07-01 19:29:47 -07001355 <constant>IRQF_SHARED</constant> is given as the interrupt flag of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 <function>request_irq()</function>.
1357 </para>
1358
1359 <para>
1360 The last argument of <function>request_irq()</function> is the
1361 data pointer passed to the interrupt handler. Usually, the
1362 chip-specific record is used for that, but you can use what you
1363 like, too.
1364 </para>
1365
1366 <para>
1367 I won't define the detail of the interrupt handler at this
1368 point, but at least its appearance can be explained now. The
1369 interrupt handler looks usually like the following:
1370
1371 <informalexample>
1372 <programlisting>
1373<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01001374 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001376 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 ....
1378 return IRQ_HANDLED;
1379 }
1380]]>
1381 </programlisting>
1382 </informalexample>
1383 </para>
1384
1385 <para>
1386 Now let's write the corresponding destructor for the resources
1387 above. The role of destructor is simple: disable the hardware
1388 (if already activated) and release the resources. So far, we
1389 have no hardware part, so the disabling is not written here.
1390 </para>
1391
1392 <para>
1393 For releasing the resources, <quote>check-and-release</quote>
1394 method is a safer way. For the interrupt, do like this:
1395
1396 <informalexample>
1397 <programlisting>
1398<![CDATA[
1399 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001400 free_irq(chip->irq, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401]]>
1402 </programlisting>
1403 </informalexample>
1404
1405 Since the irq number can start from 0, you should initialize
1406 chip-&gt;irq with a negative value (e.g. -1), so that you can
1407 check the validity of the irq number as above.
1408 </para>
1409
1410 <para>
1411 When you requested I/O ports or memory regions via
1412 <function>pci_request_region()</function> or
1413 <function>pci_request_regions()</function> like this example,
1414 release the resource(s) using the corresponding function,
1415 <function>pci_release_region()</function> or
1416 <function>pci_release_regions()</function>.
1417
1418 <informalexample>
1419 <programlisting>
1420<![CDATA[
1421 pci_release_regions(chip->pci);
1422]]>
1423 </programlisting>
1424 </informalexample>
1425 </para>
1426
1427 <para>
1428 When you requested manually via <function>request_region()</function>
1429 or <function>request_mem_region</function>, you can release it via
1430 <function>release_resource()</function>. Suppose that you keep
1431 the resource pointer returned from <function>request_region()</function>
1432 in chip-&gt;res_port, the release procedure looks like below:
1433
1434 <informalexample>
1435 <programlisting>
1436<![CDATA[
Takashi Iwaib1d57762005-10-10 11:56:31 +02001437 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438]]>
1439 </programlisting>
1440 </informalexample>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 </para>
1442
1443 <para>
1444 Don't forget to call <function>pci_disable_device()</function>
1445 before all finished.
1446 </para>
1447
1448 <para>
1449 And finally, release the chip-specific record.
1450
1451 <informalexample>
1452 <programlisting>
1453<![CDATA[
1454 kfree(chip);
1455]]>
1456 </programlisting>
1457 </informalexample>
1458 </para>
1459
1460 <para>
1461 Again, remember that you cannot
1462 set <parameter>__devexit</parameter> prefix for this destructor.
1463 </para>
1464
1465 <para>
1466 We didn't implement the hardware-disabling part in the above.
1467 If you need to do this, please note that the destructor may be
1468 called even before the initialization of the chip is completed.
1469 It would be better to have a flag to skip the hardware-disabling
1470 if the hardware was not initialized yet.
1471 </para>
1472
1473 <para>
1474 When the chip-data is assigned to the card using
1475 <function>snd_device_new()</function> with
1476 <constant>SNDRV_DEV_LOWLELVEL</constant> , its destructor is
1477 called at the last. That is, it is assured that all other
1478 components like PCMs and controls have been already released.
1479 You don't have to call stopping PCMs, etc. explicitly, but just
1480 stop the hardware in the low-level.
1481 </para>
1482
1483 <para>
1484 The management of a memory-mapped region is almost as same as
1485 the management of an i/o port. You'll need three fields like
1486 the following:
1487
1488 <informalexample>
1489 <programlisting>
1490<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001491 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ....
1493 unsigned long iobase_phys;
1494 void __iomem *iobase_virt;
1495 };
1496]]>
1497 </programlisting>
1498 </informalexample>
1499
1500 and the allocation would be like below:
1501
1502 <informalexample>
1503 <programlisting>
1504<![CDATA[
1505 if ((err = pci_request_regions(pci, "My Chip")) < 0) {
1506 kfree(chip);
1507 return err;
1508 }
1509 chip->iobase_phys = pci_resource_start(pci, 0);
1510 chip->iobase_virt = ioremap_nocache(chip->iobase_phys,
1511 pci_resource_len(pci, 0));
1512]]>
1513 </programlisting>
1514 </informalexample>
1515
1516 and the corresponding destructor would be:
1517
1518 <informalexample>
1519 <programlisting>
1520<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001521 static int snd_mychip_free(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 {
1523 ....
1524 if (chip->iobase_virt)
1525 iounmap(chip->iobase_virt);
1526 ....
1527 pci_release_regions(chip->pci);
1528 ....
1529 }
1530]]>
1531 </programlisting>
1532 </informalexample>
1533 </para>
1534
1535 </section>
1536
1537 <section id="pci-resource-device-struct">
1538 <title>Registration of Device Struct</title>
1539 <para>
1540 At some point, typically after calling <function>snd_device_new()</function>,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001541 you need to register the struct <structname>device</structname> of the chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 you're handling for udev and co. ALSA provides a macro for compatibility with
1543 older kernels. Simply call like the following:
1544 <informalexample>
1545 <programlisting>
1546<![CDATA[
1547 snd_card_set_dev(card, &pci->dev);
1548]]>
1549 </programlisting>
1550 </informalexample>
1551 so that it stores the PCI's device pointer to the card. This will be
1552 referred by ALSA core functions later when the devices are registered.
1553 </para>
1554 <para>
1555 In the case of non-PCI, pass the proper device struct pointer of the BUS
1556 instead. (In the case of legacy ISA without PnP, you don't have to do
1557 anything.)
1558 </para>
1559 </section>
1560
1561 <section id="pci-resource-entries">
1562 <title>PCI Entries</title>
1563 <para>
1564 So far, so good. Let's finish the rest of missing PCI
1565 stuffs. At first, we need a
1566 <structname>pci_device_id</structname> table for this
1567 chipset. It's a table of PCI vendor/device ID number, and some
1568 masks.
1569 </para>
1570
1571 <para>
1572 For example,
1573
1574 <informalexample>
1575 <programlisting>
1576<![CDATA[
Takashi Iwaif40b6892006-07-05 16:51:05 +02001577 static struct pci_device_id snd_mychip_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
1579 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
1580 ....
1581 { 0, }
1582 };
1583 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
1584]]>
1585 </programlisting>
1586 </informalexample>
1587 </para>
1588
1589 <para>
1590 The first and second fields of
1591 <structname>pci_device_id</structname> struct are the vendor and
1592 device IDs. If you have nothing special to filter the matching
1593 devices, you can use the rest of fields like above. The last
1594 field of <structname>pci_device_id</structname> struct is a
1595 private data for this entry. You can specify any value here, for
1596 example, to tell the type of different operations per each
1597 device IDs. Such an example is found in intel8x0 driver.
1598 </para>
1599
1600 <para>
1601 The last entry of this list is the terminator. You must
1602 specify this all-zero entry.
1603 </para>
1604
1605 <para>
1606 Then, prepare the <structname>pci_driver</structname> record:
1607
1608 <informalexample>
1609 <programlisting>
1610<![CDATA[
1611 static struct pci_driver driver = {
1612 .name = "My Own Chip",
1613 .id_table = snd_mychip_ids,
1614 .probe = snd_mychip_probe,
1615 .remove = __devexit_p(snd_mychip_remove),
1616 };
1617]]>
1618 </programlisting>
1619 </informalexample>
1620 </para>
1621
1622 <para>
1623 The <structfield>probe</structfield> and
1624 <structfield>remove</structfield> functions are what we already
1625 defined in
1626 the previous sections. The <structfield>remove</structfield> should
1627 be defined with
1628 <function>__devexit_p()</function> macro, so that it's not
1629 defined for built-in (and non-hot-pluggable) case. The
1630 <structfield>name</structfield>
1631 field is the name string of this device. Note that you must not
1632 use a slash <quote>/</quote> in this string.
1633 </para>
1634
1635 <para>
1636 And at last, the module entries:
1637
1638 <informalexample>
1639 <programlisting>
1640<![CDATA[
1641 static int __init alsa_card_mychip_init(void)
1642 {
Takashi Iwai01d25d42005-04-11 16:58:24 +02001643 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
1646 static void __exit alsa_card_mychip_exit(void)
1647 {
1648 pci_unregister_driver(&driver);
1649 }
1650
1651 module_init(alsa_card_mychip_init)
1652 module_exit(alsa_card_mychip_exit)
1653]]>
1654 </programlisting>
1655 </informalexample>
1656 </para>
1657
1658 <para>
1659 Note that these module entries are tagged with
1660 <parameter>__init</parameter> and
1661 <parameter>__exit</parameter> prefixes, not
1662 <parameter>__devinit</parameter> nor
1663 <parameter>__devexit</parameter>.
1664 </para>
1665
1666 <para>
1667 Oh, one thing was forgotten. If you have no exported symbols,
1668 you need to declare it on 2.2 or 2.4 kernels (on 2.6 kernels
1669 it's not necessary, though).
1670
1671 <informalexample>
1672 <programlisting>
1673<![CDATA[
1674 EXPORT_NO_SYMBOLS;
1675]]>
1676 </programlisting>
1677 </informalexample>
1678
1679 That's all!
1680 </para>
1681 </section>
1682 </chapter>
1683
1684
1685<!-- ****************************************************** -->
1686<!-- PCM Interface -->
1687<!-- ****************************************************** -->
1688 <chapter id="pcm-interface">
1689 <title>PCM Interface</title>
1690
1691 <section id="pcm-interface-general">
1692 <title>General</title>
1693 <para>
1694 The PCM middle layer of ALSA is quite powerful and it is only
1695 necessary for each driver to implement the low-level functions
1696 to access its hardware.
1697 </para>
1698
1699 <para>
1700 For accessing to the PCM layer, you need to include
1701 <filename>&lt;sound/pcm.h&gt;</filename> above all. In addition,
1702 <filename>&lt;sound/pcm_params.h&gt;</filename> might be needed
1703 if you access to some functions related with hw_param.
1704 </para>
1705
1706 <para>
1707 Each card device can have up to four pcm instances. A pcm
1708 instance corresponds to a pcm device file. The limitation of
1709 number of instances comes only from the available bit size of
1710 the linux's device number. Once when 64bit device number is
1711 used, we'll have more available pcm instances.
1712 </para>
1713
1714 <para>
1715 A pcm instance consists of pcm playback and capture streams,
1716 and each pcm stream consists of one or more pcm substreams. Some
1717 soundcard supports the multiple-playback function. For example,
1718 emu10k1 has a PCM playback of 32 stereo substreams. In this case, at
1719 each open, a free substream is (usually) automatically chosen
1720 and opened. Meanwhile, when only one substream exists and it was
1721 already opened, the succeeding open will result in the blocking
1722 or the error with <constant>EAGAIN</constant> according to the
1723 file open mode. But you don't have to know the detail in your
1724 driver. The PCM middle layer will take all such jobs.
1725 </para>
1726 </section>
1727
1728 <section id="pcm-interface-example">
1729 <title>Full Code Example</title>
1730 <para>
1731 The example code below does not include any hardware access
1732 routines but shows only the skeleton, how to build up the PCM
1733 interfaces.
1734
1735 <example>
1736 <title>PCM Example Code</title>
1737 <programlisting>
1738<![CDATA[
1739 #include <sound/pcm.h>
1740 ....
1741
1742 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001743 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .info = (SNDRV_PCM_INFO_MMAP |
1745 SNDRV_PCM_INFO_INTERLEAVED |
1746 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1747 SNDRV_PCM_INFO_MMAP_VALID),
1748 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1749 .rates = SNDRV_PCM_RATE_8000_48000,
1750 .rate_min = 8000,
1751 .rate_max = 48000,
1752 .channels_min = 2,
1753 .channels_max = 2,
1754 .buffer_bytes_max = 32768,
1755 .period_bytes_min = 4096,
1756 .period_bytes_max = 32768,
1757 .periods_min = 1,
1758 .periods_max = 1024,
1759 };
1760
1761 /* hardware definition */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001762 static struct snd_pcm_hardware snd_mychip_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 .info = (SNDRV_PCM_INFO_MMAP |
1764 SNDRV_PCM_INFO_INTERLEAVED |
1765 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1766 SNDRV_PCM_INFO_MMAP_VALID),
1767 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1768 .rates = SNDRV_PCM_RATE_8000_48000,
1769 .rate_min = 8000,
1770 .rate_max = 48000,
1771 .channels_min = 2,
1772 .channels_max = 2,
1773 .buffer_bytes_max = 32768,
1774 .period_bytes_min = 4096,
1775 .period_bytes_max = 32768,
1776 .periods_min = 1,
1777 .periods_max = 1024,
1778 };
1779
1780 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001781 static int snd_mychip_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001783 struct mychip *chip = snd_pcm_substream_chip(substream);
1784 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 runtime->hw = snd_mychip_playback_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001787 /* more hardware-initialization will be done here */
1788 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 0;
1790 }
1791
1792 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001793 static int snd_mychip_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001795 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001796 /* the hardware-specific codes will be here */
1797 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799
1800 }
1801
1802 /* open callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001803 static int snd_mychip_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001805 struct mychip *chip = snd_pcm_substream_chip(substream);
1806 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 runtime->hw = snd_mychip_capture_hw;
Takashi Iwai95a5b082007-07-26 16:50:09 +02001809 /* more hardware-initialization will be done here */
1810 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 0;
1812 }
1813
1814 /* close callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001815 static int snd_mychip_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001817 struct mychip *chip = snd_pcm_substream_chip(substream);
Takashi Iwai95a5b082007-07-26 16:50:09 +02001818 /* the hardware-specific codes will be here */
1819 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return 0;
1821
1822 }
1823
1824 /* hw_params callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001825 static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
1826 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 {
1828 return snd_pcm_lib_malloc_pages(substream,
1829 params_buffer_bytes(hw_params));
1830 }
1831
1832 /* hw_free callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001833 static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 {
1835 return snd_pcm_lib_free_pages(substream);
1836 }
1837
1838 /* prepare callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001839 static int snd_mychip_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001841 struct mychip *chip = snd_pcm_substream_chip(substream);
1842 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* set up the hardware with the current configuration
1845 * for example...
1846 */
1847 mychip_set_sample_format(chip, runtime->format);
1848 mychip_set_sample_rate(chip, runtime->rate);
1849 mychip_set_channels(chip, runtime->channels);
Takashi Iwai0b7bed42006-03-02 15:35:55 +01001850 mychip_set_dma_setup(chip, runtime->dma_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 chip->buffer_size,
1852 chip->period_size);
1853 return 0;
1854 }
1855
1856 /* trigger callback */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001857 static int snd_mychip_pcm_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 int cmd)
1859 {
1860 switch (cmd) {
1861 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001862 /* do something to start the PCM engine */
1863 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02001866 /* do something to stop the PCM engine */
1867 ....
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869 default:
1870 return -EINVAL;
1871 }
1872 }
1873
1874 /* pointer callback */
1875 static snd_pcm_uframes_t
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001876 snd_mychip_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001878 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 unsigned int current_ptr;
1880
1881 /* get the current hardware pointer */
1882 current_ptr = mychip_get_hw_pointer(chip);
1883 return current_ptr;
1884 }
1885
1886 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001887 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 .open = snd_mychip_playback_open,
1889 .close = snd_mychip_playback_close,
1890 .ioctl = snd_pcm_lib_ioctl,
1891 .hw_params = snd_mychip_pcm_hw_params,
1892 .hw_free = snd_mychip_pcm_hw_free,
1893 .prepare = snd_mychip_pcm_prepare,
1894 .trigger = snd_mychip_pcm_trigger,
1895 .pointer = snd_mychip_pcm_pointer,
1896 };
1897
1898 /* operators */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001899 static struct snd_pcm_ops snd_mychip_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 .open = snd_mychip_capture_open,
1901 .close = snd_mychip_capture_close,
1902 .ioctl = snd_pcm_lib_ioctl,
1903 .hw_params = snd_mychip_pcm_hw_params,
1904 .hw_free = snd_mychip_pcm_hw_free,
1905 .prepare = snd_mychip_pcm_prepare,
1906 .trigger = snd_mychip_pcm_trigger,
1907 .pointer = snd_mychip_pcm_pointer,
1908 };
1909
1910 /*
1911 * definitions of capture are omitted here...
1912 */
1913
1914 /* create a pcm device */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001915 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001917 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 int err;
1919
Takashi Iwai95a5b082007-07-26 16:50:09 +02001920 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1921 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return err;
1923 pcm->private_data = chip;
1924 strcpy(pcm->name, "My Chip");
1925 chip->pcm = pcm;
1926 /* set operators */
1927 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1928 &snd_mychip_playback_ops);
1929 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1930 &snd_mychip_capture_ops);
1931 /* pre-allocation of buffers */
1932 /* NOTE: this may fail */
1933 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1934 snd_dma_pci_data(chip->pci),
1935 64*1024, 64*1024);
1936 return 0;
1937 }
1938]]>
1939 </programlisting>
1940 </example>
1941 </para>
1942 </section>
1943
1944 <section id="pcm-interface-constructor">
1945 <title>Constructor</title>
1946 <para>
1947 A pcm instance is allocated by <function>snd_pcm_new()</function>
1948 function. It would be better to create a constructor for pcm,
1949 namely,
1950
1951 <informalexample>
1952 <programlisting>
1953<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001954 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001956 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int err;
1958
Takashi Iwai95a5b082007-07-26 16:50:09 +02001959 err = snd_pcm_new(chip->card, "My Chip", 0, 1, 1, &pcm);
1960 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return err;
1962 pcm->private_data = chip;
1963 strcpy(pcm->name, "My Chip");
1964 chip->pcm = pcm;
1965 ....
1966 return 0;
1967 }
1968]]>
1969 </programlisting>
1970 </informalexample>
1971 </para>
1972
1973 <para>
1974 The <function>snd_pcm_new()</function> function takes the four
1975 arguments. The first argument is the card pointer to which this
1976 pcm is assigned, and the second is the ID string.
1977 </para>
1978
1979 <para>
1980 The third argument (<parameter>index</parameter>, 0 in the
1981 above) is the index of this new pcm. It begins from zero. When
1982 you will create more than one pcm instances, specify the
1983 different numbers in this argument. For example,
1984 <parameter>index</parameter> = 1 for the second PCM device.
1985 </para>
1986
1987 <para>
1988 The fourth and fifth arguments are the number of substreams
1989 for playback and capture, respectively. Here both 1 are given in
1990 the above example. When no playback or no capture is available,
1991 pass 0 to the corresponding argument.
1992 </para>
1993
1994 <para>
1995 If a chip supports multiple playbacks or captures, you can
1996 specify more numbers, but they must be handled properly in
1997 open/close, etc. callbacks. When you need to know which
1998 substream you are referring to, then it can be obtained from
Takashi Iwai446ab5f2005-11-17 15:12:54 +01001999 struct <structname>snd_pcm_substream</structname> data passed to each callback
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 as follows:
2001
2002 <informalexample>
2003 <programlisting>
2004<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002005 struct snd_pcm_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int index = substream->number;
2007]]>
2008 </programlisting>
2009 </informalexample>
2010 </para>
2011
2012 <para>
2013 After the pcm is created, you need to set operators for each
2014 pcm stream.
2015
2016 <informalexample>
2017 <programlisting>
2018<![CDATA[
2019 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2020 &snd_mychip_playback_ops);
2021 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
2022 &snd_mychip_capture_ops);
2023]]>
2024 </programlisting>
2025 </informalexample>
2026 </para>
2027
2028 <para>
2029 The operators are defined typically like this:
2030
2031 <informalexample>
2032 <programlisting>
2033<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002034 static struct snd_pcm_ops snd_mychip_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 .open = snd_mychip_pcm_open,
2036 .close = snd_mychip_pcm_close,
2037 .ioctl = snd_pcm_lib_ioctl,
2038 .hw_params = snd_mychip_pcm_hw_params,
2039 .hw_free = snd_mychip_pcm_hw_free,
2040 .prepare = snd_mychip_pcm_prepare,
2041 .trigger = snd_mychip_pcm_trigger,
2042 .pointer = snd_mychip_pcm_pointer,
2043 };
2044]]>
2045 </programlisting>
2046 </informalexample>
2047
2048 Each of callbacks is explained in the subsection
2049 <link linkend="pcm-interface-operators"><citetitle>
2050 Operators</citetitle></link>.
2051 </para>
2052
2053 <para>
2054 After setting the operators, most likely you'd like to
2055 pre-allocate the buffer. For the pre-allocation, simply call
2056 the following:
2057
2058 <informalexample>
2059 <programlisting>
2060<![CDATA[
2061 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
2062 snd_dma_pci_data(chip->pci),
2063 64*1024, 64*1024);
2064]]>
2065 </programlisting>
2066 </informalexample>
2067
2068 It will allocate up to 64kB buffer as default. The details of
2069 buffer management will be described in the later section <link
2070 linkend="buffer-and-memory"><citetitle>Buffer and Memory
2071 Management</citetitle></link>.
2072 </para>
2073
2074 <para>
2075 Additionally, you can set some extra information for this pcm
2076 in pcm-&gt;info_flags.
2077 The available values are defined as
2078 <constant>SNDRV_PCM_INFO_XXX</constant> in
2079 <filename>&lt;sound/asound.h&gt;</filename>, which is used for
2080 the hardware definition (described later). When your soundchip
2081 supports only half-duplex, specify like this:
2082
2083 <informalexample>
2084 <programlisting>
2085<![CDATA[
2086 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
2087]]>
2088 </programlisting>
2089 </informalexample>
2090 </para>
2091 </section>
2092
2093 <section id="pcm-interface-destructor">
2094 <title>... And the Destructor?</title>
2095 <para>
2096 The destructor for a pcm instance is not always
2097 necessary. Since the pcm device will be released by the middle
2098 layer code automatically, you don't have to call destructor
2099 explicitly.
2100 </para>
2101
2102 <para>
2103 The destructor would be necessary when you created some
2104 special records internally and need to release them. In such a
2105 case, set the destructor function to
2106 pcm-&gt;private_free:
2107
2108 <example>
2109 <title>PCM Instance with a Destructor</title>
2110 <programlisting>
2111<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002112 static void mychip_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002114 struct mychip *chip = snd_pcm_chip(pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /* free your own data */
2116 kfree(chip->my_private_pcm_data);
Takashi Iwai95a5b082007-07-26 16:50:09 +02002117 /* do what you like else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 ....
2119 }
2120
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002121 static int __devinit snd_mychip_new_pcm(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002123 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 ....
2125 /* allocate your own data */
2126 chip->my_private_pcm_data = kmalloc(...);
2127 /* set the destructor */
2128 pcm->private_data = chip;
2129 pcm->private_free = mychip_pcm_free;
2130 ....
2131 }
2132]]>
2133 </programlisting>
2134 </example>
2135 </para>
2136 </section>
2137
2138 <section id="pcm-interface-runtime">
2139 <title>Runtime Pointer - The Chest of PCM Information</title>
2140 <para>
2141 When the PCM substream is opened, a PCM runtime instance is
2142 allocated and assigned to the substream. This pointer is
2143 accessible via <constant>substream-&gt;runtime</constant>.
2144 This runtime pointer holds the various information; it holds
2145 the copy of hw_params and sw_params configurations, the buffer
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01002146 pointers, mmap records, spinlocks, etc. Almost everything you
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 need for controlling the PCM can be found there.
2148 </para>
2149
2150 <para>
2151 The definition of runtime instance is found in
2152 <filename>&lt;sound/pcm.h&gt;</filename>. Here is the
2153 copy from the file.
2154 <informalexample>
2155 <programlisting>
2156<![CDATA[
2157struct _snd_pcm_runtime {
2158 /* -- Status -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002159 struct snd_pcm_substream *trigger_master;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 snd_timestamp_t trigger_tstamp; /* trigger timestamp */
2161 int overrange;
2162 snd_pcm_uframes_t avail_max;
2163 snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
2164 snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time*/
2165
2166 /* -- HW params -- */
2167 snd_pcm_access_t access; /* access mode */
2168 snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
2169 snd_pcm_subformat_t subformat; /* subformat */
2170 unsigned int rate; /* rate in Hz */
2171 unsigned int channels; /* channels */
2172 snd_pcm_uframes_t period_size; /* period size */
2173 unsigned int periods; /* periods */
2174 snd_pcm_uframes_t buffer_size; /* buffer size */
2175 unsigned int tick_time; /* tick time */
2176 snd_pcm_uframes_t min_align; /* Min alignment for the format */
2177 size_t byte_align;
2178 unsigned int frame_bits;
2179 unsigned int sample_bits;
2180 unsigned int info;
2181 unsigned int rate_num;
2182 unsigned int rate_den;
2183
2184 /* -- SW params -- */
Takashi Iwai07799e72005-10-10 11:49:49 +02002185 struct timespec tstamp_mode; /* mmap timestamp is updated */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 unsigned int period_step;
2187 unsigned int sleep_min; /* min ticks to sleep */
2188 snd_pcm_uframes_t xfer_align; /* xfer size need to be a multiple */
2189 snd_pcm_uframes_t start_threshold;
2190 snd_pcm_uframes_t stop_threshold;
2191 snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
2192 noise is nearest than this */
2193 snd_pcm_uframes_t silence_size; /* Silence filling size */
2194 snd_pcm_uframes_t boundary; /* pointers wrap point */
2195
2196 snd_pcm_uframes_t silenced_start;
2197 snd_pcm_uframes_t silenced_size;
2198
2199 snd_pcm_sync_id_t sync; /* hardware synchronization ID */
2200
2201 /* -- mmap -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002202 volatile struct snd_pcm_mmap_status *status;
2203 volatile struct snd_pcm_mmap_control *control;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 atomic_t mmap_count;
2205
2206 /* -- locking / scheduling -- */
2207 spinlock_t lock;
2208 wait_queue_head_t sleep;
2209 struct timer_list tick_timer;
2210 struct fasync_struct *fasync;
2211
2212 /* -- private section -- */
2213 void *private_data;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002214 void (*private_free)(struct snd_pcm_runtime *runtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* -- hardware description -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002217 struct snd_pcm_hardware hw;
2218 struct snd_pcm_hw_constraints hw_constraints;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 /* -- interrupt callbacks -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002221 void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
2222 void (*transfer_ack_end)(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /* -- timer -- */
2225 unsigned int timer_resolution; /* timer resolution */
2226
2227 /* -- DMA -- */
2228 unsigned char *dma_area; /* DMA area */
2229 dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
2230 size_t dma_bytes; /* size of DMA area */
2231
2232 struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
2233
2234#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
2235 /* -- OSS things -- */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002236 struct snd_pcm_oss_runtime oss;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237#endif
2238};
2239]]>
2240 </programlisting>
2241 </informalexample>
2242 </para>
2243
2244 <para>
2245 For the operators (callbacks) of each sound driver, most of
2246 these records are supposed to be read-only. Only the PCM
2247 middle-layer changes / updates these info. The exceptions are
2248 the hardware description (hw), interrupt callbacks
2249 (transfer_ack_xxx), DMA buffer information, and the private
2250 data. Besides, if you use the standard buffer allocation
2251 method via <function>snd_pcm_lib_malloc_pages()</function>,
2252 you don't need to set the DMA buffer information by yourself.
2253 </para>
2254
2255 <para>
2256 In the sections below, important records are explained.
2257 </para>
2258
2259 <section id="pcm-interface-runtime-hw">
2260 <title>Hardware Description</title>
2261 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002262 The hardware descriptor (struct <structname>snd_pcm_hardware</structname>)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 contains the definitions of the fundamental hardware
2264 configuration. Above all, you'll need to define this in
2265 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2266 the open callback</citetitle></link>.
2267 Note that the runtime instance holds the copy of the
2268 descriptor, not the pointer to the existing descriptor. That
2269 is, in the open callback, you can modify the copied descriptor
2270 (<constant>runtime-&gt;hw</constant>) as you need. For example, if the maximum
2271 number of channels is 1 only on some chip models, you can
2272 still use the same hardware descriptor and change the
2273 channels_max later:
2274 <informalexample>
2275 <programlisting>
2276<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002277 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 ...
2279 runtime->hw = snd_mychip_playback_hw; /* common definition */
2280 if (chip->model == VERY_OLD_ONE)
2281 runtime->hw.channels_max = 1;
2282]]>
2283 </programlisting>
2284 </informalexample>
2285 </para>
2286
2287 <para>
2288 Typically, you'll have a hardware descriptor like below:
2289 <informalexample>
2290 <programlisting>
2291<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002292 static struct snd_pcm_hardware snd_mychip_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .info = (SNDRV_PCM_INFO_MMAP |
2294 SNDRV_PCM_INFO_INTERLEAVED |
2295 SNDRV_PCM_INFO_BLOCK_TRANSFER |
2296 SNDRV_PCM_INFO_MMAP_VALID),
2297 .formats = SNDRV_PCM_FMTBIT_S16_LE,
2298 .rates = SNDRV_PCM_RATE_8000_48000,
2299 .rate_min = 8000,
2300 .rate_max = 48000,
2301 .channels_min = 2,
2302 .channels_max = 2,
2303 .buffer_bytes_max = 32768,
2304 .period_bytes_min = 4096,
2305 .period_bytes_max = 32768,
2306 .periods_min = 1,
2307 .periods_max = 1024,
2308 };
2309]]>
2310 </programlisting>
2311 </informalexample>
2312 </para>
2313
2314 <para>
2315 <itemizedlist>
2316 <listitem><para>
2317 The <structfield>info</structfield> field contains the type and
2318 capabilities of this pcm. The bit flags are defined in
2319 <filename>&lt;sound/asound.h&gt;</filename> as
2320 <constant>SNDRV_PCM_INFO_XXX</constant>. Here, at least, you
2321 have to specify whether the mmap is supported and which
2322 interleaved format is supported.
2323 When the mmap is supported, add
2324 <constant>SNDRV_PCM_INFO_MMAP</constant> flag here. When the
2325 hardware supports the interleaved or the non-interleaved
2326 format, <constant>SNDRV_PCM_INFO_INTERLEAVED</constant> or
2327 <constant>SNDRV_PCM_INFO_NONINTERLEAVED</constant> flag must
2328 be set, respectively. If both are supported, you can set both,
2329 too.
2330 </para>
2331
2332 <para>
2333 In the above example, <constant>MMAP_VALID</constant> and
2334 <constant>BLOCK_TRANSFER</constant> are specified for OSS mmap
2335 mode. Usually both are set. Of course,
2336 <constant>MMAP_VALID</constant> is set only if the mmap is
2337 really supported.
2338 </para>
2339
2340 <para>
2341 The other possible flags are
2342 <constant>SNDRV_PCM_INFO_PAUSE</constant> and
2343 <constant>SNDRV_PCM_INFO_RESUME</constant>. The
2344 <constant>PAUSE</constant> bit means that the pcm supports the
2345 <quote>pause</quote> operation, while the
2346 <constant>RESUME</constant> bit means that the pcm supports
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002347 the full <quote>suspend/resume</quote> operation.
2348 If <constant>PAUSE</constant> flag is set,
2349 the <structfield>trigger</structfield> callback below
2350 must handle the corresponding (pause push/release) commands.
2351 The suspend/resume trigger commands can be defined even without
2352 <constant>RESUME</constant> flag. See <link
2353 linkend="power-management"><citetitle>
2354 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 </para>
2356
2357 <para>
2358 When the PCM substreams can be synchronized (typically,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01002359 synchronized start/stop of a playback and a capture streams),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 you can give <constant>SNDRV_PCM_INFO_SYNC_START</constant>,
2361 too. In this case, you'll need to check the linked-list of
2362 PCM substreams in the trigger callback. This will be
2363 described in the later section.
2364 </para>
2365 </listitem>
2366
2367 <listitem>
2368 <para>
2369 <structfield>formats</structfield> field contains the bit-flags
2370 of supported formats (<constant>SNDRV_PCM_FMTBIT_XXX</constant>).
2371 If the hardware supports more than one format, give all or'ed
2372 bits. In the example above, the signed 16bit little-endian
2373 format is specified.
2374 </para>
2375 </listitem>
2376
2377 <listitem>
2378 <para>
2379 <structfield>rates</structfield> field contains the bit-flags of
2380 supported rates (<constant>SNDRV_PCM_RATE_XXX</constant>).
2381 When the chip supports continuous rates, pass
2382 <constant>CONTINUOUS</constant> bit additionally.
2383 The pre-defined rate bits are provided only for typical
2384 rates. If your chip supports unconventional rates, you need to add
2385 <constant>KNOT</constant> bit and set up the hardware
2386 constraint manually (explained later).
2387 </para>
2388 </listitem>
2389
2390 <listitem>
2391 <para>
2392 <structfield>rate_min</structfield> and
2393 <structfield>rate_max</structfield> define the minimal and
2394 maximal sample rate. This should correspond somehow to
2395 <structfield>rates</structfield> bits.
2396 </para>
2397 </listitem>
2398
2399 <listitem>
2400 <para>
2401 <structfield>channel_min</structfield> and
2402 <structfield>channel_max</structfield>
2403 define, as you might already expected, the minimal and maximal
2404 number of channels.
2405 </para>
2406 </listitem>
2407
2408 <listitem>
2409 <para>
2410 <structfield>buffer_bytes_max</structfield> defines the
2411 maximal buffer size in bytes. There is no
2412 <structfield>buffer_bytes_min</structfield> field, since
2413 it can be calculated from the minimal period size and the
2414 minimal number of periods.
2415 Meanwhile, <structfield>period_bytes_min</structfield> and
2416 define the minimal and maximal size of the period in bytes.
2417 <structfield>periods_max</structfield> and
2418 <structfield>periods_min</structfield> define the maximal and
2419 minimal number of periods in the buffer.
2420 </para>
2421
2422 <para>
2423 The <quote>period</quote> is a term, that corresponds to
2424 fragment in the OSS world. The period defines the size at
2425 which the PCM interrupt is generated. This size strongly
2426 depends on the hardware.
2427 Generally, the smaller period size will give you more
2428 interrupts, that is, more controls.
2429 In the case of capture, this size defines the input latency.
2430 On the other hand, the whole buffer size defines the
2431 output latency for the playback direction.
2432 </para>
2433 </listitem>
2434
2435 <listitem>
2436 <para>
2437 There is also a field <structfield>fifo_size</structfield>.
2438 This specifies the size of the hardware FIFO, but it's not
2439 used currently in the driver nor in the alsa-lib. So, you
2440 can ignore this field.
2441 </para>
2442 </listitem>
2443 </itemizedlist>
2444 </para>
2445 </section>
2446
2447 <section id="pcm-interface-runtime-config">
2448 <title>PCM Configurations</title>
2449 <para>
2450 Ok, let's go back again to the PCM runtime records.
2451 The most frequently referred records in the runtime instance are
2452 the PCM configurations.
2453 The PCM configurations are stored on runtime instance
2454 after the application sends <type>hw_params</type> data via
2455 alsa-lib. There are many fields copied from hw_params and
2456 sw_params structs. For example,
2457 <structfield>format</structfield> holds the format type
2458 chosen by the application. This field contains the enum value
2459 <constant>SNDRV_PCM_FORMAT_XXX</constant>.
2460 </para>
2461
2462 <para>
2463 One thing to be noted is that the configured buffer and period
2464 sizes are stored in <quote>frames</quote> in the runtime
2465 In the ALSA world, 1 frame = channels * samples-size.
2466 For conversion between frames and bytes, you can use the
2467 helper functions, <function>frames_to_bytes()</function> and
2468 <function>bytes_to_frames()</function>.
2469 <informalexample>
2470 <programlisting>
2471<![CDATA[
2472 period_bytes = frames_to_bytes(runtime, runtime->period_size);
2473]]>
2474 </programlisting>
2475 </informalexample>
2476 </para>
2477
2478 <para>
2479 Also, many software parameters (sw_params) are
2480 stored in frames, too. Please check the type of the field.
2481 <type>snd_pcm_uframes_t</type> is for the frames as unsigned
2482 integer while <type>snd_pcm_sframes_t</type> is for the frames
2483 as signed integer.
2484 </para>
2485 </section>
2486
2487 <section id="pcm-interface-runtime-dma">
2488 <title>DMA Buffer Information</title>
2489 <para>
2490 The DMA buffer is defined by the following four fields,
2491 <structfield>dma_area</structfield>,
2492 <structfield>dma_addr</structfield>,
2493 <structfield>dma_bytes</structfield> and
2494 <structfield>dma_private</structfield>.
2495 The <structfield>dma_area</structfield> holds the buffer
2496 pointer (the logical address). You can call
2497 <function>memcpy</function> from/to
2498 this pointer. Meanwhile, <structfield>dma_addr</structfield>
2499 holds the physical address of the buffer. This field is
2500 specified only when the buffer is a linear buffer.
2501 <structfield>dma_bytes</structfield> holds the size of buffer
2502 in bytes. <structfield>dma_private</structfield> is used for
2503 the ALSA DMA allocator.
2504 </para>
2505
2506 <para>
2507 If you use a standard ALSA function,
2508 <function>snd_pcm_lib_malloc_pages()</function>, for
2509 allocating the buffer, these fields are set by the ALSA middle
2510 layer, and you should <emphasis>not</emphasis> change them by
2511 yourself. You can read them but not write them.
2512 On the other hand, if you want to allocate the buffer by
2513 yourself, you'll need to manage it in hw_params callback.
2514 At least, <structfield>dma_bytes</structfield> is mandatory.
2515 <structfield>dma_area</structfield> is necessary when the
2516 buffer is mmapped. If your driver doesn't support mmap, this
2517 field is not necessary. <structfield>dma_addr</structfield>
2518 is also not mandatory. You can use
2519 <structfield>dma_private</structfield> as you like, too.
2520 </para>
2521 </section>
2522
2523 <section id="pcm-interface-runtime-status">
2524 <title>Running Status</title>
2525 <para>
2526 The running status can be referred via <constant>runtime-&gt;status</constant>.
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002527 This is the pointer to struct <structname>snd_pcm_mmap_status</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 record. For example, you can get the current DMA hardware
2529 pointer via <constant>runtime-&gt;status-&gt;hw_ptr</constant>.
2530 </para>
2531
2532 <para>
2533 The DMA application pointer can be referred via
2534 <constant>runtime-&gt;control</constant>, which points
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002535 struct <structname>snd_pcm_mmap_control</structname> record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 However, accessing directly to this value is not recommended.
2537 </para>
2538 </section>
2539
2540 <section id="pcm-interface-runtime-private">
2541 <title>Private Data</title>
2542 <para>
2543 You can allocate a record for the substream and store it in
2544 <constant>runtime-&gt;private_data</constant>. Usually, this
2545 done in
2546 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2547 the open callback</citetitle></link>.
2548 Don't mix this with <constant>pcm-&gt;private_data</constant>.
2549 The <constant>pcm-&gt;private_data</constant> usually points the
2550 chip instance assigned statically at the creation of PCM, while the
2551 <constant>runtime-&gt;private_data</constant> points a dynamic
2552 data created at the PCM open callback.
2553
2554 <informalexample>
2555 <programlisting>
2556<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002557 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002559 struct my_pcm_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 ....
2561 data = kmalloc(sizeof(*data), GFP_KERNEL);
2562 substream->runtime->private_data = data;
2563 ....
2564 }
2565]]>
2566 </programlisting>
2567 </informalexample>
2568 </para>
2569
2570 <para>
2571 The allocated object must be released in
2572 <link linkend="pcm-interface-operators-open-callback"><citetitle>
2573 the close callback</citetitle></link>.
2574 </para>
2575 </section>
2576
2577 <section id="pcm-interface-runtime-intr">
2578 <title>Interrupt Callbacks</title>
2579 <para>
2580 The field <structfield>transfer_ack_begin</structfield> and
2581 <structfield>transfer_ack_end</structfield> are called at
2582 the beginning and the end of
2583 <function>snd_pcm_period_elapsed()</function>, respectively.
2584 </para>
2585 </section>
2586
2587 </section>
2588
2589 <section id="pcm-interface-operators">
2590 <title>Operators</title>
2591 <para>
2592 OK, now let me explain the detail of each pcm callback
2593 (<parameter>ops</parameter>). In general, every callback must
2594 return 0 if successful, or a negative number with the error
2595 number such as <constant>-EINVAL</constant> at any
2596 error.
2597 </para>
2598
2599 <para>
2600 The callback function takes at least the argument with
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002601 <structname>snd_pcm_substream</structname> pointer. For retrieving the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 chip record from the given substream instance, you can use the
2603 following macro.
2604
2605 <informalexample>
2606 <programlisting>
2607<![CDATA[
2608 int xxx() {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002609 struct mychip *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 ....
2611 }
2612]]>
2613 </programlisting>
2614 </informalexample>
2615
2616 The macro reads <constant>substream-&gt;private_data</constant>,
2617 which is a copy of <constant>pcm-&gt;private_data</constant>.
2618 You can override the former if you need to assign different data
2619 records per PCM substream. For example, cmi8330 driver assigns
2620 different private_data for playback and capture directions,
2621 because it uses two different codecs (SB- and AD-compatible) for
2622 different directions.
2623 </para>
2624
2625 <section id="pcm-interface-operators-open-callback">
2626 <title>open callback</title>
2627 <para>
2628 <informalexample>
2629 <programlisting>
2630<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002631 static int snd_xxx_open(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632]]>
2633 </programlisting>
2634 </informalexample>
2635
2636 This is called when a pcm substream is opened.
2637 </para>
2638
2639 <para>
2640 At least, here you have to initialize the runtime-&gt;hw
2641 record. Typically, this is done by like this:
2642
2643 <informalexample>
2644 <programlisting>
2645<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002646 static int snd_xxx_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002648 struct mychip *chip = snd_pcm_substream_chip(substream);
2649 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 runtime->hw = snd_mychip_playback_hw;
2652 return 0;
2653 }
2654]]>
2655 </programlisting>
2656 </informalexample>
2657
2658 where <parameter>snd_mychip_playback_hw</parameter> is the
2659 pre-defined hardware description.
2660 </para>
2661
2662 <para>
2663 You can allocate a private data in this callback, as described
2664 in <link linkend="pcm-interface-runtime-private"><citetitle>
2665 Private Data</citetitle></link> section.
2666 </para>
2667
2668 <para>
2669 If the hardware configuration needs more constraints, set the
2670 hardware constraints here, too.
2671 See <link linkend="pcm-interface-constraints"><citetitle>
2672 Constraints</citetitle></link> for more details.
2673 </para>
2674 </section>
2675
2676 <section id="pcm-interface-operators-close-callback">
2677 <title>close callback</title>
2678 <para>
2679 <informalexample>
2680 <programlisting>
2681<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002682 static int snd_xxx_close(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683]]>
2684 </programlisting>
2685 </informalexample>
2686
2687 Obviously, this is called when a pcm substream is closed.
2688 </para>
2689
2690 <para>
2691 Any private instance for a pcm substream allocated in the
2692 open callback will be released here.
2693
2694 <informalexample>
2695 <programlisting>
2696<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002697 static int snd_xxx_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 {
2699 ....
2700 kfree(substream->runtime->private_data);
2701 ....
2702 }
2703]]>
2704 </programlisting>
2705 </informalexample>
2706 </para>
2707 </section>
2708
2709 <section id="pcm-interface-operators-ioctl-callback">
2710 <title>ioctl callback</title>
2711 <para>
2712 This is used for any special action to pcm ioctls. But
2713 usually you can pass a generic ioctl callback,
2714 <function>snd_pcm_lib_ioctl</function>.
2715 </para>
2716 </section>
2717
2718 <section id="pcm-interface-operators-hw-params-callback">
2719 <title>hw_params callback</title>
2720 <para>
2721 <informalexample>
2722 <programlisting>
2723<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002724 static int snd_xxx_hw_params(struct snd_pcm_substream *substream,
2725 struct snd_pcm_hw_params *hw_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726]]>
2727 </programlisting>
2728 </informalexample>
2729
2730 This and <structfield>hw_free</structfield> callbacks exist
2731 only on ALSA 0.9.x.
2732 </para>
2733
2734 <para>
2735 This is called when the hardware parameter
2736 (<structfield>hw_params</structfield>) is set
2737 up by the application,
2738 that is, once when the buffer size, the period size, the
2739 format, etc. are defined for the pcm substream.
2740 </para>
2741
2742 <para>
2743 Many hardware set-up should be done in this callback,
2744 including the allocation of buffers.
2745 </para>
2746
2747 <para>
2748 Parameters to be initialized are retrieved by
2749 <function>params_xxx()</function> macros. For allocating a
2750 buffer, you can call a helper function,
2751
2752 <informalexample>
2753 <programlisting>
2754<![CDATA[
2755 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
2756]]>
2757 </programlisting>
2758 </informalexample>
2759
2760 <function>snd_pcm_lib_malloc_pages()</function> is available
2761 only when the DMA buffers have been pre-allocated.
2762 See the section <link
2763 linkend="buffer-and-memory-buffer-types"><citetitle>
2764 Buffer Types</citetitle></link> for more details.
2765 </para>
2766
2767 <para>
2768 Note that this and <structfield>prepare</structfield> callbacks
2769 may be called multiple times per initialization.
2770 For example, the OSS emulation may
2771 call these callbacks at each change via its ioctl.
2772 </para>
2773
2774 <para>
2775 Thus, you need to take care not to allocate the same buffers
2776 many times, which will lead to memory leak! Calling the
2777 helper function above many times is OK. It will release the
2778 previous buffer automatically when it was already allocated.
2779 </para>
2780
2781 <para>
2782 Another note is that this callback is non-atomic
2783 (schedulable). This is important, because the
2784 <structfield>trigger</structfield> callback
2785 is atomic (non-schedulable). That is, mutex or any
2786 schedule-related functions are not available in
2787 <structfield>trigger</structfield> callback.
2788 Please see the subsection
2789 <link linkend="pcm-interface-atomicity"><citetitle>
2790 Atomicity</citetitle></link> for details.
2791 </para>
2792 </section>
2793
2794 <section id="pcm-interface-operators-hw-free-callback">
2795 <title>hw_free callback</title>
2796 <para>
2797 <informalexample>
2798 <programlisting>
2799<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002800 static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801]]>
2802 </programlisting>
2803 </informalexample>
2804 </para>
2805
2806 <para>
2807 This is called to release the resources allocated via
2808 <structfield>hw_params</structfield>. For example, releasing the
2809 buffer via
2810 <function>snd_pcm_lib_malloc_pages()</function> is done by
2811 calling the following:
2812
2813 <informalexample>
2814 <programlisting>
2815<![CDATA[
2816 snd_pcm_lib_free_pages(substream);
2817]]>
2818 </programlisting>
2819 </informalexample>
2820 </para>
2821
2822 <para>
2823 This function is always called before the close callback is called.
2824 Also, the callback may be called multiple times, too.
2825 Keep track whether the resource was already released.
2826 </para>
2827 </section>
2828
2829 <section id="pcm-interface-operators-prepare-callback">
2830 <title>prepare callback</title>
2831 <para>
2832 <informalexample>
2833 <programlisting>
2834<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002835 static int snd_xxx_prepare(struct snd_pcm_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836]]>
2837 </programlisting>
2838 </informalexample>
2839 </para>
2840
2841 <para>
2842 This callback is called when the pcm is
2843 <quote>prepared</quote>. You can set the format type, sample
2844 rate, etc. here. The difference from
2845 <structfield>hw_params</structfield> is that the
2846 <structfield>prepare</structfield> callback will be called at each
2847 time
2848 <function>snd_pcm_prepare()</function> is called, i.e. when
2849 recovered after underruns, etc.
2850 </para>
2851
2852 <para>
2853 Note that this callback became non-atomic since the recent version.
Akinobu Mita0b280022006-03-26 01:38:58 -08002854 You can use schedule-related functions safely in this callback now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 </para>
2856
2857 <para>
2858 In this and the following callbacks, you can refer to the
2859 values via the runtime record,
2860 substream-&gt;runtime.
2861 For example, to get the current
2862 rate, format or channels, access to
2863 runtime-&gt;rate,
2864 runtime-&gt;format or
2865 runtime-&gt;channels, respectively.
2866 The physical address of the allocated buffer is set to
2867 runtime-&gt;dma_area. The buffer and period sizes are
2868 in runtime-&gt;buffer_size and runtime-&gt;period_size,
2869 respectively.
2870 </para>
2871
2872 <para>
2873 Be careful that this callback will be called many times at
2874 each set up, too.
2875 </para>
2876 </section>
2877
2878 <section id="pcm-interface-operators-trigger-callback">
2879 <title>trigger callback</title>
2880 <para>
2881 <informalexample>
2882 <programlisting>
2883<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002884 static int snd_xxx_trigger(struct snd_pcm_substream *substream, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885]]>
2886 </programlisting>
2887 </informalexample>
2888
2889 This is called when the pcm is started, stopped or paused.
2890 </para>
2891
2892 <para>
2893 Which action is specified in the second argument,
2894 <constant>SNDRV_PCM_TRIGGER_XXX</constant> in
2895 <filename>&lt;sound/pcm.h&gt;</filename>. At least,
2896 <constant>START</constant> and <constant>STOP</constant>
2897 commands must be defined in this callback.
2898
2899 <informalexample>
2900 <programlisting>
2901<![CDATA[
2902 switch (cmd) {
2903 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002904 /* do something to start the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai95a5b082007-07-26 16:50:09 +02002907 /* do something to stop the PCM engine */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 break;
2909 default:
2910 return -EINVAL;
2911 }
2912]]>
2913 </programlisting>
2914 </informalexample>
2915 </para>
2916
2917 <para>
2918 When the pcm supports the pause operation (given in info
2919 field of the hardware table), <constant>PAUSE_PUSE</constant>
2920 and <constant>PAUSE_RELEASE</constant> commands must be
2921 handled here, too. The former is the command to pause the pcm,
2922 and the latter to restart the pcm again.
2923 </para>
2924
2925 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002926 When the pcm supports the suspend/resume operation,
2927 regardless of full or partial suspend/resume support,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 <constant>SUSPEND</constant> and <constant>RESUME</constant>
2929 commands must be handled, too.
2930 These commands are issued when the power-management status is
2931 changed. Obviously, the <constant>SUSPEND</constant> and
2932 <constant>RESUME</constant>
2933 do suspend and resume of the pcm substream, and usually, they
2934 are identical with <constant>STOP</constant> and
2935 <constant>START</constant> commands, respectively.
Takashi Iwai5fe76e42005-11-17 17:26:09 +01002936 See <link linkend="power-management"><citetitle>
2937 Power Management</citetitle></link> section for details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 </para>
2939
2940 <para>
2941 As mentioned, this callback is atomic. You cannot call
2942 the function going to sleep.
2943 The trigger callback should be as minimal as possible,
2944 just really triggering the DMA. The other stuff should be
2945 initialized hw_params and prepare callbacks properly
2946 beforehand.
2947 </para>
2948 </section>
2949
2950 <section id="pcm-interface-operators-pointer-callback">
2951 <title>pointer callback</title>
2952 <para>
2953 <informalexample>
2954 <programlisting>
2955<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01002956 static snd_pcm_uframes_t snd_xxx_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957]]>
2958 </programlisting>
2959 </informalexample>
2960
2961 This callback is called when the PCM middle layer inquires
2962 the current hardware position on the buffer. The position must
2963 be returned in frames (which was in bytes on ALSA 0.5.x),
2964 ranged from 0 to buffer_size - 1.
2965 </para>
2966
2967 <para>
2968 This is called usually from the buffer-update routine in the
2969 pcm middle layer, which is invoked when
2970 <function>snd_pcm_period_elapsed()</function> is called in the
2971 interrupt routine. Then the pcm middle layer updates the
2972 position and calculates the available space, and wakes up the
2973 sleeping poll threads, etc.
2974 </para>
2975
2976 <para>
2977 This callback is also atomic.
2978 </para>
2979 </section>
2980
2981 <section id="pcm-interface-operators-copy-silence">
2982 <title>copy and silence callbacks</title>
2983 <para>
2984 These callbacks are not mandatory, and can be omitted in
2985 most cases. These callbacks are used when the hardware buffer
2986 cannot be on the normal memory space. Some chips have their
2987 own buffer on the hardware which is not mappable. In such a
2988 case, you have to transfer the data manually from the memory
2989 buffer to the hardware buffer. Or, if the buffer is
2990 non-contiguous on both physical and virtual memory spaces,
2991 these callbacks must be defined, too.
2992 </para>
2993
2994 <para>
2995 If these two callbacks are defined, copy and set-silence
2996 operations are done by them. The detailed will be described in
2997 the later section <link
2998 linkend="buffer-and-memory"><citetitle>Buffer and Memory
2999 Management</citetitle></link>.
3000 </para>
3001 </section>
3002
3003 <section id="pcm-interface-operators-ack">
3004 <title>ack callback</title>
3005 <para>
3006 This callback is also not mandatory. This callback is called
3007 when the appl_ptr is updated in read or write operations.
3008 Some drivers like emu10k1-fx and cs46xx need to track the
3009 current appl_ptr for the internal buffer, and this callback
3010 is useful only for such a purpose.
3011 </para>
3012 <para>
3013 This callback is atomic.
3014 </para>
3015 </section>
3016
3017 <section id="pcm-interface-operators-page-callback">
3018 <title>page callback</title>
3019
3020 <para>
3021 This callback is also not mandatory. This callback is used
3022 mainly for the non-contiguous buffer. The mmap calls this
3023 callback to get the page address. Some examples will be
3024 explained in the later section <link
3025 linkend="buffer-and-memory"><citetitle>Buffer and Memory
3026 Management</citetitle></link>, too.
3027 </para>
3028 </section>
3029 </section>
3030
3031 <section id="pcm-interface-interrupt-handler">
3032 <title>Interrupt Handler</title>
3033 <para>
3034 The rest of pcm stuff is the PCM interrupt handler. The
3035 role of PCM interrupt handler in the sound driver is to update
3036 the buffer position and to tell the PCM middle layer when the
3037 buffer position goes across the prescribed period size. To
3038 inform this, call <function>snd_pcm_period_elapsed()</function>
3039 function.
3040 </para>
3041
3042 <para>
3043 There are several types of sound chips to generate the interrupts.
3044 </para>
3045
3046 <section id="pcm-interface-interrupt-handler-boundary">
3047 <title>Interrupts at the period (fragment) boundary</title>
3048 <para>
3049 This is the most frequently found type: the hardware
3050 generates an interrupt at each period boundary.
3051 In this case, you can call
3052 <function>snd_pcm_period_elapsed()</function> at each
3053 interrupt.
3054 </para>
3055
3056 <para>
3057 <function>snd_pcm_period_elapsed()</function> takes the
3058 substream pointer as its argument. Thus, you need to keep the
3059 substream pointer accessible from the chip instance. For
3060 example, define substream field in the chip record to hold the
3061 current running substream pointer, and set the pointer value
3062 at open callback (and reset at close callback).
3063 </para>
3064
3065 <para>
Adrian Bunk04187262006-06-30 18:23:04 +02003066 If you acquire a spinlock in the interrupt handler, and the
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 lock is used in other pcm callbacks, too, then you have to
3068 release the lock before calling
3069 <function>snd_pcm_period_elapsed()</function>, because
3070 <function>snd_pcm_period_elapsed()</function> calls other pcm
3071 callbacks inside.
3072 </para>
3073
3074 <para>
3075 A typical coding would be like:
3076
3077 <example>
3078 <title>Interrupt Handler Case #1</title>
3079 <programlisting>
3080<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003081 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003083 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 spin_lock(&chip->lock);
3085 ....
3086 if (pcm_irq_invoked(chip)) {
3087 /* call updater, unlock before it */
3088 spin_unlock(&chip->lock);
3089 snd_pcm_period_elapsed(chip->substream);
3090 spin_lock(&chip->lock);
Takashi Iwai95a5b082007-07-26 16:50:09 +02003091 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
3093 ....
3094 spin_unlock(&chip->lock);
3095 return IRQ_HANDLED;
3096 }
3097]]>
3098 </programlisting>
3099 </example>
3100 </para>
3101 </section>
3102
3103 <section id="pcm-interface-interrupt-handler-timer">
3104 <title>High-frequent timer interrupts</title>
3105 <para>
3106 This is the case when the hardware doesn't generate interrupts
3107 at the period boundary but do timer-interrupts at the fixed
3108 timer rate (e.g. es1968 or ymfpci drivers).
3109 In this case, you need to check the current hardware
3110 position and accumulates the processed sample length at each
3111 interrupt. When the accumulated size overcomes the period
3112 size, call
3113 <function>snd_pcm_period_elapsed()</function> and reset the
3114 accumulator.
3115 </para>
3116
3117 <para>
3118 A typical coding would be like the following.
3119
3120 <example>
3121 <title>Interrupt Handler Case #2</title>
3122 <programlisting>
3123<![CDATA[
Takashi Iwaiad4d1de2007-01-16 17:46:35 +01003124 static irqreturn_t snd_mychip_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003126 struct mychip *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 spin_lock(&chip->lock);
3128 ....
3129 if (pcm_irq_invoked(chip)) {
3130 unsigned int last_ptr, size;
3131 /* get the current hardware pointer (in frames) */
3132 last_ptr = get_hw_ptr(chip);
3133 /* calculate the processed frames since the
3134 * last update
3135 */
3136 if (last_ptr < chip->last_ptr)
3137 size = runtime->buffer_size + last_ptr
3138 - chip->last_ptr;
3139 else
3140 size = last_ptr - chip->last_ptr;
3141 /* remember the last updated point */
3142 chip->last_ptr = last_ptr;
3143 /* accumulate the size */
3144 chip->size += size;
3145 /* over the period boundary? */
3146 if (chip->size >= runtime->period_size) {
3147 /* reset the accumulator */
3148 chip->size %= runtime->period_size;
3149 /* call updater */
3150 spin_unlock(&chip->lock);
3151 snd_pcm_period_elapsed(substream);
3152 spin_lock(&chip->lock);
3153 }
Takashi Iwai95a5b082007-07-26 16:50:09 +02003154 /* acknowledge the interrupt if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 ....
3157 spin_unlock(&chip->lock);
3158 return IRQ_HANDLED;
3159 }
3160]]>
3161 </programlisting>
3162 </example>
3163 </para>
3164 </section>
3165
3166 <section id="pcm-interface-interrupt-handler-both">
3167 <title>On calling <function>snd_pcm_period_elapsed()</function></title>
3168 <para>
3169 In both cases, even if more than one period are elapsed, you
3170 don't have to call
3171 <function>snd_pcm_period_elapsed()</function> many times. Call
3172 only once. And the pcm layer will check the current hardware
3173 pointer and update to the latest status.
3174 </para>
3175 </section>
3176 </section>
3177
3178 <section id="pcm-interface-atomicity">
3179 <title>Atomicity</title>
3180 <para>
3181 One of the most important (and thus difficult to debug) problem
3182 on the kernel programming is the race condition.
3183 On linux kernel, usually it's solved via spin-locks or
3184 semaphores. In general, if the race condition may
3185 happen in the interrupt handler, it's handled as atomic, and you
3186 have to use spinlock for protecting the critical session. If it
3187 never happens in the interrupt and it may take relatively long
3188 time, you should use semaphore.
3189 </para>
3190
3191 <para>
3192 As already seen, some pcm callbacks are atomic and some are
3193 not. For example, <parameter>hw_params</parameter> callback is
3194 non-atomic, while <parameter>trigger</parameter> callback is
3195 atomic. This means, the latter is called already in a spinlock
3196 held by the PCM middle layer. Please take this atomicity into
3197 account when you use a spinlock or a semaphore in the callbacks.
3198 </para>
3199
3200 <para>
3201 In the atomic callbacks, you cannot use functions which may call
3202 <function>schedule</function> or go to
3203 <function>sleep</function>. The semaphore and mutex do sleep,
3204 and hence they cannot be used inside the atomic callbacks
3205 (e.g. <parameter>trigger</parameter> callback).
3206 For taking a certain delay in such a callback, please use
3207 <function>udelay()</function> or <function>mdelay()</function>.
3208 </para>
3209
3210 <para>
3211 All three atomic callbacks (trigger, pointer, and ack) are
3212 called with local interrupts disabled.
3213 </para>
3214
3215 </section>
3216 <section id="pcm-interface-constraints">
3217 <title>Constraints</title>
3218 <para>
3219 If your chip supports unconventional sample rates, or only the
3220 limited samples, you need to set a constraint for the
3221 condition.
3222 </para>
3223
3224 <para>
3225 For example, in order to restrict the sample rates in the some
3226 supported values, use
3227 <function>snd_pcm_hw_constraint_list()</function>.
3228 You need to call this function in the open callback.
3229
3230 <example>
3231 <title>Example of Hardware Constraints</title>
3232 <programlisting>
3233<![CDATA[
3234 static unsigned int rates[] =
3235 {4000, 10000, 22050, 44100};
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003236 static struct snd_pcm_hw_constraint_list constraints_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 .count = ARRAY_SIZE(rates),
3238 .list = rates,
3239 .mask = 0,
3240 };
3241
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003242 static int snd_mychip_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 {
3244 int err;
3245 ....
3246 err = snd_pcm_hw_constraint_list(substream->runtime, 0,
3247 SNDRV_PCM_HW_PARAM_RATE,
3248 &constraints_rates);
3249 if (err < 0)
3250 return err;
3251 ....
3252 }
3253]]>
3254 </programlisting>
3255 </example>
3256 </para>
3257
3258 <para>
3259 There are many different constraints.
3260 Look in <filename>sound/pcm.h</filename> for a complete list.
3261 You can even define your own constraint rules.
3262 For example, let's suppose my_chip can manage a substream of 1 channel
3263 if and only if the format is S16_LE, otherwise it supports any format
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003264 specified in the <structname>snd_pcm_hardware</structname> structure (or in any
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 other constraint_list). You can build a rule like this:
3266
3267 <example>
3268 <title>Example of Hardware Constraints for Channels</title>
3269 <programlisting>
3270<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003271 static int hw_rule_format_by_channels(struct snd_pcm_hw_params *params,
3272 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003274 struct snd_interval *c = hw_param_interval(params,
3275 SNDRV_PCM_HW_PARAM_CHANNELS);
3276 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3277 struct snd_mask fmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 snd_mask_any(&fmt); /* Init the struct */
3280 if (c->min < 2) {
3281 fmt.bits[0] &= SNDRV_PCM_FMTBIT_S16_LE;
3282 return snd_mask_refine(f, &fmt);
3283 }
3284 return 0;
3285 }
3286]]>
3287 </programlisting>
3288 </example>
3289 </para>
3290
3291 <para>
3292 Then you need to call this function to add your rule:
3293
3294 <informalexample>
3295 <programlisting>
3296<![CDATA[
3297 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3298 hw_rule_channels_by_format, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3299 -1);
3300]]>
3301 </programlisting>
3302 </informalexample>
3303 </para>
3304
3305 <para>
3306 The rule function is called when an application sets the number of
3307 channels. But an application can set the format before the number of
3308 channels. Thus you also need to define the inverse rule:
3309
3310 <example>
3311 <title>Example of Hardware Constraints for Channels</title>
3312 <programlisting>
3313<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003314 static int hw_rule_channels_by_format(struct snd_pcm_hw_params *params,
3315 struct snd_pcm_hw_rule *rule)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003317 struct snd_interval *c = hw_param_interval(params,
3318 SNDRV_PCM_HW_PARAM_CHANNELS);
3319 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
3320 struct snd_interval ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 snd_interval_any(&ch);
3323 if (f->bits[0] == SNDRV_PCM_FMTBIT_S16_LE) {
3324 ch.min = ch.max = 1;
3325 ch.integer = 1;
3326 return snd_interval_refine(c, &ch);
3327 }
3328 return 0;
3329 }
3330]]>
3331 </programlisting>
3332 </example>
3333 </para>
3334
3335 <para>
3336 ...and in the open callback:
3337 <informalexample>
3338 <programlisting>
3339<![CDATA[
3340 snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT,
3341 hw_rule_format_by_channels, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
3342 -1);
3343]]>
3344 </programlisting>
3345 </informalexample>
3346 </para>
3347
3348 <para>
3349 I won't explain more details here, rather I
3350 would like to say, <quote>Luke, use the source.</quote>
3351 </para>
3352 </section>
3353
3354 </chapter>
3355
3356
3357<!-- ****************************************************** -->
3358<!-- Control Interface -->
3359<!-- ****************************************************** -->
3360 <chapter id="control-interface">
3361 <title>Control Interface</title>
3362
3363 <section id="control-interface-general">
3364 <title>General</title>
3365 <para>
3366 The control interface is used widely for many switches,
3367 sliders, etc. which are accessed from the user-space. Its most
3368 important use is the mixer interface. In other words, on ALSA
3369 0.9.x, all the mixer stuff is implemented on the control kernel
3370 API (while there was an independent mixer kernel API on 0.5.x).
3371 </para>
3372
3373 <para>
3374 ALSA has a well-defined AC97 control module. If your chip
3375 supports only the AC97 and nothing else, you can skip this
3376 section.
3377 </para>
3378
3379 <para>
3380 The control API is defined in
3381 <filename>&lt;sound/control.h&gt;</filename>.
3382 Include this file if you add your own controls.
3383 </para>
3384 </section>
3385
3386 <section id="control-interface-definition">
3387 <title>Definition of Controls</title>
3388 <para>
3389 For creating a new control, you need to define the three
3390 callbacks: <structfield>info</structfield>,
3391 <structfield>get</structfield> and
3392 <structfield>put</structfield>. Then, define a
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003393 struct <structname>snd_kcontrol_new</structname> record, such as:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
3395 <example>
3396 <title>Definition of a Control</title>
3397 <programlisting>
3398<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003399 static struct snd_kcontrol_new my_control __devinitdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3401 .name = "PCM Playback Switch",
3402 .index = 0,
3403 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003404 .private_value = 0xffff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 .info = my_control_info,
3406 .get = my_control_get,
3407 .put = my_control_put
3408 };
3409]]>
3410 </programlisting>
3411 </example>
3412 </para>
3413
3414 <para>
3415 Most likely the control is created via
3416 <function>snd_ctl_new1()</function>, and in such a case, you can
3417 add <parameter>__devinitdata</parameter> prefix to the
3418 definition like above.
3419 </para>
3420
3421 <para>
3422 The <structfield>iface</structfield> field specifies the type of
Clemens Ladisch67ed4162005-07-29 15:32:58 +02003423 the control, <constant>SNDRV_CTL_ELEM_IFACE_XXX</constant>, which
3424 is usually <constant>MIXER</constant>.
3425 Use <constant>CARD</constant> for global controls that are not
3426 logically part of the mixer.
3427 If the control is closely associated with some specific device on
3428 the sound card, use <constant>HWDEP</constant>,
3429 <constant>PCM</constant>, <constant>RAWMIDI</constant>,
3430 <constant>TIMER</constant>, or <constant>SEQUENCER</constant>, and
3431 specify the device number with the
3432 <structfield>device</structfield> and
3433 <structfield>subdevice</structfield> fields.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 </para>
3435
3436 <para>
3437 The <structfield>name</structfield> is the name identifier
3438 string. On ALSA 0.9.x, the control name is very important,
3439 because its role is classified from its name. There are
3440 pre-defined standard control names. The details are described in
3441 the subsection
3442 <link linkend="control-interface-control-names"><citetitle>
3443 Control Names</citetitle></link>.
3444 </para>
3445
3446 <para>
3447 The <structfield>index</structfield> field holds the index number
3448 of this control. If there are several different controls with
3449 the same name, they can be distinguished by the index
3450 number. This is the case when
3451 several codecs exist on the card. If the index is zero, you can
3452 omit the definition above.
3453 </para>
3454
3455 <para>
3456 The <structfield>access</structfield> field contains the access
3457 type of this control. Give the combination of bit masks,
3458 <constant>SNDRV_CTL_ELEM_ACCESS_XXX</constant>, there.
3459 The detailed will be explained in the subsection
3460 <link linkend="control-interface-access-flags"><citetitle>
3461 Access Flags</citetitle></link>.
3462 </para>
3463
3464 <para>
Takashi Iwai0b7bed42006-03-02 15:35:55 +01003465 The <structfield>private_value</structfield> field contains
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 an arbitrary long integer value for this record. When using
3467 generic <structfield>info</structfield>,
3468 <structfield>get</structfield> and
3469 <structfield>put</structfield> callbacks, you can pass a value
3470 through this field. If several small numbers are necessary, you can
3471 combine them in bitwise. Or, it's possible to give a pointer
3472 (casted to unsigned long) of some record to this field, too.
3473 </para>
3474
3475 <para>
3476 The other three are
3477 <link linkend="control-interface-callbacks"><citetitle>
3478 callback functions</citetitle></link>.
3479 </para>
3480 </section>
3481
3482 <section id="control-interface-control-names">
3483 <title>Control Names</title>
3484 <para>
3485 There are some standards for defining the control names. A
3486 control is usually defined from the three parts as
3487 <quote>SOURCE DIRECTION FUNCTION</quote>.
3488 </para>
3489
3490 <para>
3491 The first, <constant>SOURCE</constant>, specifies the source
3492 of the control, and is a string such as <quote>Master</quote>,
3493 <quote>PCM</quote>, <quote>CD</quote> or
3494 <quote>Line</quote>. There are many pre-defined sources.
3495 </para>
3496
3497 <para>
3498 The second, <constant>DIRECTION</constant>, is one of the
3499 following strings according to the direction of the control:
3500 <quote>Playback</quote>, <quote>Capture</quote>, <quote>Bypass
3501 Playback</quote> and <quote>Bypass Capture</quote>. Or, it can
3502 be omitted, meaning both playback and capture directions.
3503 </para>
3504
3505 <para>
3506 The third, <constant>FUNCTION</constant>, is one of the
3507 following strings according to the function of the control:
3508 <quote>Switch</quote>, <quote>Volume</quote> and
3509 <quote>Route</quote>.
3510 </para>
3511
3512 <para>
3513 The example of control names are, thus, <quote>Master Capture
3514 Switch</quote> or <quote>PCM Playback Volume</quote>.
3515 </para>
3516
3517 <para>
3518 There are some exceptions:
3519 </para>
3520
3521 <section id="control-interface-control-names-global">
3522 <title>Global capture and playback</title>
3523 <para>
3524 <quote>Capture Source</quote>, <quote>Capture Switch</quote>
3525 and <quote>Capture Volume</quote> are used for the global
3526 capture (input) source, switch and volume. Similarly,
3527 <quote>Playback Switch</quote> and <quote>Playback
3528 Volume</quote> are used for the global output gain switch and
3529 volume.
3530 </para>
3531 </section>
3532
3533 <section id="control-interface-control-names-tone">
3534 <title>Tone-controls</title>
3535 <para>
3536 tone-control switch and volumes are specified like
3537 <quote>Tone Control - XXX</quote>, e.g. <quote>Tone Control -
3538 Switch</quote>, <quote>Tone Control - Bass</quote>,
3539 <quote>Tone Control - Center</quote>.
3540 </para>
3541 </section>
3542
3543 <section id="control-interface-control-names-3d">
3544 <title>3D controls</title>
3545 <para>
3546 3D-control switches and volumes are specified like <quote>3D
3547 Control - XXX</quote>, e.g. <quote>3D Control -
3548 Switch</quote>, <quote>3D Control - Center</quote>, <quote>3D
3549 Control - Space</quote>.
3550 </para>
3551 </section>
3552
3553 <section id="control-interface-control-names-mic">
3554 <title>Mic boost</title>
3555 <para>
3556 Mic-boost switch is set as <quote>Mic Boost</quote> or
3557 <quote>Mic Boost (6dB)</quote>.
3558 </para>
3559
3560 <para>
3561 More precise information can be found in
3562 <filename>Documentation/sound/alsa/ControlNames.txt</filename>.
3563 </para>
3564 </section>
3565 </section>
3566
3567 <section id="control-interface-access-flags">
3568 <title>Access Flags</title>
3569
3570 <para>
3571 The access flag is the bit-flags which specifies the access type
3572 of the given control. The default access type is
3573 <constant>SNDRV_CTL_ELEM_ACCESS_READWRITE</constant>,
3574 which means both read and write are allowed to this control.
3575 When the access flag is omitted (i.e. = 0), it is
3576 regarded as <constant>READWRITE</constant> access as default.
3577 </para>
3578
3579 <para>
3580 When the control is read-only, pass
3581 <constant>SNDRV_CTL_ELEM_ACCESS_READ</constant> instead.
3582 In this case, you don't have to define
3583 <structfield>put</structfield> callback.
3584 Similarly, when the control is write-only (although it's a rare
3585 case), you can use <constant>WRITE</constant> flag instead, and
3586 you don't need <structfield>get</structfield> callback.
3587 </para>
3588
3589 <para>
3590 If the control value changes frequently (e.g. the VU meter),
3591 <constant>VOLATILE</constant> flag should be given. This means
3592 that the control may be changed without
3593 <link linkend="control-interface-change-notification"><citetitle>
3594 notification</citetitle></link>. Applications should poll such
3595 a control constantly.
3596 </para>
3597
3598 <para>
3599 When the control is inactive, set
3600 <constant>INACTIVE</constant> flag, too.
3601 There are <constant>LOCK</constant> and
3602 <constant>OWNER</constant> flags for changing the write
3603 permissions.
3604 </para>
3605
3606 </section>
3607
3608 <section id="control-interface-callbacks">
3609 <title>Callbacks</title>
3610
3611 <section id="control-interface-callbacks-info">
3612 <title>info callback</title>
3613 <para>
3614 The <structfield>info</structfield> callback is used to get
3615 the detailed information of this control. This must store the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003616 values of the given struct <structname>snd_ctl_elem_info</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 object. For example, for a boolean control with a single
3618 element will be:
3619
3620 <example>
3621 <title>Example of info callback</title>
3622 <programlisting>
3623<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003624 static int snd_myctl_mono_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003625 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 {
3627 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
3628 uinfo->count = 1;
3629 uinfo->value.integer.min = 0;
3630 uinfo->value.integer.max = 1;
3631 return 0;
3632 }
3633]]>
3634 </programlisting>
3635 </example>
3636 </para>
3637
3638 <para>
3639 The <structfield>type</structfield> field specifies the type
3640 of the control. There are <constant>BOOLEAN</constant>,
3641 <constant>INTEGER</constant>, <constant>ENUMERATED</constant>,
3642 <constant>BYTES</constant>, <constant>IEC958</constant> and
3643 <constant>INTEGER64</constant>. The
3644 <structfield>count</structfield> field specifies the
3645 number of elements in this control. For example, a stereo
3646 volume would have count = 2. The
3647 <structfield>value</structfield> field is a union, and
3648 the values stored are depending on the type. The boolean and
3649 integer are identical.
3650 </para>
3651
3652 <para>
3653 The enumerated type is a bit different from others. You'll
3654 need to set the string for the currently given item index.
3655
3656 <informalexample>
3657 <programlisting>
3658<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003659 static int snd_myctl_enum_info(struct snd_kcontrol *kcontrol,
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003660 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 {
3662 static char *texts[4] = {
3663 "First", "Second", "Third", "Fourth"
3664 };
3665 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3666 uinfo->count = 1;
3667 uinfo->value.enumerated.items = 4;
3668 if (uinfo->value.enumerated.item > 3)
3669 uinfo->value.enumerated.item = 3;
3670 strcpy(uinfo->value.enumerated.name,
3671 texts[uinfo->value.enumerated.item]);
3672 return 0;
3673 }
3674]]>
3675 </programlisting>
3676 </informalexample>
3677 </para>
Takashi Iwai95a5b082007-07-26 16:50:09 +02003678
3679 <para>
3680 Some common info callbacks are prepared for easy use:
3681 <function>snd_ctl_boolean_mono_info()</function> and
3682 <function>snd_ctl_boolean_stereo_info()</function>.
3683 Obviously, the former is an info callback for a mono channel
3684 boolean item, just like <function>snd_myctl_mono_info</function>
3685 above, and the latter is for a stereo channel boolean item.
3686 </para>
3687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 </section>
3689
3690 <section id="control-interface-callbacks-get">
3691 <title>get callback</title>
3692
3693 <para>
3694 This callback is used to read the current value of the
3695 control and to return to the user-space.
3696 </para>
3697
3698 <para>
3699 For example,
3700
3701 <example>
3702 <title>Example of get callback</title>
3703 <programlisting>
3704<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003705 static int snd_myctl_get(struct snd_kcontrol *kcontrol,
3706 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003708 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 ucontrol->value.integer.value[0] = get_some_value(chip);
3710 return 0;
3711 }
3712]]>
3713 </programlisting>
3714 </example>
3715 </para>
3716
3717 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 The <structfield>value</structfield> field is depending on
3719 the type of control as well as on info callback. For example,
3720 the sb driver uses this field to store the register offset,
3721 the bit-shift and the bit-mask. The
3722 <structfield>private_value</structfield> is set like
3723 <informalexample>
3724 <programlisting>
3725<![CDATA[
3726 .private_value = reg | (shift << 16) | (mask << 24)
3727]]>
3728 </programlisting>
3729 </informalexample>
3730 and is retrieved in callbacks like
3731 <informalexample>
3732 <programlisting>
3733<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003734 static int snd_sbmixer_get_single(struct snd_kcontrol *kcontrol,
3735 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 {
3737 int reg = kcontrol->private_value & 0xff;
3738 int shift = (kcontrol->private_value >> 16) & 0xff;
3739 int mask = (kcontrol->private_value >> 24) & 0xff;
3740 ....
3741 }
3742]]>
3743 </programlisting>
3744 </informalexample>
3745 </para>
3746
3747 <para>
3748 In <structfield>get</structfield> callback, you have to fill all the elements if the
3749 control has more than one elements,
3750 i.e. <structfield>count</structfield> &gt; 1.
3751 In the example above, we filled only one element
3752 (<structfield>value.integer.value[0]</structfield>) since it's
3753 assumed as <structfield>count</structfield> = 1.
3754 </para>
3755 </section>
3756
3757 <section id="control-interface-callbacks-put">
3758 <title>put callback</title>
3759
3760 <para>
3761 This callback is used to write a value from the user-space.
3762 </para>
3763
3764 <para>
3765 For example,
3766
3767 <example>
3768 <title>Example of put callback</title>
3769 <programlisting>
3770<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003771 static int snd_myctl_put(struct snd_kcontrol *kcontrol,
3772 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003774 struct mychip *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 int changed = 0;
3776 if (chip->current_value !=
3777 ucontrol->value.integer.value[0]) {
3778 change_current_value(chip,
3779 ucontrol->value.integer.value[0]);
3780 changed = 1;
3781 }
3782 return changed;
3783 }
3784]]>
3785 </programlisting>
3786 </example>
3787
3788 As seen above, you have to return 1 if the value is
3789 changed. If the value is not changed, return 0 instead.
3790 If any fatal error happens, return a negative error code as
3791 usual.
3792 </para>
3793
3794 <para>
3795 Like <structfield>get</structfield> callback,
3796 when the control has more than one elements,
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01003797 all elements must be evaluated in this callback, too.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 </para>
3799 </section>
3800
3801 <section id="control-interface-callbacks-all">
3802 <title>Callbacks are not atomic</title>
3803 <para>
3804 All these three callbacks are basically not atomic.
3805 </para>
3806 </section>
3807 </section>
3808
3809 <section id="control-interface-constructor">
3810 <title>Constructor</title>
3811 <para>
3812 When everything is ready, finally we can create a new
3813 control. For creating a control, there are two functions to be
3814 called, <function>snd_ctl_new1()</function> and
3815 <function>snd_ctl_add()</function>.
3816 </para>
3817
3818 <para>
3819 In the simplest way, you can do like this:
3820
3821 <informalexample>
3822 <programlisting>
3823<![CDATA[
Takashi Iwai95a5b082007-07-26 16:50:09 +02003824 err = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
3825 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 return err;
3827]]>
3828 </programlisting>
3829 </informalexample>
3830
3831 where <parameter>my_control</parameter> is the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003832 struct <structname>snd_kcontrol_new</structname> object defined above, and chip
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 is the object pointer to be passed to
3834 kcontrol-&gt;private_data
3835 which can be referred in callbacks.
3836 </para>
3837
3838 <para>
3839 <function>snd_ctl_new1()</function> allocates a new
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003840 <structname>snd_kcontrol</structname> instance (that's why the definition
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 of <parameter>my_control</parameter> can be with
3842 <parameter>__devinitdata</parameter>
3843 prefix), and <function>snd_ctl_add</function> assigns the given
3844 control component to the card.
3845 </para>
3846 </section>
3847
3848 <section id="control-interface-change-notification">
3849 <title>Change Notification</title>
3850 <para>
3851 If you need to change and update a control in the interrupt
3852 routine, you can call <function>snd_ctl_notify()</function>. For
3853 example,
3854
3855 <informalexample>
3856 <programlisting>
3857<![CDATA[
3858 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE, id_pointer);
3859]]>
3860 </programlisting>
3861 </informalexample>
3862
3863 This function takes the card pointer, the event-mask, and the
3864 control id pointer for the notification. The event-mask
3865 specifies the types of notification, for example, in the above
3866 example, the change of control values is notified.
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003867 The id pointer is the pointer of struct <structname>snd_ctl_elem_id</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 to be notified.
3869 You can find some examples in <filename>es1938.c</filename> or
3870 <filename>es1968.c</filename> for hardware volume interrupts.
3871 </para>
3872 </section>
3873
3874 </chapter>
3875
3876
3877<!-- ****************************************************** -->
3878<!-- API for AC97 Codec -->
3879<!-- ****************************************************** -->
3880 <chapter id="api-ac97">
3881 <title>API for AC97 Codec</title>
3882
3883 <section>
3884 <title>General</title>
3885 <para>
3886 The ALSA AC97 codec layer is a well-defined one, and you don't
3887 have to write many codes to control it. Only low-level control
3888 routines are necessary. The AC97 codec API is defined in
3889 <filename>&lt;sound/ac97_codec.h&gt;</filename>.
3890 </para>
3891 </section>
3892
3893 <section id="api-ac97-example">
3894 <title>Full Code Example</title>
3895 <para>
3896 <example>
3897 <title>Example of AC97 Interface</title>
3898 <programlisting>
3899<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003900 struct mychip {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003902 struct snd_ac97 *ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 ....
3904 };
3905
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003906 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 unsigned short reg)
3908 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003909 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003911 /* read a register value here from the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return the_register_value;
3913 }
3914
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003915 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 unsigned short reg, unsigned short val)
3917 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003918 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 ....
Takashi Iwai95a5b082007-07-26 16:50:09 +02003920 /* write the given register value to the codec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
3922
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003923 static int snd_mychip_ac97(struct mychip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003925 struct snd_ac97_bus *bus;
3926 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int err;
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003928 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 .write = snd_mychip_ac97_write,
3930 .read = snd_mychip_ac97_read,
3931 };
3932
Takashi Iwai95a5b082007-07-26 16:50:09 +02003933 err = snd_ac97_bus(chip->card, 0, &ops, NULL, &bus);
3934 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return err;
3936 memset(&ac97, 0, sizeof(ac97));
3937 ac97.private_data = chip;
3938 return snd_ac97_mixer(bus, &ac97, &chip->ac97);
3939 }
3940
3941]]>
3942 </programlisting>
3943 </example>
3944 </para>
3945 </section>
3946
3947 <section id="api-ac97-constructor">
3948 <title>Constructor</title>
3949 <para>
3950 For creating an ac97 instance, first call <function>snd_ac97_bus</function>
3951 with an <type>ac97_bus_ops_t</type> record with callback functions.
3952
3953 <informalexample>
3954 <programlisting>
3955<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003956 struct snd_ac97_bus *bus;
3957 static struct snd_ac97_bus_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 .write = snd_mychip_ac97_write,
3959 .read = snd_mychip_ac97_read,
3960 };
3961
3962 snd_ac97_bus(card, 0, &ops, NULL, &pbus);
3963]]>
3964 </programlisting>
3965 </informalexample>
3966
3967 The bus record is shared among all belonging ac97 instances.
3968 </para>
3969
3970 <para>
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003971 And then call <function>snd_ac97_mixer()</function> with an
3972 struct <structname>snd_ac97_template</structname>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 record together with the bus pointer created above.
3974
3975 <informalexample>
3976 <programlisting>
3977<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01003978 struct snd_ac97_template ac97;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 int err;
3980
3981 memset(&ac97, 0, sizeof(ac97));
3982 ac97.private_data = chip;
3983 snd_ac97_mixer(bus, &ac97, &chip->ac97);
3984]]>
3985 </programlisting>
3986 </informalexample>
3987
3988 where chip-&gt;ac97 is the pointer of a newly created
3989 <type>ac97_t</type> instance.
3990 In this case, the chip pointer is set as the private data, so that
3991 the read/write callback functions can refer to this chip instance.
3992 This instance is not necessarily stored in the chip
3993 record. When you need to change the register values from the
3994 driver, or need the suspend/resume of ac97 codecs, keep this
3995 pointer to pass to the corresponding functions.
3996 </para>
3997 </section>
3998
3999 <section id="api-ac97-callbacks">
4000 <title>Callbacks</title>
4001 <para>
4002 The standard callbacks are <structfield>read</structfield> and
4003 <structfield>write</structfield>. Obviously they
4004 correspond to the functions for read and write accesses to the
4005 hardware low-level codes.
4006 </para>
4007
4008 <para>
4009 The <structfield>read</structfield> callback returns the
4010 register value specified in the argument.
4011
4012 <informalexample>
4013 <programlisting>
4014<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004015 static unsigned short snd_mychip_ac97_read(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 unsigned short reg)
4017 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004018 struct mychip *chip = ac97->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 ....
4020 return the_register_value;
4021 }
4022]]>
4023 </programlisting>
4024 </informalexample>
4025
4026 Here, the chip can be cast from ac97-&gt;private_data.
4027 </para>
4028
4029 <para>
4030 Meanwhile, the <structfield>write</structfield> callback is
4031 used to set the register value.
4032
4033 <informalexample>
4034 <programlisting>
4035<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004036 static void snd_mychip_ac97_write(struct snd_ac97 *ac97,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 unsigned short reg, unsigned short val)
4038]]>
4039 </programlisting>
4040 </informalexample>
4041 </para>
4042
4043 <para>
4044 These callbacks are non-atomic like the callbacks of control API.
4045 </para>
4046
4047 <para>
4048 There are also other callbacks:
4049 <structfield>reset</structfield>,
4050 <structfield>wait</structfield> and
4051 <structfield>init</structfield>.
4052 </para>
4053
4054 <para>
4055 The <structfield>reset</structfield> callback is used to reset
4056 the codec. If the chip requires a special way of reset, you can
4057 define this callback.
4058 </para>
4059
4060 <para>
4061 The <structfield>wait</structfield> callback is used for a
4062 certain wait at the standard initialization of the codec. If the
4063 chip requires the extra wait-time, define this callback.
4064 </para>
4065
4066 <para>
4067 The <structfield>init</structfield> callback is used for
4068 additional initialization of the codec.
4069 </para>
4070 </section>
4071
4072 <section id="api-ac97-updating-registers">
4073 <title>Updating Registers in The Driver</title>
4074 <para>
4075 If you need to access to the codec from the driver, you can
4076 call the following functions:
4077 <function>snd_ac97_write()</function>,
4078 <function>snd_ac97_read()</function>,
4079 <function>snd_ac97_update()</function> and
4080 <function>snd_ac97_update_bits()</function>.
4081 </para>
4082
4083 <para>
4084 Both <function>snd_ac97_write()</function> and
4085 <function>snd_ac97_update()</function> functions are used to
4086 set a value to the given register
4087 (<constant>AC97_XXX</constant>). The difference between them is
4088 that <function>snd_ac97_update()</function> doesn't write a
4089 value if the given value has been already set, while
4090 <function>snd_ac97_write()</function> always rewrites the
4091 value.
4092
4093 <informalexample>
4094 <programlisting>
4095<![CDATA[
4096 snd_ac97_write(ac97, AC97_MASTER, 0x8080);
4097 snd_ac97_update(ac97, AC97_MASTER, 0x8080);
4098]]>
4099 </programlisting>
4100 </informalexample>
4101 </para>
4102
4103 <para>
4104 <function>snd_ac97_read()</function> is used to read the value
4105 of the given register. For example,
4106
4107 <informalexample>
4108 <programlisting>
4109<![CDATA[
4110 value = snd_ac97_read(ac97, AC97_MASTER);
4111]]>
4112 </programlisting>
4113 </informalexample>
4114 </para>
4115
4116 <para>
4117 <function>snd_ac97_update_bits()</function> is used to update
4118 some bits of the given register.
4119
4120 <informalexample>
4121 <programlisting>
4122<![CDATA[
4123 snd_ac97_update_bits(ac97, reg, mask, value);
4124]]>
4125 </programlisting>
4126 </informalexample>
4127 </para>
4128
4129 <para>
4130 Also, there is a function to change the sample rate (of a
4131 certain register such as
4132 <constant>AC97_PCM_FRONT_DAC_RATE</constant>) when VRA or
4133 DRA is supported by the codec:
4134 <function>snd_ac97_set_rate()</function>.
4135
4136 <informalexample>
4137 <programlisting>
4138<![CDATA[
4139 snd_ac97_set_rate(ac97, AC97_PCM_FRONT_DAC_RATE, 44100);
4140]]>
4141 </programlisting>
4142 </informalexample>
4143 </para>
4144
4145 <para>
4146 The following registers are available for setting the rate:
4147 <constant>AC97_PCM_MIC_ADC_RATE</constant>,
4148 <constant>AC97_PCM_FRONT_DAC_RATE</constant>,
4149 <constant>AC97_PCM_LR_ADC_RATE</constant>,
4150 <constant>AC97_SPDIF</constant>. When the
4151 <constant>AC97_SPDIF</constant> is specified, the register is
4152 not really changed but the corresponding IEC958 status bits will
4153 be updated.
4154 </para>
4155 </section>
4156
4157 <section id="api-ac97-clock-adjustment">
4158 <title>Clock Adjustment</title>
4159 <para>
4160 On some chip, the clock of the codec isn't 48000 but using a
4161 PCI clock (to save a quartz!). In this case, change the field
4162 bus-&gt;clock to the corresponding
4163 value. For example, intel8x0
4164 and es1968 drivers have the auto-measurement function of the
4165 clock.
4166 </para>
4167 </section>
4168
4169 <section id="api-ac97-proc-files">
4170 <title>Proc Files</title>
4171 <para>
4172 The ALSA AC97 interface will create a proc file such as
4173 <filename>/proc/asound/card0/codec97#0/ac97#0-0</filename> and
4174 <filename>ac97#0-0+regs</filename>. You can refer to these files to
4175 see the current status and registers of the codec.
4176 </para>
4177 </section>
4178
4179 <section id="api-ac97-multiple-codecs">
4180 <title>Multiple Codecs</title>
4181 <para>
4182 When there are several codecs on the same card, you need to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004183 call <function>snd_ac97_mixer()</function> multiple times with
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 ac97.num=1 or greater. The <structfield>num</structfield> field
4185 specifies the codec
4186 number.
4187 </para>
4188
4189 <para>
4190 If you have set up multiple codecs, you need to either write
4191 different callbacks for each codec or check
4192 ac97-&gt;num in the
4193 callback routines.
4194 </para>
4195 </section>
4196
4197 </chapter>
4198
4199
4200<!-- ****************************************************** -->
4201<!-- MIDI (MPU401-UART) Interface -->
4202<!-- ****************************************************** -->
4203 <chapter id="midi-interface">
4204 <title>MIDI (MPU401-UART) Interface</title>
4205
4206 <section id="midi-interface-general">
4207 <title>General</title>
4208 <para>
4209 Many soundcards have built-in MIDI (MPU401-UART)
4210 interfaces. When the soundcard supports the standard MPU401-UART
4211 interface, most likely you can use the ALSA MPU401-UART API. The
4212 MPU401-UART API is defined in
4213 <filename>&lt;sound/mpu401.h&gt;</filename>.
4214 </para>
4215
4216 <para>
4217 Some soundchips have similar but a little bit different
4218 implementation of mpu401 stuff. For example, emu10k1 has its own
4219 mpu401 routines.
4220 </para>
4221 </section>
4222
4223 <section id="midi-interface-constructor">
4224 <title>Constructor</title>
4225 <para>
4226 For creating a rawmidi object, call
4227 <function>snd_mpu401_uart_new()</function>.
4228
4229 <informalexample>
4230 <programlisting>
4231<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004232 struct snd_rawmidi *rmidi;
Takashi Iwai302e4c22006-05-23 13:24:30 +02004233 snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401, port, info_flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 irq, irq_flags, &rmidi);
4235]]>
4236 </programlisting>
4237 </informalexample>
4238 </para>
4239
4240 <para>
4241 The first argument is the card pointer, and the second is the
4242 index of this component. You can create up to 8 rawmidi
4243 devices.
4244 </para>
4245
4246 <para>
4247 The third argument is the type of the hardware,
4248 <constant>MPU401_HW_XXX</constant>. If it's not a special one,
4249 you can use <constant>MPU401_HW_MPU401</constant>.
4250 </para>
4251
4252 <para>
4253 The 4th argument is the i/o port address. Many
4254 backward-compatible MPU401 has an i/o port such as 0x330. Or, it
4255 might be a part of its own PCI i/o region. It depends on the
4256 chip design.
4257 </para>
4258
4259 <para>
Takashi Iwai302e4c22006-05-23 13:24:30 +02004260 The 5th argument is bitflags for additional information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 When the i/o port address above is a part of the PCI i/o
4262 region, the MPU401 i/o port might have been already allocated
Takashi Iwai302e4c22006-05-23 13:24:30 +02004263 (reserved) by the driver itself. In such a case, pass a bit flag
4264 <constant>MPU401_INFO_INTEGRATED</constant>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 and
4266 the mpu401-uart layer will allocate the i/o ports by itself.
4267 </para>
4268
Takashi Iwai302e4c22006-05-23 13:24:30 +02004269 <para>
4270 When the controller supports only the input or output MIDI stream,
4271 pass <constant>MPU401_INFO_INPUT</constant> or
4272 <constant>MPU401_INFO_OUTPUT</constant> bitflag, respectively.
4273 Then the rawmidi instance is created as a single stream.
4274 </para>
4275
4276 <para>
4277 <constant>MPU401_INFO_MMIO</constant> bitflag is used to change
4278 the access method to MMIO (via readb and writeb) instead of
4279 iob and outb. In this case, you have to pass the iomapped address
4280 to <function>snd_mpu401_uart_new()</function>.
4281 </para>
4282
4283 <para>
4284 When <constant>MPU401_INFO_TX_IRQ</constant> is set, the output
4285 stream isn't checked in the default interrupt handler. The driver
4286 needs to call <function>snd_mpu401_uart_interrupt_tx()</function>
4287 by itself to start processing the output stream in irq handler.
4288 </para>
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 <para>
4291 Usually, the port address corresponds to the command port and
4292 port + 1 corresponds to the data port. If not, you may change
4293 the <structfield>cport</structfield> field of
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004294 struct <structname>snd_mpu401</structname> manually
4295 afterward. However, <structname>snd_mpu401</structname> pointer is not
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 returned explicitly by
4297 <function>snd_mpu401_uart_new()</function>. You need to cast
4298 rmidi-&gt;private_data to
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004299 <structname>snd_mpu401</structname> explicitly,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
4301 <informalexample>
4302 <programlisting>
4303<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004304 struct snd_mpu401 *mpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 mpu = rmidi->private_data;
4306]]>
4307 </programlisting>
4308 </informalexample>
4309
4310 and reset the cport as you like:
4311
4312 <informalexample>
4313 <programlisting>
4314<![CDATA[
4315 mpu->cport = my_own_control_port;
4316]]>
4317 </programlisting>
4318 </informalexample>
4319 </para>
4320
4321 <para>
4322 The 6th argument specifies the irq number for UART. If the irq
4323 is already allocated, pass 0 to the 7th argument
4324 (<parameter>irq_flags</parameter>). Otherwise, pass the flags
4325 for irq allocation
4326 (<constant>SA_XXX</constant> bits) to it, and the irq will be
4327 reserved by the mpu401-uart layer. If the card doesn't generates
4328 UART interrupts, pass -1 as the irq number. Then a timer
4329 interrupt will be invoked for polling.
4330 </para>
4331 </section>
4332
4333 <section id="midi-interface-interrupt-handler">
4334 <title>Interrupt Handler</title>
4335 <para>
4336 When the interrupt is allocated in
4337 <function>snd_mpu401_uart_new()</function>, the private
4338 interrupt handler is used, hence you don't have to do nothing
4339 else than creating the mpu401 stuff. Otherwise, you have to call
4340 <function>snd_mpu401_uart_interrupt()</function> explicitly when
4341 a UART interrupt is invoked and checked in your own interrupt
4342 handler.
4343 </para>
4344
4345 <para>
4346 In this case, you need to pass the private_data of the
4347 returned rawmidi object from
4348 <function>snd_mpu401_uart_new()</function> as the second
4349 argument of <function>snd_mpu401_uart_interrupt()</function>.
4350
4351 <informalexample>
4352 <programlisting>
4353<![CDATA[
4354 snd_mpu401_uart_interrupt(irq, rmidi->private_data, regs);
4355]]>
4356 </programlisting>
4357 </informalexample>
4358 </para>
4359 </section>
4360
4361 </chapter>
4362
4363
4364<!-- ****************************************************** -->
4365<!-- RawMIDI Interface -->
4366<!-- ****************************************************** -->
4367 <chapter id="rawmidi-interface">
4368 <title>RawMIDI Interface</title>
4369
4370 <section id="rawmidi-interface-overview">
4371 <title>Overview</title>
4372
4373 <para>
4374 The raw MIDI interface is used for hardware MIDI ports that can
4375 be accessed as a byte stream. It is not used for synthesizer
4376 chips that do not directly understand MIDI.
4377 </para>
4378
4379 <para>
4380 ALSA handles file and buffer management. All you have to do is
4381 to write some code to move data between the buffer and the
4382 hardware.
4383 </para>
4384
4385 <para>
4386 The rawmidi API is defined in
4387 <filename>&lt;sound/rawmidi.h&gt;</filename>.
4388 </para>
4389 </section>
4390
4391 <section id="rawmidi-interface-constructor">
4392 <title>Constructor</title>
4393
4394 <para>
4395 To create a rawmidi device, call the
4396 <function>snd_rawmidi_new</function> function:
4397 <informalexample>
4398 <programlisting>
4399<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004400 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 err = snd_rawmidi_new(chip->card, "MyMIDI", 0, outs, ins, &rmidi);
4402 if (err < 0)
4403 return err;
4404 rmidi->private_data = chip;
4405 strcpy(rmidi->name, "My MIDI");
4406 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
4407 SNDRV_RAWMIDI_INFO_INPUT |
4408 SNDRV_RAWMIDI_INFO_DUPLEX;
4409]]>
4410 </programlisting>
4411 </informalexample>
4412 </para>
4413
4414 <para>
4415 The first argument is the card pointer, the second argument is
4416 the ID string.
4417 </para>
4418
4419 <para>
4420 The third argument is the index of this component. You can
4421 create up to 8 rawmidi devices.
4422 </para>
4423
4424 <para>
4425 The fourth and fifth arguments are the number of output and
4426 input substreams, respectively, of this device. (A substream is
4427 the equivalent of a MIDI port.)
4428 </para>
4429
4430 <para>
4431 Set the <structfield>info_flags</structfield> field to specify
4432 the capabilities of the device.
4433 Set <constant>SNDRV_RAWMIDI_INFO_OUTPUT</constant> if there is
4434 at least one output port,
4435 <constant>SNDRV_RAWMIDI_INFO_INPUT</constant> if there is at
4436 least one input port,
4437 and <constant>SNDRV_RAWMIDI_INFO_DUPLEX</constant> if the device
4438 can handle output and input at the same time.
4439 </para>
4440
4441 <para>
4442 After the rawmidi device is created, you need to set the
4443 operators (callbacks) for each substream. There are helper
4444 functions to set the operators for all substream of a device:
4445 <informalexample>
4446 <programlisting>
4447<![CDATA[
4448 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_mymidi_output_ops);
4449 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_mymidi_input_ops);
4450]]>
4451 </programlisting>
4452 </informalexample>
4453 </para>
4454
4455 <para>
4456 The operators are usually defined like this:
4457 <informalexample>
4458 <programlisting>
4459<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004460 static struct snd_rawmidi_ops snd_mymidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 .open = snd_mymidi_output_open,
4462 .close = snd_mymidi_output_close,
4463 .trigger = snd_mymidi_output_trigger,
4464 };
4465]]>
4466 </programlisting>
4467 </informalexample>
4468 These callbacks are explained in the <link
4469 linkend="rawmidi-interface-callbacks"><citetitle>Callbacks</citetitle></link>
4470 section.
4471 </para>
4472
4473 <para>
4474 If there is more than one substream, you should give each one a
4475 unique name:
4476 <informalexample>
4477 <programlisting>
4478<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004479 struct snd_rawmidi_substream *substream;
Takashi Iwai95a5b082007-07-26 16:50:09 +02004480 list_for_each_entry(substream,
4481 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
4482 list {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 sprintf(substream->name, "My MIDI Port %d", substream->number + 1);
4484 }
4485 /* same for SNDRV_RAWMIDI_STREAM_INPUT */
4486]]>
4487 </programlisting>
4488 </informalexample>
4489 </para>
4490 </section>
4491
4492 <section id="rawmidi-interface-callbacks">
4493 <title>Callbacks</title>
4494
4495 <para>
4496 In all callbacks, the private data that you've set for the
4497 rawmidi device can be accessed as
4498 substream-&gt;rmidi-&gt;private_data.
4499 <!-- <code> isn't available before DocBook 4.3 -->
4500 </para>
4501
4502 <para>
4503 If there is more than one port, your callbacks can determine the
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004504 port index from the struct snd_rawmidi_substream data passed to each
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 callback:
4506 <informalexample>
4507 <programlisting>
4508<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004509 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 int index = substream->number;
4511]]>
4512 </programlisting>
4513 </informalexample>
4514 </para>
4515
4516 <section id="rawmidi-interface-op-open">
4517 <title><function>open</function> callback</title>
4518
4519 <informalexample>
4520 <programlisting>
4521<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004522 static int snd_xxx_open(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523]]>
4524 </programlisting>
4525 </informalexample>
4526
4527 <para>
4528 This is called when a substream is opened.
4529 You can initialize the hardware here, but you should not yet
4530 start transmitting/receiving data.
4531 </para>
4532 </section>
4533
4534 <section id="rawmidi-interface-op-close">
4535 <title><function>close</function> callback</title>
4536
4537 <informalexample>
4538 <programlisting>
4539<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004540 static int snd_xxx_close(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541]]>
4542 </programlisting>
4543 </informalexample>
4544
4545 <para>
4546 Guess what.
4547 </para>
4548
4549 <para>
4550 The <function>open</function> and <function>close</function>
4551 callbacks of a rawmidi device are serialized with a mutex,
4552 and can sleep.
4553 </para>
4554 </section>
4555
4556 <section id="rawmidi-interface-op-trigger-out">
4557 <title><function>trigger</function> callback for output
4558 substreams</title>
4559
4560 <informalexample>
4561 <programlisting>
4562<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004563 static void snd_xxx_output_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564]]>
4565 </programlisting>
4566 </informalexample>
4567
4568 <para>
4569 This is called with a nonzero <parameter>up</parameter>
4570 parameter when there is some data in the substream buffer that
4571 must be transmitted.
4572 </para>
4573
4574 <para>
4575 To read data from the buffer, call
4576 <function>snd_rawmidi_transmit_peek</function>. It will
4577 return the number of bytes that have been read; this will be
4578 less than the number of bytes requested when there is no more
4579 data in the buffer.
4580 After the data has been transmitted successfully, call
4581 <function>snd_rawmidi_transmit_ack</function> to remove the
4582 data from the substream buffer:
4583 <informalexample>
4584 <programlisting>
4585<![CDATA[
4586 unsigned char data;
4587 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004588 if (snd_mychip_try_to_transmit(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 snd_rawmidi_transmit_ack(substream, 1);
4590 else
4591 break; /* hardware FIFO full */
4592 }
4593]]>
4594 </programlisting>
4595 </informalexample>
4596 </para>
4597
4598 <para>
4599 If you know beforehand that the hardware will accept data, you
4600 can use the <function>snd_rawmidi_transmit</function> function
4601 which reads some data and removes it from the buffer at once:
4602 <informalexample>
4603 <programlisting>
4604<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004605 while (snd_mychip_transmit_possible()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 unsigned char data;
4607 if (snd_rawmidi_transmit(substream, &data, 1) != 1)
4608 break; /* no more data */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004609 snd_mychip_transmit(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 }
4611]]>
4612 </programlisting>
4613 </informalexample>
4614 </para>
4615
4616 <para>
4617 If you know beforehand how many bytes you can accept, you can
4618 use a buffer size greater than one with the
4619 <function>snd_rawmidi_transmit*</function> functions.
4620 </para>
4621
4622 <para>
4623 The <function>trigger</function> callback must not sleep. If
4624 the hardware FIFO is full before the substream buffer has been
4625 emptied, you have to continue transmitting data later, either
4626 in an interrupt handler, or with a timer if the hardware
4627 doesn't have a MIDI transmit interrupt.
4628 </para>
4629
4630 <para>
4631 The <function>trigger</function> callback is called with a
4632 zero <parameter>up</parameter> parameter when the transmission
4633 of data should be aborted.
4634 </para>
4635 </section>
4636
4637 <section id="rawmidi-interface-op-trigger-in">
4638 <title><function>trigger</function> callback for input
4639 substreams</title>
4640
4641 <informalexample>
4642 <programlisting>
4643<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004644 static void snd_xxx_input_trigger(struct snd_rawmidi_substream *substream, int up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645]]>
4646 </programlisting>
4647 </informalexample>
4648
4649 <para>
4650 This is called with a nonzero <parameter>up</parameter>
4651 parameter to enable receiving data, or with a zero
4652 <parameter>up</parameter> parameter do disable receiving data.
4653 </para>
4654
4655 <para>
4656 The <function>trigger</function> callback must not sleep; the
4657 actual reading of data from the device is usually done in an
4658 interrupt handler.
4659 </para>
4660
4661 <para>
4662 When data reception is enabled, your interrupt handler should
4663 call <function>snd_rawmidi_receive</function> for all received
4664 data:
4665 <informalexample>
4666 <programlisting>
4667<![CDATA[
4668 void snd_mychip_midi_interrupt(...)
4669 {
4670 while (mychip_midi_available()) {
4671 unsigned char data;
4672 data = mychip_midi_read();
4673 snd_rawmidi_receive(substream, &data, 1);
4674 }
4675 }
4676]]>
4677 </programlisting>
4678 </informalexample>
4679 </para>
4680 </section>
4681
4682 <section id="rawmidi-interface-op-drain">
4683 <title><function>drain</function> callback</title>
4684
4685 <informalexample>
4686 <programlisting>
4687<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004688 static void snd_xxx_drain(struct snd_rawmidi_substream *substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689]]>
4690 </programlisting>
4691 </informalexample>
4692
4693 <para>
4694 This is only used with output substreams. This function should wait
4695 until all data read from the substream buffer has been transmitted.
4696 This ensures that the device can be closed and the driver unloaded
4697 without losing data.
4698 </para>
4699
4700 <para>
4701 This callback is optional. If you do not set
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004702 <structfield>drain</structfield> in the struct snd_rawmidi_ops
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 structure, ALSA will simply wait for 50&nbsp;milliseconds
4704 instead.
4705 </para>
4706 </section>
4707 </section>
4708
4709 </chapter>
4710
4711
4712<!-- ****************************************************** -->
4713<!-- Miscellaneous Devices -->
4714<!-- ****************************************************** -->
4715 <chapter id="misc-devices">
4716 <title>Miscellaneous Devices</title>
4717
4718 <section id="misc-devices-opl3">
4719 <title>FM OPL3</title>
4720 <para>
4721 The FM OPL3 is still used on many chips (mainly for backward
4722 compatibility). ALSA has a nice OPL3 FM control layer, too. The
4723 OPL3 API is defined in
4724 <filename>&lt;sound/opl3.h&gt;</filename>.
4725 </para>
4726
4727 <para>
4728 FM registers can be directly accessed through direct-FM API,
4729 defined in <filename>&lt;sound/asound_fm.h&gt;</filename>. In
4730 ALSA native mode, FM registers are accessed through
4731 Hardware-Dependant Device direct-FM extension API, whereas in
4732 OSS compatible mode, FM registers can be accessed with OSS
4733 direct-FM compatible API on <filename>/dev/dmfmX</filename> device.
4734 </para>
4735
4736 <para>
4737 For creating the OPL3 component, you have two functions to
4738 call. The first one is a constructor for <type>opl3_t</type>
4739 instance.
4740
4741 <informalexample>
4742 <programlisting>
4743<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004744 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 snd_opl3_create(card, lport, rport, OPL3_HW_OPL3_XXX,
4746 integrated, &opl3);
4747]]>
4748 </programlisting>
4749 </informalexample>
4750 </para>
4751
4752 <para>
4753 The first argument is the card pointer, the second one is the
4754 left port address, and the third is the right port address. In
4755 most cases, the right port is placed at the left port + 2.
4756 </para>
4757
4758 <para>
4759 The fourth argument is the hardware type.
4760 </para>
4761
4762 <para>
4763 When the left and right ports have been already allocated by
4764 the card driver, pass non-zero to the fifth argument
4765 (<parameter>integrated</parameter>). Otherwise, opl3 module will
4766 allocate the specified ports by itself.
4767 </para>
4768
4769 <para>
4770 When the accessing to the hardware requires special method
4771 instead of the standard I/O access, you can create opl3 instance
4772 separately with <function>snd_opl3_new()</function>.
4773
4774 <informalexample>
4775 <programlisting>
4776<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004777 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 snd_opl3_new(card, OPL3_HW_OPL3_XXX, &opl3);
4779]]>
4780 </programlisting>
4781 </informalexample>
4782 </para>
4783
4784 <para>
4785 Then set <structfield>command</structfield>,
4786 <structfield>private_data</structfield> and
4787 <structfield>private_free</structfield> for the private
4788 access function, the private data and the destructor.
4789 The l_port and r_port are not necessarily set. Only the
4790 command must be set properly. You can retrieve the data
4791 from opl3-&gt;private_data field.
4792 </para>
4793
4794 <para>
4795 After creating the opl3 instance via <function>snd_opl3_new()</function>,
4796 call <function>snd_opl3_init()</function> to initialize the chip to the
4797 proper state. Note that <function>snd_opl3_create()</function> always
4798 calls it internally.
4799 </para>
4800
4801 <para>
4802 If the opl3 instance is created successfully, then create a
4803 hwdep device for this opl3.
4804
4805 <informalexample>
4806 <programlisting>
4807<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004808 struct snd_hwdep *opl3hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 snd_opl3_hwdep_new(opl3, 0, 1, &opl3hwdep);
4810]]>
4811 </programlisting>
4812 </informalexample>
4813 </para>
4814
4815 <para>
4816 The first argument is the <type>opl3_t</type> instance you
4817 created, and the second is the index number, usually 0.
4818 </para>
4819
4820 <para>
4821 The third argument is the index-offset for the sequencer
4822 client assigned to the OPL3 port. When there is an MPU401-UART,
4823 give 1 for here (UART always takes 0).
4824 </para>
4825 </section>
4826
4827 <section id="misc-devices-hardware-dependent">
4828 <title>Hardware-Dependent Devices</title>
4829 <para>
4830 Some chips need the access from the user-space for special
4831 controls or for loading the micro code. In such a case, you can
4832 create a hwdep (hardware-dependent) device. The hwdep API is
4833 defined in <filename>&lt;sound/hwdep.h&gt;</filename>. You can
4834 find examples in opl3 driver or
4835 <filename>isa/sb/sb16_csp.c</filename>.
4836 </para>
4837
4838 <para>
4839 Creation of the <type>hwdep</type> instance is done via
4840 <function>snd_hwdep_new()</function>.
4841
4842 <informalexample>
4843 <programlisting>
4844<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004845 struct snd_hwdep *hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 snd_hwdep_new(card, "My HWDEP", 0, &hw);
4847]]>
4848 </programlisting>
4849 </informalexample>
4850
4851 where the third argument is the index number.
4852 </para>
4853
4854 <para>
4855 You can then pass any pointer value to the
4856 <parameter>private_data</parameter>.
4857 If you assign a private data, you should define the
4858 destructor, too. The destructor function is set to
4859 <structfield>private_free</structfield> field.
4860
4861 <informalexample>
4862 <programlisting>
4863<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004864 struct mydata *p = kmalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 hw->private_data = p;
4866 hw->private_free = mydata_free;
4867]]>
4868 </programlisting>
4869 </informalexample>
4870
4871 and the implementation of destructor would be:
4872
4873 <informalexample>
4874 <programlisting>
4875<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004876 static void mydata_free(struct snd_hwdep *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01004878 struct mydata *p = hw->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 kfree(p);
4880 }
4881]]>
4882 </programlisting>
4883 </informalexample>
4884 </para>
4885
4886 <para>
4887 The arbitrary file operations can be defined for this
4888 instance. The file operators are defined in
4889 <parameter>ops</parameter> table. For example, assume that
4890 this chip needs an ioctl.
4891
4892 <informalexample>
4893 <programlisting>
4894<![CDATA[
4895 hw->ops.open = mydata_open;
4896 hw->ops.ioctl = mydata_ioctl;
4897 hw->ops.release = mydata_release;
4898]]>
4899 </programlisting>
4900 </informalexample>
4901
4902 And implement the callback functions as you like.
4903 </para>
4904 </section>
4905
4906 <section id="misc-devices-IEC958">
4907 <title>IEC958 (S/PDIF)</title>
4908 <para>
4909 Usually the controls for IEC958 devices are implemented via
4910 control interface. There is a macro to compose a name string for
4911 IEC958 controls, <function>SNDRV_CTL_NAME_IEC958()</function>
4912 defined in <filename>&lt;include/asound.h&gt;</filename>.
4913 </para>
4914
4915 <para>
4916 There are some standard controls for IEC958 status bits. These
4917 controls use the type <type>SNDRV_CTL_ELEM_TYPE_IEC958</type>,
4918 and the size of element is fixed as 4 bytes array
4919 (value.iec958.status[x]). For <structfield>info</structfield>
4920 callback, you don't specify
4921 the value field for this type (the count field must be set,
4922 though).
4923 </para>
4924
4925 <para>
4926 <quote>IEC958 Playback Con Mask</quote> is used to return the
4927 bit-mask for the IEC958 status bits of consumer mode. Similarly,
4928 <quote>IEC958 Playback Pro Mask</quote> returns the bitmask for
4929 professional mode. They are read-only controls, and are defined
4930 as MIXER controls (iface =
4931 <constant>SNDRV_CTL_ELEM_IFACE_MIXER</constant>).
4932 </para>
4933
4934 <para>
4935 Meanwhile, <quote>IEC958 Playback Default</quote> control is
4936 defined for getting and setting the current default IEC958
4937 bits. Note that this one is usually defined as a PCM control
4938 (iface = <constant>SNDRV_CTL_ELEM_IFACE_PCM</constant>),
4939 although in some places it's defined as a MIXER control.
4940 </para>
4941
4942 <para>
4943 In addition, you can define the control switches to
4944 enable/disable or to set the raw bit mode. The implementation
4945 will depend on the chip, but the control should be named as
4946 <quote>IEC958 xxx</quote>, preferably using
4947 <function>SNDRV_CTL_NAME_IEC958()</function> macro.
4948 </para>
4949
4950 <para>
4951 You can find several cases, for example,
4952 <filename>pci/emu10k1</filename>,
4953 <filename>pci/ice1712</filename>, or
4954 <filename>pci/cmipci.c</filename>.
4955 </para>
4956 </section>
4957
4958 </chapter>
4959
4960
4961<!-- ****************************************************** -->
4962<!-- Buffer and Memory Management -->
4963<!-- ****************************************************** -->
4964 <chapter id="buffer-and-memory">
4965 <title>Buffer and Memory Management</title>
4966
4967 <section id="buffer-and-memory-buffer-types">
4968 <title>Buffer Types</title>
4969 <para>
4970 ALSA provides several different buffer allocation functions
4971 depending on the bus and the architecture. All these have a
4972 consistent API. The allocation of physically-contiguous pages is
4973 done via
4974 <function>snd_malloc_xxx_pages()</function> function, where xxx
4975 is the bus type.
4976 </para>
4977
4978 <para>
4979 The allocation of pages with fallback is
4980 <function>snd_malloc_xxx_pages_fallback()</function>. This
4981 function tries to allocate the specified pages but if the pages
4982 are not available, it tries to reduce the page sizes until the
4983 enough space is found.
4984 </para>
4985
4986 <para>
4987 For releasing the space, call
4988 <function>snd_free_xxx_pages()</function> function.
4989 </para>
4990
4991 <para>
4992 Usually, ALSA drivers try to allocate and reserve
4993 a large contiguous physical space
4994 at the time the module is loaded for the later use.
4995 This is called <quote>pre-allocation</quote>.
4996 As already written, you can call the following function at the
4997 construction of pcm instance (in the case of PCI bus).
4998
4999 <informalexample>
5000 <programlisting>
5001<![CDATA[
5002 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
5003 snd_dma_pci_data(pci), size, max);
5004]]>
5005 </programlisting>
5006 </informalexample>
5007
5008 where <parameter>size</parameter> is the byte size to be
5009 pre-allocated and the <parameter>max</parameter> is the maximal
5010 size to be changed via <filename>prealloc</filename> proc file.
5011 The allocator will try to get as large area as possible
5012 within the given size.
5013 </para>
5014
5015 <para>
5016 The second argument (type) and the third argument (device pointer)
5017 are dependent on the bus.
5018 In the case of ISA bus, pass <function>snd_dma_isa_data()</function>
5019 as the third argument with <constant>SNDRV_DMA_TYPE_DEV</constant> type.
5020 For the continuous buffer unrelated to the bus can be pre-allocated
5021 with <constant>SNDRV_DMA_TYPE_CONTINUOUS</constant> type and the
5022 <function>snd_dma_continuous_data(GFP_KERNEL)</function> device pointer,
5023 whereh <constant>GFP_KERNEL</constant> is the kernel allocation flag to
5024 use. For the SBUS, <constant>SNDRV_DMA_TYPE_SBUS</constant> and
5025 <function>snd_dma_sbus_data(sbus_dev)</function> are used instead.
5026 For the PCI scatter-gather buffers, use
5027 <constant>SNDRV_DMA_TYPE_DEV_SG</constant> with
5028 <function>snd_dma_pci_data(pci)</function>
5029 (see the section
5030 <link linkend="buffer-and-memory-non-contiguous"><citetitle>Non-Contiguous Buffers
5031 </citetitle></link>).
5032 </para>
5033
5034 <para>
5035 Once when the buffer is pre-allocated, you can use the
5036 allocator in the <structfield>hw_params</structfield> callback
5037
5038 <informalexample>
5039 <programlisting>
5040<![CDATA[
5041 snd_pcm_lib_malloc_pages(substream, size);
5042]]>
5043 </programlisting>
5044 </informalexample>
5045
5046 Note that you have to pre-allocate to use this function.
5047 </para>
5048 </section>
5049
5050 <section id="buffer-and-memory-external-hardware">
5051 <title>External Hardware Buffers</title>
5052 <para>
5053 Some chips have their own hardware buffers and the DMA
5054 transfer from the host memory is not available. In such a case,
5055 you need to either 1) copy/set the audio data directly to the
5056 external hardware buffer, or 2) make an intermediate buffer and
5057 copy/set the data from it to the external hardware buffer in
5058 interrupts (or in tasklets, preferably).
5059 </para>
5060
5061 <para>
5062 The first case works fine if the external hardware buffer is enough
5063 large. This method doesn't need any extra buffers and thus is
5064 more effective. You need to define the
5065 <structfield>copy</structfield> and
5066 <structfield>silence</structfield> callbacks for
5067 the data transfer. However, there is a drawback: it cannot
5068 be mmapped. The examples are GUS's GF1 PCM or emu8000's
5069 wavetable PCM.
5070 </para>
5071
5072 <para>
5073 The second case allows the mmap of the buffer, although you have
5074 to handle an interrupt or a tasklet for transferring the data
5075 from the intermediate buffer to the hardware buffer. You can find an
5076 example in vxpocket driver.
5077 </para>
5078
5079 <para>
5080 Another case is that the chip uses a PCI memory-map
5081 region for the buffer instead of the host memory. In this case,
5082 mmap is available only on certain architectures like intel. In
5083 non-mmap mode, the data cannot be transferred as the normal
5084 way. Thus you need to define <structfield>copy</structfield> and
5085 <structfield>silence</structfield> callbacks as well
5086 as in the cases above. The examples are found in
5087 <filename>rme32.c</filename> and <filename>rme96.c</filename>.
5088 </para>
5089
5090 <para>
5091 The implementation of <structfield>copy</structfield> and
5092 <structfield>silence</structfield> callbacks depends upon
5093 whether the hardware supports interleaved or non-interleaved
5094 samples. The <structfield>copy</structfield> callback is
5095 defined like below, a bit
5096 differently depending whether the direction is playback or
5097 capture:
5098
5099 <informalexample>
5100 <programlisting>
5101<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005102 static int playback_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 snd_pcm_uframes_t pos, void *src, snd_pcm_uframes_t count);
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005104 static int capture_copy(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 snd_pcm_uframes_t pos, void *dst, snd_pcm_uframes_t count);
5106]]>
5107 </programlisting>
5108 </informalexample>
5109 </para>
5110
5111 <para>
5112 In the case of interleaved samples, the second argument
5113 (<parameter>channel</parameter>) is not used. The third argument
5114 (<parameter>pos</parameter>) points the
5115 current position offset in frames.
5116 </para>
5117
5118 <para>
5119 The meaning of the fourth argument is different between
5120 playback and capture. For playback, it holds the source data
5121 pointer, and for capture, it's the destination data pointer.
5122 </para>
5123
5124 <para>
5125 The last argument is the number of frames to be copied.
5126 </para>
5127
5128 <para>
5129 What you have to do in this callback is again different
5130 between playback and capture directions. In the case of
5131 playback, you do: copy the given amount of data
5132 (<parameter>count</parameter>) at the specified pointer
5133 (<parameter>src</parameter>) to the specified offset
5134 (<parameter>pos</parameter>) on the hardware buffer. When
5135 coded like memcpy-like way, the copy would be like:
5136
5137 <informalexample>
5138 <programlisting>
5139<![CDATA[
5140 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), src,
5141 frames_to_bytes(runtime, count));
5142]]>
5143 </programlisting>
5144 </informalexample>
5145 </para>
5146
5147 <para>
5148 For the capture direction, you do: copy the given amount of
5149 data (<parameter>count</parameter>) at the specified offset
5150 (<parameter>pos</parameter>) on the hardware buffer to the
5151 specified pointer (<parameter>dst</parameter>).
5152
5153 <informalexample>
5154 <programlisting>
5155<![CDATA[
5156 my_memcpy(dst, my_buffer + frames_to_bytes(runtime, pos),
5157 frames_to_bytes(runtime, count));
5158]]>
5159 </programlisting>
5160 </informalexample>
5161
5162 Note that both of the position and the data amount are given
5163 in frames.
5164 </para>
5165
5166 <para>
5167 In the case of non-interleaved samples, the implementation
5168 will be a bit more complicated.
5169 </para>
5170
5171 <para>
5172 You need to check the channel argument, and if it's -1, copy
5173 the whole channels. Otherwise, you have to copy only the
5174 specified channel. Please check
5175 <filename>isa/gus/gus_pcm.c</filename> as an example.
5176 </para>
5177
5178 <para>
5179 The <structfield>silence</structfield> callback is also
5180 implemented in a similar way.
5181
5182 <informalexample>
5183 <programlisting>
5184<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005185 static int silence(struct snd_pcm_substream *substream, int channel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
5187]]>
5188 </programlisting>
5189 </informalexample>
5190 </para>
5191
5192 <para>
5193 The meanings of arguments are identical with the
5194 <structfield>copy</structfield>
5195 callback, although there is no <parameter>src/dst</parameter>
5196 argument. In the case of interleaved samples, the channel
5197 argument has no meaning, as well as on
5198 <structfield>copy</structfield> callback.
5199 </para>
5200
5201 <para>
5202 The role of <structfield>silence</structfield> callback is to
5203 set the given amount
5204 (<parameter>count</parameter>) of silence data at the
5205 specified offset (<parameter>pos</parameter>) on the hardware
5206 buffer. Suppose that the data format is signed (that is, the
5207 silent-data is 0), and the implementation using a memset-like
5208 function would be like:
5209
5210 <informalexample>
5211 <programlisting>
5212<![CDATA[
5213 my_memcpy(my_buffer + frames_to_bytes(runtime, pos), 0,
5214 frames_to_bytes(runtime, count));
5215]]>
5216 </programlisting>
5217 </informalexample>
5218 </para>
5219
5220 <para>
5221 In the case of non-interleaved samples, again, the
5222 implementation becomes a bit more complicated. See, for example,
5223 <filename>isa/gus/gus_pcm.c</filename>.
5224 </para>
5225 </section>
5226
5227 <section id="buffer-and-memory-non-contiguous">
5228 <title>Non-Contiguous Buffers</title>
5229 <para>
5230 If your hardware supports the page table like emu10k1 or the
5231 buffer descriptors like via82xx, you can use the scatter-gather
5232 (SG) DMA. ALSA provides an interface for handling SG-buffers.
5233 The API is provided in <filename>&lt;sound/pcm.h&gt;</filename>.
5234 </para>
5235
5236 <para>
5237 For creating the SG-buffer handler, call
5238 <function>snd_pcm_lib_preallocate_pages()</function> or
5239 <function>snd_pcm_lib_preallocate_pages_for_all()</function>
5240 with <constant>SNDRV_DMA_TYPE_DEV_SG</constant>
5241 in the PCM constructor like other PCI pre-allocator.
5242 You need to pass the <function>snd_dma_pci_data(pci)</function>,
5243 where pci is the struct <structname>pci_dev</structname> pointer
5244 of the chip as well.
Giuliano Pochini44275f12006-01-27 12:02:05 +01005245 The <type>struct snd_sg_buf</type> instance is created as
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 substream-&gt;dma_private. You can cast
5247 the pointer like:
5248
5249 <informalexample>
5250 <programlisting>
5251<![CDATA[
Giuliano Pochini44275f12006-01-27 12:02:05 +01005252 struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253]]>
5254 </programlisting>
5255 </informalexample>
5256 </para>
5257
5258 <para>
5259 Then call <function>snd_pcm_lib_malloc_pages()</function>
5260 in <structfield>hw_params</structfield> callback
5261 as well as in the case of normal PCI buffer.
5262 The SG-buffer handler will allocate the non-contiguous kernel
5263 pages of the given size and map them onto the virtually contiguous
5264 memory. The virtual pointer is addressed in runtime-&gt;dma_area.
5265 The physical address (runtime-&gt;dma_addr) is set to zero,
5266 because the buffer is physically non-contigous.
5267 The physical address table is set up in sgbuf-&gt;table.
5268 You can get the physical address at a certain offset via
5269 <function>snd_pcm_sgbuf_get_addr()</function>.
5270 </para>
5271
5272 <para>
5273 When a SG-handler is used, you need to set
5274 <function>snd_pcm_sgbuf_ops_page</function> as
5275 the <structfield>page</structfield> callback.
5276 (See <link linkend="pcm-interface-operators-page-callback">
5277 <citetitle>page callback section</citetitle></link>.)
5278 </para>
5279
5280 <para>
5281 For releasing the data, call
5282 <function>snd_pcm_lib_free_pages()</function> in the
5283 <structfield>hw_free</structfield> callback as usual.
5284 </para>
5285 </section>
5286
5287 <section id="buffer-and-memory-vmalloced">
5288 <title>Vmalloc'ed Buffers</title>
5289 <para>
5290 It's possible to use a buffer allocated via
5291 <function>vmalloc</function>, for example, for an intermediate
5292 buffer. Since the allocated pages are not contiguous, you need
5293 to set the <structfield>page</structfield> callback to obtain
5294 the physical address at every offset.
5295 </para>
5296
5297 <para>
5298 The implementation of <structfield>page</structfield> callback
5299 would be like this:
5300
5301 <informalexample>
5302 <programlisting>
5303<![CDATA[
5304 #include <linux/vmalloc.h>
5305
5306 /* get the physical page pointer on the given offset */
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005307 static struct page *mychip_page(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 unsigned long offset)
5309 {
5310 void *pageptr = substream->runtime->dma_area + offset;
5311 return vmalloc_to_page(pageptr);
5312 }
5313]]>
5314 </programlisting>
5315 </informalexample>
5316 </para>
5317 </section>
5318
5319 </chapter>
5320
5321
5322<!-- ****************************************************** -->
5323<!-- Proc Interface -->
5324<!-- ****************************************************** -->
5325 <chapter id="proc-interface">
5326 <title>Proc Interface</title>
5327 <para>
5328 ALSA provides an easy interface for procfs. The proc files are
5329 very useful for debugging. I recommend you set up proc files if
5330 you write a driver and want to get a running status or register
5331 dumps. The API is found in
5332 <filename>&lt;sound/info.h&gt;</filename>.
5333 </para>
5334
5335 <para>
5336 For creating a proc file, call
5337 <function>snd_card_proc_new()</function>.
5338
5339 <informalexample>
5340 <programlisting>
5341<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005342 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 int err = snd_card_proc_new(card, "my-file", &entry);
5344]]>
5345 </programlisting>
5346 </informalexample>
5347
5348 where the second argument specifies the proc-file name to be
5349 created. The above example will create a file
5350 <filename>my-file</filename> under the card directory,
5351 e.g. <filename>/proc/asound/card0/my-file</filename>.
5352 </para>
5353
5354 <para>
5355 Like other components, the proc entry created via
5356 <function>snd_card_proc_new()</function> will be registered and
5357 released automatically in the card registration and release
5358 functions.
5359 </para>
5360
5361 <para>
5362 When the creation is successful, the function stores a new
5363 instance at the pointer given in the third argument.
5364 It is initialized as a text proc file for read only. For using
5365 this proc file as a read-only text file as it is, set the read
5366 callback with a private data via
5367 <function>snd_info_set_text_ops()</function>.
5368
5369 <informalexample>
5370 <programlisting>
5371<![CDATA[
Takashi Iwaibf850202006-04-28 15:13:41 +02005372 snd_info_set_text_ops(entry, chip, my_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373]]>
5374 </programlisting>
5375 </informalexample>
5376
5377 where the second argument (<parameter>chip</parameter>) is the
5378 private data to be used in the callbacks. The third parameter
5379 specifies the read buffer size and the fourth
5380 (<parameter>my_proc_read</parameter>) is the callback function, which
5381 is defined like
5382
5383 <informalexample>
5384 <programlisting>
5385<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005386 static void my_proc_read(struct snd_info_entry *entry,
5387 struct snd_info_buffer *buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388]]>
5389 </programlisting>
5390 </informalexample>
5391
5392 </para>
5393
5394 <para>
5395 In the read callback, use <function>snd_iprintf()</function> for
5396 output strings, which works just like normal
5397 <function>printf()</function>. For example,
5398
5399 <informalexample>
5400 <programlisting>
5401<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005402 static void my_proc_read(struct snd_info_entry *entry,
5403 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 {
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005405 struct my_chip *chip = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 snd_iprintf(buffer, "This is my chip!\n");
5408 snd_iprintf(buffer, "Port = %ld\n", chip->port);
5409 }
5410]]>
5411 </programlisting>
5412 </informalexample>
5413 </para>
5414
5415 <para>
5416 The file permission can be changed afterwards. As default, it's
5417 set as read only for all users. If you want to add the write
5418 permission to the user (root as default), set like below:
5419
5420 <informalexample>
5421 <programlisting>
5422<![CDATA[
5423 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
5424]]>
5425 </programlisting>
5426 </informalexample>
5427
5428 and set the write buffer size and the callback
5429
5430 <informalexample>
5431 <programlisting>
5432<![CDATA[
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 entry->c.text.write = my_proc_write;
5434]]>
5435 </programlisting>
5436 </informalexample>
5437 </para>
5438
5439 <para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 For the write callback, you can use
5441 <function>snd_info_get_line()</function> to get a text line, and
5442 <function>snd_info_get_str()</function> to retrieve a string from
5443 the line. Some examples are found in
5444 <filename>core/oss/mixer_oss.c</filename>, core/oss/and
5445 <filename>pcm_oss.c</filename>.
5446 </para>
5447
5448 <para>
5449 For a raw-data proc-file, set the attributes like the following:
5450
5451 <informalexample>
5452 <programlisting>
5453<![CDATA[
5454 static struct snd_info_entry_ops my_file_io_ops = {
5455 .read = my_file_io_read,
5456 };
5457
5458 entry->content = SNDRV_INFO_CONTENT_DATA;
5459 entry->private_data = chip;
5460 entry->c.ops = &my_file_io_ops;
5461 entry->size = 4096;
5462 entry->mode = S_IFREG | S_IRUGO;
5463]]>
5464 </programlisting>
5465 </informalexample>
5466 </para>
5467
5468 <para>
5469 The callback is much more complicated than the text-file
5470 version. You need to use a low-level i/o functions such as
5471 <function>copy_from/to_user()</function> to transfer the
5472 data.
5473
5474 <informalexample>
5475 <programlisting>
5476<![CDATA[
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005477 static long my_file_io_read(struct snd_info_entry *entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 void *file_private_data,
5479 struct file *file,
5480 char *buf,
5481 unsigned long count,
5482 unsigned long pos)
5483 {
5484 long size = count;
5485 if (pos + size > local_max_size)
5486 size = local_max_size - pos;
5487 if (copy_to_user(buf, local_data + pos, size))
5488 return -EFAULT;
5489 return size;
5490 }
5491]]>
5492 </programlisting>
5493 </informalexample>
5494 </para>
5495
5496 </chapter>
5497
5498
5499<!-- ****************************************************** -->
5500<!-- Power Management -->
5501<!-- ****************************************************** -->
5502 <chapter id="power-management">
5503 <title>Power Management</title>
5504 <para>
Paolo Ornati670e9f32006-10-03 22:57:56 +02005505 If the chip is supposed to work with suspend/resume
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 functions, you need to add the power-management codes to the
5507 driver. The additional codes for the power-management should be
5508 <function>ifdef</function>'ed with
5509 <constant>CONFIG_PM</constant>.
5510 </para>
5511
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005512 <para>
5513 If the driver supports the suspend/resume
5514 <emphasis>fully</emphasis>, that is, the device can be
5515 properly resumed to the status at the suspend is called,
5516 you can set <constant>SNDRV_PCM_INFO_RESUME</constant> flag
5517 to pcm info field. Usually, this is possible when the
5518 registers of ths chip can be safely saved and restored to the
5519 RAM. If this is set, the trigger callback is called with
5520 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> after resume
5521 callback is finished.
5522 </para>
5523
5524 <para>
5525 Even if the driver doesn't support PM fully but only the
5526 partial suspend/resume is possible, it's still worthy to
5527 implement suspend/resume callbacks. In such a case, applications
5528 would reset the status by calling
5529 <function>snd_pcm_prepare()</function> and restart the stream
5530 appropriately. Hence, you can define suspend/resume callbacks
5531 below but don't set <constant>SNDRV_PCM_INFO_RESUME</constant>
5532 info flag to the PCM.
5533 </para>
5534
5535 <para>
5536 Note that the trigger with SUSPEND can be always called when
5537 <function>snd_pcm_suspend_all</function> is called,
5538 regardless of <constant>SNDRV_PCM_INFO_RESUME</constant> flag.
5539 The <constant>RESUME</constant> flag affects only the behavior
5540 of <function>snd_pcm_resume()</function>.
5541 (Thus, in theory,
5542 <constant>SNDRV_PCM_TRIGGER_RESUME</constant> isn't needed
5543 to be handled in the trigger callback when no
5544 <constant>SNDRV_PCM_INFO_RESUME</constant> flag is set. But,
5545 it's better to keep it for compatibility reason.)
5546 </para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005548 In the earlier version of ALSA drivers, a common
5549 power-management layer was provided, but it has been removed.
5550 The driver needs to define the suspend/resume hooks according to
5551 the bus the device is assigned. In the case of PCI driver, the
5552 callbacks look like below:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554 <informalexample>
5555 <programlisting>
5556<![CDATA[
5557 #ifdef CONFIG_PM
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005558 static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005560 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 return 0;
5562 }
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005563 static int snd_my_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 {
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01005565 .... /* do things for suspend */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return 0;
5567 }
5568 #endif
5569]]>
5570 </programlisting>
5571 </informalexample>
5572 </para>
5573
5574 <para>
5575 The scheme of the real suspend job is as following.
5576
5577 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005578 <listitem><para>Retrieve the card and the chip data.</para></listitem>
5579 <listitem><para>Call <function>snd_power_change_state()</function> with
5580 <constant>SNDRV_CTL_POWER_D3hot</constant> to change the
5581 power status.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 <listitem><para>Call <function>snd_pcm_suspend_all()</function> to suspend the running PCM streams.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005583 <listitem><para>If AC97 codecs are used, call
Takashi Iwaia7306332006-05-04 11:58:43 +02005584 <function>snd_ac97_suspend()</function> for each codec.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 <listitem><para>Save the register values if necessary.</para></listitem>
5586 <listitem><para>Stop the hardware if necessary.</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005587 <listitem><para>Disable the PCI device by calling
5588 <function>pci_disable_device()</function>. Then, call
5589 <function>pci_save_state()</function> at last.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 </orderedlist>
5591 </para>
5592
5593 <para>
5594 A typical code would be like:
5595
5596 <informalexample>
5597 <programlisting>
5598<![CDATA[
Alexey Dobriyan323579882006-01-15 02:12:54 +01005599 static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 {
5601 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005602 struct snd_card *card = pci_get_drvdata(pci);
5603 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005605 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 /* (3) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005607 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 /* (4) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005609 snd_ac97_suspend(chip->ac97);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 /* (5) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005611 snd_mychip_save_registers(chip);
5612 /* (6) */
5613 snd_mychip_stop_hardware(chip);
5614 /* (7) */
5615 pci_disable_device(pci);
5616 pci_save_state(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 return 0;
5618 }
5619]]>
5620 </programlisting>
5621 </informalexample>
5622 </para>
5623
5624 <para>
5625 The scheme of the real resume job is as following.
5626
5627 <orderedlist>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005628 <listitem><para>Retrieve the card and the chip data.</para></listitem>
5629 <listitem><para>Set up PCI. First, call <function>pci_restore_state()</function>.
5630 Then enable the pci device again by calling <function>pci_enable_device()</function>.
5631 Call <function>pci_set_master()</function> if necessary, too.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 <listitem><para>Re-initialize the chip.</para></listitem>
5633 <listitem><para>Restore the saved registers if necessary.</para></listitem>
5634 <listitem><para>Resume the mixer, e.g. calling
5635 <function>snd_ac97_resume()</function>.</para></listitem>
5636 <listitem><para>Restart the hardware (if any).</para></listitem>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005637 <listitem><para>Call <function>snd_power_change_state()</function> with
5638 <constant>SNDRV_CTL_POWER_D0</constant> to notify the processes.</para></listitem>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 </orderedlist>
5640 </para>
5641
5642 <para>
5643 A typical code would be like:
5644
5645 <informalexample>
5646 <programlisting>
5647<![CDATA[
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005648 static int mychip_resume(struct pci_dev *pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 {
5650 /* (1) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005651 struct snd_card *card = pci_get_drvdata(pci);
5652 struct mychip *chip = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 /* (2) */
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005654 pci_restore_state(pci);
5655 pci_enable_device(pci);
5656 pci_set_master(pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 /* (3) */
5658 snd_mychip_reinit_chip(chip);
5659 /* (4) */
5660 snd_mychip_restore_registers(chip);
5661 /* (5) */
5662 snd_ac97_resume(chip->ac97);
5663 /* (6) */
5664 snd_mychip_restart_chip(chip);
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005665 /* (7) */
5666 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 return 0;
5668 }
5669]]>
5670 </programlisting>
5671 </informalexample>
5672 </para>
5673
5674 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005675 As shown in the above, it's better to save registers after
5676 suspending the PCM operations via
5677 <function>snd_pcm_suspend_all()</function> or
5678 <function>snd_pcm_suspend()</function>. It means that the PCM
5679 streams are already stoppped when the register snapshot is
5680 taken. But, remind that you don't have to restart the PCM
5681 stream in the resume callback. It'll be restarted via
5682 trigger call with <constant>SNDRV_PCM_TRIGGER_RESUME</constant>
5683 when necessary.
5684 </para>
5685
5686 <para>
5687 OK, we have all callbacks now. Let's set them up. In the
5688 initialization of the card, make sure that you can get the chip
5689 data from the card instance, typically via
5690 <structfield>private_data</structfield> field, in case you
5691 created the chip data individually.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 <informalexample>
5694 <programlisting>
5695<![CDATA[
5696 static int __devinit snd_mychip_probe(struct pci_dev *pci,
5697 const struct pci_device_id *pci_id)
5698 {
5699 ....
Takashi Iwai446ab5f2005-11-17 15:12:54 +01005700 struct snd_card *card;
5701 struct mychip *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 ....
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005703 card = snd_card_new(index[dev], id[dev], THIS_MODULE, NULL);
5704 ....
5705 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
5706 ....
5707 card->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 ....
5709 }
5710]]>
5711 </programlisting>
5712 </informalexample>
5713
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005714 When you created the chip data with
5715 <function>snd_card_new()</function>, it's anyway accessible
5716 via <structfield>private_data</structfield> field.
5717
5718 <informalexample>
5719 <programlisting>
5720<![CDATA[
5721 static int __devinit snd_mychip_probe(struct pci_dev *pci,
5722 const struct pci_device_id *pci_id)
5723 {
5724 ....
5725 struct snd_card *card;
5726 struct mychip *chip;
5727 ....
5728 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
5729 sizeof(struct mychip));
5730 ....
5731 chip = card->private_data;
5732 ....
5733 }
5734]]>
5735 </programlisting>
5736 </informalexample>
5737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 </para>
5739
5740 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005741 If you need a space for saving the registers, allocate the
5742 buffer for it here, too, since it would be fatal
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 if you cannot allocate a memory in the suspend phase.
5744 The allocated buffer should be released in the corresponding
5745 destructor.
5746 </para>
5747
5748 <para>
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005749 And next, set suspend/resume callbacks to the pci_driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
5751 <informalexample>
5752 <programlisting>
5753<![CDATA[
5754 static struct pci_driver driver = {
5755 .name = "My Chip",
5756 .id_table = snd_my_ids,
5757 .probe = snd_my_probe,
5758 .remove = __devexit_p(snd_my_remove),
Takashi Iwai5fe76e42005-11-17 17:26:09 +01005759 #ifdef CONFIG_PM
5760 .suspend = snd_my_suspend,
5761 .resume = snd_my_resume,
5762 #endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 };
5764]]>
5765 </programlisting>
5766 </informalexample>
5767 </para>
5768
5769 </chapter>
5770
5771
5772<!-- ****************************************************** -->
5773<!-- Module Parameters -->
5774<!-- ****************************************************** -->
5775 <chapter id="module-parameters">
5776 <title>Module Parameters</title>
5777 <para>
5778 There are standard module options for ALSA. At least, each
5779 module should have <parameter>index</parameter>,
5780 <parameter>id</parameter> and <parameter>enable</parameter>
5781 options.
5782 </para>
5783
5784 <para>
5785 If the module supports multiple cards (usually up to
5786 8 = <constant>SNDRV_CARDS</constant> cards), they should be
5787 arrays. The default initial values are defined already as
5788 constants for ease of programming:
5789
5790 <informalexample>
5791 <programlisting>
5792<![CDATA[
5793 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
5794 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
5795 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
5796]]>
5797 </programlisting>
5798 </informalexample>
5799 </para>
5800
5801 <para>
5802 If the module supports only a single card, they could be single
5803 variables, instead. <parameter>enable</parameter> option is not
5804 always necessary in this case, but it wouldn't be so bad to have a
5805 dummy option for compatibility.
5806 </para>
5807
5808 <para>
5809 The module parameters must be declared with the standard
5810 <function>module_param()()</function>,
5811 <function>module_param_array()()</function> and
5812 <function>MODULE_PARM_DESC()</function> macros.
5813 </para>
5814
5815 <para>
5816 The typical coding would be like below:
5817
5818 <informalexample>
5819 <programlisting>
5820<![CDATA[
5821 #define CARD_NAME "My Chip"
5822
5823 module_param_array(index, int, NULL, 0444);
5824 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
5825 module_param_array(id, charp, NULL, 0444);
5826 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
5827 module_param_array(enable, bool, NULL, 0444);
5828 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
5829]]>
5830 </programlisting>
5831 </informalexample>
5832 </para>
5833
5834 <para>
5835 Also, don't forget to define the module description, classes,
5836 license and devices. Especially, the recent modprobe requires to
5837 define the module license as GPL, etc., otherwise the system is
5838 shown as <quote>tainted</quote>.
5839
5840 <informalexample>
5841 <programlisting>
5842<![CDATA[
5843 MODULE_DESCRIPTION("My Chip");
5844 MODULE_LICENSE("GPL");
5845 MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
5846]]>
5847 </programlisting>
5848 </informalexample>
5849 </para>
5850
5851 </chapter>
5852
5853
5854<!-- ****************************************************** -->
5855<!-- How To Put Your Driver -->
5856<!-- ****************************************************** -->
5857 <chapter id="how-to-put-your-driver">
5858 <title>How To Put Your Driver Into ALSA Tree</title>
5859 <section>
5860 <title>General</title>
5861 <para>
5862 So far, you've learned how to write the driver codes.
5863 And you might have a question now: how to put my own
5864 driver into the ALSA driver tree?
5865 Here (finally :) the standard procedure is described briefly.
5866 </para>
5867
5868 <para>
5869 Suppose that you'll create a new PCI driver for the card
5870 <quote>xyz</quote>. The card module name would be
5871 snd-xyz. The new driver is usually put into alsa-driver
5872 tree, <filename>alsa-driver/pci</filename> directory in
5873 the case of PCI cards.
5874 Then the driver is evaluated, audited and tested
5875 by developers and users. After a certain time, the driver
5876 will go to alsa-kernel tree (to the corresponding directory,
5877 such as <filename>alsa-kernel/pci</filename>) and eventually
5878 integrated into Linux 2.6 tree (the directory would be
5879 <filename>linux/sound/pci</filename>).
5880 </para>
5881
5882 <para>
5883 In the following sections, the driver code is supposed
5884 to be put into alsa-driver tree. The two cases are assumed:
5885 a driver consisting of a single source file and one consisting
5886 of several source files.
5887 </para>
5888 </section>
5889
5890 <section>
5891 <title>Driver with A Single Source File</title>
5892 <para>
5893 <orderedlist>
5894 <listitem>
5895 <para>
5896 Modify alsa-driver/pci/Makefile
5897 </para>
5898
5899 <para>
5900 Suppose you have a file xyz.c. Add the following
5901 two lines
5902 <informalexample>
5903 <programlisting>
5904<![CDATA[
5905 snd-xyz-objs := xyz.o
5906 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
5907]]>
5908 </programlisting>
5909 </informalexample>
5910 </para>
5911 </listitem>
5912
5913 <listitem>
5914 <para>
5915 Create the Kconfig entry
5916 </para>
5917
5918 <para>
5919 Add the new entry of Kconfig for your xyz driver.
5920 <informalexample>
5921 <programlisting>
5922<![CDATA[
5923 config SND_XYZ
5924 tristate "Foobar XYZ"
5925 depends on SND
5926 select SND_PCM
5927 help
5928 Say Y here to include support for Foobar XYZ soundcard.
5929
5930 To compile this driver as a module, choose M here: the module
5931 will be called snd-xyz.
5932]]>
5933 </programlisting>
5934 </informalexample>
5935
5936 the line, select SND_PCM, specifies that the driver xyz supports
5937 PCM. In addition to SND_PCM, the following components are
5938 supported for select command:
5939 SND_RAWMIDI, SND_TIMER, SND_HWDEP, SND_MPU401_UART,
5940 SND_OPL3_LIB, SND_OPL4_LIB, SND_VX_LIB, SND_AC97_CODEC.
5941 Add the select command for each supported component.
5942 </para>
5943
5944 <para>
5945 Note that some selections imply the lowlevel selections.
5946 For example, PCM includes TIMER, MPU401_UART includes RAWMIDI,
5947 AC97_CODEC includes PCM, and OPL3_LIB includes HWDEP.
5948 You don't need to give the lowlevel selections again.
5949 </para>
5950
5951 <para>
5952 For the details of Kconfig script, refer to the kbuild
5953 documentation.
5954 </para>
5955
5956 </listitem>
5957
5958 <listitem>
5959 <para>
5960 Run cvscompile script to re-generate the configure script and
5961 build the whole stuff again.
5962 </para>
5963 </listitem>
5964 </orderedlist>
5965 </para>
5966 </section>
5967
5968 <section>
5969 <title>Drivers with Several Source Files</title>
5970 <para>
5971 Suppose that the driver snd-xyz have several source files.
5972 They are located in the new subdirectory,
5973 pci/xyz.
5974
5975 <orderedlist>
5976 <listitem>
5977 <para>
5978 Add a new directory (<filename>xyz</filename>) in
5979 <filename>alsa-driver/pci/Makefile</filename> like below
5980
5981 <informalexample>
5982 <programlisting>
5983<![CDATA[
5984 obj-$(CONFIG_SND) += xyz/
5985]]>
5986 </programlisting>
5987 </informalexample>
5988 </para>
5989 </listitem>
5990
5991 <listitem>
5992 <para>
5993 Under the directory <filename>xyz</filename>, create a Makefile
5994
5995 <example>
5996 <title>Sample Makefile for a driver xyz</title>
5997 <programlisting>
5998<![CDATA[
5999 ifndef SND_TOPDIR
6000 SND_TOPDIR=../..
6001 endif
6002
6003 include $(SND_TOPDIR)/toplevel.config
6004 include $(SND_TOPDIR)/Makefile.conf
6005
6006 snd-xyz-objs := xyz.o abc.o def.o
6007
6008 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
6009
6010 include $(SND_TOPDIR)/Rules.make
6011]]>
6012 </programlisting>
6013 </example>
6014 </para>
6015 </listitem>
6016
6017 <listitem>
6018 <para>
6019 Create the Kconfig entry
6020 </para>
6021
6022 <para>
6023 This procedure is as same as in the last section.
6024 </para>
6025 </listitem>
6026
6027 <listitem>
6028 <para>
6029 Run cvscompile script to re-generate the configure script and
6030 build the whole stuff again.
6031 </para>
6032 </listitem>
6033 </orderedlist>
6034 </para>
6035 </section>
6036
6037 </chapter>
6038
6039<!-- ****************************************************** -->
6040<!-- Useful Functions -->
6041<!-- ****************************************************** -->
6042 <chapter id="useful-functions">
6043 <title>Useful Functions</title>
6044
6045 <section id="useful-functions-snd-printk">
6046 <title><function>snd_printk()</function> and friends</title>
6047 <para>
6048 ALSA provides a verbose version of
6049 <function>printk()</function> function. If a kernel config
6050 <constant>CONFIG_SND_VERBOSE_PRINTK</constant> is set, this
6051 function prints the given message together with the file name
6052 and the line of the caller. The <constant>KERN_XXX</constant>
6053 prefix is processed as
6054 well as the original <function>printk()</function> does, so it's
6055 recommended to add this prefix, e.g.
6056
6057 <informalexample>
6058 <programlisting>
6059<![CDATA[
6060 snd_printk(KERN_ERR "Oh my, sorry, it's extremely bad!\n");
6061]]>
6062 </programlisting>
6063 </informalexample>
6064 </para>
6065
6066 <para>
6067 There are also <function>printk()</function>'s for
6068 debugging. <function>snd_printd()</function> can be used for
6069 general debugging purposes. If
6070 <constant>CONFIG_SND_DEBUG</constant> is set, this function is
6071 compiled, and works just like
6072 <function>snd_printk()</function>. If the ALSA is compiled
6073 without the debugging flag, it's ignored.
6074 </para>
6075
6076 <para>
6077 <function>snd_printdd()</function> is compiled in only when
6078 <constant>CONFIG_SND_DEBUG_DETECT</constant> is set. Please note
6079 that <constant>DEBUG_DETECT</constant> is not set as default
6080 even if you configure the alsa-driver with
6081 <option>--with-debug=full</option> option. You need to give
6082 explicitly <option>--with-debug=detect</option> option instead.
6083 </para>
6084 </section>
6085
6086 <section id="useful-functions-snd-assert">
6087 <title><function>snd_assert()</function></title>
6088 <para>
6089 <function>snd_assert()</function> macro is similar with the
6090 normal <function>assert()</function> macro. For example,
6091
6092 <informalexample>
6093 <programlisting>
6094<![CDATA[
6095 snd_assert(pointer != NULL, return -EINVAL);
6096]]>
6097 </programlisting>
6098 </informalexample>
6099 </para>
6100
6101 <para>
6102 The first argument is the expression to evaluate, and the
6103 second argument is the action if it fails. When
6104 <constant>CONFIG_SND_DEBUG</constant>, is set, it will show an
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006105 error message such as <computeroutput>BUG? (xxx)</computeroutput>
6106 together with stack trace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 </para>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 <para>
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006109 When no debug flag is set, this macro is ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 </para>
6111 </section>
6112
6113 <section id="useful-functions-snd-bug">
6114 <title><function>snd_BUG()</function></title>
6115 <para>
Takashi Iwai7c22f1a2005-10-10 11:46:31 +02006116 It shows <computeroutput>BUG?</computeroutput> message and
6117 stack trace as well as <function>snd_assert</function> at the point.
6118 It's useful to show that a fatal error happens there.
6119 </para>
6120 <para>
6121 When no debug flag is set, this macro is ignored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 </para>
6123 </section>
6124 </chapter>
6125
6126
6127<!-- ****************************************************** -->
6128<!-- Acknowledgments -->
6129<!-- ****************************************************** -->
Nicolas Kaiser5bda9fa2007-01-22 14:54:33 +01006130 <chapter id="acknowledgments">
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 <title>Acknowledgments</title>
6132 <para>
6133 I would like to thank Phil Kerr for his help for improvement and
6134 corrections of this document.
6135 </para>
6136 <para>
6137 Kevin Conder reformatted the original plain-text to the
6138 DocBook format.
6139 </para>
6140 <para>
6141 Giuliano Pochini corrected typos and contributed the example codes
6142 in the hardware constraints section.
6143 </para>
6144 </chapter>
6145
6146
6147</book>