readhappy.c revision 1.1.2.2 1 1.1.2.2 snj /* $NetBSD: readhappy.c,v 1.1.2.2 2015/06/03 06:51:40 snj Exp $ */
2 1.1.2.2 snj
3 1.1.2.2 snj /*-
4 1.1.2.2 snj * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 1.1.2.2 snj * All rights reserved.
6 1.1.2.2 snj *
7 1.1.2.2 snj * Redistribution and use in source and binary forms, with or without
8 1.1.2.2 snj * modification, are permitted provided that the following conditions
9 1.1.2.2 snj * are met:
10 1.1.2.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.1.2.2 snj * notice, this list of conditions and the following disclaimer.
12 1.1.2.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.2.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.1.2.2 snj * documentation and/or other materials provided with the distribution.
15 1.1.2.2 snj *
16 1.1.2.2 snj * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1.2.2 snj * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.2.2 snj * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.2.2 snj * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1.2.2 snj * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.2.2 snj * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.2.2 snj * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.2.2 snj * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.2.2 snj * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.2.2 snj * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.2.2 snj * POSSIBILITY OF SUCH DAMAGE.
27 1.1.2.2 snj */
28 1.1.2.2 snj
29 1.1.2.2 snj #include <sys/cdefs.h>
30 1.1.2.2 snj __KERNEL_RCSID(0, "$NetBSD: readhappy.c,v 1.1.2.2 2015/06/03 06:51:40 snj Exp $");
31 1.1.2.2 snj
32 1.1.2.2 snj #include <sys/param.h>
33 1.1.2.2 snj #include <sys/conf.h>
34 1.1.2.2 snj #include <sys/device.h>
35 1.1.2.2 snj #include <sys/kernel.h>
36 1.1.2.2 snj #include <sys/module.h>
37 1.1.2.2 snj
38 1.1.2.2 snj /*
39 1.1.2.2 snj * Create a device /dev/happy from which you can read sequential
40 1.1.2.2 snj * happy numbers.
41 1.1.2.2 snj *
42 1.1.2.2 snj * To use this device you need to do:
43 1.1.2.2 snj * mknod /dev/happy c 210 0
44 1.1.2.2 snj *
45 1.1.2.2 snj * Commentary:
46 1.1.2.2 snj * A happy number is a number defined by the following process: Starting with
47 1.1.2.2 snj * any positive integer, replace the number by the sum of the squares of its
48 1.1.2.2 snj * digits, and repeat the process until the number equals 1 (where it will
49 1.1.2.2 snj * stay), or it loops endlessly in a cycle which does not include 1. Those
50 1.1.2.2 snj * numbers for which this process ends in 1 are happy numbers, while those that
51 1.1.2.2 snj * do not end in 1 are unhappy numbers (or sad numbers).
52 1.1.2.2 snj *
53 1.1.2.2 snj * For more information on happy numbers, and the algorithms, see
54 1.1.2.2 snj * http://en.wikipedia.org/wiki/Happy_number
55 1.1.2.2 snj *
56 1.1.2.2 snj * The happy number generator is here only to have something that the user
57 1.1.2.2 snj * can read from our device. Any other arbitrary data generator could
58 1.1.2.2 snj * have been used. The algorithm is not critical to the implementation
59 1.1.2.2 snj * of the module.
60 1.1.2.2 snj */
61 1.1.2.2 snj
62 1.1.2.2 snj
63 1.1.2.2 snj #define HAPPY_NUMBER 1
64 1.1.2.2 snj
65 1.1.2.2 snj /* If n is not happy then its sequence ends in the cycle:
66 1.1.2.2 snj * 4, 16, 37, 58, 89, 145, 42, 20, 4, ... */
67 1.1.2.2 snj #define SAD_NUMBER 4
68 1.1.2.2 snj
69 1.1.2.2 snj /* Calculate the sum of the squares of the digits of n */
70 1.1.2.2 snj static unsigned
71 1.1.2.2 snj dsum(unsigned n)
72 1.1.2.2 snj {
73 1.1.2.2 snj unsigned sum, x;
74 1.1.2.2 snj for (sum = 0; n; n /= 10) {
75 1.1.2.2 snj x = n % 10;
76 1.1.2.2 snj sum += x * x;
77 1.1.2.2 snj }
78 1.1.2.2 snj return sum;
79 1.1.2.2 snj }
80 1.1.2.2 snj
81 1.1.2.2 snj static int
82 1.1.2.2 snj check_happy(unsigned n)
83 1.1.2.2 snj {
84 1.1.2.2 snj for (;;) {
85 1.1.2.2 snj unsigned total = dsum(n);
86 1.1.2.2 snj
87 1.1.2.2 snj if (total == HAPPY_NUMBER)
88 1.1.2.2 snj return 1;
89 1.1.2.2 snj if (total == SAD_NUMBER)
90 1.1.2.2 snj return 0;
91 1.1.2.2 snj
92 1.1.2.2 snj n = total;
93 1.1.2.2 snj }
94 1.1.2.2 snj }
95 1.1.2.2 snj
96 1.1.2.2 snj dev_type_open(happy_open);
97 1.1.2.2 snj dev_type_close(happy_close);
98 1.1.2.2 snj dev_type_read(happy_read);
99 1.1.2.2 snj
100 1.1.2.2 snj static struct cdevsw happy_cdevsw = {
101 1.1.2.2 snj .d_open = happy_open,
102 1.1.2.2 snj .d_close = happy_close,
103 1.1.2.2 snj .d_read = happy_read,
104 1.1.2.2 snj .d_write = nowrite,
105 1.1.2.2 snj .d_ioctl = noioctl,
106 1.1.2.2 snj .d_stop = nostop,
107 1.1.2.2 snj .d_tty = notty,
108 1.1.2.2 snj .d_poll = nopoll,
109 1.1.2.2 snj .d_mmap = nommap,
110 1.1.2.2 snj .d_kqfilter = nokqfilter,
111 1.1.2.2 snj .d_discard = nodiscard,
112 1.1.2.2 snj .d_flag = D_OTHER
113 1.1.2.2 snj };
114 1.1.2.2 snj
115 1.1.2.2 snj
116 1.1.2.2 snj struct happy_softc {
117 1.1.2.2 snj int refcnt;
118 1.1.2.2 snj unsigned last;
119 1.1.2.2 snj };
120 1.1.2.2 snj
121 1.1.2.2 snj static struct happy_softc sc;
122 1.1.2.2 snj
123 1.1.2.2 snj int
124 1.1.2.2 snj happy_open(dev_t self __unused, int flag __unused, int mode __unused,
125 1.1.2.2 snj struct lwp *l __unused)
126 1.1.2.2 snj {
127 1.1.2.2 snj if (sc.refcnt > 0)
128 1.1.2.2 snj return EBUSY;
129 1.1.2.2 snj
130 1.1.2.2 snj sc.last = 0;
131 1.1.2.2 snj ++sc.refcnt;
132 1.1.2.2 snj
133 1.1.2.2 snj return 0;
134 1.1.2.2 snj }
135 1.1.2.2 snj
136 1.1.2.2 snj int
137 1.1.2.2 snj happy_close(dev_t self __unused, int flag __unused, int mode __unused,
138 1.1.2.2 snj struct lwp *l __unused)
139 1.1.2.2 snj {
140 1.1.2.2 snj --sc.refcnt;
141 1.1.2.2 snj
142 1.1.2.2 snj return 0;
143 1.1.2.2 snj }
144 1.1.2.2 snj
145 1.1.2.2 snj int
146 1.1.2.2 snj happy_read(dev_t self __unused, struct uio *uio, int flags __unused)
147 1.1.2.2 snj {
148 1.1.2.2 snj char line[80];
149 1.1.2.2 snj
150 1.1.2.2 snj /* Get next happy number */
151 1.1.2.2 snj while (check_happy(++sc.last) == 0)
152 1.1.2.2 snj continue;
153 1.1.2.2 snj
154 1.1.2.2 snj /* Print it into line[] with trailing \n */
155 1.1.2.2 snj int len = snprintf(line, sizeof(line), "%u\n", sc.last);
156 1.1.2.2 snj
157 1.1.2.2 snj /* Is there room? */
158 1.1.2.2 snj if (uio->uio_resid < len) {
159 1.1.2.2 snj --sc.last; /* Step back */
160 1.1.2.2 snj return EINVAL;
161 1.1.2.2 snj }
162 1.1.2.2 snj
163 1.1.2.2 snj /* Send it to User-Space */
164 1.1.2.2 snj int e;
165 1.1.2.2 snj if ((e = uiomove(line, len, uio)))
166 1.1.2.2 snj return e;
167 1.1.2.2 snj
168 1.1.2.2 snj return 0;
169 1.1.2.2 snj }
170 1.1.2.2 snj
171 1.1.2.2 snj MODULE(MODULE_CLASS_MISC, happy, NULL);
172 1.1.2.2 snj
173 1.1.2.2 snj static int
174 1.1.2.2 snj happy_modcmd(modcmd_t cmd, void *arg __unused)
175 1.1.2.2 snj {
176 1.1.2.2 snj /* The major should be verified and changed if needed to avoid
177 1.1.2.2 snj * conflicts with other devices. */
178 1.1.2.2 snj int cmajor = 210, bmajor = -1;
179 1.1.2.2 snj
180 1.1.2.2 snj switch (cmd) {
181 1.1.2.2 snj case MODULE_CMD_INIT:
182 1.1.2.2 snj if (devsw_attach("happy", NULL, &bmajor, &happy_cdevsw,
183 1.1.2.2 snj &cmajor))
184 1.1.2.2 snj return ENXIO;
185 1.1.2.2 snj return 0;
186 1.1.2.2 snj case MODULE_CMD_FINI:
187 1.1.2.2 snj if (sc.refcnt > 0)
188 1.1.2.2 snj return EBUSY;
189 1.1.2.2 snj
190 1.1.2.2 snj devsw_detach(NULL, &happy_cdevsw);
191 1.1.2.2 snj return 0;
192 1.1.2.2 snj default:
193 1.1.2.2 snj return ENOTTY;
194 1.1.2.2 snj }
195 1.1.2.2 snj }
196