1 /*-
2 * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/module.h>
31 #include <sys/ioccom.h>
32 #include <sys/conf.h>
33 #include <sys/proc.h>
34
35 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
36
37 MALLOC_DECLARE(M_VCIO);
38 MALLOC_DEFINE(M_VCIO, "vcio", "VCIO temporary buffers");
39
40 static struct cdev *sdev;
41 static d_ioctl_t vcio_ioctl;
42
43 static struct cdevsw vcio_devsw = {
44 /* version */ .d_version = D_VERSION,
45 /* ioctl */ .d_ioctl = vcio_ioctl,
46 };
47
48 #define VCIO_IOC_MAGIC 100
49 #define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
50
51 int
vcio_ioctl(struct cdev * dev,u_long cmd,caddr_t arg,int mode,struct thread * td)52 vcio_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
53 struct thread *td)
54 {
55 int error;
56 void *ptr;
57 uint32_t size;
58 uint8_t *property;
59
60 error = 0;
61 switch(cmd) {
62 case IOCTL_MBOX_PROPERTY:
63 memcpy (&ptr, arg, sizeof(ptr));
64 error = copyin(ptr, &size, sizeof(size));
65
66 if (error != 0)
67 break;
68 property = malloc(size, M_VCIO, M_WAITOK);
69
70 error = copyin(ptr, property, size);
71 if (error) {
72 free(property, M_VCIO);
73 break;
74 }
75
76 error = bcm2835_mbox_property(property, size);
77 if (error) {
78 free(property, M_VCIO);
79 break;
80 }
81
82 error = copyout(property, ptr, size);
83 free(property, M_VCIO);
84
85 break;
86 default:
87 error = EINVAL;
88 break;
89 }
90 return (error);
91 }
92
93 static int
vcio_load(module_t mod,int cmd,void * arg)94 vcio_load(module_t mod, int cmd, void *arg)
95 {
96 int err = 0;
97
98 switch (cmd) {
99 case MOD_LOAD:
100 sdev = make_dev(&vcio_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "vcio");
101 break;
102
103 case MOD_UNLOAD:
104 destroy_dev(sdev);
105 break;
106
107 default:
108 err = EOPNOTSUPP;
109 break;
110 }
111
112 return(err);
113 }
114
115 DEV_MODULE(vcio, vcio_load, NULL);
116 MODULE_DEPEND(vcio, mbox, 1, 1, 1);
117