1 /*
2 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_device_object.h>
19 #include <hdf_remote_service.h>
20 #include <osal_mem.h>
21 #include <stub_collector.h>
22 #include "v4_0/iaudio_manager.h"
23 #include "audio_uhdf_log.h"
24
25 #define HDF_LOG_TAG HDF_AUDIO_PRIMARY_DRV
26
27 struct HdfAudioManagerHost {
28 struct IDeviceIoService ioService;
29 struct IAudioManager *service;
30 struct HdfRemoteService **stubObject;
31 };
32
AudioManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)33 static int32_t AudioManagerDriverDispatch(
34 struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply)
35 {
36 if (client == NULL || client->device == NULL || client->device->service == NULL) {
37 HDF_LOGE("%{public}s:Param is NULL!", __func__);
38 return HDF_ERR_INVALID_PARAM;
39 }
40
41 struct HdfAudioManagerHost *audiomanagerHost =
42 CONTAINER_OF(client->device->service, struct HdfAudioManagerHost, ioService);
43 if (audiomanagerHost->service == NULL || audiomanagerHost->stubObject == NULL) {
44 HDF_LOGE("%{public}s:invalid service obj", __func__);
45 return HDF_ERR_INVALID_OBJECT;
46 }
47
48 struct HdfRemoteService *stubObj = *audiomanagerHost->stubObject;
49 if (stubObj == NULL || stubObj->dispatcher == NULL || stubObj->dispatcher->Dispatch == NULL) {
50 return HDF_ERR_INVALID_OBJECT;
51 }
52
53 return stubObj->dispatcher->Dispatch((struct HdfRemoteService *)stubObj->target, cmdId, data, reply);
54 }
55
HdfAudioManagerDriverInit(struct HdfDeviceObject * deviceObject)56 static int32_t HdfAudioManagerDriverInit(struct HdfDeviceObject *deviceObject)
57 {
58 HDF_LOGI("%{public}s: driver init enter", __func__);
59
60 if (deviceObject == NULL) {
61 HDF_LOGE("%{public}s:deviceObject is null!", __func__);
62 return HDF_ERR_INVALID_PARAM;
63 }
64
65 if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO)) {
66 HDF_LOGE("%{public}s:set primary DEVICE_CLASS_AUDIO fail!", __func__);
67 return HDF_ERR_INVALID_OBJECT;
68 }
69
70 HDF_LOGI("%{public}s:driver init success", __func__);
71 return HDF_SUCCESS;
72 }
73
HdfAudioManagerDriverBind(struct HdfDeviceObject * deviceObject)74 static int32_t HdfAudioManagerDriverBind(struct HdfDeviceObject *deviceObject)
75 {
76 HDF_LOGI("%{public}s:bind enter", __func__);
77 if (deviceObject == NULL) {
78 HDF_LOGE("%{public}s:Param is NULL!", __func__);
79 return HDF_ERR_INVALID_PARAM;
80 }
81
82 int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IAUDIOMANAGER_INTERFACE_DESC);
83 if (ret != HDF_SUCCESS) {
84 HDF_LOGI("%{public}s:failed to set interface descriptor object! ret = %{public}d", __func__, ret);
85 return HDF_FAILURE;
86 }
87
88 struct HdfAudioManagerHost *audiomanagerHost =
89 (struct HdfAudioManagerHost *)OsalMemCalloc(sizeof(struct HdfAudioManagerHost));
90 if (audiomanagerHost == NULL) {
91 HDF_LOGE("%{public}s:alloc HdfAudioManagerHost failed!", __func__);
92 return HDF_ERR_MALLOC_FAIL;
93 }
94
95 struct IAudioManager *serviceImpl = IAudioManagerGet(true);
96 if (serviceImpl == NULL) {
97 HDF_LOGE("%{public}s:create serviceImpl failed!", __func__);
98 OsalMemFree(audiomanagerHost);
99 return HDF_FAILURE;
100 }
101
102 struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IAUDIOMANAGER_INTERFACE_DESC, serviceImpl);
103 if (stubObj == NULL) {
104 HDF_LOGE("%{public}s:failed to get stub object", __func__);
105 OsalMemFree(audiomanagerHost);
106 IAudioManagerRelease(serviceImpl, true);
107 return HDF_FAILURE;
108 }
109
110 audiomanagerHost->ioService.Dispatch = AudioManagerDriverDispatch;
111 audiomanagerHost->ioService.Open = NULL;
112 audiomanagerHost->ioService.Release = NULL;
113 audiomanagerHost->service = serviceImpl;
114 audiomanagerHost->stubObject = stubObj;
115 deviceObject->service = &audiomanagerHost->ioService;
116 HDF_LOGI("%{public}s:bind success", __func__);
117 return HDF_SUCCESS;
118 }
119
HdfAudioManagerDriverRelease(struct HdfDeviceObject * deviceObject)120 static void HdfAudioManagerDriverRelease(struct HdfDeviceObject *deviceObject)
121 {
122 HDF_LOGI("%{public}s:driver release enter", __func__);
123 if (deviceObject == NULL) {
124 HDF_LOGE("%{public}s:Param is NULL!", __func__);
125 return;
126 }
127
128 struct HdfAudioManagerHost *audiomanagerHost =
129 CONTAINER_OF(deviceObject->service, struct HdfAudioManagerHost, ioService);
130 if (audiomanagerHost == NULL) {
131 HDF_LOGE("%{public}s:HdfAudioManagerHost is NULL!", __func__);
132 return;
133 }
134
135 StubCollectorRemoveObject(IAUDIOMANAGER_INTERFACE_DESC, audiomanagerHost->service);
136 IAudioManagerRelease(audiomanagerHost->service, true);
137 OsalMemFree(audiomanagerHost);
138 HDF_LOGI("%{public}s:release success", __func__);
139 }
140
141 static struct HdfDriverEntry g_audiomanagerDriverEntry = {
142 .moduleVersion = 1,
143 .moduleName = "audio_primary_driver",
144 .Bind = HdfAudioManagerDriverBind,
145 .Init = HdfAudioManagerDriverInit,
146 .Release = HdfAudioManagerDriverRelease,
147 };
148
149 HDF_INIT(g_audiomanagerDriverEntry);