diff options
-rw-r--r-- | sound/usb/card.h | 58 | ||||
-rw-r--r-- | sound/usb/endpoint.c | 928 | ||||
-rw-r--r-- | sound/usb/endpoint.h | 26 | ||||
-rw-r--r-- | sound/usb/usbaudio.h | 1 |
4 files changed, 1002 insertions, 11 deletions
diff --git a/sound/usb/card.h b/sound/usb/card.h index da5fa1ac4ed..9acbd4a1228 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h | |||
@@ -30,13 +30,17 @@ struct audioformat { | |||
30 | }; | 30 | }; |
31 | 31 | ||
32 | struct snd_usb_substream; | 32 | struct snd_usb_substream; |
33 | struct snd_usb_endpoint; | ||
33 | 34 | ||
34 | struct snd_urb_ctx { | 35 | struct snd_urb_ctx { |
35 | struct urb *urb; | 36 | struct urb *urb; |
36 | unsigned int buffer_size; /* size of data buffer, if data URB */ | 37 | unsigned int buffer_size; /* size of data buffer, if data URB */ |
37 | struct snd_usb_substream *subs; | 38 | struct snd_usb_substream *subs; |
39 | struct snd_usb_endpoint *ep; | ||
38 | int index; /* index for urb array */ | 40 | int index; /* index for urb array */ |
39 | int packets; /* number of packets per urb */ | 41 | int packets; /* number of packets per urb */ |
42 | int packet_size[MAX_PACKS_HS]; /* size of packets for next submission */ | ||
43 | struct list_head ready_list; | ||
40 | }; | 44 | }; |
41 | 45 | ||
42 | struct snd_urb_ops { | 46 | struct snd_urb_ops { |
@@ -46,6 +50,60 @@ struct snd_urb_ops { | |||
46 | int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); | 50 | int (*retire_sync)(struct snd_usb_substream *subs, struct snd_pcm_runtime *runtime, struct urb *u); |
47 | }; | 51 | }; |
48 | 52 | ||
53 | struct snd_usb_endpoint { | ||
54 | struct snd_usb_audio *chip; | ||
55 | |||
56 | int use_count; | ||
57 | int ep_num; /* the referenced endpoint number */ | ||
58 | int type; /* SND_USB_ENDPOINT_TYPE_* */ | ||
59 | unsigned long flags; | ||
60 | |||
61 | void (*prepare_data_urb) (struct snd_usb_substream *subs, | ||
62 | struct urb *urb); | ||
63 | void (*retire_data_urb) (struct snd_usb_substream *subs, | ||
64 | struct urb *urb); | ||
65 | |||
66 | struct snd_usb_substream *data_subs; | ||
67 | struct snd_usb_endpoint *sync_master; | ||
68 | struct snd_usb_endpoint *sync_slave; | ||
69 | |||
70 | struct snd_urb_ctx urb[MAX_URBS]; | ||
71 | |||
72 | struct snd_usb_packet_info { | ||
73 | uint32_t packet_size[MAX_PACKS_HS]; | ||
74 | int packets; | ||
75 | } next_packet[MAX_URBS]; | ||
76 | int next_packet_read_pos, next_packet_write_pos; | ||
77 | struct list_head ready_playback_urbs; | ||
78 | |||
79 | unsigned int nurbs; /* # urbs */ | ||
80 | unsigned long active_mask; /* bitmask of active urbs */ | ||
81 | unsigned long unlink_mask; /* bitmask of unlinked urbs */ | ||
82 | char *syncbuf; /* sync buffer for all sync URBs */ | ||
83 | dma_addr_t sync_dma; /* DMA address of syncbuf */ | ||
84 | |||
85 | unsigned int pipe; /* the data i/o pipe */ | ||
86 | unsigned int freqn; /* nominal sampling rate in fs/fps in Q16.16 format */ | ||
87 | unsigned int freqm; /* momentary sampling rate in fs/fps in Q16.16 format */ | ||
88 | int freqshift; /* how much to shift the feedback value to get Q16.16 */ | ||
89 | unsigned int freqmax; /* maximum sampling rate, used for buffer management */ | ||
90 | unsigned int phase; /* phase accumulator */ | ||
91 | unsigned int maxpacksize; /* max packet size in bytes */ | ||
92 | unsigned int maxframesize; /* max packet size in frames */ | ||
93 | unsigned int curpacksize; /* current packet size in bytes (for capture) */ | ||
94 | unsigned int curframesize; /* current packet size in frames (for capture) */ | ||
95 | unsigned int syncmaxsize; /* sync endpoint packet size */ | ||
96 | unsigned int fill_max:1; /* fill max packet size always */ | ||
97 | unsigned int datainterval; /* log_2 of data packet interval */ | ||
98 | unsigned int syncinterval; /* P for adaptive mode, 0 otherwise */ | ||
99 | unsigned char silence_value; | ||
100 | unsigned int stride; | ||
101 | int iface, alt_idx; | ||
102 | |||
103 | spinlock_t lock; | ||
104 | struct list_head list; | ||
105 | }; | ||
106 | |||
49 | struct snd_usb_substream { | 107 | struct snd_usb_substream { |
50 | struct snd_usb_stream *stream; | 108 | struct snd_usb_stream *stream; |
51 | struct usb_device *dev; | 109 | struct usb_device *dev; |
diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c index 08dcce53720..ea25265427a 100644 --- a/sound/usb/endpoint.c +++ b/sound/usb/endpoint.c | |||
@@ -20,9 +20,11 @@ | |||
20 | #include <linux/ratelimit.h> | 20 | #include <linux/ratelimit.h> |
21 | #include <linux/usb.h> | 21 | #include <linux/usb.h> |
22 | #include <linux/usb/audio.h> | 22 | #include <linux/usb/audio.h> |
23 | #include <linux/slab.h> | ||
23 | 24 | ||
24 | #include <sound/core.h> | 25 | #include <sound/core.h> |
25 | #include <sound/pcm.h> | 26 | #include <sound/pcm.h> |
27 | #include <sound/pcm_params.h> | ||
26 | 28 | ||
27 | #include "usbaudio.h" | 29 | #include "usbaudio.h" |
28 | #include "helper.h" | 30 | #include "helper.h" |
@@ -30,6 +32,9 @@ | |||
30 | #include "endpoint.h" | 32 | #include "endpoint.h" |
31 | #include "pcm.h" | 33 | #include "pcm.h" |
32 | 34 | ||
35 | #define EP_FLAG_ACTIVATED 0 | ||
36 | #define EP_FLAG_RUNNING 1 | ||
37 | |||
33 | /* | 38 | /* |
34 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) | 39 | * convert a sampling rate into our full speed format (fs/1000 in Q16.16) |
35 | * this will overflow at approx 524 kHz | 40 | * this will overflow at approx 524 kHz |
@@ -51,7 +56,7 @@ static inline unsigned get_usb_high_speed_rate(unsigned int rate) | |||
51 | /* | 56 | /* |
52 | * unlink active urbs. | 57 | * unlink active urbs. |
53 | */ | 58 | */ |
54 | static int deactivate_urbs(struct snd_usb_substream *subs, int force, int can_sleep) | 59 | static int deactivate_urbs_old(struct snd_usb_substream *subs, int force, int can_sleep) |
55 | { | 60 | { |
56 | struct snd_usb_audio *chip = subs->stream->chip; | 61 | struct snd_usb_audio *chip = subs->stream->chip; |
57 | unsigned int i; | 62 | unsigned int i; |
@@ -113,7 +118,7 @@ static void release_urb_ctx(struct snd_urb_ctx *u) | |||
113 | /* | 118 | /* |
114 | * wait until all urbs are processed. | 119 | * wait until all urbs are processed. |
115 | */ | 120 | */ |
116 | static int wait_clear_urbs(struct snd_usb_substream *subs) | 121 | static int wait_clear_urbs_old(struct snd_usb_substream *subs) |
117 | { | 122 | { |
118 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); | 123 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); |
119 | unsigned int i; | 124 | unsigned int i; |
@@ -148,8 +153,8 @@ void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force) | |||
148 | int i; | 153 | int i; |
149 | 154 | ||
150 | /* stop urbs (to be sure) */ | 155 | /* stop urbs (to be sure) */ |
151 | deactivate_urbs(subs, force, 1); | 156 | deactivate_urbs_old(subs, force, 1); |
152 | wait_clear_urbs(subs); | 157 | wait_clear_urbs_old(subs); |
153 | 158 | ||
154 | for (i = 0; i < MAX_URBS; i++) | 159 | for (i = 0; i < MAX_URBS; i++) |
155 | release_urb_ctx(&subs->dataurb[i]); | 160 | release_urb_ctx(&subs->dataurb[i]); |
@@ -164,7 +169,7 @@ void snd_usb_release_substream_urbs(struct snd_usb_substream *subs, int force) | |||
164 | /* | 169 | /* |
165 | * complete callback from data urb | 170 | * complete callback from data urb |
166 | */ | 171 | */ |
167 | static void snd_complete_urb(struct urb *urb) | 172 | static void snd_complete_urb_old(struct urb *urb) |
168 | { | 173 | { |
169 | struct snd_urb_ctx *ctx = urb->context; | 174 | struct snd_urb_ctx *ctx = urb->context; |
170 | struct snd_usb_substream *subs = ctx->subs; | 175 | struct snd_usb_substream *subs = ctx->subs; |
@@ -318,7 +323,7 @@ int snd_usb_init_substream_urbs(struct snd_usb_substream *subs, | |||
318 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | 323 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
319 | u->urb->interval = 1 << subs->datainterval; | 324 | u->urb->interval = 1 << subs->datainterval; |
320 | u->urb->context = u; | 325 | u->urb->context = u; |
321 | u->urb->complete = snd_complete_urb; | 326 | u->urb->complete = snd_complete_urb_old; |
322 | } | 327 | } |
323 | 328 | ||
324 | if (subs->syncpipe) { | 329 | if (subs->syncpipe) { |
@@ -856,7 +861,7 @@ static int start_urbs(struct snd_usb_substream *subs, struct snd_pcm_runtime *ru | |||
856 | 861 | ||
857 | __error: | 862 | __error: |
858 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); | 863 | // snd_pcm_stop(subs->pcm_substream, SNDRV_PCM_STATE_XRUN); |
859 | deactivate_urbs(subs, 0, 0); | 864 | deactivate_urbs_old(subs, 0, 0); |
860 | return -EPIPE; | 865 | return -EPIPE; |
861 | } | 866 | } |
862 | 867 | ||
@@ -917,7 +922,7 @@ int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int | |||
917 | subs->ops.prepare = prepare_playback_urb; | 922 | subs->ops.prepare = prepare_playback_urb; |
918 | return 0; | 923 | return 0; |
919 | case SNDRV_PCM_TRIGGER_STOP: | 924 | case SNDRV_PCM_TRIGGER_STOP: |
920 | return deactivate_urbs(subs, 0, 0); | 925 | return deactivate_urbs_old(subs, 0, 0); |
921 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | 926 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
922 | subs->ops.prepare = prepare_nodata_playback_urb; | 927 | subs->ops.prepare = prepare_nodata_playback_urb; |
923 | return 0; | 928 | return 0; |
@@ -935,7 +940,7 @@ int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int c | |||
935 | subs->ops.retire = retire_capture_urb; | 940 | subs->ops.retire = retire_capture_urb; |
936 | return start_urbs(subs, substream->runtime); | 941 | return start_urbs(subs, substream->runtime); |
937 | case SNDRV_PCM_TRIGGER_STOP: | 942 | case SNDRV_PCM_TRIGGER_STOP: |
938 | return deactivate_urbs(subs, 0, 0); | 943 | return deactivate_urbs_old(subs, 0, 0); |
939 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | 944 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
940 | subs->ops.retire = retire_paused_capture_urb; | 945 | subs->ops.retire = retire_paused_capture_urb; |
941 | return 0; | 946 | return 0; |
@@ -951,8 +956,8 @@ int snd_usb_substream_prepare(struct snd_usb_substream *subs, | |||
951 | struct snd_pcm_runtime *runtime) | 956 | struct snd_pcm_runtime *runtime) |
952 | { | 957 | { |
953 | /* clear urbs (to be sure) */ | 958 | /* clear urbs (to be sure) */ |
954 | deactivate_urbs(subs, 0, 1); | 959 | deactivate_urbs_old(subs, 0, 1); |
955 | wait_clear_urbs(subs); | 960 | wait_clear_urbs_old(subs); |
956 | 961 | ||
957 | /* for playback, submit the URBs now; otherwise, the first hwptr_done | 962 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
958 | * updates for all URBs would happen at the same time when starting */ | 963 | * updates for all URBs would happen at the same time when starting */ |
@@ -964,3 +969,904 @@ int snd_usb_substream_prepare(struct snd_usb_substream *subs, | |||
964 | return 0; | 969 | return 0; |
965 | } | 970 | } |
966 | 971 | ||
972 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep) | ||
973 | { | ||
974 | return ep->sync_master && | ||
975 | ep->sync_master->type == SND_USB_ENDPOINT_TYPE_DATA && | ||
976 | ep->type == SND_USB_ENDPOINT_TYPE_DATA && | ||
977 | usb_pipeout(ep->pipe); | ||
978 | } | ||
979 | |||
980 | /* determine the number of frames in the next packet */ | ||
981 | static int next_packet_size(struct snd_usb_endpoint *ep) | ||
982 | { | ||
983 | unsigned long flags; | ||
984 | int ret; | ||
985 | |||
986 | if (ep->fill_max) | ||
987 | return ep->maxframesize; | ||
988 | |||
989 | spin_lock_irqsave(&ep->lock, flags); | ||
990 | ep->phase = (ep->phase & 0xffff) | ||
991 | + (ep->freqm << ep->datainterval); | ||
992 | ret = min(ep->phase >> 16, ep->maxframesize); | ||
993 | spin_unlock_irqrestore(&ep->lock, flags); | ||
994 | |||
995 | return ret; | ||
996 | } | ||
997 | |||
998 | static void retire_outbound_urb(struct snd_usb_endpoint *ep, | ||
999 | struct snd_urb_ctx *urb_ctx) | ||
1000 | { | ||
1001 | if (ep->retire_data_urb) | ||
1002 | ep->retire_data_urb(ep->data_subs, urb_ctx->urb); | ||
1003 | } | ||
1004 | |||
1005 | static void retire_inbound_urb(struct snd_usb_endpoint *ep, | ||
1006 | struct snd_urb_ctx *urb_ctx) | ||
1007 | { | ||
1008 | struct urb *urb = urb_ctx->urb; | ||
1009 | |||
1010 | if (ep->sync_slave) | ||
1011 | snd_usb_handle_sync_urb(ep->sync_slave, ep, urb); | ||
1012 | |||
1013 | if (ep->retire_data_urb) | ||
1014 | ep->retire_data_urb(ep->data_subs, urb); | ||
1015 | } | ||
1016 | |||
1017 | static void prepare_outbound_urb_sizes(struct snd_usb_endpoint *ep, | ||
1018 | struct snd_urb_ctx *ctx) | ||
1019 | { | ||
1020 | int i; | ||
1021 | |||
1022 | for (i = 0; i < ctx->packets; ++i) | ||
1023 | ctx->packet_size[i] = next_packet_size(ep); | ||
1024 | } | ||
1025 | |||
1026 | /* | ||
1027 | * Prepare a PLAYBACK urb for submission to the bus. | ||
1028 | */ | ||
1029 | static void prepare_outbound_urb(struct snd_usb_endpoint *ep, | ||
1030 | struct snd_urb_ctx *ctx) | ||
1031 | { | ||
1032 | int i; | ||
1033 | struct urb *urb = ctx->urb; | ||
1034 | unsigned char *cp = urb->transfer_buffer; | ||
1035 | |||
1036 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | ||
1037 | |||
1038 | switch (ep->type) { | ||
1039 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
1040 | if (ep->prepare_data_urb) { | ||
1041 | ep->prepare_data_urb(ep->data_subs, urb); | ||
1042 | } else { | ||
1043 | /* no data provider, so send silence */ | ||
1044 | unsigned int offs = 0; | ||
1045 | for (i = 0; i < ctx->packets; ++i) { | ||
1046 | int counts = ctx->packet_size[i]; | ||
1047 | urb->iso_frame_desc[i].offset = offs * ep->stride; | ||
1048 | urb->iso_frame_desc[i].length = counts * ep->stride; | ||
1049 | offs += counts; | ||
1050 | } | ||
1051 | |||
1052 | urb->number_of_packets = ctx->packets; | ||
1053 | urb->transfer_buffer_length = offs * ep->stride; | ||
1054 | memset(urb->transfer_buffer, ep->silence_value, | ||
1055 | offs * ep->stride); | ||
1056 | } | ||
1057 | break; | ||
1058 | |||
1059 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
1060 | if (snd_usb_get_speed(ep->chip->dev) >= USB_SPEED_HIGH) { | ||
1061 | /* | ||
1062 | * fill the length and offset of each urb descriptor. | ||
1063 | * the fixed 12.13 frequency is passed as 16.16 through the pipe. | ||
1064 | */ | ||
1065 | urb->iso_frame_desc[0].length = 4; | ||
1066 | urb->iso_frame_desc[0].offset = 0; | ||
1067 | cp[0] = ep->freqn; | ||
1068 | cp[1] = ep->freqn >> 8; | ||
1069 | cp[2] = ep->freqn >> 16; | ||
1070 | cp[3] = ep->freqn >> 24; | ||
1071 | } else { | ||
1072 | /* | ||
1073 | * fill the length and offset of each urb descriptor. | ||
1074 | * the fixed 10.14 frequency is passed through the pipe. | ||
1075 | */ | ||
1076 | urb->iso_frame_desc[0].length = 3; | ||
1077 | urb->iso_frame_desc[0].offset = 0; | ||
1078 | cp[0] = ep->freqn >> 2; | ||
1079 | cp[1] = ep->freqn >> 10; | ||
1080 | cp[2] = ep->freqn >> 18; | ||
1081 | } | ||
1082 | |||
1083 | break; | ||
1084 | } | ||
1085 | } | ||
1086 | |||
1087 | /* | ||
1088 | * Prepare a CAPTURE or SYNC urb for submission to the bus. | ||
1089 | */ | ||
1090 | static inline void prepare_inbound_urb(struct snd_usb_endpoint *ep, | ||
1091 | struct snd_urb_ctx *urb_ctx) | ||
1092 | { | ||
1093 | int i, offs; | ||
1094 | struct urb *urb = urb_ctx->urb; | ||
1095 | |||
1096 | urb->dev = ep->chip->dev; /* we need to set this at each time */ | ||
1097 | |||
1098 | switch (ep->type) { | ||
1099 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
1100 | offs = 0; | ||
1101 | for (i = 0; i < urb_ctx->packets; i++) { | ||
1102 | urb->iso_frame_desc[i].offset = offs; | ||
1103 | urb->iso_frame_desc[i].length = ep->curpacksize; | ||
1104 | offs += ep->curpacksize; | ||
1105 | } | ||
1106 | |||
1107 | urb->transfer_buffer_length = offs; | ||
1108 | urb->number_of_packets = urb_ctx->packets; | ||
1109 | break; | ||
1110 | |||
1111 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
1112 | urb->iso_frame_desc[0].length = min(4u, ep->syncmaxsize); | ||
1113 | urb->iso_frame_desc[0].offset = 0; | ||
1114 | break; | ||
1115 | } | ||
1116 | } | ||
1117 | |||
1118 | static void queue_pending_output_urbs(struct snd_usb_endpoint *ep) | ||
1119 | { | ||
1120 | while (test_bit(EP_FLAG_RUNNING, &ep->flags)) { | ||
1121 | |||
1122 | unsigned long flags; | ||
1123 | struct snd_usb_packet_info *packet; | ||
1124 | struct snd_urb_ctx *ctx = NULL; | ||
1125 | struct urb *urb; | ||
1126 | int err, i; | ||
1127 | |||
1128 | spin_lock_irqsave(&ep->lock, flags); | ||
1129 | if (ep->next_packet_read_pos != ep->next_packet_write_pos) { | ||
1130 | packet = ep->next_packet + ep->next_packet_read_pos; | ||
1131 | ep->next_packet_read_pos++; | ||
1132 | ep->next_packet_read_pos %= MAX_URBS; | ||
1133 | |||
1134 | /* take URB out of FIFO */ | ||
1135 | if (!list_empty(&ep->ready_playback_urbs)) | ||
1136 | ctx = list_first_entry(&ep->ready_playback_urbs, | ||
1137 | struct snd_urb_ctx, ready_list); | ||
1138 | } | ||
1139 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1140 | |||
1141 | if (ctx == NULL) | ||
1142 | return; | ||
1143 | |||
1144 | list_del_init(&ctx->ready_list); | ||
1145 | urb = ctx->urb; | ||
1146 | |||
1147 | /* copy over the length information */ | ||
1148 | for (i = 0; i < packet->packets; i++) | ||
1149 | ctx->packet_size[i] = packet->packet_size[i]; | ||
1150 | |||
1151 | prepare_outbound_urb(ep, ctx); | ||
1152 | |||
1153 | err = usb_submit_urb(ctx->urb, GFP_ATOMIC); | ||
1154 | if (err < 0) | ||
1155 | snd_printk(KERN_ERR "Unable to submit urb #%d: %d (urb %p)\n", | ||
1156 | ctx->index, err, ctx->urb); | ||
1157 | else | ||
1158 | set_bit(ctx->index, &ep->active_mask); | ||
1159 | } | ||
1160 | } | ||
1161 | |||
1162 | /* | ||
1163 | * complete callback for urbs | ||
1164 | */ | ||
1165 | static void snd_complete_urb(struct urb *urb) | ||
1166 | { | ||
1167 | struct snd_urb_ctx *ctx = urb->context; | ||
1168 | struct snd_usb_endpoint *ep = ctx->ep; | ||
1169 | int err; | ||
1170 | |||
1171 | if (unlikely(urb->status == -ENOENT || /* unlinked */ | ||
1172 | urb->status == -ENODEV || /* device removed */ | ||
1173 | urb->status == -ECONNRESET || /* unlinked */ | ||
1174 | urb->status == -ESHUTDOWN || /* device disabled */ | ||
1175 | ep->chip->shutdown)) /* device disconnected */ | ||
1176 | goto exit_clear; | ||
1177 | |||
1178 | if (usb_pipeout(ep->pipe)) { | ||
1179 | retire_outbound_urb(ep, ctx); | ||
1180 | /* can be stopped during retire callback */ | ||
1181 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | ||
1182 | goto exit_clear; | ||
1183 | |||
1184 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
1185 | unsigned long flags; | ||
1186 | |||
1187 | spin_lock_irqsave(&ep->lock, flags); | ||
1188 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | ||
1189 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1190 | queue_pending_output_urbs(ep); | ||
1191 | |||
1192 | goto exit_clear; | ||
1193 | } | ||
1194 | |||
1195 | prepare_outbound_urb_sizes(ep, ctx); | ||
1196 | prepare_outbound_urb(ep, ctx); | ||
1197 | } else { | ||
1198 | retire_inbound_urb(ep, ctx); | ||
1199 | /* can be stopped during retire callback */ | ||
1200 | if (unlikely(!test_bit(EP_FLAG_RUNNING, &ep->flags))) | ||
1201 | goto exit_clear; | ||
1202 | |||
1203 | prepare_inbound_urb(ep, ctx); | ||
1204 | } | ||
1205 | |||
1206 | err = usb_submit_urb(urb, GFP_ATOMIC); | ||
1207 | if (err == 0) | ||
1208 | return; | ||
1209 | |||
1210 | snd_printk(KERN_ERR "cannot submit urb (err = %d)\n", err); | ||
1211 | //snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN); | ||
1212 | |||
1213 | exit_clear: | ||
1214 | clear_bit(ctx->index, &ep->active_mask); | ||
1215 | } | ||
1216 | |||
1217 | struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip, | ||
1218 | struct usb_host_interface *alts, | ||
1219 | int ep_num, int direction, int type) | ||
1220 | { | ||
1221 | struct list_head *p; | ||
1222 | struct snd_usb_endpoint *ep; | ||
1223 | int ret, is_playback = direction == SNDRV_PCM_STREAM_PLAYBACK; | ||
1224 | |||
1225 | mutex_lock(&chip->mutex); | ||
1226 | |||
1227 | list_for_each(p, &chip->ep_list) { | ||
1228 | ep = list_entry(p, struct snd_usb_endpoint, list); | ||
1229 | if (ep->ep_num == ep_num && | ||
1230 | ep->iface == alts->desc.bInterfaceNumber && | ||
1231 | ep->alt_idx == alts->desc.bAlternateSetting) { | ||
1232 | snd_printdd(KERN_DEBUG "Re-using EP %x in iface %d,%d @%p\n", | ||
1233 | ep_num, ep->iface, ep->alt_idx, ep); | ||
1234 | goto __exit_unlock; | ||
1235 | } | ||
1236 | } | ||
1237 | |||
1238 | snd_printdd(KERN_DEBUG "Creating new %s %s endpoint #%x\n", | ||
1239 | is_playback ? "playback" : "capture", | ||
1240 | type == SND_USB_ENDPOINT_TYPE_DATA ? "data" : "sync", | ||
1241 | ep_num); | ||
1242 | |||
1243 | /* select the alt setting once so the endpoints become valid */ | ||
1244 | ret = usb_set_interface(chip->dev, alts->desc.bInterfaceNumber, | ||
1245 | alts->desc.bAlternateSetting); | ||
1246 | if (ret < 0) { | ||
1247 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | ||
1248 | __func__, ret); | ||
1249 | ep = NULL; | ||
1250 | goto __exit_unlock; | ||
1251 | } | ||
1252 | |||
1253 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); | ||
1254 | if (!ep) | ||
1255 | goto __exit_unlock; | ||
1256 | |||
1257 | ep->chip = chip; | ||
1258 | spin_lock_init(&ep->lock); | ||
1259 | ep->type = type; | ||
1260 | ep->ep_num = ep_num; | ||
1261 | ep->iface = alts->desc.bInterfaceNumber; | ||
1262 | ep->alt_idx = alts->desc.bAlternateSetting; | ||
1263 | INIT_LIST_HEAD(&ep->ready_playback_urbs); | ||
1264 | ep_num &= USB_ENDPOINT_NUMBER_MASK; | ||
1265 | |||
1266 | if (is_playback) | ||
1267 | ep->pipe = usb_sndisocpipe(chip->dev, ep_num); | ||
1268 | else | ||
1269 | ep->pipe = usb_rcvisocpipe(chip->dev, ep_num); | ||
1270 | |||
1271 | if (type == SND_USB_ENDPOINT_TYPE_SYNC) { | ||
1272 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | ||
1273 | get_endpoint(alts, 1)->bRefresh >= 1 && | ||
1274 | get_endpoint(alts, 1)->bRefresh <= 9) | ||
1275 | ep->syncinterval = get_endpoint(alts, 1)->bRefresh; | ||
1276 | else if (snd_usb_get_speed(chip->dev) == USB_SPEED_FULL) | ||
1277 | ep->syncinterval = 1; | ||
1278 | else if (get_endpoint(alts, 1)->bInterval >= 1 && | ||
1279 | get_endpoint(alts, 1)->bInterval <= 16) | ||
1280 | ep->syncinterval = get_endpoint(alts, 1)->bInterval - 1; | ||
1281 | else | ||
1282 | ep->syncinterval = 3; | ||
1283 | |||
1284 | ep->syncmaxsize = le16_to_cpu(get_endpoint(alts, 1)->wMaxPacketSize); | ||
1285 | } | ||
1286 | |||
1287 | list_add_tail(&ep->list, &chip->ep_list); | ||
1288 | |||
1289 | __exit_unlock: | ||
1290 | mutex_unlock(&chip->mutex); | ||
1291 | |||
1292 | return ep; | ||
1293 | } | ||
1294 | |||
1295 | /* | ||
1296 | * wait until all urbs are processed. | ||
1297 | */ | ||
1298 | static int wait_clear_urbs(struct snd_usb_endpoint *ep) | ||
1299 | { | ||
1300 | unsigned long end_time = jiffies + msecs_to_jiffies(1000); | ||
1301 | unsigned int i; | ||
1302 | int alive; | ||
1303 | |||
1304 | do { | ||
1305 | alive = 0; | ||
1306 | for (i = 0; i < ep->nurbs; i++) | ||
1307 | if (test_bit(i, &ep->active_mask)) | ||
1308 | alive++; | ||
1309 | |||
1310 | if (!alive) | ||
1311 | break; | ||
1312 | |||
1313 | schedule_timeout_uninterruptible(1); | ||
1314 | } while (time_before(jiffies, end_time)); | ||
1315 | |||
1316 | if (alive) | ||
1317 | snd_printk(KERN_ERR "timeout: still %d active urbs on EP #%x\n", | ||
1318 | alive, ep->ep_num); | ||
1319 | |||
1320 | return 0; | ||
1321 | } | ||
1322 | |||
1323 | /* | ||
1324 | * unlink active urbs. | ||
1325 | */ | ||
1326 | static int deactivate_urbs(struct snd_usb_endpoint *ep, int force, int can_sleep) | ||
1327 | { | ||
1328 | unsigned long flags; | ||
1329 | unsigned int i; | ||
1330 | int async; | ||
1331 | |||
1332 | if (!force && ep->chip->shutdown) /* to be sure... */ | ||
1333 | return -EBADFD; | ||
1334 | |||
1335 | async = !can_sleep && ep->chip->async_unlink; | ||
1336 | |||
1337 | clear_bit(EP_FLAG_RUNNING, &ep->flags); | ||
1338 | |||
1339 | INIT_LIST_HEAD(&ep->ready_playback_urbs); | ||
1340 | ep->next_packet_read_pos = 0; | ||
1341 | ep->next_packet_write_pos = 0; | ||
1342 | |||
1343 | if (!async && in_interrupt()) | ||
1344 | return 0; | ||
1345 | |||
1346 | for (i = 0; i < ep->nurbs; i++) { | ||
1347 | if (test_bit(i, &ep->active_mask)) { | ||
1348 | if (!test_and_set_bit(i, &ep->unlink_mask)) { | ||
1349 | struct urb *u = ep->urb[i].urb; | ||
1350 | if (async) | ||
1351 | usb_unlink_urb(u); | ||
1352 | else | ||
1353 | usb_kill_urb(u); | ||
1354 | } | ||
1355 | } | ||
1356 | } | ||
1357 | |||
1358 | return 0; | ||
1359 | } | ||
1360 | |||
1361 | /* | ||
1362 | * release an endpoint's urbs | ||
1363 | */ | ||
1364 | static void release_urbs(struct snd_usb_endpoint *ep, int force) | ||
1365 | { | ||
1366 | int i; | ||
1367 | |||
1368 | /* route incoming urbs to nirvana */ | ||
1369 | ep->retire_data_urb = NULL; | ||
1370 | ep->prepare_data_urb = NULL; | ||
1371 | |||
1372 | /* stop urbs */ | ||
1373 | deactivate_urbs(ep, force, 1); | ||
1374 | wait_clear_urbs(ep); | ||
1375 | |||
1376 | for (i = 0; i < ep->nurbs; i++) | ||
1377 | release_urb_ctx(&ep->urb[i]); | ||
1378 | |||
1379 | if (ep->syncbuf) | ||
1380 | usb_free_coherent(ep->chip->dev, SYNC_URBS * 4, | ||
1381 | ep->syncbuf, ep->sync_dma); | ||
1382 | |||
1383 | ep->syncbuf = NULL; | ||
1384 | ep->nurbs = 0; | ||
1385 | } | ||
1386 | |||
1387 | static int data_ep_set_params(struct snd_usb_endpoint *ep, | ||
1388 | struct snd_pcm_hw_params *hw_params, | ||
1389 | struct audioformat *fmt, | ||
1390 | struct snd_usb_endpoint *sync_ep) | ||
1391 | { | ||
1392 | unsigned int maxsize, i, urb_packs, total_packs, packs_per_ms; | ||
1393 | int period_bytes = params_period_bytes(hw_params); | ||
1394 | int format = params_format(hw_params); | ||
1395 | int is_playback = usb_pipeout(ep->pipe); | ||
1396 | int frame_bits = snd_pcm_format_physical_width(params_format(hw_params)) * | ||
1397 | params_channels(hw_params); | ||
1398 | |||
1399 | ep->datainterval = fmt->datainterval; | ||
1400 | ep->stride = frame_bits >> 3; | ||
1401 | ep->silence_value = format == SNDRV_PCM_FORMAT_U8 ? 0x80 : 0; | ||
1402 | |||
1403 | /* calculate max. frequency */ | ||
1404 | if (ep->maxpacksize) { | ||
1405 | /* whatever fits into a max. size packet */ | ||
1406 | maxsize = ep->maxpacksize; | ||
1407 | ep->freqmax = (maxsize / (frame_bits >> 3)) | ||
1408 | << (16 - ep->datainterval); | ||
1409 | } else { | ||
1410 | /* no max. packet size: just take 25% higher than nominal */ | ||
1411 | ep->freqmax = ep->freqn + (ep->freqn >> 2); | ||
1412 | maxsize = ((ep->freqmax + 0xffff) * (frame_bits >> 3)) | ||
1413 | >> (16 - ep->datainterval); | ||
1414 | } | ||
1415 | |||
1416 | if (ep->fill_max) | ||
1417 | ep->curpacksize = ep->maxpacksize; | ||
1418 | else | ||
1419 | ep->curpacksize = maxsize; | ||
1420 | |||
1421 | if (snd_usb_get_speed(ep->chip->dev) != USB_SPEED_FULL) | ||
1422 | packs_per_ms = 8 >> ep->datainterval; | ||
1423 | else | ||
1424 | packs_per_ms = 1; | ||
1425 | |||
1426 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
1427 | urb_packs = max(ep->chip->nrpacks, 1); | ||
1428 | urb_packs = min(urb_packs, (unsigned int) MAX_PACKS); | ||
1429 | } else { | ||
1430 | urb_packs = 1; | ||
1431 | } | ||
1432 | |||
1433 | urb_packs *= packs_per_ms; | ||
1434 | |||
1435 | if (sync_ep && !snd_usb_endpoint_implict_feedback_sink(ep)) | ||
1436 | urb_packs = min(urb_packs, 1U << sync_ep->syncinterval); | ||
1437 | |||
1438 | /* decide how many packets to be used */ | ||
1439 | if (is_playback && !snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
1440 | unsigned int minsize, maxpacks; | ||
1441 | /* determine how small a packet can be */ | ||
1442 | minsize = (ep->freqn >> (16 - ep->datainterval)) | ||
1443 | * (frame_bits >> 3); | ||
1444 | /* with sync from device, assume it can be 12% lower */ | ||
1445 | if (sync_ep) | ||
1446 | minsize -= minsize >> 3; | ||
1447 | minsize = max(minsize, 1u); | ||
1448 | total_packs = (period_bytes + minsize - 1) / minsize; | ||
1449 | /* we need at least two URBs for queueing */ | ||
1450 | if (total_packs < 2) { | ||
1451 | total_packs = 2; | ||
1452 | } else { | ||
1453 | /* and we don't want too long a queue either */ | ||
1454 | maxpacks = max(MAX_QUEUE * packs_per_ms, urb_packs * 2); | ||
1455 | total_packs = min(total_packs, maxpacks); | ||
1456 | } | ||
1457 | } else { | ||
1458 | while (urb_packs > 1 && urb_packs * maxsize >= period_bytes) | ||
1459 | urb_packs >>= 1; | ||
1460 | total_packs = MAX_URBS * urb_packs; | ||
1461 | } | ||
1462 | |||
1463 | ep->nurbs = (total_packs + urb_packs - 1) / urb_packs; | ||
1464 | if (ep->nurbs > MAX_URBS) { | ||
1465 | /* too much... */ | ||
1466 | ep->nurbs = MAX_URBS; | ||
1467 | total_packs = MAX_URBS * urb_packs; | ||
1468 | } else if (ep->nurbs < 2) { | ||
1469 | /* too little - we need at least two packets | ||
1470 | * to ensure contiguous playback/capture | ||
1471 | */ | ||
1472 | ep->nurbs = 2; | ||
1473 | } | ||
1474 | |||
1475 | /* allocate and initialize data urbs */ | ||
1476 | for (i = 0; i < ep->nurbs; i++) { | ||
1477 | struct snd_urb_ctx *u = &ep->urb[i]; | ||
1478 | u->index = i; | ||
1479 | u->ep = ep; | ||
1480 | u->packets = (i + 1) * total_packs / ep->nurbs | ||
1481 | - i * total_packs / ep->nurbs; | ||
1482 | u->buffer_size = maxsize * u->packets; | ||
1483 | |||
1484 | if (fmt->fmt_type == UAC_FORMAT_TYPE_II) | ||
1485 | u->packets++; /* for transfer delimiter */ | ||
1486 | u->urb = usb_alloc_urb(u->packets, GFP_KERNEL); | ||
1487 | if (!u->urb) | ||
1488 | goto out_of_memory; | ||
1489 | |||
1490 | u->urb->transfer_buffer = | ||
1491 | usb_alloc_coherent(ep->chip->dev, u->buffer_size, | ||
1492 | GFP_KERNEL, &u->urb->transfer_dma); | ||
1493 | if (!u->urb->transfer_buffer) | ||
1494 | goto out_of_memory; | ||
1495 | u->urb->pipe = ep->pipe; | ||
1496 | u->urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; | ||
1497 | u->urb->interval = 1 << ep->datainterval; | ||
1498 | u->urb->context = u; | ||
1499 | u->urb->complete = snd_complete_urb; | ||
1500 | INIT_LIST_HEAD(&u->ready_list); | ||
1501 | } | ||
1502 | |||
1503 | return 0; | ||
1504 | |||
1505 | out_of_memory: | ||
1506 | release_urbs(ep, 0); | ||
1507 | return -ENOMEM; | ||
1508 | } | ||
1509 | |||
1510 | static int sync_ep_set_params(struct snd_usb_endpoint *ep, | ||
1511 | struct snd_pcm_hw_params *hw_params, | ||
1512 | struct audioformat *fmt) | ||
1513 | { | ||
1514 | int i; | ||
1515 | |||
1516 | ep->syncbuf = usb_alloc_coherent(ep->chip->dev, SYNC_URBS * 4, | ||
1517 | GFP_KERNEL, &ep->sync_dma); | ||
1518 | if (!ep->syncbuf) | ||
1519 | return -ENOMEM; | ||
1520 | |||
1521 | for (i = 0; i < SYNC_URBS; i++) { | ||
1522 | struct snd_urb_ctx *u = &ep->urb[i]; | ||
1523 | u->index = i; | ||
1524 | u->ep = ep; | ||
1525 | u->packets = 1; | ||
1526 | u->urb = usb_alloc_urb(1, GFP_KERNEL); | ||
1527 | if (!u->urb) | ||
1528 | goto out_of_memory; | ||
1529 | u->urb->transfer_buffer = ep->syncbuf + i * 4; | ||
1530 | u->urb->transfer_dma = ep->sync_dma + i * 4; | ||
1531 | u->urb->transfer_buffer_length = 4; | ||
1532 | u->urb->pipe = ep->pipe; | ||
1533 | u->urb->transfer_flags = URB_ISO_ASAP | | ||
1534 | URB_NO_TRANSFER_DMA_MAP; | ||
1535 | u->urb->number_of_packets = 1; | ||
1536 | u->urb->interval = 1 << ep->syncinterval; | ||
1537 | u->urb->context = u; | ||
1538 | u->urb->complete = snd_complete_urb; | ||
1539 | } | ||
1540 | |||
1541 | ep->nurbs = SYNC_URBS; | ||
1542 | |||
1543 | return 0; | ||
1544 | |||
1545 | out_of_memory: | ||
1546 | release_urbs(ep, 0); | ||
1547 | return -ENOMEM; | ||
1548 | } | ||
1549 | |||
1550 | int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, | ||
1551 | struct snd_pcm_hw_params *hw_params, | ||
1552 | struct audioformat *fmt, | ||
1553 | struct snd_usb_endpoint *sync_ep) | ||
1554 | { | ||
1555 | int err; | ||
1556 | |||
1557 | if (ep->use_count != 0) { | ||
1558 | snd_printk(KERN_WARNING "Unable to change format on ep #%x: already in use\n", | ||
1559 | ep->ep_num); | ||
1560 | return -EBUSY; | ||
1561 | } | ||
1562 | |||
1563 | /* release old buffers, if any */ | ||
1564 | release_urbs(ep, 0); | ||
1565 | |||
1566 | ep->datainterval = fmt->datainterval; | ||
1567 | ep->maxpacksize = fmt->maxpacksize; | ||
1568 | ep->fill_max = fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX; | ||
1569 | |||
1570 | if (snd_usb_get_speed(ep->chip->dev) == USB_SPEED_FULL) | ||
1571 | ep->freqn = get_usb_full_speed_rate(params_rate(hw_params)); | ||
1572 | else | ||
1573 | ep->freqn = get_usb_high_speed_rate(params_rate(hw_params)); | ||
1574 | |||
1575 | /* calculate the frequency in 16.16 format */ | ||
1576 | ep->freqm = ep->freqn; | ||
1577 | ep->freqshift = INT_MIN; | ||
1578 | |||
1579 | ep->phase = 0; | ||
1580 | |||
1581 | switch (ep->type) { | ||
1582 | case SND_USB_ENDPOINT_TYPE_DATA: | ||
1583 | err = data_ep_set_params(ep, hw_params, fmt, sync_ep); | ||
1584 | break; | ||
1585 | case SND_USB_ENDPOINT_TYPE_SYNC: | ||
1586 | err = sync_ep_set_params(ep, hw_params, fmt); | ||
1587 | break; | ||
1588 | default: | ||
1589 | err = -EINVAL; | ||
1590 | } | ||
1591 | |||
1592 | snd_printdd(KERN_DEBUG "Setting params for ep #%x (type %d, %d urbs), ret=%d\n", | ||
1593 | ep->ep_num, ep->type, ep->nurbs, err); | ||
1594 | |||
1595 | return err; | ||
1596 | } | ||
1597 | |||
1598 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep) | ||
1599 | { | ||
1600 | int err; | ||
1601 | unsigned int i; | ||
1602 | |||
1603 | if (ep->chip->shutdown) | ||
1604 | return -EBADFD; | ||
1605 | |||
1606 | /* already running? */ | ||
1607 | if (++ep->use_count != 1) | ||
1608 | return 0; | ||
1609 | |||
1610 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) | ||
1611 | return -EINVAL; | ||
1612 | |||
1613 | /* just to be sure */ | ||
1614 | deactivate_urbs(ep, 0, 1); | ||
1615 | wait_clear_urbs(ep); | ||
1616 | |||
1617 | ep->active_mask = 0; | ||
1618 | ep->unlink_mask = 0; | ||
1619 | ep->phase = 0; | ||
1620 | |||
1621 | /* | ||
1622 | * If this endpoint has a data endpoint as implicit feedback source, | ||
1623 | * don't start the urbs here. Instead, mark them all as available, | ||
1624 | * wait for the record urbs to arrive and queue from that context. | ||
1625 | */ | ||
1626 | |||
1627 | set_bit(EP_FLAG_RUNNING, &ep->flags); | ||
1628 | |||
1629 | if (snd_usb_endpoint_implict_feedback_sink(ep)) { | ||
1630 | for (i = 0; i < ep->nurbs; i++) { | ||
1631 | struct snd_urb_ctx *ctx = ep->urb + i; | ||
1632 | list_add_tail(&ctx->ready_list, &ep->ready_playback_urbs); | ||
1633 | } | ||
1634 | |||
1635 | return 0; | ||
1636 | } | ||
1637 | |||
1638 | for (i = 0; i < ep->nurbs; i++) { | ||
1639 | struct urb *urb = ep->urb[i].urb; | ||
1640 | |||
1641 | if (snd_BUG_ON(!urb)) | ||
1642 | goto __error; | ||
1643 | |||
1644 | if (usb_pipeout(ep->pipe)) { | ||
1645 | prepare_outbound_urb_sizes(ep, urb->context); | ||
1646 | prepare_outbound_urb(ep, urb->context); | ||
1647 | } else { | ||
1648 | prepare_inbound_urb(ep, urb->context); | ||
1649 | } | ||
1650 | |||
1651 | err = usb_submit_urb(urb, GFP_ATOMIC); | ||
1652 | if (err < 0) { | ||
1653 | snd_printk(KERN_ERR "cannot submit urb %d, error %d: %s\n", | ||
1654 | i, err, usb_error_string(err)); | ||
1655 | goto __error; | ||
1656 | } | ||
1657 | set_bit(i, &ep->active_mask); | ||
1658 | } | ||
1659 | |||
1660 | return 0; | ||
1661 | |||
1662 | __error: | ||
1663 | clear_bit(EP_FLAG_RUNNING, &ep->flags); | ||
1664 | ep->use_count--; | ||
1665 | deactivate_urbs(ep, 0, 0); | ||
1666 | return -EPIPE; | ||
1667 | } | ||
1668 | |||
1669 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, | ||
1670 | int force, int can_sleep, int wait) | ||
1671 | { | ||
1672 | if (!ep) | ||
1673 | return; | ||
1674 | |||
1675 | if (snd_BUG_ON(ep->use_count == 0)) | ||
1676 | return; | ||
1677 | |||
1678 | if (snd_BUG_ON(!test_bit(EP_FLAG_ACTIVATED, &ep->flags))) | ||
1679 | return; | ||
1680 | |||
1681 | if (--ep->use_count == 0) { | ||
1682 | deactivate_urbs(ep, force, can_sleep); | ||
1683 | ep->data_subs = NULL; | ||
1684 | ep->sync_slave = NULL; | ||
1685 | ep->retire_data_urb = NULL; | ||
1686 | ep->prepare_data_urb = NULL; | ||
1687 | |||
1688 | if (wait) | ||
1689 | wait_clear_urbs(ep); | ||
1690 | } | ||
1691 | } | ||
1692 | |||
1693 | int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep) | ||
1694 | { | ||
1695 | if (ep->use_count != 0) | ||
1696 | return 0; | ||
1697 | |||
1698 | if (!ep->chip->shutdown && | ||
1699 | !test_and_set_bit(EP_FLAG_ACTIVATED, &ep->flags)) { | ||
1700 | int ret; | ||
1701 | |||
1702 | ret = usb_set_interface(ep->chip->dev, ep->iface, ep->alt_idx); | ||
1703 | if (ret < 0) { | ||
1704 | snd_printk(KERN_ERR "%s() usb_set_interface() failed, ret = %d\n", | ||
1705 | __func__, ret); | ||
1706 | clear_bit(EP_FLAG_ACTIVATED, &ep->flags); | ||
1707 | return ret; | ||
1708 | } | ||
1709 | |||
1710 | return 0; | ||
1711 | } | ||
1712 | |||
1713 | return -EBUSY; | ||
1714 | } | ||
1715 | |||
1716 | int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep) | ||
1717 | { | ||
1718 | if (!ep) | ||
1719 | return -EINVAL; | ||
1720 | |||
1721 | if (ep->use_count != 0) | ||
1722 | return 0; | ||
1723 | |||
1724 | if (!ep->chip->shutdown && | ||
1725 | test_and_clear_bit(EP_FLAG_ACTIVATED, &ep->flags)) { | ||
1726 | int ret; | ||
1727 | |||
1728 | ret = usb_set_interface(ep->chip->dev, ep->iface, 0); | ||
1729 | if (ret < 0) { | ||
1730 | snd_printk(KERN_ERR "%s(): usb_set_interface() failed, ret = %d\n", | ||
1731 | __func__, ret); | ||
1732 | return ret; | ||
1733 | } | ||
1734 | |||
1735 | return 0; | ||
1736 | } | ||
1737 | |||
1738 | return -EBUSY; | ||
1739 | } | ||
1740 | |||
1741 | void snd_usb_endpoint_free(struct list_head *head) | ||
1742 | { | ||
1743 | struct snd_usb_endpoint *ep; | ||
1744 | |||
1745 | ep = list_entry(head, struct snd_usb_endpoint, list); | ||
1746 | release_urbs(ep, 1); | ||
1747 | kfree(ep); | ||
1748 | } | ||
1749 | |||
1750 | /* | ||
1751 | * process after playback sync complete | ||
1752 | * | ||
1753 | * Full speed devices report feedback values in 10.14 format as samples per | ||
1754 | * frame, high speed devices in 16.16 format as samples per microframe. | ||
1755 | * Because the Audio Class 1 spec was written before USB 2.0, many high speed | ||
1756 | * devices use a wrong interpretation, some others use an entirely different | ||
1757 | * format. Therefore, we cannot predict what format any particular device uses | ||
1758 | * and must detect it automatically. | ||
1759 | */ | ||
1760 | void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, | ||
1761 | struct snd_usb_endpoint *sender, | ||
1762 | const struct urb *urb) | ||
1763 | { | ||
1764 | int shift; | ||
1765 | unsigned int f; | ||
1766 | unsigned long flags; | ||
1767 | |||
1768 | snd_BUG_ON(ep == sender); | ||
1769 | |||
1770 | if (snd_usb_endpoint_implict_feedback_sink(ep) && | ||
1771 | ep->use_count != 0) { | ||
1772 | |||
1773 | /* implicit feedback case */ | ||
1774 | int i, bytes = 0; | ||
1775 | struct snd_urb_ctx *in_ctx; | ||
1776 | struct snd_usb_packet_info *out_packet; | ||
1777 | |||
1778 | in_ctx = urb->context; | ||
1779 | |||
1780 | /* Count overall packet size */ | ||
1781 | for (i = 0; i < in_ctx->packets; i++) | ||
1782 | if (urb->iso_frame_desc[i].status == 0) | ||
1783 | bytes += urb->iso_frame_desc[i].actual_length; | ||
1784 | |||
1785 | /* | ||
1786 | * skip empty packets. At least M-Audio's Fast Track Ultra stops | ||
1787 | * streaming once it received a 0-byte OUT URB | ||
1788 | */ | ||
1789 | if (bytes == 0) | ||
1790 | return; | ||
1791 | |||
1792 | spin_lock_irqsave(&ep->lock, flags); | ||
1793 | out_packet = ep->next_packet + ep->next_packet_write_pos; | ||
1794 | |||
1795 | /* | ||
1796 | * Iterate through the inbound packet and prepare the lengths | ||
1797 | * for the output packet. The OUT packet we are about to send | ||
1798 | * will have the same amount of payload than the IN packet we | ||
1799 | * just received. | ||
1800 | */ | ||
1801 | |||
1802 | out_packet->packets = in_ctx->packets; | ||
1803 | for (i = 0; i < in_ctx->packets; i++) { | ||
1804 | if (urb->iso_frame_desc[i].status == 0) | ||
1805 | out_packet->packet_size[i] = | ||
1806 | urb->iso_frame_desc[i].actual_length / ep->stride; | ||
1807 | else | ||
1808 | out_packet->packet_size[i] = 0; | ||
1809 | } | ||
1810 | |||
1811 | ep->next_packet_write_pos++; | ||
1812 | ep->next_packet_write_pos %= MAX_URBS; | ||
1813 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1814 | queue_pending_output_urbs(ep); | ||
1815 | |||
1816 | return; | ||
1817 | } | ||
1818 | |||
1819 | /* parse sync endpoint packet */ | ||
1820 | |||
1821 | if (urb->iso_frame_desc[0].status != 0 || | ||
1822 | urb->iso_frame_desc[0].actual_length < 3) | ||
1823 | return; | ||
1824 | |||
1825 | f = le32_to_cpup(urb->transfer_buffer); | ||
1826 | if (urb->iso_frame_desc[0].actual_length == 3) | ||
1827 | f &= 0x00ffffff; | ||
1828 | else | ||
1829 | f &= 0x0fffffff; | ||
1830 | |||
1831 | if (f == 0) | ||
1832 | return; | ||
1833 | |||
1834 | if (unlikely(ep->freqshift == INT_MIN)) { | ||
1835 | /* | ||
1836 | * The first time we see a feedback value, determine its format | ||
1837 | * by shifting it left or right until it matches the nominal | ||
1838 | * frequency value. This assumes that the feedback does not | ||
1839 | * differ from the nominal value more than +50% or -25%. | ||
1840 | */ | ||
1841 | shift = 0; | ||
1842 | while (f < ep->freqn - ep->freqn / 4) { | ||
1843 | f <<= 1; | ||
1844 | shift++; | ||
1845 | } | ||
1846 | while (f > ep->freqn + ep->freqn / 2) { | ||
1847 | f >>= 1; | ||
1848 | shift--; | ||
1849 | } | ||
1850 | ep->freqshift = shift; | ||
1851 | } else if (ep->freqshift >= 0) | ||
1852 | f <<= ep->freqshift; | ||
1853 | else | ||
1854 | f >>= -ep->freqshift; | ||
1855 | |||
1856 | if (likely(f >= ep->freqn - ep->freqn / 8 && f <= ep->freqmax)) { | ||
1857 | /* | ||
1858 | * If the frequency looks valid, set it. | ||
1859 | * This value is referred to in prepare_playback_urb(). | ||
1860 | */ | ||
1861 | spin_lock_irqsave(&ep->lock, flags); | ||
1862 | ep->freqm = f; | ||
1863 | spin_unlock_irqrestore(&ep->lock, flags); | ||
1864 | } else { | ||
1865 | /* | ||
1866 | * Out of range; maybe the shift value is wrong. | ||
1867 | * Reset it so that we autodetect again the next time. | ||
1868 | */ | ||
1869 | ep->freqshift = INT_MIN; | ||
1870 | } | ||
1871 | } | ||
1872 | |||
diff --git a/sound/usb/endpoint.h b/sound/usb/endpoint.h index 88eb63a636e..9f083d7d6cf 100644 --- a/sound/usb/endpoint.h +++ b/sound/usb/endpoint.h | |||
@@ -18,4 +18,30 @@ int snd_usb_substream_prepare(struct snd_usb_substream *subs, | |||
18 | int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd); | 18 | int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, int cmd); |
19 | int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd); | 19 | int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, int cmd); |
20 | 20 | ||
21 | |||
22 | #define SND_USB_ENDPOINT_TYPE_DATA 0 | ||
23 | #define SND_USB_ENDPOINT_TYPE_SYNC 1 | ||
24 | |||
25 | struct snd_usb_endpoint *snd_usb_add_endpoint(struct snd_usb_audio *chip, | ||
26 | struct usb_host_interface *alts, | ||
27 | int ep_num, int direction, int type); | ||
28 | |||
29 | int snd_usb_endpoint_set_params(struct snd_usb_endpoint *ep, | ||
30 | struct snd_pcm_hw_params *hw_params, | ||
31 | struct audioformat *fmt, | ||
32 | struct snd_usb_endpoint *sync_ep); | ||
33 | |||
34 | int snd_usb_endpoint_start(struct snd_usb_endpoint *ep); | ||
35 | void snd_usb_endpoint_stop(struct snd_usb_endpoint *ep, | ||
36 | int force, int can_sleep, int wait); | ||
37 | int snd_usb_endpoint_activate(struct snd_usb_endpoint *ep); | ||
38 | int snd_usb_endpoint_deactivate(struct snd_usb_endpoint *ep); | ||
39 | void snd_usb_endpoint_free(struct list_head *head); | ||
40 | |||
41 | int snd_usb_endpoint_implict_feedback_sink(struct snd_usb_endpoint *ep); | ||
42 | |||
43 | void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep, | ||
44 | struct snd_usb_endpoint *sender, | ||
45 | const struct urb *urb); | ||
46 | |||
21 | #endif /* __USBAUDIO_ENDPOINT_H */ | 47 | #endif /* __USBAUDIO_ENDPOINT_H */ |
diff --git a/sound/usb/usbaudio.h b/sound/usb/usbaudio.h index a16c21d2d7e..b8233ebe250 100644 --- a/sound/usb/usbaudio.h +++ b/sound/usb/usbaudio.h | |||
@@ -47,6 +47,7 @@ struct snd_usb_audio { | |||
47 | int num_suspended_intf; | 47 | int num_suspended_intf; |
48 | 48 | ||
49 | struct list_head pcm_list; /* list of pcm streams */ | 49 | struct list_head pcm_list; /* list of pcm streams */ |
50 | struct list_head ep_list; /* list of audio-related endpoints */ | ||
50 | int pcm_devs; | 51 | int pcm_devs; |
51 | 52 | ||
52 | struct list_head midi_list; /* list of midi interfaces */ | 53 | struct list_head midi_list; /* list of midi interfaces */ |