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