oboe.c revision 1.35
1/*	$NetBSD: oboe.c,v 1.35 2009/05/12 08:23:01 cegger 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.35 2009/05/12 08:23:01 cegger 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
67static int oboe_match(device_t parent, cfdata_t match, void *aux);
68static void oboe_attach(device_t parent, device_t self, void *aux);
69static int oboe_activate(device_t self, enum devact act);
70static int oboe_detach(device_t self, int flags);
71
72static int oboe_open(void *h, int flag, int mode, struct lwp *l);
73static int oboe_close(void *h, int flag, int mode, struct lwp *l);
74static int oboe_read(void *h, struct uio *uio, int flag);
75static int oboe_write(void *h, struct uio *uio, int flag);
76static int oboe_set_params(void *h, struct irda_params *params);
77static int oboe_get_speeds(void *h, int *speeds);
78static int oboe_get_turnarounds(void *h, int *times);
79static int oboe_poll(void *h, int events, struct lwp *l);
80static int oboe_kqfilter(void *h, struct knote *kn);
81
82#ifdef OBOE_DEBUG
83#define DPRINTF(x)	if (oboedebug) printf x
84int oboedebug = 1;
85#else
86#define DPRINTF(x)
87#endif
88
89struct oboe_dma;
90
91struct 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
134static int oboe_intr(void *handle);
135static int oboe_reset(struct oboe_softc *);
136
137struct 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
149static int oboe_alloc_taskfile(struct oboe_softc *);
150static void oboe_init_taskfile(struct oboe_softc *);
151static void oboe_startchip(struct oboe_softc *);
152static void oboe_stopchip(struct oboe_softc *);
153static int oboe_setbaud(struct oboe_softc *, int);
154
155CFATTACH_DECL(oboe, sizeof(struct oboe_softc),
156    oboe_match, oboe_attach, oboe_detach, oboe_activate);
157
158static 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
163static int
164oboe_match(device_t parent, cfdata_t match, void *aux)
165{
166	struct pci_attach_args *pa = aux;
167
168	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
169	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
170	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
171		return (1);
172	return 0;
173}
174
175static void
176oboe_attach(device_t parent, device_t self, void *aux)
177{
178	struct oboe_softc *sc = device_private(self);
179	struct pci_attach_args *pa = aux;
180	pci_intr_handle_t ih;
181	struct ir_attach_args ia;
182	const char *intrstring;
183
184	sc->sc_revision = PCI_REVISION(pa->pa_class);
185	printf(": Toshiba Fast Infrared Type O, revision %d\n",
186	       sc->sc_revision);
187
188	/* Map I/O registers. */
189	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
190	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
191		aprint_error_dev(&sc->sc_dev, "can't map I/O space\n");
192		return;
193	}
194
195	sc->sc_dmatag = pa->pa_dmat;
196
197	ia.ia_type = IR_TYPE_IRFRAME;
198	ia.ia_methods = &oboe_methods;
199	ia.ia_handle = sc;
200
201	sc->sc_state = 0;
202	sc->sc_speed = IRDA_SPEED_9600;
203
204	/* Enable the device */
205	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
206	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
207	    PCI_COMMAND_MASTER_ENABLE);
208
209	/* Reset the device; bail out upon failure. */
210	if (oboe_reset(sc) != 0) {
211		aprint_error_dev(&sc->sc_dev, "can't reset\n");
212		return;
213	}
214	/* Map and establish the interrupt. */
215	if (pci_intr_map(pa, &ih)) {
216		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
217		return;
218	}
219	intrstring = pci_intr_string(pa->pa_pc, ih);
220	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
221	if (sc->sc_ih == NULL) {
222		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
223		if (intrstring != NULL)
224			printf(" at %s", intrstring);
225		printf("\n");
226		return;
227	}
228	printf("%s: interrupting at %s\n", device_xname(&sc->sc_dev), intrstring);
229
230	selinit(&sc->sc_rsel);
231	selinit(&sc->sc_wsel);
232
233	sc->sc_txs = 0;
234	sc->sc_rxs = 0;
235
236	sc->sc_speeds =
237		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
238		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
239		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
240
241	oboe_alloc_taskfile(sc);
242
243	sc->sc_child = config_found((void *)sc, &ia, ir_print);
244}
245
246static int
247oboe_activate(device_t self, enum devact act)
248{
249	struct oboe_softc *sc = device_private(self);
250	int error = 0;
251
252	DPRINTF(("%s: sc=%p\n", __func__, sc));
253
254	switch (act) {
255	case DVACT_ACTIVATE:
256		return (EOPNOTSUPP);
257		break;
258
259	case DVACT_DEACTIVATE:
260		if (sc->sc_child != NULL)
261			error = config_deactivate(sc->sc_child);
262		break;
263	}
264	return (error);
265}
266
267static int
268oboe_detach(device_t self, int flags)
269{
270	struct oboe_softc *sc = device_private(self);
271
272#ifdef OBOE_DEBUG
273	/* XXX needs reference counting for proper detach. */
274	DPRINTF(("%s: sc=%p\n", __func__, sc));
275#endif
276	seldestroy(&sc->sc_rsel);
277	seldestroy(&sc->sc_wsel);
278	return (0);
279}
280
281static int
282oboe_open(void *h, int flag, int mode, struct lwp *l)
283{
284	struct oboe_softc *sc = h;
285
286	DPRINTF(("%s: sc=%p\n", __func__, sc));
287
288	sc->sc_state = 0;
289	sc->sc_saved = 0;
290	oboe_init_taskfile(sc);
291	oboe_startchip(sc);
292
293	return (0);
294}
295
296static int
297oboe_close(void *h, int flag, int mode,
298    struct lwp *l)
299{
300	struct oboe_softc *sc = h;
301	int error = 0;
302	int s = splir();
303
304	DPRINTF(("%s: sc=%p\n", __func__, sc));
305	/* Wait for output to drain */
306
307	if (sc->sc_txpending > 0) {
308		sc->sc_state |= OBOE_CLOSING;
309		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
310	}
311	splx(s);
312
313	oboe_stopchip(sc);
314	return (error);
315}
316
317static int
318oboe_read(void *h, struct uio *uio, int flag)
319{
320	struct oboe_softc *sc = h;
321	int error = 0;
322	int s;
323	int slot;
324
325	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
326		 __func__, uio->uio_resid, uio->uio_iovcnt,
327		 (long)uio->uio_offset));
328
329	s = splir();
330	while (sc->sc_saved == 0) {
331		if (flag & IO_NDELAY) {
332			splx(s);
333			return (EWOULDBLOCK);
334		}
335		sc->sc_state |= OBOE_RSLP;
336		DPRINTF(("oboe_read: sleep\n"));
337		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
338		DPRINTF(("oboe_read: woke, error=%d\n", error));
339		if (error) {
340			sc->sc_state &= ~OBOE_RSLP;
341			break;
342		}
343	}
344
345	/* Do just one frame transfer per read */
346
347	if (!error) {
348		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
349		if (uio->uio_resid < sc->sc_lens[slot]) {
350			DPRINTF(("oboe_read: uio buffer smaller than frame size"
351			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
352			error = EINVAL;
353		} else {
354			DPRINTF(("oboe_read: moving %d bytes from %p\n",
355				 sc->sc_lens[slot],
356				 sc->sc_recv_stores[slot]));
357			error = uiomove(sc->sc_recv_stores[slot],
358					sc->sc_lens[slot], uio);
359		}
360	}
361	sc->sc_saved--;
362	splx(s);
363
364	return (error);
365}
366
367static int
368oboe_write(void *h, struct uio *uio, int flag)
369{
370	struct oboe_softc *sc = h;
371	int error = 0;
372	int n;
373	int s = splir();
374
375	DPRINTF(("%s: sc=%p\n", __func__, sc));
376	while (sc->sc_txpending == TX_SLOTS) {
377		if (flag & IO_NDELAY) {
378			splx(s);
379			return (EWOULDBLOCK);
380		}
381		sc->sc_state |= OBOE_WSLP;
382		DPRINTF(("oboe_write: sleep\n"));
383		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
384		DPRINTF(("oboe_write: woke up, error=%d\n", error));
385		if (error) {
386			sc->sc_state &= ~OBOE_WSLP;
387			break;
388		}
389	}
390	if (error)
391		goto err;
392	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
393		DPRINTF(("oboe_write: slot overrun\n"));
394	}
395
396	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
397			   sc->sc_ebofs);
398	if (n < 0) {
399		error = -n;
400		goto err;
401	}
402	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
403
404	OUTB(sc, 0, OBOE_RST);
405	OUTB(sc, 0x1e, OBOE_REG_11);
406
407	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
408
409	/* XXX Need delay here??? */
410	delay(1000);
411
412	sc->sc_txpending++;
413	OUTB(sc, 0x80, OBOE_RST);
414	OUTB(sc, 1, OBOE_REG_9);
415	sc->sc_txs++;
416	sc->sc_txs %= TX_SLOTS;
417
418 err:
419	splx(s);
420	return (error);
421}
422
423static int
424oboe_set_params(void *h, struct irda_params *p)
425{
426	struct oboe_softc *sc = h;
427	int error;
428
429	if (p->speed > 0) {
430		error = oboe_setbaud(sc, p->speed);
431		if (error)
432			return (error);
433	}
434	sc->sc_ebofs = p->ebofs;
435
436	/* XXX ignore ebofs and maxsize for now */
437	return (0);
438}
439
440static int
441oboe_get_speeds(void *h, int *speeds)
442{
443	struct oboe_softc *sc = h;
444	*speeds = sc->sc_speeds;
445	return (0);
446}
447
448static int
449oboe_get_turnarounds(void *h, int *turnarounds)
450{
451#ifdef OBOE_DEBUG
452	struct oboe_softc *sc = h;
453	DPRINTF(("%s: sc=%p\n", __func__, sc));
454#endif
455
456	/* XXX Linux driver sets all bits */
457	*turnarounds = IRDA_TURNT_10000; /* 10ms */
458
459	return (0);
460}
461
462static int
463oboe_poll(void *h, int events, struct lwp *l)
464{
465	struct oboe_softc *sc = h;
466	int revents = 0;
467	int s;
468
469	DPRINTF(("%s: sc=%p\n", __func__, sc));
470
471	s = splir();
472	if (events & (POLLOUT | POLLWRNORM))
473		revents |= events & (POLLOUT | POLLWRNORM);
474	if (events & (POLLIN | POLLRDNORM)) {
475		if (sc->sc_saved > 0) {
476			DPRINTF(("%s: have data\n", __func__));
477			revents |= events & (POLLIN | POLLRDNORM);
478		} else {
479			DPRINTF(("%s: recording select\n", __func__));
480			selrecord(l, &sc->sc_rsel);
481		}
482	}
483	splx(s);
484
485	return (revents);
486}
487
488static void
489filt_oboerdetach(struct knote *kn)
490{
491	struct oboe_softc *sc = kn->kn_hook;
492	int s;
493
494	s = splir();
495	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
496	splx(s);
497}
498
499static int
500filt_oboeread(struct knote *kn, long hint)
501{
502	struct oboe_softc *sc = kn->kn_hook;
503
504	kn->kn_data = sc->sc_saved;
505	return (kn->kn_data > 0);
506}
507
508static void
509filt_oboewdetach(struct knote *kn)
510{
511	struct oboe_softc *sc = kn->kn_hook;
512	int s;
513
514	s = splir();
515	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
516	splx(s);
517}
518
519static const struct filterops oboeread_filtops =
520	{ 1, NULL, filt_oboerdetach, filt_oboeread };
521static const struct filterops oboewrite_filtops =
522	{ 1, NULL, filt_oboewdetach, filt_seltrue };
523
524static int
525oboe_kqfilter(void *h, struct knote *kn)
526{
527	struct oboe_softc *sc = h;
528	struct klist *klist;
529	int s;
530
531	switch (kn->kn_filter) {
532	case EVFILT_READ:
533		klist = &sc->sc_rsel.sel_klist;
534		kn->kn_fop = &oboeread_filtops;
535		break;
536	case EVFILT_WRITE:
537		klist = &sc->sc_wsel.sel_klist;
538		kn->kn_fop = &oboewrite_filtops;
539		break;
540	default:
541		return (EINVAL);
542	}
543
544	kn->kn_hook = sc;
545
546	s = splir();
547	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
548	splx(s);
549
550	return (0);
551}
552
553static int
554oboe_reset(struct oboe_softc *sc)
555{
556#if 0
557	OUTB(sc, 0x00, OBOE_RST);
558	OUTB(sc, 0x80, OBOE_RST);
559#endif
560	return 0;
561}
562
563static int
564oboe_intr(void *p)
565{
566	struct oboe_softc *sc = p;
567	uint8_t irqstat	= INB(sc, OBOE_ISR);
568
569	if (!(irqstat & 0xf8))
570		return (0); /* Not for me? */
571
572	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
573
574	OUTB(sc, irqstat, OBOE_ISR);
575
576	if (irqstat & OBOE_ISR_RXDONE) {
577		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
578			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
579			if (sc->sc_saved == RX_SLOTS) {
580				DPRINTF(("oboe_intr: all buffers filled\n"));
581				return 0;
582			}
583
584			if (len > 2)
585				len -= 2; /* JSP: skip check sum? */
586
587			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
588				 sc->sc_recv_stores[sc->sc_rxs]));
589			memcpy(sc->sc_recv_stores[sc->sc_rxs],
590			       sc->sc_recv_bufs[sc->sc_rxs],
591			       len);
592			sc->sc_lens[sc->sc_rxs] = len;
593			sc->sc_saved++;
594#if 0
595			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
596				     len, &sc->sc_q);
597#endif
598			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
599			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
600			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
601			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
602		}
603		DPRINTF(("oboe_intr no more frames available\n"));
604		if (sc->sc_state & OBOE_RSLP) {
605			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
606			sc->sc_state &= ~OBOE_RSLP;
607			DPRINTF(("oboe_intr: waking up reader\n"));
608			wakeup(&sc->sc_rxs);
609		}
610		selnotify(&sc->sc_rsel, 0, 0);
611		DPRINTF(("oboe_intr returning\n"));
612	}
613	if (irqstat & OBOE_ISR_TXDONE) {
614	        DPRINTF(("oboe_intr: write done\n"));
615		sc->sc_txpending--;
616		sc->sc_txpackets++;
617
618		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
619			wakeup(&sc->sc_state);
620			return 1;
621		}
622
623		if (sc->sc_state & OBOE_WSLP) {
624			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
625			sc->sc_state &= ~OBOE_WSLP;
626			DPRINTF(("oboe_intr: waking up writer\n"));
627			wakeup(&sc->sc_txs);
628		}
629		selnotify(&sc->sc_wsel, 0, 0);
630	}
631	return (1);
632}
633
634/* XXX vtophys must go! */
635static void
636oboe_init_taskfile(struct oboe_softc *sc)
637{
638	int i;
639	int s = splir();
640
641	for (i = 0; i < TX_SLOTS; ++i) {
642		sc->sc_taskfile->xmit[i].len = 0;
643		sc->sc_taskfile->xmit[i].control = 0x00;
644		sc->sc_taskfile->xmit[i].buffer =
645			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
646	}
647
648	for (i = 0; i < RX_SLOTS; ++i) {
649		sc->sc_taskfile->recv[i].len = 0;
650		sc->sc_taskfile->recv[i].control = 0x83;
651		sc->sc_taskfile->recv[i].buffer =
652			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
653	}
654
655	sc->sc_txpending = 0;
656
657	splx(s);
658}
659
660static int
661oboe_alloc_taskfile(struct oboe_softc *sc)
662{
663	int i;
664	/* XXX */
665	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
666	if (addr == 0) {
667		goto bad;
668	}
669	addr &= ~(sizeof (struct OboeTaskFile) - 1);
670	addr += sizeof (struct OboeTaskFile);
671	sc->sc_taskfile = (struct OboeTaskFile *) addr;
672
673	for (i = 0; i < TX_SLOTS; ++i) {
674		sc->sc_xmit_bufs[i] =
675			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
676		sc->sc_xmit_stores[i] =
677			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
678		if (sc->sc_xmit_bufs[i] == NULL ||
679		    sc->sc_xmit_stores[i] == NULL) {
680			goto bad;
681		}
682	}
683	for (i = 0; i < RX_SLOTS; ++i) {
684		sc->sc_recv_bufs[i] =
685			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
686		sc->sc_recv_stores[i] =
687			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
688		if (sc->sc_recv_bufs[i] == NULL ||
689		    sc->sc_recv_stores[i] == NULL) {
690			goto bad;
691		}
692	}
693
694	return 0;
695bad:
696	printf("oboe: malloc for buffers failed()\n");
697	return 1;
698}
699
700static void
701oboe_startchip (struct oboe_softc *sc)
702{
703	uint32_t physaddr;
704
705	OUTB(sc, 0, OBOE_LOCK);
706	OUTB(sc, 0, OBOE_RST);
707	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
708	OUTB(sc, 0xf0, OBOE_REG_D);
709	OUTB(sc, 0xff, OBOE_ISR);
710	OUTB(sc, 0x0f, OBOE_REG_1A);
711	OUTB(sc, 0xff, OBOE_REG_1B);
712
713	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
714
715	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
716	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
717	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
718
719	OUTB(sc, 0x0e, OBOE_REG_11);
720	OUTB(sc, 0x80, OBOE_RST);
721
722	(void)oboe_setbaud(sc, 9600);
723
724	sc->sc_rxs = INB(sc, OBOE_RCVT);
725	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
726		sc->sc_rxs = 0;
727	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
728	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
729		sc->sc_txs = 0;
730}
731
732static void
733oboe_stopchip (struct oboe_softc *sc)
734{
735	OUTB(sc, 0x0e, OBOE_REG_11);
736	OUTB(sc, 0x00, OBOE_RST);
737	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
738	OUTB(sc, 0xff, OBOE_TFP1);
739	OUTB(sc, 0xff, OBOE_TFP0);
740	OUTB(sc, 0x0f, OBOE_REG_1B);
741	OUTB(sc, 0xff, OBOE_REG_1A);
742	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
743	OUTB(sc, 0x80, OBOE_RST);
744	OUTB(sc, 0x0e, OBOE_LOCK);
745}
746
747#define SPEEDCASE(speed, type, divisor) \
748case speed: \
749OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
750OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
751OUTB(sc, divisor, OBOE_UDIV); \
752break
753
754static int
755oboe_setbaud(struct oboe_softc *sc, int baud)
756{
757	int s;
758
759	DPRINTF(("oboe: setting baud to %d\n", baud));
760
761	s = splir();
762
763	switch (baud) {
764	SPEEDCASE(   2400, SIR, 0xbf);
765	SPEEDCASE(   9600, SIR, 0x2f);
766	SPEEDCASE(  19200, SIR, 0x17);
767	SPEEDCASE(  38400, SIR, 0x0b);
768	SPEEDCASE(  57600, SIR, 0x07);
769	SPEEDCASE( 115200, SIR, 0x03);
770	SPEEDCASE(1152000, MIR, 0x01);
771	SPEEDCASE(4000000, FIR, 0x00);
772	default:
773		DPRINTF(("oboe: cannot set speed to %d\n", baud));
774		splx(s);
775		return (EINVAL);
776	}
777
778	OUTB(sc, 0x00, OBOE_RST);
779	OUTB(sc, 0x80, OBOE_RST);
780	OUTB(sc, 0x01, OBOE_REG_9);
781
782	sc->sc_speed = baud;
783
784	splx(s);
785
786	return (0);
787}
788