xref: /freebsd/sys/arm/broadcom/bcm2835/bcm2835_vcio.c (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
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 
72 	error = copyin(ptr, property, size);
73 	if (error) {
74 		free(property, M_VCIO);
75 		break;
76 	}
77 
78 	error = bcm2835_mbox_property(property, size);
79 	if (error) {
80 		free(property, M_VCIO);
81 		break;
82 	}
83 
84 	error = copyout(property, ptr, size);
85 	free(property, M_VCIO);
86 
87 	break;
88     default:
89 	error = EINVAL;
90 	break;
91     }
92     return (error);
93 }
94 
95 static int
96 vcio_load(module_t mod, int cmd, void *arg)
97 {
98     int  err = 0;
99 
100     switch (cmd) {
101     case MOD_LOAD:
102 	sdev = make_dev(&vcio_devsw, 0, UID_ROOT, GID_WHEEL, 0600, "vcio");
103 	break;
104 
105     case MOD_UNLOAD:
106 	destroy_dev(sdev);
107 	break;
108 
109     default:
110 	err = EOPNOTSUPP;
111 	break;
112     }
113 
114     return(err);
115 }
116 
117 DEV_MODULE(vcio, vcio_load, NULL);
118 MODULE_DEPEND(vcio, mbox, 1, 1, 1);
119