pcm.h 47 KB

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