1/*- 2 * Copyright (c) 2003 Mathew Kanner 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD$"); 29 30#include <sys/param.h> 31#include <sys/types.h> 32#include <sys/param.h> 33#include <sys/queue.h> 34#include <sys/kernel.h> 35#include <sys/lock.h> 36#include <sys/mutex.h> 37#include <sys/proc.h> 38#include <sys/systm.h> 39#include <sys/kobj.h> 40#include <sys/malloc.h> 41#include <sys/bus.h> /* to get driver_intr_t */ 42 43#ifdef HAVE_KERNEL_OPTION_HEADERS 44#include "opt_snd.h" 45#endif 46 47#include <dev/sound/midi/mpu401.h> 48#include <dev/sound/midi/midi.h> 49 50#include "mpu_if.h" 51#include "mpufoi_if.h" 52 53#ifndef KOBJMETHOD_END 54#define KOBJMETHOD_END { NULL, NULL } 55#endif 56 57#define MPU_DATAPORT 0 58#define MPU_CMDPORT 1 59#define MPU_STATPORT 1 60#define MPU_RESET 0xff 61#define MPU_UART 0x3f 62#define MPU_ACK 0xfe 63#define MPU_STATMASK 0xc0 64#define MPU_OUTPUTBUSY 0x40 65#define MPU_INPUTBUSY 0x80 66#define MPU_TRYDATA 50 67#define MPU_DELAY 2500 68 69#define CMD(m,d) MPUFOI_WRITE(m, m->cookie, MPU_CMDPORT,d) 70#define STATUS(m) MPUFOI_READ(m, m->cookie, MPU_STATPORT) 71#define READ(m) MPUFOI_READ(m, m->cookie, MPU_DATAPORT) 72#define WRITE(m,d) MPUFOI_WRITE(m, m->cookie, MPU_DATAPORT,d) 73 74struct mpu401 { 75 KOBJ_FIELDS; 76 struct snd_midi *mid; 77 int flags; 78 driver_intr_t *si; 79 void *cookie; 80 struct callout timer; 81}; 82 83static void mpu401_timeout(void *m); 84static mpu401_intr_t mpu401_intr; 85 86static int mpu401_minit(struct snd_midi *, void *); 87static int mpu401_muninit(struct snd_midi *, void *); 88static int mpu401_minqsize(struct snd_midi *, void *); 89static int mpu401_moutqsize(struct snd_midi *, void *); 90static void mpu401_mcallback(struct snd_midi *, void *, int); 91static void mpu401_mcallbackp(struct snd_midi *, void *, int); 92static const char *mpu401_mdescr(struct snd_midi *, void *, int); 93static const char *mpu401_mprovider(struct snd_midi *, void *); 94 95static kobj_method_t mpu401_methods[] = { 96 KOBJMETHOD(mpu_init, mpu401_minit), 97 KOBJMETHOD(mpu_uninit, mpu401_muninit), 98 KOBJMETHOD(mpu_inqsize, mpu401_minqsize), 99 KOBJMETHOD(mpu_outqsize, mpu401_moutqsize), 100 KOBJMETHOD(mpu_callback, mpu401_mcallback), 101 KOBJMETHOD(mpu_callbackp, mpu401_mcallbackp), 102 KOBJMETHOD(mpu_descr, mpu401_mdescr), 103 KOBJMETHOD(mpu_provider, mpu401_mprovider), 104 KOBJMETHOD_END 105}; 106 107DEFINE_CLASS(mpu401, mpu401_methods, 0); 108 109void 110mpu401_timeout(void *a) 111{ 112 struct mpu401 *m = (struct mpu401 *)a; 113 114 if (m->si) 115 (m->si)(m->cookie); 116 117} 118static int 119mpu401_intr(struct mpu401 *m) 120{ 121#define MPU_INTR_BUF 16 122 MIDI_TYPE b[MPU_INTR_BUF]; 123 int i; 124 int s; 125 126/* 127 printf("mpu401_intr\n"); 128*/ 129#define RXRDY(m) ( (STATUS(m) & MPU_INPUTBUSY) == 0) 130#define TXRDY(m) ( (STATUS(m) & MPU_OUTPUTBUSY) == 0) 131#if 0 132#define D(x,l) printf("mpu401_intr %d %x %s %s\n",l, x, x&MPU_INPUTBUSY?"RX":"", x&MPU_OUTPUTBUSY?"TX":"") 133#else 134#define D(x,l) 135#endif 136 i = 0; 137 s = STATUS(m); 138 D(s, 1); 139 while ((s & MPU_INPUTBUSY) == 0 && i < MPU_INTR_BUF) { 140 b[i] = READ(m); 141/* 142 printf("mpu401_intr in i %d d %d\n", i, b[i]); 143*/ 144 i++; 145 s = STATUS(m); 146 } 147 if (i) 148 midi_in(m->mid, b, i); 149 i = 0; 150 while (!(s & MPU_OUTPUTBUSY) && i < MPU_INTR_BUF) { 151 if (midi_out(m->mid, b, 1)) { 152/* 153 printf("mpu401_intr out i %d d %d\n", i, b[0]); 154*/ 155 156 WRITE(m, *b); 157 } else { 158/* 159 printf("mpu401_intr write: no output\n"); 160*/ 161 return 0; 162 } 163 i++; 164 /* DELAY(100); */ 165 s = STATUS(m); 166 } 167 168 if ((m->flags & M_TXEN) && (m->si)) { 169 callout_reset(&m->timer, 1, mpu401_timeout, m); 170 } 171 return (m->flags & M_TXEN) == M_TXEN; 172} 173 174struct mpu401 * 175mpu401_init(kobj_class_t cls, void *cookie, driver_intr_t softintr, 176 mpu401_intr_t ** cb) 177{ 178 struct mpu401 *m; 179 180 *cb = NULL; 181 m = malloc(sizeof(*m), M_MIDI, M_NOWAIT | M_ZERO); 182 183 if (!m) 184 return NULL; 185 186 kobj_init((kobj_t)m, cls); 187 188 callout_init(&m->timer, 1); 189 190 m->si = softintr; 191 m->cookie = cookie; 192 m->flags = 0; 193 194 m->mid = midi_init(&mpu401_class, 0, 0, m); 195 if (!m->mid) 196 goto err; 197 *cb = mpu401_intr; 198 return m; 199err: 200 printf("mpu401_init error\n"); 201 free(m, M_MIDI); 202 return NULL; 203} 204 205int 206mpu401_uninit(struct mpu401 *m) 207{ 208 int retval; 209 210 CMD(m, MPU_RESET); 211 retval = midi_uninit(m->mid); 212 if (retval) 213 return retval; 214 free(m, M_MIDI); 215 return 0; 216} 217 218static int 219mpu401_minit(struct snd_midi *sm, void *arg) 220{ 221 struct mpu401 *m = arg; 222 int i; 223 224 CMD(m, MPU_RESET); 225 CMD(m, MPU_UART); 226 return 0; 227 i = 0; 228 while (++i < 2000) { 229 if (RXRDY(m)) 230 if (READ(m) == MPU_ACK) 231 break; 232 } 233 234 if (i < 2000) { 235 CMD(m, MPU_UART); 236 return 0; 237 } 238 printf("mpu401_minit failed active sensing\n"); 239 return 1; 240} 241 242 243int 244mpu401_muninit(struct snd_midi *sm, void *arg) 245{ 246 struct mpu401 *m = arg; 247 248 return MPUFOI_UNINIT(m, m->cookie); 249} 250 251int 252mpu401_minqsize(struct snd_midi *sm, void *arg) 253{ 254 return 128; 255} 256 257int 258mpu401_moutqsize(struct snd_midi *sm, void *arg) 259{ 260 return 128; 261} 262 263static void 264mpu401_mcallback(struct snd_midi *sm, void *arg, int flags) 265{ 266 struct mpu401 *m = arg; 267#if 0 268 printf("mpu401_callback %s %s %s %s\n", 269 flags & M_RX ? "M_RX" : "", 270 flags & M_TX ? "M_TX" : "", 271 flags & M_RXEN ? "M_RXEN" : "", 272 flags & M_TXEN ? "M_TXEN" : ""); 273#endif 274 if (flags & M_TXEN && m->si) { 275 callout_reset(&m->timer, 1, mpu401_timeout, m); 276 } 277 m->flags = flags; 278} 279 280static void 281mpu401_mcallbackp(struct snd_midi *sm, void *arg, int flags) 282{ 283/* printf("mpu401_callbackp\n"); */ 284 mpu401_mcallback(sm, arg, flags); 285} 286 287static const char * 288mpu401_mdescr(struct snd_midi *sm, void *arg, int verbosity) 289{ 290 291 return "descr mpu401"; 292} 293 294static const char * 295mpu401_mprovider(struct snd_midi *m, void *arg) 296{ 297 return "provider mpu401"; 298} 299