1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 #include <interface/spi/spi.h>
18 #include <interface/spi/spi_loopback.h>
19 #include <interface/spi/spi_test.h>
20 #include <lib/spi/srv/srv.h>
21 #include <lib/tipc/tipc_srv.h>
22 #include <lk/err_ptr.h>
23 
24 #include "driver/swspi.h"
25 
26 #define TLOG_TAG "swspi-srv"
27 #include <trusty_log.h>
28 
29 #define SPI_MAX_MSG_SIZE 1024
30 
31 /*
32  * Software SPI buses/devices are connected the following way:
33  * First SPI bus is shared. It has a fake and a test device connected to it.
34  * Second SPI bus is dedicated. Only software loopback device is connected to
35  * it.
36  */
37 static struct spi_bus_ctx test_bus = {
38         .num_devs = 2, /* pretend this bus is shared */
39 };
40 
41 static struct spi_dev_ctx test_dev = {
42         .bus = &test_bus,
43 };
44 
45 #if WITH_SW_SPI_LOOPBACK
46 static struct spi_bus_ctx loopback_bus = {
47         .num_devs = 1,
48 };
49 
50 static struct spi_dev_ctx loopback_dev = {
51         .bus = &loopback_bus,
52         .loopback = true,
53 };
54 #endif
55 
56 static const struct tipc_port_acl port_acl = {
57         .flags = IPC_PORT_ALLOW_TA_CONNECT,
58         .uuids = NULL,
59         .uuid_num = 0, /* allow any app to connect */
60         .extra_data = NULL,
61 };
62 
63 static const struct tipc_port ports[] = {
64         {
65                 .name = SPI_TEST_PORT,
66                 .msg_max_size = SPI_MAX_MSG_SIZE,
67                 .msg_queue_len = 1,
68                 .acl = &port_acl,
69                 .priv = &test_dev,
70         },
71 #if WITH_SW_SPI_LOOPBACK
72         {
73                 .name = SPI_LOOPBACK_PORT,
74                 .msg_max_size = SPI_MAX_MSG_SIZE,
75                 .msg_queue_len = 1,
76                 .acl = &port_acl,
77                 .priv = &loopback_dev,
78         },
79 #endif
80 };
81 
main(void)82 int main(void) {
83     int rc;
84     struct tipc_hset* hset;
85 
86     hset = tipc_hset_create();
87     if (IS_ERR(hset)) {
88         TLOGE("failed (%d) to create handle set\n", PTR_ERR(hset));
89         return PTR_ERR(hset);
90     }
91 
92     rc = add_spi_service(hset, ports, countof(ports));
93     if (rc != NO_ERROR) {
94         TLOGE("failed (%d) to initialize SPI test service\n", rc);
95         return rc;
96     }
97 
98     return tipc_run_event_loop(hset);
99 }
100