Home | History | Annotate | Line # | Download | only in usb
umidi.c revision 1.65.14.8
      1  1.65.14.8     skrll /*	$NetBSD: umidi.c,v 1.65.14.8 2015/10/06 21:32:15 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.8     skrll __KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.65.14.8 2015/10/06 21:32:15 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.65.14.8     skrll 	 * all cases. But to use usbd_create_xfer 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.64  jmcneill 	err = usbd_open_pipe(sc->sc_iface, ep->addr, USBD_MPSAFE, &ep->pipe);
    689        1.3  tshiozak 	if (err)
    690  1.65.14.8     skrll 		goto quit;
    691  1.65.14.8     skrll 	int error = usbd_create_xfer(ep->pipe, ep->buffer_size,
    692  1.65.14.8     skrll 	    USBD_SHORT_XFER_OK, 0, &ep->xfer);
    693  1.65.14.8     skrll 	if (error) {
    694  1.65.14.8     skrll 		usbd_close_pipe(ep->pipe);
    695  1.65.14.8     skrll 		return USBD_NOMEM;
    696  1.65.14.8     skrll 	}
    697  1.65.14.8     skrll 	ep->buffer = usbd_get_buffer(ep->xfer);
    698  1.65.14.8     skrll 	ep->next_slot = ep->buffer;
    699       1.65  jmcneill 	ep->solicit_cookie = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE, out_solicit, ep);
    700        1.1  tshiozak quit:
    701        1.1  tshiozak 	return err;
    702        1.1  tshiozak }
    703        1.1  tshiozak 
    704        1.1  tshiozak static void
    705        1.3  tshiozak free_pipe(struct umidi_endpoint *ep)
    706        1.1  tshiozak {
    707       1.42    dyoung 	DPRINTF(("%s: free_pipe %p\n", device_xname(ep->sc->sc_dev), ep));
    708        1.3  tshiozak 	usbd_abort_pipe(ep->pipe);
    709        1.3  tshiozak 	usbd_close_pipe(ep->pipe);
    710  1.65.14.8     skrll 	usbd_destroy_xfer(ep->xfer);
    711       1.32        ad 	softint_disestablish(ep->solicit_cookie);
    712        1.1  tshiozak }
    713        1.1  tshiozak 
    714        1.1  tshiozak 
    715        1.1  tshiozak /* alloc/free the array of endpoint structures */
    716        1.1  tshiozak 
    717        1.1  tshiozak static usbd_status alloc_all_endpoints_fixed_ep(struct umidi_softc *);
    718        1.1  tshiozak static usbd_status alloc_all_endpoints_yamaha(struct umidi_softc *);
    719        1.1  tshiozak static usbd_status alloc_all_endpoints_genuine(struct umidi_softc *);
    720        1.1  tshiozak 
    721        1.1  tshiozak static usbd_status
    722        1.1  tshiozak alloc_all_endpoints(struct umidi_softc *sc)
    723        1.1  tshiozak {
    724        1.3  tshiozak 	usbd_status err;
    725        1.3  tshiozak 	struct umidi_endpoint *ep;
    726        1.3  tshiozak 	int i;
    727        1.9  tshiozak 
    728        1.3  tshiozak 	if (UMQ_ISTYPE(sc, UMQ_TYPE_FIXED_EP)) {
    729        1.3  tshiozak 		err = alloc_all_endpoints_fixed_ep(sc);
    730        1.3  tshiozak 	} else if (UMQ_ISTYPE(sc, UMQ_TYPE_YAMAHA)) {
    731        1.3  tshiozak 		err = alloc_all_endpoints_yamaha(sc);
    732        1.1  tshiozak 	} else {
    733        1.3  tshiozak 		err = alloc_all_endpoints_genuine(sc);
    734        1.1  tshiozak 	}
    735       1.46       mrg 	if (err != USBD_NORMAL_COMPLETION)
    736        1.3  tshiozak 		return err;
    737        1.3  tshiozak 
    738        1.3  tshiozak 	ep = sc->sc_endpoints;
    739       1.52       mrg 	for (i = sc->sc_out_num_endpoints+sc->sc_in_num_endpoints; i > 0; i--) {
    740        1.3  tshiozak 		err = alloc_pipe(ep++);
    741       1.52       mrg 		if (err != USBD_NORMAL_COMPLETION) {
    742       1.52       mrg 			for (; ep != sc->sc_endpoints; ep--)
    743        1.3  tshiozak 				free_pipe(ep-1);
    744       1.52       mrg 			kmem_free(sc->sc_endpoints, sc->sc_endpoints_len);
    745        1.3  tshiozak 			sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    746        1.3  tshiozak 			break;
    747        1.3  tshiozak 		}
    748        1.3  tshiozak 	}
    749        1.3  tshiozak 	return err;
    750        1.1  tshiozak }
    751        1.1  tshiozak 
    752        1.1  tshiozak static void
    753        1.1  tshiozak free_all_endpoints(struct umidi_softc *sc)
    754        1.1  tshiozak {
    755        1.3  tshiozak 	int i;
    756       1.46       mrg 
    757        1.8  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints+sc->sc_out_num_endpoints; i++)
    758       1.46       mrg 		free_pipe(&sc->sc_endpoints[i]);
    759       1.14      kent 	if (sc->sc_endpoints != NULL)
    760       1.52       mrg 		kmem_free(sc->sc_endpoints, sc->sc_endpoints_len);
    761        1.1  tshiozak 	sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    762        1.1  tshiozak }
    763        1.1  tshiozak 
    764        1.1  tshiozak static usbd_status
    765        1.1  tshiozak alloc_all_endpoints_fixed_ep(struct umidi_softc *sc)
    766        1.1  tshiozak {
    767        1.3  tshiozak 	usbd_status err;
    768       1.38  gmcgarry 	const struct umq_fixed_ep_desc *fp;
    769        1.3  tshiozak 	struct umidi_endpoint *ep;
    770        1.1  tshiozak 	usb_endpoint_descriptor_t *epd;
    771        1.1  tshiozak 	int i;
    772        1.1  tshiozak 
    773        1.1  tshiozak 	fp = umidi_get_quirk_data_from_type(sc->sc_quirk,
    774        1.1  tshiozak 					    UMQ_TYPE_FIXED_EP);
    775        1.1  tshiozak 	sc->sc_out_num_jacks = 0;
    776        1.1  tshiozak 	sc->sc_in_num_jacks = 0;
    777        1.1  tshiozak 	sc->sc_out_num_endpoints = fp->num_out_ep;
    778        1.1  tshiozak 	sc->sc_in_num_endpoints = fp->num_in_ep;
    779       1.52       mrg 	sc->sc_endpoints_len = UMIDI_ENDPOINT_SIZE(sc);
    780       1.52       mrg 	sc->sc_endpoints = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    781       1.52       mrg 	if (!sc->sc_endpoints)
    782        1.1  tshiozak 		return USBD_NOMEM;
    783       1.52       mrg 
    784        1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
    785        1.1  tshiozak 	sc->sc_in_ep =
    786        1.1  tshiozak 	    sc->sc_in_num_endpoints ?
    787        1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
    788        1.3  tshiozak 
    789        1.3  tshiozak 	ep = &sc->sc_out_ep[0];
    790       1.52       mrg 	for (i = 0; i < sc->sc_out_num_endpoints; i++) {
    791        1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    792        1.1  tshiozak 			sc->sc_iface,
    793        1.1  tshiozak 			fp->out_ep[i].ep);
    794        1.1  tshiozak 		if (!epd) {
    795       1.37      cube 			aprint_error_dev(sc->sc_dev,
    796       1.37      cube 			    "cannot get endpoint descriptor(out:%d)\n",
    797       1.37      cube 			     fp->out_ep[i].ep);
    798        1.3  tshiozak 			err = USBD_INVAL;
    799        1.1  tshiozak 			goto error;
    800        1.1  tshiozak 		}
    801        1.1  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes)!=UE_BULK ||
    802        1.1  tshiozak 		    UE_GET_DIR(epd->bEndpointAddress)!=UE_DIR_OUT) {
    803       1.37      cube 			aprint_error_dev(sc->sc_dev, "illegal endpoint(out:%d)\n",
    804       1.37      cube 			    fp->out_ep[i].ep);
    805        1.3  tshiozak 			err = USBD_INVAL;
    806        1.1  tshiozak 			goto error;
    807        1.1  tshiozak 		}
    808        1.3  tshiozak 		ep->sc = sc;
    809        1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    810        1.3  tshiozak 		ep->num_jacks = fp->out_ep[i].num_jacks;
    811        1.1  tshiozak 		sc->sc_out_num_jacks += fp->out_ep[i].num_jacks;
    812        1.3  tshiozak 		ep->num_open = 0;
    813        1.3  tshiozak 		ep++;
    814        1.1  tshiozak 	}
    815        1.3  tshiozak 	ep = &sc->sc_in_ep[0];
    816       1.52       mrg 	for (i = 0; i < sc->sc_in_num_endpoints; i++) {
    817        1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    818        1.1  tshiozak 			sc->sc_iface,
    819        1.1  tshiozak 			fp->in_ep[i].ep);
    820        1.1  tshiozak 		if (!epd) {
    821       1.37      cube 			aprint_error_dev(sc->sc_dev,
    822       1.37      cube 			    "cannot get endpoint descriptor(in:%d)\n",
    823       1.37      cube 			     fp->in_ep[i].ep);
    824        1.3  tshiozak 			err = USBD_INVAL;
    825        1.1  tshiozak 			goto error;
    826        1.1  tshiozak 		}
    827       1.26      chap 		/*
    828       1.26      chap 		 * MIDISPORT_2X4 inputs on an interrupt rather than a bulk
    829       1.26      chap 		 * endpoint.  The existing input logic in this driver seems
    830       1.26      chap 		 * to work successfully if we just stop treating an interrupt
    831       1.26      chap 		 * endpoint as illegal (or the in_progress status we get on
    832       1.26      chap 		 * the initial transfer).  It does not seem necessary to
    833       1.26      chap 		 * actually use the interrupt flavor of alloc_pipe or make
    834       1.26      chap 		 * other serious rearrangements of logic.  I like that.
    835       1.26      chap 		 */
    836       1.26      chap 		switch ( UE_GET_XFERTYPE(epd->bmAttributes) ) {
    837       1.26      chap 		case UE_BULK:
    838       1.26      chap 		case UE_INTERRUPT:
    839  1.65.14.6     skrll 			if (UE_DIR_IN == UE_GET_DIR(epd->bEndpointAddress))
    840       1.26      chap 				break;
    841       1.26      chap 			/*FALLTHROUGH*/
    842       1.26      chap 		default:
    843       1.37      cube 			aprint_error_dev(sc->sc_dev,
    844       1.37      cube 			    "illegal endpoint(in:%d)\n", fp->in_ep[i].ep);
    845        1.3  tshiozak 			err = USBD_INVAL;
    846        1.1  tshiozak 			goto error;
    847        1.1  tshiozak 		}
    848       1.26      chap 
    849        1.3  tshiozak 		ep->sc = sc;
    850        1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    851        1.3  tshiozak 		ep->num_jacks = fp->in_ep[i].num_jacks;
    852        1.1  tshiozak 		sc->sc_in_num_jacks += fp->in_ep[i].num_jacks;
    853        1.3  tshiozak 		ep->num_open = 0;
    854        1.3  tshiozak 		ep++;
    855        1.1  tshiozak 	}
    856        1.1  tshiozak 
    857        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    858        1.1  tshiozak error:
    859       1.52       mrg 	kmem_free(sc->sc_endpoints, UMIDI_ENDPOINT_SIZE(sc));
    860        1.1  tshiozak 	sc->sc_endpoints = NULL;
    861        1.3  tshiozak 	return err;
    862        1.1  tshiozak }
    863        1.1  tshiozak 
    864        1.1  tshiozak static usbd_status
    865        1.1  tshiozak alloc_all_endpoints_yamaha(struct umidi_softc *sc)
    866        1.1  tshiozak {
    867        1.1  tshiozak 	/* This driver currently supports max 1in/1out bulk endpoints */
    868        1.1  tshiozak 	usb_descriptor_t *desc;
    869       1.29  drochner 	umidi_cs_descriptor_t *udesc;
    870        1.8  tshiozak 	usb_endpoint_descriptor_t *epd;
    871        1.8  tshiozak 	int out_addr, in_addr, i;
    872        1.8  tshiozak 	int dir;
    873        1.1  tshiozak 	size_t remain, descsize;
    874        1.1  tshiozak 
    875        1.8  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    876        1.8  tshiozak 	out_addr = in_addr = 0;
    877        1.8  tshiozak 
    878        1.8  tshiozak 	/* detect endpoints */
    879        1.1  tshiozak 	desc = TO_D(usbd_get_interface_descriptor(sc->sc_iface));
    880        1.8  tshiozak 	for (i=(int)TO_IFD(desc)->bNumEndpoints-1; i>=0; i--) {
    881        1.8  tshiozak 		epd = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    882       1.25  christos 		KASSERT(epd != NULL);
    883        1.8  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes) == UE_BULK) {
    884        1.8  tshiozak 			dir = UE_GET_DIR(epd->bEndpointAddress);
    885        1.8  tshiozak 			if (dir==UE_DIR_OUT && !out_addr)
    886        1.8  tshiozak 				out_addr = epd->bEndpointAddress;
    887        1.8  tshiozak 			else if (dir==UE_DIR_IN && !in_addr)
    888        1.8  tshiozak 				in_addr = epd->bEndpointAddress;
    889        1.8  tshiozak 		}
    890        1.8  tshiozak 	}
    891       1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(desc);
    892        1.1  tshiozak 
    893        1.8  tshiozak 	/* count jacks */
    894       1.29  drochner 	if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
    895       1.29  drochner 	      udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
    896        1.8  tshiozak 		return USBD_INVAL;
    897       1.29  drochner 	remain = (size_t)UGETW(TO_CSIFD(udesc)->wTotalLength) -
    898       1.29  drochner 		(size_t)udesc->bLength;
    899       1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    900        1.1  tshiozak 
    901       1.52       mrg 	while (remain >= sizeof(usb_descriptor_t)) {
    902       1.29  drochner 		descsize = udesc->bLength;
    903        1.1  tshiozak 		if (descsize>remain || descsize==0)
    904        1.1  tshiozak 			break;
    905       1.52       mrg 		if (udesc->bDescriptorType == UDESC_CS_INTERFACE &&
    906       1.52       mrg 		    remain >= UMIDI_JACK_DESCRIPTOR_SIZE) {
    907       1.52       mrg 			if (udesc->bDescriptorSubtype == UMIDI_OUT_JACK)
    908        1.1  tshiozak 				sc->sc_out_num_jacks++;
    909       1.52       mrg 			else if (udesc->bDescriptorSubtype == UMIDI_IN_JACK)
    910        1.1  tshiozak 				sc->sc_in_num_jacks++;
    911        1.1  tshiozak 		}
    912       1.29  drochner 		udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    913       1.52       mrg 		remain -= descsize;
    914        1.1  tshiozak 	}
    915        1.1  tshiozak 
    916        1.8  tshiozak 	/* validate some parameters */
    917        1.1  tshiozak 	if (sc->sc_out_num_jacks>UMIDI_MAX_EPJACKS)
    918        1.1  tshiozak 		sc->sc_out_num_jacks = UMIDI_MAX_EPJACKS;
    919        1.1  tshiozak 	if (sc->sc_in_num_jacks>UMIDI_MAX_EPJACKS)
    920        1.1  tshiozak 		sc->sc_in_num_jacks = UMIDI_MAX_EPJACKS;
    921        1.8  tshiozak 	if (sc->sc_out_num_jacks && out_addr) {
    922        1.1  tshiozak 		sc->sc_out_num_endpoints = 1;
    923        1.1  tshiozak 	} else {
    924        1.1  tshiozak 		sc->sc_out_num_endpoints = 0;
    925        1.1  tshiozak 		sc->sc_out_num_jacks = 0;
    926        1.1  tshiozak 	}
    927        1.8  tshiozak 	if (sc->sc_in_num_jacks && in_addr) {
    928        1.1  tshiozak 		sc->sc_in_num_endpoints = 1;
    929        1.1  tshiozak 	} else {
    930        1.1  tshiozak 		sc->sc_in_num_endpoints = 0;
    931        1.1  tshiozak 		sc->sc_in_num_jacks = 0;
    932        1.1  tshiozak 	}
    933       1.52       mrg 	sc->sc_endpoints_len = UMIDI_ENDPOINT_SIZE(sc);
    934       1.52       mrg 	sc->sc_endpoints = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    935        1.1  tshiozak 	if (!sc->sc_endpoints)
    936        1.1  tshiozak 		return USBD_NOMEM;
    937        1.1  tshiozak 	if (sc->sc_out_num_endpoints) {
    938        1.1  tshiozak 		sc->sc_out_ep = sc->sc_endpoints;
    939        1.1  tshiozak 		sc->sc_out_ep->sc = sc;
    940        1.1  tshiozak 		sc->sc_out_ep->addr = out_addr;
    941        1.1  tshiozak 		sc->sc_out_ep->num_jacks = sc->sc_out_num_jacks;
    942        1.1  tshiozak 		sc->sc_out_ep->num_open = 0;
    943        1.1  tshiozak 	} else
    944        1.1  tshiozak 		sc->sc_out_ep = NULL;
    945        1.1  tshiozak 
    946        1.1  tshiozak 	if (sc->sc_in_num_endpoints) {
    947        1.1  tshiozak 		sc->sc_in_ep = sc->sc_endpoints+sc->sc_out_num_endpoints;
    948        1.1  tshiozak 		sc->sc_in_ep->sc = sc;
    949        1.1  tshiozak 		sc->sc_in_ep->addr = in_addr;
    950        1.1  tshiozak 		sc->sc_in_ep->num_jacks = sc->sc_in_num_jacks;
    951        1.1  tshiozak 		sc->sc_in_ep->num_open = 0;
    952        1.1  tshiozak 	} else
    953        1.1  tshiozak 		sc->sc_in_ep = NULL;
    954        1.1  tshiozak 
    955        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    956        1.1  tshiozak }
    957        1.1  tshiozak 
    958        1.1  tshiozak static usbd_status
    959        1.1  tshiozak alloc_all_endpoints_genuine(struct umidi_softc *sc)
    960        1.1  tshiozak {
    961       1.20      gson 	usb_interface_descriptor_t *interface_desc;
    962       1.20      gson 	usb_config_descriptor_t *config_desc;
    963        1.1  tshiozak 	usb_descriptor_t *desc;
    964        1.7  tshiozak 	int num_ep;
    965        1.1  tshiozak 	size_t remain, descsize;
    966        1.1  tshiozak 	struct umidi_endpoint *p, *q, *lowest, *endep, tmpep;
    967        1.1  tshiozak 	int epaddr;
    968        1.1  tshiozak 
    969       1.20      gson 	interface_desc = usbd_get_interface_descriptor(sc->sc_iface);
    970       1.20      gson 	num_ep = interface_desc->bNumEndpoints;
    971       1.52       mrg 	sc->sc_endpoints_len = sizeof(struct umidi_endpoint) * num_ep;
    972       1.52       mrg 	sc->sc_endpoints = p = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    973        1.1  tshiozak 	if (!p)
    974        1.1  tshiozak 		return USBD_NOMEM;
    975        1.1  tshiozak 
    976        1.1  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    977        1.1  tshiozak 	sc->sc_out_num_endpoints = sc->sc_in_num_endpoints = 0;
    978        1.1  tshiozak 	epaddr = -1;
    979        1.1  tshiozak 
    980        1.1  tshiozak 	/* get the list of endpoints for midi stream */
    981       1.20      gson 	config_desc = usbd_get_config_descriptor(sc->sc_udev);
    982       1.20      gson 	desc = (usb_descriptor_t *) config_desc;
    983       1.20      gson 	remain = (size_t)UGETW(config_desc->wTotalLength);
    984        1.1  tshiozak 	while (remain>=sizeof(usb_descriptor_t)) {
    985        1.1  tshiozak 		descsize = desc->bLength;
    986        1.1  tshiozak 		if (descsize>remain || descsize==0)
    987        1.1  tshiozak 			break;
    988        1.1  tshiozak 		if (desc->bDescriptorType==UDESC_ENDPOINT &&
    989        1.1  tshiozak 		    remain>=USB_ENDPOINT_DESCRIPTOR_SIZE &&
    990        1.1  tshiozak 		    UE_GET_XFERTYPE(TO_EPD(desc)->bmAttributes) == UE_BULK) {
    991        1.1  tshiozak 			epaddr = TO_EPD(desc)->bEndpointAddress;
    992        1.1  tshiozak 		} else if (desc->bDescriptorType==UDESC_CS_ENDPOINT &&
    993        1.1  tshiozak 			   remain>=UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE &&
    994        1.1  tshiozak 			   epaddr!=-1) {
    995        1.1  tshiozak 			if (num_ep>0) {
    996        1.9  tshiozak 				num_ep--;
    997        1.1  tshiozak 				p->sc = sc;
    998        1.1  tshiozak 				p->addr = epaddr;
    999        1.1  tshiozak 				p->num_jacks = TO_CSEPD(desc)->bNumEmbMIDIJack;
   1000        1.1  tshiozak 				if (UE_GET_DIR(epaddr)==UE_DIR_OUT) {
   1001        1.1  tshiozak 					sc->sc_out_num_endpoints++;
   1002        1.1  tshiozak 					sc->sc_out_num_jacks += p->num_jacks;
   1003        1.1  tshiozak 				} else {
   1004        1.1  tshiozak 					sc->sc_in_num_endpoints++;
   1005        1.1  tshiozak 					sc->sc_in_num_jacks += p->num_jacks;
   1006        1.1  tshiozak 				}
   1007        1.1  tshiozak 				p++;
   1008        1.1  tshiozak 			}
   1009        1.1  tshiozak 		} else
   1010        1.1  tshiozak 			epaddr = -1;
   1011        1.1  tshiozak 		desc = NEXT_D(desc);
   1012        1.1  tshiozak 		remain-=descsize;
   1013        1.1  tshiozak 	}
   1014        1.1  tshiozak 
   1015        1.1  tshiozak 	/* sort endpoints */
   1016        1.1  tshiozak 	num_ep = sc->sc_out_num_endpoints + sc->sc_in_num_endpoints;
   1017        1.1  tshiozak 	p = sc->sc_endpoints;
   1018        1.1  tshiozak 	endep = p + num_ep;
   1019        1.1  tshiozak 	while (p<endep) {
   1020        1.1  tshiozak 		lowest = p;
   1021        1.1  tshiozak 		for (q=p+1; q<endep; q++) {
   1022        1.1  tshiozak 			if ((UE_GET_DIR(lowest->addr)==UE_DIR_IN &&
   1023        1.1  tshiozak 			     UE_GET_DIR(q->addr)==UE_DIR_OUT) ||
   1024        1.1  tshiozak 			    ((UE_GET_DIR(lowest->addr)==
   1025        1.1  tshiozak 			      UE_GET_DIR(q->addr)) &&
   1026        1.1  tshiozak 			     (UE_GET_ADDR(lowest->addr)>
   1027        1.1  tshiozak 			      UE_GET_ADDR(q->addr))))
   1028        1.1  tshiozak 				lowest = q;
   1029        1.1  tshiozak 		}
   1030        1.1  tshiozak 		if (lowest != p) {
   1031        1.1  tshiozak 			memcpy((void *)&tmpep, (void *)p, sizeof(tmpep));
   1032        1.1  tshiozak 			memcpy((void *)p, (void *)lowest, sizeof(tmpep));
   1033        1.1  tshiozak 			memcpy((void *)lowest, (void *)&tmpep, sizeof(tmpep));
   1034        1.1  tshiozak 		}
   1035        1.7  tshiozak 		p->num_open = 0;
   1036        1.7  tshiozak 		p++;
   1037        1.1  tshiozak 	}
   1038       1.16  augustss 
   1039        1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
   1040        1.1  tshiozak 	sc->sc_in_ep =
   1041        1.1  tshiozak 	    sc->sc_in_num_endpoints ?
   1042        1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
   1043        1.1  tshiozak 
   1044        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1045        1.1  tshiozak }
   1046        1.1  tshiozak 
   1047        1.1  tshiozak 
   1048        1.4  tshiozak /*
   1049        1.4  tshiozak  * jack stuffs
   1050        1.4  tshiozak  */
   1051        1.4  tshiozak 
   1052        1.1  tshiozak static usbd_status
   1053        1.1  tshiozak alloc_all_jacks(struct umidi_softc *sc)
   1054        1.1  tshiozak {
   1055        1.1  tshiozak 	int i, j;
   1056        1.1  tshiozak 	struct umidi_endpoint *ep;
   1057       1.26      chap 	struct umidi_jack *jack;
   1058       1.38  gmcgarry 	const unsigned char *cn_spec;
   1059  1.65.14.1     skrll 
   1060       1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_PER_EP))
   1061       1.26      chap 		sc->cblnums_global = 0;
   1062       1.26      chap 	else if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_GLOBAL))
   1063       1.26      chap 		sc->cblnums_global = 1;
   1064       1.26      chap 	else {
   1065       1.26      chap 		/*
   1066       1.26      chap 		 * I don't think this default is correct, but it preserves
   1067       1.26      chap 		 * the prior behavior of the code. That's why I defined two
   1068       1.26      chap 		 * complementary quirks. Any device for which the default
   1069       1.26      chap 		 * behavior is wrong can be made to work by giving it an
   1070       1.26      chap 		 * explicit quirk, and if a pattern ever develops (as I suspect
   1071       1.26      chap 		 * it will) that a lot of otherwise standard USB MIDI devices
   1072       1.26      chap 		 * need the CN_SEQ_PER_EP "quirk," then this default can be
   1073       1.26      chap 		 * changed to 0, and the only devices that will break are those
   1074       1.26      chap 		 * listing neither quirk, and they'll easily be fixed by giving
   1075       1.26      chap 		 * them the CN_SEQ_GLOBAL quirk.
   1076       1.26      chap 		 */
   1077       1.26      chap 		sc->cblnums_global = 1;
   1078       1.26      chap 	}
   1079  1.65.14.1     skrll 
   1080       1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_FIXED))
   1081       1.26      chap 		cn_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
   1082       1.26      chap 					    		 UMQ_TYPE_CN_FIXED);
   1083       1.26      chap 	else
   1084       1.26      chap 		cn_spec = NULL;
   1085        1.1  tshiozak 
   1086        1.1  tshiozak 	/* allocate/initialize structures */
   1087       1.52       mrg 	sc->sc_jacks = kmem_zalloc(sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks+
   1088       1.52       mrg 						      sc->sc_out_num_jacks), KM_SLEEP);
   1089        1.1  tshiozak 	if (!sc->sc_jacks)
   1090        1.1  tshiozak 		return USBD_NOMEM;
   1091        1.1  tshiozak 	sc->sc_out_jacks =
   1092        1.1  tshiozak 	    sc->sc_out_num_jacks ? sc->sc_jacks : NULL;
   1093        1.1  tshiozak 	sc->sc_in_jacks =
   1094        1.1  tshiozak 	    sc->sc_in_num_jacks ? sc->sc_jacks+sc->sc_out_num_jacks : NULL;
   1095        1.1  tshiozak 
   1096        1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
   1097       1.47       mrg 	for (i = 0; i < sc->sc_out_num_jacks; i++) {
   1098        1.4  tshiozak 		jack->opened = 0;
   1099  1.65.14.6     skrll 		jack->bound = 0;
   1100        1.4  tshiozak 		jack->arg = NULL;
   1101        1.4  tshiozak 		jack->u.out.intr = NULL;
   1102       1.26      chap 		jack->midiman_ppkt = NULL;
   1103       1.47       mrg 		if (sc->cblnums_global)
   1104       1.26      chap 			jack->cable_number = i;
   1105        1.1  tshiozak 		jack++;
   1106        1.1  tshiozak 	}
   1107        1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
   1108       1.47       mrg 	for (i = 0; i < sc->sc_in_num_jacks; i++) {
   1109        1.4  tshiozak 		jack->opened = 0;
   1110  1.65.14.6     skrll 		jack->bound = 0;
   1111        1.4  tshiozak 		jack->arg = NULL;
   1112        1.4  tshiozak 		jack->u.in.intr = NULL;
   1113       1.47       mrg 		if (sc->cblnums_global)
   1114       1.26      chap 			jack->cable_number = i;
   1115        1.1  tshiozak 		jack++;
   1116        1.1  tshiozak 	}
   1117        1.1  tshiozak 
   1118        1.1  tshiozak 	/* assign each jacks to each endpoints */
   1119        1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
   1120        1.1  tshiozak 	ep = &sc->sc_out_ep[0];
   1121       1.47       mrg 	for (i = 0; i < sc->sc_out_num_endpoints; i++) {
   1122       1.47       mrg 		for (j = 0; j < ep->num_jacks; j++) {
   1123        1.1  tshiozak 			jack->endpoint = ep;
   1124       1.47       mrg 			if (cn_spec != NULL)
   1125       1.26      chap 				jack->cable_number = *cn_spec++;
   1126       1.47       mrg 			else if (!sc->cblnums_global)
   1127       1.26      chap 				jack->cable_number = j;
   1128       1.26      chap 			ep->jacks[jack->cable_number] = jack;
   1129        1.1  tshiozak 			jack++;
   1130        1.1  tshiozak 		}
   1131        1.1  tshiozak 		ep++;
   1132        1.1  tshiozak 	}
   1133        1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
   1134        1.1  tshiozak 	ep = &sc->sc_in_ep[0];
   1135       1.47       mrg 	for (i = 0; i < sc->sc_in_num_endpoints; i++) {
   1136       1.47       mrg 		for (j = 0; j < ep->num_jacks; j++) {
   1137        1.1  tshiozak 			jack->endpoint = ep;
   1138       1.47       mrg 			if (cn_spec != NULL)
   1139       1.26      chap 				jack->cable_number = *cn_spec++;
   1140       1.47       mrg 			else if (!sc->cblnums_global)
   1141       1.26      chap 				jack->cable_number = j;
   1142       1.26      chap 			ep->jacks[jack->cable_number] = jack;
   1143        1.1  tshiozak 			jack++;
   1144        1.1  tshiozak 		}
   1145        1.1  tshiozak 		ep++;
   1146        1.1  tshiozak 	}
   1147        1.1  tshiozak 
   1148        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1149        1.1  tshiozak }
   1150        1.1  tshiozak 
   1151        1.1  tshiozak static void
   1152        1.1  tshiozak free_all_jacks(struct umidi_softc *sc)
   1153        1.1  tshiozak {
   1154       1.47       mrg 	struct umidi_jack *jacks;
   1155       1.58       mrg 	size_t len;
   1156       1.47       mrg 
   1157       1.47       mrg 	mutex_enter(&sc->sc_lock);
   1158       1.58       mrg 	jacks = sc->sc_jacks;
   1159       1.58       mrg 	len = sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks+sc->sc_out_num_jacks);
   1160       1.47       mrg 	sc->sc_jacks = sc->sc_in_jacks = sc->sc_out_jacks = NULL;
   1161       1.47       mrg 	mutex_exit(&sc->sc_lock);
   1162        1.1  tshiozak 
   1163       1.47       mrg 	if (jacks)
   1164       1.58       mrg 		kmem_free(jacks, len);
   1165        1.1  tshiozak }
   1166        1.1  tshiozak 
   1167        1.1  tshiozak static usbd_status
   1168       1.28  christos bind_jacks_to_mididev(struct umidi_softc *sc,
   1169        1.1  tshiozak 		      struct umidi_jack *out_jack,
   1170        1.1  tshiozak 		      struct umidi_jack *in_jack,
   1171        1.1  tshiozak 		      struct umidi_mididev *mididev)
   1172        1.1  tshiozak {
   1173  1.65.14.6     skrll 	if ((out_jack && out_jack->bound) || (in_jack && in_jack->bound))
   1174        1.4  tshiozak 		return USBD_IN_USE;
   1175        1.4  tshiozak 	if (mididev->out_jack || mididev->in_jack)
   1176        1.1  tshiozak 		return USBD_IN_USE;
   1177        1.1  tshiozak 
   1178        1.4  tshiozak 	if (out_jack)
   1179  1.65.14.6     skrll 		out_jack->bound = 1;
   1180        1.4  tshiozak 	if (in_jack)
   1181  1.65.14.6     skrll 		in_jack->bound = 1;
   1182        1.1  tshiozak 	mididev->in_jack = in_jack;
   1183        1.1  tshiozak 	mididev->out_jack = out_jack;
   1184        1.1  tshiozak 
   1185  1.65.14.6     skrll 	mididev->closing = 0;
   1186  1.65.14.6     skrll 
   1187        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1188        1.1  tshiozak }
   1189        1.1  tshiozak 
   1190        1.4  tshiozak static void
   1191        1.1  tshiozak unbind_jacks_from_mididev(struct umidi_mididev *mididev)
   1192        1.1  tshiozak {
   1193  1.65.14.6     skrll 	KASSERT(mutex_owned(&mididev->sc->sc_lock));
   1194  1.65.14.6     skrll 
   1195  1.65.14.6     skrll 	mididev->closing = 1;
   1196       1.57       mrg 
   1197       1.15  tshiozak 	if ((mididev->flags & FWRITE) && mididev->out_jack)
   1198        1.4  tshiozak 		close_out_jack(mididev->out_jack);
   1199       1.15  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack)
   1200        1.4  tshiozak 		close_in_jack(mididev->in_jack);
   1201        1.1  tshiozak 
   1202  1.65.14.6     skrll 	if (mididev->out_jack) {
   1203  1.65.14.6     skrll 		mididev->out_jack->bound = 0;
   1204  1.65.14.6     skrll 		mididev->out_jack = NULL;
   1205  1.65.14.6     skrll 	}
   1206  1.65.14.6     skrll 	if (mididev->in_jack) {
   1207  1.65.14.6     skrll 		mididev->in_jack->bound = 0;
   1208  1.65.14.6     skrll 		mididev->in_jack = NULL;
   1209  1.65.14.6     skrll 	}
   1210        1.1  tshiozak }
   1211        1.1  tshiozak 
   1212        1.4  tshiozak static void
   1213        1.1  tshiozak unbind_all_jacks(struct umidi_softc *sc)
   1214        1.1  tshiozak {
   1215        1.1  tshiozak 	int i;
   1216        1.1  tshiozak 
   1217  1.65.14.6     skrll 	mutex_spin_enter(&sc->sc_lock);
   1218        1.1  tshiozak 	if (sc->sc_mididevs)
   1219       1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++)
   1220        1.4  tshiozak 			unbind_jacks_from_mididev(&sc->sc_mididevs[i]);
   1221  1.65.14.6     skrll 	mutex_spin_exit(&sc->sc_lock);
   1222        1.1  tshiozak }
   1223        1.1  tshiozak 
   1224        1.1  tshiozak static usbd_status
   1225        1.1  tshiozak assign_all_jacks_automatically(struct umidi_softc *sc)
   1226        1.1  tshiozak {
   1227        1.1  tshiozak 	usbd_status err;
   1228        1.1  tshiozak 	int i;
   1229        1.1  tshiozak 	struct umidi_jack *out, *in;
   1230       1.38  gmcgarry 	const signed char *asg_spec;
   1231        1.1  tshiozak 
   1232        1.1  tshiozak 	err =
   1233        1.1  tshiozak 	    alloc_all_mididevs(sc,
   1234        1.1  tshiozak 			       max(sc->sc_out_num_jacks, sc->sc_in_num_jacks));
   1235        1.1  tshiozak 	if (err!=USBD_NORMAL_COMPLETION)
   1236        1.1  tshiozak 		return err;
   1237        1.1  tshiozak 
   1238  1.65.14.6     skrll 	if (UMQ_ISTYPE(sc, UMQ_TYPE_MD_FIXED))
   1239       1.26      chap 		asg_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
   1240       1.26      chap 					    		  UMQ_TYPE_MD_FIXED);
   1241       1.26      chap 	else
   1242       1.26      chap 		asg_spec = NULL;
   1243       1.26      chap 
   1244       1.47       mrg 	for (i = 0; i < sc->sc_num_mididevs; i++) {
   1245       1.47       mrg 		if (asg_spec != NULL) {
   1246       1.47       mrg 			if (*asg_spec == -1)
   1247       1.26      chap 				out = NULL;
   1248       1.26      chap 			else
   1249       1.26      chap 				out = &sc->sc_out_jacks[*asg_spec];
   1250       1.26      chap 			++ asg_spec;
   1251       1.47       mrg 			if (*asg_spec == -1)
   1252       1.26      chap 				in = NULL;
   1253       1.26      chap 			else
   1254       1.26      chap 				in = &sc->sc_in_jacks[*asg_spec];
   1255       1.26      chap 			++ asg_spec;
   1256       1.26      chap 		} else {
   1257       1.26      chap 			out = (i<sc->sc_out_num_jacks) ? &sc->sc_out_jacks[i]
   1258  1.65.14.2     skrll 						       : NULL;
   1259       1.26      chap 			in = (i<sc->sc_in_num_jacks) ? &sc->sc_in_jacks[i]
   1260       1.26      chap 						     : NULL;
   1261       1.26      chap 		}
   1262        1.1  tshiozak 		err = bind_jacks_to_mididev(sc, out, in, &sc->sc_mididevs[i]);
   1263  1.65.14.6     skrll 		if (err != USBD_NORMAL_COMPLETION) {
   1264        1.1  tshiozak 			free_all_mididevs(sc);
   1265        1.1  tshiozak 			return err;
   1266        1.1  tshiozak 		}
   1267        1.1  tshiozak 	}
   1268        1.1  tshiozak 
   1269        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1270        1.1  tshiozak }
   1271        1.1  tshiozak 
   1272        1.1  tshiozak static usbd_status
   1273        1.4  tshiozak open_out_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *))
   1274        1.4  tshiozak {
   1275        1.4  tshiozak 	struct umidi_endpoint *ep = jack->endpoint;
   1276       1.46       mrg 	struct umidi_softc *sc = ep->sc;
   1277       1.26      chap 	umidi_packet_bufp end;
   1278       1.26      chap 	int err;
   1279        1.4  tshiozak 
   1280       1.47       mrg 	KASSERT(mutex_owned(&sc->sc_lock));
   1281       1.47       mrg 
   1282        1.4  tshiozak 	if (jack->opened)
   1283        1.4  tshiozak 		return USBD_IN_USE;
   1284        1.4  tshiozak 
   1285        1.4  tshiozak 	jack->arg = arg;
   1286        1.4  tshiozak 	jack->u.out.intr = intr;
   1287       1.26      chap 	jack->midiman_ppkt = NULL;
   1288  1.65.14.7     skrll 	end = ep->buffer + ep->buffer_size / sizeof(*ep->buffer);
   1289        1.4  tshiozak 	jack->opened = 1;
   1290        1.4  tshiozak 	ep->num_open++;
   1291       1.26      chap 	/*
   1292       1.26      chap 	 * out_solicit maintains an invariant that there will always be
   1293       1.26      chap 	 * (num_open - num_scheduled) slots free in the buffer. as we have
   1294       1.26      chap 	 * just incremented num_open, the buffer may be too full to satisfy
   1295       1.26      chap 	 * the invariant until a transfer completes, for which we must wait.
   1296       1.26      chap 	 */
   1297       1.47       mrg 	while (end - ep->next_slot < ep->num_open - ep->num_scheduled) {
   1298       1.47       mrg 		err = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
   1299       1.47       mrg 		     mstohz(10));
   1300       1.47       mrg 		if (err) {
   1301       1.26      chap 			ep->num_open--;
   1302       1.26      chap 			jack->opened = 0;
   1303       1.26      chap 			return USBD_IOERROR;
   1304       1.26      chap 		}
   1305       1.26      chap 	}
   1306        1.4  tshiozak 
   1307        1.4  tshiozak 	return USBD_NORMAL_COMPLETION;
   1308        1.4  tshiozak }
   1309        1.4  tshiozak 
   1310        1.4  tshiozak static usbd_status
   1311        1.4  tshiozak open_in_jack(struct umidi_jack *jack, void *arg, void (*intr)(void *, int))
   1312        1.1  tshiozak {
   1313        1.3  tshiozak 	usbd_status err = USBD_NORMAL_COMPLETION;
   1314        1.3  tshiozak 	struct umidi_endpoint *ep = jack->endpoint;
   1315       1.47       mrg 
   1316       1.49     skrll 	KASSERT(mutex_owned(&ep->sc->sc_lock));
   1317        1.3  tshiozak 
   1318        1.4  tshiozak 	if (jack->opened)
   1319        1.4  tshiozak 		return USBD_IN_USE;
   1320        1.4  tshiozak 
   1321        1.4  tshiozak 	jack->arg = arg;
   1322        1.4  tshiozak 	jack->u.in.intr = intr;
   1323        1.4  tshiozak 	jack->opened = 1;
   1324       1.47       mrg 	if (ep->num_open++ == 0 && UE_GET_DIR(ep->addr)==UE_DIR_IN) {
   1325  1.65.14.6     skrll 		/*
   1326  1.65.14.6     skrll 		 * Can't hold the interrupt lock while calling into USB,
   1327  1.65.14.6     skrll 		 * but we can safely drop it here.
   1328  1.65.14.6     skrll 		 */
   1329  1.65.14.6     skrll 		mutex_exit(&ep->sc->sc_lock);
   1330        1.3  tshiozak 		err = start_input_transfer(ep);
   1331       1.18      gson 		if (err != USBD_NORMAL_COMPLETION &&
   1332       1.18      gson 		    err != USBD_IN_PROGRESS) {
   1333        1.3  tshiozak 			ep->num_open--;
   1334        1.3  tshiozak 		}
   1335  1.65.14.6     skrll 		mutex_enter(&ep->sc->sc_lock);
   1336        1.3  tshiozak 	}
   1337        1.3  tshiozak 
   1338        1.3  tshiozak 	return err;
   1339        1.1  tshiozak }
   1340        1.1  tshiozak 
   1341        1.1  tshiozak static void
   1342        1.4  tshiozak close_out_jack(struct umidi_jack *jack)
   1343        1.1  tshiozak {
   1344       1.26      chap 	struct umidi_endpoint *ep;
   1345       1.46       mrg 	struct umidi_softc *sc;
   1346  1.65.14.1     skrll 	uint16_t mask;
   1347       1.26      chap 	int err;
   1348        1.4  tshiozak 
   1349        1.4  tshiozak 	if (jack->opened) {
   1350       1.26      chap 		ep = jack->endpoint;
   1351       1.46       mrg 		sc = ep->sc;
   1352  1.65.14.6     skrll 
   1353  1.65.14.6     skrll 		KASSERT(mutex_owned(&sc->sc_lock));
   1354       1.26      chap 		mask = 1 << (jack->cable_number);
   1355       1.47       mrg 		while (mask & (ep->this_schedule | ep->next_schedule)) {
   1356       1.47       mrg 			err = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
   1357       1.47       mrg 			     mstohz(10));
   1358       1.47       mrg 			if (err)
   1359       1.15  tshiozak 				break;
   1360        1.3  tshiozak 		}
   1361       1.59       mrg 		/*
   1362       1.59       mrg 		 * We can re-enter this function from both close() and
   1363       1.59       mrg 		 * detach().  Make sure only one of them does this part.
   1364       1.59       mrg 		 */
   1365       1.59       mrg 		if (jack->opened) {
   1366       1.59       mrg 			jack->opened = 0;
   1367       1.59       mrg 			jack->endpoint->num_open--;
   1368       1.59       mrg 			ep->this_schedule &= ~mask;
   1369       1.59       mrg 			ep->next_schedule &= ~mask;
   1370       1.59       mrg 		}
   1371        1.3  tshiozak 	}
   1372        1.1  tshiozak }
   1373        1.1  tshiozak 
   1374        1.4  tshiozak static void
   1375        1.4  tshiozak close_in_jack(struct umidi_jack *jack)
   1376        1.4  tshiozak {
   1377        1.4  tshiozak 	if (jack->opened) {
   1378  1.65.14.6     skrll 		struct umidi_softc *sc = jack->endpoint->sc;
   1379  1.65.14.6     skrll 
   1380  1.65.14.6     skrll 		KASSERT(mutex_owned(&sc->sc_lock));
   1381  1.65.14.6     skrll 
   1382        1.4  tshiozak 		jack->opened = 0;
   1383       1.19      gson 		if (--jack->endpoint->num_open == 0) {
   1384  1.65.14.6     skrll 			/*
   1385  1.65.14.6     skrll 			 * We have to drop the (interrupt) lock so that
   1386  1.65.14.6     skrll 			 * the USB thread lock can be safely taken by
   1387  1.65.14.6     skrll 			 * the abort operation.  This is safe as this
   1388  1.65.14.6     skrll 			 * either closing or dying will be set proerly.
   1389  1.65.14.6     skrll 			 */
   1390  1.65.14.6     skrll 			mutex_spin_exit(&sc->sc_lock);
   1391       1.59       mrg 			usbd_abort_pipe(jack->endpoint->pipe);
   1392  1.65.14.6     skrll 			mutex_spin_enter(&sc->sc_lock);
   1393       1.19      gson 		}
   1394        1.4  tshiozak 	}
   1395        1.4  tshiozak }
   1396        1.1  tshiozak 
   1397        1.1  tshiozak static usbd_status
   1398        1.1  tshiozak attach_mididev(struct umidi_softc *sc, struct umidi_mididev *mididev)
   1399        1.1  tshiozak {
   1400        1.1  tshiozak 	if (mididev->sc)
   1401        1.1  tshiozak 		return USBD_IN_USE;
   1402        1.1  tshiozak 
   1403        1.1  tshiozak 	mididev->sc = sc;
   1404  1.65.14.1     skrll 
   1405       1.52       mrg 	describe_mididev(mididev);
   1406        1.1  tshiozak 
   1407       1.37      cube 	mididev->mdev = midi_attach_mi(&umidi_hw_if, mididev, sc->sc_dev);
   1408        1.1  tshiozak 
   1409        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1410        1.1  tshiozak }
   1411        1.1  tshiozak 
   1412        1.1  tshiozak static usbd_status
   1413        1.1  tshiozak detach_mididev(struct umidi_mididev *mididev, int flags)
   1414        1.1  tshiozak {
   1415  1.65.14.6     skrll 	struct umidi_softc *sc = mididev->sc;
   1416  1.65.14.6     skrll 
   1417  1.65.14.6     skrll 	if (!sc)
   1418        1.1  tshiozak 		return USBD_NO_ADDR;
   1419        1.1  tshiozak 
   1420  1.65.14.6     skrll 	mutex_spin_enter(&sc->sc_lock);
   1421        1.1  tshiozak 	if (mididev->opened) {
   1422        1.1  tshiozak 		umidi_close(mididev);
   1423        1.1  tshiozak 	}
   1424        1.4  tshiozak 	unbind_jacks_from_mididev(mididev);
   1425  1.65.14.6     skrll 	mutex_spin_exit(&sc->sc_lock);
   1426        1.1  tshiozak 
   1427       1.34    dyoung 	if (mididev->mdev != NULL)
   1428        1.1  tshiozak 		config_detach(mididev->mdev, flags);
   1429  1.65.14.1     skrll 
   1430       1.26      chap 	if (NULL != mididev->label) {
   1431       1.52       mrg 		kmem_free(mididev->label, mididev->label_len);
   1432       1.26      chap 		mididev->label = NULL;
   1433       1.26      chap 	}
   1434        1.1  tshiozak 
   1435        1.1  tshiozak 	mididev->sc = NULL;
   1436        1.1  tshiozak 
   1437        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1438        1.1  tshiozak }
   1439        1.1  tshiozak 
   1440       1.40    dyoung static void
   1441        1.1  tshiozak deactivate_mididev(struct umidi_mididev *mididev)
   1442        1.1  tshiozak {
   1443        1.4  tshiozak 	if (mididev->out_jack)
   1444  1.65.14.6     skrll 		mididev->out_jack->bound = 0;
   1445        1.4  tshiozak 	if (mididev->in_jack)
   1446  1.65.14.6     skrll 		mididev->in_jack->bound = 0;
   1447        1.1  tshiozak }
   1448        1.1  tshiozak 
   1449        1.1  tshiozak static usbd_status
   1450        1.1  tshiozak alloc_all_mididevs(struct umidi_softc *sc, int nmidi)
   1451        1.1  tshiozak {
   1452        1.1  tshiozak 	sc->sc_num_mididevs = nmidi;
   1453       1.52       mrg 	sc->sc_mididevs = kmem_zalloc(sizeof(*sc->sc_mididevs)*nmidi, KM_SLEEP);
   1454        1.1  tshiozak 	if (!sc->sc_mididevs)
   1455        1.1  tshiozak 		return USBD_NOMEM;
   1456        1.1  tshiozak 
   1457        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1458        1.1  tshiozak }
   1459        1.1  tshiozak 
   1460        1.1  tshiozak static void
   1461        1.1  tshiozak free_all_mididevs(struct umidi_softc *sc)
   1462        1.1  tshiozak {
   1463  1.65.14.6     skrll 	struct umidi_mididev *mididevs;
   1464  1.65.14.6     skrll 	size_t len;
   1465  1.65.14.6     skrll 
   1466  1.65.14.6     skrll 	mutex_enter(&sc->sc_lock);
   1467  1.65.14.6     skrll 	mididevs = sc->sc_mididevs;
   1468  1.65.14.6     skrll 	if (mididevs)
   1469  1.65.14.6     skrll 		  len = sizeof(*sc->sc_mididevs )* sc->sc_num_mididevs;
   1470  1.65.14.6     skrll 	sc->sc_mididevs = NULL;
   1471        1.1  tshiozak 	sc->sc_num_mididevs = 0;
   1472  1.65.14.6     skrll 	mutex_exit(&sc->sc_lock);
   1473  1.65.14.6     skrll 
   1474  1.65.14.6     skrll 	if (mididevs)
   1475  1.65.14.6     skrll 		kmem_free(mididevs, len);
   1476        1.1  tshiozak }
   1477        1.1  tshiozak 
   1478        1.1  tshiozak static usbd_status
   1479        1.1  tshiozak attach_all_mididevs(struct umidi_softc *sc)
   1480        1.1  tshiozak {
   1481        1.1  tshiozak 	usbd_status err;
   1482        1.1  tshiozak 	int i;
   1483        1.1  tshiozak 
   1484        1.1  tshiozak 	if (sc->sc_mididevs)
   1485       1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++) {
   1486        1.1  tshiozak 			err = attach_mididev(sc, &sc->sc_mididevs[i]);
   1487       1.47       mrg 			if (err != USBD_NORMAL_COMPLETION)
   1488        1.1  tshiozak 				return err;
   1489        1.1  tshiozak 		}
   1490        1.1  tshiozak 
   1491        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1492        1.1  tshiozak }
   1493        1.1  tshiozak 
   1494        1.1  tshiozak static usbd_status
   1495        1.1  tshiozak detach_all_mididevs(struct umidi_softc *sc, int flags)
   1496        1.1  tshiozak {
   1497        1.1  tshiozak 	usbd_status err;
   1498        1.1  tshiozak 	int i;
   1499        1.1  tshiozak 
   1500        1.1  tshiozak 	if (sc->sc_mididevs)
   1501       1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++) {
   1502        1.1  tshiozak 			err = detach_mididev(&sc->sc_mididevs[i], flags);
   1503       1.47       mrg 			if (err != USBD_NORMAL_COMPLETION)
   1504        1.1  tshiozak 				return err;
   1505        1.1  tshiozak 		}
   1506        1.1  tshiozak 
   1507        1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1508        1.1  tshiozak }
   1509        1.1  tshiozak 
   1510       1.40    dyoung static void
   1511        1.1  tshiozak deactivate_all_mididevs(struct umidi_softc *sc)
   1512        1.1  tshiozak {
   1513        1.1  tshiozak 	int i;
   1514        1.1  tshiozak 
   1515       1.40    dyoung 	if (sc->sc_mididevs) {
   1516       1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++)
   1517       1.40    dyoung 			deactivate_mididev(&sc->sc_mididevs[i]);
   1518       1.40    dyoung 	}
   1519        1.1  tshiozak }
   1520        1.1  tshiozak 
   1521       1.26      chap /*
   1522       1.26      chap  * TODO: the 0-based cable numbers will often not match the labeling of the
   1523       1.26      chap  * equipment. Ideally:
   1524       1.26      chap  *  For class-compliant devices: get the iJack string from the jack descriptor.
   1525       1.26      chap  *  Otherwise:
   1526       1.26      chap  *  - support a DISPLAY_BASE_CN quirk (add the value to each internal cable
   1527       1.26      chap  *    number for display)
   1528       1.26      chap  *  - support an array quirk explictly giving a char * for each jack.
   1529       1.26      chap  * For now, you get 0-based cable numbers. If there are multiple endpoints and
   1530       1.26      chap  * the CNs are not globally unique, each is shown with its associated endpoint
   1531       1.26      chap  * address in hex also. That should not be necessary when using iJack values
   1532       1.26      chap  * or a quirk array.
   1533       1.26      chap  */
   1534       1.52       mrg void
   1535       1.26      chap describe_mididev(struct umidi_mididev *md)
   1536       1.26      chap {
   1537       1.26      chap 	char in_label[16];
   1538       1.26      chap 	char out_label[16];
   1539       1.35    cegger 	const char *unit_label;
   1540       1.26      chap 	char *final_label;
   1541       1.26      chap 	struct umidi_softc *sc;
   1542       1.26      chap 	int show_ep_in;
   1543       1.26      chap 	int show_ep_out;
   1544       1.26      chap 	size_t len;
   1545  1.65.14.1     skrll 
   1546       1.26      chap 	sc = md->sc;
   1547       1.26      chap 	show_ep_in  = sc-> sc_in_num_endpoints > 1 && !sc->cblnums_global;
   1548       1.26      chap 	show_ep_out = sc->sc_out_num_endpoints > 1 && !sc->cblnums_global;
   1549  1.65.14.1     skrll 
   1550  1.65.14.6     skrll 	if (NULL == md->in_jack)
   1551       1.43     joerg 		in_label[0] = '\0';
   1552  1.65.14.6     skrll 	else if (show_ep_in)
   1553  1.65.14.7     skrll 		snprintf(in_label, sizeof(in_label), "<%d(%x) ",
   1554       1.43     joerg 		    md->in_jack->cable_number, md->in_jack->endpoint->addr);
   1555       1.26      chap 	else
   1556  1.65.14.7     skrll 		snprintf(in_label, sizeof(in_label), "<%d ",
   1557       1.43     joerg 		    md->in_jack->cable_number);
   1558  1.65.14.1     skrll 
   1559  1.65.14.6     skrll 	if (NULL == md->out_jack)
   1560       1.43     joerg 		out_label[0] = '\0';
   1561  1.65.14.6     skrll 	else if (show_ep_out)
   1562  1.65.14.7     skrll 		snprintf(out_label, sizeof(out_label), ">%d(%x) ",
   1563       1.43     joerg 		    md->out_jack->cable_number, md->out_jack->endpoint->addr);
   1564       1.26      chap 	else
   1565  1.65.14.7     skrll 		snprintf(out_label, sizeof(out_label), ">%d ",
   1566       1.43     joerg 		    md->out_jack->cable_number);
   1567       1.26      chap 
   1568       1.42    dyoung 	unit_label = device_xname(sc->sc_dev);
   1569  1.65.14.1     skrll 
   1570       1.26      chap 	len = strlen(in_label) + strlen(out_label) + strlen(unit_label) + 4;
   1571  1.65.14.1     skrll 
   1572       1.52       mrg 	final_label = kmem_alloc(len, KM_SLEEP);
   1573  1.65.14.1     skrll 
   1574       1.26      chap 	snprintf(final_label, len, "%s%son %s",
   1575       1.26      chap 	    in_label, out_label, unit_label);
   1576       1.26      chap 
   1577       1.52       mrg 	md->label = final_label;
   1578       1.52       mrg 	md->label_len = len;
   1579       1.26      chap }
   1580       1.26      chap 
   1581        1.1  tshiozak #ifdef UMIDI_DEBUG
   1582        1.1  tshiozak static void
   1583        1.1  tshiozak dump_sc(struct umidi_softc *sc)
   1584        1.1  tshiozak {
   1585        1.1  tshiozak 	int i;
   1586        1.1  tshiozak 
   1587       1.42    dyoung 	DPRINTFN(10, ("%s: dump_sc\n", device_xname(sc->sc_dev)));
   1588        1.1  tshiozak 	for (i=0; i<sc->sc_out_num_endpoints; i++) {
   1589        1.1  tshiozak 		DPRINTFN(10, ("\tout_ep(%p):\n", &sc->sc_out_ep[i]));
   1590        1.1  tshiozak 		dump_ep(&sc->sc_out_ep[i]);
   1591        1.1  tshiozak 	}
   1592        1.1  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints; i++) {
   1593        1.1  tshiozak 		DPRINTFN(10, ("\tin_ep(%p):\n", &sc->sc_in_ep[i]));
   1594        1.1  tshiozak 		dump_ep(&sc->sc_in_ep[i]);
   1595        1.1  tshiozak 	}
   1596        1.1  tshiozak }
   1597        1.1  tshiozak 
   1598        1.1  tshiozak static void
   1599        1.1  tshiozak dump_ep(struct umidi_endpoint *ep)
   1600        1.1  tshiozak {
   1601        1.1  tshiozak 	int i;
   1602       1.26      chap 	for (i=0; i<UMIDI_MAX_EPJACKS; i++) {
   1603       1.26      chap 		if (NULL==ep->jacks[i])
   1604       1.26      chap 			continue;
   1605       1.26      chap 		DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i, ep->jacks[i]));
   1606        1.1  tshiozak 		dump_jack(ep->jacks[i]);
   1607        1.1  tshiozak 	}
   1608        1.1  tshiozak }
   1609        1.1  tshiozak static void
   1610        1.1  tshiozak dump_jack(struct umidi_jack *jack)
   1611        1.1  tshiozak {
   1612       1.15  tshiozak 	DPRINTFN(10, ("\t\t\tep=%p\n",
   1613       1.15  tshiozak 		      jack->endpoint));
   1614        1.1  tshiozak }
   1615        1.1  tshiozak 
   1616        1.1  tshiozak #endif /* UMIDI_DEBUG */
   1617        1.1  tshiozak 
   1618        1.1  tshiozak 
   1619        1.1  tshiozak 
   1620        1.1  tshiozak /*
   1621        1.1  tshiozak  * MUX MIDI PACKET
   1622        1.1  tshiozak  */
   1623        1.1  tshiozak 
   1624        1.5  jdolecek static const int packet_length[16] = {
   1625        1.1  tshiozak 	/*0*/	-1,
   1626        1.1  tshiozak 	/*1*/	-1,
   1627        1.1  tshiozak 	/*2*/	2,
   1628        1.1  tshiozak 	/*3*/	3,
   1629        1.1  tshiozak 	/*4*/	3,
   1630        1.1  tshiozak 	/*5*/	1,
   1631        1.1  tshiozak 	/*6*/	2,
   1632        1.1  tshiozak 	/*7*/	3,
   1633        1.1  tshiozak 	/*8*/	3,
   1634        1.1  tshiozak 	/*9*/	3,
   1635        1.1  tshiozak 	/*A*/	3,
   1636        1.1  tshiozak 	/*B*/	3,
   1637        1.1  tshiozak 	/*C*/	2,
   1638        1.1  tshiozak 	/*D*/	2,
   1639        1.1  tshiozak 	/*E*/	3,
   1640        1.1  tshiozak 	/*F*/	1,
   1641        1.1  tshiozak };
   1642        1.1  tshiozak 
   1643        1.1  tshiozak #define	GET_CN(p)		(((unsigned char)(p)>>4)&0x0F)
   1644        1.1  tshiozak #define GET_CIN(p)		((unsigned char)(p)&0x0F)
   1645        1.1  tshiozak #define MIX_CN_CIN(cn, cin) \
   1646        1.1  tshiozak 	((unsigned char)((((unsigned char)(cn)&0x0F)<<4)| \
   1647        1.3  tshiozak 			  ((unsigned char)(cin)&0x0F)))
   1648        1.1  tshiozak 
   1649        1.1  tshiozak static usbd_status
   1650        1.1  tshiozak start_input_transfer(struct umidi_endpoint *ep)
   1651        1.1  tshiozak {
   1652  1.65.14.8     skrll 	usbd_setup_xfer(ep->xfer, ep, ep->buffer, ep->buffer_size,
   1653  1.65.14.8     skrll 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, in_intr);
   1654        1.1  tshiozak 	return usbd_transfer(ep->xfer);
   1655        1.1  tshiozak }
   1656        1.1  tshiozak 
   1657        1.1  tshiozak static usbd_status
   1658        1.1  tshiozak start_output_transfer(struct umidi_endpoint *ep)
   1659        1.1  tshiozak {
   1660       1.26      chap 	usbd_status rv;
   1661  1.65.14.1     skrll 	uint32_t length;
   1662       1.26      chap 	int i;
   1663  1.65.14.1     skrll 
   1664  1.65.14.7     skrll 	length = (ep->next_slot - ep->buffer) * sizeof(*ep->buffer);
   1665       1.26      chap 	DPRINTFN(200,("umidi out transfer: start %p end %p length %u\n",
   1666       1.26      chap 	    ep->buffer, ep->next_slot, length));
   1667  1.65.14.8     skrll 
   1668  1.65.14.8     skrll 	usbd_setup_xfer(ep->xfer, ep, ep->buffer, length, 0,
   1669  1.65.14.8     skrll 	    USBD_NO_TIMEOUT, out_intr);
   1670       1.26      chap 	rv = usbd_transfer(ep->xfer);
   1671  1.65.14.1     skrll 
   1672       1.26      chap 	/*
   1673       1.26      chap 	 * Once the transfer is scheduled, no more adding to partial
   1674       1.26      chap 	 * packets within it.
   1675       1.26      chap 	 */
   1676       1.26      chap 	if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1677       1.26      chap 		for (i=0; i<UMIDI_MAX_EPJACKS; ++i)
   1678       1.26      chap 			if (NULL != ep->jacks[i])
   1679       1.26      chap 				ep->jacks[i]->midiman_ppkt = NULL;
   1680       1.26      chap 	}
   1681  1.65.14.1     skrll 
   1682       1.26      chap 	return rv;
   1683        1.1  tshiozak }
   1684        1.1  tshiozak 
   1685        1.4  tshiozak #ifdef UMIDI_DEBUG
   1686        1.4  tshiozak #define DPR_PACKET(dir, sc, p)						\
   1687       1.26      chap if ((unsigned char)(p)[1]!=0xFE)				\
   1688        1.4  tshiozak 	DPRINTFN(500,							\
   1689        1.4  tshiozak 		 ("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n",	\
   1690       1.42    dyoung 		  device_xname(sc->sc_dev),				\
   1691       1.26      chap 		  (unsigned char)(p)[0],			\
   1692       1.26      chap 		  (unsigned char)(p)[1],			\
   1693       1.26      chap 		  (unsigned char)(p)[2],			\
   1694       1.26      chap 		  (unsigned char)(p)[3]));
   1695        1.4  tshiozak #else
   1696        1.4  tshiozak #define DPR_PACKET(dir, sc, p)
   1697        1.4  tshiozak #endif
   1698        1.4  tshiozak 
   1699       1.26      chap /*
   1700       1.26      chap  * A 4-byte Midiman packet superficially resembles a 4-byte USB MIDI packet
   1701       1.26      chap  * with the cable number and length in the last byte instead of the first,
   1702       1.26      chap  * but there the resemblance ends. Where a USB MIDI packet is a semantic
   1703       1.26      chap  * unit, a Midiman packet is just a wrapper for 1 to 3 bytes of raw MIDI
   1704       1.26      chap  * with a cable nybble and a length nybble (which, unlike the CIN of a
   1705       1.26      chap  * real USB MIDI packet, has no semantics at all besides the length).
   1706       1.26      chap  * A packet received from a Midiman may contain part of a MIDI message,
   1707       1.26      chap  * more than one MIDI message, or parts of more than one MIDI message. A
   1708       1.26      chap  * three-byte MIDI message may arrive in three packets of data length 1, and
   1709       1.26      chap  * running status may be used. Happily, the midi(4) driver above us will put
   1710       1.26      chap  * it all back together, so the only cost is in USB bandwidth. The device
   1711       1.26      chap  * has an easier time with what it receives from us: we'll pack messages in
   1712       1.26      chap  * and across packets, but filling the packets whenever possible and,
   1713       1.26      chap  * as midi(4) hands us a complete message at a time, we'll never send one
   1714       1.26      chap  * in a dribble of short packets.
   1715       1.26      chap  */
   1716       1.26      chap 
   1717        1.4  tshiozak static int
   1718       1.26      chap out_jack_output(struct umidi_jack *out_jack, u_char *src, int len, int cin)
   1719        1.4  tshiozak {
   1720        1.4  tshiozak 	struct umidi_endpoint *ep = out_jack->endpoint;
   1721        1.4  tshiozak 	struct umidi_softc *sc = ep->sc;
   1722       1.26      chap 	unsigned char *packet;
   1723       1.26      chap 	int plen;
   1724       1.26      chap 	int poff;
   1725        1.4  tshiozak 
   1726  1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1727  1.65.14.6     skrll 
   1728        1.4  tshiozak 	if (sc->sc_dying)
   1729        1.4  tshiozak 		return EIO;
   1730        1.4  tshiozak 
   1731       1.26      chap 	if (!out_jack->opened)
   1732       1.26      chap 		return ENODEV; /* XXX as it was, is this the right errno? */
   1733        1.4  tshiozak 
   1734  1.65.14.6     skrll 	sc->sc_refcnt++;
   1735  1.65.14.6     skrll 
   1736       1.26      chap #ifdef UMIDI_DEBUG
   1737  1.65.14.6     skrll 	if (umididebug >= 100)
   1738       1.26      chap 		microtime(&umidi_tv);
   1739       1.26      chap #endif
   1740       1.39    cegger 	DPRINTFN(100, ("umidi out: %"PRIu64".%06"PRIu64"s ep=%p cn=%d len=%d cin=%#x\n",
   1741       1.39    cegger 	    umidi_tv.tv_sec%100, (uint64_t)umidi_tv.tv_usec,
   1742       1.26      chap 	    ep, out_jack->cable_number, len, cin));
   1743  1.65.14.1     skrll 
   1744       1.26      chap 	packet = *ep->next_slot++;
   1745       1.26      chap 	KASSERT(ep->buffer_size >=
   1746  1.65.14.7     skrll 	    (ep->next_slot - ep->buffer) * sizeof(*ep->buffer));
   1747       1.26      chap 	memset(packet, 0, UMIDI_PACKET_SIZE);
   1748       1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1749       1.26      chap 		if (NULL != out_jack->midiman_ppkt) { /* fill out a prev pkt */
   1750       1.26      chap 			poff = 0x0f & (out_jack->midiman_ppkt[3]);
   1751       1.26      chap 			plen = 3 - poff;
   1752       1.26      chap 			if (plen > len)
   1753       1.26      chap 				plen = len;
   1754       1.26      chap 			memcpy(out_jack->midiman_ppkt+poff, src, plen);
   1755       1.26      chap 			src += plen;
   1756       1.26      chap 			len -= plen;
   1757       1.26      chap 			plen += poff;
   1758       1.26      chap 			out_jack->midiman_ppkt[3] =
   1759       1.26      chap 			    MIX_CN_CIN(out_jack->cable_number, plen);
   1760       1.26      chap 			DPR_PACKET(out+, sc, out_jack->midiman_ppkt);
   1761       1.26      chap 			if (3 == plen)
   1762       1.26      chap 				out_jack->midiman_ppkt = NULL; /* no more */
   1763       1.26      chap 		}
   1764       1.26      chap 		if (0 == len)
   1765       1.26      chap 			ep->next_slot--; /* won't be needed, nevermind */
   1766       1.26      chap 		else {
   1767       1.26      chap 			memcpy(packet, src, len);
   1768       1.26      chap 			packet[3] = MIX_CN_CIN(out_jack->cable_number, len);
   1769       1.26      chap 			DPR_PACKET(out, sc, packet);
   1770       1.26      chap 			if (len < 3)
   1771       1.26      chap 				out_jack->midiman_ppkt = packet;
   1772       1.26      chap 		}
   1773       1.26      chap 	} else { /* the nice simple USB class-compliant case */
   1774       1.26      chap 		packet[0] = MIX_CN_CIN(out_jack->cable_number, cin);
   1775       1.26      chap 		memcpy(packet+1, src, len);
   1776       1.26      chap 		DPR_PACKET(out, sc, packet);
   1777       1.26      chap 	}
   1778       1.26      chap 	ep->next_schedule |= 1<<(out_jack->cable_number);
   1779       1.26      chap 	++ ep->num_scheduled;
   1780  1.65.14.6     skrll 	if (!ep->armed && !ep->soliciting) {
   1781       1.26      chap 		/*
   1782       1.26      chap 		 * It would be bad to call out_solicit directly here (the
   1783       1.26      chap 		 * caller need not be reentrant) but a soft interrupt allows
   1784       1.26      chap 		 * solicit to run immediately the caller exits its critical
   1785       1.26      chap 		 * section, and if the caller has more to write we can get it
   1786       1.26      chap 		 * before starting the USB transfer, and send a longer one.
   1787       1.26      chap 		 */
   1788       1.26      chap 		ep->soliciting = 1;
   1789       1.32        ad 		softint_schedule(ep->solicit_cookie);
   1790       1.26      chap 	}
   1791  1.65.14.1     skrll 
   1792  1.65.14.6     skrll 	if (--sc->sc_refcnt < 0)
   1793  1.65.14.6     skrll 		usb_detach_broadcast(sc->sc_dev, &sc->sc_detach_cv);
   1794  1.65.14.6     skrll 
   1795       1.26      chap 	return 0;
   1796        1.4  tshiozak }
   1797        1.4  tshiozak 
   1798        1.1  tshiozak static void
   1799  1.65.14.4     skrll in_intr(struct usbd_xfer *xfer, void *priv,
   1800       1.28  christos     usbd_status status)
   1801        1.1  tshiozak {
   1802        1.4  tshiozak 	int cn, len, i;
   1803        1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1804       1.45  jmcneill 	struct umidi_softc *sc = ep->sc;
   1805        1.4  tshiozak 	struct umidi_jack *jack;
   1806       1.26      chap 	unsigned char *packet;
   1807       1.26      chap 	umidi_packet_bufp slot;
   1808       1.26      chap 	umidi_packet_bufp end;
   1809       1.26      chap 	unsigned char *data;
   1810  1.65.14.1     skrll 	uint32_t count;
   1811        1.1  tshiozak 
   1812        1.3  tshiozak 	if (ep->sc->sc_dying || !ep->num_open)
   1813        1.1  tshiozak 		return;
   1814        1.1  tshiozak 
   1815       1.47       mrg 	mutex_enter(&sc->sc_lock);
   1816       1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1817  1.65.14.2     skrll 	if (0 == count % UMIDI_PACKET_SIZE) {
   1818       1.26      chap 		DPRINTFN(200,("%s: input endpoint %p transfer length %u\n",
   1819       1.42    dyoung 			     device_xname(ep->sc->sc_dev), ep, count));
   1820  1.65.14.2     skrll 	} else {
   1821  1.65.14.2     skrll 		DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
   1822  1.65.14.2     skrll 			device_xname(ep->sc->sc_dev), ep, count));
   1823  1.65.14.2     skrll 	}
   1824  1.65.14.1     skrll 
   1825       1.26      chap 	slot = ep->buffer;
   1826  1.65.14.7     skrll 	end = slot + count / sizeof(*slot);
   1827       1.26      chap 
   1828       1.47       mrg 	for (packet = *slot; slot < end; packet = *++slot) {
   1829  1.65.14.1     skrll 
   1830       1.47       mrg 		if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1831       1.26      chap 			cn = (0xf0&(packet[3]))>>4;
   1832       1.26      chap 			len = 0x0f&(packet[3]);
   1833       1.26      chap 			data = packet;
   1834       1.26      chap 		} else {
   1835       1.26      chap 			cn = GET_CN(packet[0]);
   1836       1.26      chap 			len = packet_length[GET_CIN(packet[0])];
   1837       1.26      chap 			data = packet + 1;
   1838       1.26      chap 		}
   1839       1.26      chap 		/* 0 <= cn <= 15 by inspection of above code */
   1840       1.26      chap 		if (!(jack = ep->jacks[cn]) || cn != jack->cable_number) {
   1841       1.26      chap 			DPRINTF(("%s: stray input endpoint %p cable %d len %d: "
   1842  1.65.14.2     skrll 				 "%02X %02X %02X (try CN_SEQ quirk?)\n",
   1843       1.42    dyoung 				 device_xname(ep->sc->sc_dev), ep, cn, len,
   1844       1.26      chap 				 (unsigned)data[0],
   1845       1.26      chap 				 (unsigned)data[1],
   1846       1.26      chap 				 (unsigned)data[2]));
   1847       1.47       mrg 			mutex_exit(&sc->sc_lock);
   1848       1.26      chap 			return;
   1849       1.26      chap 		}
   1850       1.26      chap 
   1851  1.65.14.6     skrll 		if (!jack->bound || !jack->opened)
   1852       1.26      chap 			continue;
   1853       1.26      chap 
   1854       1.26      chap 		DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
   1855  1.65.14.2     skrll 			     "%02X %02X %02X\n",
   1856       1.42    dyoung 			     device_xname(ep->sc->sc_dev), ep, cn, len,
   1857       1.26      chap 			     (unsigned)data[0],
   1858       1.26      chap 			     (unsigned)data[1],
   1859       1.26      chap 			     (unsigned)data[2]));
   1860       1.26      chap 
   1861       1.26      chap 		if (jack->u.in.intr) {
   1862       1.47       mrg 			for (i = 0; i < len; i++) {
   1863       1.26      chap 				(*jack->u.in.intr)(jack->arg, data[i]);
   1864       1.26      chap 			}
   1865        1.4  tshiozak 		}
   1866       1.26      chap 
   1867        1.4  tshiozak 	}
   1868        1.1  tshiozak 
   1869        1.1  tshiozak 	(void)start_input_transfer(ep);
   1870       1.47       mrg 	mutex_exit(&sc->sc_lock);
   1871        1.1  tshiozak }
   1872        1.1  tshiozak 
   1873        1.1  tshiozak static void
   1874  1.65.14.4     skrll out_intr(struct usbd_xfer *xfer, void *priv,
   1875       1.28  christos     usbd_status status)
   1876        1.1  tshiozak {
   1877        1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1878        1.1  tshiozak 	struct umidi_softc *sc = ep->sc;
   1879  1.65.14.1     skrll 	uint32_t count;
   1880        1.1  tshiozak 
   1881       1.26      chap 	if (sc->sc_dying)
   1882        1.1  tshiozak 		return;
   1883        1.1  tshiozak 
   1884       1.47       mrg 	mutex_enter(&sc->sc_lock);
   1885       1.26      chap #ifdef UMIDI_DEBUG
   1886  1.65.14.6     skrll 	if (umididebug >= 200)
   1887       1.26      chap 		microtime(&umidi_tv);
   1888       1.26      chap #endif
   1889       1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1890  1.65.14.6     skrll 	if (0 == count % UMIDI_PACKET_SIZE) {
   1891       1.39    cegger 		DPRINTFN(200,("%s: %"PRIu64".%06"PRIu64"s out ep %p xfer length %u\n",
   1892       1.42    dyoung 			     device_xname(ep->sc->sc_dev),
   1893       1.39    cegger 			     umidi_tv.tv_sec%100, (uint64_t)umidi_tv.tv_usec, ep, count));
   1894  1.65.14.2     skrll 	} else {
   1895  1.65.14.2     skrll 		DPRINTF(("%s: output endpoint %p odd transfer length %u\n",
   1896  1.65.14.2     skrll 			device_xname(ep->sc->sc_dev), ep, count));
   1897  1.65.14.2     skrll 	}
   1898       1.26      chap 	count /= UMIDI_PACKET_SIZE;
   1899  1.65.14.1     skrll 
   1900       1.26      chap 	/*
   1901       1.26      chap 	 * If while the transfer was pending we buffered any new messages,
   1902       1.26      chap 	 * move them to the start of the buffer.
   1903       1.26      chap 	 */
   1904       1.26      chap 	ep->next_slot -= count;
   1905       1.47       mrg 	if (ep->buffer < ep->next_slot) {
   1906       1.26      chap 		memcpy(ep->buffer, ep->buffer + count,
   1907       1.26      chap 		       (char *)ep->next_slot - (char *)ep->buffer);
   1908       1.26      chap 	}
   1909       1.47       mrg 	cv_broadcast(&sc->sc_cv);
   1910       1.26      chap 	/*
   1911       1.26      chap 	 * Do not want anyone else to see armed <- 0 before soliciting <- 1.
   1912       1.46       mrg 	 * Running at IPL_USB so the following should happen to be safe.
   1913       1.26      chap 	 */
   1914       1.26      chap 	ep->armed = 0;
   1915       1.47       mrg 	if (!ep->soliciting) {
   1916       1.26      chap 		ep->soliciting = 1;
   1917       1.47       mrg 		out_solicit_locked(ep);
   1918        1.1  tshiozak 	}
   1919       1.47       mrg 	mutex_exit(&sc->sc_lock);
   1920        1.1  tshiozak }
   1921        1.1  tshiozak 
   1922       1.26      chap /*
   1923       1.26      chap  * A jack on which we have received a packet must be called back on its
   1924       1.26      chap  * out.intr handler before it will send us another; it is considered
   1925       1.26      chap  * 'scheduled'. It is nice and predictable - as long as it is scheduled,
   1926       1.26      chap  * we need no extra buffer space for it.
   1927       1.26      chap  *
   1928       1.26      chap  * In contrast, a jack that is open but not scheduled may supply us a packet
   1929       1.26      chap  * at any time, driven by the top half, and we must be able to accept it, no
   1930       1.26      chap  * excuses. So we must ensure that at any point in time there are at least
   1931       1.26      chap  * (num_open - num_scheduled) slots free.
   1932       1.26      chap  *
   1933       1.26      chap  * As long as there are more slots free than that minimum, we can loop calling
   1934       1.26      chap  * scheduled jacks back on their "interrupt" handlers, soliciting more
   1935       1.26      chap  * packets, starting the USB transfer only when the buffer space is down to
   1936       1.26      chap  * the minimum or no jack has any more to send.
   1937       1.26      chap  */
   1938       1.47       mrg 
   1939        1.1  tshiozak static void
   1940       1.47       mrg out_solicit_locked(void *arg)
   1941        1.1  tshiozak {
   1942       1.26      chap 	struct umidi_endpoint *ep = arg;
   1943       1.26      chap 	umidi_packet_bufp end;
   1944  1.65.14.1     skrll 	uint16_t which;
   1945       1.26      chap 	struct umidi_jack *jack;
   1946       1.47       mrg 
   1947       1.48  jmcneill 	KASSERT(mutex_owned(&ep->sc->sc_lock));
   1948  1.65.14.1     skrll 
   1949  1.65.14.7     skrll 	end = ep->buffer + ep->buffer_size / sizeof(*ep->buffer);
   1950  1.65.14.1     skrll 
   1951       1.26      chap 	for ( ;; ) {
   1952       1.47       mrg 		if (end - ep->next_slot <= ep->num_open - ep->num_scheduled)
   1953       1.46       mrg 			break; /* at IPL_USB */
   1954       1.47       mrg 		if (ep->this_schedule == 0) {
   1955       1.47       mrg 			if (ep->next_schedule == 0)
   1956       1.46       mrg 				break; /* at IPL_USB */
   1957       1.26      chap 			ep->this_schedule = ep->next_schedule;
   1958       1.26      chap 			ep->next_schedule = 0;
   1959       1.26      chap 		}
   1960       1.26      chap 		/*
   1961       1.26      chap 		 * At least one jack is scheduled. Find and mask off the least
   1962       1.26      chap 		 * set bit in this_schedule and decrement num_scheduled.
   1963       1.26      chap 		 * Convert mask to bit index to find the corresponding jack,
   1964       1.26      chap 		 * and call its intr handler. If it has a message, it will call
   1965       1.26      chap 		 * back one of the output methods, which will set its bit in
   1966       1.26      chap 		 * next_schedule (not copied into this_schedule until the
   1967       1.26      chap 		 * latter is empty). In this way we round-robin the jacks that
   1968       1.26      chap 		 * have messages to send, until the buffer is as full as we
   1969       1.26      chap 		 * dare, and then start a transfer.
   1970       1.26      chap 		 */
   1971       1.26      chap 		which = ep->this_schedule;
   1972       1.26      chap 		which &= (~which)+1; /* now mask of least set bit */
   1973       1.26      chap 		ep->this_schedule &= ~which;
   1974       1.47       mrg 		--ep->num_scheduled;
   1975        1.1  tshiozak 
   1976       1.47       mrg 		--which; /* now 1s below mask - count 1s to get index */
   1977       1.26      chap 		which -= ((which >> 1) & 0x5555);/* SWAR credit aggregate.org */
   1978       1.26      chap 		which = (((which >> 2) & 0x3333) + (which & 0x3333));
   1979       1.26      chap 		which = (((which >> 4) + which) & 0x0f0f);
   1980       1.26      chap 		which +=  (which >> 8);
   1981       1.26      chap 		which &= 0x1f; /* the bit index a/k/a jack number */
   1982  1.65.14.1     skrll 
   1983       1.26      chap 		jack = ep->jacks[which];
   1984       1.26      chap 		if (jack->u.out.intr)
   1985       1.26      chap 			(*jack->u.out.intr)(jack->arg);
   1986        1.1  tshiozak 	}
   1987       1.46       mrg 	/* intr lock held at loop exit */
   1988  1.65.14.6     skrll 	if (!ep->armed && ep->next_slot > ep->buffer) {
   1989  1.65.14.6     skrll 		/*
   1990  1.65.14.6     skrll 		 * Can't hold the interrupt lock while calling into USB,
   1991  1.65.14.6     skrll 		 * but we can safely drop it here.
   1992  1.65.14.6     skrll 		 */
   1993  1.65.14.6     skrll 		mutex_exit(&ep->sc->sc_lock);
   1994       1.26      chap 		ep->armed = (USBD_IN_PROGRESS == start_output_transfer(ep));
   1995  1.65.14.6     skrll 		mutex_enter(&ep->sc->sc_lock);
   1996  1.65.14.6     skrll 	}
   1997       1.26      chap 	ep->soliciting = 0;
   1998       1.47       mrg }
   1999       1.47       mrg 
   2000       1.47       mrg /* Entry point for the softintr.  */
   2001       1.47       mrg static void
   2002       1.47       mrg out_solicit(void *arg)
   2003       1.47       mrg {
   2004       1.47       mrg 	struct umidi_endpoint *ep = arg;
   2005       1.47       mrg 	struct umidi_softc *sc = ep->sc;
   2006       1.47       mrg 
   2007       1.47       mrg 	mutex_enter(&sc->sc_lock);
   2008       1.47       mrg 	out_solicit_locked(arg);
   2009       1.47       mrg 	mutex_exit(&sc->sc_lock);
   2010        1.1  tshiozak }
   2011