summaryrefslogtreecommitdiffstats
path: root/rtemsbsd/sys/fs/devfs/devfs_devs.c
blob: 027013b40a6e8bf334090895a851d0094c84a09b (plain) (blame)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright (c) 2016 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <machine/rtems-bsd-kernel-space.h>

#include <machine/pcpu.h>
#include <rtems/imfs.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <unistd.h>

#include <sys/conf.h>
#include <fs/devfs/devfs_int.h>

const char rtems_cdev_directory[] = RTEMS_CDEV_DIRECTORY;

static struct cdev *
devfs_imfs_get_context_by_iop(rtems_libio_t *iop)
{
	return IMFS_generic_get_context_by_iop(iop);
}

static int
devfs_imfs_open(rtems_libio_t *iop, const char *path, int oflag, mode_t mode)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
	struct thread *td = rtems_bsd_get_curthread_or_null();
	int error;

	if (td != NULL) {
		error = cdev->si_devsw->d_open(cdev, oflag, 0, td);
	} else {
		error = ENOMEM;
	}

	return rtems_bsd_error_to_status_and_errno(error);
}

static int
devfs_imfs_close(rtems_libio_t *iop)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
	struct thread *td = rtems_bsd_get_curthread_or_null();
	int flags = rtems_libio_to_fcntl_flags(iop->flags);
	int error;

	if (td != NULL) {
		error = cdev->si_devsw->d_close(cdev, flags, 0, td);
	} else {
		error = ENOMEM;
	}

	return rtems_bsd_error_to_status_and_errno(error);
}

static ssize_t
devfs_imfs_readv(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
    ssize_t total)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
	struct thread *td = rtems_bsd_get_curthread_or_null();
	struct uio uio = {
		.uio_iov = iov,
		.uio_iovcnt = iovcnt,
		.uio_offset = 0,
		.uio_resid = total,
		.uio_segflg = UIO_USERSPACE,
		.uio_rw = UIO_READ,
		.uio_td = td
	};
	int error;

	if (td != NULL) {
		error = cdev->si_devsw->d_read(cdev, &uio,
		    rtems_libio_to_fcntl_flags(iop->flags));
	} else {
		error = ENOMEM;
	}

	if (error == 0) {
		return (total - uio.uio_resid);
	} else {
		rtems_set_errno_and_return_minus_one(error);
	}
}

static ssize_t
devfs_imfs_read(rtems_libio_t *iop, void *buffer, size_t count)
{
	struct iovec iov = {
		.iov_base = buffer,
		.iov_len = count
	};

	return (devfs_imfs_readv(iop, &iov, 1, count));
}

static ssize_t
devfs_imfs_writev(rtems_libio_t *iop, const struct iovec *iov, int iovcnt,
    ssize_t total)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
	struct thread *td = rtems_bsd_get_curthread_or_null();
	struct uio uio = {
		.uio_iov = iov,
		.uio_iovcnt = iovcnt,
		.uio_offset = 0,
		.uio_resid = total,
		.uio_segflg = UIO_USERSPACE,
		.uio_rw = UIO_WRITE,
		.uio_td = td
	};
	int error;

	if (td != NULL) {
		error = cdev->si_devsw->d_write(cdev, &uio,
		    rtems_libio_to_fcntl_flags(iop->flags));
	} else {
		error = ENOMEM;
	}

	if (error == 0) {
		return (total - uio.uio_resid);
	} else {
		rtems_set_errno_and_return_minus_one(error);
	}
}

static ssize_t
devfs_imfs_write(rtems_libio_t *iop, const void *buffer, size_t count)
{
	struct iovec iov = {
		.iov_base = buffer,
		.iov_len = count
	};

	return (devfs_imfs_writev(iop, &iov, 1, count));
}

static int
devfs_imfs_ioctl(rtems_libio_t *iop, ioctl_command_t request, void *buffer)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);
	struct thread *td = rtems_bsd_get_curthread_or_null();
	int error;
	int flags = rtems_libio_to_fcntl_flags(iop->flags);

	if (td != 0) {
		error = cdev->si_devsw->d_ioctl(cdev, request, buffer, flags,
		    td);
	} else {
		error = ENOMEM;
	}

	return (rtems_bsd_error_to_status_and_errno(error));
}

static int
devfs_imfs_poll(rtems_libio_t *iop, int events)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);

	return (cdev->si_devsw->d_poll(cdev, events,
	    rtems_bsd_get_curthread_or_wait_forever()));
}

static int
devfs_imfs_kqfilter(rtems_libio_t *iop, struct knote *kn)
{
	struct cdev *cdev = devfs_imfs_get_context_by_iop(iop);

	return (cdev->si_devsw->d_kqfilter(cdev, kn));
}

static const rtems_filesystem_file_handlers_r devfs_imfs_handlers = {
	.open_h = devfs_imfs_open,
	.close_h = devfs_imfs_close,
	.read_h = devfs_imfs_read,
	.write_h = devfs_imfs_write,
	.ioctl_h = devfs_imfs_ioctl,
	.lseek_h = rtems_filesystem_default_lseek_file,
	.fstat_h = rtems_filesystem_default_fstat,
	.ftruncate_h = rtems_filesystem_default_ftruncate,
	.fsync_h = rtems_filesystem_default_fsync_or_fdatasync,
	.fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync,
	.fcntl_h = rtems_filesystem_default_fcntl,
	.poll_h = devfs_imfs_poll,
	.kqfilter_h = devfs_imfs_kqfilter,
	.readv_h = devfs_imfs_readv,
	.writev_h = devfs_imfs_writev,
};

static const IMFS_node_control devfs_imfs_control = IMFS_GENERIC_INITIALIZER(
    &devfs_imfs_handlers, IMFS_node_initialize_generic,
    IMFS_node_destroy_default);

struct cdev *
devfs_alloc(int flags)
{
	struct cdev *cdev;

	cdev = malloc(sizeof *cdev);
	if (cdev == NULL)
		return (NULL);

	memset(cdev, 0, sizeof *cdev);
	cdev->si_name = cdev->__si_namebuf;
	memcpy(cdev->__si_pathstruct.__si_dir, rtems_cdev_directory,
	    sizeof(rtems_cdev_directory) - 1);

	return (cdev);
}

void
devfs_free(struct cdev *cdev)
{
	free(cdev);
}

/*
 * devfs_create() and devfs_destroy() are called from kern_conf.c and
 * in both cases the devlock() mutex is held, so no further locking
 * is necesary and no sleeping allowed.
 */

void
devfs_create(struct cdev *dev)
{
	int rv;
	mode_t mode = S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO;

	rv = IMFS_make_generic_node(dev->si_path, mode, &devfs_imfs_control,
	    dev);
	BSD_ASSERT(rv == 0);
}

void
devfs_destroy(struct cdev *dev)
{
	int rv;

	rv = unlink(dev->si_path);
	BSD_ASSERT(rv == 0);
}

int
devfs_dev_exists(const char *name)
{
	const size_t dirlen = sizeof(rtems_cdev_directory) - 1;
	const size_t maxnamelen = SPECNAMELEN;

	int rv;
	char fullpath[dirlen + maxnamelen + 1];

	memcpy(fullpath, rtems_cdev_directory, dirlen);
	strncpy(fullpath + dirlen, name, maxnamelen);

	rv = access(fullpath, F_OK);

	if(rv == 0)
		return 1;
	else
		return 0;
}