pcm.h 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. #define SNDRV_PCM_IOCTL1_INFO 1
  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. struct snd_ratnum *rats;
  241. };
  242. struct snd_pcm_hw_constraint_ratdens {
  243. int nrats;
  244. 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. struct snd_pcm_hwptr_log;
  257. /*
  258. * userspace-provided audio timestamp config to kernel,
  259. * structure is for internal use only and filled with dedicated unpack routine
  260. */
  261. struct snd_pcm_audio_tstamp_config {
  262. /* 5 of max 16 bits used */
  263. u32 type_requested:4;
  264. u32 report_delay:1; /* add total delay to A/D or D/A */
  265. };
  266. static inline void snd_pcm_unpack_audio_tstamp_config(__u32 data,
  267. struct snd_pcm_audio_tstamp_config *config)
  268. {
  269. config->type_requested = data & 0xF;
  270. config->report_delay = (data >> 4) & 1;
  271. }
  272. /*
  273. * kernel-provided audio timestamp report to user-space
  274. * structure is for internal use only and read by dedicated pack routine
  275. */
  276. struct snd_pcm_audio_tstamp_report {
  277. /* 6 of max 16 bits used for bit-fields */
  278. /* for backwards compatibility */
  279. u32 valid:1;
  280. /* actual type if hardware could not support requested timestamp */
  281. u32 actual_type:4;
  282. /* accuracy represented in ns units */
  283. u32 accuracy_report:1; /* 0 if accuracy unknown, 1 if accuracy field is valid */
  284. u32 accuracy; /* up to 4.29s, will be packed in separate field */
  285. };
  286. static inline void snd_pcm_pack_audio_tstamp_report(__u32 *data, __u32 *accuracy,
  287. const struct snd_pcm_audio_tstamp_report *report)
  288. {
  289. u32 tmp;
  290. tmp = report->accuracy_report;
  291. tmp <<= 4;
  292. tmp |= report->actual_type;
  293. tmp <<= 1;
  294. tmp |= report->valid;
  295. *data &= 0xffff; /* zero-clear MSBs */
  296. *data |= (tmp << 16);
  297. *accuracy = report->accuracy;
  298. }
  299. struct snd_pcm_runtime {
  300. /* -- Status -- */
  301. struct snd_pcm_substream *trigger_master;
  302. struct timespec trigger_tstamp; /* trigger timestamp */
  303. bool trigger_tstamp_latched; /* trigger timestamp latched in low-level driver/hardware */
  304. int overrange;
  305. snd_pcm_uframes_t avail_max;
  306. snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
  307. snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
  308. unsigned long hw_ptr_jiffies; /* Time when hw_ptr is updated */
  309. unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
  310. snd_pcm_sframes_t delay; /* extra delay; typically FIFO size */
  311. u64 hw_ptr_wrap; /* offset for hw_ptr due to boundary wrap-around */
  312. /* -- HW params -- */
  313. snd_pcm_access_t access; /* access mode */
  314. snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
  315. snd_pcm_subformat_t subformat; /* subformat */
  316. unsigned int rate; /* rate in Hz */
  317. unsigned int channels; /* channels */
  318. snd_pcm_uframes_t period_size; /* period size */
  319. unsigned int periods; /* periods */
  320. snd_pcm_uframes_t buffer_size; /* buffer size */
  321. snd_pcm_uframes_t min_align; /* Min alignment for the format */
  322. size_t byte_align;
  323. unsigned int frame_bits;
  324. unsigned int sample_bits;
  325. unsigned int info;
  326. unsigned int rate_num;
  327. unsigned int rate_den;
  328. unsigned int no_period_wakeup: 1;
  329. /* -- SW params -- */
  330. int tstamp_mode; /* mmap timestamp is updated */
  331. unsigned int period_step;
  332. snd_pcm_uframes_t start_threshold;
  333. snd_pcm_uframes_t stop_threshold;
  334. snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
  335. noise is nearest than this */
  336. snd_pcm_uframes_t silence_size; /* Silence filling size */
  337. snd_pcm_uframes_t boundary; /* pointers wrap point */
  338. snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
  339. snd_pcm_uframes_t silence_filled; /* size filled with silence */
  340. union snd_pcm_sync_id sync; /* hardware synchronization ID */
  341. /* -- mmap -- */
  342. struct snd_pcm_mmap_status *status;
  343. struct snd_pcm_mmap_control *control;
  344. /* -- locking / scheduling -- */
  345. snd_pcm_uframes_t twake; /* do transfer (!poll) wakeup if non-zero */
  346. wait_queue_head_t sleep; /* poll sleep */
  347. wait_queue_head_t tsleep; /* transfer sleep */
  348. struct fasync_struct *fasync;
  349. /* -- private section -- */
  350. void *private_data;
  351. void (*private_free)(struct snd_pcm_runtime *runtime);
  352. /* -- hardware description -- */
  353. struct snd_pcm_hardware hw;
  354. struct snd_pcm_hw_constraints hw_constraints;
  355. /* -- interrupt callbacks -- */
  356. void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
  357. void (*transfer_ack_end)(struct snd_pcm_substream *substream);
  358. /* -- timer -- */
  359. unsigned int timer_resolution; /* timer resolution */
  360. int tstamp_type; /* timestamp type */
  361. /* -- DMA -- */
  362. unsigned char *dma_area; /* DMA area */
  363. dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
  364. size_t dma_bytes; /* size of DMA area */
  365. struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
  366. /* -- audio timestamp config -- */
  367. struct snd_pcm_audio_tstamp_config audio_tstamp_config;
  368. struct snd_pcm_audio_tstamp_report audio_tstamp_report;
  369. struct timespec driver_tstamp;
  370. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  371. /* -- OSS things -- */
  372. struct snd_pcm_oss_runtime oss;
  373. #endif
  374. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  375. struct snd_pcm_hwptr_log *hwptr_log;
  376. #endif
  377. };
  378. struct snd_pcm_group { /* keep linked substreams */
  379. spinlock_t lock;
  380. struct mutex mutex;
  381. struct list_head substreams;
  382. int count;
  383. };
  384. struct pid;
  385. struct snd_pcm_substream {
  386. struct snd_pcm *pcm;
  387. struct snd_pcm_str *pstr;
  388. void *private_data; /* copied from pcm->private_data */
  389. int number;
  390. char name[32]; /* substream name */
  391. int stream; /* stream (direction) */
  392. struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
  393. size_t buffer_bytes_max; /* limit ring buffer size */
  394. struct snd_dma_buffer dma_buffer;
  395. size_t dma_max;
  396. /* -- hardware operations -- */
  397. const struct snd_pcm_ops *ops;
  398. /* -- runtime information -- */
  399. struct snd_pcm_runtime *runtime;
  400. /* -- timer section -- */
  401. struct snd_timer *timer; /* timer */
  402. unsigned timer_running: 1; /* time is running */
  403. /* -- next substream -- */
  404. struct snd_pcm_substream *next;
  405. /* -- linked substreams -- */
  406. struct list_head link_list; /* linked list member */
  407. struct snd_pcm_group self_group; /* fake group for non linked substream (with substream lock inside) */
  408. struct snd_pcm_group *group; /* pointer to current group */
  409. /* -- assigned files -- */
  410. void *file;
  411. int ref_count;
  412. atomic_t mmap_count;
  413. unsigned int f_flags;
  414. void (*pcm_release)(struct snd_pcm_substream *);
  415. struct pid *pid;
  416. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  417. /* -- OSS things -- */
  418. struct snd_pcm_oss_substream oss;
  419. #endif
  420. #ifdef CONFIG_SND_VERBOSE_PROCFS
  421. struct snd_info_entry *proc_root;
  422. struct snd_info_entry *proc_info_entry;
  423. struct snd_info_entry *proc_hw_params_entry;
  424. struct snd_info_entry *proc_sw_params_entry;
  425. struct snd_info_entry *proc_status_entry;
  426. struct snd_info_entry *proc_prealloc_entry;
  427. struct snd_info_entry *proc_prealloc_max_entry;
  428. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  429. struct snd_info_entry *proc_xrun_injection_entry;
  430. #endif
  431. #endif /* CONFIG_SND_VERBOSE_PROCFS */
  432. /* misc flags */
  433. unsigned int hw_opened: 1;
  434. };
  435. #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
  436. struct snd_pcm_str {
  437. int stream; /* stream (direction) */
  438. struct snd_pcm *pcm;
  439. /* -- substreams -- */
  440. unsigned int substream_count;
  441. unsigned int substream_opened;
  442. struct snd_pcm_substream *substream;
  443. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  444. /* -- OSS things -- */
  445. struct snd_pcm_oss_stream oss;
  446. #endif
  447. #ifdef CONFIG_SND_VERBOSE_PROCFS
  448. struct snd_info_entry *proc_root;
  449. struct snd_info_entry *proc_info_entry;
  450. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  451. unsigned int xrun_debug; /* 0 = disabled, 1 = verbose, 2 = stacktrace */
  452. struct snd_info_entry *proc_xrun_debug_entry;
  453. #endif
  454. #endif
  455. struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
  456. struct device dev;
  457. };
  458. struct snd_pcm {
  459. struct snd_card *card;
  460. struct list_head list;
  461. int device; /* device number */
  462. unsigned int info_flags;
  463. unsigned short dev_class;
  464. unsigned short dev_subclass;
  465. char id[64];
  466. char name[80];
  467. struct snd_pcm_str streams[2];
  468. struct mutex open_mutex;
  469. wait_queue_head_t open_wait;
  470. void *private_data;
  471. void (*private_free) (struct snd_pcm *pcm);
  472. bool internal; /* pcm is for internal use only */
  473. bool nonatomic; /* whole PCM operations are in non-atomic context */
  474. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  475. struct snd_pcm_oss oss;
  476. #endif
  477. };
  478. struct snd_pcm_notify {
  479. int (*n_register) (struct snd_pcm * pcm);
  480. int (*n_disconnect) (struct snd_pcm * pcm);
  481. int (*n_unregister) (struct snd_pcm * pcm);
  482. struct list_head list;
  483. };
  484. /*
  485. * Registering
  486. */
  487. extern const struct file_operations snd_pcm_f_ops[2];
  488. int snd_pcm_new(struct snd_card *card, const char *id, int device,
  489. int playback_count, int capture_count,
  490. struct snd_pcm **rpcm);
  491. int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
  492. int playback_count, int capture_count,
  493. struct snd_pcm **rpcm);
  494. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
  495. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
  496. /*
  497. * Native I/O
  498. */
  499. int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
  500. int snd_pcm_info_user(struct snd_pcm_substream *substream,
  501. struct snd_pcm_info __user *info);
  502. int snd_pcm_status(struct snd_pcm_substream *substream,
  503. struct snd_pcm_status *status);
  504. int snd_pcm_start(struct snd_pcm_substream *substream);
  505. int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
  506. int snd_pcm_drain_done(struct snd_pcm_substream *substream);
  507. int snd_pcm_stop_xrun(struct snd_pcm_substream *substream);
  508. #ifdef CONFIG_PM
  509. int snd_pcm_suspend(struct snd_pcm_substream *substream);
  510. int snd_pcm_suspend_all(struct snd_pcm *pcm);
  511. #endif
  512. int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
  513. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
  514. struct snd_pcm_substream **rsubstream);
  515. void snd_pcm_release_substream(struct snd_pcm_substream *substream);
  516. int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
  517. struct snd_pcm_substream **rsubstream);
  518. void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
  519. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
  520. #ifdef CONFIG_SND_DEBUG
  521. void snd_pcm_debug_name(struct snd_pcm_substream *substream,
  522. char *name, size_t len);
  523. #else
  524. static inline void
  525. snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
  526. {
  527. *buf = 0;
  528. }
  529. #endif
  530. /*
  531. * PCM library
  532. */
  533. /**
  534. * snd_pcm_stream_linked - Check whether the substream is linked with others
  535. * @substream: substream to check
  536. *
  537. * Returns true if the given substream is being linked with others.
  538. */
  539. static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
  540. {
  541. return substream->group != &substream->self_group;
  542. }
  543. void snd_pcm_stream_lock(struct snd_pcm_substream *substream);
  544. void snd_pcm_stream_unlock(struct snd_pcm_substream *substream);
  545. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream);
  546. void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream);
  547. unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream);
  548. /**
  549. * snd_pcm_stream_lock_irqsave - Lock the PCM stream
  550. * @substream: PCM substream
  551. * @flags: irq flags
  552. *
  553. * This locks the PCM stream like snd_pcm_stream_lock() but with the local
  554. * IRQ (only when nonatomic is false). In nonatomic case, this is identical
  555. * as snd_pcm_stream_lock().
  556. */
  557. #define snd_pcm_stream_lock_irqsave(substream, flags) \
  558. do { \
  559. typecheck(unsigned long, flags); \
  560. flags = _snd_pcm_stream_lock_irqsave(substream); \
  561. } while (0)
  562. void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  563. unsigned long flags);
  564. /**
  565. * snd_pcm_group_for_each_entry - iterate over the linked substreams
  566. * @s: the iterator
  567. * @substream: the substream
  568. *
  569. * Iterate over the all linked substreams to the given @substream.
  570. * When @substream isn't linked with any others, this gives returns @substream
  571. * itself once.
  572. */
  573. #define snd_pcm_group_for_each_entry(s, substream) \
  574. list_for_each_entry(s, &substream->group->substreams, link_list)
  575. /**
  576. * snd_pcm_running - Check whether the substream is in a running state
  577. * @substream: substream to check
  578. *
  579. * Returns true if the given substream is in the state RUNNING, or in the
  580. * state DRAINING for playback.
  581. */
  582. static inline int snd_pcm_running(struct snd_pcm_substream *substream)
  583. {
  584. return (substream->runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
  585. (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  586. substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
  587. }
  588. /**
  589. * bytes_to_samples - Unit conversion of the size from bytes to samples
  590. * @runtime: PCM runtime instance
  591. * @size: size in bytes
  592. */
  593. static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
  594. {
  595. return size * 8 / runtime->sample_bits;
  596. }
  597. /**
  598. * bytes_to_frames - Unit conversion of the size from bytes to frames
  599. * @runtime: PCM runtime instance
  600. * @size: size in bytes
  601. */
  602. static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
  603. {
  604. return size * 8 / runtime->frame_bits;
  605. }
  606. /**
  607. * samples_to_bytes - Unit conversion of the size from samples to bytes
  608. * @runtime: PCM runtime instance
  609. * @size: size in samples
  610. */
  611. static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
  612. {
  613. return size * runtime->sample_bits / 8;
  614. }
  615. /**
  616. * frames_to_bytes - Unit conversion of the size from frames to bytes
  617. * @runtime: PCM runtime instance
  618. * @size: size in frames
  619. */
  620. static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
  621. {
  622. return size * runtime->frame_bits / 8;
  623. }
  624. /**
  625. * frame_aligned - Check whether the byte size is aligned to frames
  626. * @runtime: PCM runtime instance
  627. * @bytes: size in bytes
  628. */
  629. static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
  630. {
  631. return bytes % runtime->byte_align == 0;
  632. }
  633. /**
  634. * snd_pcm_lib_buffer_bytes - Get the buffer size of the current PCM in bytes
  635. * @substream: PCM substream
  636. */
  637. static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
  638. {
  639. struct snd_pcm_runtime *runtime = substream->runtime;
  640. return frames_to_bytes(runtime, runtime->buffer_size);
  641. }
  642. /**
  643. * snd_pcm_lib_period_bytes - Get the period size of the current PCM in bytes
  644. * @substream: PCM substream
  645. */
  646. static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
  647. {
  648. struct snd_pcm_runtime *runtime = substream->runtime;
  649. return frames_to_bytes(runtime, runtime->period_size);
  650. }
  651. /**
  652. * snd_pcm_playback_avail - Get the available (writable) space for playback
  653. * @runtime: PCM runtime instance
  654. *
  655. * Result is between 0 ... (boundary - 1)
  656. */
  657. static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
  658. {
  659. snd_pcm_sframes_t avail = runtime->status->hw_ptr + runtime->buffer_size - runtime->control->appl_ptr;
  660. if (avail < 0)
  661. avail += runtime->boundary;
  662. else if ((snd_pcm_uframes_t) avail >= runtime->boundary)
  663. avail -= runtime->boundary;
  664. return avail;
  665. }
  666. /**
  667. * snd_pcm_playback_avail - Get the available (readable) space for capture
  668. * @runtime: PCM runtime instance
  669. *
  670. * Result is between 0 ... (boundary - 1)
  671. */
  672. static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
  673. {
  674. snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
  675. if (avail < 0)
  676. avail += runtime->boundary;
  677. return avail;
  678. }
  679. /**
  680. * snd_pcm_playback_hw_avail - Get the queued space for playback
  681. * @runtime: PCM runtime instance
  682. */
  683. static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
  684. {
  685. return runtime->buffer_size - snd_pcm_playback_avail(runtime);
  686. }
  687. /**
  688. * snd_pcm_capture_hw_avail - Get the free space for capture
  689. * @runtime: PCM runtime instance
  690. */
  691. static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
  692. {
  693. return runtime->buffer_size - snd_pcm_capture_avail(runtime);
  694. }
  695. /**
  696. * snd_pcm_playback_ready - check whether the playback buffer is available
  697. * @substream: the pcm substream instance
  698. *
  699. * Checks whether enough free space is available on the playback buffer.
  700. *
  701. * Return: Non-zero if available, or zero if not.
  702. */
  703. static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
  704. {
  705. struct snd_pcm_runtime *runtime = substream->runtime;
  706. return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
  707. }
  708. /**
  709. * snd_pcm_capture_ready - check whether the capture buffer is available
  710. * @substream: the pcm substream instance
  711. *
  712. * Checks whether enough capture data is available on the capture buffer.
  713. *
  714. * Return: Non-zero if available, or zero if not.
  715. */
  716. static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
  717. {
  718. struct snd_pcm_runtime *runtime = substream->runtime;
  719. return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
  720. }
  721. /**
  722. * snd_pcm_playback_data - check whether any data exists on the playback buffer
  723. * @substream: the pcm substream instance
  724. *
  725. * Checks whether any data exists on the playback buffer.
  726. *
  727. * Return: Non-zero if any data exists, or zero if not. If stop_threshold
  728. * is bigger or equal to boundary, then this function returns always non-zero.
  729. */
  730. static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
  731. {
  732. struct snd_pcm_runtime *runtime = substream->runtime;
  733. if (runtime->stop_threshold >= runtime->boundary)
  734. return 1;
  735. return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
  736. }
  737. /**
  738. * snd_pcm_playback_empty - check whether the playback buffer is empty
  739. * @substream: the pcm substream instance
  740. *
  741. * Checks whether the playback buffer is empty.
  742. *
  743. * Return: Non-zero if empty, or zero if not.
  744. */
  745. static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
  746. {
  747. struct snd_pcm_runtime *runtime = substream->runtime;
  748. return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
  749. }
  750. /**
  751. * snd_pcm_capture_empty - check whether the capture buffer is empty
  752. * @substream: the pcm substream instance
  753. *
  754. * Checks whether the capture buffer is empty.
  755. *
  756. * Return: Non-zero if empty, or zero if not.
  757. */
  758. static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
  759. {
  760. struct snd_pcm_runtime *runtime = substream->runtime;
  761. return snd_pcm_capture_avail(runtime) == 0;
  762. }
  763. /**
  764. * snd_pcm_trigger_done - Mark the master substream
  765. * @substream: the pcm substream instance
  766. * @master: the linked master substream
  767. *
  768. * When multiple substreams of the same card are linked and the hardware
  769. * supports the single-shot operation, the driver calls this in the loop
  770. * in snd_pcm_group_for_each_entry() for marking the substream as "done".
  771. * Then most of trigger operations are performed only to the given master
  772. * substream.
  773. *
  774. * The trigger_master mark is cleared at timestamp updates at the end
  775. * of trigger operations.
  776. */
  777. static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
  778. struct snd_pcm_substream *master)
  779. {
  780. substream->runtime->trigger_master = master;
  781. }
  782. static inline int hw_is_mask(int var)
  783. {
  784. return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
  785. var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
  786. }
  787. static inline int hw_is_interval(int var)
  788. {
  789. return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
  790. var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
  791. }
  792. static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
  793. snd_pcm_hw_param_t var)
  794. {
  795. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  796. }
  797. static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
  798. snd_pcm_hw_param_t var)
  799. {
  800. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  801. }
  802. static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
  803. snd_pcm_hw_param_t var)
  804. {
  805. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  806. }
  807. static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
  808. snd_pcm_hw_param_t var)
  809. {
  810. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  811. }
  812. /**
  813. * params_channels - Get the number of channels from the hw params
  814. * @p: hw params
  815. */
  816. static inline unsigned int params_channels(const struct snd_pcm_hw_params *p)
  817. {
  818. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_CHANNELS)->min;
  819. }
  820. /**
  821. * params_rate - Get the sample rate from the hw params
  822. * @p: hw params
  823. */
  824. static inline unsigned int params_rate(const struct snd_pcm_hw_params *p)
  825. {
  826. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_RATE)->min;
  827. }
  828. /**
  829. * params_period_size - Get the period size (in frames) from the hw params
  830. * @p: hw params
  831. */
  832. static inline unsigned int params_period_size(const struct snd_pcm_hw_params *p)
  833. {
  834. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min;
  835. }
  836. /**
  837. * params_periods - Get the number of periods from the hw params
  838. * @p: hw params
  839. */
  840. static inline unsigned int params_periods(const struct snd_pcm_hw_params *p)
  841. {
  842. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_PERIODS)->min;
  843. }
  844. /**
  845. * params_buffer_size - Get the buffer size (in frames) from the hw params
  846. * @p: hw params
  847. */
  848. static inline unsigned int params_buffer_size(const struct snd_pcm_hw_params *p)
  849. {
  850. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min;
  851. }
  852. /**
  853. * params_buffer_bytes - Get the buffer size (in bytes) from the hw params
  854. * @p: hw params
  855. */
  856. static inline unsigned int params_buffer_bytes(const struct snd_pcm_hw_params *p)
  857. {
  858. return hw_param_interval_c(p, SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min;
  859. }
  860. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v);
  861. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
  862. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
  863. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  864. unsigned int k, struct snd_interval *c);
  865. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  866. const struct snd_interval *b, struct snd_interval *c);
  867. int snd_interval_list(struct snd_interval *i, unsigned int count,
  868. const unsigned int *list, unsigned int mask);
  869. int snd_interval_ranges(struct snd_interval *i, unsigned int count,
  870. const struct snd_interval *list, unsigned int mask);
  871. int snd_interval_ratnum(struct snd_interval *i,
  872. unsigned int rats_count, struct snd_ratnum *rats,
  873. unsigned int *nump, unsigned int *denp);
  874. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params);
  875. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var);
  876. int snd_pcm_hw_params_choose(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
  877. int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
  878. int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream);
  879. int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream);
  880. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  881. u_int32_t mask);
  882. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  883. u_int64_t mask);
  884. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  885. unsigned int min, unsigned int max);
  886. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var);
  887. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  888. unsigned int cond,
  889. snd_pcm_hw_param_t var,
  890. const struct snd_pcm_hw_constraint_list *l);
  891. int snd_pcm_hw_constraint_ranges(struct snd_pcm_runtime *runtime,
  892. unsigned int cond,
  893. snd_pcm_hw_param_t var,
  894. const struct snd_pcm_hw_constraint_ranges *r);
  895. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  896. unsigned int cond,
  897. snd_pcm_hw_param_t var,
  898. struct snd_pcm_hw_constraint_ratnums *r);
  899. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  900. unsigned int cond,
  901. snd_pcm_hw_param_t var,
  902. struct snd_pcm_hw_constraint_ratdens *r);
  903. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  904. unsigned int cond,
  905. unsigned int width,
  906. unsigned int msbits);
  907. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  908. unsigned int cond,
  909. snd_pcm_hw_param_t var,
  910. unsigned long step);
  911. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  912. unsigned int cond,
  913. snd_pcm_hw_param_t var);
  914. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  915. unsigned int base_rate);
  916. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
  917. unsigned int cond,
  918. int var,
  919. snd_pcm_hw_rule_func_t func, void *private,
  920. int dep, ...);
  921. int snd_pcm_format_signed(snd_pcm_format_t format);
  922. int snd_pcm_format_unsigned(snd_pcm_format_t format);
  923. int snd_pcm_format_linear(snd_pcm_format_t format);
  924. int snd_pcm_format_little_endian(snd_pcm_format_t format);
  925. int snd_pcm_format_big_endian(snd_pcm_format_t format);
  926. #if 0 /* just for DocBook */
  927. /**
  928. * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
  929. * @format: the format to check
  930. *
  931. * Return: 1 if the given PCM format is CPU-endian, 0 if
  932. * opposite, or a negative error code if endian not specified.
  933. */
  934. int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
  935. #endif /* DocBook */
  936. #ifdef SNDRV_LITTLE_ENDIAN
  937. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
  938. #else
  939. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
  940. #endif
  941. int snd_pcm_format_width(snd_pcm_format_t format); /* in bits */
  942. int snd_pcm_format_physical_width(snd_pcm_format_t format); /* in bits */
  943. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
  944. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format);
  945. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *buf, unsigned int frames);
  946. void snd_pcm_set_ops(struct snd_pcm * pcm, int direction,
  947. const struct snd_pcm_ops *ops);
  948. void snd_pcm_set_sync(struct snd_pcm_substream *substream);
  949. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  950. unsigned int cmd, void *arg);
  951. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  952. struct snd_pcm_runtime *runtime);
  953. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
  954. void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr);
  955. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
  956. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream,
  957. const void __user *buf,
  958. snd_pcm_uframes_t frames);
  959. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream,
  960. void __user *buf, snd_pcm_uframes_t frames);
  961. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  962. void __user **bufs, snd_pcm_uframes_t frames);
  963. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  964. void __user **bufs, snd_pcm_uframes_t frames);
  965. extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;
  966. int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime);
  967. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
  968. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit);
  969. unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
  970. unsigned int rates_b);
  971. /**
  972. * snd_pcm_set_runtime_buffer - Set the PCM runtime buffer
  973. * @substream: PCM substream to set
  974. * @bufp: the buffer information, NULL to clear
  975. *
  976. * Copy the buffer information to runtime->dma_buffer when @bufp is non-NULL.
  977. * Otherwise it clears the current buffer information.
  978. */
  979. static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
  980. struct snd_dma_buffer *bufp)
  981. {
  982. struct snd_pcm_runtime *runtime = substream->runtime;
  983. if (bufp) {
  984. runtime->dma_buffer_p = bufp;
  985. runtime->dma_area = bufp->area;
  986. runtime->dma_addr = bufp->addr;
  987. runtime->dma_bytes = bufp->bytes;
  988. } else {
  989. runtime->dma_buffer_p = NULL;
  990. runtime->dma_area = NULL;
  991. runtime->dma_addr = 0;
  992. runtime->dma_bytes = 0;
  993. }
  994. }
  995. /*
  996. * Timer interface
  997. */
  998. void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream);
  999. void snd_pcm_timer_init(struct snd_pcm_substream *substream);
  1000. void snd_pcm_timer_done(struct snd_pcm_substream *substream);
  1001. /**
  1002. * snd_pcm_gettime - Fill the timespec depending on the timestamp mode
  1003. * @runtime: PCM runtime instance
  1004. * @tv: timespec to fill
  1005. */
  1006. static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
  1007. struct timespec *tv)
  1008. {
  1009. switch (runtime->tstamp_type) {
  1010. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
  1011. ktime_get_ts(tv);
  1012. break;
  1013. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
  1014. getrawmonotonic(tv);
  1015. break;
  1016. default:
  1017. getnstimeofday(tv);
  1018. break;
  1019. }
  1020. }
  1021. /*
  1022. * Memory
  1023. */
  1024. int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream);
  1025. int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm);
  1026. int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  1027. int type, struct device *data,
  1028. size_t size, size_t max);
  1029. int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  1030. int type, void *data,
  1031. size_t size, size_t max);
  1032. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
  1033. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
  1034. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  1035. size_t size, gfp_t gfp_flags);
  1036. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream);
  1037. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  1038. unsigned long offset);
  1039. /**
  1040. * snd_pcm_lib_alloc_vmalloc_buffer - allocate virtual DMA buffer
  1041. * @substream: the substream to allocate the buffer to
  1042. * @size: the requested buffer size, in bytes
  1043. *
  1044. * Allocates the PCM substream buffer using vmalloc(), i.e., the memory is
  1045. * contiguous in kernel virtual space, but not in physical memory. Use this
  1046. * if the buffer is accessed by kernel code but not by device DMA.
  1047. *
  1048. * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
  1049. * code.
  1050. */
  1051. static inline int snd_pcm_lib_alloc_vmalloc_buffer
  1052. (struct snd_pcm_substream *substream, size_t size)
  1053. {
  1054. return _snd_pcm_lib_alloc_vmalloc_buffer(substream, size,
  1055. GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO);
  1056. }
  1057. /**
  1058. * snd_pcm_lib_alloc_vmalloc_32_buffer - allocate 32-bit-addressable buffer
  1059. * @substream: the substream to allocate the buffer to
  1060. * @size: the requested buffer size, in bytes
  1061. *
  1062. * This function works like snd_pcm_lib_alloc_vmalloc_buffer(), but uses
  1063. * vmalloc_32(), i.e., the pages are allocated from 32-bit-addressable memory.
  1064. *
  1065. * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
  1066. * code.
  1067. */
  1068. static inline int snd_pcm_lib_alloc_vmalloc_32_buffer
  1069. (struct snd_pcm_substream *substream, size_t size)
  1070. {
  1071. return _snd_pcm_lib_alloc_vmalloc_buffer(substream, size,
  1072. GFP_KERNEL | GFP_DMA32 | __GFP_ZERO);
  1073. }
  1074. #define snd_pcm_get_dma_buf(substream) ((substream)->runtime->dma_buffer_p)
  1075. #ifdef CONFIG_SND_DMA_SGBUF
  1076. /*
  1077. * SG-buffer handling
  1078. */
  1079. #define snd_pcm_substream_sgbuf(substream) \
  1080. snd_pcm_get_dma_buf(substream)->private_data
  1081. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
  1082. unsigned long offset);
  1083. #else /* !SND_DMA_SGBUF */
  1084. /*
  1085. * fake using a continuous buffer
  1086. */
  1087. #define snd_pcm_sgbuf_ops_page NULL
  1088. #endif /* SND_DMA_SGBUF */
  1089. /**
  1090. * snd_pcm_sgbuf_get_addr - Get the DMA address at the corresponding offset
  1091. * @substream: PCM substream
  1092. * @ofs: byte offset
  1093. */
  1094. static inline dma_addr_t
  1095. snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
  1096. {
  1097. return snd_sgbuf_get_addr(snd_pcm_get_dma_buf(substream), ofs);
  1098. }
  1099. /**
  1100. * snd_pcm_sgbuf_get_ptr - Get the virtual address at the corresponding offset
  1101. * @substream: PCM substream
  1102. * @ofs: byte offset
  1103. */
  1104. static inline void *
  1105. snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
  1106. {
  1107. return snd_sgbuf_get_ptr(snd_pcm_get_dma_buf(substream), ofs);
  1108. }
  1109. /**
  1110. * snd_pcm_sgbuf_chunk_size - Compute the max size that fits within the contig.
  1111. * page from the given size
  1112. * @substream: PCM substream
  1113. * @ofs: byte offset
  1114. * @size: byte size to examine
  1115. */
  1116. static inline unsigned int
  1117. snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
  1118. unsigned int ofs, unsigned int size)
  1119. {
  1120. return snd_sgbuf_get_chunk_size(snd_pcm_get_dma_buf(substream), ofs, size);
  1121. }
  1122. /**
  1123. * snd_pcm_mmap_data_open - increase the mmap counter
  1124. * @area: VMA
  1125. *
  1126. * PCM mmap callback should handle this counter properly
  1127. */
  1128. static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
  1129. {
  1130. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
  1131. atomic_inc(&substream->mmap_count);
  1132. }
  1133. /**
  1134. * snd_pcm_mmap_data_close - decrease the mmap counter
  1135. * @area: VMA
  1136. *
  1137. * PCM mmap callback should handle this counter properly
  1138. */
  1139. static inline void snd_pcm_mmap_data_close(struct vm_area_struct *area)
  1140. {
  1141. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
  1142. atomic_dec(&substream->mmap_count);
  1143. }
  1144. int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
  1145. struct vm_area_struct *area);
  1146. /* mmap for io-memory area */
  1147. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  1148. #define SNDRV_PCM_INFO_MMAP_IOMEM SNDRV_PCM_INFO_MMAP
  1149. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
  1150. #else
  1151. #define SNDRV_PCM_INFO_MMAP_IOMEM 0
  1152. #define snd_pcm_lib_mmap_iomem NULL
  1153. #endif
  1154. #define snd_pcm_lib_mmap_vmalloc NULL
  1155. /**
  1156. * snd_pcm_limit_isa_dma_size - Get the max size fitting with ISA DMA transfer
  1157. * @dma: DMA number
  1158. * @max: pointer to store the max size
  1159. */
  1160. static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
  1161. {
  1162. *max = dma < 4 ? 64 * 1024 : 128 * 1024;
  1163. }
  1164. /*
  1165. * Misc
  1166. */
  1167. #define SNDRV_PCM_DEFAULT_CON_SPDIF (IEC958_AES0_CON_EMPHASIS_NONE|\
  1168. (IEC958_AES1_CON_ORIGINAL<<8)|\
  1169. (IEC958_AES1_CON_PCM_CODER<<8)|\
  1170. (IEC958_AES3_CON_FS_48000<<24))
  1171. #define PCM_RUNTIME_CHECK(sub) snd_BUG_ON(!(sub) || !(sub)->runtime)
  1172. const char *snd_pcm_format_name(snd_pcm_format_t format);
  1173. /**
  1174. * snd_pcm_stream_str - Get a string naming the direction of a stream
  1175. * @substream: the pcm substream instance
  1176. *
  1177. * Return: A string naming the direction of the stream.
  1178. */
  1179. static inline const char *snd_pcm_stream_str(struct snd_pcm_substream *substream)
  1180. {
  1181. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  1182. return "Playback";
  1183. else
  1184. return "Capture";
  1185. }
  1186. /*
  1187. * PCM channel-mapping control API
  1188. */
  1189. /* array element of channel maps */
  1190. struct snd_pcm_chmap_elem {
  1191. unsigned char channels;
  1192. unsigned char map[15];
  1193. };
  1194. /* channel map information; retrieved via snd_kcontrol_chip() */
  1195. struct snd_pcm_chmap {
  1196. struct snd_pcm *pcm; /* assigned PCM instance */
  1197. int stream; /* PLAYBACK or CAPTURE */
  1198. struct snd_kcontrol *kctl;
  1199. const struct snd_pcm_chmap_elem *chmap;
  1200. unsigned int max_channels;
  1201. unsigned int channel_mask; /* optional: active channels bitmask */
  1202. void *private_data; /* optional: private data pointer */
  1203. };
  1204. /**
  1205. * snd_pcm_chmap_substream - get the PCM substream assigned to the given chmap info
  1206. * @info: chmap information
  1207. * @idx: the substream number index
  1208. */
  1209. static inline struct snd_pcm_substream *
  1210. snd_pcm_chmap_substream(struct snd_pcm_chmap *info, unsigned int idx)
  1211. {
  1212. struct snd_pcm_substream *s;
  1213. for (s = info->pcm->streams[info->stream].substream; s; s = s->next)
  1214. if (s->number == idx)
  1215. return s;
  1216. return NULL;
  1217. }
  1218. /* ALSA-standard channel maps (RL/RR prior to C/LFE) */
  1219. extern const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[];
  1220. /* Other world's standard channel maps (C/LFE prior to RL/RR) */
  1221. extern const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[];
  1222. /* bit masks to be passed to snd_pcm_chmap.channel_mask field */
  1223. #define SND_PCM_CHMAP_MASK_24 ((1U << 2) | (1U << 4))
  1224. #define SND_PCM_CHMAP_MASK_246 (SND_PCM_CHMAP_MASK_24 | (1U << 6))
  1225. #define SND_PCM_CHMAP_MASK_2468 (SND_PCM_CHMAP_MASK_246 | (1U << 8))
  1226. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  1227. const struct snd_pcm_chmap_elem *chmap,
  1228. int max_channels,
  1229. unsigned long private_value,
  1230. struct snd_pcm_chmap **info_ret);
  1231. /**
  1232. * pcm_format_to_bits - Strong-typed conversion of pcm_format to bitwise
  1233. * @pcm_format: PCM format
  1234. */
  1235. static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
  1236. {
  1237. return 1ULL << (__force int) pcm_format;
  1238. }
  1239. /* printk helpers */
  1240. #define pcm_err(pcm, fmt, args...) \
  1241. dev_err((pcm)->card->dev, fmt, ##args)
  1242. #define pcm_warn(pcm, fmt, args...) \
  1243. dev_warn((pcm)->card->dev, fmt, ##args)
  1244. #define pcm_dbg(pcm, fmt, args...) \
  1245. dev_dbg((pcm)->card->dev, fmt, ##args)
  1246. #endif /* __SOUND_PCM_H */