1 /*
2  * Copyright (C) 2018 Knowles Electronics
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "spi_reliability_test"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <sys/ioctl.h>
24 #include <string.h>
25 #include <math.h>
26 
27 #include <cutils/log.h>
28 
29 #include <linux/mfd/adnc/iaxxx-odsp.h>
30 
31 #define ODSP_NODE "/dev/iaxxx-odsp-celldrv"
32 #define PASS_THROUGH_PACKAGE "PassthruPackageSPITest.bin"
33 #define RANDOM_BYTES_FILE "/data/data/random.bin"
34 
35 #define PTP_PKG_ID      (9)
36 #define PTP_PROC_ID     (0)
37 #define PTP_PLG_IDX     (0)
38 #define PTP_BLOCK_ID    (1)
39 #define PTP_INST_ID     (12)
40 #define PTP_PRIORITY    (1)
41 
main()42 int main() {
43     FILE *odsp_node = NULL;
44     FILE *fp = NULL;
45     struct iaxxx_pkg_mgmt_info pkg_info;
46     struct iaxxx_plugin_param_blk ppb;
47     struct iaxxx_plugin_info pi;
48     uint32_t ptp_id;
49     int len = 0;
50     void *buf = NULL, *read_buf = NULL;
51     int err = 0;
52 
53     if ((fp = fopen(RANDOM_BYTES_FILE, "rb")) == NULL) {
54         ALOGE("Failed to open random bytes file");
55         err = -EIO;
56         goto exit;
57     }
58     fseek(fp, 0, SEEK_END);
59     len = ftell(fp);
60     fseek(fp, 0, SEEK_SET);
61 
62     buf = malloc(len);
63     if (NULL == buf) {
64         ALOGE("Failed to alloc memory");
65         err = -ENOMEM;
66         goto exit;
67     }
68 
69     read_buf = malloc(len);
70     if (NULL == read_buf) {
71         ALOGE("Failed to alloc memory");
72         err = -ENOMEM;
73         goto exit;
74     }
75 
76     fread(buf, 1, len, fp);
77     fclose(fp);
78     ALOGD("Successfully read random byte file");
79 
80     if((odsp_node = fopen(ODSP_NODE, "rw")) == NULL) {
81         ALOGE("file %s open for write error: %s\n", ODSP_NODE,
82                 strerror(errno));
83         err = -EIO;
84         goto exit;
85     }
86 
87     strcpy(pkg_info.pkg_name, PASS_THROUGH_PACKAGE);
88     pkg_info.pkg_id = PTP_PKG_ID;
89     pkg_info.proc_id = PTP_PROC_ID;
90     err = ioctl(fileno(odsp_node), ODSP_LOAD_PACKAGE, (unsigned long) &pkg_info);
91     if (-1 == err && EEXIST != errno) {
92         ALOGE("%s: ERROR: ODSP_LOAD_PACKAGE failed %d(%s)", __func__, errno, strerror(errno));
93         goto exit;
94     }
95     ALOGD("%s: Pass through package loaded 0x%x\n", __func__, pkg_info.proc_id);
96     ptp_id = pkg_info.proc_id;
97 
98     pi.plg_idx  = PTP_PLG_IDX;
99     pi.pkg_id   = ptp_id;
100     pi.block_id = PTP_BLOCK_ID;
101     pi.inst_id  = PTP_INST_ID;
102     pi.priority = PTP_PRIORITY;
103     err = ioctl(fileno(odsp_node), ODSP_PLG_CREATE, (unsigned long) &pi);
104     if (-1 == err && EEXIST != errno) {
105         ALOGE("%s: ERROR: ODSP_PLG_CREATE IOCTL failed to create VQ Plugin with error %d(%s)", __func__, errno, strerror(errno));
106         goto exit;
107     }
108 
109     ALOGD("%s: Pass through plugin created", __func__);
110 
111     ppb.block_id = PTP_BLOCK_ID;
112     ppb.inst_id = PTP_INST_ID;
113     ppb.id = 0;
114     ppb.param_size = len;
115     ppb.param_blk = (uintptr_t) buf;
116     err = ioctl(fileno(odsp_node), ODSP_PLG_SET_PARAM_BLK, &ppb);
117     if (-1 == err) {
118         ALOGE("%s: ERROR: ODSP_PLG_SET_PARAM_BLK IOCTL failed with error %d(%s)\n", __func__, errno, strerror(errno));
119         goto exit;
120     }
121 
122     ALOGD("%s: Random byte set param block completed", __func__);
123 
124     ppb.block_id = PTP_BLOCK_ID;
125     ppb.inst_id = PTP_INST_ID;
126     ppb.id = 0;
127     ppb.param_size = len;
128     ppb.param_blk = (uintptr_t) read_buf;
129     err = ioctl(fileno(odsp_node), ODSP_PLG_GET_PARAM_BLK, &ppb);
130     if (-1 == err) {
131         ALOGE("%s: ERROR: ODSP_PLG_GET_PARAM_BLK IOCTL failed with error %d(%s)\n", __func__, errno, strerror(errno));
132         goto exit;
133     }
134 
135     ALOGD("%s: Random byte get param block completed", __func__);
136 
137     err = memcmp(buf, read_buf, len);
138     if (0 != err) {
139         ALOGE("ERROR: Set/Get buffers were different");
140         err = -EINVAL;
141         goto exit;
142     }
143 
144     fp = fopen("/data/data/getbuf.bin", "wb");
145     fwrite(read_buf, 1, len, fp);
146     fclose(fp);
147 
148     ALOGD("%s: Set/Get buffers matched", __func__);
149 
150 exit:
151     if (odsp_node)
152         fclose(odsp_node);
153 
154     if (read_buf)
155         free (read_buf);
156 
157     if (buf)
158         free (buf);
159 
160     if (fp)
161         fclose(fp);
162 
163     return err;
164 }
165