2007-02-26 16:14:11 +03:00
|
|
|
/* $NetBSD: umidi.c,v 1.29 2007/02/26 13:14:11 drochner 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
|
2006-06-30 17:56:25 +04:00
|
|
|
* by Takuya SHIOZAKI (tshiozak@NetBSD.org) and (full-size transfers, extended
|
|
|
|
* hw_if) Chapman Flack (chap@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>
|
2007-02-26 16:14:11 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.29 2007/02/26 13:14:11 drochner Exp $");
|
2001-11-13 09:24:53 +03:00
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
#include <sys/types.h>
|
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>
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
#include <machine/intr.h>
|
|
|
|
#endif
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
#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
|
2006-06-30 17:56:25 +04:00
|
|
|
#include <sys/time.h>
|
|
|
|
static struct timeval umidi_tv;
|
2001-01-31 02:26:47 +03:00
|
|
|
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 *);
|
2006-06-30 17:56:25 +04:00
|
|
|
static int umidi_channelmsg(void *, int, int, u_char *, int);
|
|
|
|
static int umidi_commonmsg(void *, int, u_char *, int);
|
|
|
|
static int umidi_sysex(void *, u_char *, int);
|
|
|
|
static int umidi_rtmsg(void *, int);
|
2001-01-31 02:26:47 +03:00
|
|
|
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
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
static usbd_status attach_mididev(struct umidi_softc *, struct umidi_mididev *);
|
2001-01-31 02:26:47 +03:00
|
|
|
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 *);
|
2006-06-30 17:56:25 +04:00
|
|
|
static char *describe_mididev(struct umidi_mididev *);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
#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 usbd_status start_input_transfer(struct umidi_endpoint *);
|
|
|
|
static usbd_status start_output_transfer(struct umidi_endpoint *);
|
2006-06-30 17:56:25 +04:00
|
|
|
static int out_jack_output(struct umidi_jack *, u_char *, int, 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);
|
2006-06-30 17:56:25 +04:00
|
|
|
static void out_solicit(void *); /* struct umidi_endpoint* for softintr */
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
|
2004-10-29 16:57:15 +04:00
|
|
|
const struct midi_hw_if umidi_hw_if = {
|
2001-01-31 02:26:47 +03:00
|
|
|
umidi_open,
|
|
|
|
umidi_close,
|
2006-06-30 17:56:25 +04:00
|
|
|
umidi_rtmsg,
|
2001-01-31 02:26:47 +03:00
|
|
|
umidi_getinfo,
|
|
|
|
0, /* ioctl */
|
|
|
|
};
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
struct midi_hw_if_ext umidi_hw_if_ext = {
|
|
|
|
.channel = umidi_channelmsg,
|
|
|
|
.common = umidi_commonmsg,
|
|
|
|
.sysex = umidi_sysex,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct midi_hw_if_ext umidi_hw_if_mm = {
|
|
|
|
.channel = umidi_channelmsg,
|
|
|
|
.common = umidi_commonmsg,
|
|
|
|
.sysex = umidi_sysex,
|
|
|
|
.compress = 1,
|
|
|
|
};
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
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);
|
2005-05-11 14:02:28 +04:00
|
|
|
char *devinfop;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
DPRINTFN(1,("umidi_attach\n"));
|
|
|
|
|
2005-05-11 14:02:28 +04:00
|
|
|
devinfop = usbd_devinfo_alloc(uaa->device, 0);
|
|
|
|
printf("\n%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
|
|
|
|
usbd_devinfo_free(devinfop);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
usbd_status err;
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
if ((mididev->flags & FWRITE) && mididev->out_jack) {
|
|
|
|
err = open_out_jack(mididev->out_jack, arg, ointr);
|
|
|
|
if ( err != USBD_NORMAL_COMPLETION )
|
|
|
|
goto bad;
|
|
|
|
}
|
2001-02-03 19:49:06 +03:00
|
|
|
if ((mididev->flags & FREAD) && mididev->in_jack) {
|
2006-06-30 17:56:25 +04:00
|
|
|
err = open_in_jack(mididev->in_jack, arg, iintr);
|
|
|
|
if ( err != USBD_NORMAL_COMPLETION
|
|
|
|
&& err != USBD_IN_PROGRESS )
|
|
|
|
goto bad;
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
return 0;
|
2006-06-30 17:56:25 +04:00
|
|
|
bad:
|
|
|
|
mididev->opened = 0;
|
|
|
|
DPRINTF(("umidi_open: usbd_status %d\n", err));
|
|
|
|
return USBD_IN_USE == err ? EBUSY : EIO;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2006-11-16 04:32:37 +03:00
|
|
|
umidi_channelmsg(void *addr, int status, int channel, u_char *msg,
|
2006-10-12 05:30:41 +04:00
|
|
|
int len)
|
2006-06-30 17:56:25 +04:00
|
|
|
{
|
|
|
|
struct umidi_mididev *mididev = addr;
|
|
|
|
|
|
|
|
if (!mididev->out_jack || !mididev->opened)
|
|
|
|
return EIO;
|
|
|
|
|
|
|
|
return out_jack_output(mididev->out_jack, msg, len, (status>>4)&0xf);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-11-16 04:32:37 +03:00
|
|
|
umidi_commonmsg(void *addr, int status, u_char *msg, int len)
|
2006-06-30 17:56:25 +04:00
|
|
|
{
|
|
|
|
struct umidi_mididev *mididev = addr;
|
|
|
|
int cin;
|
|
|
|
|
|
|
|
if (!mididev->out_jack || !mididev->opened)
|
|
|
|
return EIO;
|
|
|
|
|
|
|
|
switch ( len ) {
|
|
|
|
case 1: cin = 5; break;
|
|
|
|
case 2: cin = 2; break;
|
|
|
|
case 3: cin = 3; break;
|
|
|
|
default: return EIO; /* or gcc warns of cin uninitialized */
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_jack_output(mididev->out_jack, msg, len, cin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
umidi_sysex(void *addr, u_char *msg, int len)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
2001-02-03 19:49:06 +03:00
|
|
|
struct umidi_mididev *mididev = addr;
|
2006-06-30 17:56:25 +04:00
|
|
|
int cin;
|
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;
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
switch ( len ) {
|
|
|
|
case 1: cin = 5; break;
|
|
|
|
case 2: cin = 6; break;
|
|
|
|
case 3: cin = (msg[2] == 0xf7) ? 7 : 4; break;
|
|
|
|
default: return EIO; /* or gcc warns of cin uninitialized */
|
|
|
|
}
|
|
|
|
|
|
|
|
return out_jack_output(mididev->out_jack, msg, len, cin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
umidi_rtmsg(void *addr, int d)
|
|
|
|
{
|
|
|
|
struct umidi_mididev *mididev = addr;
|
|
|
|
u_char msg = d;
|
|
|
|
|
|
|
|
if (!mididev->out_jack || !mididev->opened)
|
|
|
|
return EIO;
|
|
|
|
|
|
|
|
return out_jack_output(mididev->out_jack, &msg, 1, 0xf);
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
struct umidi_softc *sc = mididev->sc;
|
|
|
|
int mm = UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
mi->name = mididev->label;
|
2001-02-03 19:49:06 +03:00
|
|
|
mi->props = MIDI_PROP_OUT_INTR;
|
2001-01-31 02:26:47 +03:00
|
|
|
if (mididev->in_jack)
|
|
|
|
mi->props |= MIDI_PROP_CAN_INPUT;
|
2006-06-30 17:56:25 +04:00
|
|
|
midi_register_hw_if_ext(mm? &umidi_hw_if_mm : &umidi_hw_if_ext);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
usb_endpoint_descriptor_t *epd;
|
|
|
|
|
|
|
|
epd = usbd_get_endpoint_descriptor(sc->sc_iface, ep->addr);
|
|
|
|
/*
|
|
|
|
* For output, an improvement would be to have a buffer bigger than
|
|
|
|
* wMaxPacketSize by num_jacks-1 additional packet slots; that would
|
|
|
|
* allow out_solicit to fill the buffer to the full packet size in
|
|
|
|
* all cases. But to use usbd_alloc_buffer to get a slightly larger
|
|
|
|
* buffer would not be a good way to do that, because if the addition
|
|
|
|
* would make the buffer exceed USB_MEM_SMALL then a substantially
|
|
|
|
* larger block may be wastefully allocated. Some flavor of double
|
|
|
|
* buffering could serve the same purpose, but would increase the
|
|
|
|
* code complexity, so for now I will live with the current slight
|
|
|
|
* penalty of reducing max transfer size by (num_open-num_scheduled)
|
|
|
|
* packet slots.
|
|
|
|
*/
|
|
|
|
ep->buffer_size = UGETW(epd->wMaxPacketSize);
|
|
|
|
ep->buffer_size -= ep->buffer_size % UMIDI_PACKET_SIZE;
|
|
|
|
|
|
|
|
DPRINTF(("%s: alloc_pipe %p, buffer size %u\n",
|
|
|
|
USBDEVNAME(sc->sc_dev), ep, ep->buffer_size));
|
|
|
|
ep->num_scheduled = 0;
|
|
|
|
ep->this_schedule = 0;
|
|
|
|
ep->next_schedule = 0;
|
|
|
|
ep->soliciting = 0;
|
|
|
|
ep->armed = 0;
|
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;
|
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
ep->buffer = usbd_alloc_buffer(ep->xfer, ep->buffer_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
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
ep->next_slot = ep->buffer;
|
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);
|
2006-06-30 17:56:25 +04:00
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
ep->solicit_cookie = softintr_establish(IPL_SOFTCLOCK,out_solicit,ep);
|
|
|
|
#endif
|
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);
|
2006-06-30 17:56:25 +04:00
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
softintr_disestablish(ep->solicit_cookie);
|
|
|
|
#endif
|
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));
|
|
|
|
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;
|
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* MIDISPORT_2X4 inputs on an interrupt rather than a bulk
|
|
|
|
* endpoint. The existing input logic in this driver seems
|
|
|
|
* to work successfully if we just stop treating an interrupt
|
|
|
|
* endpoint as illegal (or the in_progress status we get on
|
|
|
|
* the initial transfer). It does not seem necessary to
|
|
|
|
* actually use the interrupt flavor of alloc_pipe or make
|
|
|
|
* other serious rearrangements of logic. I like that.
|
|
|
|
*/
|
|
|
|
switch ( UE_GET_XFERTYPE(epd->bmAttributes) ) {
|
|
|
|
case UE_BULK:
|
|
|
|
case UE_INTERRUPT:
|
|
|
|
if ( UE_DIR_IN == UE_GET_DIR(epd->bEndpointAddress) )
|
|
|
|
break;
|
|
|
|
/*FALLTHROUGH*/
|
|
|
|
default:
|
2001-01-31 02:26:47 +03:00
|
|
|
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;
|
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
|
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;
|
2007-02-26 16:14:11 +03:00
|
|
|
umidi_cs_descriptor_t *udesc;
|
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);
|
2006-04-15 02:25:42 +04:00
|
|
|
KASSERT(epd != NULL);
|
2001-05-29 00:52:06 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2007-02-26 16:14:11 +03:00
|
|
|
udesc = (umidi_cs_descriptor_t *)NEXT_D(desc);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2001-05-29 00:52:06 +04:00
|
|
|
/* count jacks */
|
2007-02-26 16:14:11 +03:00
|
|
|
if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
|
|
|
|
udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
|
2001-05-29 00:52:06 +04:00
|
|
|
return USBD_INVAL;
|
2007-02-26 16:14:11 +03:00
|
|
|
remain = (size_t)UGETW(TO_CSIFD(udesc)->wTotalLength) -
|
|
|
|
(size_t)udesc->bLength;
|
|
|
|
udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
while (remain>=sizeof(usb_descriptor_t)) {
|
2007-02-26 16:14:11 +03:00
|
|
|
descsize = udesc->bLength;
|
2001-01-31 02:26:47 +03:00
|
|
|
if (descsize>remain || descsize==0)
|
|
|
|
break;
|
2007-02-26 16:14:11 +03:00
|
|
|
if (udesc->bDescriptorType==UDESC_CS_INTERFACE &&
|
2001-01-31 02:26:47 +03:00
|
|
|
remain>=UMIDI_JACK_DESCRIPTOR_SIZE) {
|
2007-02-26 16:14:11 +03:00
|
|
|
if (udesc->bDescriptorSubtype==UMIDI_OUT_JACK)
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_out_num_jacks++;
|
2007-02-26 16:14:11 +03:00
|
|
|
else if (udesc->bDescriptorSubtype==UMIDI_IN_JACK)
|
2001-01-31 02:26:47 +03:00
|
|
|
sc->sc_in_num_jacks++;
|
|
|
|
}
|
2007-02-26 16:14:11 +03:00
|
|
|
udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
|
2001-01-31 02:26:47 +03:00
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
struct umidi_jack *jack;
|
|
|
|
unsigned char *cn_spec;
|
|
|
|
|
|
|
|
if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_PER_EP))
|
|
|
|
sc->cblnums_global = 0;
|
|
|
|
else if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_GLOBAL))
|
|
|
|
sc->cblnums_global = 1;
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* I don't think this default is correct, but it preserves
|
|
|
|
* the prior behavior of the code. That's why I defined two
|
|
|
|
* complementary quirks. Any device for which the default
|
|
|
|
* behavior is wrong can be made to work by giving it an
|
|
|
|
* explicit quirk, and if a pattern ever develops (as I suspect
|
|
|
|
* it will) that a lot of otherwise standard USB MIDI devices
|
|
|
|
* need the CN_SEQ_PER_EP "quirk," then this default can be
|
|
|
|
* changed to 0, and the only devices that will break are those
|
|
|
|
* listing neither quirk, and they'll easily be fixed by giving
|
|
|
|
* them the CN_SEQ_GLOBAL quirk.
|
|
|
|
*/
|
|
|
|
sc->cblnums_global = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_FIXED))
|
|
|
|
cn_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
|
|
|
|
UMQ_TYPE_CN_FIXED);
|
|
|
|
else
|
|
|
|
cn_spec = NULL;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
/* 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;
|
2006-06-30 17:56:25 +04:00
|
|
|
jack->midiman_ppkt = NULL;
|
|
|
|
if ( sc->cblnums_global )
|
|
|
|
jack->cable_number = i;
|
2001-01-31 02:26:47 +03:00
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
if ( sc->cblnums_global )
|
|
|
|
jack->cable_number = i;
|
2001-01-31 02:26:47 +03:00
|
|
|
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++) {
|
|
|
|
for (j=0; j<ep->num_jacks; j++) {
|
|
|
|
jack->endpoint = ep;
|
2006-06-30 17:56:25 +04:00
|
|
|
if ( cn_spec != NULL )
|
|
|
|
jack->cable_number = *cn_spec++;
|
|
|
|
else if ( !sc->cblnums_global )
|
|
|
|
jack->cable_number = j;
|
|
|
|
ep->jacks[jack->cable_number] = jack;
|
2001-01-31 02:26:47 +03:00
|
|
|
jack++;
|
|
|
|
}
|
|
|
|
ep++;
|
|
|
|
}
|
|
|
|
jack = &sc->sc_in_jacks[0];
|
|
|
|
ep = &sc->sc_in_ep[0];
|
|
|
|
for (i=0; i<sc->sc_in_num_endpoints; i++) {
|
|
|
|
for (j=0; j<ep->num_jacks; j++) {
|
|
|
|
jack->endpoint = ep;
|
2006-06-30 17:56:25 +04:00
|
|
|
if ( cn_spec != NULL )
|
|
|
|
jack->cable_number = *cn_spec++;
|
|
|
|
else if ( !sc->cblnums_global )
|
|
|
|
jack->cable_number = j;
|
|
|
|
ep->jacks[jack->cable_number] = jack;
|
2001-01-31 02:26:47 +03:00
|
|
|
jack++;
|
|
|
|
}
|
|
|
|
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
|
2006-11-16 04:32:37 +03:00
|
|
|
bind_jacks_to_mididev(struct umidi_softc *sc,
|
2001-01-31 02:26:47 +03:00
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
signed char *asg_spec;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
err =
|
|
|
|
alloc_all_mididevs(sc,
|
|
|
|
max(sc->sc_out_num_jacks, sc->sc_in_num_jacks));
|
|
|
|
if (err!=USBD_NORMAL_COMPLETION)
|
|
|
|
return err;
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
if ( UMQ_ISTYPE(sc, UMQ_TYPE_MD_FIXED))
|
|
|
|
asg_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
|
|
|
|
UMQ_TYPE_MD_FIXED);
|
|
|
|
else
|
|
|
|
asg_spec = NULL;
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
for (i=0; i<sc->sc_num_mididevs; i++) {
|
2006-06-30 17:56:25 +04:00
|
|
|
if ( asg_spec != NULL ) {
|
|
|
|
if ( *asg_spec == -1 )
|
|
|
|
out = NULL;
|
|
|
|
else
|
|
|
|
out = &sc->sc_out_jacks[*asg_spec];
|
|
|
|
++ asg_spec;
|
|
|
|
if ( *asg_spec == -1 )
|
|
|
|
in = NULL;
|
|
|
|
else
|
|
|
|
in = &sc->sc_in_jacks[*asg_spec];
|
|
|
|
++ asg_spec;
|
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
umidi_packet_bufp end;
|
|
|
|
int s;
|
|
|
|
int err;
|
2001-02-03 19:49:06 +03:00
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
if (jack->opened)
|
|
|
|
return USBD_IN_USE;
|
|
|
|
|
|
|
|
jack->arg = arg;
|
|
|
|
jack->u.out.intr = intr;
|
2006-06-30 17:56:25 +04:00
|
|
|
jack->midiman_ppkt = NULL;
|
|
|
|
end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
|
|
|
|
s = splusb();
|
2001-02-03 21:50:31 +03:00
|
|
|
jack->opened = 1;
|
|
|
|
ep->num_open++;
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* out_solicit maintains an invariant that there will always be
|
|
|
|
* (num_open - num_scheduled) slots free in the buffer. as we have
|
|
|
|
* just incremented num_open, the buffer may be too full to satisfy
|
|
|
|
* the invariant until a transfer completes, for which we must wait.
|
|
|
|
*/
|
|
|
|
while ( end - ep->next_slot < ep->num_open - ep->num_scheduled ) {
|
|
|
|
err = tsleep(ep, PWAIT|PCATCH, "umi op", mstohz(10));
|
|
|
|
if ( err ) {
|
|
|
|
ep->num_open--;
|
|
|
|
jack->opened = 0;
|
|
|
|
splx(s);
|
|
|
|
return USBD_IOERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
2001-02-03 21:50:31 +03:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2006-06-30 17:56:25 +04:00
|
|
|
struct umidi_endpoint *ep;
|
2001-02-03 19:49:06 +03:00
|
|
|
int s;
|
2006-06-30 17:56:25 +04:00
|
|
|
u_int16_t mask;
|
|
|
|
int err;
|
2001-02-03 21:50:31 +03:00
|
|
|
|
|
|
|
if (jack->opened) {
|
2006-06-30 17:56:25 +04:00
|
|
|
ep = jack->endpoint;
|
|
|
|
mask = 1 << (jack->cable_number);
|
2001-02-03 21:50:31 +03:00
|
|
|
s = splusb();
|
2006-06-30 17:56:25 +04:00
|
|
|
while ( mask & (ep->this_schedule | ep->next_schedule) ) {
|
|
|
|
err = tsleep(ep, PWAIT|PCATCH, "umi dr", mstohz(10));
|
|
|
|
if ( err )
|
2002-06-19 17:51:34 +04:00
|
|
|
break;
|
2001-02-03 19:49:06 +03:00
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
jack->opened = 0;
|
|
|
|
jack->endpoint->num_open--;
|
2006-06-30 17:56:25 +04:00
|
|
|
ep->this_schedule &= ~mask;
|
|
|
|
ep->next_schedule &= ~mask;
|
|
|
|
splx(s);
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
|
|
|
|
mididev->label = describe_mididev(mididev);
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
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);
|
2006-06-30 17:56:25 +04:00
|
|
|
|
|
|
|
if (NULL != mididev->label) {
|
|
|
|
free(mididev->label, M_USBDEV);
|
|
|
|
mididev->label = NULL;
|
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* TODO: the 0-based cable numbers will often not match the labeling of the
|
|
|
|
* equipment. Ideally:
|
|
|
|
* For class-compliant devices: get the iJack string from the jack descriptor.
|
|
|
|
* Otherwise:
|
|
|
|
* - support a DISPLAY_BASE_CN quirk (add the value to each internal cable
|
|
|
|
* number for display)
|
|
|
|
* - support an array quirk explictly giving a char * for each jack.
|
|
|
|
* For now, you get 0-based cable numbers. If there are multiple endpoints and
|
|
|
|
* the CNs are not globally unique, each is shown with its associated endpoint
|
|
|
|
* address in hex also. That should not be necessary when using iJack values
|
|
|
|
* or a quirk array.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
describe_mididev(struct umidi_mididev *md)
|
|
|
|
{
|
|
|
|
char in_label[16];
|
|
|
|
char out_label[16];
|
|
|
|
char *unit_label;
|
|
|
|
char *final_label;
|
|
|
|
struct umidi_softc *sc;
|
|
|
|
int show_ep_in;
|
|
|
|
int show_ep_out;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
sc = md->sc;
|
|
|
|
show_ep_in = sc-> sc_in_num_endpoints > 1 && !sc->cblnums_global;
|
|
|
|
show_ep_out = sc->sc_out_num_endpoints > 1 && !sc->cblnums_global;
|
|
|
|
|
|
|
|
if ( NULL != md->in_jack )
|
|
|
|
snprintf(in_label, sizeof in_label,
|
|
|
|
show_ep_in ? "<%d(%x) " : "<%d ",
|
|
|
|
md->in_jack->cable_number,
|
|
|
|
md->in_jack->endpoint->addr);
|
|
|
|
else
|
|
|
|
in_label[0] = '\0';
|
|
|
|
|
|
|
|
if ( NULL != md->out_jack )
|
|
|
|
snprintf(out_label, sizeof out_label,
|
|
|
|
show_ep_out ? ">%d(%x) " : ">%d ",
|
|
|
|
md->out_jack->cable_number,
|
|
|
|
md->out_jack->endpoint->addr);
|
|
|
|
else
|
|
|
|
in_label[0] = '\0';
|
|
|
|
|
|
|
|
unit_label = USBDEVNAME(sc->sc_dev);
|
|
|
|
|
|
|
|
len = strlen(in_label) + strlen(out_label) + strlen(unit_label) + 4;
|
|
|
|
|
|
|
|
final_label = malloc(len, M_USBDEV, M_WAITOK);
|
|
|
|
|
|
|
|
snprintf(final_label, len, "%s%son %s",
|
|
|
|
in_label, out_label, unit_label);
|
|
|
|
|
|
|
|
return final_label;
|
|
|
|
}
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
#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;
|
2006-06-30 17:56:25 +04:00
|
|
|
for (i=0; i<UMIDI_MAX_EPJACKS; i++) {
|
|
|
|
if (NULL==ep->jacks[i])
|
|
|
|
continue;
|
|
|
|
DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i, ep->jacks[i]));
|
2001-01-31 02:26:47 +03:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
#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 usbd_status
|
|
|
|
start_input_transfer(struct umidi_endpoint *ep)
|
|
|
|
{
|
|
|
|
usbd_setup_xfer(ep->xfer, ep->pipe,
|
|
|
|
(usbd_private_handle)ep,
|
2006-06-30 17:56:25 +04:00
|
|
|
ep->buffer, ep->buffer_size,
|
|
|
|
USBD_SHORT_XFER_OK | 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)
|
|
|
|
{
|
2006-06-30 17:56:25 +04:00
|
|
|
usbd_status rv;
|
|
|
|
u_int32_t length;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
length = (ep->next_slot - ep->buffer) * sizeof *ep->buffer;
|
|
|
|
DPRINTFN(200,("umidi out transfer: start %p end %p length %u\n",
|
|
|
|
ep->buffer, ep->next_slot, length));
|
2001-01-31 02:26:47 +03:00
|
|
|
usbd_setup_xfer(ep->xfer, ep->pipe,
|
|
|
|
(usbd_private_handle)ep,
|
2006-06-30 17:56:25 +04:00
|
|
|
ep->buffer, length,
|
2001-12-12 18:44:47 +03:00
|
|
|
USBD_NO_COPY, USBD_NO_TIMEOUT, out_intr);
|
2006-06-30 17:56:25 +04:00
|
|
|
rv = usbd_transfer(ep->xfer);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Once the transfer is scheduled, no more adding to partial
|
|
|
|
* packets within it.
|
|
|
|
*/
|
|
|
|
if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
|
|
|
|
for (i=0; i<UMIDI_MAX_EPJACKS; ++i)
|
|
|
|
if (NULL != ep->jacks[i])
|
|
|
|
ep->jacks[i]->midiman_ppkt = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
#define DPR_PACKET(dir, sc, p) \
|
2006-06-30 17:56:25 +04:00
|
|
|
if ((unsigned char)(p)[1]!=0xFE) \
|
2001-02-03 21:50:31 +03:00
|
|
|
DPRINTFN(500, \
|
|
|
|
("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n", \
|
|
|
|
USBDEVNAME(sc->sc_dev), \
|
2006-06-30 17:56:25 +04:00
|
|
|
(unsigned char)(p)[0], \
|
|
|
|
(unsigned char)(p)[1], \
|
|
|
|
(unsigned char)(p)[2], \
|
|
|
|
(unsigned char)(p)[3]));
|
2001-02-03 21:50:31 +03:00
|
|
|
#else
|
|
|
|
#define DPR_PACKET(dir, sc, p)
|
|
|
|
#endif
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* A 4-byte Midiman packet superficially resembles a 4-byte USB MIDI packet
|
|
|
|
* with the cable number and length in the last byte instead of the first,
|
|
|
|
* but there the resemblance ends. Where a USB MIDI packet is a semantic
|
|
|
|
* unit, a Midiman packet is just a wrapper for 1 to 3 bytes of raw MIDI
|
|
|
|
* with a cable nybble and a length nybble (which, unlike the CIN of a
|
|
|
|
* real USB MIDI packet, has no semantics at all besides the length).
|
|
|
|
* A packet received from a Midiman may contain part of a MIDI message,
|
|
|
|
* more than one MIDI message, or parts of more than one MIDI message. A
|
|
|
|
* three-byte MIDI message may arrive in three packets of data length 1, and
|
|
|
|
* running status may be used. Happily, the midi(4) driver above us will put
|
|
|
|
* it all back together, so the only cost is in USB bandwidth. The device
|
|
|
|
* has an easier time with what it receives from us: we'll pack messages in
|
|
|
|
* and across packets, but filling the packets whenever possible and,
|
|
|
|
* as midi(4) hands us a complete message at a time, we'll never send one
|
|
|
|
* in a dribble of short packets.
|
|
|
|
*/
|
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
static int
|
2006-06-30 17:56:25 +04:00
|
|
|
out_jack_output(struct umidi_jack *out_jack, u_char *src, int len, int cin)
|
2001-02-03 21:50:31 +03:00
|
|
|
{
|
|
|
|
struct umidi_endpoint *ep = out_jack->endpoint;
|
|
|
|
struct umidi_softc *sc = ep->sc;
|
2006-06-30 17:56:25 +04:00
|
|
|
unsigned char *packet;
|
2001-02-03 21:50:31 +03:00
|
|
|
int s;
|
2006-06-30 17:56:25 +04:00
|
|
|
int plen;
|
|
|
|
int poff;
|
2001-02-03 21:50:31 +03:00
|
|
|
|
|
|
|
if (sc->sc_dying)
|
|
|
|
return EIO;
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
if (!out_jack->opened)
|
|
|
|
return ENODEV; /* XXX as it was, is this the right errno? */
|
2001-02-03 21:50:31 +03:00
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
if ( umididebug >= 100 )
|
|
|
|
microtime(&umidi_tv);
|
|
|
|
#endif
|
|
|
|
DPRINTFN(100, ("umidi out: %lu.%06lus ep=%p cn=%d len=%d cin=%#x\n",
|
|
|
|
umidi_tv.tv_sec%100, umidi_tv.tv_usec,
|
|
|
|
ep, out_jack->cable_number, len, cin));
|
|
|
|
|
|
|
|
s = splusb();
|
|
|
|
packet = *ep->next_slot++;
|
|
|
|
KASSERT(ep->buffer_size >=
|
|
|
|
(ep->next_slot - ep->buffer) * sizeof *ep->buffer);
|
|
|
|
memset(packet, 0, UMIDI_PACKET_SIZE);
|
|
|
|
if (UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
|
|
|
|
if (NULL != out_jack->midiman_ppkt) { /* fill out a prev pkt */
|
|
|
|
poff = 0x0f & (out_jack->midiman_ppkt[3]);
|
|
|
|
plen = 3 - poff;
|
|
|
|
if (plen > len)
|
|
|
|
plen = len;
|
|
|
|
memcpy(out_jack->midiman_ppkt+poff, src, plen);
|
|
|
|
src += plen;
|
|
|
|
len -= plen;
|
|
|
|
plen += poff;
|
|
|
|
out_jack->midiman_ppkt[3] =
|
|
|
|
MIX_CN_CIN(out_jack->cable_number, plen);
|
|
|
|
DPR_PACKET(out+, sc, out_jack->midiman_ppkt);
|
|
|
|
if (3 == plen)
|
|
|
|
out_jack->midiman_ppkt = NULL; /* no more */
|
|
|
|
}
|
|
|
|
if (0 == len)
|
|
|
|
ep->next_slot--; /* won't be needed, nevermind */
|
|
|
|
else {
|
|
|
|
memcpy(packet, src, len);
|
|
|
|
packet[3] = MIX_CN_CIN(out_jack->cable_number, len);
|
|
|
|
DPR_PACKET(out, sc, packet);
|
|
|
|
if (len < 3)
|
|
|
|
out_jack->midiman_ppkt = packet;
|
|
|
|
}
|
|
|
|
} else { /* the nice simple USB class-compliant case */
|
|
|
|
packet[0] = MIX_CN_CIN(out_jack->cable_number, cin);
|
|
|
|
memcpy(packet+1, src, len);
|
|
|
|
DPR_PACKET(out, sc, packet);
|
|
|
|
}
|
|
|
|
ep->next_schedule |= 1<<(out_jack->cable_number);
|
|
|
|
++ ep->num_scheduled;
|
|
|
|
if ( !ep->armed && !ep->soliciting ) {
|
|
|
|
#ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
|
|
|
|
/*
|
|
|
|
* It would be bad to call out_solicit directly here (the
|
|
|
|
* caller need not be reentrant) but a soft interrupt allows
|
|
|
|
* solicit to run immediately the caller exits its critical
|
|
|
|
* section, and if the caller has more to write we can get it
|
|
|
|
* before starting the USB transfer, and send a longer one.
|
|
|
|
*/
|
|
|
|
ep->soliciting = 1;
|
|
|
|
softintr_schedule(ep->solicit_cookie);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* This alternative is a little less desirable, because if the
|
|
|
|
* writer has several messages to go at once, the first will go
|
|
|
|
* in a USB frame all to itself, and the rest in a full-size
|
|
|
|
* transfer one frame later (solicited on the first frame's
|
|
|
|
* completion interrupt). But it's simple.
|
|
|
|
*/
|
|
|
|
ep->armed = (USBD_IN_PROGRESS == start_output_transfer(ep));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
return 0;
|
2001-02-03 21:50:31 +03:00
|
|
|
}
|
|
|
|
|
2001-01-31 02:26:47 +03:00
|
|
|
static void
|
2006-10-12 05:30:41 +04:00
|
|
|
in_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
|
2006-11-16 04:32:37 +03:00
|
|
|
usbd_status status)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
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;
|
2006-06-30 17:56:25 +04:00
|
|
|
unsigned char *packet;
|
|
|
|
umidi_packet_bufp slot;
|
|
|
|
umidi_packet_bufp end;
|
|
|
|
unsigned char *data;
|
|
|
|
u_int32_t count;
|
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;
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
|
|
|
if ( 0 == count % UMIDI_PACKET_SIZE ) {
|
|
|
|
DPRINTFN(200,("%s: input endpoint %p transfer length %u\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev), ep, count));
|
|
|
|
} else {
|
|
|
|
DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev), ep, count));
|
|
|
|
}
|
|
|
|
|
|
|
|
slot = ep->buffer;
|
|
|
|
end = slot + count / sizeof *slot;
|
|
|
|
|
|
|
|
for ( packet = *slot; slot < end; packet = *++slot ) {
|
|
|
|
|
|
|
|
if ( UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE) ) {
|
|
|
|
cn = (0xf0&(packet[3]))>>4;
|
|
|
|
len = 0x0f&(packet[3]);
|
|
|
|
data = packet;
|
|
|
|
} else {
|
|
|
|
cn = GET_CN(packet[0]);
|
|
|
|
len = packet_length[GET_CIN(packet[0])];
|
|
|
|
data = packet + 1;
|
|
|
|
}
|
|
|
|
/* 0 <= cn <= 15 by inspection of above code */
|
|
|
|
if (!(jack = ep->jacks[cn]) || cn != jack->cable_number) {
|
|
|
|
DPRINTF(("%s: stray input endpoint %p cable %d len %d: "
|
|
|
|
"%02X %02X %02X (try CN_SEQ quirk?)\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev), ep, cn, len,
|
|
|
|
(unsigned)data[0],
|
|
|
|
(unsigned)data[1],
|
|
|
|
(unsigned)data[2]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jack->binded || !jack->opened)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
|
|
|
|
"%02X %02X %02X\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev), ep, cn, len,
|
|
|
|
(unsigned)data[0],
|
|
|
|
(unsigned)data[1],
|
|
|
|
(unsigned)data[2]));
|
|
|
|
|
|
|
|
if (jack->u.in.intr) {
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
(*jack->u.in.intr)(jack->arg, data[i]);
|
|
|
|
}
|
2001-02-03 21:50:31 +03:00
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
|
2001-02-03 21:50:31 +03:00
|
|
|
}
|
2001-01-31 02:26:47 +03:00
|
|
|
|
|
|
|
(void)start_input_transfer(ep);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2006-10-12 05:30:41 +04:00
|
|
|
out_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
|
2006-11-16 04:32:37 +03:00
|
|
|
usbd_status status)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
|
|
|
struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
|
|
|
|
struct umidi_softc *sc = ep->sc;
|
2006-06-30 17:56:25 +04:00
|
|
|
u_int32_t count;
|
2001-01-31 02:26:47 +03:00
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
if (sc->sc_dying)
|
2001-01-31 02:26:47 +03:00
|
|
|
return;
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
#ifdef UMIDI_DEBUG
|
|
|
|
if ( umididebug >= 200 )
|
|
|
|
microtime(&umidi_tv);
|
|
|
|
#endif
|
|
|
|
usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
|
|
|
|
if ( 0 == count % UMIDI_PACKET_SIZE ) {
|
|
|
|
DPRINTFN(200,("%s: %lu.%06lus out ep %p xfer length %u\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev),
|
|
|
|
umidi_tv.tv_sec%100, umidi_tv.tv_usec, ep, count));
|
|
|
|
} else {
|
|
|
|
DPRINTF(("%s: output endpoint %p odd transfer length %u\n",
|
|
|
|
USBDEVNAME(ep->sc->sc_dev), ep, count));
|
|
|
|
}
|
|
|
|
count /= UMIDI_PACKET_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If while the transfer was pending we buffered any new messages,
|
|
|
|
* move them to the start of the buffer.
|
|
|
|
*/
|
|
|
|
ep->next_slot -= count;
|
|
|
|
if ( ep->buffer < ep->next_slot ) {
|
|
|
|
memcpy(ep->buffer, ep->buffer + count,
|
|
|
|
(char *)ep->next_slot - (char *)ep->buffer);
|
|
|
|
}
|
|
|
|
wakeup(ep);
|
|
|
|
/*
|
|
|
|
* Do not want anyone else to see armed <- 0 before soliciting <- 1.
|
|
|
|
* Running at splusb so the following should happen to be safe.
|
|
|
|
*/
|
|
|
|
ep->armed = 0;
|
|
|
|
if ( !ep->soliciting ) {
|
|
|
|
ep->soliciting = 1;
|
|
|
|
out_solicit(ep);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* A jack on which we have received a packet must be called back on its
|
|
|
|
* out.intr handler before it will send us another; it is considered
|
|
|
|
* 'scheduled'. It is nice and predictable - as long as it is scheduled,
|
|
|
|
* we need no extra buffer space for it.
|
|
|
|
*
|
|
|
|
* In contrast, a jack that is open but not scheduled may supply us a packet
|
|
|
|
* at any time, driven by the top half, and we must be able to accept it, no
|
|
|
|
* excuses. So we must ensure that at any point in time there are at least
|
|
|
|
* (num_open - num_scheduled) slots free.
|
|
|
|
*
|
|
|
|
* As long as there are more slots free than that minimum, we can loop calling
|
|
|
|
* scheduled jacks back on their "interrupt" handlers, soliciting more
|
|
|
|
* packets, starting the USB transfer only when the buffer space is down to
|
|
|
|
* the minimum or no jack has any more to send.
|
|
|
|
*/
|
2001-01-31 02:26:47 +03:00
|
|
|
static void
|
2006-06-30 17:56:25 +04:00
|
|
|
out_solicit(void *arg)
|
2001-01-31 02:26:47 +03:00
|
|
|
{
|
2006-06-30 17:56:25 +04:00
|
|
|
struct umidi_endpoint *ep = arg;
|
|
|
|
int s;
|
|
|
|
umidi_packet_bufp end;
|
|
|
|
u_int16_t which;
|
|
|
|
struct umidi_jack *jack;
|
|
|
|
|
|
|
|
end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
s = splusb();
|
|
|
|
if ( end - ep->next_slot <= ep->num_open - ep->num_scheduled )
|
|
|
|
break; /* at splusb */
|
|
|
|
if ( ep->this_schedule == 0 ) {
|
|
|
|
if ( ep->next_schedule == 0 )
|
|
|
|
break; /* at splusb */
|
|
|
|
ep->this_schedule = ep->next_schedule;
|
|
|
|
ep->next_schedule = 0;
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
/*
|
|
|
|
* At least one jack is scheduled. Find and mask off the least
|
|
|
|
* set bit in this_schedule and decrement num_scheduled.
|
|
|
|
* Convert mask to bit index to find the corresponding jack,
|
|
|
|
* and call its intr handler. If it has a message, it will call
|
|
|
|
* back one of the output methods, which will set its bit in
|
|
|
|
* next_schedule (not copied into this_schedule until the
|
|
|
|
* latter is empty). In this way we round-robin the jacks that
|
|
|
|
* have messages to send, until the buffer is as full as we
|
|
|
|
* dare, and then start a transfer.
|
|
|
|
*/
|
|
|
|
which = ep->this_schedule;
|
|
|
|
which &= (~which)+1; /* now mask of least set bit */
|
|
|
|
ep->this_schedule &= ~which;
|
|
|
|
-- ep->num_scheduled;
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
-- which; /* now 1s below mask - count 1s to get index */
|
|
|
|
which -= ((which >> 1) & 0x5555);/* SWAR credit aggregate.org */
|
|
|
|
which = (((which >> 2) & 0x3333) + (which & 0x3333));
|
|
|
|
which = (((which >> 4) + which) & 0x0f0f);
|
|
|
|
which += (which >> 8);
|
|
|
|
which &= 0x1f; /* the bit index a/k/a jack number */
|
|
|
|
|
|
|
|
jack = ep->jacks[which];
|
|
|
|
if (jack->u.out.intr)
|
|
|
|
(*jack->u.out.intr)(jack->arg);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|
2006-06-30 17:56:25 +04:00
|
|
|
/* splusb at loop exit */
|
|
|
|
if ( !ep->armed && ep->next_slot > ep->buffer )
|
|
|
|
ep->armed = (USBD_IN_PROGRESS == start_output_transfer(ep));
|
|
|
|
ep->soliciting = 0;
|
|
|
|
splx(s);
|
2001-01-31 02:26:47 +03:00
|
|
|
}
|