|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// |
| 3 | +// Copyright(c) 2021 Intel Corporation. All rights reserved. |
| 4 | +// Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com> |
| 5 | +// |
| 6 | + |
| 7 | +#include <linux/debugfs.h> |
| 8 | +#include <linux/errno.h> |
| 9 | +#include <linux/list.h> |
| 10 | +#include <linux/mutex.h> |
| 11 | +#include <linux/slab.h> |
| 12 | +#include "sof-client.h" |
| 13 | +#include "sof-priv.h" |
| 14 | + |
| 15 | +static void sof_client_auxdev_release(struct device *dev) |
| 16 | +{ |
| 17 | + struct auxiliary_device *auxdev = to_auxiliary_dev(dev); |
| 18 | + struct sof_client_dev *cdev = auxiliary_dev_to_sof_client_dev(auxdev); |
| 19 | + |
| 20 | + kfree(cdev); |
| 21 | +} |
| 22 | + |
| 23 | +static int sof_client_dev_add_data(struct sof_client_dev *cdev, const void *data, |
| 24 | + size_t size) |
| 25 | +{ |
| 26 | + void *d = NULL; |
| 27 | + |
| 28 | + if (data) { |
| 29 | + d = kmemdup(data, size, GFP_KERNEL); |
| 30 | + if (!d) |
| 31 | + return -ENOMEM; |
| 32 | + } |
| 33 | + |
| 34 | + cdev->auxdev.dev.platform_data = d; |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +int sof_client_dev_register(struct snd_sof_dev *sdev, const char *name, u32 id, |
| 39 | + const void *data, size_t size) |
| 40 | +{ |
| 41 | + struct auxiliary_device *auxdev; |
| 42 | + struct sof_client_dev *cdev; |
| 43 | + int ret; |
| 44 | + |
| 45 | + cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); |
| 46 | + if (!cdev) |
| 47 | + return -ENOMEM; |
| 48 | + |
| 49 | + cdev->sdev = sdev; |
| 50 | + auxdev = &cdev->auxdev; |
| 51 | + auxdev->name = name; |
| 52 | + auxdev->dev.parent = sdev->dev; |
| 53 | + auxdev->dev.release = sof_client_auxdev_release; |
| 54 | + auxdev->id = id; |
| 55 | + |
| 56 | + ret = sof_client_dev_add_data(cdev, data, size); |
| 57 | + if (ret < 0) |
| 58 | + return ret; |
| 59 | + |
| 60 | + ret = auxiliary_device_init(auxdev); |
| 61 | + if (ret < 0) { |
| 62 | + dev_err(sdev->dev, "error: failed to initialize client dev %s\n", name); |
| 63 | + kfree(cdev); |
| 64 | + return ret; |
| 65 | + } |
| 66 | + |
| 67 | + ret = auxiliary_device_add(&cdev->auxdev); |
| 68 | + if (ret < 0) { |
| 69 | + dev_err(sdev->dev, "error: failed to add client dev %s\n", name); |
| 70 | + /* |
| 71 | + * cdev will be freed when the release callback is invoked through |
| 72 | + * put_device() |
| 73 | + */ |
| 74 | + auxiliary_device_uninit(&cdev->auxdev); |
| 75 | + return ret; |
| 76 | + } |
| 77 | + |
| 78 | + /* add to list of SOF client devices */ |
| 79 | + mutex_lock(&sdev->ipc_client_mutex); |
| 80 | + list_add(&cdev->list, &sdev->ipc_client_list); |
| 81 | + mutex_unlock(&sdev->ipc_client_mutex); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | +EXPORT_SYMBOL_NS_GPL(sof_client_dev_register, SND_SOC_SOF_CLIENT); |
| 86 | + |
| 87 | +void sof_client_dev_unregister(struct snd_sof_dev *sdev, const char *name, u32 id) |
| 88 | +{ |
| 89 | + struct sof_client_dev *cdev, *_cdev; |
| 90 | + |
| 91 | + mutex_lock(&sdev->ipc_client_mutex); |
| 92 | + |
| 93 | + /* |
| 94 | + * cdev will be freed when the release callback for the auxiliary device |
| 95 | + * is invoked |
| 96 | + */ |
| 97 | + list_for_each_entry_safe(cdev, _cdev, &sdev->ipc_client_list, list) { |
| 98 | + if (!strcmp(cdev->auxdev.name, name) && cdev->auxdev.id == id) { |
| 99 | + list_del(&cdev->list); |
| 100 | + auxiliary_device_delete(&cdev->auxdev); |
| 101 | + auxiliary_device_uninit(&cdev->auxdev); |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + mutex_unlock(&sdev->ipc_client_mutex); |
| 107 | +} |
| 108 | +EXPORT_SYMBOL_NS_GPL(sof_client_dev_unregister, SND_SOC_SOF_CLIENT); |
| 109 | + |
| 110 | +int sof_client_ipc_tx_message(struct sof_client_dev *cdev, void *ipc_msg, |
| 111 | + void *reply_data, size_t reply_bytes) |
| 112 | +{ |
| 113 | + struct sof_ipc_cmd_hdr *hdr = ipc_msg; |
| 114 | + |
| 115 | + if (!hdr) |
| 116 | + return -EINVAL; |
| 117 | + |
| 118 | + return sof_ipc_tx_message(cdev->sdev->ipc, hdr->cmd, ipc_msg, hdr->size, |
| 119 | + reply_data, reply_bytes); |
| 120 | +} |
| 121 | +EXPORT_SYMBOL_NS_GPL(sof_client_ipc_tx_message, SND_SOC_SOF_CLIENT); |
| 122 | + |
| 123 | +struct dentry *sof_client_get_debugfs_root(struct sof_client_dev *cdev) |
| 124 | +{ |
| 125 | + return cdev->sdev->debugfs_root; |
| 126 | +} |
| 127 | +EXPORT_SYMBOL_NS_GPL(sof_client_get_debugfs_root, SND_SOC_SOF_CLIENT); |
| 128 | + |
| 129 | +/* DMA buffer allocation in client drivers must use the core SOF device */ |
| 130 | +struct device *sof_client_get_dma_dev(struct sof_client_dev *cdev) |
| 131 | +{ |
| 132 | + return cdev->sdev->dev; |
| 133 | +} |
| 134 | +EXPORT_SYMBOL_NS_GPL(sof_client_get_dma_dev, SND_SOC_SOF_CLIENT); |
0 commit comments