]>
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> |
edcd3633 | 19 | #include <linux/ratelimit.h> |
e5779998 DM |
20 | #include <linux/usb.h> |
21 | #include <linux/usb/audio.h> | |
7e847894 | 22 | #include <linux/usb/audio-v2.h> |
e5779998 DM |
23 | |
24 | #include <sound/core.h> | |
25 | #include <sound/pcm.h> | |
26 | #include <sound/pcm_params.h> | |
27 | ||
28 | #include "usbaudio.h" | |
29 | #include "card.h" | |
30 | #include "quirks.h" | |
31 | #include "debug.h" | |
c731bc96 | 32 | #include "endpoint.h" |
e5779998 DM |
33 | #include "helper.h" |
34 | #include "pcm.h" | |
79f920fb | 35 | #include "clock.h" |
88a8516a | 36 | #include "power.h" |
e5779998 | 37 | |
edcd3633 DM |
38 | #define SUBSTREAM_FLAG_DATA_EP_STARTED 0 |
39 | #define SUBSTREAM_FLAG_SYNC_EP_STARTED 1 | |
40 | ||
294c4fb8 PLB |
41 | /* return the estimated delay based on USB frame counters */ |
42 | snd_pcm_uframes_t snd_usb_pcm_delay(struct snd_usb_substream *subs, | |
43 | unsigned int rate) | |
44 | { | |
45 | int current_frame_number; | |
46 | int frame_diff; | |
47 | int est_delay; | |
48 | ||
49 | current_frame_number = usb_get_current_frame_number(subs->dev); | |
50 | /* | |
51 | * HCD implementations use different widths, use lower 8 bits. | |
52 | * The delay will be managed up to 256ms, which is more than | |
53 | * enough | |
54 | */ | |
55 | frame_diff = (current_frame_number - subs->last_frame_number) & 0xff; | |
56 | ||
57 | /* Approximation based on number of samples per USB frame (ms), | |
58 | some truncation for 44.1 but the estimate is good enough */ | |
59 | est_delay = subs->last_delay - (frame_diff * rate / 1000); | |
60 | if (est_delay < 0) | |
61 | est_delay = 0; | |
62 | return est_delay; | |
63 | } | |
64 | ||
e5779998 DM |
65 | /* |
66 | * return the current pcm pointer. just based on the hwptr_done value. | |
67 | */ | |
68 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) | |
69 | { | |
70 | struct snd_usb_substream *subs; | |
71 | unsigned int hwptr_done; | |
72 | ||
73 | subs = (struct snd_usb_substream *)substream->runtime->private_data; | |
74 | spin_lock(&subs->lock); | |
75 | hwptr_done = subs->hwptr_done; | |
294c4fb8 PLB |
76 | substream->runtime->delay = snd_usb_pcm_delay(subs, |
77 | substream->runtime->rate); | |
e5779998 DM |
78 | spin_unlock(&subs->lock); |
79 | return hwptr_done / (substream->runtime->frame_bits >> 3); | |
80 | } | |
81 | ||
82 | /* | |
83 | * find a matching audio format | |
84 | */ | |
85 | static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format, | |
86 | unsigned int rate, unsigned int channels) | |
87 | { | |
88 | struct list_head *p; | |
89 | struct audioformat *found = NULL; | |
90 | int cur_attr = 0, attr; | |
91 | ||
92 | list_for_each(p, &subs->fmt_list) { | |
93 | struct audioformat *fp; | |
94 | fp = list_entry(p, struct audioformat, list); | |
015eb0b0 CL |
95 | if (!(fp->formats & (1uLL << format))) |
96 | continue; | |
97 | if (fp->channels != channels) | |
e5779998 DM |
98 | continue; |
99 | if (rate < fp->rate_min || rate > fp->rate_max) | |
100 | continue; | |
101 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { | |
102 | unsigned int i; | |
103 | for (i = 0; i < fp->nr_rates; i++) | |
104 | if (fp->rate_table[i] == rate) | |
105 | break; | |
106 | if (i >= fp->nr_rates) | |
107 | continue; | |
108 | } | |
109 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
110 | if (! found) { | |
111 | found = fp; | |
112 | cur_attr = attr; | |
113 | continue; | |
114 | } | |
115 | /* avoid async out and adaptive in if the other method | |
116 | * supports the same format. | |
117 | * this is a workaround for the case like | |
118 | * M-audio audiophile USB. | |
119 | */ | |
120 | if (attr != cur_attr) { | |
121 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && | |
122 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
123 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
124 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) | |
125 | continue; | |
126 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && | |
127 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || | |
128 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && | |
129 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { | |
130 | found = fp; | |
131 | cur_attr = attr; | |
132 | continue; | |
133 | } | |
134 | } | |
135 | /* find the format with the largest max. packet size */ | |
136 | if (fp->maxpacksize > found->maxpacksize) { | |
137 | found = fp; | |
138 | cur_attr = attr; | |
139 | } | |
140 | } | |
141 | return found; | |
142 | } | |
143 | ||
767d75ad DM |
144 | static int init_pitch_v1(struct snd_usb_audio *chip, int iface, |
145 | struct usb_host_interface *alts, | |
146 | struct audioformat *fmt) | |
147 | { | |
148 | struct usb_device *dev = chip->dev; | |
149 | unsigned int ep; | |
150 | unsigned char data[1]; | |
151 | int err; | |
152 | ||
153 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
154 | ||
767d75ad DM |
155 | data[0] = 1; |
156 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, | |
157 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, | |
158 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, | |
17d900c4 | 159 | data, sizeof(data))) < 0) { |
767d75ad DM |
160 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", |
161 | dev->devnum, iface, ep); | |
162 | return err; | |
163 | } | |
164 | ||
165 | return 0; | |
166 | } | |
e5779998 | 167 | |
92c25611 DM |
168 | static int init_pitch_v2(struct snd_usb_audio *chip, int iface, |
169 | struct usb_host_interface *alts, | |
170 | struct audioformat *fmt) | |
171 | { | |
172 | struct usb_device *dev = chip->dev; | |
173 | unsigned char data[1]; | |
174 | unsigned int ep; | |
175 | int err; | |
176 | ||
177 | ep = get_endpoint(alts, 0)->bEndpointAddress; | |
178 | ||
179 | data[0] = 1; | |
180 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR, | |
181 | USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT, | |
182 | UAC2_EP_CS_PITCH << 8, 0, | |
17d900c4 | 183 | data, sizeof(data))) < 0) { |
92c25611 DM |
184 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH (v2)\n", |
185 | dev->devnum, iface, fmt->altsetting); | |
186 | return err; | |
187 | } | |
188 | ||
189 | return 0; | |
190 | } | |
191 | ||
e5779998 | 192 | /* |
92c25611 | 193 | * initialize the pitch control and sample rate |
e5779998 | 194 | */ |
767d75ad | 195 | int snd_usb_init_pitch(struct snd_usb_audio *chip, int iface, |
e5779998 DM |
196 | struct usb_host_interface *alts, |
197 | struct audioformat *fmt) | |
198 | { | |
767d75ad DM |
199 | struct usb_interface_descriptor *altsd = get_iface_desc(alts); |
200 | ||
92c25611 DM |
201 | /* if endpoint doesn't have pitch control, bail out */ |
202 | if (!(fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL)) | |
203 | return 0; | |
204 | ||
767d75ad DM |
205 | switch (altsd->bInterfaceProtocol) { |
206 | case UAC_VERSION_1: | |
a2acad82 | 207 | default: |
767d75ad DM |
208 | return init_pitch_v1(chip, iface, alts, fmt); |
209 | ||
210 | case UAC_VERSION_2: | |
92c25611 | 211 | return init_pitch_v2(chip, iface, alts, fmt); |
767d75ad | 212 | } |
767d75ad DM |
213 | } |
214 | ||
015618b9 | 215 | static int start_endpoints(struct snd_usb_substream *subs, int can_sleep) |
edcd3633 DM |
216 | { |
217 | int err; | |
218 | ||
219 | if (!subs->data_endpoint) | |
220 | return -EINVAL; | |
221 | ||
222 | if (!test_and_set_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) { | |
223 | struct snd_usb_endpoint *ep = subs->data_endpoint; | |
224 | ||
225 | snd_printdd(KERN_DEBUG "Starting data EP @%p\n", ep); | |
226 | ||
227 | ep->data_subs = subs; | |
015618b9 | 228 | err = snd_usb_endpoint_start(ep, can_sleep); |
edcd3633 DM |
229 | if (err < 0) { |
230 | clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags); | |
231 | return err; | |
232 | } | |
233 | } | |
234 | ||
235 | if (subs->sync_endpoint && | |
236 | !test_and_set_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) { | |
237 | struct snd_usb_endpoint *ep = subs->sync_endpoint; | |
238 | ||
2e4a263c DM |
239 | if (subs->data_endpoint->iface != subs->sync_endpoint->iface || |
240 | subs->data_endpoint->alt_idx != subs->sync_endpoint->alt_idx) { | |
241 | err = usb_set_interface(subs->dev, | |
242 | subs->sync_endpoint->iface, | |
243 | subs->sync_endpoint->alt_idx); | |
244 | if (err < 0) { | |
245 | snd_printk(KERN_ERR | |
246 | "%d:%d:%d: cannot set interface (%d)\n", | |
247 | subs->dev->devnum, | |
248 | subs->sync_endpoint->iface, | |
249 | subs->sync_endpoint->alt_idx, err); | |
250 | return -EIO; | |
251 | } | |
252 | } | |
253 | ||
edcd3633 DM |
254 | snd_printdd(KERN_DEBUG "Starting sync EP @%p\n", ep); |
255 | ||
256 | ep->sync_slave = subs->data_endpoint; | |
015618b9 | 257 | err = snd_usb_endpoint_start(ep, can_sleep); |
edcd3633 DM |
258 | if (err < 0) { |
259 | clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags); | |
260 | return err; | |
261 | } | |
262 | } | |
263 | ||
264 | return 0; | |
265 | } | |
266 | ||
267 | static void stop_endpoints(struct snd_usb_substream *subs, | |
268 | int force, int can_sleep, int wait) | |
269 | { | |
270 | if (test_and_clear_bit(SUBSTREAM_FLAG_SYNC_EP_STARTED, &subs->flags)) | |
271 | snd_usb_endpoint_stop(subs->sync_endpoint, | |
272 | force, can_sleep, wait); | |
273 | ||
274 | if (test_and_clear_bit(SUBSTREAM_FLAG_DATA_EP_STARTED, &subs->flags)) | |
275 | snd_usb_endpoint_stop(subs->data_endpoint, | |
276 | force, can_sleep, wait); | |
277 | } | |
278 | ||
edcd3633 DM |
279 | static int deactivate_endpoints(struct snd_usb_substream *subs) |
280 | { | |
281 | int reta, retb; | |
282 | ||
283 | reta = snd_usb_endpoint_deactivate(subs->sync_endpoint); | |
284 | retb = snd_usb_endpoint_deactivate(subs->data_endpoint); | |
285 | ||
286 | if (reta < 0) | |
287 | return reta; | |
288 | ||
289 | if (retb < 0) | |
290 | return retb; | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
e5779998 DM |
295 | /* |
296 | * find a matching format and set up the interface | |
297 | */ | |
298 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) | |
299 | { | |
300 | struct usb_device *dev = subs->dev; | |
301 | struct usb_host_interface *alts; | |
302 | struct usb_interface_descriptor *altsd; | |
303 | struct usb_interface *iface; | |
304 | unsigned int ep, attr; | |
305 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; | |
c75a8a7a | 306 | int err, implicit_fb = 0; |
e5779998 DM |
307 | |
308 | iface = usb_ifnum_to_if(dev, fmt->iface); | |
309 | if (WARN_ON(!iface)) | |
310 | return -EINVAL; | |
311 | alts = &iface->altsetting[fmt->altset_idx]; | |
312 | altsd = get_iface_desc(alts); | |
313 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) | |
314 | return -EINVAL; | |
315 | ||
316 | if (fmt == subs->cur_audiofmt) | |
317 | return 0; | |
318 | ||
68e67f40 DM |
319 | /* close the old interface */ |
320 | if (subs->interface >= 0 && subs->interface != fmt->iface) { | |
321 | err = usb_set_interface(subs->dev, subs->interface, 0); | |
322 | if (err < 0) { | |
323 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed (%d)\n", | |
324 | dev->devnum, fmt->iface, fmt->altsetting, err); | |
325 | return -EIO; | |
326 | } | |
327 | subs->interface = -1; | |
328 | subs->altset_idx = 0; | |
329 | } | |
330 | ||
331 | /* set interface */ | |
332 | if (subs->interface != fmt->iface || | |
333 | subs->altset_idx != fmt->altset_idx) { | |
334 | err = usb_set_interface(dev, fmt->iface, fmt->altsetting); | |
335 | if (err < 0) { | |
336 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed (%d)\n", | |
337 | dev->devnum, fmt->iface, fmt->altsetting, err); | |
338 | return -EIO; | |
339 | } | |
340 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", | |
341 | fmt->iface, fmt->altsetting); | |
342 | subs->interface = fmt->iface; | |
343 | subs->altset_idx = fmt->altset_idx; | |
344 | } | |
345 | ||
edcd3633 DM |
346 | subs->data_endpoint = snd_usb_add_endpoint(subs->stream->chip, |
347 | alts, fmt->endpoint, subs->direction, | |
348 | SND_USB_ENDPOINT_TYPE_DATA); | |
349 | if (!subs->data_endpoint) | |
350 | return -EINVAL; | |
e5779998 DM |
351 | |
352 | /* we need a sync pipe in async OUT or adaptive IN mode */ | |
353 | /* check the number of EP, since some devices have broken | |
354 | * descriptors which fool us. if it has only one EP, | |
355 | * assume it as adaptive-out or sync-in. | |
356 | */ | |
357 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; | |
c75a8a7a DM |
358 | |
359 | switch (subs->stream->chip->usb_id) { | |
360 | case USB_ID(0x0763, 0x2080): /* M-Audio FastTrack Ultra */ | |
361 | case USB_ID(0x0763, 0x2081): | |
362 | if (is_playback) { | |
363 | implicit_fb = 1; | |
edcd3633 DM |
364 | ep = 0x81; |
365 | iface = usb_ifnum_to_if(dev, 2); | |
c75a8a7a DM |
366 | |
367 | if (!iface || iface->num_altsetting == 0) | |
368 | return -EINVAL; | |
369 | ||
edcd3633 DM |
370 | alts = &iface->altsetting[1]; |
371 | goto add_sync_ep; | |
372 | } | |
c75a8a7a | 373 | } |
edcd3633 | 374 | |
c75a8a7a DM |
375 | if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) || |
376 | (!is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) && | |
377 | altsd->bNumEndpoints >= 2) { | |
e5779998 DM |
378 | /* check sync-pipe endpoint */ |
379 | /* ... and check descriptor size before accessing bSynchAddress | |
380 | because there is a version of the SB Audigy 2 NX firmware lacking | |
381 | the audio fields in the endpoint descriptors */ | |
382 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || | |
383 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
c75a8a7a DM |
384 | get_endpoint(alts, 1)->bSynchAddress != 0 && |
385 | !implicit_fb)) { | |
7fb75db1 DM |
386 | snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. bmAttributes %02x, bLength %d, bSynchAddress %02x\n", |
387 | dev->devnum, fmt->iface, fmt->altsetting, | |
388 | get_endpoint(alts, 1)->bmAttributes, | |
389 | get_endpoint(alts, 1)->bLength, | |
390 | get_endpoint(alts, 1)->bSynchAddress); | |
e5779998 DM |
391 | return -EINVAL; |
392 | } | |
393 | ep = get_endpoint(alts, 1)->bEndpointAddress; | |
7fb75db1 DM |
394 | if (!implicit_fb && |
395 | get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && | |
e5779998 | 396 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
7fb75db1 DM |
397 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { |
398 | snd_printk(KERN_ERR "%d:%d:%d : invalid sync pipe. is_playback %d, ep %02x, bSynchAddress %02x\n", | |
399 | dev->devnum, fmt->iface, fmt->altsetting, | |
400 | is_playback, ep, get_endpoint(alts, 0)->bSynchAddress); | |
e5779998 DM |
401 | return -EINVAL; |
402 | } | |
c75a8a7a DM |
403 | |
404 | implicit_fb = (get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_USAGE_MASK) | |
405 | == USB_ENDPOINT_USAGE_IMPLICIT_FB; | |
406 | ||
edcd3633 DM |
407 | add_sync_ep: |
408 | subs->sync_endpoint = snd_usb_add_endpoint(subs->stream->chip, | |
409 | alts, ep, !subs->direction, | |
c75a8a7a DM |
410 | implicit_fb ? |
411 | SND_USB_ENDPOINT_TYPE_DATA : | |
412 | SND_USB_ENDPOINT_TYPE_SYNC); | |
edcd3633 DM |
413 | if (!subs->sync_endpoint) |
414 | return -EINVAL; | |
415 | ||
416 | subs->data_endpoint->sync_master = subs->sync_endpoint; | |
417 | } | |
e5779998 | 418 | |
9e9b5946 | 419 | if ((err = snd_usb_init_pitch(subs->stream->chip, fmt->iface, alts, fmt)) < 0) |
e5779998 DM |
420 | return err; |
421 | ||
422 | subs->cur_audiofmt = fmt; | |
423 | ||
424 | snd_usb_set_format_quirk(subs, fmt); | |
425 | ||
426 | #if 0 | |
427 | printk(KERN_DEBUG | |
428 | "setting done: format = %d, rate = %d..%d, channels = %d\n", | |
429 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); | |
430 | printk(KERN_DEBUG | |
431 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", | |
432 | subs->datapipe, subs->syncpipe); | |
433 | #endif | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | /* | |
439 | * hw_params callback | |
440 | * | |
441 | * allocate a buffer and set the given audio format. | |
442 | * | |
443 | * so far we use a physically linear buffer although packetize transfer | |
444 | * doesn't need a continuous area. | |
445 | * if sg buffer is supported on the later version of alsa, we'll follow | |
446 | * that. | |
447 | */ | |
448 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, | |
449 | struct snd_pcm_hw_params *hw_params) | |
450 | { | |
451 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
452 | struct audioformat *fmt; | |
453 | unsigned int channels, rate, format; | |
454 | int ret, changed; | |
455 | ||
456 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, | |
457 | params_buffer_bytes(hw_params)); | |
458 | if (ret < 0) | |
459 | return ret; | |
460 | ||
461 | format = params_format(hw_params); | |
462 | rate = params_rate(hw_params); | |
463 | channels = params_channels(hw_params); | |
464 | fmt = find_format(subs, format, rate, channels); | |
465 | if (!fmt) { | |
466 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", | |
467 | format, rate, channels); | |
468 | return -EINVAL; | |
469 | } | |
470 | ||
471 | changed = subs->cur_audiofmt != fmt || | |
472 | subs->period_bytes != params_period_bytes(hw_params) || | |
473 | subs->cur_rate != rate; | |
474 | if ((ret = set_format(subs, fmt)) < 0) | |
475 | return ret; | |
476 | ||
477 | if (subs->cur_rate != rate) { | |
478 | struct usb_host_interface *alts; | |
479 | struct usb_interface *iface; | |
480 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); | |
481 | alts = &iface->altsetting[fmt->altset_idx]; | |
9e9b5946 | 482 | ret = snd_usb_init_sample_rate(subs->stream->chip, fmt->iface, alts, fmt, rate); |
e5779998 DM |
483 | if (ret < 0) |
484 | return ret; | |
485 | subs->cur_rate = rate; | |
486 | } | |
487 | ||
488 | if (changed) { | |
715a1705 DR |
489 | subs->period_bytes = params_period_bytes(hw_params); |
490 | ||
382225e6 | 491 | mutex_lock(&subs->stream->chip->shutdown_mutex); |
e5779998 | 492 | /* format changed */ |
edcd3633 | 493 | stop_endpoints(subs, 0, 0, 0); |
35ec7aa2 DR |
494 | ret = snd_usb_endpoint_set_params(subs->data_endpoint, |
495 | format, | |
496 | channels, | |
497 | subs->period_bytes, | |
498 | rate, | |
499 | fmt, | |
edcd3633 DM |
500 | subs->sync_endpoint); |
501 | if (ret < 0) | |
502 | goto unlock; | |
503 | ||
504 | if (subs->sync_endpoint) | |
35ec7aa2 DR |
505 | ret = snd_usb_endpoint_set_params(subs->data_endpoint, |
506 | format, | |
507 | channels, | |
508 | subs->period_bytes, | |
509 | rate, | |
510 | fmt, | |
511 | NULL); | |
edcd3633 | 512 | unlock: |
382225e6 | 513 | mutex_unlock(&subs->stream->chip->shutdown_mutex); |
e5779998 DM |
514 | } |
515 | ||
97f8d3b6 DM |
516 | if (ret == 0) { |
517 | subs->interface = fmt->iface; | |
518 | subs->altset_idx = fmt->altset_idx; | |
519 | } | |
520 | ||
e5779998 DM |
521 | return ret; |
522 | } | |
523 | ||
524 | /* | |
525 | * hw_free callback | |
526 | * | |
527 | * reset the audio format and release the buffer | |
528 | */ | |
529 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) | |
530 | { | |
531 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
532 | ||
533 | subs->cur_audiofmt = NULL; | |
534 | subs->cur_rate = 0; | |
535 | subs->period_bytes = 0; | |
382225e6 | 536 | mutex_lock(&subs->stream->chip->shutdown_mutex); |
edcd3633 | 537 | stop_endpoints(subs, 0, 1, 1); |
68e67f40 | 538 | deactivate_endpoints(subs); |
382225e6 | 539 | mutex_unlock(&subs->stream->chip->shutdown_mutex); |
e5779998 DM |
540 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
541 | } | |
542 | ||
543 | /* | |
544 | * prepare callback | |
545 | * | |
546 | * only a few subtle things... | |
547 | */ | |
548 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) | |
549 | { | |
550 | struct snd_pcm_runtime *runtime = substream->runtime; | |
551 | struct snd_usb_substream *subs = runtime->private_data; | |
552 | ||
553 | if (! subs->cur_audiofmt) { | |
554 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); | |
555 | return -ENXIO; | |
556 | } | |
557 | ||
edcd3633 DM |
558 | if (snd_BUG_ON(!subs->data_endpoint)) |
559 | return -EIO; | |
560 | ||
e5779998 | 561 | /* some unit conversions in runtime */ |
edcd3633 DM |
562 | subs->data_endpoint->maxframesize = |
563 | bytes_to_frames(runtime, subs->data_endpoint->maxpacksize); | |
564 | subs->data_endpoint->curframesize = | |
565 | bytes_to_frames(runtime, subs->data_endpoint->curpacksize); | |
e5779998 DM |
566 | |
567 | /* reset the pointer */ | |
568 | subs->hwptr_done = 0; | |
569 | subs->transfer_done = 0; | |
294c4fb8 PLB |
570 | subs->last_delay = 0; |
571 | subs->last_frame_number = 0; | |
e5779998 DM |
572 | runtime->delay = 0; |
573 | ||
edcd3633 DM |
574 | /* for playback, submit the URBs now; otherwise, the first hwptr_done |
575 | * updates for all URBs would happen at the same time when starting */ | |
576 | if (subs->direction == SNDRV_PCM_STREAM_PLAYBACK) | |
015618b9 | 577 | return start_endpoints(subs, 1); |
edcd3633 DM |
578 | |
579 | return 0; | |
e5779998 DM |
580 | } |
581 | ||
582 | static struct snd_pcm_hardware snd_usb_hardware = | |
583 | { | |
584 | .info = SNDRV_PCM_INFO_MMAP | | |
585 | SNDRV_PCM_INFO_MMAP_VALID | | |
586 | SNDRV_PCM_INFO_BATCH | | |
587 | SNDRV_PCM_INFO_INTERLEAVED | | |
588 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
589 | SNDRV_PCM_INFO_PAUSE, | |
590 | .buffer_bytes_max = 1024 * 1024, | |
591 | .period_bytes_min = 64, | |
592 | .period_bytes_max = 512 * 1024, | |
593 | .periods_min = 2, | |
594 | .periods_max = 1024, | |
595 | }; | |
596 | ||
597 | static int hw_check_valid_format(struct snd_usb_substream *subs, | |
598 | struct snd_pcm_hw_params *params, | |
599 | struct audioformat *fp) | |
600 | { | |
601 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
602 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
603 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
604 | struct snd_interval *pt = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
015eb0b0 | 605 | struct snd_mask check_fmts; |
e5779998 DM |
606 | unsigned int ptime; |
607 | ||
608 | /* check the format */ | |
015eb0b0 CL |
609 | snd_mask_none(&check_fmts); |
610 | check_fmts.bits[0] = (u32)fp->formats; | |
611 | check_fmts.bits[1] = (u32)(fp->formats >> 32); | |
612 | snd_mask_intersect(&check_fmts, fmts); | |
613 | if (snd_mask_empty(&check_fmts)) { | |
e5779998 DM |
614 | hwc_debug(" > check: no supported format %d\n", fp->format); |
615 | return 0; | |
616 | } | |
617 | /* check the channels */ | |
618 | if (fp->channels < ct->min || fp->channels > ct->max) { | |
619 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); | |
620 | return 0; | |
621 | } | |
622 | /* check the rate is within the range */ | |
623 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { | |
624 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); | |
625 | return 0; | |
626 | } | |
627 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { | |
628 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); | |
629 | return 0; | |
630 | } | |
631 | /* check whether the period time is >= the data packet interval */ | |
4f4e8f69 | 632 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_FULL) { |
e5779998 DM |
633 | ptime = 125 * (1 << fp->datainterval); |
634 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { | |
635 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); | |
636 | return 0; | |
637 | } | |
638 | } | |
639 | return 1; | |
640 | } | |
641 | ||
642 | static int hw_rule_rate(struct snd_pcm_hw_params *params, | |
643 | struct snd_pcm_hw_rule *rule) | |
644 | { | |
645 | struct snd_usb_substream *subs = rule->private; | |
646 | struct list_head *p; | |
647 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); | |
648 | unsigned int rmin, rmax; | |
649 | int changed; | |
650 | ||
651 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); | |
652 | changed = 0; | |
653 | rmin = rmax = 0; | |
654 | list_for_each(p, &subs->fmt_list) { | |
655 | struct audioformat *fp; | |
656 | fp = list_entry(p, struct audioformat, list); | |
657 | if (!hw_check_valid_format(subs, params, fp)) | |
658 | continue; | |
659 | if (changed++) { | |
660 | if (rmin > fp->rate_min) | |
661 | rmin = fp->rate_min; | |
662 | if (rmax < fp->rate_max) | |
663 | rmax = fp->rate_max; | |
664 | } else { | |
665 | rmin = fp->rate_min; | |
666 | rmax = fp->rate_max; | |
667 | } | |
668 | } | |
669 | ||
670 | if (!changed) { | |
671 | hwc_debug(" --> get empty\n"); | |
672 | it->empty = 1; | |
673 | return -EINVAL; | |
674 | } | |
675 | ||
676 | changed = 0; | |
677 | if (it->min < rmin) { | |
678 | it->min = rmin; | |
679 | it->openmin = 0; | |
680 | changed = 1; | |
681 | } | |
682 | if (it->max > rmax) { | |
683 | it->max = rmax; | |
684 | it->openmax = 0; | |
685 | changed = 1; | |
686 | } | |
687 | if (snd_interval_checkempty(it)) { | |
688 | it->empty = 1; | |
689 | return -EINVAL; | |
690 | } | |
691 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
692 | return changed; | |
693 | } | |
694 | ||
695 | ||
696 | static int hw_rule_channels(struct snd_pcm_hw_params *params, | |
697 | struct snd_pcm_hw_rule *rule) | |
698 | { | |
699 | struct snd_usb_substream *subs = rule->private; | |
700 | struct list_head *p; | |
701 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); | |
702 | unsigned int rmin, rmax; | |
703 | int changed; | |
704 | ||
705 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); | |
706 | changed = 0; | |
707 | rmin = rmax = 0; | |
708 | list_for_each(p, &subs->fmt_list) { | |
709 | struct audioformat *fp; | |
710 | fp = list_entry(p, struct audioformat, list); | |
711 | if (!hw_check_valid_format(subs, params, fp)) | |
712 | continue; | |
713 | if (changed++) { | |
714 | if (rmin > fp->channels) | |
715 | rmin = fp->channels; | |
716 | if (rmax < fp->channels) | |
717 | rmax = fp->channels; | |
718 | } else { | |
719 | rmin = fp->channels; | |
720 | rmax = fp->channels; | |
721 | } | |
722 | } | |
723 | ||
724 | if (!changed) { | |
725 | hwc_debug(" --> get empty\n"); | |
726 | it->empty = 1; | |
727 | return -EINVAL; | |
728 | } | |
729 | ||
730 | changed = 0; | |
731 | if (it->min < rmin) { | |
732 | it->min = rmin; | |
733 | it->openmin = 0; | |
734 | changed = 1; | |
735 | } | |
736 | if (it->max > rmax) { | |
737 | it->max = rmax; | |
738 | it->openmax = 0; | |
739 | changed = 1; | |
740 | } | |
741 | if (snd_interval_checkempty(it)) { | |
742 | it->empty = 1; | |
743 | return -EINVAL; | |
744 | } | |
745 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); | |
746 | return changed; | |
747 | } | |
748 | ||
749 | static int hw_rule_format(struct snd_pcm_hw_params *params, | |
750 | struct snd_pcm_hw_rule *rule) | |
751 | { | |
752 | struct snd_usb_substream *subs = rule->private; | |
753 | struct list_head *p; | |
754 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); | |
755 | u64 fbits; | |
756 | u32 oldbits[2]; | |
757 | int changed; | |
758 | ||
759 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); | |
760 | fbits = 0; | |
761 | list_for_each(p, &subs->fmt_list) { | |
762 | struct audioformat *fp; | |
763 | fp = list_entry(p, struct audioformat, list); | |
764 | if (!hw_check_valid_format(subs, params, fp)) | |
765 | continue; | |
015eb0b0 | 766 | fbits |= fp->formats; |
e5779998 DM |
767 | } |
768 | ||
769 | oldbits[0] = fmt->bits[0]; | |
770 | oldbits[1] = fmt->bits[1]; | |
771 | fmt->bits[0] &= (u32)fbits; | |
772 | fmt->bits[1] &= (u32)(fbits >> 32); | |
773 | if (!fmt->bits[0] && !fmt->bits[1]) { | |
774 | hwc_debug(" --> get empty\n"); | |
775 | return -EINVAL; | |
776 | } | |
777 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); | |
778 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); | |
779 | return changed; | |
780 | } | |
781 | ||
782 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, | |
783 | struct snd_pcm_hw_rule *rule) | |
784 | { | |
785 | struct snd_usb_substream *subs = rule->private; | |
786 | struct audioformat *fp; | |
787 | struct snd_interval *it; | |
788 | unsigned char min_datainterval; | |
789 | unsigned int pmin; | |
790 | int changed; | |
791 | ||
792 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); | |
793 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); | |
794 | min_datainterval = 0xff; | |
795 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
796 | if (!hw_check_valid_format(subs, params, fp)) | |
797 | continue; | |
798 | min_datainterval = min(min_datainterval, fp->datainterval); | |
799 | } | |
800 | if (min_datainterval == 0xff) { | |
a7ce2e0d | 801 | hwc_debug(" --> get empty\n"); |
e5779998 DM |
802 | it->empty = 1; |
803 | return -EINVAL; | |
804 | } | |
805 | pmin = 125 * (1 << min_datainterval); | |
806 | changed = 0; | |
807 | if (it->min < pmin) { | |
808 | it->min = pmin; | |
809 | it->openmin = 0; | |
810 | changed = 1; | |
811 | } | |
812 | if (snd_interval_checkempty(it)) { | |
813 | it->empty = 1; | |
814 | return -EINVAL; | |
815 | } | |
816 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); | |
817 | return changed; | |
818 | } | |
819 | ||
820 | /* | |
821 | * If the device supports unusual bit rates, does the request meet these? | |
822 | */ | |
823 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, | |
824 | struct snd_usb_substream *subs) | |
825 | { | |
826 | struct audioformat *fp; | |
0717d0f5 | 827 | int *rate_list; |
e5779998 DM |
828 | int count = 0, needs_knot = 0; |
829 | int err; | |
830 | ||
5cd5d7c4 CL |
831 | kfree(subs->rate_list.list); |
832 | subs->rate_list.list = NULL; | |
833 | ||
e5779998 DM |
834 | list_for_each_entry(fp, &subs->fmt_list, list) { |
835 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) | |
836 | return 0; | |
837 | count += fp->nr_rates; | |
838 | if (fp->rates & SNDRV_PCM_RATE_KNOT) | |
839 | needs_knot = 1; | |
840 | } | |
841 | if (!needs_knot) | |
842 | return 0; | |
843 | ||
0717d0f5 TI |
844 | subs->rate_list.list = rate_list = |
845 | kmalloc(sizeof(int) * count, GFP_KERNEL); | |
8a8d56b2 JJ |
846 | if (!subs->rate_list.list) |
847 | return -ENOMEM; | |
848 | subs->rate_list.count = count; | |
e5779998 DM |
849 | subs->rate_list.mask = 0; |
850 | count = 0; | |
851 | list_for_each_entry(fp, &subs->fmt_list, list) { | |
852 | int i; | |
853 | for (i = 0; i < fp->nr_rates; i++) | |
0717d0f5 | 854 | rate_list[count++] = fp->rate_table[i]; |
e5779998 DM |
855 | } |
856 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
857 | &subs->rate_list); | |
858 | if (err < 0) | |
859 | return err; | |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
864 | ||
865 | /* | |
866 | * set up the runtime hardware information. | |
867 | */ | |
868 | ||
869 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) | |
870 | { | |
871 | struct list_head *p; | |
872 | unsigned int pt, ptmin; | |
873 | int param_period_time_if_needed; | |
874 | int err; | |
875 | ||
876 | runtime->hw.formats = subs->formats; | |
877 | ||
878 | runtime->hw.rate_min = 0x7fffffff; | |
879 | runtime->hw.rate_max = 0; | |
880 | runtime->hw.channels_min = 256; | |
881 | runtime->hw.channels_max = 0; | |
882 | runtime->hw.rates = 0; | |
883 | ptmin = UINT_MAX; | |
884 | /* check min/max rates and channels */ | |
885 | list_for_each(p, &subs->fmt_list) { | |
886 | struct audioformat *fp; | |
887 | fp = list_entry(p, struct audioformat, list); | |
888 | runtime->hw.rates |= fp->rates; | |
889 | if (runtime->hw.rate_min > fp->rate_min) | |
890 | runtime->hw.rate_min = fp->rate_min; | |
891 | if (runtime->hw.rate_max < fp->rate_max) | |
892 | runtime->hw.rate_max = fp->rate_max; | |
893 | if (runtime->hw.channels_min > fp->channels) | |
894 | runtime->hw.channels_min = fp->channels; | |
895 | if (runtime->hw.channels_max < fp->channels) | |
896 | runtime->hw.channels_max = fp->channels; | |
897 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { | |
898 | /* FIXME: there might be more than one audio formats... */ | |
899 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = | |
900 | fp->frame_size; | |
901 | } | |
902 | pt = 125 * (1 << fp->datainterval); | |
903 | ptmin = min(ptmin, pt); | |
904 | } | |
88a8516a ON |
905 | err = snd_usb_autoresume(subs->stream->chip); |
906 | if (err < 0) | |
907 | return err; | |
e5779998 DM |
908 | |
909 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; | |
4f4e8f69 | 910 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
e5779998 DM |
911 | /* full speed devices have fixed data packet interval */ |
912 | ptmin = 1000; | |
913 | if (ptmin == 1000) | |
914 | /* if period time doesn't go below 1 ms, no rules needed */ | |
915 | param_period_time_if_needed = -1; | |
916 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
917 | ptmin, UINT_MAX); | |
918 | ||
919 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, | |
920 | hw_rule_rate, subs, | |
921 | SNDRV_PCM_HW_PARAM_FORMAT, | |
922 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
923 | param_period_time_if_needed, | |
924 | -1)) < 0) | |
88a8516a | 925 | goto rep_err; |
e5779998 DM |
926 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
927 | hw_rule_channels, subs, | |
928 | SNDRV_PCM_HW_PARAM_FORMAT, | |
929 | SNDRV_PCM_HW_PARAM_RATE, | |
930 | param_period_time_if_needed, | |
931 | -1)) < 0) | |
88a8516a | 932 | goto rep_err; |
e5779998 DM |
933 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
934 | hw_rule_format, subs, | |
935 | SNDRV_PCM_HW_PARAM_RATE, | |
936 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
937 | param_period_time_if_needed, | |
938 | -1)) < 0) | |
88a8516a | 939 | goto rep_err; |
e5779998 DM |
940 | if (param_period_time_if_needed >= 0) { |
941 | err = snd_pcm_hw_rule_add(runtime, 0, | |
942 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, | |
943 | hw_rule_period_time, subs, | |
944 | SNDRV_PCM_HW_PARAM_FORMAT, | |
945 | SNDRV_PCM_HW_PARAM_CHANNELS, | |
946 | SNDRV_PCM_HW_PARAM_RATE, | |
947 | -1); | |
948 | if (err < 0) | |
88a8516a | 949 | goto rep_err; |
e5779998 DM |
950 | } |
951 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) | |
88a8516a | 952 | goto rep_err; |
e5779998 | 953 | return 0; |
88a8516a ON |
954 | |
955 | rep_err: | |
956 | snd_usb_autosuspend(subs->stream->chip); | |
957 | return err; | |
e5779998 DM |
958 | } |
959 | ||
960 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) | |
961 | { | |
962 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
963 | struct snd_pcm_runtime *runtime = substream->runtime; | |
964 | struct snd_usb_substream *subs = &as->substream[direction]; | |
965 | ||
966 | subs->interface = -1; | |
e11b4e0e | 967 | subs->altset_idx = 0; |
e5779998 DM |
968 | runtime->hw = snd_usb_hardware; |
969 | runtime->private_data = subs; | |
970 | subs->pcm_substream = substream; | |
88a8516a | 971 | /* runtime PM is also done there */ |
e5779998 DM |
972 | return setup_hw_info(runtime, subs); |
973 | } | |
974 | ||
975 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) | |
976 | { | |
977 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); | |
978 | struct snd_usb_substream *subs = &as->substream[direction]; | |
979 | ||
edcd3633 | 980 | stop_endpoints(subs, 0, 0, 0); |
68e67f40 DM |
981 | |
982 | if (!as->chip->shutdown && subs->interface >= 0) { | |
983 | usb_set_interface(subs->dev, subs->interface, 0); | |
984 | subs->interface = -1; | |
985 | } | |
986 | ||
e5779998 | 987 | subs->pcm_substream = NULL; |
88a8516a | 988 | snd_usb_autosuspend(subs->stream->chip); |
edcd3633 | 989 | |
68e67f40 | 990 | return 0; |
edcd3633 DM |
991 | } |
992 | ||
993 | /* Since a URB can handle only a single linear buffer, we must use double | |
994 | * buffering when the data to be transferred overflows the buffer boundary. | |
995 | * To avoid inconsistencies when updating hwptr_done, we use double buffering | |
996 | * for all URBs. | |
997 | */ | |
998 | static void retire_capture_urb(struct snd_usb_substream *subs, | |
999 | struct urb *urb) | |
1000 | { | |
1001 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
1002 | unsigned int stride, frames, bytes, oldptr; | |
1003 | int i, period_elapsed = 0; | |
1004 | unsigned long flags; | |
1005 | unsigned char *cp; | |
1006 | ||
1007 | stride = runtime->frame_bits >> 3; | |
1008 | ||
1009 | for (i = 0; i < urb->number_of_packets; i++) { | |
1010 | cp = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset; | |
1011 | if (urb->iso_frame_desc[i].status && printk_ratelimit()) { | |
1012 | snd_printdd(KERN_ERR "frame %d active: %d\n", i, urb->iso_frame_desc[i].status); | |
1013 | // continue; | |
1014 | } | |
1015 | bytes = urb->iso_frame_desc[i].actual_length; | |
1016 | frames = bytes / stride; | |
1017 | if (!subs->txfr_quirk) | |
1018 | bytes = frames * stride; | |
1019 | if (bytes % (runtime->sample_bits >> 3) != 0) { | |
1020 | #ifdef CONFIG_SND_DEBUG_VERBOSE | |
1021 | int oldbytes = bytes; | |
1022 | #endif | |
1023 | bytes = frames * stride; | |
1024 | snd_printdd(KERN_ERR "Corrected urb data len. %d->%d\n", | |
1025 | oldbytes, bytes); | |
1026 | } | |
1027 | /* update the current pointer */ | |
1028 | spin_lock_irqsave(&subs->lock, flags); | |
1029 | oldptr = subs->hwptr_done; | |
1030 | subs->hwptr_done += bytes; | |
1031 | if (subs->hwptr_done >= runtime->buffer_size * stride) | |
1032 | subs->hwptr_done -= runtime->buffer_size * stride; | |
1033 | frames = (bytes + (oldptr % stride)) / stride; | |
1034 | subs->transfer_done += frames; | |
1035 | if (subs->transfer_done >= runtime->period_size) { | |
1036 | subs->transfer_done -= runtime->period_size; | |
1037 | period_elapsed = 1; | |
1038 | } | |
1039 | spin_unlock_irqrestore(&subs->lock, flags); | |
1040 | /* copy a data chunk */ | |
1041 | if (oldptr + bytes > runtime->buffer_size * stride) { | |
1042 | unsigned int bytes1 = | |
1043 | runtime->buffer_size * stride - oldptr; | |
1044 | memcpy(runtime->dma_area + oldptr, cp, bytes1); | |
1045 | memcpy(runtime->dma_area, cp + bytes1, bytes - bytes1); | |
1046 | } else { | |
1047 | memcpy(runtime->dma_area + oldptr, cp, bytes); | |
1048 | } | |
1049 | } | |
1050 | ||
1051 | if (period_elapsed) | |
1052 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1053 | } | |
1054 | ||
1055 | static void prepare_playback_urb(struct snd_usb_substream *subs, | |
1056 | struct urb *urb) | |
1057 | { | |
1058 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
245baf98 | 1059 | struct snd_usb_endpoint *ep = subs->data_endpoint; |
edcd3633 DM |
1060 | struct snd_urb_ctx *ctx = urb->context; |
1061 | unsigned int counts, frames, bytes; | |
1062 | int i, stride, period_elapsed = 0; | |
1063 | unsigned long flags; | |
1064 | ||
1065 | stride = runtime->frame_bits >> 3; | |
1066 | ||
1067 | frames = 0; | |
1068 | urb->number_of_packets = 0; | |
1069 | spin_lock_irqsave(&subs->lock, flags); | |
1070 | for (i = 0; i < ctx->packets; i++) { | |
245baf98 DM |
1071 | if (ctx->packet_size[i]) |
1072 | counts = ctx->packet_size[i]; | |
1073 | else | |
1074 | counts = snd_usb_endpoint_next_packet_size(ep); | |
1075 | ||
edcd3633 DM |
1076 | /* set up descriptor */ |
1077 | urb->iso_frame_desc[i].offset = frames * stride; | |
1078 | urb->iso_frame_desc[i].length = counts * stride; | |
1079 | frames += counts; | |
1080 | urb->number_of_packets++; | |
1081 | subs->transfer_done += counts; | |
1082 | if (subs->transfer_done >= runtime->period_size) { | |
1083 | subs->transfer_done -= runtime->period_size; | |
1084 | period_elapsed = 1; | |
1085 | if (subs->fmt_type == UAC_FORMAT_TYPE_II) { | |
1086 | if (subs->transfer_done > 0) { | |
1087 | /* FIXME: fill-max mode is not | |
1088 | * supported yet */ | |
1089 | frames -= subs->transfer_done; | |
1090 | counts -= subs->transfer_done; | |
1091 | urb->iso_frame_desc[i].length = | |
1092 | counts * stride; | |
1093 | subs->transfer_done = 0; | |
1094 | } | |
1095 | i++; | |
1096 | if (i < ctx->packets) { | |
1097 | /* add a transfer delimiter */ | |
1098 | urb->iso_frame_desc[i].offset = | |
1099 | frames * stride; | |
1100 | urb->iso_frame_desc[i].length = 0; | |
1101 | urb->number_of_packets++; | |
1102 | } | |
1103 | break; | |
1104 | } | |
1105 | } | |
1106 | if (period_elapsed && | |
1107 | !snd_usb_endpoint_implict_feedback_sink(subs->data_endpoint)) /* finish at the period boundary */ | |
1108 | break; | |
1109 | } | |
1110 | bytes = frames * stride; | |
1111 | if (subs->hwptr_done + bytes > runtime->buffer_size * stride) { | |
1112 | /* err, the transferred area goes over buffer boundary. */ | |
1113 | unsigned int bytes1 = | |
1114 | runtime->buffer_size * stride - subs->hwptr_done; | |
1115 | memcpy(urb->transfer_buffer, | |
1116 | runtime->dma_area + subs->hwptr_done, bytes1); | |
1117 | memcpy(urb->transfer_buffer + bytes1, | |
1118 | runtime->dma_area, bytes - bytes1); | |
1119 | } else { | |
1120 | memcpy(urb->transfer_buffer, | |
1121 | runtime->dma_area + subs->hwptr_done, bytes); | |
1122 | } | |
1123 | subs->hwptr_done += bytes; | |
1124 | if (subs->hwptr_done >= runtime->buffer_size * stride) | |
1125 | subs->hwptr_done -= runtime->buffer_size * stride; | |
fbcfbf5f DM |
1126 | |
1127 | /* update delay with exact number of samples queued */ | |
1128 | runtime->delay = subs->last_delay; | |
edcd3633 | 1129 | runtime->delay += frames; |
fbcfbf5f DM |
1130 | subs->last_delay = runtime->delay; |
1131 | ||
1132 | /* realign last_frame_number */ | |
1133 | subs->last_frame_number = usb_get_current_frame_number(subs->dev); | |
1134 | subs->last_frame_number &= 0xFF; /* keep 8 LSBs */ | |
1135 | ||
edcd3633 DM |
1136 | spin_unlock_irqrestore(&subs->lock, flags); |
1137 | urb->transfer_buffer_length = bytes; | |
1138 | if (period_elapsed) | |
1139 | snd_pcm_period_elapsed(subs->pcm_substream); | |
1140 | } | |
1141 | ||
1142 | /* | |
1143 | * process after playback data complete | |
1144 | * - decrease the delay count again | |
1145 | */ | |
1146 | static void retire_playback_urb(struct snd_usb_substream *subs, | |
1147 | struct urb *urb) | |
1148 | { | |
1149 | unsigned long flags; | |
1150 | struct snd_pcm_runtime *runtime = subs->pcm_substream->runtime; | |
1151 | int stride = runtime->frame_bits >> 3; | |
1152 | int processed = urb->transfer_buffer_length / stride; | |
fbcfbf5f | 1153 | int est_delay; |
edcd3633 | 1154 | |
1213a205 TI |
1155 | /* ignore the delay accounting when procssed=0 is given, i.e. |
1156 | * silent payloads are procssed before handling the actual data | |
1157 | */ | |
1158 | if (!processed) | |
1159 | return; | |
1160 | ||
edcd3633 | 1161 | spin_lock_irqsave(&subs->lock, flags); |
fbcfbf5f DM |
1162 | est_delay = snd_usb_pcm_delay(subs, runtime->rate); |
1163 | /* update delay with exact number of samples played */ | |
1164 | if (processed > subs->last_delay) | |
1165 | subs->last_delay = 0; | |
edcd3633 | 1166 | else |
fbcfbf5f DM |
1167 | subs->last_delay -= processed; |
1168 | runtime->delay = subs->last_delay; | |
1169 | ||
1170 | /* | |
1171 | * Report when delay estimate is off by more than 2ms. | |
1172 | * The error should be lower than 2ms since the estimate relies | |
1173 | * on two reads of a counter updated every ms. | |
1174 | */ | |
1175 | if (abs(est_delay - subs->last_delay) * 1000 > runtime->rate * 2) | |
1176 | snd_printk(KERN_DEBUG "delay: estimated %d, actual %d\n", | |
1177 | est_delay, subs->last_delay); | |
1178 | ||
edcd3633 | 1179 | spin_unlock_irqrestore(&subs->lock, flags); |
e5779998 DM |
1180 | } |
1181 | ||
1182 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) | |
1183 | { | |
1184 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
1185 | } | |
1186 | ||
1187 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) | |
1188 | { | |
1189 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); | |
1190 | } | |
1191 | ||
1192 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) | |
1193 | { | |
1194 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); | |
1195 | } | |
1196 | ||
1197 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) | |
1198 | { | |
1199 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); | |
1200 | } | |
1201 | ||
edcd3633 DM |
1202 | static int snd_usb_substream_playback_trigger(struct snd_pcm_substream *substream, |
1203 | int cmd) | |
1204 | { | |
1205 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
1206 | ||
1207 | switch (cmd) { | |
1208 | case SNDRV_PCM_TRIGGER_START: | |
1209 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1210 | subs->data_endpoint->prepare_data_urb = prepare_playback_urb; | |
1211 | subs->data_endpoint->retire_data_urb = retire_playback_urb; | |
97f8d3b6 | 1212 | subs->running = 1; |
edcd3633 DM |
1213 | return 0; |
1214 | case SNDRV_PCM_TRIGGER_STOP: | |
1215 | stop_endpoints(subs, 0, 0, 0); | |
97f8d3b6 | 1216 | subs->running = 0; |
edcd3633 DM |
1217 | return 0; |
1218 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1219 | subs->data_endpoint->prepare_data_urb = NULL; | |
1220 | subs->data_endpoint->retire_data_urb = NULL; | |
97f8d3b6 | 1221 | subs->running = 0; |
edcd3633 DM |
1222 | return 0; |
1223 | } | |
1224 | ||
1225 | return -EINVAL; | |
1226 | } | |
1227 | ||
afe25967 DM |
1228 | static int snd_usb_substream_capture_trigger(struct snd_pcm_substream *substream, |
1229 | int cmd) | |
edcd3633 DM |
1230 | { |
1231 | int err; | |
1232 | struct snd_usb_substream *subs = substream->runtime->private_data; | |
1233 | ||
1234 | switch (cmd) { | |
1235 | case SNDRV_PCM_TRIGGER_START: | |
015618b9 | 1236 | err = start_endpoints(subs, 0); |
edcd3633 DM |
1237 | if (err < 0) |
1238 | return err; | |
1239 | ||
1240 | subs->data_endpoint->retire_data_urb = retire_capture_urb; | |
97f8d3b6 | 1241 | subs->running = 1; |
edcd3633 DM |
1242 | return 0; |
1243 | case SNDRV_PCM_TRIGGER_STOP: | |
1244 | stop_endpoints(subs, 0, 0, 0); | |
97f8d3b6 | 1245 | subs->running = 0; |
edcd3633 DM |
1246 | return 0; |
1247 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
1248 | subs->data_endpoint->retire_data_urb = NULL; | |
97f8d3b6 | 1249 | subs->running = 0; |
edcd3633 DM |
1250 | return 0; |
1251 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
1252 | subs->data_endpoint->retire_data_urb = retire_capture_urb; | |
97f8d3b6 | 1253 | subs->running = 1; |
edcd3633 DM |
1254 | return 0; |
1255 | } | |
1256 | ||
1257 | return -EINVAL; | |
1258 | } | |
1259 | ||
e5779998 DM |
1260 | static struct snd_pcm_ops snd_usb_playback_ops = { |
1261 | .open = snd_usb_playback_open, | |
1262 | .close = snd_usb_playback_close, | |
1263 | .ioctl = snd_pcm_lib_ioctl, | |
1264 | .hw_params = snd_usb_hw_params, | |
1265 | .hw_free = snd_usb_hw_free, | |
1266 | .prepare = snd_usb_pcm_prepare, | |
1267 | .trigger = snd_usb_substream_playback_trigger, | |
1268 | .pointer = snd_usb_pcm_pointer, | |
1269 | .page = snd_pcm_lib_get_vmalloc_page, | |
1270 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
1271 | }; | |
1272 | ||
1273 | static struct snd_pcm_ops snd_usb_capture_ops = { | |
1274 | .open = snd_usb_capture_open, | |
1275 | .close = snd_usb_capture_close, | |
1276 | .ioctl = snd_pcm_lib_ioctl, | |
1277 | .hw_params = snd_usb_hw_params, | |
1278 | .hw_free = snd_usb_hw_free, | |
1279 | .prepare = snd_usb_pcm_prepare, | |
1280 | .trigger = snd_usb_substream_capture_trigger, | |
1281 | .pointer = snd_usb_pcm_pointer, | |
1282 | .page = snd_pcm_lib_get_vmalloc_page, | |
1283 | .mmap = snd_pcm_lib_mmap_vmalloc, | |
1284 | }; | |
1285 | ||
1286 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) | |
1287 | { | |
1288 | snd_pcm_set_ops(pcm, stream, | |
1289 | stream == SNDRV_PCM_STREAM_PLAYBACK ? | |
1290 | &snd_usb_playback_ops : &snd_usb_capture_ops); | |
1291 | } |