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