1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2022-2025 Qualcomm Innovation Center, Inc. All rights reserved. 4 */ 5 6 #include <linux/auxiliary_bus.h> 7 #include <linux/ctype.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/dma-map-ops.h> 10 #include <linux/init.h> 11 #include <linux/iommu.h> 12 #include <linux/module.h> 13 #include <linux/moduleparam.h> 14 #include <linux/soc/qcom/qmi.h> 15 #include <linux/usb.h> 16 #include <linux/usb/audio.h> 17 #include <linux/usb/audio-v2.h> 18 #include <linux/usb/audio-v3.h> 19 #include <linux/usb/hcd.h> 20 #include <linux/usb/quirks.h> 21 #include <linux/usb/xhci-sideband.h> 22 23 #include <sound/control.h> 24 #include <sound/core.h> 25 #include <sound/info.h> 26 #include <sound/initval.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/q6usboffload.h> 30 #include <sound/soc.h> 31 #include <sound/soc-usb.h> 32 33 #include "../usbaudio.h" 34 #include "../card.h" 35 #include "../endpoint.h" 36 #include "../format.h" 37 #include "../helper.h" 38 #include "../pcm.h" 39 #include "../power.h" 40 41 #include "mixer_usb_offload.h" 42 #include "usb_audio_qmi_v01.h" 43 44 /* Stream disable request timeout during USB device disconnect */ 45 #define DEV_RELEASE_WAIT_TIMEOUT 10000 /* in ms */ 46 47 /* Data interval calculation parameters */ 48 #define BUS_INTERVAL_FULL_SPEED 1000 /* in us */ 49 #define BUS_INTERVAL_HIGHSPEED_AND_ABOVE 125 /* in us */ 50 #define MAX_BINTERVAL_ISOC_EP 16 51 52 #define QMI_STREAM_REQ_CARD_NUM_MASK 0xffff0000 53 #define QMI_STREAM_REQ_DEV_NUM_MASK 0xff00 54 #define QMI_STREAM_REQ_DIRECTION 0xff 55 56 /* iommu resource parameters and management */ 57 #define PREPEND_SID_TO_IOVA(iova, sid) ((u64)(((u64)(iova)) | \ 58 (((u64)sid) << 32))) 59 #define IOVA_MASK(iova) (((u64)(iova)) & 0xFFFFFFFF) 60 #define IOVA_BASE 0x1000 61 #define IOVA_XFER_RING_BASE (IOVA_BASE + PAGE_SIZE * (SNDRV_CARDS + 1)) 62 #define IOVA_XFER_BUF_BASE (IOVA_XFER_RING_BASE + PAGE_SIZE * SNDRV_CARDS * 32) 63 #define IOVA_XFER_RING_MAX (IOVA_XFER_BUF_BASE - PAGE_SIZE) 64 #define IOVA_XFER_BUF_MAX (0xfffff000 - PAGE_SIZE) 65 66 #define MAX_XFER_BUFF_LEN (24 * PAGE_SIZE) 67 68 struct iova_info { 69 struct list_head list; 70 unsigned long start_iova; 71 size_t size; 72 bool in_use; 73 }; 74 75 struct intf_info { 76 /* IOMMU ring/buffer mapping information */ 77 unsigned long data_xfer_ring_va; 78 size_t data_xfer_ring_size; 79 unsigned long sync_xfer_ring_va; 80 size_t sync_xfer_ring_size; 81 dma_addr_t xfer_buf_iova; 82 size_t xfer_buf_size; 83 dma_addr_t xfer_buf_dma; 84 u8 *xfer_buf_cpu; 85 86 /* USB endpoint information */ 87 unsigned int data_ep_pipe; 88 unsigned int sync_ep_pipe; 89 unsigned int data_ep_idx; 90 unsigned int sync_ep_idx; 91 92 u8 intf_num; 93 u8 pcm_card_num; 94 u8 pcm_dev_num; 95 u8 direction; 96 bool in_use; 97 }; 98 99 struct uaudio_qmi_dev { 100 struct device *dev; 101 struct q6usb_offload *data; 102 struct auxiliary_device *auxdev; 103 104 /* list to keep track of available iova */ 105 struct list_head xfer_ring_list; 106 size_t xfer_ring_iova_size; 107 unsigned long curr_xfer_ring_iova; 108 struct list_head xfer_buf_list; 109 size_t xfer_buf_iova_size; 110 unsigned long curr_xfer_buf_iova; 111 112 /* bit fields representing pcm card enabled */ 113 unsigned long card_slot; 114 /* indicate event ring mapped or not */ 115 bool er_mapped; 116 }; 117 118 struct uaudio_dev { 119 struct usb_device *udev; 120 /* audio control interface */ 121 struct usb_host_interface *ctrl_intf; 122 unsigned int usb_core_id; 123 atomic_t in_use; 124 struct kref kref; 125 wait_queue_head_t disconnect_wq; 126 127 /* interface specific */ 128 int num_intf; 129 struct intf_info *info; 130 struct snd_usb_audio *chip; 131 132 /* xhci sideband */ 133 struct xhci_sideband *sb; 134 135 /* SoC USB device */ 136 struct snd_soc_usb_device *sdev; 137 }; 138 139 static struct uaudio_dev uadev[SNDRV_CARDS]; 140 static struct uaudio_qmi_dev *uaudio_qdev; 141 static struct uaudio_qmi_svc *uaudio_svc; 142 static DEFINE_MUTEX(qdev_mutex); 143 144 struct uaudio_qmi_svc { 145 struct qmi_handle *uaudio_svc_hdl; 146 struct sockaddr_qrtr client_sq; 147 bool client_connected; 148 }; 149 150 enum mem_type { 151 MEM_EVENT_RING, 152 MEM_XFER_RING, 153 MEM_XFER_BUF, 154 }; 155 156 /* Supported audio formats */ 157 enum usb_qmi_audio_format { 158 USB_QMI_PCM_FORMAT_S8 = 0, 159 USB_QMI_PCM_FORMAT_U8, 160 USB_QMI_PCM_FORMAT_S16_LE, 161 USB_QMI_PCM_FORMAT_S16_BE, 162 USB_QMI_PCM_FORMAT_U16_LE, 163 USB_QMI_PCM_FORMAT_U16_BE, 164 USB_QMI_PCM_FORMAT_S24_LE, 165 USB_QMI_PCM_FORMAT_S24_BE, 166 USB_QMI_PCM_FORMAT_U24_LE, 167 USB_QMI_PCM_FORMAT_U24_BE, 168 USB_QMI_PCM_FORMAT_S24_3LE, 169 USB_QMI_PCM_FORMAT_S24_3BE, 170 USB_QMI_PCM_FORMAT_U24_3LE, 171 USB_QMI_PCM_FORMAT_U24_3BE, 172 USB_QMI_PCM_FORMAT_S32_LE, 173 USB_QMI_PCM_FORMAT_S32_BE, 174 USB_QMI_PCM_FORMAT_U32_LE, 175 USB_QMI_PCM_FORMAT_U32_BE, 176 }; 177 178 static int usb_qmi_get_pcm_num(struct snd_usb_audio *chip, int direction) 179 { 180 struct snd_usb_substream *subs = NULL; 181 struct snd_usb_stream *as; 182 int count = 0; 183 184 list_for_each_entry(as, &chip->pcm_list, list) { 185 subs = &as->substream[direction]; 186 if (subs->ep_num) 187 count++; 188 } 189 190 return count; 191 } 192 193 static enum usb_qmi_audio_device_speed_enum_v01 194 get_speed_info(enum usb_device_speed udev_speed) 195 { 196 switch (udev_speed) { 197 case USB_SPEED_LOW: 198 return USB_QMI_DEVICE_SPEED_LOW_V01; 199 case USB_SPEED_FULL: 200 return USB_QMI_DEVICE_SPEED_FULL_V01; 201 case USB_SPEED_HIGH: 202 return USB_QMI_DEVICE_SPEED_HIGH_V01; 203 case USB_SPEED_SUPER: 204 return USB_QMI_DEVICE_SPEED_SUPER_V01; 205 case USB_SPEED_SUPER_PLUS: 206 return USB_QMI_DEVICE_SPEED_SUPER_PLUS_V01; 207 default: 208 return USB_QMI_DEVICE_SPEED_INVALID_V01; 209 } 210 } 211 212 static struct snd_usb_substream *find_substream(unsigned int card_num, 213 unsigned int pcm_idx, 214 unsigned int direction) 215 { 216 struct snd_usb_substream *subs = NULL; 217 struct snd_usb_audio *chip; 218 struct snd_usb_stream *as; 219 220 chip = uadev[card_num].chip; 221 if (!chip || atomic_read(&chip->shutdown)) 222 goto done; 223 224 if (pcm_idx >= chip->pcm_devs) 225 goto done; 226 227 if (direction > SNDRV_PCM_STREAM_CAPTURE) 228 goto done; 229 230 list_for_each_entry(as, &chip->pcm_list, list) { 231 if (as->pcm_index == pcm_idx) { 232 subs = &as->substream[direction]; 233 goto done; 234 } 235 } 236 237 done: 238 return subs; 239 } 240 241 static int info_idx_from_ifnum(int card_num, int intf_num, bool enable) 242 { 243 int i; 244 245 /* 246 * default index 0 is used when info is allocated upon 247 * first enable audio stream req for a pcm device 248 */ 249 if (enable && !uadev[card_num].info) 250 return 0; 251 252 for (i = 0; i < uadev[card_num].num_intf; i++) { 253 if (enable && !uadev[card_num].info[i].in_use) 254 return i; 255 else if (!enable && 256 uadev[card_num].info[i].intf_num == intf_num) 257 return i; 258 } 259 260 return -EINVAL; 261 } 262 263 static int get_data_interval_from_si(struct snd_usb_substream *subs, 264 u32 service_interval) 265 { 266 unsigned int bus_intval_mult; 267 unsigned int bus_intval; 268 unsigned int binterval; 269 270 if (subs->dev->speed >= USB_SPEED_HIGH) 271 bus_intval = BUS_INTERVAL_HIGHSPEED_AND_ABOVE; 272 else 273 bus_intval = BUS_INTERVAL_FULL_SPEED; 274 275 if (service_interval % bus_intval) 276 return -EINVAL; 277 278 bus_intval_mult = service_interval / bus_intval; 279 binterval = ffs(bus_intval_mult); 280 if (!binterval || binterval > MAX_BINTERVAL_ISOC_EP) 281 return -EINVAL; 282 283 /* check if another bit is set then bail out */ 284 bus_intval_mult = bus_intval_mult >> binterval; 285 if (bus_intval_mult) 286 return -EINVAL; 287 288 return (binterval - 1); 289 } 290 291 /* maps audio format received over QMI to asound.h based pcm format */ 292 static snd_pcm_format_t map_pcm_format(enum usb_qmi_audio_format fmt_received) 293 { 294 switch (fmt_received) { 295 case USB_QMI_PCM_FORMAT_S8: 296 return SNDRV_PCM_FORMAT_S8; 297 case USB_QMI_PCM_FORMAT_U8: 298 return SNDRV_PCM_FORMAT_U8; 299 case USB_QMI_PCM_FORMAT_S16_LE: 300 return SNDRV_PCM_FORMAT_S16_LE; 301 case USB_QMI_PCM_FORMAT_S16_BE: 302 return SNDRV_PCM_FORMAT_S16_BE; 303 case USB_QMI_PCM_FORMAT_U16_LE: 304 return SNDRV_PCM_FORMAT_U16_LE; 305 case USB_QMI_PCM_FORMAT_U16_BE: 306 return SNDRV_PCM_FORMAT_U16_BE; 307 case USB_QMI_PCM_FORMAT_S24_LE: 308 return SNDRV_PCM_FORMAT_S24_LE; 309 case USB_QMI_PCM_FORMAT_S24_BE: 310 return SNDRV_PCM_FORMAT_S24_BE; 311 case USB_QMI_PCM_FORMAT_U24_LE: 312 return SNDRV_PCM_FORMAT_U24_LE; 313 case USB_QMI_PCM_FORMAT_U24_BE: 314 return SNDRV_PCM_FORMAT_U24_BE; 315 case USB_QMI_PCM_FORMAT_S24_3LE: 316 return SNDRV_PCM_FORMAT_S24_3LE; 317 case USB_QMI_PCM_FORMAT_S24_3BE: 318 return SNDRV_PCM_FORMAT_S24_3BE; 319 case USB_QMI_PCM_FORMAT_U24_3LE: 320 return SNDRV_PCM_FORMAT_U24_3LE; 321 case USB_QMI_PCM_FORMAT_U24_3BE: 322 return SNDRV_PCM_FORMAT_U24_3BE; 323 case USB_QMI_PCM_FORMAT_S32_LE: 324 return SNDRV_PCM_FORMAT_S32_LE; 325 case USB_QMI_PCM_FORMAT_S32_BE: 326 return SNDRV_PCM_FORMAT_S32_BE; 327 case USB_QMI_PCM_FORMAT_U32_LE: 328 return SNDRV_PCM_FORMAT_U32_LE; 329 case USB_QMI_PCM_FORMAT_U32_BE: 330 return SNDRV_PCM_FORMAT_U32_BE; 331 default: 332 /* 333 * We expect the caller to do input validation so we should 334 * never hit this. But we do have to return a proper 335 * snd_pcm_format_t value due to the __bitwise attribute; so 336 * just return the equivalent of 0 in case of bad input. 337 */ 338 return SNDRV_PCM_FORMAT_S8; 339 } 340 } 341 342 /* 343 * Sends QMI disconnect indication message, assumes chip->mutex and qdev_mutex 344 * lock held by caller. 345 */ 346 static int uaudio_send_disconnect_ind(struct snd_usb_audio *chip) 347 { 348 struct qmi_uaudio_stream_ind_msg_v01 disconnect_ind = {0}; 349 struct uaudio_qmi_svc *svc = uaudio_svc; 350 struct uaudio_dev *dev; 351 int ret = 0; 352 353 dev = &uadev[chip->card->number]; 354 355 if (atomic_read(&dev->in_use)) { 356 mutex_unlock(&chip->mutex); 357 mutex_unlock(&qdev_mutex); 358 dev_dbg(uaudio_qdev->data->dev, "sending qmi indication suspend\n"); 359 disconnect_ind.dev_event = USB_QMI_DEV_DISCONNECT_V01; 360 disconnect_ind.slot_id = dev->udev->slot_id; 361 disconnect_ind.controller_num = dev->usb_core_id; 362 disconnect_ind.controller_num_valid = 1; 363 ret = qmi_send_indication(svc->uaudio_svc_hdl, &svc->client_sq, 364 QMI_UAUDIO_STREAM_IND_V01, 365 QMI_UAUDIO_STREAM_IND_MSG_V01_MAX_MSG_LEN, 366 qmi_uaudio_stream_ind_msg_v01_ei, 367 &disconnect_ind); 368 if (ret < 0) 369 dev_err(uaudio_qdev->data->dev, 370 "qmi send failed with err: %d\n", ret); 371 372 ret = wait_event_interruptible_timeout(dev->disconnect_wq, 373 !atomic_read(&dev->in_use), 374 msecs_to_jiffies(DEV_RELEASE_WAIT_TIMEOUT)); 375 if (!ret) { 376 dev_err(uaudio_qdev->data->dev, 377 "timeout while waiting for dev_release\n"); 378 atomic_set(&dev->in_use, 0); 379 } else if (ret < 0) { 380 dev_err(uaudio_qdev->data->dev, 381 "failed with ret %d\n", ret); 382 atomic_set(&dev->in_use, 0); 383 } 384 mutex_lock(&qdev_mutex); 385 mutex_lock(&chip->mutex); 386 } 387 388 return ret; 389 } 390 391 /* Offloading IOMMU management */ 392 static unsigned long uaudio_get_iova(unsigned long *curr_iova, 393 size_t *curr_iova_size, 394 struct list_head *head, size_t size) 395 { 396 struct iova_info *info, *new_info = NULL; 397 struct list_head *curr_head; 398 size_t tmp_size = size; 399 unsigned long iova = 0; 400 401 if (size % PAGE_SIZE) 402 goto done; 403 404 if (size > *curr_iova_size) 405 goto done; 406 407 if (*curr_iova_size == 0) 408 goto done; 409 410 list_for_each_entry(info, head, list) { 411 /* exact size iova_info */ 412 if (!info->in_use && info->size == size) { 413 info->in_use = true; 414 iova = info->start_iova; 415 *curr_iova_size -= size; 416 goto done; 417 } else if (!info->in_use && tmp_size >= info->size) { 418 if (!new_info) 419 new_info = info; 420 tmp_size -= info->size; 421 if (tmp_size) 422 continue; 423 424 iova = new_info->start_iova; 425 for (curr_head = &new_info->list; curr_head != 426 &info->list; curr_head = curr_head->next) { 427 new_info = list_entry(curr_head, struct 428 iova_info, list); 429 new_info->in_use = true; 430 } 431 info->in_use = true; 432 *curr_iova_size -= size; 433 goto done; 434 } else { 435 /* iova region in use */ 436 new_info = NULL; 437 tmp_size = size; 438 } 439 } 440 441 info = kzalloc(sizeof(*info), GFP_KERNEL); 442 if (!info) { 443 iova = 0; 444 goto done; 445 } 446 447 iova = *curr_iova; 448 info->start_iova = *curr_iova; 449 info->size = size; 450 info->in_use = true; 451 *curr_iova += size; 452 *curr_iova_size -= size; 453 list_add_tail(&info->list, head); 454 455 done: 456 return iova; 457 } 458 459 static void uaudio_put_iova(unsigned long iova, size_t size, struct list_head 460 *head, size_t *curr_iova_size) 461 { 462 struct iova_info *info; 463 size_t tmp_size = size; 464 bool found = false; 465 466 list_for_each_entry(info, head, list) { 467 if (info->start_iova == iova) { 468 if (!info->in_use) 469 return; 470 471 found = true; 472 info->in_use = false; 473 if (info->size == size) 474 goto done; 475 } 476 477 if (found && tmp_size >= info->size) { 478 info->in_use = false; 479 tmp_size -= info->size; 480 if (!tmp_size) 481 goto done; 482 } 483 } 484 485 if (!found) 486 return; 487 488 done: 489 *curr_iova_size += size; 490 } 491 492 /** 493 * uaudio_iommu_unmap() - unmaps iommu memory for adsp 494 * @mtype: ring type 495 * @iova: virtual address to unmap 496 * @iova_size: region size 497 * @mapped_iova_size: mapped region size 498 * 499 * Unmaps the memory region that was previously assigned to the adsp. 500 * 501 */ 502 static void uaudio_iommu_unmap(enum mem_type mtype, unsigned long iova, 503 size_t iova_size, size_t mapped_iova_size) 504 { 505 size_t umap_size; 506 bool unmap = true; 507 508 if (!iova || !iova_size) 509 return; 510 511 switch (mtype) { 512 case MEM_EVENT_RING: 513 if (uaudio_qdev->er_mapped) 514 uaudio_qdev->er_mapped = false; 515 else 516 unmap = false; 517 break; 518 519 case MEM_XFER_RING: 520 uaudio_put_iova(iova, iova_size, &uaudio_qdev->xfer_ring_list, 521 &uaudio_qdev->xfer_ring_iova_size); 522 break; 523 case MEM_XFER_BUF: 524 uaudio_put_iova(iova, iova_size, &uaudio_qdev->xfer_buf_list, 525 &uaudio_qdev->xfer_buf_iova_size); 526 break; 527 default: 528 unmap = false; 529 } 530 531 if (!unmap || !mapped_iova_size) 532 return; 533 534 umap_size = iommu_unmap(uaudio_qdev->data->domain, iova, mapped_iova_size); 535 if (umap_size != mapped_iova_size) 536 dev_err(uaudio_qdev->data->dev, 537 "unmapped size %zu for iova 0x%08lx of mapped size %zu\n", 538 umap_size, iova, mapped_iova_size); 539 } 540 541 static int uaudio_iommu_map_prot(bool dma_coherent) 542 { 543 int prot = IOMMU_READ | IOMMU_WRITE; 544 545 if (dma_coherent) 546 prot |= IOMMU_CACHE; 547 return prot; 548 } 549 550 /** 551 * uaudio_iommu_map_pa() - maps iommu memory for adsp 552 * @mtype: ring type 553 * @dma_coherent: dma coherent 554 * @pa: physical address for ring/buffer 555 * @size: size of memory region 556 * 557 * Maps the XHCI related resources to a memory region that is assigned to be 558 * used by the adsp. This will be mapped to the domain, which is created by 559 * the ASoC USB backend driver. 560 * 561 */ 562 static unsigned long uaudio_iommu_map_pa(enum mem_type mtype, bool dma_coherent, 563 phys_addr_t pa, size_t size) 564 { 565 unsigned long iova = 0; 566 bool map = true; 567 int prot = uaudio_iommu_map_prot(dma_coherent); 568 569 switch (mtype) { 570 case MEM_EVENT_RING: 571 iova = IOVA_BASE; 572 /* er already mapped */ 573 if (uaudio_qdev->er_mapped) 574 map = false; 575 break; 576 case MEM_XFER_RING: 577 iova = uaudio_get_iova(&uaudio_qdev->curr_xfer_ring_iova, 578 &uaudio_qdev->xfer_ring_iova_size, 579 &uaudio_qdev->xfer_ring_list, size); 580 break; 581 default: 582 dev_err(uaudio_qdev->data->dev, "unknown mem type %d\n", mtype); 583 } 584 585 if (!iova || !map) 586 return 0; 587 588 iommu_map(uaudio_qdev->data->domain, iova, pa, size, prot, GFP_KERNEL); 589 590 return iova; 591 } 592 593 static unsigned long uaudio_iommu_map_xfer_buf(bool dma_coherent, size_t size, 594 struct sg_table *sgt) 595 { 596 struct scatterlist *sg; 597 unsigned long iova = 0; 598 size_t total_len = 0; 599 unsigned long iova_sg; 600 phys_addr_t pa_sg; 601 size_t sg_len; 602 int prot = uaudio_iommu_map_prot(dma_coherent); 603 int ret; 604 int i; 605 606 prot = IOMMU_READ | IOMMU_WRITE; 607 608 if (dma_coherent) 609 prot |= IOMMU_CACHE; 610 611 iova = uaudio_get_iova(&uaudio_qdev->curr_xfer_buf_iova, 612 &uaudio_qdev->xfer_buf_iova_size, 613 &uaudio_qdev->xfer_buf_list, size); 614 if (!iova) 615 goto done; 616 617 iova_sg = iova; 618 for_each_sg(sgt->sgl, sg, sgt->nents, i) { 619 sg_len = PAGE_ALIGN(sg->offset + sg->length); 620 pa_sg = page_to_phys(sg_page(sg)); 621 ret = iommu_map(uaudio_qdev->data->domain, iova_sg, pa_sg, sg_len, 622 prot, GFP_KERNEL); 623 if (ret) { 624 uaudio_iommu_unmap(MEM_XFER_BUF, iova, size, total_len); 625 iova = 0; 626 goto done; 627 } 628 629 iova_sg += sg_len; 630 total_len += sg_len; 631 } 632 633 if (size != total_len) { 634 uaudio_iommu_unmap(MEM_XFER_BUF, iova, size, total_len); 635 iova = 0; 636 } 637 done: 638 return iova; 639 } 640 641 /* looks up alias, if any, for controller DT node and returns the index */ 642 static int usb_get_controller_id(struct usb_device *udev) 643 { 644 if (udev->bus->sysdev && udev->bus->sysdev->of_node) 645 return of_alias_get_id(udev->bus->sysdev->of_node, "usb"); 646 647 return -ENODEV; 648 } 649 650 /** 651 * uaudio_dev_intf_cleanup() - cleanup transfer resources 652 * @udev: usb device 653 * @info: usb offloading interface 654 * 655 * Cleans up the transfer ring related resources which are assigned per 656 * endpoint from XHCI. This is invoked when the USB endpoints are no 657 * longer in use by the adsp. 658 * 659 */ 660 static void uaudio_dev_intf_cleanup(struct usb_device *udev, struct intf_info *info) 661 { 662 uaudio_iommu_unmap(MEM_XFER_RING, info->data_xfer_ring_va, 663 info->data_xfer_ring_size, info->data_xfer_ring_size); 664 info->data_xfer_ring_va = 0; 665 info->data_xfer_ring_size = 0; 666 667 uaudio_iommu_unmap(MEM_XFER_RING, info->sync_xfer_ring_va, 668 info->sync_xfer_ring_size, info->sync_xfer_ring_size); 669 info->sync_xfer_ring_va = 0; 670 info->sync_xfer_ring_size = 0; 671 672 uaudio_iommu_unmap(MEM_XFER_BUF, info->xfer_buf_iova, info->xfer_buf_size, 673 info->xfer_buf_size); 674 info->xfer_buf_iova = 0; 675 676 usb_free_coherent(udev, info->xfer_buf_size, info->xfer_buf_cpu, 677 info->xfer_buf_dma); 678 info->xfer_buf_size = 0; 679 info->xfer_buf_cpu = NULL; 680 info->xfer_buf_dma = 0; 681 682 info->in_use = false; 683 } 684 685 /** 686 * uaudio_event_ring_cleanup_free() - cleanup secondary event ring 687 * @dev: usb offload device 688 * 689 * Cleans up the secondary event ring that was requested. This will 690 * occur when the adsp is no longer transferring data on the USB bus 691 * across all endpoints. 692 * 693 */ 694 static void uaudio_event_ring_cleanup_free(struct uaudio_dev *dev) 695 { 696 clear_bit(dev->chip->card->number, &uaudio_qdev->card_slot); 697 /* all audio devices are disconnected */ 698 if (!uaudio_qdev->card_slot) { 699 uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, 700 PAGE_SIZE); 701 xhci_sideband_remove_interrupter(uadev[dev->chip->card->number].sb); 702 } 703 } 704 705 static void uaudio_dev_cleanup(struct uaudio_dev *dev) 706 { 707 int if_idx; 708 709 if (!dev->udev) 710 return; 711 712 /* free xfer buffer and unmap xfer ring and buf per interface */ 713 for (if_idx = 0; if_idx < dev->num_intf; if_idx++) { 714 if (!dev->info[if_idx].in_use) 715 continue; 716 uaudio_dev_intf_cleanup(dev->udev, &dev->info[if_idx]); 717 dev_dbg(uaudio_qdev->data->dev, 718 "release resources: intf# %d card# %d\n", 719 dev->info[if_idx].intf_num, dev->chip->card->number); 720 } 721 722 dev->num_intf = 0; 723 724 /* free interface info */ 725 kfree(dev->info); 726 dev->info = NULL; 727 uaudio_event_ring_cleanup_free(dev); 728 dev->udev = NULL; 729 } 730 731 /** 732 * disable_audio_stream() - disable usb snd endpoints 733 * @subs: usb substream 734 * 735 * Closes the USB SND endpoints associated with the current audio stream 736 * used. This will decrement the USB SND endpoint opened reference count. 737 * 738 */ 739 static void disable_audio_stream(struct snd_usb_substream *subs) 740 { 741 struct snd_usb_audio *chip = subs->stream->chip; 742 743 snd_usb_hw_free(subs); 744 snd_usb_autosuspend(chip); 745 } 746 747 /* QMI service disconnect handlers */ 748 static void qmi_stop_session(void) 749 { 750 struct snd_usb_substream *subs; 751 struct usb_host_endpoint *ep; 752 struct snd_usb_audio *chip; 753 struct intf_info *info; 754 int pcm_card_num; 755 int if_idx; 756 int idx; 757 758 guard(mutex)(&qdev_mutex); 759 /* find all active intf for set alt 0 and cleanup usb audio dev */ 760 for (idx = 0; idx < SNDRV_CARDS; idx++) { 761 if (!atomic_read(&uadev[idx].in_use)) 762 continue; 763 764 chip = uadev[idx].chip; 765 for (if_idx = 0; if_idx < uadev[idx].num_intf; if_idx++) { 766 if (!uadev[idx].info || !uadev[idx].info[if_idx].in_use) 767 continue; 768 info = &uadev[idx].info[if_idx]; 769 pcm_card_num = info->pcm_card_num; 770 subs = find_substream(pcm_card_num, info->pcm_dev_num, 771 info->direction); 772 if (!subs || !chip || atomic_read(&chip->shutdown)) { 773 dev_err(&uadev[idx].udev->dev, 774 "no sub for c#%u dev#%u dir%u\n", 775 info->pcm_card_num, 776 info->pcm_dev_num, 777 info->direction); 778 continue; 779 } 780 /* Release XHCI endpoints */ 781 if (info->data_ep_pipe) 782 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 783 info->data_ep_pipe); 784 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, ep); 785 786 if (info->sync_ep_pipe) 787 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 788 info->sync_ep_pipe); 789 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, ep); 790 791 disable_audio_stream(subs); 792 } 793 atomic_set(&uadev[idx].in_use, 0); 794 guard(mutex)(&chip->mutex); 795 uaudio_dev_cleanup(&uadev[idx]); 796 } 797 } 798 799 /** 800 * uaudio_sideband_notifier() - xHCI sideband event handler 801 * @intf: USB interface handle 802 * @evt: xHCI sideband event type 803 * 804 * This callback is executed when the xHCI sideband encounters a sequence 805 * that requires the sideband clients to take action. An example, is when 806 * xHCI frees the transfer ring, so the client has to ensure that the 807 * offload path is halted. 808 * 809 */ 810 static int uaudio_sideband_notifier(struct usb_interface *intf, 811 struct xhci_sideband_event *evt) 812 { 813 struct snd_usb_audio *chip; 814 struct uaudio_dev *dev; 815 int if_idx; 816 817 if (!intf || !evt) 818 return 0; 819 820 chip = usb_get_intfdata(intf); 821 822 guard(mutex)(&qdev_mutex); 823 guard(mutex)(&chip->mutex); 824 825 dev = &uadev[chip->card->number]; 826 827 if (evt->type == XHCI_SIDEBAND_XFER_RING_FREE) { 828 unsigned int *ep = (unsigned int *) evt->evt_data; 829 830 for (if_idx = 0; if_idx < dev->num_intf; if_idx++) { 831 if (dev->info[if_idx].data_ep_idx == *ep || 832 dev->info[if_idx].sync_ep_idx == *ep) 833 uaudio_send_disconnect_ind(chip); 834 } 835 } 836 837 return 0; 838 } 839 840 /** 841 * qmi_bye_cb() - qmi bye message callback 842 * @handle: QMI handle 843 * @node: id of the dying node 844 * 845 * This callback is invoked when the QMI bye control message is received 846 * from the QMI client. Handle the message accordingly by ensuring that 847 * the USB offload path is disabled and cleaned up. At this point, ADSP 848 * is not utilizing the USB bus. 849 * 850 */ 851 static void qmi_bye_cb(struct qmi_handle *handle, unsigned int node) 852 { 853 struct uaudio_qmi_svc *svc = uaudio_svc; 854 855 if (svc->uaudio_svc_hdl != handle) 856 return; 857 858 if (svc->client_connected && svc->client_sq.sq_node == node) { 859 qmi_stop_session(); 860 861 /* clear QMI client parameters to block further QMI messages */ 862 svc->client_sq.sq_node = 0; 863 svc->client_sq.sq_port = 0; 864 svc->client_sq.sq_family = 0; 865 svc->client_connected = false; 866 } 867 } 868 869 /** 870 * qmi_svc_disconnect_cb() - qmi client disconnected 871 * @handle: QMI handle 872 * @node: id of the dying node 873 * @port: port of the dying client 874 * 875 * Invoked when the remote QMI client is disconnected. Handle this event 876 * the same way as when the QMI bye message is received. This will ensure 877 * the USB offloading path is disabled and cleaned up. 878 * 879 */ 880 static void qmi_svc_disconnect_cb(struct qmi_handle *handle, 881 unsigned int node, unsigned int port) 882 { 883 struct uaudio_qmi_svc *svc; 884 885 if (!uaudio_svc) 886 return; 887 888 svc = uaudio_svc; 889 if (svc->uaudio_svc_hdl != handle) 890 return; 891 892 if (svc->client_connected && svc->client_sq.sq_node == node && 893 svc->client_sq.sq_port == port) { 894 qmi_stop_session(); 895 896 /* clear QMI client parameters to block further QMI messages */ 897 svc->client_sq.sq_node = 0; 898 svc->client_sq.sq_port = 0; 899 svc->client_sq.sq_family = 0; 900 svc->client_connected = false; 901 } 902 } 903 904 /* QMI client callback handlers from QMI interface */ 905 static struct qmi_ops uaudio_svc_ops_options = { 906 .bye = qmi_bye_cb, 907 .del_client = qmi_svc_disconnect_cb, 908 }; 909 910 /* kref release callback when all streams are disabled */ 911 static void uaudio_dev_release(struct kref *kref) 912 { 913 struct uaudio_dev *dev = container_of(kref, struct uaudio_dev, kref); 914 915 uaudio_event_ring_cleanup_free(dev); 916 atomic_set(&dev->in_use, 0); 917 wake_up(&dev->disconnect_wq); 918 } 919 920 /** 921 * enable_audio_stream() - enable usb snd endpoints 922 * @subs: usb substream 923 * @pcm_format: pcm format requested 924 * @channels: number of channels 925 * @cur_rate: sample rate 926 * @datainterval: interval 927 * 928 * Opens all USB SND endpoints used for the data interface. This will increment 929 * the USB SND endpoint's opened count. Requests to keep the interface resumed 930 * until the audio stream is stopped. Will issue the USB set interface control 931 * message to enable the data interface. 932 * 933 */ 934 static int enable_audio_stream(struct snd_usb_substream *subs, 935 snd_pcm_format_t pcm_format, 936 unsigned int channels, unsigned int cur_rate, 937 int datainterval) 938 { 939 struct snd_pcm_hw_params params; 940 struct snd_usb_audio *chip; 941 struct snd_interval *i; 942 struct snd_mask *m; 943 int ret; 944 945 chip = subs->stream->chip; 946 947 _snd_pcm_hw_params_any(¶ms); 948 949 m = hw_param_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT); 950 snd_mask_leave(m, pcm_format); 951 952 i = hw_param_interval(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS); 953 snd_interval_setinteger(i); 954 i->min = channels; 955 i->max = channels; 956 957 i = hw_param_interval(¶ms, SNDRV_PCM_HW_PARAM_RATE); 958 snd_interval_setinteger(i); 959 i->min = cur_rate; 960 i->max = cur_rate; 961 962 pm_runtime_barrier(&chip->intf[0]->dev); 963 snd_usb_autoresume(chip); 964 965 ret = snd_usb_hw_params(subs, ¶ms); 966 if (ret < 0) 967 goto put_suspend; 968 969 if (!atomic_read(&chip->shutdown)) { 970 CLASS(snd_usb_lock, pm)(chip); 971 if (pm.err < 0) { 972 ret = pm.err; 973 goto detach_ep; 974 } 975 976 if (subs->sync_endpoint) { 977 ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint); 978 if (ret < 0) 979 goto detach_ep; 980 } 981 982 ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); 983 if (ret < 0) 984 goto detach_ep; 985 986 dev_dbg(uaudio_qdev->data->dev, 987 "selected %s iface:%d altsetting:%d datainterval:%dus\n", 988 subs->direction ? "capture" : "playback", 989 subs->cur_audiofmt->iface, subs->cur_audiofmt->altsetting, 990 (1 << subs->cur_audiofmt->datainterval) * 991 (subs->dev->speed >= USB_SPEED_HIGH ? 992 BUS_INTERVAL_HIGHSPEED_AND_ABOVE : 993 BUS_INTERVAL_FULL_SPEED)); 994 } 995 996 return 0; 997 998 detach_ep: 999 snd_usb_hw_free(subs); 1000 1001 put_suspend: 1002 snd_usb_autosuspend(chip); 1003 1004 return ret; 1005 } 1006 1007 /** 1008 * uaudio_transfer_buffer_setup() - fetch and populate xfer buffer params 1009 * @subs: usb substream 1010 * @xfer_buf: xfer buf to be allocated 1011 * @xfer_buf_len: size of allocation 1012 * @mem_info: QMI response info 1013 * 1014 * Allocates and maps the transfer buffers that will be utilized by the 1015 * audio DSP. Will populate the information in the QMI response that is 1016 * sent back to the stream enable request. 1017 * 1018 */ 1019 static int uaudio_transfer_buffer_setup(struct snd_usb_substream *subs, 1020 void **xfer_buf_cpu, u32 xfer_buf_len, 1021 struct mem_info_v01 *mem_info) 1022 { 1023 struct sg_table xfer_buf_sgt; 1024 dma_addr_t xfer_buf_dma; 1025 void *xfer_buf; 1026 u32 len = xfer_buf_len; 1027 bool dma_coherent; 1028 dma_addr_t xfer_buf_dma_sysdev; 1029 u32 remainder; 1030 u32 mult; 1031 int ret; 1032 1033 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1034 1035 /* xfer buffer, multiple of 4K only */ 1036 if (!len) 1037 len = PAGE_SIZE; 1038 1039 mult = len / PAGE_SIZE; 1040 remainder = len % PAGE_SIZE; 1041 len = mult * PAGE_SIZE; 1042 len += remainder ? PAGE_SIZE : 0; 1043 1044 if (len > MAX_XFER_BUFF_LEN) { 1045 dev_err(uaudio_qdev->data->dev, 1046 "req buf len %d > max buf len %lu, setting %lu\n", 1047 len, MAX_XFER_BUFF_LEN, MAX_XFER_BUFF_LEN); 1048 len = MAX_XFER_BUFF_LEN; 1049 } 1050 1051 /* get buffer mapped into subs->dev */ 1052 xfer_buf = usb_alloc_coherent(subs->dev, len, GFP_KERNEL, &xfer_buf_dma); 1053 if (!xfer_buf) 1054 return -ENOMEM; 1055 1056 dma_get_sgtable(subs->dev->bus->sysdev, &xfer_buf_sgt, xfer_buf, 1057 xfer_buf_dma, len); 1058 1059 /* map the physical buffer into sysdev as well */ 1060 xfer_buf_dma_sysdev = uaudio_iommu_map_xfer_buf(dma_coherent, 1061 len, &xfer_buf_sgt); 1062 if (!xfer_buf_dma_sysdev) { 1063 ret = -ENOMEM; 1064 goto unmap_sync; 1065 } 1066 1067 mem_info->dma = xfer_buf_dma; 1068 mem_info->size = len; 1069 mem_info->iova = PREPEND_SID_TO_IOVA(xfer_buf_dma_sysdev, uaudio_qdev->data->sid); 1070 *xfer_buf_cpu = xfer_buf; 1071 sg_free_table(&xfer_buf_sgt); 1072 1073 return 0; 1074 1075 unmap_sync: 1076 usb_free_coherent(subs->dev, len, xfer_buf, xfer_buf_dma); 1077 1078 return ret; 1079 } 1080 1081 /** 1082 * uaudio_endpoint_setup() - fetch and populate endpoint params 1083 * @subs: usb substream 1084 * @endpoint: usb endpoint to add 1085 * @card_num: uadev index 1086 * @mem_info: QMI response info 1087 * @ep_desc: QMI ep desc response field 1088 * 1089 * Initialize the USB endpoint being used for a particular USB 1090 * stream. Will request XHCI sec intr to reserve the EP for 1091 * offloading as well as populating the QMI response with the 1092 * transfer ring parameters. 1093 * 1094 */ 1095 static phys_addr_t 1096 uaudio_endpoint_setup(struct snd_usb_substream *subs, 1097 struct snd_usb_endpoint *endpoint, int card_num, 1098 struct mem_info_v01 *mem_info, 1099 struct usb_endpoint_descriptor_v01 *ep_desc) 1100 { 1101 struct usb_host_endpoint *ep; 1102 phys_addr_t tr_pa = 0; 1103 struct sg_table *sgt; 1104 bool dma_coherent; 1105 unsigned long iova; 1106 struct page *pg; 1107 int ret = -ENODEV; 1108 1109 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1110 1111 ep = usb_pipe_endpoint(subs->dev, endpoint->pipe); 1112 if (!ep) { 1113 dev_err(uaudio_qdev->data->dev, "data ep # %d context is null\n", 1114 subs->data_endpoint->ep_num); 1115 goto exit; 1116 } 1117 1118 memcpy(ep_desc, &ep->desc, sizeof(ep->desc)); 1119 1120 ret = xhci_sideband_add_endpoint(uadev[card_num].sb, ep); 1121 if (ret < 0) { 1122 dev_err(&subs->dev->dev, 1123 "failed to add data ep to sec intr\n"); 1124 ret = -ENODEV; 1125 goto exit; 1126 } 1127 1128 sgt = xhci_sideband_get_endpoint_buffer(uadev[card_num].sb, ep); 1129 if (!sgt) { 1130 dev_err(&subs->dev->dev, 1131 "failed to get data ep ring address\n"); 1132 ret = -ENODEV; 1133 goto remove_ep; 1134 } 1135 1136 pg = sg_page(sgt->sgl); 1137 tr_pa = page_to_phys(pg); 1138 mem_info->dma = sg_dma_address(sgt->sgl); 1139 sg_free_table(sgt); 1140 1141 /* data transfer ring */ 1142 iova = uaudio_iommu_map_pa(MEM_XFER_RING, dma_coherent, tr_pa, 1143 PAGE_SIZE); 1144 if (!iova) { 1145 ret = -ENOMEM; 1146 goto clear_pa; 1147 } 1148 1149 mem_info->iova = PREPEND_SID_TO_IOVA(iova, uaudio_qdev->data->sid); 1150 mem_info->size = PAGE_SIZE; 1151 1152 return 0; 1153 1154 clear_pa: 1155 mem_info->dma = 0; 1156 remove_ep: 1157 xhci_sideband_remove_endpoint(uadev[card_num].sb, ep); 1158 exit: 1159 return ret; 1160 } 1161 1162 /** 1163 * uaudio_event_ring_setup() - fetch and populate event ring params 1164 * @subs: usb substream 1165 * @card_num: uadev index 1166 * @mem_info: QMI response info 1167 * 1168 * Register secondary interrupter to XHCI and fetch the event buffer info 1169 * and populate the information into the QMI response. 1170 * 1171 */ 1172 static int uaudio_event_ring_setup(struct snd_usb_substream *subs, 1173 int card_num, struct mem_info_v01 *mem_info) 1174 { 1175 struct sg_table *sgt; 1176 phys_addr_t er_pa; 1177 bool dma_coherent; 1178 unsigned long iova; 1179 struct page *pg; 1180 int ret; 1181 1182 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1183 er_pa = 0; 1184 1185 /* event ring */ 1186 ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false, 1187 0, uaudio_qdev->data->intr_num); 1188 if (ret < 0) { 1189 dev_err(&subs->dev->dev, "failed to fetch interrupter\n"); 1190 goto exit; 1191 } 1192 1193 sgt = xhci_sideband_get_event_buffer(uadev[card_num].sb); 1194 if (!sgt) { 1195 dev_err(&subs->dev->dev, 1196 "failed to get event ring address\n"); 1197 ret = -ENODEV; 1198 goto remove_interrupter; 1199 } 1200 1201 pg = sg_page(sgt->sgl); 1202 er_pa = page_to_phys(pg); 1203 mem_info->dma = sg_dma_address(sgt->sgl); 1204 sg_free_table(sgt); 1205 1206 iova = uaudio_iommu_map_pa(MEM_EVENT_RING, dma_coherent, er_pa, 1207 PAGE_SIZE); 1208 if (!iova) { 1209 ret = -ENOMEM; 1210 goto clear_pa; 1211 } 1212 1213 mem_info->iova = PREPEND_SID_TO_IOVA(iova, uaudio_qdev->data->sid); 1214 mem_info->size = PAGE_SIZE; 1215 1216 return 0; 1217 1218 clear_pa: 1219 mem_info->dma = 0; 1220 remove_interrupter: 1221 xhci_sideband_remove_interrupter(uadev[card_num].sb); 1222 exit: 1223 return ret; 1224 } 1225 1226 /** 1227 * uaudio_populate_uac_desc() - parse UAC parameters and populate QMI resp 1228 * @subs: usb substream 1229 * @resp: QMI response buffer 1230 * 1231 * Parses information specified within UAC descriptors which explain the 1232 * sample parameters that the device expects. This information is populated 1233 * to the QMI response sent back to the audio DSP. 1234 * 1235 */ 1236 static int uaudio_populate_uac_desc(struct snd_usb_substream *subs, 1237 struct qmi_uaudio_stream_resp_msg_v01 *resp) 1238 { 1239 struct usb_interface_descriptor *altsd; 1240 struct usb_host_interface *alts; 1241 struct usb_interface *iface; 1242 int protocol; 1243 1244 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); 1245 if (!iface) { 1246 dev_err(&subs->dev->dev, "interface # %d does not exist\n", 1247 subs->cur_audiofmt->iface); 1248 return -ENODEV; 1249 } 1250 1251 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; 1252 altsd = get_iface_desc(alts); 1253 protocol = altsd->bInterfaceProtocol; 1254 1255 if (protocol == UAC_VERSION_1) { 1256 struct uac1_as_header_descriptor *as; 1257 1258 as = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, 1259 UAC_AS_GENERAL); 1260 if (!as) { 1261 dev_err(&subs->dev->dev, 1262 "%u:%d : no UAC_AS_GENERAL desc\n", 1263 subs->cur_audiofmt->iface, 1264 subs->cur_audiofmt->altset_idx); 1265 return -ENODEV; 1266 } 1267 1268 resp->data_path_delay = as->bDelay; 1269 resp->data_path_delay_valid = 1; 1270 1271 resp->usb_audio_subslot_size = subs->cur_audiofmt->fmt_sz; 1272 resp->usb_audio_subslot_size_valid = 1; 1273 1274 resp->usb_audio_spec_revision = le16_to_cpu((__force __le16)0x0100); 1275 resp->usb_audio_spec_revision_valid = 1; 1276 } else if (protocol == UAC_VERSION_2) { 1277 resp->usb_audio_subslot_size = subs->cur_audiofmt->fmt_sz; 1278 resp->usb_audio_subslot_size_valid = 1; 1279 1280 resp->usb_audio_spec_revision = le16_to_cpu((__force __le16)0x0200); 1281 resp->usb_audio_spec_revision_valid = 1; 1282 } else if (protocol == UAC_VERSION_3) { 1283 if (iface->intf_assoc->bFunctionSubClass == 1284 UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0) { 1285 dev_err(&subs->dev->dev, 1286 "full adc is not supported\n"); 1287 return -EINVAL; 1288 } 1289 1290 switch (le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize)) { 1291 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: 1292 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: 1293 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: 1294 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: { 1295 resp->usb_audio_subslot_size = 0x2; 1296 break; 1297 } 1298 1299 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: 1300 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: 1301 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: 1302 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: { 1303 resp->usb_audio_subslot_size = 0x3; 1304 break; 1305 } 1306 1307 default: 1308 dev_err(&subs->dev->dev, 1309 "%d: %u: Invalid wMaxPacketSize\n", 1310 subs->cur_audiofmt->iface, 1311 subs->cur_audiofmt->altset_idx); 1312 return -EINVAL; 1313 } 1314 resp->usb_audio_subslot_size_valid = 1; 1315 } else { 1316 dev_err(&subs->dev->dev, "unknown protocol version %x\n", 1317 protocol); 1318 return -ENODEV; 1319 } 1320 1321 memcpy(&resp->std_as_opr_intf_desc, &alts->desc, sizeof(alts->desc)); 1322 1323 return 0; 1324 } 1325 1326 /** 1327 * prepare_qmi_response() - prepare stream enable response 1328 * @subs: usb substream 1329 * @req_msg: QMI request message 1330 * @resp: QMI response buffer 1331 * @info_idx: usb interface array index 1332 * 1333 * Prepares the QMI response for a USB QMI stream enable request. Will parse 1334 * out the parameters within the stream enable request, in order to match 1335 * requested audio profile to the ones exposed by the USB device connected. 1336 * 1337 * In addition, will fetch the XHCI transfer resources needed for the handoff to 1338 * happen. This includes, transfer ring and buffer addresses and secondary event 1339 * ring address. These parameters will be communicated as part of the USB QMI 1340 * stream enable response. 1341 * 1342 */ 1343 static int prepare_qmi_response(struct snd_usb_substream *subs, 1344 struct qmi_uaudio_stream_req_msg_v01 *req_msg, 1345 struct qmi_uaudio_stream_resp_msg_v01 *resp, 1346 int info_idx) 1347 { 1348 struct q6usb_offload *data; 1349 int pcm_dev_num; 1350 int card_num; 1351 void *xfer_buf_cpu; 1352 int ret; 1353 1354 pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8; 1355 card_num = (req_msg->usb_token & QMI_STREAM_REQ_CARD_NUM_MASK) >> 16; 1356 1357 if (!uadev[card_num].ctrl_intf) { 1358 dev_err(&subs->dev->dev, "audio ctrl intf info not cached\n"); 1359 return -ENODEV; 1360 } 1361 1362 ret = uaudio_populate_uac_desc(subs, resp); 1363 if (ret < 0) 1364 return ret; 1365 1366 resp->slot_id = subs->dev->slot_id; 1367 resp->slot_id_valid = 1; 1368 1369 data = snd_soc_usb_find_priv_data(uaudio_qdev->auxdev->dev.parent); 1370 if (!data) { 1371 dev_err(&subs->dev->dev, "No private data found\n"); 1372 return -ENODEV; 1373 } 1374 1375 uaudio_qdev->data = data; 1376 1377 resp->std_as_opr_intf_desc_valid = 1; 1378 ret = uaudio_endpoint_setup(subs, subs->data_endpoint, card_num, 1379 &resp->xhci_mem_info.tr_data, 1380 &resp->std_as_data_ep_desc); 1381 if (ret < 0) 1382 return ret; 1383 1384 resp->std_as_data_ep_desc_valid = 1; 1385 1386 if (subs->sync_endpoint) { 1387 ret = uaudio_endpoint_setup(subs, subs->sync_endpoint, card_num, 1388 &resp->xhci_mem_info.tr_sync, 1389 &resp->std_as_sync_ep_desc); 1390 if (ret < 0) 1391 goto drop_data_ep; 1392 1393 resp->std_as_sync_ep_desc_valid = 1; 1394 } 1395 1396 resp->interrupter_num_valid = 1; 1397 resp->controller_num_valid = 0; 1398 ret = usb_get_controller_id(subs->dev); 1399 if (ret >= 0) { 1400 resp->controller_num = ret; 1401 resp->controller_num_valid = 1; 1402 } 1403 1404 /* event ring */ 1405 ret = uaudio_event_ring_setup(subs, card_num, 1406 &resp->xhci_mem_info.evt_ring); 1407 if (ret < 0) 1408 goto drop_sync_ep; 1409 1410 uaudio_qdev->er_mapped = true; 1411 resp->interrupter_num = xhci_sideband_interrupter_id(uadev[card_num].sb); 1412 1413 resp->speed_info = get_speed_info(subs->dev->speed); 1414 if (resp->speed_info == USB_QMI_DEVICE_SPEED_INVALID_V01) { 1415 ret = -ENODEV; 1416 goto free_sec_ring; 1417 } 1418 1419 resp->speed_info_valid = 1; 1420 1421 ret = uaudio_transfer_buffer_setup(subs, &xfer_buf_cpu, req_msg->xfer_buff_size, 1422 &resp->xhci_mem_info.xfer_buff); 1423 if (ret < 0) { 1424 ret = -ENOMEM; 1425 goto free_sec_ring; 1426 } 1427 1428 resp->xhci_mem_info_valid = 1; 1429 1430 if (!atomic_read(&uadev[card_num].in_use)) { 1431 kref_init(&uadev[card_num].kref); 1432 init_waitqueue_head(&uadev[card_num].disconnect_wq); 1433 uadev[card_num].num_intf = 1434 subs->dev->config->desc.bNumInterfaces; 1435 uadev[card_num].info = kcalloc(uadev[card_num].num_intf, 1436 sizeof(struct intf_info), 1437 GFP_KERNEL); 1438 if (!uadev[card_num].info) { 1439 ret = -ENOMEM; 1440 goto unmap_er; 1441 } 1442 uadev[card_num].udev = subs->dev; 1443 atomic_set(&uadev[card_num].in_use, 1); 1444 } else { 1445 kref_get(&uadev[card_num].kref); 1446 } 1447 1448 uadev[card_num].usb_core_id = resp->controller_num; 1449 1450 /* cache intf specific info to use it for unmap and free xfer buf */ 1451 uadev[card_num].info[info_idx].data_xfer_ring_va = 1452 IOVA_MASK(resp->xhci_mem_info.tr_data.iova); 1453 uadev[card_num].info[info_idx].data_xfer_ring_size = PAGE_SIZE; 1454 uadev[card_num].info[info_idx].sync_xfer_ring_va = 1455 IOVA_MASK(resp->xhci_mem_info.tr_sync.iova); 1456 uadev[card_num].info[info_idx].sync_xfer_ring_size = PAGE_SIZE; 1457 uadev[card_num].info[info_idx].xfer_buf_iova = 1458 IOVA_MASK(resp->xhci_mem_info.xfer_buff.iova); 1459 uadev[card_num].info[info_idx].xfer_buf_dma = 1460 resp->xhci_mem_info.xfer_buff.dma; 1461 uadev[card_num].info[info_idx].xfer_buf_size = 1462 resp->xhci_mem_info.xfer_buff.size; 1463 uadev[card_num].info[info_idx].data_ep_pipe = subs->data_endpoint ? 1464 subs->data_endpoint->pipe : 0; 1465 uadev[card_num].info[info_idx].sync_ep_pipe = subs->sync_endpoint ? 1466 subs->sync_endpoint->pipe : 0; 1467 uadev[card_num].info[info_idx].data_ep_idx = subs->data_endpoint ? 1468 subs->data_endpoint->ep_num : 0; 1469 uadev[card_num].info[info_idx].sync_ep_idx = subs->sync_endpoint ? 1470 subs->sync_endpoint->ep_num : 0; 1471 uadev[card_num].info[info_idx].xfer_buf_cpu = xfer_buf_cpu; 1472 uadev[card_num].info[info_idx].pcm_card_num = card_num; 1473 uadev[card_num].info[info_idx].pcm_dev_num = pcm_dev_num; 1474 uadev[card_num].info[info_idx].direction = subs->direction; 1475 uadev[card_num].info[info_idx].intf_num = subs->cur_audiofmt->iface; 1476 uadev[card_num].info[info_idx].in_use = true; 1477 1478 set_bit(card_num, &uaudio_qdev->card_slot); 1479 1480 return 0; 1481 1482 unmap_er: 1483 uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE); 1484 free_sec_ring: 1485 xhci_sideband_remove_interrupter(uadev[card_num].sb); 1486 drop_sync_ep: 1487 if (subs->sync_endpoint) { 1488 uaudio_iommu_unmap(MEM_XFER_RING, 1489 IOVA_MASK(resp->xhci_mem_info.tr_sync.iova), 1490 PAGE_SIZE, PAGE_SIZE); 1491 xhci_sideband_remove_endpoint(uadev[card_num].sb, 1492 usb_pipe_endpoint(subs->dev, subs->sync_endpoint->pipe)); 1493 } 1494 drop_data_ep: 1495 uaudio_iommu_unmap(MEM_XFER_RING, IOVA_MASK(resp->xhci_mem_info.tr_data.iova), 1496 PAGE_SIZE, PAGE_SIZE); 1497 xhci_sideband_remove_endpoint(uadev[card_num].sb, 1498 usb_pipe_endpoint(subs->dev, subs->data_endpoint->pipe)); 1499 1500 return ret; 1501 } 1502 1503 /** 1504 * handle_uaudio_stream_req() - handle stream enable/disable request 1505 * @handle: QMI client handle 1506 * @sq: qrtr socket 1507 * @txn: QMI transaction context 1508 * @decoded_msg: decoded QMI message 1509 * 1510 * Main handler for the QMI stream enable/disable requests. This executes the 1511 * corresponding enable/disable stream apis, respectively. 1512 * 1513 */ 1514 static void handle_uaudio_stream_req(struct qmi_handle *handle, 1515 struct sockaddr_qrtr *sq, 1516 struct qmi_txn *txn, 1517 const void *decoded_msg) 1518 { 1519 struct qmi_uaudio_stream_req_msg_v01 *req_msg; 1520 struct qmi_uaudio_stream_resp_msg_v01 resp = {{0}, 0}; 1521 struct uaudio_qmi_svc *svc = uaudio_svc; 1522 struct snd_usb_audio *chip = NULL; 1523 struct snd_usb_substream *subs; 1524 struct usb_host_endpoint *ep; 1525 int datainterval = -EINVAL; 1526 int info_idx = -EINVAL; 1527 struct intf_info *info; 1528 u8 pcm_card_num; 1529 u8 pcm_dev_num; 1530 u8 direction; 1531 int ret = 0; 1532 1533 if (!svc->client_connected) { 1534 svc->client_sq = *sq; 1535 svc->client_connected = true; 1536 } 1537 1538 mutex_lock(&qdev_mutex); 1539 req_msg = (struct qmi_uaudio_stream_req_msg_v01 *)decoded_msg; 1540 if (!req_msg->audio_format_valid || !req_msg->bit_rate_valid || 1541 !req_msg->number_of_ch_valid || !req_msg->xfer_buff_size_valid) { 1542 ret = -EINVAL; 1543 goto response; 1544 } 1545 1546 if (!uaudio_qdev) { 1547 ret = -EINVAL; 1548 goto response; 1549 } 1550 1551 direction = (req_msg->usb_token & QMI_STREAM_REQ_DIRECTION); 1552 pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8; 1553 pcm_card_num = (req_msg->usb_token & QMI_STREAM_REQ_CARD_NUM_MASK) >> 16; 1554 if (pcm_card_num >= SNDRV_CARDS) { 1555 ret = -EINVAL; 1556 goto response; 1557 } 1558 1559 if (req_msg->audio_format > USB_QMI_PCM_FORMAT_U32_BE) { 1560 ret = -EINVAL; 1561 goto response; 1562 } 1563 1564 subs = find_substream(pcm_card_num, pcm_dev_num, direction); 1565 chip = uadev[pcm_card_num].chip; 1566 if (!subs || !chip || atomic_read(&chip->shutdown)) { 1567 ret = -ENODEV; 1568 goto response; 1569 } 1570 1571 info_idx = info_idx_from_ifnum(pcm_card_num, subs->cur_audiofmt ? 1572 subs->cur_audiofmt->iface : -1, req_msg->enable); 1573 if (atomic_read(&chip->shutdown) || !subs->stream || !subs->stream->pcm || 1574 !subs->stream->chip) { 1575 ret = -ENODEV; 1576 goto response; 1577 } 1578 1579 scoped_guard(mutex, &chip->mutex) { 1580 if (req_msg->enable) { 1581 if (info_idx < 0 || chip->system_suspend || subs->opened) { 1582 ret = -EBUSY; 1583 goto response; 1584 } 1585 subs->opened = 1; 1586 } 1587 } 1588 1589 if (req_msg->service_interval_valid) { 1590 ret = get_data_interval_from_si(subs, 1591 req_msg->service_interval); 1592 if (ret == -EINVAL) 1593 goto response; 1594 1595 datainterval = ret; 1596 } 1597 1598 uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf; 1599 1600 if (req_msg->enable) { 1601 ret = enable_audio_stream(subs, 1602 map_pcm_format(req_msg->audio_format), 1603 req_msg->number_of_ch, req_msg->bit_rate, 1604 datainterval); 1605 1606 if (!ret) 1607 ret = prepare_qmi_response(subs, req_msg, &resp, 1608 info_idx); 1609 if (ret < 0) { 1610 guard(mutex)(&chip->mutex); 1611 subs->opened = 0; 1612 } 1613 } else { 1614 info = &uadev[pcm_card_num].info[info_idx]; 1615 if (info->data_ep_pipe) { 1616 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 1617 info->data_ep_pipe); 1618 if (ep) { 1619 xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb, 1620 ep); 1621 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, 1622 ep); 1623 } 1624 1625 info->data_ep_pipe = 0; 1626 } 1627 1628 if (info->sync_ep_pipe) { 1629 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 1630 info->sync_ep_pipe); 1631 if (ep) { 1632 xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb, 1633 ep); 1634 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, 1635 ep); 1636 } 1637 1638 info->sync_ep_pipe = 0; 1639 } 1640 1641 disable_audio_stream(subs); 1642 guard(mutex)(&chip->mutex); 1643 subs->opened = 0; 1644 } 1645 1646 response: 1647 if (!req_msg->enable && ret != -EINVAL && ret != -ENODEV) { 1648 guard(mutex)(&chip->mutex); 1649 if (info_idx >= 0) { 1650 info = &uadev[pcm_card_num].info[info_idx]; 1651 uaudio_dev_intf_cleanup(uadev[pcm_card_num].udev, 1652 info); 1653 } 1654 if (atomic_read(&uadev[pcm_card_num].in_use)) 1655 kref_put(&uadev[pcm_card_num].kref, 1656 uaudio_dev_release); 1657 } 1658 mutex_unlock(&qdev_mutex); 1659 1660 resp.usb_token = req_msg->usb_token; 1661 resp.usb_token_valid = 1; 1662 resp.internal_status = ret; 1663 resp.internal_status_valid = 1; 1664 resp.status = ret ? USB_QMI_STREAM_REQ_FAILURE_V01 : ret; 1665 resp.status_valid = 1; 1666 ret = qmi_send_response(svc->uaudio_svc_hdl, sq, txn, 1667 QMI_UAUDIO_STREAM_RESP_V01, 1668 QMI_UAUDIO_STREAM_RESP_MSG_V01_MAX_MSG_LEN, 1669 qmi_uaudio_stream_resp_msg_v01_ei, &resp); 1670 } 1671 1672 static struct qmi_msg_handler uaudio_stream_req_handlers = { 1673 .type = QMI_REQUEST, 1674 .msg_id = QMI_UAUDIO_STREAM_REQ_V01, 1675 .ei = qmi_uaudio_stream_req_msg_v01_ei, 1676 .decoded_size = QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN, 1677 .fn = handle_uaudio_stream_req, 1678 }; 1679 1680 /** 1681 * qc_usb_audio_offload_init_qmi_dev() - initializes qmi dev 1682 * 1683 * Initializes the USB qdev, which is used to carry information pertaining to 1684 * the offloading resources. This device is freed only when there are no longer 1685 * any offloading candidates. (i.e, when all audio devices are disconnected) 1686 * 1687 */ 1688 static int qc_usb_audio_offload_init_qmi_dev(void) 1689 { 1690 uaudio_qdev = kzalloc(sizeof(*uaudio_qdev), GFP_KERNEL); 1691 if (!uaudio_qdev) 1692 return -ENOMEM; 1693 1694 /* initialize xfer ring and xfer buf iova list */ 1695 INIT_LIST_HEAD(&uaudio_qdev->xfer_ring_list); 1696 uaudio_qdev->curr_xfer_ring_iova = IOVA_XFER_RING_BASE; 1697 uaudio_qdev->xfer_ring_iova_size = 1698 IOVA_XFER_RING_MAX - IOVA_XFER_RING_BASE; 1699 1700 INIT_LIST_HEAD(&uaudio_qdev->xfer_buf_list); 1701 uaudio_qdev->curr_xfer_buf_iova = IOVA_XFER_BUF_BASE; 1702 uaudio_qdev->xfer_buf_iova_size = 1703 IOVA_XFER_BUF_MAX - IOVA_XFER_BUF_BASE; 1704 1705 return 0; 1706 } 1707 1708 /* Populates ppcm_idx array with supported PCM indexes */ 1709 static int qc_usb_audio_offload_fill_avail_pcms(struct snd_usb_audio *chip, 1710 struct snd_soc_usb_device *sdev) 1711 { 1712 struct snd_usb_stream *as; 1713 struct snd_usb_substream *subs; 1714 int idx = 0; 1715 1716 list_for_each_entry(as, &chip->pcm_list, list) { 1717 subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK]; 1718 if (subs->ep_num) { 1719 sdev->ppcm_idx[idx] = as->pcm->device; 1720 idx++; 1721 } 1722 /* 1723 * Break if the current index exceeds the number of possible 1724 * playback streams counted from the UAC descriptors. 1725 */ 1726 if (idx >= sdev->num_playback) 1727 break; 1728 } 1729 1730 return -1; 1731 } 1732 1733 /** 1734 * qc_usb_audio_offload_probe() - platform op connect handler 1735 * @chip: USB SND device 1736 * 1737 * Platform connect handler when a USB SND device is detected. Will 1738 * notify SOC USB about the connection to enable the USB ASoC backend 1739 * and populate internal USB chip array. 1740 * 1741 */ 1742 static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip) 1743 { 1744 struct usb_interface *intf = chip->intf[chip->num_interfaces - 1]; 1745 struct usb_interface_descriptor *altsd; 1746 struct usb_host_interface *alts; 1747 struct snd_soc_usb_device *sdev; 1748 struct xhci_sideband *sb; 1749 1750 /* 1751 * If there is no priv_data, or no playback paths, the connected 1752 * device doesn't support offloading. Avoid populating entries for 1753 * this device. 1754 */ 1755 if (!snd_soc_usb_find_priv_data(uaudio_qdev->auxdev->dev.parent) || 1756 !usb_qmi_get_pcm_num(chip, 0)) 1757 return; 1758 1759 guard(mutex)(&qdev_mutex); 1760 guard(mutex)(&chip->mutex); 1761 if (!uadev[chip->card->number].chip) { 1762 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 1763 if (!sdev) 1764 return; 1765 1766 sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR, 1767 uaudio_sideband_notifier); 1768 if (!sb) 1769 goto free_sdev; 1770 } else { 1771 sb = uadev[chip->card->number].sb; 1772 sdev = uadev[chip->card->number].sdev; 1773 } 1774 1775 uadev[chip->card->number].sb = sb; 1776 uadev[chip->card->number].chip = chip; 1777 uadev[chip->card->number].sdev = sdev; 1778 1779 alts = &intf->altsetting[0]; 1780 altsd = get_iface_desc(alts); 1781 1782 /* Wait until all PCM devices are populated before notifying soc-usb */ 1783 if (altsd->bInterfaceNumber == chip->last_iface) { 1784 sdev->num_playback = usb_qmi_get_pcm_num(chip, 0); 1785 1786 /* 1787 * Allocate playback pcm index array based on number of possible 1788 * playback paths within the UAC descriptors. 1789 */ 1790 sdev->ppcm_idx = kcalloc(sdev->num_playback, sizeof(unsigned int), 1791 GFP_KERNEL); 1792 if (!sdev->ppcm_idx) 1793 goto unreg_xhci; 1794 1795 qc_usb_audio_offload_fill_avail_pcms(chip, sdev); 1796 sdev->card_idx = chip->card->number; 1797 sdev->chip_idx = chip->index; 1798 1799 snd_usb_offload_create_ctl(chip, uaudio_qdev->auxdev->dev.parent); 1800 snd_soc_usb_connect(uaudio_qdev->auxdev->dev.parent, sdev); 1801 } 1802 1803 return; 1804 1805 unreg_xhci: 1806 xhci_sideband_unregister(sb); 1807 uadev[chip->card->number].sb = NULL; 1808 free_sdev: 1809 kfree(sdev); 1810 uadev[chip->card->number].sdev = NULL; 1811 uadev[chip->card->number].chip = NULL; 1812 } 1813 1814 /** 1815 * qc_usb_audio_cleanup_qmi_dev() - release qmi device 1816 * 1817 * Frees the USB qdev. Only occurs when there are no longer any potential 1818 * devices that can utilize USB audio offloading. 1819 * 1820 */ 1821 static void qc_usb_audio_cleanup_qmi_dev(void) 1822 { 1823 kfree(uaudio_qdev); 1824 uaudio_qdev = NULL; 1825 } 1826 1827 /** 1828 * qc_usb_audio_offload_disconnect() - platform op disconnect handler 1829 * @chip: USB SND device 1830 * 1831 * Platform disconnect handler. Will ensure that any pending stream is 1832 * halted by issuing a QMI disconnect indication packet to the adsp. 1833 * 1834 */ 1835 static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip) 1836 { 1837 struct uaudio_dev *dev; 1838 int card_num; 1839 1840 if (!chip) 1841 return; 1842 1843 card_num = chip->card->number; 1844 if (card_num >= SNDRV_CARDS) 1845 return; 1846 1847 guard(mutex)(&qdev_mutex); 1848 guard(mutex)(&chip->mutex); 1849 dev = &uadev[card_num]; 1850 1851 /* Device has already been cleaned up, or never populated */ 1852 if (!dev->chip) 1853 return; 1854 1855 /* cleaned up already */ 1856 if (!dev->udev) 1857 goto done; 1858 1859 uaudio_send_disconnect_ind(chip); 1860 uaudio_dev_cleanup(dev); 1861 done: 1862 /* 1863 * If num_interfaces == 1, the last USB SND interface is being removed. 1864 * This is to accommodate for devices w/ multiple UAC functions. 1865 */ 1866 if (chip->num_interfaces == 1) { 1867 snd_soc_usb_disconnect(uaudio_qdev->auxdev->dev.parent, dev->sdev); 1868 xhci_sideband_unregister(dev->sb); 1869 dev->chip = NULL; 1870 kfree(dev->sdev->ppcm_idx); 1871 kfree(dev->sdev); 1872 dev->sdev = NULL; 1873 } 1874 } 1875 1876 /** 1877 * qc_usb_audio_offload_suspend() - USB offload PM suspend handler 1878 * @intf: USB interface 1879 * @message: suspend type 1880 * 1881 * PM suspend handler to ensure that the USB offloading driver is able to stop 1882 * any pending traffic, so that the bus can be suspended. 1883 * 1884 */ 1885 static void qc_usb_audio_offload_suspend(struct usb_interface *intf, 1886 pm_message_t message) 1887 { 1888 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1889 int card_num; 1890 1891 if (!chip) 1892 return; 1893 1894 card_num = chip->card->number; 1895 if (card_num >= SNDRV_CARDS) 1896 return; 1897 1898 guard(mutex)(&qdev_mutex); 1899 guard(mutex)(&chip->mutex); 1900 1901 uaudio_send_disconnect_ind(chip); 1902 } 1903 1904 static struct snd_usb_platform_ops offload_ops = { 1905 .connect_cb = qc_usb_audio_offload_probe, 1906 .disconnect_cb = qc_usb_audio_offload_disconnect, 1907 .suspend_cb = qc_usb_audio_offload_suspend, 1908 }; 1909 1910 static int qc_usb_audio_probe(struct auxiliary_device *auxdev, 1911 const struct auxiliary_device_id *id) 1912 1913 { 1914 struct uaudio_qmi_svc *svc; 1915 int ret; 1916 1917 svc = kzalloc(sizeof(*svc), GFP_KERNEL); 1918 if (!svc) 1919 return -ENOMEM; 1920 1921 svc->uaudio_svc_hdl = kzalloc(sizeof(*svc->uaudio_svc_hdl), GFP_KERNEL); 1922 if (!svc->uaudio_svc_hdl) { 1923 ret = -ENOMEM; 1924 goto free_svc; 1925 } 1926 1927 ret = qmi_handle_init(svc->uaudio_svc_hdl, 1928 QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN, 1929 &uaudio_svc_ops_options, 1930 &uaudio_stream_req_handlers); 1931 ret = qmi_add_server(svc->uaudio_svc_hdl, UAUDIO_STREAM_SERVICE_ID_V01, 1932 UAUDIO_STREAM_SERVICE_VERS_V01, 0); 1933 1934 uaudio_svc = svc; 1935 1936 qc_usb_audio_offload_init_qmi_dev(); 1937 uaudio_qdev->auxdev = auxdev; 1938 1939 ret = snd_usb_register_platform_ops(&offload_ops); 1940 if (ret < 0) 1941 goto release_qmi; 1942 1943 snd_usb_rediscover_devices(); 1944 1945 return 0; 1946 1947 release_qmi: 1948 qc_usb_audio_cleanup_qmi_dev(); 1949 qmi_handle_release(svc->uaudio_svc_hdl); 1950 free_svc: 1951 kfree(svc); 1952 1953 return ret; 1954 } 1955 1956 static void qc_usb_audio_remove(struct auxiliary_device *auxdev) 1957 { 1958 struct uaudio_qmi_svc *svc = uaudio_svc; 1959 int idx; 1960 1961 /* 1962 * Remove all connected devices after unregistering ops, to ensure 1963 * that no further connect events will occur. The disconnect routine 1964 * will issue the QMI disconnect indication, which results in the 1965 * external DSP to stop issuing transfers. 1966 */ 1967 snd_usb_unregister_platform_ops(); 1968 for (idx = 0; idx < SNDRV_CARDS; idx++) 1969 qc_usb_audio_offload_disconnect(uadev[idx].chip); 1970 1971 qc_usb_audio_cleanup_qmi_dev(); 1972 1973 qmi_handle_release(svc->uaudio_svc_hdl); 1974 kfree(svc); 1975 uaudio_svc = NULL; 1976 } 1977 1978 static const struct auxiliary_device_id qc_usb_audio_table[] = { 1979 { .name = "q6usb.qc-usb-audio-offload" }, 1980 {}, 1981 }; 1982 MODULE_DEVICE_TABLE(auxiliary, qc_usb_audio_table); 1983 1984 static struct auxiliary_driver qc_usb_audio_offload_drv = { 1985 .name = "qc-usb-audio-offload", 1986 .id_table = qc_usb_audio_table, 1987 .probe = qc_usb_audio_probe, 1988 .remove = qc_usb_audio_remove, 1989 }; 1990 module_auxiliary_driver(qc_usb_audio_offload_drv); 1991 1992 MODULE_DESCRIPTION("QC USB Audio Offloading"); 1993 MODULE_LICENSE("GPL"); 1994