aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/apf/xlnk-eng.c
blob: bc40128e93cf8a0a19373994b1e621cdb371c65e (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Xilinx XLNK Engine Driver
 *
 * Copyright (C) 2010 Xilinx, Inc. All rights reserved.
 *
 *
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/io.h>
#include <linux/spinlock_types.h>
#include <linux/slab.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/string.h>
#include <linux/uio_driver.h>


#include "xlnk-eng.h"

static DEFINE_MUTEX(xlnk_eng_list_mutex);
static LIST_HEAD(xlnk_eng_list);

int xlnk_eng_register_device(struct xlnk_eng_device *xlnk_dev)
{
	mutex_lock(&xlnk_eng_list_mutex);
	/* todo: need to add more error checking */

	list_add_tail(&xlnk_dev->global_node, &xlnk_eng_list);

	mutex_unlock(&xlnk_eng_list_mutex);

	return 0;
}
EXPORT_SYMBOL(xlnk_eng_register_device);


void xlnk_eng_unregister_device(struct xlnk_eng_device *xlnk_dev)
{
	mutex_lock(&xlnk_eng_list_mutex);
	/* todo: need to add more error checking */

	list_del(&xlnk_dev->global_node);

	mutex_unlock(&xlnk_eng_list_mutex);
}
EXPORT_SYMBOL(xlnk_eng_unregister_device);

struct xlnk_eng_device *xlnk_eng_request_by_name(char *name)
{
	struct xlnk_eng_device *device, *_d;
	int found = 0;

	mutex_lock(&xlnk_eng_list_mutex);

	list_for_each_entry_safe(device, _d, &xlnk_eng_list, global_node) {
		if (!strcmp(dev_name(device->dev), name)) {
			found = 1;
			break;
		}
	}
	if (found)
		device = device->alloc(device);
	else
		device = NULL;

	mutex_unlock(&xlnk_eng_list_mutex);

	return device;
}
EXPORT_SYMBOL(xlnk_eng_request_by_name);

/**
 * struct xilinx_xlnk_eng_device - device structure for xilinx_xlnk_eng
 * @common:	common device info
 * @base:	base address for device
 * @lock:	lock used by device
 * @cnt:	usage count
 * @info:	info for registering and unregistering uio device
 */
struct xilinx_xlnk_eng_device {
	struct xlnk_eng_device common;
	void __iomem *base;
	spinlock_t lock;
	int cnt;
	struct uio_info *info;
};

static void xlnk_eng_release(struct device *dev)
{
	struct xilinx_xlnk_eng_device *xdev;
	struct xlnk_eng_device *xlnk_dev;

	xdev = dev_get_drvdata(dev);
	xlnk_dev = &xdev->common;
	if (!xlnk_dev)
		return;

	xlnk_dev->free(xlnk_dev);
}

#define DRIVER_NAME "xilinx-xlnk-eng"

#define to_xilinx_xlnk(dev)	container_of(dev, \
					struct xilinx_xlnk_eng_device, common)

static struct xlnk_eng_device *xilinx_xlnk_alloc(
					struct xlnk_eng_device *xlnkdev)
{
	struct xilinx_xlnk_eng_device *xdev;
	struct xlnk_eng_device *retdev;

	xdev = to_xilinx_xlnk(xlnkdev);

	if (xdev->cnt == 0) {
		xdev->cnt++;
		retdev = xlnkdev;
	} else
		retdev = NULL;

	return retdev;
}

static void xilinx_xlnk_free(struct xlnk_eng_device *xlnkdev)
{
	struct xilinx_xlnk_eng_device *xdev;

	xdev = to_xilinx_xlnk(xlnkdev);

	xdev->cnt = 0;
}

static int xlnk_eng_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct xilinx_xlnk_eng_device *xdev;
	struct uio_info *info;
	char *devname;

	pr_info("xlnk_eng_probe ...\n");
	xdev = devm_kzalloc(&pdev->dev, sizeof(*xdev), GFP_KERNEL);
	if (!xdev) {
		dev_err(&pdev->dev, "Not enough memory for device\n");
		return -ENOMEM;
	}

	/* more error handling */
	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
	if (!info) {
		dev_err(&pdev->dev, "Not enough memory for device\n");
		return -ENOMEM;
	}
	xdev->info = info;
	devname = devm_kzalloc(&pdev->dev, 64, GFP_KERNEL);
	if (!devname) {
		dev_err(&pdev->dev, "Not enough memory for device\n");
		return -ENOMEM;
	}
	sprintf(devname, "%s.%d", DRIVER_NAME, pdev->id);
	pr_info("uio name %s\n", devname);
	/* iomap registers */

	/* Get the data from the platform device */
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	xdev->base = devm_ioremap_resource(&pdev->dev, res);

	/* %pa types should be used here */
	dev_info(&pdev->dev, "physical base : 0x%lx\n",
		(unsigned long)res->start);
	dev_info(&pdev->dev, "register range : 0x%lx\n",
		(unsigned long)resource_size(res));
	dev_info(&pdev->dev, "base remapped to: 0x%lx\n",
		(unsigned long)xdev->base);
	if (!xdev->base) {
		dev_err(&pdev->dev, "unable to iomap registers\n");
		return -ENOMEM;
	}

	info->mem[0].addr = res->start;
	info->mem[0].size = resource_size(res);
	info->mem[0].memtype = UIO_MEM_PHYS;
	info->mem[0].internal_addr = xdev->base;

	/* info->name = DRIVER_NAME; */
	info->name = devname;
	info->version = "0.0.1";

	info->irq = -1;

	xdev->common.dev = &pdev->dev;

	xdev->common.alloc = xilinx_xlnk_alloc;
	xdev->common.free = xilinx_xlnk_free;
	xdev->common.dev->release = xlnk_eng_release;

	dev_set_drvdata(&pdev->dev, xdev);

	spin_lock_init(&xdev->lock);

	xdev->cnt = 0;

	xlnk_eng_register_device(&xdev->common);

	if (uio_register_device(&pdev->dev, info)) {
		dev_err(&pdev->dev, "uio_register_device failed\n");
		return -ENODEV;
	}
	dev_info(&pdev->dev, "xilinx-xlnk-eng uio registered\n");

	return 0;
}

static int xlnk_eng_remove(struct platform_device *pdev)
{
	struct uio_info *info;
	struct xilinx_xlnk_eng_device *xdev;

	xdev = dev_get_drvdata(&pdev->dev);
	info = xdev->info;

	uio_unregister_device(info);
	dev_info(&pdev->dev, "xilinx-xlnk-eng uio unregistered\n");
	xlnk_eng_unregister_device(&xdev->common);

	return 0;
}

static struct platform_driver xlnk_eng_driver = {
	.probe = xlnk_eng_probe,
	.remove = xlnk_eng_remove,
	.driver = {
		.owner = THIS_MODULE,
		.name = DRIVER_NAME,
	},
};

module_platform_driver(xlnk_eng_driver);

MODULE_DESCRIPTION("Xilinx xlnk engine generic driver");
MODULE_LICENSE("GPL");