pcm.h 47 KB

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