xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_vcio.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
31 #include <sys/malloc.h>
32 #include <sys/module.h>
33 #include <sys/ioccom.h>
34 #include <sys/conf.h>
35 #include <sys/proc.h>
36 
37 #include <arm/broadcom/bcm2835/bcm2835_mbox_prop.h>
38 
39 MALLOC_DECLARE(M_VCIO);
40 MALLOC_DEFINE(M_VCIO, "vcio", "VCIO temporary buffers");
41 
42 static struct cdev *sdev;
43 static d_ioctl_t vcio_ioctl;
44 
45 static struct cdevsw vcio_devsw = {
46 	/* version */	.d_version = D_VERSION,
47 	/* ioctl */	.d_ioctl = vcio_ioctl,
48 };
49 
50 #define VCIO_IOC_MAGIC 100
51 #define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
52 
53 int
54 vcio_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int mode,
55     struct thread *td)
56 {
57     int error;
58     void *ptr;
59     uint32_t size;
60     uint8_t *property;
61 
62     error = 0;
63     switch(cmd) {
64     case IOCTL_MBOX_PROPERTY:
65     	memcpy (&ptr, arg, sizeof(ptr));
66 	error = copyin(ptr, &size, sizeof(size));
67 
68 	if (error != 0)
69 		break;
70 	property = malloc(size, M_VCIO, M_WAITOK);
71 	if (property == NULL) {
72 		error = ENOMEM;
73 		break;
74 	}
75 
76 	error = copyin(ptr, property, size);
77 	if (error) {
78 		free(property, M_VCIO);
79 		break;
80 	}
81 
82 	error = bcm2835_mbox_property(property, size);
83 	if (error) {
84 		free(property, M_VCIO);
85 		break;
86 	}
87 
88 	error = copyout(property, ptr, size);
89 	free(property, M_VCIO);
90 
91 	break;
92     default:
93 	error = EINVAL;
94 	break;
95     }
96     return (error);
97 }
98 
99 static int
100 vcio_load(module_t mod, int cmd, void *arg)
101 {
102     int  err = 0;
103 
104     switch (cmd) {
105     case MOD_LOAD:
106 	sdev = make_dev(&vcio_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "vcio");
107 	break;
108 
109     case MOD_UNLOAD:
110 	destroy_dev(sdev);
111 	break;
112 
113     default:
114 	err = EOPNOTSUPP;
115 	break;
116     }
117 
118     return(err);
119 }
120 
121 DEV_MODULE(vcio, vcio_load, NULL);
122 MODULE_DEPEND(vcio, mbox, 1, 1, 1);
123