Tweak some comments
[eleutheria.git] / netbsd / cdev / mydev.c
blob32885f03be5eaccec05aba7930e15d3619dbcb59
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/mydev.h>
9 #include <prop/proplib.h>
11 struct mydev_softc {
12 struct device mydev_dev;
15 /* Autoconfiguration glue */
16 void mydevattach(struct device *parent, struct device *self, void *aux);
17 int mydevopen(dev_t dev, int flags, int fmt, struct lwp *process);
18 int mydevclose(dev_t dev, int flags, int fmt, struct lwp *process);
19 int mydevioctl(dev_t dev, u_long cmd, caddr_t data,
20 int flags, struct lwp *process);
22 /* Just define the character dev handlers because that is all we need */
23 const struct cdevsw mydev_cdevsw = {
24 mydevopen,
25 mydevclose,
26 noread,
27 nowrite,
28 mydevioctl,
29 nostop,
30 notty,
31 nopoll,
32 nommap,
33 nokqfilter,
34 0 /* int d_type; */
38 * Attach for autoconfig to find.
40 void
41 mydevattach(struct device *parent, struct device *self, void *aux)
44 * Nothing to do for mydev.
45 * This is where resources that need to be allocated/initialised
46 * before open is called can be set up.
51 * Handle an open request on the dev.
53 int
54 mydevopen(dev_t dev, int flags, int fmt, struct lwp *process)
56 return 0; /* This always succeeds */
60 * Handle the close request for the dev.
62 int
63 mydevclose(dev_t dev, int flags, int fmt, struct lwp *process)
65 return 0; /* Again this always succeeds */
69 * Handle the ioctl for the dev.
71 int
72 mydevioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
73 struct lwp *process)
75 prop_dictionary_t dict;
76 prop_string_t ps;
77 struct mydev_params *params;
78 const struct plistref *pref;
79 int error;
80 char *val;
82 error = 0;
83 switch (cmd) {
84 case MYDEVTEST:
85 /* Pass data from userspace to kernel in the conventional way */
86 params = (struct mydev_params *)data;
87 printf("Got number of %d and string of %s\n",
88 params->number, params->string);
89 break;
91 case MYDEVSETPROPS:
92 /* Use proplib(3) for userspace/kernel communication */
93 pref = (const struct plistref *)data;
94 error = prop_dictionary_copyin_ioctl(pref, cmd, &dict);
95 if (error)
96 return error;
98 /* Print dict's count for debugging purposes */
99 printf("count = %u\n", prop_dictionary_count(dict));
101 /* Retrieve object associated with "key" key */
102 ps = prop_dictionary_get(dict, "key");
103 if (ps == NULL) {
104 prop_object_release(dict);
105 printf("prop_dictionary_get()\n");
106 return -1;
109 /* Print data */
110 val = prop_string_cstring(ps);
111 prop_object_release(ps);
112 printf("<key, val> = (%s, %s)\n", "key", val == NULL ? "null" : val);
114 /* Done */
115 prop_object_release(dict);
116 break;
118 default:
119 /* Invalid operation */
120 error = ENODEV;
123 return error;