io_dummy.c 3.25 KB
Newer Older
Gerald Lejeune's avatar
Gerald Lejeune committed
1
2
3
/*
 * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
Gerald Lejeune's avatar
Gerald Lejeune committed
5
6
7
8
9
 */

#include <assert.h>
#include <string.h>

10
11
12
13
14
#include <common/debug.h>
#include <drivers/io/io_driver.h>
#include <drivers/io/io_dummy.h>
#include <drivers/io/io_storage.h>

Gerald Lejeune's avatar
Gerald Lejeune committed
15
16
17
18
19
20
21
22
struct file_state {
	int in_use;
	size_t size;
};

static struct file_state current_file = {0};

/* Identify the device type as dummy */
23
static io_type_t device_type_dummy(void)
Gerald Lejeune's avatar
Gerald Lejeune committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{
	return IO_TYPE_DUMMY;
}

/* Dummy device functions */
static int dummy_dev_open(const uintptr_t dev_spec, io_dev_info_t **dev_info);
static int dummy_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
			     io_entity_t *entity);
static int dummy_block_len(io_entity_t *entity, size_t *length);
static int dummy_block_read(io_entity_t *entity, uintptr_t buffer,
			     size_t length, size_t *length_read);
static int dummy_block_close(io_entity_t *entity);
static int dummy_dev_close(io_dev_info_t *dev_info);


static const io_dev_connector_t dummy_dev_connector = {
	.dev_open = dummy_dev_open
};


static const io_dev_funcs_t dummy_dev_funcs = {
	.type = device_type_dummy,
	.open = dummy_block_open,
	.seek = NULL,
	.size = dummy_block_len,
	.read = dummy_block_read,
	.write = NULL,
	.close = dummy_block_close,
	.dev_init = NULL,
	.dev_close = dummy_dev_close,
};


static const io_dev_info_t dummy_dev_info = {
	.funcs = &dummy_dev_funcs,
	.info = (uintptr_t)NULL
};


/* Open a connection to the dummy device */
static int dummy_dev_open(const uintptr_t dev_spec __attribute__((unused)),
			   io_dev_info_t **dev_info)
{
	assert(dev_info != NULL);
	*dev_info = (io_dev_info_t *)&dummy_dev_info;

	return 0;
}


/* Close a connection to the dummy device */
static int dummy_dev_close(io_dev_info_t *dev_info)
{
	return 0;
}


/* Open a file on the dummy device */
static int dummy_block_open(io_dev_info_t *dev_info, const uintptr_t spec,
			     io_entity_t *entity)
{
	int result;
	const io_block_spec_t *block_spec = (io_block_spec_t *)spec;

	if (current_file.in_use == 0) {
		assert(block_spec != NULL);
		assert(entity != NULL);

		current_file.in_use = 1;
		current_file.size = block_spec->length;
		entity->info = (uintptr_t)&current_file;
		result = 0;
	} else {
		WARN("A Dummy device is already active. Close first.\n");
		result = -ENOMEM;
	}

	return result;
}


/* Return the size of a file on the dummy device */
static int dummy_block_len(io_entity_t *entity, size_t *length)
{
	assert(entity != NULL);
	assert(length != NULL);

	*length =  ((struct file_state *)entity->info)->size;

	return 0;
}


/* Read data from a file on the dummy device */
static int dummy_block_read(io_entity_t *entity, uintptr_t buffer,
			     size_t length, size_t *length_read)
{
	assert(length_read != NULL);

	*length_read = length;

	return 0;
}


/* Close a file on the dummy device */
static int dummy_block_close(io_entity_t *entity)
{
	assert(entity != NULL);

	entity->info = 0;
	current_file.in_use = 0;

	return 0;
}


/* Exported functions */

/* Register the dummy driver with the IO abstraction */
int register_io_dev_dummy(const io_dev_connector_t **dev_con)
{
	int result;

	assert(dev_con != NULL);

	result = io_register_device(&dummy_dev_info);
	if (result == 0)
		*dev_con = &dummy_dev_connector;

	return result;
}