Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions etc/etc.evbppc/MAKEDEV.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ all_md)
makedev virtio
;;

# TODO - i think i can add something here to create my device files
# instead of manually calling mknod i just dont know yet..
# ramdisk definition is found at distrib/evbppc/ramdisk/Makefile

xlcom[0-9]*)
Expand Down
1 change: 1 addition & 0 deletions sys/arch/evbppc/conf/NINTENDO
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ options WSDISPLAY_MULTICONS
ahb0 at mainbus0 irq 14
si0 at mainbus0 addr 0x0d006400 irq 3 # Serial interface
uhid* at si0
gcport* at si0
exi0 at mainbus0 addr 0x0d006800 irq 4 # External interface
rtcsram0 at exi0 # RTC/SRAM chip
gecko0 at exi0 # USB Gecko
Expand Down
2 changes: 2 additions & 0 deletions sys/arch/evbppc/conf/majors.evbppc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ device-major twe char 76 twe
#device-major obsolete char 98 obsolete (nsmb)
device-major xlcom char 99 xlcom

device-major gcport char 100

# Majors up to 143 are reserved for machine-dependent drivers.
# New machine-independent driver majors are assigned in
# sys/conf/majors.
4 changes: 4 additions & 0 deletions sys/arch/evbppc/nintendo/dev/files.dev
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ file arch/evbppc/nintendo/dev/si.c si
attach uhid at si with uhid_si
file arch/evbppc/nintendo/dev/uhid_si.c uhid_si

device gcport
attach gcport at si with gcport_si
file arch/evbppc/nintendo/dev/gcport_si.c gcport_si

define ahb { [addr=-1], [irq=-1] }
device ahb: ahb
attach ahb at mainbus
Expand Down
237 changes: 237 additions & 0 deletions sys/arch/evbppc/nintendo/dev/gcport_si.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/* $NetBSD: gcport_si.c,v 1.0 2026/05/18 22:54:30 gummybuns Exp $ */

/*-
* Copyright (c) 2026 ZacBrown <gummybuns@protonmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: gcport_si.c,v 1.0 2026/05/18 22:53:30 gummybuns Exp $");

#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/device.h>
#include <sys/ioccom.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/types.h>

#include <dev/usb/usb.h>
#include <dev/hid/uhid.h>

#include "si.h"
#include "joybus.h"

struct si_payload {
uint32_t insize; /* bytes to receive. max 128 */
uint32_t outsize; /* bytes to send. max 128 */
uint32_t *status; /* sisr status for this channel */
void *in; /* buffer to store response */
void *out; /* buffer to send out to device */
long delay; /* delay the transaction (microsec) */
};

extern struct cfdriver gcport_cd;
struct gcport_softc {
device_t sc_dev;
struct si_channel *ch;
bus_space_tag_t sc_bst;
bus_space_handle_t sc_bsh;
};


#define SI_SEND _IOWR(0, 1, struct si_payload)

static int gcport_si_match(device_t, cfdata_t, void *);
static void gcport_si_attach(device_t, device_t, void *);
static int siioctl_send(struct si_channel *ch, struct si_payload *sp);

dev_type_open(gcport_open);
dev_type_close(gcport_close);
dev_type_ioctl(gcport_ioctl);

const struct cdevsw gcport_cdevsw = {
.d_open = gcport_open,
.d_close = gcport_close,
.d_ioctl = gcport_ioctl,
.d_read = noread,
.d_write = nowrite,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER
};

CFATTACH_DECL_NEW(gcport_si, sizeof(struct gcport_softc),
gcport_si_match, gcport_si_attach, NULL, NULL);

static int
gcport_si_match(device_t parent, cfdata_t cf, void *aux)
{
struct si_softc * const sc = device_private(parent);
struct si_attach_args * const saa = aux;
struct si_channel *ch;
unsigned chan;

chan = saa->saa_index;
ch = &sc->sc_chan[chan];
aprint_normal("gcport: checking 0x%08X...\n", ch->ch_id);
/* NOTE - existing functionality uses uhid for gc_pad. This will
* match any other device
*/
if (ch->ch_id != 0 && !(IS_GCPAD(ch->ch_id))) {
aprint_normal("gcport: identified ch%d as a device 0x%08X\n",
chan, ch->ch_id);
return 1;
}

return 0;
}

static void
gcport_si_attach(device_t parent, device_t self, void *aux)
{
struct si_softc * const psc = device_private(parent);
struct si_attach_args * const saa = aux;
struct gcport_softc * const sc = device_private(self);
struct si_channel *ch = &psc->sc_chan[saa->saa_index];

sc->sc_dev = self;
sc->ch = ch;
sc->sc_bst = psc->sc_bst;
sc->sc_bsh = psc->sc_bsh;
}

int
gcport_open(dev_t dev, int flags, int mode, struct lwp *l)
{
struct gcport_softc *sc;
struct si_channel *ch;
int error;

/* TODO maybe there is a way to have the minor number reflect the
* actual port instead of an auto increment thing
*/
sc = device_lookup_private(&gcport_cd, minor(dev));

if (sc == NULL) {
return ENXIO;
}

ch = sc->ch;
mutex_enter(&ch->ch_lock);

if (ISSET(ch->ch_state, SI_STATE_OPEN)) {
error = EBUSY;
goto unlock;
}

ch->ch_state |= SI_STATE_OPEN;
error = 0;
unlock:
mutex_exit(&ch->ch_lock);
return error;
}

int
gcport_close(dev_t dev, int flags, int mode, struct lwp *l)
{
struct gcport_softc *sc = device_lookup_private(&gcport_cd, minor(dev));
struct si_channel *ch = sc->ch;

mutex_enter(&ch->ch_lock);
ch->ch_state &= ~(SI_STATE_OPEN | SI_STATE_STOPPED);

/* cv_init is called in parent's si_attach */
cv_broadcast(&ch->ch_cv);

mutex_exit(&ch->ch_lock);
return 0;
}

int
gcport_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
{
struct gcport_softc *sc = device_lookup_private(&gcport_cd, minor(dev));
struct si_channel *ch = sc->ch;
int err;

switch(cmd) {
case SI_SEND:
err = siioctl_send(ch, (struct si_payload *)data);
break;
default:
err = EINVAL;
break;
}

return err;
}

static int
siioctl_send(struct si_channel *ch, struct si_payload *gbs)
{
int err;
struct si_softc *sc;
struct siio_send sd;

err = 0;
sc = ch->ch_sc;
if (gbs->outsize > SIIOBUF_SIZE || gbs->insize > SIIOBUF_SIZE) {
return EINVAL;
}

sd.chan = ch->ch_index;
sd.outsize = gbs->outsize;
sd.insize = gbs->insize;
sd.out = kmem_alloc(gbs->outsize, KM_SLEEP);
sd.in = kmem_alloc(gbs->insize, KM_SLEEP);
sd.delay = gbs->delay;

if ((err = copyin(gbs->out, sd.out, sd.outsize)) != 0) {
goto si_send_cleanup;
}

if ((err = __si_send(sc, &sd)) != 0) {
goto si_send_cleanup;
}

if ((err = copyout(sd.in, gbs->in, sd.insize)) != 0) {
goto si_send_cleanup;
}

if ((err = copyout(&sd.status, gbs->status, sizeof(uint32_t))) != 0) {
goto si_send_cleanup;
}
si_send_cleanup:
kmem_free(sd.out, sd.outsize);
kmem_free(sd.in, sd.insize);
return err;
}
71 changes: 71 additions & 0 deletions sys/arch/evbppc/nintendo/dev/joybus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* $NetBSD: joybus.h,v 1.0 2026/05/18 22:54:30 gummybuns Exp $ */

/*-
* Copyright (c) 2026 Zac Brown <gummybuns@protonmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef _JOYBUS_H_
#define _JOYBUS_H_

#include <sys/param.h>
#include "si.h"

#define JB_WIRELESS __BIT(15)
#define JB_WIRELESS_RECV __BIT(14)
#define JB_RUMBLE __BIT(13)
#define JB_CONTROLLER __BIT(11)
#define JB_WIRELESS_TYPE __BIT(10)
#define JB_WIRELESS_STATE __BIT(9)
#define JB_DOLPHIN __BIT(8)
#define JB_WIRELESS_ORIGIN __BIT(5)
#define JB_WIRELESS_FIXID __BIT(4)
#define JB_WIRELESS_NONCTRL __BIT(3)
#define JB_WIRELESS_LITE __BIT(2)

#define CMD_IDENTIFY 0x00000000 /* pad with zeros to clear siiobuf */
#define CMD_RESET 0xFF000000 /* pad with zeros to clear siiobuf */
#define CMD_GBA_READ 0x14
#define CMD_GBA_WRITE 0x15

#define JB_NONE 0x0000
#define JB_N64 0x0500
#define JB_N64MIC 0x0001
#define JB_N64KB 0x0002
#define JB_N64MS 0x0200
#define JB_GBA 0x0004
#define JB_GC 0x0900
#define JB_WAVEBRD_RECV 0xe960
#define JB_WAVEBRD JB_WIRELESS & JB_RUMBLE & JB_CONTROLLER
#define JB_GCKB 0x0802
#define JB_GCSTEER 0x0800
#define JB_GBABIOS 0x08 /* GBA BIOS actually sends a 1byte response and
* sets SISR to NOREP. The second byte will be
* whatever was in SIIOBUF before send */

#define IS_DOLPHIN(n) ISSET(n, JB_CONTROLLER)
#define IS_N64(n) !ISSET(n, JB_CONTROLLER)
#define IS_GCPAD(n) (((n) & (JB_CONTROLLER | JB_DOLPHIN)) == JB_GC) || \
ISSET(n, JB_WIRELESS)
#endif
Loading