Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Edirol UA-101 driver |
| 3 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> |
| 4 | * |
| 5 | * This driver is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License, version 2. |
| 7 | * |
| 8 | * This driver is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this driver. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/usb.h> |
| 21 | #include <linux/usb/audio.h> |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 22 | #include <sound/core.h> |
| 23 | #include <sound/initval.h> |
| 24 | #include <sound/pcm.h> |
| 25 | #include <sound/pcm_params.h> |
| 26 | #include "usbaudio.h" |
| 27 | |
| 28 | MODULE_DESCRIPTION("Edirol UA-101 driver"); |
| 29 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 30 | MODULE_LICENSE("GPL v2"); |
| 31 | MODULE_SUPPORTED_DEVICE("{{Edirol,UA-101}}"); |
| 32 | |
| 33 | /* I use my UA-1A for testing because I don't have a UA-101 ... */ |
| 34 | #define UA1A_HACK |
| 35 | |
| 36 | /* |
| 37 | * Should not be lower than the minimum scheduling delay of the host |
| 38 | * controller. Some Intel controllers need more than one frame; as long as |
| 39 | * that driver doesn't tell us about this, use 1.5 frames just to be sure. |
| 40 | */ |
| 41 | #define MIN_QUEUE_LENGTH 12 |
| 42 | /* Somewhat random. */ |
| 43 | #define MAX_QUEUE_LENGTH 30 |
| 44 | /* |
| 45 | * This magic value optimizes memory usage efficiency for the UA-101's packet |
| 46 | * sizes at all sample rates, taking into account the stupid cache pool sizes |
| 47 | * that usb_buffer_alloc() uses. |
| 48 | */ |
| 49 | #define DEFAULT_QUEUE_LENGTH 21 |
| 50 | |
| 51 | #define MAX_PACKET_SIZE 672 /* hardware specific */ |
| 52 | #define MAX_MEMORY_BUFFERS DIV_ROUND_UP(MAX_QUEUE_LENGTH, \ |
| 53 | PAGE_SIZE / MAX_PACKET_SIZE) |
| 54 | |
| 55 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; |
| 56 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; |
| 57 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; |
| 58 | static unsigned int queue_length = 21; |
| 59 | |
| 60 | module_param_array(index, int, NULL, 0444); |
| 61 | MODULE_PARM_DESC(index, "card index"); |
| 62 | module_param_array(id, charp, NULL, 0444); |
| 63 | MODULE_PARM_DESC(id, "ID string"); |
| 64 | module_param_array(enable, bool, NULL, 0444); |
| 65 | MODULE_PARM_DESC(enable, "enable card"); |
| 66 | module_param(queue_length, uint, 0644); |
| 67 | MODULE_PARM_DESC(queue_length, "USB queue length in microframes, " |
| 68 | __stringify(MIN_QUEUE_LENGTH)"-"__stringify(MAX_QUEUE_LENGTH)); |
| 69 | |
| 70 | enum { |
| 71 | INTF_PLAYBACK, |
| 72 | INTF_CAPTURE, |
| 73 | INTF_MIDI, |
| 74 | |
| 75 | INTF_COUNT |
| 76 | }; |
| 77 | |
| 78 | /* bits in struct ua101::states */ |
| 79 | enum { |
| 80 | USB_CAPTURE_RUNNING, |
| 81 | USB_PLAYBACK_RUNNING, |
| 82 | ALSA_CAPTURE_OPEN, |
| 83 | ALSA_PLAYBACK_OPEN, |
| 84 | ALSA_CAPTURE_RUNNING, |
| 85 | ALSA_PLAYBACK_RUNNING, |
| 86 | CAPTURE_URB_COMPLETED, |
| 87 | PLAYBACK_URB_COMPLETED, |
| 88 | DISCONNECTED, |
| 89 | }; |
| 90 | |
| 91 | struct ua101 { |
| 92 | struct usb_device *dev; |
| 93 | struct snd_card *card; |
| 94 | struct usb_interface *intf[INTF_COUNT]; |
| 95 | int card_index; |
| 96 | struct snd_pcm *pcm; |
| 97 | struct list_head midi_list; |
| 98 | u64 format_bit; |
| 99 | unsigned int rate; |
| 100 | unsigned int packets_per_second; |
| 101 | spinlock_t lock; |
| 102 | struct mutex mutex; |
| 103 | unsigned long states; |
| 104 | |
| 105 | /* FIFO to synchronize playback rate to capture rate */ |
| 106 | unsigned int rate_feedback_start; |
| 107 | unsigned int rate_feedback_count; |
| 108 | u8 rate_feedback[MAX_QUEUE_LENGTH]; |
| 109 | |
| 110 | struct list_head ready_playback_urbs; |
| 111 | struct tasklet_struct playback_tasklet; |
| 112 | wait_queue_head_t alsa_capture_wait; |
| 113 | wait_queue_head_t rate_feedback_wait; |
| 114 | wait_queue_head_t alsa_playback_wait; |
| 115 | struct ua101_stream { |
| 116 | struct snd_pcm_substream *substream; |
| 117 | unsigned int usb_pipe; |
| 118 | unsigned int channels; |
| 119 | unsigned int frame_bytes; |
| 120 | unsigned int max_packet_bytes; |
| 121 | unsigned int period_pos; |
| 122 | unsigned int buffer_pos; |
| 123 | unsigned int queue_length; |
| 124 | struct ua101_urb { |
| 125 | struct urb urb; |
| 126 | struct usb_iso_packet_descriptor iso_frame_desc[1]; |
| 127 | struct list_head ready_list; |
| 128 | } *urbs[MAX_QUEUE_LENGTH]; |
| 129 | struct { |
| 130 | unsigned int size; |
| 131 | void *addr; |
| 132 | dma_addr_t dma; |
| 133 | } buffers[MAX_MEMORY_BUFFERS]; |
| 134 | } capture, playback; |
| 135 | |
| 136 | unsigned int fps[10]; |
| 137 | unsigned int frame_counter; |
| 138 | }; |
| 139 | |
| 140 | static DEFINE_MUTEX(devices_mutex); |
| 141 | static unsigned int devices_used; |
| 142 | static struct usb_driver ua101_driver; |
| 143 | |
| 144 | static void abort_alsa_playback(struct ua101 *ua); |
| 145 | static void abort_alsa_capture(struct ua101 *ua); |
| 146 | |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 147 | static const char *usb_error_string(int err) |
| 148 | { |
| 149 | switch (err) { |
| 150 | case -ENODEV: |
| 151 | return "no device"; |
| 152 | case -ENOENT: |
| 153 | return "endpoint not enabled"; |
| 154 | case -EPIPE: |
| 155 | return "endpoint stalled"; |
| 156 | case -ENOSPC: |
| 157 | return "not enough bandwidth"; |
| 158 | case -ESHUTDOWN: |
| 159 | return "device disabled"; |
| 160 | case -EHOSTUNREACH: |
| 161 | return "device suspended"; |
| 162 | case -EINVAL: |
| 163 | case -EAGAIN: |
| 164 | case -EFBIG: |
| 165 | case -EMSGSIZE: |
| 166 | return "internal error"; |
| 167 | default: |
| 168 | return "unknown error"; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | static void abort_usb_capture(struct ua101 *ua) |
| 173 | { |
| 174 | if (test_and_clear_bit(USB_CAPTURE_RUNNING, &ua->states)) { |
| 175 | wake_up(&ua->alsa_capture_wait); |
| 176 | wake_up(&ua->rate_feedback_wait); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | static void abort_usb_playback(struct ua101 *ua) |
| 181 | { |
| 182 | if (test_and_clear_bit(USB_PLAYBACK_RUNNING, &ua->states)) |
| 183 | wake_up(&ua->alsa_playback_wait); |
| 184 | } |
| 185 | |
| 186 | static void playback_urb_complete(struct urb *usb_urb) |
| 187 | { |
| 188 | struct ua101_urb *urb = (struct ua101_urb *)usb_urb; |
| 189 | struct ua101 *ua = urb->urb.context; |
| 190 | unsigned long flags; |
| 191 | |
| 192 | if (unlikely(urb->urb.status == -ENOENT || /* unlinked */ |
| 193 | urb->urb.status == -ENODEV || /* device removed */ |
| 194 | urb->urb.status == -ECONNRESET || /* unlinked */ |
| 195 | urb->urb.status == -ESHUTDOWN)) { /* device disabled */ |
| 196 | abort_usb_playback(ua); |
| 197 | abort_alsa_playback(ua); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) { |
| 202 | /* append URB to FIFO */ |
| 203 | spin_lock_irqsave(&ua->lock, flags); |
| 204 | list_add_tail(&urb->ready_list, &ua->ready_playback_urbs); |
| 205 | if (ua->rate_feedback_count > 0) |
| 206 | tasklet_schedule(&ua->playback_tasklet); |
| 207 | ua->playback.substream->runtime->delay -= |
| 208 | urb->urb.iso_frame_desc[0].length / |
| 209 | ua->playback.frame_bytes; |
| 210 | spin_unlock_irqrestore(&ua->lock, flags); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void first_playback_urb_complete(struct urb *urb) |
| 215 | { |
| 216 | struct ua101 *ua = urb->context; |
| 217 | |
| 218 | urb->complete = playback_urb_complete; |
| 219 | playback_urb_complete(urb); |
| 220 | |
| 221 | set_bit(PLAYBACK_URB_COMPLETED, &ua->states); |
| 222 | wake_up(&ua->alsa_playback_wait); |
| 223 | } |
| 224 | |
| 225 | /* copy data from the ALSA ring buffer into the URB buffer */ |
| 226 | static bool copy_playback_data(struct ua101_stream *stream, struct urb *urb, |
| 227 | unsigned int frames) |
| 228 | { |
| 229 | struct snd_pcm_runtime *runtime; |
| 230 | unsigned int frame_bytes, frames1; |
| 231 | const u8 *source; |
| 232 | |
| 233 | runtime = stream->substream->runtime; |
| 234 | frame_bytes = stream->frame_bytes; |
| 235 | source = runtime->dma_area + stream->buffer_pos * frame_bytes; |
| 236 | if (stream->buffer_pos + frames <= runtime->buffer_size) { |
| 237 | memcpy(urb->transfer_buffer, source, frames * frame_bytes); |
| 238 | } else { |
| 239 | /* wrap around at end of ring buffer */ |
| 240 | frames1 = runtime->buffer_size - stream->buffer_pos; |
| 241 | memcpy(urb->transfer_buffer, source, frames1 * frame_bytes); |
| 242 | memcpy(urb->transfer_buffer + frames1 * frame_bytes, |
| 243 | runtime->dma_area, (frames - frames1) * frame_bytes); |
| 244 | } |
| 245 | |
| 246 | stream->buffer_pos += frames; |
| 247 | if (stream->buffer_pos >= runtime->buffer_size) |
| 248 | stream->buffer_pos -= runtime->buffer_size; |
| 249 | stream->period_pos += frames; |
| 250 | if (stream->period_pos >= runtime->period_size) { |
| 251 | stream->period_pos -= runtime->period_size; |
| 252 | return true; |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | static inline void add_with_wraparound(struct ua101 *ua, |
| 258 | unsigned int *value, unsigned int add) |
| 259 | { |
| 260 | *value += add; |
| 261 | if (*value >= ua->playback.queue_length) |
| 262 | *value -= ua->playback.queue_length; |
| 263 | } |
| 264 | |
| 265 | static void playback_tasklet(unsigned long data) |
| 266 | { |
| 267 | struct ua101 *ua = (void *)data; |
| 268 | unsigned long flags; |
| 269 | unsigned int frames; |
| 270 | struct ua101_urb *urb; |
| 271 | bool do_period_elapsed = false; |
| 272 | int err; |
| 273 | |
| 274 | if (unlikely(!test_bit(USB_PLAYBACK_RUNNING, &ua->states))) |
| 275 | return; |
| 276 | |
| 277 | /* |
| 278 | * Synchronizing the playback rate to the capture rate is done by using |
| 279 | * the same sequence of packet sizes for both streams. |
| 280 | * Submitting a playback URB therefore requires both a ready URB and |
| 281 | * the size of the corresponding capture packet, i.e., both playback |
| 282 | * and capture URBs must have been completed. Since the USB core does |
| 283 | * not guarantee that playback and capture complete callbacks are |
| 284 | * called alternately, we use two FIFOs for packet sizes and read URBs; |
| 285 | * submitting playback URBs is possible as long as both FIFOs are |
| 286 | * nonempty. |
| 287 | */ |
| 288 | spin_lock_irqsave(&ua->lock, flags); |
| 289 | while (ua->rate_feedback_count > 0 && |
| 290 | !list_empty(&ua->ready_playback_urbs)) { |
| 291 | /* take packet size out of FIFO */ |
| 292 | frames = ua->rate_feedback[ua->rate_feedback_start]; |
| 293 | add_with_wraparound(ua, &ua->rate_feedback_start, 1); |
| 294 | ua->rate_feedback_count--; |
| 295 | |
| 296 | /* take URB out of FIFO */ |
| 297 | urb = list_first_entry(&ua->ready_playback_urbs, |
| 298 | struct ua101_urb, ready_list); |
| 299 | list_del(&urb->ready_list); |
| 300 | |
| 301 | /* fill packet with data or silence */ |
| 302 | urb->urb.iso_frame_desc[0].length = |
| 303 | frames * ua->playback.frame_bytes; |
| 304 | if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) |
| 305 | do_period_elapsed |= copy_playback_data(&ua->playback, |
| 306 | &urb->urb, |
| 307 | frames); |
| 308 | else |
| 309 | memset(urb->urb.transfer_buffer, 0, |
| 310 | urb->urb.iso_frame_desc[0].length); |
| 311 | |
| 312 | /* and off you go ... */ |
| 313 | err = usb_submit_urb(&urb->urb, GFP_ATOMIC); |
| 314 | if (unlikely(err < 0)) { |
| 315 | spin_unlock_irqrestore(&ua->lock, flags); |
| 316 | abort_usb_playback(ua); |
| 317 | abort_alsa_playback(ua); |
| 318 | dev_err(&ua->dev->dev, "USB request error %d: %s\n", |
| 319 | err, usb_error_string(err)); |
| 320 | return; |
| 321 | } |
| 322 | ua->playback.substream->runtime->delay += frames; |
| 323 | } |
| 324 | spin_unlock_irqrestore(&ua->lock, flags); |
| 325 | if (do_period_elapsed) |
| 326 | snd_pcm_period_elapsed(ua->playback.substream); |
| 327 | } |
| 328 | |
| 329 | /* copy data from the URB buffer into the ALSA ring buffer */ |
| 330 | static bool copy_capture_data(struct ua101_stream *stream, struct urb *urb, |
| 331 | unsigned int frames) |
| 332 | { |
| 333 | struct snd_pcm_runtime *runtime; |
| 334 | unsigned int frame_bytes, frames1; |
| 335 | u8 *dest; |
| 336 | |
| 337 | runtime = stream->substream->runtime; |
| 338 | frame_bytes = stream->frame_bytes; |
| 339 | dest = runtime->dma_area + stream->buffer_pos * frame_bytes; |
| 340 | if (stream->buffer_pos + frames <= runtime->buffer_size) { |
| 341 | memcpy(dest, urb->transfer_buffer, frames * frame_bytes); |
| 342 | } else { |
| 343 | /* wrap around at end of ring buffer */ |
| 344 | frames1 = runtime->buffer_size - stream->buffer_pos; |
| 345 | memcpy(dest, urb->transfer_buffer, frames1 * frame_bytes); |
| 346 | memcpy(runtime->dma_area, |
| 347 | urb->transfer_buffer + frames1 * frame_bytes, |
| 348 | (frames - frames1) * frame_bytes); |
| 349 | } |
| 350 | |
| 351 | stream->buffer_pos += frames; |
| 352 | if (stream->buffer_pos >= runtime->buffer_size) |
| 353 | stream->buffer_pos -= runtime->buffer_size; |
| 354 | stream->period_pos += frames; |
| 355 | if (stream->period_pos >= runtime->period_size) { |
| 356 | stream->period_pos -= runtime->period_size; |
| 357 | return true; |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | static void capture_urb_complete(struct urb *urb) |
| 363 | { |
| 364 | struct ua101 *ua = urb->context; |
| 365 | struct ua101_stream *stream = &ua->capture; |
| 366 | unsigned long flags; |
| 367 | unsigned int frames, write_ptr; |
| 368 | bool do_period_elapsed; |
| 369 | int err; |
| 370 | |
| 371 | if (unlikely(urb->status == -ENOENT || /* unlinked */ |
| 372 | urb->status == -ENODEV || /* device removed */ |
| 373 | urb->status == -ECONNRESET || /* unlinked */ |
| 374 | urb->status == -ESHUTDOWN)) /* device disabled */ |
| 375 | goto stream_stopped; |
| 376 | |
| 377 | if (urb->status >= 0 && urb->iso_frame_desc[0].status >= 0) |
| 378 | frames = urb->iso_frame_desc[0].actual_length / |
| 379 | stream->frame_bytes; |
| 380 | else |
| 381 | frames = 0; |
| 382 | |
| 383 | spin_lock_irqsave(&ua->lock, flags); |
| 384 | |
| 385 | if (frames > 0 && test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) |
| 386 | do_period_elapsed = copy_capture_data(stream, urb, frames); |
| 387 | else |
| 388 | do_period_elapsed = false; |
| 389 | |
| 390 | if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) { |
| 391 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 392 | if (unlikely(err < 0)) { |
| 393 | spin_unlock_irqrestore(&ua->lock, flags); |
| 394 | dev_err(&ua->dev->dev, "USB request error %d: %s\n", |
| 395 | err, usb_error_string(err)); |
| 396 | goto stream_stopped; |
| 397 | } |
| 398 | |
| 399 | /* append packet size to FIFO */ |
| 400 | write_ptr = ua->rate_feedback_start; |
| 401 | add_with_wraparound(ua, &write_ptr, ua->rate_feedback_count); |
| 402 | ua->rate_feedback[write_ptr] = frames; |
| 403 | if (ua->rate_feedback_count < ua->playback.queue_length) { |
| 404 | ua->rate_feedback_count++; |
| 405 | if (ua->rate_feedback_count == |
| 406 | ua->playback.queue_length) |
| 407 | wake_up(&ua->rate_feedback_wait); |
| 408 | } else { |
| 409 | /* |
| 410 | * Ring buffer overflow; this happens when the playback |
| 411 | * stream is not running. Throw away the oldest entry, |
| 412 | * so that the playback stream, when it starts, sees |
| 413 | * the most recent packet sizes. |
| 414 | */ |
| 415 | add_with_wraparound(ua, &ua->rate_feedback_start, 1); |
| 416 | } |
| 417 | if (test_bit(USB_PLAYBACK_RUNNING, &ua->states) && |
| 418 | !list_empty(&ua->ready_playback_urbs)) |
| 419 | tasklet_schedule(&ua->playback_tasklet); |
| 420 | } |
| 421 | |
| 422 | spin_unlock_irqrestore(&ua->lock, flags); |
| 423 | |
| 424 | if (do_period_elapsed) |
| 425 | snd_pcm_period_elapsed(stream->substream); |
| 426 | |
| 427 | /* for debugging: measure the sample rate relative to the USB clock */ |
| 428 | ua->fps[ua->frame_counter++ / ua->packets_per_second] += frames; |
| 429 | if (ua->frame_counter >= ARRAY_SIZE(ua->fps) * ua->packets_per_second) { |
| 430 | printk(KERN_DEBUG "capture rate:"); |
| 431 | for (frames = 0; frames < ARRAY_SIZE(ua->fps); ++frames) |
| 432 | printk(KERN_CONT " %u", ua->fps[frames]); |
| 433 | printk(KERN_CONT "\n"); |
| 434 | memset(ua->fps, 0, sizeof(ua->fps)); |
| 435 | ua->frame_counter = 0; |
| 436 | } |
| 437 | return; |
| 438 | |
| 439 | stream_stopped: |
| 440 | abort_usb_playback(ua); |
| 441 | abort_usb_capture(ua); |
| 442 | abort_alsa_playback(ua); |
| 443 | abort_alsa_capture(ua); |
| 444 | } |
| 445 | |
| 446 | static void first_capture_urb_complete(struct urb *urb) |
| 447 | { |
| 448 | struct ua101 *ua = urb->context; |
| 449 | |
| 450 | urb->complete = capture_urb_complete; |
| 451 | capture_urb_complete(urb); |
| 452 | |
| 453 | set_bit(CAPTURE_URB_COMPLETED, &ua->states); |
| 454 | wake_up(&ua->alsa_capture_wait); |
| 455 | } |
| 456 | |
| 457 | static int submit_stream_urbs(struct ua101 *ua, struct ua101_stream *stream) |
| 458 | { |
| 459 | unsigned int i; |
| 460 | |
| 461 | for (i = 0; i < stream->queue_length; ++i) { |
| 462 | int err = usb_submit_urb(&stream->urbs[i]->urb, GFP_KERNEL); |
| 463 | if (err < 0) { |
| 464 | dev_err(&ua->dev->dev, "USB request error %d: %s\n", |
| 465 | err, usb_error_string(err)); |
| 466 | return err; |
| 467 | } |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static void kill_stream_urbs(struct ua101_stream *stream) |
| 473 | { |
| 474 | unsigned int i; |
| 475 | |
| 476 | for (i = 0; i < stream->queue_length; ++i) |
| 477 | usb_kill_urb(&stream->urbs[i]->urb); |
| 478 | } |
| 479 | |
| 480 | static int enable_iso_interface(struct ua101 *ua, unsigned int intf_index) |
| 481 | { |
| 482 | struct usb_host_interface *alts; |
| 483 | |
| 484 | alts = ua->intf[intf_index]->cur_altsetting; |
| 485 | if (alts->desc.bAlternateSetting != 1) { |
| 486 | int err = usb_set_interface(ua->dev, |
| 487 | alts->desc.bInterfaceNumber, 1); |
| 488 | if (err < 0) { |
| 489 | dev_err(&ua->dev->dev, |
| 490 | "cannot initialize interface; error %d: %s\n", |
| 491 | err, usb_error_string(err)); |
| 492 | return err; |
| 493 | } |
| 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static void disable_iso_interface(struct ua101 *ua, unsigned int intf_index) |
| 499 | { |
| 500 | struct usb_host_interface *alts; |
| 501 | |
| 502 | alts = ua->intf[intf_index]->cur_altsetting; |
| 503 | if (alts->desc.bAlternateSetting != 0) { |
| 504 | int err = usb_set_interface(ua->dev, |
| 505 | alts->desc.bInterfaceNumber, 0); |
| 506 | if (err < 0 && !test_bit(DISCONNECTED, &ua->states)) |
| 507 | dev_warn(&ua->dev->dev, |
| 508 | "interface reset failed; error %d: %s\n", |
| 509 | err, usb_error_string(err)); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | static void stop_usb_capture(struct ua101 *ua) |
| 514 | { |
| 515 | clear_bit(USB_CAPTURE_RUNNING, &ua->states); |
| 516 | |
| 517 | kill_stream_urbs(&ua->capture); |
| 518 | |
| 519 | disable_iso_interface(ua, INTF_CAPTURE); |
| 520 | } |
| 521 | |
| 522 | static int start_usb_capture(struct ua101 *ua) |
| 523 | { |
| 524 | int err; |
| 525 | |
| 526 | if (test_bit(DISCONNECTED, &ua->states)) |
| 527 | return -ENODEV; |
| 528 | |
| 529 | if (test_bit(USB_CAPTURE_RUNNING, &ua->states)) |
| 530 | return 0; |
| 531 | |
| 532 | kill_stream_urbs(&ua->capture); |
| 533 | |
| 534 | err = enable_iso_interface(ua, INTF_CAPTURE); |
| 535 | if (err < 0) |
| 536 | return err; |
| 537 | |
| 538 | clear_bit(CAPTURE_URB_COMPLETED, &ua->states); |
| 539 | ua->capture.urbs[0]->urb.complete = first_capture_urb_complete; |
| 540 | ua->rate_feedback_start = 0; |
| 541 | ua->rate_feedback_count = 0; |
| 542 | |
| 543 | set_bit(USB_CAPTURE_RUNNING, &ua->states); |
| 544 | err = submit_stream_urbs(ua, &ua->capture); |
| 545 | if (err < 0) |
| 546 | stop_usb_capture(ua); |
| 547 | return err; |
| 548 | } |
| 549 | |
| 550 | static void stop_usb_playback(struct ua101 *ua) |
| 551 | { |
| 552 | clear_bit(USB_PLAYBACK_RUNNING, &ua->states); |
| 553 | |
| 554 | kill_stream_urbs(&ua->playback); |
| 555 | |
| 556 | tasklet_kill(&ua->playback_tasklet); |
| 557 | |
| 558 | disable_iso_interface(ua, INTF_PLAYBACK); |
| 559 | } |
| 560 | |
| 561 | static int start_usb_playback(struct ua101 *ua) |
| 562 | { |
| 563 | unsigned int i, frames; |
| 564 | struct urb *urb; |
| 565 | int err = 0; |
| 566 | |
| 567 | if (test_bit(DISCONNECTED, &ua->states)) |
| 568 | return -ENODEV; |
| 569 | |
| 570 | if (test_bit(USB_PLAYBACK_RUNNING, &ua->states)) |
| 571 | return 0; |
| 572 | |
| 573 | kill_stream_urbs(&ua->playback); |
| 574 | tasklet_kill(&ua->playback_tasklet); |
| 575 | |
| 576 | err = enable_iso_interface(ua, INTF_PLAYBACK); |
| 577 | if (err < 0) |
| 578 | return err; |
| 579 | |
| 580 | clear_bit(PLAYBACK_URB_COMPLETED, &ua->states); |
| 581 | ua->playback.urbs[0]->urb.complete = |
| 582 | first_playback_urb_complete; |
| 583 | spin_lock_irq(&ua->lock); |
| 584 | INIT_LIST_HEAD(&ua->ready_playback_urbs); |
| 585 | spin_unlock_irq(&ua->lock); |
| 586 | |
| 587 | /* |
| 588 | * We submit the initial URBs all at once, so we have to wait for the |
| 589 | * packet size FIFO to be full. |
| 590 | */ |
| 591 | wait_event(ua->rate_feedback_wait, |
| 592 | ua->rate_feedback_count >= ua->playback.queue_length || |
| 593 | !test_bit(USB_CAPTURE_RUNNING, &ua->states) || |
| 594 | test_bit(DISCONNECTED, &ua->states)); |
| 595 | if (test_bit(DISCONNECTED, &ua->states)) { |
| 596 | stop_usb_playback(ua); |
| 597 | return -ENODEV; |
| 598 | } |
| 599 | if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) { |
| 600 | stop_usb_playback(ua); |
| 601 | return -EIO; |
| 602 | } |
| 603 | |
| 604 | for (i = 0; i < ua->playback.queue_length; ++i) { |
| 605 | /* all initial URBs contain silence */ |
| 606 | spin_lock_irq(&ua->lock); |
| 607 | frames = ua->rate_feedback[ua->rate_feedback_start]; |
| 608 | add_with_wraparound(ua, &ua->rate_feedback_start, 1); |
| 609 | ua->rate_feedback_count--; |
| 610 | spin_unlock_irq(&ua->lock); |
| 611 | urb = &ua->playback.urbs[i]->urb; |
| 612 | urb->iso_frame_desc[0].length = |
| 613 | frames * ua->playback.frame_bytes; |
| 614 | memset(urb->transfer_buffer, 0, |
| 615 | urb->iso_frame_desc[0].length); |
| 616 | } |
| 617 | |
| 618 | set_bit(USB_PLAYBACK_RUNNING, &ua->states); |
| 619 | err = submit_stream_urbs(ua, &ua->playback); |
| 620 | if (err < 0) |
| 621 | stop_usb_playback(ua); |
| 622 | return err; |
| 623 | } |
| 624 | |
| 625 | static void abort_alsa_capture(struct ua101 *ua) |
| 626 | { |
| 627 | if (test_bit(ALSA_CAPTURE_RUNNING, &ua->states)) |
| 628 | snd_pcm_stop(ua->capture.substream, SNDRV_PCM_STATE_XRUN); |
| 629 | } |
| 630 | |
| 631 | static void abort_alsa_playback(struct ua101 *ua) |
| 632 | { |
| 633 | if (test_bit(ALSA_PLAYBACK_RUNNING, &ua->states)) |
| 634 | snd_pcm_stop(ua->playback.substream, SNDRV_PCM_STATE_XRUN); |
| 635 | } |
| 636 | |
| 637 | static int set_stream_hw(struct ua101 *ua, struct snd_pcm_substream *substream, |
| 638 | unsigned int channels) |
| 639 | { |
| 640 | int err; |
| 641 | |
| 642 | substream->runtime->hw.info = |
| 643 | SNDRV_PCM_INFO_MMAP | |
| 644 | SNDRV_PCM_INFO_MMAP_VALID | |
| 645 | SNDRV_PCM_INFO_BATCH | |
| 646 | SNDRV_PCM_INFO_INTERLEAVED | |
| 647 | SNDRV_PCM_INFO_BLOCK_TRANSFER | |
| 648 | SNDRV_PCM_INFO_FIFO_IN_FRAMES; |
| 649 | substream->runtime->hw.formats = ua->format_bit; |
| 650 | substream->runtime->hw.rates = snd_pcm_rate_to_rate_bit(ua->rate); |
| 651 | substream->runtime->hw.rate_min = ua->rate; |
| 652 | substream->runtime->hw.rate_max = ua->rate; |
| 653 | substream->runtime->hw.channels_min = channels; |
| 654 | substream->runtime->hw.channels_max = channels; |
| 655 | substream->runtime->hw.buffer_bytes_max = 45000 * 1024; |
| 656 | substream->runtime->hw.period_bytes_min = 1; |
| 657 | substream->runtime->hw.period_bytes_max = UINT_MAX; |
| 658 | substream->runtime->hw.periods_min = 2; |
| 659 | substream->runtime->hw.periods_max = UINT_MAX; |
| 660 | err = snd_pcm_hw_constraint_minmax(substream->runtime, |
| 661 | SNDRV_PCM_HW_PARAM_PERIOD_TIME, |
| 662 | 1500000 / ua->packets_per_second, |
| 663 | 8192000); |
| 664 | if (err < 0) |
| 665 | return err; |
| 666 | err = snd_pcm_hw_constraint_msbits(substream->runtime, 0, 32, 24); |
| 667 | return err; |
| 668 | } |
| 669 | |
| 670 | static int capture_pcm_open(struct snd_pcm_substream *substream) |
| 671 | { |
| 672 | struct ua101 *ua = substream->private_data; |
| 673 | int err; |
| 674 | |
| 675 | ua->capture.substream = substream; |
| 676 | err = set_stream_hw(ua, substream, ua->capture.channels); |
| 677 | if (err < 0) |
| 678 | return err; |
| 679 | substream->runtime->hw.fifo_size = |
| 680 | DIV_ROUND_CLOSEST(ua->rate, ua->packets_per_second); |
| 681 | substream->runtime->delay = substream->runtime->hw.fifo_size; |
| 682 | |
| 683 | mutex_lock(&ua->mutex); |
| 684 | err = start_usb_capture(ua); |
| 685 | if (err >= 0) |
| 686 | set_bit(ALSA_CAPTURE_OPEN, &ua->states); |
| 687 | mutex_unlock(&ua->mutex); |
| 688 | return err; |
| 689 | } |
| 690 | |
| 691 | static int playback_pcm_open(struct snd_pcm_substream *substream) |
| 692 | { |
| 693 | struct ua101 *ua = substream->private_data; |
| 694 | int err; |
| 695 | |
| 696 | ua->playback.substream = substream; |
| 697 | err = set_stream_hw(ua, substream, ua->playback.channels); |
| 698 | if (err < 0) |
| 699 | return err; |
| 700 | substream->runtime->hw.fifo_size = |
| 701 | DIV_ROUND_CLOSEST(ua->rate * ua->playback.queue_length, |
| 702 | ua->packets_per_second); |
| 703 | |
| 704 | mutex_lock(&ua->mutex); |
| 705 | err = start_usb_capture(ua); |
| 706 | if (err < 0) |
| 707 | goto error; |
| 708 | err = start_usb_playback(ua); |
| 709 | if (err < 0) { |
| 710 | if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) |
| 711 | stop_usb_capture(ua); |
| 712 | goto error; |
| 713 | } |
| 714 | set_bit(ALSA_PLAYBACK_OPEN, &ua->states); |
| 715 | error: |
| 716 | mutex_unlock(&ua->mutex); |
| 717 | return err; |
| 718 | } |
| 719 | |
| 720 | static int capture_pcm_close(struct snd_pcm_substream *substream) |
| 721 | { |
| 722 | struct ua101 *ua = substream->private_data; |
| 723 | |
| 724 | mutex_lock(&ua->mutex); |
| 725 | clear_bit(ALSA_CAPTURE_OPEN, &ua->states); |
| 726 | if (!test_bit(ALSA_PLAYBACK_OPEN, &ua->states)) |
| 727 | stop_usb_capture(ua); |
| 728 | mutex_unlock(&ua->mutex); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int playback_pcm_close(struct snd_pcm_substream *substream) |
| 733 | { |
| 734 | struct ua101 *ua = substream->private_data; |
| 735 | |
| 736 | mutex_lock(&ua->mutex); |
| 737 | stop_usb_playback(ua); |
| 738 | clear_bit(ALSA_PLAYBACK_OPEN, &ua->states); |
| 739 | if (!test_bit(ALSA_CAPTURE_OPEN, &ua->states)) |
| 740 | stop_usb_capture(ua); |
| 741 | mutex_unlock(&ua->mutex); |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int capture_pcm_hw_params(struct snd_pcm_substream *substream, |
| 746 | struct snd_pcm_hw_params *hw_params) |
| 747 | { |
| 748 | struct ua101 *ua = substream->private_data; |
| 749 | int err; |
| 750 | |
| 751 | mutex_lock(&ua->mutex); |
| 752 | err = start_usb_capture(ua); |
| 753 | mutex_unlock(&ua->mutex); |
| 754 | if (err < 0) |
| 755 | return err; |
| 756 | |
Clemens Ladisch | 5b4b2a4 | 2009-12-18 09:32:00 +0100 | [diff] [blame] | 757 | return snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 758 | params_buffer_bytes(hw_params)); |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static int playback_pcm_hw_params(struct snd_pcm_substream *substream, |
| 762 | struct snd_pcm_hw_params *hw_params) |
| 763 | { |
| 764 | struct ua101 *ua = substream->private_data; |
| 765 | int err; |
| 766 | |
| 767 | mutex_lock(&ua->mutex); |
| 768 | err = start_usb_capture(ua); |
| 769 | if (err >= 0) |
| 770 | err = start_usb_playback(ua); |
| 771 | mutex_unlock(&ua->mutex); |
| 772 | if (err < 0) |
| 773 | return err; |
| 774 | |
Clemens Ladisch | 5b4b2a4 | 2009-12-18 09:32:00 +0100 | [diff] [blame] | 775 | return snd_pcm_lib_alloc_vmalloc_buffer(substream, |
| 776 | params_buffer_bytes(hw_params)); |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static int ua101_pcm_hw_free(struct snd_pcm_substream *substream) |
| 780 | { |
Clemens Ladisch | 5b4b2a4 | 2009-12-18 09:32:00 +0100 | [diff] [blame] | 781 | return snd_pcm_lib_free_vmalloc_buffer(substream); |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static int capture_pcm_prepare(struct snd_pcm_substream *substream) |
| 785 | { |
| 786 | struct ua101 *ua = substream->private_data; |
| 787 | int err; |
| 788 | |
| 789 | mutex_lock(&ua->mutex); |
| 790 | err = start_usb_capture(ua); |
| 791 | mutex_unlock(&ua->mutex); |
| 792 | if (err < 0) |
| 793 | return err; |
| 794 | |
| 795 | /* |
| 796 | * The EHCI driver schedules the first packet of an iso stream at 10 ms |
| 797 | * in the future, i.e., no data is actually captured for that long. |
| 798 | * Take the wait here so that the stream is known to be actually |
| 799 | * running when the start trigger has been called. |
| 800 | */ |
| 801 | wait_event(ua->alsa_capture_wait, |
| 802 | test_bit(CAPTURE_URB_COMPLETED, &ua->states) || |
| 803 | !test_bit(USB_CAPTURE_RUNNING, &ua->states)); |
| 804 | if (test_bit(DISCONNECTED, &ua->states)) |
| 805 | return -ENODEV; |
| 806 | if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) |
| 807 | return -EIO; |
| 808 | |
| 809 | ua->capture.period_pos = 0; |
| 810 | ua->capture.buffer_pos = 0; |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static int playback_pcm_prepare(struct snd_pcm_substream *substream) |
| 815 | { |
| 816 | struct ua101 *ua = substream->private_data; |
| 817 | int err; |
| 818 | |
| 819 | mutex_lock(&ua->mutex); |
| 820 | err = start_usb_capture(ua); |
| 821 | if (err >= 0) |
| 822 | err = start_usb_playback(ua); |
| 823 | mutex_unlock(&ua->mutex); |
| 824 | if (err < 0) |
| 825 | return err; |
| 826 | |
| 827 | /* see the comment in capture_pcm_prepare() */ |
| 828 | wait_event(ua->alsa_playback_wait, |
| 829 | test_bit(PLAYBACK_URB_COMPLETED, &ua->states) || |
| 830 | !test_bit(USB_PLAYBACK_RUNNING, &ua->states)); |
| 831 | if (test_bit(DISCONNECTED, &ua->states)) |
| 832 | return -ENODEV; |
| 833 | if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) |
| 834 | return -EIO; |
| 835 | |
| 836 | substream->runtime->delay = 0; |
| 837 | ua->playback.period_pos = 0; |
| 838 | ua->playback.buffer_pos = 0; |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int capture_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 843 | { |
| 844 | struct ua101 *ua = substream->private_data; |
| 845 | |
| 846 | switch (cmd) { |
| 847 | case SNDRV_PCM_TRIGGER_START: |
| 848 | if (!test_bit(USB_CAPTURE_RUNNING, &ua->states)) |
| 849 | return -EIO; |
| 850 | set_bit(ALSA_CAPTURE_RUNNING, &ua->states); |
| 851 | return 0; |
| 852 | case SNDRV_PCM_TRIGGER_STOP: |
| 853 | clear_bit(ALSA_CAPTURE_RUNNING, &ua->states); |
| 854 | return 0; |
| 855 | default: |
| 856 | return -EINVAL; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | static int playback_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 861 | { |
| 862 | struct ua101 *ua = substream->private_data; |
| 863 | |
| 864 | switch (cmd) { |
| 865 | case SNDRV_PCM_TRIGGER_START: |
| 866 | if (!test_bit(USB_PLAYBACK_RUNNING, &ua->states)) |
| 867 | return -EIO; |
| 868 | set_bit(ALSA_PLAYBACK_RUNNING, &ua->states); |
| 869 | return 0; |
| 870 | case SNDRV_PCM_TRIGGER_STOP: |
| 871 | clear_bit(ALSA_PLAYBACK_RUNNING, &ua->states); |
| 872 | return 0; |
| 873 | default: |
| 874 | return -EINVAL; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | static inline snd_pcm_uframes_t ua101_pcm_pointer(struct ua101 *ua, |
| 879 | struct ua101_stream *stream) |
| 880 | { |
| 881 | unsigned long flags; |
| 882 | unsigned int pos; |
| 883 | |
| 884 | spin_lock_irqsave(&ua->lock, flags); |
| 885 | pos = stream->buffer_pos; |
| 886 | spin_unlock_irqrestore(&ua->lock, flags); |
| 887 | return pos; |
| 888 | } |
| 889 | |
| 890 | static snd_pcm_uframes_t capture_pcm_pointer(struct snd_pcm_substream *subs) |
| 891 | { |
| 892 | struct ua101 *ua = subs->private_data; |
| 893 | |
| 894 | return ua101_pcm_pointer(ua, &ua->capture); |
| 895 | } |
| 896 | |
| 897 | static snd_pcm_uframes_t playback_pcm_pointer(struct snd_pcm_substream *subs) |
| 898 | { |
| 899 | struct ua101 *ua = subs->private_data; |
| 900 | |
| 901 | return ua101_pcm_pointer(ua, &ua->playback); |
| 902 | } |
| 903 | |
| 904 | static struct snd_pcm_ops capture_pcm_ops = { |
| 905 | .open = capture_pcm_open, |
| 906 | .close = capture_pcm_close, |
| 907 | .ioctl = snd_pcm_lib_ioctl, |
| 908 | .hw_params = capture_pcm_hw_params, |
| 909 | .hw_free = ua101_pcm_hw_free, |
| 910 | .prepare = capture_pcm_prepare, |
| 911 | .trigger = capture_pcm_trigger, |
| 912 | .pointer = capture_pcm_pointer, |
Clemens Ladisch | 5b4b2a4 | 2009-12-18 09:32:00 +0100 | [diff] [blame] | 913 | .page = snd_pcm_lib_get_vmalloc_page, |
Takashi Iwai | c32d977 | 2010-01-18 14:58:57 +0100 | [diff] [blame] | 914 | .mmap = snd_pcm_lib_mmap_vmalloc, |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 915 | }; |
| 916 | |
| 917 | static struct snd_pcm_ops playback_pcm_ops = { |
| 918 | .open = playback_pcm_open, |
| 919 | .close = playback_pcm_close, |
| 920 | .ioctl = snd_pcm_lib_ioctl, |
| 921 | .hw_params = playback_pcm_hw_params, |
| 922 | .hw_free = ua101_pcm_hw_free, |
| 923 | .prepare = playback_pcm_prepare, |
| 924 | .trigger = playback_pcm_trigger, |
| 925 | .pointer = playback_pcm_pointer, |
Clemens Ladisch | 5b4b2a4 | 2009-12-18 09:32:00 +0100 | [diff] [blame] | 926 | .page = snd_pcm_lib_get_vmalloc_page, |
Takashi Iwai | c32d977 | 2010-01-18 14:58:57 +0100 | [diff] [blame] | 927 | .mmap = snd_pcm_lib_mmap_vmalloc, |
Clemens Ladisch | 63978ab | 2009-12-14 12:48:35 +0100 | [diff] [blame] | 928 | }; |
| 929 | |
| 930 | static const struct uac_format_type_i_discrete_descriptor * |
| 931 | find_format_descriptor(struct usb_interface *interface) |
| 932 | { |
| 933 | struct usb_host_interface *alt; |
| 934 | u8 *extra; |
| 935 | int extralen; |
| 936 | |
| 937 | if (interface->num_altsetting != 2) { |
| 938 | dev_err(&interface->dev, "invalid num_altsetting\n"); |
| 939 | return NULL; |
| 940 | } |
| 941 | |
| 942 | alt = &interface->altsetting[0]; |
| 943 | if (alt->desc.bNumEndpoints != 0) { |
| 944 | dev_err(&interface->dev, "invalid bNumEndpoints\n"); |
| 945 | return NULL; |
| 946 | } |
| 947 | |
| 948 | alt = &interface->altsetting[1]; |
| 949 | if (alt->desc.bNumEndpoints != 1) { |
| 950 | dev_err(&interface->dev, "invalid bNumEndpoints\n"); |
| 951 | return NULL; |
| 952 | } |
| 953 | |
| 954 | extra = alt->extra; |
| 955 | extralen = alt->extralen; |
| 956 | while (extralen >= sizeof(struct usb_descriptor_header)) { |
| 957 | struct uac_format_type_i_discrete_descriptor *desc; |
| 958 | |
| 959 | desc = (struct uac_format_type_i_discrete_descriptor *)extra; |
| 960 | if (desc->bLength > extralen) { |
| 961 | dev_err(&interface->dev, "descriptor overflow\n"); |
| 962 | return NULL; |
| 963 | } |
| 964 | if (desc->bLength == UAC_FORMAT_TYPE_I_DISCRETE_DESC_SIZE(1) && |
| 965 | desc->bDescriptorType == USB_DT_CS_INTERFACE && |
| 966 | desc->bDescriptorSubtype == UAC_FORMAT_TYPE) { |
| 967 | if (desc->bFormatType != UAC_FORMAT_TYPE_I_PCM || |
| 968 | desc->bSamFreqType != 1) { |
| 969 | dev_err(&interface->dev, |
| 970 | "invalid format type\n"); |
| 971 | return NULL; |
| 972 | } |
| 973 | return desc; |
| 974 | } |
| 975 | extralen -= desc->bLength; |
| 976 | extra += desc->bLength; |
| 977 | } |
| 978 | dev_err(&interface->dev, "sample format descriptor not found\n"); |
| 979 | return NULL; |
| 980 | } |
| 981 | |
| 982 | static int detect_usb_format(struct ua101 *ua) |
| 983 | { |
| 984 | const struct uac_format_type_i_discrete_descriptor *fmt_capture; |
| 985 | const struct uac_format_type_i_discrete_descriptor *fmt_playback; |
| 986 | const struct usb_endpoint_descriptor *epd; |
| 987 | unsigned int rate2; |
| 988 | |
| 989 | fmt_capture = find_format_descriptor(ua->intf[INTF_CAPTURE]); |
| 990 | fmt_playback = find_format_descriptor(ua->intf[INTF_PLAYBACK]); |
| 991 | if (!fmt_capture || !fmt_playback) |
| 992 | return -ENXIO; |
| 993 | |
| 994 | switch (fmt_capture->bSubframeSize) { |
| 995 | case 3: |
| 996 | ua->format_bit = SNDRV_PCM_FMTBIT_S24_3LE; |
| 997 | break; |
| 998 | case 4: |
| 999 | ua->format_bit = SNDRV_PCM_FMTBIT_S32_LE; |
| 1000 | break; |
| 1001 | default: |
| 1002 | dev_err(&ua->dev->dev, "sample width is not 24 or 32 bits\n"); |
| 1003 | return -ENXIO; |
| 1004 | } |
| 1005 | if (fmt_capture->bSubframeSize != fmt_playback->bSubframeSize) { |
| 1006 | dev_err(&ua->dev->dev, |
| 1007 | "playback/capture sample widths do not match\n"); |
| 1008 | return -ENXIO; |
| 1009 | } |
| 1010 | |
| 1011 | if (fmt_capture->bBitResolution != 24 || |
| 1012 | fmt_playback->bBitResolution != 24) { |
| 1013 | dev_err(&ua->dev->dev, "sample width is not 24 bits\n"); |
| 1014 | return -ENXIO; |
| 1015 | } |
| 1016 | |
| 1017 | ua->rate = combine_triple(fmt_capture->tSamFreq[0]); |
| 1018 | rate2 = combine_triple(fmt_playback->tSamFreq[0]); |
| 1019 | if (ua->rate != rate2) { |
| 1020 | dev_err(&ua->dev->dev, |
| 1021 | "playback/capture rates do not match: %u/%u\n", |
| 1022 | rate2, ua->rate); |
| 1023 | return -ENXIO; |
| 1024 | } |
| 1025 | |
| 1026 | switch (ua->dev->speed) { |
| 1027 | case USB_SPEED_FULL: |
| 1028 | ua->packets_per_second = 1000; |
| 1029 | break; |
| 1030 | case USB_SPEED_HIGH: |
| 1031 | ua->packets_per_second = 8000; |
| 1032 | break; |
| 1033 | default: |
| 1034 | dev_err(&ua->dev->dev, "unknown device speed\n"); |
| 1035 | return -ENXIO; |
| 1036 | } |
| 1037 | |
| 1038 | ua->capture.channels = fmt_capture->bNrChannels; |
| 1039 | ua->playback.channels = fmt_playback->bNrChannels; |
| 1040 | ua->capture.frame_bytes = |
| 1041 | fmt_capture->bSubframeSize * ua->capture.channels; |
| 1042 | ua->playback.frame_bytes = |
| 1043 | fmt_playback->bSubframeSize * ua->playback.channels; |
| 1044 | |
| 1045 | epd = &ua->intf[INTF_CAPTURE]->altsetting[1].endpoint[0].desc; |
| 1046 | if (!usb_endpoint_is_isoc_in(epd)) { |
| 1047 | dev_err(&ua->dev->dev, "invalid capture endpoint\n"); |
| 1048 | return -ENXIO; |
| 1049 | } |
| 1050 | ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, usb_endpoint_num(epd)); |
| 1051 | ua->capture.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize); |
| 1052 | |
| 1053 | epd = &ua->intf[INTF_PLAYBACK]->altsetting[1].endpoint[0].desc; |
| 1054 | if (!usb_endpoint_is_isoc_out(epd)) { |
| 1055 | dev_err(&ua->dev->dev, "invalid playback endpoint\n"); |
| 1056 | return -ENXIO; |
| 1057 | } |
| 1058 | ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, usb_endpoint_num(epd)); |
| 1059 | ua->playback.max_packet_bytes = le16_to_cpu(epd->wMaxPacketSize); |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static int alloc_stream_buffers(struct ua101 *ua, struct ua101_stream *stream) |
| 1064 | { |
| 1065 | unsigned int remaining_packets, packets, packets_per_page, i; |
| 1066 | size_t size; |
| 1067 | |
| 1068 | stream->queue_length = queue_length; |
| 1069 | stream->queue_length = max(stream->queue_length, |
| 1070 | (unsigned int)MIN_QUEUE_LENGTH); |
| 1071 | stream->queue_length = min(stream->queue_length, |
| 1072 | (unsigned int)MAX_QUEUE_LENGTH); |
| 1073 | |
| 1074 | /* |
| 1075 | * The cache pool sizes used by usb_buffer_alloc() (128, 512, 2048) are |
| 1076 | * quite bad when used with the packet sizes of this device (e.g. 280, |
| 1077 | * 520, 624). Therefore, we allocate and subdivide entire pages, using |
| 1078 | * a smaller buffer only for the last chunk. |
| 1079 | */ |
| 1080 | remaining_packets = stream->queue_length; |
| 1081 | packets_per_page = PAGE_SIZE / stream->max_packet_bytes; |
| 1082 | for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) { |
| 1083 | packets = min(remaining_packets, packets_per_page); |
| 1084 | size = packets * stream->max_packet_bytes; |
| 1085 | stream->buffers[i].addr = |
| 1086 | usb_buffer_alloc(ua->dev, size, GFP_KERNEL, |
| 1087 | &stream->buffers[i].dma); |
| 1088 | if (!stream->buffers[i].addr) |
| 1089 | return -ENOMEM; |
| 1090 | stream->buffers[i].size = size; |
| 1091 | remaining_packets -= packets; |
| 1092 | if (!remaining_packets) |
| 1093 | break; |
| 1094 | } |
| 1095 | if (remaining_packets) { |
| 1096 | dev_err(&ua->dev->dev, "too many packets\n"); |
| 1097 | return -ENXIO; |
| 1098 | } |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static void free_stream_buffers(struct ua101 *ua, struct ua101_stream *stream) |
| 1103 | { |
| 1104 | unsigned int i; |
| 1105 | |
| 1106 | for (i = 0; i < ARRAY_SIZE(stream->buffers); ++i) |
| 1107 | usb_buffer_free(ua->dev, |
| 1108 | stream->buffers[i].size, |
| 1109 | stream->buffers[i].addr, |
| 1110 | stream->buffers[i].dma); |
| 1111 | } |
| 1112 | |
| 1113 | static int alloc_stream_urbs(struct ua101 *ua, struct ua101_stream *stream, |
| 1114 | void (*urb_complete)(struct urb *)) |
| 1115 | { |
| 1116 | unsigned max_packet_size = stream->max_packet_bytes; |
| 1117 | struct ua101_urb *urb; |
| 1118 | unsigned int b, u = 0; |
| 1119 | |
| 1120 | for (b = 0; b < ARRAY_SIZE(stream->buffers); ++b) { |
| 1121 | unsigned int size = stream->buffers[b].size; |
| 1122 | u8 *addr = stream->buffers[b].addr; |
| 1123 | dma_addr_t dma = stream->buffers[b].dma; |
| 1124 | |
| 1125 | while (size >= max_packet_size) { |
| 1126 | if (u >= stream->queue_length) |
| 1127 | goto bufsize_error; |
| 1128 | urb = kmalloc(sizeof(*urb), GFP_KERNEL); |
| 1129 | if (!urb) |
| 1130 | return -ENOMEM; |
| 1131 | usb_init_urb(&urb->urb); |
| 1132 | urb->urb.dev = ua->dev; |
| 1133 | urb->urb.pipe = stream->usb_pipe; |
| 1134 | urb->urb.transfer_flags = URB_ISO_ASAP | |
| 1135 | URB_NO_TRANSFER_DMA_MAP; |
| 1136 | urb->urb.transfer_buffer = addr; |
| 1137 | urb->urb.transfer_dma = dma; |
| 1138 | urb->urb.transfer_buffer_length = max_packet_size; |
| 1139 | urb->urb.number_of_packets = 1; |
| 1140 | urb->urb.interval = 1; |
| 1141 | urb->urb.context = ua; |
| 1142 | urb->urb.complete = urb_complete; |
| 1143 | urb->urb.iso_frame_desc[0].offset = 0; |
| 1144 | urb->urb.iso_frame_desc[0].length = max_packet_size; |
| 1145 | stream->urbs[u++] = urb; |
| 1146 | size -= max_packet_size; |
| 1147 | addr += max_packet_size; |
| 1148 | dma += max_packet_size; |
| 1149 | } |
| 1150 | } |
| 1151 | if (u == stream->queue_length) |
| 1152 | return 0; |
| 1153 | bufsize_error: |
| 1154 | dev_err(&ua->dev->dev, "internal buffer size error\n"); |
| 1155 | return -ENXIO; |
| 1156 | } |
| 1157 | |
| 1158 | static void free_stream_urbs(struct ua101_stream *stream) |
| 1159 | { |
| 1160 | unsigned int i; |
| 1161 | |
| 1162 | for (i = 0; i < stream->queue_length; ++i) |
| 1163 | kfree(stream->urbs[i]); |
| 1164 | } |
| 1165 | |
| 1166 | static void free_usb_related_resources(struct ua101 *ua, |
| 1167 | struct usb_interface *interface) |
| 1168 | { |
| 1169 | unsigned int i; |
| 1170 | |
| 1171 | free_stream_urbs(&ua->capture); |
| 1172 | free_stream_urbs(&ua->playback); |
| 1173 | free_stream_buffers(ua, &ua->capture); |
| 1174 | free_stream_buffers(ua, &ua->playback); |
| 1175 | |
| 1176 | for (i = 0; i < ARRAY_SIZE(ua->intf); ++i) |
| 1177 | if (ua->intf[i]) { |
| 1178 | usb_set_intfdata(ua->intf[i], NULL); |
| 1179 | if (ua->intf[i] != interface) |
| 1180 | usb_driver_release_interface(&ua101_driver, |
| 1181 | ua->intf[i]); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | static void ua101_card_free(struct snd_card *card) |
| 1186 | { |
| 1187 | struct ua101 *ua = card->private_data; |
| 1188 | |
| 1189 | mutex_destroy(&ua->mutex); |
| 1190 | } |
| 1191 | |
| 1192 | static int ua101_probe(struct usb_interface *interface, |
| 1193 | const struct usb_device_id *usb_id) |
| 1194 | { |
| 1195 | static const struct snd_usb_midi_endpoint_info midi_ep = { |
| 1196 | .out_cables = 0x0001, |
| 1197 | .in_cables = 0x0001 |
| 1198 | }; |
| 1199 | static const struct snd_usb_audio_quirk midi_quirk = { |
| 1200 | .type = QUIRK_MIDI_FIXED_ENDPOINT, |
| 1201 | .data = &midi_ep |
| 1202 | }; |
| 1203 | struct snd_card *card; |
| 1204 | struct ua101 *ua; |
| 1205 | unsigned int card_index, i; |
| 1206 | char usb_path[32]; |
| 1207 | int err; |
| 1208 | |
| 1209 | if (interface->altsetting->desc.bInterfaceNumber != 0) |
| 1210 | return -ENODEV; |
| 1211 | |
| 1212 | mutex_lock(&devices_mutex); |
| 1213 | |
| 1214 | for (card_index = 0; card_index < SNDRV_CARDS; ++card_index) |
| 1215 | if (enable[card_index] && !(devices_used & (1 << card_index))) |
| 1216 | break; |
| 1217 | if (card_index >= SNDRV_CARDS) { |
| 1218 | mutex_unlock(&devices_mutex); |
| 1219 | return -ENOENT; |
| 1220 | } |
| 1221 | err = snd_card_create(index[card_index], id[card_index], THIS_MODULE, |
| 1222 | sizeof(*ua), &card); |
| 1223 | if (err < 0) { |
| 1224 | mutex_unlock(&devices_mutex); |
| 1225 | return err; |
| 1226 | } |
| 1227 | card->private_free = ua101_card_free; |
| 1228 | ua = card->private_data; |
| 1229 | ua->dev = interface_to_usbdev(interface); |
| 1230 | ua->card = card; |
| 1231 | ua->card_index = card_index; |
| 1232 | INIT_LIST_HEAD(&ua->midi_list); |
| 1233 | spin_lock_init(&ua->lock); |
| 1234 | mutex_init(&ua->mutex); |
| 1235 | INIT_LIST_HEAD(&ua->ready_playback_urbs); |
| 1236 | tasklet_init(&ua->playback_tasklet, |
| 1237 | playback_tasklet, (unsigned long)ua); |
| 1238 | init_waitqueue_head(&ua->alsa_capture_wait); |
| 1239 | init_waitqueue_head(&ua->rate_feedback_wait); |
| 1240 | init_waitqueue_head(&ua->alsa_playback_wait); |
| 1241 | |
| 1242 | #ifdef UA1A_HACK |
| 1243 | if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) { |
| 1244 | ua->intf[2] = interface; |
| 1245 | ua->intf[0] = usb_ifnum_to_if(ua->dev, 1); |
| 1246 | ua->intf[1] = usb_ifnum_to_if(ua->dev, 2); |
| 1247 | usb_driver_claim_interface(&ua101_driver, ua->intf[0], ua); |
| 1248 | usb_driver_claim_interface(&ua101_driver, ua->intf[1], ua); |
| 1249 | } else { |
| 1250 | #endif |
| 1251 | ua->intf[0] = interface; |
| 1252 | for (i = 1; i < ARRAY_SIZE(ua->intf); ++i) { |
| 1253 | ua->intf[i] = usb_ifnum_to_if(ua->dev, i); |
| 1254 | if (!ua->intf[i]) { |
| 1255 | dev_err(&ua->dev->dev, "interface %u not found\n", i); |
| 1256 | err = -ENXIO; |
| 1257 | goto probe_error; |
| 1258 | } |
| 1259 | err = usb_driver_claim_interface(&ua101_driver, |
| 1260 | ua->intf[i], ua); |
| 1261 | if (err < 0) { |
| 1262 | ua->intf[i] = NULL; |
| 1263 | err = -EBUSY; |
| 1264 | goto probe_error; |
| 1265 | } |
| 1266 | } |
| 1267 | #ifdef UA1A_HACK |
| 1268 | } |
| 1269 | #endif |
| 1270 | |
| 1271 | snd_card_set_dev(card, &interface->dev); |
| 1272 | |
| 1273 | #ifdef UA1A_HACK |
| 1274 | if (ua->dev->descriptor.idProduct == cpu_to_le16(0x0018)) { |
| 1275 | ua->format_bit = SNDRV_PCM_FMTBIT_S16_LE; |
| 1276 | ua->rate = 44100; |
| 1277 | ua->packets_per_second = 1000; |
| 1278 | ua->capture.channels = 2; |
| 1279 | ua->playback.channels = 2; |
| 1280 | ua->capture.frame_bytes = 4; |
| 1281 | ua->playback.frame_bytes = 4; |
| 1282 | ua->capture.usb_pipe = usb_rcvisocpipe(ua->dev, 2); |
| 1283 | ua->playback.usb_pipe = usb_sndisocpipe(ua->dev, 1); |
| 1284 | ua->capture.max_packet_bytes = 192; |
| 1285 | ua->playback.max_packet_bytes = 192; |
| 1286 | } else { |
| 1287 | #endif |
| 1288 | err = detect_usb_format(ua); |
| 1289 | if (err < 0) |
| 1290 | goto probe_error; |
| 1291 | #ifdef UA1A_HACK |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
| 1295 | strcpy(card->driver, "UA-101"); |
| 1296 | strcpy(card->shortname, "UA-101"); |
| 1297 | usb_make_path(ua->dev, usb_path, sizeof(usb_path)); |
| 1298 | snprintf(ua->card->longname, sizeof(ua->card->longname), |
| 1299 | "EDIROL UA-101 (serial %s), %u Hz at %s, %s speed", |
| 1300 | ua->dev->serial ? ua->dev->serial : "?", ua->rate, usb_path, |
| 1301 | ua->dev->speed == USB_SPEED_HIGH ? "high" : "full"); |
| 1302 | |
| 1303 | err = alloc_stream_buffers(ua, &ua->capture); |
| 1304 | if (err < 0) |
| 1305 | goto probe_error; |
| 1306 | err = alloc_stream_buffers(ua, &ua->playback); |
| 1307 | if (err < 0) |
| 1308 | goto probe_error; |
| 1309 | |
| 1310 | err = alloc_stream_urbs(ua, &ua->capture, capture_urb_complete); |
| 1311 | if (err < 0) |
| 1312 | goto probe_error; |
| 1313 | err = alloc_stream_urbs(ua, &ua->playback, playback_urb_complete); |
| 1314 | if (err < 0) |
| 1315 | goto probe_error; |
| 1316 | |
| 1317 | err = snd_pcm_new(card, "UA-101", 0, 1, 1, &ua->pcm); |
| 1318 | if (err < 0) |
| 1319 | goto probe_error; |
| 1320 | ua->pcm->private_data = ua; |
| 1321 | strcpy(ua->pcm->name, "UA-101"); |
| 1322 | snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_PLAYBACK, &playback_pcm_ops); |
| 1323 | snd_pcm_set_ops(ua->pcm, SNDRV_PCM_STREAM_CAPTURE, &capture_pcm_ops); |
| 1324 | |
| 1325 | #ifdef UA1A_HACK |
| 1326 | if (ua->dev->descriptor.idProduct != cpu_to_le16(0x0018)) { |
| 1327 | #endif |
| 1328 | err = snd_usbmidi_create(card, ua->intf[INTF_MIDI], |
| 1329 | &ua->midi_list, &midi_quirk); |
| 1330 | if (err < 0) |
| 1331 | goto probe_error; |
| 1332 | #ifdef UA1A_HACK |
| 1333 | } |
| 1334 | #endif |
| 1335 | |
| 1336 | err = snd_card_register(card); |
| 1337 | if (err < 0) |
| 1338 | goto probe_error; |
| 1339 | |
| 1340 | usb_set_intfdata(interface, ua); |
| 1341 | devices_used |= 1 << card_index; |
| 1342 | |
| 1343 | mutex_unlock(&devices_mutex); |
| 1344 | return 0; |
| 1345 | |
| 1346 | probe_error: |
| 1347 | free_usb_related_resources(ua, interface); |
| 1348 | snd_card_free(card); |
| 1349 | mutex_unlock(&devices_mutex); |
| 1350 | return err; |
| 1351 | } |
| 1352 | |
| 1353 | static void ua101_disconnect(struct usb_interface *interface) |
| 1354 | { |
| 1355 | struct ua101 *ua = usb_get_intfdata(interface); |
| 1356 | struct list_head *midi; |
| 1357 | |
| 1358 | if (!ua) |
| 1359 | return; |
| 1360 | |
| 1361 | mutex_lock(&devices_mutex); |
| 1362 | |
| 1363 | set_bit(DISCONNECTED, &ua->states); |
| 1364 | wake_up(&ua->rate_feedback_wait); |
| 1365 | |
| 1366 | /* make sure that userspace cannot create new requests */ |
| 1367 | snd_card_disconnect(ua->card); |
| 1368 | |
| 1369 | /* make sure that there are no pending USB requests */ |
| 1370 | __list_for_each(midi, &ua->midi_list) |
| 1371 | snd_usbmidi_disconnect(midi); |
| 1372 | abort_alsa_playback(ua); |
| 1373 | abort_alsa_capture(ua); |
| 1374 | mutex_lock(&ua->mutex); |
| 1375 | stop_usb_playback(ua); |
| 1376 | stop_usb_capture(ua); |
| 1377 | mutex_unlock(&ua->mutex); |
| 1378 | |
| 1379 | free_usb_related_resources(ua, interface); |
| 1380 | |
| 1381 | devices_used &= ~(1 << ua->card_index); |
| 1382 | |
| 1383 | snd_card_free_when_closed(ua->card); |
| 1384 | |
| 1385 | mutex_unlock(&devices_mutex); |
| 1386 | } |
| 1387 | |
| 1388 | static struct usb_device_id ua101_ids[] = { |
| 1389 | #ifdef UA1A_HACK |
| 1390 | { USB_DEVICE(0x0582, 0x0018) }, |
| 1391 | #endif |
| 1392 | { USB_DEVICE(0x0582, 0x007d) }, |
| 1393 | { USB_DEVICE(0x0582, 0x008d) }, |
| 1394 | { } |
| 1395 | }; |
| 1396 | MODULE_DEVICE_TABLE(usb, ua101_ids); |
| 1397 | |
| 1398 | static struct usb_driver ua101_driver = { |
| 1399 | .name = "snd-ua101", |
| 1400 | .id_table = ua101_ids, |
| 1401 | .probe = ua101_probe, |
| 1402 | .disconnect = ua101_disconnect, |
| 1403 | #if 0 |
| 1404 | .suspend = ua101_suspend, |
| 1405 | .resume = ua101_resume, |
| 1406 | #endif |
| 1407 | }; |
| 1408 | |
| 1409 | static int __init alsa_card_ua101_init(void) |
| 1410 | { |
| 1411 | return usb_register(&ua101_driver); |
| 1412 | } |
| 1413 | |
| 1414 | static void __exit alsa_card_ua101_exit(void) |
| 1415 | { |
| 1416 | usb_deregister(&ua101_driver); |
| 1417 | mutex_destroy(&devices_mutex); |
| 1418 | } |
| 1419 | |
| 1420 | module_init(alsa_card_ua101_init); |
| 1421 | module_exit(alsa_card_ua101_exit); |