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