-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathevdi_platform_drv.c
214 lines (174 loc) · 5.16 KB
/
evdi_platform_drv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2012 Red Hat
* Copyright (c) 2015 - 2020 DisplayLink (UK) Ltd.
*
* This file is subject to the terms and conditions of the GNU General Public
* License v2. See the file COPYING in the main directory of this archive for
* more details.
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
#include "evdi_params.h"
#include "evdi_debug.h"
#include "evdi_platform_drv.h"
#include "evdi_platform_dev.h"
#include "evdi_sysfs.h"
MODULE_AUTHOR("DisplayLink (UK) Ltd.");
MODULE_DESCRIPTION("Extensible Virtual Display Interface");
MODULE_LICENSE("GPL");
#define EVDI_DEVICE_COUNT_MAX 16
static struct evdi_platform_drv_context {
struct device *root_dev;
unsigned int dev_count;
struct platform_device *devices[EVDI_DEVICE_COUNT_MAX];
struct mutex lock;
} g_ctx;
#define evdi_platform_drv_context_lock(ctx) \
mutex_lock(&ctx->lock)
#define evdi_platform_drv_context_unlock(ctx) \
mutex_unlock(&ctx->lock)
static int evdi_platform_drv_get_free_idx(struct evdi_platform_drv_context *ctx)
{
int i;
for (i = 0; i < EVDI_DEVICE_COUNT_MAX; ++i) {
if (ctx->devices[i] == NULL)
return i;
}
return -ENOMEM;
}
static struct platform_device *evdi_platform_drv_get_free_device(struct evdi_platform_drv_context *ctx)
{
int i;
struct platform_device *pdev = NULL;
for (i = 0; i < EVDI_DEVICE_COUNT_MAX; ++i) {
pdev = ctx->devices[i];
if (pdev && evdi_platform_device_is_free(pdev))
return pdev;
}
return NULL;
}
static struct platform_device *evdi_platform_drv_create_new_device(struct evdi_platform_drv_context *ctx)
{
struct platform_device *pdev = NULL;
struct platform_device_info pdevinfo = {
.parent = NULL,
.name = DRIVER_NAME,
.id = evdi_platform_drv_get_free_idx(ctx),
.res = NULL,
.num_res = 0,
.data = NULL,
.size_data = 0,
.dma_mask = DMA_BIT_MASK(32),
};
if (pdevinfo.id < 0 || ctx->dev_count >= EVDI_DEVICE_COUNT_MAX) {
EVDI_ERROR("Evdi device add failed. Too many devices.\n");
return ERR_PTR(-EINVAL);
}
pdev = evdi_platform_dev_create(&pdevinfo);
ctx->devices[pdevinfo.id] = pdev;
ctx->dev_count++;
return pdev;
}
int evdi_platform_device_add(struct device *device, struct device *parent)
{
struct evdi_platform_drv_context *ctx =
(struct evdi_platform_drv_context *)dev_get_drvdata(device);
struct platform_device *pdev = NULL;
evdi_platform_drv_context_lock(ctx);
if (parent)
pdev = evdi_platform_drv_get_free_device(ctx);
if (IS_ERR_OR_NULL(pdev))
pdev = evdi_platform_drv_create_new_device(ctx);
evdi_platform_drv_context_unlock(ctx);
if (IS_ERR_OR_NULL(pdev))
return -EINVAL;
evdi_platform_device_link(pdev, parent);
return 0;
}
int evdi_platform_add_devices(struct device *device, unsigned int val)
{
unsigned int dev_count = evdi_platform_device_count(device);
if (val == 0) {
EVDI_WARN("Adding 0 devices has no effect\n");
return 0;
}
if (val > EVDI_DEVICE_COUNT_MAX - dev_count) {
EVDI_ERROR("Evdi device add failed. Too many devices.\n");
return -EINVAL;
}
EVDI_INFO("Increasing device count to %u\n", dev_count + val);
while (val-- && evdi_platform_device_add(device, NULL) == 0)
;
return 0;
}
void evdi_platform_remove_all_devices(struct device *device)
{
int i;
struct evdi_platform_drv_context *ctx =
(struct evdi_platform_drv_context *)dev_get_drvdata(device);
evdi_platform_drv_context_lock(ctx);
for (i = 0; i < EVDI_DEVICE_COUNT_MAX; ++i) {
if (ctx->devices[i]) {
EVDI_INFO("Removing evdi %d\n", i);
evdi_platform_dev_destroy(ctx->devices[i]);
g_ctx.dev_count--;
g_ctx.devices[i] = NULL;
}
}
ctx->dev_count = 0;
evdi_platform_drv_context_unlock(ctx);
}
unsigned int evdi_platform_device_count(struct device *device)
{
unsigned int count = 0;
struct evdi_platform_drv_context *ctx = NULL;
ctx = (struct evdi_platform_drv_context *)dev_get_drvdata(device);
evdi_platform_drv_context_lock(ctx);
count = ctx->dev_count;
evdi_platform_drv_context_unlock(ctx);
return count;
}
static struct platform_driver evdi_platform_driver = {
.probe = evdi_platform_device_probe,
.remove = evdi_platform_device_remove,
.driver = {
.name = DRIVER_NAME,
.mod_name = KBUILD_MODNAME,
.owner = THIS_MODULE,
}
};
static int __init evdi_init(void)
{
int ret;
EVDI_INFO("Initialising logging on level %u\n", evdi_loglevel);
EVDI_INFO("Atomic driver: yes");
memset(&g_ctx, 0, sizeof(g_ctx));
g_ctx.root_dev = root_device_register(DRIVER_NAME);
mutex_init(&g_ctx.lock);
dev_set_drvdata(g_ctx.root_dev, &g_ctx);
evdi_sysfs_init(g_ctx.root_dev);
ret = platform_driver_register(&evdi_platform_driver);
if (ret)
return ret;
if (evdi_initial_device_count)
return evdi_platform_add_devices(
g_ctx.root_dev, evdi_initial_device_count);
return 0;
}
static void __exit evdi_exit(void)
{
EVDI_CHECKPT();
evdi_platform_remove_all_devices(g_ctx.root_dev);
platform_driver_unregister(&evdi_platform_driver);
if (!PTR_ERR_OR_ZERO(g_ctx.root_dev)) {
evdi_sysfs_exit(g_ctx.root_dev);
dev_set_drvdata(g_ctx.root_dev, NULL);
root_device_unregister(g_ctx.root_dev);
}
EVDI_INFO("Exit %s driver\n", DRIVER_NAME);
}
module_init(evdi_init);
module_exit(evdi_exit);