pcm.h 37 KB

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