pcm.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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_ops {
  56. int (*open)(struct snd_pcm_substream *substream);
  57. int (*close)(struct snd_pcm_substream *substream);
  58. int (*ioctl)(struct snd_pcm_substream * substream,
  59. unsigned int cmd, void *arg);
  60. int (*hw_params)(struct snd_pcm_substream *substream,
  61. struct snd_pcm_hw_params *params);
  62. int (*hw_free)(struct snd_pcm_substream *substream);
  63. int (*prepare)(struct snd_pcm_substream *substream);
  64. int (*trigger)(struct snd_pcm_substream *substream, int cmd);
  65. snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
  66. int (*wall_clock)(struct snd_pcm_substream *substream,
  67. struct timespec *audio_ts);
  68. int (*copy)(struct snd_pcm_substream *substream, int channel,
  69. snd_pcm_uframes_t pos,
  70. void __user *buf, snd_pcm_uframes_t count);
  71. int (*silence)(struct snd_pcm_substream *substream, int channel,
  72. snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
  73. struct page *(*page)(struct snd_pcm_substream *substream,
  74. unsigned long offset);
  75. int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma);
  76. int (*ack)(struct snd_pcm_substream *substream);
  77. };
  78. /*
  79. *
  80. */
  81. #if defined(CONFIG_SND_DYNAMIC_MINORS)
  82. #define SNDRV_PCM_DEVICES (SNDRV_OS_MINORS-2)
  83. #else
  84. #define SNDRV_PCM_DEVICES 8
  85. #endif
  86. #define SNDRV_PCM_IOCTL1_FALSE ((void *)0)
  87. #define SNDRV_PCM_IOCTL1_TRUE ((void *)1)
  88. #define SNDRV_PCM_IOCTL1_RESET 0
  89. #define SNDRV_PCM_IOCTL1_INFO 1
  90. #define SNDRV_PCM_IOCTL1_CHANNEL_INFO 2
  91. #define SNDRV_PCM_IOCTL1_GSTATE 3
  92. #define SNDRV_PCM_IOCTL1_FIFO_SIZE 4
  93. #define SNDRV_PCM_TRIGGER_STOP 0
  94. #define SNDRV_PCM_TRIGGER_START 1
  95. #define SNDRV_PCM_TRIGGER_PAUSE_PUSH 3
  96. #define SNDRV_PCM_TRIGGER_PAUSE_RELEASE 4
  97. #define SNDRV_PCM_TRIGGER_SUSPEND 5
  98. #define SNDRV_PCM_TRIGGER_RESUME 6
  99. #define SNDRV_PCM_POS_XRUN ((snd_pcm_uframes_t)-1)
  100. /* If you change this don't forget to change rates[] table in pcm_native.c */
  101. #define SNDRV_PCM_RATE_5512 (1<<0) /* 5512Hz */
  102. #define SNDRV_PCM_RATE_8000 (1<<1) /* 8000Hz */
  103. #define SNDRV_PCM_RATE_11025 (1<<2) /* 11025Hz */
  104. #define SNDRV_PCM_RATE_16000 (1<<3) /* 16000Hz */
  105. #define SNDRV_PCM_RATE_22050 (1<<4) /* 22050Hz */
  106. #define SNDRV_PCM_RATE_32000 (1<<5) /* 32000Hz */
  107. #define SNDRV_PCM_RATE_44100 (1<<6) /* 44100Hz */
  108. #define SNDRV_PCM_RATE_48000 (1<<7) /* 48000Hz */
  109. #define SNDRV_PCM_RATE_64000 (1<<8) /* 64000Hz */
  110. #define SNDRV_PCM_RATE_88200 (1<<9) /* 88200Hz */
  111. #define SNDRV_PCM_RATE_96000 (1<<10) /* 96000Hz */
  112. #define SNDRV_PCM_RATE_176400 (1<<11) /* 176400Hz */
  113. #define SNDRV_PCM_RATE_192000 (1<<12) /* 192000Hz */
  114. #define SNDRV_PCM_RATE_CONTINUOUS (1<<30) /* continuous range */
  115. #define SNDRV_PCM_RATE_KNOT (1<<31) /* supports more non-continuos rates */
  116. #define SNDRV_PCM_RATE_8000_44100 (SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_11025|\
  117. SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_22050|\
  118. SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100)
  119. #define SNDRV_PCM_RATE_8000_48000 (SNDRV_PCM_RATE_8000_44100|SNDRV_PCM_RATE_48000)
  120. #define SNDRV_PCM_RATE_8000_96000 (SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_64000|\
  121. SNDRV_PCM_RATE_88200|SNDRV_PCM_RATE_96000)
  122. #define SNDRV_PCM_RATE_8000_192000 (SNDRV_PCM_RATE_8000_96000|SNDRV_PCM_RATE_176400|\
  123. SNDRV_PCM_RATE_192000)
  124. #define _SNDRV_PCM_FMTBIT(fmt) (1ULL << (__force int)SNDRV_PCM_FORMAT_##fmt)
  125. #define SNDRV_PCM_FMTBIT_S8 _SNDRV_PCM_FMTBIT(S8)
  126. #define SNDRV_PCM_FMTBIT_U8 _SNDRV_PCM_FMTBIT(U8)
  127. #define SNDRV_PCM_FMTBIT_S16_LE _SNDRV_PCM_FMTBIT(S16_LE)
  128. #define SNDRV_PCM_FMTBIT_S16_BE _SNDRV_PCM_FMTBIT(S16_BE)
  129. #define SNDRV_PCM_FMTBIT_U16_LE _SNDRV_PCM_FMTBIT(U16_LE)
  130. #define SNDRV_PCM_FMTBIT_U16_BE _SNDRV_PCM_FMTBIT(U16_BE)
  131. #define SNDRV_PCM_FMTBIT_S24_LE _SNDRV_PCM_FMTBIT(S24_LE)
  132. #define SNDRV_PCM_FMTBIT_S24_BE _SNDRV_PCM_FMTBIT(S24_BE)
  133. #define SNDRV_PCM_FMTBIT_U24_LE _SNDRV_PCM_FMTBIT(U24_LE)
  134. #define SNDRV_PCM_FMTBIT_U24_BE _SNDRV_PCM_FMTBIT(U24_BE)
  135. #define SNDRV_PCM_FMTBIT_S32_LE _SNDRV_PCM_FMTBIT(S32_LE)
  136. #define SNDRV_PCM_FMTBIT_S32_BE _SNDRV_PCM_FMTBIT(S32_BE)
  137. #define SNDRV_PCM_FMTBIT_U32_LE _SNDRV_PCM_FMTBIT(U32_LE)
  138. #define SNDRV_PCM_FMTBIT_U32_BE _SNDRV_PCM_FMTBIT(U32_BE)
  139. #define SNDRV_PCM_FMTBIT_FLOAT_LE _SNDRV_PCM_FMTBIT(FLOAT_LE)
  140. #define SNDRV_PCM_FMTBIT_FLOAT_BE _SNDRV_PCM_FMTBIT(FLOAT_BE)
  141. #define SNDRV_PCM_FMTBIT_FLOAT64_LE _SNDRV_PCM_FMTBIT(FLOAT64_LE)
  142. #define SNDRV_PCM_FMTBIT_FLOAT64_BE _SNDRV_PCM_FMTBIT(FLOAT64_BE)
  143. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_LE)
  144. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_BE)
  145. #define SNDRV_PCM_FMTBIT_MU_LAW _SNDRV_PCM_FMTBIT(MU_LAW)
  146. #define SNDRV_PCM_FMTBIT_A_LAW _SNDRV_PCM_FMTBIT(A_LAW)
  147. #define SNDRV_PCM_FMTBIT_IMA_ADPCM _SNDRV_PCM_FMTBIT(IMA_ADPCM)
  148. #define SNDRV_PCM_FMTBIT_MPEG _SNDRV_PCM_FMTBIT(MPEG)
  149. #define SNDRV_PCM_FMTBIT_GSM _SNDRV_PCM_FMTBIT(GSM)
  150. #define SNDRV_PCM_FMTBIT_SPECIAL _SNDRV_PCM_FMTBIT(SPECIAL)
  151. #define SNDRV_PCM_FMTBIT_S24_3LE _SNDRV_PCM_FMTBIT(S24_3LE)
  152. #define SNDRV_PCM_FMTBIT_U24_3LE _SNDRV_PCM_FMTBIT(U24_3LE)
  153. #define SNDRV_PCM_FMTBIT_S24_3BE _SNDRV_PCM_FMTBIT(S24_3BE)
  154. #define SNDRV_PCM_FMTBIT_U24_3BE _SNDRV_PCM_FMTBIT(U24_3BE)
  155. #define SNDRV_PCM_FMTBIT_S20_3LE _SNDRV_PCM_FMTBIT(S20_3LE)
  156. #define SNDRV_PCM_FMTBIT_U20_3LE _SNDRV_PCM_FMTBIT(U20_3LE)
  157. #define SNDRV_PCM_FMTBIT_S20_3BE _SNDRV_PCM_FMTBIT(S20_3BE)
  158. #define SNDRV_PCM_FMTBIT_U20_3BE _SNDRV_PCM_FMTBIT(U20_3BE)
  159. #define SNDRV_PCM_FMTBIT_S18_3LE _SNDRV_PCM_FMTBIT(S18_3LE)
  160. #define SNDRV_PCM_FMTBIT_U18_3LE _SNDRV_PCM_FMTBIT(U18_3LE)
  161. #define SNDRV_PCM_FMTBIT_S18_3BE _SNDRV_PCM_FMTBIT(S18_3BE)
  162. #define SNDRV_PCM_FMTBIT_U18_3BE _SNDRV_PCM_FMTBIT(U18_3BE)
  163. #define SNDRV_PCM_FMTBIT_G723_24 _SNDRV_PCM_FMTBIT(G723_24)
  164. #define SNDRV_PCM_FMTBIT_G723_24_1B _SNDRV_PCM_FMTBIT(G723_24_1B)
  165. #define SNDRV_PCM_FMTBIT_G723_40 _SNDRV_PCM_FMTBIT(G723_40)
  166. #define SNDRV_PCM_FMTBIT_G723_40_1B _SNDRV_PCM_FMTBIT(G723_40_1B)
  167. #define SNDRV_PCM_FMTBIT_DSD_U8 _SNDRV_PCM_FMTBIT(DSD_U8)
  168. #define SNDRV_PCM_FMTBIT_DSD_U16_LE _SNDRV_PCM_FMTBIT(DSD_U16_LE)
  169. #define SNDRV_PCM_FMTBIT_DSD_U32_LE _SNDRV_PCM_FMTBIT(DSD_U32_LE)
  170. #define SNDRV_PCM_FMTBIT_DSD_U16_BE _SNDRV_PCM_FMTBIT(DSD_U16_BE)
  171. #define SNDRV_PCM_FMTBIT_DSD_U32_BE _SNDRV_PCM_FMTBIT(DSD_U32_BE)
  172. #ifdef SNDRV_LITTLE_ENDIAN
  173. #define SNDRV_PCM_FMTBIT_S16 SNDRV_PCM_FMTBIT_S16_LE
  174. #define SNDRV_PCM_FMTBIT_U16 SNDRV_PCM_FMTBIT_U16_LE
  175. #define SNDRV_PCM_FMTBIT_S24 SNDRV_PCM_FMTBIT_S24_LE
  176. #define SNDRV_PCM_FMTBIT_U24 SNDRV_PCM_FMTBIT_U24_LE
  177. #define SNDRV_PCM_FMTBIT_S32 SNDRV_PCM_FMTBIT_S32_LE
  178. #define SNDRV_PCM_FMTBIT_U32 SNDRV_PCM_FMTBIT_U32_LE
  179. #define SNDRV_PCM_FMTBIT_FLOAT SNDRV_PCM_FMTBIT_FLOAT_LE
  180. #define SNDRV_PCM_FMTBIT_FLOAT64 SNDRV_PCM_FMTBIT_FLOAT64_LE
  181. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
  182. #endif
  183. #ifdef SNDRV_BIG_ENDIAN
  184. #define SNDRV_PCM_FMTBIT_S16 SNDRV_PCM_FMTBIT_S16_BE
  185. #define SNDRV_PCM_FMTBIT_U16 SNDRV_PCM_FMTBIT_U16_BE
  186. #define SNDRV_PCM_FMTBIT_S24 SNDRV_PCM_FMTBIT_S24_BE
  187. #define SNDRV_PCM_FMTBIT_U24 SNDRV_PCM_FMTBIT_U24_BE
  188. #define SNDRV_PCM_FMTBIT_S32 SNDRV_PCM_FMTBIT_S32_BE
  189. #define SNDRV_PCM_FMTBIT_U32 SNDRV_PCM_FMTBIT_U32_BE
  190. #define SNDRV_PCM_FMTBIT_FLOAT SNDRV_PCM_FMTBIT_FLOAT_BE
  191. #define SNDRV_PCM_FMTBIT_FLOAT64 SNDRV_PCM_FMTBIT_FLOAT64_BE
  192. #define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
  193. #endif
  194. struct snd_pcm_file {
  195. struct snd_pcm_substream *substream;
  196. int no_compat_mmap;
  197. };
  198. struct snd_pcm_hw_rule;
  199. typedef int (*snd_pcm_hw_rule_func_t)(struct snd_pcm_hw_params *params,
  200. struct snd_pcm_hw_rule *rule);
  201. struct snd_pcm_hw_rule {
  202. unsigned int cond;
  203. snd_pcm_hw_rule_func_t func;
  204. int var;
  205. int deps[4];
  206. void *private;
  207. };
  208. struct snd_pcm_hw_constraints {
  209. struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK -
  210. SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
  211. struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
  212. SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
  213. unsigned int rules_num;
  214. unsigned int rules_all;
  215. struct snd_pcm_hw_rule *rules;
  216. };
  217. static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
  218. snd_pcm_hw_param_t var)
  219. {
  220. return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  221. }
  222. static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
  223. snd_pcm_hw_param_t var)
  224. {
  225. return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  226. }
  227. struct snd_ratnum {
  228. unsigned int num;
  229. unsigned int den_min, den_max, den_step;
  230. };
  231. struct snd_ratden {
  232. unsigned int num_min, num_max, num_step;
  233. unsigned int den;
  234. };
  235. struct snd_pcm_hw_constraint_ratnums {
  236. int nrats;
  237. struct snd_ratnum *rats;
  238. };
  239. struct snd_pcm_hw_constraint_ratdens {
  240. int nrats;
  241. struct snd_ratden *rats;
  242. };
  243. struct snd_pcm_hw_constraint_list {
  244. unsigned int count;
  245. const unsigned int *list;
  246. unsigned int mask;
  247. };
  248. struct snd_pcm_hwptr_log;
  249. struct snd_pcm_runtime {
  250. /* -- Status -- */
  251. struct snd_pcm_substream *trigger_master;
  252. struct timespec trigger_tstamp; /* trigger timestamp */
  253. int overrange;
  254. snd_pcm_uframes_t avail_max;
  255. snd_pcm_uframes_t hw_ptr_base; /* Position at buffer restart */
  256. snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
  257. unsigned long hw_ptr_jiffies; /* Time when hw_ptr is updated */
  258. unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
  259. snd_pcm_sframes_t delay; /* extra delay; typically FIFO size */
  260. u64 hw_ptr_wrap; /* offset for hw_ptr due to boundary wrap-around */
  261. /* -- HW params -- */
  262. snd_pcm_access_t access; /* access mode */
  263. snd_pcm_format_t format; /* SNDRV_PCM_FORMAT_* */
  264. snd_pcm_subformat_t subformat; /* subformat */
  265. unsigned int rate; /* rate in Hz */
  266. unsigned int channels; /* channels */
  267. snd_pcm_uframes_t period_size; /* period size */
  268. unsigned int periods; /* periods */
  269. snd_pcm_uframes_t buffer_size; /* buffer size */
  270. snd_pcm_uframes_t min_align; /* Min alignment for the format */
  271. size_t byte_align;
  272. unsigned int frame_bits;
  273. unsigned int sample_bits;
  274. unsigned int info;
  275. unsigned int rate_num;
  276. unsigned int rate_den;
  277. unsigned int no_period_wakeup: 1;
  278. /* -- SW params -- */
  279. int tstamp_mode; /* mmap timestamp is updated */
  280. unsigned int period_step;
  281. snd_pcm_uframes_t start_threshold;
  282. snd_pcm_uframes_t stop_threshold;
  283. snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
  284. noise is nearest than this */
  285. snd_pcm_uframes_t silence_size; /* Silence filling size */
  286. snd_pcm_uframes_t boundary; /* pointers wrap point */
  287. snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
  288. snd_pcm_uframes_t silence_filled; /* size filled with silence */
  289. union snd_pcm_sync_id sync; /* hardware synchronization ID */
  290. /* -- mmap -- */
  291. struct snd_pcm_mmap_status *status;
  292. struct snd_pcm_mmap_control *control;
  293. /* -- locking / scheduling -- */
  294. snd_pcm_uframes_t twake; /* do transfer (!poll) wakeup if non-zero */
  295. wait_queue_head_t sleep; /* poll sleep */
  296. wait_queue_head_t tsleep; /* transfer sleep */
  297. struct fasync_struct *fasync;
  298. /* -- private section -- */
  299. void *private_data;
  300. void (*private_free)(struct snd_pcm_runtime *runtime);
  301. /* -- hardware description -- */
  302. struct snd_pcm_hardware hw;
  303. struct snd_pcm_hw_constraints hw_constraints;
  304. /* -- interrupt callbacks -- */
  305. void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
  306. void (*transfer_ack_end)(struct snd_pcm_substream *substream);
  307. /* -- timer -- */
  308. unsigned int timer_resolution; /* timer resolution */
  309. int tstamp_type; /* timestamp type */
  310. /* -- DMA -- */
  311. unsigned char *dma_area; /* DMA area */
  312. dma_addr_t dma_addr; /* physical bus address (not accessible from main CPU) */
  313. size_t dma_bytes; /* size of DMA area */
  314. struct snd_dma_buffer *dma_buffer_p; /* allocated buffer */
  315. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  316. /* -- OSS things -- */
  317. struct snd_pcm_oss_runtime oss;
  318. #endif
  319. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  320. struct snd_pcm_hwptr_log *hwptr_log;
  321. #endif
  322. };
  323. struct snd_pcm_group { /* keep linked substreams */
  324. spinlock_t lock;
  325. struct mutex mutex;
  326. struct list_head substreams;
  327. int count;
  328. };
  329. struct pid;
  330. struct snd_pcm_substream {
  331. struct snd_pcm *pcm;
  332. struct snd_pcm_str *pstr;
  333. void *private_data; /* copied from pcm->private_data */
  334. int number;
  335. char name[32]; /* substream name */
  336. int stream; /* stream (direction) */
  337. struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
  338. size_t buffer_bytes_max; /* limit ring buffer size */
  339. struct snd_dma_buffer dma_buffer;
  340. size_t dma_max;
  341. /* -- hardware operations -- */
  342. const struct snd_pcm_ops *ops;
  343. /* -- runtime information -- */
  344. struct snd_pcm_runtime *runtime;
  345. /* -- timer section -- */
  346. struct snd_timer *timer; /* timer */
  347. unsigned timer_running: 1; /* time is running */
  348. /* -- next substream -- */
  349. struct snd_pcm_substream *next;
  350. /* -- linked substreams -- */
  351. struct list_head link_list; /* linked list member */
  352. struct snd_pcm_group self_group; /* fake group for non linked substream (with substream lock inside) */
  353. struct snd_pcm_group *group; /* pointer to current group */
  354. /* -- assigned files -- */
  355. void *file;
  356. int ref_count;
  357. atomic_t mmap_count;
  358. unsigned int f_flags;
  359. void (*pcm_release)(struct snd_pcm_substream *);
  360. struct pid *pid;
  361. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  362. /* -- OSS things -- */
  363. struct snd_pcm_oss_substream oss;
  364. #endif
  365. #ifdef CONFIG_SND_VERBOSE_PROCFS
  366. struct snd_info_entry *proc_root;
  367. struct snd_info_entry *proc_info_entry;
  368. struct snd_info_entry *proc_hw_params_entry;
  369. struct snd_info_entry *proc_sw_params_entry;
  370. struct snd_info_entry *proc_status_entry;
  371. struct snd_info_entry *proc_prealloc_entry;
  372. struct snd_info_entry *proc_prealloc_max_entry;
  373. #endif
  374. /* misc flags */
  375. unsigned int hw_opened: 1;
  376. };
  377. #define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
  378. struct snd_pcm_str {
  379. int stream; /* stream (direction) */
  380. struct snd_pcm *pcm;
  381. /* -- substreams -- */
  382. unsigned int substream_count;
  383. unsigned int substream_opened;
  384. struct snd_pcm_substream *substream;
  385. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  386. /* -- OSS things -- */
  387. struct snd_pcm_oss_stream oss;
  388. #endif
  389. #ifdef CONFIG_SND_VERBOSE_PROCFS
  390. struct snd_info_entry *proc_root;
  391. struct snd_info_entry *proc_info_entry;
  392. #ifdef CONFIG_SND_PCM_XRUN_DEBUG
  393. unsigned int xrun_debug; /* 0 = disabled, 1 = verbose, 2 = stacktrace */
  394. struct snd_info_entry *proc_xrun_debug_entry;
  395. #endif
  396. #endif
  397. struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
  398. };
  399. struct snd_pcm {
  400. struct snd_card *card;
  401. struct list_head list;
  402. int device; /* device number */
  403. unsigned int info_flags;
  404. unsigned short dev_class;
  405. unsigned short dev_subclass;
  406. char id[64];
  407. char name[80];
  408. struct snd_pcm_str streams[2];
  409. struct mutex open_mutex;
  410. wait_queue_head_t open_wait;
  411. void *private_data;
  412. void (*private_free) (struct snd_pcm *pcm);
  413. struct device *dev; /* actual hw device this belongs to */
  414. bool internal; /* pcm is for internal use only */
  415. bool nonatomic; /* whole PCM operations are in non-atomic context */
  416. #if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
  417. struct snd_pcm_oss oss;
  418. #endif
  419. };
  420. struct snd_pcm_notify {
  421. int (*n_register) (struct snd_pcm * pcm);
  422. int (*n_disconnect) (struct snd_pcm * pcm);
  423. int (*n_unregister) (struct snd_pcm * pcm);
  424. struct list_head list;
  425. };
  426. /*
  427. * Registering
  428. */
  429. extern const struct file_operations snd_pcm_f_ops[2];
  430. int snd_pcm_new(struct snd_card *card, const char *id, int device,
  431. int playback_count, int capture_count,
  432. struct snd_pcm **rpcm);
  433. int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
  434. int playback_count, int capture_count,
  435. struct snd_pcm **rpcm);
  436. int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
  437. int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
  438. /*
  439. * Native I/O
  440. */
  441. int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
  442. int snd_pcm_info_user(struct snd_pcm_substream *substream,
  443. struct snd_pcm_info __user *info);
  444. int snd_pcm_status(struct snd_pcm_substream *substream,
  445. struct snd_pcm_status *status);
  446. int snd_pcm_start(struct snd_pcm_substream *substream);
  447. int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
  448. int snd_pcm_drain_done(struct snd_pcm_substream *substream);
  449. #ifdef CONFIG_PM
  450. int snd_pcm_suspend(struct snd_pcm_substream *substream);
  451. int snd_pcm_suspend_all(struct snd_pcm *pcm);
  452. #endif
  453. int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
  454. int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
  455. struct snd_pcm_substream **rsubstream);
  456. void snd_pcm_release_substream(struct snd_pcm_substream *substream);
  457. int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
  458. struct snd_pcm_substream **rsubstream);
  459. void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
  460. void snd_pcm_vma_notify_data(void *client, void *data);
  461. int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
  462. #ifdef CONFIG_SND_DEBUG
  463. void snd_pcm_debug_name(struct snd_pcm_substream *substream,
  464. char *name, size_t len);
  465. #else
  466. static inline void
  467. snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
  468. {
  469. *buf = 0;
  470. }
  471. #endif
  472. /*
  473. * PCM library
  474. */
  475. static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
  476. {
  477. return substream->group != &substream->self_group;
  478. }
  479. void snd_pcm_stream_lock(struct snd_pcm_substream *substream);
  480. void snd_pcm_stream_unlock(struct snd_pcm_substream *substream);
  481. void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream);
  482. void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream);
  483. unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream);
  484. #define snd_pcm_stream_lock_irqsave(substream, flags) \
  485. do { \
  486. typecheck(unsigned long, flags); \
  487. flags = _snd_pcm_stream_lock_irqsave(substream); \
  488. } while (0)
  489. void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
  490. unsigned long flags);
  491. #define snd_pcm_group_for_each_entry(s, substream) \
  492. list_for_each_entry(s, &substream->group->substreams, link_list)
  493. static inline int snd_pcm_running(struct snd_pcm_substream *substream)
  494. {
  495. return (substream->runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
  496. (substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
  497. substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
  498. }
  499. static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
  500. {
  501. return size * 8 / runtime->sample_bits;
  502. }
  503. static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
  504. {
  505. return size * 8 / runtime->frame_bits;
  506. }
  507. static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
  508. {
  509. return size * runtime->sample_bits / 8;
  510. }
  511. static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
  512. {
  513. return size * runtime->frame_bits / 8;
  514. }
  515. static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
  516. {
  517. return bytes % runtime->byte_align == 0;
  518. }
  519. static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
  520. {
  521. struct snd_pcm_runtime *runtime = substream->runtime;
  522. return frames_to_bytes(runtime, runtime->buffer_size);
  523. }
  524. static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
  525. {
  526. struct snd_pcm_runtime *runtime = substream->runtime;
  527. return frames_to_bytes(runtime, runtime->period_size);
  528. }
  529. /*
  530. * result is: 0 ... (boundary - 1)
  531. */
  532. static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
  533. {
  534. snd_pcm_sframes_t avail = runtime->status->hw_ptr + runtime->buffer_size - runtime->control->appl_ptr;
  535. if (avail < 0)
  536. avail += runtime->boundary;
  537. else if ((snd_pcm_uframes_t) avail >= runtime->boundary)
  538. avail -= runtime->boundary;
  539. return avail;
  540. }
  541. /*
  542. * result is: 0 ... (boundary - 1)
  543. */
  544. static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
  545. {
  546. snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
  547. if (avail < 0)
  548. avail += runtime->boundary;
  549. return avail;
  550. }
  551. static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
  552. {
  553. return runtime->buffer_size - snd_pcm_playback_avail(runtime);
  554. }
  555. static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
  556. {
  557. return runtime->buffer_size - snd_pcm_capture_avail(runtime);
  558. }
  559. /**
  560. * snd_pcm_playback_ready - check whether the playback buffer is available
  561. * @substream: the pcm substream instance
  562. *
  563. * Checks whether enough free space is available on the playback buffer.
  564. *
  565. * Return: Non-zero if available, or zero if not.
  566. */
  567. static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
  568. {
  569. struct snd_pcm_runtime *runtime = substream->runtime;
  570. return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
  571. }
  572. /**
  573. * snd_pcm_capture_ready - check whether the capture buffer is available
  574. * @substream: the pcm substream instance
  575. *
  576. * Checks whether enough capture data is available on the capture buffer.
  577. *
  578. * Return: Non-zero if available, or zero if not.
  579. */
  580. static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
  581. {
  582. struct snd_pcm_runtime *runtime = substream->runtime;
  583. return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
  584. }
  585. /**
  586. * snd_pcm_playback_data - check whether any data exists on the playback buffer
  587. * @substream: the pcm substream instance
  588. *
  589. * Checks whether any data exists on the playback buffer.
  590. *
  591. * Return: Non-zero if any data exists, or zero if not. If stop_threshold
  592. * is bigger or equal to boundary, then this function returns always non-zero.
  593. */
  594. static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
  595. {
  596. struct snd_pcm_runtime *runtime = substream->runtime;
  597. if (runtime->stop_threshold >= runtime->boundary)
  598. return 1;
  599. return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
  600. }
  601. /**
  602. * snd_pcm_playback_empty - check whether the playback buffer is empty
  603. * @substream: the pcm substream instance
  604. *
  605. * Checks whether the playback buffer is empty.
  606. *
  607. * Return: Non-zero if empty, or zero if not.
  608. */
  609. static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
  610. {
  611. struct snd_pcm_runtime *runtime = substream->runtime;
  612. return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
  613. }
  614. /**
  615. * snd_pcm_capture_empty - check whether the capture buffer is empty
  616. * @substream: the pcm substream instance
  617. *
  618. * Checks whether the capture buffer is empty.
  619. *
  620. * Return: Non-zero if empty, or zero if not.
  621. */
  622. static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
  623. {
  624. struct snd_pcm_runtime *runtime = substream->runtime;
  625. return snd_pcm_capture_avail(runtime) == 0;
  626. }
  627. static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream,
  628. struct snd_pcm_substream *master)
  629. {
  630. substream->runtime->trigger_master = master;
  631. }
  632. static inline int hw_is_mask(int var)
  633. {
  634. return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
  635. var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
  636. }
  637. static inline int hw_is_interval(int var)
  638. {
  639. return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
  640. var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
  641. }
  642. static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
  643. snd_pcm_hw_param_t var)
  644. {
  645. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  646. }
  647. static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
  648. snd_pcm_hw_param_t var)
  649. {
  650. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  651. }
  652. static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
  653. snd_pcm_hw_param_t var)
  654. {
  655. return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
  656. }
  657. static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
  658. snd_pcm_hw_param_t var)
  659. {
  660. return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
  661. }
  662. #define params_channels(p) \
  663. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_CHANNELS)->min)
  664. #define params_rate(p) \
  665. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_RATE)->min)
  666. #define params_period_size(p) \
  667. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min)
  668. #define params_periods(p) \
  669. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_PERIODS)->min)
  670. #define params_buffer_size(p) \
  671. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min)
  672. #define params_buffer_bytes(p) \
  673. (hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min)
  674. int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v);
  675. void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
  676. void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
  677. void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b,
  678. unsigned int k, struct snd_interval *c);
  679. void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
  680. const struct snd_interval *b, struct snd_interval *c);
  681. int snd_interval_list(struct snd_interval *i, unsigned int count,
  682. const unsigned int *list, unsigned int mask);
  683. int snd_interval_ratnum(struct snd_interval *i,
  684. unsigned int rats_count, struct snd_ratnum *rats,
  685. unsigned int *nump, unsigned int *denp);
  686. void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params);
  687. void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var);
  688. int snd_pcm_hw_params_choose(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
  689. int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
  690. int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream);
  691. int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream);
  692. int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  693. u_int32_t mask);
  694. int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  695. u_int64_t mask);
  696. int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
  697. unsigned int min, unsigned int max);
  698. int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var);
  699. int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
  700. unsigned int cond,
  701. snd_pcm_hw_param_t var,
  702. const struct snd_pcm_hw_constraint_list *l);
  703. int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime,
  704. unsigned int cond,
  705. snd_pcm_hw_param_t var,
  706. struct snd_pcm_hw_constraint_ratnums *r);
  707. int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime,
  708. unsigned int cond,
  709. snd_pcm_hw_param_t var,
  710. struct snd_pcm_hw_constraint_ratdens *r);
  711. int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime,
  712. unsigned int cond,
  713. unsigned int width,
  714. unsigned int msbits);
  715. int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
  716. unsigned int cond,
  717. snd_pcm_hw_param_t var,
  718. unsigned long step);
  719. int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
  720. unsigned int cond,
  721. snd_pcm_hw_param_t var);
  722. int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
  723. unsigned int base_rate);
  724. int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
  725. unsigned int cond,
  726. int var,
  727. snd_pcm_hw_rule_func_t func, void *private,
  728. int dep, ...);
  729. int snd_pcm_format_signed(snd_pcm_format_t format);
  730. int snd_pcm_format_unsigned(snd_pcm_format_t format);
  731. int snd_pcm_format_linear(snd_pcm_format_t format);
  732. int snd_pcm_format_little_endian(snd_pcm_format_t format);
  733. int snd_pcm_format_big_endian(snd_pcm_format_t format);
  734. #if 0 /* just for DocBook */
  735. /**
  736. * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
  737. * @format: the format to check
  738. *
  739. * Return: 1 if the given PCM format is CPU-endian, 0 if
  740. * opposite, or a negative error code if endian not specified.
  741. */
  742. int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
  743. #endif /* DocBook */
  744. #ifdef SNDRV_LITTLE_ENDIAN
  745. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
  746. #else
  747. #define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
  748. #endif
  749. int snd_pcm_format_width(snd_pcm_format_t format); /* in bits */
  750. int snd_pcm_format_physical_width(snd_pcm_format_t format); /* in bits */
  751. ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
  752. const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format);
  753. int snd_pcm_format_set_silence(snd_pcm_format_t format, void *buf, unsigned int frames);
  754. snd_pcm_format_t snd_pcm_build_linear_format(int width, int unsigned, int big_endian);
  755. void snd_pcm_set_ops(struct snd_pcm * pcm, int direction,
  756. const struct snd_pcm_ops *ops);
  757. void snd_pcm_set_sync(struct snd_pcm_substream *substream);
  758. int snd_pcm_lib_interleave_len(struct snd_pcm_substream *substream);
  759. int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
  760. unsigned int cmd, void *arg);
  761. int snd_pcm_update_state(struct snd_pcm_substream *substream,
  762. struct snd_pcm_runtime *runtime);
  763. int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
  764. int snd_pcm_playback_xrun_check(struct snd_pcm_substream *substream);
  765. int snd_pcm_capture_xrun_check(struct snd_pcm_substream *substream);
  766. int snd_pcm_playback_xrun_asap(struct snd_pcm_substream *substream);
  767. int snd_pcm_capture_xrun_asap(struct snd_pcm_substream *substream);
  768. void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr);
  769. void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
  770. snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream,
  771. const void __user *buf,
  772. snd_pcm_uframes_t frames);
  773. snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream,
  774. void __user *buf, snd_pcm_uframes_t frames);
  775. snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
  776. void __user **bufs, snd_pcm_uframes_t frames);
  777. snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
  778. void __user **bufs, snd_pcm_uframes_t frames);
  779. extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;
  780. int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime);
  781. unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
  782. unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit);
  783. unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
  784. unsigned int rates_b);
  785. static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
  786. struct snd_dma_buffer *bufp)
  787. {
  788. struct snd_pcm_runtime *runtime = substream->runtime;
  789. if (bufp) {
  790. runtime->dma_buffer_p = bufp;
  791. runtime->dma_area = bufp->area;
  792. runtime->dma_addr = bufp->addr;
  793. runtime->dma_bytes = bufp->bytes;
  794. } else {
  795. runtime->dma_buffer_p = NULL;
  796. runtime->dma_area = NULL;
  797. runtime->dma_addr = 0;
  798. runtime->dma_bytes = 0;
  799. }
  800. }
  801. /*
  802. * Timer interface
  803. */
  804. void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream);
  805. void snd_pcm_timer_init(struct snd_pcm_substream *substream);
  806. void snd_pcm_timer_done(struct snd_pcm_substream *substream);
  807. static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
  808. struct timespec *tv)
  809. {
  810. switch (runtime->tstamp_type) {
  811. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
  812. ktime_get_ts(tv);
  813. break;
  814. case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
  815. getrawmonotonic(tv);
  816. break;
  817. default:
  818. getnstimeofday(tv);
  819. break;
  820. }
  821. }
  822. /*
  823. * Memory
  824. */
  825. int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream);
  826. int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm);
  827. int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
  828. int type, struct device *data,
  829. size_t size, size_t max);
  830. int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
  831. int type, void *data,
  832. size_t size, size_t max);
  833. int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
  834. int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
  835. int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
  836. size_t size, gfp_t gfp_flags);
  837. int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream);
  838. struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
  839. unsigned long offset);
  840. #if 0 /* for kernel-doc */
  841. /**
  842. * snd_pcm_lib_alloc_vmalloc_buffer - allocate virtual DMA buffer
  843. * @substream: the substream to allocate the buffer to
  844. * @size: the requested buffer size, in bytes
  845. *
  846. * Allocates the PCM substream buffer using vmalloc(), i.e., the memory is
  847. * contiguous in kernel virtual space, but not in physical memory. Use this
  848. * if the buffer is accessed by kernel code but not by device DMA.
  849. *
  850. * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
  851. * code.
  852. */
  853. static int snd_pcm_lib_alloc_vmalloc_buffer
  854. (struct snd_pcm_substream *substream, size_t size);
  855. /**
  856. * snd_pcm_lib_alloc_vmalloc_32_buffer - allocate 32-bit-addressable buffer
  857. * @substream: the substream to allocate the buffer to
  858. * @size: the requested buffer size, in bytes
  859. *
  860. * This function works like snd_pcm_lib_alloc_vmalloc_buffer(), but uses
  861. * vmalloc_32(), i.e., the pages are allocated from 32-bit-addressable memory.
  862. *
  863. * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
  864. * code.
  865. */
  866. static int snd_pcm_lib_alloc_vmalloc_32_buffer
  867. (struct snd_pcm_substream *substream, size_t size);
  868. #endif
  869. #define snd_pcm_lib_alloc_vmalloc_buffer(subs, size) \
  870. _snd_pcm_lib_alloc_vmalloc_buffer \
  871. (subs, size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO)
  872. #define snd_pcm_lib_alloc_vmalloc_32_buffer(subs, size) \
  873. _snd_pcm_lib_alloc_vmalloc_buffer \
  874. (subs, size, GFP_KERNEL | GFP_DMA32 | __GFP_ZERO)
  875. #define snd_pcm_get_dma_buf(substream) ((substream)->runtime->dma_buffer_p)
  876. #ifdef CONFIG_SND_DMA_SGBUF
  877. /*
  878. * SG-buffer handling
  879. */
  880. #define snd_pcm_substream_sgbuf(substream) \
  881. snd_pcm_get_dma_buf(substream)->private_data
  882. struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
  883. unsigned long offset);
  884. #else /* !SND_DMA_SGBUF */
  885. /*
  886. * fake using a continuous buffer
  887. */
  888. #define snd_pcm_sgbuf_ops_page NULL
  889. #endif /* SND_DMA_SGBUF */
  890. static inline dma_addr_t
  891. snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
  892. {
  893. return snd_sgbuf_get_addr(snd_pcm_get_dma_buf(substream), ofs);
  894. }
  895. static inline void *
  896. snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
  897. {
  898. return snd_sgbuf_get_ptr(snd_pcm_get_dma_buf(substream), ofs);
  899. }
  900. static inline unsigned int
  901. snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
  902. unsigned int ofs, unsigned int size)
  903. {
  904. return snd_sgbuf_get_chunk_size(snd_pcm_get_dma_buf(substream), ofs, size);
  905. }
  906. /* handle mmap counter - PCM mmap callback should handle this counter properly */
  907. static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
  908. {
  909. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
  910. atomic_inc(&substream->mmap_count);
  911. }
  912. static inline void snd_pcm_mmap_data_close(struct vm_area_struct *area)
  913. {
  914. struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
  915. atomic_dec(&substream->mmap_count);
  916. }
  917. int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
  918. struct vm_area_struct *area);
  919. /* mmap for io-memory area */
  920. #if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
  921. #define SNDRV_PCM_INFO_MMAP_IOMEM SNDRV_PCM_INFO_MMAP
  922. int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
  923. #else
  924. #define SNDRV_PCM_INFO_MMAP_IOMEM 0
  925. #define snd_pcm_lib_mmap_iomem NULL
  926. #endif
  927. #define snd_pcm_lib_mmap_vmalloc NULL
  928. static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
  929. {
  930. *max = dma < 4 ? 64 * 1024 : 128 * 1024;
  931. }
  932. /*
  933. * Misc
  934. */
  935. #define SNDRV_PCM_DEFAULT_CON_SPDIF (IEC958_AES0_CON_EMPHASIS_NONE|\
  936. (IEC958_AES1_CON_ORIGINAL<<8)|\
  937. (IEC958_AES1_CON_PCM_CODER<<8)|\
  938. (IEC958_AES3_CON_FS_48000<<24))
  939. #define PCM_RUNTIME_CHECK(sub) snd_BUG_ON(!(sub) || !(sub)->runtime)
  940. const char *snd_pcm_format_name(snd_pcm_format_t format);
  941. /**
  942. * snd_pcm_stream_str - Get a string naming the direction of a stream
  943. * @substream: the pcm substream instance
  944. *
  945. * Return: A string naming the direction of the stream.
  946. */
  947. static inline const char *snd_pcm_stream_str(struct snd_pcm_substream *substream)
  948. {
  949. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  950. return "Playback";
  951. else
  952. return "Capture";
  953. }
  954. /*
  955. * PCM channel-mapping control API
  956. */
  957. /* array element of channel maps */
  958. struct snd_pcm_chmap_elem {
  959. unsigned char channels;
  960. unsigned char map[15];
  961. };
  962. /* channel map information; retrieved via snd_kcontrol_chip() */
  963. struct snd_pcm_chmap {
  964. struct snd_pcm *pcm; /* assigned PCM instance */
  965. int stream; /* PLAYBACK or CAPTURE */
  966. struct snd_kcontrol *kctl;
  967. const struct snd_pcm_chmap_elem *chmap;
  968. unsigned int max_channels;
  969. unsigned int channel_mask; /* optional: active channels bitmask */
  970. void *private_data; /* optional: private data pointer */
  971. };
  972. /* get the PCM substream assigned to the given chmap info */
  973. static inline struct snd_pcm_substream *
  974. snd_pcm_chmap_substream(struct snd_pcm_chmap *info, unsigned int idx)
  975. {
  976. struct snd_pcm_substream *s;
  977. for (s = info->pcm->streams[info->stream].substream; s; s = s->next)
  978. if (s->number == idx)
  979. return s;
  980. return NULL;
  981. }
  982. /* ALSA-standard channel maps (RL/RR prior to C/LFE) */
  983. extern const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[];
  984. /* Other world's standard channel maps (C/LFE prior to RL/RR) */
  985. extern const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[];
  986. /* bit masks to be passed to snd_pcm_chmap.channel_mask field */
  987. #define SND_PCM_CHMAP_MASK_24 ((1U << 2) | (1U << 4))
  988. #define SND_PCM_CHMAP_MASK_246 (SND_PCM_CHMAP_MASK_24 | (1U << 6))
  989. #define SND_PCM_CHMAP_MASK_2468 (SND_PCM_CHMAP_MASK_246 | (1U << 8))
  990. int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
  991. const struct snd_pcm_chmap_elem *chmap,
  992. int max_channels,
  993. unsigned long private_value,
  994. struct snd_pcm_chmap **info_ret);
  995. /* Strong-typed conversion of pcm_format to bitwise */
  996. static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
  997. {
  998. return 1ULL << (__force int) pcm_format;
  999. }
  1000. /* printk helpers */
  1001. #define pcm_err(pcm, fmt, args...) \
  1002. dev_err((pcm)->card->dev, fmt, ##args)
  1003. #define pcm_warn(pcm, fmt, args...) \
  1004. dev_warn((pcm)->card->dev, fmt, ##args)
  1005. #define pcm_dbg(pcm, fmt, args...) \
  1006. dev_dbg((pcm)->card->dev, fmt, ##args)
  1007. #endif /* __SOUND_PCM_H */