Home | History | Annotate | Line # | Download | only in usb
umidi.c revision 1.29.4.1
      1  1.29.4.1        ad /*	$NetBSD: umidi.c,v 1.29.4.1 2007/03/13 16:50:56 ad Exp $	*/
      2       1.1  tshiozak /*
      3       1.1  tshiozak  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      4       1.1  tshiozak  * All rights reserved.
      5       1.1  tshiozak  *
      6       1.1  tshiozak  * This code is derived from software contributed to The NetBSD Foundation
      7      1.26      chap  * by Takuya SHIOZAKI (tshiozak (at) NetBSD.org) and (full-size transfers, extended
      8      1.26      chap  * hw_if) Chapman Flack (chap (at) NetBSD.org).
      9       1.1  tshiozak  *
     10       1.1  tshiozak  * Redistribution and use in source and binary forms, with or without
     11       1.1  tshiozak  * modification, are permitted provided that the following conditions
     12       1.1  tshiozak  * are met:
     13       1.1  tshiozak  * 1. Redistributions of source code must retain the above copyright
     14       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer.
     15       1.1  tshiozak  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  tshiozak  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  tshiozak  *    documentation and/or other materials provided with the distribution.
     18       1.1  tshiozak  * 3. All advertising materials mentioning features or use of this software
     19       1.1  tshiozak  *    must display the following acknowledgement:
     20       1.1  tshiozak  *	  This product includes software developed by the NetBSD
     21       1.1  tshiozak  *	  Foundation, Inc. and its contributors.
     22       1.1  tshiozak  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1  tshiozak  *    contributors may be used to endorse or promote products derived
     24       1.1  tshiozak  *    from this software without specific prior written permission.
     25       1.1  tshiozak  *
     26       1.1  tshiozak  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1  tshiozak  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1  tshiozak  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1  tshiozak  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1  tshiozak  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1  tshiozak  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1  tshiozak  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1  tshiozak  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1  tshiozak  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1  tshiozak  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1  tshiozak  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1  tshiozak  */
     38      1.10     lukem 
     39      1.10     lukem #include <sys/cdefs.h>
     40  1.29.4.1        ad __KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.29.4.1 2007/03/13 16:50:56 ad Exp $");
     41       1.1  tshiozak 
     42      1.26      chap #include <sys/types.h>
     43       1.1  tshiozak #include <sys/param.h>
     44       1.1  tshiozak #include <sys/systm.h>
     45       1.1  tshiozak #include <sys/kernel.h>
     46       1.1  tshiozak #include <sys/malloc.h>
     47       1.1  tshiozak #include <sys/device.h>
     48       1.1  tshiozak #include <sys/ioctl.h>
     49       1.1  tshiozak #include <sys/conf.h>
     50       1.1  tshiozak #include <sys/file.h>
     51       1.1  tshiozak #include <sys/select.h>
     52       1.1  tshiozak #include <sys/proc.h>
     53       1.1  tshiozak #include <sys/vnode.h>
     54       1.1  tshiozak #include <sys/poll.h>
     55       1.1  tshiozak #include <sys/lock.h>
     56       1.1  tshiozak 
     57      1.26      chap #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
     58      1.26      chap #include <machine/intr.h>
     59      1.26      chap #endif
     60      1.26      chap 
     61       1.1  tshiozak #include <dev/usb/usb.h>
     62       1.1  tshiozak #include <dev/usb/usbdi.h>
     63       1.1  tshiozak #include <dev/usb/usbdi_util.h>
     64       1.1  tshiozak 
     65       1.1  tshiozak #include <dev/usb/usbdevs.h>
     66       1.1  tshiozak #include <dev/usb/uaudioreg.h>
     67       1.1  tshiozak #include <dev/usb/umidireg.h>
     68       1.1  tshiozak #include <dev/usb/umidivar.h>
     69       1.1  tshiozak #include <dev/usb/umidi_quirks.h>
     70       1.1  tshiozak 
     71       1.1  tshiozak #include <dev/midi_if.h>
     72       1.1  tshiozak 
     73       1.1  tshiozak #ifdef UMIDI_DEBUG
     74       1.1  tshiozak #define DPRINTF(x)	if (umididebug) printf x
     75       1.1  tshiozak #define DPRINTFN(n,x)	if (umididebug >= (n)) printf x
     76      1.26      chap #include <sys/time.h>
     77      1.26      chap static struct timeval umidi_tv;
     78       1.1  tshiozak int	umididebug = 0;
     79       1.1  tshiozak #else
     80       1.1  tshiozak #define DPRINTF(x)
     81       1.1  tshiozak #define DPRINTFN(n,x)
     82       1.1  tshiozak #endif
     83       1.1  tshiozak 
     84       1.1  tshiozak 
     85       1.1  tshiozak static int umidi_open(void *, int,
     86       1.1  tshiozak 		      void (*)(void *, int), void (*)(void *), void *);
     87       1.1  tshiozak static void umidi_close(void *);
     88      1.26      chap static int umidi_channelmsg(void *, int, int, u_char *, int);
     89      1.26      chap static int umidi_commonmsg(void *, int, u_char *, int);
     90      1.26      chap static int umidi_sysex(void *, u_char *, int);
     91      1.26      chap static int umidi_rtmsg(void *, int);
     92       1.1  tshiozak static void umidi_getinfo(void *, struct midi_info *);
     93       1.1  tshiozak 
     94       1.3  tshiozak static usbd_status alloc_pipe(struct umidi_endpoint *);
     95       1.3  tshiozak static void free_pipe(struct umidi_endpoint *);
     96       1.1  tshiozak 
     97       1.1  tshiozak static usbd_status alloc_all_endpoints(struct umidi_softc *);
     98       1.1  tshiozak static void free_all_endpoints(struct umidi_softc *);
     99       1.1  tshiozak 
    100       1.1  tshiozak static usbd_status alloc_all_jacks(struct umidi_softc *);
    101       1.1  tshiozak static void free_all_jacks(struct umidi_softc *);
    102       1.1  tshiozak static usbd_status bind_jacks_to_mididev(struct umidi_softc *,
    103       1.1  tshiozak 					 struct umidi_jack *,
    104       1.1  tshiozak 					 struct umidi_jack *,
    105       1.1  tshiozak 					 struct umidi_mididev *);
    106       1.4  tshiozak static void unbind_jacks_from_mididev(struct umidi_mididev *);
    107       1.4  tshiozak static void unbind_all_jacks(struct umidi_softc *);
    108       1.1  tshiozak static usbd_status assign_all_jacks_automatically(struct umidi_softc *);
    109       1.4  tshiozak static usbd_status open_out_jack(struct umidi_jack *, void *,
    110       1.4  tshiozak 				 void (*)(void *));
    111       1.4  tshiozak static usbd_status open_in_jack(struct umidi_jack *, void *,
    112       1.4  tshiozak 				void (*)(void *, int));
    113       1.4  tshiozak static void close_out_jack(struct umidi_jack *);
    114       1.4  tshiozak static void close_in_jack(struct umidi_jack *);
    115       1.1  tshiozak 
    116      1.26      chap static usbd_status attach_mididev(struct umidi_softc *, struct umidi_mididev *);
    117       1.1  tshiozak static usbd_status detach_mididev(struct umidi_mididev *, int);
    118       1.1  tshiozak static usbd_status deactivate_mididev(struct umidi_mididev *);
    119       1.1  tshiozak static usbd_status alloc_all_mididevs(struct umidi_softc *, int);
    120       1.1  tshiozak static void free_all_mididevs(struct umidi_softc *);
    121       1.1  tshiozak static usbd_status attach_all_mididevs(struct umidi_softc *);
    122       1.1  tshiozak static usbd_status detach_all_mididevs(struct umidi_softc *, int);
    123       1.1  tshiozak static usbd_status deactivate_all_mididevs(struct umidi_softc *);
    124      1.26      chap static char *describe_mididev(struct umidi_mididev *);
    125       1.1  tshiozak 
    126       1.1  tshiozak #ifdef UMIDI_DEBUG
    127       1.1  tshiozak static void dump_sc(struct umidi_softc *);
    128       1.1  tshiozak static void dump_ep(struct umidi_endpoint *);
    129       1.1  tshiozak static void dump_jack(struct umidi_jack *);
    130       1.1  tshiozak #endif
    131       1.1  tshiozak 
    132       1.1  tshiozak static usbd_status start_input_transfer(struct umidi_endpoint *);
    133       1.1  tshiozak static usbd_status start_output_transfer(struct umidi_endpoint *);
    134      1.26      chap static int out_jack_output(struct umidi_jack *, u_char *, int, int);
    135       1.1  tshiozak static void in_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    136       1.1  tshiozak static void out_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
    137      1.26      chap static void out_solicit(void *); /* struct umidi_endpoint* for softintr */
    138       1.1  tshiozak 
    139       1.1  tshiozak 
    140      1.22      yamt const struct midi_hw_if umidi_hw_if = {
    141       1.1  tshiozak 	umidi_open,
    142       1.1  tshiozak 	umidi_close,
    143      1.26      chap 	umidi_rtmsg,
    144       1.1  tshiozak 	umidi_getinfo,
    145       1.1  tshiozak 	0,		/* ioctl */
    146       1.1  tshiozak };
    147       1.1  tshiozak 
    148      1.26      chap struct midi_hw_if_ext umidi_hw_if_ext = {
    149      1.26      chap 	.channel = umidi_channelmsg,
    150      1.26      chap 	.common  = umidi_commonmsg,
    151      1.26      chap 	.sysex   = umidi_sysex,
    152      1.26      chap };
    153      1.26      chap 
    154      1.26      chap struct midi_hw_if_ext umidi_hw_if_mm = {
    155      1.26      chap 	.channel = umidi_channelmsg,
    156      1.26      chap 	.common  = umidi_commonmsg,
    157      1.26      chap 	.sysex   = umidi_sysex,
    158      1.26      chap 	.compress = 1,
    159      1.26      chap };
    160      1.26      chap 
    161       1.1  tshiozak USB_DECLARE_DRIVER(umidi);
    162       1.1  tshiozak 
    163       1.1  tshiozak USB_MATCH(umidi)
    164       1.1  tshiozak {
    165  1.29.4.1        ad 	USB_IFMATCH_START(umidi, uaa);
    166       1.1  tshiozak 
    167       1.1  tshiozak 	DPRINTFN(1,("umidi_match\n"));
    168       1.1  tshiozak 
    169       1.3  tshiozak 	if (umidi_search_quirk(uaa->vendor, uaa->product, uaa->ifaceno))
    170       1.3  tshiozak 		return UMATCH_IFACECLASS_IFACESUBCLASS;
    171       1.1  tshiozak 
    172  1.29.4.1        ad 	if (uaa->class == UICLASS_AUDIO &&
    173  1.29.4.1        ad 	    uaa->subclass == UISUBCLASS_MIDISTREAM)
    174       1.3  tshiozak 		return UMATCH_IFACECLASS_IFACESUBCLASS;
    175       1.1  tshiozak 
    176       1.3  tshiozak 	return UMATCH_NONE;
    177       1.1  tshiozak }
    178       1.1  tshiozak 
    179       1.1  tshiozak USB_ATTACH(umidi)
    180       1.1  tshiozak {
    181       1.1  tshiozak 	usbd_status err;
    182  1.29.4.1        ad 	USB_IFATTACH_START(umidi, sc, uaa);
    183      1.23  augustss 	char *devinfop;
    184       1.1  tshiozak 
    185       1.1  tshiozak 	DPRINTFN(1,("umidi_attach\n"));
    186       1.1  tshiozak 
    187      1.23  augustss 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
    188      1.23  augustss 	printf("\n%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
    189      1.23  augustss 	usbd_devinfo_free(devinfop);
    190       1.1  tshiozak 
    191       1.1  tshiozak 	sc->sc_iface = uaa->iface;
    192       1.1  tshiozak 	sc->sc_udev = uaa->device;
    193       1.1  tshiozak 
    194       1.1  tshiozak 	sc->sc_quirk =
    195       1.1  tshiozak 	    umidi_search_quirk(uaa->vendor, uaa->product, uaa->ifaceno);
    196       1.1  tshiozak 	printf("%s: ", USBDEVNAME(sc->sc_dev));
    197       1.1  tshiozak 	umidi_print_quirk(sc->sc_quirk);
    198       1.1  tshiozak 
    199       1.1  tshiozak 
    200       1.1  tshiozak 	err = alloc_all_endpoints(sc);
    201       1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION) {
    202       1.1  tshiozak 		printf("%s: alloc_all_endpoints failed. (err=%d)\n",
    203       1.1  tshiozak 		       USBDEVNAME(sc->sc_dev), err);
    204       1.1  tshiozak 		goto error;
    205       1.1  tshiozak 	}
    206       1.1  tshiozak 	err = alloc_all_jacks(sc);
    207       1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION) {
    208       1.1  tshiozak 		free_all_endpoints(sc);
    209       1.1  tshiozak 		printf("%s: alloc_all_jacks failed. (err=%d)\n",
    210       1.1  tshiozak 		       USBDEVNAME(sc->sc_dev), err);
    211       1.1  tshiozak 		goto error;
    212       1.1  tshiozak 	}
    213       1.1  tshiozak 	printf("%s: out=%d, in=%d\n",
    214       1.1  tshiozak 	       USBDEVNAME(sc->sc_dev),
    215       1.1  tshiozak 	       sc->sc_out_num_jacks, sc->sc_in_num_jacks);
    216       1.1  tshiozak 
    217       1.1  tshiozak 	err = assign_all_jacks_automatically(sc);
    218       1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION) {
    219       1.1  tshiozak 		unbind_all_jacks(sc);
    220       1.1  tshiozak 		free_all_jacks(sc);
    221       1.1  tshiozak 		free_all_endpoints(sc);
    222       1.1  tshiozak 		printf("%s: assign_all_jacks_automatically failed. (err=%d)\n",
    223       1.1  tshiozak 		       USBDEVNAME(sc->sc_dev), err);
    224       1.1  tshiozak 		goto error;
    225       1.1  tshiozak 	}
    226       1.1  tshiozak 	err = attach_all_mididevs(sc);
    227       1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION) {
    228       1.1  tshiozak 		free_all_jacks(sc);
    229       1.1  tshiozak 		free_all_endpoints(sc);
    230       1.1  tshiozak 		printf("%s: attach_all_mididevs failed. (err=%d)\n",
    231       1.1  tshiozak 		       USBDEVNAME(sc->sc_dev), err);
    232       1.1  tshiozak 	}
    233       1.1  tshiozak 
    234       1.1  tshiozak #ifdef UMIDI_DEBUG
    235       1.1  tshiozak 	dump_sc(sc);
    236       1.1  tshiozak #endif
    237       1.1  tshiozak 
    238       1.1  tshiozak 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH,
    239       1.1  tshiozak 			   sc->sc_udev, USBDEV(sc->sc_dev));
    240      1.16  augustss 
    241       1.1  tshiozak 	USB_ATTACH_SUCCESS_RETURN;
    242       1.1  tshiozak error:
    243       1.1  tshiozak 	printf("%s: disabled.\n", USBDEVNAME(sc->sc_dev));
    244       1.1  tshiozak 	sc->sc_dying = 1;
    245       1.1  tshiozak 	USB_ATTACH_ERROR_RETURN;
    246       1.1  tshiozak }
    247       1.1  tshiozak 
    248       1.1  tshiozak int
    249       1.1  tshiozak umidi_activate(device_ptr_t self, enum devact act)
    250       1.1  tshiozak {
    251       1.3  tshiozak 	struct umidi_softc *sc = (struct umidi_softc *)self;
    252       1.1  tshiozak 
    253       1.1  tshiozak 	switch (act) {
    254       1.1  tshiozak 	case DVACT_ACTIVATE:
    255       1.1  tshiozak 		DPRINTFN(1,("umidi_activate (activate)\n"));
    256       1.1  tshiozak 
    257       1.1  tshiozak 		return EOPNOTSUPP;
    258       1.1  tshiozak 		break;
    259       1.1  tshiozak 	case DVACT_DEACTIVATE:
    260       1.1  tshiozak 		DPRINTFN(1,("umidi_activate (deactivate)\n"));
    261       1.1  tshiozak 		sc->sc_dying = 1;
    262       1.1  tshiozak 		deactivate_all_mididevs(sc);
    263       1.1  tshiozak 		break;
    264       1.1  tshiozak 	}
    265       1.1  tshiozak 	return 0;
    266       1.1  tshiozak }
    267       1.1  tshiozak 
    268       1.1  tshiozak USB_DETACH(umidi)
    269       1.1  tshiozak {
    270       1.3  tshiozak 	USB_DETACH_START(umidi, sc);
    271       1.1  tshiozak 
    272       1.1  tshiozak 	DPRINTFN(1,("umidi_detach\n"));
    273       1.1  tshiozak 
    274       1.1  tshiozak 	sc->sc_dying = 1;
    275       1.1  tshiozak 	detach_all_mididevs(sc, flags);
    276       1.1  tshiozak 	free_all_mididevs(sc);
    277       1.1  tshiozak 	free_all_jacks(sc);
    278       1.1  tshiozak 	free_all_endpoints(sc);
    279       1.1  tshiozak 
    280       1.3  tshiozak 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    281       1.3  tshiozak 			   USBDEV(sc->sc_dev));
    282       1.1  tshiozak 
    283       1.1  tshiozak 	return 0;
    284       1.1  tshiozak }
    285       1.1  tshiozak 
    286       1.1  tshiozak 
    287       1.4  tshiozak /*
    288       1.4  tshiozak  * midi_if stuffs
    289       1.4  tshiozak  */
    290       1.1  tshiozak int
    291       1.1  tshiozak umidi_open(void *addr,
    292       1.1  tshiozak 	   int flags,
    293      1.12  augustss 	   void (*iintr)(void *, int),
    294      1.12  augustss 	   void (*ointr)(void *),
    295       1.1  tshiozak 	   void *arg)
    296       1.1  tshiozak {
    297       1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    298       1.3  tshiozak 	struct umidi_softc *sc = mididev->sc;
    299      1.26      chap 	usbd_status err;
    300       1.1  tshiozak 
    301       1.1  tshiozak 	DPRINTF(("umidi_open: sc=%p\n", sc));
    302       1.1  tshiozak 
    303       1.1  tshiozak 	if (!sc)
    304       1.1  tshiozak 		return ENXIO;
    305       1.1  tshiozak 	if (mididev->opened)
    306       1.1  tshiozak 		return EBUSY;
    307       1.4  tshiozak 	if (sc->sc_dying)
    308       1.1  tshiozak 		return EIO;
    309       1.1  tshiozak 
    310       1.4  tshiozak 	mididev->opened = 1;
    311       1.1  tshiozak 	mididev->flags = flags;
    312      1.26      chap 	if ((mididev->flags & FWRITE) && mididev->out_jack) {
    313      1.26      chap 		err = open_out_jack(mididev->out_jack, arg, ointr);
    314      1.26      chap 		if ( err != USBD_NORMAL_COMPLETION )
    315      1.26      chap 			goto bad;
    316      1.26      chap 	}
    317       1.3  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack) {
    318      1.26      chap 		err = open_in_jack(mididev->in_jack, arg, iintr);
    319      1.26      chap 		if ( err != USBD_NORMAL_COMPLETION
    320      1.26      chap 		&&   err != USBD_IN_PROGRESS )
    321      1.26      chap 			goto bad;
    322       1.3  tshiozak 	}
    323       1.1  tshiozak 
    324       1.1  tshiozak 	return 0;
    325      1.26      chap bad:
    326      1.26      chap 	mididev->opened = 0;
    327      1.26      chap 	DPRINTF(("umidi_open: usbd_status %d\n", err));
    328      1.26      chap 	return USBD_IN_USE == err ? EBUSY : EIO;
    329       1.1  tshiozak }
    330       1.1  tshiozak 
    331       1.1  tshiozak void
    332       1.1  tshiozak umidi_close(void *addr)
    333       1.1  tshiozak {
    334       1.1  tshiozak 	int s;
    335       1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    336       1.1  tshiozak 
    337       1.1  tshiozak 	s = splusb();
    338       1.1  tshiozak 	if ((mididev->flags & FWRITE) && mididev->out_jack)
    339       1.4  tshiozak 		close_out_jack(mididev->out_jack);
    340       1.1  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack)
    341       1.4  tshiozak 		close_in_jack(mididev->in_jack);
    342       1.1  tshiozak 	mididev->opened = 0;
    343       1.1  tshiozak 	splx(s);
    344       1.1  tshiozak }
    345       1.1  tshiozak 
    346       1.1  tshiozak int
    347      1.28  christos umidi_channelmsg(void *addr, int status, int channel, u_char *msg,
    348      1.27  christos     int len)
    349      1.26      chap {
    350      1.26      chap 	struct umidi_mididev *mididev = addr;
    351      1.26      chap 
    352      1.26      chap 	if (!mididev->out_jack || !mididev->opened)
    353      1.26      chap 		return EIO;
    354      1.26      chap 
    355      1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, (status>>4)&0xf);
    356      1.26      chap }
    357      1.26      chap 
    358      1.26      chap int
    359      1.28  christos umidi_commonmsg(void *addr, int status, u_char *msg, int len)
    360       1.1  tshiozak {
    361       1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    362      1.26      chap 	int cin;
    363       1.1  tshiozak 
    364       1.4  tshiozak 	if (!mididev->out_jack || !mididev->opened)
    365       1.1  tshiozak 		return EIO;
    366       1.1  tshiozak 
    367      1.26      chap 	switch ( len ) {
    368      1.26      chap 	case 1: cin = 5; break;
    369      1.26      chap 	case 2: cin = 2; break;
    370      1.26      chap 	case 3: cin = 3; break;
    371      1.26      chap 	default: return EIO; /* or gcc warns of cin uninitialized */
    372      1.26      chap 	}
    373      1.26      chap 
    374      1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, cin);
    375      1.26      chap }
    376      1.26      chap 
    377      1.26      chap int
    378      1.26      chap umidi_sysex(void *addr, u_char *msg, int len)
    379      1.26      chap {
    380      1.26      chap 	struct umidi_mididev *mididev = addr;
    381      1.26      chap 	int cin;
    382      1.26      chap 
    383      1.26      chap 	if (!mididev->out_jack || !mididev->opened)
    384      1.26      chap 		return EIO;
    385      1.26      chap 
    386      1.26      chap 	switch ( len ) {
    387      1.26      chap 	case 1: cin = 5; break;
    388      1.26      chap 	case 2: cin = 6; break;
    389      1.26      chap 	case 3: cin = (msg[2] == 0xf7) ? 7 : 4; break;
    390      1.26      chap 	default: return EIO; /* or gcc warns of cin uninitialized */
    391      1.26      chap 	}
    392      1.26      chap 
    393      1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, cin);
    394      1.26      chap }
    395      1.26      chap 
    396      1.26      chap int
    397      1.26      chap umidi_rtmsg(void *addr, int d)
    398      1.26      chap {
    399      1.26      chap 	struct umidi_mididev *mididev = addr;
    400      1.26      chap 	u_char msg = d;
    401      1.26      chap 
    402      1.26      chap 	if (!mididev->out_jack || !mididev->opened)
    403      1.26      chap 		return EIO;
    404      1.26      chap 
    405      1.26      chap 	return out_jack_output(mididev->out_jack, &msg, 1, 0xf);
    406       1.1  tshiozak }
    407       1.1  tshiozak 
    408       1.1  tshiozak void
    409       1.1  tshiozak umidi_getinfo(void *addr, struct midi_info *mi)
    410       1.1  tshiozak {
    411       1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    412      1.26      chap 	struct umidi_softc *sc = mididev->sc;
    413      1.26      chap 	int mm = UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE);
    414       1.1  tshiozak 
    415      1.26      chap 	mi->name = mididev->label;
    416       1.3  tshiozak 	mi->props = MIDI_PROP_OUT_INTR;
    417       1.1  tshiozak 	if (mididev->in_jack)
    418       1.1  tshiozak 		mi->props |= MIDI_PROP_CAN_INPUT;
    419      1.26      chap 	midi_register_hw_if_ext(mm? &umidi_hw_if_mm : &umidi_hw_if_ext);
    420       1.1  tshiozak }
    421       1.1  tshiozak 
    422       1.1  tshiozak 
    423       1.4  tshiozak /*
    424       1.4  tshiozak  * each endpoint stuffs
    425       1.4  tshiozak  */
    426       1.1  tshiozak 
    427       1.3  tshiozak /* alloc/free pipe */
    428       1.1  tshiozak static usbd_status
    429       1.3  tshiozak alloc_pipe(struct umidi_endpoint *ep)
    430       1.1  tshiozak {
    431       1.1  tshiozak 	struct umidi_softc *sc = ep->sc;
    432       1.1  tshiozak 	usbd_status err;
    433      1.26      chap 	usb_endpoint_descriptor_t *epd;
    434      1.26      chap 
    435      1.26      chap 	epd = usbd_get_endpoint_descriptor(sc->sc_iface, ep->addr);
    436      1.26      chap 	/*
    437      1.26      chap 	 * For output, an improvement would be to have a buffer bigger than
    438      1.26      chap 	 * wMaxPacketSize by num_jacks-1 additional packet slots; that would
    439      1.26      chap 	 * allow out_solicit to fill the buffer to the full packet size in
    440      1.26      chap 	 * all cases. But to use usbd_alloc_buffer to get a slightly larger
    441      1.26      chap 	 * buffer would not be a good way to do that, because if the addition
    442      1.26      chap 	 * would make the buffer exceed USB_MEM_SMALL then a substantially
    443      1.26      chap 	 * larger block may be wastefully allocated. Some flavor of double
    444      1.26      chap 	 * buffering could serve the same purpose, but would increase the
    445      1.26      chap 	 * code complexity, so for now I will live with the current slight
    446      1.26      chap 	 * penalty of reducing max transfer size by (num_open-num_scheduled)
    447      1.26      chap 	 * packet slots.
    448      1.26      chap 	 */
    449      1.26      chap 	ep->buffer_size = UGETW(epd->wMaxPacketSize);
    450      1.26      chap 	ep->buffer_size -= ep->buffer_size % UMIDI_PACKET_SIZE;
    451      1.26      chap 
    452      1.26      chap 	DPRINTF(("%s: alloc_pipe %p, buffer size %u\n",
    453      1.26      chap 	        USBDEVNAME(sc->sc_dev), ep, ep->buffer_size));
    454      1.26      chap 	ep->num_scheduled = 0;
    455      1.26      chap 	ep->this_schedule = 0;
    456      1.26      chap 	ep->next_schedule = 0;
    457      1.26      chap 	ep->soliciting = 0;
    458      1.26      chap 	ep->armed = 0;
    459       1.3  tshiozak 	ep->xfer = usbd_alloc_xfer(sc->sc_udev);
    460      1.11  augustss 	if (ep->xfer == NULL) {
    461       1.3  tshiozak 	    err = USBD_NOMEM;
    462       1.3  tshiozak 	    goto quit;
    463       1.3  tshiozak 	}
    464      1.26      chap 	ep->buffer = usbd_alloc_buffer(ep->xfer, ep->buffer_size);
    465      1.11  augustss 	if (ep->buffer == NULL) {
    466       1.3  tshiozak 	    usbd_free_xfer(ep->xfer);
    467       1.3  tshiozak 	    err = USBD_NOMEM;
    468       1.3  tshiozak 	    goto quit;
    469       1.3  tshiozak 	}
    470      1.26      chap 	ep->next_slot = ep->buffer;
    471       1.3  tshiozak 	err = usbd_open_pipe(sc->sc_iface, ep->addr, 0, &ep->pipe);
    472       1.3  tshiozak 	if (err)
    473       1.3  tshiozak 	    usbd_free_xfer(ep->xfer);
    474      1.26      chap #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    475      1.26      chap 	ep->solicit_cookie = softintr_establish(IPL_SOFTCLOCK,out_solicit,ep);
    476      1.26      chap #endif
    477       1.1  tshiozak quit:
    478       1.1  tshiozak 	return err;
    479       1.1  tshiozak }
    480       1.1  tshiozak 
    481       1.1  tshiozak static void
    482       1.3  tshiozak free_pipe(struct umidi_endpoint *ep)
    483       1.1  tshiozak {
    484       1.3  tshiozak 	DPRINTF(("%s: free_pipe %p\n", USBDEVNAME(ep->sc->sc_dev), ep));
    485       1.3  tshiozak 	usbd_abort_pipe(ep->pipe);
    486       1.3  tshiozak 	usbd_close_pipe(ep->pipe);
    487       1.3  tshiozak 	usbd_free_xfer(ep->xfer);
    488      1.26      chap #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    489      1.26      chap 	softintr_disestablish(ep->solicit_cookie);
    490      1.26      chap #endif
    491       1.1  tshiozak }
    492       1.1  tshiozak 
    493       1.1  tshiozak 
    494       1.1  tshiozak /* alloc/free the array of endpoint structures */
    495       1.1  tshiozak 
    496       1.1  tshiozak static usbd_status alloc_all_endpoints_fixed_ep(struct umidi_softc *);
    497       1.1  tshiozak static usbd_status alloc_all_endpoints_yamaha(struct umidi_softc *);
    498       1.1  tshiozak static usbd_status alloc_all_endpoints_genuine(struct umidi_softc *);
    499       1.1  tshiozak 
    500       1.1  tshiozak static usbd_status
    501       1.1  tshiozak alloc_all_endpoints(struct umidi_softc *sc)
    502       1.1  tshiozak {
    503       1.3  tshiozak 	usbd_status err;
    504       1.3  tshiozak 	struct umidi_endpoint *ep;
    505       1.3  tshiozak 	int i;
    506       1.9  tshiozak 
    507       1.3  tshiozak 	if (UMQ_ISTYPE(sc, UMQ_TYPE_FIXED_EP)) {
    508       1.3  tshiozak 		err = alloc_all_endpoints_fixed_ep(sc);
    509       1.3  tshiozak 	} else if (UMQ_ISTYPE(sc, UMQ_TYPE_YAMAHA)) {
    510       1.3  tshiozak 		err = alloc_all_endpoints_yamaha(sc);
    511       1.1  tshiozak 	} else {
    512       1.3  tshiozak 		err = alloc_all_endpoints_genuine(sc);
    513       1.1  tshiozak 	}
    514       1.3  tshiozak 	if (err!=USBD_NORMAL_COMPLETION)
    515       1.3  tshiozak 		return err;
    516       1.3  tshiozak 
    517       1.3  tshiozak 	ep = sc->sc_endpoints;
    518       1.8  tshiozak 	for (i=sc->sc_out_num_endpoints+sc->sc_in_num_endpoints; i>0; i--) {
    519       1.3  tshiozak 		err = alloc_pipe(ep++);
    520       1.3  tshiozak 		if (err!=USBD_NORMAL_COMPLETION) {
    521       1.3  tshiozak 			for (; ep!=sc->sc_endpoints; ep--)
    522       1.3  tshiozak 				free_pipe(ep-1);
    523       1.3  tshiozak 			free(sc->sc_endpoints, M_USBDEV);
    524       1.3  tshiozak 			sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    525       1.3  tshiozak 			break;
    526       1.3  tshiozak 		}
    527       1.3  tshiozak 	}
    528       1.3  tshiozak 	return err;
    529       1.1  tshiozak }
    530       1.1  tshiozak 
    531       1.1  tshiozak static void
    532       1.1  tshiozak free_all_endpoints(struct umidi_softc *sc)
    533       1.1  tshiozak {
    534       1.3  tshiozak 	int i;
    535       1.8  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints+sc->sc_out_num_endpoints; i++)
    536       1.3  tshiozak 	    free_pipe(&sc->sc_endpoints[i]);
    537      1.14      kent 	if (sc->sc_endpoints != NULL)
    538      1.14      kent 		free(sc->sc_endpoints, M_USBDEV);
    539       1.1  tshiozak 	sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    540       1.1  tshiozak }
    541       1.1  tshiozak 
    542       1.1  tshiozak static usbd_status
    543       1.1  tshiozak alloc_all_endpoints_fixed_ep(struct umidi_softc *sc)
    544       1.1  tshiozak {
    545       1.3  tshiozak 	usbd_status err;
    546       1.1  tshiozak 	struct umq_fixed_ep_desc *fp;
    547       1.3  tshiozak 	struct umidi_endpoint *ep;
    548       1.1  tshiozak 	usb_endpoint_descriptor_t *epd;
    549       1.1  tshiozak 	int i;
    550       1.1  tshiozak 
    551       1.1  tshiozak 	fp = umidi_get_quirk_data_from_type(sc->sc_quirk,
    552       1.1  tshiozak 					    UMQ_TYPE_FIXED_EP);
    553       1.1  tshiozak 	sc->sc_out_num_jacks = 0;
    554       1.1  tshiozak 	sc->sc_in_num_jacks = 0;
    555       1.1  tshiozak 	sc->sc_out_num_endpoints = fp->num_out_ep;
    556       1.1  tshiozak 	sc->sc_in_num_endpoints = fp->num_in_ep;
    557       1.1  tshiozak 	sc->sc_endpoints = malloc(sizeof(*sc->sc_out_ep)*
    558       1.1  tshiozak 				  (sc->sc_out_num_endpoints+
    559       1.1  tshiozak 				   sc->sc_in_num_endpoints),
    560       1.1  tshiozak 				  M_USBDEV, M_WAITOK);
    561       1.1  tshiozak 	if (!sc->sc_endpoints) {
    562       1.1  tshiozak 		return USBD_NOMEM;
    563       1.1  tshiozak 	}
    564       1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
    565       1.1  tshiozak 	sc->sc_in_ep =
    566       1.1  tshiozak 	    sc->sc_in_num_endpoints ?
    567       1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
    568       1.3  tshiozak 
    569       1.3  tshiozak 	ep = &sc->sc_out_ep[0];
    570       1.1  tshiozak 	for (i=0; i<sc->sc_out_num_endpoints; i++) {
    571       1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    572       1.1  tshiozak 			sc->sc_iface,
    573       1.1  tshiozak 			fp->out_ep[i].ep);
    574       1.1  tshiozak 		if (!epd) {
    575       1.1  tshiozak 			printf("%s: cannot get endpoint descriptor(out:%d)\n",
    576       1.1  tshiozak 			       USBDEVNAME(sc->sc_dev), fp->out_ep[i].ep);
    577       1.3  tshiozak 			err = USBD_INVAL;
    578       1.1  tshiozak 			goto error;
    579       1.1  tshiozak 		}
    580       1.1  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes)!=UE_BULK ||
    581       1.1  tshiozak 		    UE_GET_DIR(epd->bEndpointAddress)!=UE_DIR_OUT) {
    582       1.1  tshiozak 			printf("%s: illegal endpoint(out:%d)\n",
    583       1.1  tshiozak 			       USBDEVNAME(sc->sc_dev), fp->out_ep[i].ep);
    584       1.3  tshiozak 			err = USBD_INVAL;
    585       1.1  tshiozak 			goto error;
    586       1.1  tshiozak 		}
    587       1.3  tshiozak 		ep->sc = sc;
    588       1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    589       1.3  tshiozak 		ep->num_jacks = fp->out_ep[i].num_jacks;
    590       1.1  tshiozak 		sc->sc_out_num_jacks += fp->out_ep[i].num_jacks;
    591       1.3  tshiozak 		ep->num_open = 0;
    592       1.3  tshiozak 		memset(ep->jacks, 0, sizeof(ep->jacks));
    593       1.3  tshiozak 		ep++;
    594       1.1  tshiozak 	}
    595       1.3  tshiozak 	ep = &sc->sc_in_ep[0];
    596       1.1  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints; i++) {
    597       1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    598       1.1  tshiozak 			sc->sc_iface,
    599       1.1  tshiozak 			fp->in_ep[i].ep);
    600       1.1  tshiozak 		if (!epd) {
    601       1.1  tshiozak 			printf("%s: cannot get endpoint descriptor(in:%d)\n",
    602       1.1  tshiozak 			       USBDEVNAME(sc->sc_dev), fp->in_ep[i].ep);
    603       1.3  tshiozak 			err = USBD_INVAL;
    604       1.1  tshiozak 			goto error;
    605       1.1  tshiozak 		}
    606      1.26      chap 		/*
    607      1.26      chap 		 * MIDISPORT_2X4 inputs on an interrupt rather than a bulk
    608      1.26      chap 		 * endpoint.  The existing input logic in this driver seems
    609      1.26      chap 		 * to work successfully if we just stop treating an interrupt
    610      1.26      chap 		 * endpoint as illegal (or the in_progress status we get on
    611      1.26      chap 		 * the initial transfer).  It does not seem necessary to
    612      1.26      chap 		 * actually use the interrupt flavor of alloc_pipe or make
    613      1.26      chap 		 * other serious rearrangements of logic.  I like that.
    614      1.26      chap 		 */
    615      1.26      chap 		switch ( UE_GET_XFERTYPE(epd->bmAttributes) ) {
    616      1.26      chap 		case UE_BULK:
    617      1.26      chap 		case UE_INTERRUPT:
    618      1.26      chap 			if ( UE_DIR_IN == UE_GET_DIR(epd->bEndpointAddress) )
    619      1.26      chap 				break;
    620      1.26      chap 			/*FALLTHROUGH*/
    621      1.26      chap 		default:
    622       1.1  tshiozak 			printf("%s: illegal endpoint(in:%d)\n",
    623       1.1  tshiozak 			       USBDEVNAME(sc->sc_dev), fp->in_ep[i].ep);
    624       1.3  tshiozak 			err = USBD_INVAL;
    625       1.1  tshiozak 			goto error;
    626       1.1  tshiozak 		}
    627      1.26      chap 
    628       1.3  tshiozak 		ep->sc = sc;
    629       1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    630       1.3  tshiozak 		ep->num_jacks = fp->in_ep[i].num_jacks;
    631       1.1  tshiozak 		sc->sc_in_num_jacks += fp->in_ep[i].num_jacks;
    632       1.3  tshiozak 		ep->num_open = 0;
    633       1.3  tshiozak 		memset(ep->jacks, 0, sizeof(ep->jacks));
    634       1.3  tshiozak 		ep++;
    635       1.1  tshiozak 	}
    636       1.1  tshiozak 
    637       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    638       1.1  tshiozak error:
    639       1.1  tshiozak 	free(sc->sc_endpoints, M_USBDEV);
    640       1.1  tshiozak 	sc->sc_endpoints = NULL;
    641       1.3  tshiozak 	return err;
    642       1.1  tshiozak }
    643       1.1  tshiozak 
    644       1.1  tshiozak static usbd_status
    645       1.1  tshiozak alloc_all_endpoints_yamaha(struct umidi_softc *sc)
    646       1.1  tshiozak {
    647       1.1  tshiozak 	/* This driver currently supports max 1in/1out bulk endpoints */
    648       1.1  tshiozak 	usb_descriptor_t *desc;
    649      1.29  drochner 	umidi_cs_descriptor_t *udesc;
    650       1.8  tshiozak 	usb_endpoint_descriptor_t *epd;
    651       1.8  tshiozak 	int out_addr, in_addr, i;
    652       1.8  tshiozak 	int dir;
    653       1.1  tshiozak 	size_t remain, descsize;
    654       1.1  tshiozak 
    655       1.8  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    656       1.8  tshiozak 	out_addr = in_addr = 0;
    657       1.8  tshiozak 
    658       1.8  tshiozak 	/* detect endpoints */
    659       1.1  tshiozak 	desc = TO_D(usbd_get_interface_descriptor(sc->sc_iface));
    660       1.8  tshiozak 	for (i=(int)TO_IFD(desc)->bNumEndpoints-1; i>=0; i--) {
    661       1.8  tshiozak 		epd = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    662      1.25  christos 		KASSERT(epd != NULL);
    663       1.8  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes) == UE_BULK) {
    664       1.8  tshiozak 			dir = UE_GET_DIR(epd->bEndpointAddress);
    665       1.8  tshiozak 			if (dir==UE_DIR_OUT && !out_addr)
    666       1.8  tshiozak 				out_addr = epd->bEndpointAddress;
    667       1.8  tshiozak 			else if (dir==UE_DIR_IN && !in_addr)
    668       1.8  tshiozak 				in_addr = epd->bEndpointAddress;
    669       1.8  tshiozak 		}
    670       1.8  tshiozak 	}
    671      1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(desc);
    672       1.1  tshiozak 
    673       1.8  tshiozak 	/* count jacks */
    674      1.29  drochner 	if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
    675      1.29  drochner 	      udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
    676       1.8  tshiozak 		return USBD_INVAL;
    677      1.29  drochner 	remain = (size_t)UGETW(TO_CSIFD(udesc)->wTotalLength) -
    678      1.29  drochner 		(size_t)udesc->bLength;
    679      1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    680       1.1  tshiozak 
    681       1.1  tshiozak 	while (remain>=sizeof(usb_descriptor_t)) {
    682      1.29  drochner 		descsize = udesc->bLength;
    683       1.1  tshiozak 		if (descsize>remain || descsize==0)
    684       1.1  tshiozak 			break;
    685      1.29  drochner 		if (udesc->bDescriptorType==UDESC_CS_INTERFACE &&
    686       1.1  tshiozak 		    remain>=UMIDI_JACK_DESCRIPTOR_SIZE) {
    687      1.29  drochner 			if (udesc->bDescriptorSubtype==UMIDI_OUT_JACK)
    688       1.1  tshiozak 				sc->sc_out_num_jacks++;
    689      1.29  drochner 			else if (udesc->bDescriptorSubtype==UMIDI_IN_JACK)
    690       1.1  tshiozak 				sc->sc_in_num_jacks++;
    691       1.1  tshiozak 		}
    692      1.29  drochner 		udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    693       1.1  tshiozak 		remain-=descsize;
    694       1.1  tshiozak 	}
    695       1.1  tshiozak 
    696       1.8  tshiozak 	/* validate some parameters */
    697       1.1  tshiozak 	if (sc->sc_out_num_jacks>UMIDI_MAX_EPJACKS)
    698       1.1  tshiozak 		sc->sc_out_num_jacks = UMIDI_MAX_EPJACKS;
    699       1.1  tshiozak 	if (sc->sc_in_num_jacks>UMIDI_MAX_EPJACKS)
    700       1.1  tshiozak 		sc->sc_in_num_jacks = UMIDI_MAX_EPJACKS;
    701       1.8  tshiozak 	if (sc->sc_out_num_jacks && out_addr) {
    702       1.1  tshiozak 		sc->sc_out_num_endpoints = 1;
    703       1.1  tshiozak 	} else {
    704       1.1  tshiozak 		sc->sc_out_num_endpoints = 0;
    705       1.1  tshiozak 		sc->sc_out_num_jacks = 0;
    706       1.1  tshiozak 	}
    707       1.8  tshiozak 	if (sc->sc_in_num_jacks && in_addr) {
    708       1.1  tshiozak 		sc->sc_in_num_endpoints = 1;
    709       1.1  tshiozak 	} else {
    710       1.1  tshiozak 		sc->sc_in_num_endpoints = 0;
    711       1.1  tshiozak 		sc->sc_in_num_jacks = 0;
    712       1.1  tshiozak 	}
    713       1.1  tshiozak 	sc->sc_endpoints = malloc(sizeof(struct umidi_endpoint)*
    714       1.1  tshiozak 				  (sc->sc_out_num_endpoints+
    715       1.1  tshiozak 				   sc->sc_in_num_endpoints),
    716       1.1  tshiozak 				  M_USBDEV, M_WAITOK);
    717       1.1  tshiozak 	if (!sc->sc_endpoints)
    718       1.1  tshiozak 		return USBD_NOMEM;
    719       1.1  tshiozak 	if (sc->sc_out_num_endpoints) {
    720       1.1  tshiozak 		sc->sc_out_ep = sc->sc_endpoints;
    721       1.1  tshiozak 		sc->sc_out_ep->sc = sc;
    722       1.1  tshiozak 		sc->sc_out_ep->addr = out_addr;
    723       1.1  tshiozak 		sc->sc_out_ep->num_jacks = sc->sc_out_num_jacks;
    724       1.1  tshiozak 		sc->sc_out_ep->num_open = 0;
    725       1.1  tshiozak 		memset(sc->sc_out_ep->jacks, 0, sizeof(sc->sc_out_ep->jacks));
    726       1.1  tshiozak 	} else
    727       1.1  tshiozak 		sc->sc_out_ep = NULL;
    728       1.1  tshiozak 
    729       1.1  tshiozak 	if (sc->sc_in_num_endpoints) {
    730       1.1  tshiozak 		sc->sc_in_ep = sc->sc_endpoints+sc->sc_out_num_endpoints;
    731       1.1  tshiozak 		sc->sc_in_ep->sc = sc;
    732       1.1  tshiozak 		sc->sc_in_ep->addr = in_addr;
    733       1.1  tshiozak 		sc->sc_in_ep->num_jacks = sc->sc_in_num_jacks;
    734       1.1  tshiozak 		sc->sc_in_ep->num_open = 0;
    735       1.1  tshiozak 		memset(sc->sc_in_ep->jacks, 0, sizeof(sc->sc_in_ep->jacks));
    736       1.1  tshiozak 	} else
    737       1.1  tshiozak 		sc->sc_in_ep = NULL;
    738       1.1  tshiozak 
    739       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    740       1.1  tshiozak }
    741       1.1  tshiozak 
    742       1.1  tshiozak static usbd_status
    743       1.1  tshiozak alloc_all_endpoints_genuine(struct umidi_softc *sc)
    744       1.1  tshiozak {
    745      1.20      gson 	usb_interface_descriptor_t *interface_desc;
    746      1.20      gson 	usb_config_descriptor_t *config_desc;
    747       1.1  tshiozak 	usb_descriptor_t *desc;
    748       1.7  tshiozak 	int num_ep;
    749       1.1  tshiozak 	size_t remain, descsize;
    750       1.1  tshiozak 	struct umidi_endpoint *p, *q, *lowest, *endep, tmpep;
    751       1.1  tshiozak 	int epaddr;
    752       1.1  tshiozak 
    753      1.20      gson 	interface_desc = usbd_get_interface_descriptor(sc->sc_iface);
    754      1.20      gson 	num_ep = interface_desc->bNumEndpoints;
    755      1.20      gson 	sc->sc_endpoints = p = malloc(sizeof(struct umidi_endpoint) * num_ep,
    756       1.1  tshiozak 				      M_USBDEV, M_WAITOK);
    757       1.1  tshiozak 	if (!p)
    758       1.1  tshiozak 		return USBD_NOMEM;
    759       1.1  tshiozak 
    760       1.1  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    761       1.1  tshiozak 	sc->sc_out_num_endpoints = sc->sc_in_num_endpoints = 0;
    762       1.1  tshiozak 	epaddr = -1;
    763       1.1  tshiozak 
    764       1.1  tshiozak 	/* get the list of endpoints for midi stream */
    765      1.20      gson 	config_desc = usbd_get_config_descriptor(sc->sc_udev);
    766      1.20      gson 	desc = (usb_descriptor_t *) config_desc;
    767      1.20      gson 	remain = (size_t)UGETW(config_desc->wTotalLength);
    768       1.1  tshiozak 	while (remain>=sizeof(usb_descriptor_t)) {
    769       1.1  tshiozak 		descsize = desc->bLength;
    770       1.1  tshiozak 		if (descsize>remain || descsize==0)
    771       1.1  tshiozak 			break;
    772       1.1  tshiozak 		if (desc->bDescriptorType==UDESC_ENDPOINT &&
    773       1.1  tshiozak 		    remain>=USB_ENDPOINT_DESCRIPTOR_SIZE &&
    774       1.1  tshiozak 		    UE_GET_XFERTYPE(TO_EPD(desc)->bmAttributes) == UE_BULK) {
    775       1.1  tshiozak 			epaddr = TO_EPD(desc)->bEndpointAddress;
    776       1.1  tshiozak 		} else if (desc->bDescriptorType==UDESC_CS_ENDPOINT &&
    777       1.1  tshiozak 			   remain>=UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE &&
    778       1.1  tshiozak 			   epaddr!=-1) {
    779       1.1  tshiozak 			if (num_ep>0) {
    780       1.9  tshiozak 				num_ep--;
    781       1.1  tshiozak 				p->sc = sc;
    782       1.1  tshiozak 				p->addr = epaddr;
    783       1.1  tshiozak 				p->num_jacks = TO_CSEPD(desc)->bNumEmbMIDIJack;
    784       1.1  tshiozak 				if (UE_GET_DIR(epaddr)==UE_DIR_OUT) {
    785       1.1  tshiozak 					sc->sc_out_num_endpoints++;
    786       1.1  tshiozak 					sc->sc_out_num_jacks += p->num_jacks;
    787       1.1  tshiozak 				} else {
    788       1.1  tshiozak 					sc->sc_in_num_endpoints++;
    789       1.1  tshiozak 					sc->sc_in_num_jacks += p->num_jacks;
    790       1.1  tshiozak 				}
    791       1.1  tshiozak 				p++;
    792       1.1  tshiozak 			}
    793       1.1  tshiozak 		} else
    794       1.1  tshiozak 			epaddr = -1;
    795       1.1  tshiozak 		desc = NEXT_D(desc);
    796       1.1  tshiozak 		remain-=descsize;
    797       1.1  tshiozak 	}
    798       1.1  tshiozak 
    799       1.1  tshiozak 	/* sort endpoints */
    800       1.1  tshiozak 	num_ep = sc->sc_out_num_endpoints + sc->sc_in_num_endpoints;
    801       1.1  tshiozak 	p = sc->sc_endpoints;
    802       1.1  tshiozak 	endep = p + num_ep;
    803       1.1  tshiozak 	while (p<endep) {
    804       1.1  tshiozak 		lowest = p;
    805       1.1  tshiozak 		for (q=p+1; q<endep; q++) {
    806       1.1  tshiozak 			if ((UE_GET_DIR(lowest->addr)==UE_DIR_IN &&
    807       1.1  tshiozak 			     UE_GET_DIR(q->addr)==UE_DIR_OUT) ||
    808       1.1  tshiozak 			    ((UE_GET_DIR(lowest->addr)==
    809       1.1  tshiozak 			      UE_GET_DIR(q->addr)) &&
    810       1.1  tshiozak 			     (UE_GET_ADDR(lowest->addr)>
    811       1.1  tshiozak 			      UE_GET_ADDR(q->addr))))
    812       1.1  tshiozak 				lowest = q;
    813       1.1  tshiozak 		}
    814       1.1  tshiozak 		if (lowest != p) {
    815       1.1  tshiozak 			memcpy((void *)&tmpep, (void *)p, sizeof(tmpep));
    816       1.1  tshiozak 			memcpy((void *)p, (void *)lowest, sizeof(tmpep));
    817       1.1  tshiozak 			memcpy((void *)lowest, (void *)&tmpep, sizeof(tmpep));
    818       1.1  tshiozak 		}
    819       1.7  tshiozak 		p->num_open = 0;
    820       1.7  tshiozak 		p++;
    821       1.1  tshiozak 	}
    822      1.16  augustss 
    823       1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
    824       1.1  tshiozak 	sc->sc_in_ep =
    825       1.1  tshiozak 	    sc->sc_in_num_endpoints ?
    826       1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
    827       1.1  tshiozak 
    828       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    829       1.1  tshiozak }
    830       1.1  tshiozak 
    831       1.1  tshiozak 
    832       1.4  tshiozak /*
    833       1.4  tshiozak  * jack stuffs
    834       1.4  tshiozak  */
    835       1.4  tshiozak 
    836       1.1  tshiozak static usbd_status
    837       1.1  tshiozak alloc_all_jacks(struct umidi_softc *sc)
    838       1.1  tshiozak {
    839       1.1  tshiozak 	int i, j;
    840       1.1  tshiozak 	struct umidi_endpoint *ep;
    841      1.26      chap 	struct umidi_jack *jack;
    842      1.26      chap 	unsigned char *cn_spec;
    843      1.26      chap 
    844      1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_PER_EP))
    845      1.26      chap 		sc->cblnums_global = 0;
    846      1.26      chap 	else if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_GLOBAL))
    847      1.26      chap 		sc->cblnums_global = 1;
    848      1.26      chap 	else {
    849      1.26      chap 		/*
    850      1.26      chap 		 * I don't think this default is correct, but it preserves
    851      1.26      chap 		 * the prior behavior of the code. That's why I defined two
    852      1.26      chap 		 * complementary quirks. Any device for which the default
    853      1.26      chap 		 * behavior is wrong can be made to work by giving it an
    854      1.26      chap 		 * explicit quirk, and if a pattern ever develops (as I suspect
    855      1.26      chap 		 * it will) that a lot of otherwise standard USB MIDI devices
    856      1.26      chap 		 * need the CN_SEQ_PER_EP "quirk," then this default can be
    857      1.26      chap 		 * changed to 0, and the only devices that will break are those
    858      1.26      chap 		 * listing neither quirk, and they'll easily be fixed by giving
    859      1.26      chap 		 * them the CN_SEQ_GLOBAL quirk.
    860      1.26      chap 		 */
    861      1.26      chap 		sc->cblnums_global = 1;
    862      1.26      chap 	}
    863      1.26      chap 
    864      1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_FIXED))
    865      1.26      chap 		cn_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
    866      1.26      chap 					    		 UMQ_TYPE_CN_FIXED);
    867      1.26      chap 	else
    868      1.26      chap 		cn_spec = NULL;
    869       1.1  tshiozak 
    870       1.1  tshiozak 	/* allocate/initialize structures */
    871       1.1  tshiozak 	sc->sc_jacks =
    872       1.1  tshiozak 	    malloc(sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks+
    873       1.1  tshiozak 					      sc->sc_out_num_jacks),
    874       1.1  tshiozak 		   M_USBDEV, M_WAITOK);
    875       1.1  tshiozak 	if (!sc->sc_jacks)
    876       1.1  tshiozak 		return USBD_NOMEM;
    877       1.1  tshiozak 	sc->sc_out_jacks =
    878       1.1  tshiozak 	    sc->sc_out_num_jacks ? sc->sc_jacks : NULL;
    879       1.1  tshiozak 	sc->sc_in_jacks =
    880       1.1  tshiozak 	    sc->sc_in_num_jacks ? sc->sc_jacks+sc->sc_out_num_jacks : NULL;
    881       1.1  tshiozak 
    882       1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
    883       1.1  tshiozak 	for (i=0; i<sc->sc_out_num_jacks; i++) {
    884       1.4  tshiozak 		jack->opened = 0;
    885       1.4  tshiozak 		jack->binded = 0;
    886       1.4  tshiozak 		jack->arg = NULL;
    887       1.4  tshiozak 		jack->u.out.intr = NULL;
    888      1.26      chap 		jack->midiman_ppkt = NULL;
    889      1.26      chap 		if ( sc->cblnums_global )
    890      1.26      chap 			jack->cable_number = i;
    891       1.1  tshiozak 		jack++;
    892       1.1  tshiozak 	}
    893       1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
    894       1.1  tshiozak 	for (i=0; i<sc->sc_in_num_jacks; i++) {
    895       1.4  tshiozak 		jack->opened = 0;
    896       1.4  tshiozak 		jack->binded = 0;
    897       1.4  tshiozak 		jack->arg = NULL;
    898       1.4  tshiozak 		jack->u.in.intr = NULL;
    899      1.26      chap 		if ( sc->cblnums_global )
    900      1.26      chap 			jack->cable_number = i;
    901       1.1  tshiozak 		jack++;
    902       1.1  tshiozak 	}
    903       1.1  tshiozak 
    904       1.1  tshiozak 	/* assign each jacks to each endpoints */
    905       1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
    906       1.1  tshiozak 	ep = &sc->sc_out_ep[0];
    907       1.1  tshiozak 	for (i=0; i<sc->sc_out_num_endpoints; i++) {
    908       1.1  tshiozak 		for (j=0; j<ep->num_jacks; j++) {
    909       1.1  tshiozak 			jack->endpoint = ep;
    910      1.26      chap 			if ( cn_spec != NULL )
    911      1.26      chap 				jack->cable_number = *cn_spec++;
    912      1.26      chap 			else if ( !sc->cblnums_global )
    913      1.26      chap 				jack->cable_number = j;
    914      1.26      chap 			ep->jacks[jack->cable_number] = jack;
    915       1.1  tshiozak 			jack++;
    916       1.1  tshiozak 		}
    917       1.1  tshiozak 		ep++;
    918       1.1  tshiozak 	}
    919       1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
    920       1.1  tshiozak 	ep = &sc->sc_in_ep[0];
    921       1.1  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints; i++) {
    922       1.1  tshiozak 		for (j=0; j<ep->num_jacks; j++) {
    923       1.1  tshiozak 			jack->endpoint = ep;
    924      1.26      chap 			if ( cn_spec != NULL )
    925      1.26      chap 				jack->cable_number = *cn_spec++;
    926      1.26      chap 			else if ( !sc->cblnums_global )
    927      1.26      chap 				jack->cable_number = j;
    928      1.26      chap 			ep->jacks[jack->cable_number] = jack;
    929       1.1  tshiozak 			jack++;
    930       1.1  tshiozak 		}
    931       1.1  tshiozak 		ep++;
    932       1.1  tshiozak 	}
    933       1.1  tshiozak 
    934       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    935       1.1  tshiozak }
    936       1.1  tshiozak 
    937       1.1  tshiozak static void
    938       1.1  tshiozak free_all_jacks(struct umidi_softc *sc)
    939       1.1  tshiozak {
    940       1.1  tshiozak 	int s;
    941       1.1  tshiozak 
    942       1.1  tshiozak 	s = splaudio();
    943       1.1  tshiozak 	if (sc->sc_out_jacks) {
    944       1.1  tshiozak 		free(sc->sc_jacks, M_USBDEV);
    945       1.1  tshiozak 		sc->sc_jacks = sc->sc_in_jacks = sc->sc_out_jacks = NULL;
    946       1.1  tshiozak 	}
    947       1.1  tshiozak 	splx(s);
    948       1.1  tshiozak }
    949       1.1  tshiozak 
    950       1.1  tshiozak static usbd_status
    951      1.28  christos bind_jacks_to_mididev(struct umidi_softc *sc,
    952       1.1  tshiozak 		      struct umidi_jack *out_jack,
    953       1.1  tshiozak 		      struct umidi_jack *in_jack,
    954       1.1  tshiozak 		      struct umidi_mididev *mididev)
    955       1.1  tshiozak {
    956       1.8  tshiozak 	if ((out_jack && out_jack->binded) || (in_jack && in_jack->binded))
    957       1.4  tshiozak 		return USBD_IN_USE;
    958       1.4  tshiozak 	if (mididev->out_jack || mididev->in_jack)
    959       1.1  tshiozak 		return USBD_IN_USE;
    960       1.1  tshiozak 
    961       1.4  tshiozak 	if (out_jack)
    962       1.4  tshiozak 		out_jack->binded = 1;
    963       1.4  tshiozak 	if (in_jack)
    964       1.4  tshiozak 		in_jack->binded = 1;
    965       1.1  tshiozak 	mididev->in_jack = in_jack;
    966       1.1  tshiozak 	mididev->out_jack = out_jack;
    967       1.1  tshiozak 
    968       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    969       1.1  tshiozak }
    970       1.1  tshiozak 
    971       1.4  tshiozak static void
    972       1.1  tshiozak unbind_jacks_from_mididev(struct umidi_mididev *mididev)
    973       1.1  tshiozak {
    974      1.15  tshiozak 	if ((mididev->flags & FWRITE) && mididev->out_jack)
    975       1.4  tshiozak 		close_out_jack(mididev->out_jack);
    976      1.15  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack)
    977       1.4  tshiozak 		close_in_jack(mididev->in_jack);
    978       1.1  tshiozak 
    979       1.4  tshiozak 	if (mididev->out_jack)
    980       1.4  tshiozak 		mididev->out_jack->binded = 0;
    981       1.4  tshiozak 	if (mididev->in_jack)
    982       1.4  tshiozak 		mididev->in_jack->binded = 0;
    983       1.4  tshiozak 	mididev->out_jack = mididev->in_jack = NULL;
    984       1.1  tshiozak }
    985       1.1  tshiozak 
    986       1.4  tshiozak static void
    987       1.1  tshiozak unbind_all_jacks(struct umidi_softc *sc)
    988       1.1  tshiozak {
    989       1.1  tshiozak 	int i;
    990       1.1  tshiozak 
    991       1.1  tshiozak 	if (sc->sc_mididevs)
    992       1.1  tshiozak 		for (i=0; i<sc->sc_num_mididevs; i++) {
    993       1.4  tshiozak 			unbind_jacks_from_mididev(&sc->sc_mididevs[i]);
    994       1.1  tshiozak 		}
    995       1.1  tshiozak }
    996       1.1  tshiozak 
    997       1.1  tshiozak static usbd_status
    998       1.1  tshiozak assign_all_jacks_automatically(struct umidi_softc *sc)
    999       1.1  tshiozak {
   1000       1.1  tshiozak 	usbd_status err;
   1001       1.1  tshiozak 	int i;
   1002       1.1  tshiozak 	struct umidi_jack *out, *in;
   1003      1.26      chap 	signed char *asg_spec;
   1004       1.1  tshiozak 
   1005       1.1  tshiozak 	err =
   1006       1.1  tshiozak 	    alloc_all_mididevs(sc,
   1007       1.1  tshiozak 			       max(sc->sc_out_num_jacks, sc->sc_in_num_jacks));
   1008       1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION)
   1009       1.1  tshiozak 		return err;
   1010       1.1  tshiozak 
   1011      1.26      chap 	if ( UMQ_ISTYPE(sc, UMQ_TYPE_MD_FIXED))
   1012      1.26      chap 		asg_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
   1013      1.26      chap 					    		  UMQ_TYPE_MD_FIXED);
   1014      1.26      chap 	else
   1015      1.26      chap 		asg_spec = NULL;
   1016      1.26      chap 
   1017       1.1  tshiozak 	for (i=0; i<sc->sc_num_mididevs; i++) {
   1018      1.26      chap 		if ( asg_spec != NULL ) {
   1019      1.26      chap 			if ( *asg_spec == -1 )
   1020      1.26      chap 				out = NULL;
   1021      1.26      chap 			else
   1022      1.26      chap 				out = &sc->sc_out_jacks[*asg_spec];
   1023      1.26      chap 			++ asg_spec;
   1024      1.26      chap 			if ( *asg_spec == -1 )
   1025      1.26      chap 				in = NULL;
   1026      1.26      chap 			else
   1027      1.26      chap 				in = &sc->sc_in_jacks[*asg_spec];
   1028      1.26      chap 			++ asg_spec;
   1029      1.26      chap 		} else {
   1030      1.26      chap 			out = (i<sc->sc_out_num_jacks) ? &sc->sc_out_jacks[i]
   1031      1.26      chap 			                               : NULL;
   1032      1.26      chap 			in = (i<sc->sc_in_num_jacks) ? &sc->sc_in_jacks[i]
   1033      1.26      chap 						     : NULL;
   1034      1.26      chap 		}
   1035       1.1  tshiozak 		err = bind_jacks_to_mididev(sc, out, in, &sc->sc_mididevs[i]);
   1036       1.1  tshiozak 		if (err!=USBD_NORMAL_COMPLETION) {
   1037       1.1  tshiozak 			free_all_mididevs(sc);
   1038       1.1  tshiozak 			return err;
   1039       1.1  tshiozak 		}
   1040       1.1  tshiozak 	}
   1041       1.1  tshiozak 
   1042       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1043       1.1  tshiozak }
   1044       1.1  tshiozak 
   1045       1.1  tshiozak static usbd_status
   1046       1.4  tshiozak open_out_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *))
   1047       1.4  tshiozak {
   1048       1.4  tshiozak 	struct umidi_endpoint *ep = jack->endpoint;
   1049      1.26      chap 	umidi_packet_bufp end;
   1050      1.26      chap 	int s;
   1051      1.26      chap 	int err;
   1052       1.4  tshiozak 
   1053       1.4  tshiozak 	if (jack->opened)
   1054       1.4  tshiozak 		return USBD_IN_USE;
   1055       1.4  tshiozak 
   1056       1.4  tshiozak 	jack->arg = arg;
   1057       1.4  tshiozak 	jack->u.out.intr = intr;
   1058      1.26      chap 	jack->midiman_ppkt = NULL;
   1059      1.26      chap 	end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
   1060      1.26      chap 	s = splusb();
   1061       1.4  tshiozak 	jack->opened = 1;
   1062       1.4  tshiozak 	ep->num_open++;
   1063      1.26      chap 	/*
   1064      1.26      chap 	 * out_solicit maintains an invariant that there will always be
   1065      1.26      chap 	 * (num_open - num_scheduled) slots free in the buffer. as we have
   1066      1.26      chap 	 * just incremented num_open, the buffer may be too full to satisfy
   1067      1.26      chap 	 * the invariant until a transfer completes, for which we must wait.
   1068      1.26      chap 	 */
   1069      1.26      chap 	while ( end - ep->next_slot < ep->num_open - ep->num_scheduled ) {
   1070      1.26      chap 		err = tsleep(ep, PWAIT|PCATCH, "umi op", mstohz(10));
   1071      1.26      chap 		if ( err ) {
   1072      1.26      chap 			ep->num_open--;
   1073      1.26      chap 			jack->opened = 0;
   1074      1.26      chap 			splx(s);
   1075      1.26      chap 			return USBD_IOERROR;
   1076      1.26      chap 		}
   1077      1.26      chap 	}
   1078      1.26      chap 	splx(s);
   1079       1.4  tshiozak 
   1080       1.4  tshiozak 	return USBD_NORMAL_COMPLETION;
   1081       1.4  tshiozak }
   1082       1.4  tshiozak 
   1083       1.4  tshiozak static usbd_status
   1084       1.4  tshiozak open_in_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *, int))
   1085       1.1  tshiozak {
   1086       1.3  tshiozak 	usbd_status err = USBD_NORMAL_COMPLETION;
   1087       1.3  tshiozak 	struct umidi_endpoint *ep = jack->endpoint;
   1088       1.3  tshiozak 
   1089       1.4  tshiozak 	if (jack->opened)
   1090       1.4  tshiozak 		return USBD_IN_USE;
   1091       1.4  tshiozak 
   1092       1.4  tshiozak 	jack->arg = arg;
   1093       1.4  tshiozak 	jack->u.in.intr = intr;
   1094       1.4  tshiozak 	jack->opened = 1;
   1095       1.3  tshiozak 	if (ep->num_open++==0 && UE_GET_DIR(ep->addr)==UE_DIR_IN) {
   1096       1.3  tshiozak 		err = start_input_transfer(ep);
   1097      1.18      gson 		if (err != USBD_NORMAL_COMPLETION &&
   1098      1.18      gson 		    err != USBD_IN_PROGRESS) {
   1099       1.3  tshiozak 			ep->num_open--;
   1100       1.3  tshiozak 		}
   1101       1.3  tshiozak 	}
   1102       1.3  tshiozak 
   1103       1.3  tshiozak 	return err;
   1104       1.1  tshiozak }
   1105       1.1  tshiozak 
   1106       1.1  tshiozak static void
   1107       1.4  tshiozak close_out_jack(struct umidi_jack *jack)
   1108       1.1  tshiozak {
   1109      1.26      chap 	struct umidi_endpoint *ep;
   1110       1.3  tshiozak 	int s;
   1111      1.26      chap 	u_int16_t mask;
   1112      1.26      chap 	int err;
   1113       1.4  tshiozak 
   1114       1.4  tshiozak 	if (jack->opened) {
   1115      1.26      chap 		ep = jack->endpoint;
   1116      1.26      chap 		mask = 1 << (jack->cable_number);
   1117       1.4  tshiozak 		s = splusb();
   1118      1.26      chap 		while ( mask & (ep->this_schedule | ep->next_schedule) ) {
   1119      1.26      chap 			err = tsleep(ep, PWAIT|PCATCH, "umi dr", mstohz(10));
   1120      1.26      chap 			if ( err )
   1121      1.15  tshiozak 				break;
   1122       1.3  tshiozak 		}
   1123       1.4  tshiozak 		jack->opened = 0;
   1124       1.4  tshiozak 		jack->endpoint->num_open--;
   1125      1.26      chap 		ep->this_schedule &= ~mask;
   1126      1.26      chap 		ep->next_schedule &= ~mask;
   1127      1.26      chap 		splx(s);
   1128       1.3  tshiozak 	}
   1129       1.1  tshiozak }
   1130       1.1  tshiozak 
   1131       1.4  tshiozak static void
   1132       1.4  tshiozak close_in_jack(struct umidi_jack *jack)
   1133       1.4  tshiozak {
   1134       1.4  tshiozak 	if (jack->opened) {
   1135       1.4  tshiozak 		jack->opened = 0;
   1136      1.19      gson 		if (--jack->endpoint->num_open == 0) {
   1137      1.19      gson 		    usbd_abort_pipe(jack->endpoint->pipe);
   1138      1.19      gson 		}
   1139       1.4  tshiozak 	}
   1140       1.4  tshiozak }
   1141       1.1  tshiozak 
   1142       1.1  tshiozak static usbd_status
   1143       1.1  tshiozak attach_mididev(struct umidi_softc *sc, struct umidi_mididev *mididev)
   1144       1.1  tshiozak {
   1145       1.1  tshiozak 	if (mididev->sc)
   1146       1.1  tshiozak 		return USBD_IN_USE;
   1147       1.1  tshiozak 
   1148       1.1  tshiozak 	mididev->sc = sc;
   1149      1.26      chap 
   1150      1.26      chap 	mididev->label = describe_mididev(mididev);
   1151       1.1  tshiozak 
   1152       1.1  tshiozak 	mididev->mdev = midi_attach_mi(&umidi_hw_if, mididev, &sc->sc_dev);
   1153       1.1  tshiozak 
   1154       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1155       1.1  tshiozak }
   1156       1.1  tshiozak 
   1157       1.1  tshiozak static usbd_status
   1158       1.1  tshiozak detach_mididev(struct umidi_mididev *mididev, int flags)
   1159       1.1  tshiozak {
   1160       1.1  tshiozak 	if (!mididev->sc)
   1161       1.1  tshiozak 		return USBD_NO_ADDR;
   1162       1.1  tshiozak 
   1163       1.1  tshiozak 	if (mididev->opened) {
   1164       1.1  tshiozak 		umidi_close(mididev);
   1165       1.1  tshiozak 	}
   1166       1.4  tshiozak 	unbind_jacks_from_mididev(mididev);
   1167       1.1  tshiozak 
   1168       1.1  tshiozak 	if (mididev->mdev)
   1169       1.1  tshiozak 		config_detach(mididev->mdev, flags);
   1170      1.26      chap 
   1171      1.26      chap 	if (NULL != mididev->label) {
   1172      1.26      chap 		free(mididev->label, M_USBDEV);
   1173      1.26      chap 		mididev->label = NULL;
   1174      1.26      chap 	}
   1175       1.1  tshiozak 
   1176       1.1  tshiozak 	mididev->sc = NULL;
   1177       1.1  tshiozak 
   1178       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1179       1.1  tshiozak }
   1180       1.1  tshiozak 
   1181       1.1  tshiozak static usbd_status
   1182       1.1  tshiozak deactivate_mididev(struct umidi_mididev *mididev)
   1183       1.1  tshiozak {
   1184       1.4  tshiozak 	if (mididev->out_jack)
   1185       1.4  tshiozak 		mididev->out_jack->binded = 0;
   1186       1.4  tshiozak 	if (mididev->in_jack)
   1187       1.4  tshiozak 		mididev->in_jack->binded = 0;
   1188       1.1  tshiozak 	config_deactivate(mididev->mdev);
   1189       1.1  tshiozak 
   1190       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1191       1.1  tshiozak }
   1192       1.1  tshiozak 
   1193       1.1  tshiozak static usbd_status
   1194       1.1  tshiozak alloc_all_mididevs(struct umidi_softc *sc, int nmidi)
   1195       1.1  tshiozak {
   1196       1.1  tshiozak 	sc->sc_num_mididevs = nmidi;
   1197       1.1  tshiozak 	sc->sc_mididevs = malloc(sizeof(*sc->sc_mididevs)*nmidi,
   1198      1.13   tsutsui 				 M_USBDEV, M_WAITOK|M_ZERO);
   1199       1.1  tshiozak 	if (!sc->sc_mididevs)
   1200       1.1  tshiozak 		return USBD_NOMEM;
   1201       1.1  tshiozak 
   1202       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1203       1.1  tshiozak }
   1204       1.1  tshiozak 
   1205       1.1  tshiozak static void
   1206       1.1  tshiozak free_all_mididevs(struct umidi_softc *sc)
   1207       1.1  tshiozak {
   1208       1.1  tshiozak 	sc->sc_num_mididevs = 0;
   1209       1.1  tshiozak 	if (sc->sc_mididevs)
   1210       1.1  tshiozak 		free(sc->sc_mididevs, M_USBDEV);
   1211       1.1  tshiozak }
   1212       1.1  tshiozak 
   1213       1.1  tshiozak static usbd_status
   1214       1.1  tshiozak attach_all_mididevs(struct umidi_softc *sc)
   1215       1.1  tshiozak {
   1216       1.1  tshiozak 	usbd_status err;
   1217       1.1  tshiozak 	int i;
   1218       1.1  tshiozak 
   1219       1.1  tshiozak 	if (sc->sc_mididevs)
   1220       1.1  tshiozak 		for (i=0; i<sc->sc_num_mididevs; i++) {
   1221       1.1  tshiozak 			err = attach_mididev(sc, &sc->sc_mididevs[i]);
   1222       1.1  tshiozak 			if (err!=USBD_NORMAL_COMPLETION)
   1223       1.1  tshiozak 				return err;
   1224       1.1  tshiozak 		}
   1225       1.1  tshiozak 
   1226       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1227       1.1  tshiozak }
   1228       1.1  tshiozak 
   1229       1.1  tshiozak static usbd_status
   1230       1.1  tshiozak detach_all_mididevs(struct umidi_softc *sc, int flags)
   1231       1.1  tshiozak {
   1232       1.1  tshiozak 	usbd_status err;
   1233       1.1  tshiozak 	int i;
   1234       1.1  tshiozak 
   1235       1.1  tshiozak 	if (sc->sc_mididevs)
   1236       1.1  tshiozak 		for (i=0; i<sc->sc_num_mididevs; i++) {
   1237       1.1  tshiozak 			err = detach_mididev(&sc->sc_mididevs[i], flags);
   1238       1.1  tshiozak 			if (err!=USBD_NORMAL_COMPLETION)
   1239       1.1  tshiozak 				return err;
   1240       1.1  tshiozak 		}
   1241       1.1  tshiozak 
   1242       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1243       1.1  tshiozak }
   1244       1.1  tshiozak 
   1245       1.1  tshiozak static usbd_status
   1246       1.1  tshiozak deactivate_all_mididevs(struct umidi_softc *sc)
   1247       1.1  tshiozak {
   1248       1.1  tshiozak 	usbd_status err;
   1249       1.1  tshiozak 	int i;
   1250       1.1  tshiozak 
   1251       1.1  tshiozak 	if (sc->sc_mididevs)
   1252       1.1  tshiozak 		for (i=0; i<sc->sc_num_mididevs; i++) {
   1253       1.1  tshiozak 			err = deactivate_mididev(&sc->sc_mididevs[i]);
   1254       1.1  tshiozak 			if (err!=USBD_NORMAL_COMPLETION)
   1255       1.1  tshiozak 				return err;
   1256       1.1  tshiozak 		}
   1257       1.1  tshiozak 
   1258       1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1259       1.1  tshiozak }
   1260       1.1  tshiozak 
   1261      1.26      chap /*
   1262      1.26      chap  * TODO: the 0-based cable numbers will often not match the labeling of the
   1263      1.26      chap  * equipment. Ideally:
   1264      1.26      chap  *  For class-compliant devices: get the iJack string from the jack descriptor.
   1265      1.26      chap  *  Otherwise:
   1266      1.26      chap  *  - support a DISPLAY_BASE_CN quirk (add the value to each internal cable
   1267      1.26      chap  *    number for display)
   1268      1.26      chap  *  - support an array quirk explictly giving a char * for each jack.
   1269      1.26      chap  * For now, you get 0-based cable numbers. If there are multiple endpoints and
   1270      1.26      chap  * the CNs are not globally unique, each is shown with its associated endpoint
   1271      1.26      chap  * address in hex also. That should not be necessary when using iJack values
   1272      1.26      chap  * or a quirk array.
   1273      1.26      chap  */
   1274      1.26      chap static char *
   1275      1.26      chap describe_mididev(struct umidi_mididev *md)
   1276      1.26      chap {
   1277      1.26      chap 	char in_label[16];
   1278      1.26      chap 	char out_label[16];
   1279      1.26      chap 	char *unit_label;
   1280      1.26      chap 	char *final_label;
   1281      1.26      chap 	struct umidi_softc *sc;
   1282      1.26      chap 	int show_ep_in;
   1283      1.26      chap 	int show_ep_out;
   1284      1.26      chap 	size_t len;
   1285      1.26      chap 
   1286      1.26      chap 	sc = md->sc;
   1287      1.26      chap 	show_ep_in  = sc-> sc_in_num_endpoints > 1 && !sc->cblnums_global;
   1288      1.26      chap 	show_ep_out = sc->sc_out_num_endpoints > 1 && !sc->cblnums_global;
   1289      1.26      chap 
   1290      1.26      chap 	if ( NULL != md->in_jack )
   1291      1.26      chap 		snprintf(in_label, sizeof in_label,
   1292      1.26      chap 		    show_ep_in ? "<%d(%x) " : "<%d ",
   1293      1.26      chap 		    md->in_jack->cable_number,
   1294      1.26      chap 		    md->in_jack->endpoint->addr);
   1295      1.26      chap 	else
   1296      1.26      chap 		in_label[0] = '\0';
   1297      1.26      chap 
   1298      1.26      chap 	if ( NULL != md->out_jack )
   1299      1.26      chap 		snprintf(out_label, sizeof out_label,
   1300      1.26      chap 		    show_ep_out ? ">%d(%x) " : ">%d ",
   1301      1.26      chap 		    md->out_jack->cable_number,
   1302      1.26      chap 		    md->out_jack->endpoint->addr);
   1303      1.26      chap 	else
   1304      1.26      chap 		in_label[0] = '\0';
   1305      1.26      chap 
   1306      1.26      chap 	unit_label = USBDEVNAME(sc->sc_dev);
   1307      1.26      chap 
   1308      1.26      chap 	len = strlen(in_label) + strlen(out_label) + strlen(unit_label) + 4;
   1309      1.26      chap 
   1310      1.26      chap 	final_label = malloc(len, M_USBDEV, M_WAITOK);
   1311      1.26      chap 
   1312      1.26      chap 	snprintf(final_label, len, "%s%son %s",
   1313      1.26      chap 	    in_label, out_label, unit_label);
   1314      1.26      chap 
   1315      1.26      chap 	return final_label;
   1316      1.26      chap }
   1317      1.26      chap 
   1318       1.1  tshiozak #ifdef UMIDI_DEBUG
   1319       1.1  tshiozak static void
   1320       1.1  tshiozak dump_sc(struct umidi_softc *sc)
   1321       1.1  tshiozak {
   1322       1.1  tshiozak 	int i;
   1323       1.1  tshiozak 
   1324       1.1  tshiozak 	DPRINTFN(10, ("%s: dump_sc\n", USBDEVNAME(sc->sc_dev)));
   1325       1.1  tshiozak 	for (i=0; i<sc->sc_out_num_endpoints; i++) {
   1326       1.1  tshiozak 		DPRINTFN(10, ("\tout_ep(%p):\n", &sc->sc_out_ep[i]));
   1327       1.1  tshiozak 		dump_ep(&sc->sc_out_ep[i]);
   1328       1.1  tshiozak 	}
   1329       1.1  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints; i++) {
   1330       1.1  tshiozak 		DPRINTFN(10, ("\tin_ep(%p):\n", &sc->sc_in_ep[i]));
   1331       1.1  tshiozak 		dump_ep(&sc->sc_in_ep[i]);
   1332       1.1  tshiozak 	}
   1333       1.1  tshiozak }
   1334       1.1  tshiozak 
   1335       1.1  tshiozak static void
   1336       1.1  tshiozak dump_ep(struct umidi_endpoint *ep)
   1337       1.1  tshiozak {
   1338       1.1  tshiozak 	int i;
   1339      1.26      chap 	for (i=0; i<UMIDI_MAX_EPJACKS; i++) {
   1340      1.26      chap 		if (NULL==ep->jacks[i])
   1341      1.26      chap 			continue;
   1342      1.26      chap 		DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i, ep->jacks[i]));
   1343       1.1  tshiozak 		dump_jack(ep->jacks[i]);
   1344       1.1  tshiozak 	}
   1345       1.1  tshiozak }
   1346       1.1  tshiozak static void
   1347       1.1  tshiozak dump_jack(struct umidi_jack *jack)
   1348       1.1  tshiozak {
   1349      1.15  tshiozak 	DPRINTFN(10, ("\t\t\tep=%p\n",
   1350      1.15  tshiozak 		      jack->endpoint));
   1351       1.1  tshiozak }
   1352       1.1  tshiozak 
   1353       1.1  tshiozak #endif /* UMIDI_DEBUG */
   1354       1.1  tshiozak 
   1355       1.1  tshiozak 
   1356       1.1  tshiozak 
   1357       1.1  tshiozak /*
   1358       1.1  tshiozak  * MUX MIDI PACKET
   1359       1.1  tshiozak  */
   1360       1.1  tshiozak 
   1361       1.5  jdolecek static const int packet_length[16] = {
   1362       1.1  tshiozak 	/*0*/	-1,
   1363       1.1  tshiozak 	/*1*/	-1,
   1364       1.1  tshiozak 	/*2*/	2,
   1365       1.1  tshiozak 	/*3*/	3,
   1366       1.1  tshiozak 	/*4*/	3,
   1367       1.1  tshiozak 	/*5*/	1,
   1368       1.1  tshiozak 	/*6*/	2,
   1369       1.1  tshiozak 	/*7*/	3,
   1370       1.1  tshiozak 	/*8*/	3,
   1371       1.1  tshiozak 	/*9*/	3,
   1372       1.1  tshiozak 	/*A*/	3,
   1373       1.1  tshiozak 	/*B*/	3,
   1374       1.1  tshiozak 	/*C*/	2,
   1375       1.1  tshiozak 	/*D*/	2,
   1376       1.1  tshiozak 	/*E*/	3,
   1377       1.1  tshiozak 	/*F*/	1,
   1378       1.1  tshiozak };
   1379       1.1  tshiozak 
   1380       1.1  tshiozak #define	GET_CN(p)		(((unsigned char)(p)>>4)&0x0F)
   1381       1.1  tshiozak #define GET_CIN(p)		((unsigned char)(p)&0x0F)
   1382       1.1  tshiozak #define MIX_CN_CIN(cn, cin) \
   1383       1.1  tshiozak 	((unsigned char)((((unsigned char)(cn)&0x0F)<<4)| \
   1384       1.3  tshiozak 			  ((unsigned char)(cin)&0x0F)))
   1385       1.1  tshiozak 
   1386       1.1  tshiozak static usbd_status
   1387       1.1  tshiozak start_input_transfer(struct umidi_endpoint *ep)
   1388       1.1  tshiozak {
   1389       1.1  tshiozak 	usbd_setup_xfer(ep->xfer, ep->pipe,
   1390       1.1  tshiozak 			(usbd_private_handle)ep,
   1391      1.26      chap 			ep->buffer, ep->buffer_size,
   1392      1.26      chap 			USBD_SHORT_XFER_OK | USBD_NO_COPY,
   1393      1.26      chap                         USBD_NO_TIMEOUT, in_intr);
   1394       1.1  tshiozak 	return usbd_transfer(ep->xfer);
   1395       1.1  tshiozak }
   1396       1.1  tshiozak 
   1397       1.1  tshiozak static usbd_status
   1398       1.1  tshiozak start_output_transfer(struct umidi_endpoint *ep)
   1399       1.1  tshiozak {
   1400      1.26      chap 	usbd_status rv;
   1401      1.26      chap 	u_int32_t length;
   1402      1.26      chap 	int i;
   1403      1.26      chap 
   1404      1.26      chap 	length = (ep->next_slot - ep->buffer) * sizeof *ep->buffer;
   1405      1.26      chap 	DPRINTFN(200,("umidi out transfer: start %p end %p length %u\n",
   1406      1.26      chap 	    ep->buffer, ep->next_slot, length));
   1407       1.1  tshiozak 	usbd_setup_xfer(ep->xfer, ep->pipe,
   1408       1.1  tshiozak 			(usbd_private_handle)ep,
   1409      1.26      chap 			ep->buffer, length,
   1410      1.11  augustss 			USBD_NO_COPY, USBD_NO_TIMEOUT, out_intr);
   1411      1.26      chap 	rv = usbd_transfer(ep->xfer);
   1412      1.26      chap 
   1413      1.26      chap 	/*
   1414      1.26      chap 	 * Once the transfer is scheduled, no more adding to partial
   1415      1.26      chap 	 * packets within it.
   1416      1.26      chap 	 */
   1417      1.26      chap 	if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1418      1.26      chap 		for (i=0; i<UMIDI_MAX_EPJACKS; ++i)
   1419      1.26      chap 			if (NULL != ep->jacks[i])
   1420      1.26      chap 				ep->jacks[i]->midiman_ppkt = NULL;
   1421      1.26      chap 	}
   1422      1.26      chap 
   1423      1.26      chap 	return rv;
   1424       1.1  tshiozak }
   1425       1.1  tshiozak 
   1426       1.4  tshiozak #ifdef UMIDI_DEBUG
   1427       1.4  tshiozak #define DPR_PACKET(dir, sc, p)						\
   1428      1.26      chap if ((unsigned char)(p)[1]!=0xFE)				\
   1429       1.4  tshiozak 	DPRINTFN(500,							\
   1430       1.4  tshiozak 		 ("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n",	\
   1431       1.4  tshiozak 		  USBDEVNAME(sc->sc_dev),				\
   1432      1.26      chap 		  (unsigned char)(p)[0],			\
   1433      1.26      chap 		  (unsigned char)(p)[1],			\
   1434      1.26      chap 		  (unsigned char)(p)[2],			\
   1435      1.26      chap 		  (unsigned char)(p)[3]));
   1436       1.4  tshiozak #else
   1437       1.4  tshiozak #define DPR_PACKET(dir, sc, p)
   1438       1.4  tshiozak #endif
   1439       1.4  tshiozak 
   1440      1.26      chap /*
   1441      1.26      chap  * A 4-byte Midiman packet superficially resembles a 4-byte USB MIDI packet
   1442      1.26      chap  * with the cable number and length in the last byte instead of the first,
   1443      1.26      chap  * but there the resemblance ends. Where a USB MIDI packet is a semantic
   1444      1.26      chap  * unit, a Midiman packet is just a wrapper for 1 to 3 bytes of raw MIDI
   1445      1.26      chap  * with a cable nybble and a length nybble (which, unlike the CIN of a
   1446      1.26      chap  * real USB MIDI packet, has no semantics at all besides the length).
   1447      1.26      chap  * A packet received from a Midiman may contain part of a MIDI message,
   1448      1.26      chap  * more than one MIDI message, or parts of more than one MIDI message. A
   1449      1.26      chap  * three-byte MIDI message may arrive in three packets of data length 1, and
   1450      1.26      chap  * running status may be used. Happily, the midi(4) driver above us will put
   1451      1.26      chap  * it all back together, so the only cost is in USB bandwidth. The device
   1452      1.26      chap  * has an easier time with what it receives from us: we'll pack messages in
   1453      1.26      chap  * and across packets, but filling the packets whenever possible and,
   1454      1.26      chap  * as midi(4) hands us a complete message at a time, we'll never send one
   1455      1.26      chap  * in a dribble of short packets.
   1456      1.26      chap  */
   1457      1.26      chap 
   1458       1.4  tshiozak static int
   1459      1.26      chap out_jack_output(struct umidi_jack *out_jack, u_char *src, int len, int cin)
   1460       1.4  tshiozak {
   1461       1.4  tshiozak 	struct umidi_endpoint *ep = out_jack->endpoint;
   1462       1.4  tshiozak 	struct umidi_softc *sc = ep->sc;
   1463      1.26      chap 	unsigned char *packet;
   1464       1.4  tshiozak 	int s;
   1465      1.26      chap 	int plen;
   1466      1.26      chap 	int poff;
   1467       1.4  tshiozak 
   1468       1.4  tshiozak 	if (sc->sc_dying)
   1469       1.4  tshiozak 		return EIO;
   1470       1.4  tshiozak 
   1471      1.26      chap 	if (!out_jack->opened)
   1472      1.26      chap 		return ENODEV; /* XXX as it was, is this the right errno? */
   1473       1.4  tshiozak 
   1474      1.26      chap #ifdef UMIDI_DEBUG
   1475      1.26      chap 	if ( umididebug >= 100 )
   1476      1.26      chap 		microtime(&umidi_tv);
   1477      1.26      chap #endif
   1478      1.26      chap 	DPRINTFN(100, ("umidi out: %lu.%06lus ep=%p cn=%d len=%d cin=%#x\n",
   1479      1.26      chap 	    umidi_tv.tv_sec%100, umidi_tv.tv_usec,
   1480      1.26      chap 	    ep, out_jack->cable_number, len, cin));
   1481      1.26      chap 
   1482      1.26      chap 	s = splusb();
   1483      1.26      chap 	packet = *ep->next_slot++;
   1484      1.26      chap 	KASSERT(ep->buffer_size >=
   1485      1.26      chap 	    (ep->next_slot - ep->buffer) * sizeof *ep->buffer);
   1486      1.26      chap 	memset(packet, 0, UMIDI_PACKET_SIZE);
   1487      1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1488      1.26      chap 		if (NULL != out_jack->midiman_ppkt) { /* fill out a prev pkt */
   1489      1.26      chap 			poff = 0x0f & (out_jack->midiman_ppkt[3]);
   1490      1.26      chap 			plen = 3 - poff;
   1491      1.26      chap 			if (plen > len)
   1492      1.26      chap 				plen = len;
   1493      1.26      chap 			memcpy(out_jack->midiman_ppkt+poff, src, plen);
   1494      1.26      chap 			src += plen;
   1495      1.26      chap 			len -= plen;
   1496      1.26      chap 			plen += poff;
   1497      1.26      chap 			out_jack->midiman_ppkt[3] =
   1498      1.26      chap 			    MIX_CN_CIN(out_jack->cable_number, plen);
   1499      1.26      chap 			DPR_PACKET(out+, sc, out_jack->midiman_ppkt);
   1500      1.26      chap 			if (3 == plen)
   1501      1.26      chap 				out_jack->midiman_ppkt = NULL; /* no more */
   1502      1.26      chap 		}
   1503      1.26      chap 		if (0 == len)
   1504      1.26      chap 			ep->next_slot--; /* won't be needed, nevermind */
   1505      1.26      chap 		else {
   1506      1.26      chap 			memcpy(packet, src, len);
   1507      1.26      chap 			packet[3] = MIX_CN_CIN(out_jack->cable_number, len);
   1508      1.26      chap 			DPR_PACKET(out, sc, packet);
   1509      1.26      chap 			if (len < 3)
   1510      1.26      chap 				out_jack->midiman_ppkt = packet;
   1511      1.26      chap 		}
   1512      1.26      chap 	} else { /* the nice simple USB class-compliant case */
   1513      1.26      chap 		packet[0] = MIX_CN_CIN(out_jack->cable_number, cin);
   1514      1.26      chap 		memcpy(packet+1, src, len);
   1515      1.26      chap 		DPR_PACKET(out, sc, packet);
   1516      1.26      chap 	}
   1517      1.26      chap 	ep->next_schedule |= 1<<(out_jack->cable_number);
   1518      1.26      chap 	++ ep->num_scheduled;
   1519      1.26      chap 	if ( !ep->armed  &&  !ep->soliciting ) {
   1520      1.26      chap #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
   1521      1.26      chap 		/*
   1522      1.26      chap 		 * It would be bad to call out_solicit directly here (the
   1523      1.26      chap 		 * caller need not be reentrant) but a soft interrupt allows
   1524      1.26      chap 		 * solicit to run immediately the caller exits its critical
   1525      1.26      chap 		 * section, and if the caller has more to write we can get it
   1526      1.26      chap 		 * before starting the USB transfer, and send a longer one.
   1527      1.26      chap 		 */
   1528      1.26      chap 		ep->soliciting = 1;
   1529      1.26      chap 		softintr_schedule(ep->solicit_cookie);
   1530      1.26      chap #else
   1531      1.26      chap 		/*
   1532      1.26      chap 		 * This alternative is a little less desirable, because if the
   1533      1.26      chap 		 * writer has several messages to go at once, the first will go
   1534      1.26      chap 		 * in a USB frame all to itself, and the rest in a full-size
   1535      1.26      chap 		 * transfer one frame later (solicited on the first frame's
   1536      1.26      chap 		 * completion interrupt). But it's simple.
   1537      1.26      chap 		 */
   1538      1.26      chap 		ep->armed = (USBD_IN_PROGRESS == start_output_transfer(ep));
   1539      1.26      chap #endif
   1540      1.26      chap 	}
   1541      1.26      chap 	splx(s);
   1542      1.26      chap 
   1543      1.26      chap 	return 0;
   1544       1.4  tshiozak }
   1545       1.4  tshiozak 
   1546       1.1  tshiozak static void
   1547      1.27  christos in_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
   1548      1.28  christos     usbd_status status)
   1549       1.1  tshiozak {
   1550       1.4  tshiozak 	int cn, len, i;
   1551       1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1552       1.4  tshiozak 	struct umidi_jack *jack;
   1553      1.26      chap 	unsigned char *packet;
   1554      1.26      chap 	umidi_packet_bufp slot;
   1555      1.26      chap 	umidi_packet_bufp end;
   1556      1.26      chap 	unsigned char *data;
   1557      1.26      chap 	u_int32_t count;
   1558       1.1  tshiozak 
   1559       1.3  tshiozak 	if (ep->sc->sc_dying || !ep->num_open)
   1560       1.1  tshiozak 		return;
   1561       1.1  tshiozak 
   1562      1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1563      1.26      chap         if ( 0 == count % UMIDI_PACKET_SIZE ) {
   1564      1.26      chap 		DPRINTFN(200,("%s: input endpoint %p transfer length %u\n",
   1565      1.26      chap 			     USBDEVNAME(ep->sc->sc_dev), ep, count));
   1566      1.26      chap         } else {
   1567      1.26      chap                 DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
   1568      1.26      chap                         USBDEVNAME(ep->sc->sc_dev), ep, count));
   1569      1.26      chap         }
   1570      1.26      chap 
   1571      1.26      chap 	slot = ep->buffer;
   1572      1.26      chap 	end = slot + count / sizeof *slot;
   1573      1.26      chap 
   1574      1.26      chap 	for ( packet = *slot; slot < end; packet = *++slot ) {
   1575      1.26      chap 
   1576      1.26      chap 		if ( UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE) ) {
   1577      1.26      chap 			cn = (0xf0&(packet[3]))>>4;
   1578      1.26      chap 			len = 0x0f&(packet[3]);
   1579      1.26      chap 			data = packet;
   1580      1.26      chap 		} else {
   1581      1.26      chap 			cn = GET_CN(packet[0]);
   1582      1.26      chap 			len = packet_length[GET_CIN(packet[0])];
   1583      1.26      chap 			data = packet + 1;
   1584      1.26      chap 		}
   1585      1.26      chap 		/* 0 <= cn <= 15 by inspection of above code */
   1586      1.26      chap 		if (!(jack = ep->jacks[cn]) || cn != jack->cable_number) {
   1587      1.26      chap 			DPRINTF(("%s: stray input endpoint %p cable %d len %d: "
   1588      1.26      chap 			         "%02X %02X %02X (try CN_SEQ quirk?)\n",
   1589      1.26      chap 				 USBDEVNAME(ep->sc->sc_dev), ep, cn, len,
   1590      1.26      chap 				 (unsigned)data[0],
   1591      1.26      chap 				 (unsigned)data[1],
   1592      1.26      chap 				 (unsigned)data[2]));
   1593      1.26      chap 			return;
   1594      1.26      chap 		}
   1595      1.26      chap 
   1596      1.26      chap 		if (!jack->binded || !jack->opened)
   1597      1.26      chap 			continue;
   1598      1.26      chap 
   1599      1.26      chap 		DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
   1600      1.26      chap 		             "%02X %02X %02X\n",
   1601      1.26      chap 			     USBDEVNAME(ep->sc->sc_dev), ep, cn, len,
   1602      1.26      chap 			     (unsigned)data[0],
   1603      1.26      chap 			     (unsigned)data[1],
   1604      1.26      chap 			     (unsigned)data[2]));
   1605      1.26      chap 
   1606      1.26      chap 		if (jack->u.in.intr) {
   1607      1.26      chap 			for (i=0; i<len; i++) {
   1608      1.26      chap 				(*jack->u.in.intr)(jack->arg, data[i]);
   1609      1.26      chap 			}
   1610       1.4  tshiozak 		}
   1611      1.26      chap 
   1612       1.4  tshiozak 	}
   1613       1.1  tshiozak 
   1614       1.1  tshiozak 	(void)start_input_transfer(ep);
   1615       1.1  tshiozak }
   1616       1.1  tshiozak 
   1617       1.1  tshiozak static void
   1618      1.27  christos out_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
   1619      1.28  christos     usbd_status status)
   1620       1.1  tshiozak {
   1621       1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1622       1.1  tshiozak 	struct umidi_softc *sc = ep->sc;
   1623      1.26      chap 	u_int32_t count;
   1624       1.1  tshiozak 
   1625      1.26      chap 	if (sc->sc_dying)
   1626       1.1  tshiozak 		return;
   1627       1.1  tshiozak 
   1628      1.26      chap #ifdef UMIDI_DEBUG
   1629      1.26      chap 	if ( umididebug >= 200 )
   1630      1.26      chap 		microtime(&umidi_tv);
   1631      1.26      chap #endif
   1632      1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1633      1.26      chap         if ( 0 == count % UMIDI_PACKET_SIZE ) {
   1634      1.26      chap 		DPRINTFN(200,("%s: %lu.%06lus out ep %p xfer length %u\n",
   1635      1.26      chap 			     USBDEVNAME(ep->sc->sc_dev),
   1636      1.26      chap 			     umidi_tv.tv_sec%100, umidi_tv.tv_usec, ep, count));
   1637      1.26      chap         } else {
   1638      1.26      chap                 DPRINTF(("%s: output endpoint %p odd transfer length %u\n",
   1639      1.26      chap                         USBDEVNAME(ep->sc->sc_dev), ep, count));
   1640      1.26      chap         }
   1641      1.26      chap 	count /= UMIDI_PACKET_SIZE;
   1642      1.26      chap 
   1643      1.26      chap 	/*
   1644      1.26      chap 	 * If while the transfer was pending we buffered any new messages,
   1645      1.26      chap 	 * move them to the start of the buffer.
   1646      1.26      chap 	 */
   1647      1.26      chap 	ep->next_slot -= count;
   1648      1.26      chap 	if ( ep->buffer < ep->next_slot ) {
   1649      1.26      chap 		memcpy(ep->buffer, ep->buffer + count,
   1650      1.26      chap 		       (char *)ep->next_slot - (char *)ep->buffer);
   1651      1.26      chap 	}
   1652      1.26      chap 	wakeup(ep);
   1653      1.26      chap 	/*
   1654      1.26      chap 	 * Do not want anyone else to see armed <- 0 before soliciting <- 1.
   1655      1.26      chap 	 * Running at splusb so the following should happen to be safe.
   1656      1.26      chap 	 */
   1657      1.26      chap 	ep->armed = 0;
   1658      1.26      chap 	if ( !ep->soliciting ) {
   1659      1.26      chap 		ep->soliciting = 1;
   1660      1.26      chap 		out_solicit(ep);
   1661       1.1  tshiozak 	}
   1662       1.1  tshiozak }
   1663       1.1  tshiozak 
   1664      1.26      chap /*
   1665      1.26      chap  * A jack on which we have received a packet must be called back on its
   1666      1.26      chap  * out.intr handler before it will send us another; it is considered
   1667      1.26      chap  * 'scheduled'. It is nice and predictable - as long as it is scheduled,
   1668      1.26      chap  * we need no extra buffer space for it.
   1669      1.26      chap  *
   1670      1.26      chap  * In contrast, a jack that is open but not scheduled may supply us a packet
   1671      1.26      chap  * at any time, driven by the top half, and we must be able to accept it, no
   1672      1.26      chap  * excuses. So we must ensure that at any point in time there are at least
   1673      1.26      chap  * (num_open - num_scheduled) slots free.
   1674      1.26      chap  *
   1675      1.26      chap  * As long as there are more slots free than that minimum, we can loop calling
   1676      1.26      chap  * scheduled jacks back on their "interrupt" handlers, soliciting more
   1677      1.26      chap  * packets, starting the USB transfer only when the buffer space is down to
   1678      1.26      chap  * the minimum or no jack has any more to send.
   1679      1.26      chap  */
   1680       1.1  tshiozak static void
   1681      1.26      chap out_solicit(void *arg)
   1682       1.1  tshiozak {
   1683      1.26      chap 	struct umidi_endpoint *ep = arg;
   1684      1.26      chap 	int s;
   1685      1.26      chap 	umidi_packet_bufp end;
   1686      1.26      chap 	u_int16_t which;
   1687      1.26      chap 	struct umidi_jack *jack;
   1688      1.26      chap 
   1689      1.26      chap 	end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
   1690      1.26      chap 
   1691      1.26      chap 	for ( ;; ) {
   1692      1.26      chap 		s = splusb();
   1693      1.26      chap 		if ( end - ep->next_slot <= ep->num_open - ep->num_scheduled )
   1694      1.26      chap 			break; /* at splusb */
   1695      1.26      chap 		if ( ep->this_schedule == 0 ) {
   1696      1.26      chap 			if ( ep->next_schedule == 0 )
   1697      1.26      chap 				break; /* at splusb */
   1698      1.26      chap 			ep->this_schedule = ep->next_schedule;
   1699      1.26      chap 			ep->next_schedule = 0;
   1700      1.26      chap 		}
   1701      1.26      chap 		/*
   1702      1.26      chap 		 * At least one jack is scheduled. Find and mask off the least
   1703      1.26      chap 		 * set bit in this_schedule and decrement num_scheduled.
   1704      1.26      chap 		 * Convert mask to bit index to find the corresponding jack,
   1705      1.26      chap 		 * and call its intr handler. If it has a message, it will call
   1706      1.26      chap 		 * back one of the output methods, which will set its bit in
   1707      1.26      chap 		 * next_schedule (not copied into this_schedule until the
   1708      1.26      chap 		 * latter is empty). In this way we round-robin the jacks that
   1709      1.26      chap 		 * have messages to send, until the buffer is as full as we
   1710      1.26      chap 		 * dare, and then start a transfer.
   1711      1.26      chap 		 */
   1712      1.26      chap 		which = ep->this_schedule;
   1713      1.26      chap 		which &= (~which)+1; /* now mask of least set bit */
   1714      1.26      chap 		ep->this_schedule &= ~which;
   1715      1.26      chap 		-- ep->num_scheduled;
   1716      1.26      chap 		splx(s);
   1717       1.1  tshiozak 
   1718      1.26      chap 		-- which; /* now 1s below mask - count 1s to get index */
   1719      1.26      chap 		which -= ((which >> 1) & 0x5555);/* SWAR credit aggregate.org */
   1720      1.26      chap 		which = (((which >> 2) & 0x3333) + (which & 0x3333));
   1721      1.26      chap 		which = (((which >> 4) + which) & 0x0f0f);
   1722      1.26      chap 		which +=  (which >> 8);
   1723      1.26      chap 		which &= 0x1f; /* the bit index a/k/a jack number */
   1724      1.26      chap 
   1725      1.26      chap 		jack = ep->jacks[which];
   1726      1.26      chap 		if (jack->u.out.intr)
   1727      1.26      chap 			(*jack->u.out.intr)(jack->arg);
   1728       1.1  tshiozak 	}
   1729      1.26      chap 	/* splusb at loop exit */
   1730      1.26      chap 	if ( !ep->armed  &&  ep->next_slot > ep->buffer )
   1731      1.26      chap 		ep->armed = (USBD_IN_PROGRESS == start_output_transfer(ep));
   1732      1.26      chap 	ep->soliciting = 0;
   1733      1.26      chap 	splx(s);
   1734       1.1  tshiozak }
   1735