pcm.h 47 KB

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