xref: /netbsd-src/sys/dev/pci/oboe.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: oboe.c,v 1.32 2008/04/28 20:23:55 martin Exp $	*/
2 
3 /*	XXXXFVDL THIS DRIVER IS BROKEN FOR NON-i386 -- vtophys() usage	*/
4 
5 /*-
6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jan Sparud.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Toshiba OBOE IrDA SIR/FIR driver.
36  *
37  * Based on information from the Linux driver, thus the magic hex numbers.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: oboe.c,v 1.32 2008/04/28 20:23:55 martin Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/malloc.h>
48 #include <sys/tty.h>
49 #include <sys/vnode.h>
50 #include <sys/poll.h>
51 #include <sys/proc.h>
52 
53 #include <dev/ir/ir.h>
54 #include <dev/ir/irdaio.h>
55 #include <dev/ir/irframevar.h>
56 #include <dev/ir/sir.h>
57 
58 #include <dev/pci/pcidevs.h>
59 #include <dev/pci/pcivar.h>
60 
61 #include <sys/bus.h>
62 #include <sys/intr.h>
63 #include <uvm/uvm_extern.h>
64 
65 #include <dev/pci/oboereg.h>
66 
67 static int oboe_match(struct device *parent, struct cfdata *match, void *aux);
68 static void oboe_attach(struct device *parent, struct device *self, void *aux);
69 static int oboe_activate(struct device *self, enum devact act);
70 static int oboe_detach(struct device *self, int flags);
71 
72 static int oboe_open(void *h, int flag, int mode, struct lwp *l);
73 static int oboe_close(void *h, int flag, int mode, struct lwp *l);
74 static int oboe_read(void *h, struct uio *uio, int flag);
75 static int oboe_write(void *h, struct uio *uio, int flag);
76 static int oboe_set_params(void *h, struct irda_params *params);
77 static int oboe_get_speeds(void *h, int *speeds);
78 static int oboe_get_turnarounds(void *h, int *times);
79 static int oboe_poll(void *h, int events, struct lwp *l);
80 static int oboe_kqfilter(void *h, struct knote *kn);
81 
82 #ifdef OBOE_DEBUG
83 #define DPRINTF(x)	if (oboedebug) printf x
84 int oboedebug = 1;
85 #else
86 #define DPRINTF(x)
87 #endif
88 
89 struct oboe_dma;
90 
91 struct oboe_softc {
92 	struct device		sc_dev;
93 	struct device		*sc_child;
94 	struct pci_attach_args	sc_pa;
95 	pci_intr_handle_t *	sc_ih;
96 	unsigned int		sc_revision;	/* PCI Revision ID */
97 	/* I/O Base device */
98 	bus_space_tag_t		sc_iot;
99 	bus_space_handle_t	sc_ioh;
100 	bus_dma_tag_t		sc_dmatag;
101 	struct selinfo		sc_rsel;
102 	struct selinfo		sc_wsel;
103 
104 	int			sc_state;
105 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
106 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
107 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
108 
109 	int			sc_speeds;
110 	int			sc_flags;
111 	int			sc_speed;
112 	int			sc_ebofs;
113 
114 	struct oboe_dma		*sc_dmas;
115 	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
116 	u_char *		sc_xmit_bufs[TX_SLOTS];
117 	u_char *		sc_recv_bufs[RX_SLOTS];
118 	void *			sc_xmit_stores[TX_SLOTS];
119 	void *			sc_recv_stores[RX_SLOTS];
120 	int			sc_txs; /* Current transmit slot number */
121 	int			sc_rxs; /* Current receive slot number */
122 	int			sc_saved; /* number of saved frames */
123 	int			sc_lens[RX_SLOTS];
124 
125 	int			sc_txpending;
126 
127 	/* Statistics */
128 	int			sc_txpackets;
129 	int			sc_rxpackets;
130 	int			sc_txerrors;
131 	int			sc_rxerrors;
132 };
133 
134 static int oboe_intr(void *handle);
135 static int oboe_reset(struct oboe_softc *);
136 
137 struct oboe_dma {
138 	bus_dmamap_t map;
139 	void *addr;
140 	bus_dma_segment_t segs[1];
141 	int nsegs;
142 	size_t size;
143 	struct oboe_dma *next;
144 };
145 
146 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
147 #define KERNADDR(p) ((void *)((p)->addr))
148 
149 static int oboe_alloc_taskfile(struct oboe_softc *);
150 static void oboe_init_taskfile(struct oboe_softc *);
151 static void oboe_startchip(struct oboe_softc *);
152 static void oboe_stopchip(struct oboe_softc *);
153 static int oboe_setbaud(struct oboe_softc *, int);
154 
155 CFATTACH_DECL(oboe, sizeof(struct oboe_softc),
156     oboe_match, oboe_attach, oboe_detach, oboe_activate);
157 
158 static struct irframe_methods oboe_methods = {
159 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
160 	oboe_kqfilter, oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
161 };
162 
163 static int
164 oboe_match(struct device *parent, struct cfdata *match,
165     void *aux)
166 {
167 	struct pci_attach_args *pa = aux;
168 
169 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
170 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
171 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
172 		return (1);
173 	return 0;
174 }
175 
176 static void
177 oboe_attach(struct device *parent, struct device *self, void *aux)
178 {
179 	struct oboe_softc *sc = (struct oboe_softc *)self;
180 	struct pci_attach_args *pa = aux;
181 	pci_intr_handle_t ih;
182 	struct ir_attach_args ia;
183 	const char *intrstring;
184 
185 	sc->sc_revision = PCI_REVISION(pa->pa_class);
186 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
187 	       sc->sc_revision);
188 
189 	/* Map I/O registers. */
190 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
191 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
192 		aprint_error_dev(&sc->sc_dev, "can't map I/O space\n");
193 		return;
194 	}
195 
196 	sc->sc_dmatag = pa->pa_dmat;
197 
198 	ia.ia_type = IR_TYPE_IRFRAME;
199 	ia.ia_methods = &oboe_methods;
200 	ia.ia_handle = sc;
201 
202 	sc->sc_state = 0;
203 	sc->sc_speed = IRDA_SPEED_9600;
204 
205 	/* Enable the device */
206 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
207 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
208 	    PCI_COMMAND_MASTER_ENABLE);
209 
210 	/* Reset the device; bail out upon failure. */
211 	if (oboe_reset(sc) != 0) {
212 		aprint_error_dev(&sc->sc_dev, "can't reset\n");
213 		return;
214 	}
215 	/* Map and establish the interrupt. */
216 	if (pci_intr_map(pa, &ih)) {
217 		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
218 		return;
219 	}
220 	intrstring = pci_intr_string(pa->pa_pc, ih);
221 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
222 	if (sc->sc_ih == NULL) {
223 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
224 		if (intrstring != NULL)
225 			printf(" at %s", intrstring);
226 		printf("\n");
227 		return;
228 	}
229 	printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev), intrstring);
230 
231 	selinit(&sc->sc_rsel);
232 	selinit(&sc->sc_wsel);
233 
234 	sc->sc_txs = 0;
235 	sc->sc_rxs = 0;
236 
237 	sc->sc_speeds =
238 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
239 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
240 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
241 
242 	oboe_alloc_taskfile(sc);
243 
244 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
245 }
246 
247 static int
248 oboe_activate(struct device *self, enum devact act)
249 {
250 	struct oboe_softc *sc = (struct oboe_softc *)self;
251 	int error = 0;
252 
253 	DPRINTF(("%s: sc=%p\n", __func__, sc));
254 
255 	switch (act) {
256 	case DVACT_ACTIVATE:
257 		return (EOPNOTSUPP);
258 		break;
259 
260 	case DVACT_DEACTIVATE:
261 		if (sc->sc_child != NULL)
262 			error = config_deactivate(sc->sc_child);
263 		break;
264 	}
265 	return (error);
266 }
267 
268 static int
269 oboe_detach(struct device *self, int flags)
270 {
271 	struct oboe_softc *sc = (struct oboe_softc *)self;
272 
273 #ifdef OBOE_DEBUG
274 	/* XXX needs reference counting for proper detach. */
275 	DPRINTF(("%s: sc=%p\n", __func__, sc));
276 #endif
277 	seldestroy(&sc->sc_rsel);
278 	seldestroy(&sc->sc_wsel);
279 	return (0);
280 }
281 
282 static int
283 oboe_open(void *h, int flag, int mode, struct lwp *l)
284 {
285 	struct oboe_softc *sc = h;
286 
287 	DPRINTF(("%s: sc=%p\n", __func__, sc));
288 
289 	sc->sc_state = 0;
290 	sc->sc_saved = 0;
291 	oboe_init_taskfile(sc);
292 	oboe_startchip(sc);
293 
294 	return (0);
295 }
296 
297 static int
298 oboe_close(void *h, int flag, int mode,
299     struct lwp *l)
300 {
301 	struct oboe_softc *sc = h;
302 	int error = 0;
303 	int s = splir();
304 
305 	DPRINTF(("%s: sc=%p\n", __func__, sc));
306 	/* Wait for output to drain */
307 
308 	if (sc->sc_txpending > 0) {
309 		sc->sc_state |= OBOE_CLOSING;
310 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
311 	}
312 	splx(s);
313 
314 	oboe_stopchip(sc);
315 	return (error);
316 }
317 
318 static int
319 oboe_read(void *h, struct uio *uio, int flag)
320 {
321 	struct oboe_softc *sc = h;
322 	int error = 0;
323 	int s;
324 	int slot;
325 
326 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
327 		 __func__, uio->uio_resid, uio->uio_iovcnt,
328 		 (long)uio->uio_offset));
329 
330 	s = splir();
331 	while (sc->sc_saved == 0) {
332 		if (flag & IO_NDELAY) {
333 			splx(s);
334 			return (EWOULDBLOCK);
335 		}
336 		sc->sc_state |= OBOE_RSLP;
337 		DPRINTF(("oboe_read: sleep\n"));
338 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
339 		DPRINTF(("oboe_read: woke, error=%d\n", error));
340 		if (error) {
341 			sc->sc_state &= ~OBOE_RSLP;
342 			break;
343 		}
344 	}
345 
346 	/* Do just one frame transfer per read */
347 
348 	if (!error) {
349 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
350 		if (uio->uio_resid < sc->sc_lens[slot]) {
351 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
352 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
353 			error = EINVAL;
354 		} else {
355 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
356 				 sc->sc_lens[slot],
357 				 sc->sc_recv_stores[slot]));
358 			error = uiomove(sc->sc_recv_stores[slot],
359 					sc->sc_lens[slot], uio);
360 		}
361 	}
362 	sc->sc_saved--;
363 	splx(s);
364 
365 	return (error);
366 }
367 
368 static int
369 oboe_write(void *h, struct uio *uio, int flag)
370 {
371 	struct oboe_softc *sc = h;
372 	int error = 0;
373 	int n;
374 	int s = splir();
375 
376 	DPRINTF(("%s: sc=%p\n", __func__, sc));
377 	while (sc->sc_txpending == TX_SLOTS) {
378 		if (flag & IO_NDELAY) {
379 			splx(s);
380 			return (EWOULDBLOCK);
381 		}
382 		sc->sc_state |= OBOE_WSLP;
383 		DPRINTF(("oboe_write: sleep\n"));
384 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
385 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
386 		if (error) {
387 			sc->sc_state &= ~OBOE_WSLP;
388 			break;
389 		}
390 	}
391 	if (error)
392 		goto err;
393 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
394 		DPRINTF(("oboe_write: slot overrun\n"));
395 	}
396 
397 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
398 			   sc->sc_ebofs);
399 	if (n < 0) {
400 		error = -n;
401 		goto err;
402 	}
403 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
404 
405 	OUTB(sc, 0, OBOE_RST);
406 	OUTB(sc, 0x1e, OBOE_REG_11);
407 
408 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
409 
410 	/* XXX Need delay here??? */
411 	delay(1000);
412 
413 	sc->sc_txpending++;
414 	OUTB(sc, 0x80, OBOE_RST);
415 	OUTB(sc, 1, OBOE_REG_9);
416 	sc->sc_txs++;
417 	sc->sc_txs %= TX_SLOTS;
418 
419  err:
420 	splx(s);
421 	return (error);
422 }
423 
424 static int
425 oboe_set_params(void *h, struct irda_params *p)
426 {
427 	struct oboe_softc *sc = h;
428 	int error;
429 
430 	if (p->speed > 0) {
431 		error = oboe_setbaud(sc, p->speed);
432 		if (error)
433 			return (error);
434 	}
435 	sc->sc_ebofs = p->ebofs;
436 
437 	/* XXX ignore ebofs and maxsize for now */
438 	return (0);
439 }
440 
441 static int
442 oboe_get_speeds(void *h, int *speeds)
443 {
444 	struct oboe_softc *sc = h;
445 	*speeds = sc->sc_speeds;
446 	return (0);
447 }
448 
449 static int
450 oboe_get_turnarounds(void *h, int *turnarounds)
451 {
452 #ifdef OBOE_DEBUG
453 	struct oboe_softc *sc = h;
454 	DPRINTF(("%s: sc=%p\n", __func__, sc));
455 #endif
456 
457 	/* XXX Linux driver sets all bits */
458 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
459 
460 	return (0);
461 }
462 
463 static int
464 oboe_poll(void *h, int events, struct lwp *l)
465 {
466 	struct oboe_softc *sc = h;
467 	int revents = 0;
468 	int s;
469 
470 	DPRINTF(("%s: sc=%p\n", __func__, sc));
471 
472 	s = splir();
473 	if (events & (POLLOUT | POLLWRNORM))
474 		revents |= events & (POLLOUT | POLLWRNORM);
475 	if (events & (POLLIN | POLLRDNORM)) {
476 		if (sc->sc_saved > 0) {
477 			DPRINTF(("%s: have data\n", __func__));
478 			revents |= events & (POLLIN | POLLRDNORM);
479 		} else {
480 			DPRINTF(("%s: recording select\n", __func__));
481 			selrecord(l, &sc->sc_rsel);
482 		}
483 	}
484 	splx(s);
485 
486 	return (revents);
487 }
488 
489 static void
490 filt_oboerdetach(struct knote *kn)
491 {
492 	struct oboe_softc *sc = kn->kn_hook;
493 	int s;
494 
495 	s = splir();
496 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
497 	splx(s);
498 }
499 
500 static int
501 filt_oboeread(struct knote *kn, long hint)
502 {
503 	struct oboe_softc *sc = kn->kn_hook;
504 
505 	kn->kn_data = sc->sc_saved;
506 	return (kn->kn_data > 0);
507 }
508 
509 static void
510 filt_oboewdetach(struct knote *kn)
511 {
512 	struct oboe_softc *sc = kn->kn_hook;
513 	int s;
514 
515 	s = splir();
516 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
517 	splx(s);
518 }
519 
520 static const struct filterops oboeread_filtops =
521 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
522 static const struct filterops oboewrite_filtops =
523 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
524 
525 static int
526 oboe_kqfilter(void *h, struct knote *kn)
527 {
528 	struct oboe_softc *sc = h;
529 	struct klist *klist;
530 	int s;
531 
532 	switch (kn->kn_filter) {
533 	case EVFILT_READ:
534 		klist = &sc->sc_rsel.sel_klist;
535 		kn->kn_fop = &oboeread_filtops;
536 		break;
537 	case EVFILT_WRITE:
538 		klist = &sc->sc_wsel.sel_klist;
539 		kn->kn_fop = &oboewrite_filtops;
540 		break;
541 	default:
542 		return (EINVAL);
543 	}
544 
545 	kn->kn_hook = sc;
546 
547 	s = splir();
548 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
549 	splx(s);
550 
551 	return (0);
552 }
553 
554 static int
555 oboe_reset(struct oboe_softc *sc)
556 {
557 #if 0
558 	OUTB(sc, 0x00, OBOE_RST);
559 	OUTB(sc, 0x80, OBOE_RST);
560 #endif
561 	return 0;
562 }
563 
564 static int
565 oboe_intr(void *p)
566 {
567 	struct oboe_softc *sc = p;
568 	uint8_t irqstat	= INB(sc, OBOE_ISR);
569 
570 	if (!(irqstat & 0xf8))
571 		return (0); /* Not for me? */
572 
573 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
574 
575 	OUTB(sc, irqstat, OBOE_ISR);
576 
577 	if (irqstat & OBOE_ISR_RXDONE) {
578 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
579 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
580 			if (sc->sc_saved == RX_SLOTS) {
581 				DPRINTF(("oboe_intr: all buffers filled\n"));
582 				return 0;
583 			}
584 
585 			if (len > 2)
586 				len -= 2; /* JSP: skip check sum? */
587 
588 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
589 				 sc->sc_recv_stores[sc->sc_rxs]));
590 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
591 			       sc->sc_recv_bufs[sc->sc_rxs],
592 			       len);
593 			sc->sc_lens[sc->sc_rxs] = len;
594 			sc->sc_saved++;
595 #if 0
596 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
597 				     len, &sc->sc_q);
598 #endif
599 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
600 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
601 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
602 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
603 		}
604 		DPRINTF(("oboe_intr no more frames available\n"));
605 		if (sc->sc_state & OBOE_RSLP) {
606 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
607 			sc->sc_state &= ~OBOE_RSLP;
608 			DPRINTF(("oboe_intr: waking up reader\n"));
609 			wakeup(&sc->sc_rxs);
610 		}
611 		selnotify(&sc->sc_rsel, 0, 0);
612 		DPRINTF(("oboe_intr returning\n"));
613 	}
614 	if (irqstat & OBOE_ISR_TXDONE) {
615 	        DPRINTF(("oboe_intr: write done\n"));
616 		sc->sc_txpending--;
617 		sc->sc_txpackets++;
618 
619 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
620 			wakeup(&sc->sc_state);
621 			return 1;
622 		}
623 
624 		if (sc->sc_state & OBOE_WSLP) {
625 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
626 			sc->sc_state &= ~OBOE_WSLP;
627 			DPRINTF(("oboe_intr: waking up writer\n"));
628 			wakeup(&sc->sc_txs);
629 		}
630 		selnotify(&sc->sc_wsel, 0, 0);
631 	}
632 	return (1);
633 }
634 
635 /* XXX vtophys must go! */
636 static void
637 oboe_init_taskfile(struct oboe_softc *sc)
638 {
639 	int i;
640 	int s = splir();
641 
642 	for (i = 0; i < TX_SLOTS; ++i) {
643 		sc->sc_taskfile->xmit[i].len = 0;
644 		sc->sc_taskfile->xmit[i].control = 0x00;
645 		sc->sc_taskfile->xmit[i].buffer =
646 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
647 	}
648 
649 	for (i = 0; i < RX_SLOTS; ++i) {
650 		sc->sc_taskfile->recv[i].len = 0;
651 		sc->sc_taskfile->recv[i].control = 0x83;
652 		sc->sc_taskfile->recv[i].buffer =
653 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
654 	}
655 
656 	sc->sc_txpending = 0;
657 
658 	splx(s);
659 }
660 
661 static int
662 oboe_alloc_taskfile(struct oboe_softc *sc)
663 {
664 	int i;
665 	/* XXX */
666 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
667 	if (addr == 0) {
668 		goto bad;
669 	}
670 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
671 	addr += sizeof (struct OboeTaskFile);
672 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
673 
674 	for (i = 0; i < TX_SLOTS; ++i) {
675 		sc->sc_xmit_bufs[i] =
676 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
677 		sc->sc_xmit_stores[i] =
678 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
679 		if (sc->sc_xmit_bufs[i] == NULL ||
680 		    sc->sc_xmit_stores[i] == NULL) {
681 			goto bad;
682 		}
683 	}
684 	for (i = 0; i < RX_SLOTS; ++i) {
685 		sc->sc_recv_bufs[i] =
686 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
687 		sc->sc_recv_stores[i] =
688 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
689 		if (sc->sc_recv_bufs[i] == NULL ||
690 		    sc->sc_recv_stores[i] == NULL) {
691 			goto bad;
692 		}
693 	}
694 
695 	return 0;
696 bad:
697 	printf("oboe: malloc for buffers failed()\n");
698 	return 1;
699 }
700 
701 static void
702 oboe_startchip (struct oboe_softc *sc)
703 {
704 	uint32_t physaddr;
705 
706 	OUTB(sc, 0, OBOE_LOCK);
707 	OUTB(sc, 0, OBOE_RST);
708 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
709 	OUTB(sc, 0xf0, OBOE_REG_D);
710 	OUTB(sc, 0xff, OBOE_ISR);
711 	OUTB(sc, 0x0f, OBOE_REG_1A);
712 	OUTB(sc, 0xff, OBOE_REG_1B);
713 
714 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
715 
716 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
717 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
718 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
719 
720 	OUTB(sc, 0x0e, OBOE_REG_11);
721 	OUTB(sc, 0x80, OBOE_RST);
722 
723 	(void)oboe_setbaud(sc, 9600);
724 
725 	sc->sc_rxs = INB(sc, OBOE_RCVT);
726 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
727 		sc->sc_rxs = 0;
728 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
729 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
730 		sc->sc_txs = 0;
731 }
732 
733 static void
734 oboe_stopchip (struct oboe_softc *sc)
735 {
736 	OUTB(sc, 0x0e, OBOE_REG_11);
737 	OUTB(sc, 0x00, OBOE_RST);
738 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
739 	OUTB(sc, 0xff, OBOE_TFP1);
740 	OUTB(sc, 0xff, OBOE_TFP0);
741 	OUTB(sc, 0x0f, OBOE_REG_1B);
742 	OUTB(sc, 0xff, OBOE_REG_1A);
743 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
744 	OUTB(sc, 0x80, OBOE_RST);
745 	OUTB(sc, 0x0e, OBOE_LOCK);
746 }
747 
748 #define SPEEDCASE(speed, type, divisor) \
749 case speed: \
750 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
751 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
752 OUTB(sc, divisor, OBOE_UDIV); \
753 break
754 
755 static int
756 oboe_setbaud(struct oboe_softc *sc, int baud)
757 {
758 	int s;
759 
760 	DPRINTF(("oboe: setting baud to %d\n", baud));
761 
762 	s = splir();
763 
764 	switch (baud) {
765 	SPEEDCASE(   2400, SIR, 0xbf);
766 	SPEEDCASE(   9600, SIR, 0x2f);
767 	SPEEDCASE(  19200, SIR, 0x17);
768 	SPEEDCASE(  38400, SIR, 0x0b);
769 	SPEEDCASE(  57600, SIR, 0x07);
770 	SPEEDCASE( 115200, SIR, 0x03);
771 	SPEEDCASE(1152000, MIR, 0x01);
772 	SPEEDCASE(4000000, FIR, 0x00);
773 	default:
774 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
775 		splx(s);
776 		return (EINVAL);
777 	}
778 
779 	OUTB(sc, 0x00, OBOE_RST);
780 	OUTB(sc, 0x80, OBOE_RST);
781 	OUTB(sc, 0x01, OBOE_REG_9);
782 
783 	sc->sc_speed = baud;
784 
785 	splx(s);
786 
787 	return (0);
788 }
789