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