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