| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* MIDI 2.0 support |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/bitops.h> |
| 7 |
#include <linux/string.h> |
| 8 |
#include <linux/init.h> |
| 9 |
#include <linux/slab.h> |
| 10 |
#include <linux/usb.h> |
| 11 |
#include <linux/wait.h> |
| 12 |
#include <linux/module.h> |
| 13 |
#include <linux/moduleparam.h> |
| 14 |
#include <linux/usb/audio.h> |
| 15 |
#include <linux/usb/midi.h> |
| 16 |
#include <linux/usb/midi-v2.h> |
| 17 |
|
| 18 |
#include <sound/core.h> |
| 19 |
#include <sound/control.h> |
| 20 |
#include <sound/ump.h> |
| 21 |
#include "usbaudio.h" |
| 22 |
#include "midi.h" |
| 23 |
#include "midi2.h" |
| 24 |
#include "helper.h" |
| 25 |
|
| 26 |
static bool midi2_enable = true; |
| 27 |
module_param(midi2_enable, bool, 0444); |
| 28 |
MODULE_PARM_DESC(midi2_enable, "Enable MIDI 2.0 support."); |
| 29 |
|
| 30 |
static bool midi2_ump_probe = true; |
| 31 |
module_param(midi2_ump_probe, bool, 0444); |
| 32 |
MODULE_PARM_DESC(midi2_ump_probe, "Probe UMP v1.1 support at first."); |
| 33 |
|
| 34 |
/* stream direction; just shorter names */ |
| 35 |
enum { |
| 36 |
STR_OUT = SNDRV_RAWMIDI_STREAM_OUTPUT, |
| 37 |
STR_IN = SNDRV_RAWMIDI_STREAM_INPUT |
| 38 |
}; |
| 39 |
|
| 40 |
#define NUM_URBS 8 |
| 41 |
|
| 42 |
struct snd_usb_midi2_urb; |
| 43 |
struct snd_usb_midi2_endpoint; |
| 44 |
struct snd_usb_midi2_ump; |
| 45 |
struct snd_usb_midi2_interface; |
| 46 |
|
| 47 |
/* URB context */ |
| 48 |
struct snd_usb_midi2_urb { |
| 49 |
struct urb *urb; |
| 50 |
struct snd_usb_midi2_endpoint *ep; |
| 51 |
unsigned int index; /* array index */ |
| 52 |
}; |
| 53 |
|
| 54 |
/* A USB MIDI input/output endpoint */ |
| 55 |
struct snd_usb_midi2_endpoint { |
| 56 |
struct usb_device *dev; |
| 57 |
const struct usb_ms20_endpoint_descriptor *ms_ep; /* reference to EP descriptor */ |
| 58 |
struct snd_usb_midi2_endpoint *pair; /* bidirectional pair EP */ |
| 59 |
struct snd_usb_midi2_ump *rmidi; /* assigned UMP EP pair */ |
| 60 |
struct snd_ump_endpoint *ump; /* assigned UMP EP */ |
| 61 |
int direction; /* direction (STR_IN/OUT) */ |
| 62 |
unsigned int endpoint; /* EP number */ |
| 63 |
unsigned int pipe; /* URB pipe */ |
| 64 |
unsigned int packets; /* packet buffer size in bytes */ |
| 65 |
unsigned int interval; /* interval for INT EP */ |
| 66 |
wait_queue_head_t wait; /* URB waiter */ |
| 67 |
spinlock_t lock; /* URB locking */ |
| 68 |
struct snd_rawmidi_substream *substream; /* NULL when closed */ |
| 69 |
unsigned int num_urbs; /* number of allocated URBs */ |
| 70 |
unsigned long urb_free; /* bitmap for free URBs */ |
| 71 |
unsigned long urb_free_mask; /* bitmask for free URBs */ |
| 72 |
atomic_t running; /* running status */ |
| 73 |
atomic_t suspended; /* saved running status for suspend */ |
| 74 |
bool disconnected; /* shadow of umidi->disconnected */ |
| 75 |
struct list_head list; /* list to umidi->ep_list */ |
| 76 |
struct snd_usb_midi2_urb urbs[NUM_URBS]; |
| 77 |
}; |
| 78 |
|
| 79 |
/* A UMP endpoint - one or two USB MIDI endpoints are assigned */ |
| 80 |
struct snd_usb_midi2_ump { |
| 81 |
struct usb_device *dev; |
| 82 |
struct snd_usb_midi2_interface *umidi; /* reference to MIDI iface */ |
| 83 |
struct snd_ump_endpoint *ump; /* assigned UMP EP object */ |
| 84 |
struct snd_usb_midi2_endpoint *eps[2]; /* USB MIDI endpoints */ |
| 85 |
int index; /* rawmidi device index */ |
| 86 |
unsigned char usb_block_id; /* USB GTB id used for finding a pair */ |
| 87 |
bool ump_parsed; /* Parsed UMP 1.1 EP/FB info*/ |
| 88 |
struct list_head list; /* list to umidi->rawmidi_list */ |
| 89 |
}; |
| 90 |
|
| 91 |
/* top-level instance per USB MIDI interface */ |
| 92 |
struct snd_usb_midi2_interface { |
| 93 |
struct snd_usb_audio *chip; /* assigned USB-audio card */ |
| 94 |
struct usb_interface *iface; /* assigned USB interface */ |
| 95 |
struct usb_host_interface *hostif; |
| 96 |
const char *blk_descs; /* group terminal block descriptors */ |
| 97 |
unsigned int blk_desc_size; /* size of GTB descriptors */ |
| 98 |
bool disconnected; |
| 99 |
struct list_head ep_list; /* list of endpoints */ |
| 100 |
struct list_head rawmidi_list; /* list of UMP rawmidis */ |
| 101 |
struct list_head list; /* list to chip->midi_v2_list */ |
| 102 |
}; |
| 103 |
|
| 104 |
/* submit URBs as much as possible; used for both input and output */ |
| 105 |
static void do_submit_urbs_locked(struct snd_usb_midi2_endpoint *ep, |
| 106 |
int (*prepare)(struct snd_usb_midi2_endpoint *, |
| 107 |
struct urb *)) |
| 108 |
{ |
| 109 |
struct snd_usb_midi2_urb *ctx; |
| 110 |
int index, err = 0; |
| 111 |
|
| 112 |
if (ep->disconnected) |
| 113 |
return; |
| 114 |
|
| 115 |
while (ep->urb_free) { |
| 116 |
index = find_first_bit(&ep->urb_free, ep->num_urbs); |
| 117 |
if (index >= ep->num_urbs) |
| 118 |
return; |
| 119 |
ctx = &ep->urbs[index]; |
| 120 |
err = prepare(ep, ctx->urb); |
| 121 |
if (err < 0) |
| 122 |
return; |
| 123 |
if (!ctx->urb->transfer_buffer_length) |
| 124 |
return; |
| 125 |
ctx->urb->dev = ep->dev; |
| 126 |
err = usb_submit_urb(ctx->urb, GFP_ATOMIC); |
| 127 |
if (err < 0) { |
| 128 |
dev_dbg(&ep->dev->dev, |
| 129 |
"usb_submit_urb error %d\n", err); |
| 130 |
return; |
| 131 |
} |
| 132 |
clear_bit(index, &ep->urb_free); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/* prepare for output submission: copy from rawmidi buffer to urb packet */ |
| 137 |
static int prepare_output_urb(struct snd_usb_midi2_endpoint *ep, |
| 138 |
struct urb *urb) |
| 139 |
{ |
| 140 |
int count; |
| 141 |
|
| 142 |
count = snd_ump_transmit(ep->ump, urb->transfer_buffer, |
| 143 |
ep->packets); |
| 144 |
if (count < 0) { |
| 145 |
dev_dbg(&ep->dev->dev, "rawmidi transmit error %d\n", count); |
| 146 |
return count; |
| 147 |
} |
| 148 |
cpu_to_le32_array((u32 *)urb->transfer_buffer, count >> 2); |
| 149 |
urb->transfer_buffer_length = count; |
| 150 |
return 0; |
| 151 |
} |
| 152 |
|
| 153 |
static void submit_output_urbs_locked(struct snd_usb_midi2_endpoint *ep) |
| 154 |
{ |
| 155 |
do_submit_urbs_locked(ep, prepare_output_urb); |
| 156 |
} |
| 157 |
|
| 158 |
/* URB completion for output; re-filling and re-submit */ |
| 159 |
static void output_urb_complete(struct urb *urb) |
| 160 |
{ |
| 161 |
struct snd_usb_midi2_urb *ctx = urb->context; |
| 162 |
struct snd_usb_midi2_endpoint *ep = ctx->ep; |
| 163 |
|
| 164 |
guard(spinlock_irqsave)(&ep->lock); |
| 165 |
set_bit(ctx->index, &ep->urb_free); |
| 166 |
if (urb->status >= 0 && atomic_read(&ep->running)) |
| 167 |
submit_output_urbs_locked(ep); |
| 168 |
if (ep->urb_free == ep->urb_free_mask) |
| 169 |
wake_up(&ep->wait); |
| 170 |
} |
| 171 |
|
| 172 |
/* prepare for input submission: just set the buffer length */ |
| 173 |
static int prepare_input_urb(struct snd_usb_midi2_endpoint *ep, |
| 174 |
struct urb *urb) |
| 175 |
{ |
| 176 |
urb->transfer_buffer_length = ep->packets; |
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
static void submit_input_urbs_locked(struct snd_usb_midi2_endpoint *ep) |
| 181 |
{ |
| 182 |
do_submit_urbs_locked(ep, prepare_input_urb); |
| 183 |
} |
| 184 |
|
| 185 |
/* URB completion for input; copy into rawmidi buffer and resubmit */ |
| 186 |
static void input_urb_complete(struct urb *urb) |
| 187 |
{ |
| 188 |
struct snd_usb_midi2_urb *ctx = urb->context; |
| 189 |
struct snd_usb_midi2_endpoint *ep = ctx->ep; |
| 190 |
int len; |
| 191 |
|
| 192 |
guard(spinlock_irqsave)(&ep->lock); |
| 193 |
if (ep->disconnected || urb->status < 0) |
| 194 |
goto dequeue; |
| 195 |
len = urb->actual_length; |
| 196 |
len &= ~3; /* align UMP */ |
| 197 |
if (len > ep->packets) |
| 198 |
len = ep->packets; |
| 199 |
if (len > 0) { |
| 200 |
le32_to_cpu_array((u32 *)urb->transfer_buffer, len >> 2); |
| 201 |
snd_ump_receive(ep->ump, (u32 *)urb->transfer_buffer, len); |
| 202 |
} |
| 203 |
dequeue: |
| 204 |
set_bit(ctx->index, &ep->urb_free); |
| 205 |
submit_input_urbs_locked(ep); |
| 206 |
if (ep->urb_free == ep->urb_free_mask) |
| 207 |
wake_up(&ep->wait); |
| 208 |
} |
| 209 |
|
| 210 |
/* URB submission helper; for both direction */ |
| 211 |
static void submit_io_urbs(struct snd_usb_midi2_endpoint *ep) |
| 212 |
{ |
| 213 |
if (!ep) |
| 214 |
return; |
| 215 |
guard(spinlock_irqsave)(&ep->lock); |
| 216 |
if (ep->direction == STR_IN) |
| 217 |
submit_input_urbs_locked(ep); |
| 218 |
else |
| 219 |
submit_output_urbs_locked(ep); |
| 220 |
} |
| 221 |
|
| 222 |
/* kill URBs for close, suspend and disconnect */ |
| 223 |
static void kill_midi_urbs(struct snd_usb_midi2_endpoint *ep, bool suspending) |
| 224 |
{ |
| 225 |
int i; |
| 226 |
|
| 227 |
if (!ep) |
| 228 |
return; |
| 229 |
if (suspending) |
| 230 |
atomic_set(&ep->suspended, atomic_read(&ep->running)); |
| 231 |
atomic_set(&ep->running, 0); |
| 232 |
for (i = 0; i < ep->num_urbs; i++) { |
| 233 |
if (!ep->urbs[i].urb) |
| 234 |
break; |
| 235 |
usb_kill_urb(ep->urbs[i].urb); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/* wait until all URBs get freed */ |
| 240 |
static void drain_urb_queue(struct snd_usb_midi2_endpoint *ep) |
| 241 |
{ |
| 242 |
if (!ep) |
| 243 |
return; |
| 244 |
guard(spinlock_irq)(&ep->lock); |
| 245 |
atomic_set(&ep->running, 0); |
| 246 |
wait_event_lock_irq_timeout(ep->wait, |
| 247 |
ep->disconnected || |
| 248 |
ep->urb_free == ep->urb_free_mask, |
| 249 |
ep->lock, msecs_to_jiffies(500)); |
| 250 |
} |
| 251 |
|
| 252 |
/* release URBs for an EP */ |
| 253 |
static void free_midi_urbs(struct snd_usb_midi2_endpoint *ep) |
| 254 |
{ |
| 255 |
struct snd_usb_midi2_urb *ctx; |
| 256 |
int i; |
| 257 |
|
| 258 |
if (!ep) |
| 259 |
return; |
| 260 |
for (i = 0; i < NUM_URBS; ++i) { |
| 261 |
ctx = &ep->urbs[i]; |
| 262 |
if (!ctx->urb) |
| 263 |
break; |
| 264 |
usb_free_coherent(ep->dev, ep->packets, |
| 265 |
ctx->urb->transfer_buffer, |
| 266 |
ctx->urb->transfer_dma); |
| 267 |
usb_free_urb(ctx->urb); |
| 268 |
ctx->urb = NULL; |
| 269 |
} |
| 270 |
ep->num_urbs = 0; |
| 271 |
} |
| 272 |
|
| 273 |
/* allocate URBs for an EP */ |
| 274 |
/* the callers should handle allocation errors via free_midi_urbs() */ |
| 275 |
static int alloc_midi_urbs(struct snd_usb_midi2_endpoint *ep) |
| 276 |
{ |
| 277 |
struct snd_usb_midi2_urb *ctx; |
| 278 |
void (*comp)(struct urb *urb); |
| 279 |
void *buffer; |
| 280 |
int i, err; |
| 281 |
int endpoint, len; |
| 282 |
|
| 283 |
endpoint = ep->endpoint; |
| 284 |
len = ep->packets; |
| 285 |
if (ep->direction == STR_IN) |
| 286 |
comp = input_urb_complete; |
| 287 |
else |
| 288 |
comp = output_urb_complete; |
| 289 |
|
| 290 |
ep->num_urbs = 0; |
| 291 |
ep->urb_free = ep->urb_free_mask = 0; |
| 292 |
for (i = 0; i < NUM_URBS; i++) { |
| 293 |
ctx = &ep->urbs[i]; |
| 294 |
ctx->index = i; |
| 295 |
ctx->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 296 |
if (!ctx->urb) { |
| 297 |
dev_err(&ep->dev->dev, "URB alloc failed\n"); |
| 298 |
return -ENOMEM; |
| 299 |
} |
| 300 |
ctx->ep = ep; |
| 301 |
buffer = usb_alloc_coherent(ep->dev, len, GFP_KERNEL, |
| 302 |
&ctx->urb->transfer_dma); |
| 303 |
if (!buffer) { |
| 304 |
dev_err(&ep->dev->dev, |
| 305 |
"URB buffer alloc failed (size %d)\n", len); |
| 306 |
return -ENOMEM; |
| 307 |
} |
| 308 |
if (ep->interval) |
| 309 |
usb_fill_int_urb(ctx->urb, ep->dev, ep->pipe, |
| 310 |
buffer, len, comp, ctx, ep->interval); |
| 311 |
else |
| 312 |
usb_fill_bulk_urb(ctx->urb, ep->dev, ep->pipe, |
| 313 |
buffer, len, comp, ctx); |
| 314 |
err = usb_urb_ep_type_check(ctx->urb); |
| 315 |
if (err < 0) { |
| 316 |
dev_err(&ep->dev->dev, "invalid MIDI EP %x\n", |
| 317 |
endpoint); |
| 318 |
return err; |
| 319 |
} |
| 320 |
ctx->urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
| 321 |
ep->num_urbs++; |
| 322 |
} |
| 323 |
ep->urb_free = ep->urb_free_mask = GENMASK(ep->num_urbs - 1, 0); |
| 324 |
return 0; |
| 325 |
} |
| 326 |
|
| 327 |
static struct snd_usb_midi2_endpoint * |
| 328 |
ump_to_endpoint(struct snd_ump_endpoint *ump, int dir) |
| 329 |
{ |
| 330 |
struct snd_usb_midi2_ump *rmidi = ump->private_data; |
| 331 |
|
| 332 |
return rmidi->eps[dir]; |
| 333 |
} |
| 334 |
|
| 335 |
/* ump open callback */ |
| 336 |
static int snd_usb_midi_v2_open(struct snd_ump_endpoint *ump, int dir) |
| 337 |
{ |
| 338 |
struct snd_usb_midi2_endpoint *ep = ump_to_endpoint(ump, dir); |
| 339 |
int err = 0; |
| 340 |
|
| 341 |
if (!ep || !ep->endpoint) |
| 342 |
return -ENODEV; |
| 343 |
if (ep->disconnected) |
| 344 |
return -EIO; |
| 345 |
if (ep->direction == STR_OUT) { |
| 346 |
err = alloc_midi_urbs(ep); |
| 347 |
if (err) { |
| 348 |
free_midi_urbs(ep); |
| 349 |
return err; |
| 350 |
} |
| 351 |
} |
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
/* ump close callback */ |
| 356 |
static void snd_usb_midi_v2_close(struct snd_ump_endpoint *ump, int dir) |
| 357 |
{ |
| 358 |
struct snd_usb_midi2_endpoint *ep = ump_to_endpoint(ump, dir); |
| 359 |
|
| 360 |
if (ep->direction == STR_OUT) { |
| 361 |
kill_midi_urbs(ep, false); |
| 362 |
drain_urb_queue(ep); |
| 363 |
free_midi_urbs(ep); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/* ump trigger callback */ |
| 368 |
static void snd_usb_midi_v2_trigger(struct snd_ump_endpoint *ump, int dir, |
| 369 |
int up) |
| 370 |
{ |
| 371 |
struct snd_usb_midi2_endpoint *ep = ump_to_endpoint(ump, dir); |
| 372 |
|
| 373 |
atomic_set(&ep->running, up); |
| 374 |
if (up && ep->direction == STR_OUT && !ep->disconnected) |
| 375 |
submit_io_urbs(ep); |
| 376 |
} |
| 377 |
|
| 378 |
/* ump drain callback */ |
| 379 |
static void snd_usb_midi_v2_drain(struct snd_ump_endpoint *ump, int dir) |
| 380 |
{ |
| 381 |
struct snd_usb_midi2_endpoint *ep = ump_to_endpoint(ump, dir); |
| 382 |
|
| 383 |
drain_urb_queue(ep); |
| 384 |
} |
| 385 |
|
| 386 |
/* allocate and start all input streams */ |
| 387 |
static int start_input_streams(struct snd_usb_midi2_interface *umidi) |
| 388 |
{ |
| 389 |
struct snd_usb_midi2_endpoint *ep; |
| 390 |
int err; |
| 391 |
|
| 392 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 393 |
if (ep->direction == STR_IN) { |
| 394 |
err = alloc_midi_urbs(ep); |
| 395 |
if (err < 0) |
| 396 |
goto error; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 401 |
if (ep->direction == STR_IN) |
| 402 |
submit_io_urbs(ep); |
| 403 |
} |
| 404 |
|
| 405 |
return 0; |
| 406 |
|
| 407 |
error: |
| 408 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 409 |
if (ep->direction == STR_IN) |
| 410 |
free_midi_urbs(ep); |
| 411 |
} |
| 412 |
|
| 413 |
return err; |
| 414 |
} |
| 415 |
|
| 416 |
static const struct snd_ump_ops snd_usb_midi_v2_ump_ops = { |
| 417 |
.open = snd_usb_midi_v2_open, |
| 418 |
.close = snd_usb_midi_v2_close, |
| 419 |
.trigger = snd_usb_midi_v2_trigger, |
| 420 |
.drain = snd_usb_midi_v2_drain, |
| 421 |
}; |
| 422 |
|
| 423 |
/* create a USB MIDI 2.0 endpoint object */ |
| 424 |
static int create_midi2_endpoint(struct snd_usb_midi2_interface *umidi, |
| 425 |
struct usb_host_endpoint *hostep, |
| 426 |
const struct usb_ms20_endpoint_descriptor *ms_ep) |
| 427 |
{ |
| 428 |
struct snd_usb_midi2_endpoint *ep; |
| 429 |
int endpoint, dir; |
| 430 |
|
| 431 |
usb_audio_dbg(umidi->chip, "Creating an EP 0x%02x, #GTB=%d\n", |
| 432 |
hostep->desc.bEndpointAddress, |
| 433 |
ms_ep->bNumGrpTrmBlock); |
| 434 |
|
| 435 |
ep = kzalloc_obj(*ep); |
| 436 |
if (!ep) |
| 437 |
return -ENOMEM; |
| 438 |
|
| 439 |
spin_lock_init(&ep->lock); |
| 440 |
init_waitqueue_head(&ep->wait); |
| 441 |
ep->dev = umidi->chip->dev; |
| 442 |
endpoint = hostep->desc.bEndpointAddress; |
| 443 |
dir = (endpoint & USB_DIR_IN) ? STR_IN : STR_OUT; |
| 444 |
|
| 445 |
ep->endpoint = endpoint; |
| 446 |
ep->direction = dir; |
| 447 |
ep->ms_ep = ms_ep; |
| 448 |
if (usb_endpoint_xfer_int(&hostep->desc)) |
| 449 |
ep->interval = hostep->desc.bInterval; |
| 450 |
else |
| 451 |
ep->interval = 0; |
| 452 |
if (dir == STR_IN) { |
| 453 |
if (ep->interval) |
| 454 |
ep->pipe = usb_rcvintpipe(ep->dev, endpoint); |
| 455 |
else |
| 456 |
ep->pipe = usb_rcvbulkpipe(ep->dev, endpoint); |
| 457 |
} else { |
| 458 |
if (ep->interval) |
| 459 |
ep->pipe = usb_sndintpipe(ep->dev, endpoint); |
| 460 |
else |
| 461 |
ep->pipe = usb_sndbulkpipe(ep->dev, endpoint); |
| 462 |
} |
| 463 |
ep->packets = usb_maxpacket(ep->dev, ep->pipe); |
| 464 |
list_add_tail(&ep->list, &umidi->ep_list); |
| 465 |
|
| 466 |
return 0; |
| 467 |
} |
| 468 |
|
| 469 |
/* destructor for endpoint; from snd_usb_midi_v2_free() */ |
| 470 |
static void free_midi2_endpoint(struct snd_usb_midi2_endpoint *ep) |
| 471 |
{ |
| 472 |
list_del(&ep->list); |
| 473 |
free_midi_urbs(ep); |
| 474 |
kfree(ep); |
| 475 |
} |
| 476 |
|
| 477 |
/* call all endpoint destructors */ |
| 478 |
static void free_all_midi2_endpoints(struct snd_usb_midi2_interface *umidi) |
| 479 |
{ |
| 480 |
struct snd_usb_midi2_endpoint *ep; |
| 481 |
|
| 482 |
while (!list_empty(&umidi->ep_list)) { |
| 483 |
ep = list_first_entry(&umidi->ep_list, |
| 484 |
struct snd_usb_midi2_endpoint, list); |
| 485 |
free_midi2_endpoint(ep); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/* find a MIDI STREAMING descriptor with a given subtype */ |
| 490 |
static void *find_usb_ms_endpoint_descriptor(struct usb_host_endpoint *hostep, |
| 491 |
unsigned char subtype) |
| 492 |
{ |
| 493 |
unsigned char *extra = hostep->extra; |
| 494 |
int extralen = hostep->extralen; |
| 495 |
|
| 496 |
while (extralen > 3) { |
| 497 |
struct usb_ms_endpoint_descriptor *ms_ep = |
| 498 |
(struct usb_ms_endpoint_descriptor *)extra; |
| 499 |
int length = ms_ep->bLength; |
| 500 |
|
| 501 |
if (!length || length > extralen) |
| 502 |
break; |
| 503 |
|
| 504 |
if (length > 3 && |
| 505 |
ms_ep->bDescriptorType == USB_DT_CS_ENDPOINT && |
| 506 |
ms_ep->bDescriptorSubtype == subtype) |
| 507 |
return ms_ep; |
| 508 |
extralen -= length; |
| 509 |
extra += length; |
| 510 |
} |
| 511 |
return NULL; |
| 512 |
} |
| 513 |
|
| 514 |
/* get the full group terminal block descriptors and return the size */ |
| 515 |
static int get_group_terminal_block_descs(struct snd_usb_midi2_interface *umidi) |
| 516 |
{ |
| 517 |
struct usb_host_interface *hostif = umidi->hostif; |
| 518 |
struct usb_device *dev = umidi->chip->dev; |
| 519 |
struct usb_ms20_gr_trm_block_header_descriptor header = { 0 }; |
| 520 |
unsigned char *data; |
| 521 |
int err, size; |
| 522 |
|
| 523 |
err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 524 |
USB_REQ_GET_DESCRIPTOR, |
| 525 |
USB_RECIP_INTERFACE | USB_TYPE_STANDARD | USB_DIR_IN, |
| 526 |
USB_DT_CS_GR_TRM_BLOCK << 8 | hostif->desc.bAlternateSetting, |
| 527 |
hostif->desc.bInterfaceNumber, |
| 528 |
&header, sizeof(header)); |
| 529 |
if (err < 0) |
| 530 |
return err; |
| 531 |
size = __le16_to_cpu(header.wTotalLength); |
| 532 |
if (!size) { |
| 533 |
dev_err(&dev->dev, "Failed to get GTB descriptors for %d:%d\n", |
| 534 |
hostif->desc.bInterfaceNumber, hostif->desc.bAlternateSetting); |
| 535 |
return -EINVAL; |
| 536 |
} |
| 537 |
|
| 538 |
data = kzalloc(size, GFP_KERNEL); |
| 539 |
if (!data) |
| 540 |
return -ENOMEM; |
| 541 |
|
| 542 |
err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 543 |
USB_REQ_GET_DESCRIPTOR, |
| 544 |
USB_RECIP_INTERFACE | USB_TYPE_STANDARD | USB_DIR_IN, |
| 545 |
USB_DT_CS_GR_TRM_BLOCK << 8 | hostif->desc.bAlternateSetting, |
| 546 |
hostif->desc.bInterfaceNumber, data, size); |
| 547 |
if (err < 0) { |
| 548 |
kfree(data); |
| 549 |
return err; |
| 550 |
} |
| 551 |
|
| 552 |
umidi->blk_descs = data; |
| 553 |
umidi->blk_desc_size = size; |
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
/* find the corresponding group terminal block descriptor */ |
| 558 |
static const struct usb_ms20_gr_trm_block_descriptor * |
| 559 |
find_group_terminal_block(struct snd_usb_midi2_interface *umidi, int id) |
| 560 |
{ |
| 561 |
const unsigned char *data = umidi->blk_descs; |
| 562 |
int size = umidi->blk_desc_size; |
| 563 |
const struct usb_ms20_gr_trm_block_descriptor *desc; |
| 564 |
|
| 565 |
size -= sizeof(struct usb_ms20_gr_trm_block_header_descriptor); |
| 566 |
data += sizeof(struct usb_ms20_gr_trm_block_header_descriptor); |
| 567 |
while (size > 0 && *data && *data <= size) { |
| 568 |
desc = (const struct usb_ms20_gr_trm_block_descriptor *)data; |
| 569 |
if (desc->bLength >= sizeof(*desc) && |
| 570 |
desc->bDescriptorType == USB_DT_CS_GR_TRM_BLOCK && |
| 571 |
desc->bDescriptorSubtype == USB_MS_GR_TRM_BLOCK && |
| 572 |
desc->bGrpTrmBlkID == id) |
| 573 |
return desc; |
| 574 |
size -= *data; |
| 575 |
data += *data; |
| 576 |
} |
| 577 |
|
| 578 |
return NULL; |
| 579 |
} |
| 580 |
|
| 581 |
/* fill up the information from GTB */ |
| 582 |
static int parse_group_terminal_block(struct snd_usb_midi2_ump *rmidi, |
| 583 |
const struct usb_ms20_gr_trm_block_descriptor *desc) |
| 584 |
{ |
| 585 |
struct snd_ump_endpoint *ump = rmidi->ump; |
| 586 |
unsigned int protocol, protocol_caps; |
| 587 |
|
| 588 |
/* set default protocol */ |
| 589 |
switch (desc->bMIDIProtocol) { |
| 590 |
case USB_MS_MIDI_PROTO_1_0_64: |
| 591 |
case USB_MS_MIDI_PROTO_1_0_64_JRTS: |
| 592 |
case USB_MS_MIDI_PROTO_1_0_128: |
| 593 |
case USB_MS_MIDI_PROTO_1_0_128_JRTS: |
| 594 |
protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI1; |
| 595 |
break; |
| 596 |
case USB_MS_MIDI_PROTO_2_0: |
| 597 |
case USB_MS_MIDI_PROTO_2_0_JRTS: |
| 598 |
protocol = SNDRV_UMP_EP_INFO_PROTO_MIDI2; |
| 599 |
break; |
| 600 |
default: |
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
if (!ump->info.protocol) |
| 605 |
ump->info.protocol = protocol; |
| 606 |
|
| 607 |
protocol_caps = protocol; |
| 608 |
switch (desc->bMIDIProtocol) { |
| 609 |
case USB_MS_MIDI_PROTO_1_0_64_JRTS: |
| 610 |
case USB_MS_MIDI_PROTO_1_0_128_JRTS: |
| 611 |
case USB_MS_MIDI_PROTO_2_0_JRTS: |
| 612 |
protocol_caps |= SNDRV_UMP_EP_INFO_PROTO_JRTS_TX | |
| 613 |
SNDRV_UMP_EP_INFO_PROTO_JRTS_RX; |
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
ump->info.protocol_caps |= protocol_caps; |
| 618 |
return 0; |
| 619 |
} |
| 620 |
|
| 621 |
/* allocate and parse for each assigned group terminal block */ |
| 622 |
static int parse_group_terminal_blocks(struct snd_usb_midi2_interface *umidi) |
| 623 |
{ |
| 624 |
struct snd_usb_midi2_ump *rmidi; |
| 625 |
const struct usb_ms20_gr_trm_block_descriptor *desc; |
| 626 |
int err; |
| 627 |
|
| 628 |
err = get_group_terminal_block_descs(umidi); |
| 629 |
if (err < 0) |
| 630 |
return err; |
| 631 |
if (!umidi->blk_descs) |
| 632 |
return 0; |
| 633 |
|
| 634 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 635 |
desc = find_group_terminal_block(umidi, rmidi->usb_block_id); |
| 636 |
if (!desc) |
| 637 |
continue; |
| 638 |
err = parse_group_terminal_block(rmidi, desc); |
| 639 |
if (err < 0) |
| 640 |
return err; |
| 641 |
} |
| 642 |
|
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
/* parse endpoints included in the given interface and create objects */ |
| 647 |
static int parse_midi_2_0_endpoints(struct snd_usb_midi2_interface *umidi) |
| 648 |
{ |
| 649 |
struct usb_host_interface *hostif = umidi->hostif; |
| 650 |
struct usb_host_endpoint *hostep; |
| 651 |
struct usb_ms20_endpoint_descriptor *ms_ep; |
| 652 |
int i, err; |
| 653 |
|
| 654 |
for (i = 0; i < hostif->desc.bNumEndpoints; i++) { |
| 655 |
hostep = &hostif->endpoint[i]; |
| 656 |
if (!usb_endpoint_xfer_bulk(&hostep->desc) && |
| 657 |
!usb_endpoint_xfer_int(&hostep->desc)) |
| 658 |
continue; |
| 659 |
ms_ep = find_usb_ms_endpoint_descriptor(hostep, USB_MS_GENERAL_2_0); |
| 660 |
if (!ms_ep) |
| 661 |
continue; |
| 662 |
if (ms_ep->bLength <= sizeof(*ms_ep)) |
| 663 |
continue; |
| 664 |
if (!ms_ep->bNumGrpTrmBlock) |
| 665 |
continue; |
| 666 |
if (ms_ep->bLength < sizeof(*ms_ep) + ms_ep->bNumGrpTrmBlock) |
| 667 |
continue; |
| 668 |
err = create_midi2_endpoint(umidi, hostep, ms_ep); |
| 669 |
if (err < 0) |
| 670 |
return err; |
| 671 |
} |
| 672 |
return 0; |
| 673 |
} |
| 674 |
|
| 675 |
static void free_all_midi2_umps(struct snd_usb_midi2_interface *umidi) |
| 676 |
{ |
| 677 |
struct snd_usb_midi2_ump *rmidi; |
| 678 |
|
| 679 |
while (!list_empty(&umidi->rawmidi_list)) { |
| 680 |
rmidi = list_first_entry(&umidi->rawmidi_list, |
| 681 |
struct snd_usb_midi2_ump, list); |
| 682 |
list_del(&rmidi->list); |
| 683 |
kfree(rmidi); |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
static int create_midi2_ump(struct snd_usb_midi2_interface *umidi, |
| 688 |
struct snd_usb_midi2_endpoint *ep_in, |
| 689 |
struct snd_usb_midi2_endpoint *ep_out, |
| 690 |
int blk_id) |
| 691 |
{ |
| 692 |
struct snd_usb_midi2_ump *rmidi; |
| 693 |
struct snd_ump_endpoint *ump; |
| 694 |
int input, output; |
| 695 |
char idstr[16]; |
| 696 |
int err; |
| 697 |
|
| 698 |
rmidi = kzalloc_obj(*rmidi); |
| 699 |
if (!rmidi) |
| 700 |
return -ENOMEM; |
| 701 |
INIT_LIST_HEAD(&rmidi->list); |
| 702 |
rmidi->dev = umidi->chip->dev; |
| 703 |
rmidi->umidi = umidi; |
| 704 |
rmidi->usb_block_id = blk_id; |
| 705 |
|
| 706 |
rmidi->index = umidi->chip->num_rawmidis; |
| 707 |
snprintf(idstr, sizeof(idstr), "UMP %d", rmidi->index); |
| 708 |
input = ep_in ? 1 : 0; |
| 709 |
output = ep_out ? 1 : 0; |
| 710 |
err = snd_ump_endpoint_new(umidi->chip->card, idstr, rmidi->index, |
| 711 |
output, input, &ump); |
| 712 |
if (err < 0) { |
| 713 |
usb_audio_dbg(umidi->chip, "Failed to create a UMP object\n"); |
| 714 |
kfree(rmidi); |
| 715 |
return err; |
| 716 |
} |
| 717 |
|
| 718 |
rmidi->ump = ump; |
| 719 |
umidi->chip->num_rawmidis++; |
| 720 |
|
| 721 |
ump->private_data = rmidi; |
| 722 |
ump->ops = &snd_usb_midi_v2_ump_ops; |
| 723 |
|
| 724 |
rmidi->eps[STR_IN] = ep_in; |
| 725 |
rmidi->eps[STR_OUT] = ep_out; |
| 726 |
if (ep_in) { |
| 727 |
ep_in->pair = ep_out; |
| 728 |
ep_in->rmidi = rmidi; |
| 729 |
ep_in->ump = ump; |
| 730 |
} |
| 731 |
if (ep_out) { |
| 732 |
ep_out->pair = ep_in; |
| 733 |
ep_out->rmidi = rmidi; |
| 734 |
ep_out->ump = ump; |
| 735 |
} |
| 736 |
|
| 737 |
list_add_tail(&rmidi->list, &umidi->rawmidi_list); |
| 738 |
return 0; |
| 739 |
} |
| 740 |
|
| 741 |
/* find the UMP EP with the given USB block id */ |
| 742 |
static struct snd_usb_midi2_ump * |
| 743 |
find_midi2_ump(struct snd_usb_midi2_interface *umidi, int blk_id) |
| 744 |
{ |
| 745 |
struct snd_usb_midi2_ump *rmidi; |
| 746 |
|
| 747 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 748 |
if (rmidi->usb_block_id == blk_id) |
| 749 |
return rmidi; |
| 750 |
} |
| 751 |
return NULL; |
| 752 |
} |
| 753 |
|
| 754 |
/* look for the matching output endpoint and create UMP object if found */ |
| 755 |
static int find_matching_ep_partner(struct snd_usb_midi2_interface *umidi, |
| 756 |
struct snd_usb_midi2_endpoint *ep, |
| 757 |
int blk_id) |
| 758 |
{ |
| 759 |
struct snd_usb_midi2_endpoint *pair_ep; |
| 760 |
int blk; |
| 761 |
|
| 762 |
usb_audio_dbg(umidi->chip, "Looking for a pair for EP-in 0x%02x\n", |
| 763 |
ep->endpoint); |
| 764 |
list_for_each_entry(pair_ep, &umidi->ep_list, list) { |
| 765 |
if (pair_ep->direction != STR_OUT) |
| 766 |
continue; |
| 767 |
if (pair_ep->pair) |
| 768 |
continue; /* already paired */ |
| 769 |
for (blk = 0; blk < pair_ep->ms_ep->bNumGrpTrmBlock; blk++) { |
| 770 |
if (pair_ep->ms_ep->baAssoGrpTrmBlkID[blk] == blk_id) { |
| 771 |
usb_audio_dbg(umidi->chip, |
| 772 |
"Found a match with EP-out 0x%02x blk %d\n", |
| 773 |
pair_ep->endpoint, blk); |
| 774 |
return create_midi2_ump(umidi, ep, pair_ep, blk_id); |
| 775 |
} |
| 776 |
} |
| 777 |
} |
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
/* Call UMP helper to parse UMP endpoints; |
| 782 |
* this needs to be called after starting the input streams for bi-directional |
| 783 |
* communications |
| 784 |
*/ |
| 785 |
static int parse_ump_endpoints(struct snd_usb_midi2_interface *umidi) |
| 786 |
{ |
| 787 |
struct snd_usb_midi2_ump *rmidi; |
| 788 |
int err; |
| 789 |
|
| 790 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 791 |
if (!rmidi->ump || |
| 792 |
!(rmidi->ump->core.info_flags & SNDRV_RAWMIDI_INFO_DUPLEX)) |
| 793 |
continue; |
| 794 |
err = snd_ump_parse_endpoint(rmidi->ump); |
| 795 |
if (!err) { |
| 796 |
rmidi->ump_parsed = true; |
| 797 |
} else { |
| 798 |
if (err == -ENOMEM) |
| 799 |
return err; |
| 800 |
/* fall back to GTB later */ |
| 801 |
} |
| 802 |
} |
| 803 |
return 0; |
| 804 |
} |
| 805 |
|
| 806 |
/* create a UMP block from a GTB entry */ |
| 807 |
static int create_gtb_block(struct snd_usb_midi2_ump *rmidi, int dir, int blk) |
| 808 |
{ |
| 809 |
struct snd_usb_midi2_interface *umidi = rmidi->umidi; |
| 810 |
const struct usb_ms20_gr_trm_block_descriptor *desc; |
| 811 |
struct snd_ump_block *fb; |
| 812 |
int type, err; |
| 813 |
|
| 814 |
desc = find_group_terminal_block(umidi, blk); |
| 815 |
if (!desc) |
| 816 |
return 0; |
| 817 |
|
| 818 |
usb_audio_dbg(umidi->chip, |
| 819 |
"GTB %d: type=%d, group=%d/%d, protocol=%d, in bw=%d, out bw=%d\n", |
| 820 |
blk, desc->bGrpTrmBlkType, desc->nGroupTrm, |
| 821 |
desc->nNumGroupTrm, desc->bMIDIProtocol, |
| 822 |
__le16_to_cpu(desc->wMaxInputBandwidth), |
| 823 |
__le16_to_cpu(desc->wMaxOutputBandwidth)); |
| 824 |
|
| 825 |
/* assign the direction */ |
| 826 |
switch (desc->bGrpTrmBlkType) { |
| 827 |
case USB_MS_GR_TRM_BLOCK_TYPE_BIDIRECTIONAL: |
| 828 |
type = SNDRV_UMP_DIR_BIDIRECTION; |
| 829 |
break; |
| 830 |
case USB_MS_GR_TRM_BLOCK_TYPE_INPUT_ONLY: |
| 831 |
type = SNDRV_UMP_DIR_INPUT; |
| 832 |
break; |
| 833 |
case USB_MS_GR_TRM_BLOCK_TYPE_OUTPUT_ONLY: |
| 834 |
type = SNDRV_UMP_DIR_OUTPUT; |
| 835 |
break; |
| 836 |
default: |
| 837 |
usb_audio_dbg(umidi->chip, "Unsupported GTB type %d\n", |
| 838 |
desc->bGrpTrmBlkType); |
| 839 |
return 0; /* unsupported */ |
| 840 |
} |
| 841 |
|
| 842 |
/* guess work: set blk-1 as the (0-based) block ID */ |
| 843 |
err = snd_ump_block_new(rmidi->ump, blk - 1, type, |
| 844 |
desc->nGroupTrm, desc->nNumGroupTrm, |
| 845 |
&fb); |
| 846 |
if (err == -EBUSY) |
| 847 |
return 0; /* already present */ |
| 848 |
else if (err) |
| 849 |
return err; |
| 850 |
|
| 851 |
if (desc->iBlockItem) |
| 852 |
usb_string(rmidi->dev, desc->iBlockItem, |
| 853 |
fb->info.name, sizeof(fb->info.name)); |
| 854 |
|
| 855 |
if (__le16_to_cpu(desc->wMaxInputBandwidth) == 1 || |
| 856 |
__le16_to_cpu(desc->wMaxOutputBandwidth) == 1) |
| 857 |
fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1 | |
| 858 |
SNDRV_UMP_BLOCK_IS_LOWSPEED; |
| 859 |
|
| 860 |
/* if MIDI 2.0 protocol is supported and yet the GTB shows MIDI 1.0, |
| 861 |
* treat it as a MIDI 1.0-specific block |
| 862 |
*/ |
| 863 |
if (rmidi->ump->info.protocol_caps & SNDRV_UMP_EP_INFO_PROTO_MIDI2) { |
| 864 |
switch (desc->bMIDIProtocol) { |
| 865 |
case USB_MS_MIDI_PROTO_1_0_64: |
| 866 |
case USB_MS_MIDI_PROTO_1_0_64_JRTS: |
| 867 |
case USB_MS_MIDI_PROTO_1_0_128: |
| 868 |
case USB_MS_MIDI_PROTO_1_0_128_JRTS: |
| 869 |
fb->info.flags |= SNDRV_UMP_BLOCK_IS_MIDI1; |
| 870 |
break; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
snd_ump_update_group_attrs(rmidi->ump); |
| 875 |
|
| 876 |
usb_audio_dbg(umidi->chip, |
| 877 |
"Created a UMP block %d from GTB, name=%s, flags=0x%x\n", |
| 878 |
blk, fb->info.name, fb->info.flags); |
| 879 |
return 0; |
| 880 |
} |
| 881 |
|
| 882 |
/* Create UMP blocks for each UMP EP */ |
| 883 |
static int create_blocks_from_gtb(struct snd_usb_midi2_interface *umidi) |
| 884 |
{ |
| 885 |
struct snd_usb_midi2_ump *rmidi; |
| 886 |
int i, blk, err, dir; |
| 887 |
|
| 888 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 889 |
if (!rmidi->ump) |
| 890 |
continue; |
| 891 |
/* Blocks have been already created? */ |
| 892 |
if (rmidi->ump_parsed || rmidi->ump->info.num_blocks) |
| 893 |
continue; |
| 894 |
/* GTB is static-only */ |
| 895 |
rmidi->ump->info.flags |= SNDRV_UMP_EP_INFO_STATIC_BLOCKS; |
| 896 |
/* loop over GTBs */ |
| 897 |
for (dir = 0; dir < 2; dir++) { |
| 898 |
if (!rmidi->eps[dir]) |
| 899 |
continue; |
| 900 |
for (i = 0; i < rmidi->eps[dir]->ms_ep->bNumGrpTrmBlock; i++) { |
| 901 |
blk = rmidi->eps[dir]->ms_ep->baAssoGrpTrmBlkID[i]; |
| 902 |
err = create_gtb_block(rmidi, dir, blk); |
| 903 |
if (err < 0) |
| 904 |
return err; |
| 905 |
} |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
/* attach legacy rawmidis */ |
| 913 |
static int attach_legacy_rawmidi(struct snd_usb_midi2_interface *umidi) |
| 914 |
{ |
| 915 |
#if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI) |
| 916 |
struct snd_usb_midi2_ump *rmidi; |
| 917 |
int err; |
| 918 |
|
| 919 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 920 |
err = snd_ump_attach_legacy_rawmidi(rmidi->ump, |
| 921 |
"Legacy MIDI", |
| 922 |
umidi->chip->num_rawmidis); |
| 923 |
if (err < 0) |
| 924 |
return err; |
| 925 |
umidi->chip->num_rawmidis++; |
| 926 |
} |
| 927 |
#endif |
| 928 |
return 0; |
| 929 |
} |
| 930 |
|
| 931 |
static void snd_usb_midi_v2_free(struct snd_usb_midi2_interface *umidi) |
| 932 |
{ |
| 933 |
free_all_midi2_endpoints(umidi); |
| 934 |
free_all_midi2_umps(umidi); |
| 935 |
list_del(&umidi->list); |
| 936 |
kfree(umidi->blk_descs); |
| 937 |
kfree(umidi); |
| 938 |
} |
| 939 |
|
| 940 |
/* parse the interface for MIDI 2.0 */ |
| 941 |
static int parse_midi_2_0(struct snd_usb_midi2_interface *umidi) |
| 942 |
{ |
| 943 |
struct snd_usb_midi2_endpoint *ep; |
| 944 |
int blk, id, err; |
| 945 |
|
| 946 |
/* First, create an object for each USB MIDI Endpoint */ |
| 947 |
err = parse_midi_2_0_endpoints(umidi); |
| 948 |
if (err < 0) |
| 949 |
return err; |
| 950 |
if (list_empty(&umidi->ep_list)) { |
| 951 |
usb_audio_warn(umidi->chip, "No MIDI endpoints found\n"); |
| 952 |
return -ENODEV; |
| 953 |
} |
| 954 |
|
| 955 |
/* |
| 956 |
* Next, look for EP I/O pairs that are found in group terminal blocks |
| 957 |
* A UMP object is created for each EP I/O pair as bidirecitonal |
| 958 |
* UMP EP |
| 959 |
*/ |
| 960 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 961 |
/* only input in this loop; output is matched in find_midi_ump() */ |
| 962 |
if (ep->direction != STR_IN) |
| 963 |
continue; |
| 964 |
for (blk = 0; blk < ep->ms_ep->bNumGrpTrmBlock; blk++) { |
| 965 |
id = ep->ms_ep->baAssoGrpTrmBlkID[blk]; |
| 966 |
err = find_matching_ep_partner(umidi, ep, id); |
| 967 |
if (err < 0) |
| 968 |
return err; |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
/* |
| 973 |
* For the remaining EPs, treat as singles, create a UMP object with |
| 974 |
* unidirectional EP |
| 975 |
*/ |
| 976 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 977 |
if (ep->rmidi) |
| 978 |
continue; /* already paired */ |
| 979 |
for (blk = 0; blk < ep->ms_ep->bNumGrpTrmBlock; blk++) { |
| 980 |
id = ep->ms_ep->baAssoGrpTrmBlkID[blk]; |
| 981 |
if (find_midi2_ump(umidi, id)) |
| 982 |
continue; |
| 983 |
usb_audio_dbg(umidi->chip, |
| 984 |
"Creating a unidirection UMP for EP=0x%02x, blk=%d\n", |
| 985 |
ep->endpoint, id); |
| 986 |
if (ep->direction == STR_IN) |
| 987 |
err = create_midi2_ump(umidi, ep, NULL, id); |
| 988 |
else |
| 989 |
err = create_midi2_ump(umidi, NULL, ep, id); |
| 990 |
if (err < 0) |
| 991 |
return err; |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
return 0; |
| 997 |
} |
| 998 |
|
| 999 |
/* is the given interface for MIDI 2.0? */ |
| 1000 |
static bool is_midi2_altset(struct usb_host_interface *hostif) |
| 1001 |
{ |
| 1002 |
struct usb_ms_header_descriptor *ms_header = |
| 1003 |
(struct usb_ms_header_descriptor *)hostif->extra; |
| 1004 |
|
| 1005 |
if (hostif->extralen < 7 || |
| 1006 |
ms_header->bLength < 7 || |
| 1007 |
ms_header->bDescriptorType != USB_DT_CS_INTERFACE || |
| 1008 |
ms_header->bDescriptorSubtype != UAC_HEADER) |
| 1009 |
return false; |
| 1010 |
|
| 1011 |
return le16_to_cpu(ms_header->bcdMSC) == USB_MS_REV_MIDI_2_0; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/* change the altsetting */ |
| 1015 |
static int set_altset(struct snd_usb_midi2_interface *umidi) |
| 1016 |
{ |
| 1017 |
usb_audio_dbg(umidi->chip, "Setting host iface %d:%d\n", |
| 1018 |
umidi->hostif->desc.bInterfaceNumber, |
| 1019 |
umidi->hostif->desc.bAlternateSetting); |
| 1020 |
return usb_set_interface(umidi->chip->dev, |
| 1021 |
umidi->hostif->desc.bInterfaceNumber, |
| 1022 |
umidi->hostif->desc.bAlternateSetting); |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* fill UMP Endpoint name string from USB descriptor */ |
| 1026 |
static void fill_ump_ep_name(struct snd_ump_endpoint *ump, |
| 1027 |
struct usb_device *dev, int id) |
| 1028 |
{ |
| 1029 |
int len; |
| 1030 |
|
| 1031 |
usb_string(dev, id, ump->info.name, sizeof(ump->info.name)); |
| 1032 |
|
| 1033 |
/* trim superfluous "MIDI" suffix */ |
| 1034 |
len = strlen(ump->info.name); |
| 1035 |
if (len > 5 && !strcmp(ump->info.name + len - 5, " MIDI")) |
| 1036 |
ump->info.name[len - 5] = 0; |
| 1037 |
} |
| 1038 |
|
| 1039 |
/* fill the fallback name string for each rawmidi instance */ |
| 1040 |
static void set_fallback_rawmidi_names(struct snd_usb_midi2_interface *umidi) |
| 1041 |
{ |
| 1042 |
struct usb_device *dev = umidi->chip->dev; |
| 1043 |
struct snd_usb_midi2_ump *rmidi; |
| 1044 |
struct snd_ump_endpoint *ump; |
| 1045 |
|
| 1046 |
list_for_each_entry(rmidi, &umidi->rawmidi_list, list) { |
| 1047 |
ump = rmidi->ump; |
| 1048 |
/* fill UMP EP name from USB descriptors */ |
| 1049 |
if (!*ump->info.name && umidi->hostif->desc.iInterface) |
| 1050 |
fill_ump_ep_name(ump, dev, umidi->hostif->desc.iInterface); |
| 1051 |
else if (!*ump->info.name && dev->descriptor.iProduct) |
| 1052 |
fill_ump_ep_name(ump, dev, dev->descriptor.iProduct); |
| 1053 |
/* fill fallback name */ |
| 1054 |
if (!*ump->info.name) |
| 1055 |
scnprintf(ump->info.name, sizeof(ump->info.name), |
| 1056 |
"USB MIDI %d", rmidi->index); |
| 1057 |
/* copy as rawmidi name if not set */ |
| 1058 |
if (!*ump->core.name) |
| 1059 |
strscpy(ump->core.name, ump->info.name, |
| 1060 |
sizeof(ump->core.name)); |
| 1061 |
/* use serial number string as unique UMP product id */ |
| 1062 |
if (!*ump->info.product_id && dev->descriptor.iSerialNumber) |
| 1063 |
usb_string(dev, dev->descriptor.iSerialNumber, |
| 1064 |
ump->info.product_id, |
| 1065 |
sizeof(ump->info.product_id)); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
/* create MIDI interface; fallback to MIDI 1.0 if needed */ |
| 1070 |
int snd_usb_midi_v2_create(struct snd_usb_audio *chip, |
| 1071 |
struct usb_interface *iface, |
| 1072 |
const struct snd_usb_audio_quirk *quirk, |
| 1073 |
unsigned int usb_id) |
| 1074 |
{ |
| 1075 |
struct snd_usb_midi2_interface *umidi; |
| 1076 |
struct usb_host_interface *hostif; |
| 1077 |
int err; |
| 1078 |
|
| 1079 |
usb_audio_dbg(chip, "Parsing interface %d...\n", |
| 1080 |
iface->altsetting[0].desc.bInterfaceNumber); |
| 1081 |
|
| 1082 |
/* fallback to MIDI 1.0? */ |
| 1083 |
if (!midi2_enable) { |
| 1084 |
usb_audio_info(chip, "Falling back to MIDI 1.0 by module option\n"); |
| 1085 |
goto fallback_to_midi1; |
| 1086 |
} |
| 1087 |
if ((quirk && quirk->type != QUIRK_MIDI_STANDARD_INTERFACE) || |
| 1088 |
iface->num_altsetting < 2) { |
| 1089 |
usb_audio_info(chip, "Quirk or no altset; falling back to MIDI 1.0\n"); |
| 1090 |
goto fallback_to_midi1; |
| 1091 |
} |
| 1092 |
hostif = &iface->altsetting[1]; |
| 1093 |
if (!is_midi2_altset(hostif)) { |
| 1094 |
usb_audio_info(chip, "No MIDI 2.0 at altset 1, falling back to MIDI 1.0\n"); |
| 1095 |
goto fallback_to_midi1; |
| 1096 |
} |
| 1097 |
if (!hostif->desc.bNumEndpoints) { |
| 1098 |
usb_audio_info(chip, "No endpoint at altset 1, falling back to MIDI 1.0\n"); |
| 1099 |
goto fallback_to_midi1; |
| 1100 |
} |
| 1101 |
|
| 1102 |
usb_audio_dbg(chip, "Creating a MIDI 2.0 instance for %d:%d\n", |
| 1103 |
hostif->desc.bInterfaceNumber, |
| 1104 |
hostif->desc.bAlternateSetting); |
| 1105 |
|
| 1106 |
umidi = kzalloc_obj(*umidi); |
| 1107 |
if (!umidi) |
| 1108 |
return -ENOMEM; |
| 1109 |
umidi->chip = chip; |
| 1110 |
umidi->iface = iface; |
| 1111 |
umidi->hostif = hostif; |
| 1112 |
INIT_LIST_HEAD(&umidi->rawmidi_list); |
| 1113 |
INIT_LIST_HEAD(&umidi->ep_list); |
| 1114 |
|
| 1115 |
list_add_tail(&umidi->list, &chip->midi_v2_list); |
| 1116 |
|
| 1117 |
err = set_altset(umidi); |
| 1118 |
if (err < 0) { |
| 1119 |
usb_audio_err(chip, "Failed to set altset\n"); |
| 1120 |
goto error; |
| 1121 |
} |
| 1122 |
|
| 1123 |
/* assume only altset 1 corresponding to MIDI 2.0 interface */ |
| 1124 |
err = parse_midi_2_0(umidi); |
| 1125 |
if (err < 0) { |
| 1126 |
usb_audio_err(chip, "Failed to parse MIDI 2.0 interface\n"); |
| 1127 |
goto error; |
| 1128 |
} |
| 1129 |
|
| 1130 |
/* parse USB group terminal blocks */ |
| 1131 |
err = parse_group_terminal_blocks(umidi); |
| 1132 |
if (err < 0) { |
| 1133 |
usb_audio_err(chip, "Failed to parse GTB\n"); |
| 1134 |
goto error; |
| 1135 |
} |
| 1136 |
|
| 1137 |
err = start_input_streams(umidi); |
| 1138 |
if (err < 0) { |
| 1139 |
usb_audio_err(chip, "Failed to start input streams\n"); |
| 1140 |
goto error; |
| 1141 |
} |
| 1142 |
|
| 1143 |
if (midi2_ump_probe) { |
| 1144 |
err = parse_ump_endpoints(umidi); |
| 1145 |
if (err < 0) { |
| 1146 |
usb_audio_err(chip, "Failed to parse UMP endpoint\n"); |
| 1147 |
goto error; |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
err = create_blocks_from_gtb(umidi); |
| 1152 |
if (err < 0) { |
| 1153 |
usb_audio_err(chip, "Failed to create GTB blocks\n"); |
| 1154 |
goto error; |
| 1155 |
} |
| 1156 |
|
| 1157 |
set_fallback_rawmidi_names(umidi); |
| 1158 |
|
| 1159 |
err = attach_legacy_rawmidi(umidi); |
| 1160 |
if (err < 0) { |
| 1161 |
usb_audio_err(chip, "Failed to create legacy rawmidi\n"); |
| 1162 |
goto error; |
| 1163 |
} |
| 1164 |
|
| 1165 |
return 0; |
| 1166 |
|
| 1167 |
error: |
| 1168 |
snd_usb_midi_v2_free(umidi); |
| 1169 |
return err; |
| 1170 |
|
| 1171 |
fallback_to_midi1: |
| 1172 |
return __snd_usbmidi_create(chip->card, iface, &chip->midi_list, |
| 1173 |
quirk, usb_id, &chip->num_rawmidis); |
| 1174 |
} |
| 1175 |
|
| 1176 |
static void suspend_midi2_endpoint(struct snd_usb_midi2_endpoint *ep) |
| 1177 |
{ |
| 1178 |
kill_midi_urbs(ep, true); |
| 1179 |
drain_urb_queue(ep); |
| 1180 |
} |
| 1181 |
|
| 1182 |
void snd_usb_midi_v2_suspend_all(struct snd_usb_audio *chip) |
| 1183 |
{ |
| 1184 |
struct snd_usb_midi2_interface *umidi; |
| 1185 |
struct snd_usb_midi2_endpoint *ep; |
| 1186 |
|
| 1187 |
list_for_each_entry(umidi, &chip->midi_v2_list, list) { |
| 1188 |
list_for_each_entry(ep, &umidi->ep_list, list) |
| 1189 |
suspend_midi2_endpoint(ep); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
static void resume_midi2_endpoint(struct snd_usb_midi2_endpoint *ep) |
| 1194 |
{ |
| 1195 |
atomic_set(&ep->running, atomic_read(&ep->suspended)); |
| 1196 |
atomic_set(&ep->suspended, 0); |
| 1197 |
|
| 1198 |
if (ep->direction == STR_IN || atomic_read(&ep->running)) |
| 1199 |
submit_io_urbs(ep); |
| 1200 |
} |
| 1201 |
|
| 1202 |
void snd_usb_midi_v2_resume_all(struct snd_usb_audio *chip) |
| 1203 |
{ |
| 1204 |
struct snd_usb_midi2_interface *umidi; |
| 1205 |
struct snd_usb_midi2_endpoint *ep; |
| 1206 |
|
| 1207 |
list_for_each_entry(umidi, &chip->midi_v2_list, list) { |
| 1208 |
set_altset(umidi); |
| 1209 |
list_for_each_entry(ep, &umidi->ep_list, list) |
| 1210 |
resume_midi2_endpoint(ep); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
void snd_usb_midi_v2_disconnect_all(struct snd_usb_audio *chip) |
| 1215 |
{ |
| 1216 |
struct snd_usb_midi2_interface *umidi; |
| 1217 |
struct snd_usb_midi2_endpoint *ep; |
| 1218 |
|
| 1219 |
list_for_each_entry(umidi, &chip->midi_v2_list, list) { |
| 1220 |
umidi->disconnected = 1; |
| 1221 |
list_for_each_entry(ep, &umidi->ep_list, list) { |
| 1222 |
ep->disconnected = 1; |
| 1223 |
kill_midi_urbs(ep, false); |
| 1224 |
drain_urb_queue(ep); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
/* release the MIDI instance */ |
| 1230 |
void snd_usb_midi_v2_free_all(struct snd_usb_audio *chip) |
| 1231 |
{ |
| 1232 |
struct snd_usb_midi2_interface *umidi, *next; |
| 1233 |
|
| 1234 |
list_for_each_entry_safe(umidi, next, &chip->midi_v2_list, list) |
| 1235 |
snd_usb_midi_v2_free(umidi); |
| 1236 |
} |