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