• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_sbuf_ipc.h>
19 #include "v1_0/scsi_peripheral_ddk_stub.h"
20 #include "hdf_log.h"
21 #include "usbd_wrapper.h"
22 
23 #define HDF_LOG_TAG scsi_ddk_driver
24 
25 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
26 
27 struct HdfScsiDdkHost {
28     struct IDeviceIoService ioService;
29     OHOS::sptr<OHOS::IRemoteObject> stub;
30 };
31 
ScsiDdkDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)32 static int32_t ScsiDdkDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
33     struct HdfSBuf *reply)
34 {
35     auto *hdfScsiDdkHost = CONTAINER_OF(client->device->service, struct HdfScsiDdkHost, ioService);
36 
37     OHOS::MessageParcel *dataParcel = nullptr;
38     OHOS::MessageParcel *replyParcel = nullptr;
39     OHOS::MessageOption option;
40 
41     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
42         HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
43         return HDF_ERR_INVALID_PARAM;
44     }
45     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46         HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
47         return HDF_ERR_INVALID_PARAM;
48     }
49 
50     return hdfScsiDdkHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51 }
52 
HdfScsiDdkDriverInit(struct HdfDeviceObject * deviceObject)53 static int HdfScsiDdkDriverInit(struct HdfDeviceObject *deviceObject)
54 {
55     HDF_LOGI("%{public}s: driver init start", __func__);
56     return HDF_SUCCESS;
57 }
58 
HdfScsiDdkDriverBind(struct HdfDeviceObject * deviceObject)59 static int HdfScsiDdkDriverBind(struct HdfDeviceObject *deviceObject)
60 {
61     HDF_LOGI("%{public}s: driver bind start", __func__);
62     auto *hdfScsiDdkHost = new (std::nothrow) HdfScsiDdkHost;
63     if (hdfScsiDdkHost == nullptr) {
64         HDF_LOGE("%{public}s: failed to create HdfScsiDdkHost object", __func__);
65         return HDF_FAILURE;
66     }
67 
68     hdfScsiDdkHost->ioService.Dispatch = ScsiDdkDriverDispatch;
69     hdfScsiDdkHost->ioService.Open = nullptr;
70     hdfScsiDdkHost->ioService.Release = nullptr;
71 
72     auto serviceImpl = OHOS::HDI::Usb::ScsiDdk::V1_0::IScsiPeripheralDdk::Get(true);
73     if (serviceImpl == nullptr) {
74         HDF_LOGE("%{public}s: failed to get implement service", __func__);
75         delete hdfScsiDdkHost;
76         return HDF_FAILURE;
77     }
78 
79     hdfScsiDdkHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
80         OHOS::HDI::Usb::ScsiDdk::V1_0::IScsiPeripheralDdk::GetDescriptor());
81     if (hdfScsiDdkHost->stub == nullptr) {
82         HDF_LOGE("%{public}s: failed to get stub object", __func__);
83         delete hdfScsiDdkHost;
84         return HDF_FAILURE;
85     }
86 
87     deviceObject->service = &hdfScsiDdkHost->ioService;
88     return HDF_SUCCESS;
89 }
90 
HdfScsiDdkDriverRelease(struct HdfDeviceObject * deviceObject)91 static void HdfScsiDdkDriverRelease(struct HdfDeviceObject *deviceObject)
92 {
93     HDF_LOGI("%{public}s: driver release start", __func__);
94 
95     if (deviceObject->service == nullptr) {
96         return;
97     }
98 
99     auto *hdfScsiDdkHost = CONTAINER_OF(deviceObject->service, struct HdfScsiDdkHost, ioService);
100     if (hdfScsiDdkHost != nullptr) {
101         delete hdfScsiDdkHost;
102     }
103 }
104 
105 static struct HdfDriverEntry g_scsiddkDriverEntry = {
106     .moduleVersion = 1,
107     .moduleName = "",
108     .Bind = HdfScsiDdkDriverBind,
109     .Init = HdfScsiDdkDriverInit,
110     .Release = HdfScsiDdkDriverRelease,
111 };
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif /* __cplusplus */
116 HDF_INIT(g_scsiddkDriverEntry);
117 #ifdef __cplusplus
118 }
119 #endif /* __cplusplus */
120