pcm_lib.c 67.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
			r = (num - rats[k].num_min) % rats[k].num_step;
			if (r != 0)
				num -= r;
		}
		diff = q * den - num;
		if (best_num == 0 ||
		    diff * best_den < best_diff * den) {
			best_diff = diff;
			best_den = den;
			best_num = num;
		}
	}
	if (best_den == 0) {
		i->empty = 1;
		return -EINVAL;
	}
	t.max = div_up(best_num, best_den);
	t.openmax = !!(best_num % best_den);
	t.integer = 0;
	err = snd_interval_refine(i, &t);
	if (err < 0)
		return err;

	if (snd_interval_single(i)) {
		if (nump)
			*nump = best_num;
		if (denp)
			*denp = best_den;
	}
	return err;
}

/**
 * snd_interval_list - refine the interval value from the list
 * @i: the interval value to refine
 * @count: the number of elements in the list
 * @list: the value list
 * @mask: the bit-mask to evaluate
 *
 * Refines the interval value from the list.
 * When mask is non-zero, only the elements corresponding to bit 1 are
 * evaluated.
 *
 * Returns non-zero if the value is changed, zero if not changed.
 */
1046
1047
int snd_interval_list(struct snd_interval *i, unsigned int count,
		      const unsigned int *list, unsigned int mask)
Linus Torvalds's avatar
Linus Torvalds committed
1048
1049
{
        unsigned int k;
1050
	struct snd_interval list_range;
1051
1052
1053
1054
1055

	if (!count) {
		i->empty = 1;
		return -EINVAL;
	}
1056
1057
1058
	snd_interval_any(&list_range);
	list_range.min = UINT_MAX;
	list_range.max = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1059
1060
1061
        for (k = 0; k < count; k++) {
		if (mask && !(mask & (1 << k)))
			continue;
1062
		if (!snd_interval_test(i, list[k]))
Linus Torvalds's avatar
Linus Torvalds committed
1063
			continue;
1064
1065
		list_range.min = min(list_range.min, list[k]);
		list_range.max = max(list_range.max, list[k]);
Linus Torvalds's avatar
Linus Torvalds committed
1066
        }
1067
	return snd_interval_refine(i, &list_range);
Linus Torvalds's avatar
Linus Torvalds committed
1068
1069
}

1070
1071
EXPORT_SYMBOL(snd_interval_list);

1072
static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned int step)
Linus Torvalds's avatar
Linus Torvalds committed
1073
1074
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
{
	unsigned int n;
	int changed = 0;
	n = (i->min - min) % step;
	if (n != 0 || i->openmin) {
		i->min += step - n;
		changed = 1;
	}
	n = (i->max - min) % step;
	if (n != 0 || i->openmax) {
		i->max -= n;
		changed = 1;
	}
	if (snd_interval_checkempty(i)) {
		i->empty = 1;
		return -EINVAL;
	}
	return changed;
}

/* Info constraints helpers */

/**
 * snd_pcm_hw_rule_add - add the hw-constraint rule
 * @runtime: the pcm runtime instance
 * @cond: condition bits
 * @var: the variable to evaluate
 * @func: the evaluation function
 * @private: the private data pointer passed to function
 * @dep: the dependent variables
 *
 * Returns zero if successful, or a negative error code on failure.
 */
1106
int snd_pcm_hw_rule_add(struct snd_pcm_runtime *runtime, unsigned int cond,
Linus Torvalds's avatar
Linus Torvalds committed
1107
1108
1109
1110
			int var,
			snd_pcm_hw_rule_func_t func, void *private,
			int dep, ...)
{
1111
1112
	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
	struct snd_pcm_hw_rule *c;
Linus Torvalds's avatar
Linus Torvalds committed
1113
1114
1115
1116
	unsigned int k;
	va_list args;
	va_start(args, dep);
	if (constrs->rules_num >= constrs->rules_all) {
1117
		struct snd_pcm_hw_rule *new;
Linus Torvalds's avatar
Linus Torvalds committed
1118
1119
		unsigned int new_rules = constrs->rules_all + 16;
		new = kcalloc(new_rules, sizeof(*c), GFP_KERNEL);
1120
1121
		if (!new) {
			va_end(args);
Linus Torvalds's avatar
Linus Torvalds committed
1122
			return -ENOMEM;
1123
		}
Linus Torvalds's avatar
Linus Torvalds committed
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
		if (constrs->rules) {
			memcpy(new, constrs->rules,
			       constrs->rules_num * sizeof(*c));
			kfree(constrs->rules);
		}
		constrs->rules = new;
		constrs->rules_all = new_rules;
	}
	c = &constrs->rules[constrs->rules_num];
	c->cond = cond;
	c->func = func;
	c->var = var;
	c->private = private;
	k = 0;
	while (1) {
1139
1140
		if (snd_BUG_ON(k >= ARRAY_SIZE(c->deps))) {
			va_end(args);
1141
			return -EINVAL;
1142
		}
Linus Torvalds's avatar
Linus Torvalds committed
1143
1144
1145
1146
1147
1148
1149
1150
		c->deps[k++] = dep;
		if (dep < 0)
			break;
		dep = va_arg(args, int);
	}
	constrs->rules_num++;
	va_end(args);
	return 0;
1151
}
Linus Torvalds's avatar
Linus Torvalds committed
1152

1153
1154
EXPORT_SYMBOL(snd_pcm_hw_rule_add);

Linus Torvalds's avatar
Linus Torvalds committed
1155
/**
1156
 * snd_pcm_hw_constraint_mask - apply the given bitmap mask constraint
Takashi Iwai's avatar
Takashi Iwai committed
1157
1158
1159
1160
 * @runtime: PCM runtime instance
 * @var: hw_params variable to apply the mask
 * @mask: the bitmap mask
 *
1161
 * Apply the constraint of the given bitmap mask to a 32-bit mask parameter.
Linus Torvalds's avatar
Linus Torvalds committed
1162
 */
1163
int snd_pcm_hw_constraint_mask(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
1164
1165
			       u_int32_t mask)
{
1166
1167
	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
	struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds's avatar
Linus Torvalds committed
1168
1169
1170
1171
1172
1173
1174
1175
	*maskp->bits &= mask;
	memset(maskp->bits + 1, 0, (SNDRV_MASK_MAX-32) / 8); /* clear rest */
	if (*maskp->bits == 0)
		return -EINVAL;
	return 0;
}

/**
1176
 * snd_pcm_hw_constraint_mask64 - apply the given bitmap mask constraint
Takashi Iwai's avatar
Takashi Iwai committed
1177
1178
1179
1180
 * @runtime: PCM runtime instance
 * @var: hw_params variable to apply the mask
 * @mask: the 64bit bitmap mask
 *
1181
 * Apply the constraint of the given bitmap mask to a 64-bit mask parameter.
Linus Torvalds's avatar
Linus Torvalds committed
1182
 */
1183
int snd_pcm_hw_constraint_mask64(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
1184
1185
				 u_int64_t mask)
{
1186
1187
	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
	struct snd_mask *maskp = constrs_mask(constrs, var);
Linus Torvalds's avatar
Linus Torvalds committed
1188
1189
1190
1191
1192
1193
1194
1195
1196
	maskp->bits[0] &= (u_int32_t)mask;
	maskp->bits[1] &= (u_int32_t)(mask >> 32);
	memset(maskp->bits + 2, 0, (SNDRV_MASK_MAX-64) / 8); /* clear rest */
	if (! maskp->bits[0] && ! maskp->bits[1])
		return -EINVAL;
	return 0;
}

/**
1197
 * snd_pcm_hw_constraint_integer - apply an integer constraint to an interval
Takashi Iwai's avatar
Takashi Iwai committed
1198
1199
1200
1201
 * @runtime: PCM runtime instance
 * @var: hw_params variable to apply the integer constraint
 *
 * Apply the constraint of integer to an interval parameter.
Linus Torvalds's avatar
Linus Torvalds committed
1202
 */
1203
int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1204
{
1205
	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
Linus Torvalds's avatar
Linus Torvalds committed
1206
1207
1208
	return snd_interval_setinteger(constrs_interval(constrs, var));
}

1209
1210
EXPORT_SYMBOL(snd_pcm_hw_constraint_integer);

Linus Torvalds's avatar
Linus Torvalds committed
1211
/**
1212
 * snd_pcm_hw_constraint_minmax - apply a min/max range constraint to an interval
Takashi Iwai's avatar
Takashi Iwai committed
1213
1214
1215
1216
1217
1218
 * @runtime: PCM runtime instance
 * @var: hw_params variable to apply the range
 * @min: the minimal value
 * @max: the maximal value
 * 
 * Apply the min/max range constraint to an interval parameter.
Linus Torvalds's avatar
Linus Torvalds committed
1219
 */
1220
int snd_pcm_hw_constraint_minmax(struct snd_pcm_runtime *runtime, snd_pcm_hw_param_t var,
Linus Torvalds's avatar
Linus Torvalds committed
1221
1222
				 unsigned int min, unsigned int max)
{
1223
1224
	struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
	struct snd_interval t;
Linus Torvalds's avatar
Linus Torvalds committed
1225
1226
1227
1228
1229
1230
1231
	t.min = min;
	t.max = max;
	t.openmin = t.openmax = 0;
	t.integer = 0;
	return snd_interval_refine(constrs_interval(constrs, var), &t);
}

1232
1233
EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);

1234
1235
static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,
				struct snd_pcm_hw_rule *rule)
Linus Torvalds's avatar
Linus Torvalds committed
1236
{
1237
	struct snd_pcm_hw_constraint_list *list = rule->private;
Linus Torvalds's avatar
Linus Torvalds committed
1238
1239
1240
1241
1242
	return snd_interval_list(hw_param_interval(params, rule->var), list->count, list->list, list->mask);
}		


/**
1243
 * snd_pcm_hw_constraint_list - apply a list of constraints to a parameter
Takashi Iwai's avatar
Takashi Iwai committed
1244
1245
1246
1247
1248
1249
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @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
1250
 */
1251
int snd_pcm_hw_constraint_list(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1252
1253
			       unsigned int cond,
			       snd_pcm_hw_param_t var,
1254
			       const struct snd_pcm_hw_constraint_list *l)
Linus Torvalds's avatar
Linus Torvalds committed
1255
1256
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
1257
				   snd_pcm_hw_rule_list, (void *)l,
Linus Torvalds's avatar
Linus Torvalds committed
1258
1259
1260
				   var, -1);
}

1261
1262
EXPORT_SYMBOL(snd_pcm_hw_constraint_list);

1263
1264
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
1265
{
1266
	struct snd_pcm_hw_constraint_ratnums *r = rule->private;
Linus Torvalds's avatar
Linus Torvalds committed
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
	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;
}

/**
1279
 * snd_pcm_hw_constraint_ratnums - apply ratnums constraint to a parameter
Takashi Iwai's avatar
Takashi Iwai committed
1280
1281
1282
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the ratnums constraint
1283
 * @r: struct snd_ratnums constriants
Linus Torvalds's avatar
Linus Torvalds committed
1284
 */
1285
int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1286
1287
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
1288
				  struct snd_pcm_hw_constraint_ratnums *r)
Linus Torvalds's avatar
Linus Torvalds committed
1289
1290
1291
1292
1293
1294
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
				   snd_pcm_hw_rule_ratnums, r,
				   var, -1);
}

1295
1296
EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);

1297
1298
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
1299
{
1300
	struct snd_pcm_hw_constraint_ratdens *r = rule->private;
Linus Torvalds's avatar
Linus Torvalds committed
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
	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;
}

/**
1312
 * snd_pcm_hw_constraint_ratdens - apply ratdens constraint to a parameter
Takashi Iwai's avatar
Takashi Iwai committed
1313
1314
1315
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @var: hw_params variable to apply the ratdens constraint
1316
 * @r: struct snd_ratdens constriants
Linus Torvalds's avatar
Linus Torvalds committed
1317
 */
1318
int snd_pcm_hw_constraint_ratdens(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1319
1320
				  unsigned int cond,
				  snd_pcm_hw_param_t var,
1321
				  struct snd_pcm_hw_constraint_ratdens *r)
Linus Torvalds's avatar
Linus Torvalds committed
1322
1323
1324
1325
1326
1327
{
	return snd_pcm_hw_rule_add(runtime, cond, var,
				   snd_pcm_hw_rule_ratdens, r,
				   var, -1);
}

1328
1329
EXPORT_SYMBOL(snd_pcm_hw_constraint_ratdens);

1330
1331
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
1332
1333
1334
1335
{
	unsigned int l = (unsigned long) rule->private;
	int width = l & 0xffff;
	unsigned int msbits = l >> 16;
1336
	struct snd_interval *i = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS);
Linus Torvalds's avatar
Linus Torvalds committed
1337
1338
1339
1340
1341
1342
	if (snd_interval_single(i) && snd_interval_value(i) == width)
		params->msbits = msbits;
	return 0;
}

/**
1343
 * snd_pcm_hw_constraint_msbits - add a hw constraint msbits rule
Takashi Iwai's avatar
Takashi Iwai committed
1344
1345
1346
1347
 * @runtime: PCM runtime instance
 * @cond: condition bits
 * @width: sample bits width
 * @msbits: msbits width
Linus Torvalds's avatar
Linus Torvalds committed
1348
 */
1349
int snd_pcm_hw_constraint_msbits(struct snd_pcm_runtime *runtime, 
Linus Torvalds's avatar
Linus Torvalds committed
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
				 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);
}

1361
1362
EXPORT_SYMBOL(snd_pcm_hw_constraint_msbits);

1363
1364
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
1365
1366
1367
1368
1369
1370
{
	unsigned long step = (unsigned long) rule->private;
	return snd_interval_step(hw_param_interval(params, rule->var), 0, step);
}

/**
1371
 * snd_pcm_hw_constraint_step - add a hw constraint step rule
Takashi Iwai's avatar
Takashi Iwai committed
1372
1373
1374
1375
 * @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
1376
 */
1377
int snd_pcm_hw_constraint_step(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
			       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);
}

1387
1388
EXPORT_SYMBOL(snd_pcm_hw_constraint_step);

1389
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
1390
{
1391
	static unsigned int pow2_sizes[] = {
Linus Torvalds's avatar
Linus Torvalds committed
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
		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);
}		

/**
1402
 * snd_pcm_hw_constraint_pow2 - add a hw constraint power-of-2 rule
Takashi Iwai's avatar
Takashi Iwai committed
1403
1404
1405
 * @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
1406
 */
1407
int snd_pcm_hw_constraint_pow2(struct snd_pcm_runtime *runtime,
Linus Torvalds's avatar
Linus Torvalds committed
1408
1409
1410
1411
1412
1413
1414
1415
			       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);
}

1416
1417
EXPORT_SYMBOL(snd_pcm_hw_constraint_pow2);

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
static int snd_pcm_hw_rule_noresample_func(struct snd_pcm_hw_params *params,
					   struct snd_pcm_hw_rule *rule)
{
	unsigned int base_rate = (unsigned int)(uintptr_t)rule->private;
	struct snd_interval *rate;

	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
	return snd_interval_list(rate, 1, &base_rate, 0);
}

/**
 * snd_pcm_hw_rule_noresample - add a rule to allow disabling hw resampling
 * @runtime: PCM runtime instance
 * @base_rate: the rate at which the hardware does not resample
 */
int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,
			       unsigned int base_rate)
{
	return snd_pcm_hw_rule_add(runtime, SNDRV_PCM_HW_PARAMS_NORESAMPLE,
				   SNDRV_PCM_HW_PARAM_RATE,
				   snd_pcm_hw_rule_noresample_func,
				   (void *)(uintptr_t)base_rate,
				   SNDRV_PCM_HW_PARAM_RATE, -1);
}
EXPORT_SYMBOL(snd_pcm_hw_rule_noresample);

1444
static void _snd_pcm_hw_param_any(struct snd_pcm_hw_params *params,
1445
				  snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
{
	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();
}

1462
void _snd_pcm_hw_params_any(struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
{
	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;
}

1473
EXPORT_SYMBOL(_snd_pcm_hw_params_any);
Linus Torvalds's avatar
Linus Torvalds committed
1474
1475

/**
1476
 * snd_pcm_hw_param_value - return @params field @var value
Takashi Iwai's avatar
Takashi Iwai committed
1477
1478
 * @params: the hw_params instance
 * @var: parameter to retrieve
1479
 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds's avatar
Linus Torvalds committed
1480
 *
1481
1482
 * Return the value for field @var if it's fixed in configuration space
 * defined by @params. Return -%EINVAL otherwise.
Linus Torvalds's avatar
Linus Torvalds committed
1483
 */
1484
1485
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
1486
1487
{
	if (hw_is_mask(var)) {
1488
		const struct snd_mask *mask = hw_param_mask_c(params, var);
Linus Torvalds's avatar
Linus Torvalds committed
1489
1490
1491
1492
1493
1494
1495
		if (!snd_mask_single(mask))
			return -EINVAL;
		if (dir)
			*dir = 0;
		return snd_mask_value(mask);
	}
	if (hw_is_interval(var)) {
1496
		const struct snd_interval *i = hw_param_interval_c(params, var);
Linus Torvalds's avatar
Linus Torvalds committed
1497
1498
1499
1500
1501
1502
1503
1504
1505
		if (!snd_interval_single(i))
			return -EINVAL;
		if (dir)
			*dir = i->openmin;
		return snd_interval_value(i);
	}
	return -EINVAL;
}

1506
EXPORT_SYMBOL(snd_pcm_hw_param_value);
Linus Torvalds's avatar
Linus Torvalds committed
1507

1508
void _snd_pcm_hw_param_setempty(struct snd_pcm_hw_params *params,
Linus Torvalds's avatar
Linus Torvalds committed
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
				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();
	}
}

1524
EXPORT_SYMBOL(_snd_pcm_hw_param_setempty);
Linus Torvalds's avatar
Linus Torvalds committed
1525

1526
static int _snd_pcm_hw_param_first(struct snd_pcm_hw_params *params,
1527
				   snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1528
1529
1530
1531
1532
1533
{
	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));
1534
	else
Linus Torvalds's avatar
Linus Torvalds committed
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}


/**
1545
 * snd_pcm_hw_param_first - refine config space and return minimum value
Takashi Iwai's avatar
Takashi Iwai committed
1546
1547
1548
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
1549
 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds's avatar
Linus Torvalds committed
1550
 *
1551
 * Inside configuration space defined by @params remove from @var all
Linus Torvalds's avatar
Linus Torvalds committed
1552
1553
1554
 * values > minimum. Reduce configuration space accordingly.
 * Return the minimum.
 */
1555
1556
1557
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
1558
1559
1560
1561
1562
1563
{
	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);
1564
1565
		if (snd_BUG_ON(err < 0))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1566
1567
1568
1569
	}
	return snd_pcm_hw_param_value(params, var, dir);
}

1570
1571
EXPORT_SYMBOL(snd_pcm_hw_param_first);

1572
static int _snd_pcm_hw_param_last(struct snd_pcm_hw_params *params,
1573
				  snd_pcm_hw_param_t var)
Linus Torvalds's avatar
Linus Torvalds committed
1574
1575
1576
1577
1578
1579
{
	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));
1580
	else
Linus Torvalds's avatar
Linus Torvalds committed
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
		return -EINVAL;
	if (changed) {
		params->cmask |= 1 << var;
		params->rmask |= 1 << var;
	}
	return changed;
}


/**
1591
 * snd_pcm_hw_param_last - refine config space and return maximum value
Takashi Iwai's avatar
Takashi Iwai committed
1592
1593
1594
 * @pcm: PCM instance
 * @params: the hw_params instance
 * @var: parameter to retrieve
1595
 * @dir: pointer to the direction (-1,0,1) or %NULL
Linus Torvalds's avatar
Linus Torvalds committed
1596
 *
1597
 * Inside configuration space defined by @params remove from @var all
Linus Torvalds's avatar
Linus Torvalds committed
1598
1599
1600
 * values < maximum. Reduce configuration space accordingly.
 * Return the maximum.
 */
1601
1602
1603
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
1604
1605
1606
1607
1608
1609
{
	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);
1610
1611
		if (snd_BUG_ON(err < 0))
			return err;
Linus Torvalds's avatar
Linus Torvalds committed
1612
1613
1614
1615
	}
	return snd_pcm_hw_param_value(params, var, dir);
}

1616
EXPORT_SYMBOL(snd_pcm_hw_param_last);
Linus Torvalds's avatar
Linus Torvalds committed
1617
1618

/**
1619
 * snd_pcm_hw_param_choose - choose a configuration defined by @params
Takashi Iwai's avatar
Takashi Iwai committed
1620
1621
 * @pcm: PCM instance
 * @params: the hw_params instance
Linus Torvalds's avatar
Linus Torvalds committed
1622
 *
1623
 * Choose one configuration from configuration space defined by @params.
Linus Torvalds's avatar
Linus Torvalds committed
1624
1625
1626
1627
 * 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
 */
1628
1629
int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm,
			     struct snd_pcm_hw_params *params)
Linus Torvalds's avatar
Linus Torvalds committed
1630
{
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
	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
1643

1644
1645
1646
1647
1648
	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);
1649
1650
		if (snd_BUG_ON(err < 0))
			return err;
1651
	}
Linus Torvalds's avatar
Linus Torvalds committed
1652
1653
1654
	return 0;
}

1655
static int snd_pcm_lib_ioctl_reset(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1656
1657
				   void *arg)
{
1658
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
	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;
}

1670
static int snd_pcm_lib_ioctl_channel_info(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1671
1672
					  void *arg)
{
1673
1674
	struct snd_pcm_channel_info *info = arg;
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
	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;
}

1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
static int snd_pcm_lib_ioctl_fifo_size(struct snd_pcm_substream *substream,
				       void *arg)
{
	struct snd_pcm_hw_params *params = arg;
	snd_pcm_format_t format;
	int channels, width;

	params->fifo_size = substream->runtime->hw.fifo_size;
	if (!(substream->runtime->hw.info & SNDRV_PCM_INFO_FIFO_IN_FRAMES)) {
		format = params_format(params);
		channels = params_channels(params);
		width = snd_pcm_format_physical_width(format);
		params->fifo_size /= width * channels;
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
/**
 * 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.
 */
1733
int snd_pcm_lib_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1734
1735
1736
1737
1738
1739
1740
1741
1742
		      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);
1743
1744
	case SNDRV_PCM_IOCTL1_FIFO_SIZE:
		return snd_pcm_lib_ioctl_fifo_size(substream, arg);
Linus Torvalds's avatar
Linus Torvalds committed
1745
1746
1747
1748
	}
	return -ENXIO;
}

1749
1750
EXPORT_SYMBOL(snd_pcm_lib_ioctl);

Linus Torvalds's avatar
Linus Torvalds committed
1751
1752
1753
1754
1755
1756
/**
 * 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
1757
 * pointer, wake up sleepers, etc.
Linus Torvalds's avatar
Linus Torvalds committed
1758
1759
1760
1761
 *
 * Even if more than one periods have elapsed since the last call, you
 * have to call this only once.
 */
1762
void snd_pcm_period_elapsed(struct snd_pcm_substream *substream)
Linus Torvalds's avatar
Linus Torvalds committed
1763
{
1764
	struct snd_pcm_runtime *runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1765
1766
	unsigned long flags;

1767
1768
	if (PCM_RUNTIME_CHECK(substream))
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1769
1770
1771
1772
1773
1774
1775
	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) ||
1776
	    snd_pcm_update_hw_ptr0(substream, 1) < 0)
Linus Torvalds's avatar
Linus Torvalds committed
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
		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);
}

1788
1789
EXPORT_SYMBOL(snd_pcm_period_elapsed);

1790
1791
1792
1793
1794
1795
/*
 * 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.
 */
1796
static int wait_for_avail(struct snd_pcm_substream *substream,
1797
1798
1799
1800
1801
1802
1803
			      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;
1804
1805
	long wait_time, tout;

1806
1807
1808
1809
	init_waitqueue_entry(&wait, current);
	set_current_state(TASK_INTERRUPTIBLE);
	add_wait_queue(&runtime->tsleep, &wait);

1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
	if (runtime->no_period_wakeup)
		wait_time = MAX_SCHEDULE_TIMEOUT;
	else {
		wait_time = 10;
		if (runtime->rate) {
			long t = runtime->period_size * 2 / runtime->rate;
			wait_time = max(t, wait_time);
		}
		wait_time = msecs_to_jiffies(wait_time * 1000);
	}
1820

1821
1822
1823
1824
1825
	for (;;) {
		if (signal_pending(current)) {
			err = -ERESTARTSYS;
			break;
		}
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839

		/*
		 * We need to check if space became available already
		 * (and thus the wakeup happened already) first to close
		 * the race of space already having become available.
		 * This check must happen after been added to the waitqueue
		 * and having current state be INTERRUPTIBLE.
		 */
		if (is_playback)
			avail = snd_pcm_playback_avail(runtime);
		else
			avail = snd_pcm_capture_avail(runtime);
		if (avail >= runtime->twake)
			break;
1840
		snd_pcm_stream_unlock_irq(substream);
1841
1842
1843

		tout = schedule_timeout(wait_time);

1844
		snd_pcm_stream_lock_irq(substream);
1845
		set_current_state(TASK_INTERRUPTIBLE);
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
		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;
		}
	}
 _endloop:
1873
	set_current_state(TASK_RUNNING);
1874
	remove_wait_queue(&runtime->tsleep, &wait);
1875
1876
1877
1878
	*availp = avail;
	return err;
}
	
1879
static int snd_pcm_lib_write_transfer(struct snd_pcm_substream *substream,
Linus Torvalds's avatar
Linus Torvalds committed
1880
1881
1882
1883
				      unsigned int hwoff,
				      unsigned long data, unsigned int off,
				      snd_pcm_uframes_t frames)
{
1884
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
	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;
}
 
1898
typedef int (*transfer_f)(struct snd_pcm_substream *substream, unsigned int hwoff,
Linus Torvalds's avatar
Linus Torvalds committed
1899
1900
1901
			  unsigned long data, unsigned int off,
			  snd_pcm_uframes_t size);

1902
static snd_pcm_sframes_t snd_pcm_lib_write1(struct snd_pcm_substream *substream, 
Linus Torvalds's avatar
Linus Torvalds committed
1903
1904
1905
1906
1907
					    unsigned long data,
					    snd_pcm_uframes_t size,
					    int nonblock,
					    transfer_f transfer)
{
1908
	struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds's avatar
Linus Torvalds committed
1909
1910
	snd_pcm_uframes_t xfer = 0;
	snd_pcm_uframes_t offset = 0;
1911
	snd_pcm_uframes_t avail;
Linus Torvalds's avatar
Linus Torvalds committed
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
	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;
	}

1934
	runtime->twake = runtime->control->avail_min ? : 1;
1935
1936
1937
	if (runtime->status->state == SNDRV_PCM_STATE_RUNNING)
		snd_pcm_update_hw_ptr(substream);
	avail = snd_pcm_playback_avail(runtime);
Linus Torvalds's avatar
Linus Torvalds committed
1938
1939
1940
	while (size > 0) {
		snd_pcm_uframes_t frames, appl_ptr, appl_ofs;
		snd_pcm_uframes_t cont;
1941
		if (!avail) {
Linus Torvalds's avatar
Linus Torvalds committed
1942
1943
1944
1945
			if (nonblock) {
				err = -EAGAIN;
				goto _end_unlock;
			}
1946
1947
1948
			runtime->twake = min_t(snd_pcm_uframes_t, size,
					runtime->control->avail_min ? : 1);
			err = wait_for_avail(substream, &avail);
1949
			if (err < 0)
1950
				goto _end_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
1951
1952
1953
1954
1955
		}
		frames = size > avail ? avail : size;
		cont = runtime->buffer_size - runtime->control->appl_ptr % runtime->buffer_size;
		if (frames > cont)
			frames = cont;
1956
		if (snd_BUG_ON(!frames)) {
1957
			runtime->twake = 0;
1958
1959
1960
			snd_pcm_stream_unlock_irq(substream);
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1961
1962
1963
		appl_ptr = runtime->control->appl_ptr;
		appl_ofs = appl_ptr % runtime->buffer_size;
		snd_pcm_stream_unlock_irq(substream);
1964
		err = transfer(substream, appl_ofs, data, offset, frames);
Linus Torvalds's avatar
Linus Torvalds committed
1965
		snd_pcm_stream_lock_irq(substream);
1966
1967
		if (err < 0)
			goto _end_unlock;
Linus Torvalds's avatar
Linus Torvalds committed
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
		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;
1988
		avail -= frames;
Linus Torvalds's avatar
Linus Torvalds committed
1989
1990
1991
1992
1993
1994
1995
1996
		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:
1997
	runtime->twake = 0;
1998
1999
	if (xfer > 0 && err >= 0)
		snd_pcm_update_state(substream, runtime);
Linus Torvalds's avatar
Linus Torvalds committed
2000
	snd_pcm_stream_unlock_irq(substream);
For faster browsing, not all history is shown. View entire blame