endpoint.c 32.9 KB
Newer Older
1001
1002
1003
1004
1005
1006
 * snd_usb_endpoint_stop: stop an snd_usb_endpoint
 *
 * @ep: the endpoint to stop (may be NULL)
 *
 * A call to this function will decrement the use count of the endpoint.
 * In case the last user has requested the endpoint stop, the URBs will
1007
 * actually be deactivated.
1008
1009
 *
 * Must be balanced to calls of snd_usb_endpoint_start().
1010
1011
1012
 *
 * The caller needs to synchronize the pending stop operation via
 * snd_usb_endpoint_sync_pending_stop().
1013
 */
1014
void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep)
1015
1016
1017
1018
1019
1020
1021
1022
{
	if (!ep)
		return;

	if (snd_BUG_ON(ep->use_count == 0))
		return;

	if (--ep->use_count == 0) {
1023
		deactivate_urbs(ep, false);
1024
		set_bit(EP_FLAG_STOPPING, &ep->flags);
1025
1026
1027
	}
}

1028
1029
1030
1031
1032
/**
 * snd_usb_endpoint_deactivate: deactivate an snd_usb_endpoint
 *
 * @ep: the endpoint to deactivate
 *
1033
1034
 * If the endpoint is not currently in use, this functions will
 * deactivate its associated URBs.
1035
1036
1037
 *
 * In case of any active users, this functions does nothing.
 */
1038
void snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep)
1039
1040
{
	if (!ep)
1041
		return;
1042
1043

	if (ep->use_count != 0)
1044
		return;
1045

1046
1047
	deactivate_urbs(ep, true);
	wait_clear_urbs(ep);
1048
1049
}

1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
/**
 * snd_usb_endpoint_release: Tear down an snd_usb_endpoint
 *
 * @ep: the endpoint to release
 *
 * This function does not care for the endpoint's use count but will tear
 * down all the streaming URBs immediately.
 */
void snd_usb_endpoint_release(struct snd_usb_endpoint *ep)
{
	release_urbs(ep, 1);
}

1063
1064
/**
 * snd_usb_endpoint_free: Free the resources of an snd_usb_endpoint
1065
 *
1066
 * @ep: the endpoint to free
1067
 *
1068
 * This free all resources of the given ep.
1069
 */
1070
void snd_usb_endpoint_free(struct snd_usb_endpoint *ep)
1071
1072
1073
1074
{
	kfree(ep);
}

1075
1076
1077
1078
1079
1080
1081
1082
1083
/**
 * snd_usb_handle_sync_urb: parse an USB sync packet
 *
 * @ep: the endpoint to handle the packet
 * @sender: the sending endpoint
 * @urb: the received packet
 *
 * This function is called from the context of an endpoint that received
 * the packet and is used to let another endpoint object handle the payload.
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
 */
void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,
			     struct snd_usb_endpoint *sender,
			     const struct urb *urb)
{
	int shift;
	unsigned int f;
	unsigned long flags;

	snd_BUG_ON(ep == sender);

1095
1096
	/*
	 * In case the endpoint is operating in implicit feedback mode, prepare
1097
1098
1099
	 * a new outbound URB that has the same layout as the received packet
	 * and add it to the list of pending urbs. queue_pending_output_urbs()
	 * will take care of them later.
1100
	 */
1101
	if (snd_usb_endpoint_implicit_feedback_sink(ep) &&
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
	    ep->use_count != 0) {

		/* implicit feedback case */
		int i, bytes = 0;
		struct snd_urb_ctx *in_ctx;
		struct snd_usb_packet_info *out_packet;

		in_ctx = urb->context;

		/* Count overall packet size */
		for (i = 0; i < in_ctx->packets; i++)
			if (urb->iso_frame_desc[i].status == 0)
				bytes += urb->iso_frame_desc[i].actual_length;

		/*
		 * skip empty packets. At least M-Audio's Fast Track Ultra stops
		 * streaming once it received a 0-byte OUT URB
		 */
		if (bytes == 0)
			return;

		spin_lock_irqsave(&ep->lock, flags);
		out_packet = ep->next_packet + ep->next_packet_write_pos;

		/*
		 * Iterate through the inbound packet and prepare the lengths
		 * for the output packet. The OUT packet we are about to send
1129
1130
1131
1132
1133
		 * will have the same amount of payload bytes per stride as the
		 * IN packet we just received. Since the actual size is scaled
		 * by the stride, use the sender stride to calculate the length
		 * in case the number of channels differ between the implicitly
		 * fed-back endpoint and the synchronizing endpoint.
1134
1135
1136
1137
1138
1139
		 */

		out_packet->packets = in_ctx->packets;
		for (i = 0; i < in_ctx->packets; i++) {
			if (urb->iso_frame_desc[i].status == 0)
				out_packet->packet_size[i] =
1140
					urb->iso_frame_desc[i].actual_length / sender->stride;
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
			else
				out_packet->packet_size[i] = 0;
		}

		ep->next_packet_write_pos++;
		ep->next_packet_write_pos %= MAX_URBS;
		spin_unlock_irqrestore(&ep->lock, flags);
		queue_pending_output_urbs(ep);

		return;
	}

1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
	/*
	 * process after playback sync complete
	 *
	 * Full speed devices report feedback values in 10.14 format as samples
	 * per frame, high speed devices in 16.16 format as samples per
	 * microframe.
	 *
	 * Because the Audio Class 1 spec was written before USB 2.0, many high
	 * speed devices use a wrong interpretation, some others use an
	 * entirely different format.
	 *
	 * Therefore, we cannot predict what format any particular device uses
	 * and must detect it automatically.
	 */
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180

	if (urb->iso_frame_desc[0].status != 0 ||
	    urb->iso_frame_desc[0].actual_length < 3)
		return;

	f = le32_to_cpup(urb->transfer_buffer);
	if (urb->iso_frame_desc[0].actual_length == 3)
		f &= 0x00ffffff;
	else
		f &= 0x0fffffff;

	if (f == 0)
		return;

1181
	if (unlikely(sender->tenor_fb_quirk)) {
1182
		/*
1183
1184
		 * Devices based on Tenor 8802 chipsets (TEAC UD-H01
		 * and others) sometimes change the feedback value
1185
1186
1187
		 * by +/- 0x1.0000.
		 */
		if (f < ep->freqn - 0x8000)
1188
			f += 0xf000;
1189
		else if (f > ep->freqn + 0x8000)
1190
			f -= 0xf000;
1191
	} else if (unlikely(ep->freqshift == INT_MIN)) {
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
		/*
		 * The first time we see a feedback value, determine its format
		 * by shifting it left or right until it matches the nominal
		 * frequency value.  This assumes that the feedback does not
		 * differ from the nominal value more than +50% or -25%.
		 */
		shift = 0;
		while (f < ep->freqn - ep->freqn / 4) {
			f <<= 1;
			shift++;
		}
		while (f > ep->freqn + ep->freqn / 2) {
			f >>= 1;
			shift--;
		}
		ep->freqshift = shift;
	} else if (ep->freqshift >= 0)
		f <<= ep->freqshift;
	else
		f >>= -ep->freqshift;

	if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) {
		/*
		 * If the frequency looks valid, set it.
		 * This value is referred to in prepare_playback_urb().
		 */
		spin_lock_irqsave(&ep->lock, flags);
		ep->freqm = f;
		spin_unlock_irqrestore(&ep->lock, flags);
	} else {
		/*
		 * Out of range; maybe the shift value is wrong.
		 * Reset it so that we autodetect again the next time.
		 */
		ep->freqshift = INT_MIN;
	}
}
For faster browsing, not all history is shown. View entire blame