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 mutex_lock(&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 mutex_lock(&chip->mutex); 795 uaudio_dev_cleanup(&uadev[idx]); 796 mutex_unlock(&chip->mutex); 797 } 798 mutex_unlock(&qdev_mutex); 799 } 800 801 /** 802 * uaudio_sideband_notifier() - xHCI sideband event handler 803 * @intf: USB interface handle 804 * @evt: xHCI sideband event type 805 * 806 * This callback is executed when the xHCI sideband encounters a sequence 807 * that requires the sideband clients to take action. An example, is when 808 * xHCI frees the transfer ring, so the client has to ensure that the 809 * offload path is halted. 810 * 811 */ 812 static int uaudio_sideband_notifier(struct usb_interface *intf, 813 struct xhci_sideband_event *evt) 814 { 815 struct snd_usb_audio *chip; 816 struct uaudio_dev *dev; 817 int if_idx; 818 819 if (!intf || !evt) 820 return 0; 821 822 chip = usb_get_intfdata(intf); 823 824 mutex_lock(&qdev_mutex); 825 mutex_lock(&chip->mutex); 826 827 dev = &uadev[chip->card->number]; 828 829 if (evt->type == XHCI_SIDEBAND_XFER_RING_FREE) { 830 unsigned int *ep = (unsigned int *) evt->evt_data; 831 832 for (if_idx = 0; if_idx < dev->num_intf; if_idx++) { 833 if (dev->info[if_idx].data_ep_idx == *ep || 834 dev->info[if_idx].sync_ep_idx == *ep) 835 uaudio_send_disconnect_ind(chip); 836 } 837 } 838 839 mutex_unlock(&chip->mutex); 840 mutex_unlock(&qdev_mutex); 841 842 return 0; 843 } 844 845 /** 846 * qmi_bye_cb() - qmi bye message callback 847 * @handle: QMI handle 848 * @node: id of the dying node 849 * 850 * This callback is invoked when the QMI bye control message is received 851 * from the QMI client. Handle the message accordingly by ensuring that 852 * the USB offload path is disabled and cleaned up. At this point, ADSP 853 * is not utilizing the USB bus. 854 * 855 */ 856 static void qmi_bye_cb(struct qmi_handle *handle, unsigned int node) 857 { 858 struct uaudio_qmi_svc *svc = uaudio_svc; 859 860 if (svc->uaudio_svc_hdl != handle) 861 return; 862 863 if (svc->client_connected && svc->client_sq.sq_node == node) { 864 qmi_stop_session(); 865 866 /* clear QMI client parameters to block further QMI messages */ 867 svc->client_sq.sq_node = 0; 868 svc->client_sq.sq_port = 0; 869 svc->client_sq.sq_family = 0; 870 svc->client_connected = false; 871 } 872 } 873 874 /** 875 * qmi_svc_disconnect_cb() - qmi client disconnected 876 * @handle: QMI handle 877 * @node: id of the dying node 878 * @port: port of the dying client 879 * 880 * Invoked when the remote QMI client is disconnected. Handle this event 881 * the same way as when the QMI bye message is received. This will ensure 882 * the USB offloading path is disabled and cleaned up. 883 * 884 */ 885 static void qmi_svc_disconnect_cb(struct qmi_handle *handle, 886 unsigned int node, unsigned int port) 887 { 888 struct uaudio_qmi_svc *svc; 889 890 if (!uaudio_svc) 891 return; 892 893 svc = uaudio_svc; 894 if (svc->uaudio_svc_hdl != handle) 895 return; 896 897 if (svc->client_connected && svc->client_sq.sq_node == node && 898 svc->client_sq.sq_port == port) { 899 qmi_stop_session(); 900 901 /* clear QMI client parameters to block further QMI messages */ 902 svc->client_sq.sq_node = 0; 903 svc->client_sq.sq_port = 0; 904 svc->client_sq.sq_family = 0; 905 svc->client_connected = false; 906 } 907 } 908 909 /* QMI client callback handlers from QMI interface */ 910 static struct qmi_ops uaudio_svc_ops_options = { 911 .bye = qmi_bye_cb, 912 .del_client = qmi_svc_disconnect_cb, 913 }; 914 915 /* kref release callback when all streams are disabled */ 916 static void uaudio_dev_release(struct kref *kref) 917 { 918 struct uaudio_dev *dev = container_of(kref, struct uaudio_dev, kref); 919 920 uaudio_event_ring_cleanup_free(dev); 921 atomic_set(&dev->in_use, 0); 922 wake_up(&dev->disconnect_wq); 923 } 924 925 /** 926 * enable_audio_stream() - enable usb snd endpoints 927 * @subs: usb substream 928 * @pcm_format: pcm format requested 929 * @channels: number of channels 930 * @cur_rate: sample rate 931 * @datainterval: interval 932 * 933 * Opens all USB SND endpoints used for the data interface. This will increment 934 * the USB SND endpoint's opened count. Requests to keep the interface resumed 935 * until the audio stream is stopped. Will issue the USB set interface control 936 * message to enable the data interface. 937 * 938 */ 939 static int enable_audio_stream(struct snd_usb_substream *subs, 940 snd_pcm_format_t pcm_format, 941 unsigned int channels, unsigned int cur_rate, 942 int datainterval) 943 { 944 struct snd_pcm_hw_params params; 945 struct snd_usb_audio *chip; 946 struct snd_interval *i; 947 struct snd_mask *m; 948 int ret; 949 950 chip = subs->stream->chip; 951 952 _snd_pcm_hw_params_any(¶ms); 953 954 m = hw_param_mask(¶ms, SNDRV_PCM_HW_PARAM_FORMAT); 955 snd_mask_leave(m, pcm_format); 956 957 i = hw_param_interval(¶ms, SNDRV_PCM_HW_PARAM_CHANNELS); 958 snd_interval_setinteger(i); 959 i->min = channels; 960 i->max = channels; 961 962 i = hw_param_interval(¶ms, SNDRV_PCM_HW_PARAM_RATE); 963 snd_interval_setinteger(i); 964 i->min = cur_rate; 965 i->max = cur_rate; 966 967 pm_runtime_barrier(&chip->intf[0]->dev); 968 snd_usb_autoresume(chip); 969 970 ret = snd_usb_hw_params(subs, ¶ms); 971 if (ret < 0) 972 goto put_suspend; 973 974 if (!atomic_read(&chip->shutdown)) { 975 ret = snd_usb_lock_shutdown(chip); 976 if (ret < 0) 977 goto detach_ep; 978 979 if (subs->sync_endpoint) { 980 ret = snd_usb_endpoint_prepare(chip, subs->sync_endpoint); 981 if (ret < 0) 982 goto unlock; 983 } 984 985 ret = snd_usb_endpoint_prepare(chip, subs->data_endpoint); 986 if (ret < 0) 987 goto unlock; 988 989 snd_usb_unlock_shutdown(chip); 990 991 dev_dbg(uaudio_qdev->data->dev, 992 "selected %s iface:%d altsetting:%d datainterval:%dus\n", 993 subs->direction ? "capture" : "playback", 994 subs->cur_audiofmt->iface, subs->cur_audiofmt->altsetting, 995 (1 << subs->cur_audiofmt->datainterval) * 996 (subs->dev->speed >= USB_SPEED_HIGH ? 997 BUS_INTERVAL_HIGHSPEED_AND_ABOVE : 998 BUS_INTERVAL_FULL_SPEED)); 999 } 1000 1001 return 0; 1002 1003 unlock: 1004 snd_usb_unlock_shutdown(chip); 1005 1006 detach_ep: 1007 snd_usb_hw_free(subs); 1008 1009 put_suspend: 1010 snd_usb_autosuspend(chip); 1011 1012 return ret; 1013 } 1014 1015 /** 1016 * uaudio_transfer_buffer_setup() - fetch and populate xfer buffer params 1017 * @subs: usb substream 1018 * @xfer_buf: xfer buf to be allocated 1019 * @xfer_buf_len: size of allocation 1020 * @mem_info: QMI response info 1021 * 1022 * Allocates and maps the transfer buffers that will be utilized by the 1023 * audio DSP. Will populate the information in the QMI response that is 1024 * sent back to the stream enable request. 1025 * 1026 */ 1027 static int uaudio_transfer_buffer_setup(struct snd_usb_substream *subs, 1028 void **xfer_buf_cpu, u32 xfer_buf_len, 1029 struct mem_info_v01 *mem_info) 1030 { 1031 struct sg_table xfer_buf_sgt; 1032 dma_addr_t xfer_buf_dma; 1033 void *xfer_buf; 1034 u32 len = xfer_buf_len; 1035 bool dma_coherent; 1036 dma_addr_t xfer_buf_dma_sysdev; 1037 u32 remainder; 1038 u32 mult; 1039 int ret; 1040 1041 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1042 1043 /* xfer buffer, multiple of 4K only */ 1044 if (!len) 1045 len = PAGE_SIZE; 1046 1047 mult = len / PAGE_SIZE; 1048 remainder = len % PAGE_SIZE; 1049 len = mult * PAGE_SIZE; 1050 len += remainder ? PAGE_SIZE : 0; 1051 1052 if (len > MAX_XFER_BUFF_LEN) { 1053 dev_err(uaudio_qdev->data->dev, 1054 "req buf len %d > max buf len %lu, setting %lu\n", 1055 len, MAX_XFER_BUFF_LEN, MAX_XFER_BUFF_LEN); 1056 len = MAX_XFER_BUFF_LEN; 1057 } 1058 1059 /* get buffer mapped into subs->dev */ 1060 xfer_buf = usb_alloc_coherent(subs->dev, len, GFP_KERNEL, &xfer_buf_dma); 1061 if (!xfer_buf) 1062 return -ENOMEM; 1063 1064 dma_get_sgtable(subs->dev->bus->sysdev, &xfer_buf_sgt, xfer_buf, 1065 xfer_buf_dma, len); 1066 1067 /* map the physical buffer into sysdev as well */ 1068 xfer_buf_dma_sysdev = uaudio_iommu_map_xfer_buf(dma_coherent, 1069 len, &xfer_buf_sgt); 1070 if (!xfer_buf_dma_sysdev) { 1071 ret = -ENOMEM; 1072 goto unmap_sync; 1073 } 1074 1075 mem_info->dma = xfer_buf_dma; 1076 mem_info->size = len; 1077 mem_info->iova = PREPEND_SID_TO_IOVA(xfer_buf_dma_sysdev, uaudio_qdev->data->sid); 1078 *xfer_buf_cpu = xfer_buf; 1079 sg_free_table(&xfer_buf_sgt); 1080 1081 return 0; 1082 1083 unmap_sync: 1084 usb_free_coherent(subs->dev, len, xfer_buf, xfer_buf_dma); 1085 1086 return ret; 1087 } 1088 1089 /** 1090 * uaudio_endpoint_setup() - fetch and populate endpoint params 1091 * @subs: usb substream 1092 * @endpoint: usb endpoint to add 1093 * @card_num: uadev index 1094 * @mem_info: QMI response info 1095 * @ep_desc: QMI ep desc response field 1096 * 1097 * Initialize the USB endpoint being used for a particular USB 1098 * stream. Will request XHCI sec intr to reserve the EP for 1099 * offloading as well as populating the QMI response with the 1100 * transfer ring parameters. 1101 * 1102 */ 1103 static phys_addr_t 1104 uaudio_endpoint_setup(struct snd_usb_substream *subs, 1105 struct snd_usb_endpoint *endpoint, int card_num, 1106 struct mem_info_v01 *mem_info, 1107 struct usb_endpoint_descriptor_v01 *ep_desc) 1108 { 1109 struct usb_host_endpoint *ep; 1110 phys_addr_t tr_pa = 0; 1111 struct sg_table *sgt; 1112 bool dma_coherent; 1113 unsigned long iova; 1114 struct page *pg; 1115 int ret = -ENODEV; 1116 1117 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1118 1119 ep = usb_pipe_endpoint(subs->dev, endpoint->pipe); 1120 if (!ep) { 1121 dev_err(uaudio_qdev->data->dev, "data ep # %d context is null\n", 1122 subs->data_endpoint->ep_num); 1123 goto exit; 1124 } 1125 1126 memcpy(ep_desc, &ep->desc, sizeof(ep->desc)); 1127 1128 ret = xhci_sideband_add_endpoint(uadev[card_num].sb, ep); 1129 if (ret < 0) { 1130 dev_err(&subs->dev->dev, 1131 "failed to add data ep to sec intr\n"); 1132 ret = -ENODEV; 1133 goto exit; 1134 } 1135 1136 sgt = xhci_sideband_get_endpoint_buffer(uadev[card_num].sb, ep); 1137 if (!sgt) { 1138 dev_err(&subs->dev->dev, 1139 "failed to get data ep ring address\n"); 1140 ret = -ENODEV; 1141 goto remove_ep; 1142 } 1143 1144 pg = sg_page(sgt->sgl); 1145 tr_pa = page_to_phys(pg); 1146 mem_info->dma = sg_dma_address(sgt->sgl); 1147 sg_free_table(sgt); 1148 1149 /* data transfer ring */ 1150 iova = uaudio_iommu_map_pa(MEM_XFER_RING, dma_coherent, tr_pa, 1151 PAGE_SIZE); 1152 if (!iova) { 1153 ret = -ENOMEM; 1154 goto clear_pa; 1155 } 1156 1157 mem_info->iova = PREPEND_SID_TO_IOVA(iova, uaudio_qdev->data->sid); 1158 mem_info->size = PAGE_SIZE; 1159 1160 return 0; 1161 1162 clear_pa: 1163 mem_info->dma = 0; 1164 remove_ep: 1165 xhci_sideband_remove_endpoint(uadev[card_num].sb, ep); 1166 exit: 1167 return ret; 1168 } 1169 1170 /** 1171 * uaudio_event_ring_setup() - fetch and populate event ring params 1172 * @subs: usb substream 1173 * @card_num: uadev index 1174 * @mem_info: QMI response info 1175 * 1176 * Register secondary interrupter to XHCI and fetch the event buffer info 1177 * and populate the information into the QMI response. 1178 * 1179 */ 1180 static int uaudio_event_ring_setup(struct snd_usb_substream *subs, 1181 int card_num, struct mem_info_v01 *mem_info) 1182 { 1183 struct sg_table *sgt; 1184 phys_addr_t er_pa; 1185 bool dma_coherent; 1186 unsigned long iova; 1187 struct page *pg; 1188 int ret; 1189 1190 dma_coherent = dev_is_dma_coherent(subs->dev->bus->sysdev); 1191 er_pa = 0; 1192 1193 /* event ring */ 1194 ret = xhci_sideband_create_interrupter(uadev[card_num].sb, 1, false, 1195 0, uaudio_qdev->data->intr_num); 1196 if (ret < 0) { 1197 dev_err(&subs->dev->dev, "failed to fetch interrupter\n"); 1198 goto exit; 1199 } 1200 1201 sgt = xhci_sideband_get_event_buffer(uadev[card_num].sb); 1202 if (!sgt) { 1203 dev_err(&subs->dev->dev, 1204 "failed to get event ring address\n"); 1205 ret = -ENODEV; 1206 goto remove_interrupter; 1207 } 1208 1209 pg = sg_page(sgt->sgl); 1210 er_pa = page_to_phys(pg); 1211 mem_info->dma = sg_dma_address(sgt->sgl); 1212 sg_free_table(sgt); 1213 1214 iova = uaudio_iommu_map_pa(MEM_EVENT_RING, dma_coherent, er_pa, 1215 PAGE_SIZE); 1216 if (!iova) { 1217 ret = -ENOMEM; 1218 goto clear_pa; 1219 } 1220 1221 mem_info->iova = PREPEND_SID_TO_IOVA(iova, uaudio_qdev->data->sid); 1222 mem_info->size = PAGE_SIZE; 1223 1224 return 0; 1225 1226 clear_pa: 1227 mem_info->dma = 0; 1228 remove_interrupter: 1229 xhci_sideband_remove_interrupter(uadev[card_num].sb); 1230 exit: 1231 return ret; 1232 } 1233 1234 /** 1235 * uaudio_populate_uac_desc() - parse UAC parameters and populate QMI resp 1236 * @subs: usb substream 1237 * @resp: QMI response buffer 1238 * 1239 * Parses information specified within UAC descriptors which explain the 1240 * sample parameters that the device expects. This information is populated 1241 * to the QMI response sent back to the audio DSP. 1242 * 1243 */ 1244 static int uaudio_populate_uac_desc(struct snd_usb_substream *subs, 1245 struct qmi_uaudio_stream_resp_msg_v01 *resp) 1246 { 1247 struct usb_interface_descriptor *altsd; 1248 struct usb_host_interface *alts; 1249 struct usb_interface *iface; 1250 int protocol; 1251 1252 iface = usb_ifnum_to_if(subs->dev, subs->cur_audiofmt->iface); 1253 if (!iface) { 1254 dev_err(&subs->dev->dev, "interface # %d does not exist\n", 1255 subs->cur_audiofmt->iface); 1256 return -ENODEV; 1257 } 1258 1259 alts = &iface->altsetting[subs->cur_audiofmt->altset_idx]; 1260 altsd = get_iface_desc(alts); 1261 protocol = altsd->bInterfaceProtocol; 1262 1263 if (protocol == UAC_VERSION_1) { 1264 struct uac1_as_header_descriptor *as; 1265 1266 as = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, 1267 UAC_AS_GENERAL); 1268 if (!as) { 1269 dev_err(&subs->dev->dev, 1270 "%u:%d : no UAC_AS_GENERAL desc\n", 1271 subs->cur_audiofmt->iface, 1272 subs->cur_audiofmt->altset_idx); 1273 return -ENODEV; 1274 } 1275 1276 resp->data_path_delay = as->bDelay; 1277 resp->data_path_delay_valid = 1; 1278 1279 resp->usb_audio_subslot_size = subs->cur_audiofmt->fmt_sz; 1280 resp->usb_audio_subslot_size_valid = 1; 1281 1282 resp->usb_audio_spec_revision = le16_to_cpu((__force __le16)0x0100); 1283 resp->usb_audio_spec_revision_valid = 1; 1284 } else if (protocol == UAC_VERSION_2) { 1285 resp->usb_audio_subslot_size = subs->cur_audiofmt->fmt_sz; 1286 resp->usb_audio_subslot_size_valid = 1; 1287 1288 resp->usb_audio_spec_revision = le16_to_cpu((__force __le16)0x0200); 1289 resp->usb_audio_spec_revision_valid = 1; 1290 } else if (protocol == UAC_VERSION_3) { 1291 if (iface->intf_assoc->bFunctionSubClass == 1292 UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0) { 1293 dev_err(&subs->dev->dev, 1294 "full adc is not supported\n"); 1295 return -EINVAL; 1296 } 1297 1298 switch (le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize)) { 1299 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16: 1300 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16: 1301 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16: 1302 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16: { 1303 resp->usb_audio_subslot_size = 0x2; 1304 break; 1305 } 1306 1307 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24: 1308 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24: 1309 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24: 1310 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24: { 1311 resp->usb_audio_subslot_size = 0x3; 1312 break; 1313 } 1314 1315 default: 1316 dev_err(&subs->dev->dev, 1317 "%d: %u: Invalid wMaxPacketSize\n", 1318 subs->cur_audiofmt->iface, 1319 subs->cur_audiofmt->altset_idx); 1320 return -EINVAL; 1321 } 1322 resp->usb_audio_subslot_size_valid = 1; 1323 } else { 1324 dev_err(&subs->dev->dev, "unknown protocol version %x\n", 1325 protocol); 1326 return -ENODEV; 1327 } 1328 1329 memcpy(&resp->std_as_opr_intf_desc, &alts->desc, sizeof(alts->desc)); 1330 1331 return 0; 1332 } 1333 1334 /** 1335 * prepare_qmi_response() - prepare stream enable response 1336 * @subs: usb substream 1337 * @req_msg: QMI request message 1338 * @resp: QMI response buffer 1339 * @info_idx: usb interface array index 1340 * 1341 * Prepares the QMI response for a USB QMI stream enable request. Will parse 1342 * out the parameters within the stream enable request, in order to match 1343 * requested audio profile to the ones exposed by the USB device connected. 1344 * 1345 * In addition, will fetch the XHCI transfer resources needed for the handoff to 1346 * happen. This includes, transfer ring and buffer addresses and secondary event 1347 * ring address. These parameters will be communicated as part of the USB QMI 1348 * stream enable response. 1349 * 1350 */ 1351 static int prepare_qmi_response(struct snd_usb_substream *subs, 1352 struct qmi_uaudio_stream_req_msg_v01 *req_msg, 1353 struct qmi_uaudio_stream_resp_msg_v01 *resp, 1354 int info_idx) 1355 { 1356 struct q6usb_offload *data; 1357 int pcm_dev_num; 1358 int card_num; 1359 void *xfer_buf_cpu; 1360 int ret; 1361 1362 pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8; 1363 card_num = (req_msg->usb_token & QMI_STREAM_REQ_CARD_NUM_MASK) >> 16; 1364 1365 if (!uadev[card_num].ctrl_intf) { 1366 dev_err(&subs->dev->dev, "audio ctrl intf info not cached\n"); 1367 return -ENODEV; 1368 } 1369 1370 ret = uaudio_populate_uac_desc(subs, resp); 1371 if (ret < 0) 1372 return ret; 1373 1374 resp->slot_id = subs->dev->slot_id; 1375 resp->slot_id_valid = 1; 1376 1377 data = snd_soc_usb_find_priv_data(uaudio_qdev->auxdev->dev.parent); 1378 if (!data) { 1379 dev_err(&subs->dev->dev, "No private data found\n"); 1380 return -ENODEV; 1381 } 1382 1383 uaudio_qdev->data = data; 1384 1385 resp->std_as_opr_intf_desc_valid = 1; 1386 ret = uaudio_endpoint_setup(subs, subs->data_endpoint, card_num, 1387 &resp->xhci_mem_info.tr_data, 1388 &resp->std_as_data_ep_desc); 1389 if (ret < 0) 1390 return ret; 1391 1392 resp->std_as_data_ep_desc_valid = 1; 1393 1394 if (subs->sync_endpoint) { 1395 ret = uaudio_endpoint_setup(subs, subs->sync_endpoint, card_num, 1396 &resp->xhci_mem_info.tr_sync, 1397 &resp->std_as_sync_ep_desc); 1398 if (ret < 0) 1399 goto drop_data_ep; 1400 1401 resp->std_as_sync_ep_desc_valid = 1; 1402 } 1403 1404 resp->interrupter_num_valid = 1; 1405 resp->controller_num_valid = 0; 1406 ret = usb_get_controller_id(subs->dev); 1407 if (ret >= 0) { 1408 resp->controller_num = ret; 1409 resp->controller_num_valid = 1; 1410 } 1411 1412 /* event ring */ 1413 ret = uaudio_event_ring_setup(subs, card_num, 1414 &resp->xhci_mem_info.evt_ring); 1415 if (ret < 0) 1416 goto drop_sync_ep; 1417 1418 uaudio_qdev->er_mapped = true; 1419 resp->interrupter_num = xhci_sideband_interrupter_id(uadev[card_num].sb); 1420 1421 resp->speed_info = get_speed_info(subs->dev->speed); 1422 if (resp->speed_info == USB_QMI_DEVICE_SPEED_INVALID_V01) { 1423 ret = -ENODEV; 1424 goto free_sec_ring; 1425 } 1426 1427 resp->speed_info_valid = 1; 1428 1429 ret = uaudio_transfer_buffer_setup(subs, &xfer_buf_cpu, req_msg->xfer_buff_size, 1430 &resp->xhci_mem_info.xfer_buff); 1431 if (ret < 0) { 1432 ret = -ENOMEM; 1433 goto free_sec_ring; 1434 } 1435 1436 resp->xhci_mem_info_valid = 1; 1437 1438 if (!atomic_read(&uadev[card_num].in_use)) { 1439 kref_init(&uadev[card_num].kref); 1440 init_waitqueue_head(&uadev[card_num].disconnect_wq); 1441 uadev[card_num].num_intf = 1442 subs->dev->config->desc.bNumInterfaces; 1443 uadev[card_num].info = kcalloc(uadev[card_num].num_intf, 1444 sizeof(struct intf_info), 1445 GFP_KERNEL); 1446 if (!uadev[card_num].info) { 1447 ret = -ENOMEM; 1448 goto unmap_er; 1449 } 1450 uadev[card_num].udev = subs->dev; 1451 atomic_set(&uadev[card_num].in_use, 1); 1452 } else { 1453 kref_get(&uadev[card_num].kref); 1454 } 1455 1456 uadev[card_num].usb_core_id = resp->controller_num; 1457 1458 /* cache intf specific info to use it for unmap and free xfer buf */ 1459 uadev[card_num].info[info_idx].data_xfer_ring_va = 1460 IOVA_MASK(resp->xhci_mem_info.tr_data.iova); 1461 uadev[card_num].info[info_idx].data_xfer_ring_size = PAGE_SIZE; 1462 uadev[card_num].info[info_idx].sync_xfer_ring_va = 1463 IOVA_MASK(resp->xhci_mem_info.tr_sync.iova); 1464 uadev[card_num].info[info_idx].sync_xfer_ring_size = PAGE_SIZE; 1465 uadev[card_num].info[info_idx].xfer_buf_iova = 1466 IOVA_MASK(resp->xhci_mem_info.xfer_buff.iova); 1467 uadev[card_num].info[info_idx].xfer_buf_dma = 1468 resp->xhci_mem_info.xfer_buff.dma; 1469 uadev[card_num].info[info_idx].xfer_buf_size = 1470 resp->xhci_mem_info.xfer_buff.size; 1471 uadev[card_num].info[info_idx].data_ep_pipe = subs->data_endpoint ? 1472 subs->data_endpoint->pipe : 0; 1473 uadev[card_num].info[info_idx].sync_ep_pipe = subs->sync_endpoint ? 1474 subs->sync_endpoint->pipe : 0; 1475 uadev[card_num].info[info_idx].data_ep_idx = subs->data_endpoint ? 1476 subs->data_endpoint->ep_num : 0; 1477 uadev[card_num].info[info_idx].sync_ep_idx = subs->sync_endpoint ? 1478 subs->sync_endpoint->ep_num : 0; 1479 uadev[card_num].info[info_idx].xfer_buf_cpu = xfer_buf_cpu; 1480 uadev[card_num].info[info_idx].pcm_card_num = card_num; 1481 uadev[card_num].info[info_idx].pcm_dev_num = pcm_dev_num; 1482 uadev[card_num].info[info_idx].direction = subs->direction; 1483 uadev[card_num].info[info_idx].intf_num = subs->cur_audiofmt->iface; 1484 uadev[card_num].info[info_idx].in_use = true; 1485 1486 set_bit(card_num, &uaudio_qdev->card_slot); 1487 1488 return 0; 1489 1490 unmap_er: 1491 uaudio_iommu_unmap(MEM_EVENT_RING, IOVA_BASE, PAGE_SIZE, PAGE_SIZE); 1492 free_sec_ring: 1493 xhci_sideband_remove_interrupter(uadev[card_num].sb); 1494 drop_sync_ep: 1495 if (subs->sync_endpoint) { 1496 uaudio_iommu_unmap(MEM_XFER_RING, 1497 IOVA_MASK(resp->xhci_mem_info.tr_sync.iova), 1498 PAGE_SIZE, PAGE_SIZE); 1499 xhci_sideband_remove_endpoint(uadev[card_num].sb, 1500 usb_pipe_endpoint(subs->dev, subs->sync_endpoint->pipe)); 1501 } 1502 drop_data_ep: 1503 uaudio_iommu_unmap(MEM_XFER_RING, IOVA_MASK(resp->xhci_mem_info.tr_data.iova), 1504 PAGE_SIZE, PAGE_SIZE); 1505 xhci_sideband_remove_endpoint(uadev[card_num].sb, 1506 usb_pipe_endpoint(subs->dev, subs->data_endpoint->pipe)); 1507 1508 return ret; 1509 } 1510 1511 /** 1512 * handle_uaudio_stream_req() - handle stream enable/disable request 1513 * @handle: QMI client handle 1514 * @sq: qrtr socket 1515 * @txn: QMI transaction context 1516 * @decoded_msg: decoded QMI message 1517 * 1518 * Main handler for the QMI stream enable/disable requests. This executes the 1519 * corresponding enable/disable stream apis, respectively. 1520 * 1521 */ 1522 static void handle_uaudio_stream_req(struct qmi_handle *handle, 1523 struct sockaddr_qrtr *sq, 1524 struct qmi_txn *txn, 1525 const void *decoded_msg) 1526 { 1527 struct qmi_uaudio_stream_req_msg_v01 *req_msg; 1528 struct qmi_uaudio_stream_resp_msg_v01 resp = {{0}, 0}; 1529 struct uaudio_qmi_svc *svc = uaudio_svc; 1530 struct snd_usb_audio *chip = NULL; 1531 struct snd_usb_substream *subs; 1532 struct usb_host_endpoint *ep; 1533 int datainterval = -EINVAL; 1534 int info_idx = -EINVAL; 1535 struct intf_info *info; 1536 u8 pcm_card_num; 1537 u8 pcm_dev_num; 1538 u8 direction; 1539 int ret = 0; 1540 1541 if (!svc->client_connected) { 1542 svc->client_sq = *sq; 1543 svc->client_connected = true; 1544 } 1545 1546 mutex_lock(&qdev_mutex); 1547 req_msg = (struct qmi_uaudio_stream_req_msg_v01 *)decoded_msg; 1548 if (!req_msg->audio_format_valid || !req_msg->bit_rate_valid || 1549 !req_msg->number_of_ch_valid || !req_msg->xfer_buff_size_valid) { 1550 ret = -EINVAL; 1551 goto response; 1552 } 1553 1554 if (!uaudio_qdev) { 1555 ret = -EINVAL; 1556 goto response; 1557 } 1558 1559 direction = (req_msg->usb_token & QMI_STREAM_REQ_DIRECTION); 1560 pcm_dev_num = (req_msg->usb_token & QMI_STREAM_REQ_DEV_NUM_MASK) >> 8; 1561 pcm_card_num = (req_msg->usb_token & QMI_STREAM_REQ_CARD_NUM_MASK) >> 16; 1562 if (pcm_card_num >= SNDRV_CARDS) { 1563 ret = -EINVAL; 1564 goto response; 1565 } 1566 1567 if (req_msg->audio_format > USB_QMI_PCM_FORMAT_U32_BE) { 1568 ret = -EINVAL; 1569 goto response; 1570 } 1571 1572 subs = find_substream(pcm_card_num, pcm_dev_num, direction); 1573 chip = uadev[pcm_card_num].chip; 1574 if (!subs || !chip || atomic_read(&chip->shutdown)) { 1575 ret = -ENODEV; 1576 goto response; 1577 } 1578 1579 info_idx = info_idx_from_ifnum(pcm_card_num, subs->cur_audiofmt ? 1580 subs->cur_audiofmt->iface : -1, req_msg->enable); 1581 if (atomic_read(&chip->shutdown) || !subs->stream || !subs->stream->pcm || 1582 !subs->stream->chip) { 1583 ret = -ENODEV; 1584 goto response; 1585 } 1586 1587 mutex_lock(&chip->mutex); 1588 if (req_msg->enable) { 1589 if (info_idx < 0 || chip->system_suspend || subs->opened) { 1590 ret = -EBUSY; 1591 mutex_unlock(&chip->mutex); 1592 1593 goto response; 1594 } 1595 subs->opened = 1; 1596 } 1597 mutex_unlock(&chip->mutex); 1598 1599 if (req_msg->service_interval_valid) { 1600 ret = get_data_interval_from_si(subs, 1601 req_msg->service_interval); 1602 if (ret == -EINVAL) 1603 goto response; 1604 1605 datainterval = ret; 1606 } 1607 1608 uadev[pcm_card_num].ctrl_intf = chip->ctrl_intf; 1609 1610 if (req_msg->enable) { 1611 ret = enable_audio_stream(subs, 1612 map_pcm_format(req_msg->audio_format), 1613 req_msg->number_of_ch, req_msg->bit_rate, 1614 datainterval); 1615 1616 if (!ret) 1617 ret = prepare_qmi_response(subs, req_msg, &resp, 1618 info_idx); 1619 if (ret < 0) { 1620 mutex_lock(&chip->mutex); 1621 subs->opened = 0; 1622 mutex_unlock(&chip->mutex); 1623 } 1624 } else { 1625 info = &uadev[pcm_card_num].info[info_idx]; 1626 if (info->data_ep_pipe) { 1627 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 1628 info->data_ep_pipe); 1629 if (ep) { 1630 xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb, 1631 ep); 1632 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, 1633 ep); 1634 } 1635 1636 info->data_ep_pipe = 0; 1637 } 1638 1639 if (info->sync_ep_pipe) { 1640 ep = usb_pipe_endpoint(uadev[pcm_card_num].udev, 1641 info->sync_ep_pipe); 1642 if (ep) { 1643 xhci_sideband_stop_endpoint(uadev[pcm_card_num].sb, 1644 ep); 1645 xhci_sideband_remove_endpoint(uadev[pcm_card_num].sb, 1646 ep); 1647 } 1648 1649 info->sync_ep_pipe = 0; 1650 } 1651 1652 disable_audio_stream(subs); 1653 mutex_lock(&chip->mutex); 1654 subs->opened = 0; 1655 mutex_unlock(&chip->mutex); 1656 } 1657 1658 response: 1659 if (!req_msg->enable && ret != -EINVAL && ret != -ENODEV) { 1660 mutex_lock(&chip->mutex); 1661 if (info_idx >= 0) { 1662 info = &uadev[pcm_card_num].info[info_idx]; 1663 uaudio_dev_intf_cleanup(uadev[pcm_card_num].udev, 1664 info); 1665 } 1666 if (atomic_read(&uadev[pcm_card_num].in_use)) 1667 kref_put(&uadev[pcm_card_num].kref, 1668 uaudio_dev_release); 1669 mutex_unlock(&chip->mutex); 1670 } 1671 mutex_unlock(&qdev_mutex); 1672 1673 resp.usb_token = req_msg->usb_token; 1674 resp.usb_token_valid = 1; 1675 resp.internal_status = ret; 1676 resp.internal_status_valid = 1; 1677 resp.status = ret ? USB_QMI_STREAM_REQ_FAILURE_V01 : ret; 1678 resp.status_valid = 1; 1679 ret = qmi_send_response(svc->uaudio_svc_hdl, sq, txn, 1680 QMI_UAUDIO_STREAM_RESP_V01, 1681 QMI_UAUDIO_STREAM_RESP_MSG_V01_MAX_MSG_LEN, 1682 qmi_uaudio_stream_resp_msg_v01_ei, &resp); 1683 } 1684 1685 static struct qmi_msg_handler uaudio_stream_req_handlers = { 1686 .type = QMI_REQUEST, 1687 .msg_id = QMI_UAUDIO_STREAM_REQ_V01, 1688 .ei = qmi_uaudio_stream_req_msg_v01_ei, 1689 .decoded_size = QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN, 1690 .fn = handle_uaudio_stream_req, 1691 }; 1692 1693 /** 1694 * qc_usb_audio_offload_init_qmi_dev() - initializes qmi dev 1695 * 1696 * Initializes the USB qdev, which is used to carry information pertaining to 1697 * the offloading resources. This device is freed only when there are no longer 1698 * any offloading candidates. (i.e, when all audio devices are disconnected) 1699 * 1700 */ 1701 static int qc_usb_audio_offload_init_qmi_dev(void) 1702 { 1703 uaudio_qdev = kzalloc(sizeof(*uaudio_qdev), GFP_KERNEL); 1704 if (!uaudio_qdev) 1705 return -ENOMEM; 1706 1707 /* initialize xfer ring and xfer buf iova list */ 1708 INIT_LIST_HEAD(&uaudio_qdev->xfer_ring_list); 1709 uaudio_qdev->curr_xfer_ring_iova = IOVA_XFER_RING_BASE; 1710 uaudio_qdev->xfer_ring_iova_size = 1711 IOVA_XFER_RING_MAX - IOVA_XFER_RING_BASE; 1712 1713 INIT_LIST_HEAD(&uaudio_qdev->xfer_buf_list); 1714 uaudio_qdev->curr_xfer_buf_iova = IOVA_XFER_BUF_BASE; 1715 uaudio_qdev->xfer_buf_iova_size = 1716 IOVA_XFER_BUF_MAX - IOVA_XFER_BUF_BASE; 1717 1718 return 0; 1719 } 1720 1721 /* Populates ppcm_idx array with supported PCM indexes */ 1722 static int qc_usb_audio_offload_fill_avail_pcms(struct snd_usb_audio *chip, 1723 struct snd_soc_usb_device *sdev) 1724 { 1725 struct snd_usb_stream *as; 1726 struct snd_usb_substream *subs; 1727 int idx = 0; 1728 1729 list_for_each_entry(as, &chip->pcm_list, list) { 1730 subs = &as->substream[SNDRV_PCM_STREAM_PLAYBACK]; 1731 if (subs->ep_num) { 1732 sdev->ppcm_idx[idx] = as->pcm->device; 1733 idx++; 1734 } 1735 /* 1736 * Break if the current index exceeds the number of possible 1737 * playback streams counted from the UAC descriptors. 1738 */ 1739 if (idx >= sdev->num_playback) 1740 break; 1741 } 1742 1743 return -1; 1744 } 1745 1746 /** 1747 * qc_usb_audio_offload_probe() - platform op connect handler 1748 * @chip: USB SND device 1749 * 1750 * Platform connect handler when a USB SND device is detected. Will 1751 * notify SOC USB about the connection to enable the USB ASoC backend 1752 * and populate internal USB chip array. 1753 * 1754 */ 1755 static void qc_usb_audio_offload_probe(struct snd_usb_audio *chip) 1756 { 1757 struct usb_interface *intf = chip->intf[chip->num_interfaces - 1]; 1758 struct usb_interface_descriptor *altsd; 1759 struct usb_host_interface *alts; 1760 struct snd_soc_usb_device *sdev; 1761 struct xhci_sideband *sb; 1762 1763 /* 1764 * If there is no priv_data, or no playback paths, the connected 1765 * device doesn't support offloading. Avoid populating entries for 1766 * this device. 1767 */ 1768 if (!snd_soc_usb_find_priv_data(uaudio_qdev->auxdev->dev.parent) || 1769 !usb_qmi_get_pcm_num(chip, 0)) 1770 return; 1771 1772 mutex_lock(&qdev_mutex); 1773 mutex_lock(&chip->mutex); 1774 if (!uadev[chip->card->number].chip) { 1775 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL); 1776 if (!sdev) 1777 goto exit; 1778 1779 sb = xhci_sideband_register(intf, XHCI_SIDEBAND_VENDOR, 1780 uaudio_sideband_notifier); 1781 if (!sb) 1782 goto free_sdev; 1783 } else { 1784 sb = uadev[chip->card->number].sb; 1785 sdev = uadev[chip->card->number].sdev; 1786 } 1787 1788 uadev[chip->card->number].sb = sb; 1789 uadev[chip->card->number].chip = chip; 1790 uadev[chip->card->number].sdev = sdev; 1791 1792 alts = &intf->altsetting[0]; 1793 altsd = get_iface_desc(alts); 1794 1795 /* Wait until all PCM devices are populated before notifying soc-usb */ 1796 if (altsd->bInterfaceNumber == chip->last_iface) { 1797 sdev->num_playback = usb_qmi_get_pcm_num(chip, 0); 1798 1799 /* 1800 * Allocate playback pcm index array based on number of possible 1801 * playback paths within the UAC descriptors. 1802 */ 1803 sdev->ppcm_idx = kcalloc(sdev->num_playback, sizeof(unsigned int), 1804 GFP_KERNEL); 1805 if (!sdev->ppcm_idx) 1806 goto unreg_xhci; 1807 1808 qc_usb_audio_offload_fill_avail_pcms(chip, sdev); 1809 sdev->card_idx = chip->card->number; 1810 sdev->chip_idx = chip->index; 1811 1812 snd_usb_offload_create_ctl(chip, uaudio_qdev->auxdev->dev.parent); 1813 snd_soc_usb_connect(uaudio_qdev->auxdev->dev.parent, sdev); 1814 } 1815 1816 mutex_unlock(&chip->mutex); 1817 mutex_unlock(&qdev_mutex); 1818 1819 return; 1820 1821 unreg_xhci: 1822 xhci_sideband_unregister(sb); 1823 uadev[chip->card->number].sb = NULL; 1824 free_sdev: 1825 kfree(sdev); 1826 uadev[chip->card->number].sdev = NULL; 1827 uadev[chip->card->number].chip = NULL; 1828 exit: 1829 mutex_unlock(&chip->mutex); 1830 mutex_unlock(&qdev_mutex); 1831 } 1832 1833 /** 1834 * qc_usb_audio_cleanup_qmi_dev() - release qmi device 1835 * 1836 * Frees the USB qdev. Only occurs when there are no longer any potential 1837 * devices that can utilize USB audio offloading. 1838 * 1839 */ 1840 static void qc_usb_audio_cleanup_qmi_dev(void) 1841 { 1842 kfree(uaudio_qdev); 1843 uaudio_qdev = NULL; 1844 } 1845 1846 /** 1847 * qc_usb_audio_offload_disconnect() - platform op disconnect handler 1848 * @chip: USB SND device 1849 * 1850 * Platform disconnect handler. Will ensure that any pending stream is 1851 * halted by issuing a QMI disconnect indication packet to the adsp. 1852 * 1853 */ 1854 static void qc_usb_audio_offload_disconnect(struct snd_usb_audio *chip) 1855 { 1856 struct uaudio_dev *dev; 1857 int card_num; 1858 1859 if (!chip) 1860 return; 1861 1862 card_num = chip->card->number; 1863 if (card_num >= SNDRV_CARDS) 1864 return; 1865 1866 mutex_lock(&qdev_mutex); 1867 mutex_lock(&chip->mutex); 1868 dev = &uadev[card_num]; 1869 1870 /* Device has already been cleaned up, or never populated */ 1871 if (!dev->chip) { 1872 mutex_unlock(&chip->mutex); 1873 mutex_unlock(&qdev_mutex); 1874 return; 1875 } 1876 1877 /* cleaned up already */ 1878 if (!dev->udev) 1879 goto done; 1880 1881 uaudio_send_disconnect_ind(chip); 1882 uaudio_dev_cleanup(dev); 1883 done: 1884 /* 1885 * If num_interfaces == 1, the last USB SND interface is being removed. 1886 * This is to accommodate for devices w/ multiple UAC functions. 1887 */ 1888 if (chip->num_interfaces == 1) { 1889 snd_soc_usb_disconnect(uaudio_qdev->auxdev->dev.parent, dev->sdev); 1890 xhci_sideband_unregister(dev->sb); 1891 dev->chip = NULL; 1892 kfree(dev->sdev->ppcm_idx); 1893 kfree(dev->sdev); 1894 dev->sdev = NULL; 1895 } 1896 mutex_unlock(&chip->mutex); 1897 1898 mutex_unlock(&qdev_mutex); 1899 } 1900 1901 /** 1902 * qc_usb_audio_offload_suspend() - USB offload PM suspend handler 1903 * @intf: USB interface 1904 * @message: suspend type 1905 * 1906 * PM suspend handler to ensure that the USB offloading driver is able to stop 1907 * any pending traffic, so that the bus can be suspended. 1908 * 1909 */ 1910 static void qc_usb_audio_offload_suspend(struct usb_interface *intf, 1911 pm_message_t message) 1912 { 1913 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1914 int card_num; 1915 1916 if (!chip) 1917 return; 1918 1919 card_num = chip->card->number; 1920 if (card_num >= SNDRV_CARDS) 1921 return; 1922 1923 mutex_lock(&qdev_mutex); 1924 mutex_lock(&chip->mutex); 1925 1926 uaudio_send_disconnect_ind(chip); 1927 1928 mutex_unlock(&chip->mutex); 1929 mutex_unlock(&qdev_mutex); 1930 } 1931 1932 static struct snd_usb_platform_ops offload_ops = { 1933 .connect_cb = qc_usb_audio_offload_probe, 1934 .disconnect_cb = qc_usb_audio_offload_disconnect, 1935 .suspend_cb = qc_usb_audio_offload_suspend, 1936 }; 1937 1938 static int qc_usb_audio_probe(struct auxiliary_device *auxdev, 1939 const struct auxiliary_device_id *id) 1940 1941 { 1942 struct uaudio_qmi_svc *svc; 1943 int ret; 1944 1945 svc = kzalloc(sizeof(*svc), GFP_KERNEL); 1946 if (!svc) 1947 return -ENOMEM; 1948 1949 svc->uaudio_svc_hdl = kzalloc(sizeof(*svc->uaudio_svc_hdl), GFP_KERNEL); 1950 if (!svc->uaudio_svc_hdl) { 1951 ret = -ENOMEM; 1952 goto free_svc; 1953 } 1954 1955 ret = qmi_handle_init(svc->uaudio_svc_hdl, 1956 QMI_UAUDIO_STREAM_REQ_MSG_V01_MAX_MSG_LEN, 1957 &uaudio_svc_ops_options, 1958 &uaudio_stream_req_handlers); 1959 ret = qmi_add_server(svc->uaudio_svc_hdl, UAUDIO_STREAM_SERVICE_ID_V01, 1960 UAUDIO_STREAM_SERVICE_VERS_V01, 0); 1961 1962 uaudio_svc = svc; 1963 1964 qc_usb_audio_offload_init_qmi_dev(); 1965 uaudio_qdev->auxdev = auxdev; 1966 1967 ret = snd_usb_register_platform_ops(&offload_ops); 1968 if (ret < 0) 1969 goto release_qmi; 1970 1971 snd_usb_rediscover_devices(); 1972 1973 return 0; 1974 1975 release_qmi: 1976 qc_usb_audio_cleanup_qmi_dev(); 1977 qmi_handle_release(svc->uaudio_svc_hdl); 1978 free_svc: 1979 kfree(svc); 1980 1981 return ret; 1982 } 1983 1984 static void qc_usb_audio_remove(struct auxiliary_device *auxdev) 1985 { 1986 struct uaudio_qmi_svc *svc = uaudio_svc; 1987 int idx; 1988 1989 /* 1990 * Remove all connected devices after unregistering ops, to ensure 1991 * that no further connect events will occur. The disconnect routine 1992 * will issue the QMI disconnect indication, which results in the 1993 * external DSP to stop issuing transfers. 1994 */ 1995 snd_usb_unregister_platform_ops(); 1996 for (idx = 0; idx < SNDRV_CARDS; idx++) 1997 qc_usb_audio_offload_disconnect(uadev[idx].chip); 1998 1999 qc_usb_audio_cleanup_qmi_dev(); 2000 2001 qmi_handle_release(svc->uaudio_svc_hdl); 2002 kfree(svc); 2003 uaudio_svc = NULL; 2004 } 2005 2006 static const struct auxiliary_device_id qc_usb_audio_table[] = { 2007 { .name = "q6usb.qc-usb-audio-offload" }, 2008 {}, 2009 }; 2010 MODULE_DEVICE_TABLE(auxiliary, qc_usb_audio_table); 2011 2012 static struct auxiliary_driver qc_usb_audio_offload_drv = { 2013 .name = "qc-usb-audio-offload", 2014 .id_table = qc_usb_audio_table, 2015 .probe = qc_usb_audio_probe, 2016 .remove = qc_usb_audio_remove, 2017 }; 2018 module_auxiliary_driver(qc_usb_audio_offload_drv); 2019 2020 MODULE_DESCRIPTION("QC USB Audio Offloading"); 2021 MODULE_LICENSE("GPL"); 2022