• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019-2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 // SOF client support:
8 //  Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
9 //  Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
10 //
11 
12 #include <linux/debugfs.h>
13 #include <linux/module.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/string_helpers.h>
16 
17 #include <sound/soc.h>
18 #include <sound/sof/header.h>
19 #include "sof-client.h"
20 #include "sof-client-probes.h"
21 
22 #define SOF_PROBES_SUSPEND_DELAY_MS 3000
23 /* only extraction supported for now */
24 #define SOF_PROBES_NUM_DAI_LINKS 1
25 
26 #define SOF_PROBES_INVALID_NODE_ID UINT_MAX
27 
28 static bool __read_mostly sof_probes_enabled;
29 module_param_named(enable, sof_probes_enabled, bool, 0444);
30 MODULE_PARM_DESC(enable, "Enable SOF probes support");
31 
32 struct sof_probes_priv {
33 	struct dentry *dfs_points;
34 	struct dentry *dfs_points_remove;
35 	u32 extractor_stream_tag;
36 	struct snd_soc_card card;
37 
38 	const struct sof_probes_host_ops *host_ops;
39 };
40 
41 struct sof_probe_point_desc {
42 	unsigned int buffer_id;
43 	unsigned int purpose;
44 	unsigned int stream_tag;
45 } __packed;
46 
47 struct sof_probe_dma {
48 	unsigned int stream_tag;
49 	unsigned int dma_buffer_size;
50 } __packed;
51 
52 struct sof_ipc_probe_dma_add_params {
53 	struct sof_ipc_cmd_hdr hdr;
54 	unsigned int num_elems;
55 	struct sof_probe_dma dma[];
56 } __packed;
57 
58 struct sof_ipc_probe_info_params {
59 	struct sof_ipc_reply rhdr;
60 	unsigned int num_elems;
61 	union {
62 		struct sof_probe_dma dma[0];
63 		struct sof_probe_point_desc desc[0];
64 	};
65 } __packed;
66 
67 struct sof_ipc_probe_point_add_params {
68 	struct sof_ipc_cmd_hdr hdr;
69 	unsigned int num_elems;
70 	struct sof_probe_point_desc desc[];
71 } __packed;
72 
73 struct sof_ipc_probe_point_remove_params {
74 	struct sof_ipc_cmd_hdr hdr;
75 	unsigned int num_elems;
76 	unsigned int buffer_id[];
77 } __packed;
78 
79 /**
80  * sof_probes_init - initialize data probing
81  * @cdev:		SOF client device
82  * @stream_tag:		Extractor stream tag
83  * @buffer_size:	DMA buffer size to set for extractor
84  *
85  * Host chooses whether extraction is supported or not by providing
86  * valid stream tag to DSP. Once specified, stream described by that
87  * tag will be tied to DSP for extraction for the entire lifetime of
88  * probe.
89  *
90  * Probing is initialized only once and each INIT request must be
91  * matched by DEINIT call.
92  */
sof_probes_init(struct sof_client_dev * cdev,u32 stream_tag,size_t buffer_size)93 static int sof_probes_init(struct sof_client_dev *cdev, u32 stream_tag,
94 			   size_t buffer_size)
95 {
96 	struct sof_ipc_probe_dma_add_params *msg;
97 	size_t size = struct_size(msg, dma, 1);
98 	struct sof_ipc_reply reply;
99 	int ret;
100 
101 	msg = kmalloc(size, GFP_KERNEL);
102 	if (!msg)
103 		return -ENOMEM;
104 	msg->hdr.size = size;
105 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_INIT;
106 	msg->num_elems = 1;
107 	msg->dma[0].stream_tag = stream_tag;
108 	msg->dma[0].dma_buffer_size = buffer_size;
109 
110 	ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
111 	kfree(msg);
112 	return ret;
113 }
114 
115 /**
116  * sof_probes_deinit - cleanup after data probing
117  * @cdev:		SOF client device
118  *
119  * Host sends DEINIT request to free previously initialized probe
120  * on DSP side once it is no longer needed. DEINIT only when there
121  * are no probes connected and with all injectors detached.
122  */
sof_probes_deinit(struct sof_client_dev * cdev)123 static int sof_probes_deinit(struct sof_client_dev *cdev)
124 {
125 	struct sof_ipc_cmd_hdr msg;
126 	struct sof_ipc_reply reply;
127 
128 	msg.size = sizeof(msg);
129 	msg.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_DEINIT;
130 
131 	return sof_client_ipc_tx_message(cdev, &msg, &reply, sizeof(reply));
132 }
133 
sof_probes_info(struct sof_client_dev * cdev,unsigned int cmd,void ** params,size_t * num_params)134 static int sof_probes_info(struct sof_client_dev *cdev, unsigned int cmd,
135 			   void **params, size_t *num_params)
136 {
137 	size_t max_msg_size = sof_client_get_ipc_max_payload_size(cdev);
138 	struct sof_ipc_probe_info_params msg = {{{0}}};
139 	struct sof_ipc_probe_info_params *reply;
140 	size_t bytes;
141 	int ret;
142 
143 	*params = NULL;
144 	*num_params = 0;
145 
146 	reply = kzalloc(max_msg_size, GFP_KERNEL);
147 	if (!reply)
148 		return -ENOMEM;
149 	msg.rhdr.hdr.size = sizeof(msg);
150 	msg.rhdr.hdr.cmd = SOF_IPC_GLB_PROBE | cmd;
151 
152 	ret = sof_client_ipc_tx_message(cdev, &msg, reply, max_msg_size);
153 	if (ret < 0 || reply->rhdr.error < 0)
154 		goto exit;
155 
156 	if (!reply->num_elems)
157 		goto exit;
158 
159 	if (cmd == SOF_IPC_PROBE_DMA_INFO)
160 		bytes = sizeof(reply->dma[0]);
161 	else
162 		bytes = sizeof(reply->desc[0]);
163 	bytes *= reply->num_elems;
164 	*params = kmemdup(&reply->dma[0], bytes, GFP_KERNEL);
165 	if (!*params) {
166 		ret = -ENOMEM;
167 		goto exit;
168 	}
169 	*num_params = reply->num_elems;
170 
171 exit:
172 	kfree(reply);
173 	return ret;
174 }
175 
176 /**
177  * sof_probes_points_info - retrieve list of active probe points
178  * @cdev:		SOF client device
179  * @desc:	Returned list of active probes
180  * @num_desc:	Returned count of active probes
181  *
182  * Host sends PROBE_POINT_INFO request to obtain list of active probe
183  * points, valid for disconnection when given probe is no longer
184  * required.
185  */
sof_probes_points_info(struct sof_client_dev * cdev,struct sof_probe_point_desc ** desc,size_t * num_desc)186 static int sof_probes_points_info(struct sof_client_dev *cdev,
187 				  struct sof_probe_point_desc **desc,
188 				  size_t *num_desc)
189 {
190 	return sof_probes_info(cdev, SOF_IPC_PROBE_POINT_INFO,
191 			       (void **)desc, num_desc);
192 }
193 
194 /**
195  * sof_probes_points_add - connect specified probes
196  * @cdev:		SOF client device
197  * @desc:	List of probe points to connect
198  * @num_desc:	Number of elements in @desc
199  *
200  * Dynamically connects to provided set of endpoints. Immediately
201  * after connection is established, host must be prepared to
202  * transfer data from or to target stream given the probing purpose.
203  *
204  * Each probe point should be removed using PROBE_POINT_REMOVE
205  * request when no longer needed.
206  */
sof_probes_points_add(struct sof_client_dev * cdev,struct sof_probe_point_desc * desc,size_t num_desc)207 static int sof_probes_points_add(struct sof_client_dev *cdev,
208 				 struct sof_probe_point_desc *desc,
209 				 size_t num_desc)
210 {
211 	struct sof_ipc_probe_point_add_params *msg;
212 	size_t size = struct_size(msg, desc, num_desc);
213 	struct sof_ipc_reply reply;
214 	int ret;
215 
216 	msg = kmalloc(size, GFP_KERNEL);
217 	if (!msg)
218 		return -ENOMEM;
219 	msg->hdr.size = size;
220 	msg->num_elems = num_desc;
221 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_ADD;
222 	memcpy(&msg->desc[0], desc, size - sizeof(*msg));
223 
224 	ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
225 	kfree(msg);
226 	return ret;
227 }
228 
229 /**
230  * sof_probes_points_remove - disconnect specified probes
231  * @cdev:		SOF client device
232  * @buffer_id:		List of probe points to disconnect
233  * @num_buffer_id:	Number of elements in @desc
234  *
235  * Removes previously connected probes from list of active probe
236  * points and frees all resources on DSP side.
237  */
sof_probes_points_remove(struct sof_client_dev * cdev,unsigned int * buffer_id,size_t num_buffer_id)238 static int sof_probes_points_remove(struct sof_client_dev *cdev,
239 				    unsigned int *buffer_id, size_t num_buffer_id)
240 {
241 	struct sof_ipc_probe_point_remove_params *msg;
242 	size_t size = struct_size(msg, buffer_id, num_buffer_id);
243 	struct sof_ipc_reply reply;
244 	int ret;
245 
246 	msg = kmalloc(size, GFP_KERNEL);
247 	if (!msg)
248 		return -ENOMEM;
249 	msg->hdr.size = size;
250 	msg->num_elems = num_buffer_id;
251 	msg->hdr.cmd = SOF_IPC_GLB_PROBE | SOF_IPC_PROBE_POINT_REMOVE;
252 	memcpy(&msg->buffer_id[0], buffer_id, size - sizeof(*msg));
253 
254 	ret = sof_client_ipc_tx_message(cdev, msg, &reply, sizeof(reply));
255 	kfree(msg);
256 	return ret;
257 }
258 
sof_probes_compr_startup(struct snd_compr_stream * cstream,struct snd_soc_dai * dai)259 static int sof_probes_compr_startup(struct snd_compr_stream *cstream,
260 				    struct snd_soc_dai *dai)
261 {
262 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
263 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
264 	struct sof_probes_priv *priv = cdev->data;
265 	const struct sof_probes_host_ops *ops = priv->host_ops;
266 	int ret;
267 
268 	if (sof_client_get_fw_state(cdev) == SOF_FW_CRASHED)
269 		return -ENODEV;
270 
271 	ret = sof_client_core_module_get(cdev);
272 	if (ret)
273 		return ret;
274 
275 	ret = ops->startup(cdev, cstream, dai, &priv->extractor_stream_tag);
276 	if (ret) {
277 		dev_err(dai->dev, "Failed to startup probe stream: %d\n", ret);
278 		priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
279 		sof_client_core_module_put(cdev);
280 	}
281 
282 	return ret;
283 }
284 
sof_probes_compr_shutdown(struct snd_compr_stream * cstream,struct snd_soc_dai * dai)285 static int sof_probes_compr_shutdown(struct snd_compr_stream *cstream,
286 				     struct snd_soc_dai *dai)
287 {
288 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
289 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
290 	struct sof_probes_priv *priv = cdev->data;
291 	const struct sof_probes_host_ops *ops = priv->host_ops;
292 	struct sof_probe_point_desc *desc;
293 	size_t num_desc;
294 	int i, ret;
295 
296 	/* disconnect all probe points */
297 	ret = sof_probes_points_info(cdev, &desc, &num_desc);
298 	if (ret < 0) {
299 		dev_err(dai->dev, "Failed to get probe points: %d\n", ret);
300 		goto exit;
301 	}
302 
303 	for (i = 0; i < num_desc; i++)
304 		sof_probes_points_remove(cdev, &desc[i].buffer_id, 1);
305 	kfree(desc);
306 
307 exit:
308 	ret = sof_probes_deinit(cdev);
309 	if (ret < 0)
310 		dev_err(dai->dev, "Failed to deinit probe: %d\n", ret);
311 
312 	priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
313 	snd_compr_free_pages(cstream);
314 
315 	ret = ops->shutdown(cdev, cstream, dai);
316 
317 	sof_client_core_module_put(cdev);
318 
319 	return ret;
320 }
321 
sof_probes_compr_set_params(struct snd_compr_stream * cstream,struct snd_compr_params * params,struct snd_soc_dai * dai)322 static int sof_probes_compr_set_params(struct snd_compr_stream *cstream,
323 				       struct snd_compr_params *params,
324 				       struct snd_soc_dai *dai)
325 {
326 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
327 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
328 	struct snd_compr_runtime *rtd = cstream->runtime;
329 	struct sof_probes_priv *priv = cdev->data;
330 	const struct sof_probes_host_ops *ops = priv->host_ops;
331 	int ret;
332 
333 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
334 	cstream->dma_buffer.dev.dev = sof_client_get_dma_dev(cdev);
335 	ret = snd_compr_malloc_pages(cstream, rtd->buffer_size);
336 	if (ret < 0)
337 		return ret;
338 
339 	ret = ops->set_params(cdev, cstream, params, dai);
340 	if (ret)
341 		return ret;
342 
343 	ret = sof_probes_init(cdev, priv->extractor_stream_tag, rtd->dma_bytes);
344 	if (ret < 0) {
345 		dev_err(dai->dev, "Failed to init probe: %d\n", ret);
346 		return ret;
347 	}
348 
349 	return 0;
350 }
351 
sof_probes_compr_trigger(struct snd_compr_stream * cstream,int cmd,struct snd_soc_dai * dai)352 static int sof_probes_compr_trigger(struct snd_compr_stream *cstream, int cmd,
353 				    struct snd_soc_dai *dai)
354 {
355 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
356 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
357 	struct sof_probes_priv *priv = cdev->data;
358 	const struct sof_probes_host_ops *ops = priv->host_ops;
359 
360 	return ops->trigger(cdev, cstream, cmd, dai);
361 }
362 
sof_probes_compr_pointer(struct snd_compr_stream * cstream,struct snd_compr_tstamp * tstamp,struct snd_soc_dai * dai)363 static int sof_probes_compr_pointer(struct snd_compr_stream *cstream,
364 				    struct snd_compr_tstamp *tstamp,
365 				    struct snd_soc_dai *dai)
366 {
367 	struct snd_soc_card *card = snd_soc_component_get_drvdata(dai->component);
368 	struct sof_client_dev *cdev = snd_soc_card_get_drvdata(card);
369 	struct sof_probes_priv *priv = cdev->data;
370 	const struct sof_probes_host_ops *ops = priv->host_ops;
371 
372 	return ops->pointer(cdev, cstream, tstamp, dai);
373 }
374 
375 static const struct snd_soc_cdai_ops sof_probes_compr_ops = {
376 	.startup = sof_probes_compr_startup,
377 	.shutdown = sof_probes_compr_shutdown,
378 	.set_params = sof_probes_compr_set_params,
379 	.trigger = sof_probes_compr_trigger,
380 	.pointer = sof_probes_compr_pointer,
381 };
382 
sof_probes_compr_copy(struct snd_soc_component * component,struct snd_compr_stream * cstream,char __user * buf,size_t count)383 static int sof_probes_compr_copy(struct snd_soc_component *component,
384 				 struct snd_compr_stream *cstream,
385 				 char __user *buf, size_t count)
386 {
387 	struct snd_compr_runtime *rtd = cstream->runtime;
388 	unsigned int offset, n;
389 	void *ptr;
390 	int ret;
391 
392 	if (count > rtd->buffer_size)
393 		count = rtd->buffer_size;
394 
395 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
396 	ptr = rtd->dma_area + offset;
397 	n = rtd->buffer_size - offset;
398 
399 	if (count < n) {
400 		ret = copy_to_user(buf, ptr, count);
401 	} else {
402 		ret = copy_to_user(buf, ptr, n);
403 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
404 	}
405 
406 	if (ret)
407 		return count - ret;
408 	return count;
409 }
410 
411 static const struct snd_compress_ops sof_probes_compressed_ops = {
412 	.copy = sof_probes_compr_copy,
413 };
414 
sof_probes_dfs_points_read(struct file * file,char __user * to,size_t count,loff_t * ppos)415 static ssize_t sof_probes_dfs_points_read(struct file *file, char __user *to,
416 					  size_t count, loff_t *ppos)
417 {
418 	struct sof_client_dev *cdev = file->private_data;
419 	struct sof_probes_priv *priv = cdev->data;
420 	struct device *dev = &cdev->auxdev.dev;
421 	struct sof_probe_point_desc *desc;
422 	int remaining, offset;
423 	size_t num_desc;
424 	char *buf;
425 	int i, ret, err;
426 
427 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
428 		dev_warn(dev, "no extractor stream running\n");
429 		return -ENOENT;
430 	}
431 
432 	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
433 	if (!buf)
434 		return -ENOMEM;
435 
436 	ret = pm_runtime_resume_and_get(dev);
437 	if (ret < 0 && ret != -EACCES) {
438 		dev_err_ratelimited(dev, "debugfs read failed to resume %d\n", ret);
439 		goto exit;
440 	}
441 
442 	ret = sof_probes_points_info(cdev, &desc, &num_desc);
443 	if (ret < 0)
444 		goto pm_error;
445 
446 	for (i = 0; i < num_desc; i++) {
447 		offset = strlen(buf);
448 		remaining = PAGE_SIZE - offset;
449 		ret = snprintf(buf + offset, remaining,
450 			       "Id: %#010x  Purpose: %u  Node id: %#x\n",
451 				desc[i].buffer_id, desc[i].purpose, desc[i].stream_tag);
452 		if (ret < 0 || ret >= remaining) {
453 			/* truncate the output buffer at the last full line */
454 			buf[offset] = '\0';
455 			break;
456 		}
457 	}
458 
459 	ret = simple_read_from_buffer(to, count, ppos, buf, strlen(buf));
460 
461 	kfree(desc);
462 
463 pm_error:
464 	pm_runtime_mark_last_busy(dev);
465 	err = pm_runtime_put_autosuspend(dev);
466 	if (err < 0)
467 		dev_err_ratelimited(dev, "debugfs read failed to idle %d\n", err);
468 
469 exit:
470 	kfree(buf);
471 	return ret;
472 }
473 
474 static ssize_t
sof_probes_dfs_points_write(struct file * file,const char __user * from,size_t count,loff_t * ppos)475 sof_probes_dfs_points_write(struct file *file, const char __user *from,
476 			    size_t count, loff_t *ppos)
477 {
478 	struct sof_client_dev *cdev = file->private_data;
479 	struct sof_probes_priv *priv = cdev->data;
480 	struct device *dev = &cdev->auxdev.dev;
481 	struct sof_probe_point_desc *desc;
482 	u32 num_elems, *array;
483 	size_t bytes;
484 	int ret, err;
485 
486 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
487 		dev_warn(dev, "no extractor stream running\n");
488 		return -ENOENT;
489 	}
490 
491 	ret = parse_int_array_user(from, count, (int **)&array);
492 	if (ret < 0)
493 		return ret;
494 
495 	num_elems = *array;
496 	bytes = sizeof(*array) * num_elems;
497 	if (bytes % sizeof(*desc)) {
498 		ret = -EINVAL;
499 		goto exit;
500 	}
501 
502 	desc = (struct sof_probe_point_desc *)&array[1];
503 
504 	ret = pm_runtime_resume_and_get(dev);
505 	if (ret < 0 && ret != -EACCES) {
506 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
507 		goto exit;
508 	}
509 
510 	ret = sof_probes_points_add(cdev, desc, bytes / sizeof(*desc));
511 	if (!ret)
512 		ret = count;
513 
514 	pm_runtime_mark_last_busy(dev);
515 	err = pm_runtime_put_autosuspend(dev);
516 	if (err < 0)
517 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
518 exit:
519 	kfree(array);
520 	return ret;
521 }
522 
523 static const struct file_operations sof_probes_points_fops = {
524 	.open = simple_open,
525 	.read = sof_probes_dfs_points_read,
526 	.write = sof_probes_dfs_points_write,
527 	.llseek = default_llseek,
528 
529 	.owner = THIS_MODULE,
530 };
531 
532 static ssize_t
sof_probes_dfs_points_remove_write(struct file * file,const char __user * from,size_t count,loff_t * ppos)533 sof_probes_dfs_points_remove_write(struct file *file, const char __user *from,
534 				   size_t count, loff_t *ppos)
535 {
536 	struct sof_client_dev *cdev = file->private_data;
537 	struct sof_probes_priv *priv = cdev->data;
538 	struct device *dev = &cdev->auxdev.dev;
539 	int ret, err;
540 	u32 *array;
541 
542 	if (priv->extractor_stream_tag == SOF_PROBES_INVALID_NODE_ID) {
543 		dev_warn(dev, "no extractor stream running\n");
544 		return -ENOENT;
545 	}
546 
547 	ret = parse_int_array_user(from, count, (int **)&array);
548 	if (ret < 0)
549 		return ret;
550 
551 	ret = pm_runtime_resume_and_get(dev);
552 	if (ret < 0) {
553 		dev_err_ratelimited(dev, "debugfs write failed to resume %d\n", ret);
554 		goto exit;
555 	}
556 
557 	ret = sof_probes_points_remove(cdev, &array[1], array[0]);
558 	if (!ret)
559 		ret = count;
560 
561 	pm_runtime_mark_last_busy(dev);
562 	err = pm_runtime_put_autosuspend(dev);
563 	if (err < 0)
564 		dev_err_ratelimited(dev, "debugfs write failed to idle %d\n", err);
565 exit:
566 	kfree(array);
567 	return ret;
568 }
569 
570 static const struct file_operations sof_probes_points_remove_fops = {
571 	.open = simple_open,
572 	.write = sof_probes_dfs_points_remove_write,
573 	.llseek = default_llseek,
574 
575 	.owner = THIS_MODULE,
576 };
577 
578 static struct snd_soc_dai_driver sof_probes_dai_drv[] = {
579 {
580 	.name = "Probe Extraction CPU DAI",
581 	.compress_new = snd_soc_new_compress,
582 	.cops = &sof_probes_compr_ops,
583 	.capture = {
584 		.stream_name = "Probe Extraction",
585 		.channels_min = 1,
586 		.channels_max = 8,
587 		.rates = SNDRV_PCM_RATE_48000,
588 		.rate_min = 48000,
589 		.rate_max = 48000,
590 	},
591 },
592 };
593 
594 static const struct snd_soc_component_driver sof_probes_component = {
595 	.name = "sof-probes-component",
596 	.compress_ops = &sof_probes_compressed_ops,
597 	.module_get_upon_open = 1,
598 	.legacy_dai_naming = 1,
599 };
600 
601 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
602 
sof_probes_client_probe(struct auxiliary_device * auxdev,const struct auxiliary_device_id * id)603 static int sof_probes_client_probe(struct auxiliary_device *auxdev,
604 				   const struct auxiliary_device_id *id)
605 {
606 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
607 	struct dentry *dfsroot = sof_client_get_debugfs_root(cdev);
608 	struct device *dev = &auxdev->dev;
609 	struct snd_soc_dai_link_component platform_component[] = {
610 		{
611 			.name = dev_name(dev),
612 		}
613 	};
614 	struct snd_soc_card *card;
615 	struct sof_probes_priv *priv;
616 	struct snd_soc_dai_link_component *cpus;
617 	struct sof_probes_host_ops *ops;
618 	struct snd_soc_dai_link *links;
619 	int ret;
620 
621 	/* do not set up the probes support if it is not enabled */
622 	if (!sof_probes_enabled)
623 		return -ENXIO;
624 
625 	/* only ipc3 is supported */
626 	if (sof_client_get_ipc_type(cdev) != SOF_IPC)
627 		return -ENXIO;
628 
629 	if (!dev->platform_data) {
630 		dev_err(dev, "missing platform data\n");
631 		return -ENODEV;
632 	}
633 
634 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
635 	if (!priv)
636 		return -ENOMEM;
637 
638 	ops = dev->platform_data;
639 
640 	if (!ops->startup || !ops->shutdown || !ops->set_params || !ops->trigger ||
641 	    !ops->pointer) {
642 		dev_err(dev, "missing platform callback(s)\n");
643 		return -ENODEV;
644 	}
645 
646 	priv->host_ops = ops;
647 	cdev->data = priv;
648 
649 	/* register probes component driver and dai */
650 	ret = devm_snd_soc_register_component(dev, &sof_probes_component,
651 					      sof_probes_dai_drv,
652 					      ARRAY_SIZE(sof_probes_dai_drv));
653 	if (ret < 0) {
654 		dev_err(dev, "failed to register SOF probes DAI driver %d\n", ret);
655 		return ret;
656 	}
657 
658 	/* set client data */
659 	priv->extractor_stream_tag = SOF_PROBES_INVALID_NODE_ID;
660 
661 	/* create read-write probes_points debugfs entry */
662 	priv->dfs_points = debugfs_create_file("probe_points", 0644, dfsroot,
663 					       cdev, &sof_probes_points_fops);
664 
665 	/* create read-write probe_points_remove debugfs entry */
666 	priv->dfs_points_remove = debugfs_create_file("probe_points_remove", 0644,
667 						      dfsroot, cdev,
668 						      &sof_probes_points_remove_fops);
669 
670 	links = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*links), GFP_KERNEL);
671 	cpus = devm_kcalloc(dev, SOF_PROBES_NUM_DAI_LINKS, sizeof(*cpus), GFP_KERNEL);
672 	if (!links || !cpus) {
673 		debugfs_remove(priv->dfs_points);
674 		debugfs_remove(priv->dfs_points_remove);
675 		return -ENOMEM;
676 	}
677 
678 	/* extraction DAI link */
679 	links[0].name = "Compress Probe Capture";
680 	links[0].id = 0;
681 	links[0].cpus = &cpus[0];
682 	links[0].num_cpus = 1;
683 	links[0].cpus->dai_name = "Probe Extraction CPU DAI";
684 	links[0].codecs = dummy;
685 	links[0].num_codecs = 1;
686 	links[0].platforms = platform_component;
687 	links[0].num_platforms = ARRAY_SIZE(platform_component);
688 	links[0].nonatomic = 1;
689 
690 	card = &priv->card;
691 
692 	card->dev = dev;
693 	card->name = "sof-probes";
694 	card->owner = THIS_MODULE;
695 	card->num_links = SOF_PROBES_NUM_DAI_LINKS;
696 	card->dai_link = links;
697 
698 	/* set idle_bias_off to prevent the core from resuming the card->dev */
699 	card->dapm.idle_bias_off = true;
700 
701 	snd_soc_card_set_drvdata(card, cdev);
702 
703 	ret = devm_snd_soc_register_card(dev, card);
704 	if (ret < 0) {
705 		debugfs_remove(priv->dfs_points);
706 		debugfs_remove(priv->dfs_points_remove);
707 		dev_err(dev, "Probes card register failed %d\n", ret);
708 		return ret;
709 	}
710 
711 	/* enable runtime PM */
712 	pm_runtime_set_autosuspend_delay(dev, SOF_PROBES_SUSPEND_DELAY_MS);
713 	pm_runtime_use_autosuspend(dev);
714 	pm_runtime_enable(dev);
715 	pm_runtime_mark_last_busy(dev);
716 	pm_runtime_idle(dev);
717 
718 	return 0;
719 }
720 
sof_probes_client_remove(struct auxiliary_device * auxdev)721 static void sof_probes_client_remove(struct auxiliary_device *auxdev)
722 {
723 	struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev);
724 	struct sof_probes_priv *priv = cdev->data;
725 
726 	if (!sof_probes_enabled)
727 		return;
728 
729 	pm_runtime_disable(&auxdev->dev);
730 	debugfs_remove(priv->dfs_points);
731 	debugfs_remove(priv->dfs_points_remove);
732 }
733 
734 static const struct auxiliary_device_id sof_probes_client_id_table[] = {
735 	{ .name = "snd_sof.hda-probes", },
736 	{},
737 };
738 MODULE_DEVICE_TABLE(auxiliary, sof_probes_client_id_table);
739 
740 /* driver name will be set based on KBUILD_MODNAME */
741 static struct auxiliary_driver sof_probes_client_drv = {
742 	.probe = sof_probes_client_probe,
743 	.remove = sof_probes_client_remove,
744 
745 	.id_table = sof_probes_client_id_table,
746 };
747 
748 module_auxiliary_driver(sof_probes_client_drv);
749 
750 MODULE_DESCRIPTION("SOF Probes Client Driver");
751 MODULE_LICENSE("GPL v2");
752 MODULE_IMPORT_NS(SND_SOC_SOF_CLIENT);
753