pcm.h 39.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
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>
Linus Torvalds's avatar
Linus Torvalds committed
29
#include <linux/poll.h>
Al Viro's avatar
Al Viro committed
30
#include <linux/mm.h>
Linus Torvalds's avatar
Linus Torvalds committed
31
#include <linux/bitops.h>
32
#include <linux/pm_qos.h>
Linus Torvalds's avatar
Linus Torvalds committed
33 34 35 36 37

#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)
38
#include <sound/pcm_oss.h>
Linus Torvalds's avatar
Linus Torvalds committed
39 40 41 42 43 44
#endif

/*
 *  Hardware (lowlevel) section
 */

45
struct snd_pcm_hardware {
Linus Torvalds's avatar
Linus Torvalds committed
46 47 48 49 50 51 52 53 54 55 56 57 58
	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 */
59
};
Linus Torvalds's avatar
Linus Torvalds committed
60

61 62
struct snd_pcm_substream;

63 64 65 66
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,
Linus Torvalds's avatar
Linus Torvalds committed
67
		     unsigned int cmd, void *arg);
68 69 70 71 72 73
	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);
74 75
	int (*wall_clock)(struct snd_pcm_substream *substream,
			  struct timespec *audio_ts);
76 77
	int (*copy)(struct snd_pcm_substream *substream, int channel,
		    snd_pcm_uframes_t pos,
Linus Torvalds's avatar
Linus Torvalds committed
78
		    void __user *buf, snd_pcm_uframes_t count);
79
	int (*silence)(struct snd_pcm_substream *substream, int channel, 
Linus Torvalds's avatar
Linus Torvalds committed
80
		       snd_pcm_uframes_t pos, snd_pcm_uframes_t count);
81 82 83 84 85
	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);
};
Linus Torvalds's avatar
Linus Torvalds committed
86 87 88 89 90

/*
 *
 */

91 92 93 94 95
#if defined(CONFIG_SND_DYNAMIC_MINORS)
#define SNDRV_PCM_DEVICES	(SNDRV_OS_MINORS-2)
#else
#define SNDRV_PCM_DEVICES	8
#endif
96

Linus Torvalds's avatar
Linus Torvalds committed
97 98 99 100 101 102 103
#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
104
#define SNDRV_PCM_IOCTL1_FIFO_SIZE	4
Linus Torvalds's avatar
Linus Torvalds committed
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

#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)
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
#define _SNDRV_PCM_FMTBIT(fmt)		(1ULL << (__force int)SNDRV_PCM_FORMAT_##fmt)
#define SNDRV_PCM_FMTBIT_S8		_SNDRV_PCM_FMTBIT(S8)
#define SNDRV_PCM_FMTBIT_U8		_SNDRV_PCM_FMTBIT(U8)
#define SNDRV_PCM_FMTBIT_S16_LE		_SNDRV_PCM_FMTBIT(S16_LE)
#define SNDRV_PCM_FMTBIT_S16_BE		_SNDRV_PCM_FMTBIT(S16_BE)
#define SNDRV_PCM_FMTBIT_U16_LE		_SNDRV_PCM_FMTBIT(U16_LE)
#define SNDRV_PCM_FMTBIT_U16_BE		_SNDRV_PCM_FMTBIT(U16_BE)
#define SNDRV_PCM_FMTBIT_S24_LE		_SNDRV_PCM_FMTBIT(S24_LE)
#define SNDRV_PCM_FMTBIT_S24_BE		_SNDRV_PCM_FMTBIT(S24_BE)
#define SNDRV_PCM_FMTBIT_U24_LE		_SNDRV_PCM_FMTBIT(U24_LE)
#define SNDRV_PCM_FMTBIT_U24_BE		_SNDRV_PCM_FMTBIT(U24_BE)
#define SNDRV_PCM_FMTBIT_S32_LE		_SNDRV_PCM_FMTBIT(S32_LE)
#define SNDRV_PCM_FMTBIT_S32_BE		_SNDRV_PCM_FMTBIT(S32_BE)
#define SNDRV_PCM_FMTBIT_U32_LE		_SNDRV_PCM_FMTBIT(U32_LE)
#define SNDRV_PCM_FMTBIT_U32_BE		_SNDRV_PCM_FMTBIT(U32_BE)
#define SNDRV_PCM_FMTBIT_FLOAT_LE	_SNDRV_PCM_FMTBIT(FLOAT_LE)
#define SNDRV_PCM_FMTBIT_FLOAT_BE	_SNDRV_PCM_FMTBIT(FLOAT_BE)
#define SNDRV_PCM_FMTBIT_FLOAT64_LE	_SNDRV_PCM_FMTBIT(FLOAT64_LE)
#define SNDRV_PCM_FMTBIT_FLOAT64_BE	_SNDRV_PCM_FMTBIT(FLOAT64_BE)
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_LE)
#define SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE _SNDRV_PCM_FMTBIT(IEC958_SUBFRAME_BE)
#define SNDRV_PCM_FMTBIT_MU_LAW		_SNDRV_PCM_FMTBIT(MU_LAW)
#define SNDRV_PCM_FMTBIT_A_LAW		_SNDRV_PCM_FMTBIT(A_LAW)
#define SNDRV_PCM_FMTBIT_IMA_ADPCM	_SNDRV_PCM_FMTBIT(IMA_ADPCM)
#define SNDRV_PCM_FMTBIT_MPEG		_SNDRV_PCM_FMTBIT(MPEG)
#define SNDRV_PCM_FMTBIT_GSM		_SNDRV_PCM_FMTBIT(GSM)
#define SNDRV_PCM_FMTBIT_SPECIAL	_SNDRV_PCM_FMTBIT(SPECIAL)
#define SNDRV_PCM_FMTBIT_S24_3LE	_SNDRV_PCM_FMTBIT(S24_3LE)
#define SNDRV_PCM_FMTBIT_U24_3LE	_SNDRV_PCM_FMTBIT(U24_3LE)
#define SNDRV_PCM_FMTBIT_S24_3BE	_SNDRV_PCM_FMTBIT(S24_3BE)
#define SNDRV_PCM_FMTBIT_U24_3BE	_SNDRV_PCM_FMTBIT(U24_3BE)
#define SNDRV_PCM_FMTBIT_S20_3LE	_SNDRV_PCM_FMTBIT(S20_3LE)
#define SNDRV_PCM_FMTBIT_U20_3LE	_SNDRV_PCM_FMTBIT(U20_3LE)
#define SNDRV_PCM_FMTBIT_S20_3BE	_SNDRV_PCM_FMTBIT(S20_3BE)
#define SNDRV_PCM_FMTBIT_U20_3BE	_SNDRV_PCM_FMTBIT(U20_3BE)
#define SNDRV_PCM_FMTBIT_S18_3LE	_SNDRV_PCM_FMTBIT(S18_3LE)
#define SNDRV_PCM_FMTBIT_U18_3LE	_SNDRV_PCM_FMTBIT(U18_3LE)
#define SNDRV_PCM_FMTBIT_S18_3BE	_SNDRV_PCM_FMTBIT(S18_3BE)
#define SNDRV_PCM_FMTBIT_U18_3BE	_SNDRV_PCM_FMTBIT(U18_3BE)
#define SNDRV_PCM_FMTBIT_G723_24	_SNDRV_PCM_FMTBIT(G723_24)
#define SNDRV_PCM_FMTBIT_G723_24_1B	_SNDRV_PCM_FMTBIT(G723_24_1B)
#define SNDRV_PCM_FMTBIT_G723_40	_SNDRV_PCM_FMTBIT(G723_40)
#define SNDRV_PCM_FMTBIT_G723_40_1B	_SNDRV_PCM_FMTBIT(G723_40_1B)
Daniel Mack's avatar
Daniel Mack committed
184 185
#define SNDRV_PCM_FMTBIT_DSD_U8		_SNDRV_PCM_FMTBIT(DSD_U8)
#define SNDRV_PCM_FMTBIT_DSD_U16_LE	_SNDRV_PCM_FMTBIT(DSD_U16_LE)
Linus Torvalds's avatar
Linus Torvalds committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

#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

210 211
struct snd_pcm_file {
	struct snd_pcm_substream *substream;
212
	int no_compat_mmap;
Linus Torvalds's avatar
Linus Torvalds committed
213 214
};

215 216 217
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);
Linus Torvalds's avatar
Linus Torvalds committed
218

219
struct snd_pcm_hw_rule {
Linus Torvalds's avatar
Linus Torvalds committed
220 221 222 223 224 225 226
	unsigned int cond;
	snd_pcm_hw_rule_func_t func;
	int var;
	int deps[4];
	void *private;
};

227 228
struct snd_pcm_hw_constraints {
	struct snd_mask masks[SNDRV_PCM_HW_PARAM_LAST_MASK - 
Linus Torvalds's avatar
Linus Torvalds committed
229
			 SNDRV_PCM_HW_PARAM_FIRST_MASK + 1];
230
	struct snd_interval intervals[SNDRV_PCM_HW_PARAM_LAST_INTERVAL -
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233
			     SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1];
	unsigned int rules_num;
	unsigned int rules_all;
234 235
	struct snd_pcm_hw_rule *rules;
};
Linus Torvalds's avatar
Linus Torvalds committed
236

237 238
static inline struct snd_mask *constrs_mask(struct snd_pcm_hw_constraints *constrs,
					    snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
239 240 241 242
{
	return &constrs->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
}

243 244
static inline struct snd_interval *constrs_interval(struct snd_pcm_hw_constraints *constrs,
						    snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
245 246 247 248
{
	return &constrs->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
}

249
struct snd_ratnum {
Linus Torvalds's avatar
Linus Torvalds committed
250 251
	unsigned int num;
	unsigned int den_min, den_max, den_step;
252
};
Linus Torvalds's avatar
Linus Torvalds committed
253

254
struct snd_ratden {
Linus Torvalds's avatar
Linus Torvalds committed
255 256
	unsigned int num_min, num_max, num_step;
	unsigned int den;
257
};
Linus Torvalds's avatar
Linus Torvalds committed
258

259
struct snd_pcm_hw_constraint_ratnums {
Linus Torvalds's avatar
Linus Torvalds committed
260
	int nrats;
261 262
	struct snd_ratnum *rats;
};
Linus Torvalds's avatar
Linus Torvalds committed
263

264
struct snd_pcm_hw_constraint_ratdens {
Linus Torvalds's avatar
Linus Torvalds committed
265
	int nrats;
266 267
	struct snd_ratden *rats;
};
Linus Torvalds's avatar
Linus Torvalds committed
268

269
struct snd_pcm_hw_constraint_list {
Linus Torvalds's avatar
Linus Torvalds committed
270
	unsigned int count;
271
	const unsigned int *list;
Linus Torvalds's avatar
Linus Torvalds committed
272
	unsigned int mask;
273
};
Linus Torvalds's avatar
Linus Torvalds committed
274

275 276
struct snd_pcm_hwptr_log;

277
struct snd_pcm_runtime {
Linus Torvalds's avatar
Linus Torvalds committed
278
	/* -- Status -- */
279
	struct snd_pcm_substream *trigger_master;
Takashi Iwai's avatar
Takashi Iwai committed
280
	struct timespec trigger_tstamp;	/* trigger timestamp */
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283
	int overrange;
	snd_pcm_uframes_t avail_max;
	snd_pcm_uframes_t hw_ptr_base;	/* Position at buffer restart */
284
	snd_pcm_uframes_t hw_ptr_interrupt; /* Position at interrupt time */
285
	unsigned long hw_ptr_jiffies;	/* Time when hw_ptr is updated */
286
	unsigned long hw_ptr_buffer_jiffies; /* buffer time in jiffies */
287
	snd_pcm_sframes_t delay;	/* extra delay; typically FIFO size */
288
	u64 hw_ptr_wrap;                /* offset for hw_ptr due to boundary wrap-around */
Linus Torvalds's avatar
Linus Torvalds committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	/* -- 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;
306
	unsigned int no_period_wakeup: 1;
Linus Torvalds's avatar
Linus Torvalds committed
307 308

	/* -- SW params -- */
309
	int tstamp_mode;		/* mmap timestamp is updated */
Linus Torvalds's avatar
Linus Torvalds committed
310 311 312 313 314 315 316 317 318 319 320
  	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 */

321
	union snd_pcm_sync_id sync;	/* hardware synchronization ID */
Linus Torvalds's avatar
Linus Torvalds committed
322 323

	/* -- mmap -- */
324 325
	struct snd_pcm_mmap_status *status;
	struct snd_pcm_mmap_control *control;
Linus Torvalds's avatar
Linus Torvalds committed
326 327

	/* -- locking / scheduling -- */
328
	snd_pcm_uframes_t twake; 	/* do transfer (!poll) wakeup if non-zero */
329 330
	wait_queue_head_t sleep;	/* poll sleep */
	wait_queue_head_t tsleep;	/* transfer sleep */
Linus Torvalds's avatar
Linus Torvalds committed
331 332 333 334
	struct fasync_struct *fasync;

	/* -- private section -- */
	void *private_data;
335
	void (*private_free)(struct snd_pcm_runtime *runtime);
Linus Torvalds's avatar
Linus Torvalds committed
336 337

	/* -- hardware description -- */
338 339
	struct snd_pcm_hardware hw;
	struct snd_pcm_hw_constraints hw_constraints;
Linus Torvalds's avatar
Linus Torvalds committed
340 341

	/* -- interrupt callbacks -- */
342 343
	void (*transfer_ack_begin)(struct snd_pcm_substream *substream);
	void (*transfer_ack_end)(struct snd_pcm_substream *substream);
Linus Torvalds's avatar
Linus Torvalds committed
344 345 346

	/* -- timer -- */
	unsigned int timer_resolution;	/* timer resolution */
347
	int tstamp_type;		/* timestamp type */
Linus Torvalds's avatar
Linus Torvalds committed
348 349 350 351 352 353 354 355 356 357

	/* -- 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 -- */
358
	struct snd_pcm_oss_runtime oss;
Linus Torvalds's avatar
Linus Torvalds committed
359
#endif
360 361 362 363

#ifdef CONFIG_SND_PCM_XRUN_DEBUG
	struct snd_pcm_hwptr_log *hwptr_log;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
364 365
};

366
struct snd_pcm_group {		/* keep linked substreams */
Linus Torvalds's avatar
Linus Torvalds committed
367
	spinlock_t lock;
368
	struct mutex mutex;
Linus Torvalds's avatar
Linus Torvalds committed
369 370
	struct list_head substreams;
	int count;
371
};
Linus Torvalds's avatar
Linus Torvalds committed
372

373 374
struct pid;

375 376 377
struct snd_pcm_substream {
	struct snd_pcm *pcm;
	struct snd_pcm_str *pstr;
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381
	void *private_data;		/* copied from pcm->private_data */
	int number;
	char name[32];			/* substream name */
	int stream;			/* stream (direction) */
Jean Pihet's avatar
Jean Pihet committed
382
	struct pm_qos_request latency_pm_qos_req; /* pm_qos request */
Linus Torvalds's avatar
Linus Torvalds committed
383 384 385 386
	size_t buffer_bytes_max;	/* limit ring buffer size */
	struct snd_dma_buffer dma_buffer;
	size_t dma_max;
	/* -- hardware operations -- */
387
	const struct snd_pcm_ops *ops;
Linus Torvalds's avatar
Linus Torvalds committed
388
	/* -- runtime information -- */
389
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
390
        /* -- timer section -- */
391
	struct snd_timer *timer;		/* timer */
Linus Torvalds's avatar
Linus Torvalds committed
392 393
	unsigned timer_running: 1;	/* time is running */
	/* -- next substream -- */
394
	struct snd_pcm_substream *next;
Linus Torvalds's avatar
Linus Torvalds committed
395 396
	/* -- linked substreams -- */
	struct list_head link_list;	/* linked list member */
397 398
	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 */
Linus Torvalds's avatar
Linus Torvalds committed
399
	/* -- assigned files -- */
400
	void *file;
401
	int ref_count;
402
	atomic_t mmap_count;
403
	unsigned int f_flags;
404
	void (*pcm_release)(struct snd_pcm_substream *);
405
	struct pid *pid;
Linus Torvalds's avatar
Linus Torvalds committed
406 407
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
	/* -- OSS things -- */
408
	struct snd_pcm_oss_substream oss;
Linus Torvalds's avatar
Linus Torvalds committed
409
#endif
410
#ifdef CONFIG_SND_VERBOSE_PROCFS
411 412 413 414 415 416
	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;
417
	struct snd_info_entry *proc_prealloc_max_entry;
418
#endif
Linus Torvalds's avatar
Linus Torvalds committed
419
	/* misc flags */
420
	unsigned int hw_opened: 1;
Linus Torvalds's avatar
Linus Torvalds committed
421 422
};

423
#define SUBSTREAM_BUSY(substream) ((substream)->ref_count > 0)
Linus Torvalds's avatar
Linus Torvalds committed
424 425


426
struct snd_pcm_str {
Linus Torvalds's avatar
Linus Torvalds committed
427
	int stream;				/* stream (direction) */
428
	struct snd_pcm *pcm;
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431
	/* -- substreams -- */
	unsigned int substream_count;
	unsigned int substream_opened;
432
	struct snd_pcm_substream *substream;
Linus Torvalds's avatar
Linus Torvalds committed
433 434
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
	/* -- OSS things -- */
435
	struct snd_pcm_oss_stream oss;
Linus Torvalds's avatar
Linus Torvalds committed
436
#endif
437
#ifdef CONFIG_SND_VERBOSE_PROCFS
438 439
	struct snd_info_entry *proc_root;
	struct snd_info_entry *proc_info_entry;
440
#ifdef CONFIG_SND_PCM_XRUN_DEBUG
Linus Torvalds's avatar
Linus Torvalds committed
441
	unsigned int xrun_debug;	/* 0 = disabled, 1 = verbose, 2 = stacktrace */
442
	struct snd_info_entry *proc_xrun_debug_entry;
Linus Torvalds's avatar
Linus Torvalds committed
443
#endif
444
#endif
445
	struct snd_kcontrol *chmap_kctl; /* channel-mapping controls */
Linus Torvalds's avatar
Linus Torvalds committed
446 447
};

448 449
struct snd_pcm {
	struct snd_card *card;
450
	struct list_head list;
451
	int device; /* device number */
Linus Torvalds's avatar
Linus Torvalds committed
452 453 454 455 456
	unsigned int info_flags;
	unsigned short dev_class;
	unsigned short dev_subclass;
	char id[64];
	char name[80];
457
	struct snd_pcm_str streams[2];
458
	struct mutex open_mutex;
Linus Torvalds's avatar
Linus Torvalds committed
459 460
	wait_queue_head_t open_wait;
	void *private_data;
461
	void (*private_free) (struct snd_pcm *pcm);
462
	struct device *dev; /* actual hw device this belongs to */
463
	bool internal; /* pcm is for internal use only */
464
	bool nonatomic; /* whole PCM operations are in non-atomic context */
Linus Torvalds's avatar
Linus Torvalds committed
465
#if defined(CONFIG_SND_PCM_OSS) || defined(CONFIG_SND_PCM_OSS_MODULE)
466
	struct snd_pcm_oss oss;
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469
#endif
};

470 471 472 473
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);
Linus Torvalds's avatar
Linus Torvalds committed
474
	struct list_head list;
475
};
Linus Torvalds's avatar
Linus Torvalds committed
476 477 478 479 480

/*
 *  Registering
 */

481
extern const struct file_operations snd_pcm_f_ops[2];
Linus Torvalds's avatar
Linus Torvalds committed
482

483
int snd_pcm_new(struct snd_card *card, const char *id, int device,
Linus Torvalds's avatar
Linus Torvalds committed
484
		int playback_count, int capture_count,
485
		struct snd_pcm **rpcm);
486 487 488
int snd_pcm_new_internal(struct snd_card *card, const char *id, int device,
		int playback_count, int capture_count,
		struct snd_pcm **rpcm);
489
int snd_pcm_new_stream(struct snd_pcm *pcm, int stream, int substream_count);
Linus Torvalds's avatar
Linus Torvalds committed
490

491
int snd_pcm_notify(struct snd_pcm_notify *notify, int nfree);
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495 496

/*
 *  Native I/O
 */

497 498 499 500 501 502
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);
503
int snd_pcm_stop(struct snd_pcm_substream *substream, snd_pcm_state_t status);
504
int snd_pcm_drain_done(struct snd_pcm_substream *substream);
Linus Torvalds's avatar
Linus Torvalds committed
505
#ifdef CONFIG_PM
506 507
int snd_pcm_suspend(struct snd_pcm_substream *substream);
int snd_pcm_suspend_all(struct snd_pcm *pcm);
Linus Torvalds's avatar
Linus Torvalds committed
508
#endif
509
int snd_pcm_kernel_ioctl(struct snd_pcm_substream *substream, unsigned int cmd, void *arg);
510 511
int snd_pcm_open_substream(struct snd_pcm *pcm, int stream, struct file *file,
			   struct snd_pcm_substream **rsubstream);
512
void snd_pcm_release_substream(struct snd_pcm_substream *substream);
513 514 515
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);
Linus Torvalds's avatar
Linus Torvalds committed
516
void snd_pcm_vma_notify_data(void *client, void *data);
517
int snd_pcm_mmap_data(struct snd_pcm_substream *substream, struct file *file, struct vm_area_struct *area);
Linus Torvalds's avatar
Linus Torvalds committed
518

519 520 521 522 523 524 525 526 527 528 529 530

#ifdef CONFIG_SND_DEBUG
void snd_pcm_debug_name(struct snd_pcm_substream *substream,
			   char *name, size_t len);
#else
static inline void
snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
{
	*buf = 0;
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
531 532 533 534
/*
 *  PCM library
 */

535
static inline int snd_pcm_stream_linked(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539
{
	return substream->group != &substream->self_group;
}

540 541 542 543 544 545 546 547 548 549 550 551
void snd_pcm_stream_lock(struct snd_pcm_substream *substream);
void snd_pcm_stream_unlock(struct snd_pcm_substream *substream);
void snd_pcm_stream_lock_irq(struct snd_pcm_substream *substream);
void snd_pcm_stream_unlock_irq(struct snd_pcm_substream *substream);
unsigned long _snd_pcm_stream_lock_irqsave(struct snd_pcm_substream *substream);
#define snd_pcm_stream_lock_irqsave(substream, flags)		 \
	do {							 \
		typecheck(unsigned long, flags);		 \
		flags = _snd_pcm_stream_lock_irqsave(substream); \
	} while (0)
void snd_pcm_stream_unlock_irqrestore(struct snd_pcm_substream *substream,
				      unsigned long flags);
Linus Torvalds's avatar
Linus Torvalds committed
552

553 554
#define snd_pcm_group_for_each_entry(s, substream) \
	list_for_each_entry(s, &substream->group->substreams, link_list)
Linus Torvalds's avatar
Linus Torvalds committed
555

556
static inline int snd_pcm_running(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
557 558 559 560 561 562
{
	return (substream->runtime->status->state == SNDRV_PCM_STATE_RUNNING ||
		(substream->runtime->status->state == SNDRV_PCM_STATE_DRAINING &&
		 substream->stream == SNDRV_PCM_STREAM_PLAYBACK));
}

563
static inline ssize_t bytes_to_samples(struct snd_pcm_runtime *runtime, ssize_t size)
Linus Torvalds's avatar
Linus Torvalds committed
564 565 566 567
{
	return size * 8 / runtime->sample_bits;
}

568
static inline snd_pcm_sframes_t bytes_to_frames(struct snd_pcm_runtime *runtime, ssize_t size)
Linus Torvalds's avatar
Linus Torvalds committed
569 570 571 572
{
	return size * 8 / runtime->frame_bits;
}

573
static inline ssize_t samples_to_bytes(struct snd_pcm_runtime *runtime, ssize_t size)
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577
{
	return size * runtime->sample_bits / 8;
}

578
static inline ssize_t frames_to_bytes(struct snd_pcm_runtime *runtime, snd_pcm_sframes_t size)
Linus Torvalds's avatar
Linus Torvalds committed
579 580 581 582
{
	return size * runtime->frame_bits / 8;
}

583
static inline int frame_aligned(struct snd_pcm_runtime *runtime, ssize_t bytes)
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586 587
{
	return bytes % runtime->byte_align == 0;
}

588
static inline size_t snd_pcm_lib_buffer_bytes(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
589
{
590
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
591 592 593
	return frames_to_bytes(runtime, runtime->buffer_size);
}

594
static inline size_t snd_pcm_lib_period_bytes(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
595
{
596
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
597 598 599 600 601 602
	return frames_to_bytes(runtime, runtime->period_size);
}

/*
 *  result is: 0 ... (boundary - 1)
 */
603
static inline snd_pcm_uframes_t snd_pcm_playback_avail(struct snd_pcm_runtime *runtime)
Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607 608 609 610 611 612 613 614 615
{
	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)
 */
616
static inline snd_pcm_uframes_t snd_pcm_capture_avail(struct snd_pcm_runtime *runtime)
Linus Torvalds's avatar
Linus Torvalds committed
617 618 619 620 621 622 623
{
	snd_pcm_sframes_t avail = runtime->status->hw_ptr - runtime->control->appl_ptr;
	if (avail < 0)
		avail += runtime->boundary;
	return avail;
}

624
static inline snd_pcm_sframes_t snd_pcm_playback_hw_avail(struct snd_pcm_runtime *runtime)
Linus Torvalds's avatar
Linus Torvalds committed
625 626 627 628
{
	return runtime->buffer_size - snd_pcm_playback_avail(runtime);
}

629
static inline snd_pcm_sframes_t snd_pcm_capture_hw_avail(struct snd_pcm_runtime *runtime)
Linus Torvalds's avatar
Linus Torvalds committed
630 631 632 633 634 635 636 637 638 639
{
	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.
 *
640
 * Return: Non-zero if available, or zero if not.
Linus Torvalds's avatar
Linus Torvalds committed
641
 */
642
static inline int snd_pcm_playback_ready(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
643
{
644
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
645 646 647 648 649 650 651 652 653
	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.
 *
654
 * Return: Non-zero if available, or zero if not.
Linus Torvalds's avatar
Linus Torvalds committed
655
 */
656
static inline int snd_pcm_capture_ready(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
657
{
658
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662 663 664 665
	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
 *
666
 * Checks whether any data exists on the playback buffer.
Linus Torvalds's avatar
Linus Torvalds committed
667
 *
668 669
 * Return: Non-zero if any data exists, or zero if not. If stop_threshold
 * is bigger or equal to boundary, then this function returns always non-zero.
Linus Torvalds's avatar
Linus Torvalds committed
670
 */
671
static inline int snd_pcm_playback_data(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
672
{
673
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
674 675 676 677 678 679 680 681 682 683 684 685
	
	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.
 *
686
 * Return: Non-zero if empty, or zero if not.
Linus Torvalds's avatar
Linus Torvalds committed
687
 */
688
static inline int snd_pcm_playback_empty(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
689
{
690
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
691 692 693 694 695 696 697 698 699
	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.
 *
700
 * Return: Non-zero if empty, or zero if not.
Linus Torvalds's avatar
Linus Torvalds committed
701
 */
702
static inline int snd_pcm_capture_empty(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
703
{
704
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
705 706 707
	return snd_pcm_capture_avail(runtime) == 0;
}

708 709
static inline void snd_pcm_trigger_done(struct snd_pcm_substream *substream, 
					struct snd_pcm_substream *master)
Linus Torvalds's avatar
Linus Torvalds committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
{
	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;
}

726
static inline struct snd_mask *hw_param_mask(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
727 728 729 730 731
				     snd_pcm_hw_param_t var)
{
	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
}

732
static inline struct snd_interval *hw_param_interval(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
733 734 735 736 737
					     snd_pcm_hw_param_t var)
{
	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
}

738
static inline const struct snd_mask *hw_param_mask_c(const struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
739 740
					     snd_pcm_hw_param_t var)
{
741
	return &params->masks[var - SNDRV_PCM_HW_PARAM_FIRST_MASK];
Linus Torvalds's avatar
Linus Torvalds committed
742 743
}

744
static inline const struct snd_interval *hw_param_interval_c(const struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
745 746
						     snd_pcm_hw_param_t var)
{
747
	return &params->intervals[var - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL];
Linus Torvalds's avatar
Linus Torvalds committed
748 749
}

750 751 752 753 754 755 756 757 758 759 760 761
#define params_channels(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_CHANNELS)->min)
#define params_rate(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_RATE)->min)
#define params_period_size(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_PERIOD_SIZE)->min)
#define params_periods(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_PERIODS)->min)
#define params_buffer_size(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_BUFFER_SIZE)->min)
#define params_buffer_bytes(p) \
	(hw_param_interval_c((p), SNDRV_PCM_HW_PARAM_BUFFER_BYTES)->min)
Linus Torvalds's avatar
Linus Torvalds committed
762

763 764 765 766 767 768 769
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);
770 771
int snd_interval_list(struct snd_interval *i, unsigned int count,
		      const unsigned int *list, unsigned int mask);
772 773
int snd_interval_ratnum(struct snd_interval *i,
			unsigned int rats_count, struct snd_ratnum *rats,
Linus Torvalds's avatar
Linus Torvalds committed
774 775
			unsigned int *nump, unsigned int *denp);

776 777 778
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);
Linus Torvalds's avatar
Linus Torvalds committed
779

780
int snd_pcm_hw_refine(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params);
Linus Torvalds's avatar
Linus Torvalds committed
781

782 783
int snd_pcm_hw_constraints_init(struct snd_pcm_substream *substream);
int snd_pcm_hw_constraints_complete(struct snd_pcm_substream *substream);
Linus Torvalds's avatar
Linus Torvalds committed
784

785
int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
786
			       u_int32_t mask);
787
int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
788
				 u_int64_t mask);
789
int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
790
				 unsigned int min, unsigned int max);
791 792
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, 
Linus Torvalds's avatar
Linus Torvalds committed
793 794
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
795
			       const struct snd_pcm_hw_constraint_list *l);
796
int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
797 798
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
799 800
				  struct snd_pcm_hw_constraint_ratnums *r);
int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
801 802
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
803 804
				  struct snd_pcm_hw_constraint_ratdens *r);
int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
805 806 807
				 unsigned int cond,
				 unsigned int width,
				 unsigned int msbits);
808
int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
809 810 811
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
			       unsigned long step);
812
int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
813 814
			       unsigned int cond,
			       snd_pcm_hw_param_t var);
815 816
int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
			       unsigned int base_rate);
817
int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
818 819 820 821 822 823 824 825 826 827
			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);
828 829
#if 0 /* just for DocBook */
/**
830 831 832
 * snd_pcm_format_cpu_endian - Check the PCM format is CPU-endian
 * @format: the format to check
 *
833
 * Return: 1 if the given PCM format is CPU-endian, 0 if
834 835
 * opposite, or a negative error code if endian not specified.
 */
836
int snd_pcm_format_cpu_endian(snd_pcm_format_t format);
837
#endif /* DocBook */
838
#ifdef SNDRV_LITTLE_ENDIAN
839
#define snd_pcm_format_cpu_endian(format) snd_pcm_format_little_endian(format)
840
#else
841
#define snd_pcm_format_cpu_endian(format) snd_pcm_format_big_endian(format)
842
#endif
Linus Torvalds's avatar
Linus Torvalds committed
843 844
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 */
845
ssize_t snd_pcm_format_size(snd_pcm_format_t format, size_t samples);
Linus Torvalds's avatar
Linus Torvalds committed
846 847
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);
848
snd_pcm_format_t snd_pcm_build_linear_format(int width, int unsigned, int big_endian);
Linus Torvalds's avatar
Linus Torvalds committed
849

850 851
void snd_pcm_set_ops(struct snd_pcm * pcm, int direction,
		     const struct snd_pcm_ops *ops);
852 853 854
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,
Linus Torvalds's avatar
Linus Torvalds committed
855
		      unsigned int cmd, void *arg);                      
856 857
int snd_pcm_update_state(struct snd_pcm_substream *substream,
			 struct snd_pcm_runtime *runtime);
858 859 860 861 862 863 864 865
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,
Linus Torvalds's avatar
Linus Torvalds committed
866 867
				    const void __user *buf,
				    snd_pcm_uframes_t frames);
868
snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
869
				   void __user *buf, snd_pcm_uframes_t frames);
870
snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
871
				     void __user **bufs, snd_pcm_uframes_t frames);
872
snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
873 874
				    void __user **bufs, snd_pcm_uframes_t frames);

875 876
extern const struct snd_pcm_hw_constraint_list snd_pcm_known_rates;

877
int snd_pcm_limit_hw_rates(struct snd_pcm_runtime *runtime);
878
unsigned int snd_pcm_rate_to_rate_bit(unsigned int rate);
879
unsigned int snd_pcm_rate_bit_to_rate(unsigned int rate_bit);
880 881
unsigned int snd_pcm_rate_mask_intersect(unsigned int rates_a,
					 unsigned int rates_b);
Linus Torvalds's avatar
Linus Torvalds committed
882

883
static inline void snd_pcm_set_runtime_buffer(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
884 885
					      struct snd_dma_buffer *bufp)
{
886
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
	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
 */

904 905 906
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);
Linus Torvalds's avatar
Linus Torvalds committed
907

908 909 910
static inline void snd_pcm_gettime(struct snd_pcm_runtime *runtime,
				   struct timespec *tv)
{
911 912
	switch (runtime->tstamp_type) {
	case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC:
913
		ktime_get_ts(tv);
914 915 916 917 918
		break;
	case SNDRV_PCM_TSTAMP_TYPE_MONOTONIC_RAW:
		getrawmonotonic(tv);
		break;
	default:
919
		getnstimeofday(tv);
920 921
		break;
	}
922 923
}

Linus Torvalds's avatar
Linus Torvalds committed
924 925 926 927
/*
 *  Memory
 */

928 929 930
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,
Linus Torvalds's avatar
Linus Torvalds committed
931 932
				  int type, struct device *data,
				  size_t size, size_t max);
933
int snd_pcm_lib_preallocate_pages_for_all(struct snd_pcm *pcm,
Linus Torvalds's avatar
Linus Torvalds committed
934 935
					  int type, void *data,
					  size_t size, size_t max);
936 937
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);
Linus Torvalds's avatar
Linus Torvalds committed
938

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
int _snd_pcm_lib_alloc_vmalloc_buffer(struct snd_pcm_substream *substream,
				      size_t size, gfp_t gfp_flags);
int snd_pcm_lib_free_vmalloc_buffer(struct snd_pcm_substream *substream);
struct page *snd_pcm_lib_get_vmalloc_page(struct snd_pcm_substream *substream,
					  unsigned long offset);
#if 0 /* for kernel-doc */
/**
 * snd_pcm_lib_alloc_vmalloc_buffer - allocate virtual DMA buffer
 * @substream: the substream to allocate the buffer to
 * @size: the requested buffer size, in bytes
 *
 * Allocates the PCM substream buffer using vmalloc(), i.e., the memory is
 * contiguous in kernel virtual space, but not in physical memory.  Use this
 * if the buffer is accessed by kernel code but not by device DMA.
 *
954
 * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
955 956 957 958 959 960 961 962 963 964 965
 * code.
 */
static int snd_pcm_lib_alloc_vmalloc_buffer
			(struct snd_pcm_substream *substream, size_t size);
/**
 * snd_pcm_lib_alloc_vmalloc_32_buffer - allocate 32-bit-addressable buffer
 * @substream: the substream to allocate the buffer to
 * @size: the requested buffer size, in bytes
 *
 * This function works like snd_pcm_lib_alloc_vmalloc_buffer(), but uses
 * vmalloc_32(), i.e., the pages are allocated from 32-bit-addressable memory.
966 967 968
 *
 * Return: 1 if the buffer was changed, 0 if not changed, or a negative error
 * code.
969 970 971 972 973 974 975 976 977 978 979
 */
static int snd_pcm_lib_alloc_vmalloc_32_buffer
			(struct snd_pcm_substream *substream, size_t size);
#endif
#define snd_pcm_lib_alloc_vmalloc_buffer(subs, size) \
	_snd_pcm_lib_alloc_vmalloc_buffer \
			(subs, size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO)
#define snd_pcm_lib_alloc_vmalloc_32_buffer(subs, size) \
	_snd_pcm_lib_alloc_vmalloc_buffer \
			(subs, size, GFP_KERNEL | GFP_DMA32 | __GFP_ZERO)

980 981
#define snd_pcm_get_dma_buf(substream) ((substream)->runtime->dma_buffer_p)

982
#ifdef CONFIG_SND_DMA_SGBUF
983 984 985 986
/*
 * SG-buffer handling
 */
#define snd_pcm_substream_sgbuf(substream) \
987
	snd_pcm_get_dma_buf(substream)->private_data
988 989 990

struct page *snd_pcm_sgbuf_ops_page(struct snd_pcm_substream *substream,
				    unsigned long offset);
991 992 993 994
#else /* !SND_DMA_SGBUF */
/*
 * fake using a continuous buffer
 */
995 996 997
#define snd_pcm_sgbuf_ops_page	NULL
#endif /* SND_DMA_SGBUF */

998 999 1000
static inline dma_addr_t
snd_pcm_sgbuf_get_addr(struct snd_pcm_substream *substream, unsigned int ofs)
{
1001
	return snd_sgbuf_get_addr(snd_pcm_get_dma_buf(substream), ofs);
1002 1003 1004 1005 1006
}

static inline void *
snd_pcm_sgbuf_get_ptr(struct snd_pcm_substream *substream, unsigned int ofs)
{
1007
	return snd_sgbuf_get_ptr(snd_pcm_get_dma_buf(substream), ofs);
1008 1009
}

1010 1011 1012 1013 1014 1015
static inline unsigned int
snd_pcm_sgbuf_get_chunk_size(struct snd_pcm_substream *substream,
			     unsigned int ofs, unsigned int size)
{
	return snd_sgbuf_get_chunk_size(snd_pcm_get_dma_buf(substream), ofs, size);
}
1016

Linus Torvalds's avatar
Linus Torvalds committed
1017 1018 1019
/* handle mmap counter - PCM mmap callback should handle this counter properly */
static inline void snd_pcm_mmap_data_open(struct vm_area_struct *area)
{
1020
	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
1021
	atomic_inc(&substream->mmap_count);
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023 1024 1025
}

static inline void snd_pcm_mmap_data_close(struct vm_area_struct *area)
{
1026
	struct snd_pcm_substream *substream = (struct snd_pcm_substream *)area->vm_private_data;
1027
	atomic_dec(&substream->mmap_count);
Linus Torvalds's avatar
Linus Torvalds committed
1028 1029
}

1030 1031
int snd_pcm_lib_default_mmap(struct snd_pcm_substream *substream,
			     struct vm_area_struct *area);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033 1034
/* 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
1035
int snd_pcm_lib_mmap_iomem(struct snd_pcm_substream *substream, struct vm_area_struct *area);
Linus Torvalds's avatar
Linus Torvalds committed
1036 1037 1038 1039 1040
#else
#define SNDRV_PCM_INFO_MMAP_IOMEM	0
#define snd_pcm_lib_mmap_iomem	NULL
#endif

1041
#define snd_pcm_lib_mmap_vmalloc NULL
1042

Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
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))

1057 1058
#define PCM_RUNTIME_CHECK(sub) snd_BUG_ON(!(sub) || !(sub)->runtime)

1059 1060
const char *snd_pcm_format_name(snd_pcm_format_t format);

1061
/**
1062 1063
 * snd_pcm_stream_str - Get a string naming the direction of a stream
 * @substream: the pcm substream instance
1064 1065
 *
 * Return: A string naming the direction of the stream.
1066 1067 1068 1069 1070 1071 1072 1073 1074
 */
static inline const char *snd_pcm_stream_str(struct snd_pcm_substream *substream)
{
	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
		return "Playback";
	else
		return "Capture";
}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/*
 * PCM channel-mapping control API
 */
/* array element of channel maps */
struct snd_pcm_chmap_elem {
	unsigned char channels;
	unsigned char map[15];
};

/* channel map information; retrieved via snd_kcontrol_chip() */
struct snd_pcm_chmap {
	struct snd_pcm *pcm;	/* assigned PCM instance */
	int stream;		/* PLAYBACK or CAPTURE */
	struct snd_kcontrol *kctl;
	const struct snd_pcm_chmap_elem *chmap;
	unsigned int max_channels;
	unsigned int channel_mask;	/* optional: active channels bitmask */
	void *private_data;	/* optional: private data pointer */
};

/* get the PCM substream assigned to the given chmap info */
static inline struct snd_pcm_substream *
snd_pcm_chmap_substream(struct snd_pcm_chmap *info, unsigned int idx)
{
	struct snd_pcm_substream *s;
	for (s = info->pcm->streams[info->stream].substream; s; s = s->next)
		if (s->number == idx)
			return s;
	return NULL;
}

/* ALSA-standard channel maps (RL/RR prior to C/LFE) */
extern const struct snd_pcm_chmap_elem snd_pcm_std_chmaps[];
/* Other world's standard channel maps (C/LFE prior to RL/RR) */
extern const struct snd_pcm_chmap_elem snd_pcm_alt_chmaps[];

/* bit masks to be passed to snd_pcm_chmap.channel_mask field */
#define SND_PCM_CHMAP_MASK_24	((1U << 2) | (1U << 4))
#define SND_PCM_CHMAP_MASK_246	(SND_PCM_CHMAP_MASK_24 | (1U << 6))
#define SND_PCM_CHMAP_MASK_2468	(SND_PCM_CHMAP_MASK_246 | (1U << 8))

int snd_pcm_add_chmap_ctls(struct snd_pcm *pcm, int stream,
			   const struct snd_pcm_chmap_elem *chmap,
			   int max_channels,
			   unsigned long private_value,
			   struct snd_pcm_chmap **info_ret);

1122 1123 1124 1125 1126 1127
/* Strong-typed conversion of pcm_format to bitwise */
static inline u64 pcm_format_to_bits(snd_pcm_format_t pcm_format)
{
	return 1ULL << (__force int) pcm_format;
}

1128 1129 1130 1131 1132 1133 1134 1135
/* printk helpers */
#define pcm_err(pcm, fmt, args...) \
	dev_err((pcm)->card->dev, fmt, ##args)
#define pcm_warn(pcm, fmt, args...) \
	dev_warn((pcm)->card->dev, fmt, ##args)
#define pcm_dbg(pcm, fmt, args...) \
	dev_dbg((pcm)->card->dev, fmt, ##args)

Linus Torvalds's avatar
Linus Torvalds committed
1136
#endif /* __SOUND_PCM_H */