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