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