oj6sh.c revision 1.1.4.2 1 1.1.4.2 rmind /* $NetBSD: oj6sh.c,v 1.1.4.2 2014/05/18 17:45:47 rmind Exp $ */
2 1.1.4.2 rmind
3 1.1.4.2 rmind /*
4 1.1.4.2 rmind * Copyright (c) 2014 Genetec Corporation. All rights reserved.
5 1.1.4.2 rmind * Written by Hashimoto Kenichi for Genetec Corporation.
6 1.1.4.2 rmind *
7 1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
8 1.1.4.2 rmind * modification, are permitted provided that the following conditions
9 1.1.4.2 rmind * are met:
10 1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
12 1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
15 1.1.4.2 rmind *
16 1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
17 1.1.4.2 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION
20 1.1.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
27 1.1.4.2 rmind */
28 1.1.4.2 rmind
29 1.1.4.2 rmind /*
30 1.1.4.2 rmind * Sharp NetWalker's Optical Joystick
31 1.1.4.2 rmind */
32 1.1.4.2 rmind
33 1.1.4.2 rmind #include <sys/cdefs.h>
34 1.1.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: oj6sh.c,v 1.1.4.2 2014/05/18 17:45:47 rmind Exp $");
35 1.1.4.2 rmind
36 1.1.4.2 rmind #include "opt_oj6sh.h"
37 1.1.4.2 rmind
38 1.1.4.2 rmind #include <sys/param.h>
39 1.1.4.2 rmind #include <sys/systm.h>
40 1.1.4.2 rmind #include <sys/kernel.h>
41 1.1.4.2 rmind #include <sys/device.h>
42 1.1.4.2 rmind #include <sys/lock.h>
43 1.1.4.2 rmind #include <sys/callout.h>
44 1.1.4.2 rmind #include <sys/bus.h>
45 1.1.4.2 rmind #include <sys/mutex.h>
46 1.1.4.2 rmind
47 1.1.4.2 rmind #include <dev/wscons/wsconsio.h>
48 1.1.4.2 rmind #include <dev/wscons/wsmousevar.h>
49 1.1.4.2 rmind #include <dev/wscons/wsdisplayvar.h>
50 1.1.4.2 rmind
51 1.1.4.2 rmind #include <dev/hpc/hpcfbio.h>
52 1.1.4.2 rmind #include <dev/hpc/hpctpanelvar.h>
53 1.1.4.2 rmind
54 1.1.4.2 rmind #include <dev/spi/spivar.h>
55 1.1.4.2 rmind
56 1.1.4.2 rmind #ifdef OJ6SH_DEBUG
57 1.1.4.2 rmind int oj6sh_debug = OJ6SH_DEBUG;
58 1.1.4.2 rmind #define DPRINTF(n,x) if (oj6sh_debug>(n)) printf x;
59 1.1.4.2 rmind #else
60 1.1.4.2 rmind #define DPRINTF(n,x)
61 1.1.4.2 rmind #endif
62 1.1.4.2 rmind
63 1.1.4.2 rmind #define POLLRATE (hz/30)
64 1.1.4.2 rmind
65 1.1.4.2 rmind /* register address */
66 1.1.4.2 rmind #define OJ6SH_PRODUCT 0x00
67 1.1.4.2 rmind #define OJ6SH_REVISION 0x01
68 1.1.4.2 rmind #define OJ6SH_MOTION 0x02
69 1.1.4.2 rmind #define OJ6SH_DELTA_X 0x03
70 1.1.4.2 rmind #define OJ6SH_DELTA_Y 0x04
71 1.1.4.2 rmind #define OJ6SH_SQUAL 0x05
72 1.1.4.2 rmind #define OJ6SH_SHUTTER 0x06
73 1.1.4.2 rmind #define OJ6SH_CONFIG 0x11
74 1.1.4.2 rmind #define OJ6SH_RESET 0x3a
75 1.1.4.2 rmind #define POWERON_RESET 0x5a
76 1.1.4.2 rmind #define OJ6SH_N_REVISION 0x3e
77 1.1.4.2 rmind #define OJ6SH_N_PRODUCT 0x3f
78 1.1.4.2 rmind
79 1.1.4.2 rmind struct oj6sh_softc {
80 1.1.4.2 rmind device_t sc_dev;
81 1.1.4.2 rmind
82 1.1.4.2 rmind struct spi_handle *sc_sh;
83 1.1.4.2 rmind struct callout sc_c;
84 1.1.4.2 rmind
85 1.1.4.2 rmind kmutex_t sc_lock;
86 1.1.4.2 rmind int sc_enabled;
87 1.1.4.2 rmind
88 1.1.4.2 rmind device_t sc_wsmousedev;
89 1.1.4.2 rmind };
90 1.1.4.2 rmind
91 1.1.4.2 rmind struct oj6sh_delta {
92 1.1.4.2 rmind int x;
93 1.1.4.2 rmind int y;
94 1.1.4.2 rmind };
95 1.1.4.2 rmind
96 1.1.4.2 rmind static uint8_t oj6sh_read(struct spi_handle *, uint8_t);
97 1.1.4.2 rmind static void oj6sh_write(struct spi_handle *, uint8_t, uint8_t);
98 1.1.4.2 rmind
99 1.1.4.2 rmind static int oj6sh_match(device_t , cfdata_t , void *);
100 1.1.4.2 rmind static void oj6sh_attach(device_t , device_t , void *);
101 1.1.4.2 rmind
102 1.1.4.2 rmind CFATTACH_DECL_NEW(oj6sh, sizeof(struct oj6sh_softc),
103 1.1.4.2 rmind oj6sh_match, oj6sh_attach, NULL, NULL);
104 1.1.4.2 rmind
105 1.1.4.2 rmind static bool oj6sh_motion(struct spi_handle *);
106 1.1.4.2 rmind static bool oj6sh_squal(struct spi_handle *);
107 1.1.4.2 rmind static bool oj6sh_shuttrer(struct spi_handle *);
108 1.1.4.2 rmind static int oj6sh_readdelta(struct spi_handle *, struct oj6sh_delta *);
109 1.1.4.2 rmind
110 1.1.4.2 rmind static void oj6sh_poll(void *);
111 1.1.4.2 rmind static int oj6sh_enable(void *v);
112 1.1.4.2 rmind static void oj6sh_disable(void *v);
113 1.1.4.2 rmind static int oj6sh_ioctl(void *, u_long, void *, int, struct lwp *);
114 1.1.4.2 rmind
115 1.1.4.2 rmind static bool oj6sh_resume(device_t, const pmf_qual_t *);
116 1.1.4.2 rmind static bool oj6sh_suspend(device_t, const pmf_qual_t *);
117 1.1.4.2 rmind
118 1.1.4.2 rmind static const struct wsmouse_accessops oj6sh_accessops = {
119 1.1.4.2 rmind .enable = oj6sh_enable,
120 1.1.4.2 rmind .ioctl = oj6sh_ioctl,
121 1.1.4.2 rmind .disable = oj6sh_disable
122 1.1.4.2 rmind };
123 1.1.4.2 rmind
124 1.1.4.2 rmind static int
125 1.1.4.2 rmind oj6sh_match(device_t parent, cfdata_t match, void *aux)
126 1.1.4.2 rmind {
127 1.1.4.2 rmind struct spi_attach_args *sa = aux;
128 1.1.4.2 rmind
129 1.1.4.2 rmind if (strcmp(match->cf_name, "oj6sh"))
130 1.1.4.2 rmind return 0;
131 1.1.4.2 rmind if (spi_configure(sa->sa_handle, SPI_MODE_0, 2500000))
132 1.1.4.2 rmind return 0;
133 1.1.4.2 rmind
134 1.1.4.2 rmind return 2;
135 1.1.4.2 rmind }
136 1.1.4.2 rmind
137 1.1.4.2 rmind static void
138 1.1.4.2 rmind oj6sh_doattach(device_t self)
139 1.1.4.2 rmind {
140 1.1.4.2 rmind struct oj6sh_softc *sc = device_private(self);
141 1.1.4.2 rmind uint8_t product;
142 1.1.4.2 rmind uint8_t rev;
143 1.1.4.2 rmind uint8_t product_inv;
144 1.1.4.2 rmind uint8_t rev_inv;
145 1.1.4.2 rmind
146 1.1.4.2 rmind /* reset */
147 1.1.4.2 rmind oj6sh_write(sc->sc_sh, OJ6SH_RESET, POWERON_RESET);
148 1.1.4.2 rmind delay(10000);
149 1.1.4.2 rmind
150 1.1.4.2 rmind /* resolution */
151 1.1.4.2 rmind oj6sh_write(sc->sc_sh, OJ6SH_CONFIG, 0x80);
152 1.1.4.2 rmind
153 1.1.4.2 rmind product = oj6sh_read(sc->sc_sh, OJ6SH_PRODUCT);
154 1.1.4.2 rmind rev = oj6sh_read(sc->sc_sh, OJ6SH_REVISION);
155 1.1.4.2 rmind product_inv = oj6sh_read(sc->sc_sh, OJ6SH_N_PRODUCT);
156 1.1.4.2 rmind rev_inv = oj6sh_read(sc->sc_sh, OJ6SH_N_REVISION);
157 1.1.4.2 rmind
158 1.1.4.2 rmind if (((product | product_inv) != 0xff) || ((rev | rev_inv) != 0xff)) {
159 1.1.4.2 rmind aprint_error_dev(self,
160 1.1.4.2 rmind "mismatch product (%02x:%02x), rev (%02x:%02x)\n",
161 1.1.4.2 rmind product, product_inv, rev, rev_inv);
162 1.1.4.2 rmind return;
163 1.1.4.2 rmind }
164 1.1.4.2 rmind
165 1.1.4.2 rmind aprint_normal("%s: id 0x%02x, revision 0x%02x\n",
166 1.1.4.2 rmind device_xname(sc->sc_dev), product, rev);
167 1.1.4.2 rmind
168 1.1.4.2 rmind return;
169 1.1.4.2 rmind }
170 1.1.4.2 rmind
171 1.1.4.2 rmind static void
172 1.1.4.2 rmind oj6sh_attach(device_t parent, device_t self, void *aux)
173 1.1.4.2 rmind {
174 1.1.4.2 rmind struct oj6sh_softc *sc = device_private(self);
175 1.1.4.2 rmind struct spi_attach_args *sa = aux;
176 1.1.4.2 rmind struct wsmousedev_attach_args a;
177 1.1.4.2 rmind
178 1.1.4.2 rmind aprint_normal(": OJ6SH-T25 Optical Joystick\n");
179 1.1.4.2 rmind
180 1.1.4.2 rmind mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
181 1.1.4.2 rmind
182 1.1.4.2 rmind sc->sc_dev = self;
183 1.1.4.2 rmind sc->sc_enabled = 0;
184 1.1.4.2 rmind
185 1.1.4.2 rmind callout_init(&sc->sc_c, 0);
186 1.1.4.2 rmind
187 1.1.4.2 rmind sc->sc_sh = sa->sa_handle;
188 1.1.4.2 rmind
189 1.1.4.2 rmind a.accessops = &oj6sh_accessops;
190 1.1.4.2 rmind a.accesscookie = sc;
191 1.1.4.2 rmind
192 1.1.4.2 rmind sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
193 1.1.4.2 rmind
194 1.1.4.2 rmind config_interrupts(self, oj6sh_doattach);
195 1.1.4.2 rmind }
196 1.1.4.2 rmind
197 1.1.4.2 rmind static void
198 1.1.4.2 rmind oj6sh_poll(void *arg)
199 1.1.4.2 rmind {
200 1.1.4.2 rmind struct oj6sh_softc *sc = (struct oj6sh_softc *)arg;
201 1.1.4.2 rmind struct oj6sh_delta delta = {0, 0};
202 1.1.4.2 rmind uint32_t buttons = 0;
203 1.1.4.2 rmind int s;
204 1.1.4.2 rmind int x, y;
205 1.1.4.2 rmind
206 1.1.4.2 rmind mutex_enter(&sc->sc_lock);
207 1.1.4.2 rmind
208 1.1.4.2 rmind if (oj6sh_motion(sc->sc_sh) == false)
209 1.1.4.2 rmind goto out;
210 1.1.4.2 rmind else if ((oj6sh_squal(sc->sc_sh) == true) &&
211 1.1.4.2 rmind (oj6sh_shuttrer(sc->sc_sh) == true))
212 1.1.4.2 rmind goto out;
213 1.1.4.2 rmind
214 1.1.4.2 rmind oj6sh_readdelta(sc->sc_sh, &delta);
215 1.1.4.2 rmind DPRINTF(3,("%s: x = %d, y = %d\n", device_xname(sc->sc_dev),
216 1.1.4.2 rmind delta.x, delta.y));
217 1.1.4.2 rmind
218 1.1.4.2 rmind #if defined(J6SH_DOWN_Y_LEFT_X)
219 1.1.4.2 rmind y = -delta.y;
220 1.1.4.2 rmind x = -delta.x;
221 1.1.4.2 rmind #elif defined(OJ6SH_UP_X_LEFT_Y)
222 1.1.4.2 rmind y = delta.x;
223 1.1.4.2 rmind x = -delta.y;
224 1.1.4.2 rmind #elif defined(OJ6SH_DOWN_X_RIGHT_Y)
225 1.1.4.2 rmind y = -delta.x;
226 1.1.4.2 rmind x = delta.y;
227 1.1.4.2 rmind #else /* OJ6SH_UP_Y_RIGHT_X */
228 1.1.4.2 rmind y = delta.y;
229 1.1.4.2 rmind x = delta.x;
230 1.1.4.2 rmind #endif
231 1.1.4.2 rmind s = spltty();
232 1.1.4.2 rmind wsmouse_input(sc->sc_wsmousedev, buttons, x, y, 0, 0,
233 1.1.4.2 rmind WSMOUSE_INPUT_DELTA);
234 1.1.4.2 rmind splx(s);
235 1.1.4.2 rmind out:
236 1.1.4.2 rmind mutex_exit(&sc->sc_lock);
237 1.1.4.2 rmind
238 1.1.4.2 rmind if (sc->sc_enabled)
239 1.1.4.2 rmind callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
240 1.1.4.2 rmind
241 1.1.4.2 rmind return;
242 1.1.4.2 rmind }
243 1.1.4.2 rmind
244 1.1.4.2 rmind static uint8_t
245 1.1.4.2 rmind oj6sh_read(struct spi_handle *spi, uint8_t reg)
246 1.1.4.2 rmind {
247 1.1.4.2 rmind uint8_t ret = 0;
248 1.1.4.2 rmind
249 1.1.4.2 rmind spi_send_recv(spi, 1, ®, 1, &ret);
250 1.1.4.2 rmind DPRINTF(4,("%s: 0x%02x = 0x%02x\n", __func__, reg, ret));
251 1.1.4.2 rmind return ret;
252 1.1.4.2 rmind }
253 1.1.4.2 rmind
254 1.1.4.2 rmind static void
255 1.1.4.2 rmind oj6sh_write(struct spi_handle *spi, uint8_t reg, uint8_t val)
256 1.1.4.2 rmind {
257 1.1.4.2 rmind uint8_t tmp[2] = {reg | 0x80, val};
258 1.1.4.2 rmind
259 1.1.4.2 rmind spi_send(spi, 2, tmp);
260 1.1.4.2 rmind DPRINTF(4,("%s: 0x%02x = 0x%02x\n", __func__, reg, val));
261 1.1.4.2 rmind return;
262 1.1.4.2 rmind }
263 1.1.4.2 rmind
264 1.1.4.2 rmind static bool
265 1.1.4.2 rmind oj6sh_motion(struct spi_handle *spi)
266 1.1.4.2 rmind {
267 1.1.4.2 rmind uint16_t motion;
268 1.1.4.2 rmind motion = oj6sh_read(spi, OJ6SH_MOTION);
269 1.1.4.2 rmind return (motion & __BIT(7) ? true : false);
270 1.1.4.2 rmind }
271 1.1.4.2 rmind
272 1.1.4.2 rmind static bool
273 1.1.4.2 rmind oj6sh_squal(struct spi_handle *spi)
274 1.1.4.2 rmind {
275 1.1.4.2 rmind uint16_t squal;
276 1.1.4.2 rmind squal = oj6sh_read(spi, OJ6SH_SQUAL);
277 1.1.4.2 rmind return (squal < 25 ? true : false);
278 1.1.4.2 rmind }
279 1.1.4.2 rmind
280 1.1.4.2 rmind static bool
281 1.1.4.2 rmind oj6sh_shuttrer(struct spi_handle *spi)
282 1.1.4.2 rmind {
283 1.1.4.2 rmind uint16_t shutter;
284 1.1.4.2 rmind shutter = oj6sh_read(spi, OJ6SH_SHUTTER) << 8;
285 1.1.4.2 rmind shutter |= oj6sh_read(spi, OJ6SH_SHUTTER + 1);
286 1.1.4.2 rmind return (shutter > 600 ? true : false);
287 1.1.4.2 rmind }
288 1.1.4.2 rmind
289 1.1.4.2 rmind static int
290 1.1.4.2 rmind oj6sh_readdelta(struct spi_handle *spi, struct oj6sh_delta *delta)
291 1.1.4.2 rmind {
292 1.1.4.2 rmind delta->x = (int8_t)oj6sh_read(spi, OJ6SH_DELTA_X);
293 1.1.4.2 rmind delta->y = (int8_t)oj6sh_read(spi, OJ6SH_DELTA_Y);
294 1.1.4.2 rmind return 0;
295 1.1.4.2 rmind }
296 1.1.4.2 rmind
297 1.1.4.2 rmind int
298 1.1.4.2 rmind oj6sh_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
299 1.1.4.2 rmind {
300 1.1.4.2 rmind struct wsmouse_id *id;
301 1.1.4.2 rmind
302 1.1.4.2 rmind switch (cmd) {
303 1.1.4.2 rmind case WSMOUSEIO_GTYPE:
304 1.1.4.2 rmind *(u_int *)data = WSMOUSE_TYPE_PS2;
305 1.1.4.2 rmind return 0;
306 1.1.4.2 rmind case WSMOUSEIO_GETID:
307 1.1.4.2 rmind id = (struct wsmouse_id *)data;
308 1.1.4.2 rmind if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
309 1.1.4.2 rmind return EINVAL;
310 1.1.4.2 rmind strlcpy(id->data, "OJ6SH-T25", WSMOUSE_ID_MAXLEN);
311 1.1.4.2 rmind id->length = strlen(id->data);
312 1.1.4.2 rmind return 0;
313 1.1.4.2 rmind }
314 1.1.4.2 rmind
315 1.1.4.2 rmind return EPASSTHROUGH;
316 1.1.4.2 rmind }
317 1.1.4.2 rmind
318 1.1.4.2 rmind int
319 1.1.4.2 rmind oj6sh_enable(void *v)
320 1.1.4.2 rmind {
321 1.1.4.2 rmind struct oj6sh_softc *sc = (struct oj6sh_softc *)v;
322 1.1.4.2 rmind
323 1.1.4.2 rmind DPRINTF(3,("%s: oj6sh_enable()\n", device_xname(sc->sc_dev)));
324 1.1.4.2 rmind if (sc->sc_enabled) {
325 1.1.4.2 rmind DPRINTF(3,("%s: already enabled\n", device_xname(sc->sc_dev)));
326 1.1.4.2 rmind return EBUSY;
327 1.1.4.2 rmind }
328 1.1.4.2 rmind
329 1.1.4.2 rmind if (!pmf_device_register(sc->sc_dev, oj6sh_suspend, oj6sh_resume))
330 1.1.4.2 rmind aprint_error_dev(sc->sc_dev, "couldn't establish power handler\n");
331 1.1.4.2 rmind
332 1.1.4.2 rmind sc->sc_enabled = 1;
333 1.1.4.2 rmind callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
334 1.1.4.2 rmind
335 1.1.4.2 rmind return 0;
336 1.1.4.2 rmind }
337 1.1.4.2 rmind
338 1.1.4.2 rmind void
339 1.1.4.2 rmind oj6sh_disable(void *v)
340 1.1.4.2 rmind {
341 1.1.4.2 rmind struct oj6sh_softc *sc = (struct oj6sh_softc *)v;
342 1.1.4.2 rmind
343 1.1.4.2 rmind DPRINTF(3,("%s: oj6sh_disable()\n", device_xname(sc->sc_dev)));
344 1.1.4.2 rmind if (!sc->sc_enabled) {
345 1.1.4.2 rmind DPRINTF(3,("%s: already disabled()\n", device_xname(sc->sc_dev)));
346 1.1.4.2 rmind return;
347 1.1.4.2 rmind }
348 1.1.4.2 rmind
349 1.1.4.2 rmind pmf_device_deregister(sc->sc_dev);
350 1.1.4.2 rmind
351 1.1.4.2 rmind sc->sc_enabled = 0;
352 1.1.4.2 rmind
353 1.1.4.2 rmind return;
354 1.1.4.2 rmind }
355 1.1.4.2 rmind
356 1.1.4.2 rmind static bool
357 1.1.4.2 rmind oj6sh_suspend(device_t dv, const pmf_qual_t *qual)
358 1.1.4.2 rmind {
359 1.1.4.2 rmind struct oj6sh_softc *sc = device_private(dv);
360 1.1.4.2 rmind
361 1.1.4.2 rmind DPRINTF(3,("%s: oj6sh_suspend()\n", device_xname(sc->sc_dev)));
362 1.1.4.2 rmind callout_stop(&sc->sc_c);
363 1.1.4.2 rmind sc->sc_enabled = 0;
364 1.1.4.2 rmind
365 1.1.4.2 rmind return true;
366 1.1.4.2 rmind }
367 1.1.4.2 rmind
368 1.1.4.2 rmind static bool
369 1.1.4.2 rmind oj6sh_resume(device_t dv, const pmf_qual_t *qual)
370 1.1.4.2 rmind {
371 1.1.4.2 rmind struct oj6sh_softc *sc = device_private(dv);
372 1.1.4.2 rmind
373 1.1.4.2 rmind DPRINTF(3,("%s: oj6sh_resume()\n", device_xname(sc->sc_dev)));
374 1.1.4.2 rmind sc->sc_enabled = 1;
375 1.1.4.2 rmind callout_reset(&sc->sc_c, POLLRATE, oj6sh_poll, sc);
376 1.1.4.2 rmind
377 1.1.4.2 rmind return true;
378 1.1.4.2 rmind }
379 1.1.4.2 rmind
380