pcm_lib.c 51.7 KB
Newer Older
Takashi Iwai's avatar
Takashi Iwai committed
1001
1002
1003
1004
 * @var: hw_params variable to apply the list constraint
 * @l: list
 * 
 * Apply the list of constraints to an interval parameter.
Linus Torvalds's avatar
Linus Torvalds committed
1005
 */
1006
int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1007
1008
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
1009
			       struct snd_pcm_hw_constraint_list *l)
Linus Torvalds's avatar
Linus Torvalds committed
1010
1011
1012
1013
1014
1015
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
				   snd_pcm_hw_rule_list, l,
				   var, -1);
}

1016
1017
EXPORT_SYMBOL(snd_pcm_hw_constraint_list);

1018
1019
static int snd_pcm_hw_rule_ratnums(struct snd_pcm_hw_params *params,
				   struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1020
{
1021
	struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds's avatar
Linus Torvalds committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
	unsigned int num = 0, den = 0;
	int err;
	err = snd_interval_ratnum(hw_param_interval(params, rule->var),
				  r->nrats, r->rats, &num, &den);
	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
		params->rate_num = num;
		params->rate_den = den;
	}
	return err;
}

/**
 * snd_pcm_hw_constraint_ratnums
Takashi Iwai's avatar
Takashi Iwai committed
1035
1036
1037
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the ratnums constraint
1038
 * @r: struct snd_ratnums constriants
Linus Torvalds's avatar
Linus Torvalds committed
1039
 */
1040
int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1041
1042
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
1043
				  struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds's avatar
Linus Torvalds committed
1044
1045
1046
1047
1048
1049
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
				   snd_pcm_hw_rule_ratnums, r,
				   var, -1);
}

1050
1051
EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);

1052
1053
static int snd_pcm_hw_rule_ratdens(struct snd_pcm_hw_params *params,
				   struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1054
{
1055
	struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds's avatar
Linus Torvalds committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
	unsigned int num = 0, den = 0;
	int err = snd_interval_ratden(hw_param_interval(params, rule->var),
				  r->nrats, r->rats, &num, &den);
	if (err >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) {
		params->rate_num = num;
		params->rate_den = den;
	}
	return err;
}

/**
 * snd_pcm_hw_constraint_ratdens
Takashi Iwai's avatar
Takashi Iwai committed
1068
1069
1070
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the ratdens constraint
1071
 * @r: struct snd_ratdens constriants
Linus Torvalds's avatar
Linus Torvalds committed
1072
 */
1073
int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1074
1075
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
1076
				  struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds's avatar
Linus Torvalds committed
1077
1078
1079
1080
1081
1082
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
				   snd_pcm_hw_rule_ratdens, r,
				   var, -1);
}

1083
1084
EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);

1085
1086
static int snd_pcm_hw_rule_msbits(struct snd_pcm_hw_params *params,
				  struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1087
1088
1089
1090
{
	unsigned int l = (unsigned long) rule->private;
	int width = l & 0xffff;
	unsigned int msbits = l >> 16;
1091
	struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds's avatar
Linus Torvalds committed
1092
1093
1094
1095
1096
1097
1098
	if (snd_interval_single(i) && snd_interval_value(i) == width)
		params->msbits = msbits;
	return 0;
}

/**
 * snd_pcm_hw_constraint_msbits
Takashi Iwai's avatar
Takashi Iwai committed
1099
1100
1101
1102
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @width: sample bits width
 * @msbits: msbits width
Linus Torvalds's avatar
Linus Torvalds committed
1103
 */
1104
int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
				 unsigned int cond,
				 unsigned int width,
				 unsigned int msbits)
{
	unsigned long l = (msbits << 16) | width;
	return snd_pcm_hw_rule_add(runtime, cond, -1,
				    snd_pcm_hw_rule_msbits,
				    (void*) l,
				    SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
}

1116
1117
EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);

1118
1119
static int snd_pcm_hw_rule_step(struct snd_pcm_hw_params *params,
				struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1120
1121
1122
1123
1124
1125
1126
{
	unsigned long step = (unsigned long) rule->private;
	return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
}

/**
 * snd_pcm_hw_constraint_step
Takashi Iwai's avatar
Takashi Iwai committed
1127
1128
1129
1130
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the step constraint
 * @step: step size
Linus Torvalds's avatar
Linus Torvalds committed
1131
 */
1132
int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1133
1134
1135
1136
1137
1138
1139
1140
1141
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
			       unsigned long step)
{
	return snd_pcm_hw_rule_add(runtime, cond, var, 
				   snd_pcm_hw_rule_step, (void *) step,
				   var, -1);
}

1142
1143
EXPORT_SYMBOL(snd_pcm_hw_constraint_step);

1144
static int snd_pcm_hw_rule_pow2(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1145
{
1146
	static unsigned int pow2_sizes[] = {
Linus Torvalds's avatar
Linus Torvalds committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
		1<<0, 1<<1, 1<<2, 1<<3, 1<<4, 1<<5, 1<<6, 1<<7,
		1<<8, 1<<9, 1<<10, 1<<11, 1<<12, 1<<13, 1<<14, 1<<15,
		1<<16, 1<<17, 1<<18, 1<<19, 1<<20, 1<<21, 1<<22, 1<<23,
		1<<24, 1<<25, 1<<26, 1<<27, 1<<28, 1<<29, 1<<30
	};
	return snd_interval_list(hw_param_interval(params, rule->var),
				 ARRAY_SIZE(pow2_sizes), pow2_sizes, 0);
}		

/**
 * snd_pcm_hw_constraint_pow2
Takashi Iwai's avatar
Takashi Iwai committed
1158
1159
1160
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the power-of-2 constraint
Linus Torvalds's avatar
Linus Torvalds committed
1161
 */
1162
int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1163
1164
1165
1166
1167
1168
1169
1170
			       unsigned int cond,
			       snd_pcm_hw_param_t var)
{
	return snd_pcm_hw_rule_add(runtime, cond, var, 
				   snd_pcm_hw_rule_pow2, NULL,
				   var, -1);
}

1171
1172
EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);

1173
static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1174
				  snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
{
	if (hw_is_mask(var)) {
		snd_mask_any(hw_param_mask(params, var));
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
		return;
	}
	if (hw_is_interval(var)) {
		snd_interval_any(hw_param_interval(params, var));
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
		return;
	}
	snd_BUG();
}

1191
void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
{
	unsigned int k;
	memset(params, 0, sizeof(*params));
	for (k = SNDRV_PCM_HW_PARAM_FIRST_MASK; k <= SNDRV_PCM_HW_PARAM_LAST_MASK; k++)
		_snd_pcm_hw_param_any(params, k);
	for (k = SNDRV_PCM_HW_PARAM_FIRST_INTERVAL; k <= SNDRV_PCM_HW_PARAM_LAST_INTERVAL; k++)
		_snd_pcm_hw_param_any(params, k);
	params->info = ~0U;
}

1202
EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds's avatar
Linus Torvalds committed
1203
1204
1205

/**
 * snd_pcm_hw_param_value
Takashi Iwai's avatar
Takashi Iwai committed
1206
1207
1208
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds's avatar
Linus Torvalds committed
1209
1210
1211
1212
 *
 * Return the value for field PAR if it's fixed in configuration space 
 *  defined by PARAMS. Return -EINVAL otherwise
 */
1213
1214
int snd_pcm_hw_param_value(const struct snd_pcm_hw_params *params,
			   snd_pcm_hw_param_t var, int *dir)
Linus Torvalds's avatar
Linus Torvalds committed
1215
1216
{
	if (hw_is_mask(var)) {
1217
		const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds's avatar
Linus Torvalds committed
1218
1219
1220
1221
1222
1223
1224
		if (!snd_mask_single(mask))
			return -EINVAL;
		if (dir)
			*dir = 0;
		return snd_mask_value(mask);
	}
	if (hw_is_interval(var)) {
1225
		const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds's avatar
Linus Torvalds committed
1226
1227
1228
1229
1230
1231
1232
1233
1234
		if (!snd_interval_single(i))
			return -EINVAL;
		if (dir)
			*dir = i->openmin;
		return snd_interval_value(i);
	}
	return -EINVAL;
}

1235
EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds's avatar
Linus Torvalds committed
1236

1237
void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
				snd_pcm_hw_param_t var)
{
	if (hw_is_mask(var)) {
		snd_mask_none(hw_param_mask(params, var));
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	} else if (hw_is_interval(var)) {
		snd_interval_none(hw_param_interval(params, var));
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	} else {
		snd_BUG();
	}
}

1253
EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds's avatar
Linus Torvalds committed
1254

1255
static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1256
				   snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1257
1258
1259
1260
1261
1262
{
	int changed;
	if (hw_is_mask(var))
		changed = snd_mask_refine_first(hw_param_mask(params, var));
	else if (hw_is_interval(var))
		changed = snd_interval_refine_first(hw_param_interval(params, var));
1263
	else
Linus Torvalds's avatar
Linus Torvalds committed
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}


/**
 * snd_pcm_hw_param_first
Takashi Iwai's avatar
Takashi Iwai committed
1275
1276
1277
1278
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds's avatar
Linus Torvalds committed
1279
1280
1281
1282
1283
 *
 * Inside configuration space defined by PARAMS remove from PAR all 
 * values > minimum. Reduce configuration space accordingly.
 * Return the minimum.
 */
1284
1285
1286
int snd_pcm_hw_param_first(struct snd_pcm_substream *pcm, 
			   struct snd_pcm_hw_params *params, 
			   snd_pcm_hw_param_t var, int *dir)
Linus Torvalds's avatar
Linus Torvalds committed
1287
1288
1289
1290
1291
1292
{
	int changed = _snd_pcm_hw_param_first(params, var);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
1293
1294
		if (snd_BUG_ON(err < 0))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1295
1296
1297
1298
	}
	return snd_pcm_hw_param_value(params, var, dir);
}

1299
1300
EXPORT_SYMBOL(snd_pcm_hw_param_first);

1301
static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1302
				  snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1303
1304
1305
1306
1307
1308
{
	int changed;
	if (hw_is_mask(var))
		changed = snd_mask_refine_last(hw_param_mask(params, var));
	else if (hw_is_interval(var))
		changed = snd_interval_refine_last(hw_param_interval(params, var));
1309
	else
Linus Torvalds's avatar
Linus Torvalds committed
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}


/**
 * snd_pcm_hw_param_last
Takashi Iwai's avatar
Takashi Iwai committed
1321
1322
1323
1324
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
 * @dir: pointer to the direction (-1,0,1) or NULL
Linus Torvalds's avatar
Linus Torvalds committed
1325
1326
1327
1328
1329
 *
 * Inside configuration space defined by PARAMS remove from PAR all 
 * values < maximum. Reduce configuration space accordingly.
 * Return the maximum.
 */
1330
1331
1332
int snd_pcm_hw_param_last(struct snd_pcm_substream *pcm, 
			  struct snd_pcm_hw_params *params,
			  snd_pcm_hw_param_t var, int *dir)
Linus Torvalds's avatar
Linus Torvalds committed
1333
1334
1335
1336
1337
1338
{
	int changed = _snd_pcm_hw_param_last(params, var);
	if (changed < 0)
		return changed;
	if (params->rmask) {
		int err = snd_pcm_hw_refine(pcm, params);
1339
1340
		if (snd_BUG_ON(err < 0))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1341
1342
1343
1344
	}
	return snd_pcm_hw_param_value(params, var, dir);
}

1345
EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds's avatar
Linus Torvalds committed
1346
1347
1348

/**
 * snd_pcm_hw_param_choose
Takashi Iwai's avatar
Takashi Iwai committed
1349
1350
 * @pcm: PCM instance
 * @params: the hw_params instance
Linus Torvalds's avatar
Linus Torvalds committed
1351
1352
1353
1354
1355
1356
 *
 * Choose one configuration from configuration space defined by PARAMS
 * The configuration chosen is that obtained fixing in this order:
 * first access, first format, first subformat, min channels,
 * min rate, min period time, max buffer size, min tick time
 */
1357
1358
int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
			     struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
1359
{
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
	static int vars[] = {
		SNDRV_PCM_HW_PARAM_ACCESS,
		SNDRV_PCM_HW_PARAM_FORMAT,
		SNDRV_PCM_HW_PARAM_SUBFORMAT,
		SNDRV_PCM_HW_PARAM_CHANNELS,
		SNDRV_PCM_HW_PARAM_RATE,
		SNDRV_PCM_HW_PARAM_PERIOD_TIME,
		SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
		SNDRV_PCM_HW_PARAM_TICK_TIME,
		-1
	};
	int err, *v;
Linus Torvalds's avatar
Linus Torvalds committed
1372

1373
1374
1375
1376
1377
	for (v = vars; *v != -1; v++) {
		if (*v != SNDRV_PCM_HW_PARAM_BUFFER_SIZE)
			err = snd_pcm_hw_param_first(pcm, params, *v, NULL);
		else
			err = snd_pcm_hw_param_last(pcm, params, *v, NULL);
1378
1379
		if (snd_BUG_ON(err < 0))
			return err;
1380
	}
Linus Torvalds's avatar
Linus Torvalds committed
1381
1382
1383
	return 0;
}

1384
static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1385
1386
				   void *arg)
{
1387
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
	unsigned long flags;
	snd_pcm_stream_lock_irqsave(substream, flags);
	if (snd_pcm_running(substream) &&
	    snd_pcm_update_hw_ptr(substream) >= 0)
		runtime->status->hw_ptr %= runtime->buffer_size;
	else
		runtime->status->hw_ptr = 0;
	snd_pcm_stream_unlock_irqrestore(substream, flags);
	return 0;
}

1399
static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1400
1401
					  void *arg)
{
1402
1403
	struct snd_pcm_channel_info *info = arg;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
	int width;
	if (!(runtime->info & SNDRV_PCM_INFO_MMAP)) {
		info->offset = -1;
		return 0;
	}
	width = snd_pcm_format_physical_width(runtime->format);
	if (width < 0)
		return width;
	info->offset = 0;
	switch (runtime->access) {
	case SNDRV_PCM_ACCESS_MMAP_INTERLEAVED:
	case SNDRV_PCM_ACCESS_RW_INTERLEAVED:
		info->first = info->channel * width;
		info->step = runtime->channels * width;
		break;
	case SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED:
	case SNDRV_PCM_ACCESS_RW_NONINTERLEAVED:
	{
		size_t size = runtime->dma_bytes / runtime->channels;
		info->first = info->channel * size * 8;
		info->step = width;
		break;
	}
	default:
		snd_BUG();
		break;
	}
	return 0;
}

/**
 * snd_pcm_lib_ioctl - a generic PCM ioctl callback
 * @substream: the pcm substream instance
 * @cmd: ioctl command
 * @arg: ioctl argument
 *
 * Processes the generic ioctl commands for PCM.
 * Can be passed as the ioctl callback for PCM ops.
 *
 * Returns zero if successful, or a negative error code on failure.
 */
1445
int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
		      unsigned int cmd, void *arg)
{
	switch (cmd) {
	case SNDRV_PCM_IOCTL1_INFO:
		return 0;
	case SNDRV_PCM_IOCTL1_RESET:
		return snd_pcm_lib_ioctl_reset(substream, arg);
	case SNDRV_PCM_IOCTL1_CHANNEL_INFO:
		return snd_pcm_lib_ioctl_channel_info(substream, arg);
	}
	return -ENXIO;
}

1459
1460
EXPORT_SYMBOL(snd_pcm_lib_ioctl);

Linus Torvalds's avatar
Linus Torvalds committed
1461
1462
1463
1464
1465
1466
/**
 * snd_pcm_period_elapsed - update the pcm status for the next period
 * @substream: the pcm substream instance
 *
 * This function is called from the interrupt handler when the
 * PCM has processed the period size.  It will update the current
1467
 * pointer, wake up sleepers, etc.
Linus Torvalds's avatar
Linus Torvalds committed
1468
1469
1470
1471
 *
 * Even if more than one periods have elapsed since the last call, you
 * have to call this only once.
 */
1472
void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1473
{
1474
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1475
1476
	unsigned long flags;

1477
1478
	if (PCM_RUNTIME_CHECK(substream))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
	runtime = substream->runtime;

	if (runtime->transfer_ack_begin)
		runtime->transfer_ack_begin(substream);

	snd_pcm_stream_lock_irqsave(substream, flags);
	if (!snd_pcm_running(substream) ||
	    snd_pcm_update_hw_ptr_interrupt(substream) < 0)
		goto _end;

	if (substream->timer_running)
		snd_timer_interrupt(substream->timer, 1);
 _end:
	snd_pcm_stream_unlock_irqrestore(substream, flags);
	if (runtime->transfer_ack_end)
		runtime->transfer_ack_end(substream);
	kill_fasync(&runtime->fasync, SIGIO, POLL_IN);
}

1498
1499
EXPORT_SYMBOL(snd_pcm_period_elapsed);

1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
/*
 * Wait until avail_min data becomes available
 * Returns a negative error code if any error occurs during operation.
 * The available space is stored on availp.  When err = 0 and avail = 0
 * on the capture stream, it indicates the stream is in DRAINING state.
 */
static int wait_for_avail_min(struct snd_pcm_substream *substream,
			      snd_pcm_uframes_t *availp)
{
	struct snd_pcm_runtime *runtime = substream->runtime;
	int is_playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
	wait_queue_t wait;
	int err = 0;
	snd_pcm_uframes_t avail = 0;
	long tout;

	init_waitqueue_entry(&wait, current);
	add_wait_queue(&runtime->sleep, &wait);
	for (;;) {
		if (signal_pending(current)) {
			err = -ERESTARTSYS;
			break;
		}
		set_current_state(TASK_INTERRUPTIBLE);
		snd_pcm_stream_unlock_irq(substream);
		tout = schedule_timeout(msecs_to_jiffies(10000));
		snd_pcm_stream_lock_irq(substream);
		switch (runtime->status->state) {
		case SNDRV_PCM_STATE_SUSPENDED:
			err = -ESTRPIPE;
			goto _endloop;
		case SNDRV_PCM_STATE_XRUN:
			err = -EPIPE;
			goto _endloop;
		case SNDRV_PCM_STATE_DRAINING:
			if (is_playback)
				err = -EPIPE;
			else 
				avail = 0; /* indicate draining */
			goto _endloop;
		case SNDRV_PCM_STATE_OPEN:
		case SNDRV_PCM_STATE_SETUP:
		case SNDRV_PCM_STATE_DISCONNECTED:
			err = -EBADFD;
			goto _endloop;
		}
		if (!tout) {
			snd_printd("%s write error (DMA or IRQ trouble?)\n",
				   is_playback ? "playback" : "capture");
			err = -EIO;
			break;
		}
		if (is_playback)
			avail = snd_pcm_playback_avail(runtime);
		else
			avail = snd_pcm_capture_avail(runtime);
		if (avail >= runtime->control->avail_min)
			break;
	}
 _endloop:
	remove_wait_queue(&runtime->sleep, &wait);
	*availp = avail;
	return err;
}
	
1565
static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1566
1567
1568
1569
				      unsigned int hwoff,
				      unsigned long data, unsigned int off,
				      snd_pcm_uframes_t frames)
{
1570
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
	int err;
	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
	if (substream->ops->copy) {
		if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
			return err;
	} else {
		char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
		if (copy_from_user(hwbuf, buf, frames_to_bytes(runtime, frames)))
			return -EFAULT;
	}
	return 0;
}
 
1584
typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds's avatar
Linus Torvalds committed
1585
1586
1587
			  unsigned long data, unsigned int off,
			  snd_pcm_uframes_t size);

1588
static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, 
Linus Torvalds's avatar
Linus Torvalds committed
1589
1590
1591
1592
1593
					    unsigned long data,
					    snd_pcm_uframes_t size,
					    int nonblock,
					    transfer_f transfer)
{
1594
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
	snd_pcm_uframes_t xfer = 0;
	snd_pcm_uframes_t offset = 0;
	int err = 0;

	if (size == 0)
		return 0;

	snd_pcm_stream_lock_irq(substream);
	switch (runtime->status->state) {
	case SNDRV_PCM_STATE_PREPARED:
	case SNDRV_PCM_STATE_RUNNING:
	case SNDRV_PCM_STATE_PAUSED:
		break;
	case SNDRV_PCM_STATE_XRUN:
		err = -EPIPE;
		goto _end_unlock;
	case SNDRV_PCM_STATE_SUSPENDED:
		err = -ESTRPIPE;
		goto _end_unlock;
	default:
		err = -EBADFD;
		goto _end_unlock;
	}

	while (size > 0) {
		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
		snd_pcm_uframes_t avail;
		snd_pcm_uframes_t cont;
1623
		if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds's avatar
Linus Torvalds committed
1624
1625
			snd_pcm_update_hw_ptr(substream);
		avail = snd_pcm_playback_avail(runtime);
1626
		if (!avail) {
Linus Torvalds's avatar
Linus Torvalds committed
1627
1628
1629
1630
			if (nonblock) {
				err = -EAGAIN;
				goto _end_unlock;
			}
1631
1632
			err = wait_for_avail_min(substream, &avail);
			if (err < 0)
1633
				goto _end_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
1634
1635
1636
1637
1638
		}
		frames = size > avail ? avail : size;
		cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
		if (frames > cont)
			frames = cont;
1639
1640
1641
1642
		if (snd_BUG_ON(!frames)) {
			snd_pcm_stream_unlock_irq(substream);
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
		appl_ptr = runtime->control->appl_ptr;
		appl_ofs = appl_ptr % runtime->buffer_size;
		snd_pcm_stream_unlock_irq(substream);
		if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
			goto _end;
		snd_pcm_stream_lock_irq(substream);
		switch (runtime->status->state) {
		case SNDRV_PCM_STATE_XRUN:
			err = -EPIPE;
			goto _end_unlock;
		case SNDRV_PCM_STATE_SUSPENDED:
			err = -ESTRPIPE;
			goto _end_unlock;
		default:
			break;
		}
		appl_ptr += frames;
		if (appl_ptr >= runtime->boundary)
			appl_ptr -= runtime->boundary;
		runtime->control->appl_ptr = appl_ptr;
		if (substream->ops->ack)
			substream->ops->ack(substream);

		offset += frames;
		size -= frames;
		xfer += frames;
		if (runtime->status->state == SNDRV_PCM_STATE_PREPARED &&
		    snd_pcm_playback_hw_avail(runtime) >= (snd_pcm_sframes_t)runtime->start_threshold) {
			err = snd_pcm_start(substream);
			if (err < 0)
				goto _end_unlock;
		}
	}
 _end_unlock:
	snd_pcm_stream_unlock_irq(substream);
 _end:
	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
}

1682
1683
/* sanity-check for read/write methods */
static int pcm_sanity_check(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1684
{
1685
	struct snd_pcm_runtime *runtime;
1686
1687
	if (PCM_RUNTIME_CHECK(substream))
		return -ENXIO;
Linus Torvalds's avatar
Linus Torvalds committed
1688
	runtime = substream->runtime;
1689
1690
	if (snd_BUG_ON(!substream->ops->copy && !runtime->dma_area))
		return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1691
1692
	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
		return -EBADFD;
1693
1694
1695
1696
1697
1698
1699
1700
	return 0;
}

snd_pcm_sframes_t snd_pcm_lib_write(struct snd_pcm_substream *substream, const void __user *buf, snd_pcm_uframes_t size)
{
	struct snd_pcm_runtime *runtime;
	int nonblock;
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1701

1702
1703
1704
1705
	err = pcm_sanity_check(substream);
	if (err < 0)
		return err;
	runtime = substream->runtime;
1706
	nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
1707
1708
1709
1710
1711
1712
1713
1714

	if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED &&
	    runtime->channels > 1)
		return -EINVAL;
	return snd_pcm_lib_write1(substream, (unsigned long)buf, size, nonblock,
				  snd_pcm_lib_write_transfer);
}

1715
1716
EXPORT_SYMBOL(snd_pcm_lib_write);

1717
static int snd_pcm_lib_writev_transfer(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1718
1719
1720
1721
				       unsigned int hwoff,
				       unsigned long data, unsigned int off,
				       snd_pcm_uframes_t frames)
{
1722
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1723
1724
1725
1726
1727
	int err;
	void __user **bufs = (void __user **)data;
	int channels = runtime->channels;
	int c;
	if (substream->ops->copy) {
1728
1729
		if (snd_BUG_ON(!substream->ops->silence))
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
		for (c = 0; c < channels; ++c, ++bufs) {
			if (*bufs == NULL) {
				if ((err = substream->ops->silence(substream, c, hwoff, frames)) < 0)
					return err;
			} else {
				char __user *buf = *bufs + samples_to_bytes(runtime, off);
				if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
					return err;
			}
		}
	} else {
		/* default transfer behaviour */
		size_t dma_csize = runtime->dma_bytes / channels;
		for (c = 0; c < channels; ++c, ++bufs) {
			char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
			if (*bufs == NULL) {
				snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
			} else {
				char __user *buf = *bufs + samples_to_bytes(runtime, off);
				if (copy_from_user(hwbuf, buf, samples_to_bytes(runtime, frames)))
					return -EFAULT;
			}
		}
	}
	return 0;
}
 
1757
snd_pcm_sframes_t snd_pcm_lib_writev(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1758
1759
1760
				     void __user **bufs,
				     snd_pcm_uframes_t frames)
{
1761
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1762
	int nonblock;
1763
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1764

1765
1766
1767
	err = pcm_sanity_check(substream);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
1768
	runtime = substream->runtime;
1769
	nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
1770
1771
1772
1773
1774
1775
1776

	if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
		return -EINVAL;
	return snd_pcm_lib_write1(substream, (unsigned long)bufs, frames,
				  nonblock, snd_pcm_lib_writev_transfer);
}

1777
1778
EXPORT_SYMBOL(snd_pcm_lib_writev);

1779
static int snd_pcm_lib_read_transfer(struct snd_pcm_substream *substream, 
Linus Torvalds's avatar
Linus Torvalds committed
1780
1781
1782
1783
				     unsigned int hwoff,
				     unsigned long data, unsigned int off,
				     snd_pcm_uframes_t frames)
{
1784
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
	int err;
	char __user *buf = (char __user *) data + frames_to_bytes(runtime, off);
	if (substream->ops->copy) {
		if ((err = substream->ops->copy(substream, -1, hwoff, buf, frames)) < 0)
			return err;
	} else {
		char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, hwoff);
		if (copy_to_user(buf, hwbuf, frames_to_bytes(runtime, frames)))
			return -EFAULT;
	}
	return 0;
}

1798
static snd_pcm_sframes_t snd_pcm_lib_read1(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1799
1800
1801
1802
1803
					   unsigned long data,
					   snd_pcm_uframes_t size,
					   int nonblock,
					   transfer_f transfer)
{
1804
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
	snd_pcm_uframes_t xfer = 0;
	snd_pcm_uframes_t offset = 0;
	int err = 0;

	if (size == 0)
		return 0;

	snd_pcm_stream_lock_irq(substream);
	switch (runtime->status->state) {
	case SNDRV_PCM_STATE_PREPARED:
		if (size >= runtime->start_threshold) {
			err = snd_pcm_start(substream);
			if (err < 0)
				goto _end_unlock;
		}
		break;
	case SNDRV_PCM_STATE_DRAINING:
	case SNDRV_PCM_STATE_RUNNING:
	case SNDRV_PCM_STATE_PAUSED:
		break;
	case SNDRV_PCM_STATE_XRUN:
		err = -EPIPE;
		goto _end_unlock;
	case SNDRV_PCM_STATE_SUSPENDED:
		err = -ESTRPIPE;
		goto _end_unlock;
	default:
		err = -EBADFD;
		goto _end_unlock;
	}

	while (size > 0) {
		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
		snd_pcm_uframes_t avail;
		snd_pcm_uframes_t cont;
1840
		if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
Linus Torvalds's avatar
Linus Torvalds committed
1841
1842
			snd_pcm_update_hw_ptr(substream);
		avail = snd_pcm_capture_avail(runtime);
1843
1844
1845
1846
		if (!avail) {
			if (runtime->status->state ==
			    SNDRV_PCM_STATE_DRAINING) {
				snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Linus Torvalds's avatar
Linus Torvalds committed
1847
1848
1849
1850
1851
1852
				goto _end_unlock;
			}
			if (nonblock) {
				err = -EAGAIN;
				goto _end_unlock;
			}
1853
1854
			err = wait_for_avail_min(substream, &avail);
			if (err < 0)
1855
				goto _end_unlock;
1856
1857
			if (!avail)
				continue; /* draining */
Linus Torvalds's avatar
Linus Torvalds committed
1858
1859
1860
1861
1862
		}
		frames = size > avail ? avail : size;
		cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
		if (frames > cont)
			frames = cont;
1863
1864
1865
1866
		if (snd_BUG_ON(!frames)) {
			snd_pcm_stream_unlock_irq(substream);
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
		appl_ptr = runtime->control->appl_ptr;
		appl_ofs = appl_ptr % runtime->buffer_size;
		snd_pcm_stream_unlock_irq(substream);
		if ((err = transfer(substream, appl_ofs, data, offset, frames)) < 0)
			goto _end;
		snd_pcm_stream_lock_irq(substream);
		switch (runtime->status->state) {
		case SNDRV_PCM_STATE_XRUN:
			err = -EPIPE;
			goto _end_unlock;
		case SNDRV_PCM_STATE_SUSPENDED:
			err = -ESTRPIPE;
			goto _end_unlock;
		default:
			break;
		}
		appl_ptr += frames;
		if (appl_ptr >= runtime->boundary)
			appl_ptr -= runtime->boundary;
		runtime->control->appl_ptr = appl_ptr;
		if (substream->ops->ack)
			substream->ops->ack(substream);

		offset += frames;
		size -= frames;
		xfer += frames;
	}
 _end_unlock:
	snd_pcm_stream_unlock_irq(substream);
 _end:
	return xfer > 0 ? (snd_pcm_sframes_t)xfer : err;
}

1900
snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream *substream, void __user *buf, snd_pcm_uframes_t size)
Linus Torvalds's avatar
Linus Torvalds committed
1901
{
1902
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1903
	int nonblock;
1904
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1905
	
1906
1907
1908
	err = pcm_sanity_check(substream);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
1909
	runtime = substream->runtime;
1910
	nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
1911
1912
1913
1914
1915
	if (runtime->access != SNDRV_PCM_ACCESS_RW_INTERLEAVED)
		return -EINVAL;
	return snd_pcm_lib_read1(substream, (unsigned long)buf, size, nonblock, snd_pcm_lib_read_transfer);
}

1916
1917
EXPORT_SYMBOL(snd_pcm_lib_read);

1918
static int snd_pcm_lib_readv_transfer(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1919
1920
1921
1922
				      unsigned int hwoff,
				      unsigned long data, unsigned int off,
				      snd_pcm_uframes_t frames)
{
1923
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
	int err;
	void __user **bufs = (void __user **)data;
	int channels = runtime->channels;
	int c;
	if (substream->ops->copy) {
		for (c = 0; c < channels; ++c, ++bufs) {
			char __user *buf;
			if (*bufs == NULL)
				continue;
			buf = *bufs + samples_to_bytes(runtime, off);
			if ((err = substream->ops->copy(substream, c, hwoff, buf, frames)) < 0)
				return err;
		}
	} else {
		snd_pcm_uframes_t dma_csize = runtime->dma_bytes / channels;
		for (c = 0; c < channels; ++c, ++bufs) {
			char *hwbuf;
			char __user *buf;
			if (*bufs == NULL)
				continue;

			hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, hwoff);
			buf = *bufs + samples_to_bytes(runtime, off);
			if (copy_to_user(buf, hwbuf, samples_to_bytes(runtime, frames)))
				return -EFAULT;
		}
	}
	return 0;
}
 
1954
snd_pcm_sframes_t snd_pcm_lib_readv(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1955
1956
1957
				    void __user **bufs,
				    snd_pcm_uframes_t frames)
{
1958
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1959
	int nonblock;
1960
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
1961

1962
1963
1964
	err = pcm_sanity_check(substream);
	if (err < 0)
		return err;
Linus Torvalds's avatar
Linus Torvalds committed
1965
1966
1967
1968
	runtime = substream->runtime;
	if (runtime->status->state == SNDRV_PCM_STATE_OPEN)
		return -EBADFD;

1969
	nonblock = !!(substream->f_flags & O_NONBLOCK);
Linus Torvalds's avatar
Linus Torvalds committed
1970
1971
1972
1973
1974
1975
	if (runtime->access != SNDRV_PCM_ACCESS_RW_NONINTERLEAVED)
		return -EINVAL;
	return snd_pcm_lib_read1(substream, (unsigned long)bufs, frames, nonblock, snd_pcm_lib_readv_transfer);
}

EXPORT_SYMBOL(snd_pcm_lib_readv);
For faster browsing, not all history is shown. View entire blame