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