Home | History | Annotate | Line # | Download | only in usb
umidi.c revision 1.65.14.15
      1  1.65.14.15     skrll /*	$NetBSD: umidi.c,v 1.65.14.15 2017/08/28 17:52:28 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.15     skrll __KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.65.14.15 2017/08/28 17:52:28 skrll Exp $");
     36  1.65.14.14     skrll 
     37  1.65.14.14     skrll #ifdef _KERNEL_OPT
     38  1.65.14.14     skrll #include "opt_usb.h"
     39  1.65.14.14     skrll #endif
     40         1.1  tshiozak 
     41        1.26      chap #include <sys/types.h>
     42         1.1  tshiozak #include <sys/param.h>
     43         1.1  tshiozak #include <sys/systm.h>
     44         1.1  tshiozak #include <sys/kernel.h>
     45        1.52       mrg #include <sys/kmem.h>
     46         1.1  tshiozak #include <sys/device.h>
     47         1.1  tshiozak #include <sys/ioctl.h>
     48         1.1  tshiozak #include <sys/conf.h>
     49         1.1  tshiozak #include <sys/file.h>
     50         1.1  tshiozak #include <sys/select.h>
     51         1.1  tshiozak #include <sys/proc.h>
     52         1.1  tshiozak #include <sys/vnode.h>
     53         1.1  tshiozak #include <sys/poll.h>
     54        1.32        ad #include <sys/intr.h>
     55        1.26      chap 
     56         1.1  tshiozak #include <dev/usb/usb.h>
     57         1.1  tshiozak #include <dev/usb/usbdi.h>
     58         1.1  tshiozak #include <dev/usb/usbdi_util.h>
     59         1.1  tshiozak 
     60        1.62       jdc #include <dev/auconv.h>
     61         1.1  tshiozak #include <dev/usb/usbdevs.h>
     62         1.1  tshiozak #include <dev/usb/umidi_quirks.h>
     63         1.1  tshiozak #include <dev/midi_if.h>
     64         1.1  tshiozak 
     65   1.65.14.6     skrll /* Jack Descriptor */
     66   1.65.14.6     skrll #define UMIDI_MS_HEADER	0x01
     67   1.65.14.6     skrll #define UMIDI_IN_JACK	0x02
     68   1.65.14.6     skrll #define UMIDI_OUT_JACK	0x03
     69   1.65.14.6     skrll 
     70   1.65.14.6     skrll /* Jack Type */
     71   1.65.14.6     skrll #define UMIDI_EMBEDDED	0x01
     72   1.65.14.6     skrll #define UMIDI_EXTERNAL	0x02
     73   1.65.14.6     skrll 
     74   1.65.14.6     skrll /* generic, for iteration */
     75   1.65.14.6     skrll typedef struct {
     76   1.65.14.6     skrll 	uByte		bLength;
     77   1.65.14.6     skrll 	uByte		bDescriptorType;
     78   1.65.14.6     skrll 	uByte		bDescriptorSubtype;
     79   1.65.14.6     skrll } UPACKED umidi_cs_descriptor_t;
     80   1.65.14.6     skrll 
     81   1.65.14.6     skrll typedef struct {
     82   1.65.14.6     skrll 	uByte		bLength;
     83   1.65.14.6     skrll 	uByte		bDescriptorType;
     84   1.65.14.6     skrll 	uByte		bDescriptorSubtype;
     85   1.65.14.6     skrll 	uWord		bcdMSC;
     86   1.65.14.6     skrll 	uWord		wTotalLength;
     87   1.65.14.6     skrll } UPACKED umidi_cs_interface_descriptor_t;
     88   1.65.14.6     skrll #define UMIDI_CS_INTERFACE_DESCRIPTOR_SIZE 7
     89   1.65.14.6     skrll 
     90   1.65.14.6     skrll typedef struct {
     91   1.65.14.6     skrll 	uByte		bLength;
     92   1.65.14.6     skrll 	uByte		bDescriptorType;
     93   1.65.14.6     skrll 	uByte		bDescriptorSubtype;
     94   1.65.14.6     skrll 	uByte		bNumEmbMIDIJack;
     95   1.65.14.6     skrll } UPACKED umidi_cs_endpoint_descriptor_t;
     96   1.65.14.6     skrll #define UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE 4
     97   1.65.14.6     skrll 
     98   1.65.14.6     skrll typedef struct {
     99   1.65.14.6     skrll 	uByte		bLength;
    100   1.65.14.6     skrll 	uByte		bDescriptorType;
    101   1.65.14.6     skrll 	uByte		bDescriptorSubtype;
    102   1.65.14.6     skrll 	uByte		bJackType;
    103   1.65.14.6     skrll 	uByte		bJackID;
    104   1.65.14.6     skrll } UPACKED umidi_jack_descriptor_t;
    105   1.65.14.6     skrll #define	UMIDI_JACK_DESCRIPTOR_SIZE	5
    106   1.65.14.6     skrll 
    107   1.65.14.6     skrll 
    108   1.65.14.6     skrll #define TO_D(p) ((usb_descriptor_t *)(p))
    109   1.65.14.6     skrll #define NEXT_D(desc) TO_D((char *)(desc)+(desc)->bLength)
    110   1.65.14.6     skrll #define TO_IFD(desc) ((usb_interface_descriptor_t *)(desc))
    111   1.65.14.6     skrll #define TO_CSIFD(desc) ((umidi_cs_interface_descriptor_t *)(desc))
    112   1.65.14.6     skrll #define TO_EPD(desc) ((usb_endpoint_descriptor_t *)(desc))
    113   1.65.14.6     skrll #define TO_CSEPD(desc) ((umidi_cs_endpoint_descriptor_t *)(desc))
    114   1.65.14.6     skrll 
    115   1.65.14.6     skrll 
    116   1.65.14.6     skrll #define UMIDI_PACKET_SIZE 4
    117   1.65.14.6     skrll 
    118   1.65.14.6     skrll /*
    119   1.65.14.6     skrll  * hierarchie
    120   1.65.14.6     skrll  *
    121   1.65.14.6     skrll  * <-- parent	       child -->
    122   1.65.14.6     skrll  *
    123   1.65.14.6     skrll  * umidi(sc) -> endpoint -> jack   <- (dynamically assignable) - mididev
    124   1.65.14.6     skrll  *	   ^	 |    ^	    |
    125   1.65.14.6     skrll  *	   +-----+    +-----+
    126   1.65.14.6     skrll  */
    127   1.65.14.6     skrll 
    128   1.65.14.6     skrll /* midi device */
    129   1.65.14.6     skrll struct umidi_mididev {
    130   1.65.14.6     skrll 	struct umidi_softc	*sc;
    131   1.65.14.6     skrll 	device_t		mdev;
    132   1.65.14.6     skrll 	/* */
    133   1.65.14.6     skrll 	struct umidi_jack	*in_jack;
    134   1.65.14.6     skrll 	struct umidi_jack	*out_jack;
    135   1.65.14.6     skrll 	char			*label;
    136   1.65.14.6     skrll 	size_t			label_len;
    137   1.65.14.6     skrll 	/* */
    138   1.65.14.6     skrll 	int			opened;
    139   1.65.14.6     skrll 	int			closing;
    140   1.65.14.6     skrll 	int			flags;
    141   1.65.14.6     skrll };
    142   1.65.14.6     skrll 
    143   1.65.14.6     skrll /* Jack Information */
    144   1.65.14.6     skrll struct umidi_jack {
    145   1.65.14.6     skrll 	struct umidi_endpoint	*endpoint;
    146   1.65.14.6     skrll 	/* */
    147   1.65.14.6     skrll 	int			cable_number;
    148   1.65.14.6     skrll 	void			*arg;
    149   1.65.14.6     skrll 	int			bound;
    150   1.65.14.6     skrll 	int			opened;
    151   1.65.14.6     skrll 	unsigned char		*midiman_ppkt;
    152   1.65.14.6     skrll 	union {
    153   1.65.14.6     skrll 		struct {
    154   1.65.14.6     skrll 			void			(*intr)(void *);
    155   1.65.14.6     skrll 		} out;
    156   1.65.14.6     skrll 		struct {
    157   1.65.14.6     skrll 			void			(*intr)(void *, int);
    158   1.65.14.6     skrll 		} in;
    159   1.65.14.6     skrll 	} u;
    160   1.65.14.6     skrll };
    161   1.65.14.6     skrll 
    162   1.65.14.6     skrll #define UMIDI_MAX_EPJACKS	16
    163   1.65.14.6     skrll typedef unsigned char (*umidi_packet_bufp)[UMIDI_PACKET_SIZE];
    164   1.65.14.6     skrll /* endpoint data */
    165   1.65.14.6     skrll struct umidi_endpoint {
    166   1.65.14.6     skrll 	struct umidi_softc	*sc;
    167   1.65.14.6     skrll 	/* */
    168   1.65.14.6     skrll 	int			addr;
    169   1.65.14.6     skrll 	struct usbd_pipe	*pipe;
    170   1.65.14.6     skrll 	struct usbd_xfer	*xfer;
    171   1.65.14.6     skrll 	umidi_packet_bufp	buffer;
    172   1.65.14.6     skrll 	umidi_packet_bufp	next_slot;
    173   1.65.14.6     skrll 	uint32_t               buffer_size;
    174   1.65.14.6     skrll 	int			num_scheduled;
    175   1.65.14.6     skrll 	int			num_open;
    176   1.65.14.6     skrll 	int			num_jacks;
    177   1.65.14.6     skrll 	int			soliciting;
    178   1.65.14.6     skrll 	void			*solicit_cookie;
    179   1.65.14.6     skrll 	int			armed;
    180   1.65.14.6     skrll 	struct umidi_jack	*jacks[UMIDI_MAX_EPJACKS];
    181   1.65.14.6     skrll 	uint16_t		this_schedule; /* see UMIDI_MAX_EPJACKS */
    182   1.65.14.6     skrll 	uint16_t		next_schedule;
    183   1.65.14.6     skrll };
    184   1.65.14.6     skrll 
    185   1.65.14.6     skrll /* software context */
    186   1.65.14.6     skrll struct umidi_softc {
    187   1.65.14.6     skrll 	device_t		sc_dev;
    188   1.65.14.6     skrll 	struct usbd_device	*sc_udev;
    189   1.65.14.6     skrll 	struct usbd_interface	*sc_iface;
    190   1.65.14.6     skrll 	const struct umidi_quirk	*sc_quirk;
    191   1.65.14.6     skrll 
    192   1.65.14.6     skrll 	int			sc_dying;
    193   1.65.14.6     skrll 
    194   1.65.14.6     skrll 	int			sc_out_num_jacks;
    195   1.65.14.6     skrll 	struct umidi_jack	*sc_out_jacks;
    196   1.65.14.6     skrll 	int			sc_in_num_jacks;
    197   1.65.14.6     skrll 	struct umidi_jack	*sc_in_jacks;
    198   1.65.14.6     skrll 	struct umidi_jack	*sc_jacks;
    199   1.65.14.6     skrll 
    200   1.65.14.6     skrll 	int			sc_num_mididevs;
    201   1.65.14.6     skrll 	struct umidi_mididev	*sc_mididevs;
    202   1.65.14.6     skrll 
    203   1.65.14.6     skrll 	int			sc_out_num_endpoints;
    204   1.65.14.6     skrll 	struct umidi_endpoint	*sc_out_ep;
    205   1.65.14.6     skrll 	int			sc_in_num_endpoints;
    206   1.65.14.6     skrll 	struct umidi_endpoint	*sc_in_ep;
    207   1.65.14.6     skrll 	struct umidi_endpoint	*sc_endpoints;
    208   1.65.14.6     skrll 	size_t			sc_endpoints_len;
    209   1.65.14.6     skrll 	int			cblnums_global;
    210   1.65.14.6     skrll 
    211   1.65.14.6     skrll 	kmutex_t		sc_lock;
    212   1.65.14.6     skrll 	kcondvar_t		sc_cv;
    213   1.65.14.6     skrll 	kcondvar_t		sc_detach_cv;
    214   1.65.14.6     skrll 
    215   1.65.14.6     skrll 	int			sc_refcnt;
    216   1.65.14.6     skrll };
    217   1.65.14.6     skrll 
    218         1.1  tshiozak #ifdef UMIDI_DEBUG
    219         1.1  tshiozak #define DPRINTF(x)	if (umididebug) printf x
    220         1.1  tshiozak #define DPRINTFN(n,x)	if (umididebug >= (n)) printf x
    221        1.26      chap #include <sys/time.h>
    222        1.26      chap static struct timeval umidi_tv;
    223         1.1  tshiozak int	umididebug = 0;
    224         1.1  tshiozak #else
    225         1.1  tshiozak #define DPRINTF(x)
    226         1.1  tshiozak #define DPRINTFN(n,x)
    227         1.1  tshiozak #endif
    228         1.1  tshiozak 
    229        1.52       mrg #define UMIDI_ENDPOINT_SIZE(sc)	(sizeof(*(sc)->sc_out_ep) * \
    230        1.52       mrg 				 (sc->sc_out_num_endpoints + \
    231        1.52       mrg 				  sc->sc_in_num_endpoints))
    232        1.52       mrg 
    233         1.1  tshiozak 
    234         1.1  tshiozak static int umidi_open(void *, int,
    235         1.1  tshiozak 		      void (*)(void *, int), void (*)(void *), void *);
    236         1.1  tshiozak static void umidi_close(void *);
    237        1.26      chap static int umidi_channelmsg(void *, int, int, u_char *, int);
    238        1.26      chap static int umidi_commonmsg(void *, int, u_char *, int);
    239        1.26      chap static int umidi_sysex(void *, u_char *, int);
    240        1.26      chap static int umidi_rtmsg(void *, int);
    241         1.1  tshiozak static void umidi_getinfo(void *, struct midi_info *);
    242        1.45  jmcneill static void umidi_get_locks(void *, kmutex_t **, kmutex_t **);
    243         1.1  tshiozak 
    244         1.3  tshiozak static usbd_status alloc_pipe(struct umidi_endpoint *);
    245         1.3  tshiozak static void free_pipe(struct umidi_endpoint *);
    246         1.1  tshiozak 
    247         1.1  tshiozak static usbd_status alloc_all_endpoints(struct umidi_softc *);
    248         1.1  tshiozak static void free_all_endpoints(struct umidi_softc *);
    249         1.1  tshiozak 
    250         1.1  tshiozak static usbd_status alloc_all_jacks(struct umidi_softc *);
    251         1.1  tshiozak static void free_all_jacks(struct umidi_softc *);
    252         1.1  tshiozak static usbd_status bind_jacks_to_mididev(struct umidi_softc *,
    253         1.1  tshiozak 					 struct umidi_jack *,
    254         1.1  tshiozak 					 struct umidi_jack *,
    255         1.1  tshiozak 					 struct umidi_mididev *);
    256         1.4  tshiozak static void unbind_jacks_from_mididev(struct umidi_mididev *);
    257         1.4  tshiozak static void unbind_all_jacks(struct umidi_softc *);
    258         1.1  tshiozak static usbd_status assign_all_jacks_automatically(struct umidi_softc *);
    259         1.4  tshiozak static usbd_status open_out_jack(struct umidi_jack *, void *,
    260         1.4  tshiozak 				 void (*)(void *));
    261         1.4  tshiozak static usbd_status open_in_jack(struct umidi_jack *, void *,
    262         1.4  tshiozak 				void (*)(void *, int));
    263         1.4  tshiozak static void close_out_jack(struct umidi_jack *);
    264         1.4  tshiozak static void close_in_jack(struct umidi_jack *);
    265         1.1  tshiozak 
    266        1.26      chap static usbd_status attach_mididev(struct umidi_softc *, struct umidi_mididev *);
    267         1.1  tshiozak static usbd_status detach_mididev(struct umidi_mididev *, int);
    268        1.40    dyoung static void deactivate_mididev(struct umidi_mididev *);
    269         1.1  tshiozak static usbd_status alloc_all_mididevs(struct umidi_softc *, int);
    270         1.1  tshiozak static void free_all_mididevs(struct umidi_softc *);
    271         1.1  tshiozak static usbd_status attach_all_mididevs(struct umidi_softc *);
    272         1.1  tshiozak static usbd_status detach_all_mididevs(struct umidi_softc *, int);
    273        1.40    dyoung static void deactivate_all_mididevs(struct umidi_softc *);
    274        1.52       mrg static void describe_mididev(struct umidi_mididev *);
    275         1.1  tshiozak 
    276         1.1  tshiozak #ifdef UMIDI_DEBUG
    277         1.1  tshiozak static void dump_sc(struct umidi_softc *);
    278         1.1  tshiozak static void dump_ep(struct umidi_endpoint *);
    279         1.1  tshiozak static void dump_jack(struct umidi_jack *);
    280         1.1  tshiozak #endif
    281         1.1  tshiozak 
    282         1.1  tshiozak static usbd_status start_input_transfer(struct umidi_endpoint *);
    283         1.1  tshiozak static usbd_status start_output_transfer(struct umidi_endpoint *);
    284        1.26      chap static int out_jack_output(struct umidi_jack *, u_char *, int, int);
    285   1.65.14.4     skrll static void in_intr(struct usbd_xfer *, void *, usbd_status);
    286   1.65.14.4     skrll static void out_intr(struct usbd_xfer *, void *, usbd_status);
    287        1.26      chap static void out_solicit(void *); /* struct umidi_endpoint* for softintr */
    288        1.47       mrg static void out_solicit_locked(void *); /* pre-locked version */
    289         1.1  tshiozak 
    290         1.1  tshiozak 
    291        1.22      yamt const struct midi_hw_if umidi_hw_if = {
    292        1.45  jmcneill 	.open = umidi_open,
    293        1.45  jmcneill 	.close = umidi_close,
    294        1.45  jmcneill 	.output = umidi_rtmsg,
    295        1.45  jmcneill 	.getinfo = umidi_getinfo,
    296        1.45  jmcneill 	.get_locks = umidi_get_locks,
    297         1.1  tshiozak };
    298         1.1  tshiozak 
    299        1.26      chap struct midi_hw_if_ext umidi_hw_if_ext = {
    300        1.26      chap 	.channel = umidi_channelmsg,
    301        1.26      chap 	.common  = umidi_commonmsg,
    302        1.26      chap 	.sysex   = umidi_sysex,
    303        1.26      chap };
    304        1.26      chap 
    305        1.26      chap struct midi_hw_if_ext umidi_hw_if_mm = {
    306        1.26      chap 	.channel = umidi_channelmsg,
    307        1.26      chap 	.common  = umidi_commonmsg,
    308        1.26      chap 	.sysex   = umidi_sysex,
    309        1.26      chap 	.compress = 1,
    310        1.26      chap };
    311        1.26      chap 
    312        1.37      cube int umidi_match(device_t, cfdata_t, void *);
    313        1.34    dyoung void umidi_attach(device_t, device_t, void *);
    314        1.34    dyoung void umidi_childdet(device_t, device_t);
    315        1.34    dyoung int umidi_detach(device_t, int);
    316        1.34    dyoung int umidi_activate(device_t, enum devact);
    317        1.34    dyoung extern struct cfdriver umidi_cd;
    318        1.37      cube CFATTACH_DECL2_NEW(umidi, sizeof(struct umidi_softc), umidi_match,
    319        1.34    dyoung     umidi_attach, umidi_detach, umidi_activate, NULL, umidi_childdet);
    320         1.1  tshiozak 
    321   1.65.14.1     skrll int
    322        1.42    dyoung umidi_match(device_t parent, cfdata_t match, void *aux)
    323         1.1  tshiozak {
    324   1.65.14.5     skrll 	struct usbif_attach_arg *uiaa = aux;
    325         1.1  tshiozak 
    326         1.1  tshiozak 	DPRINTFN(1,("umidi_match\n"));
    327         1.1  tshiozak 
    328   1.65.14.5     skrll 	if (umidi_search_quirk(uiaa->uiaa_vendor, uiaa->uiaa_product,
    329   1.65.14.5     skrll 	    uiaa->uiaa_ifaceno))
    330         1.3  tshiozak 		return UMATCH_IFACECLASS_IFACESUBCLASS;
    331         1.1  tshiozak 
    332   1.65.14.5     skrll 	if (uiaa->uiaa_class == UICLASS_AUDIO &&
    333   1.65.14.5     skrll 	    uiaa->uiaa_subclass == UISUBCLASS_MIDISTREAM)
    334         1.3  tshiozak 		return UMATCH_IFACECLASS_IFACESUBCLASS;
    335         1.1  tshiozak 
    336         1.3  tshiozak 	return UMATCH_NONE;
    337         1.1  tshiozak }
    338         1.1  tshiozak 
    339   1.65.14.1     skrll void
    340        1.42    dyoung umidi_attach(device_t parent, device_t self, void *aux)
    341         1.1  tshiozak {
    342        1.42    dyoung 	usbd_status     err;
    343        1.42    dyoung 	struct umidi_softc *sc = device_private(self);
    344   1.65.14.5     skrll 	struct usbif_attach_arg *uiaa = aux;
    345        1.23  augustss 	char *devinfop;
    346         1.1  tshiozak 
    347         1.1  tshiozak 	DPRINTFN(1,("umidi_attach\n"));
    348         1.1  tshiozak 
    349        1.37      cube 	sc->sc_dev = self;
    350        1.37      cube 
    351        1.44  jakllsch 	aprint_naive("\n");
    352        1.44  jakllsch 	aprint_normal("\n");
    353        1.44  jakllsch 
    354   1.65.14.5     skrll 	devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
    355        1.44  jakllsch 	aprint_normal_dev(self, "%s\n", devinfop);
    356        1.23  augustss 	usbd_devinfo_free(devinfop);
    357         1.1  tshiozak 
    358   1.65.14.5     skrll 	sc->sc_iface = uiaa->uiaa_iface;
    359   1.65.14.5     skrll 	sc->sc_udev = uiaa->uiaa_device;
    360   1.65.14.5     skrll 
    361   1.65.14.5     skrll 	sc->sc_quirk = umidi_search_quirk(uiaa->uiaa_vendor,
    362   1.65.14.5     skrll 	    uiaa->uiaa_product, uiaa->uiaa_ifaceno);
    363         1.1  tshiozak 
    364        1.37      cube 	aprint_normal_dev(self, "");
    365         1.1  tshiozak 	umidi_print_quirk(sc->sc_quirk);
    366         1.1  tshiozak 
    367  1.65.14.10     skrll 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
    368        1.47       mrg 	cv_init(&sc->sc_cv, "umidopcl");
    369   1.65.14.6     skrll 	cv_init(&sc->sc_detach_cv, "umidetcv");
    370   1.65.14.6     skrll 	sc->sc_refcnt = 0;
    371         1.1  tshiozak 
    372         1.1  tshiozak 	err = alloc_all_endpoints(sc);
    373        1.47       mrg 	if (err != USBD_NORMAL_COMPLETION) {
    374        1.37      cube 		aprint_error_dev(self,
    375        1.37      cube 		    "alloc_all_endpoints failed. (err=%d)\n", err);
    376   1.65.14.6     skrll 		goto out;
    377         1.1  tshiozak 	}
    378         1.1  tshiozak 	err = alloc_all_jacks(sc);
    379        1.47       mrg 	if (err != USBD_NORMAL_COMPLETION) {
    380        1.37      cube 		aprint_error_dev(self, "alloc_all_jacks failed. (err=%d)\n",
    381        1.37      cube 		    err);
    382   1.65.14.6     skrll 		goto out_free_endpoints;
    383         1.1  tshiozak 	}
    384        1.37      cube 	aprint_normal_dev(self, "out=%d, in=%d\n",
    385         1.1  tshiozak 	       sc->sc_out_num_jacks, sc->sc_in_num_jacks);
    386         1.1  tshiozak 
    387         1.1  tshiozak 	err = assign_all_jacks_automatically(sc);
    388        1.47       mrg 	if (err != USBD_NORMAL_COMPLETION) {
    389        1.37      cube 		aprint_error_dev(self,
    390        1.37      cube 		    "assign_all_jacks_automatically failed. (err=%d)\n", err);
    391   1.65.14.6     skrll 		goto out_free_jacks;
    392         1.1  tshiozak 	}
    393         1.1  tshiozak 	err = attach_all_mididevs(sc);
    394        1.47       mrg 	if (err != USBD_NORMAL_COMPLETION) {
    395        1.37      cube 		aprint_error_dev(self,
    396        1.37      cube 		    "attach_all_mididevs failed. (err=%d)\n", err);
    397   1.65.14.6     skrll 		goto out_free_jacks;
    398         1.1  tshiozak 	}
    399         1.1  tshiozak 
    400         1.1  tshiozak #ifdef UMIDI_DEBUG
    401         1.1  tshiozak 	dump_sc(sc);
    402         1.1  tshiozak #endif
    403         1.1  tshiozak 
    404  1.65.14.12     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
    405        1.16  augustss 
    406        1.42    dyoung 	return;
    407   1.65.14.6     skrll 
    408   1.65.14.6     skrll out_free_jacks:
    409   1.65.14.6     skrll 	unbind_all_jacks(sc);
    410   1.65.14.6     skrll 	free_all_jacks(sc);
    411   1.65.14.6     skrll 
    412   1.65.14.6     skrll out_free_endpoints:
    413   1.65.14.6     skrll 	free_all_endpoints(sc);
    414   1.65.14.6     skrll 
    415   1.65.14.6     skrll out:
    416        1.37      cube 	aprint_error_dev(self, "disabled.\n");
    417         1.1  tshiozak 	sc->sc_dying = 1;
    418        1.56    plunky 	KERNEL_UNLOCK_ONE(curlwp);
    419        1.42    dyoung 	return;
    420         1.1  tshiozak }
    421         1.1  tshiozak 
    422        1.34    dyoung void
    423        1.34    dyoung umidi_childdet(device_t self, device_t child)
    424        1.34    dyoung {
    425        1.34    dyoung 	int i;
    426        1.34    dyoung 	struct umidi_softc *sc = device_private(self);
    427        1.34    dyoung 
    428        1.34    dyoung 	KASSERT(sc->sc_mididevs != NULL);
    429        1.34    dyoung 
    430        1.34    dyoung 	for (i = 0; i < sc->sc_num_mididevs; i++) {
    431        1.34    dyoung 		if (sc->sc_mididevs[i].mdev == child)
    432        1.34    dyoung 			break;
    433        1.34    dyoung 	}
    434        1.34    dyoung 	KASSERT(i < sc->sc_num_mididevs);
    435        1.34    dyoung 	sc->sc_mididevs[i].mdev = NULL;
    436        1.34    dyoung }
    437        1.34    dyoung 
    438         1.1  tshiozak int
    439        1.34    dyoung umidi_activate(device_t self, enum devact act)
    440         1.1  tshiozak {
    441        1.34    dyoung 	struct umidi_softc *sc = device_private(self);
    442         1.1  tshiozak 
    443         1.1  tshiozak 	switch (act) {
    444         1.1  tshiozak 	case DVACT_DEACTIVATE:
    445         1.1  tshiozak 		DPRINTFN(1,("umidi_activate (deactivate)\n"));
    446         1.1  tshiozak 		sc->sc_dying = 1;
    447         1.1  tshiozak 		deactivate_all_mididevs(sc);
    448        1.40    dyoung 		return 0;
    449        1.40    dyoung 	default:
    450        1.40    dyoung 		DPRINTFN(1,("umidi_activate (%d)\n", act));
    451        1.40    dyoung 		return EOPNOTSUPP;
    452         1.1  tshiozak 	}
    453         1.1  tshiozak }
    454         1.1  tshiozak 
    455   1.65.14.1     skrll int
    456        1.42    dyoung umidi_detach(device_t self, int flags)
    457         1.1  tshiozak {
    458        1.42    dyoung 	struct umidi_softc *sc = device_private(self);
    459         1.1  tshiozak 
    460         1.1  tshiozak 	DPRINTFN(1,("umidi_detach\n"));
    461         1.1  tshiozak 
    462   1.65.14.6     skrll 	mutex_enter(&sc->sc_lock);
    463         1.1  tshiozak 	sc->sc_dying = 1;
    464   1.65.14.6     skrll 	if (--sc->sc_refcnt >= 0)
    465  1.65.14.13     skrll 		if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60)) {
    466  1.65.14.13     skrll 			printf("%s: %s didn't detach\n", __func__,
    467  1.65.14.13     skrll 			    device_xname(sc->sc_dev));
    468  1.65.14.13     skrll 		}
    469   1.65.14.6     skrll 	mutex_exit(&sc->sc_lock);
    470   1.65.14.6     skrll 
    471         1.1  tshiozak 	detach_all_mididevs(sc, flags);
    472         1.1  tshiozak 	free_all_mididevs(sc);
    473         1.1  tshiozak 	free_all_jacks(sc);
    474         1.1  tshiozak 	free_all_endpoints(sc);
    475         1.1  tshiozak 
    476  1.65.14.12     skrll 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
    477         1.1  tshiozak 
    478        1.45  jmcneill 	mutex_destroy(&sc->sc_lock);
    479   1.65.14.6     skrll 	cv_destroy(&sc->sc_detach_cv);
    480        1.47       mrg 	cv_destroy(&sc->sc_cv);
    481        1.45  jmcneill 
    482         1.1  tshiozak 	return 0;
    483         1.1  tshiozak }
    484         1.1  tshiozak 
    485         1.1  tshiozak 
    486         1.4  tshiozak /*
    487         1.4  tshiozak  * midi_if stuffs
    488         1.4  tshiozak  */
    489         1.1  tshiozak int
    490         1.1  tshiozak umidi_open(void *addr,
    491         1.1  tshiozak 	   int flags,
    492        1.12  augustss 	   void (*iintr)(void *, int),
    493        1.12  augustss 	   void (*ointr)(void *),
    494         1.1  tshiozak 	   void *arg)
    495         1.1  tshiozak {
    496         1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    497         1.3  tshiozak 	struct umidi_softc *sc = mididev->sc;
    498        1.26      chap 	usbd_status err;
    499         1.1  tshiozak 
    500   1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    501         1.1  tshiozak 	DPRINTF(("umidi_open: sc=%p\n", sc));
    502         1.1  tshiozak 
    503         1.1  tshiozak 	if (mididev->opened)
    504         1.1  tshiozak 		return EBUSY;
    505         1.4  tshiozak 	if (sc->sc_dying)
    506         1.1  tshiozak 		return EIO;
    507         1.1  tshiozak 
    508         1.4  tshiozak 	mididev->opened = 1;
    509         1.1  tshiozak 	mididev->flags = flags;
    510        1.26      chap 	if ((mididev->flags & FWRITE) && mididev->out_jack) {
    511        1.26      chap 		err = open_out_jack(mididev->out_jack, arg, ointr);
    512   1.65.14.6     skrll 		if (err != USBD_NORMAL_COMPLETION)
    513        1.26      chap 			goto bad;
    514        1.26      chap 	}
    515         1.3  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack) {
    516        1.26      chap 		err = open_in_jack(mididev->in_jack, arg, iintr);
    517   1.65.14.6     skrll 		KASSERT(mididev->opened);
    518   1.65.14.6     skrll 		if (err != USBD_NORMAL_COMPLETION &&
    519   1.65.14.6     skrll 		    err != USBD_IN_PROGRESS) {
    520   1.65.14.6     skrll 			if (mididev->out_jack)
    521   1.65.14.6     skrll 				close_out_jack(mididev->out_jack);
    522        1.26      chap 			goto bad;
    523   1.65.14.6     skrll 		}
    524         1.3  tshiozak 	}
    525         1.1  tshiozak 
    526         1.1  tshiozak 	return 0;
    527        1.26      chap bad:
    528        1.26      chap 	mididev->opened = 0;
    529        1.26      chap 	DPRINTF(("umidi_open: usbd_status %d\n", err));
    530   1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    531        1.26      chap 	return USBD_IN_USE == err ? EBUSY : EIO;
    532         1.1  tshiozak }
    533         1.1  tshiozak 
    534         1.1  tshiozak void
    535         1.1  tshiozak umidi_close(void *addr)
    536         1.1  tshiozak {
    537         1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    538   1.65.14.6     skrll 	struct umidi_softc *sc = mididev->sc;
    539   1.65.14.6     skrll 
    540   1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    541   1.65.14.6     skrll 
    542   1.65.14.6     skrll 	if (mididev->closing)
    543   1.65.14.6     skrll 		return;
    544         1.1  tshiozak 
    545        1.59       mrg 	mididev->closing = 1;
    546        1.59       mrg 
    547   1.65.14.6     skrll 	sc->sc_refcnt++;
    548        1.57       mrg 
    549         1.1  tshiozak 	if ((mididev->flags & FWRITE) && mididev->out_jack)
    550         1.4  tshiozak 		close_out_jack(mididev->out_jack);
    551         1.1  tshiozak 	if ((mididev->flags & FREAD) && mididev->in_jack)
    552         1.4  tshiozak 		close_in_jack(mididev->in_jack);
    553        1.57       mrg 
    554   1.65.14.6     skrll 	if (--sc->sc_refcnt < 0)
    555  1.65.14.13     skrll 		cv_broadcast(&sc->sc_detach_cv);
    556        1.59       mrg 
    557        1.59       mrg 	mididev->opened = 0;
    558   1.65.14.6     skrll 	mididev->closing = 0;
    559         1.1  tshiozak }
    560         1.1  tshiozak 
    561         1.1  tshiozak int
    562        1.28  christos umidi_channelmsg(void *addr, int status, int channel, u_char *msg,
    563        1.27  christos     int len)
    564        1.26      chap {
    565        1.26      chap 	struct umidi_mididev *mididev = addr;
    566        1.26      chap 
    567   1.65.14.6     skrll 	KASSERT(mutex_owned(&mididev->sc->sc_lock));
    568   1.65.14.6     skrll 
    569        1.60       mrg 	if (!mididev->out_jack || !mididev->opened || mididev->closing)
    570        1.26      chap 		return EIO;
    571   1.65.14.1     skrll 
    572        1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, (status>>4)&0xf);
    573        1.26      chap }
    574        1.26      chap 
    575        1.26      chap int
    576        1.28  christos umidi_commonmsg(void *addr, int status, u_char *msg, int len)
    577         1.1  tshiozak {
    578         1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    579        1.26      chap 	int cin;
    580         1.1  tshiozak 
    581   1.65.14.6     skrll 	KASSERT(mutex_owned(&mididev->sc->sc_lock));
    582   1.65.14.6     skrll 
    583        1.60       mrg 	if (!mididev->out_jack || !mididev->opened || mididev->closing)
    584         1.1  tshiozak 		return EIO;
    585         1.1  tshiozak 
    586        1.26      chap 	switch ( len ) {
    587        1.26      chap 	case 1: cin = 5; break;
    588        1.26      chap 	case 2: cin = 2; break;
    589        1.26      chap 	case 3: cin = 3; break;
    590        1.26      chap 	default: return EIO; /* or gcc warns of cin uninitialized */
    591        1.26      chap 	}
    592   1.65.14.1     skrll 
    593        1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, cin);
    594        1.26      chap }
    595        1.26      chap 
    596        1.26      chap int
    597        1.26      chap umidi_sysex(void *addr, u_char *msg, int len)
    598        1.26      chap {
    599        1.26      chap 	struct umidi_mididev *mididev = addr;
    600        1.26      chap 	int cin;
    601        1.26      chap 
    602   1.65.14.6     skrll 	KASSERT(mutex_owned(&mididev->sc->sc_lock));
    603   1.65.14.6     skrll 
    604        1.60       mrg 	if (!mididev->out_jack || !mididev->opened || mididev->closing)
    605        1.26      chap 		return EIO;
    606        1.26      chap 
    607        1.26      chap 	switch ( len ) {
    608        1.26      chap 	case 1: cin = 5; break;
    609        1.26      chap 	case 2: cin = 6; break;
    610        1.26      chap 	case 3: cin = (msg[2] == 0xf7) ? 7 : 4; break;
    611        1.26      chap 	default: return EIO; /* or gcc warns of cin uninitialized */
    612        1.26      chap 	}
    613   1.65.14.1     skrll 
    614        1.26      chap 	return out_jack_output(mididev->out_jack, msg, len, cin);
    615        1.26      chap }
    616        1.26      chap 
    617        1.26      chap int
    618        1.26      chap umidi_rtmsg(void *addr, int d)
    619        1.26      chap {
    620        1.26      chap 	struct umidi_mididev *mididev = addr;
    621        1.26      chap 	u_char msg = d;
    622        1.26      chap 
    623   1.65.14.6     skrll 	KASSERT(mutex_owned(&mididev->sc->sc_lock));
    624   1.65.14.6     skrll 
    625        1.60       mrg 	if (!mididev->out_jack || !mididev->opened || mididev->closing)
    626        1.26      chap 		return EIO;
    627        1.26      chap 
    628        1.26      chap 	return out_jack_output(mididev->out_jack, &msg, 1, 0xf);
    629         1.1  tshiozak }
    630         1.1  tshiozak 
    631         1.1  tshiozak void
    632         1.1  tshiozak umidi_getinfo(void *addr, struct midi_info *mi)
    633         1.1  tshiozak {
    634         1.3  tshiozak 	struct umidi_mididev *mididev = addr;
    635        1.26      chap 	struct umidi_softc *sc = mididev->sc;
    636        1.26      chap 	int mm = UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE);
    637         1.1  tshiozak 
    638   1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
    639   1.65.14.6     skrll 
    640        1.26      chap 	mi->name = mididev->label;
    641         1.3  tshiozak 	mi->props = MIDI_PROP_OUT_INTR;
    642         1.1  tshiozak 	if (mididev->in_jack)
    643         1.1  tshiozak 		mi->props |= MIDI_PROP_CAN_INPUT;
    644        1.26      chap 	midi_register_hw_if_ext(mm? &umidi_hw_if_mm : &umidi_hw_if_ext);
    645         1.1  tshiozak }
    646         1.1  tshiozak 
    647        1.45  jmcneill static void
    648        1.47       mrg umidi_get_locks(void *addr, kmutex_t **thread, kmutex_t **intr)
    649        1.45  jmcneill {
    650        1.45  jmcneill 	struct umidi_mididev *mididev = addr;
    651        1.45  jmcneill 	struct umidi_softc *sc = mididev->sc;
    652        1.45  jmcneill 
    653        1.47       mrg 	*intr = NULL;
    654        1.45  jmcneill 	*thread = &sc->sc_lock;
    655        1.45  jmcneill }
    656         1.1  tshiozak 
    657         1.4  tshiozak /*
    658         1.4  tshiozak  * each endpoint stuffs
    659         1.4  tshiozak  */
    660         1.1  tshiozak 
    661         1.3  tshiozak /* alloc/free pipe */
    662         1.1  tshiozak static usbd_status
    663         1.3  tshiozak alloc_pipe(struct umidi_endpoint *ep)
    664         1.1  tshiozak {
    665         1.1  tshiozak 	struct umidi_softc *sc = ep->sc;
    666         1.1  tshiozak 	usbd_status err;
    667        1.26      chap 	usb_endpoint_descriptor_t *epd;
    668   1.65.14.1     skrll 
    669        1.26      chap 	epd = usbd_get_endpoint_descriptor(sc->sc_iface, ep->addr);
    670        1.26      chap 	/*
    671        1.26      chap 	 * For output, an improvement would be to have a buffer bigger than
    672        1.26      chap 	 * wMaxPacketSize by num_jacks-1 additional packet slots; that would
    673        1.26      chap 	 * allow out_solicit to fill the buffer to the full packet size in
    674   1.65.14.8     skrll 	 * all cases. But to use usbd_create_xfer to get a slightly larger
    675        1.26      chap 	 * buffer would not be a good way to do that, because if the addition
    676        1.26      chap 	 * would make the buffer exceed USB_MEM_SMALL then a substantially
    677        1.26      chap 	 * larger block may be wastefully allocated. Some flavor of double
    678        1.26      chap 	 * buffering could serve the same purpose, but would increase the
    679        1.26      chap 	 * code complexity, so for now I will live with the current slight
    680        1.26      chap 	 * penalty of reducing max transfer size by (num_open-num_scheduled)
    681        1.26      chap 	 * packet slots.
    682        1.26      chap 	 */
    683        1.26      chap 	ep->buffer_size = UGETW(epd->wMaxPacketSize);
    684        1.26      chap 	ep->buffer_size -= ep->buffer_size % UMIDI_PACKET_SIZE;
    685        1.26      chap 
    686        1.26      chap 	DPRINTF(("%s: alloc_pipe %p, buffer size %u\n",
    687   1.65.14.2     skrll 		device_xname(sc->sc_dev), ep, ep->buffer_size));
    688        1.26      chap 	ep->num_scheduled = 0;
    689        1.26      chap 	ep->this_schedule = 0;
    690        1.26      chap 	ep->next_schedule = 0;
    691        1.26      chap 	ep->soliciting = 0;
    692        1.26      chap 	ep->armed = 0;
    693        1.64  jmcneill 	err = usbd_open_pipe(sc->sc_iface, ep->addr, USBD_MPSAFE, &ep->pipe);
    694         1.3  tshiozak 	if (err)
    695   1.65.14.8     skrll 		goto quit;
    696   1.65.14.8     skrll 	int error = usbd_create_xfer(ep->pipe, ep->buffer_size,
    697   1.65.14.8     skrll 	    USBD_SHORT_XFER_OK, 0, &ep->xfer);
    698   1.65.14.8     skrll 	if (error) {
    699   1.65.14.8     skrll 		usbd_close_pipe(ep->pipe);
    700   1.65.14.8     skrll 		return USBD_NOMEM;
    701   1.65.14.8     skrll 	}
    702   1.65.14.8     skrll 	ep->buffer = usbd_get_buffer(ep->xfer);
    703   1.65.14.8     skrll 	ep->next_slot = ep->buffer;
    704  1.65.14.12     skrll 	ep->solicit_cookie = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    705  1.65.14.12     skrll 	    out_solicit, ep);
    706         1.1  tshiozak quit:
    707         1.1  tshiozak 	return err;
    708         1.1  tshiozak }
    709         1.1  tshiozak 
    710         1.1  tshiozak static void
    711         1.3  tshiozak free_pipe(struct umidi_endpoint *ep)
    712         1.1  tshiozak {
    713        1.42    dyoung 	DPRINTF(("%s: free_pipe %p\n", device_xname(ep->sc->sc_dev), ep));
    714         1.3  tshiozak 	usbd_abort_pipe(ep->pipe);
    715   1.65.14.8     skrll 	usbd_destroy_xfer(ep->xfer);
    716   1.65.14.9     skrll 	usbd_close_pipe(ep->pipe);
    717        1.32        ad 	softint_disestablish(ep->solicit_cookie);
    718         1.1  tshiozak }
    719         1.1  tshiozak 
    720         1.1  tshiozak 
    721         1.1  tshiozak /* alloc/free the array of endpoint structures */
    722         1.1  tshiozak 
    723         1.1  tshiozak static usbd_status alloc_all_endpoints_fixed_ep(struct umidi_softc *);
    724         1.1  tshiozak static usbd_status alloc_all_endpoints_yamaha(struct umidi_softc *);
    725         1.1  tshiozak static usbd_status alloc_all_endpoints_genuine(struct umidi_softc *);
    726         1.1  tshiozak 
    727         1.1  tshiozak static usbd_status
    728         1.1  tshiozak alloc_all_endpoints(struct umidi_softc *sc)
    729         1.1  tshiozak {
    730         1.3  tshiozak 	usbd_status err;
    731         1.3  tshiozak 	struct umidi_endpoint *ep;
    732         1.3  tshiozak 	int i;
    733         1.9  tshiozak 
    734         1.3  tshiozak 	if (UMQ_ISTYPE(sc, UMQ_TYPE_FIXED_EP)) {
    735         1.3  tshiozak 		err = alloc_all_endpoints_fixed_ep(sc);
    736         1.3  tshiozak 	} else if (UMQ_ISTYPE(sc, UMQ_TYPE_YAMAHA)) {
    737         1.3  tshiozak 		err = alloc_all_endpoints_yamaha(sc);
    738         1.1  tshiozak 	} else {
    739         1.3  tshiozak 		err = alloc_all_endpoints_genuine(sc);
    740         1.1  tshiozak 	}
    741        1.46       mrg 	if (err != USBD_NORMAL_COMPLETION)
    742         1.3  tshiozak 		return err;
    743         1.3  tshiozak 
    744         1.3  tshiozak 	ep = sc->sc_endpoints;
    745        1.52       mrg 	for (i = sc->sc_out_num_endpoints+sc->sc_in_num_endpoints; i > 0; i--) {
    746         1.3  tshiozak 		err = alloc_pipe(ep++);
    747        1.52       mrg 		if (err != USBD_NORMAL_COMPLETION) {
    748        1.52       mrg 			for (; ep != sc->sc_endpoints; ep--)
    749         1.3  tshiozak 				free_pipe(ep-1);
    750        1.52       mrg 			kmem_free(sc->sc_endpoints, sc->sc_endpoints_len);
    751         1.3  tshiozak 			sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    752         1.3  tshiozak 			break;
    753         1.3  tshiozak 		}
    754         1.3  tshiozak 	}
    755         1.3  tshiozak 	return err;
    756         1.1  tshiozak }
    757         1.1  tshiozak 
    758         1.1  tshiozak static void
    759         1.1  tshiozak free_all_endpoints(struct umidi_softc *sc)
    760         1.1  tshiozak {
    761         1.3  tshiozak 	int i;
    762        1.46       mrg 
    763         1.8  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints+sc->sc_out_num_endpoints; i++)
    764        1.46       mrg 		free_pipe(&sc->sc_endpoints[i]);
    765        1.14      kent 	if (sc->sc_endpoints != NULL)
    766        1.52       mrg 		kmem_free(sc->sc_endpoints, sc->sc_endpoints_len);
    767         1.1  tshiozak 	sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
    768         1.1  tshiozak }
    769         1.1  tshiozak 
    770         1.1  tshiozak static usbd_status
    771         1.1  tshiozak alloc_all_endpoints_fixed_ep(struct umidi_softc *sc)
    772         1.1  tshiozak {
    773         1.3  tshiozak 	usbd_status err;
    774        1.38  gmcgarry 	const struct umq_fixed_ep_desc *fp;
    775         1.3  tshiozak 	struct umidi_endpoint *ep;
    776         1.1  tshiozak 	usb_endpoint_descriptor_t *epd;
    777         1.1  tshiozak 	int i;
    778         1.1  tshiozak 
    779         1.1  tshiozak 	fp = umidi_get_quirk_data_from_type(sc->sc_quirk,
    780         1.1  tshiozak 					    UMQ_TYPE_FIXED_EP);
    781         1.1  tshiozak 	sc->sc_out_num_jacks = 0;
    782         1.1  tshiozak 	sc->sc_in_num_jacks = 0;
    783         1.1  tshiozak 	sc->sc_out_num_endpoints = fp->num_out_ep;
    784         1.1  tshiozak 	sc->sc_in_num_endpoints = fp->num_in_ep;
    785        1.52       mrg 	sc->sc_endpoints_len = UMIDI_ENDPOINT_SIZE(sc);
    786        1.52       mrg 	sc->sc_endpoints = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    787         1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
    788         1.1  tshiozak 	sc->sc_in_ep =
    789         1.1  tshiozak 	    sc->sc_in_num_endpoints ?
    790         1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
    791         1.3  tshiozak 
    792         1.3  tshiozak 	ep = &sc->sc_out_ep[0];
    793        1.52       mrg 	for (i = 0; i < sc->sc_out_num_endpoints; i++) {
    794         1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    795         1.1  tshiozak 			sc->sc_iface,
    796         1.1  tshiozak 			fp->out_ep[i].ep);
    797         1.1  tshiozak 		if (!epd) {
    798        1.37      cube 			aprint_error_dev(sc->sc_dev,
    799        1.37      cube 			    "cannot get endpoint descriptor(out:%d)\n",
    800        1.37      cube 			     fp->out_ep[i].ep);
    801         1.3  tshiozak 			err = USBD_INVAL;
    802         1.1  tshiozak 			goto error;
    803         1.1  tshiozak 		}
    804         1.1  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes)!=UE_BULK ||
    805         1.1  tshiozak 		    UE_GET_DIR(epd->bEndpointAddress)!=UE_DIR_OUT) {
    806  1.65.14.12     skrll 			aprint_error_dev(sc->sc_dev,
    807  1.65.14.12     skrll 			    "illegal endpoint(out:%d)\n", fp->out_ep[i].ep);
    808         1.3  tshiozak 			err = USBD_INVAL;
    809         1.1  tshiozak 			goto error;
    810         1.1  tshiozak 		}
    811         1.3  tshiozak 		ep->sc = sc;
    812         1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    813         1.3  tshiozak 		ep->num_jacks = fp->out_ep[i].num_jacks;
    814         1.1  tshiozak 		sc->sc_out_num_jacks += fp->out_ep[i].num_jacks;
    815         1.3  tshiozak 		ep->num_open = 0;
    816         1.3  tshiozak 		ep++;
    817         1.1  tshiozak 	}
    818         1.3  tshiozak 	ep = &sc->sc_in_ep[0];
    819        1.52       mrg 	for (i = 0; i < sc->sc_in_num_endpoints; i++) {
    820         1.1  tshiozak 		epd = usbd_interface2endpoint_descriptor(
    821         1.1  tshiozak 			sc->sc_iface,
    822         1.1  tshiozak 			fp->in_ep[i].ep);
    823         1.1  tshiozak 		if (!epd) {
    824        1.37      cube 			aprint_error_dev(sc->sc_dev,
    825        1.37      cube 			    "cannot get endpoint descriptor(in:%d)\n",
    826        1.37      cube 			     fp->in_ep[i].ep);
    827         1.3  tshiozak 			err = USBD_INVAL;
    828         1.1  tshiozak 			goto error;
    829         1.1  tshiozak 		}
    830        1.26      chap 		/*
    831        1.26      chap 		 * MIDISPORT_2X4 inputs on an interrupt rather than a bulk
    832        1.26      chap 		 * endpoint.  The existing input logic in this driver seems
    833        1.26      chap 		 * to work successfully if we just stop treating an interrupt
    834        1.26      chap 		 * endpoint as illegal (or the in_progress status we get on
    835        1.26      chap 		 * the initial transfer).  It does not seem necessary to
    836        1.26      chap 		 * actually use the interrupt flavor of alloc_pipe or make
    837        1.26      chap 		 * other serious rearrangements of logic.  I like that.
    838        1.26      chap 		 */
    839        1.26      chap 		switch ( UE_GET_XFERTYPE(epd->bmAttributes) ) {
    840        1.26      chap 		case UE_BULK:
    841        1.26      chap 		case UE_INTERRUPT:
    842   1.65.14.6     skrll 			if (UE_DIR_IN == UE_GET_DIR(epd->bEndpointAddress))
    843        1.26      chap 				break;
    844        1.26      chap 			/*FALLTHROUGH*/
    845        1.26      chap 		default:
    846        1.37      cube 			aprint_error_dev(sc->sc_dev,
    847        1.37      cube 			    "illegal endpoint(in:%d)\n", fp->in_ep[i].ep);
    848         1.3  tshiozak 			err = USBD_INVAL;
    849         1.1  tshiozak 			goto error;
    850         1.1  tshiozak 		}
    851        1.26      chap 
    852         1.3  tshiozak 		ep->sc = sc;
    853         1.3  tshiozak 		ep->addr = epd->bEndpointAddress;
    854         1.3  tshiozak 		ep->num_jacks = fp->in_ep[i].num_jacks;
    855         1.1  tshiozak 		sc->sc_in_num_jacks += fp->in_ep[i].num_jacks;
    856         1.3  tshiozak 		ep->num_open = 0;
    857         1.3  tshiozak 		ep++;
    858         1.1  tshiozak 	}
    859         1.1  tshiozak 
    860         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    861         1.1  tshiozak error:
    862        1.52       mrg 	kmem_free(sc->sc_endpoints, UMIDI_ENDPOINT_SIZE(sc));
    863         1.1  tshiozak 	sc->sc_endpoints = NULL;
    864         1.3  tshiozak 	return err;
    865         1.1  tshiozak }
    866         1.1  tshiozak 
    867         1.1  tshiozak static usbd_status
    868         1.1  tshiozak alloc_all_endpoints_yamaha(struct umidi_softc *sc)
    869         1.1  tshiozak {
    870         1.1  tshiozak 	/* This driver currently supports max 1in/1out bulk endpoints */
    871         1.1  tshiozak 	usb_descriptor_t *desc;
    872        1.29  drochner 	umidi_cs_descriptor_t *udesc;
    873         1.8  tshiozak 	usb_endpoint_descriptor_t *epd;
    874         1.8  tshiozak 	int out_addr, in_addr, i;
    875         1.8  tshiozak 	int dir;
    876         1.1  tshiozak 	size_t remain, descsize;
    877         1.1  tshiozak 
    878         1.8  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    879         1.8  tshiozak 	out_addr = in_addr = 0;
    880         1.8  tshiozak 
    881         1.8  tshiozak 	/* detect endpoints */
    882         1.1  tshiozak 	desc = TO_D(usbd_get_interface_descriptor(sc->sc_iface));
    883         1.8  tshiozak 	for (i=(int)TO_IFD(desc)->bNumEndpoints-1; i>=0; i--) {
    884         1.8  tshiozak 		epd = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
    885        1.25  christos 		KASSERT(epd != NULL);
    886         1.8  tshiozak 		if (UE_GET_XFERTYPE(epd->bmAttributes) == UE_BULK) {
    887         1.8  tshiozak 			dir = UE_GET_DIR(epd->bEndpointAddress);
    888         1.8  tshiozak 			if (dir==UE_DIR_OUT && !out_addr)
    889         1.8  tshiozak 				out_addr = epd->bEndpointAddress;
    890         1.8  tshiozak 			else if (dir==UE_DIR_IN && !in_addr)
    891         1.8  tshiozak 				in_addr = epd->bEndpointAddress;
    892         1.8  tshiozak 		}
    893         1.8  tshiozak 	}
    894        1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(desc);
    895         1.1  tshiozak 
    896         1.8  tshiozak 	/* count jacks */
    897        1.29  drochner 	if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
    898        1.29  drochner 	      udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
    899         1.8  tshiozak 		return USBD_INVAL;
    900        1.29  drochner 	remain = (size_t)UGETW(TO_CSIFD(udesc)->wTotalLength) -
    901        1.29  drochner 		(size_t)udesc->bLength;
    902        1.29  drochner 	udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    903         1.1  tshiozak 
    904        1.52       mrg 	while (remain >= sizeof(usb_descriptor_t)) {
    905        1.29  drochner 		descsize = udesc->bLength;
    906         1.1  tshiozak 		if (descsize>remain || descsize==0)
    907         1.1  tshiozak 			break;
    908        1.52       mrg 		if (udesc->bDescriptorType == UDESC_CS_INTERFACE &&
    909        1.52       mrg 		    remain >= UMIDI_JACK_DESCRIPTOR_SIZE) {
    910        1.52       mrg 			if (udesc->bDescriptorSubtype == UMIDI_OUT_JACK)
    911         1.1  tshiozak 				sc->sc_out_num_jacks++;
    912        1.52       mrg 			else if (udesc->bDescriptorSubtype == UMIDI_IN_JACK)
    913         1.1  tshiozak 				sc->sc_in_num_jacks++;
    914         1.1  tshiozak 		}
    915        1.29  drochner 		udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
    916        1.52       mrg 		remain -= descsize;
    917         1.1  tshiozak 	}
    918         1.1  tshiozak 
    919         1.8  tshiozak 	/* validate some parameters */
    920         1.1  tshiozak 	if (sc->sc_out_num_jacks>UMIDI_MAX_EPJACKS)
    921         1.1  tshiozak 		sc->sc_out_num_jacks = UMIDI_MAX_EPJACKS;
    922         1.1  tshiozak 	if (sc->sc_in_num_jacks>UMIDI_MAX_EPJACKS)
    923         1.1  tshiozak 		sc->sc_in_num_jacks = UMIDI_MAX_EPJACKS;
    924         1.8  tshiozak 	if (sc->sc_out_num_jacks && out_addr) {
    925         1.1  tshiozak 		sc->sc_out_num_endpoints = 1;
    926         1.1  tshiozak 	} else {
    927         1.1  tshiozak 		sc->sc_out_num_endpoints = 0;
    928         1.1  tshiozak 		sc->sc_out_num_jacks = 0;
    929         1.1  tshiozak 	}
    930         1.8  tshiozak 	if (sc->sc_in_num_jacks && in_addr) {
    931         1.1  tshiozak 		sc->sc_in_num_endpoints = 1;
    932         1.1  tshiozak 	} else {
    933         1.1  tshiozak 		sc->sc_in_num_endpoints = 0;
    934         1.1  tshiozak 		sc->sc_in_num_jacks = 0;
    935         1.1  tshiozak 	}
    936        1.52       mrg 	sc->sc_endpoints_len = UMIDI_ENDPOINT_SIZE(sc);
    937        1.52       mrg 	sc->sc_endpoints = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    938         1.1  tshiozak 	if (sc->sc_out_num_endpoints) {
    939         1.1  tshiozak 		sc->sc_out_ep = sc->sc_endpoints;
    940         1.1  tshiozak 		sc->sc_out_ep->sc = sc;
    941         1.1  tshiozak 		sc->sc_out_ep->addr = out_addr;
    942         1.1  tshiozak 		sc->sc_out_ep->num_jacks = sc->sc_out_num_jacks;
    943         1.1  tshiozak 		sc->sc_out_ep->num_open = 0;
    944         1.1  tshiozak 	} else
    945         1.1  tshiozak 		sc->sc_out_ep = NULL;
    946         1.1  tshiozak 
    947         1.1  tshiozak 	if (sc->sc_in_num_endpoints) {
    948         1.1  tshiozak 		sc->sc_in_ep = sc->sc_endpoints+sc->sc_out_num_endpoints;
    949         1.1  tshiozak 		sc->sc_in_ep->sc = sc;
    950         1.1  tshiozak 		sc->sc_in_ep->addr = in_addr;
    951         1.1  tshiozak 		sc->sc_in_ep->num_jacks = sc->sc_in_num_jacks;
    952         1.1  tshiozak 		sc->sc_in_ep->num_open = 0;
    953         1.1  tshiozak 	} else
    954         1.1  tshiozak 		sc->sc_in_ep = NULL;
    955         1.1  tshiozak 
    956         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
    957         1.1  tshiozak }
    958         1.1  tshiozak 
    959         1.1  tshiozak static usbd_status
    960         1.1  tshiozak alloc_all_endpoints_genuine(struct umidi_softc *sc)
    961         1.1  tshiozak {
    962        1.20      gson 	usb_interface_descriptor_t *interface_desc;
    963        1.20      gson 	usb_config_descriptor_t *config_desc;
    964         1.1  tshiozak 	usb_descriptor_t *desc;
    965         1.7  tshiozak 	int num_ep;
    966         1.1  tshiozak 	size_t remain, descsize;
    967         1.1  tshiozak 	struct umidi_endpoint *p, *q, *lowest, *endep, tmpep;
    968         1.1  tshiozak 	int epaddr;
    969         1.1  tshiozak 
    970        1.20      gson 	interface_desc = usbd_get_interface_descriptor(sc->sc_iface);
    971        1.20      gson 	num_ep = interface_desc->bNumEndpoints;
    972        1.52       mrg 	sc->sc_endpoints_len = sizeof(struct umidi_endpoint) * num_ep;
    973        1.52       mrg 	sc->sc_endpoints = p = kmem_zalloc(sc->sc_endpoints_len, KM_SLEEP);
    974         1.1  tshiozak 	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
    975         1.1  tshiozak 	sc->sc_out_num_endpoints = sc->sc_in_num_endpoints = 0;
    976         1.1  tshiozak 	epaddr = -1;
    977         1.1  tshiozak 
    978         1.1  tshiozak 	/* get the list of endpoints for midi stream */
    979        1.20      gson 	config_desc = usbd_get_config_descriptor(sc->sc_udev);
    980        1.20      gson 	desc = (usb_descriptor_t *) config_desc;
    981        1.20      gson 	remain = (size_t)UGETW(config_desc->wTotalLength);
    982         1.1  tshiozak 	while (remain>=sizeof(usb_descriptor_t)) {
    983         1.1  tshiozak 		descsize = desc->bLength;
    984         1.1  tshiozak 		if (descsize>remain || descsize==0)
    985         1.1  tshiozak 			break;
    986         1.1  tshiozak 		if (desc->bDescriptorType==UDESC_ENDPOINT &&
    987         1.1  tshiozak 		    remain>=USB_ENDPOINT_DESCRIPTOR_SIZE &&
    988         1.1  tshiozak 		    UE_GET_XFERTYPE(TO_EPD(desc)->bmAttributes) == UE_BULK) {
    989         1.1  tshiozak 			epaddr = TO_EPD(desc)->bEndpointAddress;
    990         1.1  tshiozak 		} else if (desc->bDescriptorType==UDESC_CS_ENDPOINT &&
    991         1.1  tshiozak 			   remain>=UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE &&
    992         1.1  tshiozak 			   epaddr!=-1) {
    993         1.1  tshiozak 			if (num_ep>0) {
    994         1.9  tshiozak 				num_ep--;
    995         1.1  tshiozak 				p->sc = sc;
    996         1.1  tshiozak 				p->addr = epaddr;
    997         1.1  tshiozak 				p->num_jacks = TO_CSEPD(desc)->bNumEmbMIDIJack;
    998         1.1  tshiozak 				if (UE_GET_DIR(epaddr)==UE_DIR_OUT) {
    999         1.1  tshiozak 					sc->sc_out_num_endpoints++;
   1000         1.1  tshiozak 					sc->sc_out_num_jacks += p->num_jacks;
   1001         1.1  tshiozak 				} else {
   1002         1.1  tshiozak 					sc->sc_in_num_endpoints++;
   1003         1.1  tshiozak 					sc->sc_in_num_jacks += p->num_jacks;
   1004         1.1  tshiozak 				}
   1005         1.1  tshiozak 				p++;
   1006         1.1  tshiozak 			}
   1007         1.1  tshiozak 		} else
   1008         1.1  tshiozak 			epaddr = -1;
   1009         1.1  tshiozak 		desc = NEXT_D(desc);
   1010         1.1  tshiozak 		remain-=descsize;
   1011         1.1  tshiozak 	}
   1012         1.1  tshiozak 
   1013         1.1  tshiozak 	/* sort endpoints */
   1014         1.1  tshiozak 	num_ep = sc->sc_out_num_endpoints + sc->sc_in_num_endpoints;
   1015         1.1  tshiozak 	p = sc->sc_endpoints;
   1016         1.1  tshiozak 	endep = p + num_ep;
   1017         1.1  tshiozak 	while (p<endep) {
   1018         1.1  tshiozak 		lowest = p;
   1019         1.1  tshiozak 		for (q=p+1; q<endep; q++) {
   1020         1.1  tshiozak 			if ((UE_GET_DIR(lowest->addr)==UE_DIR_IN &&
   1021         1.1  tshiozak 			     UE_GET_DIR(q->addr)==UE_DIR_OUT) ||
   1022         1.1  tshiozak 			    ((UE_GET_DIR(lowest->addr)==
   1023         1.1  tshiozak 			      UE_GET_DIR(q->addr)) &&
   1024         1.1  tshiozak 			     (UE_GET_ADDR(lowest->addr)>
   1025         1.1  tshiozak 			      UE_GET_ADDR(q->addr))))
   1026         1.1  tshiozak 				lowest = q;
   1027         1.1  tshiozak 		}
   1028         1.1  tshiozak 		if (lowest != p) {
   1029         1.1  tshiozak 			memcpy((void *)&tmpep, (void *)p, sizeof(tmpep));
   1030         1.1  tshiozak 			memcpy((void *)p, (void *)lowest, sizeof(tmpep));
   1031         1.1  tshiozak 			memcpy((void *)lowest, (void *)&tmpep, sizeof(tmpep));
   1032         1.1  tshiozak 		}
   1033         1.7  tshiozak 		p->num_open = 0;
   1034         1.7  tshiozak 		p++;
   1035         1.1  tshiozak 	}
   1036        1.16  augustss 
   1037         1.1  tshiozak 	sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
   1038         1.1  tshiozak 	sc->sc_in_ep =
   1039         1.1  tshiozak 	    sc->sc_in_num_endpoints ?
   1040         1.1  tshiozak 		sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
   1041         1.1  tshiozak 
   1042         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1043         1.1  tshiozak }
   1044         1.1  tshiozak 
   1045         1.1  tshiozak 
   1046         1.4  tshiozak /*
   1047         1.4  tshiozak  * jack stuffs
   1048         1.4  tshiozak  */
   1049         1.4  tshiozak 
   1050         1.1  tshiozak static usbd_status
   1051         1.1  tshiozak alloc_all_jacks(struct umidi_softc *sc)
   1052         1.1  tshiozak {
   1053         1.1  tshiozak 	int i, j;
   1054         1.1  tshiozak 	struct umidi_endpoint *ep;
   1055        1.26      chap 	struct umidi_jack *jack;
   1056        1.38  gmcgarry 	const unsigned char *cn_spec;
   1057   1.65.14.1     skrll 
   1058        1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_PER_EP))
   1059        1.26      chap 		sc->cblnums_global = 0;
   1060        1.26      chap 	else if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_SEQ_GLOBAL))
   1061        1.26      chap 		sc->cblnums_global = 1;
   1062        1.26      chap 	else {
   1063        1.26      chap 		/*
   1064        1.26      chap 		 * I don't think this default is correct, but it preserves
   1065        1.26      chap 		 * the prior behavior of the code. That's why I defined two
   1066        1.26      chap 		 * complementary quirks. Any device for which the default
   1067        1.26      chap 		 * behavior is wrong can be made to work by giving it an
   1068        1.26      chap 		 * explicit quirk, and if a pattern ever develops (as I suspect
   1069        1.26      chap 		 * it will) that a lot of otherwise standard USB MIDI devices
   1070        1.26      chap 		 * need the CN_SEQ_PER_EP "quirk," then this default can be
   1071        1.26      chap 		 * changed to 0, and the only devices that will break are those
   1072        1.26      chap 		 * listing neither quirk, and they'll easily be fixed by giving
   1073        1.26      chap 		 * them the CN_SEQ_GLOBAL quirk.
   1074        1.26      chap 		 */
   1075        1.26      chap 		sc->cblnums_global = 1;
   1076        1.26      chap 	}
   1077   1.65.14.1     skrll 
   1078        1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_CN_FIXED))
   1079        1.26      chap 		cn_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
   1080        1.26      chap 					    		 UMQ_TYPE_CN_FIXED);
   1081        1.26      chap 	else
   1082        1.26      chap 		cn_spec = NULL;
   1083         1.1  tshiozak 
   1084         1.1  tshiozak 	/* allocate/initialize structures */
   1085  1.65.14.12     skrll 	sc->sc_jacks =
   1086  1.65.14.12     skrll 	    kmem_zalloc(sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks
   1087  1.65.14.12     skrll 		    + sc->sc_out_num_jacks), KM_SLEEP);
   1088         1.1  tshiozak 	if (!sc->sc_jacks)
   1089         1.1  tshiozak 		return USBD_NOMEM;
   1090         1.1  tshiozak 	sc->sc_out_jacks =
   1091         1.1  tshiozak 	    sc->sc_out_num_jacks ? sc->sc_jacks : NULL;
   1092         1.1  tshiozak 	sc->sc_in_jacks =
   1093         1.1  tshiozak 	    sc->sc_in_num_jacks ? sc->sc_jacks+sc->sc_out_num_jacks : NULL;
   1094         1.1  tshiozak 
   1095         1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
   1096        1.47       mrg 	for (i = 0; i < sc->sc_out_num_jacks; i++) {
   1097         1.4  tshiozak 		jack->opened = 0;
   1098   1.65.14.6     skrll 		jack->bound = 0;
   1099         1.4  tshiozak 		jack->arg = NULL;
   1100         1.4  tshiozak 		jack->u.out.intr = NULL;
   1101        1.26      chap 		jack->midiman_ppkt = NULL;
   1102        1.47       mrg 		if (sc->cblnums_global)
   1103        1.26      chap 			jack->cable_number = i;
   1104         1.1  tshiozak 		jack++;
   1105         1.1  tshiozak 	}
   1106         1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
   1107        1.47       mrg 	for (i = 0; i < sc->sc_in_num_jacks; i++) {
   1108         1.4  tshiozak 		jack->opened = 0;
   1109   1.65.14.6     skrll 		jack->bound = 0;
   1110         1.4  tshiozak 		jack->arg = NULL;
   1111         1.4  tshiozak 		jack->u.in.intr = NULL;
   1112        1.47       mrg 		if (sc->cblnums_global)
   1113        1.26      chap 			jack->cable_number = i;
   1114         1.1  tshiozak 		jack++;
   1115         1.1  tshiozak 	}
   1116         1.1  tshiozak 
   1117         1.1  tshiozak 	/* assign each jacks to each endpoints */
   1118         1.1  tshiozak 	jack = &sc->sc_out_jacks[0];
   1119         1.1  tshiozak 	ep = &sc->sc_out_ep[0];
   1120        1.47       mrg 	for (i = 0; i < sc->sc_out_num_endpoints; i++) {
   1121        1.47       mrg 		for (j = 0; j < ep->num_jacks; j++) {
   1122         1.1  tshiozak 			jack->endpoint = ep;
   1123        1.47       mrg 			if (cn_spec != NULL)
   1124        1.26      chap 				jack->cable_number = *cn_spec++;
   1125        1.47       mrg 			else if (!sc->cblnums_global)
   1126        1.26      chap 				jack->cable_number = j;
   1127        1.26      chap 			ep->jacks[jack->cable_number] = jack;
   1128         1.1  tshiozak 			jack++;
   1129         1.1  tshiozak 		}
   1130         1.1  tshiozak 		ep++;
   1131         1.1  tshiozak 	}
   1132         1.1  tshiozak 	jack = &sc->sc_in_jacks[0];
   1133         1.1  tshiozak 	ep = &sc->sc_in_ep[0];
   1134        1.47       mrg 	for (i = 0; i < sc->sc_in_num_endpoints; i++) {
   1135        1.47       mrg 		for (j = 0; j < ep->num_jacks; j++) {
   1136         1.1  tshiozak 			jack->endpoint = ep;
   1137        1.47       mrg 			if (cn_spec != NULL)
   1138        1.26      chap 				jack->cable_number = *cn_spec++;
   1139        1.47       mrg 			else if (!sc->cblnums_global)
   1140        1.26      chap 				jack->cable_number = j;
   1141        1.26      chap 			ep->jacks[jack->cable_number] = jack;
   1142         1.1  tshiozak 			jack++;
   1143         1.1  tshiozak 		}
   1144         1.1  tshiozak 		ep++;
   1145         1.1  tshiozak 	}
   1146         1.1  tshiozak 
   1147         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1148         1.1  tshiozak }
   1149         1.1  tshiozak 
   1150         1.1  tshiozak static void
   1151         1.1  tshiozak free_all_jacks(struct umidi_softc *sc)
   1152         1.1  tshiozak {
   1153        1.47       mrg 	struct umidi_jack *jacks;
   1154        1.58       mrg 	size_t len;
   1155        1.47       mrg 
   1156        1.47       mrg 	mutex_enter(&sc->sc_lock);
   1157        1.58       mrg 	jacks = sc->sc_jacks;
   1158  1.65.14.12     skrll 	len = sizeof(*sc->sc_out_jacks)
   1159  1.65.14.12     skrll 	    * (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.10     skrll 	mutex_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.10     skrll 	mutex_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.10     skrll 			mutex_exit(&sc->sc_lock);
   1391        1.59       mrg 			usbd_abort_pipe(jack->endpoint->pipe);
   1392  1.65.14.10     skrll 			mutex_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.10     skrll 	mutex_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.10     skrll 	mutex_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 	return USBD_NORMAL_COMPLETION;
   1455         1.1  tshiozak }
   1456         1.1  tshiozak 
   1457         1.1  tshiozak static void
   1458         1.1  tshiozak free_all_mididevs(struct umidi_softc *sc)
   1459         1.1  tshiozak {
   1460   1.65.14.6     skrll 	struct umidi_mididev *mididevs;
   1461   1.65.14.6     skrll 	size_t len;
   1462   1.65.14.6     skrll 
   1463   1.65.14.6     skrll 	mutex_enter(&sc->sc_lock);
   1464   1.65.14.6     skrll 	mididevs = sc->sc_mididevs;
   1465   1.65.14.6     skrll 	if (mididevs)
   1466   1.65.14.6     skrll 		  len = sizeof(*sc->sc_mididevs )* sc->sc_num_mididevs;
   1467   1.65.14.6     skrll 	sc->sc_mididevs = NULL;
   1468         1.1  tshiozak 	sc->sc_num_mididevs = 0;
   1469   1.65.14.6     skrll 	mutex_exit(&sc->sc_lock);
   1470   1.65.14.6     skrll 
   1471   1.65.14.6     skrll 	if (mididevs)
   1472   1.65.14.6     skrll 		kmem_free(mididevs, len);
   1473         1.1  tshiozak }
   1474         1.1  tshiozak 
   1475         1.1  tshiozak static usbd_status
   1476         1.1  tshiozak attach_all_mididevs(struct umidi_softc *sc)
   1477         1.1  tshiozak {
   1478         1.1  tshiozak 	usbd_status err;
   1479         1.1  tshiozak 	int i;
   1480         1.1  tshiozak 
   1481         1.1  tshiozak 	if (sc->sc_mididevs)
   1482        1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++) {
   1483         1.1  tshiozak 			err = attach_mididev(sc, &sc->sc_mididevs[i]);
   1484        1.47       mrg 			if (err != USBD_NORMAL_COMPLETION)
   1485         1.1  tshiozak 				return err;
   1486         1.1  tshiozak 		}
   1487         1.1  tshiozak 
   1488         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1489         1.1  tshiozak }
   1490         1.1  tshiozak 
   1491         1.1  tshiozak static usbd_status
   1492         1.1  tshiozak detach_all_mididevs(struct umidi_softc *sc, int flags)
   1493         1.1  tshiozak {
   1494         1.1  tshiozak 	usbd_status err;
   1495         1.1  tshiozak 	int i;
   1496         1.1  tshiozak 
   1497         1.1  tshiozak 	if (sc->sc_mididevs)
   1498        1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++) {
   1499         1.1  tshiozak 			err = detach_mididev(&sc->sc_mididevs[i], flags);
   1500        1.47       mrg 			if (err != USBD_NORMAL_COMPLETION)
   1501         1.1  tshiozak 				return err;
   1502         1.1  tshiozak 		}
   1503         1.1  tshiozak 
   1504         1.1  tshiozak 	return USBD_NORMAL_COMPLETION;
   1505         1.1  tshiozak }
   1506         1.1  tshiozak 
   1507        1.40    dyoung static void
   1508         1.1  tshiozak deactivate_all_mididevs(struct umidi_softc *sc)
   1509         1.1  tshiozak {
   1510         1.1  tshiozak 	int i;
   1511         1.1  tshiozak 
   1512        1.40    dyoung 	if (sc->sc_mididevs) {
   1513        1.47       mrg 		for (i = 0; i < sc->sc_num_mididevs; i++)
   1514        1.40    dyoung 			deactivate_mididev(&sc->sc_mididevs[i]);
   1515        1.40    dyoung 	}
   1516         1.1  tshiozak }
   1517         1.1  tshiozak 
   1518        1.26      chap /*
   1519        1.26      chap  * TODO: the 0-based cable numbers will often not match the labeling of the
   1520        1.26      chap  * equipment. Ideally:
   1521        1.26      chap  *  For class-compliant devices: get the iJack string from the jack descriptor.
   1522        1.26      chap  *  Otherwise:
   1523        1.26      chap  *  - support a DISPLAY_BASE_CN quirk (add the value to each internal cable
   1524        1.26      chap  *    number for display)
   1525        1.26      chap  *  - support an array quirk explictly giving a char * for each jack.
   1526        1.26      chap  * For now, you get 0-based cable numbers. If there are multiple endpoints and
   1527        1.26      chap  * the CNs are not globally unique, each is shown with its associated endpoint
   1528        1.26      chap  * address in hex also. That should not be necessary when using iJack values
   1529        1.26      chap  * or a quirk array.
   1530        1.26      chap  */
   1531        1.52       mrg void
   1532        1.26      chap describe_mididev(struct umidi_mididev *md)
   1533        1.26      chap {
   1534        1.26      chap 	char in_label[16];
   1535        1.26      chap 	char out_label[16];
   1536        1.35    cegger 	const char *unit_label;
   1537        1.26      chap 	char *final_label;
   1538        1.26      chap 	struct umidi_softc *sc;
   1539        1.26      chap 	int show_ep_in;
   1540        1.26      chap 	int show_ep_out;
   1541        1.26      chap 	size_t len;
   1542   1.65.14.1     skrll 
   1543        1.26      chap 	sc = md->sc;
   1544        1.26      chap 	show_ep_in  = sc-> sc_in_num_endpoints > 1 && !sc->cblnums_global;
   1545        1.26      chap 	show_ep_out = sc->sc_out_num_endpoints > 1 && !sc->cblnums_global;
   1546   1.65.14.1     skrll 
   1547   1.65.14.6     skrll 	if (NULL == md->in_jack)
   1548        1.43     joerg 		in_label[0] = '\0';
   1549   1.65.14.6     skrll 	else if (show_ep_in)
   1550   1.65.14.7     skrll 		snprintf(in_label, sizeof(in_label), "<%d(%x) ",
   1551        1.43     joerg 		    md->in_jack->cable_number, md->in_jack->endpoint->addr);
   1552        1.26      chap 	else
   1553   1.65.14.7     skrll 		snprintf(in_label, sizeof(in_label), "<%d ",
   1554        1.43     joerg 		    md->in_jack->cable_number);
   1555   1.65.14.1     skrll 
   1556   1.65.14.6     skrll 	if (NULL == md->out_jack)
   1557        1.43     joerg 		out_label[0] = '\0';
   1558   1.65.14.6     skrll 	else if (show_ep_out)
   1559   1.65.14.7     skrll 		snprintf(out_label, sizeof(out_label), ">%d(%x) ",
   1560        1.43     joerg 		    md->out_jack->cable_number, md->out_jack->endpoint->addr);
   1561        1.26      chap 	else
   1562   1.65.14.7     skrll 		snprintf(out_label, sizeof(out_label), ">%d ",
   1563        1.43     joerg 		    md->out_jack->cable_number);
   1564        1.26      chap 
   1565        1.42    dyoung 	unit_label = device_xname(sc->sc_dev);
   1566   1.65.14.1     skrll 
   1567        1.26      chap 	len = strlen(in_label) + strlen(out_label) + strlen(unit_label) + 4;
   1568   1.65.14.1     skrll 
   1569        1.52       mrg 	final_label = kmem_alloc(len, KM_SLEEP);
   1570   1.65.14.1     skrll 
   1571        1.26      chap 	snprintf(final_label, len, "%s%son %s",
   1572        1.26      chap 	    in_label, out_label, unit_label);
   1573        1.26      chap 
   1574        1.52       mrg 	md->label = final_label;
   1575        1.52       mrg 	md->label_len = len;
   1576        1.26      chap }
   1577        1.26      chap 
   1578         1.1  tshiozak #ifdef UMIDI_DEBUG
   1579         1.1  tshiozak static void
   1580         1.1  tshiozak dump_sc(struct umidi_softc *sc)
   1581         1.1  tshiozak {
   1582         1.1  tshiozak 	int i;
   1583         1.1  tshiozak 
   1584        1.42    dyoung 	DPRINTFN(10, ("%s: dump_sc\n", device_xname(sc->sc_dev)));
   1585         1.1  tshiozak 	for (i=0; i<sc->sc_out_num_endpoints; i++) {
   1586         1.1  tshiozak 		DPRINTFN(10, ("\tout_ep(%p):\n", &sc->sc_out_ep[i]));
   1587         1.1  tshiozak 		dump_ep(&sc->sc_out_ep[i]);
   1588         1.1  tshiozak 	}
   1589         1.1  tshiozak 	for (i=0; i<sc->sc_in_num_endpoints; i++) {
   1590         1.1  tshiozak 		DPRINTFN(10, ("\tin_ep(%p):\n", &sc->sc_in_ep[i]));
   1591         1.1  tshiozak 		dump_ep(&sc->sc_in_ep[i]);
   1592         1.1  tshiozak 	}
   1593         1.1  tshiozak }
   1594         1.1  tshiozak 
   1595         1.1  tshiozak static void
   1596         1.1  tshiozak dump_ep(struct umidi_endpoint *ep)
   1597         1.1  tshiozak {
   1598         1.1  tshiozak 	int i;
   1599        1.26      chap 	for (i=0; i<UMIDI_MAX_EPJACKS; i++) {
   1600        1.26      chap 		if (NULL==ep->jacks[i])
   1601        1.26      chap 			continue;
   1602        1.26      chap 		DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i, ep->jacks[i]));
   1603         1.1  tshiozak 		dump_jack(ep->jacks[i]);
   1604         1.1  tshiozak 	}
   1605         1.1  tshiozak }
   1606         1.1  tshiozak static void
   1607         1.1  tshiozak dump_jack(struct umidi_jack *jack)
   1608         1.1  tshiozak {
   1609        1.15  tshiozak 	DPRINTFN(10, ("\t\t\tep=%p\n",
   1610        1.15  tshiozak 		      jack->endpoint));
   1611         1.1  tshiozak }
   1612         1.1  tshiozak 
   1613         1.1  tshiozak #endif /* UMIDI_DEBUG */
   1614         1.1  tshiozak 
   1615         1.1  tshiozak 
   1616         1.1  tshiozak 
   1617         1.1  tshiozak /*
   1618         1.1  tshiozak  * MUX MIDI PACKET
   1619         1.1  tshiozak  */
   1620         1.1  tshiozak 
   1621         1.5  jdolecek static const int packet_length[16] = {
   1622         1.1  tshiozak 	/*0*/	-1,
   1623         1.1  tshiozak 	/*1*/	-1,
   1624         1.1  tshiozak 	/*2*/	2,
   1625         1.1  tshiozak 	/*3*/	3,
   1626         1.1  tshiozak 	/*4*/	3,
   1627         1.1  tshiozak 	/*5*/	1,
   1628         1.1  tshiozak 	/*6*/	2,
   1629         1.1  tshiozak 	/*7*/	3,
   1630         1.1  tshiozak 	/*8*/	3,
   1631         1.1  tshiozak 	/*9*/	3,
   1632         1.1  tshiozak 	/*A*/	3,
   1633         1.1  tshiozak 	/*B*/	3,
   1634         1.1  tshiozak 	/*C*/	2,
   1635         1.1  tshiozak 	/*D*/	2,
   1636         1.1  tshiozak 	/*E*/	3,
   1637         1.1  tshiozak 	/*F*/	1,
   1638         1.1  tshiozak };
   1639         1.1  tshiozak 
   1640         1.1  tshiozak #define	GET_CN(p)		(((unsigned char)(p)>>4)&0x0F)
   1641         1.1  tshiozak #define GET_CIN(p)		((unsigned char)(p)&0x0F)
   1642         1.1  tshiozak #define MIX_CN_CIN(cn, cin) \
   1643         1.1  tshiozak 	((unsigned char)((((unsigned char)(cn)&0x0F)<<4)| \
   1644         1.3  tshiozak 			  ((unsigned char)(cin)&0x0F)))
   1645         1.1  tshiozak 
   1646         1.1  tshiozak static usbd_status
   1647         1.1  tshiozak start_input_transfer(struct umidi_endpoint *ep)
   1648         1.1  tshiozak {
   1649   1.65.14.8     skrll 	usbd_setup_xfer(ep->xfer, ep, ep->buffer, ep->buffer_size,
   1650   1.65.14.8     skrll 	    USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, in_intr);
   1651         1.1  tshiozak 	return usbd_transfer(ep->xfer);
   1652         1.1  tshiozak }
   1653         1.1  tshiozak 
   1654         1.1  tshiozak static usbd_status
   1655         1.1  tshiozak start_output_transfer(struct umidi_endpoint *ep)
   1656         1.1  tshiozak {
   1657        1.26      chap 	usbd_status rv;
   1658   1.65.14.1     skrll 	uint32_t length;
   1659        1.26      chap 	int i;
   1660   1.65.14.1     skrll 
   1661   1.65.14.7     skrll 	length = (ep->next_slot - ep->buffer) * sizeof(*ep->buffer);
   1662        1.26      chap 	DPRINTFN(200,("umidi out transfer: start %p end %p length %u\n",
   1663        1.26      chap 	    ep->buffer, ep->next_slot, length));
   1664   1.65.14.8     skrll 
   1665   1.65.14.8     skrll 	usbd_setup_xfer(ep->xfer, ep, ep->buffer, length, 0,
   1666   1.65.14.8     skrll 	    USBD_NO_TIMEOUT, out_intr);
   1667        1.26      chap 	rv = usbd_transfer(ep->xfer);
   1668   1.65.14.1     skrll 
   1669        1.26      chap 	/*
   1670        1.26      chap 	 * Once the transfer is scheduled, no more adding to partial
   1671        1.26      chap 	 * packets within it.
   1672        1.26      chap 	 */
   1673        1.26      chap 	if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1674        1.26      chap 		for (i=0; i<UMIDI_MAX_EPJACKS; ++i)
   1675        1.26      chap 			if (NULL != ep->jacks[i])
   1676        1.26      chap 				ep->jacks[i]->midiman_ppkt = NULL;
   1677        1.26      chap 	}
   1678   1.65.14.1     skrll 
   1679        1.26      chap 	return rv;
   1680         1.1  tshiozak }
   1681         1.1  tshiozak 
   1682         1.4  tshiozak #ifdef UMIDI_DEBUG
   1683         1.4  tshiozak #define DPR_PACKET(dir, sc, p)						\
   1684        1.26      chap if ((unsigned char)(p)[1]!=0xFE)				\
   1685         1.4  tshiozak 	DPRINTFN(500,							\
   1686         1.4  tshiozak 		 ("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n",	\
   1687        1.42    dyoung 		  device_xname(sc->sc_dev),				\
   1688        1.26      chap 		  (unsigned char)(p)[0],			\
   1689        1.26      chap 		  (unsigned char)(p)[1],			\
   1690        1.26      chap 		  (unsigned char)(p)[2],			\
   1691        1.26      chap 		  (unsigned char)(p)[3]));
   1692         1.4  tshiozak #else
   1693         1.4  tshiozak #define DPR_PACKET(dir, sc, p)
   1694         1.4  tshiozak #endif
   1695         1.4  tshiozak 
   1696        1.26      chap /*
   1697        1.26      chap  * A 4-byte Midiman packet superficially resembles a 4-byte USB MIDI packet
   1698        1.26      chap  * with the cable number and length in the last byte instead of the first,
   1699        1.26      chap  * but there the resemblance ends. Where a USB MIDI packet is a semantic
   1700        1.26      chap  * unit, a Midiman packet is just a wrapper for 1 to 3 bytes of raw MIDI
   1701        1.26      chap  * with a cable nybble and a length nybble (which, unlike the CIN of a
   1702        1.26      chap  * real USB MIDI packet, has no semantics at all besides the length).
   1703        1.26      chap  * A packet received from a Midiman may contain part of a MIDI message,
   1704        1.26      chap  * more than one MIDI message, or parts of more than one MIDI message. A
   1705        1.26      chap  * three-byte MIDI message may arrive in three packets of data length 1, and
   1706        1.26      chap  * running status may be used. Happily, the midi(4) driver above us will put
   1707        1.26      chap  * it all back together, so the only cost is in USB bandwidth. The device
   1708        1.26      chap  * has an easier time with what it receives from us: we'll pack messages in
   1709        1.26      chap  * and across packets, but filling the packets whenever possible and,
   1710        1.26      chap  * as midi(4) hands us a complete message at a time, we'll never send one
   1711        1.26      chap  * in a dribble of short packets.
   1712        1.26      chap  */
   1713        1.26      chap 
   1714         1.4  tshiozak static int
   1715        1.26      chap out_jack_output(struct umidi_jack *out_jack, u_char *src, int len, int cin)
   1716         1.4  tshiozak {
   1717         1.4  tshiozak 	struct umidi_endpoint *ep = out_jack->endpoint;
   1718         1.4  tshiozak 	struct umidi_softc *sc = ep->sc;
   1719        1.26      chap 	unsigned char *packet;
   1720        1.26      chap 	int plen;
   1721        1.26      chap 	int poff;
   1722         1.4  tshiozak 
   1723   1.65.14.6     skrll 	KASSERT(mutex_owned(&sc->sc_lock));
   1724   1.65.14.6     skrll 
   1725         1.4  tshiozak 	if (sc->sc_dying)
   1726         1.4  tshiozak 		return EIO;
   1727         1.4  tshiozak 
   1728        1.26      chap 	if (!out_jack->opened)
   1729        1.26      chap 		return ENODEV; /* XXX as it was, is this the right errno? */
   1730         1.4  tshiozak 
   1731   1.65.14.6     skrll 	sc->sc_refcnt++;
   1732   1.65.14.6     skrll 
   1733        1.26      chap #ifdef UMIDI_DEBUG
   1734   1.65.14.6     skrll 	if (umididebug >= 100)
   1735        1.26      chap 		microtime(&umidi_tv);
   1736        1.26      chap #endif
   1737  1.65.14.12     skrll 	DPRINTFN(100, ("umidi out: %"PRIu64".%06"PRIu64
   1738  1.65.14.12     skrll 	    "s ep=%p cn=%d len=%d cin=%#x\n", umidi_tv.tv_sec%100,
   1739  1.65.14.12     skrll 	    (uint64_t)umidi_tv.tv_usec, ep, out_jack->cable_number, len, cin));
   1740   1.65.14.1     skrll 
   1741        1.26      chap 	packet = *ep->next_slot++;
   1742        1.26      chap 	KASSERT(ep->buffer_size >=
   1743   1.65.14.7     skrll 	    (ep->next_slot - ep->buffer) * sizeof(*ep->buffer));
   1744        1.26      chap 	memset(packet, 0, UMIDI_PACKET_SIZE);
   1745        1.26      chap 	if (UMQ_ISTYPE(sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1746        1.26      chap 		if (NULL != out_jack->midiman_ppkt) { /* fill out a prev pkt */
   1747        1.26      chap 			poff = 0x0f & (out_jack->midiman_ppkt[3]);
   1748        1.26      chap 			plen = 3 - poff;
   1749        1.26      chap 			if (plen > len)
   1750        1.26      chap 				plen = len;
   1751        1.26      chap 			memcpy(out_jack->midiman_ppkt+poff, src, plen);
   1752        1.26      chap 			src += plen;
   1753        1.26      chap 			len -= plen;
   1754        1.26      chap 			plen += poff;
   1755        1.26      chap 			out_jack->midiman_ppkt[3] =
   1756        1.26      chap 			    MIX_CN_CIN(out_jack->cable_number, plen);
   1757        1.26      chap 			DPR_PACKET(out+, sc, out_jack->midiman_ppkt);
   1758        1.26      chap 			if (3 == plen)
   1759        1.26      chap 				out_jack->midiman_ppkt = NULL; /* no more */
   1760        1.26      chap 		}
   1761        1.26      chap 		if (0 == len)
   1762        1.26      chap 			ep->next_slot--; /* won't be needed, nevermind */
   1763        1.26      chap 		else {
   1764        1.26      chap 			memcpy(packet, src, len);
   1765        1.26      chap 			packet[3] = MIX_CN_CIN(out_jack->cable_number, len);
   1766        1.26      chap 			DPR_PACKET(out, sc, packet);
   1767        1.26      chap 			if (len < 3)
   1768        1.26      chap 				out_jack->midiman_ppkt = packet;
   1769        1.26      chap 		}
   1770        1.26      chap 	} else { /* the nice simple USB class-compliant case */
   1771        1.26      chap 		packet[0] = MIX_CN_CIN(out_jack->cable_number, cin);
   1772        1.26      chap 		memcpy(packet+1, src, len);
   1773        1.26      chap 		DPR_PACKET(out, sc, packet);
   1774        1.26      chap 	}
   1775        1.26      chap 	ep->next_schedule |= 1<<(out_jack->cable_number);
   1776        1.26      chap 	++ ep->num_scheduled;
   1777   1.65.14.6     skrll 	if (!ep->armed && !ep->soliciting) {
   1778        1.26      chap 		/*
   1779        1.26      chap 		 * It would be bad to call out_solicit directly here (the
   1780        1.26      chap 		 * caller need not be reentrant) but a soft interrupt allows
   1781        1.26      chap 		 * solicit to run immediately the caller exits its critical
   1782        1.26      chap 		 * section, and if the caller has more to write we can get it
   1783        1.26      chap 		 * before starting the USB transfer, and send a longer one.
   1784        1.26      chap 		 */
   1785        1.26      chap 		ep->soliciting = 1;
   1786  1.65.14.11     skrll 		kpreempt_disable();
   1787        1.32        ad 		softint_schedule(ep->solicit_cookie);
   1788  1.65.14.11     skrll 		kpreempt_enable();
   1789        1.26      chap 	}
   1790   1.65.14.1     skrll 
   1791   1.65.14.6     skrll 	if (--sc->sc_refcnt < 0)
   1792  1.65.14.13     skrll 		cv_broadcast(&sc->sc_detach_cv);
   1793   1.65.14.6     skrll 
   1794        1.26      chap 	return 0;
   1795         1.4  tshiozak }
   1796         1.4  tshiozak 
   1797         1.1  tshiozak static void
   1798   1.65.14.4     skrll in_intr(struct usbd_xfer *xfer, void *priv,
   1799        1.28  christos     usbd_status status)
   1800         1.1  tshiozak {
   1801         1.4  tshiozak 	int cn, len, i;
   1802         1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1803        1.45  jmcneill 	struct umidi_softc *sc = ep->sc;
   1804         1.4  tshiozak 	struct umidi_jack *jack;
   1805        1.26      chap 	unsigned char *packet;
   1806        1.26      chap 	umidi_packet_bufp slot;
   1807        1.26      chap 	umidi_packet_bufp end;
   1808        1.26      chap 	unsigned char *data;
   1809   1.65.14.1     skrll 	uint32_t count;
   1810         1.1  tshiozak 
   1811         1.3  tshiozak 	if (ep->sc->sc_dying || !ep->num_open)
   1812         1.1  tshiozak 		return;
   1813         1.1  tshiozak 
   1814        1.47       mrg 	mutex_enter(&sc->sc_lock);
   1815        1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1816   1.65.14.2     skrll 	if (0 == count % UMIDI_PACKET_SIZE) {
   1817        1.26      chap 		DPRINTFN(200,("%s: input endpoint %p transfer length %u\n",
   1818        1.42    dyoung 			     device_xname(ep->sc->sc_dev), ep, count));
   1819   1.65.14.2     skrll 	} else {
   1820   1.65.14.2     skrll 		DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
   1821   1.65.14.2     skrll 			device_xname(ep->sc->sc_dev), ep, count));
   1822   1.65.14.2     skrll 	}
   1823   1.65.14.1     skrll 
   1824        1.26      chap 	slot = ep->buffer;
   1825   1.65.14.7     skrll 	end = slot + count / sizeof(*slot);
   1826        1.26      chap 
   1827        1.47       mrg 	for (packet = *slot; slot < end; packet = *++slot) {
   1828   1.65.14.1     skrll 
   1829        1.47       mrg 		if (UMQ_ISTYPE(ep->sc, UMQ_TYPE_MIDIMAN_GARBLE)) {
   1830        1.26      chap 			cn = (0xf0&(packet[3]))>>4;
   1831        1.26      chap 			len = 0x0f&(packet[3]);
   1832        1.26      chap 			data = packet;
   1833        1.26      chap 		} else {
   1834        1.26      chap 			cn = GET_CN(packet[0]);
   1835        1.26      chap 			len = packet_length[GET_CIN(packet[0])];
   1836        1.26      chap 			data = packet + 1;
   1837        1.26      chap 		}
   1838        1.26      chap 		/* 0 <= cn <= 15 by inspection of above code */
   1839        1.26      chap 		if (!(jack = ep->jacks[cn]) || cn != jack->cable_number) {
   1840        1.26      chap 			DPRINTF(("%s: stray input endpoint %p cable %d len %d: "
   1841   1.65.14.2     skrll 				 "%02X %02X %02X (try CN_SEQ quirk?)\n",
   1842        1.42    dyoung 				 device_xname(ep->sc->sc_dev), ep, cn, len,
   1843        1.26      chap 				 (unsigned)data[0],
   1844        1.26      chap 				 (unsigned)data[1],
   1845        1.26      chap 				 (unsigned)data[2]));
   1846        1.47       mrg 			mutex_exit(&sc->sc_lock);
   1847        1.26      chap 			return;
   1848        1.26      chap 		}
   1849        1.26      chap 
   1850   1.65.14.6     skrll 		if (!jack->bound || !jack->opened)
   1851        1.26      chap 			continue;
   1852        1.26      chap 
   1853        1.26      chap 		DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
   1854   1.65.14.2     skrll 			     "%02X %02X %02X\n",
   1855        1.42    dyoung 			     device_xname(ep->sc->sc_dev), ep, cn, len,
   1856        1.26      chap 			     (unsigned)data[0],
   1857        1.26      chap 			     (unsigned)data[1],
   1858        1.26      chap 			     (unsigned)data[2]));
   1859        1.26      chap 
   1860        1.26      chap 		if (jack->u.in.intr) {
   1861        1.47       mrg 			for (i = 0; i < len; i++) {
   1862        1.26      chap 				(*jack->u.in.intr)(jack->arg, data[i]);
   1863        1.26      chap 			}
   1864         1.4  tshiozak 		}
   1865        1.26      chap 
   1866         1.4  tshiozak 	}
   1867         1.1  tshiozak 
   1868         1.1  tshiozak 	(void)start_input_transfer(ep);
   1869        1.47       mrg 	mutex_exit(&sc->sc_lock);
   1870         1.1  tshiozak }
   1871         1.1  tshiozak 
   1872         1.1  tshiozak static void
   1873   1.65.14.4     skrll out_intr(struct usbd_xfer *xfer, void *priv,
   1874        1.28  christos     usbd_status status)
   1875         1.1  tshiozak {
   1876         1.1  tshiozak 	struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
   1877         1.1  tshiozak 	struct umidi_softc *sc = ep->sc;
   1878   1.65.14.1     skrll 	uint32_t count;
   1879         1.1  tshiozak 
   1880        1.26      chap 	if (sc->sc_dying)
   1881         1.1  tshiozak 		return;
   1882         1.1  tshiozak 
   1883        1.47       mrg 	mutex_enter(&sc->sc_lock);
   1884        1.26      chap #ifdef UMIDI_DEBUG
   1885   1.65.14.6     skrll 	if (umididebug >= 200)
   1886        1.26      chap 		microtime(&umidi_tv);
   1887        1.26      chap #endif
   1888        1.26      chap 	usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
   1889   1.65.14.6     skrll 	if (0 == count % UMIDI_PACKET_SIZE) {
   1890  1.65.14.12     skrll 		DPRINTFN(200, ("%s: %"PRIu64".%06"PRIu64"s out ep %p xfer "
   1891  1.65.14.12     skrll 		    "length %u\n", device_xname(ep->sc->sc_dev),
   1892  1.65.14.12     skrll 		    umidi_tv.tv_sec%100, (uint64_t)umidi_tv.tv_usec, ep,
   1893  1.65.14.12     skrll 		    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