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