1
0

pcm.h 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_PCM_H
  3. #define __SOUND_PCM_H
  4. /*
  5. * Digital Audio (PCM) abstract layer
  6. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  7. * Abramo Bagnara <abramo@alsa-project.org>
  8. */
  9. #include <sound/asound.h>
  10. #include <sound/memalloc.h>
  11. #include <sound/minors.h>
  12. #include <linux/poll.h>
  13. #include <linux/mm.h>
  14. #include <linux/bitops.h>
  15. #include <linux/pm_qos.h>
  16. #include <linux/refcount.h>
  17. #include <linux/uio.h>
  18. #define snd_pcm_substream_chip(substream) ((substream)->private_data)
  19. #define snd_pcm_chip(pcm) ((pcm)->private_data)
  20. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  21. #include <sound/pcm_oss.h>
  22. #endif
  23. /*
  24. * Hardware (lowlevel) section
  25. */
  26. struct snd_pcm_hardware {
  27. unsigned int info; /* SNDRV_PCM_INFO_* */
  28. u64 formats; /* SNDRV_PCM_FMTBIT_* */
  29. u32 subformats; /* for S32_LE, SNDRV_PCM_SUBFMTBIT_* */
  30. unsigned int rates; /* SNDRV_PCM_RATE_* */
  31. unsigned int rate_min; /* min rate */
  32. unsigned int rate_max; /* max rate */
  33. unsigned int channels_min; /* min channels */
  34. unsigned int channels_max; /* max channels */
  35. size_t buffer_bytes_max; /* max buffer size */
  36. size_t period_bytes_min; /* min period size */
  37. size_t period_bytes_max; /* max period size */
  38. unsigned int periods_min; /* min # of periods */
  39. unsigned int periods_max; /* max # of periods */
  40. size_t fifo_size; /* fifo size in bytes */
  41. };
  42. struct snd_pcm_status64;
  43. struct snd_pcm_substream;
  44. struct snd_pcm_audio_tstamp_config; /* definitions further down */
  45. struct snd_pcm_audio_tstamp_report;
  46. struct snd_pcm_ops {
  47. int (*open)(struct snd_pcm_substream *substream);
  48. int (*close)(struct snd_pcm_substream *substream);
  49. int (*ioctl)(struct snd_pcm_substream * substream,
  50. unsigned int cmd, void *arg);
  51. int (*hw_params)(struct snd_pcm_substream *substream,
  52. struct snd_pcm_hw_params *params);
  53. int (*hw_free)(struct snd_pcm_substream *substream);
  54. int (*prepare)(struct snd_pcm_substream *substream);
  55. int (*trigger)(struct snd_pcm_substream *substream, int cmd);
  56. int (*sync_stop)(struct snd_pcm_substream *substream);
  57. snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
  58. int (*get_time_info)(struct snd_pcm_substream *substream,
  59. struct timespec64 *system_ts, struct timespec64 *audio_ts,
  60. struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
  61. struct snd_pcm_audio_tstamp_report *audio_tstamp_report);
  62. int (*fill_silence)(struct snd_pcm_substream *substream, int channel,
  63. unsigned long pos, unsigned long bytes);
  64. int (*copy)(struct snd_pcm_substream *substream, int channel,
  65. unsigned long pos, struct iov_iter *iter, unsigned long bytes);
  66. struct page *(*page)(struct snd_pcm_substream *substream,
  67. unsigned long offset);
  68. int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma);
  69. int (*ack)(struct snd_pcm_substream *substream);
  70. };
  71. /*
  72. *
  73. */
  74. #if defined(CONFIG_SND_DYNAMIC_MINORS)
  75. #define SNDRV_PCM_DEVICES (SNDRV_OS_MINORS-2)
  76. #else
  77. #define SNDRV_PCM_DEVICES 8
  78. #endif
  79. #define SNDRV_PCM_IOCTL1_RESET 0
  80. /* 1 is absent slot. */
  81. #define SNDRV_PCM_IOCTL1_CHANNEL_INFO 2
  82. /* 3 is absent slot. */
  83. #define SNDRV_PCM_IOCTL1_FIFO_SIZE 4
  84. #define SNDRV_PCM_IOCTL1_SYNC_ID 5
  85. #define SNDRV_PCM_TRIGGER_STOP 0
  86. #define SNDRV_PCM_TRIGGER_START 1
  87. #define SNDRV_PCM_TRIGGER_PAUSE_PUSH 2
  88. #define SNDRV_PCM_TRIGGER_PAUSE_RELEASE 3
  89. #define SNDRV_PCM_TRIGGER_SUSPEND 4
  90. #define SNDRV_PCM_TRIGGER_RESUME 5
  91. #define SNDRV_PCM_TRIGGER_DRAIN 6
  92. #define SNDRV_PCM_POS_XRUN ((snd_pcm_uframes_t)-1)
  93. /* If you change this don't forget to change rates[] table in pcm_native.c */
  94. #define SNDRV_PCM_RATE_5512 (1U<<0) /* 5512Hz */
  95. #define SNDRV_PCM_RATE_8000 (1U<<1) /* 8000Hz */
  96. #define SNDRV_PCM_RATE_11025 (1U<<2) /* 11025Hz */
  97. #define SNDRV_PCM_RATE_16000 (1U<<3) /* 16000Hz */
  98. #define SNDRV_PCM_RATE_22050 (1U<<4) /* 22050Hz */
  99. #define SNDRV_PCM_RATE_32000 (1U<<5) /* 32000Hz */
  100. #define SNDRV_PCM_RATE_44100 (1U<<6) /* 44100Hz */
  101. #define SNDRV_PCM_RATE_48000 (1U<<7) /* 48000Hz */
  102. #define SNDRV_PCM_RATE_64000 (1U<<8) /* 64000Hz */
  103. #define SNDRV_PCM_RATE_88200 (1U<<9) /* 88200Hz */
  104. #define SNDRV_PCM_RATE_96000 (1U<<10) /* 96000Hz */
  105. #define SNDRV_PCM_RATE_176400 (1U<<11) /* 176400Hz */
  106. #define SNDRV_PCM_RATE_192000 (1U<<12) /* 192000Hz */
  107. #define SNDRV_PCM_RATE_352800 (1U<<13) /* 352800Hz */
  108. #define SNDRV_PCM_RATE_384000 (1U<<14) /* 384000Hz */
  109. #define SNDRV_PCM_RATE_705600 (1U<<15) /* 705600Hz */
  110. #define SNDRV_PCM_RATE_768000 (1U<<16) /* 768000Hz */
  111. /* extended rates since 6.12 */
  112. #define SNDRV_PCM_RATE_12000 (1U<<17) /* 12000Hz */
  113. #define SNDRV_PCM_RATE_24000 (1U<<18) /* 24000Hz */
  114. #define SNDRV_PCM_RATE_128000 (1U<<19) /* 128000Hz */
  115. #define SNDRV_PCM_RATE_CONTINUOUS (1U<<30) /* continuous range */
  116. #define SNDRV_PCM_RATE_KNOT (1U<<31) /* supports more non-continuous rates */
  117. #define SNDRV_PCM_RATE_8000_44100 (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_11025|\
  118. SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_22050|\
  119. SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100)
  120. #define SNDRV_PCM_RATE_8000_48000 (SNDRV_PCM_RATE_8000_44100|SNDRV_PCM_RATE_48000)
  121. #define SNDRV_PCM_RATE_8000_96000 (SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_64000|\
  122. SNDRV_PCM_RATE_88200|SNDRV_PCM_RATE_96000)
  123. #define SNDRV_PCM_RATE_8000_192000 (SNDRV_PCM_RATE_8000_96000|SNDRV_PCM_RATE_176400|\
  124. SNDRV_PCM_RATE_192000)
  125. #define SNDRV_PCM_RATE_8000_384000 (SNDRV_PCM_RATE_8000_192000|\
  126. SNDRV_PCM_RATE_352800|\
  127. SNDRV_PCM_RATE_384000)
  128. #define SNDRV_PCM_RATE_8000_768000 (SNDRV_PCM_RATE_8000_384000|\
  129. SNDRV_PCM_RATE_705600|\
  130. SNDRV_PCM_RATE_768000)
  131. #define _SNDRV_PCM_FMTBIT(fmt) (1ULL << (__force int)SNDRV_PCM_FORMAT_##fmt)
  132. #define SNDRV_PCM_FMTBIT_S8 _SNDRV_PCM_FMTBIT(S8)
  133. #define SNDRV_PCM_FMTBIT_U8 _SNDRV_PCM_FMTBIT(U8)
  134. #define SNDRV_PCM_FMTBIT_S16_LE _SNDRV_PCM_FMTBIT(S16_LE)
  135. #define SNDRV_PCM_FMTBIT_S16_BE _SNDRV_PCM_FMTBIT(S16_BE)
  136. #define SNDRV_PCM_FMTBIT_U16_LE _SNDRV_PCM_FMTBIT(U16_LE)
  137. #define SNDRV_PCM_FMTBIT_U16_BE _SNDRV_PCM_FMTBIT(U16_BE)
  138. #define SNDRV_PCM_FMTBIT_S24_LE _SNDRV_PCM_FMTBIT(S24_LE)
  139. #define SNDRV_PCM_FMTBIT_S24_BE _SNDRV_PCM_FMTBIT(S24_BE)
  140. #define SNDRV_PCM_FMTBIT_U24_LE _SNDRV_PCM_FMTBIT(U24_LE)
  141. #define SNDRV_PCM_FMTBIT_U24_BE _SNDRV_PCM_FMTBIT(U24_BE)
  142. // For S32/U32 formats, 'msbits' hardware parameter is often used to deliver information about the
  143. // available bit count in most significant bit. It's for the case of so-called 'left-justified' or
  144. // `right-padding` sample which has less width than 32 bit.
  145. #define SNDRV_PCM_FMTBIT_S32_LE _SNDRV_PCM_FMTBIT(S32_LE)
  146. #define SNDRV_PCM_FMTBIT_S32_BE _SNDRV_PCM_FMTBIT(S32_BE)
  147. #define SNDRV_PCM_FMTBIT_U32_LE _SNDRV_PCM_FMTBIT(U32_LE)
  148. #define SNDRV_PCM_FMTBIT_U32_BE _SNDRV_PCM_FMTBIT(U32_BE)
  149. #define SNDRV_PCM_FMTBIT_FLOAT_LE _SNDRV_PCM_FMTBIT(FLOAT_LE)
  150. #define SNDRV_PCM_FMTBIT_FLOAT_BE _SNDRV_PCM_FMTBIT(FLOAT_BE)
  151. #define SNDRV_PCM_FMTBIT_FLOAT64_LE _SNDRV_PCM_FMTBIT(FLOAT64_LE)
  152. #define SNDRV_PCM_FMTBIT_FLOAT64_BE _SNDRV_PCM_FMTBIT(FLOAT64_BE)
  153. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_LE)
  154. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_BE)
  155. #define SNDRV_PCM_FMTBIT_MU_LAW _SNDRV_PCM_FMTBIT(MU_LAW)
  156. #define SNDRV_PCM_FMTBIT_A_LAW _SNDRV_PCM_FMTBIT(A_LAW)
  157. #define SNDRV_PCM_FMTBIT_IMA_ADPCM _SNDRV_PCM_FMTBIT(IMA_ADPCM)
  158. #define SNDRV_PCM_FMTBIT_MPEG _SNDRV_PCM_FMTBIT(MPEG)
  159. #define SNDRV_PCM_FMTBIT_GSM _SNDRV_PCM_FMTBIT(GSM)
  160. #define SNDRV_PCM_FMTBIT_S20_LE _SNDRV_PCM_FMTBIT(S20_LE)
  161. #define SNDRV_PCM_FMTBIT_U20_LE _SNDRV_PCM_FMTBIT(U20_LE)
  162. #define SNDRV_PCM_FMTBIT_S20_BE _SNDRV_PCM_FMTBIT(S20_BE)
  163. #define SNDRV_PCM_FMTBIT_U20_BE _SNDRV_PCM_FMTBIT(U20_BE)
  164. #define SNDRV_PCM_FMTBIT_SPECIAL _SNDRV_PCM_FMTBIT(SPECIAL)
  165. #define SNDRV_PCM_FMTBIT_S24_3LE _SNDRV_PCM_FMTBIT(S24_3LE)
  166. #define SNDRV_PCM_FMTBIT_U24_3LE _SNDRV_PCM_FMTBIT(U24_3LE)
  167. #define SNDRV_PCM_FMTBIT_S24_3BE _SNDRV_PCM_FMTBIT(S24_3BE)
  168. #define SNDRV_PCM_FMTBIT_U24_3BE _SNDRV_PCM_FMTBIT(U24_3BE)
  169. #define SNDRV_PCM_FMTBIT_S20_3LE _SNDRV_PCM_FMTBIT(S20_3LE)
  170. #define SNDRV_PCM_FMTBIT_U20_3LE _SNDRV_PCM_FMTBIT(U20_3LE)
  171. #define SNDRV_PCM_FMTBIT_S20_3BE _SNDRV_PCM_FMTBIT(S20_3BE)
  172. #define SNDRV_PCM_FMTBIT_U20_3BE _SNDRV_PCM_FMTBIT(U20_3BE)
  173. #define SNDRV_PCM_FMTBIT_S18_3LE _SNDRV_PCM_FMTBIT(S18_3LE)
  174. #define SNDRV_PCM_FMTBIT_U18_3LE _SNDRV_PCM_FMTBIT(U18_3LE)
  175. #define SNDRV_PCM_FMTBIT_S18_3BE _SNDRV_PCM_FMTBIT(S18_3BE)
  176. #define SNDRV_PCM_FMTBIT_U18_3BE _SNDRV_PCM_FMTBIT(U18_3BE)
  177. #define SNDRV_PCM_FMTBIT_G723_24 _SNDRV_PCM_FMTBIT(G723_24)
  178. #define SNDRV_PCM_FMTBIT_G723_24_1B _SNDRV_PCM_FMTBIT(G723_24_1B)
  179. #define SNDRV_PCM_FMTBIT_G723_40 _SNDRV_PCM_FMTBIT(G723_40)
  180. #define SNDRV_PCM_FMTBIT_G723_40_1B _SNDRV_PCM_FMTBIT(G723_40_1B)
  181. #define SNDRV_PCM_FMTBIT_DSD_U8 _SNDRV_PCM_FMTBIT(DSD_U8)
  182. #define SNDRV_PCM_FMTBIT_DSD_U16_LE _SNDRV_PCM_FMTBIT(DSD_U16_LE)
  183. #define SNDRV_PCM_FMTBIT_DSD_U32_LE _SNDRV_PCM_FMTBIT(DSD_U32_LE)
  184. #define SNDRV_PCM_FMTBIT_DSD_U16_BE _SNDRV_PCM_FMTBIT(DSD_U16_BE)
  185. #define SNDRV_PCM_FMTBIT_DSD_U32_BE _SNDRV_PCM_FMTBIT(DSD_U32_BE)
  186. #ifdef SNDRV_LITTLE_ENDIAN
  187. #define SNDRV_PCM_FMTBIT_S16 SNDRV_PCM_FMTBIT_S16_LE
  188. #define SNDRV_PCM_FMTBIT_U16 SNDRV_PCM_FMTBIT_U16_LE
  189. #define SNDRV_PCM_FMTBIT_S24 SNDRV_PCM_FMTBIT_S24_LE
  190. #define SNDRV_PCM_FMTBIT_U24 SNDRV_PCM_FMTBIT_U24_LE
  191. #define SNDRV_PCM_FMTBIT_S32 SNDRV_PCM_FMTBIT_S32_LE
  192. #define SNDRV_PCM_FMTBIT_U32 SNDRV_PCM_FMTBIT_U32_LE
  193. #define SNDRV_PCM_FMTBIT_FLOAT SNDRV_PCM_FMTBIT_FLOAT_LE
  194. #define SNDRV_PCM_FMTBIT_FLOAT64 SNDRV_PCM_FMTBIT_FLOAT64_LE
  195. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  196. #define SNDRV_PCM_FMTBIT_S20 SNDRV_PCM_FMTBIT_S20_LE
  197. #define SNDRV_PCM_FMTBIT_U20 SNDRV_PCM_FMTBIT_U20_LE
  198. #endif
  199. #ifdef SNDRV_BIG_ENDIAN
  200. #define SNDRV_PCM_FMTBIT_S16 SNDRV_PCM_FMTBIT_S16_BE
  201. #define SNDRV_PCM_FMTBIT_U16 SNDRV_PCM_FMTBIT_U16_BE
  202. #define SNDRV_PCM_FMTBIT_S24 SNDRV_PCM_FMTBIT_S24_BE
  203. #define SNDRV_PCM_FMTBIT_U24 SNDRV_PCM_FMTBIT_U24_BE
  204. #define SNDRV_PCM_FMTBIT_S32 SNDRV_PCM_FMTBIT_S32_BE
  205. #define SNDRV_PCM_FMTBIT_U32 SNDRV_PCM_FMTBIT_U32_BE
  206. #define SNDRV_PCM_FMTBIT_FLOAT SNDRV_PCM_FMTBIT_FLOAT_BE
  207. #define SNDRV_PCM_FMTBIT_FLOAT64 SNDRV_PCM_FMTBIT_FLOAT64_BE
  208. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
  209. #define SNDRV_PCM_FMTBIT_S20 SNDRV_PCM_FMTBIT_S20_BE
  210. #define SNDRV_PCM_FMTBIT_U20 SNDRV_PCM_FMTBIT_U20_BE
  211. #endif
  212. #define _SNDRV_PCM_SUBFMTBIT(fmt) BIT((__force int)SNDRV_PCM_SUBFORMAT_##fmt)
  213. #define SNDRV_PCM_SUBFMTBIT_STD _SNDRV_PCM_SUBFMTBIT(STD)
  214. #define SNDRV_PCM_SUBFMTBIT_MSBITS_MAX _SNDRV_PCM_SUBFMTBIT(MSBITS_MAX)
  215. #define SNDRV_PCM_SUBFMTBIT_MSBITS_20 _SNDRV_PCM_SUBFMTBIT(MSBITS_20)
  216. #define SNDRV_PCM_SUBFMTBIT_MSBITS_24 _SNDRV_PCM_SUBFMTBIT(MSBITS_24)
  217. struct snd_pcm_file {
  218. struct snd_pcm_substream *substream;
  219. int no_compat_mmap;
  220. unsigned int user_pversion; /* supported protocol version */
  221. };
  222. struct snd_pcm_hw_rule;
  223. typedef int (*snd_pcm_hw_rule_func_t)(struct snd_pcm_hw_params *params,
  224. struct snd_pcm_hw_rule *rule);
  225. struct snd_pcm_hw_rule {
  226. unsigned int cond;
  227. int var;
  228. int deps[5];
  229. snd_pcm_hw_rule_func_t func;
  230. void *private;
  231. };
  232. struct snd_pcm_hw_constraints {
  233. struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK -
  234. SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
  235. struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
  236. SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
  237. unsigned int rules_num;
  238. unsigned int rules_all;
  239. struct snd_pcm_hw_rule *rules;
  240. };
  241. static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
  242. snd_pcm_hw_param_t var)
  243. {
  244. return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  245. }
  246. static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
  247. snd_pcm_hw_param_t var)
  248. {
  249. return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  250. }
  251. struct snd_ratnum {
  252. unsigned int num;
  253. unsigned int den_min, den_max, den_step;
  254. };
  255. struct snd_ratden {
  256. unsigned int num_min, num_max, num_step;
  257. unsigned int den;
  258. };
  259. struct snd_pcm_hw_constraint_ratnums {
  260. int nrats;
  261. const struct snd_ratnum *rats;
  262. };
  263. struct snd_pcm_hw_constraint_ratdens {
  264. int nrats;
  265. const struct snd_ratden *rats;
  266. };
  267. struct snd_pcm_hw_constraint_list {
  268. const unsigned int *list;
  269. unsigned int count;
  270. unsigned int mask;
  271. };
  272. struct snd_pcm_hw_constraint_ranges {
  273. unsigned int count;
  274. const struct snd_interval *ranges;
  275. unsigned int mask;
  276. };
  277. /*
  278. * userspace-provided audio timestamp config to kernel,
  279. * structure is for internal use only and filled with dedicated unpack routine
  280. */
  281. struct snd_pcm_audio_tstamp_config {
  282. /* 5 of max 16 bits used */
  283. u32 type_requested:4;
  284. u32 report_delay:1; /* add total delay to A/D or D/A */
  285. };
  286. static inline void snd_pcm_unpack_audio_tstamp_config(__u32 data,
  287. struct snd_pcm_audio_tstamp_config *config)
  288. {
  289. config->type_requested = data & 0xF;
  290. config->report_delay = (data >> 4) & 1;
  291. }
  292. /*
  293. * kernel-provided audio timestamp report to user-space
  294. * structure is for internal use only and read by dedicated pack routine
  295. */
  296. struct snd_pcm_audio_tstamp_report {
  297. /* 6 of max 16 bits used for bit-fields */
  298. /* for backwards compatibility */
  299. u32 valid:1;
  300. /* actual type if hardware could not support requested timestamp */
  301. u32 actual_type:4;
  302. /* accuracy represented in ns units */
  303. u32 accuracy_report:1; /* 0 if accuracy unknown, 1 if accuracy field is valid */
  304. u32 accuracy; /* up to 4.29s, will be packed in separate field */
  305. };
  306. static inline void snd_pcm_pack_audio_tstamp_report(__u32 *data, __u32 *accuracy,
  307. const struct snd_pcm_audio_tstamp_report *report)
  308. {
  309. u32 tmp;
  310. tmp = report->accuracy_report;
  311. tmp <<= 4;
  312. tmp |= report->actual_type;
  313. tmp <<= 1;
  314. tmp |= report->valid;
  315. *data &= 0xffff; /* zero-clear MSBs */
  316. *data |= (tmp << 16);
  317. *accuracy = report->accuracy;
  318. }
  319. struct snd_pcm_runtime {
  320. /* -- Status -- */
  321. snd_pcm_state_t state; /* stream state */
  322. snd_pcm_state_t suspended_state; /* suspended stream state */
  323. struct snd_pcm_substream *trigger_master;
  324. struct timespec64 trigger_tstamp; /* trigger timestamp */
  325. bool trigger_tstamp_latched; /* trigger timestamp latched in low-level driver/hardware */
  326. int overrange;
  327. snd_pcm_uframes_t avail_max;
  328. snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
  329. snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
  330. unsigned long hw_ptr_jiffies; /* Time when hw_ptr is updated */
  331. unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
  332. snd_pcm_sframes_t delay; /* extra delay; typically FIFO size */
  333. u64 hw_ptr_wrap; /* offset for hw_ptr due to boundary wrap-around */
  334. /* -- HW params -- */
  335. snd_pcm_access_t access; /* access mode */
  336. snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
  337. snd_pcm_subformat_t subformat; /* subformat */
  338. unsigned int rate; /* rate in Hz */
  339. unsigned int channels; /* channels */
  340. snd_pcm_uframes_t period_size; /* period size */
  341. unsigned int periods; /* periods */
  342. snd_pcm_uframes_t buffer_size; /* buffer size */
  343. snd_pcm_uframes_t min_align; /* Min alignment for the format */
  344. size_t byte_align;
  345. unsigned int frame_bits;
  346. unsigned int sample_bits;
  347. unsigned int info;
  348. unsigned int rate_num;
  349. unsigned int rate_den;
  350. unsigned int no_period_wakeup: 1;
  351. /* -- SW params; see struct snd_pcm_sw_params for comments -- */
  352. int tstamp_mode;
  353. unsigned int period_step;
  354. snd_pcm_uframes_t start_threshold;
  355. snd_pcm_uframes_t stop_threshold;
  356. snd_pcm_uframes_t silence_threshold;
  357. snd_pcm_uframes_t silence_size;
  358. snd_pcm_uframes_t boundary;
  359. /* internal data of auto-silencer */
  360. snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
  361. snd_pcm_uframes_t silence_filled; /* already filled part of silence area */
  362. bool std_sync_id; /* hardware synchronization - standard per card ID */
  363. /* -- mmap -- */
  364. struct snd_pcm_mmap_status *status;
  365. struct snd_pcm_mmap_control *control;
  366. /* -- locking / scheduling -- */
  367. snd_pcm_uframes_t twake; /* do transfer (!poll) wakeup if non-zero */
  368. wait_queue_head_t sleep; /* poll sleep */
  369. wait_queue_head_t tsleep; /* transfer sleep */
  370. struct snd_fasync *fasync;
  371. bool stop_operating; /* sync_stop will be called */
  372. struct mutex buffer_mutex; /* protect for buffer changes */
  373. atomic_t buffer_accessing; /* >0: in r/w operation, <0: blocked */
  374. /* -- private section -- */
  375. void *private_data;
  376. void (*private_free)(struct snd_pcm_runtime *runtime);
  377. /* -- hardware description -- */
  378. struct snd_pcm_hardware hw;
  379. struct snd_pcm_hw_constraints hw_constraints;
  380. /* -- timer -- */
  381. unsigned int timer_resolution; /* timer resolution */
  382. int tstamp_type; /* timestamp type */
  383. /* -- DMA -- */
  384. unsigned char *dma_area; /* DMA area */
  385. dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
  386. size_t dma_bytes; /* size of DMA area */
  387. struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
  388. unsigned int buffer_changed:1; /* buffer allocation changed; set only in managed mode */
  389. /* -- audio timestamp config -- */
  390. struct snd_pcm_audio_tstamp_config audio_tstamp_config;
  391. struct snd_pcm_audio_tstamp_report audio_tstamp_report;
  392. struct timespec64 driver_tstamp;
  393. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  394. /* -- OSS things -- */
  395. struct snd_pcm_oss_runtime oss;
  396. #endif
  397. };
  398. struct snd_pcm_group { /* keep linked substreams */
  399. spinlock_t lock;
  400. struct mutex mutex;
  401. struct list_head substreams;
  402. refcount_t refs;
  403. };
  404. struct pid;
  405. struct snd_pcm_substream {
  406. struct snd_pcm *pcm;
  407. struct snd_pcm_str *pstr;
  408. void *private_data; /* copied from pcm->private_data */
  409. int number;
  410. char name[32]; /* substream name */
  411. int stream; /* stream (direction) */
  412. struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
  413. size_t buffer_bytes_max; /* limit ring buffer size */
  414. struct snd_dma_buffer dma_buffer;
  415. size_t dma_max;
  416. /* -- hardware operations -- */
  417. const struct snd_pcm_ops *ops;
  418. /* -- runtime information -- */
  419. struct snd_pcm_runtime *runtime;
  420. /* -- timer section -- */
  421. struct snd_timer *timer; /* timer */
  422. unsigned timer_running: 1; /* time is running */
  423. long wait_time; /* time in ms for R/W to wait for avail */
  424. /* -- next substream -- */
  425. struct snd_pcm_substream *next;
  426. /* -- linked substreams -- */
  427. struct list_head link_list; /* linked list member */
  428. struct snd_pcm_group self_group; /* fake group for non linked substream (with substream lock inside) */
  429. struct snd_pcm_group *group; /* pointer to current group */
  430. /* -- assigned files -- */
  431. int ref_count;
  432. atomic_t mmap_count;
  433. unsigned int f_flags;
  434. void (*pcm_release)(struct snd_pcm_substream *);
  435. struct pid *pid;
  436. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  437. /* -- OSS things -- */
  438. struct snd_pcm_oss_substream oss;
  439. #endif
  440. #ifdef CONFIG_SND_VERBOSE_PROCFS
  441. struct snd_info_entry *proc_root;
  442. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  443. /* misc flags */
  444. unsigned int hw_opened: 1;
  445. unsigned int managed_buffer_alloc:1;
  446. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  447. unsigned int xrun_counter; /* number of times xrun happens */
  448. #endif /* CONFIG_SND_PCM_XRUN_DEBUG */
  449. };
  450. #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
  451. struct snd_pcm_str {
  452. int stream; /* stream (direction) */
  453. struct snd_pcm *pcm;
  454. /* -- substreams -- */
  455. unsigned int substream_count;
  456. unsigned int substream_opened;
  457. struct snd_pcm_substream *substream;
  458. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  459. /* -- OSS things -- */
  460. struct snd_pcm_oss_stream oss;
  461. #endif
  462. #ifdef CONFIG_SND_VERBOSE_PROCFS
  463. struct snd_info_entry *proc_root;
  464. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  465. unsigned int xrun_debug; /* 0 = disabled, 1 = verbose, 2 = stacktrace */
  466. #endif
  467. #endif
  468. struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
  469. struct device *dev;
  470. };
  471. struct snd_pcm {
  472. struct snd_card *card;
  473. struct list_head list;
  474. int device; /* device number */
  475. unsigned int info_flags;
  476. unsigned short dev_class;
  477. unsigned short dev_subclass;
  478. char id[64];
  479. char name[80];
  480. struct snd_pcm_str streams[2];
  481. struct mutex open_mutex;
  482. wait_queue_head_t open_wait;
  483. void *private_data;
  484. void (*private_free) (struct snd_pcm *pcm);
  485. bool internal; /* pcm is for internal use only */
  486. bool nonatomic; /* whole PCM operations are in non-atomic context */
  487. bool no_device_suspend; /* don't invoke device PM suspend */
  488. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  489. struct snd_pcm_oss oss;
  490. #endif
  491. };
  492. /*
  493. * Registering
  494. */
  495. extern const struct file_operations snd_pcm_f_ops[2];
  496. int snd_pcm_new(struct snd_card *card, const char *id, int device,
  497. int playback_count, int capture_count,
  498. struct snd_pcm **rpcm);
  499. int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
  500. int playback_count, int capture_count,
  501. struct snd_pcm **rpcm);
  502. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
  503. #if IS_ENABLED(CONFIG_SND_PCM_OSS)
  504. struct snd_pcm_notify {
  505. int (*n_register) (struct snd_pcm * pcm);
  506. int (*n_disconnect) (struct snd_pcm * pcm);
  507. int (*n_unregister) (struct snd_pcm * pcm);
  508. struct list_head list;
  509. };
  510. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
  511. #endif
  512. /*
  513. * Native I/O
  514. */
  515. int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
  516. int snd_pcm_info_user(struct snd_pcm_substream *substream,
  517. struct snd_pcm_info __user *info);
  518. int snd_pcm_status64(struct snd_pcm_substream *substream,
  519. struct snd_pcm_status64 *status);
  520. int snd_pcm_start(struct snd_pcm_substream *substream);
  521. int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
  522. int snd_pcm_drain_done(struct snd_pcm_substream *substream);
  523. int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
  524. #ifdef CONFIG_PM
  525. int snd_pcm_suspend_all(struct snd_pcm *pcm);
  526. #else
  527. static inline int snd_pcm_suspend_all(struct snd_pcm *pcm)
  528. {
  529. return 0;
  530. }
  531. #endif
  532. int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
  533. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
  534. struct snd_pcm_substream **rsubstream);
  535. void snd_pcm_release_substream(struct snd_pcm_substream *substream);
  536. int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
  537. struct snd_pcm_substream **rsubstream);
  538. void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
  539. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
  540. #ifdef CONFIG_SND_DEBUG
  541. void snd_pcm_debug_name(struct snd_pcm_substream *substream,
  542. char *name, size_t len);
  543. #else
  544. static inline void
  545. snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
  546. {
  547. *buf = 0;
  548. }
  549. #endif
  550. /*
  551. * PCM library
  552. */
  553. /**
  554. * snd_pcm_stream_linked - Check whether the substream is linked with others
  555. * @substream: substream to check
  556. *
  557. * Return: true if the given substream is being linked with others
  558. */
  559. static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
  560. {
  561. return substream->group != &substream->self_group;
  562. }
  563. void snd_pcm_stream_lock(struct snd_pcm_substream *substream);
  564. void snd_pcm_stream_unlock(struct snd_pcm_substream *substream);
  565. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream);
  566. void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream);
  567. unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream);
  568. unsigned long _snd_pcm_stream_lock_irqsave_nested(struct snd_pcm_substream *substream);
  569. /**
  570. * snd_pcm_stream_lock_irqsave - Lock the PCM stream
  571. * @substream: PCM substream
  572. * @flags: irq flags
  573. *
  574. * This locks the PCM stream like snd_pcm_stream_lock() but with the local
  575. * IRQ (only when nonatomic is false). In nonatomic case, this is identical
  576. * as snd_pcm_stream_lock().
  577. */
  578. #define snd_pcm_stream_lock_irqsave(substream, flags) \
  579. do { \
  580. typecheck(unsigned long, flags); \
  581. flags = _snd_pcm_stream_lock_irqsave(substream); \
  582. } while (0)
  583. void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  584. unsigned long flags);
  585. /**
  586. * snd_pcm_stream_lock_irqsave_nested - Single-nested PCM stream locking
  587. * @substream: PCM substream
  588. * @flags: irq flags
  589. *
  590. * This locks the PCM stream like snd_pcm_stream_lock_irqsave() but with
  591. * the single-depth lockdep subclass.
  592. */
  593. #define snd_pcm_stream_lock_irqsave_nested(substream, flags) \
  594. do { \
  595. typecheck(unsigned long, flags); \
  596. flags = _snd_pcm_stream_lock_irqsave_nested(substream); \
  597. } while (0)
  598. /* definitions for guard(); use like guard(pcm_stream_lock) */
  599. DEFINE_LOCK_GUARD_1(pcm_stream_lock, struct snd_pcm_substream,
  600. snd_pcm_stream_lock(_T->lock),
  601. snd_pcm_stream_unlock(_T->lock))
  602. DEFINE_LOCK_GUARD_1(pcm_stream_lock_irq, struct snd_pcm_substream,
  603. snd_pcm_stream_lock_irq(_T->lock),
  604. snd_pcm_stream_unlock_irq(_T->lock))
  605. DEFINE_LOCK_GUARD_1(pcm_stream_lock_irqsave, struct snd_pcm_substream,
  606. snd_pcm_stream_lock_irqsave(_T->lock, _T->flags),
  607. snd_pcm_stream_unlock_irqrestore(_T->lock, _T->flags),
  608. unsigned long flags)
  609. /**
  610. * snd_pcm_group_for_each_entry - iterate over the linked substreams
  611. * @s: the iterator
  612. * @substream: the substream
  613. *
  614. * Iterate over the all linked substreams to the given @substream.
  615. * When @substream isn't linked with any others, this gives returns @substream
  616. * itself once.
  617. */
  618. #define snd_pcm_group_for_each_entry(s, substream) \
  619. list_for_each_entry(s, &substream->group->substreams, link_list)
  620. #define for_each_pcm_streams(stream) \
  621. for (stream = SNDRV_PCM_STREAM_PLAYBACK; \
  622. stream <= SNDRV_PCM_STREAM_LAST; \
  623. stream++)
  624. /**
  625. * snd_pcm_running - Check whether the substream is in a running state
  626. * @substream: substream to check
  627. *
  628. * Return: true if the given substream is in the state RUNNING, or in the
  629. * state DRAINING for playback.
  630. */
  631. static inline int snd_pcm_running(struct snd_pcm_substream *substream)
  632. {
  633. return (substream->runtime->state == SNDRV_PCM_STATE_RUNNING ||
  634. (substream->runtime->state == SNDRV_PCM_STATE_DRAINING &&
  635. substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
  636. }
  637. /**
  638. * __snd_pcm_set_state - Change the current PCM state
  639. * @runtime: PCM runtime to set
  640. * @state: the current state to set
  641. *
  642. * Call within the stream lock
  643. */
  644. static inline void __snd_pcm_set_state(struct snd_pcm_runtime *runtime,
  645. snd_pcm_state_t state)
  646. {
  647. runtime->state = state;
  648. runtime->status->state = state; /* copy for mmap */
  649. }
  650. /**
  651. * bytes_to_samples - Unit conversion of the size from bytes to samples
  652. * @runtime: PCM runtime instance
  653. * @size: size in bytes
  654. *
  655. * Return: the size in samples
  656. */
  657. static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
  658. {
  659. return size * 8 / runtime->sample_bits;
  660. }
  661. /**
  662. * bytes_to_frames - Unit conversion of the size from bytes to frames
  663. * @runtime: PCM runtime instance
  664. * @size: size in bytes
  665. *
  666. * Return: the size in frames
  667. */
  668. static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
  669. {
  670. return size * 8 / runtime->frame_bits;
  671. }
  672. /**
  673. * samples_to_bytes - Unit conversion of the size from samples to bytes
  674. * @runtime: PCM runtime instance
  675. * @size: size in samples
  676. *
  677. * Return: the byte size
  678. */
  679. static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
  680. {
  681. return size * runtime->sample_bits / 8;
  682. }
  683. /**
  684. * frames_to_bytes - Unit conversion of the size from frames to bytes
  685. * @runtime: PCM runtime instance
  686. * @size: size in frames
  687. *
  688. * Return: the byte size
  689. */
  690. static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
  691. {
  692. return size * runtime->frame_bits / 8;
  693. }
  694. /**
  695. * frame_aligned - Check whether the byte size is aligned to frames
  696. * @runtime: PCM runtime instance
  697. * @bytes: size in bytes
  698. *
  699. * Return: true if aligned, or false if not
  700. */
  701. static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
  702. {
  703. return bytes % runtime->byte_align == 0;
  704. }
  705. /**
  706. * snd_pcm_lib_buffer_bytes - Get the buffer size of the current PCM in bytes
  707. * @substream: PCM substream
  708. *
  709. * Return: buffer byte size
  710. */
  711. static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
  712. {
  713. struct snd_pcm_runtime *runtime = substream->runtime;
  714. return frames_to_bytes(runtime, runtime->buffer_size);
  715. }
  716. /**
  717. * snd_pcm_lib_period_bytes - Get the period size of the current PCM in bytes
  718. * @substream: PCM substream
  719. *
  720. * Return: period byte size
  721. */
  722. static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
  723. {
  724. struct snd_pcm_runtime *runtime = substream->runtime;
  725. return frames_to_bytes(runtime, runtime->period_size);
  726. }
  727. /**
  728. * snd_pcm_playback_avail - Get the available (writable) space for playback
  729. * @runtime: PCM runtime instance
  730. *
  731. * Result is between 0 ... (boundary - 1)
  732. *
  733. * Return: available frame size
  734. */
  735. static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
  736. {
  737. snd_pcm_sframes_t avail = runtime->status->hw_ptr + runtime->buffer_size - runtime->control->appl_ptr;
  738. if (avail < 0)
  739. avail += runtime->boundary;
  740. else if ((snd_pcm_uframes_t) avail >= runtime->boundary)
  741. avail -= runtime->boundary;
  742. return avail;
  743. }
  744. /**
  745. * snd_pcm_capture_avail - Get the available (readable) space for capture
  746. * @runtime: PCM runtime instance
  747. *
  748. * Result is between 0 ... (boundary - 1)
  749. *
  750. * Return: available frame size
  751. */
  752. static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
  753. {
  754. snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
  755. if (avail < 0)
  756. avail += runtime->boundary;
  757. return avail;
  758. }
  759. /**
  760. * snd_pcm_playback_hw_avail - Get the queued space for playback
  761. * @runtime: PCM runtime instance
  762. *
  763. * Return: available frame size
  764. */
  765. static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
  766. {
  767. return runtime->buffer_size - snd_pcm_playback_avail(runtime);
  768. }
  769. /**
  770. * snd_pcm_capture_hw_avail - Get the free space for capture
  771. * @runtime: PCM runtime instance
  772. *
  773. * Return: available frame size
  774. */
  775. static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
  776. {
  777. return runtime->buffer_size - snd_pcm_capture_avail(runtime);
  778. }
  779. /**
  780. * snd_pcm_playback_ready - check whether the playback buffer is available
  781. * @substream: the pcm substream instance
  782. *
  783. * Checks whether enough free space is available on the playback buffer.
  784. *
  785. * Return: Non-zero if available, or zero if not.
  786. */
  787. static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
  788. {
  789. struct snd_pcm_runtime *runtime = substream->runtime;
  790. return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
  791. }
  792. /**
  793. * snd_pcm_capture_ready - check whether the capture buffer is available
  794. * @substream: the pcm substream instance
  795. *
  796. * Checks whether enough capture data is available on the capture buffer.
  797. *
  798. * Return: Non-zero if available, or zero if not.
  799. */
  800. static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
  801. {
  802. struct snd_pcm_runtime *runtime = substream->runtime;
  803. return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
  804. }
  805. /**
  806. * snd_pcm_playback_data - check whether any data exists on the playback buffer
  807. * @substream: the pcm substream instance
  808. *
  809. * Checks whether any data exists on the playback buffer.
  810. *
  811. * Return: Non-zero if any data exists, or zero if not. If stop_threshold
  812. * is bigger or equal to boundary, then this function returns always non-zero.
  813. */
  814. static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
  815. {
  816. struct snd_pcm_runtime *runtime = substream->runtime;
  817. if (runtime->stop_threshold >= runtime->boundary)
  818. return 1;
  819. return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
  820. }
  821. /**
  822. * snd_pcm_playback_empty - check whether the playback buffer is empty
  823. * @substream: the pcm substream instance
  824. *
  825. * Checks whether the playback buffer is empty.
  826. *
  827. * Return: Non-zero if empty, or zero if not.
  828. */
  829. static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
  830. {
  831. struct snd_pcm_runtime *runtime = substream->runtime;
  832. return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
  833. }
  834. /**
  835. * snd_pcm_capture_empty - check whether the capture buffer is empty
  836. * @substream: the pcm substream instance
  837. *
  838. * Checks whether the capture buffer is empty.
  839. *
  840. * Return: Non-zero if empty, or zero if not.
  841. */
  842. static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
  843. {
  844. struct snd_pcm_runtime *runtime = substream->runtime;
  845. return snd_pcm_capture_avail(runtime) == 0;
  846. }
  847. /**
  848. * snd_pcm_trigger_done - Mark the master substream
  849. * @substream: the pcm substream instance
  850. * @master: the linked master substream
  851. *
  852. * When multiple substreams of the same card are linked and the hardware
  853. * supports the single-shot operation, the driver calls this in the loop
  854. * in snd_pcm_group_for_each_entry() for marking the substream as "done".
  855. * Then most of trigger operations are performed only to the given master
  856. * substream.
  857. *
  858. * The trigger_master mark is cleared at timestamp updates at the end
  859. * of trigger operations.
  860. */
  861. static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
  862. struct snd_pcm_substream *master)
  863. {
  864. substream->runtime->trigger_master = master;
  865. }
  866. static inline int hw_is_mask(int var)
  867. {
  868. return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
  869. var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
  870. }
  871. static inline int hw_is_interval(int var)
  872. {
  873. return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
  874. var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
  875. }
  876. static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
  877. snd_pcm_hw_param_t var)
  878. {
  879. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  880. }
  881. static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
  882. snd_pcm_hw_param_t var)
  883. {
  884. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  885. }
  886. static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
  887. snd_pcm_hw_param_t var)
  888. {
  889. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  890. }
  891. static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
  892. snd_pcm_hw_param_t var)
  893. {
  894. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  895. }
  896. /**
  897. * params_channels - Get the number of channels from the hw params
  898. * @p: hw params
  899. *
  900. * Return: the number of channels
  901. */
  902. static inline unsigned int params_channels(const struct snd_pcm_hw_params *p)
  903. {
  904. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_CHANNELS)->min;
  905. }
  906. /**
  907. * params_rate - Get the sample rate from the hw params
  908. * @p: hw params
  909. *
  910. * Return: the sample rate
  911. */
  912. static inline unsigned int params_rate(const struct snd_pcm_hw_params *p)
  913. {
  914. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_RATE)->min;
  915. }
  916. /**
  917. * params_period_size - Get the period size (in frames) from the hw params
  918. * @p: hw params
  919. *
  920. * Return: the period size in frames
  921. */
  922. static inline unsigned int params_period_size(const struct snd_pcm_hw_params *p)
  923. {
  924. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min;
  925. }
  926. /**
  927. * params_periods - Get the number of periods from the hw params
  928. * @p: hw params
  929. *
  930. * Return: the number of periods
  931. */
  932. static inline unsigned int params_periods(const struct snd_pcm_hw_params *p)
  933. {
  934. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIODS)->min;
  935. }
  936. /**
  937. * params_buffer_size - Get the buffer size (in frames) from the hw params
  938. * @p: hw params
  939. *
  940. * Return: the buffer size in frames
  941. */
  942. static inline unsigned int params_buffer_size(const struct snd_pcm_hw_params *p)
  943. {
  944. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min;
  945. }
  946. /**
  947. * params_buffer_bytes - Get the buffer size (in bytes) from the hw params
  948. * @p: hw params
  949. *
  950. * Return: the buffer size in bytes
  951. */
  952. static inline unsigned int params_buffer_bytes(const struct snd_pcm_hw_params *p)
  953. {
  954. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min;
  955. }
  956. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v);
  957. int snd_interval_list(struct snd_interval *i, unsigned int count,
  958. const unsigned int *list, unsigned int mask);
  959. int snd_interval_ranges(struct snd_interval *i, unsigned int count,
  960. const struct snd_interval *list, unsigned int mask);
  961. int snd_interval_ratnum(struct snd_interval *i,
  962. unsigned int rats_count, const struct snd_ratnum *rats,
  963. unsigned int *nump, unsigned int *denp);
  964. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params);
  965. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var);
  966. int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
  967. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  968. u_int64_t mask);
  969. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  970. unsigned int min, unsigned int max);
  971. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var);
  972. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  973. unsigned int cond,
  974. snd_pcm_hw_param_t var,
  975. const struct snd_pcm_hw_constraint_list *l);
  976. int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
  977. unsigned int cond,
  978. snd_pcm_hw_param_t var,
  979. const struct snd_pcm_hw_constraint_ranges *r);
  980. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  981. unsigned int cond,
  982. snd_pcm_hw_param_t var,
  983. const struct snd_pcm_hw_constraint_ratnums *r);
  984. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  985. unsigned int cond,
  986. snd_pcm_hw_param_t var,
  987. const struct snd_pcm_hw_constraint_ratdens *r);
  988. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  989. unsigned int cond,
  990. unsigned int width,
  991. unsigned int msbits);
  992. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  993. unsigned int cond,
  994. snd_pcm_hw_param_t var,
  995. unsigned long step);
  996. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  997. unsigned int cond,
  998. snd_pcm_hw_param_t var);
  999. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  1000. unsigned int base_rate);
  1001. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
  1002. unsigned int cond,
  1003. int var,
  1004. snd_pcm_hw_rule_func_t func, void *private,
  1005. int dep, ...);
  1006. /**
  1007. * snd_pcm_hw_constraint_single() - Constrain parameter to a single value
  1008. * @runtime: PCM runtime instance
  1009. * @var: The hw_params variable to constrain
  1010. * @val: The value to constrain to
  1011. *
  1012. * Return: Positive if the value is changed, zero if it's not changed, or a
  1013. * negative error code.
  1014. */
  1015. static inline int snd_pcm_hw_constraint_single(
  1016. struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  1017. unsigned int val)
  1018. {
  1019. return snd_pcm_hw_constraint_minmax(runtime, var, val, val);
  1020. }
  1021. int snd_pcm_format_signed(snd_pcm_format_t format);
  1022. int snd_pcm_format_unsigned(snd_pcm_format_t format);
  1023. int snd_pcm_format_linear(snd_pcm_format_t format);
  1024. int snd_pcm_format_little_endian(snd_pcm_format_t format);
  1025. int snd_pcm_format_big_endian(snd_pcm_format_t format);
  1026. #if 0 /* just for kernel-doc */
  1027. /**
  1028. * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
  1029. * @format: the format to check
  1030. *
  1031. * Return: 1 if the given PCM format is CPU-endian, 0 if
  1032. * opposite, or a negative error code if endian not specified.
  1033. */
  1034. int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
  1035. #endif /* DocBook */
  1036. #ifdef SNDRV_LITTLE_ENDIAN
  1037. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
  1038. #else
  1039. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
  1040. #endif
  1041. int snd_pcm_format_width(snd_pcm_format_t format); /* in bits */
  1042. int snd_pcm_format_physical_width(snd_pcm_format_t format); /* in bits */
  1043. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
  1044. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format);
  1045. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *buf, unsigned int frames);
  1046. void snd_pcm_set_ops(struct snd_pcm * pcm, int direction,
  1047. const struct snd_pcm_ops *ops);
  1048. void snd_pcm_set_sync_per_card(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params,
  1049. const unsigned char *id, unsigned int len);
  1050. /**
  1051. * snd_pcm_set_sync - set the PCM sync id
  1052. * @substream: the pcm substream
  1053. *
  1054. * Use the default PCM sync identifier for the specific card.
  1055. */
  1056. static inline void snd_pcm_set_sync(struct snd_pcm_substream *substream)
  1057. {
  1058. substream->runtime->std_sync_id = true;
  1059. }
  1060. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  1061. unsigned int cmd, void *arg);
  1062. void snd_pcm_period_elapsed_under_stream_lock(struct snd_pcm_substream *substream);
  1063. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
  1064. snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream,
  1065. void *buf, bool interleaved,
  1066. snd_pcm_uframes_t frames, bool in_kernel);
  1067. static inline snd_pcm_sframes_t
  1068. snd_pcm_lib_write(struct snd_pcm_substream *substream,
  1069. const void __user *buf, snd_pcm_uframes_t frames)
  1070. {
  1071. return __snd_pcm_lib_xfer(substream, (void __force *)buf, true, frames, false);
  1072. }
  1073. static inline snd_pcm_sframes_t
  1074. snd_pcm_lib_read(struct snd_pcm_substream *substream,
  1075. void __user *buf, snd_pcm_uframes_t frames)
  1076. {
  1077. return __snd_pcm_lib_xfer(substream, (void __force *)buf, true, frames, false);
  1078. }
  1079. static inline snd_pcm_sframes_t
  1080. snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  1081. void __user **bufs, snd_pcm_uframes_t frames)
  1082. {
  1083. return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
  1084. }
  1085. static inline snd_pcm_sframes_t
  1086. snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  1087. void __user **bufs, snd_pcm_uframes_t frames)
  1088. {
  1089. return __snd_pcm_lib_xfer(substream, (void *)bufs, false, frames, false);
  1090. }
  1091. static inline snd_pcm_sframes_t
  1092. snd_pcm_kernel_write(struct snd_pcm_substream *substream,
  1093. const void *buf, snd_pcm_uframes_t frames)
  1094. {
  1095. return __snd_pcm_lib_xfer(substream, (void *)buf, true, frames, true);
  1096. }
  1097. static inline snd_pcm_sframes_t
  1098. snd_pcm_kernel_read(struct snd_pcm_substream *substream,
  1099. void *buf, snd_pcm_uframes_t frames)
  1100. {
  1101. return __snd_pcm_lib_xfer(substream, buf, true, frames, true);
  1102. }
  1103. static inline snd_pcm_sframes_t
  1104. snd_pcm_kernel_writev(struct snd_pcm_substream *substream,
  1105. void **bufs, snd_pcm_uframes_t frames)
  1106. {
  1107. return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
  1108. }
  1109. static inline snd_pcm_sframes_t
  1110. snd_pcm_kernel_readv(struct snd_pcm_substream *substream,
  1111. void **bufs, snd_pcm_uframes_t frames)
  1112. {
  1113. return __snd_pcm_lib_xfer(substream, bufs, false, frames, true);
  1114. }
  1115. int snd_pcm_hw_limit_rates(struct snd_pcm_hardware *hw);
  1116. static inline int
  1117. snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime)
  1118. {
  1119. return snd_pcm_hw_limit_rates(&runtime->hw);
  1120. }
  1121. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
  1122. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit);
  1123. unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
  1124. unsigned int rates_b);
  1125. /**
  1126. * snd_pcm_set_runtime_buffer - Set the PCM runtime buffer
  1127. * @substream: PCM substream to set
  1128. * @bufp: the buffer information, NULL to clear
  1129. *
  1130. * Copy the buffer information to runtime->dma_buffer when @bufp is non-NULL.
  1131. * Otherwise it clears the current buffer information.
  1132. */
  1133. static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
  1134. struct snd_dma_buffer *bufp)
  1135. {
  1136. struct snd_pcm_runtime *runtime = substream->runtime;
  1137. if (bufp) {
  1138. runtime->dma_buffer_p = bufp;
  1139. runtime->dma_area = bufp->area;
  1140. runtime->dma_addr = bufp->addr;
  1141. runtime->dma_bytes = bufp->bytes;
  1142. } else {
  1143. runtime->dma_buffer_p = NULL;
  1144. runtime->dma_area = NULL;
  1145. runtime->dma_addr = 0;
  1146. runtime->dma_bytes = 0;
  1147. }
  1148. }
  1149. /**
  1150. * snd_pcm_gettime - Fill the timespec64 depending on the timestamp mode
  1151. * @runtime: PCM runtime instance
  1152. * @tv: timespec64 to fill
  1153. */
  1154. static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
  1155. struct timespec64 *tv)
  1156. {
  1157. switch (runtime->tstamp_type) {
  1158. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
  1159. ktime_get_ts64(tv);
  1160. break;
  1161. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
  1162. ktime_get_raw_ts64(tv);
  1163. break;
  1164. default:
  1165. ktime_get_real_ts64(tv);
  1166. break;
  1167. }
  1168. }
  1169. /*
  1170. * Memory
  1171. */
  1172. void snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream);
  1173. void snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm);
  1174. void snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  1175. int type, struct device *data,
  1176. size_t size, size_t max);
  1177. void snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  1178. int type, void *data,
  1179. size_t size, size_t max);
  1180. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
  1181. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
  1182. int snd_pcm_set_managed_buffer(struct snd_pcm_substream *substream, int type,
  1183. struct device *data, size_t size, size_t max);
  1184. int snd_pcm_set_managed_buffer_all(struct snd_pcm *pcm, int type,
  1185. struct device *data,
  1186. size_t size, size_t max);
  1187. /**
  1188. * snd_pcm_set_fixed_buffer - Preallocate and set up the fixed size PCM buffer
  1189. * @substream: the pcm substream instance
  1190. * @type: DMA type (SNDRV_DMA_TYPE_*)
  1191. * @data: DMA type dependent data
  1192. * @size: the requested pre-allocation size in bytes
  1193. *
  1194. * This is a variant of snd_pcm_set_managed_buffer(), but this pre-allocates
  1195. * only the given sized buffer and doesn't allow re-allocation nor dynamic
  1196. * allocation of a larger buffer unlike the standard one.
  1197. * The function may return -ENOMEM error, hence the caller must check it.
  1198. *
  1199. * Return: zero if successful, or a negative error code
  1200. */
  1201. static inline int __must_check
  1202. snd_pcm_set_fixed_buffer(struct snd_pcm_substream *substream, int type,
  1203. struct device *data, size_t size)
  1204. {
  1205. return snd_pcm_set_managed_buffer(substream, type, data, size, 0);
  1206. }
  1207. /**
  1208. * snd_pcm_set_fixed_buffer_all - Preallocate and set up the fixed size PCM buffer
  1209. * @pcm: the pcm instance
  1210. * @type: DMA type (SNDRV_DMA_TYPE_*)
  1211. * @data: DMA type dependent data
  1212. * @size: the requested pre-allocation size in bytes
  1213. *
  1214. * Apply the set up of the fixed buffer via snd_pcm_set_fixed_buffer() for
  1215. * all substream. If any of allocation fails, it returns -ENOMEM, hence the
  1216. * caller must check the return value.
  1217. *
  1218. * Return: zero if successful, or a negative error code
  1219. */
  1220. static inline int __must_check
  1221. snd_pcm_set_fixed_buffer_all(struct snd_pcm *pcm, int type,
  1222. struct device *data, size_t size)
  1223. {
  1224. return snd_pcm_set_managed_buffer_all(pcm, type, data, size, 0);
  1225. }
  1226. #define snd_pcm_get_dma_buf(substream) ((substream)->runtime->dma_buffer_p)
  1227. /**
  1228. * snd_pcm_sgbuf_get_addr - Get the DMA address at the corresponding offset
  1229. * @substream: PCM substream
  1230. * @ofs: byte offset
  1231. *
  1232. * Return: DMA address
  1233. */
  1234. static inline dma_addr_t
  1235. snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
  1236. {
  1237. return snd_sgbuf_get_addr(snd_pcm_get_dma_buf(substream), ofs);
  1238. }
  1239. /**
  1240. * snd_pcm_sgbuf_get_chunk_size - Compute the max size that fits within the
  1241. * contig. page from the given size
  1242. * @substream: PCM substream
  1243. * @ofs: byte offset
  1244. * @size: byte size to examine
  1245. *
  1246. * Return: chunk size
  1247. */
  1248. static inline unsigned int
  1249. snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
  1250. unsigned int ofs, unsigned int size)
  1251. {
  1252. return snd_sgbuf_get_chunk_size(snd_pcm_get_dma_buf(substream), ofs, size);
  1253. }
  1254. int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
  1255. struct vm_area_struct *area);
  1256. /* mmap for io-memory area */
  1257. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  1258. #define SNDRV_PCM_INFO_MMAP_IOMEM SNDRV_PCM_INFO_MMAP
  1259. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
  1260. #else
  1261. #define SNDRV_PCM_INFO_MMAP_IOMEM 0
  1262. #define snd_pcm_lib_mmap_iomem NULL
  1263. #endif
  1264. int snd_pcm_runtime_buffer_set_silence(struct snd_pcm_runtime *runtime);
  1265. /**
  1266. * snd_pcm_limit_isa_dma_size - Get the max size fitting with ISA DMA transfer
  1267. * @dma: DMA number
  1268. * @max: pointer to store the max size
  1269. */
  1270. static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
  1271. {
  1272. *max = dma < 4 ? 64 * 1024 : 128 * 1024;
  1273. }
  1274. /*
  1275. * Misc
  1276. */
  1277. #define SNDRV_PCM_DEFAULT_CON_SPDIF (IEC958_AES0_CON_EMPHASIS_NONE|\
  1278. (IEC958_AES1_CON_ORIGINAL<<8)|\
  1279. (IEC958_AES1_CON_PCM_CODER<<8)|\
  1280. (IEC958_AES3_CON_FS_48000<<24))
  1281. const char *snd_pcm_format_name(snd_pcm_format_t format);
  1282. /**
  1283. * snd_pcm_direction_name - Get a string naming the direction of a stream
  1284. * @direction: Stream's direction, one of SNDRV_PCM_STREAM_XXX
  1285. *
  1286. * Returns a string naming the direction of the stream.
  1287. */
  1288. static inline const char *snd_pcm_direction_name(int direction)
  1289. {
  1290. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  1291. return "Playback";
  1292. else
  1293. return "Capture";
  1294. }
  1295. /**
  1296. * snd_pcm_stream_str - Get a string naming the direction of a stream
  1297. * @substream: the pcm substream instance
  1298. *
  1299. * Return: A string naming the direction of the stream.
  1300. */
  1301. static inline const char *snd_pcm_stream_str(struct snd_pcm_substream *substream)
  1302. {
  1303. return snd_pcm_direction_name(substream->stream);
  1304. }
  1305. /*
  1306. * PCM channel-mapping control API
  1307. */
  1308. /* array element of channel maps */
  1309. struct snd_pcm_chmap_elem {
  1310. unsigned char channels;
  1311. unsigned char map[15];
  1312. };
  1313. /* channel map information; retrieved via snd_kcontrol_chip() */
  1314. struct snd_pcm_chmap {
  1315. struct snd_pcm *pcm; /* assigned PCM instance */
  1316. int stream; /* PLAYBACK or CAPTURE */
  1317. struct snd_kcontrol *kctl;
  1318. const struct snd_pcm_chmap_elem *chmap;
  1319. unsigned int max_channels;
  1320. unsigned int channel_mask; /* optional: active channels bitmask */
  1321. void *private_data; /* optional: private data pointer */
  1322. };
  1323. /**
  1324. * snd_pcm_chmap_substream - get the PCM substream assigned to the given chmap info
  1325. * @info: chmap information
  1326. * @idx: the substream number index
  1327. *
  1328. * Return: the matched PCM substream, or NULL if not found
  1329. */
  1330. static inline struct snd_pcm_substream *
  1331. snd_pcm_chmap_substream(struct snd_pcm_chmap *info, unsigned int idx)
  1332. {
  1333. struct snd_pcm_substream *s;
  1334. for (s = info->pcm->streams[info->stream].substream; s; s = s->next)
  1335. if (s->number == idx)
  1336. return s;
  1337. return NULL;
  1338. }
  1339. /* ALSA-standard channel maps (RL/RR prior to C/LFE) */
  1340. extern const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[];
  1341. /* Other world's standard channel maps (C/LFE prior to RL/RR) */
  1342. extern const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[];
  1343. /* bit masks to be passed to snd_pcm_chmap.channel_mask field */
  1344. #define SND_PCM_CHMAP_MASK_24 ((1U << 2) | (1U << 4))
  1345. #define SND_PCM_CHMAP_MASK_246 (SND_PCM_CHMAP_MASK_24 | (1U << 6))
  1346. #define SND_PCM_CHMAP_MASK_2468 (SND_PCM_CHMAP_MASK_246 | (1U << 8))
  1347. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  1348. const struct snd_pcm_chmap_elem *chmap,
  1349. int max_channels,
  1350. unsigned long private_value,
  1351. struct snd_pcm_chmap **info_ret);
  1352. /**
  1353. * pcm_format_to_bits - Strong-typed conversion of pcm_format to bitwise
  1354. * @pcm_format: PCM format
  1355. *
  1356. * Return: 64bit mask corresponding to the given PCM format
  1357. */
  1358. static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
  1359. {
  1360. return 1ULL << (__force int) pcm_format;
  1361. }
  1362. /**
  1363. * pcm_for_each_format - helper to iterate for each format type
  1364. * @f: the iterator variable in snd_pcm_format_t type
  1365. */
  1366. #define pcm_for_each_format(f) \
  1367. for ((f) = SNDRV_PCM_FORMAT_FIRST; \
  1368. (__force int)(f) <= (__force int)SNDRV_PCM_FORMAT_LAST; \
  1369. (f) = (__force snd_pcm_format_t)((__force int)(f) + 1))
  1370. /* printk helpers */
  1371. #define pcm_err(pcm, fmt, args...) \
  1372. dev_err((pcm)->card->dev, fmt, ##args)
  1373. #define pcm_warn(pcm, fmt, args...) \
  1374. dev_warn((pcm)->card->dev, fmt, ##args)
  1375. #define pcm_dbg(pcm, fmt, args...) \
  1376. dev_dbg((pcm)->card->dev, fmt, ##args)
  1377. /* helpers for copying between iov_iter and iomem */
  1378. size_t copy_to_iter_fromio(const void __iomem *src, size_t bytes,
  1379. struct iov_iter *iter) __must_check;
  1380. size_t copy_from_iter_toio(void __iomem *dst, size_t bytes,
  1381. struct iov_iter *iter) __must_check;
  1382. struct snd_pcm_status64 {
  1383. snd_pcm_state_t state; /* stream state */
  1384. u8 rsvd[4];
  1385. s64 trigger_tstamp_sec; /* time when stream was started/stopped/paused */
  1386. s64 trigger_tstamp_nsec;
  1387. s64 tstamp_sec; /* reference timestamp */
  1388. s64 tstamp_nsec;
  1389. snd_pcm_uframes_t appl_ptr; /* appl ptr */
  1390. snd_pcm_uframes_t hw_ptr; /* hw ptr */
  1391. snd_pcm_sframes_t delay; /* current delay in frames */
  1392. snd_pcm_uframes_t avail; /* number of frames available */
  1393. snd_pcm_uframes_t avail_max; /* max frames available on hw since last status */
  1394. snd_pcm_uframes_t overrange; /* count of ADC (capture) overrange detections from last status */
  1395. snd_pcm_state_t suspended_state; /* suspended stream state */
  1396. __u32 audio_tstamp_data; /* needed for 64-bit alignment, used for configs/report to/from userspace */
  1397. s64 audio_tstamp_sec; /* sample counter, wall clock, PHC or on-demand sync'ed */
  1398. s64 audio_tstamp_nsec;
  1399. s64 driver_tstamp_sec; /* useful in case reference system tstamp is reported with delay */
  1400. s64 driver_tstamp_nsec;
  1401. __u32 audio_tstamp_accuracy; /* in ns units, only valid if indicated in audio_tstamp_data */
  1402. unsigned char reserved[52-4*sizeof(s64)]; /* must be filled with zero */
  1403. };
  1404. #define SNDRV_PCM_IOCTL_STATUS64 _IOR('A', 0x20, struct snd_pcm_status64)
  1405. #define SNDRV_PCM_IOCTL_STATUS_EXT64 _IOWR('A', 0x24, struct snd_pcm_status64)
  1406. struct snd_pcm_status32 {
  1407. snd_pcm_state_t state; /* stream state */
  1408. s32 trigger_tstamp_sec; /* time when stream was started/stopped/paused */
  1409. s32 trigger_tstamp_nsec;
  1410. s32 tstamp_sec; /* reference timestamp */
  1411. s32 tstamp_nsec;
  1412. u32 appl_ptr; /* appl ptr */
  1413. u32 hw_ptr; /* hw ptr */
  1414. s32 delay; /* current delay in frames */
  1415. u32 avail; /* number of frames available */
  1416. u32 avail_max; /* max frames available on hw since last status */
  1417. u32 overrange; /* count of ADC (capture) overrange detections from last status */
  1418. snd_pcm_state_t suspended_state; /* suspended stream state */
  1419. u32 audio_tstamp_data; /* needed for 64-bit alignment, used for configs/report to/from userspace */
  1420. s32 audio_tstamp_sec; /* sample counter, wall clock, PHC or on-demand sync'ed */
  1421. s32 audio_tstamp_nsec;
  1422. s32 driver_tstamp_sec; /* useful in case reference system tstamp is reported with delay */
  1423. s32 driver_tstamp_nsec;
  1424. u32 audio_tstamp_accuracy; /* in ns units, only valid if indicated in audio_tstamp_data */
  1425. unsigned char reserved[52-4*sizeof(s32)]; /* must be filled with zero */
  1426. };
  1427. #define SNDRV_PCM_IOCTL_STATUS32 _IOR('A', 0x20, struct snd_pcm_status32)
  1428. #define SNDRV_PCM_IOCTL_STATUS_EXT32 _IOWR('A', 0x24, struct snd_pcm_status32)
  1429. #endif /* __SOUND_PCM_H */