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