oboe.c revision 1.13
1/*	$NetBSD: oboe.c,v 1.13 2003/06/29 22:30:26 fvdl 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/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/device.h>
51#include <sys/malloc.h>
52#include <sys/tty.h>
53#include <sys/vnode.h>
54#include <sys/poll.h>
55
56#include <dev/ir/ir.h>
57#include <dev/ir/irdaio.h>
58#include <dev/ir/irframevar.h>
59#include <dev/ir/sir.h>
60
61#include <dev/pci/pcidevs.h>
62#include <dev/pci/pcivar.h>
63
64#include <machine/bus.h>
65#include <machine/intr.h>
66#include <uvm/uvm_extern.h>
67
68#include <dev/pci/oboereg.h>
69
70int oboe_match(struct device *parent, struct cfdata *match, void *aux);
71void oboe_attach(struct device *parent, struct device *self, void *aux);
72int oboe_activate(struct device *self, enum devact act);
73int oboe_detach(struct device *self, int flags);
74
75int oboe_open(void *h, int flag, int mode, struct proc *p);
76int oboe_close(void *h, int flag, int mode, struct proc *p);
77int oboe_read(void *h, struct uio *uio, int flag);
78int oboe_write(void *h, struct uio *uio, int flag);
79int oboe_set_params(void *h, struct irda_params *params);
80int oboe_get_speeds(void *h, int *speeds);
81int oboe_get_turnarounds(void *h, int *times);
82int oboe_poll(void *h, int events, struct proc *p);
83int oboe_kqfilter(void *h, struct knote *kn);
84
85#ifdef OBOE_DEBUG
86#define DPRINTF(x)	if (oboedebug) printf x
87int oboedebug = 1;
88#else
89#define DPRINTF(x)
90#endif
91
92struct oboe_dma;
93
94struct oboe_softc {
95	struct device		sc_dev;
96	struct device		*sc_child;
97	struct pci_attach_args	sc_pa;
98	pci_intr_handle_t *	sc_ih;
99	unsigned int		sc_revision;	/* PCI Revision ID */
100	/* I/O Base device */
101	bus_space_tag_t		sc_iot;
102	bus_space_handle_t	sc_ioh;
103	bus_dma_tag_t		sc_dmatag;
104	struct selinfo		sc_rsel;
105	struct selinfo		sc_wsel;
106
107	int			sc_state;
108#define	OBOE_RSLP		0x01	/* waiting for data (read) */
109#define	OBOE_WSLP		0x02	/* waiting for data (write) */
110#define OBOE_CLOSING		0x04	/* waiting for output to drain */
111
112	int			sc_speeds;
113	int			sc_flags;
114	int			sc_speed;
115	int			sc_ebofs;
116
117	struct oboe_dma		*sc_dmas;
118	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
119	u_char *		sc_xmit_bufs[TX_SLOTS];
120	u_char *		sc_recv_bufs[RX_SLOTS];
121	void *			sc_xmit_stores[TX_SLOTS];
122	void *			sc_recv_stores[RX_SLOTS];
123	int			sc_txs; /* Current transmit slot number */
124	int			sc_rxs; /* Current receive slot number */
125	int			sc_saved; /* number of saved frames */
126	int			sc_lens[RX_SLOTS];
127
128	int			sc_txpending;
129
130	/* Statistics */
131	int			sc_txpackets;
132	int			sc_rxpackets;
133	int			sc_txerrors;
134	int			sc_rxerrors;
135};
136
137static int oboe_intr(void *handle);
138static int oboe_reset(struct oboe_softc *);
139
140struct oboe_dma {
141	bus_dmamap_t map;
142	caddr_t addr;
143	bus_dma_segment_t segs[1];
144	int nsegs;
145	size_t size;
146	struct oboe_dma *next;
147};
148
149#define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
150#define KERNADDR(p) ((void *)((p)->addr))
151
152static int oboe_alloc_taskfile(struct oboe_softc *);
153static void oboe_init_taskfile(struct oboe_softc *);
154static void oboe_startchip(struct oboe_softc *);
155static void oboe_stopchip(struct oboe_softc *);
156static int oboe_setbaud(struct oboe_softc *, int);
157
158CFATTACH_DECL(oboe, sizeof(struct oboe_softc),
159    oboe_match, oboe_attach, oboe_detach, oboe_activate);
160
161struct irframe_methods oboe_methods = {
162	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
163	oboe_kqfilter, oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
164};
165
166int
167oboe_match(struct device *parent, struct cfdata *match, void *aux)
168{
169	struct pci_attach_args *pa = aux;
170
171	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
172	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
173	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
174		return (1);
175	return 0;
176}
177
178void
179oboe_attach(struct device *parent, struct device *self, void *aux)
180{
181	struct oboe_softc *sc = (struct oboe_softc *)self;
182	struct pci_attach_args *pa = aux;
183	pci_intr_handle_t ih;
184	struct ir_attach_args ia;
185	const char *intrstring;
186
187	sc->sc_revision = PCI_REVISION(pa->pa_class);
188	printf(": Toshiba Fast Infrared Type O, revision %d\n",
189	       sc->sc_revision);
190
191	/* Map I/O registers. */
192	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
193	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
194		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
195		return;
196	}
197
198	sc->sc_dmatag = pa->pa_dmat;
199
200	ia.ia_type = IR_TYPE_IRFRAME;
201	ia.ia_methods = &oboe_methods;
202	ia.ia_handle = sc;
203
204	sc->sc_state = 0;
205	sc->sc_speed = IRDA_SPEED_9600;
206
207	/* Enable the device */
208	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
209	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
210	    PCI_COMMAND_MASTER_ENABLE);
211
212	/* Reset the device; bail out upon failure. */
213	if (oboe_reset(sc) != 0) {
214		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
215		return;
216	}
217	/* Map and establish the interrupt. */
218	if (pci_intr_map(pa, &ih)) {
219		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
220		return;
221	}
222	intrstring = pci_intr_string(pa->pa_pc, ih);
223	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
224	if (sc->sc_ih == NULL) {
225		printf("%s: couldn't establish interrupt",
226		    sc->sc_dev.dv_xname);
227		if (intrstring != NULL)
228			printf(" at %s", intrstring);
229		printf("\n");
230		return;
231	}
232	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
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
247int
248oboe_activate(struct device *self, enum devact act)
249{
250	struct oboe_softc *sc = (struct oboe_softc *)self;
251	int error;
252
253	DPRINTF(("%s: sc=%p\n", __FUNCTION__, 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		else
264			error = 0;
265		break;
266	}
267	return (error);
268}
269
270int
271oboe_detach(struct device *self, int flags)
272{
273#if 0
274	struct oboe_softc *sc = (struct oboe_softc *)self;
275#endif
276	/* XXX needs reference counting for proper detach. */
277	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
278	return (0);
279}
280
281int
282oboe_open(void *h, int flag, int mode, struct proc *p)
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
296int
297oboe_close(void *h, int flag, int mode, struct proc *p)
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
316int
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
366int
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
422int
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
439int
440oboe_get_speeds(void *h, int *speeds)
441{
442	struct oboe_softc *sc = h;
443	*speeds = sc->sc_speeds;
444	return (0);
445}
446
447int
448oboe_get_turnarounds(void *h, int *turnarounds)
449{
450#if 0
451	struct oboe_softc *sc = h;
452#endif
453	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
454	/* XXX Linux driver sets all bits */
455	*turnarounds = IRDA_TURNT_10000; /* 10ms */
456	return (0);
457}
458
459int
460oboe_poll(void *h, int events, struct proc *p)
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(p, &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
521int
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 == NULL) {
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