• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "device_resource_if.h"
10 #include "hdf_log.h"
11 #include "osal_mem.h"
12 #include "pcie_core.h"
13 
14 #define HDF_LOG_TAG pcie_virtual_c
15 
16 #define PCIE_VIRTUAL_ADAPTER_ONE_BYTE 1
17 #define PCIE_VIRTUAL_ADAPTER_TWO_BYTE 2
18 #define PCIE_VIRTUAL_ADAPTER_FOUR_BYTE 4
19 #define PCIE_VIRTUAL_ADAPTER_READ_DATA_1 0x95
20 #define PCIE_VIRTUAL_ADAPTER_READ_DATA_2 0x27
21 #define PCIE_VIRTUAL_ADAPTER_READ_DATA_3 0x89
22 
23 struct PcieVirtualAdapterHost {
24     struct PcieCntlr cntlr;
25 };
26 
PcieVirtualAdapterRead(struct PcieCntlr * cntlr,uint32_t pos,uint8_t * data,uint32_t len)27 static int32_t PcieVirtualAdapterRead(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len)
28 {
29     if (cntlr == NULL) {
30         return HDF_ERR_INVALID_OBJECT;
31     }
32     if (len == PCIE_VIRTUAL_ADAPTER_ONE_BYTE) {
33         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1;
34     } else if (len == PCIE_VIRTUAL_ADAPTER_TWO_BYTE) {
35         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1;
36         data++;
37         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2;
38     } else {
39         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_1;
40         data++;
41         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2;
42         data++;
43         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_2;
44         data++;
45         *data = PCIE_VIRTUAL_ADAPTER_READ_DATA_3;
46     }
47     return HDF_SUCCESS;
48 }
49 
PcieVirtualAdapterWrite(struct PcieCntlr * cntlr,uint32_t pos,uint8_t * data,uint32_t len)50 static int32_t PcieVirtualAdapterWrite(struct PcieCntlr *cntlr, uint32_t pos, uint8_t *data, uint32_t len)
51 {
52     if (cntlr == NULL) {
53         return HDF_ERR_INVALID_OBJECT;
54     }
55     return HDF_SUCCESS;
56 }
57 
58 static struct PcieCntlrOps g_pcieVirtualAdapterHostOps = {
59     .read = PcieVirtualAdapterRead,
60     .write = PcieVirtualAdapterWrite,
61 };
62 
PcieVirtualAdapterBind(struct HdfDeviceObject * obj)63 static int32_t PcieVirtualAdapterBind(struct HdfDeviceObject *obj)
64 {
65     struct PcieVirtualAdapterHost *host = NULL;
66     int32_t ret;
67 
68     if (obj == NULL) {
69         HDF_LOGE("PcieVirtualAdapterBind: Fail, device is NULL.");
70         return HDF_ERR_INVALID_OBJECT;
71     }
72 
73     host = (struct PcieVirtualAdapterHost *)OsalMemCalloc(sizeof(struct PcieVirtualAdapterHost));
74     if (host == NULL) {
75         HDF_LOGE("PcieVirtualAdapterBind: no mem for PcieAdapterHost.");
76         return HDF_ERR_MALLOC_FAIL;
77     }
78     host->cntlr.ops = &g_pcieVirtualAdapterHostOps;
79     host->cntlr.hdfDevObj = obj;
80     obj->service = &(host->cntlr.service);
81 
82     ret = PcieCntlrParse(&(host->cntlr), obj);
83     if (ret != HDF_SUCCESS) {
84         goto ERR;
85     }
86 
87     ret = PcieCntlrAdd(&(host->cntlr));
88     if (ret != HDF_SUCCESS) {
89         goto ERR;
90     }
91 
92     HDF_LOGD("PcieVirtualAdapterBind: success.");
93     return HDF_SUCCESS;
94 ERR:
95     PcieCntlrRemove(&(host->cntlr));
96     OsalMemFree(host);
97     HDF_LOGD("PcieAdapterBind: fail, err = %d.", ret);
98     return ret;
99 }
100 
PcieVirtualAdapterInit(struct HdfDeviceObject * obj)101 static int32_t PcieVirtualAdapterInit(struct HdfDeviceObject *obj)
102 {
103     (void)obj;
104 
105     HDF_LOGD("PcieVirtualAdapterInit: success.");
106     return HDF_SUCCESS;
107 }
108 
PcieVirtualAdapterRelease(struct HdfDeviceObject * obj)109 static void PcieVirtualAdapterRelease(struct HdfDeviceObject *obj)
110 {
111     struct PcieCntlr *cntlr = NULL;
112     struct PcieVirtualAdapterHost *host = NULL;
113 
114     if (obj == NULL) {
115         return;
116     }
117 
118     cntlr = (struct PcieCntlr *)obj->service;
119     if (cntlr == NULL) {
120         return;
121     }
122     PcieCntlrRemove(cntlr);
123     host = (struct PcieVirtualAdapterHost *)cntlr;
124     OsalMemFree(host);
125     HDF_LOGD("PcieAdapterRelease: success.");
126 }
127 
128 struct HdfDriverEntry g_pcieVirtualDriverEntry = {
129     .moduleVersion = 1,
130     .Bind = PcieVirtualAdapterBind,
131     .Init = PcieVirtualAdapterInit,
132     .Release = PcieVirtualAdapterRelease,
133     .moduleName = "PLATFORM_PCIE_VIRTUAL",
134 };
135 HDF_INIT(g_pcieVirtualDriverEntry);
136