hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/Documentation/sound/kernel-api/writing-an-alsa-driver.rst
....@@ -3,8 +3,6 @@
33 ======================
44
55 :Author: Takashi Iwai <tiwai@suse.de>
6
-:Date: Oct 15, 2007
7
-:Edition: 0.3.7
86
97 Preface
108 =======
....@@ -21,11 +19,6 @@
2119 low-level driver implementation details. It only describes the standard
2220 way to write a PCI sound driver on ALSA.
2321
24
-If you are already familiar with the older ALSA ver.0.5.x API, you can
25
-check the drivers such as ``sound/pci/es1938.c`` or
26
-``sound/pci/maestro3.c`` which have also almost the same code-base in
27
-the ALSA 0.5.x tree, so you can compare the differences.
28
-
2922 This document is still a draft version. Any feedback and corrections,
3023 please!!
3124
....@@ -35,24 +28,7 @@
3528 General
3629 -------
3730
38
-The ALSA drivers are provided in two ways.
39
-
40
-One is the trees provided as a tarball or via cvs from the ALSA's ftp
41
-site, and another is the 2.6 (or later) Linux kernel tree. To
42
-synchronize both, the ALSA driver tree is split into two different
43
-trees: alsa-kernel and alsa-driver. The former contains purely the
44
-source code for the Linux 2.6 (or later) tree. This tree is designed
45
-only for compilation on 2.6 or later environment. The latter,
46
-alsa-driver, contains many subtle files for compiling ALSA drivers
47
-outside of the Linux kernel tree, wrapper functions for older 2.2 and
48
-2.4 kernels, to adapt the latest kernel API, and additional drivers
49
-which are still in development or in tests. The drivers in alsa-driver
50
-tree will be moved to alsa-kernel (and eventually to the 2.6 kernel
51
-tree) when they are finished and confirmed to work fine.
52
-
53
-The file tree structure of ALSA driver is depicted below. Both
54
-alsa-kernel and alsa-driver have almost the same file structure, except
55
-for “core” directory. It's named as “acore” in alsa-driver tree.
31
+The file tree structure of ALSA driver is depicted below.
5632
5733 ::
5834
....@@ -61,14 +37,11 @@
6137 /oss
6238 /seq
6339 /oss
64
- /instr
65
- /ioctl32
6640 /include
6741 /drivers
6842 /mpu401
6943 /opl3
7044 /i2c
71
- /l3
7245 /synth
7346 /emux
7447 /pci
....@@ -80,6 +53,7 @@
8053 /sparc
8154 /usb
8255 /pcmcia /(cards)
56
+ /soc
8357 /oss
8458
8559
....@@ -99,13 +73,6 @@
9973 code since it's quite small. The sequencer code is stored in
10074 ``core/seq/oss`` directory (see `below <#core-seq-oss>`__).
10175
102
-core/ioctl32
103
-~~~~~~~~~~~~
104
-
105
-This directory contains the 32bit-ioctl wrappers for 64bit architectures
106
-such like x86-64, ppc64 and sparc64. For 32bit and alpha architectures,
107
-these are not compiled.
108
-
10976 core/seq
11077 ~~~~~~~~
11178
....@@ -118,11 +85,6 @@
11885 ~~~~~~~~~~~~
11986
12087 This contains the OSS sequencer emulation codes.
121
-
122
-core/seq/instr
123
-~~~~~~~~~~~~~~
124
-
125
-This directory contains the modules for the sequencer instrument layer.
12688
12789 include directory
12890 -----------------
....@@ -160,11 +122,6 @@
160122 Although there is a standard i2c layer on Linux, ALSA has its own i2c
161123 code for some cards, because the soundcard needs only a simple operation
162124 and the standard i2c API is too complicated for such a purpose.
163
-
164
-i2c/l3
165
-~~~~~~
166
-
167
-This is a sub-directory for ARM L3 i2c.
168125
169126 synth directory
170127 ---------------
....@@ -209,11 +166,19 @@
209166 be in the pci directory, because their API is identical to that of
210167 standard PCI cards.
211168
169
+soc directory
170
+-------------
171
+
172
+This directory contains the codes for ASoC (ALSA System on Chip)
173
+layer including ASoC core, codec and machine drivers.
174
+
212175 oss directory
213176 -------------
214177
215
-The OSS/Lite source files are stored here in Linux 2.6 (or later) tree.
216
-In the ALSA driver tarball, this directory is empty, of course :)
178
+Here contains OSS/Lite codes.
179
+All codes have been deprecated except for dmasound on m68k as of
180
+writing this.
181
+
217182
218183 Basic Flow for PCI Drivers
219184 ==========================
....@@ -229,7 +194,7 @@
229194
230195 - create ``remove`` callback.
231196
232
-- create a :c:type:`struct pci_driver <pci_driver>` structure
197
+- create a struct pci_driver structure
233198 containing the three pointers above.
234199
235200 - create an ``init`` function just calling the
....@@ -294,7 +259,7 @@
294259 {
295260 struct mychip *chip;
296261 int err;
297
- static struct snd_device_ops ops = {
262
+ static const struct snd_device_ops ops = {
298263 .dev_free = snd_mychip_dev_free,
299264 };
300265
....@@ -352,38 +317,37 @@
352317
353318 /* (3) */
354319 err = snd_mychip_create(card, pci, &chip);
355
- if (err < 0) {
356
- snd_card_free(card);
357
- return err;
358
- }
320
+ if (err < 0)
321
+ goto error;
359322
360323 /* (4) */
361324 strcpy(card->driver, "My Chip");
362325 strcpy(card->shortname, "My Own Chip 123");
363326 sprintf(card->longname, "%s at 0x%lx irq %i",
364
- card->shortname, chip->ioport, chip->irq);
327
+ card->shortname, chip->port, chip->irq);
365328
366329 /* (5) */
367330 .... /* implemented later */
368331
369332 /* (6) */
370333 err = snd_card_register(card);
371
- if (err < 0) {
372
- snd_card_free(card);
373
- return err;
374
- }
334
+ if (err < 0)
335
+ goto error;
375336
376337 /* (7) */
377338 pci_set_drvdata(pci, card);
378339 dev++;
379340 return 0;
341
+
342
+ error:
343
+ snd_card_free(card);
344
+ return err;
380345 }
381346
382347 /* destructor -- see the "Destructor" sub-section */
383348 static void snd_mychip_remove(struct pci_dev *pci)
384349 {
385350 snd_card_free(pci_get_drvdata(pci));
386
- pci_set_drvdata(pci, NULL);
387351 }
388352
389353
....@@ -445,13 +409,25 @@
445409 struct mychip *chip;
446410 ....
447411 err = snd_mychip_create(card, pci, &chip);
448
- if (err < 0) {
449
- snd_card_free(card);
450
- return err;
451
- }
412
+ if (err < 0)
413
+ goto error;
452414
453415 The details will be explained in the section `PCI Resource
454416 Management`_.
417
+
418
+When something goes wrong, the probe function needs to deal with the
419
+error. In this example, we have a single error handling path placed
420
+at the end of the function.
421
+
422
+::
423
+
424
+ error:
425
+ snd_card_free(card);
426
+ return err;
427
+
428
+Since each component can be properly freed, the single
429
+:c:func:`snd_card_free()` call should suffice in most cases.
430
+
455431
456432 4) Set the driver ID and name strings.
457433 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
....@@ -461,7 +437,7 @@
461437 strcpy(card->driver, "My Chip");
462438 strcpy(card->shortname, "My Own Chip 123");
463439 sprintf(card->longname, "%s at 0x%lx irq %i",
464
- card->shortname, chip->ioport, chip->irq);
440
+ card->shortname, chip->port, chip->irq);
465441
466442 The driver field holds the minimal ID string of the chip. This is used
467443 by alsa-lib's configurator, so keep it simple but unique. Even the
....@@ -486,10 +462,8 @@
486462 ::
487463
488464 err = snd_card_register(card);
489
- if (err < 0) {
490
- snd_card_free(card);
491
- return err;
492
- }
465
+ if (err < 0)
466
+ goto error;
493467
494468 Will be explained in the section `Management of Cards and
495469 Components`_, too.
....@@ -513,14 +487,13 @@
513487 the ALSA middle layer will release all the attached components
514488 automatically.
515489
516
-It would be typically like the following:
490
+It would be typically just calling :c:func:`snd_card_free()`:
517491
518492 ::
519493
520494 static void snd_mychip_remove(struct pci_dev *pci)
521495 {
522496 snd_card_free(pci_get_drvdata(pci));
523
- pci_set_drvdata(pci, NULL);
524497 }
525498
526499
....@@ -546,7 +519,7 @@
546519 without module options don't need them.
547520
548521 In addition to these headers, you'll need ``<linux/interrupt.h>`` for
549
-interrupt handling, and ``<asm/io.h>`` for I/O access. If you use the
522
+interrupt handling, and ``<linux/io.h>`` for I/O access. If you use the
550523 :c:func:`mdelay()` or :c:func:`udelay()` functions, you'll need
551524 to include ``<linux/delay.h>`` too.
552525
....@@ -587,16 +560,15 @@
587560 card->private_data for the chip-specific data. Note that these data are
588561 allocated by :c:func:`snd_card_new()`.
589562
590
-The first argument, the pointer of struct :c:type:`struct device
591
-<device>`, specifies the parent device. For PCI devices, typically
592
-``&pci->`` is passed there.
563
+The first argument, the pointer of struct device, specifies the parent
564
+device. For PCI devices, typically ``&pci->`` is passed there.
593565
594566 Components
595567 ----------
596568
597569 After the card is created, you can attach the components (devices) to
598570 the card instance. In an ALSA driver, a component is represented as a
599
-:c:type:`struct snd_device <snd_device>` object. A component
571
+struct snd_device object. A component
600572 can be a PCM instance, a control interface, a raw MIDI interface, etc.
601573 Each such instance has one component entry.
602574
....@@ -655,7 +627,7 @@
655627 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
656628 sizeof(struct mychip), &card);
657629
658
-:c:type:`struct mychip <mychip>` is the type of the chip record.
630
+struct mychip is the type of the chip record.
659631
660632 In return, the allocated record can be accessed as
661633
....@@ -702,7 +674,7 @@
702674
703675 ::
704676
705
- static struct snd_device_ops ops = {
677
+ static const struct snd_device_ops ops = {
706678 .dev_free = snd_mychip_dev_free,
707679 };
708680 ....
....@@ -719,6 +691,13 @@
719691 }
720692
721693 where :c:func:`snd_mychip_free()` is the real destructor.
694
+
695
+The demerit of this method is the obviously more amount of codes.
696
+The merit is, however, you can trigger the own callback at registering
697
+and disconnecting the card via setting in snd_device_ops.
698
+About the registering and disconnecting the card, see the subsections
699
+below.
700
+
722701
723702 Registration and Release
724703 ------------------------
....@@ -781,7 +760,7 @@
781760 {
782761 struct mychip *chip;
783762 int err;
784
- static struct snd_device_ops ops = {
763
+ static const struct snd_device_ops ops = {
785764 .dev_free = snd_mychip_dev_free,
786765 };
787766
....@@ -825,6 +804,7 @@
825804 return -EBUSY;
826805 }
827806 chip->irq = pci->irq;
807
+ card->sync_irq = chip->irq;
828808
829809 /* (2) initialization of the chip hardware */
830810 .... /* (not implemented in this document) */
....@@ -905,13 +885,11 @@
905885 -------------------
906886
907887 The allocation of I/O ports and irqs is done via standard kernel
908
-functions. Unlike ALSA ver.0.5.x., there are no helpers for that. And
909
-these resources must be released in the destructor function (see below).
910
-Also, on ALSA 0.9.x, you don't need to allocate (pseudo-)DMA for PCI
911
-like in ALSA 0.5.x.
888
+functions. These resources must be released in the destructor
889
+function (see below).
912890
913891 Now assume that the PCI device has an I/O port with 8 bytes and an
914
-interrupt. Then :c:type:`struct mychip <mychip>` will have the
892
+interrupt. Then struct mychip will have the
915893 following fields:
916894
917895 ::
....@@ -987,6 +965,15 @@
987965 return IRQ_HANDLED;
988966 }
989967
968
+After requesting the IRQ, you can passed it to ``card->sync_irq``
969
+field:
970
+::
971
+
972
+ card->irq = chip->irq;
973
+
974
+This allows PCM core automatically performing
975
+:c:func:`synchronize_irq()` at the necessary timing like ``hw_free``.
976
+See the later section `sync_stop callback`_ for details.
990977
991978 Now let's write the corresponding destructor for the resources above.
992979 The role of destructor is simple: disable the hardware (if already
....@@ -1064,12 +1051,13 @@
10641051
10651052 ::
10661053
1067
- if ((err = pci_request_regions(pci, "My Chip")) < 0) {
1054
+ err = pci_request_regions(pci, "My Chip");
1055
+ if (err < 0) {
10681056 kfree(chip);
10691057 return err;
10701058 }
10711059 chip->iobase_phys = pci_resource_start(pci, 0);
1072
- chip->iobase_virt = ioremap_nocache(chip->iobase_phys,
1060
+ chip->iobase_virt = ioremap(chip->iobase_phys,
10731061 pci_resource_len(pci, 0));
10741062
10751063 and the corresponding destructor would be:
....@@ -1086,11 +1074,26 @@
10861074 ....
10871075 }
10881076
1077
+Of course, a modern way with :c:func:`pci_iomap()` will make things a
1078
+bit easier, too.
1079
+
1080
+::
1081
+
1082
+ err = pci_request_regions(pci, "My Chip");
1083
+ if (err < 0) {
1084
+ kfree(chip);
1085
+ return err;
1086
+ }
1087
+ chip->iobase_virt = pci_iomap(pci, 0, 0);
1088
+
1089
+which is paired with :c:func:`pci_iounmap()` at destructor.
1090
+
1091
+
10891092 PCI Entries
10901093 -----------
10911094
10921095 So far, so good. Let's finish the missing PCI stuff. At first, we need a
1093
-:c:type:`struct pci_device_id <pci_device_id>` table for
1096
+struct pci_device_id table for
10941097 this chipset. It's a table of PCI vendor/device ID number, and some
10951098 masks.
10961099
....@@ -1106,19 +1109,17 @@
11061109 };
11071110 MODULE_DEVICE_TABLE(pci, snd_mychip_ids);
11081111
1109
-The first and second fields of the :c:type:`struct pci_device_id
1110
-<pci_device_id>` structure are the vendor and device IDs. If you
1111
-have no reason to filter the matching devices, you can leave the
1112
-remaining fields as above. The last field of the :c:type:`struct
1113
-pci_device_id <pci_device_id>` struct contains private data
1114
-for this entry. You can specify any value here, for example, to define
1115
-specific operations for supported device IDs. Such an example is found
1116
-in the intel8x0 driver.
1112
+The first and second fields of the struct pci_device_id are the vendor
1113
+and device IDs. If you have no reason to filter the matching devices, you can
1114
+leave the remaining fields as above. The last field of the
1115
+struct pci_device_id contains private data for this entry. You can specify
1116
+any value here, for example, to define specific operations for supported
1117
+device IDs. Such an example is found in the intel8x0 driver.
11171118
11181119 The last entry of this list is the terminator. You must specify this
11191120 all-zero entry.
11201121
1121
-Then, prepare the :c:type:`struct pci_driver <pci_driver>`
1122
+Then, prepare the struct pci_driver
11221123 record:
11231124
11241125 ::
....@@ -1153,13 +1154,6 @@
11531154
11541155 Note that these module entries are tagged with ``__init`` and ``__exit``
11551156 prefixes.
1156
-
1157
-Oh, one thing was forgotten. If you have no exported symbols, you need
1158
-to declare it in 2.2 or 2.4 kernels (it's not necessary in 2.6 kernels).
1159
-
1160
-::
1161
-
1162
- EXPORT_NO_SYMBOLS;
11631157
11641158 That's all!
11651159
....@@ -1283,21 +1277,23 @@
12831277 /* the hardware-specific codes will be here */
12841278 ....
12851279 return 0;
1286
-
12871280 }
12881281
12891282 /* hw_params callback */
12901283 static int snd_mychip_pcm_hw_params(struct snd_pcm_substream *substream,
12911284 struct snd_pcm_hw_params *hw_params)
12921285 {
1293
- return snd_pcm_lib_malloc_pages(substream,
1294
- params_buffer_bytes(hw_params));
1286
+ /* the hardware-specific codes will be here */
1287
+ ....
1288
+ return 0;
12951289 }
12961290
12971291 /* hw_free callback */
12981292 static int snd_mychip_pcm_hw_free(struct snd_pcm_substream *substream)
12991293 {
1300
- return snd_pcm_lib_free_pages(substream);
1294
+ /* the hardware-specific codes will be here */
1295
+ ....
1296
+ return 0;
13011297 }
13021298
13031299 /* prepare callback */
....@@ -1352,7 +1348,6 @@
13521348 static struct snd_pcm_ops snd_mychip_playback_ops = {
13531349 .open = snd_mychip_playback_open,
13541350 .close = snd_mychip_playback_close,
1355
- .ioctl = snd_pcm_lib_ioctl,
13561351 .hw_params = snd_mychip_pcm_hw_params,
13571352 .hw_free = snd_mychip_pcm_hw_free,
13581353 .prepare = snd_mychip_pcm_prepare,
....@@ -1364,7 +1359,6 @@
13641359 static struct snd_pcm_ops snd_mychip_capture_ops = {
13651360 .open = snd_mychip_capture_open,
13661361 .close = snd_mychip_capture_close,
1367
- .ioctl = snd_pcm_lib_ioctl,
13681362 .hw_params = snd_mychip_pcm_hw_params,
13691363 .hw_free = snd_mychip_pcm_hw_free,
13701364 .prepare = snd_mychip_pcm_prepare,
....@@ -1395,9 +1389,9 @@
13951389 &snd_mychip_capture_ops);
13961390 /* pre-allocation of buffers */
13971391 /* NOTE: this may fail */
1398
- snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1399
- snd_dma_pci_data(chip->pci),
1400
- 64*1024, 64*1024);
1392
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1393
+ &chip->pci->dev,
1394
+ 64*1024, 64*1024);
14011395 return 0;
14021396 }
14031397
....@@ -1442,8 +1436,8 @@
14421436 If a chip supports multiple playbacks or captures, you can specify more
14431437 numbers, but they must be handled properly in open/close, etc.
14441438 callbacks. When you need to know which substream you are referring to,
1445
-then it can be obtained from :c:type:`struct snd_pcm_substream
1446
-<snd_pcm_substream>` data passed to each callback as follows:
1439
+then it can be obtained from struct snd_pcm_substream data passed to each
1440
+callback as follows:
14471441
14481442 ::
14491443
....@@ -1467,7 +1461,6 @@
14671461 static struct snd_pcm_ops snd_mychip_playback_ops = {
14681462 .open = snd_mychip_pcm_open,
14691463 .close = snd_mychip_pcm_close,
1470
- .ioctl = snd_pcm_lib_ioctl,
14711464 .hw_params = snd_mychip_pcm_hw_params,
14721465 .hw_free = snd_mychip_pcm_hw_free,
14731466 .prepare = snd_mychip_pcm_prepare,
....@@ -1478,13 +1471,14 @@
14781471 All the callbacks are described in the Operators_ subsection.
14791472
14801473 After setting the operators, you probably will want to pre-allocate the
1481
-buffer. For the pre-allocation, simply call the following:
1474
+buffer and set up the managed allocation mode.
1475
+For that, simply call the following:
14821476
14831477 ::
14841478
1485
- snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1486
- snd_dma_pci_data(chip->pci),
1487
- 64*1024, 64*1024);
1479
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1480
+ &chip->pci->dev,
1481
+ 64*1024, 64*1024);
14881482
14891483 It will allocate a buffer up to 64kB as default. Buffer management
14901484 details will be described in the later section `Buffer and Memory
....@@ -1634,8 +1628,7 @@
16341628 records are supposed to be read-only. Only the PCM middle-layer changes
16351629 / updates them. The exceptions are the hardware description (hw) DMA
16361630 buffer information and the private data. Besides, if you use the
1637
-standard buffer allocation method via
1638
-:c:func:`snd_pcm_lib_malloc_pages()`, you don't need to set the
1631
+standard managed buffer allocation mode, you don't need to set the
16391632 DMA buffer information by yourself.
16401633
16411634 In the sections below, important records are explained.
....@@ -1643,10 +1636,9 @@
16431636 Hardware Description
16441637 ~~~~~~~~~~~~~~~~~~~~
16451638
1646
-The hardware descriptor (:c:type:`struct snd_pcm_hardware
1647
-<snd_pcm_hardware>`) contains the definitions of the fundamental
1648
-hardware configuration. Above all, you'll need to define this in the
1649
-`PCM open callback`_. Note that the runtime instance holds the copy of
1639
+The hardware descriptor (struct snd_pcm_hardware) contains the definitions of
1640
+the fundamental hardware configuration. Above all, you'll need to define this
1641
+in the `PCM open callback`_. Note that the runtime instance holds the copy of
16501642 the descriptor, not the pointer to the existing descriptor. That is,
16511643 in the open callback, you can modify the copied descriptor
16521644 (``runtime->hw``) as you need. For example, if the maximum number of
....@@ -1789,8 +1781,8 @@
17891781 the buffer is a linear buffer. ``dma_bytes`` holds the size of buffer
17901782 in bytes. ``dma_private`` is used for the ALSA DMA allocator.
17911783
1792
-If you use a standard ALSA function,
1793
-:c:func:`snd_pcm_lib_malloc_pages()`, for allocating the buffer,
1784
+If you use either the managed buffer allocation mode or the standard
1785
+API function :c:func:`snd_pcm_lib_malloc_pages()` for allocating the buffer,
17941786 these fields are set by the ALSA middle layer, and you should *not*
17951787 change them by yourself. You can read them but not write them. On the
17961788 other hand, if you want to allocate the buffer by yourself, you'll
....@@ -1804,14 +1796,13 @@
18041796 ~~~~~~~~~~~~~~
18051797
18061798 The running status can be referred via ``runtime->status``. This is
1807
-the pointer to the :c:type:`struct snd_pcm_mmap_status
1808
-<snd_pcm_mmap_status>` record. For example, you can get the current
1799
+the pointer to the struct snd_pcm_mmap_status record.
1800
+For example, you can get the current
18091801 DMA hardware pointer via ``runtime->status->hw_ptr``.
18101802
18111803 The DMA application pointer can be referred via ``runtime->control``,
1812
-which points to the :c:type:`struct snd_pcm_mmap_control
1813
-<snd_pcm_mmap_control>` record. However, accessing directly to
1814
-this value is not recommended.
1804
+which points to the struct snd_pcm_mmap_control record.
1805
+However, accessing directly to this value is not recommended.
18151806
18161807 Private Data
18171808 ~~~~~~~~~~~~
....@@ -1847,8 +1838,8 @@
18471838 number, it is advised to check what value other parts of the kernel
18481839 return when the same kind of request fails.
18491840
1850
-The callback function takes at least the argument with :c:type:`struct
1851
-snd_pcm_substream <snd_pcm_substream>` pointer. To retrieve the chip
1841
+The callback function takes at least the argument with
1842
+struct snd_pcm_substream pointer. To retrieve the chip
18521843 record from the given substream instance, you can use the following
18531844 macro.
18541845
....@@ -1924,7 +1915,10 @@
19241915 ~~~~~~~~~~~~~~
19251916
19261917 This is used for any special call to pcm ioctls. But usually you can
1927
-pass a generic ioctl callback, :c:func:`snd_pcm_lib_ioctl()`.
1918
+leave it as NULL, then PCM core calls the generic ioctl callback
1919
+function :c:func:`snd_pcm_lib_ioctl()`. If you need to deal with the
1920
+unique setup of channel info or reset procedure, you can pass your own
1921
+callback function here.
19281922
19291923 hw_params callback
19301924 ~~~~~~~~~~~~~~~~~~~
....@@ -1942,8 +1936,12 @@
19421936 allocation of buffers.
19431937
19441938 Parameters to be initialized are retrieved by
1945
-:c:func:`params_xxx()` macros. To allocate buffer, you can call a
1946
-helper function,
1939
+:c:func:`params_xxx()` macros.
1940
+
1941
+When you set up the managed buffer allocation mode for the substream,
1942
+a buffer is already allocated before this callback gets
1943
+called. Alternatively, you can call a helper function below for
1944
+allocating the buffer, too.
19471945
19481946 ::
19491947
....@@ -1977,17 +1975,22 @@
19771975 static int snd_xxx_hw_free(struct snd_pcm_substream *substream);
19781976
19791977 This is called to release the resources allocated via
1980
-``hw_params``. For example, releasing the buffer via
1981
-:c:func:`snd_pcm_lib_malloc_pages()` is done by calling the
1982
-following:
1983
-
1984
-::
1985
-
1986
- snd_pcm_lib_free_pages(substream);
1978
+``hw_params``.
19871979
19881980 This function is always called before the close callback is called.
19891981 Also, the callback may be called multiple times, too. Keep track
19901982 whether the resource was already released.
1983
+
1984
+When you have set up the managed buffer allocation mode for the PCM
1985
+substream, the allocated PCM buffer will be automatically released
1986
+after this callback gets called. Otherwise you'll have to release the
1987
+buffer manually. Typically, when the buffer was allocated from the
1988
+pre-allocated pool, you can use the standard API function
1989
+:c:func:`snd_pcm_lib_malloc_pages()` like:
1990
+
1991
+::
1992
+
1993
+ snd_pcm_lib_free_pages(substream);
19911994
19921995 prepare callback
19931996 ~~~~~~~~~~~~~~~~
....@@ -2061,6 +2064,37 @@
20612064 triggering the DMA. The other stuff should be initialized
20622065 ``hw_params`` and ``prepare`` callbacks properly beforehand.
20632066
2067
+sync_stop callback
2068
+~~~~~~~~~~~~~~~~~~
2069
+
2070
+::
2071
+
2072
+ static int snd_xxx_sync_stop(struct snd_pcm_substream *substream);
2073
+
2074
+This callback is optional, and NULL can be passed. It's called after
2075
+the PCM core stops the stream and changes the stream state
2076
+``prepare``, ``hw_params`` or ``hw_free``.
2077
+Since the IRQ handler might be still pending, we need to wait until
2078
+the pending task finishes before moving to the next step; otherwise it
2079
+might lead to a crash due to resource conflicts or access to the freed
2080
+resources. A typical behavior is to call a synchronization function
2081
+like :c:func:`synchronize_irq()` here.
2082
+
2083
+For majority of drivers that need only a call of
2084
+:c:func:`synchronize_irq()`, there is a simpler setup, too.
2085
+While keeping NULL to ``sync_stop`` PCM callback, the driver can set
2086
+``card->sync_irq`` field to store the valid interrupt number after
2087
+requesting an IRQ, instead. Then PCM core will look call
2088
+:c:func:`synchronize_irq()` with the given IRQ appropriately.
2089
+
2090
+If the IRQ handler is released at the card destructor, you don't need
2091
+to clear ``card->sync_irq``, as the card itself is being released.
2092
+So, usually you'll need to add just a single line for assigning
2093
+``card->sync_irq`` in the driver code unless the driver re-acquires
2094
+the IRQ. When the driver frees and re-acquires the IRQ dynamically
2095
+(e.g. for suspend/resume), it needs to clear and re-set
2096
+``card->sync_irq`` again appropriately.
2097
+
20642098 pointer callback
20652099 ~~~~~~~~~~~~~~~~
20662100
....@@ -2108,10 +2142,22 @@
21082142 page callback
21092143 ~~~~~~~~~~~~~
21102144
2111
-This callback is optional too. This callback is used mainly for
2112
-non-contiguous buffers. The mmap calls this callback to get the page
2113
-address. Some examples will be explained in the later section `Buffer
2114
-and Memory Management`_, too.
2145
+This callback is optional too. The mmap calls this callback to get the
2146
+page fault address.
2147
+
2148
+Since the recent changes, you need no special callback any longer for
2149
+the standard SG-buffer or vmalloc-buffer. Hence this callback should
2150
+be rarely used.
2151
+
2152
+mmap calllback
2153
+~~~~~~~~~~~~~~
2154
+
2155
+This is another optional callback for controlling mmap behavior.
2156
+Once when defined, PCM core calls this callback when a page is
2157
+memory-mapped instead of dealing via the standard helper.
2158
+If you need special handling (due to some architecture or
2159
+device-specific issues), implement everything here as you like.
2160
+
21152161
21162162 PCM Interrupt Handler
21172163 ---------------------
....@@ -2262,10 +2308,10 @@
22622308 :c:func:`snd_pcm_period_elapsed()` is called typically from the
22632309 interrupt handler. But, if you set up the driver to use a threaded
22642310 interrupt handler, this call can be in non-atomic context, too. In such
2265
-a case, you can set ``nonatomic`` filed of :c:type:`struct snd_pcm
2266
-<snd_pcm>` object after creating it. When this flag is set, mutex
2267
-and rwsem are used internally in the PCM core instead of spin and
2268
-rwlocks, so that you can call all PCM functions safely in a non-atomic
2311
+a case, you can set ``nonatomic`` filed of struct snd_pcm object
2312
+after creating it. When this flag is set, mutex and rwsem are used internally
2313
+in the PCM core instead of spin and rwlocks, so that you can call all PCM
2314
+functions safely in a non-atomic
22692315 context.
22702316
22712317 Constraints
....@@ -2306,8 +2352,7 @@
23062352 complete list. You can even define your own constraint rules. For
23072353 example, let's suppose my_chip can manage a substream of 1 channel if
23082354 and only if the format is ``S16_LE``, otherwise it supports any format
2309
-specified in the :c:type:`struct snd_pcm_hardware
2310
-<snd_pcm_hardware>` structure (or in any other
2355
+specified in struct snd_pcm_hardware> (or in any other
23112356 constraint_list). You can build a rule like this:
23122357
23132358 ::
....@@ -2370,6 +2415,27 @@
23702415 hw_rule_format_by_channels, NULL,
23712416 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
23722417
2418
+One typical usage of the hw constraints is to align the buffer size
2419
+with the period size. As default, ALSA PCM core doesn't enforce the
2420
+buffer size to be aligned with the period size. For example, it'd be
2421
+possible to have a combination like 256 period bytes with 999 buffer
2422
+bytes.
2423
+
2424
+Many device chips, however, require the buffer to be a multiple of
2425
+periods. In such a case, call
2426
+:c:func:`snd_pcm_hw_constraint_integer()` for
2427
+``SNDRV_PCM_HW_PARAM_PERIODS``.
2428
+
2429
+::
2430
+
2431
+ snd_pcm_hw_constraint_integer(substream->runtime,
2432
+ SNDRV_PCM_HW_PARAM_PERIODS);
2433
+
2434
+This assures that the number of periods is integer, hence the buffer
2435
+size is aligned with the period size.
2436
+
2437
+The hw constraint is a very much powerful mechanism to define the
2438
+preferred PCM configuration, and there are relevant helpers.
23732439 I won't give more details here, rather I would like to say, “Luke, use
23742440 the source.”
23752441
....@@ -2395,7 +2461,7 @@
23952461
23962462 To create a new control, you need to define the following three
23972463 callbacks: ``info``, ``get`` and ``put``. Then, define a
2398
-:c:type:`struct snd_kcontrol_new <snd_kcontrol_new>` record, such as:
2464
+struct snd_kcontrol_new record, such as:
23992465
24002466 ::
24012467
....@@ -2530,8 +2596,8 @@
25302596 ~~~~~~~~~~~~~
25312597
25322598 The ``info`` callback is used to get detailed information on this
2533
-control. This must store the values of the given :c:type:`struct
2534
-snd_ctl_elem_info <snd_ctl_elem_info>` object. For example,
2599
+control. This must store the values of the given
2600
+struct snd_ctl_elem_info object. For example,
25352601 for a boolean control with a single element:
25362602
25372603 ::
....@@ -2702,13 +2768,11 @@
27022768 if (err < 0)
27032769 return err;
27042770
2705
-where ``my_control`` is the :c:type:`struct snd_kcontrol_new
2706
-<snd_kcontrol_new>` object defined above, and chip is the object
2707
-pointer to be passed to kcontrol->private_data which can be referred
2708
-to in callbacks.
2771
+where ``my_control`` is the struct snd_kcontrol_new object defined above,
2772
+and chip is the object pointer to be passed to kcontrol->private_data which
2773
+can be referred to in callbacks.
27092774
2710
-:c:func:`snd_ctl_new1()` allocates a new :c:type:`struct
2711
-snd_kcontrol <snd_kcontrol>` instance, and
2775
+:c:func:`snd_ctl_new1()` allocates a new struct snd_kcontrol instance, and
27122776 :c:func:`snd_ctl_add()` assigns the given control component to the
27132777 card.
27142778
....@@ -2725,10 +2789,9 @@
27252789 This function takes the card pointer, the event-mask, and the control id
27262790 pointer for the notification. The event-mask specifies the types of
27272791 notification, for example, in the above example, the change of control
2728
-values is notified. The id pointer is the pointer of :c:type:`struct
2729
-snd_ctl_elem_id <snd_ctl_elem_id>` to be notified. You can
2730
-find some examples in ``es1938.c`` or ``es1968.c`` for hardware volume
2731
-interrupts.
2792
+values is notified. The id pointer is the pointer of struct snd_ctl_elem_id
2793
+to be notified. You can find some examples in ``es1938.c`` or ``es1968.c``
2794
+for hardware volume interrupts.
27322795
27332796 Metadata
27342797 --------
....@@ -2843,9 +2906,8 @@
28432906
28442907 The bus record is shared among all belonging ac97 instances.
28452908
2846
-And then call :c:func:`snd_ac97_mixer()` with an :c:type:`struct
2847
-snd_ac97_template <snd_ac97_template>` record together with
2848
-the bus pointer created above.
2909
+And then call :c:func:`snd_ac97_mixer()` with an struct snd_ac97_template
2910
+record together with the bus pointer created above.
28492911
28502912 ::
28512913
....@@ -3046,11 +3108,10 @@
30463108
30473109 Usually, the port address corresponds to the command port and port + 1
30483110 corresponds to the data port. If not, you may change the ``cport``
3049
-field of :c:type:`struct snd_mpu401 <snd_mpu401>` manually afterward.
3050
-However, :c:type:`struct snd_mpu401 <snd_mpu401>` pointer is
3111
+field of struct snd_mpu401 manually afterward.
3112
+However, struct snd_mpu401 pointer is
30513113 not returned explicitly by :c:func:`snd_mpu401_uart_new()`. You
3052
-need to cast ``rmidi->private_data`` to :c:type:`struct snd_mpu401
3053
-<snd_mpu401>` explicitly,
3114
+need to cast ``rmidi->private_data`` to struct snd_mpu401 explicitly,
30543115
30553116 ::
30563117
....@@ -3254,8 +3315,7 @@
32543315 }
32553316
32563317 If you know beforehand how many bytes you can accept, you can use a
3257
-buffer size greater than one with the
3258
-:c:func:`snd_rawmidi_transmit\*()` functions.
3318
+buffer size greater than one with the ``snd_rawmidi_transmit*()`` functions.
32593319
32603320 The ``trigger`` callback must not sleep. If the hardware FIFO is full
32613321 before the substream buffer has been emptied, you have to continue
....@@ -3448,14 +3508,15 @@
34483508
34493509 “IEC958 Playback Con Mask” is used to return the bit-mask for the IEC958
34503510 status bits of consumer mode. Similarly, “IEC958 Playback Pro Mask”
3451
-returns the bitmask for professional mode. They are read-only controls,
3452
-and are defined as MIXER controls (iface =
3453
-``SNDRV_CTL_ELEM_IFACE_MIXER``).
3511
+returns the bitmask for professional mode. They are read-only controls.
34543512
34553513 Meanwhile, “IEC958 Playback Default” control is defined for getting and
3456
-setting the current default IEC958 bits. Note that this one is usually
3457
-defined as a PCM control (iface = ``SNDRV_CTL_ELEM_IFACE_PCM``),
3458
-although in some places it's defined as a MIXER control.
3514
+setting the current default IEC958 bits.
3515
+
3516
+Due to historical reasons, both variants of the Playback Mask and the
3517
+Playback Default controls can be implemented on either a
3518
+``SNDRV_CTL_ELEM_IFACE_PCM`` or a ``SNDRV_CTL_ELEM_IFACE_MIXER`` iface.
3519
+Drivers should expose the mask and default on the same iface though.
34593520
34603521 In addition, you can define the control switches to enable/disable or to
34613522 set the raw bit mode. The implementation will depend on the chip, but
....@@ -3494,7 +3555,7 @@
34943555 ::
34953556
34963557 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
3497
- snd_dma_pci_data(pci), size, max);
3558
+ &pci->dev, size, max);
34983559
34993560 where ``size`` is the byte size to be pre-allocated and the ``max`` is
35003561 the maximum size to be changed via the ``prealloc`` proc file. The
....@@ -3502,15 +3563,17 @@
35023563 given size.
35033564
35043565 The second argument (type) and the third argument (device pointer) are
3505
-dependent on the bus. In the case of the ISA bus, pass
3506
-:c:func:`snd_dma_isa_data()` as the third argument with
3566
+dependent on the bus. For normal devices, pass the device pointer
3567
+(typically identical as ``card->dev``) to the third argument with
35073568 ``SNDRV_DMA_TYPE_DEV`` type. For the continuous buffer unrelated to the
3508
-bus can be pre-allocated with ``SNDRV_DMA_TYPE_CONTINUOUS`` type and the
3509
-``snd_dma_continuous_data(GFP_KERNEL)`` device pointer, where
3510
-``GFP_KERNEL`` is the kernel allocation flag to use. For the PCI
3511
-scatter-gather buffers, use ``SNDRV_DMA_TYPE_DEV_SG`` with
3512
-``snd_dma_pci_data(pci)`` (see the `Non-Contiguous Buffers`_
3513
-section).
3569
+bus can be pre-allocated with ``SNDRV_DMA_TYPE_CONTINUOUS`` type.
3570
+You can pass NULL to the device pointer in that case, which is the
3571
+default mode implying to allocate with ``GFP_KERNEL`` flag.
3572
+If you need a different GFP flag, you can pass it by encoding the flag
3573
+into the device pointer via a special macro
3574
+:c:func:`snd_dma_continuous_data()`.
3575
+For the scatter-gather buffers, use ``SNDRV_DMA_TYPE_DEV_SG`` with the
3576
+device pointer (see the `Non-Contiguous Buffers`_ section).
35143577
35153578 Once the buffer is pre-allocated, you can use the allocator in the
35163579 ``hw_params`` callback:
....@@ -3520,6 +3583,25 @@
35203583 snd_pcm_lib_malloc_pages(substream, size);
35213584
35223585 Note that you have to pre-allocate to use this function.
3586
+
3587
+Most of drivers use, though, rather the newly introduced "managed
3588
+buffer allocation mode" instead of the manual allocation or release.
3589
+This is done by calling :c:func:`snd_pcm_set_managed_buffer_all()`
3590
+instead of :c:func:`snd_pcm_lib_preallocate_pages_for_all()`.
3591
+
3592
+::
3593
+
3594
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
3595
+ &pci->dev, size, max);
3596
+
3597
+where passed arguments are identical in both functions.
3598
+The difference in the managed mode is that PCM core will call
3599
+:c:func:`snd_pcm_lib_malloc_pages()` internally already before calling
3600
+the PCM ``hw_params`` callback, and call :c:func:`snd_pcm_lib_free_pages()`
3601
+after the PCM ``hw_free`` callback automatically. So the driver
3602
+doesn't have to call these functions explicitly in its callback any
3603
+longer. This made many driver code having NULL ``hw_params`` and
3604
+``hw_free`` entries.
35233605
35243606 External Hardware Buffers
35253607 -------------------------
....@@ -3675,20 +3757,26 @@
36753757 ``<sound/pcm.h>``.
36763758
36773759 For creating the SG-buffer handler, call
3678
-:c:func:`snd_pcm_lib_preallocate_pages()` or
3679
-:c:func:`snd_pcm_lib_preallocate_pages_for_all()` with
3760
+:c:func:`snd_pcm_set_managed_buffer()` or
3761
+:c:func:`snd_pcm_set_managed_buffer_all()` with
36803762 ``SNDRV_DMA_TYPE_DEV_SG`` in the PCM constructor like other PCI
3681
-pre-allocator. You need to pass ``snd_dma_pci_data(pci)``, where pci is
3682
-the :c:type:`struct pci_dev <pci_dev>` pointer of the chip as
3683
-well. The ``struct snd_sg_buf`` instance is created as
3684
-``substream->dma_private``. You can cast the pointer like:
3763
+pre-allocator. You need to pass ``&pci->dev``, where pci is
3764
+the struct pci_dev pointer of the chip as
3765
+well.
3766
+
3767
+::
3768
+
3769
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
3770
+ &pci->dev, size, max);
3771
+
3772
+The ``struct snd_sg_buf`` instance is created as
3773
+``substream->dma_private`` in turn. You can cast the pointer like:
36853774
36863775 ::
36873776
36883777 struct snd_sg_buf *sgbuf = (struct snd_sg_buf *)substream->dma_private;
36893778
3690
-Then call :c:func:`snd_pcm_lib_malloc_pages()` in the ``hw_params``
3691
-callback as well as in the case of normal PCI buffer. The SG-buffer
3779
+Then in :c:func:`snd_pcm_lib_malloc_pages()` call, the common SG-buffer
36923780 handler will allocate the non-contiguous kernel pages of the given size
36933781 and map them onto the virtually contiguous memory. The virtual pointer
36943782 is addressed in runtime->dma_area. The physical address
....@@ -3697,34 +3785,40 @@
36973785 ``sgbuf->table``. You can get the physical address at a certain offset
36983786 via :c:func:`snd_pcm_sgbuf_get_addr()`.
36993787
3700
-When a SG-handler is used, you need to set
3701
-:c:func:`snd_pcm_sgbuf_ops_page()` as the ``page`` callback. (See
3702
-`page callback`_ section.)
3703
-
3704
-To release the data, call :c:func:`snd_pcm_lib_free_pages()` in
3705
-the ``hw_free`` callback as usual.
3788
+If you need to release the SG-buffer data explicitly, call the
3789
+standard API function :c:func:`snd_pcm_lib_free_pages()` as usual.
37063790
37073791 Vmalloc'ed Buffers
37083792 ------------------
37093793
37103794 It's possible to use a buffer allocated via :c:func:`vmalloc()`, for
3711
-example, for an intermediate buffer. Since the allocated pages are not
3712
-contiguous, you need to set the ``page`` callback to obtain the physical
3713
-address at every offset.
3714
-
3715
-The implementation of ``page`` callback would be like this:
3795
+example, for an intermediate buffer. In the recent version of kernel,
3796
+you can simply allocate it via standard
3797
+:c:func:`snd_pcm_lib_malloc_pages()` and co after setting up the
3798
+buffer preallocation with ``SNDRV_DMA_TYPE_VMALLOC`` type.
37163799
37173800 ::
37183801
3719
- #include <linux/vmalloc.h>
3802
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
3803
+ NULL, 0, 0);
37203804
3721
- /* get the physical page pointer on the given offset */
3722
- static struct page *mychip_page(struct snd_pcm_substream *substream,
3723
- unsigned long offset)
3724
- {
3725
- void *pageptr = substream->runtime->dma_area + offset;
3726
- return vmalloc_to_page(pageptr);
3727
- }
3805
+The NULL is passed to the device pointer argument, which indicates
3806
+that the default pages (GFP_KERNEL and GFP_HIGHMEM) will be
3807
+allocated.
3808
+
3809
+Also, note that zero is passed to both the size and the max size
3810
+arguments here. Since each vmalloc call should succeed at any time,
3811
+we don't need to pre-allocate the buffers like other continuous
3812
+pages.
3813
+
3814
+If you need the 32bit DMA allocation, pass the device pointer encoded
3815
+by :c:func:`snd_dma_continuous_data()` with ``GFP_KERNEL|__GFP_DMA32``
3816
+argument.
3817
+
3818
+::
3819
+
3820
+ snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
3821
+ snd_dma_continuous_data(GFP_KERNEL | __GFP_DMA32), 0, 0);
37283822
37293823 Proc Interface
37303824 ==============
....@@ -3807,7 +3901,7 @@
38073901
38083902 ::
38093903
3810
- static struct snd_info_entry_ops my_file_io_ops = {
3904
+ static const struct snd_info_entry_ops my_file_io_ops = {
38113905 .read = my_file_io_read,
38123906 };
38133907
....@@ -3822,7 +3916,7 @@
38223916
38233917 The read/write callbacks of raw mode are more direct than the text mode.
38243918 You need to use a low-level I/O functions such as
3825
-:c:func:`copy_from/to_user()` to transfer the data.
3919
+:c:func:`copy_from_user()` and :c:func:`copy_to_user()` to transfer the data.
38263920
38273921 ::
38283922
....@@ -3848,7 +3942,9 @@
38483942
38493943 If the chip is supposed to work with suspend/resume functions, you need
38503944 to add power-management code to the driver. The additional code for
3851
-power-management should be ifdef-ed with ``CONFIG_PM``.
3945
+power-management should be ifdef-ed with ``CONFIG_PM``, or annotated
3946
+with __maybe_unused attribute; otherwise the compiler will complain
3947
+you.
38523948
38533949 If the driver *fully* supports suspend/resume that is, the device can be
38543950 properly resumed to its state when suspend was called, you can set the
....@@ -3879,18 +3975,16 @@
38793975
38803976 ::
38813977
3882
- #ifdef CONFIG_PM
3883
- static int snd_my_suspend(struct pci_dev *pci, pm_message_t state)
3978
+ static int __maybe_unused snd_my_suspend(struct device *dev)
38843979 {
38853980 .... /* do things for suspend */
38863981 return 0;
38873982 }
3888
- static int snd_my_resume(struct pci_dev *pci)
3983
+ static int __maybe_unused snd_my_resume(struct device *dev)
38893984 {
38903985 .... /* do things for suspend */
38913986 return 0;
38923987 }
3893
- #endif
38943988
38953989 The scheme of the real suspend job is as follows.
38963990
....@@ -3899,42 +3993,30 @@
38993993 2. Call :c:func:`snd_power_change_state()` with
39003994 ``SNDRV_CTL_POWER_D3hot`` to change the power status.
39013995
3902
-3. Call :c:func:`snd_pcm_suspend_all()` to suspend the running
3903
- PCM streams.
3904
-
3905
-4. If AC97 codecs are used, call :c:func:`snd_ac97_suspend()` for
3996
+3. If AC97 codecs are used, call :c:func:`snd_ac97_suspend()` for
39063997 each codec.
39073998
3908
-5. Save the register values if necessary.
3999
+4. Save the register values if necessary.
39094000
3910
-6. Stop the hardware if necessary.
3911
-
3912
-7. Disable the PCI device by calling
3913
- :c:func:`pci_disable_device()`. Then, call
3914
- :c:func:`pci_save_state()` at last.
4001
+5. Stop the hardware if necessary.
39154002
39164003 A typical code would be like:
39174004
39184005 ::
39194006
3920
- static int mychip_suspend(struct pci_dev *pci, pm_message_t state)
4007
+ static int __maybe_unused mychip_suspend(struct device *dev)
39214008 {
39224009 /* (1) */
3923
- struct snd_card *card = pci_get_drvdata(pci);
4010
+ struct snd_card *card = dev_get_drvdata(dev);
39244011 struct mychip *chip = card->private_data;
39254012 /* (2) */
39264013 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
39274014 /* (3) */
3928
- snd_pcm_suspend_all(chip->pcm);
3929
- /* (4) */
39304015 snd_ac97_suspend(chip->ac97);
3931
- /* (5) */
4016
+ /* (4) */
39324017 snd_mychip_save_registers(chip);
3933
- /* (6) */
4018
+ /* (5) */
39344019 snd_mychip_stop_hardware(chip);
3935
- /* (7) */
3936
- pci_disable_device(pci);
3937
- pci_save_state(pci);
39384020 return 0;
39394021 }
39404022
....@@ -3943,55 +4025,42 @@
39434025
39444026 1. Retrieve the card and the chip data.
39454027
3946
-2. Set up PCI. First, call :c:func:`pci_restore_state()`. Then
3947
- enable the pci device again by calling
3948
- :c:func:`pci_enable_device()`. Call
3949
- :c:func:`pci_set_master()` if necessary, too.
4028
+2. Re-initialize the chip.
39504029
3951
-3. Re-initialize the chip.
4030
+3. Restore the saved registers if necessary.
39524031
3953
-4. Restore the saved registers if necessary.
4032
+4. Resume the mixer, e.g. calling :c:func:`snd_ac97_resume()`.
39544033
3955
-5. Resume the mixer, e.g. calling :c:func:`snd_ac97_resume()`.
4034
+5. Restart the hardware (if any).
39564035
3957
-6. Restart the hardware (if any).
3958
-
3959
-7. Call :c:func:`snd_power_change_state()` with
4036
+6. Call :c:func:`snd_power_change_state()` with
39604037 ``SNDRV_CTL_POWER_D0`` to notify the processes.
39614038
39624039 A typical code would be like:
39634040
39644041 ::
39654042
3966
- static int mychip_resume(struct pci_dev *pci)
4043
+ static int __maybe_unused mychip_resume(struct pci_dev *pci)
39674044 {
39684045 /* (1) */
3969
- struct snd_card *card = pci_get_drvdata(pci);
4046
+ struct snd_card *card = dev_get_drvdata(dev);
39704047 struct mychip *chip = card->private_data;
39714048 /* (2) */
3972
- pci_restore_state(pci);
3973
- pci_enable_device(pci);
3974
- pci_set_master(pci);
3975
- /* (3) */
39764049 snd_mychip_reinit_chip(chip);
3977
- /* (4) */
4050
+ /* (3) */
39784051 snd_mychip_restore_registers(chip);
3979
- /* (5) */
4052
+ /* (4) */
39804053 snd_ac97_resume(chip->ac97);
3981
- /* (6) */
4054
+ /* (5) */
39824055 snd_mychip_restart_chip(chip);
3983
- /* (7) */
4056
+ /* (6) */
39844057 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
39854058 return 0;
39864059 }
39874060
3988
-As shown in the above, it's better to save registers after suspending
3989
-the PCM operations via :c:func:`snd_pcm_suspend_all()` or
3990
-:c:func:`snd_pcm_suspend()`. It means that the PCM streams are
3991
-already stopped when the register snapshot is taken. But, remember that
3992
-you don't have to restart the PCM stream in the resume callback. It'll
3993
-be restarted via trigger call with ``SNDRV_PCM_TRIGGER_RESUME`` when
3994
-necessary.
4061
+Note that, at the time this callback gets called, the PCM stream has
4062
+been already suspended via its own PM ops calling
4063
+:c:func:`snd_pcm_suspend_all()` internally.
39954064
39964065 OK, we have all callbacks now. Let's set them up. In the initialization
39974066 of the card, make sure that you can get the chip data from the card
....@@ -4046,15 +4115,14 @@
40464115
40474116 ::
40484117
4118
+ static SIMPLE_DEV_PM_OPS(snd_my_pm_ops, mychip_suspend, mychip_resume);
4119
+
40494120 static struct pci_driver driver = {
40504121 .name = KBUILD_MODNAME,
40514122 .id_table = snd_my_ids,
40524123 .probe = snd_my_probe,
40534124 .remove = snd_my_remove,
4054
- #ifdef CONFIG_PM
4055
- .suspend = snd_my_suspend,
4056
- .resume = snd_my_resume,
4057
- #endif
4125
+ .driver.pm = &snd_my_pm_ops,
40584126 };
40594127
40604128 Module Parameters
....@@ -4078,7 +4146,7 @@
40784146 case, but it would be better to have a dummy option for compatibility.
40794147
40804148 The module parameters must be declared with the standard
4081
-``module_param()()``, ``module_param_array()()`` and
4149
+``module_param()``, ``module_param_array()`` and
40824150 :c:func:`MODULE_PARM_DESC()` macros.
40834151
40844152 The typical coding would be like below:
....@@ -4094,15 +4162,14 @@
40944162 module_param_array(enable, bool, NULL, 0444);
40954163 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
40964164
4097
-Also, don't forget to define the module description, classes, license
4098
-and devices. Especially, the recent modprobe requires to define the
4165
+Also, don't forget to define the module description and the license.
4166
+Especially, the recent modprobe requires to define the
40994167 module license as GPL, etc., otherwise the system is shown as “tainted”.
41004168
41014169 ::
41024170
4103
- MODULE_DESCRIPTION("My Chip");
4171
+ MODULE_DESCRIPTION("Sound driver for My Chip");
41044172 MODULE_LICENSE("GPL");
4105
- MODULE_SUPPORTED_DEVICE("{{Vendor,My Chip Name}}");
41064173
41074174
41084175 How To Put Your Driver Into ALSA Tree
....@@ -4117,21 +4184,17 @@
41174184
41184185 Suppose that you create a new PCI driver for the card “xyz”. The card
41194186 module name would be snd-xyz. The new driver is usually put into the
4120
-alsa-driver tree, ``alsa-driver/pci`` directory in the case of PCI
4121
-cards. Then the driver is evaluated, audited and tested by developers
4122
-and users. After a certain time, the driver will go to the alsa-kernel
4123
-tree (to the corresponding directory, such as ``alsa-kernel/pci``) and
4124
-eventually will be integrated into the Linux 2.6 tree (the directory
4125
-would be ``linux/sound/pci``).
4187
+alsa-driver tree, ``sound/pci`` directory in the case of PCI
4188
+cards.
41264189
41274190 In the following sections, the driver code is supposed to be put into
4128
-alsa-driver tree. The two cases are covered: a driver consisting of a
4191
+Linux kernel tree. The two cases are covered: a driver consisting of a
41294192 single source file and one consisting of several source files.
41304193
41314194 Driver with A Single Source File
41324195 --------------------------------
41334196
4134
-1. Modify alsa-driver/pci/Makefile
4197
+1. Modify sound/pci/Makefile
41354198
41364199 Suppose you have a file xyz.c. Add the following two lines
41374200
....@@ -4160,52 +4223,43 @@
41604223
41614224 For the details of Kconfig script, refer to the kbuild documentation.
41624225
4163
-3. Run cvscompile script to re-generate the configure script and build
4164
- the whole stuff again.
4165
-
41664226 Drivers with Several Source Files
41674227 ---------------------------------
41684228
41694229 Suppose that the driver snd-xyz have several source files. They are
4170
-located in the new subdirectory, pci/xyz.
4230
+located in the new subdirectory, sound/pci/xyz.
41714231
4172
-1. Add a new directory (``xyz``) in ``alsa-driver/pci/Makefile`` as
4173
- below
4232
+1. Add a new directory (``sound/pci/xyz``) in ``sound/pci/Makefile``
4233
+ as below
41744234
41754235 ::
41764236
4177
- obj-$(CONFIG_SND) += xyz/
4237
+ obj-$(CONFIG_SND) += sound/pci/xyz/
41784238
41794239
4180
-2. Under the directory ``xyz``, create a Makefile
4240
+2. Under the directory ``sound/pci/xyz``, create a Makefile
41814241
41824242 ::
4183
-
4184
- ifndef SND_TOPDIR
4185
- SND_TOPDIR=../..
4186
- endif
4187
-
4188
- include $(SND_TOPDIR)/toplevel.config
4189
- include $(SND_TOPDIR)/Makefile.conf
41904243
41914244 snd-xyz-objs := xyz.o abc.o def.o
4192
-
41934245 obj-$(CONFIG_SND_XYZ) += snd-xyz.o
4194
-
4195
- include $(SND_TOPDIR)/Rules.make
41964246
41974247 3. Create the Kconfig entry
41984248
41994249 This procedure is as same as in the last section.
42004250
4201
-4. Run cvscompile script to re-generate the configure script and build
4202
- the whole stuff again.
42034251
42044252 Useful Functions
42054253 ================
42064254
42074255 :c:func:`snd_printk()` and friends
4208
----------------------------------------
4256
+----------------------------------
4257
+
4258
+.. note:: This subsection describes a few helper functions for
4259
+ decorating a bit more on the standard :c:func:`printk()` & co.
4260
+ However, in general, the use of such helpers is no longer recommended.
4261
+ If possible, try to stick with the standard functions like
4262
+ :c:func:`dev_err()` or :c:func:`pr_err()`.
42094263
42104264 ALSA provides a verbose version of the :c:func:`printk()` function.
42114265 If a kernel config ``CONFIG_SND_VERBOSE_PRINTK`` is set, this function
....@@ -4221,13 +4275,10 @@
42214275 the debugging flag, it's ignored.
42224276
42234277 :c:func:`snd_printdd()` is compiled in only when
4224
-``CONFIG_SND_DEBUG_VERBOSE`` is set. Please note that
4225
-``CONFIG_SND_DEBUG_VERBOSE`` is not set as default even if you configure
4226
-the alsa-driver with ``--with-debug=full`` option. You need to give
4227
-explicitly ``--with-debug=detect`` option instead.
4278
+``CONFIG_SND_DEBUG_VERBOSE`` is set.
42284279
42294280 :c:func:`snd_BUG()`
4230
-------------------------
4281
+-------------------
42314282
42324283 It shows the ``BUG?`` message and stack trace as well as
42334284 :c:func:`snd_BUG_ON()` at the point. It's useful to show that a
....@@ -4236,7 +4287,7 @@
42364287 When no debug flag is set, this macro is ignored.
42374288
42384289 :c:func:`snd_BUG_ON()`
4239
-----------------------------
4290
+----------------------
42404291
42414292 :c:func:`snd_BUG_ON()` macro is similar with
42424293 :c:func:`WARN_ON()` macro. For example, snd_BUG_ON(!pointer); or