Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / usb / umidi.c
blobdad636071d3feafb6f6fd9fca84f54956da36e64
1 /* $NetBSD: umidi.c,v 1.39 2009/01/11 11:06:08 cegger Exp $ */
2 /*
3 * Copyright (c) 2001 The NetBSD Foundation, Inc.
4 * All rights reserved.
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
12 * are met:
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>
42 #include <sys/conf.h>
43 #include <sys/file.h>
44 #include <sys/select.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/poll.h>
48 #include <sys/intr.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>
62 #ifdef UMIDI_DEBUG
63 #define DPRINTF(x) if (umididebug) printf x
64 #define DPRINTFN(n,x) if (umididebug >= (n)) printf x
65 #include <sys/time.h>
66 static struct timeval umidi_tv;
67 int umididebug = 0;
68 #else
69 #define DPRINTF(x)
70 #define DPRINTFN(n,x)
71 #endif
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 *,
92 struct umidi_jack *,
93 struct umidi_jack *,
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 *,
99 void (*)(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 *);
115 #ifdef UMIDI_DEBUG
116 static void dump_sc(struct umidi_softc *);
117 static void dump_ep(struct umidi_endpoint *);
118 static void dump_jack(struct umidi_jack *);
119 #endif
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 = {
130 umidi_open,
131 umidi_close,
132 umidi_rtmsg,
133 umidi_getinfo,
134 0, /* ioctl */
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,
147 .compress = 1,
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);
159 USB_MATCH(umidi)
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;
172 return UMATCH_NONE;
175 USB_ATTACH(umidi)
177 usbd_status err;
178 USB_IFATTACH_START(umidi, sc, uaa);
179 char *devinfop;
181 DPRINTFN(1,("umidi_attach\n"));
183 sc->sc_dev = self;
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;
192 sc->sc_quirk =
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);
202 goto error;
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",
208 err);
209 goto error;
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);
217 free_all_jacks(sc);
218 free_all_endpoints(sc);
219 aprint_error_dev(self,
220 "assign_all_jacks_automatically failed. (err=%d)\n", err);
221 goto error;
223 err = attach_all_mididevs(sc);
224 if (err!=USBD_NORMAL_COMPLETION) {
225 free_all_jacks(sc);
226 free_all_endpoints(sc);
227 aprint_error_dev(self,
228 "attach_all_mididevs failed. (err=%d)\n", err);
231 #ifdef UMIDI_DEBUG
232 dump_sc(sc);
233 #endif
235 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH,
236 sc->sc_udev, USBDEV(sc->sc_dev));
238 USB_ATTACH_SUCCESS_RETURN;
239 error:
240 aprint_error_dev(self, "disabled.\n");
241 sc->sc_dying = 1;
242 USB_ATTACH_ERROR_RETURN;
245 void
246 umidi_childdet(device_t self, device_t child)
248 int i;
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)
255 break;
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);
266 switch (act) {
267 case DVACT_DEACTIVATE:
268 DPRINTFN(1,("umidi_activate (deactivate)\n"));
269 sc->sc_dying = 1;
270 deactivate_all_mididevs(sc);
271 return 0;
272 default:
273 DPRINTFN(1,("umidi_activate (%d)\n", act));
274 return EOPNOTSUPP;
278 USB_DETACH(umidi)
280 USB_DETACH_START(umidi, sc);
282 DPRINTFN(1,("umidi_detach\n"));
284 sc->sc_dying = 1;
285 detach_all_mididevs(sc, flags);
286 free_all_mididevs(sc);
287 free_all_jacks(sc);
288 free_all_endpoints(sc);
290 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
291 USBDEV(sc->sc_dev));
293 return 0;
298 * midi_if stuffs
301 umidi_open(void *addr,
302 int flags,
303 void (*iintr)(void *, int),
304 void (*ointr)(void *),
305 void *arg)
307 struct umidi_mididev *mididev = addr;
308 struct umidi_softc *sc = mididev->sc;
309 usbd_status err;
311 DPRINTF(("umidi_open: sc=%p\n", sc));
313 if (!sc)
314 return ENXIO;
315 if (mididev->opened)
316 return EBUSY;
317 if (sc->sc_dying)
318 return EIO;
320 mididev->opened = 1;
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 )
325 goto bad;
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 )
331 goto bad;
334 return 0;
335 bad:
336 mididev->opened = 0;
337 DPRINTF(("umidi_open: usbd_status %d\n", err));
338 return USBD_IN_USE == err ? EBUSY : EIO;
341 void
342 umidi_close(void *addr)
344 int s;
345 struct umidi_mididev *mididev = addr;
347 s = splusb();
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);
352 mididev->opened = 0;
353 splx(s);
357 umidi_channelmsg(void *addr, int status, int channel, u_char *msg,
358 int len)
360 struct umidi_mididev *mididev = addr;
362 if (!mididev->out_jack || !mididev->opened)
363 return EIO;
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;
372 int cin;
374 if (!mididev->out_jack || !mididev->opened)
375 return EIO;
377 switch ( len ) {
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;
391 int cin;
393 if (!mididev->out_jack || !mididev->opened)
394 return EIO;
396 switch ( len ) {
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;
410 u_char msg = d;
412 if (!mididev->out_jack || !mididev->opened)
413 return EIO;
415 return out_jack_output(mididev->out_jack, &msg, 1, 0xf);
418 void
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 */
438 static usbd_status
439 alloc_pipe(struct umidi_endpoint *ep)
441 struct umidi_softc *sc = ep->sc;
442 usbd_status err;
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)
457 * packet slots.
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;
467 ep->soliciting = 0;
468 ep->armed = 0;
469 ep->xfer = usbd_alloc_xfer(sc->sc_udev);
470 if (ep->xfer == NULL) {
471 err = USBD_NOMEM;
472 goto quit;
474 ep->buffer = usbd_alloc_buffer(ep->xfer, ep->buffer_size);
475 if (ep->buffer == NULL) {
476 usbd_free_xfer(ep->xfer);
477 err = USBD_NOMEM;
478 goto quit;
480 ep->next_slot = ep->buffer;
481 err = usbd_open_pipe(sc->sc_iface, ep->addr, 0, &ep->pipe);
482 if (err)
483 usbd_free_xfer(ep->xfer);
484 ep->solicit_cookie = softint_establish(SOFTINT_CLOCK, out_solicit, ep);
485 quit:
486 return err;
489 static void
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 *);
506 static usbd_status
507 alloc_all_endpoints(struct umidi_softc *sc)
509 usbd_status err;
510 struct umidi_endpoint *ep;
511 int i;
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);
517 } else {
518 err = alloc_all_endpoints_genuine(sc);
520 if (err!=USBD_NORMAL_COMPLETION)
521 return err;
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--)
528 free_pipe(ep-1);
529 free(sc->sc_endpoints, M_USBDEV);
530 sc->sc_endpoints = sc->sc_out_ep = sc->sc_in_ep = NULL;
531 break;
534 return err;
537 static void
538 free_all_endpoints(struct umidi_softc *sc)
540 int i;
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;
548 static usbd_status
549 alloc_all_endpoints_fixed_ep(struct umidi_softc *sc)
551 usbd_status err;
552 const struct umq_fixed_ep_desc *fp;
553 struct umidi_endpoint *ep;
554 usb_endpoint_descriptor_t *epd;
555 int i;
557 fp = umidi_get_quirk_data_from_type(sc->sc_quirk,
558 UMQ_TYPE_FIXED_EP);
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),
566 M_USBDEV, M_WAITOK);
567 if (!sc->sc_endpoints) {
568 return USBD_NOMEM;
570 sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
571 sc->sc_in_ep =
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(
578 sc->sc_iface,
579 fp->out_ep[i].ep);
580 if (!epd) {
581 aprint_error_dev(sc->sc_dev,
582 "cannot get endpoint descriptor(out:%d)\n",
583 fp->out_ep[i].ep);
584 err = USBD_INVAL;
585 goto error;
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",
590 fp->out_ep[i].ep);
591 err = USBD_INVAL;
592 goto error;
594 ep->sc = sc;
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;
598 ep->num_open = 0;
599 memset(ep->jacks, 0, sizeof(ep->jacks));
600 ep++;
602 ep = &sc->sc_in_ep[0];
603 for (i=0; i<sc->sc_in_num_endpoints; i++) {
604 epd = usbd_interface2endpoint_descriptor(
605 sc->sc_iface,
606 fp->in_ep[i].ep);
607 if (!epd) {
608 aprint_error_dev(sc->sc_dev,
609 "cannot get endpoint descriptor(in:%d)\n",
610 fp->in_ep[i].ep);
611 err = USBD_INVAL;
612 goto error;
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) ) {
624 case UE_BULK:
625 case UE_INTERRUPT:
626 if ( UE_DIR_IN == UE_GET_DIR(epd->bEndpointAddress) )
627 break;
628 /*FALLTHROUGH*/
629 default:
630 aprint_error_dev(sc->sc_dev,
631 "illegal endpoint(in:%d)\n", fp->in_ep[i].ep);
632 err = USBD_INVAL;
633 goto error;
636 ep->sc = sc;
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;
640 ep->num_open = 0;
641 memset(ep->jacks, 0, sizeof(ep->jacks));
642 ep++;
645 return USBD_NORMAL_COMPLETION;
646 error:
647 free(sc->sc_endpoints, M_USBDEV);
648 sc->sc_endpoints = NULL;
649 return err;
652 static usbd_status
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;
660 int dir;
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);
681 /* count jacks */
682 if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
683 udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
684 return USBD_INVAL;
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)
692 break;
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);
701 remain-=descsize;
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;
711 } else {
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;
717 } else {
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),
724 M_USBDEV, M_WAITOK);
725 if (!sc->sc_endpoints)
726 return USBD_NOMEM;
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));
734 } else
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));
744 } else
745 sc->sc_in_ep = NULL;
747 return USBD_NORMAL_COMPLETION;
750 static usbd_status
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;
756 int num_ep;
757 size_t remain, descsize;
758 struct umidi_endpoint *p, *q, *lowest, *endep, tmpep;
759 int epaddr;
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,
764 M_USBDEV, M_WAITOK);
765 if (!p)
766 return USBD_NOMEM;
768 sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
769 sc->sc_out_num_endpoints = sc->sc_in_num_endpoints = 0;
770 epaddr = -1;
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)
779 break;
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 &&
786 epaddr!=-1) {
787 if (num_ep>0) {
788 num_ep--;
789 p->sc = sc;
790 p->addr = epaddr;
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;
795 } else {
796 sc->sc_in_num_endpoints++;
797 sc->sc_in_num_jacks += p->num_jacks;
799 p++;
801 } else
802 epaddr = -1;
803 desc = NEXT_D(desc);
804 remain-=descsize;
807 /* sort endpoints */
808 num_ep = sc->sc_out_num_endpoints + sc->sc_in_num_endpoints;
809 p = sc->sc_endpoints;
810 endep = p + num_ep;
811 while (p<endep) {
812 lowest = p;
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))))
820 lowest = q;
822 if (lowest != p) {
823 memcpy((void *)&tmpep, (void *)p, sizeof(tmpep));
824 memcpy((void *)p, (void *)lowest, sizeof(tmpep));
825 memcpy((void *)lowest, (void *)&tmpep, sizeof(tmpep));
827 p->num_open = 0;
828 p++;
831 sc->sc_out_ep = sc->sc_out_num_endpoints ? sc->sc_endpoints : NULL;
832 sc->sc_in_ep =
833 sc->sc_in_num_endpoints ?
834 sc->sc_endpoints+sc->sc_out_num_endpoints : NULL;
836 return USBD_NORMAL_COMPLETION;
841 * jack stuffs
844 static usbd_status
845 alloc_all_jacks(struct umidi_softc *sc)
847 int i, j;
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;
856 else {
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,
874 UMQ_TYPE_CN_FIXED);
875 else
876 cn_spec = NULL;
878 /* allocate/initialize structures */
879 sc->sc_jacks =
880 malloc(sizeof(*sc->sc_out_jacks)*(sc->sc_in_num_jacks+
881 sc->sc_out_num_jacks),
882 M_USBDEV, M_WAITOK);
883 if (!sc->sc_jacks)
884 return USBD_NOMEM;
885 sc->sc_out_jacks =
886 sc->sc_out_num_jacks ? sc->sc_jacks : NULL;
887 sc->sc_in_jacks =
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++) {
892 jack->opened = 0;
893 jack->binded = 0;
894 jack->arg = NULL;
895 jack->u.out.intr = NULL;
896 jack->midiman_ppkt = NULL;
897 if ( sc->cblnums_global )
898 jack->cable_number = i;
899 jack++;
901 jack = &sc->sc_in_jacks[0];
902 for (i=0; i<sc->sc_in_num_jacks; i++) {
903 jack->opened = 0;
904 jack->binded = 0;
905 jack->arg = NULL;
906 jack->u.in.intr = NULL;
907 if ( sc->cblnums_global )
908 jack->cable_number = i;
909 jack++;
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++) {
917 jack->endpoint = ep;
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;
923 jack++;
925 ep++;
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++) {
931 jack->endpoint = ep;
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;
937 jack++;
939 ep++;
942 return USBD_NORMAL_COMPLETION;
945 static void
946 free_all_jacks(struct umidi_softc *sc)
948 int s;
950 s = splaudio();
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;
955 splx(s);
958 static usbd_status
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))
965 return USBD_IN_USE;
966 if (mididev->out_jack || mididev->in_jack)
967 return USBD_IN_USE;
969 if (out_jack)
970 out_jack->binded = 1;
971 if (in_jack)
972 in_jack->binded = 1;
973 mididev->in_jack = in_jack;
974 mididev->out_jack = out_jack;
976 return USBD_NORMAL_COMPLETION;
979 static void
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;
994 static void
995 unbind_all_jacks(struct umidi_softc *sc)
997 int i;
999 if (sc->sc_mididevs)
1000 for (i=0; i<sc->sc_num_mididevs; i++) {
1001 unbind_jacks_from_mididev(&sc->sc_mididevs[i]);
1005 static usbd_status
1006 assign_all_jacks_automatically(struct umidi_softc *sc)
1008 usbd_status err;
1009 int i;
1010 struct umidi_jack *out, *in;
1011 const signed char *asg_spec;
1013 err =
1014 alloc_all_mididevs(sc,
1015 max(sc->sc_out_num_jacks, sc->sc_in_num_jacks));
1016 if (err!=USBD_NORMAL_COMPLETION)
1017 return err;
1019 if ( UMQ_ISTYPE(sc, UMQ_TYPE_MD_FIXED))
1020 asg_spec = umidi_get_quirk_data_from_type(sc->sc_quirk,
1021 UMQ_TYPE_MD_FIXED);
1022 else
1023 asg_spec = NULL;
1025 for (i=0; i<sc->sc_num_mididevs; i++) {
1026 if ( asg_spec != NULL ) {
1027 if ( *asg_spec == -1 )
1028 out = NULL;
1029 else
1030 out = &sc->sc_out_jacks[*asg_spec];
1031 ++ asg_spec;
1032 if ( *asg_spec == -1 )
1033 in = NULL;
1034 else
1035 in = &sc->sc_in_jacks[*asg_spec];
1036 ++ asg_spec;
1037 } else {
1038 out = (i<sc->sc_out_num_jacks) ? &sc->sc_out_jacks[i]
1039 : NULL;
1040 in = (i<sc->sc_in_num_jacks) ? &sc->sc_in_jacks[i]
1041 : NULL;
1043 err = bind_jacks_to_mididev(sc, out, in, &sc->sc_mididevs[i]);
1044 if (err!=USBD_NORMAL_COMPLETION) {
1045 free_all_mididevs(sc);
1046 return err;
1050 return USBD_NORMAL_COMPLETION;
1053 static usbd_status
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;
1058 int s;
1059 int err;
1061 if (jack->opened)
1062 return USBD_IN_USE;
1064 jack->arg = arg;
1065 jack->u.out.intr = intr;
1066 jack->midiman_ppkt = NULL;
1067 end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
1068 s = splusb();
1069 jack->opened = 1;
1070 ep->num_open++;
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));
1079 if ( err ) {
1080 ep->num_open--;
1081 jack->opened = 0;
1082 splx(s);
1083 return USBD_IOERROR;
1086 splx(s);
1088 return USBD_NORMAL_COMPLETION;
1091 static usbd_status
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;
1097 if (jack->opened)
1098 return USBD_IN_USE;
1100 jack->arg = arg;
1101 jack->u.in.intr = intr;
1102 jack->opened = 1;
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) {
1107 ep->num_open--;
1111 return err;
1114 static void
1115 close_out_jack(struct umidi_jack *jack)
1117 struct umidi_endpoint *ep;
1118 int s;
1119 u_int16_t mask;
1120 int err;
1122 if (jack->opened) {
1123 ep = jack->endpoint;
1124 mask = 1 << (jack->cable_number);
1125 s = splusb();
1126 while ( mask & (ep->this_schedule | ep->next_schedule) ) {
1127 err = tsleep(ep, PWAIT|PCATCH, "umi dr", mstohz(10));
1128 if ( err )
1129 break;
1131 jack->opened = 0;
1132 jack->endpoint->num_open--;
1133 ep->this_schedule &= ~mask;
1134 ep->next_schedule &= ~mask;
1135 splx(s);
1139 static void
1140 close_in_jack(struct umidi_jack *jack)
1142 if (jack->opened) {
1143 jack->opened = 0;
1144 if (--jack->endpoint->num_open == 0) {
1145 usbd_abort_pipe(jack->endpoint->pipe);
1150 static usbd_status
1151 attach_mididev(struct umidi_softc *sc, struct umidi_mididev *mididev)
1153 if (mididev->sc)
1154 return USBD_IN_USE;
1156 mididev->sc = sc;
1158 mididev->label = describe_mididev(mididev);
1160 mididev->mdev = midi_attach_mi(&umidi_hw_if, mididev, sc->sc_dev);
1162 return USBD_NORMAL_COMPLETION;
1165 static usbd_status
1166 detach_mididev(struct umidi_mididev *mididev, int flags)
1168 if (!mididev->sc)
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;
1184 mididev->sc = NULL;
1186 return USBD_NORMAL_COMPLETION;
1189 static void
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;
1198 static usbd_status
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)
1205 return USBD_NOMEM;
1207 return USBD_NORMAL_COMPLETION;
1210 static void
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);
1218 static usbd_status
1219 attach_all_mididevs(struct umidi_softc *sc)
1221 usbd_status err;
1222 int i;
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)
1228 return err;
1231 return USBD_NORMAL_COMPLETION;
1234 static usbd_status
1235 detach_all_mididevs(struct umidi_softc *sc, int flags)
1237 usbd_status err;
1238 int i;
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)
1244 return err;
1247 return USBD_NORMAL_COMPLETION;
1250 static void
1251 deactivate_all_mididevs(struct umidi_softc *sc)
1253 int i;
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.
1265 * Otherwise:
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
1272 * or a quirk array.
1274 static char *
1275 describe_mididev(struct umidi_mididev *md)
1277 char in_label[16];
1278 char out_label[16];
1279 const char *unit_label;
1280 char *final_label;
1281 struct umidi_softc *sc;
1282 int show_ep_in;
1283 int show_ep_out;
1284 size_t len;
1286 sc = md->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);
1295 else
1296 in_label[0] = '\0';
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);
1303 else
1304 in_label[0] = '\0';
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);
1315 return final_label;
1318 #ifdef UMIDI_DEBUG
1319 static void
1320 dump_sc(struct umidi_softc *sc)
1322 int i;
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]);
1335 static void
1336 dump_ep(struct umidi_endpoint *ep)
1338 int i;
1339 for (i=0; i<UMIDI_MAX_EPJACKS; i++) {
1340 if (NULL==ep->jacks[i])
1341 continue;
1342 DPRINTFN(10, ("\t\tjack[%d]:%p:\n", i, ep->jacks[i]));
1343 dump_jack(ep->jacks[i]);
1346 static void
1347 dump_jack(struct umidi_jack *jack)
1349 DPRINTFN(10, ("\t\t\tep=%p\n",
1350 jack->endpoint));
1353 #endif /* UMIDI_DEBUG */
1358 * MUX MIDI PACKET
1361 static const int packet_length[16] = {
1362 /*0*/ -1,
1363 /*1*/ -1,
1364 /*2*/ 2,
1365 /*3*/ 3,
1366 /*4*/ 3,
1367 /*5*/ 1,
1368 /*6*/ 2,
1369 /*7*/ 3,
1370 /*8*/ 3,
1371 /*9*/ 3,
1372 /*A*/ 3,
1373 /*B*/ 3,
1374 /*C*/ 2,
1375 /*D*/ 2,
1376 /*E*/ 3,
1377 /*F*/ 1,
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)))
1386 static usbd_status
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);
1397 static usbd_status
1398 start_output_transfer(struct umidi_endpoint *ep)
1400 usbd_status rv;
1401 u_int32_t length;
1402 int i;
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,
1409 ep->buffer, length,
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;
1423 return rv;
1426 #ifdef UMIDI_DEBUG
1427 #define DPR_PACKET(dir, sc, p) \
1428 if ((unsigned char)(p)[1]!=0xFE) \
1429 DPRINTFN(500, \
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]));
1436 #else
1437 #define DPR_PACKET(dir, sc, p)
1438 #endif
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.
1458 static int
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;
1464 int s;
1465 int plen;
1466 int poff;
1468 if (sc->sc_dying)
1469 return EIO;
1471 if (!out_jack->opened)
1472 return ENODEV; /* XXX as it was, is this the right errno? */
1474 #ifdef UMIDI_DEBUG
1475 if ( umididebug >= 100 )
1476 microtime(&umidi_tv);
1477 #endif
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));
1482 s = splusb();
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]);
1490 plen = 3 - poff;
1491 if (plen > len)
1492 plen = len;
1493 memcpy(out_jack->midiman_ppkt+poff, src, plen);
1494 src += plen;
1495 len -= plen;
1496 plen += poff;
1497 out_jack->midiman_ppkt[3] =
1498 MIX_CN_CIN(out_jack->cable_number, plen);
1499 DPR_PACKET(out+, sc, out_jack->midiman_ppkt);
1500 if (3 == plen)
1501 out_jack->midiman_ppkt = NULL; /* no more */
1503 if (0 == len)
1504 ep->next_slot--; /* won't be needed, nevermind */
1505 else {
1506 memcpy(packet, src, len);
1507 packet[3] = MIX_CN_CIN(out_jack->cable_number, len);
1508 DPR_PACKET(out, sc, packet);
1509 if (len < 3)
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.
1527 ep->soliciting = 1;
1528 softint_schedule(ep->solicit_cookie);
1530 splx(s);
1532 return 0;
1535 static void
1536 in_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
1537 usbd_status status)
1539 int cn, len, i;
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;
1546 u_int32_t count;
1548 if (ep->sc->sc_dying || !ep->num_open)
1549 return;
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));
1555 } else {
1556 DPRINTF(("%s: input endpoint %p odd transfer length %u\n",
1557 USBDEVNAME(ep->sc->sc_dev), ep, count));
1560 slot = ep->buffer;
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]);
1568 data = packet;
1569 } else {
1570 cn = GET_CN(packet[0]);
1571 len = packet_length[GET_CIN(packet[0])];
1572 data = packet + 1;
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,
1579 (unsigned)data[0],
1580 (unsigned)data[1],
1581 (unsigned)data[2]));
1582 return;
1585 if (!jack->binded || !jack->opened)
1586 continue;
1588 DPRINTFN(500,("%s: input endpoint %p cable %d len %d: "
1589 "%02X %02X %02X\n",
1590 USBDEVNAME(ep->sc->sc_dev), ep, cn, len,
1591 (unsigned)data[0],
1592 (unsigned)data[1],
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);
1606 static void
1607 out_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
1608 usbd_status status)
1610 struct umidi_endpoint *ep = (struct umidi_endpoint *)priv;
1611 struct umidi_softc *sc = ep->sc;
1612 u_int32_t count;
1614 if (sc->sc_dying)
1615 return;
1617 #ifdef UMIDI_DEBUG
1618 if ( umididebug >= 200 )
1619 microtime(&umidi_tv);
1620 #endif
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));
1626 } else {
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);
1641 wakeup(ep);
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.
1646 ep->armed = 0;
1647 if ( !ep->soliciting ) {
1648 ep->soliciting = 1;
1649 out_solicit(ep);
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.
1669 static void
1670 out_solicit(void *arg)
1672 struct umidi_endpoint *ep = arg;
1673 int s;
1674 umidi_packet_bufp end;
1675 u_int16_t which;
1676 struct umidi_jack *jack;
1678 end = ep->buffer + ep->buffer_size / sizeof *ep->buffer;
1680 for ( ;; ) {
1681 s = splusb();
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;
1705 splx(s);
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));
1721 ep->soliciting = 0;
1722 splx(s);