AMD64 - Refactor uio_resid and size_t assumptions.
[dragonfly.git] / sys / dev / misc / spic / spic.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2000 Nick Sayer
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * spic -- the Sony Programmable I/O Controller
27 *
28 * This device exists on most recent Sony laptops. It is the means by which
29 * you can watch the Jog Dial and some other functions.
30 *
31 * At the moment, this driver merely tries to turn the jog dial into a
32 * device that moused can park on, with the intent of supplying a Z axis
33 * and mouse button out of the jog dial. I suspect that this device will
34 * end up having to support at least 2 different minor devices: One to be
35 * the jog wheel device for moused to camp out on and the other to perform
36 * all of the other miscelaneous functions of this device. But for now,
37 * the jog wheel is all you get.
38 *
39 * At the moment, the data sent back by the device is rather primitive.
40 * It sends a single character per event:
41 * u = up, d = down -- that's the jog button
42 * l = left, r = right -- that's the dial.
43 * "left" and "right" are rather caprecious. They actually represent
44 * ccw and cw, respectively
45 *
46 * What documentation exists is thanks to Andrew Tridge, and his page at
47 * http://samba.org/picturebook/ Special thanks also to Ian Dowse, who
48 * also provided sample code upon which this driver was based.
49 *
50 * $FreeBSD: src/sys/i386/isa/spic.c,v 1.4.2.1 2002/04/15 00:52:12 will Exp $
21ce0dfa 51 * $DragonFly: src/sys/dev/misc/spic/spic.c,v 1.17 2008/08/02 01:14:42 dillon Exp $
984263bc
MD
52 */
53
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/kernel.h>
57#include <sys/bus.h>
984263bc 58#include <sys/rman.h>
984263bc 59#include <sys/poll.h>
984263bc
MD
60#include <sys/tty.h>
61#include <sys/conf.h>
62#include <sys/fcntl.h>
63#include <sys/dkstat.h>
64#include <sys/malloc.h>
65#include <sys/sysctl.h>
66#include <sys/uio.h>
67#include <sys/proc.h>
514c0755 68#include <sys/thread2.h>
984263bc 69
1f7ab7c9
MD
70#include <bus/isa/isavar.h>
71
21ce0dfa 72#include <bus/pci/pci_cfgreg.h>
1f7ab7c9
MD
73#include <machine/clock.h>
74
1f2de5d4 75#include "spicreg.h"
984263bc
MD
76
77static int spic_pollrate;
78
79SYSCTL_INT(_machdep, OID_AUTO, spic_pollrate, CTLFLAG_RW, &spic_pollrate, 0, "")
80;
81
82devclass_t spic_devclass;
83
84static d_open_t spicopen;
85static d_close_t spicclose;
86static d_read_t spicread;
87static d_ioctl_t spicioctl;
88static d_poll_t spicpoll;
89
fef8985e
MD
90static struct dev_ops spic_ops = {
91 { "spic", CDEV_MAJOR, 0 },
92 .d_open = spicopen,
93 .d_close = spicclose,
94 .d_read = spicread,
95 .d_ioctl = spicioctl,
96 .d_poll = spicpoll,
984263bc
MD
97};
98
99#define SCBUFLEN 128
100
101struct spic_softc {
102 u_short sc_port_addr;
103 u_char sc_intr;
104 struct resource *sc_port_res,*sc_intr_res;
105 int sc_port_rid,sc_intr_rid;
106 int sc_opened;
107 int sc_sleeping;
108 int sc_buttonlast;
726b8254 109 struct callout sc_timeout_ch;
984263bc
MD
110 device_t sc_dev;
111 struct selinfo sc_rsel;
112 u_char sc_buf[SCBUFLEN];
113 int sc_count;
114 int sc_model;
115};
116
117static void
118write_port1(struct spic_softc *sc, u_char val)
119{
120 DELAY(10);
121 outb(sc->sc_port_addr, val);
122}
123
124static void
125write_port2(struct spic_softc *sc, u_char val)
126{
127 DELAY(10);
128 outb(sc->sc_port_addr + 4, val);
129}
130
131static u_char
132read_port1(struct spic_softc *sc)
133{
134 DELAY(10);
135 return inb(sc->sc_port_addr);
136}
137
138static u_char
139read_port2(struct spic_softc *sc)
140{
141 DELAY(10);
142 return inb(sc->sc_port_addr + 4);
143}
144
145static u_char
146read_port_cst(struct spic_softc *sc)
147{
148 DELAY(10);
149 return inb(SPIC_CST_IOPORT);
150}
151
152static void
153busy_wait(struct spic_softc *sc)
154{
155 int i=0;
156
157 while(read_port2(sc) & 2) {
158 DELAY(10);
159 if (i++>10000) {
e3869ec7 160 kprintf("spic busy wait abort\n");
984263bc
MD
161 return;
162 }
163 }
164}
165
166static void
167busy_wait_cst(struct spic_softc *sc, int mask)
168{
169 int i=0;
170
171 while(read_port_cst(sc) & mask) {
172 DELAY(10);
173 if (i++>10000) {
e3869ec7 174 kprintf("spic busy wait abort\n");
984263bc
MD
175 return;
176 }
177 }
178}
179
180static u_char
181spic_call1(struct spic_softc *sc, u_char dev) {
182 busy_wait(sc);
183 write_port2(sc, dev);
184 read_port2(sc);
185 return read_port1(sc);
186}
187
188static u_char
189spic_call2(struct spic_softc *sc, u_char dev, u_char fn)
190{
191 busy_wait(sc);
192 write_port2(sc, dev);
193 busy_wait(sc);
194 write_port1(sc, fn);
195 return read_port1(sc);
196}
197
198static void
199spic_ecrset(struct spic_softc *sc, u_int16_t addr, u_int16_t value)
200{
201 busy_wait_cst(sc, 3);
202 outb(SPIC_CST_IOPORT, 0x81);
203 busy_wait_cst(sc, 2);
204 outb(SPIC_DATA_IOPORT, addr);
205 busy_wait_cst(sc, 2);
206 outb(SPIC_DATA_IOPORT, value);
207 busy_wait_cst(sc, 2);
208}
209
210static void
211spic_type2_srs(struct spic_softc *sc)
212{
213 spic_ecrset(sc, SPIC_SHIB, (sc->sc_port_addr & 0xFF00) >> 8);
214 spic_ecrset(sc, SPIC_SLOB, sc->sc_port_addr & 0x00FF);
215 spic_ecrset(sc, SPIC_SIRQ, 0x00); /* using polling mode (IRQ=0)*/
216 DELAY(10);
217}
218
219static int
220spic_probe(device_t dev)
221{
222 struct spic_softc *sc;
223 u_char t, spic_irq;
224
225 sc = device_get_softc(dev);
226
227 /*
228 * We can only have 1 of these. Attempting to probe for a unit 1
229 * will destroy the work we did for unit 0
230 */
231 if (device_get_unit(dev))
232 return ENXIO;
233
234
235 bzero(sc, sizeof(struct spic_softc));
236
237 if (!(sc->sc_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
238 &sc->sc_port_rid, 0, ~0, 5, RF_ACTIVE))) {
239 device_printf(dev,"Couldn't map I/O\n");
240 return ENXIO;
241 }
242 sc->sc_port_addr = (u_short)rman_get_start(sc->sc_port_res);
243
244#ifdef notyet
245 if (!(sc->sc_intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
246 &sc->sc_intr_rid, 0, ~0, 1, RF_ACTIVE))) {
247 device_printf(dev,"Couldn't map IRQ\n");
248 bus_release_resource(dev, SYS_RES_IOPORT,
249 sc->sc_port_rid, sc->sc_port_res);
250 return ENXIO;
251 }
252 sc->sc_intr = (u_short)rman_get_start(sc->sc_intr_res);
253
254 switch (sc->sc_intr) {
255 case 0: spic_irq = 3; break;
256 case 5: spic_irq = 0; break;
257 case 0xa: spic_irq = 1; break;
258 case 0xb: spic_irq = 2; break;
259 default: device_printf(dev,"Invalid IRQ\n");
260 bus_release_resource(dev, SYS_RES_IOPORT,
261 sc->sc_port_rid, sc->sc_port_res);
262 bus_release_resource(dev, SYS_RES_IRQ,
263 sc->sc_intr_rid, sc->sc_intr_res);
264 return ENXIO;
265 }
266#else
267 spic_irq = 3;
268#endif
269
270#if 0
271 if (sc->sc_port_addr != 0x10A0) {
272 bus_release_resource(dev, SYS_RES_IOPORT,
273 sc->sc_port_rid, sc->sc_port_res);
274 bus_release_resource(dev, SYS_RES_IRQ,
275 sc->sc_intr_rid, sc->sc_intr_res);
276 return ENXIO;
277 }
278#endif
279
280 /* PIIX4 chipset at least? */
281 if (pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, 0, 4) ==
282 PIIX4_DEVID) {
283 sc->sc_model = SPIC_DEVICE_MODEL_TYPE1;
284 } else {
285 /* For newer VAIOs (R505, SRX7, ...) */
286 sc->sc_model = SPIC_DEVICE_MODEL_TYPE2;
287 }
288
289 /*
290 * This is an ugly hack. It is necessary until ACPI works correctly.
291 *
292 * The SPIC consists of 2 registers. They are mapped onto I/O by the
293 * PIIX4's General Device 10 function. There is also an interrupt
294 * control port at a somewhat magic location, but this first pass is
295 * polled.
296 *
297 * So the first thing we need to do is map the G10 space in.
298 *
299 */
300
301 /* Enable ACPI mode to get Fn key events */
302 /* XXX This may slow down your VAIO if ACPI is not supported in the kernel.
303 outb(0xb2, 0xf0);
304 */
305
306 device_printf(dev,"device model type = %d\n", sc->sc_model);
307
308 if(sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
309 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10A,
310 sc->sc_port_addr, 2);
311 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
312 t &= 0xf0;
313 t |= 4;
314 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
315 outw(SPIC_IRQ_PORT, (inw(SPIC_IRQ_PORT) & ~(0x3 << SPIC_IRQ_SHIFT)) | (spic_irq << SPIC_IRQ_SHIFT));
316 t = pci_cfgregread(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, 1);
317 t &= 0x1f;
318 t |= 0xc0;
319 pci_cfgregwrite(PIIX4_BUS, PIIX4_SLOT, PIIX4_FUNC, G10L, t, 1);
320 } else {
321 spic_type2_srs(sc);
322 }
323
324 /*
325 * XXX: Should try and see if there's anything actually there.
326 */
327
328 device_set_desc(dev, "Sony Programmable I/O Controller");
329
330 return 0;
331}
332
333static int
334spic_attach(device_t dev)
335{
336 struct spic_softc *sc;
337
338 sc = device_get_softc(dev);
339
340 sc->sc_dev = dev;
726b8254 341 callout_init(&sc->sc_timeout_ch);
984263bc
MD
342
343 spic_pollrate = (hz/50); /* Every 50th of a second */
344
345 spic_call1(sc, 0x82);
346 spic_call2(sc, 0x81, 0xff);
347 spic_call1(sc, 0x92);
348
349 /* There can be only one */
fef8985e 350 make_dev(&spic_ops, device_get_unit(dev), 0, 0, 0600, "jogdial");
984263bc
MD
351
352 return 0;
353}
354
355static void
356spictimeout(void *arg)
357{
358 struct spic_softc *sc = arg;
359 u_char b, event, param;
360 int j;
361
362 if (!sc->sc_opened) {
363 device_printf(sc->sc_dev, "timeout called while closed!\n");
364 return;
365 }
366
367 event = read_port2(sc);
368 param = read_port1(sc);
369
370 if ((event != 4) && (!(event & 0x1)))
371 switch(event) {
372 case 0x10: /* jog wheel event (type1) */
373 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE1) {
374 b = !!(param & 0x40);
375 if (b != sc->sc_buttonlast) {
376 sc->sc_buttonlast = b;
377 sc->sc_buf[sc->sc_count++] =
378 b?'d':'u';
379 }
380 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
381 if (j<0)
382 while(j++!=0) {
383 sc->sc_buf[sc->sc_count++] =
384 'l';
385 }
386 else if (j>0)
387 while(j--!=0) {
388 sc->sc_buf[sc->sc_count++] =
389 'r';
390 }
391 }
392 break;
393 case 0x08: /* jog wheel event (type2) */
394 case 0x00:
395 /* SPIC_DEVICE_MODEL_TYPE2 returns jog wheel event=0x00 */
396 if (sc->sc_model == SPIC_DEVICE_MODEL_TYPE2) {
397 b = !!(param & 0x40);
398 if (b != sc->sc_buttonlast) {
399 sc->sc_buttonlast = b;
400 sc->sc_buf[sc->sc_count++] =
401 b?'d':'u';
402 }
403 j = (param & 0xf) | ((param & 0x10)? ~0xf:0);
404 if (j<0)
405 while(j++!=0) {
406 sc->sc_buf[sc->sc_count++] =
407 'l';
408 }
409 else if (j>0)
410 while(j--!=0) {
411 sc->sc_buf[sc->sc_count++] =
412 'r';
413 }
414 }
415 break;
416 case 0x60: /* Capture button */
e3869ec7 417 kprintf("Capture button event: %x\n",param);
984263bc
MD
418 break;
419 case 0x30: /* Lid switch */
e3869ec7 420 kprintf("Lid switch event: %x\n",param);
984263bc
MD
421 break;
422 default:
e3869ec7 423 kprintf("Unknown event: event %02x param %02x\n", event, param);
984263bc
MD
424 break;
425 }
426 else {
427 /* No event. Wait some more */
726b8254
MD
428 callout_reset(&sc->sc_timeout_ch, spic_pollrate,
429 spictimeout, sc);
984263bc
MD
430 return;
431 }
432
433 if (sc->sc_count) {
434 if (sc->sc_sleeping) {
435 sc->sc_sleeping = 0;
436 wakeup((caddr_t) sc);
437 }
438 selwakeup(&sc->sc_rsel);
439 }
440 spic_call2(sc, 0x81, 0xff); /* Clear event */
441
726b8254 442 callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
984263bc
MD
443}
444
445static int
fef8985e 446spicopen(struct dev_open_args *ap)
984263bc
MD
447{
448 struct spic_softc *sc;
449
450 sc = devclass_get_softc(spic_devclass, 0);
451
452 if (sc->sc_opened)
453 return EBUSY;
454
455 sc->sc_opened++;
456 sc->sc_count=0;
457
458 /* Start the polling */
7a08a71e 459 callout_reset(&sc->sc_timeout_ch, spic_pollrate, spictimeout, sc);
984263bc
MD
460 return 0;
461}
462
463static int
fef8985e 464spicclose(struct dev_close_args *ap)
984263bc
MD
465{
466 struct spic_softc *sc;
467
468 sc = devclass_get_softc(spic_devclass, 0);
469
470 /* Stop polling */
726b8254 471 callout_stop(&sc->sc_timeout_ch);
984263bc
MD
472 sc->sc_opened = 0;
473 return 0;
474}
475
476static int
fef8985e 477spicread(struct dev_read_args *ap)
984263bc 478{
fef8985e 479 struct uio *uio = ap->a_uio;
984263bc 480 struct spic_softc *sc;
514c0755 481 int l, error;
984263bc
MD
482 u_char buf[SCBUFLEN];
483
484 sc = devclass_get_softc(spic_devclass, 0);
485
e54488bb 486 if (uio->uio_resid == 0) /* What kind of a read is this?! */
984263bc
MD
487 return 0;
488
514c0755 489 crit_enter();
984263bc
MD
490 while (!(sc->sc_count)) {
491 sc->sc_sleeping=1;
377d4740 492 error = tsleep((caddr_t) sc, PCATCH, "jogrea", 0);
984263bc
MD
493 sc->sc_sleeping=0;
494 if (error) {
514c0755 495 crit_exit();
984263bc
MD
496 return error;
497 }
498 }
514c0755 499 crit_exit();
984263bc 500
514c0755 501 crit_enter();
e54488bb 502 l = (int)szmin(uio->uio_resid, sc->sc_count);
984263bc
MD
503 bcopy(sc->sc_buf, buf, l);
504 sc->sc_count -= l;
505 bcopy(sc->sc_buf + l, sc->sc_buf, l);
514c0755 506 crit_exit();
e54488bb 507 return uiomove(buf, (size_t)l, uio);
984263bc
MD
508
509}
510
511static int
fef8985e 512spicioctl(struct dev_ioctl_args *ap)
984263bc
MD
513{
514 struct spic_softc *sc;
515
516 sc = devclass_get_softc(spic_devclass, 0);
517
518 return EIO;
519}
520
521static int
fef8985e 522spicpoll(struct dev_poll_args *ap)
984263bc
MD
523{
524 struct spic_softc *sc;
da8b443d 525 int revents = 0;
984263bc
MD
526
527 sc = devclass_get_softc(spic_devclass, 0);
514c0755 528 crit_enter();
fef8985e
MD
529 if (ap->a_events & (POLLIN | POLLRDNORM)) {
530 if (sc->sc_count) {
531 revents |= ap->a_events & (POLLIN | POLLRDNORM);
532 } else {
533 selrecord(curthread, &sc->sc_rsel);
984263bc
MD
534 }
535 }
514c0755 536 crit_exit();
fef8985e
MD
537 ap->a_events = revents;
538 return(0);
984263bc
MD
539}
540
541
542static device_method_t spic_methods[] = {
543 DEVMETHOD(device_probe, spic_probe),
544 DEVMETHOD(device_attach, spic_attach),
545
546 { 0, 0 }
547};
548
549static driver_t spic_driver = {
550 "spic",
551 spic_methods,
552 sizeof(struct spic_softc),
553};
554
555DRIVER_MODULE(spic, isa, spic_driver, spic_devclass, 0, 0);
556