2004-04-23 21:25:25 +04:00
|
|
|
/* $NetBSD: umidi.c,v 1.21 2004/04/23 17:25:25 itojun Exp $ */
|
2001-01-31 02:26:47 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2001 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2003-12-04 16:57:30 +03:00
|
|
|
* by Takuya SHIOZAKI (tshiozak@NetBSD.org).
|
2001-01-31 02:26:47 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 09:24:53 +03:00
|
|
|
#include <sys/cdefs.h>
|
2004-04-23 21:25:25 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.21 2004/04/23 17:25:25 itojun Exp $");
|
2001-11-13 09:24:53 +03:00
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
|
|
|
|
#include <dev/usb/usb.h>
|
|
|
|
#include <dev/usb/usbdi.h>
|
|
|
|
#include <dev/usb/usbdi_util.h>
|
|
|
|
|
|
|
|
#include <dev/usb/usbdevs.h>
|
|
|
|
#include <dev/usb/uaudioreg.h>
|
|
|
|
#include <dev/usb/umidireg.h>
|
|
|
|
#include <dev/usb/umidivar.h>
|
|
|
|
#include <dev/usb/umidi_quirks.h>
|
|
|
|
|
|
|
|
#include <dev/midi_if.h>
|
|
|
|
|
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
#define DPRINTF(x) if (umididebug) printf x
|
|
|
|
#define DPRINTFN(n,x) if (umididebug >= (n)) printf x
|
|
|
|
int umididebug = 0;
|
|
|
|
#else
|
|
|
|
#define DPRINTF(x)
|
|
|
|
#define DPRINTFN(n,x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static int umidi_open(void *, int,
|
|
|
|
void (*)(void *, int), void (*)(void *), void *);
|
|
|
|
static void umidi_close(void *);
|
|
|
|
static int umidi_output(void *, int);
|
|
|
|
static void umidi_getinfo(void *, struct midi_info *);
|
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
static usbd_status alloc_pipe(struct umidi_endpoint *);
|
|
|
|
static void free_pipe(struct umidi_endpoint *);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
static usbd_status alloc_all_endpoints(struct umidi_softc *);
|
|
|
|
static void free_all_endpoints(struct umidi_softc *);
|
|
|
|
|
|
|
|
static usbd_status alloc_all_jacks(struct umidi_softc *);
|
|
|
|
static void free_all_jacks(struct umidi_softc *);
|
|
|
|
static usbd_status bind_jacks_to_mididev(struct umidi_softc *,
|
|
|
|
struct umidi_jack *,
|
|
|
|
struct umidi_jack *,
|
|
|
|
struct umidi_mididev *);
|
2001-02-03 21:50:31 +03:00
|
|
|
static void unbind_jacks_from_mididev(struct umidi_mididev *);
|
|
|
|
static void unbind_all_jacks(struct umidi_softc *);
|
2001-01-31 02:26:47 +03:00
|
|
|
static usbd_status assign_all_jacks_automatically(struct umidi_softc *);
|
2001-02-03 21:50:31 +03:00
|
|
|
static usbd_status open_out_jack(struct umidi_jack *, void *,
|
|
|
|
void (*)(void *));
|
|
|
|
static usbd_status open_in_jack(struct umidi_jack *, void *,
|
|
|
|
void (*)(void *, int));
|
|
|
|
static void close_out_jack(struct umidi_jack *);
|
|
|
|
static void close_in_jack(struct umidi_jack *);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
static usbd_status attach_mididev(struct umidi_softc *,
|
|
|
|
struct umidi_mididev *);
|
|
|
|
static usbd_status detach_mididev(struct umidi_mididev *, int);
|
|
|
|
static usbd_status deactivate_mididev(struct umidi_mididev *);
|
|
|
|
static usbd_status alloc_all_mididevs(struct umidi_softc *, int);
|
|
|
|
static void free_all_mididevs(struct umidi_softc *);
|
|
|
|
static usbd_status attach_all_mididevs(struct umidi_softc *);
|
|
|
|
static usbd_status detach_all_mididevs(struct umidi_softc *, int);
|
|
|
|
static usbd_status deactivate_all_mididevs(struct umidi_softc *);
|
|
|
|
|
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
static void dump_sc(struct umidi_softc *);
|
|
|
|
static void dump_ep(struct umidi_endpoint *);
|
|
|
|
static void dump_jack(struct umidi_jack *);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void init_packet(struct umidi_packet *);
|
|
|
|
|
|
|
|
static usbd_status start_input_transfer(struct umidi_endpoint *);
|
|
|
|
static usbd_status start_output_transfer(struct umidi_endpoint *);
|
2001-02-03 21:50:31 +03:00
|
|
|
static int out_jack_output(struct umidi_jack *, int);
|
2001-01-31 02:26:47 +03:00
|
|
|
static void in_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
|
|
|
|
static void out_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
|
|
|
|
static void out_build_packet(int, struct umidi_packet *, uByte);
|
|
|
|
|
|
|
|
|
|
|
|
struct midi_hw_if umidi_hw_if = {
|
|
|
|
umidi_open,
|
|
|
|
umidi_close,
|
|
|
|
umidi_output,
|
|
|
|
umidi_getinfo,
|
|
|
|
0, /* ioctl */
|
|
|
|
};
|
|
|
|
|
|
|
|
USB_DECLARE_DRIVER(umidi);
|
|
|
|
|
|
|
|
USB_MATCH(umidi)
|
|
|
|
{
|
|
|
|
USB_MATCH_START(umidi, uaa);
|
|
|
|
usb_interface_descriptor_t *id;
|
|
|
|
|
|
|
|
DPRINTFN(1,("umidi_match\n"));
|
|
|
|
|
|
|
|
if (uaa->iface == NULL)
|
|
|
|
return UMATCH_NONE;
|
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
if (umidi_search_quirk(uaa->vendor, uaa->product, uaa->ifaceno))
|
|
|
|
return UMATCH_IFACECLASS_IFACESUBCLASS;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
id = usbd_get_interface_descriptor(uaa->iface);
|
2002-07-12 01:14:24 +04:00
|
|
|
if (id!=NULL &&
|
2001-01-31 02:26:47 +03:00
|
|
|
id->bInterfaceClass==UICLASS_AUDIO &&
|
2001-02-03 19:49:06 +03:00
|
|
|
id->bInterfaceSubClass==UISUBCLASS_MIDISTREAM)
|
|
|
|
return UMATCH_IFACECLASS_IFACESUBCLASS;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
return UMATCH_NONE;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
USB_ATTACH(umidi)
|
|
|
|
{
|
|
|
|
usbd_status err;
|
|
|
|
USB_ATTACH_START(umidi, sc, uaa);
|
|
|
|
char devinfo[1024];
|
|
|
|
|
|
|
|
DPRINTFN(1,("umidi_attach\n"));
|
|
|
|
|
2004-04-23 21:25:25 +04:00
|
|
|
usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
|
2001-01-31 02:26:47 +03:00
|
|
|
printf("\n%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
|
|
|
|
|
|
|
|
sc->sc_iface = uaa->iface;
|
|
|
|
sc->sc_udev = uaa->device;
|
|
|
|
|
|
|
|
sc->sc_quirk =
|
|
|
|
umidi_search_quirk(uaa->vendor, uaa->product, uaa->ifaceno);
|
|
|
|
printf("%s: ", USBDEVNAME(sc->sc_dev));
|
|
|
|
umidi_print_quirk(sc->sc_quirk);
|
|
|
|
|
|
|
|
|
|
|
|
err = alloc_all_endpoints(sc);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
printf("%s: alloc_all_endpoints failed. (err=%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), err);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = alloc_all_jacks(sc);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
free_all_endpoints(sc);
|
|
|
|
printf("%s: alloc_all_jacks failed. (err=%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), err);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
printf("%s: out=%d, in=%d\n",
|
|
|
|
USBDEVNAME(sc->sc_dev),
|
|
|
|
sc->sc_out_num_jacks, sc->sc_in_num_jacks);
|
|
|
|
|
|
|
|
err = assign_all_jacks_automatically(sc);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
unbind_all_jacks(sc);
|
|
|
|
free_all_jacks(sc);
|
|
|
|
free_all_endpoints(sc);
|
|
|
|
printf("%s: assign_all_jacks_automatically failed. (err=%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), err);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
err = attach_all_mididevs(sc);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
free_all_jacks(sc);
|
|
|
|
free_all_endpoints(sc);
|
|
|
|
printf("%s: attach_all_mididevs failed. (err=%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), err);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
dump_sc(sc);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH,
|
|
|
|
sc->sc_udev, USBDEV(sc->sc_dev));
|
2002-07-12 01:14:24 +04:00
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
USB_ATTACH_SUCCESS_RETURN;
|
|
|
|
error:
|
|
|
|
printf("%s: disabled.\n", USBDEVNAME(sc->sc_dev));
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
USB_ATTACH_ERROR_RETURN;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
umidi_activate(device_ptr_t self, enum devact act)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_softc *sc = (struct umidi_softc *)self;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
switch (act) {
|
|
|
|
case DVACT_ACTIVATE:
|
|
|
|
DPRINTFN(1,("umidi_activate (activate)\n"));
|
|
|
|
|
|
|
|
return EOPNOTSUPP;
|
|
|
|
break;
|
|
|
|
case DVACT_DEACTIVATE:
|
|
|
|
DPRINTFN(1,("umidi_activate (deactivate)\n"));
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
deactivate_all_mididevs(sc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
USB_DETACH(umidi)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
USB_DETACH_START(umidi, sc);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
DPRINTFN(1,("umidi_detach\n"));
|
|
|
|
|
|
|
|
sc->sc_dying = 1;
|
|
|
|
detach_all_mididevs(sc, flags);
|
|
|
|
free_all_mididevs(sc);
|
|
|
|
free_all_jacks(sc);
|
|
|
|
free_all_endpoints(sc);
|
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
|
|
|
|
USBDEV(sc->sc_dev));
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
/*
|
|
|
|
* midi_if stuffs
|
|
|
|
*/
|
2001-01-31 02:26:47 +03:00
|
|
|
int
|
|
|
|
umidi_open(void *addr,
|
|
|
|
int flags,
|
2002-01-02 20:20:09 +03:00
|
|
|
void (*iintr)(void *, int),
|
|
|
|
void (*ointr)(void *),
|
2001-01-31 02:26:47 +03:00
|
|
|
void *arg)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_mididev *mididev = addr;
|
|
|
|
struct umidi_softc *sc = mididev->sc;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
DPRINTF(("umidi_open: sc=%p\n", sc));
|
|
|
|
|
|
|
|
if (!sc)
|
|
|
|
return ENXIO;
|
|
|
|
if (mididev->opened)
|
|
|
|
return EBUSY;
|
2001-02-03 21:50:31 +03:00
|
|
|
if (sc->sc_dying)
|
2001-01-31 02:26:47 +03:00
|
|
|
return EIO;
|
|
|
|
|
|
|
|
mididev->opened = 1;
|
2001-02-03 21:50:31 +03:00
|
|
|
mididev->flags = flags;
|
2001-01-31 02:26:47 +03:00
|
|
|
if ((mididev->flags & FWRITE) && mididev->out_jack)
|
2001-02-03 21:50:31 +03:00
|
|
|
open_out_jack(mididev->out_jack, arg, ointr);
|
2001-02-03 19:49:06 +03:00
|
|
|
if ((mididev->flags & FREAD) && mididev->in_jack) {
|
2001-02-03 21:50:31 +03:00
|
|
|
open_in_jack(mididev->in_jack, arg, iintr);
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
umidi_close(void *addr)
|
|
|
|
{
|
|
|
|
int s;
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_mididev *mididev = addr;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
if ((mididev->flags & FWRITE) && mididev->out_jack)
|
2001-02-03 21:50:31 +03:00
|
|
|
close_out_jack(mididev->out_jack);
|
2001-01-31 02:26:47 +03:00
|
|
|
if ((mididev->flags & FREAD) && mididev->in_jack)
|
2001-02-03 21:50:31 +03:00
|
|
|
close_in_jack(mididev->in_jack);
|
2001-01-31 02:26:47 +03:00
|
|
|
mididev->opened = 0;
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
umidi_output(void *addr, int d)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_mididev *mididev = addr;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
if (!mididev->out_jack || !mididev->opened)
|
2001-01-31 02:26:47 +03:00
|
|
|
return EIO;
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
return out_jack_output(mididev->out_jack, d);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
umidi_getinfo(void *addr, struct midi_info *mi)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_mididev *mididev = addr;
|
|
|
|
/* struct umidi_softc *sc = mididev->sc; */
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
mi->name = "USB MIDI I/F"; /* XXX: model name */
|
|
|
|
mi->props = MIDI_PROP_OUT_INTR;
|
2001-01-31 02:26:47 +03:00
|
|
|
if (mididev->in_jack)
|
|
|
|
mi->props |= MIDI_PROP_CAN_INPUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
/*
|
|
|
|
* each endpoint stuffs
|
|
|
|
*/
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
/* alloc/free pipe */
|
2001-01-31 02:26:47 +03:00
|
|
|
static usbd_status
|
2001-02-03 19:49:06 +03:00
|
|
|
alloc_pipe(struct umidi_endpoint *ep)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
|
|
|
struct umidi_softc *sc = ep->sc;
|
|
|
|
usbd_status err;
|
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
DPRINTF(("%s: alloc_pipe %p\n", USBDEVNAME(sc->sc_dev), ep));
|
2001-02-03 21:50:31 +03:00
|
|
|
LIST_INIT(&ep->queue_head);
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->xfer = usbd_alloc_xfer(sc->sc_udev);
|
2001-12-12 18:44:47 +03:00
|
|
|
if (ep->xfer == NULL) {
|
2001-02-03 19:49:06 +03:00
|
|
|
err = USBD_NOMEM;
|
|
|
|
goto quit;
|
|
|
|
}
|
|
|
|
ep->buffer = usbd_alloc_buffer(ep->xfer, UMIDI_PACKET_SIZE);
|
2001-12-12 18:44:47 +03:00
|
|
|
if (ep->buffer == NULL) {
|
2001-02-03 19:49:06 +03:00
|
|
|
usbd_free_xfer(ep->xfer);
|
|
|
|
err = USBD_NOMEM;
|
|
|
|
goto quit;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
err = usbd_open_pipe(sc->sc_iface, ep->addr, 0, &ep->pipe);
|
|
|
|
if (err)
|
|
|
|
usbd_free_xfer(ep->xfer);
|
2001-01-31 02:26:47 +03:00
|
|
|
quit:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-02-03 19:49:06 +03:00
|
|
|
free_pipe(struct umidi_endpoint *ep)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
DPRINTF(("%s: free_pipe %p\n", USBDEVNAME(ep->sc->sc_dev), ep));
|
|
|
|
usbd_abort_pipe(ep->pipe);
|
|
|
|
usbd_close_pipe(ep->pipe);
|
|
|
|
usbd_free_xfer(ep->xfer);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* alloc/free the array of endpoint structures */
|
|
|
|
|
|
|
|
static usbd_status alloc_all_endpoints_fixed_ep(struct umidi_softc *);
|
|
|
|
static usbd_status alloc_all_endpoints_yamaha(struct umidi_softc *);
|
|
|
|
static usbd_status alloc_all_endpoints_genuine(struct umidi_softc *);
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
alloc_all_endpoints(struct umidi_softc *sc)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
usbd_status err;
|
|
|
|
struct umidi_endpoint *ep;
|
|
|
|
int i;
|
2001-09-30 02:00:47 +04:00
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
if (UMQ_ISTYPE(sc, UMQ_TYPE_FIXED_EP)) {
|
|
|
|
err = alloc_all_endpoints_fixed_ep(sc);
|
|
|
|
} else if (UMQ_ISTYPE(sc, UMQ_TYPE_YAMAHA)) {
|
|
|
|
err = alloc_all_endpoints_yamaha(sc);
|
2001-01-31 02:26:47 +03:00
|
|
|
} else {
|
2001-02-03 19:49:06 +03:00
|
|
|
err = alloc_all_endpoints_genuine(sc);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
ep = sc->sc_endpoints;
|
2001-05-29 00:52:06 +04:00
|
|
|
for (i=sc->sc_out_num_endpoints+sc->sc_in_num_endpoints; i>0; i--) {
|
2001-02-03 19:49:06 +03:00
|
|
|
err = alloc_pipe(ep++);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
for (; ep!=sc->sc_endpoints; ep--)
|
|
|
|
free_pipe(ep-1);
|
|
|
|
free(sc->sc_endpoints, M_USBDEV);
|
|
|
|
sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_all_endpoints(struct umidi_softc *sc)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
int i;
|
2001-05-29 00:52:06 +04:00
|
|
|
for (i=0; i<sc->sc_in_num_endpoints+sc->sc_out_num_endpoints; i++)
|
2001-02-03 19:49:06 +03:00
|
|
|
free_pipe(&sc->sc_endpoints[i]);
|
2002-03-08 20:24:06 +03:00
|
|
|
if (sc->sc_endpoints != NULL)
|
|
|
|
free(sc->sc_endpoints, M_USBDEV);
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
alloc_all_endpoints_fixed_ep(struct umidi_softc *sc)
|
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
usbd_status err;
|
2001-01-31 02:26:47 +03:00
|
|
|
struct umq_fixed_ep_desc *fp;
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_endpoint *ep;
|
2001-01-31 02:26:47 +03:00
|
|
|
usb_endpoint_descriptor_t *epd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
fp = umidi_get_quirk_data_from_type(sc->sc_quirk,
|
|
|
|
UMQ_TYPE_FIXED_EP);
|
|
|
|
sc->sc_out_num_jacks = 0;
|
|
|
|
sc->sc_in_num_jacks = 0;
|
|
|
|
sc->sc_out_num_endpoints = fp->num_out_ep;
|
|
|
|
sc->sc_in_num_endpoints = fp->num_in_ep;
|
|
|
|
sc->sc_endpoints = malloc(sizeof(*sc->sc_out_ep)*
|
|
|
|
(sc->sc_out_num_endpoints+
|
|
|
|
sc->sc_in_num_endpoints),
|
|
|
|
M_USBDEV, M_WAITOK);
|
|
|
|
if (!sc->sc_endpoints) {
|
|
|
|
return USBD_NOMEM;
|
|
|
|
}
|
|
|
|
sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
|
|
|
|
sc->sc_in_ep =
|
|
|
|
sc->sc_in_num_endpoints ?
|
|
|
|
sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
|
2001-02-03 19:49:06 +03:00
|
|
|
|
|
|
|
ep = &sc->sc_out_ep[0];
|
2001-01-31 02:26:47 +03:00
|
|
|
for (i=0; i<sc->sc_out_num_endpoints; i++) {
|
|
|
|
epd = usbd_interface2endpoint_descriptor(
|
|
|
|
sc->sc_iface,
|
|
|
|
fp->out_ep[i].ep);
|
|
|
|
if (!epd) {
|
|
|
|
printf("%s: cannot get endpoint descriptor(out:%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), fp->out_ep[i].ep);
|
2001-02-03 19:49:06 +03:00
|
|
|
err = USBD_INVAL;
|
2001-01-31 02:26:47 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (UE_GET_XFERTYPE(epd->bmAttributes)!=UE_BULK ||
|
|
|
|
UE_GET_DIR(epd->bEndpointAddress)!=UE_DIR_OUT) {
|
|
|
|
printf("%s: illegal endpoint(out:%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), fp->out_ep[i].ep);
|
2001-02-03 19:49:06 +03:00
|
|
|
err = USBD_INVAL;
|
2001-01-31 02:26:47 +03:00
|
|
|
goto error;
|
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->sc = sc;
|
|
|
|
ep->addr = epd->bEndpointAddress;
|
|
|
|
ep->num_jacks = fp->out_ep[i].num_jacks;
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_out_num_jacks += fp->out_ep[i].num_jacks;
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->num_open = 0;
|
|
|
|
memset(ep->jacks, 0, sizeof(ep->jacks));
|
2001-02-03 21:50:31 +03:00
|
|
|
LIST_INIT(&ep->queue_head);
|
2001-02-03 19:49:06 +03:00
|
|
|
ep++;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
ep = &sc->sc_in_ep[0];
|
2001-01-31 02:26:47 +03:00
|
|
|
for (i=0; i<sc->sc_in_num_endpoints; i++) {
|
|
|
|
epd = usbd_interface2endpoint_descriptor(
|
|
|
|
sc->sc_iface,
|
|
|
|
fp->in_ep[i].ep);
|
|
|
|
if (!epd) {
|
|
|
|
printf("%s: cannot get endpoint descriptor(in:%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), fp->in_ep[i].ep);
|
2001-02-03 19:49:06 +03:00
|
|
|
err = USBD_INVAL;
|
2001-01-31 02:26:47 +03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (UE_GET_XFERTYPE(epd->bmAttributes)!=UE_BULK ||
|
|
|
|
UE_GET_DIR(epd->bEndpointAddress)!=UE_DIR_IN) {
|
|
|
|
printf("%s: illegal endpoint(in:%d)\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), fp->in_ep[i].ep);
|
2001-02-03 19:49:06 +03:00
|
|
|
err = USBD_INVAL;
|
2001-01-31 02:26:47 +03:00
|
|
|
goto error;
|
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->sc = sc;
|
|
|
|
ep->addr = epd->bEndpointAddress;
|
|
|
|
ep->num_jacks = fp->in_ep[i].num_jacks;
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_in_num_jacks += fp->in_ep[i].num_jacks;
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->num_open = 0;
|
|
|
|
memset(ep->jacks, 0, sizeof(ep->jacks));
|
|
|
|
ep++;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
error:
|
|
|
|
free(sc->sc_endpoints, M_USBDEV);
|
|
|
|
sc->sc_endpoints = NULL;
|
2001-02-03 19:49:06 +03:00
|
|
|
return err;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
alloc_all_endpoints_yamaha(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
/* This driver currently supports max 1in/1out bulk endpoints */
|
|
|
|
usb_descriptor_t *desc;
|
2001-05-29 00:52:06 +04:00
|
|
|
usb_endpoint_descriptor_t *epd;
|
|
|
|
int out_addr, in_addr, i;
|
|
|
|
int dir;
|
2001-01-31 02:26:47 +03:00
|
|
|
size_t remain, descsize;
|
|
|
|
|
2001-05-29 00:52:06 +04:00
|
|
|
sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
|
|
|
|
out_addr = in_addr = 0;
|
|
|
|
|
|
|
|
/* detect endpoints */
|
2001-01-31 02:26:47 +03:00
|
|
|
desc = TO_D(usbd_get_interface_descriptor(sc->sc_iface));
|
2001-05-29 00:52:06 +04:00
|
|
|
for (i=(int)TO_IFD(desc)->bNumEndpoints-1; i>=0; i--) {
|
|
|
|
epd = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
|
|
|
|
if (UE_GET_XFERTYPE(epd->bmAttributes) == UE_BULK) {
|
|
|
|
dir = UE_GET_DIR(epd->bEndpointAddress);
|
|
|
|
if (dir==UE_DIR_OUT && !out_addr)
|
|
|
|
out_addr = epd->bEndpointAddress;
|
|
|
|
else if (dir==UE_DIR_IN && !in_addr)
|
|
|
|
in_addr = epd->bEndpointAddress;
|
|
|
|
}
|
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
desc = NEXT_D(desc);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-05-29 00:52:06 +04:00
|
|
|
/* count jacks */
|
|
|
|
if (!(desc->bDescriptorType==UDESC_CS_INTERFACE &&
|
|
|
|
desc->bDescriptorSubtype==UMIDI_MS_HEADER))
|
|
|
|
return USBD_INVAL;
|
|
|
|
remain = (size_t)UGETW(TO_CSIFD(desc)->wTotalLength) -
|
|
|
|
(size_t)desc->bLength;
|
|
|
|
desc = NEXT_D(desc);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
while (remain>=sizeof(usb_descriptor_t)) {
|
|
|
|
descsize = desc->bLength;
|
|
|
|
if (descsize>remain || descsize==0)
|
|
|
|
break;
|
|
|
|
if (desc->bDescriptorType==UDESC_CS_INTERFACE &&
|
|
|
|
remain>=UMIDI_JACK_DESCRIPTOR_SIZE) {
|
|
|
|
if (desc->bDescriptorSubtype==UMIDI_OUT_JACK)
|
|
|
|
sc->sc_out_num_jacks++;
|
|
|
|
else if (desc->bDescriptorSubtype==UMIDI_IN_JACK)
|
|
|
|
sc->sc_in_num_jacks++;
|
|
|
|
}
|
|
|
|
desc = NEXT_D(desc);
|
|
|
|
remain-=descsize;
|
|
|
|
}
|
|
|
|
|
2001-05-29 00:52:06 +04:00
|
|
|
/* validate some parameters */
|
2001-01-31 02:26:47 +03:00
|
|
|
if (sc->sc_out_num_jacks>UMIDI_MAX_EPJACKS)
|
|
|
|
sc->sc_out_num_jacks = UMIDI_MAX_EPJACKS;
|
|
|
|
if (sc->sc_in_num_jacks>UMIDI_MAX_EPJACKS)
|
|
|
|
sc->sc_in_num_jacks = UMIDI_MAX_EPJACKS;
|
2001-05-29 00:52:06 +04:00
|
|
|
if (sc->sc_out_num_jacks && out_addr) {
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_out_num_endpoints = 1;
|
|
|
|
} else {
|
|
|
|
sc->sc_out_num_endpoints = 0;
|
|
|
|
sc->sc_out_num_jacks = 0;
|
|
|
|
}
|
2001-05-29 00:52:06 +04:00
|
|
|
if (sc->sc_in_num_jacks && in_addr) {
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_in_num_endpoints = 1;
|
|
|
|
} else {
|
|
|
|
sc->sc_in_num_endpoints = 0;
|
|
|
|
sc->sc_in_num_jacks = 0;
|
|
|
|
}
|
|
|
|
sc->sc_endpoints = malloc(sizeof(struct umidi_endpoint)*
|
|
|
|
(sc->sc_out_num_endpoints+
|
|
|
|
sc->sc_in_num_endpoints),
|
|
|
|
M_USBDEV, M_WAITOK);
|
|
|
|
if (!sc->sc_endpoints)
|
|
|
|
return USBD_NOMEM;
|
|
|
|
if (sc->sc_out_num_endpoints) {
|
|
|
|
sc->sc_out_ep = sc->sc_endpoints;
|
|
|
|
sc->sc_out_ep->sc = sc;
|
|
|
|
sc->sc_out_ep->addr = out_addr;
|
|
|
|
sc->sc_out_ep->num_jacks = sc->sc_out_num_jacks;
|
|
|
|
sc->sc_out_ep->num_open = 0;
|
|
|
|
memset(sc->sc_out_ep->jacks, 0, sizeof(sc->sc_out_ep->jacks));
|
|
|
|
} else
|
|
|
|
sc->sc_out_ep = NULL;
|
|
|
|
|
|
|
|
if (sc->sc_in_num_endpoints) {
|
|
|
|
sc->sc_in_ep = sc->sc_endpoints+sc->sc_out_num_endpoints;
|
|
|
|
sc->sc_in_ep->sc = sc;
|
|
|
|
sc->sc_in_ep->addr = in_addr;
|
|
|
|
sc->sc_in_ep->num_jacks = sc->sc_in_num_jacks;
|
|
|
|
sc->sc_in_ep->num_open = 0;
|
|
|
|
memset(sc->sc_in_ep->jacks, 0, sizeof(sc->sc_in_ep->jacks));
|
|
|
|
} else
|
|
|
|
sc->sc_in_ep = NULL;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
alloc_all_endpoints_genuine(struct umidi_softc *sc)
|
|
|
|
{
|
2004-01-19 10:36:35 +03:00
|
|
|
usb_interface_descriptor_t *interface_desc;
|
|
|
|
usb_config_descriptor_t *config_desc;
|
2001-01-31 02:26:47 +03:00
|
|
|
usb_descriptor_t *desc;
|
2001-05-25 23:33:36 +04:00
|
|
|
int num_ep;
|
2001-01-31 02:26:47 +03:00
|
|
|
size_t remain, descsize;
|
|
|
|
struct umidi_endpoint *p, *q, *lowest, *endep, tmpep;
|
|
|
|
int epaddr;
|
|
|
|
|
2004-01-19 10:36:35 +03:00
|
|
|
interface_desc = usbd_get_interface_descriptor(sc->sc_iface);
|
|
|
|
num_ep = interface_desc->bNumEndpoints;
|
|
|
|
sc->sc_endpoints = p = malloc(sizeof(struct umidi_endpoint) * num_ep,
|
2001-01-31 02:26:47 +03:00
|
|
|
M_USBDEV, M_WAITOK);
|
|
|
|
if (!p)
|
|
|
|
return USBD_NOMEM;
|
|
|
|
|
|
|
|
sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
|
|
|
|
sc->sc_out_num_endpoints = sc->sc_in_num_endpoints = 0;
|
|
|
|
epaddr = -1;
|
|
|
|
|
|
|
|
/* get the list of endpoints for midi stream */
|
2004-01-19 10:36:35 +03:00
|
|
|
config_desc = usbd_get_config_descriptor(sc->sc_udev);
|
|
|
|
desc = (usb_descriptor_t *) config_desc;
|
|
|
|
remain = (size_t)UGETW(config_desc->wTotalLength);
|
2001-01-31 02:26:47 +03:00
|
|
|
while (remain>=sizeof(usb_descriptor_t)) {
|
|
|
|
descsize = desc->bLength;
|
|
|
|
if (descsize>remain || descsize==0)
|
|
|
|
break;
|
|
|
|
if (desc->bDescriptorType==UDESC_ENDPOINT &&
|
|
|
|
remain>=USB_ENDPOINT_DESCRIPTOR_SIZE &&
|
|
|
|
UE_GET_XFERTYPE(TO_EPD(desc)->bmAttributes) == UE_BULK) {
|
|
|
|
epaddr = TO_EPD(desc)->bEndpointAddress;
|
|
|
|
} else if (desc->bDescriptorType==UDESC_CS_ENDPOINT &&
|
|
|
|
remain>=UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE &&
|
|
|
|
epaddr!=-1) {
|
|
|
|
if (num_ep>0) {
|
2001-09-30 02:00:47 +04:00
|
|
|
num_ep--;
|
2001-01-31 02:26:47 +03:00
|
|
|
p->sc = sc;
|
|
|
|
p->addr = epaddr;
|
|
|
|
p->num_jacks = TO_CSEPD(desc)->bNumEmbMIDIJack;
|
|
|
|
if (UE_GET_DIR(epaddr)==UE_DIR_OUT) {
|
|
|
|
sc->sc_out_num_endpoints++;
|
|
|
|
sc->sc_out_num_jacks += p->num_jacks;
|
|
|
|
} else {
|
|
|
|
sc->sc_in_num_endpoints++;
|
|
|
|
sc->sc_in_num_jacks += p->num_jacks;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
epaddr = -1;
|
|
|
|
desc = NEXT_D(desc);
|
|
|
|
remain-=descsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort endpoints */
|
|
|
|
num_ep = sc->sc_out_num_endpoints + sc->sc_in_num_endpoints;
|
|
|
|
p = sc->sc_endpoints;
|
|
|
|
endep = p + num_ep;
|
|
|
|
while (p<endep) {
|
|
|
|
lowest = p;
|
|
|
|
for (q=p+1; q<endep; q++) {
|
|
|
|
if ((UE_GET_DIR(lowest->addr)==UE_DIR_IN &&
|
|
|
|
UE_GET_DIR(q->addr)==UE_DIR_OUT) ||
|
|
|
|
((UE_GET_DIR(lowest->addr)==
|
|
|
|
UE_GET_DIR(q->addr)) &&
|
|
|
|
(UE_GET_ADDR(lowest->addr)>
|
|
|
|
UE_GET_ADDR(q->addr))))
|
|
|
|
lowest = q;
|
|
|
|
}
|
|
|
|
if (lowest != p) {
|
|
|
|
memcpy((void *)&tmpep, (void *)p, sizeof(tmpep));
|
|
|
|
memcpy((void *)p, (void *)lowest, sizeof(tmpep));
|
|
|
|
memcpy((void *)lowest, (void *)&tmpep, sizeof(tmpep));
|
|
|
|
}
|
2001-05-25 23:33:36 +04:00
|
|
|
p->num_open = 0;
|
|
|
|
p++;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2002-07-12 01:14:24 +04:00
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
|
|
|
|
sc->sc_in_ep =
|
|
|
|
sc->sc_in_num_endpoints ?
|
|
|
|
sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
/*
|
|
|
|
* jack stuffs
|
|
|
|
*/
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
static usbd_status
|
|
|
|
alloc_all_jacks(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct umidi_endpoint *ep;
|
|
|
|
struct umidi_jack *jack, **rjack;
|
|
|
|
|
|
|
|
/* allocate/initialize structures */
|
|
|
|
sc->sc_jacks =
|
|
|
|
malloc(sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks+
|
|
|
|
sc->sc_out_num_jacks),
|
|
|
|
M_USBDEV, M_WAITOK);
|
|
|
|
if (!sc->sc_jacks)
|
|
|
|
return USBD_NOMEM;
|
|
|
|
sc->sc_out_jacks =
|
|
|
|
sc->sc_out_num_jacks ? sc->sc_jacks : NULL;
|
|
|
|
sc->sc_in_jacks =
|
|
|
|
sc->sc_in_num_jacks ? sc->sc_jacks+sc->sc_out_num_jacks : NULL;
|
|
|
|
|
|
|
|
jack = &sc->sc_out_jacks[0];
|
|
|
|
for (i=0; i<sc->sc_out_num_jacks; i++) {
|
2001-02-03 21:50:31 +03:00
|
|
|
jack->opened = 0;
|
|
|
|
jack->binded = 0;
|
|
|
|
jack->arg = NULL;
|
|
|
|
jack->u.out.intr = NULL;
|
2001-01-31 02:26:47 +03:00
|
|
|
jack->cable_number = i;
|
|
|
|
jack++;
|
|
|
|
}
|
|
|
|
jack = &sc->sc_in_jacks[0];
|
|
|
|
for (i=0; i<sc->sc_in_num_jacks; i++) {
|
2001-02-03 21:50:31 +03:00
|
|
|
jack->opened = 0;
|
|
|
|
jack->binded = 0;
|
|
|
|
jack->arg = NULL;
|
|
|
|
jack->u.in.intr = NULL;
|
2001-01-31 02:26:47 +03:00
|
|
|
jack->cable_number = i;
|
|
|
|
jack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assign each jacks to each endpoints */
|
|
|
|
jack = &sc->sc_out_jacks[0];
|
|
|
|
ep = &sc->sc_out_ep[0];
|
|
|
|
for (i=0; i<sc->sc_out_num_endpoints; i++) {
|
|
|
|
rjack = &ep->jacks[0];
|
|
|
|
for (j=0; j<ep->num_jacks; j++) {
|
|
|
|
*rjack = jack;
|
|
|
|
jack->endpoint = ep;
|
|
|
|
jack++;
|
|
|
|
rjack++;
|
|
|
|
}
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
jack = &sc->sc_in_jacks[0];
|
|
|
|
ep = &sc->sc_in_ep[0];
|
|
|
|
for (i=0; i<sc->sc_in_num_endpoints; i++) {
|
|
|
|
rjack = &ep->jacks[0];
|
|
|
|
for (j=0; j<ep->num_jacks; j++) {
|
|
|
|
*rjack = jack;
|
|
|
|
jack->endpoint = ep;
|
|
|
|
jack++;
|
|
|
|
rjack++;
|
|
|
|
}
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_all_jacks(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
|
|
|
s = splaudio();
|
|
|
|
if (sc->sc_out_jacks) {
|
|
|
|
free(sc->sc_jacks, M_USBDEV);
|
|
|
|
sc->sc_jacks = sc->sc_in_jacks = sc->sc_out_jacks = NULL;
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
bind_jacks_to_mididev(struct umidi_softc *sc,
|
|
|
|
struct umidi_jack *out_jack,
|
|
|
|
struct umidi_jack *in_jack,
|
|
|
|
struct umidi_mididev *mididev)
|
|
|
|
{
|
2001-05-29 00:52:06 +04:00
|
|
|
if ((out_jack && out_jack->binded) || (in_jack && in_jack->binded))
|
2001-02-03 21:50:31 +03:00
|
|
|
return USBD_IN_USE;
|
|
|
|
if (mididev->out_jack || mididev->in_jack)
|
2001-01-31 02:26:47 +03:00
|
|
|
return USBD_IN_USE;
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
if (out_jack)
|
|
|
|
out_jack->binded = 1;
|
|
|
|
if (in_jack)
|
|
|
|
in_jack->binded = 1;
|
2001-01-31 02:26:47 +03:00
|
|
|
mididev->in_jack = in_jack;
|
|
|
|
mididev->out_jack = out_jack;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
static void
|
2001-01-31 02:26:47 +03:00
|
|
|
unbind_jacks_from_mididev(struct umidi_mididev *mididev)
|
|
|
|
{
|
2002-06-19 17:51:34 +04:00
|
|
|
if ((mididev->flags & FWRITE) && mididev->out_jack)
|
2001-02-03 21:50:31 +03:00
|
|
|
close_out_jack(mididev->out_jack);
|
2002-06-19 17:51:34 +04:00
|
|
|
if ((mididev->flags & FREAD) && mididev->in_jack)
|
2001-02-03 21:50:31 +03:00
|
|
|
close_in_jack(mididev->in_jack);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
if (mididev->out_jack)
|
|
|
|
mididev->out_jack->binded = 0;
|
|
|
|
if (mididev->in_jack)
|
|
|
|
mididev->in_jack->binded = 0;
|
|
|
|
mididev->out_jack = mididev->in_jack = NULL;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
static void
|
2001-01-31 02:26:47 +03:00
|
|
|
unbind_all_jacks(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sc->sc_mididevs)
|
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
2001-02-03 21:50:31 +03:00
|
|
|
unbind_jacks_from_mididev(&sc->sc_mididevs[i]);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
assign_all_jacks_automatically(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
usbd_status err;
|
|
|
|
int i;
|
|
|
|
struct umidi_jack *out, *in;
|
|
|
|
|
|
|
|
err =
|
|
|
|
alloc_all_mididevs(sc,
|
|
|
|
max(sc->sc_out_num_jacks, sc->sc_in_num_jacks));
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
|
|
|
out = (i<sc->sc_out_num_jacks) ? &sc->sc_out_jacks[i]:NULL;
|
|
|
|
in = (i<sc->sc_in_num_jacks) ? &sc->sc_in_jacks[i]:NULL;
|
|
|
|
err = bind_jacks_to_mididev(sc, out, in, &sc->sc_mididevs[i]);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION) {
|
|
|
|
free_all_mididevs(sc);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
2001-02-03 21:50:31 +03:00
|
|
|
open_out_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *))
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_endpoint *ep = jack->endpoint;
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
if (jack->opened)
|
|
|
|
return USBD_IN_USE;
|
|
|
|
|
|
|
|
jack->arg = arg;
|
|
|
|
jack->u.out.intr = intr;
|
2001-02-03 19:49:06 +03:00
|
|
|
init_packet(&jack->packet);
|
2001-02-03 21:50:31 +03:00
|
|
|
jack->opened = 1;
|
|
|
|
ep->num_open++;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
open_in_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *, int))
|
|
|
|
{
|
|
|
|
usbd_status err = USBD_NORMAL_COMPLETION;
|
|
|
|
struct umidi_endpoint *ep = jack->endpoint;
|
|
|
|
|
|
|
|
if (jack->opened)
|
|
|
|
return USBD_IN_USE;
|
|
|
|
|
|
|
|
jack->arg = arg;
|
|
|
|
jack->u.in.intr = intr;
|
|
|
|
jack->opened = 1;
|
2001-02-03 19:49:06 +03:00
|
|
|
if (ep->num_open++==0 && UE_GET_DIR(ep->addr)==UE_DIR_IN) {
|
|
|
|
err = start_input_transfer(ep);
|
2003-12-05 09:01:59 +03:00
|
|
|
if (err != USBD_NORMAL_COMPLETION &&
|
|
|
|
err != USBD_IN_PROGRESS) {
|
2001-02-03 19:49:06 +03:00
|
|
|
ep->num_open--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-02-03 21:50:31 +03:00
|
|
|
close_out_jack(struct umidi_jack *jack)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_jack *tail;
|
|
|
|
int s;
|
2001-02-03 21:50:31 +03:00
|
|
|
|
|
|
|
if (jack->opened) {
|
|
|
|
s = splusb();
|
2002-06-19 17:51:34 +04:00
|
|
|
LIST_FOREACH(tail,
|
|
|
|
&jack->endpoint->queue_head,
|
|
|
|
u.out.queue_entry)
|
|
|
|
if (tail == jack) {
|
|
|
|
LIST_REMOVE(jack, u.out.queue_entry);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (jack == jack->endpoint->queue_tail) {
|
2001-02-03 21:50:31 +03:00
|
|
|
/* find tail */
|
|
|
|
LIST_FOREACH(tail,
|
|
|
|
&jack->endpoint->queue_head,
|
|
|
|
u.out.queue_entry) {
|
|
|
|
if (!LIST_NEXT(tail, u.out.queue_entry)) {
|
|
|
|
jack->endpoint->queue_tail = tail;
|
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
splx(s);
|
|
|
|
jack->opened = 0;
|
|
|
|
jack->endpoint->num_open--;
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
static void
|
|
|
|
close_in_jack(struct umidi_jack *jack)
|
|
|
|
{
|
|
|
|
if (jack->opened) {
|
|
|
|
jack->opened = 0;
|
2004-01-06 08:42:47 +03:00
|
|
|
if (--jack->endpoint->num_open == 0) {
|
|
|
|
usbd_abort_pipe(jack->endpoint->pipe);
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
}
|
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
attach_mididev(struct umidi_softc *sc, struct umidi_mididev *mididev)
|
|
|
|
{
|
|
|
|
if (mididev->sc)
|
|
|
|
return USBD_IN_USE;
|
|
|
|
|
|
|
|
mididev->sc = sc;
|
|
|
|
|
|
|
|
mididev->mdev = midi_attach_mi(&umidi_hw_if, mididev, &sc->sc_dev);
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
detach_mididev(struct umidi_mididev *mididev, int flags)
|
|
|
|
{
|
|
|
|
if (!mididev->sc)
|
|
|
|
return USBD_NO_ADDR;
|
|
|
|
|
|
|
|
if (mididev->opened) {
|
|
|
|
umidi_close(mididev);
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
unbind_jacks_from_mididev(mididev);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
if (mididev->mdev)
|
|
|
|
config_detach(mididev->mdev, flags);
|
|
|
|
|
|
|
|
mididev->sc = NULL;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
deactivate_mididev(struct umidi_mididev *mididev)
|
|
|
|
{
|
2001-02-03 21:50:31 +03:00
|
|
|
if (mididev->out_jack)
|
|
|
|
mididev->out_jack->binded = 0;
|
|
|
|
if (mididev->in_jack)
|
|
|
|
mididev->in_jack->binded = 0;
|
2001-01-31 02:26:47 +03:00
|
|
|
config_deactivate(mididev->mdev);
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
alloc_all_mididevs(struct umidi_softc *sc, int nmidi)
|
|
|
|
{
|
|
|
|
sc->sc_num_mididevs = nmidi;
|
|
|
|
sc->sc_mididevs = malloc(sizeof(*sc->sc_mididevs)*nmidi,
|
2002-01-12 19:58:16 +03:00
|
|
|
M_USBDEV, M_WAITOK|M_ZERO);
|
2001-01-31 02:26:47 +03:00
|
|
|
if (!sc->sc_mididevs)
|
|
|
|
return USBD_NOMEM;
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_all_mididevs(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
sc->sc_num_mididevs = 0;
|
|
|
|
if (sc->sc_mididevs)
|
|
|
|
free(sc->sc_mididevs, M_USBDEV);
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
attach_all_mididevs(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
usbd_status err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sc->sc_mididevs)
|
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
|
|
|
err = attach_mididev(sc, &sc->sc_mididevs[i]);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
detach_all_mididevs(struct umidi_softc *sc, int flags)
|
|
|
|
{
|
|
|
|
usbd_status err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sc->sc_mididevs)
|
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
|
|
|
err = detach_mididev(&sc->sc_mididevs[i], flags);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
deactivate_all_mididevs(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
usbd_status err;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sc->sc_mididevs)
|
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
|
|
|
err = deactivate_mididev(&sc->sc_mididevs[i]);
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return USBD_NORMAL_COMPLETION;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
static void
|
|
|
|
dump_sc(struct umidi_softc *sc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
DPRINTFN(10, ("%s: dump_sc\n", USBDEVNAME(sc->sc_dev)));
|
|
|
|
for (i=0; i<sc->sc_out_num_endpoints; i++) {
|
|
|
|
DPRINTFN(10, ("\tout_ep(%p):\n", &sc->sc_out_ep[i]));
|
|
|
|
dump_ep(&sc->sc_out_ep[i]);
|
|
|
|
}
|
|
|
|
for (i=0; i<sc->sc_in_num_endpoints; i++) {
|
|
|
|
DPRINTFN(10, ("\tin_ep(%p):\n", &sc->sc_in_ep[i]));
|
|
|
|
dump_ep(&sc->sc_in_ep[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_ep(struct umidi_endpoint *ep)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<ep->num_jacks; i++) {
|
|
|
|
DPRINTFN(10, ("\t\tjack(%p):\n", ep->jacks[i]));
|
|
|
|
dump_jack(ep->jacks[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
dump_jack(struct umidi_jack *jack)
|
|
|
|
{
|
2002-06-19 17:51:34 +04:00
|
|
|
DPRINTFN(10, ("\t\t\tep=%p\n",
|
|
|
|
jack->endpoint));
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* UMIDI_DEBUG */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* MUX MIDI PACKET
|
|
|
|
*/
|
|
|
|
|
2001-02-22 00:39:52 +03:00
|
|
|
static const int packet_length[16] = {
|
2001-01-31 02:26:47 +03:00
|
|
|
/*0*/ -1,
|
|
|
|
/*1*/ -1,
|
|
|
|
/*2*/ 2,
|
|
|
|
/*3*/ 3,
|
|
|
|
/*4*/ 3,
|
|
|
|
/*5*/ 1,
|
|
|
|
/*6*/ 2,
|
|
|
|
/*7*/ 3,
|
|
|
|
/*8*/ 3,
|
|
|
|
/*9*/ 3,
|
|
|
|
/*A*/ 3,
|
|
|
|
/*B*/ 3,
|
|
|
|
/*C*/ 2,
|
|
|
|
/*D*/ 2,
|
|
|
|
/*E*/ 3,
|
|
|
|
/*F*/ 1,
|
|
|
|
};
|
|
|
|
|
2001-02-22 00:39:52 +03:00
|
|
|
static const struct {
|
2001-01-31 02:26:47 +03:00
|
|
|
int cin;
|
|
|
|
packet_state_t next;
|
|
|
|
} packet_0xFX[16] = {
|
|
|
|
/*F0: SysEx */ { 0x04, PS_EXCL_1 },
|
|
|
|
/*F1: MTC */ { 0x02, PS_NORMAL_1OF2 },
|
|
|
|
/*F2: S.POS */ { 0x03, PS_NORMAL_1OF3 },
|
|
|
|
/*F3: S.SEL */ { 0x02, PS_NORMAL_1OF2 },
|
|
|
|
/*F4: UNDEF */ { 0x00, PS_INITIAL },
|
|
|
|
/*F5: UNDEF */ { 0x00, PS_INITIAL },
|
|
|
|
/*F6: Tune */ { 0x0F, PS_END },
|
|
|
|
/*F7: EofEx */ { 0x00, PS_INITIAL },
|
|
|
|
/*F8: Timing */ { 0x0F, PS_END },
|
|
|
|
/*F9: UNDEF */ { 0x00, PS_INITIAL },
|
|
|
|
/*FA: Start */ { 0x0F, PS_END },
|
|
|
|
/*FB: Cont */ { 0x0F, PS_END },
|
|
|
|
/*FC: Stop */ { 0x0F, PS_END },
|
|
|
|
/*FD: UNDEF */ { 0x00, PS_INITIAL },
|
|
|
|
/*FE: ActS */ { 0x0F, PS_END },
|
|
|
|
/*FF: Reset */ { 0x0F, PS_END },
|
|
|
|
};
|
|
|
|
|
|
|
|
#define GET_CN(p) (((unsigned char)(p)>>4)&0x0F)
|
|
|
|
#define GET_CIN(p) ((unsigned char)(p)&0x0F)
|
|
|
|
#define MIX_CN_CIN(cn, cin) \
|
|
|
|
((unsigned char)((((unsigned char)(cn)&0x0F)<<4)| \
|
2001-02-03 19:49:06 +03:00
|
|
|
((unsigned char)(cin)&0x0F)))
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_packet(struct umidi_packet *packet)
|
|
|
|
{
|
|
|
|
memset(packet->buffer, 0, UMIDI_PACKET_SIZE);
|
|
|
|
packet->state = PS_INITIAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
start_input_transfer(struct umidi_endpoint *ep)
|
|
|
|
{
|
|
|
|
usbd_setup_xfer(ep->xfer, ep->pipe,
|
|
|
|
(usbd_private_handle)ep,
|
|
|
|
ep->buffer, UMIDI_PACKET_SIZE,
|
2001-12-12 18:44:47 +03:00
|
|
|
USBD_NO_COPY, USBD_NO_TIMEOUT, in_intr);
|
2001-01-31 02:26:47 +03:00
|
|
|
return usbd_transfer(ep->xfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static usbd_status
|
|
|
|
start_output_transfer(struct umidi_endpoint *ep)
|
|
|
|
{
|
|
|
|
usbd_setup_xfer(ep->xfer, ep->pipe,
|
|
|
|
(usbd_private_handle)ep,
|
|
|
|
ep->buffer, UMIDI_PACKET_SIZE,
|
2001-12-12 18:44:47 +03:00
|
|
|
USBD_NO_COPY, USBD_NO_TIMEOUT, out_intr);
|
2001-01-31 02:26:47 +03:00
|
|
|
return usbd_transfer(ep->xfer);
|
|
|
|
}
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
#define DPR_PACKET(dir, sc, p) \
|
|
|
|
if ((unsigned char)(p)->buffer[1]!=0xFE) \
|
|
|
|
DPRINTFN(500, \
|
|
|
|
("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n", \
|
|
|
|
USBDEVNAME(sc->sc_dev), \
|
|
|
|
(unsigned char)(p)->buffer[0], \
|
|
|
|
(unsigned char)(p)->buffer[1], \
|
|
|
|
(unsigned char)(p)->buffer[2], \
|
|
|
|
(unsigned char)(p)->buffer[3]));
|
|
|
|
#else
|
|
|
|
#define DPR_PACKET(dir, sc, p)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
out_jack_output(struct umidi_jack *out_jack, int d)
|
|
|
|
{
|
|
|
|
struct umidi_endpoint *ep = out_jack->endpoint;
|
|
|
|
struct umidi_softc *sc = ep->sc;
|
|
|
|
int error;
|
|
|
|
int s;
|
|
|
|
|
|
|
|
if (sc->sc_dying)
|
|
|
|
return EIO;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
if (out_jack->opened) {
|
|
|
|
DPRINTFN(1000, ("umidi_output: ep=%p 0x%02x\n", ep, d));
|
|
|
|
out_build_packet(out_jack->cable_number, &out_jack->packet, d);
|
|
|
|
switch (out_jack->packet.state) {
|
|
|
|
case PS_EXCL_0:
|
|
|
|
case PS_END:
|
|
|
|
DPR_PACKET(out, sc, &out_jack->packet);
|
|
|
|
s = splusb();
|
|
|
|
if (LIST_EMPTY(&ep->queue_head)) {
|
|
|
|
memcpy(ep->buffer,
|
|
|
|
out_jack->packet.buffer,
|
|
|
|
UMIDI_PACKET_SIZE);
|
|
|
|
start_output_transfer(ep);
|
|
|
|
}
|
|
|
|
if (LIST_EMPTY(&ep->queue_head))
|
|
|
|
LIST_INSERT_HEAD(&ep->queue_head,
|
|
|
|
out_jack, u.out.queue_entry);
|
|
|
|
else
|
|
|
|
LIST_INSERT_AFTER(ep->queue_tail,
|
|
|
|
out_jack, u.out.queue_entry);
|
|
|
|
ep->queue_tail = out_jack;
|
|
|
|
splx(s);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINPROGRESS;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
error = ENODEV;
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
static void
|
|
|
|
in_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
|
|
|
|
{
|
2001-02-03 21:50:31 +03:00
|
|
|
int cn, len, i;
|
2001-01-31 02:26:47 +03:00
|
|
|
struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
|
2001-02-03 21:50:31 +03:00
|
|
|
struct umidi_jack *jack;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
if (ep->sc->sc_dying || !ep->num_open)
|
2001-01-31 02:26:47 +03:00
|
|
|
return;
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
cn = GET_CN(ep->buffer[0]);
|
|
|
|
len = packet_length[GET_CIN(ep->buffer[0])];
|
|
|
|
jack = ep->jacks[cn];
|
|
|
|
if (cn>=ep->num_jacks || !jack) {
|
|
|
|
DPRINTF(("%s: stray umidi packet (in): %02X %02X %02X %02X\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev),
|
|
|
|
(unsigned)ep->buffer[0],
|
|
|
|
(unsigned)ep->buffer[1],
|
|
|
|
(unsigned)ep->buffer[2],
|
|
|
|
(unsigned)ep->buffer[3]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!jack->binded || !jack->opened)
|
|
|
|
return;
|
2002-06-19 17:51:34 +04:00
|
|
|
DPR_PACKET(in, ep->sc, &jack->packet);
|
2001-02-03 21:50:31 +03:00
|
|
|
if (jack->u.in.intr) {
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
(*jack->u.in.intr)(jack->arg, ep->buffer[i+1]);
|
|
|
|
}
|
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
(void)start_input_transfer(ep);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
out_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
|
|
|
|
{
|
|
|
|
struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
|
|
|
|
struct umidi_softc *sc = ep->sc;
|
|
|
|
struct umidi_jack *jack;
|
|
|
|
|
2001-02-03 19:49:06 +03:00
|
|
|
if (sc->sc_dying || !ep->num_open)
|
2001-01-31 02:26:47 +03:00
|
|
|
return;
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
jack = LIST_FIRST(&ep->queue_head);
|
|
|
|
if (jack && jack->opened) {
|
|
|
|
LIST_REMOVE(jack, u.out.queue_entry);
|
|
|
|
if (!LIST_EMPTY(&ep->queue_head)) {
|
2001-02-03 19:49:06 +03:00
|
|
|
memcpy(ep->buffer,
|
2001-02-03 21:50:31 +03:00
|
|
|
LIST_FIRST(&ep->queue_head)->packet.buffer,
|
2001-02-03 19:49:06 +03:00
|
|
|
UMIDI_PACKET_SIZE);
|
|
|
|
(void)start_output_transfer(ep);
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
if (jack->u.out.intr) {
|
|
|
|
(*jack->u.out.intr)(jack->arg);
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
out_build_packet(int cable_number, struct umidi_packet *packet, uByte in)
|
|
|
|
{
|
|
|
|
int cin;
|
2001-04-08 02:01:34 +04:00
|
|
|
uByte prev;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
retry:
|
|
|
|
switch (packet->state) {
|
|
|
|
case PS_END:
|
|
|
|
case PS_INITIAL:
|
2001-04-08 02:01:34 +04:00
|
|
|
prev = packet->buffer[1];
|
2001-01-31 02:26:47 +03:00
|
|
|
memset(packet->buffer, 0, UMIDI_PACKET_SIZE);
|
|
|
|
if (in<0x80) {
|
2001-04-08 02:01:34 +04:00
|
|
|
if (prev>=0x80 && prev<0xf0) {
|
|
|
|
/* running status */
|
|
|
|
out_build_packet(cable_number, packet, prev);
|
|
|
|
goto retry;
|
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
/* ??? */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (in>=0xf0) {
|
|
|
|
cin=packet_0xFX[in&0x0F].cin;
|
|
|
|
packet->state=packet_0xFX[in&0x0F].next;
|
|
|
|
} else {
|
|
|
|
cin=(unsigned char)in>>4;
|
|
|
|
switch (packet_length[cin]) {
|
|
|
|
case 2:
|
|
|
|
packet->state = PS_NORMAL_1OF2;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
packet->state = PS_NORMAL_1OF3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ??? */
|
|
|
|
packet->state = PS_INITIAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
packet->buffer[0] = MIX_CN_CIN(cable_number, cin);
|
|
|
|
packet->buffer[1] = in;
|
|
|
|
break;
|
|
|
|
case PS_NORMAL_1OF3:
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
|
|
|
packet->buffer[2] = in;
|
|
|
|
packet->state = PS_NORMAL_2OF3;
|
|
|
|
break;
|
|
|
|
case PS_NORMAL_2OF3:
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
|
|
|
packet->buffer[3] = in;
|
|
|
|
packet->state = PS_END;
|
|
|
|
break;
|
|
|
|
case PS_NORMAL_1OF2:
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
|
|
|
packet->buffer[2] = in;
|
|
|
|
packet->state = PS_END;
|
|
|
|
break;
|
|
|
|
case PS_EXCL_0:
|
|
|
|
memset(packet->buffer, 0, UMIDI_PACKET_SIZE);
|
|
|
|
if (in==0xF7) {
|
|
|
|
packet->buffer[0] = MIX_CN_CIN(cable_number, 0x05);
|
|
|
|
packet->buffer[1] = 0xF7;
|
|
|
|
packet->state = PS_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
|
|
|
packet->buffer[1] = in;
|
|
|
|
packet->state = PS_EXCL_1;
|
|
|
|
break;
|
|
|
|
case PS_EXCL_1:
|
|
|
|
if (in==0xF7) {
|
|
|
|
packet->buffer[0] = MIX_CN_CIN(cable_number, 0x06);
|
|
|
|
packet->buffer[2] = 0xF7;
|
|
|
|
packet->state = PS_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
|
|
|
packet->buffer[2] = in;
|
|
|
|
packet->state = PS_EXCL_2;
|
|
|
|
break;
|
|
|
|
case PS_EXCL_2:
|
|
|
|
if (in==0xF7) {
|
|
|
|
packet->buffer[0] = MIX_CN_CIN(cable_number, 0x07);
|
|
|
|
packet->buffer[3] = 0xF7;
|
|
|
|
packet->state = PS_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (in>=0x80) { /* ??? */ packet->state = PS_END; break; }
|
2001-01-31 19:02:38 +03:00
|
|
|
packet->buffer[0] = MIX_CN_CIN(cable_number, 0x04);
|
2001-01-31 02:26:47 +03:00
|
|
|
packet->buffer[3] = in;
|
|
|
|
packet->state = PS_EXCL_0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("umidi: ambiguous state.\n");
|
|
|
|
packet->state = PS_INITIAL;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
|