1 /* $NetBSD: umidi.c,v 1.39 2009/01/11 11:06:08 cegger Exp $ */
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Takuya SHIOZAKI (tshiozak@NetBSD.org) and (full-size transfers, extended
8 * hw_if) Chapman Flack (chap@NetBSD.org).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: umidi.c,v 1.39 2009/01/11 11:06:08 cegger Exp $");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/device.h>
41 #include <sys/ioctl.h>
44 #include <sys/select.h>
46 #include <sys/vnode.h>
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
54 #include <dev/usb/usbdevs.h>
55 #include <dev/usb/uaudioreg.h>
56 #include <dev/usb/umidireg.h>
57 #include <dev/usb/umidivar.h>
58 #include <dev/usb/umidi_quirks.h>
60 #include <dev/midi_if.h>
63 #define DPRINTF(x) if (umididebug) printf x
64 #define DPRINTFN(n,x) if (umididebug >= (n)) printf x
66 static struct timeval umidi_tv
;
74 static int umidi_open(void *, int,
75 void (*)(void *, int), void (*)(void *), void *);
76 static void umidi_close(void *);
77 static int umidi_channelmsg(void *, int, int, u_char
*, int);
78 static int umidi_commonmsg(void *, int, u_char
*, int);
79 static int umidi_sysex(void *, u_char
*, int);
80 static int umidi_rtmsg(void *, int);
81 static void umidi_getinfo(void *, struct midi_info
*);
83 static usbd_status
alloc_pipe(struct umidi_endpoint
*);
84 static void free_pipe(struct umidi_endpoint
*);
86 static usbd_status
alloc_all_endpoints(struct umidi_softc
*);
87 static void free_all_endpoints(struct umidi_softc
*);
89 static usbd_status
alloc_all_jacks(struct umidi_softc
*);
90 static void free_all_jacks(struct umidi_softc
*);
91 static usbd_status
bind_jacks_to_mididev(struct umidi_softc
*,
94 struct umidi_mididev
*);
95 static void unbind_jacks_from_mididev(struct umidi_mididev
*);
96 static void unbind_all_jacks(struct umidi_softc
*);
97 static usbd_status
assign_all_jacks_automatically(struct umidi_softc
*);
98 static usbd_status
open_out_jack(struct umidi_jack
*, void *,
100 static usbd_status
open_in_jack(struct umidi_jack
*, void *,
101 void (*)(void *, int));
102 static void close_out_jack(struct umidi_jack
*);
103 static void close_in_jack(struct umidi_jack
*);
105 static usbd_status
attach_mididev(struct umidi_softc
*, struct umidi_mididev
*);
106 static usbd_status
detach_mididev(struct umidi_mididev
*, int);
107 static void deactivate_mididev(struct umidi_mididev
*);
108 static usbd_status
alloc_all_mididevs(struct umidi_softc
*, int);
109 static void free_all_mididevs(struct umidi_softc
*);
110 static usbd_status
attach_all_mididevs(struct umidi_softc
*);
111 static usbd_status
detach_all_mididevs(struct umidi_softc
*, int);
112 static void deactivate_all_mididevs(struct umidi_softc
*);
113 static char *describe_mididev(struct umidi_mididev
*);
116 static void dump_sc(struct umidi_softc
*);
117 static void dump_ep(struct umidi_endpoint
*);
118 static void dump_jack(struct umidi_jack
*);
121 static usbd_status
start_input_transfer(struct umidi_endpoint
*);
122 static usbd_status
start_output_transfer(struct umidi_endpoint
*);
123 static int out_jack_output(struct umidi_jack
*, u_char
*, int, int);
124 static void in_intr(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
125 static void out_intr(usbd_xfer_handle
, usbd_private_handle
, usbd_status
);
126 static void out_solicit(void *); /* struct umidi_endpoint* for softintr */
129 const struct midi_hw_if umidi_hw_if
= {
137 struct midi_hw_if_ext umidi_hw_if_ext
= {
138 .channel
= umidi_channelmsg
,
139 .common
= umidi_commonmsg
,
140 .sysex
= umidi_sysex
,
143 struct midi_hw_if_ext umidi_hw_if_mm
= {
144 .channel
= umidi_channelmsg
,
145 .common
= umidi_commonmsg
,
146 .sysex
= umidi_sysex
,
150 int umidi_match(device_t
, cfdata_t
, void *);
151 void umidi_attach(device_t
, device_t
, void *);
152 void umidi_childdet(device_t
, device_t
);
153 int umidi_detach(device_t
, int);
154 int umidi_activate(device_t
, enum devact
);
155 extern struct cfdriver umidi_cd
;
156 CFATTACH_DECL2_NEW(umidi
, sizeof(struct umidi_softc
), umidi_match
,
157 umidi_attach
, umidi_detach
, umidi_activate
, NULL
, umidi_childdet
);
161 USB_IFMATCH_START(umidi
, uaa
);
163 DPRINTFN(1,("umidi_match\n"));
165 if (umidi_search_quirk(uaa
->vendor
, uaa
->product
, uaa
->ifaceno
))
166 return UMATCH_IFACECLASS_IFACESUBCLASS
;
168 if (uaa
->class == UICLASS_AUDIO
&&
169 uaa
->subclass
== UISUBCLASS_MIDISTREAM
)
170 return UMATCH_IFACECLASS_IFACESUBCLASS
;
178 USB_IFATTACH_START(umidi
, sc
, uaa
);
181 DPRINTFN(1,("umidi_attach\n"));
185 devinfop
= usbd_devinfo_alloc(uaa
->device
, 0);
186 aprint_normal("%s\n", devinfop
);
187 usbd_devinfo_free(devinfop
);
189 sc
->sc_iface
= uaa
->iface
;
190 sc
->sc_udev
= uaa
->device
;
193 umidi_search_quirk(uaa
->vendor
, uaa
->product
, uaa
->ifaceno
);
194 aprint_normal_dev(self
, "");
195 umidi_print_quirk(sc
->sc_quirk
);
198 err
= alloc_all_endpoints(sc
);
199 if (err
!=USBD_NORMAL_COMPLETION
) {
200 aprint_error_dev(self
,
201 "alloc_all_endpoints failed. (err=%d)\n", err
);
204 err
= alloc_all_jacks(sc
);
205 if (err
!=USBD_NORMAL_COMPLETION
) {
206 free_all_endpoints(sc
);
207 aprint_error_dev(self
, "alloc_all_jacks failed. (err=%d)\n",
211 aprint_normal_dev(self
, "out=%d, in=%d\n",
212 sc
->sc_out_num_jacks
, sc
->sc_in_num_jacks
);
214 err
= assign_all_jacks_automatically(sc
);
215 if (err
!=USBD_NORMAL_COMPLETION
) {
216 unbind_all_jacks(sc
);
218 free_all_endpoints(sc
);
219 aprint_error_dev(self
,
220 "assign_all_jacks_automatically failed. (err=%d)\n", err
);
223 err
= attach_all_mididevs(sc
);
224 if (err
!=USBD_NORMAL_COMPLETION
) {
226 free_all_endpoints(sc
);
227 aprint_error_dev(self
,
228 "attach_all_mididevs failed. (err=%d)\n", err
);
235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH
,
236 sc
->sc_udev
, USBDEV(sc
->sc_dev
));
238 USB_ATTACH_SUCCESS_RETURN
;
240 aprint_error_dev(self
, "disabled.\n");
242 USB_ATTACH_ERROR_RETURN
;
246 umidi_childdet(device_t self
, device_t child
)
249 struct umidi_softc
*sc
= device_private(self
);
251 KASSERT(sc
->sc_mididevs
!= NULL
);
253 for (i
= 0; i
< sc
->sc_num_mididevs
; i
++) {
254 if (sc
->sc_mididevs
[i
].mdev
== child
)
257 KASSERT(i
< sc
->sc_num_mididevs
);
258 sc
->sc_mididevs
[i
].mdev
= NULL
;
262 umidi_activate(device_t self
, enum devact act
)
264 struct umidi_softc
*sc
= device_private(self
);
267 case DVACT_DEACTIVATE
:
268 DPRINTFN(1,("umidi_activate (deactivate)\n"));
270 deactivate_all_mididevs(sc
);
273 DPRINTFN(1,("umidi_activate (%d)\n", act
));
280 USB_DETACH_START(umidi
, sc
);
282 DPRINTFN(1,("umidi_detach\n"));
285 detach_all_mididevs(sc
, flags
);
286 free_all_mididevs(sc
);
288 free_all_endpoints(sc
);
290 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH
, sc
->sc_udev
,
301 umidi_open(void *addr
,
303 void (*iintr
)(void *, int),
304 void (*ointr
)(void *),
307 struct umidi_mididev
*mididev
= addr
;
308 struct umidi_softc
*sc
= mididev
->sc
;
311 DPRINTF(("umidi_open: sc=%p\n", sc
));
321 mididev
->flags
= flags
;
322 if ((mididev
->flags
& FWRITE
) && mididev
->out_jack
) {
323 err
= open_out_jack(mididev
->out_jack
, arg
, ointr
);
324 if ( err
!= USBD_NORMAL_COMPLETION
)
327 if ((mididev
->flags
& FREAD
) && mididev
->in_jack
) {
328 err
= open_in_jack(mididev
->in_jack
, arg
, iintr
);
329 if ( err
!= USBD_NORMAL_COMPLETION
330 && err
!= USBD_IN_PROGRESS
)
337 DPRINTF(("umidi_open: usbd_status %d\n", err
));
338 return USBD_IN_USE
== err
? EBUSY
: EIO
;
342 umidi_close(void *addr
)
345 struct umidi_mididev
*mididev
= addr
;
348 if ((mididev
->flags
& FWRITE
) && mididev
->out_jack
)
349 close_out_jack(mididev
->out_jack
);
350 if ((mididev
->flags
& FREAD
) && mididev
->in_jack
)
351 close_in_jack(mididev
->in_jack
);
357 umidi_channelmsg(void *addr
, int status
, int channel
, u_char
*msg
,
360 struct umidi_mididev
*mididev
= addr
;
362 if (!mididev
->out_jack
|| !mididev
->opened
)
365 return out_jack_output(mididev
->out_jack
, msg
, len
, (status
>>4)&0xf);
369 umidi_commonmsg(void *addr
, int status
, u_char
*msg
, int len
)
371 struct umidi_mididev
*mididev
= addr
;
374 if (!mididev
->out_jack
|| !mididev
->opened
)
378 case 1: cin
= 5; break;
379 case 2: cin
= 2; break;
380 case 3: cin
= 3; break;
381 default: return EIO
; /* or gcc warns of cin uninitialized */
384 return out_jack_output(mididev
->out_jack
, msg
, len
, cin
);
388 umidi_sysex(void *addr
, u_char
*msg
, int len
)
390 struct umidi_mididev
*mididev
= addr
;
393 if (!mididev
->out_jack
|| !mididev
->opened
)
397 case 1: cin
= 5; break;
398 case 2: cin
= 6; break;
399 case 3: cin
= (msg
[2] == 0xf7) ? 7 : 4; break;
400 default: return EIO
; /* or gcc warns of cin uninitialized */
403 return out_jack_output(mididev
->out_jack
, msg
, len
, cin
);
407 umidi_rtmsg(void *addr
, int d
)
409 struct umidi_mididev
*mididev
= addr
;
412 if (!mididev
->out_jack
|| !mididev
->opened
)
415 return out_jack_output(mididev
->out_jack
, &msg
, 1, 0xf);
419 umidi_getinfo(void *addr
, struct midi_info
*mi
)
421 struct umidi_mididev
*mididev
= addr
;
422 struct umidi_softc
*sc
= mididev
->sc
;
423 int mm
= UMQ_ISTYPE(sc
, UMQ_TYPE_MIDIMAN_GARBLE
);
425 mi
->name
= mididev
->label
;
426 mi
->props
= MIDI_PROP_OUT_INTR
;
427 if (mididev
->in_jack
)
428 mi
->props
|= MIDI_PROP_CAN_INPUT
;
429 midi_register_hw_if_ext(mm
? &umidi_hw_if_mm
: &umidi_hw_if_ext
);
434 * each endpoint stuffs
437 /* alloc/free pipe */
439 alloc_pipe(struct umidi_endpoint
*ep
)
441 struct umidi_softc
*sc
= ep
->sc
;
443 usb_endpoint_descriptor_t
*epd
;
445 epd
= usbd_get_endpoint_descriptor(sc
->sc_iface
, ep
->addr
);
447 * For output, an improvement would be to have a buffer bigger than
448 * wMaxPacketSize by num_jacks-1 additional packet slots; that would
449 * allow out_solicit to fill the buffer to the full packet size in
450 * all cases. But to use usbd_alloc_buffer to get a slightly larger
451 * buffer would not be a good way to do that, because if the addition
452 * would make the buffer exceed USB_MEM_SMALL then a substantially
453 * larger block may be wastefully allocated. Some flavor of double
454 * buffering could serve the same purpose, but would increase the
455 * code complexity, so for now I will live with the current slight
456 * penalty of reducing max transfer size by (num_open-num_scheduled)
459 ep
->buffer_size
= UGETW(epd
->wMaxPacketSize
);
460 ep
->buffer_size
-= ep
->buffer_size
% UMIDI_PACKET_SIZE
;
462 DPRINTF(("%s: alloc_pipe %p, buffer size %u\n",
463 USBDEVNAME(sc
->sc_dev
), ep
, ep
->buffer_size
));
464 ep
->num_scheduled
= 0;
465 ep
->this_schedule
= 0;
466 ep
->next_schedule
= 0;
469 ep
->xfer
= usbd_alloc_xfer(sc
->sc_udev
);
470 if (ep
->xfer
== NULL
) {
474 ep
->buffer
= usbd_alloc_buffer(ep
->xfer
, ep
->buffer_size
);
475 if (ep
->buffer
== NULL
) {
476 usbd_free_xfer(ep
->xfer
);
480 ep
->next_slot
= ep
->buffer
;
481 err
= usbd_open_pipe(sc
->sc_iface
, ep
->addr
, 0, &ep
->pipe
);
483 usbd_free_xfer(ep
->xfer
);
484 ep
->solicit_cookie
= softint_establish(SOFTINT_CLOCK
, out_solicit
, ep
);
490 free_pipe(struct umidi_endpoint
*ep
)
492 DPRINTF(("%s: free_pipe %p\n", USBDEVNAME(ep
->sc
->sc_dev
), ep
));
493 usbd_abort_pipe(ep
->pipe
);
494 usbd_close_pipe(ep
->pipe
);
495 usbd_free_xfer(ep
->xfer
);
496 softint_disestablish(ep
->solicit_cookie
);
500 /* alloc/free the array of endpoint structures */
502 static usbd_status
alloc_all_endpoints_fixed_ep(struct umidi_softc
*);
503 static usbd_status
alloc_all_endpoints_yamaha(struct umidi_softc
*);
504 static usbd_status
alloc_all_endpoints_genuine(struct umidi_softc
*);
507 alloc_all_endpoints(struct umidi_softc
*sc
)
510 struct umidi_endpoint
*ep
;
513 if (UMQ_ISTYPE(sc
, UMQ_TYPE_FIXED_EP
)) {
514 err
= alloc_all_endpoints_fixed_ep(sc
);
515 } else if (UMQ_ISTYPE(sc
, UMQ_TYPE_YAMAHA
)) {
516 err
= alloc_all_endpoints_yamaha(sc
);
518 err
= alloc_all_endpoints_genuine(sc
);
520 if (err
!=USBD_NORMAL_COMPLETION
)
523 ep
= sc
->sc_endpoints
;
524 for (i
=sc
->sc_out_num_endpoints
+sc
->sc_in_num_endpoints
; i
>0; i
--) {
525 err
= alloc_pipe(ep
++);
526 if (err
!=USBD_NORMAL_COMPLETION
) {
527 for (; ep
!=sc
->sc_endpoints
; ep
--)
529 free(sc
->sc_endpoints
, M_USBDEV
);
530 sc
->sc_endpoints
= sc
->sc_out_ep
= sc
->sc_in_ep
= NULL
;
538 free_all_endpoints(struct umidi_softc
*sc
)
541 for (i
=0; i
<sc
->sc_in_num_endpoints
+sc
->sc_out_num_endpoints
; i
++)
542 free_pipe(&sc
->sc_endpoints
[i
]);
543 if (sc
->sc_endpoints
!= NULL
)
544 free(sc
->sc_endpoints
, M_USBDEV
);
545 sc
->sc_endpoints
= sc
->sc_out_ep
= sc
->sc_in_ep
= NULL
;
549 alloc_all_endpoints_fixed_ep(struct umidi_softc
*sc
)
552 const struct umq_fixed_ep_desc
*fp
;
553 struct umidi_endpoint
*ep
;
554 usb_endpoint_descriptor_t
*epd
;
557 fp
= umidi_get_quirk_data_from_type(sc
->sc_quirk
,
559 sc
->sc_out_num_jacks
= 0;
560 sc
->sc_in_num_jacks
= 0;
561 sc
->sc_out_num_endpoints
= fp
->num_out_ep
;
562 sc
->sc_in_num_endpoints
= fp
->num_in_ep
;
563 sc
->sc_endpoints
= malloc(sizeof(*sc
->sc_out_ep
)*
564 (sc
->sc_out_num_endpoints
+
565 sc
->sc_in_num_endpoints
),
567 if (!sc
->sc_endpoints
) {
570 sc
->sc_out_ep
= sc
->sc_out_num_endpoints
? sc
->sc_endpoints
: NULL
;
572 sc
->sc_in_num_endpoints
?
573 sc
->sc_endpoints
+sc
->sc_out_num_endpoints
: NULL
;
575 ep
= &sc
->sc_out_ep
[0];
576 for (i
=0; i
<sc
->sc_out_num_endpoints
; i
++) {
577 epd
= usbd_interface2endpoint_descriptor(
581 aprint_error_dev(sc
->sc_dev
,
582 "cannot get endpoint descriptor(out:%d)\n",
587 if (UE_GET_XFERTYPE(epd
->bmAttributes
)!=UE_BULK
||
588 UE_GET_DIR(epd
->bEndpointAddress
)!=UE_DIR_OUT
) {
589 aprint_error_dev(sc
->sc_dev
, "illegal endpoint(out:%d)\n",
595 ep
->addr
= epd
->bEndpointAddress
;
596 ep
->num_jacks
= fp
->out_ep
[i
].num_jacks
;
597 sc
->sc_out_num_jacks
+= fp
->out_ep
[i
].num_jacks
;
599 memset(ep
->jacks
, 0, sizeof(ep
->jacks
));
602 ep
= &sc
->sc_in_ep
[0];
603 for (i
=0; i
<sc
->sc_in_num_endpoints
; i
++) {
604 epd
= usbd_interface2endpoint_descriptor(
608 aprint_error_dev(sc
->sc_dev
,
609 "cannot get endpoint descriptor(in:%d)\n",
615 * MIDISPORT_2X4 inputs on an interrupt rather than a bulk
616 * endpoint. The existing input logic in this driver seems
617 * to work successfully if we just stop treating an interrupt
618 * endpoint as illegal (or the in_progress status we get on
619 * the initial transfer). It does not seem necessary to
620 * actually use the interrupt flavor of alloc_pipe or make
621 * other serious rearrangements of logic. I like that.
623 switch ( UE_GET_XFERTYPE(epd
->bmAttributes
) ) {
626 if ( UE_DIR_IN
== UE_GET_DIR(epd
->bEndpointAddress
) )
630 aprint_error_dev(sc
->sc_dev
,
631 "illegal endpoint(in:%d)\n", fp
->in_ep
[i
].ep
);
637 ep
->addr
= epd
->bEndpointAddress
;
638 ep
->num_jacks
= fp
->in_ep
[i
].num_jacks
;
639 sc
->sc_in_num_jacks
+= fp
->in_ep
[i
].num_jacks
;
641 memset(ep
->jacks
, 0, sizeof(ep
->jacks
));
645 return USBD_NORMAL_COMPLETION
;
647 free(sc
->sc_endpoints
, M_USBDEV
);
648 sc
->sc_endpoints
= NULL
;
653 alloc_all_endpoints_yamaha(struct umidi_softc
*sc
)
655 /* This driver currently supports max 1in/1out bulk endpoints */
656 usb_descriptor_t
*desc
;
657 umidi_cs_descriptor_t
*udesc
;
658 usb_endpoint_descriptor_t
*epd
;
659 int out_addr
, in_addr
, i
;
661 size_t remain
, descsize
;
663 sc
->sc_out_num_jacks
= sc
->sc_in_num_jacks
= 0;
664 out_addr
= in_addr
= 0;
666 /* detect endpoints */
667 desc
= TO_D(usbd_get_interface_descriptor(sc
->sc_iface
));
668 for (i
=(int)TO_IFD(desc
)->bNumEndpoints
-1; i
>=0; i
--) {
669 epd
= usbd_interface2endpoint_descriptor(sc
->sc_iface
, i
);
670 KASSERT(epd
!= NULL
);
671 if (UE_GET_XFERTYPE(epd
->bmAttributes
) == UE_BULK
) {
672 dir
= UE_GET_DIR(epd
->bEndpointAddress
);
673 if (dir
==UE_DIR_OUT
&& !out_addr
)
674 out_addr
= epd
->bEndpointAddress
;
675 else if (dir
==UE_DIR_IN
&& !in_addr
)
676 in_addr
= epd
->bEndpointAddress
;
679 udesc
= (umidi_cs_descriptor_t
*)NEXT_D(desc
);
682 if (!(udesc
->bDescriptorType
==UDESC_CS_INTERFACE
&&
683 udesc
->bDescriptorSubtype
==UMIDI_MS_HEADER
))
685 remain
= (size_t)UGETW(TO_CSIFD(udesc
)->wTotalLength
) -
686 (size_t)udesc
->bLength
;
687 udesc
= (umidi_cs_descriptor_t
*)NEXT_D(udesc
);
689 while (remain
>=sizeof(usb_descriptor_t
)) {
690 descsize
= udesc
->bLength
;
691 if (descsize
>remain
|| descsize
==0)
693 if (udesc
->bDescriptorType
==UDESC_CS_INTERFACE
&&
694 remain
>=UMIDI_JACK_DESCRIPTOR_SIZE
) {
695 if (udesc
->bDescriptorSubtype
==UMIDI_OUT_JACK
)
696 sc
->sc_out_num_jacks
++;
697 else if (udesc
->bDescriptorSubtype
==UMIDI_IN_JACK
)
698 sc
->sc_in_num_jacks
++;
700 udesc
= (umidi_cs_descriptor_t
*)NEXT_D(udesc
);
704 /* validate some parameters */
705 if (sc
->sc_out_num_jacks
>UMIDI_MAX_EPJACKS
)
706 sc
->sc_out_num_jacks
= UMIDI_MAX_EPJACKS
;
707 if (sc
->sc_in_num_jacks
>UMIDI_MAX_EPJACKS
)
708 sc
->sc_in_num_jacks
= UMIDI_MAX_EPJACKS
;
709 if (sc
->sc_out_num_jacks
&& out_addr
) {
710 sc
->sc_out_num_endpoints
= 1;
712 sc
->sc_out_num_endpoints
= 0;
713 sc
->sc_out_num_jacks
= 0;
715 if (sc
->sc_in_num_jacks
&& in_addr
) {
716 sc
->sc_in_num_endpoints
= 1;
718 sc
->sc_in_num_endpoints
= 0;
719 sc
->sc_in_num_jacks
= 0;
721 sc
->sc_endpoints
= malloc(sizeof(struct umidi_endpoint
)*
722 (sc
->sc_out_num_endpoints
+
723 sc
->sc_in_num_endpoints
),
725 if (!sc
->sc_endpoints
)
727 if (sc
->sc_out_num_endpoints
) {
728 sc
->sc_out_ep
= sc
->sc_endpoints
;
729 sc
->sc_out_ep
->sc
= sc
;
730 sc
->sc_out_ep
->addr
= out_addr
;
731 sc
->sc_out_ep
->num_jacks
= sc
->sc_out_num_jacks
;
732 sc
->sc_out_ep
->num_open
= 0;
733 memset(sc
->sc_out_ep
->jacks
, 0, sizeof(sc
->sc_out_ep
->jacks
));
735 sc
->sc_out_ep
= NULL
;
737 if (sc
->sc_in_num_endpoints
) {
738 sc
->sc_in_ep
= sc
->sc_endpoints
+sc
->sc_out_num_endpoints
;
739 sc
->sc_in_ep
->sc
= sc
;
740 sc
->sc_in_ep
->addr
= in_addr
;
741 sc
->sc_in_ep
->num_jacks
= sc
->sc_in_num_jacks
;
742 sc
->sc_in_ep
->num_open
= 0;
743 memset(sc
->sc_in_ep
->jacks
, 0, sizeof(sc
->sc_in_ep
->jacks
));
747 return USBD_NORMAL_COMPLETION
;
751 alloc_all_endpoints_genuine(struct umidi_softc
*sc
)
753 usb_interface_descriptor_t
*interface_desc
;
754 usb_config_descriptor_t
*config_desc
;
755 usb_descriptor_t
*desc
;
757 size_t remain
, descsize
;
758 struct umidi_endpoint
*p
, *q
, *lowest
, *endep
, tmpep
;
761 interface_desc
= usbd_get_interface_descriptor(sc
->sc_iface
);
762 num_ep
= interface_desc
->bNumEndpoints
;
763 sc
->sc_endpoints
= p
= malloc(sizeof(struct umidi_endpoint
) * num_ep
,
768 sc
->sc_out_num_jacks
= sc
->sc_in_num_jacks
= 0;
769 sc
->sc_out_num_endpoints
= sc
->sc_in_num_endpoints
= 0;
772 /* get the list of endpoints for midi stream */
773 config_desc
= usbd_get_config_descriptor(sc
->sc_udev
);
774 desc
= (usb_descriptor_t
*) config_desc
;
775 remain
= (size_t)UGETW(config_desc
->wTotalLength
);
776 while (remain
>=sizeof(usb_descriptor_t
)) {
777 descsize
= desc
->bLength
;
778 if (descsize
>remain
|| descsize
==0)
780 if (desc
->bDescriptorType
==UDESC_ENDPOINT
&&
781 remain
>=USB_ENDPOINT_DESCRIPTOR_SIZE
&&
782 UE_GET_XFERTYPE(TO_EPD(desc
)->bmAttributes
) == UE_BULK
) {
783 epaddr
= TO_EPD(desc
)->bEndpointAddress
;
784 } else if (desc
->bDescriptorType
==UDESC_CS_ENDPOINT
&&
785 remain
>=UMIDI_CS_ENDPOINT_DESCRIPTOR_SIZE
&&
791 p
->num_jacks
= TO_CSEPD(desc
)->bNumEmbMIDIJack
;
792 if (UE_GET_DIR(epaddr
)==UE_DIR_OUT
) {
793 sc
->sc_out_num_endpoints
++;
794 sc
->sc_out_num_jacks
+= p
->num_jacks
;
796 sc
->sc_in_num_endpoints
++;
797 sc
->sc_in_num_jacks
+= p
->num_jacks
;
808 num_ep
= sc
->sc_out_num_endpoints
+ sc
->sc_in_num_endpoints
;
809 p
= sc
->sc_endpoints
;
813 for (q
=p
+1; q
<endep
; q
++) {
814 if ((UE_GET_DIR(lowest
->addr
)==UE_DIR_IN
&&
815 UE_GET_DIR(q
->addr
)==UE_DIR_OUT
) ||
816 ((UE_GET_DIR(lowest
->addr
)==
817 UE_GET_DIR(q
->addr
)) &&
818 (UE_GET_ADDR(lowest
->addr
)>
819 UE_GET_ADDR(q
->addr
))))
823 memcpy((void *)&tmpep
, (void *)p
, sizeof(tmpep
));
824 memcpy((void *)p
, (void *)lowest
, sizeof(tmpep
));
825 memcpy((void *)lowest
, (void *)&tmpep
, sizeof(tmpep
));
831 sc
->sc_out_ep
= sc
->sc_out_num_endpoints
? sc
->sc_endpoints
: NULL
;
833 sc
->sc_in_num_endpoints
?
834 sc
->sc_endpoints
+sc
->sc_out_num_endpoints
: NULL
;
836 return USBD_NORMAL_COMPLETION
;
845 alloc_all_jacks(struct umidi_softc
*sc
)
848 struct umidi_endpoint
*ep
;
849 struct umidi_jack
*jack
;
850 const unsigned char *cn_spec
;
852 if (UMQ_ISTYPE(sc
, UMQ_TYPE_CN_SEQ_PER_EP
))
853 sc
->cblnums_global
= 0;
854 else if (UMQ_ISTYPE(sc
, UMQ_TYPE_CN_SEQ_GLOBAL
))
855 sc
->cblnums_global
= 1;
858 * I don't think this default is correct, but it preserves
859 * the prior behavior of the code. That's why I defined two
860 * complementary quirks. Any device for which the default
861 * behavior is wrong can be made to work by giving it an
862 * explicit quirk, and if a pattern ever develops (as I suspect
863 * it will) that a lot of otherwise standard USB MIDI devices
864 * need the CN_SEQ_PER_EP "quirk," then this default can be
865 * changed to 0, and the only devices that will break are those
866 * listing neither quirk, and they'll easily be fixed by giving
867 * them the CN_SEQ_GLOBAL quirk.
869 sc
->cblnums_global
= 1;
872 if (UMQ_ISTYPE(sc
, UMQ_TYPE_CN_FIXED
))
873 cn_spec
= umidi_get_quirk_data_from_type(sc
->sc_quirk
,
878 /* allocate/initialize structures */
880 malloc(sizeof(*sc
->sc_out_jacks
)*(sc
->sc_in_num_jacks
+
881 sc
->sc_out_num_jacks
),
886 sc
->sc_out_num_jacks
? sc
->sc_jacks
: NULL
;
888 sc
->sc_in_num_jacks
? sc
->sc_jacks
+sc
->sc_out_num_jacks
: NULL
;
890 jack
= &sc
->sc_out_jacks
[0];
891 for (i
=0; i
<sc
->sc_out_num_jacks
; i
++) {
895 jack
->u
.out
.intr
= NULL
;
896 jack
->midiman_ppkt
= NULL
;
897 if ( sc
->cblnums_global
)
898 jack
->cable_number
= i
;
901 jack
= &sc
->sc_in_jacks
[0];
902 for (i
=0; i
<sc
->sc_in_num_jacks
; i
++) {
906 jack
->u
.in
.intr
= NULL
;
907 if ( sc
->cblnums_global
)
908 jack
->cable_number
= i
;
912 /* assign each jacks to each endpoints */
913 jack
= &sc
->sc_out_jacks
[0];
914 ep
= &sc
->sc_out_ep
[0];
915 for (i
=0; i
<sc
->sc_out_num_endpoints
; i
++) {
916 for (j
=0; j
<ep
->num_jacks
; j
++) {
918 if ( cn_spec
!= NULL
)
919 jack
->cable_number
= *cn_spec
++;
920 else if ( !sc
->cblnums_global
)
921 jack
->cable_number
= j
;
922 ep
->jacks
[jack
->cable_number
] = jack
;
927 jack
= &sc
->sc_in_jacks
[0];
928 ep
= &sc
->sc_in_ep
[0];
929 for (i
=0; i
<sc
->sc_in_num_endpoints
; i
++) {
930 for (j
=0; j
<ep
->num_jacks
; j
++) {
932 if ( cn_spec
!= NULL
)
933 jack
->cable_number
= *cn_spec
++;
934 else if ( !sc
->cblnums_global
)
935 jack
->cable_number
= j
;
936 ep
->jacks
[jack
->cable_number
] = jack
;
942 return USBD_NORMAL_COMPLETION
;
946 free_all_jacks(struct umidi_softc
*sc
)
951 if (sc
->sc_out_jacks
) {
952 free(sc
->sc_jacks
, M_USBDEV
);
953 sc
->sc_jacks
= sc
->sc_in_jacks
= sc
->sc_out_jacks
= NULL
;
959 bind_jacks_to_mididev(struct umidi_softc
*sc
,
960 struct umidi_jack
*out_jack
,
961 struct umidi_jack
*in_jack
,
962 struct umidi_mididev
*mididev
)
964 if ((out_jack
&& out_jack
->binded
) || (in_jack
&& in_jack
->binded
))
966 if (mididev
->out_jack
|| mididev
->in_jack
)
970 out_jack
->binded
= 1;
973 mididev
->in_jack
= in_jack
;
974 mididev
->out_jack
= out_jack
;
976 return USBD_NORMAL_COMPLETION
;
980 unbind_jacks_from_mididev(struct umidi_mididev
*mididev
)
982 if ((mididev
->flags
& FWRITE
) && mididev
->out_jack
)
983 close_out_jack(mididev
->out_jack
);
984 if ((mididev
->flags
& FREAD
) && mididev
->in_jack
)
985 close_in_jack(mididev
->in_jack
);
987 if (mididev
->out_jack
)
988 mididev
->out_jack
->binded
= 0;
989 if (mididev
->in_jack
)
990 mididev
->in_jack
->binded
= 0;
991 mididev
->out_jack
= mididev
->in_jack
= NULL
;
995 unbind_all_jacks(struct umidi_softc
*sc
)
1000 for (i
=0; i
<sc
->sc_num_mididevs
; i
++) {
1001 unbind_jacks_from_mididev(&sc
->sc_mididevs
[i
]);
1006 assign_all_jacks_automatically(struct umidi_softc
*sc
)
1010 struct umidi_jack
*out
, *in
;
1011 const signed char *asg_spec
;
1014 alloc_all_mididevs(sc
,
1015 max(sc
->sc_out_num_jacks
, sc
->sc_in_num_jacks
));
1016 if (err
!=USBD_NORMAL_COMPLETION
)
1019 if ( UMQ_ISTYPE(sc
, UMQ_TYPE_MD_FIXED
))
1020 asg_spec
= umidi_get_quirk_data_from_type(sc
->sc_quirk
,
1025 for (i
=0; i
<sc
->sc_num_mididevs
; i
++) {
1026 if ( asg_spec
!= NULL
) {
1027 if ( *asg_spec
== -1 )
1030 out
= &sc
->sc_out_jacks
[*asg_spec
];
1032 if ( *asg_spec
== -1 )
1035 in
= &sc
->sc_in_jacks
[*asg_spec
];
1038 out
= (i
<sc
->sc_out_num_jacks
) ? &sc
->sc_out_jacks
[i
]
1040 in
= (i
<sc
->sc_in_num_jacks
) ? &sc
->sc_in_jacks
[i
]
1043 err
= bind_jacks_to_mididev(sc
, out
, in
, &sc
->sc_mididevs
[i
]);
1044 if (err
!=USBD_NORMAL_COMPLETION
) {
1045 free_all_mididevs(sc
);
1050 return USBD_NORMAL_COMPLETION
;
1054 open_out_jack(struct umidi_jack
*jack
, void *arg
, void (*intr
)(void *))
1056 struct umidi_endpoint
*ep
= jack
->endpoint
;
1057 umidi_packet_bufp end
;
1065 jack
->u
.out
.intr
= intr
;
1066 jack
->midiman_ppkt
= NULL
;
1067 end
= ep
->buffer
+ ep
->buffer_size
/ sizeof *ep
->buffer
;
1072 * out_solicit maintains an invariant that there will always be
1073 * (num_open - num_scheduled) slots free in the buffer. as we have
1074 * just incremented num_open, the buffer may be too full to satisfy
1075 * the invariant until a transfer completes, for which we must wait.
1077 while ( end
- ep
->next_slot
< ep
->num_open
- ep
->num_scheduled
) {
1078 err
= tsleep(ep
, PWAIT
|PCATCH
, "umi op", mstohz(10));
1083 return USBD_IOERROR
;
1088 return USBD_NORMAL_COMPLETION
;
1092 open_in_jack(struct umidi_jack
*jack
, void *arg
, void (*intr
)(void *, int))
1094 usbd_status err
= USBD_NORMAL_COMPLETION
;
1095 struct umidi_endpoint
*ep
= jack
->endpoint
;
1101 jack
->u
.in
.intr
= intr
;
1103 if (ep
->num_open
++==0 && UE_GET_DIR(ep
->addr
)==UE_DIR_IN
) {
1104 err
= start_input_transfer(ep
);
1105 if (err
!= USBD_NORMAL_COMPLETION
&&
1106 err
!= USBD_IN_PROGRESS
) {
1115 close_out_jack(struct umidi_jack
*jack
)
1117 struct umidi_endpoint
*ep
;
1123 ep
= jack
->endpoint
;
1124 mask
= 1 << (jack
->cable_number
);
1126 while ( mask
& (ep
->this_schedule
| ep
->next_schedule
) ) {
1127 err
= tsleep(ep
, PWAIT
|PCATCH
, "umi dr", mstohz(10));
1132 jack
->endpoint
->num_open
--;
1133 ep
->this_schedule
&= ~mask
;
1134 ep
->next_schedule
&= ~mask
;
1140 close_in_jack(struct umidi_jack
*jack
)
1144 if (--jack
->endpoint
->num_open
== 0) {
1145 usbd_abort_pipe(jack
->endpoint
->pipe
);
1151 attach_mididev(struct umidi_softc
*sc
, struct umidi_mididev
*mididev
)
1158 mididev
->label
= describe_mididev(mididev
);
1160 mididev
->mdev
= midi_attach_mi(&umidi_hw_if
, mididev
, sc
->sc_dev
);
1162 return USBD_NORMAL_COMPLETION
;
1166 detach_mididev(struct umidi_mididev
*mididev
, int flags
)
1169 return USBD_NO_ADDR
;
1171 if (mididev
->opened
) {
1172 umidi_close(mididev
);
1174 unbind_jacks_from_mididev(mididev
);
1176 if (mididev
->mdev
!= NULL
)
1177 config_detach(mididev
->mdev
, flags
);
1179 if (NULL
!= mididev
->label
) {
1180 free(mididev
->label
, M_USBDEV
);
1181 mididev
->label
= NULL
;
1186 return USBD_NORMAL_COMPLETION
;
1190 deactivate_mididev(struct umidi_mididev
*mididev
)
1192 if (mididev
->out_jack
)
1193 mididev
->out_jack
->binded
= 0;
1194 if (mididev
->in_jack
)
1195 mididev
->in_jack
->binded
= 0;
1199 alloc_all_mididevs(struct umidi_softc
*sc
, int nmidi
)
1201 sc
->sc_num_mididevs
= nmidi
;
1202 sc
->sc_mididevs
= malloc(sizeof(*sc
->sc_mididevs
)*nmidi
,
1203 M_USBDEV
, M_WAITOK
|M_ZERO
);
1204 if (!sc
->sc_mididevs
)
1207 return USBD_NORMAL_COMPLETION
;
1211 free_all_mididevs(struct umidi_softc
*sc
)
1213 sc
->sc_num_mididevs
= 0;
1214 if (sc
->sc_mididevs
)
1215 free(sc
->sc_mididevs
, M_USBDEV
);
1219 attach_all_mididevs(struct umidi_softc
*sc
)
1224 if (sc
->sc_mididevs
)
1225 for (i
=0; i
<sc
->sc_num_mididevs
; i
++) {
1226 err
= attach_mididev(sc
, &sc
->sc_mididevs
[i
]);
1227 if (err
!=USBD_NORMAL_COMPLETION
)
1231 return USBD_NORMAL_COMPLETION
;
1235 detach_all_mididevs(struct umidi_softc
*sc
, int flags
)
1240 if (sc
->sc_mididevs
)
1241 for (i
=0; i
<sc
->sc_num_mididevs
; i
++) {
1242 err
= detach_mididev(&sc
->sc_mididevs
[i
], flags
);
1243 if (err
!=USBD_NORMAL_COMPLETION
)
1247 return USBD_NORMAL_COMPLETION
;
1251 deactivate_all_mididevs(struct umidi_softc
*sc
)
1255 if (sc
->sc_mididevs
) {
1256 for (i
=0; i
<sc
->sc_num_mididevs
; i
++)
1257 deactivate_mididev(&sc
->sc_mididevs
[i
]);
1262 * TODO: the 0-based cable numbers will often not match the labeling of the
1263 * equipment. Ideally:
1264 * For class-compliant devices: get the iJack string from the jack descriptor.
1266 * - support a DISPLAY_BASE_CN quirk (add the value to each internal cable
1267 * number for display)
1268 * - support an array quirk explictly giving a char * for each jack.
1269 * For now, you get 0-based cable numbers. If there are multiple endpoints and
1270 * the CNs are not globally unique, each is shown with its associated endpoint
1271 * address in hex also. That should not be necessary when using iJack values
1275 describe_mididev(struct umidi_mididev
*md
)
1279 const char *unit_label
;
1281 struct umidi_softc
*sc
;
1287 show_ep_in
= sc
-> sc_in_num_endpoints
> 1 && !sc
->cblnums_global
;
1288 show_ep_out
= sc
->sc_out_num_endpoints
> 1 && !sc
->cblnums_global
;
1290 if ( NULL
!= md
->in_jack
)
1291 snprintf(in_label
, sizeof in_label
,
1292 show_ep_in
? "<%d(%x) " : "<%d ",
1293 md
->in_jack
->cable_number
,
1294 md
->in_jack
->endpoint
->addr
);
1298 if ( NULL
!= md
->out_jack
)
1299 snprintf(out_label
, sizeof out_label
,
1300 show_ep_out
? ">%d(%x) " : ">%d ",
1301 md
->out_jack
->cable_number
,
1302 md
->out_jack
->endpoint
->addr
);
1306 unit_label
= USBDEVNAME(sc
->sc_dev
);
1308 len
= strlen(in_label
) + strlen(out_label
) + strlen(unit_label
) + 4;
1310 final_label
= malloc(len
, M_USBDEV
, M_WAITOK
);
1312 snprintf(final_label
, len
, "%s%son %s",
1313 in_label
, out_label
, unit_label
);
1320 dump_sc(struct umidi_softc
*sc
)
1324 DPRINTFN(10, ("%s: dump_sc\n", USBDEVNAME(sc
->sc_dev
)));
1325 for (i
=0; i
<sc
->sc_out_num_endpoints
; i
++) {
1326 DPRINTFN(10, ("\tout_ep(%p):\n", &sc
->sc_out_ep
[i
]));
1327 dump_ep(&sc
->sc_out_ep
[i
]);
1329 for (i
=0; i
<sc
->sc_in_num_endpoints
; i
++) {
1330 DPRINTFN(10, ("\tin_ep(%p):\n", &sc
->sc_in_ep
[i
]));
1331 dump_ep(&sc
->sc_in_ep
[i
]);
1336 dump_ep(struct umidi_endpoint
*ep
)
1339 for (i
=0; i
<UMIDI_MAX_EPJACKS
; i
++) {
1340 if (NULL
==ep
->jacks
[i
])
1342 DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i
, ep
->jacks
[i
]));
1343 dump_jack(ep
->jacks
[i
]);
1347 dump_jack(struct umidi_jack
*jack
)
1349 DPRINTFN(10, ("\t\t\tep=%p\n",
1353 #endif /* UMIDI_DEBUG */
1361 static const int packet_length
[16] = {
1380 #define GET_CN(p) (((unsigned char)(p)>>4)&0x0F)
1381 #define GET_CIN(p) ((unsigned char)(p)&0x0F)
1382 #define MIX_CN_CIN(cn, cin) \
1383 ((unsigned char)((((unsigned char)(cn)&0x0F)<<4)| \
1384 ((unsigned char)(cin)&0x0F)))
1387 start_input_transfer(struct umidi_endpoint
*ep
)
1389 usbd_setup_xfer(ep
->xfer
, ep
->pipe
,
1390 (usbd_private_handle
)ep
,
1391 ep
->buffer
, ep
->buffer_size
,
1392 USBD_SHORT_XFER_OK
| USBD_NO_COPY
,
1393 USBD_NO_TIMEOUT
, in_intr
);
1394 return usbd_transfer(ep
->xfer
);
1398 start_output_transfer(struct umidi_endpoint
*ep
)
1404 length
= (ep
->next_slot
- ep
->buffer
) * sizeof *ep
->buffer
;
1405 DPRINTFN(200,("umidi out transfer: start %p end %p length %u\n",
1406 ep
->buffer
, ep
->next_slot
, length
));
1407 usbd_setup_xfer(ep
->xfer
, ep
->pipe
,
1408 (usbd_private_handle
)ep
,
1410 USBD_NO_COPY
, USBD_NO_TIMEOUT
, out_intr
);
1411 rv
= usbd_transfer(ep
->xfer
);
1414 * Once the transfer is scheduled, no more adding to partial
1415 * packets within it.
1417 if (UMQ_ISTYPE(ep
->sc
, UMQ_TYPE_MIDIMAN_GARBLE
)) {
1418 for (i
=0; i
<UMIDI_MAX_EPJACKS
; ++i
)
1419 if (NULL
!= ep
->jacks
[i
])
1420 ep
->jacks
[i
]->midiman_ppkt
= NULL
;
1427 #define DPR_PACKET(dir, sc, p) \
1428 if ((unsigned char)(p)[1]!=0xFE) \
1430 ("%s: umidi packet(" #dir "): %02X %02X %02X %02X\n", \
1431 USBDEVNAME(sc->sc_dev), \
1432 (unsigned char)(p)[0], \
1433 (unsigned char)(p)[1], \
1434 (unsigned char)(p)[2], \
1435 (unsigned char)(p)[3]));
1437 #define DPR_PACKET(dir, sc, p)
1441 * A 4-byte Midiman packet superficially resembles a 4-byte USB MIDI packet
1442 * with the cable number and length in the last byte instead of the first,
1443 * but there the resemblance ends. Where a USB MIDI packet is a semantic
1444 * unit, a Midiman packet is just a wrapper for 1 to 3 bytes of raw MIDI
1445 * with a cable nybble and a length nybble (which, unlike the CIN of a
1446 * real USB MIDI packet, has no semantics at all besides the length).
1447 * A packet received from a Midiman may contain part of a MIDI message,
1448 * more than one MIDI message, or parts of more than one MIDI message. A
1449 * three-byte MIDI message may arrive in three packets of data length 1, and
1450 * running status may be used. Happily, the midi(4) driver above us will put
1451 * it all back together, so the only cost is in USB bandwidth. The device
1452 * has an easier time with what it receives from us: we'll pack messages in
1453 * and across packets, but filling the packets whenever possible and,
1454 * as midi(4) hands us a complete message at a time, we'll never send one
1455 * in a dribble of short packets.
1459 out_jack_output(struct umidi_jack
*out_jack
, u_char
*src
, int len
, int cin
)
1461 struct umidi_endpoint
*ep
= out_jack
->endpoint
;
1462 struct umidi_softc
*sc
= ep
->sc
;
1463 unsigned char *packet
;
1471 if (!out_jack
->opened
)
1472 return ENODEV
; /* XXX as it was, is this the right errno? */
1475 if ( umididebug
>= 100 )
1476 microtime(&umidi_tv
);
1478 DPRINTFN(100, ("umidi out: %"PRIu64
".%06"PRIu64
"s ep=%p cn=%d len=%d cin=%#x\n",
1479 umidi_tv
.tv_sec
%100, (uint64_t)umidi_tv
.tv_usec
,
1480 ep
, out_jack
->cable_number
, len
, cin
));
1483 packet
= *ep
->next_slot
++;
1484 KASSERT(ep
->buffer_size
>=
1485 (ep
->next_slot
- ep
->buffer
) * sizeof *ep
->buffer
);
1486 memset(packet
, 0, UMIDI_PACKET_SIZE
);
1487 if (UMQ_ISTYPE(sc
, UMQ_TYPE_MIDIMAN_GARBLE
)) {
1488 if (NULL
!= out_jack
->midiman_ppkt
) { /* fill out a prev pkt */
1489 poff
= 0x0f & (out_jack
->midiman_ppkt
[3]);
1493 memcpy(out_jack
->midiman_ppkt
+poff
, src
, plen
);
1497 out_jack
->midiman_ppkt
[3] =
1498 MIX_CN_CIN(out_jack
->cable_number
, plen
);
1499 DPR_PACKET(out
+, sc
, out_jack
->midiman_ppkt
);
1501 out_jack
->midiman_ppkt
= NULL
; /* no more */
1504 ep
->next_slot
--; /* won't be needed, nevermind */
1506 memcpy(packet
, src
, len
);
1507 packet
[3] = MIX_CN_CIN(out_jack
->cable_number
, len
);
1508 DPR_PACKET(out
, sc
, packet
);
1510 out_jack
->midiman_ppkt
= packet
;
1512 } else { /* the nice simple USB class-compliant case */
1513 packet
[0] = MIX_CN_CIN(out_jack
->cable_number
, cin
);
1514 memcpy(packet
+1, src
, len
);
1515 DPR_PACKET(out
, sc
, packet
);
1517 ep
->next_schedule
|= 1<<(out_jack
->cable_number
);
1518 ++ ep
->num_scheduled
;
1519 if ( !ep
->armed
&& !ep
->soliciting
) {
1521 * It would be bad to call out_solicit directly here (the
1522 * caller need not be reentrant) but a soft interrupt allows
1523 * solicit to run immediately the caller exits its critical
1524 * section, and if the caller has more to write we can get it
1525 * before starting the USB transfer, and send a longer one.
1528 softint_schedule(ep
->solicit_cookie
);
1536 in_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
,
1540 struct umidi_endpoint
*ep
= (struct umidi_endpoint
*)priv
;
1541 struct umidi_jack
*jack
;
1542 unsigned char *packet
;
1543 umidi_packet_bufp slot
;
1544 umidi_packet_bufp end
;
1545 unsigned char *data
;
1548 if (ep
->sc
->sc_dying
|| !ep
->num_open
)
1551 usbd_get_xfer_status(xfer
, NULL
, NULL
, &count
, NULL
);
1552 if ( 0 == count
% UMIDI_PACKET_SIZE
) {
1553 DPRINTFN(200,("%s: input endpoint %p transfer length %u\n",
1554 USBDEVNAME(ep
->sc
->sc_dev
), ep
, count
));
1556 DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
1557 USBDEVNAME(ep
->sc
->sc_dev
), ep
, count
));
1561 end
= slot
+ count
/ sizeof *slot
;
1563 for ( packet
= *slot
; slot
< end
; packet
= *++slot
) {
1565 if ( UMQ_ISTYPE(ep
->sc
, UMQ_TYPE_MIDIMAN_GARBLE
) ) {
1566 cn
= (0xf0&(packet
[3]))>>4;
1567 len
= 0x0f&(packet
[3]);
1570 cn
= GET_CN(packet
[0]);
1571 len
= packet_length
[GET_CIN(packet
[0])];
1574 /* 0 <= cn <= 15 by inspection of above code */
1575 if (!(jack
= ep
->jacks
[cn
]) || cn
!= jack
->cable_number
) {
1576 DPRINTF(("%s: stray input endpoint %p cable %d len %d: "
1577 "%02X %02X %02X (try CN_SEQ quirk?)\n",
1578 USBDEVNAME(ep
->sc
->sc_dev
), ep
, cn
, len
,
1581 (unsigned)data
[2]));
1585 if (!jack
->binded
|| !jack
->opened
)
1588 DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
1590 USBDEVNAME(ep
->sc
->sc_dev
), ep
, cn
, len
,
1593 (unsigned)data
[2]));
1595 if (jack
->u
.in
.intr
) {
1596 for (i
=0; i
<len
; i
++) {
1597 (*jack
->u
.in
.intr
)(jack
->arg
, data
[i
]);
1603 (void)start_input_transfer(ep
);
1607 out_intr(usbd_xfer_handle xfer
, usbd_private_handle priv
,
1610 struct umidi_endpoint
*ep
= (struct umidi_endpoint
*)priv
;
1611 struct umidi_softc
*sc
= ep
->sc
;
1618 if ( umididebug
>= 200 )
1619 microtime(&umidi_tv
);
1621 usbd_get_xfer_status(xfer
, NULL
, NULL
, &count
, NULL
);
1622 if ( 0 == count
% UMIDI_PACKET_SIZE
) {
1623 DPRINTFN(200,("%s: %"PRIu64
".%06"PRIu64
"s out ep %p xfer length %u\n",
1624 USBDEVNAME(ep
->sc
->sc_dev
),
1625 umidi_tv
.tv_sec
%100, (uint64_t)umidi_tv
.tv_usec
, ep
, count
));
1627 DPRINTF(("%s: output endpoint %p odd transfer length %u\n",
1628 USBDEVNAME(ep
->sc
->sc_dev
), ep
, count
));
1630 count
/= UMIDI_PACKET_SIZE
;
1633 * If while the transfer was pending we buffered any new messages,
1634 * move them to the start of the buffer.
1636 ep
->next_slot
-= count
;
1637 if ( ep
->buffer
< ep
->next_slot
) {
1638 memcpy(ep
->buffer
, ep
->buffer
+ count
,
1639 (char *)ep
->next_slot
- (char *)ep
->buffer
);
1643 * Do not want anyone else to see armed <- 0 before soliciting <- 1.
1644 * Running at splusb so the following should happen to be safe.
1647 if ( !ep
->soliciting
) {
1654 * A jack on which we have received a packet must be called back on its
1655 * out.intr handler before it will send us another; it is considered
1656 * 'scheduled'. It is nice and predictable - as long as it is scheduled,
1657 * we need no extra buffer space for it.
1659 * In contrast, a jack that is open but not scheduled may supply us a packet
1660 * at any time, driven by the top half, and we must be able to accept it, no
1661 * excuses. So we must ensure that at any point in time there are at least
1662 * (num_open - num_scheduled) slots free.
1664 * As long as there are more slots free than that minimum, we can loop calling
1665 * scheduled jacks back on their "interrupt" handlers, soliciting more
1666 * packets, starting the USB transfer only when the buffer space is down to
1667 * the minimum or no jack has any more to send.
1670 out_solicit(void *arg
)
1672 struct umidi_endpoint
*ep
= arg
;
1674 umidi_packet_bufp end
;
1676 struct umidi_jack
*jack
;
1678 end
= ep
->buffer
+ ep
->buffer_size
/ sizeof *ep
->buffer
;
1682 if ( end
- ep
->next_slot
<= ep
->num_open
- ep
->num_scheduled
)
1683 break; /* at splusb */
1684 if ( ep
->this_schedule
== 0 ) {
1685 if ( ep
->next_schedule
== 0 )
1686 break; /* at splusb */
1687 ep
->this_schedule
= ep
->next_schedule
;
1688 ep
->next_schedule
= 0;
1691 * At least one jack is scheduled. Find and mask off the least
1692 * set bit in this_schedule and decrement num_scheduled.
1693 * Convert mask to bit index to find the corresponding jack,
1694 * and call its intr handler. If it has a message, it will call
1695 * back one of the output methods, which will set its bit in
1696 * next_schedule (not copied into this_schedule until the
1697 * latter is empty). In this way we round-robin the jacks that
1698 * have messages to send, until the buffer is as full as we
1699 * dare, and then start a transfer.
1701 which
= ep
->this_schedule
;
1702 which
&= (~which
)+1; /* now mask of least set bit */
1703 ep
->this_schedule
&= ~which
;
1704 -- ep
->num_scheduled
;
1707 -- which
; /* now 1s below mask - count 1s to get index */
1708 which
-= ((which
>> 1) & 0x5555);/* SWAR credit aggregate.org */
1709 which
= (((which
>> 2) & 0x3333) + (which
& 0x3333));
1710 which
= (((which
>> 4) + which
) & 0x0f0f);
1711 which
+= (which
>> 8);
1712 which
&= 0x1f; /* the bit index a/k/a jack number */
1714 jack
= ep
->jacks
[which
];
1715 if (jack
->u
.out
.intr
)
1716 (*jack
->u
.out
.intr
)(jack
->arg
);
1718 /* splusb at loop exit */
1719 if ( !ep
->armed
&& ep
->next_slot
> ep
->buffer
)
1720 ep
->armed
= (USBD_IN_PROGRESS
== start_output_transfer(ep
));