]>
Commit | Line | Data |
---|---|---|
e5779998 DM |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License as published by | |
4 | * the Free Software Foundation; either version 2 of the License, or | |
5 | * (at your option) any later version. | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
15 | */ | |
16 | ||
17 | #include <linux/init.h> | |
9966ddaf | 18 | #include <linux/slab.h> |
44dcbbb1 | 19 | #include <linux/bitrev.h> |
edcd3633 | 20 | #include <linux/ratelimit.h> |
e5779998 DM |
21 | #include <linux/usb.h> |
22 | #include <linux/usb/audio.h> | |
7e847894 | 23 | #include <linux/usb/audio-v2.h> |
e5779998 DM |
24 | |
25 | #include <sound/core.h> | |
26 | #include <sound/pcm.h> | |
27 | #include <sound/pcm_params.h> | |
28 | ||
29 | #include "usbaudio.h" | |
30 | #include "card.h" | |
31 | #include "quirks.h" | |
32 | #include "debug.h" | |
c731bc96 | 33 | #include "endpoint.h" |
e5779998 DM |
34 | #include "helper.h" |
35 | #include "pcm.h" | |
79f920fb | 36 | #include "clock.h" |
88a8516a | 37 | #include "power.h" |
e5779998 | 38 | |
edcd3633 DM |
39 | #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 |
40 | #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 | |
41 | ||
294c4fb8 PLB |
42 | /* return the estimated delay based on USB frame counters */ |
43 | snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, | |
44 | unsigned int rate) | |
45 | { | |
46 | int current_frame_number; | |
47 | int frame_diff; | |
48 | int est_delay; | |
49 | ||
48779a0b TI |
50 | if (!subs->last_delay) |
51 | return 0; /* short path */ | |
52 | ||
294c4fb8 PLB |
53 | current_frame_number = usb_get_current_frame_number(subs->dev); |
54 | /* | |
55 | * HCD implementations use different widths, use lower 8 bits. | |
56 | * The delay will be managed up to 256ms, which is more than | |
57 | * enough | |
58 | */ | |
59 | frame_diff = (current_frame_number - subs->last_frame_number) & 0xff; | |
60 | ||
61 | /* Approximation based on number of samples per USB frame (ms), | |
62 | some truncation for 44.1 but the estimate is good enough */ | |
e4cc6153 PLB |
63 | est_delay = frame_diff * rate / 1000; |
64 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) | |
65 | est_delay = subs->last_delay - est_delay; | |
66 | else | |
67 | est_delay = subs->last_delay + est_delay; | |
68 | ||
294c4fb8 PLB |
69 | if (est_delay < 0) |
70 | est_delay = 0; | |
71 | return est_delay; | |
72 | } | |
73 | ||
e5779998 DM |
74 | /* |
75 | * return the current pcm pointer. just based on the hwptr_done value. | |
76 | */ | |
77 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) | |
78 | { | |
79 | struct snd_usb_substream *subs; | |
80 | unsigned int hwptr_done; | |
81 | ||
82 | subs = (struct snd_usb_substream *)substream->runtime->private_data; | |
978520b7 TI |
83 | if (subs->stream->chip->shutdown) |
84 | return SNDRV_PCM_POS_XRUN; | |
e5779998 DM |
85 | spin_lock(&subs->lock); |
86 | hwptr_done = subs->hwptr_done; | |
e4cc6153 | 87 | substream->runtime->delay = snd_usb_pcm_delay(subs, |
294c4fb8 | 88 | substream->runtime->rate); |
e5779998 DM |
89 | spin_unlock(&subs->lock); |
90 | return hwptr_done / (substream->runtime->frame_bits >> 3); | |
91 | } | |
92 | ||
93 | /* | |
94 | * find a matching audio format | |
95 | */ | |
61a70950 | 96 | static struct audioformat *find_format(struct snd_usb_substream *subs) |
e5779998 | 97 | { |
88766f04 | 98 | struct audioformat *fp; |
e5779998 DM |
99 | struct audioformat *found = NULL; |
100 | int cur_attr = 0, attr; | |
101 | ||
88766f04 | 102 | list_for_each_entry(fp, &subs->fmt_list, list) { |
74c34ca1 | 103 | if (!(fp->formats & pcm_format_to_bits(subs->pcm_format))) |
015eb0b0 | 104 | continue; |
61a70950 | 105 | if (fp->channels != subs->channels) |
e5779998 | 106 | continue; |
61a70950 DR |
107 | if (subs->cur_rate < fp->rate_min || |
108 | subs->cur_rate > fp->rate_max) | |
e5779998 DM |
109 | continue; |
110 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { | |
111 | unsigned int i; | |
112 | for (i = 0; i < fp->nr_rates; i++) | |
61a70950 | 113 | if (fp->rate_table[i] == subs->cur_rate) |
e5779998 DM |
114 | break; |
115 | if (i >= fp->nr_rates) | |
116 | continue; | |
117 | } | |
118 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
119 | if (! found) { | |
120 | found = fp; | |
121 | cur_attr = attr; | |
122 | continue; | |
123 | } | |
124 | /* avoid async out and adaptive in if the other method | |
125 | * supports the same format. | |
126 | * this is a workaround for the case like | |
127 | * M-audio audiophile USB. | |
128 | */ | |
129 | if (attr != cur_attr) { | |
130 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && | |
131 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
132 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
133 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) | |
134 | continue; | |
135 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && | |
136 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
137 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
138 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { | |
139 | found = fp; | |
140 | cur_attr = attr; | |
141 | continue; | |
142 | } | |
143 | } | |
144 | /* find the format with the largest max. packet size */ | |
145 | if (fp->maxpacksize > found->maxpacksize) { | |
146 | found = fp; | |
147 | cur_attr = attr; | |
148 | } | |
149 | } | |
150 | return found; | |
151 | } | |
152 | ||
767d75ad DM |
153 | static int init_pitch_v1(struct snd_usb_audio *chip, int iface, |
154 | struct usb_host_interface *alts, | |
155 | struct audioformat *fmt) | |
156 | { | |
157 | struct usb_device *dev = chip->dev; | |
158 | unsigned int ep; | |
159 | unsigned char data[1]; | |
160 | int err; | |
161 | ||
162 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
163 | ||
767d75ad DM |
164 | data[0] = 1; |
165 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, | |
166 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | |
167 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, | |
17d900c4 | 168 | data, sizeof(data))) < 0) { |
767d75ad DM |
169 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", |
170 | dev->devnum, iface, ep); | |
171 | return err; | |
172 | } | |
173 | ||
174 | return 0; | |
175 | } | |
e5779998 | 176 | |
92c25611 DM |
177 | static int init_pitch_v2(struct snd_usb_audio *chip, int iface, |
178 | struct usb_host_interface *alts, | |
179 | struct audioformat *fmt) | |
180 | { | |
181 | struct usb_device *dev = chip->dev; | |
182 | unsigned char data[1]; | |
92c25611 DM |
183 | int err; |
184 | ||
92c25611 DM |
185 | data[0] = 1; |
186 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, | |
187 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, | |
188 | UAC2_EP_CS_PITCH << 8, 0, | |
17d900c4 | 189 | data, sizeof(data))) < 0) { |
92c25611 DM |
190 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n", |
191 | dev->devnum, iface, fmt->altsetting); | |
192 | return err; | |
193 | } | |
194 | ||
195 | return 0; | |
196 | } | |
197 | ||
e5779998 | 198 | /* |
92c25611 | 199 | * initialize the pitch control and sample rate |
e5779998 | 200 | */ |
767d75ad | 201 | int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, |
e5779998 DM |
202 | struct usb_host_interface *alts, |
203 | struct audioformat *fmt) | |
204 | { | |
92c25611 DM |
205 | /* if endpoint doesn't have pitch control, bail out */ |
206 | if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) | |
207 | return 0; | |
208 | ||
8f898e92 | 209 | switch (fmt->protocol) { |
767d75ad | 210 | case UAC_VERSION_1: |
a2acad82 | 211 | default: |
767d75ad DM |
212 | return init_pitch_v1(chip, iface, alts, fmt); |
213 | ||
214 | case UAC_VERSION_2: | |
92c25611 | 215 | return init_pitch_v2(chip, iface, alts, fmt); |
767d75ad | 216 | } |
767d75ad DM |
217 | } |
218 | ||
a9bb3626 | 219 | static int start_endpoints(struct snd_usb_substream *subs, bool can_sleep) |
edcd3633 DM |
220 | { |
221 | int err; | |
222 | ||
223 | if (!subs->data_endpoint) | |
224 | return -EINVAL; | |
225 | ||
226 | if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { | |
227 | struct snd_usb_endpoint *ep = subs->data_endpoint; | |
228 | ||
229 | snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep); | |
230 | ||
231 | ep->data_subs = subs; | |
015618b9 | 232 | err = snd_usb_endpoint_start(ep, can_sleep); |
edcd3633 DM |
233 | if (err < 0) { |
234 | clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); | |
235 | return err; | |
236 | } | |
237 | } | |
238 | ||
239 | if (subs->sync_endpoint && | |
240 | !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { | |
241 | struct snd_usb_endpoint *ep = subs->sync_endpoint; | |
242 | ||
2e4a263c DM |
243 | if (subs->data_endpoint->iface != subs->sync_endpoint->iface || |
244 | subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) { | |
245 | err = usb_set_interface(subs->dev, | |
246 | subs->sync_endpoint->iface, | |
247 | subs->sync_endpoint->alt_idx); | |
248 | if (err < 0) { | |
249 | snd_printk(KERN_ERR | |
250 | "%d:%d:%d: cannot set interface (%d)\n", | |
251 | subs->dev->devnum, | |
252 | subs->sync_endpoint->iface, | |
253 | subs->sync_endpoint->alt_idx, err); | |
254 | return -EIO; | |
255 | } | |
256 | } | |
257 | ||
edcd3633 DM |
258 | snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep); |
259 | ||
260 | ep->sync_slave = subs->data_endpoint; | |
015618b9 | 261 | err = snd_usb_endpoint_start(ep, can_sleep); |
edcd3633 DM |
262 | if (err < 0) { |
263 | clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); | |
264 | return err; | |
265 | } | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
a9bb3626 | 271 | static void stop_endpoints(struct snd_usb_substream *subs, bool wait) |
edcd3633 DM |
272 | { |
273 | if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) | |
b2eb950d | 274 | snd_usb_endpoint_stop(subs->sync_endpoint); |
edcd3633 DM |
275 | |
276 | if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) | |
b2eb950d TI |
277 | snd_usb_endpoint_stop(subs->data_endpoint); |
278 | ||
279 | if (wait) { | |
280 | snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint); | |
281 | snd_usb_endpoint_sync_pending_stop(subs->data_endpoint); | |
282 | } | |
edcd3633 DM |
283 | } |
284 | ||
edcd3633 DM |
285 | static int deactivate_endpoints(struct snd_usb_substream *subs) |
286 | { | |
287 | int reta, retb; | |
288 | ||
289 | reta = snd_usb_endpoint_deactivate(subs->sync_endpoint); | |
290 | retb = snd_usb_endpoint_deactivate(subs->data_endpoint); | |
291 | ||
292 | if (reta < 0) | |
293 | return reta; | |
294 | ||
295 | if (retb < 0) | |
296 | return retb; | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
ba7c2be1 CL |
301 | static int search_roland_implicit_fb(struct usb_device *dev, int ifnum, |
302 | unsigned int altsetting, | |
303 | struct usb_host_interface **alts, | |
304 | unsigned int *ep) | |
305 | { | |
306 | struct usb_interface *iface; | |
307 | struct usb_interface_descriptor *altsd; | |
308 | struct usb_endpoint_descriptor *epd; | |
309 | ||
310 | iface = usb_ifnum_to_if(dev, ifnum); | |
311 | if (!iface || iface->num_altsetting < altsetting + 1) | |
312 | return -ENOENT; | |
313 | *alts = &iface->altsetting[altsetting]; | |
314 | altsd = get_iface_desc(*alts); | |
315 | if (altsd->bAlternateSetting != altsetting || | |
316 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC || | |
317 | (altsd->bInterfaceSubClass != 2 && | |
318 | altsd->bInterfaceProtocol != 2 ) || | |
319 | altsd->bNumEndpoints < 1) | |
320 | return -ENOENT; | |
321 | epd = get_endpoint(*alts, 0); | |
322 | if (!usb_endpoint_is_isoc_in(epd) || | |
323 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != | |
324 | USB_ENDPOINT_USAGE_IMPLICIT_FB) | |
325 | return -ENOENT; | |
326 | *ep = epd->bEndpointAddress; | |
327 | return 0; | |
328 | } | |
329 | ||
a60945fd EZ |
330 | static int set_sync_ep_implicit_fb_quirk(struct snd_usb_substream *subs, |
331 | struct usb_device *dev, | |
332 | struct usb_interface_descriptor *altsd, | |
333 | unsigned int attr) | |
e5779998 | 334 | { |
a60945fd | 335 | struct usb_host_interface *alts; |
e5779998 | 336 | struct usb_interface *iface; |
a60945fd | 337 | unsigned int ep; |
c75a8a7a | 338 | |
914273c7 EZ |
339 | /* Implicit feedback sync EPs consumers are always playback EPs */ |
340 | if (subs->direction != SNDRV_PCM_STREAM_PLAYBACK) | |
341 | return 0; | |
342 | ||
c75a8a7a | 343 | switch (subs->stream->chip->usb_id) { |
ca10a7eb | 344 | case USB_ID(0x0763, 0x2030): /* M-Audio Fast Track C400 */ |
e9a25e04 | 345 | case USB_ID(0x0763, 0x2031): /* M-Audio Fast Track C600 */ |
914273c7 EZ |
346 | ep = 0x81; |
347 | iface = usb_ifnum_to_if(dev, 3); | |
ca10a7eb | 348 | |
914273c7 EZ |
349 | if (!iface || iface->num_altsetting == 0) |
350 | return -EINVAL; | |
ca10a7eb | 351 | |
914273c7 EZ |
352 | alts = &iface->altsetting[1]; |
353 | goto add_sync_ep; | |
ca10a7eb | 354 | break; |
c75a8a7a DM |
355 | case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */ |
356 | case USB_ID(0x0763, 0x2081): | |
914273c7 EZ |
357 | ep = 0x81; |
358 | iface = usb_ifnum_to_if(dev, 2); | |
c75a8a7a | 359 | |
914273c7 EZ |
360 | if (!iface || iface->num_altsetting == 0) |
361 | return -EINVAL; | |
c75a8a7a | 362 | |
914273c7 EZ |
363 | alts = &iface->altsetting[1]; |
364 | goto add_sync_ep; | |
c75a8a7a | 365 | } |
914273c7 | 366 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
ba7c2be1 CL |
367 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && |
368 | altsd->bInterfaceProtocol == 2 && | |
369 | altsd->bNumEndpoints == 1 && | |
370 | USB_ID_VENDOR(subs->stream->chip->usb_id) == 0x0582 /* Roland */ && | |
371 | search_roland_implicit_fb(dev, altsd->bInterfaceNumber + 1, | |
372 | altsd->bAlternateSetting, | |
373 | &alts, &ep) >= 0) { | |
ba7c2be1 CL |
374 | goto add_sync_ep; |
375 | } | |
edcd3633 | 376 | |
a60945fd EZ |
377 | /* No quirk */ |
378 | return 0; | |
379 | ||
380 | add_sync_ep: | |
381 | subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip, | |
382 | alts, ep, !subs->direction, | |
88abb8ef | 383 | SND_USB_ENDPOINT_TYPE_DATA); |
a60945fd EZ |
384 | if (!subs->sync_endpoint) |
385 | return -EINVAL; | |
386 | ||
387 | subs->data_endpoint->sync_master = subs->sync_endpoint; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int set_sync_endpoint(struct snd_usb_substream *subs, | |
393 | struct audioformat *fmt, | |
394 | struct usb_device *dev, | |
395 | struct usb_host_interface *alts, | |
396 | struct usb_interface_descriptor *altsd) | |
397 | { | |
398 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | |
399 | unsigned int ep, attr; | |
95fec883 | 400 | bool implicit_fb; |
a60945fd EZ |
401 | int err; |
402 | ||
403 | /* we need a sync pipe in async OUT or adaptive IN mode */ | |
404 | /* check the number of EP, since some devices have broken | |
405 | * descriptors which fool us. if it has only one EP, | |
406 | * assume it as adaptive-out or sync-in. | |
407 | */ | |
408 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
409 | ||
410 | err = set_sync_ep_implicit_fb_quirk(subs, dev, altsd, attr); | |
411 | if (err < 0) | |
412 | return err; | |
413 | ||
f34d0650 EZ |
414 | if (altsd->bNumEndpoints < 2) |
415 | return 0; | |
c75a8a7a | 416 | |
f34d0650 EZ |
417 | if ((is_playback && attr != USB_ENDPOINT_SYNC_ASYNC) || |
418 | (!is_playback && attr != USB_ENDPOINT_SYNC_ADAPTIVE)) | |
419 | return 0; | |
edcd3633 | 420 | |
f34d0650 EZ |
421 | /* check sync-pipe endpoint */ |
422 | /* ... and check descriptor size before accessing bSynchAddress | |
423 | because there is a version of the SB Audigy 2 NX firmware lacking | |
424 | the audio fields in the endpoint descriptors */ | |
425 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC || | |
426 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
95fec883 | 427 | get_endpoint(alts, 1)->bSynchAddress != 0)) { |
f34d0650 EZ |
428 | snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n", |
429 | dev->devnum, fmt->iface, fmt->altsetting, | |
430 | get_endpoint(alts, 1)->bmAttributes, | |
431 | get_endpoint(alts, 1)->bLength, | |
432 | get_endpoint(alts, 1)->bSynchAddress); | |
433 | return -EINVAL; | |
434 | } | |
435 | ep = get_endpoint(alts, 1)->bEndpointAddress; | |
95fec883 | 436 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
f34d0650 EZ |
437 | ((is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
438 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { | |
439 | snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n", | |
440 | dev->devnum, fmt->iface, fmt->altsetting, | |
441 | is_playback, ep, get_endpoint(alts, 0)->bSynchAddress); | |
442 | return -EINVAL; | |
edcd3633 | 443 | } |
e5779998 | 444 | |
f34d0650 EZ |
445 | implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK) |
446 | == USB_ENDPOINT_USAGE_IMPLICIT_FB; | |
447 | ||
448 | subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip, | |
449 | alts, ep, !subs->direction, | |
450 | implicit_fb ? | |
451 | SND_USB_ENDPOINT_TYPE_DATA : | |
452 | SND_USB_ENDPOINT_TYPE_SYNC); | |
453 | if (!subs->sync_endpoint) | |
454 | return -EINVAL; | |
455 | ||
456 | subs->data_endpoint->sync_master = subs->sync_endpoint; | |
457 | ||
71bb64c5 EZ |
458 | return 0; |
459 | } | |
460 | ||
461 | /* | |
462 | * find a matching format and set up the interface | |
463 | */ | |
464 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) | |
465 | { | |
466 | struct usb_device *dev = subs->dev; | |
467 | struct usb_host_interface *alts; | |
468 | struct usb_interface_descriptor *altsd; | |
469 | struct usb_interface *iface; | |
470 | int err; | |
471 | ||
472 | iface = usb_ifnum_to_if(dev, fmt->iface); | |
473 | if (WARN_ON(!iface)) | |
474 | return -EINVAL; | |
475 | alts = &iface->altsetting[fmt->altset_idx]; | |
476 | altsd = get_iface_desc(alts); | |
477 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) | |
478 | return -EINVAL; | |
479 | ||
480 | if (fmt == subs->cur_audiofmt) | |
481 | return 0; | |
482 | ||
483 | /* close the old interface */ | |
484 | if (subs->interface >= 0 && subs->interface != fmt->iface) { | |
485 | err = usb_set_interface(subs->dev, subs->interface, 0); | |
486 | if (err < 0) { | |
487 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n", | |
488 | dev->devnum, fmt->iface, fmt->altsetting, err); | |
489 | return -EIO; | |
490 | } | |
491 | subs->interface = -1; | |
492 | subs->altset_idx = 0; | |
493 | } | |
494 | ||
495 | /* set interface */ | |
496 | if (subs->interface != fmt->iface || | |
497 | subs->altset_idx != fmt->altset_idx) { | |
498 | err = usb_set_interface(dev, fmt->iface, fmt->altsetting); | |
499 | if (err < 0) { | |
500 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n", | |
501 | dev->devnum, fmt->iface, fmt->altsetting, err); | |
502 | return -EIO; | |
503 | } | |
504 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", | |
505 | fmt->iface, fmt->altsetting); | |
506 | subs->interface = fmt->iface; | |
507 | subs->altset_idx = fmt->altset_idx; | |
508 | ||
509 | snd_usb_set_interface_quirk(dev); | |
510 | } | |
511 | ||
512 | subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip, | |
513 | alts, fmt->endpoint, subs->direction, | |
514 | SND_USB_ENDPOINT_TYPE_DATA); | |
515 | ||
516 | if (!subs->data_endpoint) | |
517 | return -EINVAL; | |
518 | ||
519 | err = set_sync_endpoint(subs, fmt, dev, alts, altsd); | |
520 | if (err < 0) | |
521 | return err; | |
522 | ||
d133f2c2 EZ |
523 | err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt); |
524 | if (err < 0) | |
e5779998 DM |
525 | return err; |
526 | ||
527 | subs->cur_audiofmt = fmt; | |
528 | ||
529 | snd_usb_set_format_quirk(subs, fmt); | |
530 | ||
e5779998 DM |
531 | return 0; |
532 | } | |
533 | ||
0d9741c0 EZ |
534 | /* |
535 | * Return the score of matching two audioformats. | |
536 | * Veto the audioformat if: | |
537 | * - It has no channels for some reason. | |
538 | * - Requested PCM format is not supported. | |
539 | * - Requested sample rate is not supported. | |
540 | */ | |
541 | static int match_endpoint_audioformats(struct audioformat *fp, | |
542 | struct audioformat *match, int rate, | |
543 | snd_pcm_format_t pcm_format) | |
544 | { | |
545 | int i; | |
546 | int score = 0; | |
547 | ||
548 | if (fp->channels < 1) { | |
549 | snd_printdd("%s: (fmt @%p) no channels\n", __func__, fp); | |
550 | return 0; | |
551 | } | |
552 | ||
74c34ca1 | 553 | if (!(fp->formats & pcm_format_to_bits(pcm_format))) { |
0d9741c0 EZ |
554 | snd_printdd("%s: (fmt @%p) no match for format %d\n", __func__, |
555 | fp, pcm_format); | |
556 | return 0; | |
557 | } | |
558 | ||
559 | for (i = 0; i < fp->nr_rates; i++) { | |
560 | if (fp->rate_table[i] == rate) { | |
561 | score++; | |
562 | break; | |
563 | } | |
564 | } | |
565 | if (!score) { | |
566 | snd_printdd("%s: (fmt @%p) no match for rate %d\n", __func__, | |
567 | fp, rate); | |
568 | return 0; | |
569 | } | |
570 | ||
571 | if (fp->channels == match->channels) | |
572 | score++; | |
573 | ||
574 | snd_printdd("%s: (fmt @%p) score %d\n", __func__, fp, score); | |
575 | ||
576 | return score; | |
577 | } | |
578 | ||
579 | /* | |
580 | * Configure the sync ep using the rate and pcm format of the data ep. | |
581 | */ | |
582 | static int configure_sync_endpoint(struct snd_usb_substream *subs) | |
583 | { | |
584 | int ret; | |
585 | struct audioformat *fp; | |
586 | struct audioformat *sync_fp = NULL; | |
587 | int cur_score = 0; | |
588 | int sync_period_bytes = subs->period_bytes; | |
589 | struct snd_usb_substream *sync_subs = | |
590 | &subs->stream->substream[subs->direction ^ 1]; | |
591 | ||
31be5425 TI |
592 | if (subs->sync_endpoint->type != SND_USB_ENDPOINT_TYPE_DATA || |
593 | !subs->stream) | |
594 | return snd_usb_endpoint_set_params(subs->sync_endpoint, | |
595 | subs->pcm_format, | |
596 | subs->channels, | |
597 | subs->period_bytes, | |
976b6c06 | 598 | 0, 0, |
31be5425 TI |
599 | subs->cur_rate, |
600 | subs->cur_audiofmt, | |
601 | NULL); | |
602 | ||
0d9741c0 EZ |
603 | /* Try to find the best matching audioformat. */ |
604 | list_for_each_entry(fp, &sync_subs->fmt_list, list) { | |
605 | int score = match_endpoint_audioformats(fp, subs->cur_audiofmt, | |
606 | subs->cur_rate, subs->pcm_format); | |
607 | ||
608 | if (score > cur_score) { | |
609 | sync_fp = fp; | |
610 | cur_score = score; | |
611 | } | |
612 | } | |
613 | ||
614 | if (unlikely(sync_fp == NULL)) { | |
615 | snd_printk(KERN_ERR "%s: no valid audioformat for sync ep %x found\n", | |
616 | __func__, sync_subs->ep_num); | |
617 | return -EINVAL; | |
618 | } | |
619 | ||
620 | /* | |
621 | * Recalculate the period bytes if channel number differ between | |
622 | * data and sync ep audioformat. | |
623 | */ | |
624 | if (sync_fp->channels != subs->channels) { | |
625 | sync_period_bytes = (subs->period_bytes / subs->channels) * | |
626 | sync_fp->channels; | |
627 | snd_printdd("%s: adjusted sync ep period bytes (%d -> %d)\n", | |
628 | __func__, subs->period_bytes, sync_period_bytes); | |
629 | } | |
630 | ||
631 | ret = snd_usb_endpoint_set_params(subs->sync_endpoint, | |
632 | subs->pcm_format, | |
633 | sync_fp->channels, | |
634 | sync_period_bytes, | |
976b6c06 | 635 | 0, 0, |
0d9741c0 EZ |
636 | subs->cur_rate, |
637 | sync_fp, | |
638 | NULL); | |
639 | ||
640 | return ret; | |
641 | } | |
642 | ||
61a70950 DR |
643 | /* |
644 | * configure endpoint params | |
645 | * | |
646 | * called during initial setup and upon resume | |
647 | */ | |
648 | static int configure_endpoint(struct snd_usb_substream *subs) | |
649 | { | |
650 | int ret; | |
651 | ||
61a70950 | 652 | /* format changed */ |
b0db6063 | 653 | stop_endpoints(subs, true); |
61a70950 DR |
654 | ret = snd_usb_endpoint_set_params(subs->data_endpoint, |
655 | subs->pcm_format, | |
656 | subs->channels, | |
657 | subs->period_bytes, | |
976b6c06 AS |
658 | subs->period_frames, |
659 | subs->buffer_periods, | |
61a70950 DR |
660 | subs->cur_rate, |
661 | subs->cur_audiofmt, | |
662 | subs->sync_endpoint); | |
663 | if (ret < 0) | |
978520b7 | 664 | return ret; |
61a70950 DR |
665 | |
666 | if (subs->sync_endpoint) | |
0d9741c0 EZ |
667 | ret = configure_sync_endpoint(subs); |
668 | ||
61a70950 DR |
669 | return ret; |
670 | } | |
671 | ||
e5779998 DM |
672 | /* |
673 | * hw_params callback | |
674 | * | |
675 | * allocate a buffer and set the given audio format. | |
676 | * | |
677 | * so far we use a physically linear buffer although packetize transfer | |
678 | * doesn't need a continuous area. | |
679 | * if sg buffer is supported on the later version of alsa, we'll follow | |
680 | * that. | |
681 | */ | |
682 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, | |
683 | struct snd_pcm_hw_params *hw_params) | |
684 | { | |
685 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
686 | struct audioformat *fmt; | |
61a70950 | 687 | int ret; |
e5779998 DM |
688 | |
689 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
690 | params_buffer_bytes(hw_params)); | |
691 | if (ret < 0) | |
692 | return ret; | |
693 | ||
61a70950 DR |
694 | subs->pcm_format = params_format(hw_params); |
695 | subs->period_bytes = params_period_bytes(hw_params); | |
976b6c06 AS |
696 | subs->period_frames = params_period_size(hw_params); |
697 | subs->buffer_periods = params_periods(hw_params); | |
61a70950 DR |
698 | subs->channels = params_channels(hw_params); |
699 | subs->cur_rate = params_rate(hw_params); | |
700 | ||
701 | fmt = find_format(subs); | |
e5779998 DM |
702 | if (!fmt) { |
703 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", | |
61a70950 | 704 | subs->pcm_format, subs->cur_rate, subs->channels); |
e5779998 DM |
705 | return -EINVAL; |
706 | } | |
707 | ||
34f3c89f | 708 | down_read(&subs->stream->chip->shutdown_rwsem); |
978520b7 TI |
709 | if (subs->stream->chip->shutdown) |
710 | ret = -ENODEV; | |
711 | else | |
712 | ret = set_format(subs, fmt); | |
34f3c89f | 713 | up_read(&subs->stream->chip->shutdown_rwsem); |
978520b7 | 714 | if (ret < 0) |
e5779998 DM |
715 | return ret; |
716 | ||
61a70950 DR |
717 | subs->interface = fmt->iface; |
718 | subs->altset_idx = fmt->altset_idx; | |
384dc085 | 719 | subs->need_setup_ep = true; |
715a1705 | 720 | |
61a70950 | 721 | return 0; |
e5779998 DM |
722 | } |
723 | ||
724 | /* | |
725 | * hw_free callback | |
726 | * | |
727 | * reset the audio format and release the buffer | |
728 | */ | |
729 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) | |
730 | { | |
731 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
732 | ||
733 | subs->cur_audiofmt = NULL; | |
734 | subs->cur_rate = 0; | |
735 | subs->period_bytes = 0; | |
34f3c89f | 736 | down_read(&subs->stream->chip->shutdown_rwsem); |
978520b7 | 737 | if (!subs->stream->chip->shutdown) { |
a9bb3626 | 738 | stop_endpoints(subs, true); |
978520b7 TI |
739 | deactivate_endpoints(subs); |
740 | } | |
34f3c89f | 741 | up_read(&subs->stream->chip->shutdown_rwsem); |
e5779998 DM |
742 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
743 | } | |
744 | ||
745 | /* | |
746 | * prepare callback | |
747 | * | |
748 | * only a few subtle things... | |
749 | */ | |
750 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) | |
751 | { | |
752 | struct snd_pcm_runtime *runtime = substream->runtime; | |
753 | struct snd_usb_substream *subs = runtime->private_data; | |
61a70950 DR |
754 | struct usb_host_interface *alts; |
755 | struct usb_interface *iface; | |
756 | int ret; | |
e5779998 DM |
757 | |
758 | if (! subs->cur_audiofmt) { | |
759 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); | |
760 | return -ENXIO; | |
761 | } | |
762 | ||
34f3c89f | 763 | down_read(&subs->stream->chip->shutdown_rwsem); |
978520b7 TI |
764 | if (subs->stream->chip->shutdown) { |
765 | ret = -ENODEV; | |
766 | goto unlock; | |
767 | } | |
768 | if (snd_BUG_ON(!subs->data_endpoint)) { | |
769 | ret = -EIO; | |
770 | goto unlock; | |
771 | } | |
edcd3633 | 772 | |
f58161ba TI |
773 | snd_usb_endpoint_sync_pending_stop(subs->sync_endpoint); |
774 | snd_usb_endpoint_sync_pending_stop(subs->data_endpoint); | |
775 | ||
61a70950 DR |
776 | ret = set_format(subs, subs->cur_audiofmt); |
777 | if (ret < 0) | |
978520b7 | 778 | goto unlock; |
61a70950 DR |
779 | |
780 | iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); | |
781 | alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; | |
782 | ret = snd_usb_init_sample_rate(subs->stream->chip, | |
783 | subs->cur_audiofmt->iface, | |
784 | alts, | |
785 | subs->cur_audiofmt, | |
786 | subs->cur_rate); | |
787 | if (ret < 0) | |
978520b7 | 788 | goto unlock; |
61a70950 | 789 | |
384dc085 TI |
790 | if (subs->need_setup_ep) { |
791 | ret = configure_endpoint(subs); | |
792 | if (ret < 0) | |
978520b7 | 793 | goto unlock; |
384dc085 TI |
794 | subs->need_setup_ep = false; |
795 | } | |
61a70950 | 796 | |
e5779998 | 797 | /* some unit conversions in runtime */ |
edcd3633 DM |
798 | subs->data_endpoint->maxframesize = |
799 | bytes_to_frames(runtime, subs->data_endpoint->maxpacksize); | |
800 | subs->data_endpoint->curframesize = | |
801 | bytes_to_frames(runtime, subs->data_endpoint->curpacksize); | |
e5779998 DM |
802 | |
803 | /* reset the pointer */ | |
804 | subs->hwptr_done = 0; | |
805 | subs->transfer_done = 0; | |
294c4fb8 PLB |
806 | subs->last_delay = 0; |
807 | subs->last_frame_number = 0; | |
e5779998 DM |
808 | runtime->delay = 0; |
809 | ||
edcd3633 DM |
810 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
811 | * updates for all URBs would happen at the same time when starting */ | |
812 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) | |
a9bb3626 | 813 | ret = start_endpoints(subs, true); |
edcd3633 | 814 | |
978520b7 | 815 | unlock: |
34f3c89f | 816 | up_read(&subs->stream->chip->shutdown_rwsem); |
978520b7 | 817 | return ret; |
e5779998 DM |
818 | } |
819 | ||
820 | static struct snd_pcm_hardware snd_usb_hardware = | |
821 | { | |
822 | .info = SNDRV_PCM_INFO_MMAP | | |
823 | SNDRV_PCM_INFO_MMAP_VALID | | |
824 | SNDRV_PCM_INFO_BATCH | | |
825 | SNDRV_PCM_INFO_INTERLEAVED | | |
826 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
827 | SNDRV_PCM_INFO_PAUSE, | |
828 | .buffer_bytes_max = 1024 * 1024, | |
829 | .period_bytes_min = 64, | |
830 | .period_bytes_max = 512 * 1024, | |
831 | .periods_min = 2, | |
832 | .periods_max = 1024, | |
833 | }; | |
834 | ||
835 | static int hw_check_valid_format(struct snd_usb_substream *subs, | |
836 | struct snd_pcm_hw_params *params, | |
837 | struct audioformat *fp) | |
838 | { | |
839 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
840 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
841 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
842 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
015eb0b0 | 843 | struct snd_mask check_fmts; |
e5779998 DM |
844 | unsigned int ptime; |
845 | ||
846 | /* check the format */ | |
015eb0b0 CL |
847 | snd_mask_none(&check_fmts); |
848 | check_fmts.bits[0] = (u32)fp->formats; | |
849 | check_fmts.bits[1] = (u32)(fp->formats >> 32); | |
850 | snd_mask_intersect(&check_fmts, fmts); | |
851 | if (snd_mask_empty(&check_fmts)) { | |
e5779998 DM |
852 | hwc_debug(" > check: no supported format %d\n", fp->format); |
853 | return 0; | |
854 | } | |
855 | /* check the channels */ | |
856 | if (fp->channels < ct->min || fp->channels > ct->max) { | |
857 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); | |
858 | return 0; | |
859 | } | |
860 | /* check the rate is within the range */ | |
861 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { | |
862 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); | |
863 | return 0; | |
864 | } | |
865 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { | |
866 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); | |
867 | return 0; | |
868 | } | |
869 | /* check whether the period time is >= the data packet interval */ | |
978520b7 | 870 | if (subs->speed != USB_SPEED_FULL) { |
e5779998 DM |
871 | ptime = 125 * (1 << fp->datainterval); |
872 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { | |
873 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); | |
874 | return 0; | |
875 | } | |
876 | } | |
877 | return 1; | |
878 | } | |
879 | ||
880 | static int hw_rule_rate(struct snd_pcm_hw_params *params, | |
881 | struct snd_pcm_hw_rule *rule) | |
882 | { | |
883 | struct snd_usb_substream *subs = rule->private; | |
88766f04 | 884 | struct audioformat *fp; |
e5779998 DM |
885 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
886 | unsigned int rmin, rmax; | |
887 | int changed; | |
888 | ||
889 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); | |
890 | changed = 0; | |
891 | rmin = rmax = 0; | |
88766f04 | 892 | list_for_each_entry(fp, &subs->fmt_list, list) { |
e5779998 DM |
893 | if (!hw_check_valid_format(subs, params, fp)) |
894 | continue; | |
895 | if (changed++) { | |
896 | if (rmin > fp->rate_min) | |
897 | rmin = fp->rate_min; | |
898 | if (rmax < fp->rate_max) | |
899 | rmax = fp->rate_max; | |
900 | } else { | |
901 | rmin = fp->rate_min; | |
902 | rmax = fp->rate_max; | |
903 | } | |
904 | } | |
905 | ||
906 | if (!changed) { | |
907 | hwc_debug(" --> get empty\n"); | |
908 | it->empty = 1; | |
909 | return -EINVAL; | |
910 | } | |
911 | ||
912 | changed = 0; | |
913 | if (it->min < rmin) { | |
914 | it->min = rmin; | |
915 | it->openmin = 0; | |
916 | changed = 1; | |
917 | } | |
918 | if (it->max > rmax) { | |
919 | it->max = rmax; | |
920 | it->openmax = 0; | |
921 | changed = 1; | |
922 | } | |
923 | if (snd_interval_checkempty(it)) { | |
924 | it->empty = 1; | |
925 | return -EINVAL; | |
926 | } | |
927 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
928 | return changed; | |
929 | } | |
930 | ||
931 | ||
932 | static int hw_rule_channels(struct snd_pcm_hw_params *params, | |
933 | struct snd_pcm_hw_rule *rule) | |
934 | { | |
935 | struct snd_usb_substream *subs = rule->private; | |
88766f04 | 936 | struct audioformat *fp; |
e5779998 DM |
937 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
938 | unsigned int rmin, rmax; | |
939 | int changed; | |
940 | ||
941 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); | |
942 | changed = 0; | |
943 | rmin = rmax = 0; | |
88766f04 | 944 | list_for_each_entry(fp, &subs->fmt_list, list) { |
e5779998 DM |
945 | if (!hw_check_valid_format(subs, params, fp)) |
946 | continue; | |
947 | if (changed++) { | |
948 | if (rmin > fp->channels) | |
949 | rmin = fp->channels; | |
950 | if (rmax < fp->channels) | |
951 | rmax = fp->channels; | |
952 | } else { | |
953 | rmin = fp->channels; | |
954 | rmax = fp->channels; | |
955 | } | |
956 | } | |
957 | ||
958 | if (!changed) { | |
959 | hwc_debug(" --> get empty\n"); | |
960 | it->empty = 1; | |
961 | return -EINVAL; | |
962 | } | |
963 | ||
964 | changed = 0; | |
965 | if (it->min < rmin) { | |
966 | it->min = rmin; | |
967 | it->openmin = 0; | |
968 | changed = 1; | |
969 | } | |
970 | if (it->max > rmax) { | |
971 | it->max = rmax; | |
972 | it->openmax = 0; | |
973 | changed = 1; | |
974 | } | |
975 | if (snd_interval_checkempty(it)) { | |
976 | it->empty = 1; | |
977 | return -EINVAL; | |
978 | } | |
979 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
980 | return changed; | |
981 | } | |
982 | ||
983 | static int hw_rule_format(struct snd_pcm_hw_params *params, | |
984 | struct snd_pcm_hw_rule *rule) | |
985 | { | |
986 | struct snd_usb_substream *subs = rule->private; | |
88766f04 | 987 | struct audioformat *fp; |
e5779998 DM |
988 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
989 | u64 fbits; | |
990 | u32 oldbits[2]; | |
991 | int changed; | |
992 | ||
993 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); | |
994 | fbits = 0; | |
88766f04 | 995 | list_for_each_entry(fp, &subs->fmt_list, list) { |
e5779998 DM |
996 | if (!hw_check_valid_format(subs, params, fp)) |
997 | continue; | |
015eb0b0 | 998 | fbits |= fp->formats; |
e5779998 DM |
999 | } |
1000 | ||
1001 | oldbits[0] = fmt->bits[0]; | |
1002 | oldbits[1] = fmt->bits[1]; | |
1003 | fmt->bits[0] &= (u32)fbits; | |
1004 | fmt->bits[1] &= (u32)(fbits >> 32); | |
1005 | if (!fmt->bits[0] && !fmt->bits[1]) { | |
1006 | hwc_debug(" --> get empty\n"); | |
1007 | return -EINVAL; | |
1008 | } | |
1009 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); | |
1010 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); | |
1011 | return changed; | |
1012 | } | |
1013 | ||
1014 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, | |
1015 | struct snd_pcm_hw_rule *rule) | |
1016 | { | |
1017 | struct snd_usb_substream *subs = rule->private; | |
1018 | struct audioformat *fp; | |
1019 | struct snd_interval *it; | |
1020 | unsigned char min_datainterval; | |
1021 | unsigned int pmin; | |
1022 | int changed; | |
1023 | ||
1024 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
1025 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); | |
1026 | min_datainterval = 0xff; | |
1027 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
1028 | if (!hw_check_valid_format(subs, params, fp)) | |
1029 | continue; | |
1030 | min_datainterval = min(min_datainterval, fp->datainterval); | |
1031 | } | |
1032 | if (min_datainterval == 0xff) { | |
a7ce2e0d | 1033 | hwc_debug(" --> get empty\n"); |
e5779998 DM |
1034 | it->empty = 1; |
1035 | return -EINVAL; | |
1036 | } | |
1037 | pmin = 125 * (1 << min_datainterval); | |
1038 | changed = 0; | |
1039 | if (it->min < pmin) { | |
1040 | it->min = pmin; | |
1041 | it->openmin = 0; | |
1042 | changed = 1; | |
1043 | } | |
1044 | if (snd_interval_checkempty(it)) { | |
1045 | it->empty = 1; | |
1046 | return -EINVAL; | |
1047 | } | |
1048 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); | |
1049 | return changed; | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * If the device supports unusual bit rates, does the request meet these? | |
1054 | */ | |
1055 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, | |
1056 | struct snd_usb_substream *subs) | |
1057 | { | |
1058 | struct audioformat *fp; | |
0717d0f5 | 1059 | int *rate_list; |
e5779998 DM |
1060 | int count = 0, needs_knot = 0; |
1061 | int err; | |
1062 | ||
5cd5d7c4 CL |
1063 | kfree(subs->rate_list.list); |
1064 | subs->rate_list.list = NULL; | |
1065 | ||
e5779998 DM |
1066 | list_for_each_entry(fp, &subs->fmt_list, list) { |
1067 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) | |
1068 | return 0; | |
1069 | count += fp->nr_rates; | |
1070 | if (fp->rates & SNDRV_PCM_RATE_KNOT) | |
1071 | needs_knot = 1; | |
1072 | } | |
1073 | if (!needs_knot) | |
1074 | return 0; | |
1075 | ||
0717d0f5 TI |
1076 | subs->rate_list.list = rate_list = |
1077 | kmalloc(sizeof(int) * count, GFP_KERNEL); | |
8a8d56b2 JJ |
1078 | if (!subs->rate_list.list) |
1079 | return -ENOMEM; | |
1080 | subs->rate_list.count = count; | |
e5779998 DM |
1081 | subs->rate_list.mask = 0; |
1082 | count = 0; | |
1083 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
1084 | int i; | |
1085 | for (i = 0; i < fp->nr_rates; i++) | |
0717d0f5 | 1086 | rate_list[count++] = fp->rate_table[i]; |
e5779998 DM |
1087 | } |
1088 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
1089 | &subs->rate_list); | |
1090 | if (err < 0) | |
1091 | return err; | |
1092 | ||
1093 | return 0; | |
1094 | } | |
1095 | ||
1096 | ||
1097 | /* | |
1098 | * set up the runtime hardware information. | |
1099 | */ | |
1100 | ||
1101 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) | |
1102 | { | |
88766f04 | 1103 | struct audioformat *fp; |
e5779998 DM |
1104 | unsigned int pt, ptmin; |
1105 | int param_period_time_if_needed; | |
1106 | int err; | |
1107 | ||
1108 | runtime->hw.formats = subs->formats; | |
1109 | ||
1110 | runtime->hw.rate_min = 0x7fffffff; | |
1111 | runtime->hw.rate_max = 0; | |
1112 | runtime->hw.channels_min = 256; | |
1113 | runtime->hw.channels_max = 0; | |
1114 | runtime->hw.rates = 0; | |
1115 | ptmin = UINT_MAX; | |
1116 | /* check min/max rates and channels */ | |
88766f04 | 1117 | list_for_each_entry(fp, &subs->fmt_list, list) { |
e5779998 DM |
1118 | runtime->hw.rates |= fp->rates; |
1119 | if (runtime->hw.rate_min > fp->rate_min) | |
1120 | runtime->hw.rate_min = fp->rate_min; | |
1121 | if (runtime->hw.rate_max < fp->rate_max) | |
1122 | runtime->hw.rate_max = fp->rate_max; | |
1123 | if (runtime->hw.channels_min > fp->channels) | |
1124 | runtime->hw.channels_min = fp->channels; | |
1125 | if (runtime->hw.channels_max < fp->channels) | |
1126 | runtime->hw.channels_max = fp->channels; | |
1127 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { | |
1128 | /* FIXME: there might be more than one audio formats... */ | |
1129 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = | |
1130 | fp->frame_size; | |
1131 | } | |
1132 | pt = 125 * (1 << fp->datainterval); | |
1133 | ptmin = min(ptmin, pt); | |
1134 | } | |
88a8516a ON |
1135 | err = snd_usb_autoresume(subs->stream->chip); |
1136 | if (err < 0) | |
1137 | return err; | |
e5779998 DM |
1138 | |
1139 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; | |
978520b7 | 1140 | if (subs->speed == USB_SPEED_FULL) |
e5779998 DM |
1141 | /* full speed devices have fixed data packet interval */ |
1142 | ptmin = 1000; | |
1143 | if (ptmin == 1000) | |
1144 | /* if period time doesn't go below 1 ms, no rules needed */ | |
1145 | param_period_time_if_needed = -1; | |
1146 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
1147 | ptmin, UINT_MAX); | |
1148 | ||
1149 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
1150 | hw_rule_rate, subs, | |
1151 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1152 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
1153 | param_period_time_if_needed, | |
1154 | -1)) < 0) | |
88a8516a | 1155 | goto rep_err; |
e5779998 DM |
1156 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
1157 | hw_rule_channels, subs, | |
1158 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1159 | SNDRV_PCM_HW_PARAM_RATE, | |
1160 | param_period_time_if_needed, | |
1161 | -1)) < 0) | |
88a8516a | 1162 | goto rep_err; |
e5779998 DM |
1163 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
1164 | hw_rule_format, subs, | |
1165 | SNDRV_PCM_HW_PARAM_RATE, | |
1166 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
1167 | param_period_time_if_needed, | |
1168 | -1)) < 0) | |
88a8516a | 1169 | goto rep_err; |
e5779998 DM |
1170 | if (param_period_time_if_needed >= 0) { |
1171 | err = snd_pcm_hw_rule_add(runtime, 0, | |
1172 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
1173 | hw_rule_period_time, subs, | |
1174 | SNDRV_PCM_HW_PARAM_FORMAT, | |
1175 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
1176 | SNDRV_PCM_HW_PARAM_RATE, | |
1177 | -1); | |
1178 | if (err < 0) | |
88a8516a | 1179 | goto rep_err; |
e5779998 DM |
1180 | } |
1181 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) | |
88a8516a | 1182 | goto rep_err; |
e5779998 | 1183 | return 0; |
88a8516a ON |
1184 | |
1185 | rep_err: | |
1186 | snd_usb_autosuspend(subs->stream->chip); | |
1187 | return err; | |
e5779998 DM |
1188 | } |
1189 | ||
1190 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) | |
1191 | { | |
1192 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
1193 | struct snd_pcm_runtime *runtime = substream->runtime; | |
1194 | struct snd_usb_substream *subs = &as->substream[direction]; | |
1195 | ||
1196 | subs->interface = -1; | |
e11b4e0e | 1197 | subs->altset_idx = 0; |
e5779998 DM |
1198 | runtime->hw = snd_usb_hardware; |
1199 | runtime->private_data = subs; | |
1200 | subs->pcm_substream = substream; | |
88a8516a | 1201 | /* runtime PM is also done there */ |
d24f5061 DM |
1202 | |
1203 | /* initialize DSD/DOP context */ | |
1204 | subs->dsd_dop.byte_idx = 0; | |
1205 | subs->dsd_dop.channel = 0; | |
1206 | subs->dsd_dop.marker = 1; | |
1207 | ||
e5779998 DM |
1208 | return setup_hw_info(runtime, subs); |
1209 | } | |
1210 | ||
1211 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) | |
1212 | { | |
1213 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
1214 | struct snd_usb_substream *subs = &as->substream[direction]; | |
1215 | ||
b0db6063 | 1216 | stop_endpoints(subs, true); |
68e67f40 DM |
1217 | |
1218 | if (!as->chip->shutdown && subs->interface >= 0) { | |
1219 | usb_set_interface(subs->dev, subs->interface, 0); | |
1220 | subs->interface = -1; | |
1221 | } | |
1222 | ||
e5779998 | 1223 | subs->pcm_substream = NULL; |
88a8516a | 1224 | snd_usb_autosuspend(subs->stream->chip); |
edcd3633 | 1225 | |
68e67f40 | 1226 | return 0; |
edcd3633 DM |
1227 | } |
1228 | ||
1229 | /* Since a URB can handle only a single linear buffer, we must use double | |
1230 | * buffering when the data to be transferred overflows the buffer boundary. | |
1231 | * To avoid inconsistencies when updating hwptr_done, we use double buffering | |
1232 | * for all URBs. | |
1233 | */ | |
1234 | static void retire_capture_urb(struct snd_usb_substream *subs, | |
1235 | struct urb *urb) | |
1236 | { | |
1237 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
1238 | unsigned int stride, frames, bytes, oldptr; | |
1239 | int i, period_elapsed = 0; | |
1240 | unsigned long flags; | |
1241 | unsigned char *cp; | |
e4cc6153 PLB |
1242 | int current_frame_number; |
1243 | ||
1244 | /* read frame number here, update pointer in critical section */ | |
1245 | current_frame_number = usb_get_current_frame_number(subs->dev); | |
edcd3633 DM |
1246 | |
1247 | stride = runtime->frame_bits >> 3; | |
1248 | ||
1249 | for (i = 0; i < urb->number_of_packets; i++) { | |
1539d4f8 | 1250 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset + subs->pkt_offset_adj; |
edcd3633 DM |
1251 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { |
1252 | snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status); | |
1253 | // continue; | |
1254 | } | |
1255 | bytes = urb->iso_frame_desc[i].actual_length; | |
1256 | frames = bytes / stride; | |
1257 | if (!subs->txfr_quirk) | |
1258 | bytes = frames * stride; | |
1259 | if (bytes % (runtime->sample_bits >> 3) != 0) { | |
edcd3633 | 1260 | int oldbytes = bytes; |
edcd3633 DM |
1261 | bytes = frames * stride; |
1262 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", | |
1263 | oldbytes, bytes); | |
1264 | } | |
1265 | /* update the current pointer */ | |
1266 | spin_lock_irqsave(&subs->lock, flags); | |
1267 | oldptr = subs->hwptr_done; | |
1268 | subs->hwptr_done += bytes; | |
1269 | if (subs->hwptr_done >= runtime->buffer_size * stride) | |
1270 | subs->hwptr_done -= runtime->buffer_size * stride; | |
1271 | frames = (bytes + (oldptr % stride)) / stride; | |
1272 | subs->transfer_done += frames; | |
1273 | if (subs->transfer_done >= runtime->period_size) { | |
1274 | subs->transfer_done -= runtime->period_size; | |
1275 | period_elapsed = 1; | |
1276 | } | |
e4cc6153 PLB |
1277 | /* capture delay is by construction limited to one URB, |
1278 | * reset delays here | |
1279 | */ | |
1280 | runtime->delay = subs->last_delay = 0; | |
1281 | ||
1282 | /* realign last_frame_number */ | |
1283 | subs->last_frame_number = current_frame_number; | |
1284 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ | |
1285 | ||
edcd3633 DM |
1286 | spin_unlock_irqrestore(&subs->lock, flags); |
1287 | /* copy a data chunk */ | |
1288 | if (oldptr + bytes > runtime->buffer_size * stride) { | |
1289 | unsigned int bytes1 = | |
1290 | runtime->buffer_size * stride - oldptr; | |
1291 | memcpy(runtime->dma_area + oldptr, cp, bytes1); | |
1292 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); | |
1293 | } else { | |
1294 | memcpy(runtime->dma_area + oldptr, cp, bytes); | |
1295 | } | |
1296 | } | |
1297 | ||
1298 | if (period_elapsed) | |
1299 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1300 | } | |
1301 | ||
d24f5061 DM |
1302 | static inline void fill_playback_urb_dsd_dop(struct snd_usb_substream *subs, |
1303 | struct urb *urb, unsigned int bytes) | |
1304 | { | |
1305 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
1306 | unsigned int stride = runtime->frame_bits >> 3; | |
1307 | unsigned int dst_idx = 0; | |
1308 | unsigned int src_idx = subs->hwptr_done; | |
1309 | unsigned int wrap = runtime->buffer_size * stride; | |
1310 | u8 *dst = urb->transfer_buffer; | |
1311 | u8 *src = runtime->dma_area; | |
1312 | u8 marker[] = { 0x05, 0xfa }; | |
1313 | ||
1314 | /* | |
1315 | * The DSP DOP format defines a way to transport DSD samples over | |
1316 | * normal PCM data endpoints. It requires stuffing of marker bytes | |
1317 | * (0x05 and 0xfa, alternating per sample frame), and then expects | |
1318 | * 2 additional bytes of actual payload. The whole frame is stored | |
1319 | * LSB. | |
1320 | * | |
1321 | * Hence, for a stereo transport, the buffer layout looks like this, | |
1322 | * where L refers to left channel samples and R to right. | |
1323 | * | |
1324 | * L1 L2 0x05 R1 R2 0x05 L3 L4 0xfa R3 R4 0xfa | |
1325 | * L5 L6 0x05 R5 R6 0x05 L7 L8 0xfa R7 R8 0xfa | |
1326 | * ..... | |
1327 | * | |
1328 | */ | |
1329 | ||
1330 | while (bytes--) { | |
1331 | if (++subs->dsd_dop.byte_idx == 3) { | |
1332 | /* frame boundary? */ | |
1333 | dst[dst_idx++] = marker[subs->dsd_dop.marker]; | |
1334 | src_idx += 2; | |
1335 | subs->dsd_dop.byte_idx = 0; | |
1336 | ||
1337 | if (++subs->dsd_dop.channel % runtime->channels == 0) { | |
1338 | /* alternate the marker */ | |
1339 | subs->dsd_dop.marker++; | |
1340 | subs->dsd_dop.marker %= ARRAY_SIZE(marker); | |
1341 | subs->dsd_dop.channel = 0; | |
1342 | } | |
1343 | } else { | |
1344 | /* stuff the DSD payload */ | |
1345 | int idx = (src_idx + subs->dsd_dop.byte_idx - 1) % wrap; | |
44dcbbb1 DM |
1346 | |
1347 | if (subs->cur_audiofmt->dsd_bitrev) | |
1348 | dst[dst_idx++] = bitrev8(src[idx]); | |
1349 | else | |
1350 | dst[dst_idx++] = src[idx]; | |
1351 | ||
d24f5061 DM |
1352 | subs->hwptr_done++; |
1353 | } | |
1354 | } | |
1355 | } | |
1356 | ||
edcd3633 DM |
1357 | static void prepare_playback_urb(struct snd_usb_substream *subs, |
1358 | struct urb *urb) | |
1359 | { | |
1360 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
245baf98 | 1361 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
edcd3633 DM |
1362 | struct snd_urb_ctx *ctx = urb->context; |
1363 | unsigned int counts, frames, bytes; | |
1364 | int i, stride, period_elapsed = 0; | |
1365 | unsigned long flags; | |
1366 | ||
1367 | stride = runtime->frame_bits >> 3; | |
1368 | ||
1369 | frames = 0; | |
1370 | urb->number_of_packets = 0; | |
1371 | spin_lock_irqsave(&subs->lock, flags); | |
976b6c06 | 1372 | subs->frame_limit += ep->max_urb_frames; |
edcd3633 | 1373 | for (i = 0; i < ctx->packets; i++) { |
245baf98 DM |
1374 | if (ctx->packet_size[i]) |
1375 | counts = ctx->packet_size[i]; | |
1376 | else | |
1377 | counts = snd_usb_endpoint_next_packet_size(ep); | |
1378 | ||
edcd3633 | 1379 | /* set up descriptor */ |
8a2a74d2 DM |
1380 | urb->iso_frame_desc[i].offset = frames * ep->stride; |
1381 | urb->iso_frame_desc[i].length = counts * ep->stride; | |
edcd3633 DM |
1382 | frames += counts; |
1383 | urb->number_of_packets++; | |
1384 | subs->transfer_done += counts; | |
1385 | if (subs->transfer_done >= runtime->period_size) { | |
1386 | subs->transfer_done -= runtime->period_size; | |
976b6c06 | 1387 | subs->frame_limit = 0; |
edcd3633 DM |
1388 | period_elapsed = 1; |
1389 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { | |
1390 | if (subs->transfer_done > 0) { | |
1391 | /* FIXME: fill-max mode is not | |
1392 | * supported yet */ | |
1393 | frames -= subs->transfer_done; | |
1394 | counts -= subs->transfer_done; | |
1395 | urb->iso_frame_desc[i].length = | |
8a2a74d2 | 1396 | counts * ep->stride; |
edcd3633 DM |
1397 | subs->transfer_done = 0; |
1398 | } | |
1399 | i++; | |
1400 | if (i < ctx->packets) { | |
1401 | /* add a transfer delimiter */ | |
1402 | urb->iso_frame_desc[i].offset = | |
8a2a74d2 | 1403 | frames * ep->stride; |
edcd3633 DM |
1404 | urb->iso_frame_desc[i].length = 0; |
1405 | urb->number_of_packets++; | |
1406 | } | |
1407 | break; | |
1408 | } | |
1409 | } | |
976b6c06 AS |
1410 | /* finish at the period boundary or after enough frames */ |
1411 | if ((period_elapsed || | |
1412 | subs->transfer_done >= subs->frame_limit) && | |
1413 | !snd_usb_endpoint_implicit_feedback_sink(ep)) | |
edcd3633 DM |
1414 | break; |
1415 | } | |
8a2a74d2 | 1416 | bytes = frames * ep->stride; |
d24f5061 DM |
1417 | |
1418 | if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U16_LE && | |
1419 | subs->cur_audiofmt->dsd_dop)) { | |
1420 | fill_playback_urb_dsd_dop(subs, urb, bytes); | |
44dcbbb1 DM |
1421 | } else if (unlikely(subs->pcm_format == SNDRV_PCM_FORMAT_DSD_U8 && |
1422 | subs->cur_audiofmt->dsd_bitrev)) { | |
1423 | /* bit-reverse the bytes */ | |
1424 | u8 *buf = urb->transfer_buffer; | |
1425 | for (i = 0; i < bytes; i++) { | |
1426 | int idx = (subs->hwptr_done + i) | |
1427 | % (runtime->buffer_size * stride); | |
1428 | buf[i] = bitrev8(runtime->dma_area[idx]); | |
1429 | } | |
1430 | ||
1431 | subs->hwptr_done += bytes; | |
edcd3633 | 1432 | } else { |
d24f5061 DM |
1433 | /* usual PCM */ |
1434 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { | |
1435 | /* err, the transferred area goes over buffer boundary. */ | |
1436 | unsigned int bytes1 = | |
1437 | runtime->buffer_size * stride - subs->hwptr_done; | |
1438 | memcpy(urb->transfer_buffer, | |
1439 | runtime->dma_area + subs->hwptr_done, bytes1); | |
1440 | memcpy(urb->transfer_buffer + bytes1, | |
1441 | runtime->dma_area, bytes - bytes1); | |
1442 | } else { | |
1443 | memcpy(urb->transfer_buffer, | |
1444 | runtime->dma_area + subs->hwptr_done, bytes); | |
1445 | } | |
1446 | ||
1447 | subs->hwptr_done += bytes; | |
edcd3633 | 1448 | } |
d24f5061 | 1449 | |
edcd3633 DM |
1450 | if (subs->hwptr_done >= runtime->buffer_size * stride) |
1451 | subs->hwptr_done -= runtime->buffer_size * stride; | |
fbcfbf5f DM |
1452 | |
1453 | /* update delay with exact number of samples queued */ | |
1454 | runtime->delay = subs->last_delay; | |
edcd3633 | 1455 | runtime->delay += frames; |
fbcfbf5f DM |
1456 | subs->last_delay = runtime->delay; |
1457 | ||
1458 | /* realign last_frame_number */ | |
1459 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); | |
1460 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ | |
1461 | ||
edcd3633 DM |
1462 | spin_unlock_irqrestore(&subs->lock, flags); |
1463 | urb->transfer_buffer_length = bytes; | |
1464 | if (period_elapsed) | |
1465 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * process after playback data complete | |
1470 | * - decrease the delay count again | |
1471 | */ | |
1472 | static void retire_playback_urb(struct snd_usb_substream *subs, | |
1473 | struct urb *urb) | |
1474 | { | |
1475 | unsigned long flags; | |
1476 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
8a2a74d2 DM |
1477 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
1478 | int processed = urb->transfer_buffer_length / ep->stride; | |
fbcfbf5f | 1479 | int est_delay; |
edcd3633 | 1480 | |
1213a205 TI |
1481 | /* ignore the delay accounting when procssed=0 is given, i.e. |
1482 | * silent payloads are procssed before handling the actual data | |
1483 | */ | |
1484 | if (!processed) | |
1485 | return; | |
1486 | ||
edcd3633 | 1487 | spin_lock_irqsave(&subs->lock, flags); |
48779a0b TI |
1488 | if (!subs->last_delay) |
1489 | goto out; /* short path */ | |
1490 | ||
fbcfbf5f DM |
1491 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); |
1492 | /* update delay with exact number of samples played */ | |
1493 | if (processed > subs->last_delay) | |
1494 | subs->last_delay = 0; | |
edcd3633 | 1495 | else |
fbcfbf5f DM |
1496 | subs->last_delay -= processed; |
1497 | runtime->delay = subs->last_delay; | |
1498 | ||
1499 | /* | |
1500 | * Report when delay estimate is off by more than 2ms. | |
1501 | * The error should be lower than 2ms since the estimate relies | |
1502 | * on two reads of a counter updated every ms. | |
1503 | */ | |
1504 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) | |
1505 | snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", | |
1506 | est_delay, subs->last_delay); | |
1507 | ||
48779a0b TI |
1508 | if (!subs->running) { |
1509 | /* update last_frame_number for delay counting here since | |
1510 | * prepare_playback_urb won't be called during pause | |
1511 | */ | |
1512 | subs->last_frame_number = | |
1513 | usb_get_current_frame_number(subs->dev) & 0xff; | |
1514 | } | |
1515 | ||
1516 | out: | |
edcd3633 | 1517 | spin_unlock_irqrestore(&subs->lock, flags); |
e5779998 DM |
1518 | } |
1519 | ||
1520 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) | |
1521 | { | |
1522 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
1523 | } | |
1524 | ||
1525 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) | |
1526 | { | |
1527 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
1528 | } | |
1529 | ||
1530 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) | |
1531 | { | |
1532 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); | |
1533 | } | |
1534 | ||
1535 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) | |
1536 | { | |
1537 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); | |
1538 | } | |
1539 | ||
edcd3633 DM |
1540 | static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, |
1541 | int cmd) | |
1542 | { | |
1543 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
1544 | ||
1545 | switch (cmd) { | |
1546 | case SNDRV_PCM_TRIGGER_START: | |
1547 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1548 | subs->data_endpoint->prepare_data_urb = prepare_playback_urb; | |
1549 | subs->data_endpoint->retire_data_urb = retire_playback_urb; | |
97f8d3b6 | 1550 | subs->running = 1; |
edcd3633 DM |
1551 | return 0; |
1552 | case SNDRV_PCM_TRIGGER_STOP: | |
a9bb3626 | 1553 | stop_endpoints(subs, false); |
97f8d3b6 | 1554 | subs->running = 0; |
edcd3633 DM |
1555 | return 0; |
1556 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1557 | subs->data_endpoint->prepare_data_urb = NULL; | |
48779a0b TI |
1558 | /* keep retire_data_urb for delay calculation */ |
1559 | subs->data_endpoint->retire_data_urb = retire_playback_urb; | |
97f8d3b6 | 1560 | subs->running = 0; |
edcd3633 DM |
1561 | return 0; |
1562 | } | |
1563 | ||
1564 | return -EINVAL; | |
1565 | } | |
1566 | ||
afe25967 DM |
1567 | static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, |
1568 | int cmd) | |
edcd3633 DM |
1569 | { |
1570 | int err; | |
1571 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
1572 | ||
1573 | switch (cmd) { | |
1574 | case SNDRV_PCM_TRIGGER_START: | |
a9bb3626 | 1575 | err = start_endpoints(subs, false); |
edcd3633 DM |
1576 | if (err < 0) |
1577 | return err; | |
1578 | ||
1579 | subs->data_endpoint->retire_data_urb = retire_capture_urb; | |
97f8d3b6 | 1580 | subs->running = 1; |
edcd3633 DM |
1581 | return 0; |
1582 | case SNDRV_PCM_TRIGGER_STOP: | |
a9bb3626 | 1583 | stop_endpoints(subs, false); |
97f8d3b6 | 1584 | subs->running = 0; |
edcd3633 DM |
1585 | return 0; |
1586 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1587 | subs->data_endpoint->retire_data_urb = NULL; | |
97f8d3b6 | 1588 | subs->running = 0; |
edcd3633 DM |
1589 | return 0; |
1590 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1591 | subs->data_endpoint->retire_data_urb = retire_capture_urb; | |
97f8d3b6 | 1592 | subs->running = 1; |
edcd3633 DM |
1593 | return 0; |
1594 | } | |
1595 | ||
1596 | return -EINVAL; | |
1597 | } | |
1598 | ||
e5779998 DM |
1599 | static struct snd_pcm_ops snd_usb_playback_ops = { |
1600 | .open = snd_usb_playback_open, | |
1601 | .close = snd_usb_playback_close, | |
1602 | .ioctl = snd_pcm_lib_ioctl, | |
1603 | .hw_params = snd_usb_hw_params, | |
1604 | .hw_free = snd_usb_hw_free, | |
1605 | .prepare = snd_usb_pcm_prepare, | |
1606 | .trigger = snd_usb_substream_playback_trigger, | |
1607 | .pointer = snd_usb_pcm_pointer, | |
1608 | .page = snd_pcm_lib_get_vmalloc_page, | |
1609 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
1610 | }; | |
1611 | ||
1612 | static struct snd_pcm_ops snd_usb_capture_ops = { | |
1613 | .open = snd_usb_capture_open, | |
1614 | .close = snd_usb_capture_close, | |
1615 | .ioctl = snd_pcm_lib_ioctl, | |
1616 | .hw_params = snd_usb_hw_params, | |
1617 | .hw_free = snd_usb_hw_free, | |
1618 | .prepare = snd_usb_pcm_prepare, | |
1619 | .trigger = snd_usb_substream_capture_trigger, | |
1620 | .pointer = snd_usb_pcm_pointer, | |
1621 | .page = snd_pcm_lib_get_vmalloc_page, | |
1622 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
1623 | }; | |
1624 | ||
1625 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) | |
1626 | { | |
1627 | snd_pcm_set_ops(pcm, stream, | |
1628 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
1629 | &snd_usb_playback_ops : &snd_usb_capture_ops); | |
1630 | } |