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> |
| 18 | #include <linux/usb.h> |
| 19 | #include <linux/usb/audio.h> |
| 20 | |
| 21 | #include <sound/core.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | |
| 25 | #include "usbaudio.h" |
| 26 | #include "card.h" |
| 27 | #include "quirks.h" |
| 28 | #include "debug.h" |
| 29 | #include "urb.h" |
| 30 | #include "helper.h" |
| 31 | #include "pcm.h" |
| 32 | |
| 33 | /* |
| 34 | * return the current pcm pointer. just based on the hwptr_done value. |
| 35 | */ |
| 36 | static snd_pcm_uframes_t snd_usb_pcm_pointer(struct snd_pcm_substream *substream) |
| 37 | { |
| 38 | struct snd_usb_substream *subs; |
| 39 | unsigned int hwptr_done; |
| 40 | |
| 41 | subs = (struct snd_usb_substream *)substream->runtime->private_data; |
| 42 | spin_lock(&subs->lock); |
| 43 | hwptr_done = subs->hwptr_done; |
| 44 | spin_unlock(&subs->lock); |
| 45 | return hwptr_done / (substream->runtime->frame_bits >> 3); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * find a matching audio format |
| 50 | */ |
| 51 | static struct audioformat *find_format(struct snd_usb_substream *subs, unsigned int format, |
| 52 | unsigned int rate, unsigned int channels) |
| 53 | { |
| 54 | struct list_head *p; |
| 55 | struct audioformat *found = NULL; |
| 56 | int cur_attr = 0, attr; |
| 57 | |
| 58 | list_for_each(p, &subs->fmt_list) { |
| 59 | struct audioformat *fp; |
| 60 | fp = list_entry(p, struct audioformat, list); |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame^] | 61 | if (!(fp->formats & (1uLL << format))) |
| 62 | continue; |
| 63 | if (fp->channels != channels) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 64 | continue; |
| 65 | if (rate < fp->rate_min || rate > fp->rate_max) |
| 66 | continue; |
| 67 | if (! (fp->rates & SNDRV_PCM_RATE_CONTINUOUS)) { |
| 68 | unsigned int i; |
| 69 | for (i = 0; i < fp->nr_rates; i++) |
| 70 | if (fp->rate_table[i] == rate) |
| 71 | break; |
| 72 | if (i >= fp->nr_rates) |
| 73 | continue; |
| 74 | } |
| 75 | attr = fp->ep_attr & USB_ENDPOINT_SYNCTYPE; |
| 76 | if (! found) { |
| 77 | found = fp; |
| 78 | cur_attr = attr; |
| 79 | continue; |
| 80 | } |
| 81 | /* avoid async out and adaptive in if the other method |
| 82 | * supports the same format. |
| 83 | * this is a workaround for the case like |
| 84 | * M-audio audiophile USB. |
| 85 | */ |
| 86 | if (attr != cur_attr) { |
| 87 | if ((attr == USB_ENDPOINT_SYNC_ASYNC && |
| 88 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 89 | (attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 90 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) |
| 91 | continue; |
| 92 | if ((cur_attr == USB_ENDPOINT_SYNC_ASYNC && |
| 93 | subs->direction == SNDRV_PCM_STREAM_PLAYBACK) || |
| 94 | (cur_attr == USB_ENDPOINT_SYNC_ADAPTIVE && |
| 95 | subs->direction == SNDRV_PCM_STREAM_CAPTURE)) { |
| 96 | found = fp; |
| 97 | cur_attr = attr; |
| 98 | continue; |
| 99 | } |
| 100 | } |
| 101 | /* find the format with the largest max. packet size */ |
| 102 | if (fp->maxpacksize > found->maxpacksize) { |
| 103 | found = fp; |
| 104 | cur_attr = attr; |
| 105 | } |
| 106 | } |
| 107 | return found; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* |
| 112 | * initialize the picth control and sample rate |
| 113 | */ |
| 114 | int snd_usb_init_pitch(struct usb_device *dev, int iface, |
| 115 | struct usb_host_interface *alts, |
| 116 | struct audioformat *fmt) |
| 117 | { |
| 118 | unsigned int ep; |
| 119 | unsigned char data[1]; |
| 120 | int err; |
| 121 | |
| 122 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
| 123 | /* if endpoint has pitch control, enable it */ |
| 124 | if (fmt->attributes & UAC_EP_CS_ATTR_PITCH_CONTROL) { |
| 125 | data[0] = 1; |
| 126 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
| 127 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 128 | UAC_EP_CS_ATTR_PITCH_CONTROL << 8, ep, data, 1, 1000)) < 0) { |
| 129 | snd_printk(KERN_ERR "%d:%d:%d: cannot set enable PITCH\n", |
| 130 | dev->devnum, iface, ep); |
| 131 | return err; |
| 132 | } |
| 133 | } |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | int snd_usb_init_sample_rate(struct usb_device *dev, int iface, |
| 138 | struct usb_host_interface *alts, |
| 139 | struct audioformat *fmt, int rate) |
| 140 | { |
| 141 | unsigned int ep; |
| 142 | unsigned char data[3]; |
| 143 | int err; |
| 144 | |
| 145 | ep = get_endpoint(alts, 0)->bEndpointAddress; |
| 146 | /* if endpoint has sampling rate control, set it */ |
| 147 | if (fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE) { |
| 148 | int crate; |
| 149 | data[0] = rate; |
| 150 | data[1] = rate >> 8; |
| 151 | data[2] = rate >> 16; |
| 152 | if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR, |
| 153 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_OUT, |
| 154 | UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) { |
| 155 | snd_printk(KERN_ERR "%d:%d:%d: cannot set freq %d to ep %#x\n", |
| 156 | dev->devnum, iface, fmt->altsetting, rate, ep); |
| 157 | return err; |
| 158 | } |
| 159 | if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR, |
| 160 | USB_TYPE_CLASS|USB_RECIP_ENDPOINT|USB_DIR_IN, |
| 161 | UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep, data, 3, 1000)) < 0) { |
| 162 | snd_printk(KERN_WARNING "%d:%d:%d: cannot get freq at ep %#x\n", |
| 163 | dev->devnum, iface, fmt->altsetting, ep); |
| 164 | return 0; /* some devices don't support reading */ |
| 165 | } |
| 166 | crate = data[0] | (data[1] << 8) | (data[2] << 16); |
| 167 | if (crate != rate) { |
| 168 | snd_printd(KERN_WARNING "current rate %d is different from the runtime rate %d\n", crate, rate); |
| 169 | // runtime->rate = crate; |
| 170 | } |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * find a matching format and set up the interface |
| 177 | */ |
| 178 | static int set_format(struct snd_usb_substream *subs, struct audioformat *fmt) |
| 179 | { |
| 180 | struct usb_device *dev = subs->dev; |
| 181 | struct usb_host_interface *alts; |
| 182 | struct usb_interface_descriptor *altsd; |
| 183 | struct usb_interface *iface; |
| 184 | unsigned int ep, attr; |
| 185 | int is_playback = subs->direction == SNDRV_PCM_STREAM_PLAYBACK; |
| 186 | int err; |
| 187 | |
| 188 | iface = usb_ifnum_to_if(dev, fmt->iface); |
| 189 | if (WARN_ON(!iface)) |
| 190 | return -EINVAL; |
| 191 | alts = &iface->altsetting[fmt->altset_idx]; |
| 192 | altsd = get_iface_desc(alts); |
| 193 | if (WARN_ON(altsd->bAlternateSetting != fmt->altsetting)) |
| 194 | return -EINVAL; |
| 195 | |
| 196 | if (fmt == subs->cur_audiofmt) |
| 197 | return 0; |
| 198 | |
| 199 | /* close the old interface */ |
| 200 | if (subs->interface >= 0 && subs->interface != fmt->iface) { |
| 201 | if (usb_set_interface(subs->dev, subs->interface, 0) < 0) { |
| 202 | snd_printk(KERN_ERR "%d:%d:%d: return to setting 0 failed\n", |
| 203 | dev->devnum, fmt->iface, fmt->altsetting); |
| 204 | return -EIO; |
| 205 | } |
| 206 | subs->interface = -1; |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 207 | subs->altset_idx = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* set interface */ |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 211 | if (subs->interface != fmt->iface || subs->altset_idx != fmt->altset_idx) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 212 | if (usb_set_interface(dev, fmt->iface, fmt->altsetting) < 0) { |
| 213 | snd_printk(KERN_ERR "%d:%d:%d: usb_set_interface failed\n", |
| 214 | dev->devnum, fmt->iface, fmt->altsetting); |
| 215 | return -EIO; |
| 216 | } |
| 217 | snd_printdd(KERN_INFO "setting usb interface %d:%d\n", fmt->iface, fmt->altsetting); |
| 218 | subs->interface = fmt->iface; |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 219 | subs->altset_idx = fmt->altset_idx; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* create a data pipe */ |
| 223 | ep = fmt->endpoint & USB_ENDPOINT_NUMBER_MASK; |
| 224 | if (is_playback) |
| 225 | subs->datapipe = usb_sndisocpipe(dev, ep); |
| 226 | else |
| 227 | subs->datapipe = usb_rcvisocpipe(dev, ep); |
| 228 | subs->datainterval = fmt->datainterval; |
| 229 | subs->syncpipe = subs->syncinterval = 0; |
| 230 | subs->maxpacksize = fmt->maxpacksize; |
| 231 | subs->fill_max = 0; |
| 232 | |
| 233 | /* we need a sync pipe in async OUT or adaptive IN mode */ |
| 234 | /* check the number of EP, since some devices have broken |
| 235 | * descriptors which fool us. if it has only one EP, |
| 236 | * assume it as adaptive-out or sync-in. |
| 237 | */ |
| 238 | attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
| 239 | if (((is_playback && attr == USB_ENDPOINT_SYNC_ASYNC) || |
| 240 | (! is_playback && attr == USB_ENDPOINT_SYNC_ADAPTIVE)) && |
| 241 | altsd->bNumEndpoints >= 2) { |
| 242 | /* check sync-pipe endpoint */ |
| 243 | /* ... and check descriptor size before accessing bSynchAddress |
| 244 | because there is a version of the SB Audigy 2 NX firmware lacking |
| 245 | the audio fields in the endpoint descriptors */ |
| 246 | if ((get_endpoint(alts, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != 0x01 || |
| 247 | (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 248 | get_endpoint(alts, 1)->bSynchAddress != 0)) { |
| 249 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", |
| 250 | dev->devnum, fmt->iface, fmt->altsetting); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | ep = get_endpoint(alts, 1)->bEndpointAddress; |
| 254 | if (get_endpoint(alts, 0)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 255 | (( is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress | USB_DIR_IN)) || |
| 256 | (!is_playback && ep != (unsigned int)(get_endpoint(alts, 0)->bSynchAddress & ~USB_DIR_IN)))) { |
| 257 | snd_printk(KERN_ERR "%d:%d:%d : invalid synch pipe\n", |
| 258 | dev->devnum, fmt->iface, fmt->altsetting); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | ep &= USB_ENDPOINT_NUMBER_MASK; |
| 262 | if (is_playback) |
| 263 | subs->syncpipe = usb_rcvisocpipe(dev, ep); |
| 264 | else |
| 265 | subs->syncpipe = usb_sndisocpipe(dev, ep); |
| 266 | if (get_endpoint(alts, 1)->bLength >= USB_DT_ENDPOINT_AUDIO_SIZE && |
| 267 | get_endpoint(alts, 1)->bRefresh >= 1 && |
| 268 | get_endpoint(alts, 1)->bRefresh <= 9) |
| 269 | subs->syncinterval = get_endpoint(alts, 1)->bRefresh; |
| 270 | else if (snd_usb_get_speed(subs->dev) == USB_SPEED_FULL) |
| 271 | subs->syncinterval = 1; |
| 272 | else if (get_endpoint(alts, 1)->bInterval >= 1 && |
| 273 | get_endpoint(alts, 1)->bInterval <= 16) |
| 274 | subs->syncinterval = get_endpoint(alts, 1)->bInterval - 1; |
| 275 | else |
| 276 | subs->syncinterval = 3; |
| 277 | } |
| 278 | |
| 279 | /* always fill max packet size */ |
| 280 | if (fmt->attributes & UAC_EP_CS_ATTR_FILL_MAX) |
| 281 | subs->fill_max = 1; |
| 282 | |
| 283 | if ((err = snd_usb_init_pitch(dev, subs->interface, alts, fmt)) < 0) |
| 284 | return err; |
| 285 | |
| 286 | subs->cur_audiofmt = fmt; |
| 287 | |
| 288 | snd_usb_set_format_quirk(subs, fmt); |
| 289 | |
| 290 | #if 0 |
| 291 | printk(KERN_DEBUG |
| 292 | "setting done: format = %d, rate = %d..%d, channels = %d\n", |
| 293 | fmt->format, fmt->rate_min, fmt->rate_max, fmt->channels); |
| 294 | printk(KERN_DEBUG |
| 295 | " datapipe = 0x%0x, syncpipe = 0x%0x\n", |
| 296 | subs->datapipe, subs->syncpipe); |
| 297 | #endif |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * hw_params callback |
| 304 | * |
| 305 | * allocate a buffer and set the given audio format. |
| 306 | * |
| 307 | * so far we use a physically linear buffer although packetize transfer |
| 308 | * doesn't need a continuous area. |
| 309 | * if sg buffer is supported on the later version of alsa, we'll follow |
| 310 | * that. |
| 311 | */ |
| 312 | static int snd_usb_hw_params(struct snd_pcm_substream *substream, |
| 313 | struct snd_pcm_hw_params *hw_params) |
| 314 | { |
| 315 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 316 | struct audioformat *fmt; |
| 317 | unsigned int channels, rate, format; |
| 318 | int ret, changed; |
| 319 | |
| 320 | ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 321 | params_buffer_bytes(hw_params)); |
| 322 | if (ret < 0) |
| 323 | return ret; |
| 324 | |
| 325 | format = params_format(hw_params); |
| 326 | rate = params_rate(hw_params); |
| 327 | channels = params_channels(hw_params); |
| 328 | fmt = find_format(subs, format, rate, channels); |
| 329 | if (!fmt) { |
| 330 | snd_printd(KERN_DEBUG "cannot set format: format = %#x, rate = %d, channels = %d\n", |
| 331 | format, rate, channels); |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | |
| 335 | changed = subs->cur_audiofmt != fmt || |
| 336 | subs->period_bytes != params_period_bytes(hw_params) || |
| 337 | subs->cur_rate != rate; |
| 338 | if ((ret = set_format(subs, fmt)) < 0) |
| 339 | return ret; |
| 340 | |
| 341 | if (subs->cur_rate != rate) { |
| 342 | struct usb_host_interface *alts; |
| 343 | struct usb_interface *iface; |
| 344 | iface = usb_ifnum_to_if(subs->dev, fmt->iface); |
| 345 | alts = &iface->altsetting[fmt->altset_idx]; |
| 346 | ret = snd_usb_init_sample_rate(subs->dev, subs->interface, alts, fmt, rate); |
| 347 | if (ret < 0) |
| 348 | return ret; |
| 349 | subs->cur_rate = rate; |
| 350 | } |
| 351 | |
| 352 | if (changed) { |
| 353 | /* format changed */ |
| 354 | snd_usb_release_substream_urbs(subs, 0); |
| 355 | /* influenced: period_bytes, channels, rate, format, */ |
| 356 | ret = snd_usb_init_substream_urbs(subs, params_period_bytes(hw_params), |
| 357 | params_rate(hw_params), |
| 358 | snd_pcm_format_physical_width(params_format(hw_params)) * |
| 359 | params_channels(hw_params)); |
| 360 | } |
| 361 | |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * hw_free callback |
| 367 | * |
| 368 | * reset the audio format and release the buffer |
| 369 | */ |
| 370 | static int snd_usb_hw_free(struct snd_pcm_substream *substream) |
| 371 | { |
| 372 | struct snd_usb_substream *subs = substream->runtime->private_data; |
| 373 | |
| 374 | subs->cur_audiofmt = NULL; |
| 375 | subs->cur_rate = 0; |
| 376 | subs->period_bytes = 0; |
| 377 | if (!subs->stream->chip->shutdown) |
| 378 | snd_usb_release_substream_urbs(subs, 0); |
| 379 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * prepare callback |
| 384 | * |
| 385 | * only a few subtle things... |
| 386 | */ |
| 387 | static int snd_usb_pcm_prepare(struct snd_pcm_substream *substream) |
| 388 | { |
| 389 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 390 | struct snd_usb_substream *subs = runtime->private_data; |
| 391 | |
| 392 | if (! subs->cur_audiofmt) { |
| 393 | snd_printk(KERN_ERR "usbaudio: no format is specified!\n"); |
| 394 | return -ENXIO; |
| 395 | } |
| 396 | |
| 397 | /* some unit conversions in runtime */ |
| 398 | subs->maxframesize = bytes_to_frames(runtime, subs->maxpacksize); |
| 399 | subs->curframesize = bytes_to_frames(runtime, subs->curpacksize); |
| 400 | |
| 401 | /* reset the pointer */ |
| 402 | subs->hwptr_done = 0; |
| 403 | subs->transfer_done = 0; |
| 404 | subs->phase = 0; |
| 405 | runtime->delay = 0; |
| 406 | |
| 407 | return snd_usb_substream_prepare(subs, runtime); |
| 408 | } |
| 409 | |
| 410 | static struct snd_pcm_hardware snd_usb_hardware = |
| 411 | { |
| 412 | .info = SNDRV_PCM_INFO_MMAP | |
| 413 | SNDRV_PCM_INFO_MMAP_VALID | |
| 414 | SNDRV_PCM_INFO_BATCH | |
| 415 | SNDRV_PCM_INFO_INTERLEAVED | |
| 416 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 417 | SNDRV_PCM_INFO_PAUSE, |
| 418 | .buffer_bytes_max = 1024 * 1024, |
| 419 | .period_bytes_min = 64, |
| 420 | .period_bytes_max = 512 * 1024, |
| 421 | .periods_min = 2, |
| 422 | .periods_max = 1024, |
| 423 | }; |
| 424 | |
| 425 | static int hw_check_valid_format(struct snd_usb_substream *subs, |
| 426 | struct snd_pcm_hw_params *params, |
| 427 | struct audioformat *fp) |
| 428 | { |
| 429 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 430 | struct snd_interval *ct = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 431 | struct snd_mask *fmts = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 432 | 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^] | 433 | struct snd_mask check_fmts; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 434 | unsigned int ptime; |
| 435 | |
| 436 | /* check the format */ |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame^] | 437 | snd_mask_none(&check_fmts); |
| 438 | check_fmts.bits[0] = (u32)fp->formats; |
| 439 | check_fmts.bits[1] = (u32)(fp->formats >> 32); |
| 440 | snd_mask_intersect(&check_fmts, fmts); |
| 441 | if (snd_mask_empty(&check_fmts)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 442 | hwc_debug(" > check: no supported format %d\n", fp->format); |
| 443 | return 0; |
| 444 | } |
| 445 | /* check the channels */ |
| 446 | if (fp->channels < ct->min || fp->channels > ct->max) { |
| 447 | hwc_debug(" > check: no valid channels %d (%d/%d)\n", fp->channels, ct->min, ct->max); |
| 448 | return 0; |
| 449 | } |
| 450 | /* check the rate is within the range */ |
| 451 | if (fp->rate_min > it->max || (fp->rate_min == it->max && it->openmax)) { |
| 452 | hwc_debug(" > check: rate_min %d > max %d\n", fp->rate_min, it->max); |
| 453 | return 0; |
| 454 | } |
| 455 | if (fp->rate_max < it->min || (fp->rate_max == it->min && it->openmin)) { |
| 456 | hwc_debug(" > check: rate_max %d < min %d\n", fp->rate_max, it->min); |
| 457 | return 0; |
| 458 | } |
| 459 | /* check whether the period time is >= the data packet interval */ |
| 460 | if (snd_usb_get_speed(subs->dev) == USB_SPEED_HIGH) { |
| 461 | ptime = 125 * (1 << fp->datainterval); |
| 462 | if (ptime > pt->max || (ptime == pt->max && pt->openmax)) { |
| 463 | hwc_debug(" > check: ptime %u > max %u\n", ptime, pt->max); |
| 464 | return 0; |
| 465 | } |
| 466 | } |
| 467 | return 1; |
| 468 | } |
| 469 | |
| 470 | static int hw_rule_rate(struct snd_pcm_hw_params *params, |
| 471 | struct snd_pcm_hw_rule *rule) |
| 472 | { |
| 473 | struct snd_usb_substream *subs = rule->private; |
| 474 | struct list_head *p; |
| 475 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); |
| 476 | unsigned int rmin, rmax; |
| 477 | int changed; |
| 478 | |
| 479 | hwc_debug("hw_rule_rate: (%d,%d)\n", it->min, it->max); |
| 480 | changed = 0; |
| 481 | rmin = rmax = 0; |
| 482 | list_for_each(p, &subs->fmt_list) { |
| 483 | struct audioformat *fp; |
| 484 | fp = list_entry(p, struct audioformat, list); |
| 485 | if (!hw_check_valid_format(subs, params, fp)) |
| 486 | continue; |
| 487 | if (changed++) { |
| 488 | if (rmin > fp->rate_min) |
| 489 | rmin = fp->rate_min; |
| 490 | if (rmax < fp->rate_max) |
| 491 | rmax = fp->rate_max; |
| 492 | } else { |
| 493 | rmin = fp->rate_min; |
| 494 | rmax = fp->rate_max; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (!changed) { |
| 499 | hwc_debug(" --> get empty\n"); |
| 500 | it->empty = 1; |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
| 504 | changed = 0; |
| 505 | if (it->min < rmin) { |
| 506 | it->min = rmin; |
| 507 | it->openmin = 0; |
| 508 | changed = 1; |
| 509 | } |
| 510 | if (it->max > rmax) { |
| 511 | it->max = rmax; |
| 512 | it->openmax = 0; |
| 513 | changed = 1; |
| 514 | } |
| 515 | if (snd_interval_checkempty(it)) { |
| 516 | it->empty = 1; |
| 517 | return -EINVAL; |
| 518 | } |
| 519 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 520 | return changed; |
| 521 | } |
| 522 | |
| 523 | |
| 524 | static int hw_rule_channels(struct snd_pcm_hw_params *params, |
| 525 | struct snd_pcm_hw_rule *rule) |
| 526 | { |
| 527 | struct snd_usb_substream *subs = rule->private; |
| 528 | struct list_head *p; |
| 529 | struct snd_interval *it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); |
| 530 | unsigned int rmin, rmax; |
| 531 | int changed; |
| 532 | |
| 533 | hwc_debug("hw_rule_channels: (%d,%d)\n", it->min, it->max); |
| 534 | changed = 0; |
| 535 | rmin = rmax = 0; |
| 536 | list_for_each(p, &subs->fmt_list) { |
| 537 | struct audioformat *fp; |
| 538 | fp = list_entry(p, struct audioformat, list); |
| 539 | if (!hw_check_valid_format(subs, params, fp)) |
| 540 | continue; |
| 541 | if (changed++) { |
| 542 | if (rmin > fp->channels) |
| 543 | rmin = fp->channels; |
| 544 | if (rmax < fp->channels) |
| 545 | rmax = fp->channels; |
| 546 | } else { |
| 547 | rmin = fp->channels; |
| 548 | rmax = fp->channels; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | if (!changed) { |
| 553 | hwc_debug(" --> get empty\n"); |
| 554 | it->empty = 1; |
| 555 | return -EINVAL; |
| 556 | } |
| 557 | |
| 558 | changed = 0; |
| 559 | if (it->min < rmin) { |
| 560 | it->min = rmin; |
| 561 | it->openmin = 0; |
| 562 | changed = 1; |
| 563 | } |
| 564 | if (it->max > rmax) { |
| 565 | it->max = rmax; |
| 566 | it->openmax = 0; |
| 567 | changed = 1; |
| 568 | } |
| 569 | if (snd_interval_checkempty(it)) { |
| 570 | it->empty = 1; |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | hwc_debug(" --> (%d, %d) (changed = %d)\n", it->min, it->max, changed); |
| 574 | return changed; |
| 575 | } |
| 576 | |
| 577 | static int hw_rule_format(struct snd_pcm_hw_params *params, |
| 578 | struct snd_pcm_hw_rule *rule) |
| 579 | { |
| 580 | struct snd_usb_substream *subs = rule->private; |
| 581 | struct list_head *p; |
| 582 | struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); |
| 583 | u64 fbits; |
| 584 | u32 oldbits[2]; |
| 585 | int changed; |
| 586 | |
| 587 | hwc_debug("hw_rule_format: %x:%x\n", fmt->bits[0], fmt->bits[1]); |
| 588 | fbits = 0; |
| 589 | list_for_each(p, &subs->fmt_list) { |
| 590 | struct audioformat *fp; |
| 591 | fp = list_entry(p, struct audioformat, list); |
| 592 | if (!hw_check_valid_format(subs, params, fp)) |
| 593 | continue; |
Clemens Ladisch | 015eb0b | 2010-03-04 19:46:15 +0100 | [diff] [blame^] | 594 | fbits |= fp->formats; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | oldbits[0] = fmt->bits[0]; |
| 598 | oldbits[1] = fmt->bits[1]; |
| 599 | fmt->bits[0] &= (u32)fbits; |
| 600 | fmt->bits[1] &= (u32)(fbits >> 32); |
| 601 | if (!fmt->bits[0] && !fmt->bits[1]) { |
| 602 | hwc_debug(" --> get empty\n"); |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | changed = (oldbits[0] != fmt->bits[0] || oldbits[1] != fmt->bits[1]); |
| 606 | hwc_debug(" --> %x:%x (changed = %d)\n", fmt->bits[0], fmt->bits[1], changed); |
| 607 | return changed; |
| 608 | } |
| 609 | |
| 610 | static int hw_rule_period_time(struct snd_pcm_hw_params *params, |
| 611 | struct snd_pcm_hw_rule *rule) |
| 612 | { |
| 613 | struct snd_usb_substream *subs = rule->private; |
| 614 | struct audioformat *fp; |
| 615 | struct snd_interval *it; |
| 616 | unsigned char min_datainterval; |
| 617 | unsigned int pmin; |
| 618 | int changed; |
| 619 | |
| 620 | it = hw_param_interval(params, SNDRV_PCM_HW_PARAM_PERIOD_TIME); |
| 621 | hwc_debug("hw_rule_period_time: (%u,%u)\n", it->min, it->max); |
| 622 | min_datainterval = 0xff; |
| 623 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 624 | if (!hw_check_valid_format(subs, params, fp)) |
| 625 | continue; |
| 626 | min_datainterval = min(min_datainterval, fp->datainterval); |
| 627 | } |
| 628 | if (min_datainterval == 0xff) { |
| 629 | hwc_debug(" --> get emtpy\n"); |
| 630 | it->empty = 1; |
| 631 | return -EINVAL; |
| 632 | } |
| 633 | pmin = 125 * (1 << min_datainterval); |
| 634 | changed = 0; |
| 635 | if (it->min < pmin) { |
| 636 | it->min = pmin; |
| 637 | it->openmin = 0; |
| 638 | changed = 1; |
| 639 | } |
| 640 | if (snd_interval_checkempty(it)) { |
| 641 | it->empty = 1; |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | hwc_debug(" --> (%u,%u) (changed = %d)\n", it->min, it->max, changed); |
| 645 | return changed; |
| 646 | } |
| 647 | |
| 648 | /* |
| 649 | * If the device supports unusual bit rates, does the request meet these? |
| 650 | */ |
| 651 | static int snd_usb_pcm_check_knot(struct snd_pcm_runtime *runtime, |
| 652 | struct snd_usb_substream *subs) |
| 653 | { |
| 654 | struct audioformat *fp; |
| 655 | int count = 0, needs_knot = 0; |
| 656 | int err; |
| 657 | |
| 658 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 659 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) |
| 660 | return 0; |
| 661 | count += fp->nr_rates; |
| 662 | if (fp->rates & SNDRV_PCM_RATE_KNOT) |
| 663 | needs_knot = 1; |
| 664 | } |
| 665 | if (!needs_knot) |
| 666 | return 0; |
| 667 | |
| 668 | subs->rate_list.count = count; |
| 669 | subs->rate_list.list = kmalloc(sizeof(int) * count, GFP_KERNEL); |
| 670 | subs->rate_list.mask = 0; |
| 671 | count = 0; |
| 672 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 673 | int i; |
| 674 | for (i = 0; i < fp->nr_rates; i++) |
| 675 | subs->rate_list.list[count++] = fp->rate_table[i]; |
| 676 | } |
| 677 | err = snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 678 | &subs->rate_list); |
| 679 | if (err < 0) |
| 680 | return err; |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | |
| 686 | /* |
| 687 | * set up the runtime hardware information. |
| 688 | */ |
| 689 | |
| 690 | static int setup_hw_info(struct snd_pcm_runtime *runtime, struct snd_usb_substream *subs) |
| 691 | { |
| 692 | struct list_head *p; |
| 693 | unsigned int pt, ptmin; |
| 694 | int param_period_time_if_needed; |
| 695 | int err; |
| 696 | |
| 697 | runtime->hw.formats = subs->formats; |
| 698 | |
| 699 | runtime->hw.rate_min = 0x7fffffff; |
| 700 | runtime->hw.rate_max = 0; |
| 701 | runtime->hw.channels_min = 256; |
| 702 | runtime->hw.channels_max = 0; |
| 703 | runtime->hw.rates = 0; |
| 704 | ptmin = UINT_MAX; |
| 705 | /* check min/max rates and channels */ |
| 706 | list_for_each(p, &subs->fmt_list) { |
| 707 | struct audioformat *fp; |
| 708 | fp = list_entry(p, struct audioformat, list); |
| 709 | runtime->hw.rates |= fp->rates; |
| 710 | if (runtime->hw.rate_min > fp->rate_min) |
| 711 | runtime->hw.rate_min = fp->rate_min; |
| 712 | if (runtime->hw.rate_max < fp->rate_max) |
| 713 | runtime->hw.rate_max = fp->rate_max; |
| 714 | if (runtime->hw.channels_min > fp->channels) |
| 715 | runtime->hw.channels_min = fp->channels; |
| 716 | if (runtime->hw.channels_max < fp->channels) |
| 717 | runtime->hw.channels_max = fp->channels; |
| 718 | if (fp->fmt_type == UAC_FORMAT_TYPE_II && fp->frame_size > 0) { |
| 719 | /* FIXME: there might be more than one audio formats... */ |
| 720 | runtime->hw.period_bytes_min = runtime->hw.period_bytes_max = |
| 721 | fp->frame_size; |
| 722 | } |
| 723 | pt = 125 * (1 << fp->datainterval); |
| 724 | ptmin = min(ptmin, pt); |
| 725 | } |
| 726 | |
| 727 | param_period_time_if_needed = SNDRV_PCM_HW_PARAM_PERIOD_TIME; |
| 728 | if (snd_usb_get_speed(subs->dev) != USB_SPEED_HIGH) |
| 729 | /* full speed devices have fixed data packet interval */ |
| 730 | ptmin = 1000; |
| 731 | if (ptmin == 1000) |
| 732 | /* if period time doesn't go below 1 ms, no rules needed */ |
| 733 | param_period_time_if_needed = -1; |
| 734 | snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 735 | ptmin, UINT_MAX); |
| 736 | |
| 737 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, |
| 738 | hw_rule_rate, subs, |
| 739 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 740 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 741 | param_period_time_if_needed, |
| 742 | -1)) < 0) |
| 743 | return err; |
| 744 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 745 | hw_rule_channels, subs, |
| 746 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 747 | SNDRV_PCM_HW_PARAM_RATE, |
| 748 | param_period_time_if_needed, |
| 749 | -1)) < 0) |
| 750 | return err; |
| 751 | if ((err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_FORMAT, |
| 752 | hw_rule_format, subs, |
| 753 | SNDRV_PCM_HW_PARAM_RATE, |
| 754 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 755 | param_period_time_if_needed, |
| 756 | -1)) < 0) |
| 757 | return err; |
| 758 | if (param_period_time_if_needed >= 0) { |
| 759 | err = snd_pcm_hw_rule_add(runtime, 0, |
| 760 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 761 | hw_rule_period_time, subs, |
| 762 | SNDRV_PCM_HW_PARAM_FORMAT, |
| 763 | SNDRV_PCM_HW_PARAM_CHANNELS, |
| 764 | SNDRV_PCM_HW_PARAM_RATE, |
| 765 | -1); |
| 766 | if (err < 0) |
| 767 | return err; |
| 768 | } |
| 769 | if ((err = snd_usb_pcm_check_knot(runtime, subs)) < 0) |
| 770 | return err; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static int snd_usb_pcm_open(struct snd_pcm_substream *substream, int direction) |
| 775 | { |
| 776 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 777 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 778 | struct snd_usb_substream *subs = &as->substream[direction]; |
| 779 | |
| 780 | subs->interface = -1; |
Clemens Ladisch | e11b4e0 | 2010-03-04 19:46:14 +0100 | [diff] [blame] | 781 | subs->altset_idx = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 782 | runtime->hw = snd_usb_hardware; |
| 783 | runtime->private_data = subs; |
| 784 | subs->pcm_substream = substream; |
| 785 | return setup_hw_info(runtime, subs); |
| 786 | } |
| 787 | |
| 788 | static int snd_usb_pcm_close(struct snd_pcm_substream *substream, int direction) |
| 789 | { |
| 790 | struct snd_usb_stream *as = snd_pcm_substream_chip(substream); |
| 791 | struct snd_usb_substream *subs = &as->substream[direction]; |
| 792 | |
| 793 | if (!as->chip->shutdown && subs->interface >= 0) { |
| 794 | usb_set_interface(subs->dev, subs->interface, 0); |
| 795 | subs->interface = -1; |
| 796 | } |
| 797 | subs->pcm_substream = NULL; |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static int snd_usb_playback_open(struct snd_pcm_substream *substream) |
| 802 | { |
| 803 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_PLAYBACK); |
| 804 | } |
| 805 | |
| 806 | static int snd_usb_playback_close(struct snd_pcm_substream *substream) |
| 807 | { |
| 808 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_PLAYBACK); |
| 809 | } |
| 810 | |
| 811 | static int snd_usb_capture_open(struct snd_pcm_substream *substream) |
| 812 | { |
| 813 | return snd_usb_pcm_open(substream, SNDRV_PCM_STREAM_CAPTURE); |
| 814 | } |
| 815 | |
| 816 | static int snd_usb_capture_close(struct snd_pcm_substream *substream) |
| 817 | { |
| 818 | return snd_usb_pcm_close(substream, SNDRV_PCM_STREAM_CAPTURE); |
| 819 | } |
| 820 | |
| 821 | static struct snd_pcm_ops snd_usb_playback_ops = { |
| 822 | .open = snd_usb_playback_open, |
| 823 | .close = snd_usb_playback_close, |
| 824 | .ioctl = snd_pcm_lib_ioctl, |
| 825 | .hw_params = snd_usb_hw_params, |
| 826 | .hw_free = snd_usb_hw_free, |
| 827 | .prepare = snd_usb_pcm_prepare, |
| 828 | .trigger = snd_usb_substream_playback_trigger, |
| 829 | .pointer = snd_usb_pcm_pointer, |
| 830 | .page = snd_pcm_lib_get_vmalloc_page, |
| 831 | .mmap = snd_pcm_lib_mmap_vmalloc, |
| 832 | }; |
| 833 | |
| 834 | static struct snd_pcm_ops snd_usb_capture_ops = { |
| 835 | .open = snd_usb_capture_open, |
| 836 | .close = snd_usb_capture_close, |
| 837 | .ioctl = snd_pcm_lib_ioctl, |
| 838 | .hw_params = snd_usb_hw_params, |
| 839 | .hw_free = snd_usb_hw_free, |
| 840 | .prepare = snd_usb_pcm_prepare, |
| 841 | .trigger = snd_usb_substream_capture_trigger, |
| 842 | .pointer = snd_usb_pcm_pointer, |
| 843 | .page = snd_pcm_lib_get_vmalloc_page, |
| 844 | .mmap = snd_pcm_lib_mmap_vmalloc, |
| 845 | }; |
| 846 | |
| 847 | void snd_usb_set_pcm_ops(struct snd_pcm *pcm, int stream) |
| 848 | { |
| 849 | snd_pcm_set_ops(pcm, stream, |
| 850 | stream == SNDRV_PCM_STREAM_PLAYBACK ? |
| 851 | &snd_usb_playback_ops : &snd_usb_capture_ops); |
| 852 | } |