summaryrefslogtreecommitdiffstats
path: root/package/lqtapi/src/tapi/tapi-control.c
blob: a4f245e92fc13b2716bf58adf60b69c2d5af6df1 (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
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/list.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>

#include <linux/tapi/tapi.h>
#include <linux/tapi/tapi-ioctl.h>

/* FIXME Does it acutally make sense to allow more then one application at a
 * time to open the control device? For example calling sync from one app will
 * also sync all others. */

struct tapi_control_file {
	struct tapi_device *tdev;
	struct list_head links;
};

static struct tapi_endpoint *tapi_lookup_endpoint(struct tapi_device *tdev,
	unsigned int ep_id)
{
	struct tapi_stream *stream;

	if (ep_id < tdev->num_ports)
		return &tdev->ports[ep_id].ep;

	list_for_each_entry(stream, &tdev->streams, head) {
		if (stream->ep.id == ep_id)
			return &stream->ep;
	}

	return ERR_PTR(-ENOENT);
}

static inline struct tapi_device *inode_to_tdev(struct inode *inode)
{
	return container_of(inode->i_cdev, struct tapi_char_device, cdev)->tdev;
}

static int tapi_control_open(struct inode *inode, struct file *file)
{
	int ret;
	struct tapi_device *tdev = inode_to_tdev(inode);
	struct tapi_control_file *tctrl;

	get_device(&tdev->dev);

	tctrl = kzalloc(sizeof(*tctrl), GFP_KERNEL);
	if (!tctrl) {
		ret = -ENOMEM;
		goto err_put_device;
	}

	INIT_LIST_HEAD(&tctrl->links);
	tctrl->tdev = tdev;

	file->private_data = tctrl;

	return 0;

err_put_device:
	put_device(&tdev->dev);

	return ret;
}

static int tapi_control_release(struct inode *inode, struct file *file)
{
	struct tapi_control_file *tctrl = file->private_data;
	struct tapi_link *link;

	if (tctrl) {
		list_for_each_entry(link, &tctrl->links, head)
			tapi_link_free(tctrl->tdev, link);

		put_device(&tctrl->tdev->dev);
	}

	return 0;
}

static long tapi_control_ioctl_link_alloc(struct tapi_control_file *tctrl,
	unsigned long arg)
{
	struct tapi_link *link;
	struct tapi_endpoint *ep1, *ep2;

	ep1 = tapi_lookup_endpoint(tctrl->tdev, arg >> 16);
	ep2 = tapi_lookup_endpoint(tctrl->tdev, arg & 0xffff);

	link = tapi_link_alloc(tctrl->tdev, ep1, ep2);
	if (IS_ERR(link))
		return PTR_ERR(link);

	list_add_tail(&link->head, &tctrl->links);

	return link->id;
}

struct tapi_link *tapi_control_lookup_link(struct tapi_control_file *tctrl,
	unsigned int id)
{
	struct tapi_link *link;

	list_for_each_entry(link, &tctrl->links, head) {
		if (link->id == id)
			return link;
	}

	return NULL;
}

static long tapi_control_ioctl_link_free(struct tapi_control_file *tctrl,
	unsigned long arg)
{
	struct tapi_link *link = tapi_control_lookup_link(tctrl, arg);
	if (!link)
		return -ENOENT;

	tapi_link_free(tctrl->tdev, link);
	list_del(&link->head);

	return 0;
}

static long tapi_control_ioctl_link_enable(struct tapi_control_file *tctrl,
	unsigned long arg)
{
	struct tapi_link *link = tapi_control_lookup_link(tctrl, arg);
	if (!link)
		return -ENOENT;

	return tapi_link_enable(tctrl->tdev, link);
}

static long tapi_control_ioctl_link_disable(struct tapi_control_file *tctrl,
	unsigned long arg)
{
	struct tapi_link *link = tapi_control_lookup_link(tctrl, arg);
	if (!link)
		return -ENOENT;

	return tapi_link_disable(tctrl->tdev, link);
}

static long tapi_control_ioctl_sync(struct tapi_control_file *tctrl)
{
	return tapi_sync(tctrl->tdev);
}

static long tapi_control_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg)
{
	int ret;
	struct tapi_control_file *tctrl = file->private_data;

	switch (cmd) {
	case TAPI_CONTROL_IOCTL_LINK_ALLOC:
		ret = tapi_control_ioctl_link_alloc(tctrl, arg);
		break;
	case TAPI_CONTROL_IOCTL_LINK_FREE:
		ret = tapi_control_ioctl_link_free(tctrl, arg);
		break;
	case TAPI_CONTROL_IOCTL_LINK_ENABLE:
		ret = tapi_control_ioctl_link_enable(tctrl, arg);
		break;
	case TAPI_CONTROL_IOCTL_LINK_DISABLE:
		ret = tapi_control_ioctl_link_disable(tctrl, arg);
		break;
	case TAPI_CONTROL_IOCTL_SYNC:
		ret = tapi_control_ioctl_sync(tctrl);
		break;
	default:
		return -EINVAL;
	}

	return ret;
}

static const struct file_operations tapi_control_file_ops = {
	.owner = THIS_MODULE,
	.open = tapi_control_open,
	.release = tapi_control_release,
	.unlocked_ioctl = tapi_control_ioctl,
};

int tapi_register_control_device(struct tapi_device* tdev)
{
	dev_set_name(&tdev->control_dev.dev, "tapi%uC", tdev->id);
	return tapi_char_device_register(tdev, &tdev->control_dev, &tapi_control_file_ops);
}