Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: usb-audio: qcom: Introduce QC USB SND offloading support

Several Qualcomm SoCs have a dedicated audio DSP, which has the ability to
support USB sound devices. This vendor driver will implement the required
handshaking with the DSP, in order to pass along required resources that
will be utilized by the DSP's USB SW. The communication channel used for
this handshaking will be using the QMI protocol. Required resources
include:
- Allocated secondary event ring address
- EP transfer ring address
- Interrupter number

The above information will allow for the audio DSP to execute USB transfers
over the USB bus. It will also be able to support devices that have an
implicit feedback and sync endpoint as well. Offloading these data
transfers will allow the main/applications processor to enter lower CPU
power modes, and sustain a longer duration in those modes.

Audio offloading is initiated with the following sequence:
1. Userspace configures to route audio playback to USB backend and starts
playback on the platform soundcard.
2. The Q6DSP AFE will communicate to the audio DSP to start the USB AFE
port.
3. This results in a QMI packet with a STREAM enable command.
4. The QC audio offload driver will fetch the required resources, and pass
this information as part of the QMI response to the STREAM enable command.
5. Once the QMI response is received the audio DSP will start queuing data
on the USB bus.

As part of step#2, the audio DSP is aware of the USB SND card and pcm
device index that is being selected, and is communicated as part of the QMI
request received by QC audio offload. These indices will be used to handle
the stream enable QMI request.

Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
Acked-by: Mark Brown <broonie@kernel.org>
Link: https://lore.kernel.org/r/20250409194804.3773260-29-quic_wcheng@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Wesley Cheng and committed by
Greg Kroah-Hartman
326bbc34 bd1979b9

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