pcm.h 35.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
#ifndef __SOUND_PCM_H
#define __SOUND_PCM_H

/*
 *  Digital Audio (PCM) abstract layer
6
 *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *                   Abramo Bagnara <abramo@alsa-project.org>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <sound/asound.h>
#include <sound/memalloc.h>
28
#include <sound/minors.h>
L
Linus Torvalds 已提交
29
#include <linux/poll.h>
A
Al Viro 已提交
30
#include <linux/mm.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43
#include <linux/bitops.h>

#define snd_pcm_substream_chip(substream) ((substream)->private_data)
#define snd_pcm_chip(pcm) ((pcm)->private_data)

#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
#include "pcm_oss.h"
#endif

/*
 *  Hardware (lowlevel) section
 */

T
Takashi Iwai 已提交
44
struct snd_pcm_hardware {
L
Linus Torvalds 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57
	unsigned int info;		/* SNDRV_PCM_INFO_* */
	u64 formats;			/* SNDRV_PCM_FMTBIT_* */
	unsigned int rates;		/* SNDRV_PCM_RATE_* */
	unsigned int rate_min;		/* min rate */
	unsigned int rate_max;		/* max rate */
	unsigned int channels_min;	/* min channels */
	unsigned int channels_max;	/* max channels */
	size_t buffer_bytes_max;	/* max buffer size */
	size_t period_bytes_min;	/* min period size */
	size_t period_bytes_max;	/* max period size */
	unsigned int periods_min;	/* min # of periods */
	unsigned int periods_max;	/* max # of periods */
	size_t fifo_size;		/* fifo size in bytes */
T
Takashi Iwai 已提交
58
};
L
Linus Torvalds 已提交
59

60 61
struct snd_pcm_substream;

T
Takashi Iwai 已提交
62 63 64 65
struct snd_pcm_ops {
	int (*open)(struct snd_pcm_substream *substream);
	int (*close)(struct snd_pcm_substream *substream);
	int (*ioctl)(struct snd_pcm_substream * substream,
L
Linus Torvalds 已提交
66
		     unsigned int cmd, void *arg);
T
Takashi Iwai 已提交
67 68 69 70 71 72 73 74
	int (*hw_params)(struct snd_pcm_substream *substream,
			 struct snd_pcm_hw_params *params);
	int (*hw_free)(struct snd_pcm_substream *substream);
	int (*prepare)(struct snd_pcm_substream *substream);
	int (*trigger)(struct snd_pcm_substream *substream, int cmd);
	snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *substream);
	int (*copy)(struct snd_pcm_substream *substream, int channel,
		    snd_pcm_uframes_t pos,
L
Linus Torvalds 已提交
75
		    void __user *buf, snd_pcm_uframes_t count);
T
Takashi Iwai 已提交
76
	int (*silence)(struct snd_pcm_substream *substream, int channel, 
L
Linus Torvalds 已提交
77
		       snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
T
Takashi Iwai 已提交
78 79 80 81 82
	struct page *(*page)(struct snd_pcm_substream *substream,
			     unsigned long offset);
	int (*mmap)(struct snd_pcm_substream *substream, struct vm_area_struct *vma);
	int (*ack)(struct snd_pcm_substream *substream);
};
L
Linus Torvalds 已提交
83 84 85 86 87

/*
 *
 */

88 89 90 91 92
#if defined(CONFIG_SND_DYNAMIC_MINORS)
#define SNDRV_PCM_DEVICES	(SNDRV_OS_MINORS-2)
#else
#define SNDRV_PCM_DEVICES	8
#endif
93

L
Linus Torvalds 已提交
94 95 96 97 98 99 100
#define SNDRV_PCM_IOCTL1_FALSE		((void *)0)
#define SNDRV_PCM_IOCTL1_TRUE		((void *)1)

#define SNDRV_PCM_IOCTL1_RESET		0
#define SNDRV_PCM_IOCTL1_INFO		1
#define SNDRV_PCM_IOCTL1_CHANNEL_INFO	2
#define SNDRV_PCM_IOCTL1_GSTATE		3
101
#define SNDRV_PCM_IOCTL1_FIFO_SIZE	4
L
Linus Torvalds 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

#define SNDRV_PCM_TRIGGER_STOP		0
#define SNDRV_PCM_TRIGGER_START		1
#define SNDRV_PCM_TRIGGER_PAUSE_PUSH	3
#define SNDRV_PCM_TRIGGER_PAUSE_RELEASE	4
#define SNDRV_PCM_TRIGGER_SUSPEND	5
#define SNDRV_PCM_TRIGGER_RESUME	6

#define SNDRV_PCM_POS_XRUN		((snd_pcm_uframes_t)-1)

/* If you change this don't forget to change rates[] table in pcm_native.c */
#define SNDRV_PCM_RATE_5512		(1<<0)		/* 5512Hz */
#define SNDRV_PCM_RATE_8000		(1<<1)		/* 8000Hz */
#define SNDRV_PCM_RATE_11025		(1<<2)		/* 11025Hz */
#define SNDRV_PCM_RATE_16000		(1<<3)		/* 16000Hz */
#define SNDRV_PCM_RATE_22050		(1<<4)		/* 22050Hz */
#define SNDRV_PCM_RATE_32000		(1<<5)		/* 32000Hz */
#define SNDRV_PCM_RATE_44100		(1<<6)		/* 44100Hz */
#define SNDRV_PCM_RATE_48000		(1<<7)		/* 48000Hz */
#define SNDRV_PCM_RATE_64000		(1<<8)		/* 64000Hz */
#define SNDRV_PCM_RATE_88200		(1<<9)		/* 88200Hz */
#define SNDRV_PCM_RATE_96000		(1<<10)		/* 96000Hz */
#define SNDRV_PCM_RATE_176400		(1<<11)		/* 176400Hz */
#define SNDRV_PCM_RATE_192000		(1<<12)		/* 192000Hz */

#define SNDRV_PCM_RATE_CONTINUOUS	(1<<30)		/* continuous range */
#define SNDRV_PCM_RATE_KNOT		(1<<31)		/* supports more non-continuos rates */

#define SNDRV_PCM_RATE_8000_44100	(SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_11025|\
					 SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_22050|\
					 SNDRV_PCM_RATE_32000|SNDRV_PCM_RATE_44100)
#define SNDRV_PCM_RATE_8000_48000	(SNDRV_PCM_RATE_8000_44100|SNDRV_PCM_RATE_48000)
#define SNDRV_PCM_RATE_8000_96000	(SNDRV_PCM_RATE_8000_48000|SNDRV_PCM_RATE_64000|\
					 SNDRV_PCM_RATE_88200|SNDRV_PCM_RATE_96000)
#define SNDRV_PCM_RATE_8000_192000	(SNDRV_PCM_RATE_8000_96000|SNDRV_PCM_RATE_176400|\
					 SNDRV_PCM_RATE_192000)
#define SNDRV_PCM_FMTBIT_S8		(1ULL << SNDRV_PCM_FORMAT_S8)
#define SNDRV_PCM_FMTBIT_U8		(1ULL << SNDRV_PCM_FORMAT_U8)
#define SNDRV_PCM_FMTBIT_S16_LE		(1ULL << SNDRV_PCM_FORMAT_S16_LE)
#define SNDRV_PCM_FMTBIT_S16_BE		(1ULL << SNDRV_PCM_FORMAT_S16_BE)
#define SNDRV_PCM_FMTBIT_U16_LE		(1ULL << SNDRV_PCM_FORMAT_U16_LE)
#define SNDRV_PCM_FMTBIT_U16_BE		(1ULL << SNDRV_PCM_FORMAT_U16_BE)
#define SNDRV_PCM_FMTBIT_S24_LE		(1ULL << SNDRV_PCM_FORMAT_S24_LE)
#define SNDRV_PCM_FMTBIT_S24_BE		(1ULL << SNDRV_PCM_FORMAT_S24_BE)
#define SNDRV_PCM_FMTBIT_U24_LE		(1ULL << SNDRV_PCM_FORMAT_U24_LE)
#define SNDRV_PCM_FMTBIT_U24_BE		(1ULL << SNDRV_PCM_FORMAT_U24_BE)
#define SNDRV_PCM_FMTBIT_S32_LE		(1ULL << SNDRV_PCM_FORMAT_S32_LE)
#define SNDRV_PCM_FMTBIT_S32_BE		(1ULL << SNDRV_PCM_FORMAT_S32_BE)
#define SNDRV_PCM_FMTBIT_U32_LE		(1ULL << SNDRV_PCM_FORMAT_U32_LE)
#define SNDRV_PCM_FMTBIT_U32_BE		(1ULL << SNDRV_PCM_FORMAT_U32_BE)
#define SNDRV_PCM_FMTBIT_FLOAT_LE	(1ULL << SNDRV_PCM_FORMAT_FLOAT_LE)
#define SNDRV_PCM_FMTBIT_FLOAT_BE	(1ULL << SNDRV_PCM_FORMAT_FLOAT_BE)
#define SNDRV_PCM_FMTBIT_FLOAT64_LE	(1ULL << SNDRV_PCM_FORMAT_FLOAT64_LE)
#define SNDRV_PCM_FMTBIT_FLOAT64_BE	(1ULL << SNDRV_PCM_FORMAT_FLOAT64_BE)
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE (1ULL << SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE)
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE (1ULL << SNDRV_PCM_FORMAT_IEC958_SUBFRAME_BE)
#define SNDRV_PCM_FMTBIT_MU_LAW		(1ULL << SNDRV_PCM_FORMAT_MU_LAW)
#define SNDRV_PCM_FMTBIT_A_LAW		(1ULL << SNDRV_PCM_FORMAT_A_LAW)
#define SNDRV_PCM_FMTBIT_IMA_ADPCM	(1ULL << SNDRV_PCM_FORMAT_IMA_ADPCM)
#define SNDRV_PCM_FMTBIT_MPEG		(1ULL << SNDRV_PCM_FORMAT_MPEG)
#define SNDRV_PCM_FMTBIT_GSM		(1ULL << SNDRV_PCM_FORMAT_GSM)
#define SNDRV_PCM_FMTBIT_SPECIAL	(1ULL << SNDRV_PCM_FORMAT_SPECIAL)
#define SNDRV_PCM_FMTBIT_S24_3LE	(1ULL << SNDRV_PCM_FORMAT_S24_3LE)
#define SNDRV_PCM_FMTBIT_U24_3LE	(1ULL << SNDRV_PCM_FORMAT_U24_3LE)
#define SNDRV_PCM_FMTBIT_S24_3BE	(1ULL << SNDRV_PCM_FORMAT_S24_3BE)
#define SNDRV_PCM_FMTBIT_U24_3BE	(1ULL << SNDRV_PCM_FORMAT_U24_3BE)
#define SNDRV_PCM_FMTBIT_S20_3LE	(1ULL << SNDRV_PCM_FORMAT_S20_3LE)
#define SNDRV_PCM_FMTBIT_U20_3LE	(1ULL << SNDRV_PCM_FORMAT_U20_3LE)
#define SNDRV_PCM_FMTBIT_S20_3BE	(1ULL << SNDRV_PCM_FORMAT_S20_3BE)
#define SNDRV_PCM_FMTBIT_U20_3BE	(1ULL << SNDRV_PCM_FORMAT_U20_3BE)
#define SNDRV_PCM_FMTBIT_S18_3LE	(1ULL << SNDRV_PCM_FORMAT_S18_3LE)
#define SNDRV_PCM_FMTBIT_U18_3LE	(1ULL << SNDRV_PCM_FORMAT_U18_3LE)
#define SNDRV_PCM_FMTBIT_S18_3BE	(1ULL << SNDRV_PCM_FORMAT_S18_3BE)
#define SNDRV_PCM_FMTBIT_U18_3BE	(1ULL << SNDRV_PCM_FORMAT_U18_3BE)

#ifdef SNDRV_LITTLE_ENDIAN
#define SNDRV_PCM_FMTBIT_S16		SNDRV_PCM_FMTBIT_S16_LE
#define SNDRV_PCM_FMTBIT_U16		SNDRV_PCM_FMTBIT_U16_LE
#define SNDRV_PCM_FMTBIT_S24		SNDRV_PCM_FMTBIT_S24_LE
#define SNDRV_PCM_FMTBIT_U24		SNDRV_PCM_FMTBIT_U24_LE
#define SNDRV_PCM_FMTBIT_S32		SNDRV_PCM_FMTBIT_S32_LE
#define SNDRV_PCM_FMTBIT_U32		SNDRV_PCM_FMTBIT_U32_LE
#define SNDRV_PCM_FMTBIT_FLOAT		SNDRV_PCM_FMTBIT_FLOAT_LE
#define SNDRV_PCM_FMTBIT_FLOAT64	SNDRV_PCM_FMTBIT_FLOAT64_LE
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
#endif
#ifdef SNDRV_BIG_ENDIAN
#define SNDRV_PCM_FMTBIT_S16		SNDRV_PCM_FMTBIT_S16_BE
#define SNDRV_PCM_FMTBIT_U16		SNDRV_PCM_FMTBIT_U16_BE
#define SNDRV_PCM_FMTBIT_S24		SNDRV_PCM_FMTBIT_S24_BE
#define SNDRV_PCM_FMTBIT_U24		SNDRV_PCM_FMTBIT_U24_BE
#define SNDRV_PCM_FMTBIT_S32		SNDRV_PCM_FMTBIT_S32_BE
#define SNDRV_PCM_FMTBIT_U32		SNDRV_PCM_FMTBIT_U32_BE
#define SNDRV_PCM_FMTBIT_FLOAT		SNDRV_PCM_FMTBIT_FLOAT_BE
#define SNDRV_PCM_FMTBIT_FLOAT64	SNDRV_PCM_FMTBIT_FLOAT64_BE
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE
#endif

T
Takashi Iwai 已提交
200 201
struct snd_pcm_file {
	struct snd_pcm_substream *substream;
202
	int no_compat_mmap;
L
Linus Torvalds 已提交
203 204
};

T
Takashi Iwai 已提交
205 206 207
struct snd_pcm_hw_rule;
typedef int (*snd_pcm_hw_rule_func_t)(struct snd_pcm_hw_params *params,
				      struct snd_pcm_hw_rule *rule);
L
Linus Torvalds 已提交
208

T
Takashi Iwai 已提交
209
struct snd_pcm_hw_rule {
L
Linus Torvalds 已提交
210 211 212 213 214 215 216
	unsigned int cond;
	snd_pcm_hw_rule_func_t func;
	int var;
	int deps[4];
	void *private;
};

T
Takashi Iwai 已提交
217 218
struct snd_pcm_hw_constraints {
	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - 
L
Linus Torvalds 已提交
219
			 SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
T
Takashi Iwai 已提交
220
	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
L
Linus Torvalds 已提交
221 222 223
			     SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
	unsigned int rules_num;
	unsigned int rules_all;
T
Takashi Iwai 已提交
224 225
	struct snd_pcm_hw_rule *rules;
};
L
Linus Torvalds 已提交
226

T
Takashi Iwai 已提交
227 228
static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
					    snd_pcm_hw_param_t var)
L
Linus Torvalds 已提交
229 230 231 232
{
	return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
}

T
Takashi Iwai 已提交
233 234
static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
						    snd_pcm_hw_param_t var)
L
Linus Torvalds 已提交
235 236 237 238
{
	return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
}

T
Takashi Iwai 已提交
239
struct snd_ratnum {
L
Linus Torvalds 已提交
240 241
	unsigned int num;
	unsigned int den_min, den_max, den_step;
T
Takashi Iwai 已提交
242
};
L
Linus Torvalds 已提交
243

T
Takashi Iwai 已提交
244
struct snd_ratden {
L
Linus Torvalds 已提交
245 246
	unsigned int num_min, num_max, num_step;
	unsigned int den;
T
Takashi Iwai 已提交
247
};
L
Linus Torvalds 已提交
248

T
Takashi Iwai 已提交
249
struct snd_pcm_hw_constraint_ratnums {
L
Linus Torvalds 已提交
250
	int nrats;
T
Takashi Iwai 已提交
251 252
	struct snd_ratnum *rats;
};
L
Linus Torvalds 已提交
253

T
Takashi Iwai 已提交
254
struct snd_pcm_hw_constraint_ratdens {
L
Linus Torvalds 已提交
255
	int nrats;
T
Takashi Iwai 已提交
256 257
	struct snd_ratden *rats;
};
L
Linus Torvalds 已提交
258

T
Takashi Iwai 已提交
259
struct snd_pcm_hw_constraint_list {
L
Linus Torvalds 已提交
260 261 262
	unsigned int count;
	unsigned int *list;
	unsigned int mask;
T
Takashi Iwai 已提交
263
};
L
Linus Torvalds 已提交
264

265 266
struct snd_pcm_hwptr_log;

T
Takashi Iwai 已提交
267
struct snd_pcm_runtime {
L
Linus Torvalds 已提交
268
	/* -- Status -- */
T
Takashi Iwai 已提交
269
	struct snd_pcm_substream *trigger_master;
T
Takashi Iwai 已提交
270
	struct timespec trigger_tstamp;	/* trigger timestamp */
L
Linus Torvalds 已提交
271 272 273
	int overrange;
	snd_pcm_uframes_t avail_max;
	snd_pcm_uframes_t hw_ptr_base;	/* Position at buffer restart */
274
	unsigned long hw_ptr_jiffies;	/* Time when hw_ptr is updated */
275
	snd_pcm_sframes_t delay;	/* extra delay; typically FIFO size */
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	/* -- HW params -- */
	snd_pcm_access_t access;	/* access mode */
	snd_pcm_format_t format;	/* SNDRV_PCM_FORMAT_* */
	snd_pcm_subformat_t subformat;	/* subformat */
	unsigned int rate;		/* rate in Hz */
	unsigned int channels;		/* channels */
	snd_pcm_uframes_t period_size;	/* period size */
	unsigned int periods;		/* periods */
	snd_pcm_uframes_t buffer_size;	/* buffer size */
	snd_pcm_uframes_t min_align;	/* Min alignment for the format */
	size_t byte_align;
	unsigned int frame_bits;
	unsigned int sample_bits;
	unsigned int info;
	unsigned int rate_num;
	unsigned int rate_den;

	/* -- SW params -- */
T
Takashi Iwai 已提交
295
	int tstamp_mode;		/* mmap timestamp is updated */
L
Linus Torvalds 已提交
296 297 298 299 300 301 302 303 304 305 306
  	unsigned int period_step;
	snd_pcm_uframes_t start_threshold;
	snd_pcm_uframes_t stop_threshold;
	snd_pcm_uframes_t silence_threshold; /* Silence filling happens when
						noise is nearest than this */
	snd_pcm_uframes_t silence_size;	/* Silence filling size */
	snd_pcm_uframes_t boundary;	/* pointers wrap point */

	snd_pcm_uframes_t silence_start; /* starting pointer to silence area */
	snd_pcm_uframes_t silence_filled; /* size filled with silence */

T
Takashi Iwai 已提交
307
	union snd_pcm_sync_id sync;	/* hardware synchronization ID */
L
Linus Torvalds 已提交
308 309

	/* -- mmap -- */
310 311
	struct snd_pcm_mmap_status *status;
	struct snd_pcm_mmap_control *control;
L
Linus Torvalds 已提交
312 313

	/* -- locking / scheduling -- */
314
	unsigned int nowake: 1;		/* no wakeup (data-copy in progress) */
L
Linus Torvalds 已提交
315 316 317 318 319
	wait_queue_head_t sleep;
	struct fasync_struct *fasync;

	/* -- private section -- */
	void *private_data;
T
Takashi Iwai 已提交
320
	void (*private_free)(struct snd_pcm_runtime *runtime);
L
Linus Torvalds 已提交
321 322

	/* -- hardware description -- */
T
Takashi Iwai 已提交
323 324
	struct snd_pcm_hardware hw;
	struct snd_pcm_hw_constraints hw_constraints;
L
Linus Torvalds 已提交
325 326

	/* -- interrupt callbacks -- */
T
Takashi Iwai 已提交
327 328
	void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
	void (*transfer_ack_end)(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
329 330 331

	/* -- timer -- */
	unsigned int timer_resolution;	/* timer resolution */
332
	int tstamp_type;		/* timestamp type */
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340 341 342

	/* -- DMA -- */           
	unsigned char *dma_area;	/* DMA area */
	dma_addr_t dma_addr;		/* physical bus address (not accessible from main CPU) */
	size_t dma_bytes;		/* size of DMA area */

	struct snd_dma_buffer *dma_buffer_p;	/* allocated buffer */

#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
	/* -- OSS things -- */
T
Takashi Iwai 已提交
343
	struct snd_pcm_oss_runtime oss;
L
Linus Torvalds 已提交
344
#endif
345 346 347 348

#ifdef CONFIG_SND_PCM_XRUN_DEBUG
	struct snd_pcm_hwptr_log *hwptr_log;
#endif
L
Linus Torvalds 已提交
349 350
};

T
Takashi Iwai 已提交
351
struct snd_pcm_group {		/* keep linked substreams */
L
Linus Torvalds 已提交
352 353 354
	spinlock_t lock;
	struct list_head substreams;
	int count;
T
Takashi Iwai 已提交
355
};
L
Linus Torvalds 已提交
356

357 358
struct pid;

T
Takashi Iwai 已提交
359 360 361
struct snd_pcm_substream {
	struct snd_pcm *pcm;
	struct snd_pcm_str *pstr;
L
Linus Torvalds 已提交
362 363 364 365
	void *private_data;		/* copied from pcm->private_data */
	int number;
	char name[32];			/* substream name */
	int stream;			/* stream (direction) */
366
	char latency_id[20];		/* latency identifier */
L
Linus Torvalds 已提交
367 368 369 370 371
	size_t buffer_bytes_max;	/* limit ring buffer size */
	struct snd_dma_buffer dma_buffer;
	unsigned int dma_buf_id;
	size_t dma_max;
	/* -- hardware operations -- */
T
Takashi Iwai 已提交
372
	struct snd_pcm_ops *ops;
L
Linus Torvalds 已提交
373
	/* -- runtime information -- */
T
Takashi Iwai 已提交
374
	struct snd_pcm_runtime *runtime;
L
Linus Torvalds 已提交
375
        /* -- timer section -- */
T
Takashi Iwai 已提交
376
	struct snd_timer *timer;		/* timer */
L
Linus Torvalds 已提交
377 378
	unsigned timer_running: 1;	/* time is running */
	/* -- next substream -- */
T
Takashi Iwai 已提交
379
	struct snd_pcm_substream *next;
L
Linus Torvalds 已提交
380 381
	/* -- linked substreams -- */
	struct list_head link_list;	/* linked list member */
T
Takashi Iwai 已提交
382 383
	struct snd_pcm_group self_group;	/* fake group for non linked substream (with substream lock inside) */
	struct snd_pcm_group *group;		/* pointer to current group */
L
Linus Torvalds 已提交
384
	/* -- assigned files -- */
385
	void *file;
386
	int ref_count;
387
	atomic_t mmap_count;
388
	unsigned int f_flags;
389
	void (*pcm_release)(struct snd_pcm_substream *);
390
	struct pid *pid;
L
Linus Torvalds 已提交
391 392
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
	/* -- OSS things -- */
T
Takashi Iwai 已提交
393
	struct snd_pcm_oss_substream oss;
L
Linus Torvalds 已提交
394
#endif
395
#ifdef CONFIG_SND_VERBOSE_PROCFS
T
Takashi Iwai 已提交
396 397 398 399 400 401
	struct snd_info_entry *proc_root;
	struct snd_info_entry *proc_info_entry;
	struct snd_info_entry *proc_hw_params_entry;
	struct snd_info_entry *proc_sw_params_entry;
	struct snd_info_entry *proc_status_entry;
	struct snd_info_entry *proc_prealloc_entry;
402
	struct snd_info_entry *proc_prealloc_max_entry;
403
#endif
L
Linus Torvalds 已提交
404
	/* misc flags */
405
	unsigned int hw_opened: 1;
L
Linus Torvalds 已提交
406 407
};

408
#define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
L
Linus Torvalds 已提交
409 410


T
Takashi Iwai 已提交
411
struct snd_pcm_str {
L
Linus Torvalds 已提交
412
	int stream;				/* stream (direction) */
T
Takashi Iwai 已提交
413
	struct snd_pcm *pcm;
L
Linus Torvalds 已提交
414 415 416
	/* -- substreams -- */
	unsigned int substream_count;
	unsigned int substream_opened;
T
Takashi Iwai 已提交
417
	struct snd_pcm_substream *substream;
L
Linus Torvalds 已提交
418 419
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
	/* -- OSS things -- */
T
Takashi Iwai 已提交
420
	struct snd_pcm_oss_stream oss;
L
Linus Torvalds 已提交
421
#endif
422
#ifdef CONFIG_SND_VERBOSE_PROCFS
T
Takashi Iwai 已提交
423 424
	struct snd_info_entry *proc_root;
	struct snd_info_entry *proc_info_entry;
425
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
L
Linus Torvalds 已提交
426
	unsigned int xrun_debug;	/* 0 = disabled, 1 = verbose, 2 = stacktrace */
T
Takashi Iwai 已提交
427
	struct snd_info_entry *proc_xrun_debug_entry;
L
Linus Torvalds 已提交
428
#endif
429
#endif
L
Linus Torvalds 已提交
430 431
};

T
Takashi Iwai 已提交
432 433
struct snd_pcm {
	struct snd_card *card;
434
	struct list_head list;
435
	int device; /* device number */
L
Linus Torvalds 已提交
436 437 438 439 440
	unsigned int info_flags;
	unsigned short dev_class;
	unsigned short dev_subclass;
	char id[64];
	char name[80];
T
Takashi Iwai 已提交
441
	struct snd_pcm_str streams[2];
442
	struct mutex open_mutex;
L
Linus Torvalds 已提交
443 444
	wait_queue_head_t open_wait;
	void *private_data;
T
Takashi Iwai 已提交
445
	void (*private_free) (struct snd_pcm *pcm);
446
	struct device *dev; /* actual hw device this belongs to */
L
Linus Torvalds 已提交
447
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
T
Takashi Iwai 已提交
448
	struct snd_pcm_oss oss;
L
Linus Torvalds 已提交
449 450 451
#endif
};

T
Takashi Iwai 已提交
452 453 454 455
struct snd_pcm_notify {
	int (*n_register) (struct snd_pcm * pcm);
	int (*n_disconnect) (struct snd_pcm * pcm);
	int (*n_unregister) (struct snd_pcm * pcm);
L
Linus Torvalds 已提交
456
	struct list_head list;
T
Takashi Iwai 已提交
457
};
L
Linus Torvalds 已提交
458 459 460 461 462

/*
 *  Registering
 */

463
extern const struct file_operations snd_pcm_f_ops[2];
L
Linus Torvalds 已提交
464

T
Tim Blechmann 已提交
465
int snd_pcm_new(struct snd_card *card, const char *id, int device,
L
Linus Torvalds 已提交
466
		int playback_count, int capture_count,
T
Takashi Iwai 已提交
467 468
		struct snd_pcm **rpcm);
int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
L
Linus Torvalds 已提交
469

T
Takashi Iwai 已提交
470
int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
L
Linus Torvalds 已提交
471 472 473 474 475 476 477

/*
 *  Native I/O
 */

extern rwlock_t snd_pcm_link_rwlock;

T
Takashi Iwai 已提交
478 479 480 481 482 483 484 485
int snd_pcm_info(struct snd_pcm_substream *substream, struct snd_pcm_info *info);
int snd_pcm_info_user(struct snd_pcm_substream *substream,
		      struct snd_pcm_info __user *info);
int snd_pcm_status(struct snd_pcm_substream *substream,
		   struct snd_pcm_status *status);
int snd_pcm_start(struct snd_pcm_substream *substream);
int snd_pcm_stop(struct snd_pcm_substream *substream, int status);
int snd_pcm_drain_done(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
486
#ifdef CONFIG_PM
T
Takashi Iwai 已提交
487 488
int snd_pcm_suspend(struct snd_pcm_substream *substream);
int snd_pcm_suspend_all(struct snd_pcm *pcm);
L
Linus Torvalds 已提交
489
#endif
T
Takashi Iwai 已提交
490
int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
491 492
int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
			   struct snd_pcm_substream **rsubstream);
T
Takashi Iwai 已提交
493
void snd_pcm_release_substream(struct snd_pcm_substream *substream);
494 495 496
int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, struct file *file,
			     struct snd_pcm_substream **rsubstream);
void snd_pcm_detach_substream(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
497
void snd_pcm_vma_notify_data(void *client, void *data);
T
Takashi Iwai 已提交
498
int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
L
Linus Torvalds 已提交
499 500 501 502 503

/*
 *  PCM library
 */

T
Takashi Iwai 已提交
504
static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
505 506 507 508
{
	return substream->group != &substream->self_group;
}

T
Takashi Iwai 已提交
509
static inline void snd_pcm_stream_lock(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
510 511 512 513 514
{
	read_lock(&snd_pcm_link_rwlock);
	spin_lock(&substream->self_group.lock);
}

T
Takashi Iwai 已提交
515
static inline void snd_pcm_stream_unlock(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
516 517 518 519 520
{
	spin_unlock(&substream->self_group.lock);
	read_unlock(&snd_pcm_link_rwlock);
}

T
Takashi Iwai 已提交
521
static inline void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
522 523 524 525 526
{
	read_lock_irq(&snd_pcm_link_rwlock);
	spin_lock(&substream->self_group.lock);
}

T
Takashi Iwai 已提交
527
static inline void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
{
	spin_unlock(&substream->self_group.lock);
	read_unlock_irq(&snd_pcm_link_rwlock);
}

#define snd_pcm_stream_lock_irqsave(substream, flags) \
do { \
	read_lock_irqsave(&snd_pcm_link_rwlock, (flags)); \
	spin_lock(&substream->self_group.lock); \
} while (0)

#define snd_pcm_stream_unlock_irqrestore(substream, flags) \
do { \
	spin_unlock(&substream->self_group.lock); \
	read_unlock_irqrestore(&snd_pcm_link_rwlock, (flags)); \
} while (0)

545 546
#define snd_pcm_group_for_each_entry(s, substream) \
	list_for_each_entry(s, &substream->group->substreams, link_list)
L
Linus Torvalds 已提交
547

T
Takashi Iwai 已提交
548
static inline int snd_pcm_running(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
549 550 551 552 553 554
{
	return (substream->runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
		(substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
		 substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
}

T
Takashi Iwai 已提交
555
static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
L
Linus Torvalds 已提交
556 557 558 559
{
	return size * 8 / runtime->sample_bits;
}

T
Takashi Iwai 已提交
560
static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
L
Linus Torvalds 已提交
561 562 563 564
{
	return size * 8 / runtime->frame_bits;
}

T
Takashi Iwai 已提交
565
static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
L
Linus Torvalds 已提交
566 567 568 569
{
	return size * runtime->sample_bits / 8;
}

T
Takashi Iwai 已提交
570
static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
L
Linus Torvalds 已提交
571 572 573 574
{
	return size * runtime->frame_bits / 8;
}

T
Takashi Iwai 已提交
575
static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
L
Linus Torvalds 已提交
576 577 578 579
{
	return bytes % runtime->byte_align == 0;
}

T
Takashi Iwai 已提交
580
static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
581
{
T
Takashi Iwai 已提交
582
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
583 584 585
	return frames_to_bytes(runtime, runtime->buffer_size);
}

T
Takashi Iwai 已提交
586
static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
587
{
T
Takashi Iwai 已提交
588
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
589 590 591 592 593 594
	return frames_to_bytes(runtime, runtime->period_size);
}

/*
 *  result is: 0 ... (boundary - 1)
 */
T
Takashi Iwai 已提交
595
static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
596 597 598 599 600 601 602 603 604 605 606 607
{
	snd_pcm_sframes_t avail = runtime->status->hw_ptr + runtime->buffer_size - runtime->control->appl_ptr;
	if (avail < 0)
		avail += runtime->boundary;
	else if ((snd_pcm_uframes_t) avail >= runtime->boundary)
		avail -= runtime->boundary;
	return avail;
}

/*
 *  result is: 0 ... (boundary - 1)
 */
T
Takashi Iwai 已提交
608
static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
609 610 611 612 613 614 615
{
	snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
	if (avail < 0)
		avail += runtime->boundary;
	return avail;
}

T
Takashi Iwai 已提交
616
static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
617 618 619 620
{
	return runtime->buffer_size - snd_pcm_playback_avail(runtime);
}

T
Takashi Iwai 已提交
621
static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
L
Linus Torvalds 已提交
622 623 624 625 626 627 628 629 630 631 632 633
{
	return runtime->buffer_size - snd_pcm_capture_avail(runtime);
}

/**
 * snd_pcm_playback_ready - check whether the playback buffer is available
 * @substream: the pcm substream instance
 *
 * Checks whether enough free space is available on the playback buffer.
 *
 * Returns non-zero if available, or zero if not.
 */
T
Takashi Iwai 已提交
634
static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
635
{
T
Takashi Iwai 已提交
636
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
637 638 639 640 641 642 643 644 645 646 647
	return snd_pcm_playback_avail(runtime) >= runtime->control->avail_min;
}

/**
 * snd_pcm_capture_ready - check whether the capture buffer is available
 * @substream: the pcm substream instance
 *
 * Checks whether enough capture data is available on the capture buffer.
 *
 * Returns non-zero if available, or zero if not.
 */
T
Takashi Iwai 已提交
648
static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
649
{
T
Takashi Iwai 已提交
650
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662
	return snd_pcm_capture_avail(runtime) >= runtime->control->avail_min;
}

/**
 * snd_pcm_playback_data - check whether any data exists on the playback buffer
 * @substream: the pcm substream instance
 *
 * Checks whether any data exists on the playback buffer. If stop_threshold
 * is bigger or equal to boundary, then this function returns always non-zero.
 *
 * Returns non-zero if exists, or zero if not.
 */
T
Takashi Iwai 已提交
663
static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
664
{
T
Takashi Iwai 已提交
665
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679
	
	if (runtime->stop_threshold >= runtime->boundary)
		return 1;
	return snd_pcm_playback_avail(runtime) < runtime->buffer_size;
}

/**
 * snd_pcm_playback_empty - check whether the playback buffer is empty
 * @substream: the pcm substream instance
 *
 * Checks whether the playback buffer is empty.
 *
 * Returns non-zero if empty, or zero if not.
 */
T
Takashi Iwai 已提交
680
static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
681
{
T
Takashi Iwai 已提交
682
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
683 684 685 686 687 688 689 690 691 692 693
	return snd_pcm_playback_avail(runtime) >= runtime->buffer_size;
}

/**
 * snd_pcm_capture_empty - check whether the capture buffer is empty
 * @substream: the pcm substream instance
 *
 * Checks whether the capture buffer is empty.
 *
 * Returns non-zero if empty, or zero if not.
 */
T
Takashi Iwai 已提交
694
static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
L
Linus Torvalds 已提交
695
{
T
Takashi Iwai 已提交
696
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
697 698 699
	return snd_pcm_capture_avail(runtime) == 0;
}

T
Takashi Iwai 已提交
700 701
static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream, 
					struct snd_pcm_substream *master)
L
Linus Torvalds 已提交
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
{
	substream->runtime->trigger_master = master;
}

static inline int hw_is_mask(int var)
{
	return var >= SNDRV_PCM_HW_PARAM_FIRST_MASK &&
		var <= SNDRV_PCM_HW_PARAM_LAST_MASK;
}

static inline int hw_is_interval(int var)
{
	return var >= SNDRV_PCM_HW_PARAM_FIRST_INTERVAL &&
		var <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL;
}

T
Takashi Iwai 已提交
718
static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
L
Linus Torvalds 已提交
719 720 721 722 723
				     snd_pcm_hw_param_t var)
{
	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
}

T
Takashi Iwai 已提交
724
static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
L
Linus Torvalds 已提交
725 726 727 728 729
					     snd_pcm_hw_param_t var)
{
	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
}

T
Takashi Iwai 已提交
730
static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
L
Linus Torvalds 已提交
731 732
					     snd_pcm_hw_param_t var)
{
733
	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
L
Linus Torvalds 已提交
734 735
}

T
Takashi Iwai 已提交
736
static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
L
Linus Torvalds 已提交
737 738
						     snd_pcm_hw_param_t var)
{
739
	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
L
Linus Torvalds 已提交
740 741 742 743 744 745 746 747 748 749 750 751 752 753
}

#define params_access(p) snd_mask_min(hw_param_mask((p), SNDRV_PCM_HW_PARAM_ACCESS))
#define params_format(p) snd_mask_min(hw_param_mask((p), SNDRV_PCM_HW_PARAM_FORMAT))
#define params_subformat(p) snd_mask_min(hw_param_mask((p), SNDRV_PCM_HW_PARAM_SUBFORMAT))
#define params_channels(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_CHANNELS)->min
#define params_rate(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_RATE)->min
#define params_period_size(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min
#define params_period_bytes(p) ((params_period_size(p)*snd_pcm_format_physical_width(params_format(p))*params_channels(p))/8)
#define params_periods(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_PERIODS)->min
#define params_buffer_size(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min
#define params_buffer_bytes(p) hw_param_interval((p), SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min


T
Takashi Iwai 已提交
754 755 756 757 758 759 760 761 762 763
int snd_interval_refine(struct snd_interval *i, const struct snd_interval *v);
void snd_interval_mul(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
void snd_interval_div(const struct snd_interval *a, const struct snd_interval *b, struct snd_interval *c);
void snd_interval_muldivk(const struct snd_interval *a, const struct snd_interval *b, 
			  unsigned int k, struct snd_interval *c);
void snd_interval_mulkdiv(const struct snd_interval *a, unsigned int k,
			  const struct snd_interval *b, struct snd_interval *c);
int snd_interval_list(struct snd_interval *i, unsigned int count, unsigned int *list, unsigned int mask);
int snd_interval_ratnum(struct snd_interval *i,
			unsigned int rats_count, struct snd_ratnum *rats,
L
Linus Torvalds 已提交
764 765
			unsigned int *nump, unsigned int *denp);

T
Takashi Iwai 已提交
766 767 768
void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params);
void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params, snd_pcm_hw_param_t var);
int snd_pcm_hw_params_choose(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
L
Linus Torvalds 已提交
769

T
Takashi Iwai 已提交
770
int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
L
Linus Torvalds 已提交
771

T
Takashi Iwai 已提交
772 773
int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream);
int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
774

T
Takashi Iwai 已提交
775
int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
L
Linus Torvalds 已提交
776
			       u_int32_t mask);
T
Takashi Iwai 已提交
777
int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
L
Linus Torvalds 已提交
778
				 u_int64_t mask);
T
Takashi Iwai 已提交
779
int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
L
Linus Torvalds 已提交
780
				 unsigned int min, unsigned int max);
T
Takashi Iwai 已提交
781 782
int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var);
int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime, 
L
Linus Torvalds 已提交
783 784
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
T
Takashi Iwai 已提交
785 786
			       struct snd_pcm_hw_constraint_list *l);
int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 
L
Linus Torvalds 已提交
787 788
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
T
Takashi Iwai 已提交
789 790
				  struct snd_pcm_hw_constraint_ratnums *r);
int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 
L
Linus Torvalds 已提交
791 792
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
T
Takashi Iwai 已提交
793 794
				  struct snd_pcm_hw_constraint_ratdens *r);
int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 
L
Linus Torvalds 已提交
795 796 797
				 unsigned int cond,
				 unsigned int width,
				 unsigned int msbits);
T
Takashi Iwai 已提交
798
int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
L
Linus Torvalds 已提交
799 800 801
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
			       unsigned long step);
T
Takashi Iwai 已提交
802
int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
L
Linus Torvalds 已提交
803 804
			       unsigned int cond,
			       snd_pcm_hw_param_t var);
T
Takashi Iwai 已提交
805
int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
L
Linus Torvalds 已提交
806 807 808 809 810 811 812 813 814 815
			unsigned int cond,
			int var,
			snd_pcm_hw_rule_func_t func, void *private,
			int dep, ...);

int snd_pcm_format_signed(snd_pcm_format_t format);
int snd_pcm_format_unsigned(snd_pcm_format_t format);
int snd_pcm_format_linear(snd_pcm_format_t format);
int snd_pcm_format_little_endian(snd_pcm_format_t format);
int snd_pcm_format_big_endian(snd_pcm_format_t format);
T
Takashi Iwai 已提交
816 817
#if 0 /* just for DocBook */
/**
818 819 820 821 822 823
 * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
 * @format: the format to check
 *
 * Returns 1 if the given PCM format is CPU-endian, 0 if
 * opposite, or a negative error code if endian not specified.
 */
824
int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
T
Takashi Iwai 已提交
825
#endif /* DocBook */
826
#ifdef SNDRV_LITTLE_ENDIAN
827
#define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
828
#else
829
#define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
830
#endif
L
Linus Torvalds 已提交
831 832
int snd_pcm_format_width(snd_pcm_format_t format);			/* in bits */
int snd_pcm_format_physical_width(snd_pcm_format_t format);		/* in bits */
833
ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
L
Linus Torvalds 已提交
834 835 836 837
const unsigned char *snd_pcm_format_silence_64(snd_pcm_format_t format);
int snd_pcm_format_set_silence(snd_pcm_format_t format, void *buf, unsigned int frames);
snd_pcm_format_t snd_pcm_build_linear_format(int width, int unsignd, int big_endian);

T
Takashi Iwai 已提交
838 839 840 841
void snd_pcm_set_ops(struct snd_pcm * pcm, int direction, struct snd_pcm_ops *ops);
void snd_pcm_set_sync(struct snd_pcm_substream *substream);
int snd_pcm_lib_interleave_len(struct snd_pcm_substream *substream);
int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
842
		      unsigned int cmd, void *arg);                      
843 844
int snd_pcm_update_state(struct snd_pcm_substream *substream,
			 struct snd_pcm_runtime *runtime);
T
Takashi Iwai 已提交
845 846 847 848 849 850 851 852
int snd_pcm_update_hw_ptr(struct snd_pcm_substream *substream);
int snd_pcm_playback_xrun_check(struct snd_pcm_substream *substream);
int snd_pcm_capture_xrun_check(struct snd_pcm_substream *substream);
int snd_pcm_playback_xrun_asap(struct snd_pcm_substream *substream);
int snd_pcm_capture_xrun_asap(struct snd_pcm_substream *substream);
void snd_pcm_playback_silence(struct snd_pcm_substream *substream, snd_pcm_uframes_t new_hw_ptr);
void snd_pcm_period_elapsed(struct snd_pcm_substream *substream);
snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
853 854
				    const void __user *buf,
				    snd_pcm_uframes_t frames);
T
Takashi Iwai 已提交
855
snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
856
				   void __user *buf, snd_pcm_uframes_t frames);
T
Takashi Iwai 已提交
857
snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
858
				     void __user **bufs, snd_pcm_uframes_t frames);
T
Takashi Iwai 已提交
859
snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
860 861
				    void __user **bufs, snd_pcm_uframes_t frames);

862 863
extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;

T
Takashi Iwai 已提交
864
int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime);
865
unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
L
Linus Torvalds 已提交
866

T
Takashi Iwai 已提交
867
static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
868 869
					      struct snd_dma_buffer *bufp)
{
T
Takashi Iwai 已提交
870
	struct snd_pcm_runtime *runtime = substream->runtime;
L
Linus Torvalds 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
	if (bufp) {
		runtime->dma_buffer_p = bufp;
		runtime->dma_area = bufp->area;
		runtime->dma_addr = bufp->addr;
		runtime->dma_bytes = bufp->bytes;
	} else {
		runtime->dma_buffer_p = NULL;
		runtime->dma_area = NULL;
		runtime->dma_addr = 0;
		runtime->dma_bytes = 0;
	}
}

/*
 *  Timer interface
 */

T
Takashi Iwai 已提交
888 889 890
void snd_pcm_timer_resolution_change(struct snd_pcm_substream *substream);
void snd_pcm_timer_init(struct snd_pcm_substream *substream);
void snd_pcm_timer_done(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
891

892 893 894 895 896 897 898 899 900
static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
				   struct timespec *tv)
{
	if (runtime->tstamp_type == SNDRV_PCM_TSTAMP_TYPE_MONOTONIC)
		do_posix_clock_monotonic_gettime(tv);
	else
		getnstimeofday(tv);
}

L
Linus Torvalds 已提交
901 902 903 904
/*
 *  Memory
 */

T
Takashi Iwai 已提交
905 906 907
int snd_pcm_lib_preallocate_free(struct snd_pcm_substream *substream);
int snd_pcm_lib_preallocate_free_for_all(struct snd_pcm *pcm);
int snd_pcm_lib_preallocate_pages(struct snd_pcm_substream *substream,
L
Linus Torvalds 已提交
908 909
				  int type, struct device *data,
				  size_t size, size_t max);
T
Takashi Iwai 已提交
910
int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
L
Linus Torvalds 已提交
911 912
					  int type, void *data,
					  size_t size, size_t max);
T
Takashi Iwai 已提交
913 914
int snd_pcm_lib_malloc_pages(struct snd_pcm_substream *substream, size_t size);
int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream);
L
Linus Torvalds 已提交
915

916
#ifdef CONFIG_SND_DMA_SGBUF
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
/*
 * SG-buffer handling
 */
#define snd_pcm_substream_sgbuf(substream) \
	((substream)->runtime->dma_buffer_p->private_data)

static inline dma_addr_t
snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
{
	struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
	return snd_sgbuf_get_addr(sg, ofs);
}

static inline void *
snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
{
	struct snd_sg_buf *sg = snd_pcm_substream_sgbuf(substream);
	return snd_sgbuf_get_ptr(sg, ofs);
}

struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
				    unsigned long offset);
939 940
unsigned int snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
					  unsigned int ofs, unsigned int size);
L
Linus Torvalds 已提交
941

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
#else /* !SND_DMA_SGBUF */
/*
 * fake using a continuous buffer
 */
static inline dma_addr_t
snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
{
	return substream->runtime->dma_addr + ofs;
}

static inline void *
snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
{
	return substream->runtime->dma_area + ofs;
}

#define snd_pcm_sgbuf_ops_page	NULL

#define snd_pcm_sgbuf_get_chunk_size(subs, ofs, size)	(size)

#endif /* SND_DMA_SGBUF */

L
Linus Torvalds 已提交
964 965 966
/* handle mmap counter - PCM mmap callback should handle this counter properly */
static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
{
T
Takashi Iwai 已提交
967
	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
968
	atomic_inc(&substream->mmap_count);
L
Linus Torvalds 已提交
969 970 971 972
}

static inline void snd_pcm_mmap_data_close(struct vm_area_struct *area)
{
T
Takashi Iwai 已提交
973
	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
974
	atomic_dec(&substream->mmap_count);
L
Linus Torvalds 已提交
975 976 977 978 979
}

/* mmap for io-memory area */
#if defined(CONFIG_X86) || defined(CONFIG_PPC) || defined(CONFIG_ALPHA)
#define SNDRV_PCM_INFO_MMAP_IOMEM	SNDRV_PCM_INFO_MMAP
T
Takashi Iwai 已提交
980
int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
L
Linus Torvalds 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
#else
#define SNDRV_PCM_INFO_MMAP_IOMEM	0
#define snd_pcm_lib_mmap_iomem	NULL
#endif

static inline void snd_pcm_limit_isa_dma_size(int dma, size_t *max)
{
	*max = dma < 4 ? 64 * 1024 : 128 * 1024;
}

/*
 *  Misc
 */

#define SNDRV_PCM_DEFAULT_CON_SPDIF	(IEC958_AES0_CON_EMPHASIS_NONE|\
					 (IEC958_AES1_CON_ORIGINAL<<8)|\
					 (IEC958_AES1_CON_PCM_CODER<<8)|\
					 (IEC958_AES3_CON_FS_48000<<24))

1000 1001
#define PCM_RUNTIME_CHECK(sub) snd_BUG_ON(!(sub) || !(sub)->runtime)

1002 1003
const char *snd_pcm_format_name(snd_pcm_format_t format);

L
Linus Torvalds 已提交
1004
#endif /* __SOUND_PCM_H */