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