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