Initliaze error value in declaration
[eleutheria.git] / netbsd / cdev / mydev.c
blobc0984c459ed6890f43bea73c132269723de14ae4
1 #include <sys/param.h>
2 #include <sys/systm.h>
3 #include <sys/proc.h>
4 #include <sys/errno.h>
5 #include <sys/ioctl.h>
6 #include <sys/device.h>
7 #include <sys/conf.h>
8 #include <sys/malloc.h> /* for free(9) */
9 #include <sys/mydev.h>
10 #include <sys/kauth.h>
11 #include <sys/syslog.h>
12 #include <prop/proplib.h>
14 struct mydev_softc {
15 struct device mydev_dev;
18 /* Autoconfiguration glue */
19 void mydevattach(struct device *parent, struct device *self, void *aux);
20 int mydevopen(dev_t dev, int flags, int fmt, struct lwp *proc);
21 int mydevclose(dev_t dev, int flags, int fmt, struct lwp *proc);
22 int mydevioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
23 struct lwp *proc);
25 /* Just define the character dev handlers because that is all we need */
26 const struct cdevsw mydev_cdevsw = {
27 mydevopen,
28 mydevclose,
29 noread,
30 nowrite,
31 mydevioctl,
32 nostop,
33 notty,
34 nopoll,
35 nommap,
36 nokqfilter,
37 0 /* int d_type; */
40 /* Count of number of times device is open */
41 static unsigned int mydev_usage = 0;
44 * Attach for autoconfig to find.
46 void
47 mydevattach(struct device *parent, struct device *self, void *aux)
50 * This is where resources that need to be allocated/initialised
51 * before open is called can be set up.
53 mydev_usage = 0;
54 log(LOG_DEBUG, "mydev: pseudo-device attached\n");
58 * Handle an open request on the dev.
60 int
61 mydevopen(dev_t dev, int flags, int fmt, struct lwp *proc)
63 log(LOG_DEBUG, "mydev: pseudo-device open attempt by "
64 "uid=%u, pid=%u. (dev=%u)\n",
65 kauth_cred_geteuid(proc->l_cred), proc->l_proc->p_pid,
66 dev);
68 if (mydev_usage > 0) {
69 log(LOG_DEBUG, "mydev: pseudo-device already in use\n");
70 return EBUSY;
73 return 0; /* Success */
77 * Handle the close request for the dev.
79 int
80 mydevclose(dev_t dev, int flags, int fmt, struct lwp *proc)
82 if (mydev_usage > 0)
83 mydev_usage--;
85 log(LOG_DEBUG, "mydev: pseudo-device closed\n");
87 return 0;
91 * Handle the ioctl for the dev.
93 int
94 mydevioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
95 struct lwp *proc)
97 prop_dictionary_t dict;
98 prop_string_t ps;
99 struct mydev_params *params;
100 const struct plistref *pref;
101 int error = 0;
102 char *val;
104 switch (cmd) {
105 case MYDEVOLDIOCTL:
106 /* Pass data from userspace to kernel in the conventional way */
107 params = (struct mydev_params *)data;
108 log(LOG_DEBUG, "Got number of %d and string of %s\n",
109 params->number, params->string);
110 break;
112 case MYDEVSETPROPS:
113 /* Use proplib(3) for userspace/kernel communication */
114 pref = (const struct plistref *)data;
115 error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
116 if (error)
117 return error;
119 /* Print dict's count for debugging purposes */
120 log(LOG_DEBUG, "mydev: dict count = %u\n",
121 prop_dictionary_count(dict));
123 /* Retrieve object associated with "key" key */
124 ps = prop_dictionary_get(dict, "key");
125 if (ps == NULL || prop_object_type(ps) != PROP_TYPE_STRING) {
126 prop_object_release(dict);
127 log(LOG_DEBUG, "mydev: prop_dictionary_get() failed\n");
128 return -1;
131 /* Print data */
132 val = prop_string_cstring(ps);
133 prop_object_release(ps);
134 log(LOG_DEBUG, "<x, y> = (%s, %s)\n", "key", val);
135 free(val, M_TEMP);
137 /* Done */
138 prop_object_release(dict);
139 break;
141 default:
142 /* Invalid operation */
143 error = ENODEV;
146 return error;