sequencer.c revision 1.16.2.3 1 /* $NetBSD: sequencer.c,v 1.16.2.3 2001/09/21 12:08:30 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) netbsd.org).
9 *
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include "sequencer.h"
40 #if NSEQUENCER > 0
41
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
44 #include <sys/fcntl.h>
45 #include <sys/vnode.h>
46 #include <sys/select.h>
47 #include <sys/poll.h>
48 #include <sys/malloc.h>
49 #include <sys/proc.h>
50 #include <sys/systm.h>
51 #include <sys/syslog.h>
52 #include <sys/kernel.h>
53 #include <sys/signalvar.h>
54 #include <sys/conf.h>
55 #include <sys/audioio.h>
56 #include <sys/midiio.h>
57 #include <sys/device.h>
58 #include <sys/vnode.h>
59
60 #include <miscfs/specfs/specdev.h>
61
62 #include <dev/midi_if.h>
63 #include <dev/midivar.h>
64 #include <dev/sequencervar.h>
65
66 #define ADDTIMEVAL(a, b) ( \
67 (a)->tv_sec += (b)->tv_sec, \
68 (a)->tv_usec += (b)->tv_usec, \
69 (a)->tv_usec > 1000000 ? ((a)->tv_sec++, (a)->tv_usec -= 1000000) : 0\
70 )
71
72 #define SUBTIMEVAL(a, b) ( \
73 (a)->tv_sec -= (b)->tv_sec, \
74 (a)->tv_usec -= (b)->tv_usec, \
75 (a)->tv_usec < 0 ? ((a)->tv_sec--, (a)->tv_usec += 1000000) : 0\
76 )
77
78 #ifdef AUDIO_DEBUG
79 #define DPRINTF(x) if (sequencerdebug) printf x
80 #define DPRINTFN(n,x) if (sequencerdebug >= (n)) printf x
81 int sequencerdebug = 0;
82 #else
83 #define DPRINTF(x)
84 #define DPRINTFN(n,x)
85 #endif
86
87 #define SEQ_CMD(b) ((b)->arr[0])
88
89 #define SEQ_EDEV(b) ((b)->arr[1])
90 #define SEQ_ECMD(b) ((b)->arr[2])
91 #define SEQ_ECHAN(b) ((b)->arr[3])
92 #define SEQ_ENOTE(b) ((b)->arr[4])
93 #define SEQ_EPARM(b) ((b)->arr[5])
94
95 #define SEQ_EP1(b) ((b)->arr[4])
96 #define SEQ_EP2(b) ((b)->arr[5])
97
98 #define SEQ_XCMD(b) ((b)->arr[1])
99 #define SEQ_XDEV(b) ((b)->arr[2])
100 #define SEQ_XCHAN(b) ((b)->arr[3])
101 #define SEQ_XNOTE(b) ((b)->arr[4])
102 #define SEQ_XVEL(b) ((b)->arr[5])
103
104 #define SEQ_TCMD(b) ((b)->arr[1])
105 #define SEQ_TPARM(b) ((b)->arr[4])
106
107 #define SEQ_NOTE_MAX 128
108 #define SEQ_NOTE_XXX 255
109 #define SEQ_VEL_OFF 0
110
111 #define RECALC_TICK(t) ((t)->tick = 60 * 1000000L / ((t)->tempo * (t)->timebase))
112
113 struct sequencer_softc seqdevs[NSEQUENCER];
114
115 void sequencerattach __P((int));
116 void seq_reset __P((struct sequencer_softc *));
117 int seq_do_command __P((struct sequencer_softc *, seq_event_rec *));
118 int seq_do_extcommand __P((struct sequencer_softc *, seq_event_rec *));
119 int seq_do_chnvoice __P((struct sequencer_softc *, seq_event_rec *));
120 int seq_do_chncommon __P((struct sequencer_softc *, seq_event_rec *));
121 int seq_do_timing __P((struct sequencer_softc *, seq_event_rec *));
122 int seq_do_local __P((struct sequencer_softc *, seq_event_rec *));
123 int seq_do_sysex __P((struct sequencer_softc *, seq_event_rec *));
124 int seq_do_fullsize __P((struct sequencer_softc *, seq_event_rec *,
125 struct uio *));
126 int seq_timer __P((struct sequencer_softc *, int, int, seq_event_rec *));
127 static int seq_input_event __P((struct sequencer_softc *, seq_event_rec *));
128 int seq_drain __P((struct sequencer_softc *));
129 void seq_startoutput __P((struct sequencer_softc *));
130 void seq_timeout __P((void *));
131 int seq_to_new __P((seq_event_rec *, struct uio *));
132 static int seq_sleep_timo(int *, char *, int);
133 static int seq_sleep(int *, char *);
134 static void seq_wakeup(int *);
135
136 struct midi_softc;
137 int midiseq_out __P((struct midi_dev *, u_char *, u_int, int));
138 struct midi_dev *midiseq_open __P((int, int, struct proc *));
139 void midiseq_close __P((struct midi_dev *, int, struct proc *));
140 void midiseq_reset __P((struct midi_dev *));
141 int midiseq_noteon __P((struct midi_dev *, int, int, int));
142 int midiseq_noteoff __P((struct midi_dev *, int, int, int));
143 int midiseq_keypressure __P((struct midi_dev *, int, int, int));
144 int midiseq_pgmchange __P((struct midi_dev *, int, int));
145 int midiseq_chnpressure __P((struct midi_dev *, int, int));
146 int midiseq_ctlchange __P((struct midi_dev *, int, int, int));
147 int midiseq_pitchbend __P((struct midi_dev *, int, int));
148 int midiseq_loadpatch __P((struct midi_dev *, struct sysex_info *,
149 struct uio *));
150 int midiseq_putc __P((struct midi_dev *, int));
151 void midiseq_in __P((struct midi_dev *, u_char *, int));
152
153 void
154 sequencerattach(n)
155 int n;
156 {
157
158 for (n = 0; n < NSEQUENCER; n++)
159 callout_init(&seqdevs[n].sc_callout);
160 }
161
162 int
163 sequenceropen(devvp, flags, ifmt, p)
164 struct vnode *devvp;
165 int flags, ifmt;
166 struct proc *p;
167 {
168 int unit = SEQUENCERUNIT(devvp->v_rdev);
169 struct sequencer_softc *sc;
170 struct midi_dev *md;
171 int nmidi;
172
173 DPRINTF(("sequenceropen\n"));
174
175 if (unit >= NSEQUENCER)
176 return (ENXIO);
177 sc = &seqdevs[unit];
178 if (sc->isopen)
179 return EBUSY;
180
181 devvp->v_devcookie = sc;
182
183 if (SEQ_IS_OLD(unit))
184 sc->mode = SEQ_OLD;
185 else
186 sc->mode = SEQ_NEW;
187 sc->isopen++;
188 sc->flags = flags & (FREAD|FWRITE);
189 sc->rchan = 0;
190 sc->wchan = 0;
191 sc->pbus = 0;
192 sc->async = 0;
193 sc->input_stamp = ~0;
194
195 sc->nmidi = 0;
196 nmidi = midi_unit_count();
197
198 sc->devs = malloc(nmidi * sizeof(struct midi_dev *),
199 M_DEVBUF, M_WAITOK);
200 for (unit = 0; unit < nmidi; unit++) {
201 md = midiseq_open(unit, flags, p);
202 if (md) {
203 sc->devs[sc->nmidi++] = md;
204 md->seq = sc;
205 }
206 }
207
208 sc->timer.timebase = 100;
209 sc->timer.tempo = 60;
210 sc->doingsysex = 0;
211 RECALC_TICK(&sc->timer);
212 sc->timer.last = 0;
213 microtime(&sc->timer.start);
214
215 SEQ_QINIT(&sc->inq);
216 SEQ_QINIT(&sc->outq);
217 sc->lowat = SEQ_MAXQ / 2;
218
219 seq_reset(sc);
220
221 DPRINTF(("sequenceropen: mode=%d, nmidi=%d\n", sc->mode, sc->nmidi));
222 return 0;
223 }
224
225 static int
226 seq_sleep_timo(chan, label, timo)
227 int *chan;
228 char *label;
229 int timo;
230 {
231 int st;
232
233 if (!label)
234 label = "seq";
235
236 DPRINTFN(5, ("seq_sleep_timo: %p %s %d\n", chan, label, timo));
237 *chan = 1;
238 st = tsleep(chan, PWAIT | PCATCH, label, timo);
239 *chan = 0;
240 #ifdef MIDI_DEBUG
241 if (st != 0)
242 printf("seq_sleep: %d\n", st);
243 #endif
244 return st;
245 }
246
247 static int
248 seq_sleep(chan, label)
249 int *chan;
250 char *label;
251 {
252 return seq_sleep_timo(chan, label, 0);
253 }
254
255 static void
256 seq_wakeup(chan)
257 int *chan;
258 {
259 if (*chan) {
260 DPRINTFN(5, ("seq_wakeup: %p\n", chan));
261 wakeup(chan);
262 *chan = 0;
263 }
264 }
265
266 int
267 seq_drain(sc)
268 struct sequencer_softc *sc;
269 {
270 int error;
271
272 DPRINTFN(3, ("seq_drain: %p, len=%d\n", sc, SEQ_QLEN(&sc->outq)));
273 seq_startoutput(sc);
274 error = 0;
275 while(!SEQ_QEMPTY(&sc->outq) && !error)
276 error = seq_sleep_timo(&sc->wchan, "seq_dr", 60*hz);
277 return (error);
278 }
279
280 void
281 seq_timeout(addr)
282 void *addr;
283 {
284 struct sequencer_softc *sc = addr;
285 DPRINTFN(4, ("seq_timeout: %p\n", sc));
286 sc->timeout = 0;
287 seq_startoutput(sc);
288 if (SEQ_QLEN(&sc->outq) < sc->lowat) {
289 seq_wakeup(&sc->wchan);
290 selwakeup(&sc->wsel);
291 if (sc->async)
292 psignal(sc->async, SIGIO);
293 }
294
295 }
296
297 void
298 seq_startoutput(sc)
299 struct sequencer_softc *sc;
300 {
301 struct sequencer_queue *q = &sc->outq;
302 seq_event_rec cmd;
303
304 if (sc->timeout)
305 return;
306 DPRINTFN(4, ("seq_startoutput: %p, len=%d\n", sc, SEQ_QLEN(q)));
307 while(!SEQ_QEMPTY(q) && !sc->timeout) {
308 SEQ_QGET(q, cmd);
309 seq_do_command(sc, &cmd);
310 }
311 }
312
313 int
314 sequencerclose(devvp, flags, ifmt, p)
315 struct vnode *devvp;
316 int flags, ifmt;
317 struct proc *p;
318 {
319 struct sequencer_softc *sc = devvp->v_devcookie;
320 int n, s;
321
322 DPRINTF(("sequencerclose: %p\n", sc));
323
324 seq_drain(sc);
325 s = splaudio();
326 if (sc->timeout) {
327 callout_stop(&sc->sc_callout);
328 sc->timeout = 0;
329 }
330 splx(s);
331
332 for (n = 0; n < sc->nmidi; n++)
333 midiseq_close(sc->devs[n], flags, p);
334 free(sc->devs, M_DEVBUF);
335 sc->isopen = 0;
336 return (0);
337 }
338
339 static int
340 seq_input_event(sc, cmd)
341 struct sequencer_softc *sc;
342 seq_event_rec *cmd;
343 {
344 struct sequencer_queue *q = &sc->inq;
345
346 DPRINTFN(2, ("seq_input_event: %02x %02x %02x %02x %02x %02x %02x %02x\n",
347 cmd->arr[0], cmd->arr[1], cmd->arr[2], cmd->arr[3],
348 cmd->arr[4], cmd->arr[5], cmd->arr[6], cmd->arr[7]));
349 if (SEQ_QFULL(q))
350 return (ENOMEM);
351 SEQ_QPUT(q, *cmd);
352 seq_wakeup(&sc->rchan);
353 selwakeup(&sc->rsel);
354 if (sc->async)
355 psignal(sc->async, SIGIO);
356 return 0;
357 }
358
359 void
360 seq_event_intr(addr, iev)
361 void *addr;
362 seq_event_rec *iev;
363 {
364 struct sequencer_softc *sc = addr;
365 union {
366 u_int32_t l;
367 u_int8_t b[4];
368 } u;
369 u_long t;
370 struct timeval now;
371 seq_event_rec ev;
372
373 microtime(&now);
374 SUBTIMEVAL(&now, &sc->timer.start);
375 t = now.tv_sec * 1000000 + now.tv_usec;
376 t /= sc->timer.tick;
377 if (t != sc->input_stamp) {
378 ev.arr[0] = SEQ_TIMING;
379 ev.arr[1] = TMR_WAIT_ABS;
380 ev.arr[2] = 0;
381 ev.arr[3] = 0;
382 u.l = t;
383 ev.arr[4] = u.b[0];
384 ev.arr[5] = u.b[1];
385 ev.arr[6] = u.b[2];
386 ev.arr[7] = u.b[3];
387 seq_input_event(sc, &ev);
388 sc->input_stamp = t;
389 }
390 seq_input_event(sc, iev);
391 }
392
393 int
394 sequencerread(devvp, uio, ioflag)
395 struct vnode *devvp;
396 struct uio *uio;
397 int ioflag;
398 {
399 struct sequencer_softc *sc = devvp->v_devcookie;
400 struct sequencer_queue *q = &sc->inq;
401 seq_event_rec ev;
402 int error, s;
403
404 DPRINTFN(20, ("sequencerread: %p, count=%d, ioflag=%x\n",
405 sc, (int) uio->uio_resid, ioflag));
406
407 if (sc->mode == SEQ_OLD) {
408 DPRINTFN(-1,("sequencerread: old read\n"));
409 return (EINVAL); /* XXX unimplemented */
410 }
411
412 error = 0;
413 while (SEQ_QEMPTY(q)) {
414 if (ioflag & IO_NDELAY)
415 return EWOULDBLOCK;
416 else {
417 error = seq_sleep(&sc->rchan, "seq rd");
418 if (error)
419 return error;
420 }
421 }
422 s = splaudio();
423 while (uio->uio_resid >= sizeof ev && !error && !SEQ_QEMPTY(q)) {
424 SEQ_QGET(q, ev);
425 error = uiomove(&ev, sizeof ev, uio);
426 }
427 splx(s);
428 return error;
429 }
430
431 int
432 sequencerwrite(devvp, uio, ioflag)
433 struct vnode *devvp;
434 struct uio *uio;
435 int ioflag;
436 {
437 struct sequencer_softc *sc = devvp->v_devcookie;
438 struct sequencer_queue *q = &sc->outq;
439 int error;
440 seq_event_rec cmdbuf;
441 int size;
442
443 DPRINTFN(2, ("sequencerwrite: %p, count=%d\n", sc, (int) uio->uio_resid));
444
445 error = 0;
446 size = sc->mode == SEQ_NEW ? sizeof cmdbuf : SEQOLD_CMDSIZE;
447 while (uio->uio_resid >= size) {
448 error = uiomove(&cmdbuf, size, uio);
449 if (error)
450 break;
451 if (sc->mode == SEQ_OLD)
452 if (seq_to_new(&cmdbuf, uio))
453 continue;
454 if (SEQ_CMD(&cmdbuf) == SEQ_FULLSIZE) {
455 /* We do it like OSS does, asynchronously */
456 error = seq_do_fullsize(sc, &cmdbuf, uio);
457 if (error)
458 break;
459 continue;
460 }
461 while (SEQ_QFULL(q)) {
462 seq_startoutput(sc);
463 if (SEQ_QFULL(q)) {
464 if (ioflag & IO_NDELAY)
465 return EWOULDBLOCK;
466 error = seq_sleep(&sc->wchan, "seq_wr");
467 if (error)
468 return error;
469 }
470 }
471 SEQ_QPUT(q, cmdbuf);
472 }
473 seq_startoutput(sc);
474
475 #ifdef SEQUENCER_DEBUG
476 if (error)
477 DPRINTFN(2, ("sequencerwrite: error=%d\n", error));
478 #endif
479 return error;
480 }
481
482 int
483 sequencerioctl(devvp, cmd, addr, flag, p)
484 struct vnode *devvp;
485 u_long cmd;
486 caddr_t addr;
487 int flag;
488 struct proc *p;
489 {
490 struct sequencer_softc *sc = devvp->v_devcookie;
491 struct synth_info *si;
492 struct midi_dev *md;
493 int devno;
494 int error;
495 int t;
496
497 DPRINTFN(2, ("sequencerioctl: %p cmd=0x%08lx\n", sc, cmd));
498
499 error = 0;
500 switch (cmd) {
501 case FIONBIO:
502 /* All handled in the upper FS layer. */
503 break;
504
505 case FIOASYNC:
506 if (*(int *)addr) {
507 if (sc->async)
508 return EBUSY;
509 sc->async = p;
510 DPRINTF(("sequencer_ioctl: FIOASYNC %p\n", p));
511 } else
512 sc->async = 0;
513 break;
514
515 case SEQUENCER_RESET:
516 seq_reset(sc);
517 break;
518
519 case SEQUENCER_PANIC:
520 seq_reset(sc);
521 /* Do more? OSS doesn't */
522 break;
523
524 case SEQUENCER_SYNC:
525 if (sc->flags == FREAD)
526 return 0;
527 seq_drain(sc);
528 error = 0;
529 break;
530
531 case SEQUENCER_INFO:
532 si = (struct synth_info*)addr;
533 devno = si->device;
534 if (devno < 0 || devno >= sc->nmidi)
535 return EINVAL;
536 md = sc->devs[devno];
537 strncpy(si->name, md->name, sizeof si->name);
538 si->synth_type = SYNTH_TYPE_MIDI;
539 si->synth_subtype = md->subtype;
540 si->nr_voices = md->nr_voices;
541 si->instr_bank_size = md->instr_bank_size;
542 si->capabilities = md->capabilities;
543 break;
544
545 case SEQUENCER_NRSYNTHS:
546 *(int *)addr = sc->nmidi;
547 break;
548
549 case SEQUENCER_NRMIDIS:
550 *(int *)addr = sc->nmidi;
551 break;
552
553 case SEQUENCER_OUTOFBAND:
554 DPRINTFN(3, ("sequencer_ioctl: OOB=%02x %02x %02x %02x %02x %02x %02x %02x\n",
555 *(u_char *)addr, *(u_char *)(addr+1),
556 *(u_char *)(addr+2), *(u_char *)(addr+3),
557 *(u_char *)(addr+4), *(u_char *)(addr+5),
558 *(u_char *)(addr+6), *(u_char *)(addr+7)));
559 error = seq_do_command(sc, (seq_event_rec *)addr);
560 break;
561
562 case SEQUENCER_TMR_TIMEBASE:
563 t = *(int *)addr;
564 if (t < 1)
565 t = 1;
566 if (t > 10000)
567 t = 10000;
568 sc->timer.timebase = t;
569 *(int *)addr = t;
570 RECALC_TICK(&sc->timer);
571 break;
572
573 case SEQUENCER_TMR_START:
574 error = seq_timer(sc, TMR_START, 0, 0);
575 break;
576
577 case SEQUENCER_TMR_STOP:
578 error = seq_timer(sc, TMR_STOP, 0, 0);
579 break;
580
581 case SEQUENCER_TMR_CONTINUE:
582 error = seq_timer(sc, TMR_CONTINUE, 0, 0);
583 break;
584
585 case SEQUENCER_TMR_TEMPO:
586 t = *(int *)addr;
587 if (t < 8)
588 t = 8;
589 if (t > 250)
590 t = 250;
591 sc->timer.tempo = t;
592 *(int *)addr = t;
593 RECALC_TICK(&sc->timer);
594 break;
595
596 case SEQUENCER_TMR_SOURCE:
597 *(int *)addr = SEQUENCER_TMR_INTERNAL;
598 break;
599
600 case SEQUENCER_TMR_METRONOME:
601 /* noop */
602 break;
603
604 case SEQUENCER_THRESHOLD:
605 t = SEQ_MAXQ - *(int *)addr / sizeof (seq_event_rec);
606 if (t < 1)
607 t = 1;
608 if (t > SEQ_MAXQ)
609 t = SEQ_MAXQ;
610 sc->lowat = t;
611 break;
612
613 case SEQUENCER_CTRLRATE:
614 *(int *)addr = (sc->timer.tempo*sc->timer.timebase + 30) / 60;
615 break;
616
617 case SEQUENCER_GETTIME:
618 {
619 struct timeval now;
620 u_long t;
621 microtime(&now);
622 SUBTIMEVAL(&now, &sc->timer.start);
623 t = now.tv_sec * 1000000 + now.tv_usec;
624 t /= sc->timer.tick;
625 *(int *)addr = t;
626 break;
627 }
628
629 default:
630 DPRINTFN(-1,("sequencer_ioctl: unimpl %08lx\n", cmd));
631 error = EINVAL;
632 break;
633 }
634 return error;
635 }
636
637 int
638 sequencerpoll(devvp, events, p)
639 struct vnode *devvp;
640 int events;
641 struct proc *p;
642 {
643 struct sequencer_softc *sc = devvp->v_devcookie;
644 int revents = 0;
645
646 DPRINTF(("sequencerpoll: %p events=0x%x\n", sc, events));
647
648 if (events & (POLLIN | POLLRDNORM))
649 if (!SEQ_QEMPTY(&sc->inq))
650 revents |= events & (POLLIN | POLLRDNORM);
651
652 if (events & (POLLOUT | POLLWRNORM))
653 if (SEQ_QLEN(&sc->outq) < sc->lowat)
654 revents |= events & (POLLOUT | POLLWRNORM);
655
656 if (revents == 0) {
657 if (events & (POLLIN | POLLRDNORM))
658 selrecord(p, &sc->rsel);
659
660 if (events & (POLLOUT | POLLWRNORM))
661 selrecord(p, &sc->wsel);
662 }
663
664 return revents;
665 }
666
667 void
668 seq_reset(sc)
669 struct sequencer_softc *sc;
670 {
671 int i, chn;
672 struct midi_dev *md;
673
674 for (i = 0; i < sc->nmidi; i++) {
675 md = sc->devs[i];
676 midiseq_reset(md);
677 for (chn = 0; chn < MAXCHAN; chn++) {
678 midiseq_ctlchange(md, chn, MIDI_CTRL_ALLOFF, 0);
679 midiseq_ctlchange(md, chn, MIDI_CTRL_RESET, 0);
680 midiseq_pitchbend(md, chn, MIDI_BEND_NEUTRAL);
681 }
682 }
683 }
684
685 int
686 seq_do_command(sc, b)
687 struct sequencer_softc *sc;
688 seq_event_rec *b;
689 {
690 int dev;
691
692 DPRINTFN(4, ("seq_do_command: %p cmd=0x%02x\n", sc, SEQ_CMD(b)));
693
694 switch(SEQ_CMD(b)) {
695 case SEQ_LOCAL:
696 return seq_do_local(sc, b);
697 case SEQ_TIMING:
698 return seq_do_timing(sc, b);
699 case SEQ_CHN_VOICE:
700 return seq_do_chnvoice(sc, b);
701 case SEQ_CHN_COMMON:
702 return seq_do_chncommon(sc, b);
703 case SEQ_SYSEX:
704 return seq_do_sysex(sc, b);
705 /* COMPAT */
706 case SEQOLD_MIDIPUTC:
707 dev = b->arr[2];
708 if (dev < 0 || dev >= sc->nmidi)
709 return (ENXIO);
710 return midiseq_putc(sc->devs[dev], b->arr[1]);
711 default:
712 DPRINTFN(-1,("seq_do_command: unimpl command %02x\n",
713 SEQ_CMD(b)));
714 return (EINVAL);
715 }
716 }
717
718 int
719 seq_do_chnvoice(sc, b)
720 struct sequencer_softc *sc;
721 seq_event_rec *b;
722 {
723 int cmd, dev, chan, note, parm, voice;
724 int error;
725 struct midi_dev *md;
726
727 dev = SEQ_EDEV(b);
728 if (dev < 0 || dev >= sc->nmidi)
729 return ENXIO;
730 md = sc->devs[dev];
731 cmd = SEQ_ECMD(b);
732 chan = SEQ_ECHAN(b);
733 note = SEQ_ENOTE(b);
734 parm = SEQ_EPARM(b);
735 DPRINTFN(2,("seq_do_chnvoice: cmd=%02x dev=%d chan=%d note=%d parm=%d\n",
736 cmd, dev, chan, note, parm));
737 voice = chan;
738 if (cmd == MIDI_NOTEON && parm == 0) {
739 cmd = MIDI_NOTEOFF;
740 parm = MIDI_HALF_VEL;
741 }
742 switch(cmd) {
743 case MIDI_NOTEON:
744 DPRINTFN(5, ("seq_do_chnvoice: noteon %p %d %d %d\n",
745 md, voice, note, parm));
746 error = midiseq_noteon(md, voice, note, parm);
747 break;
748 case MIDI_NOTEOFF:
749 error = midiseq_noteoff(md, voice, note, parm);
750 break;
751 case MIDI_KEY_PRESSURE:
752 error = midiseq_keypressure(md, voice, note, parm);
753 break;
754 default:
755 DPRINTFN(-1,("seq_do_chnvoice: unimpl command %02x\n", cmd));
756 error = EINVAL;
757 break;
758 }
759 return error;
760 }
761
762 int
763 seq_do_chncommon(sc, b)
764 struct sequencer_softc *sc;
765 seq_event_rec *b;
766 {
767 int cmd, dev, chan, p1, w14;
768 int error;
769 struct midi_dev *md;
770 union {
771 int16_t s;
772 u_int8_t b[2];
773 } u;
774
775 dev = SEQ_EDEV(b);
776 if (dev < 0 || dev >= sc->nmidi)
777 return ENXIO;
778 md = sc->devs[dev];
779 cmd = SEQ_ECMD(b);
780 chan = SEQ_ECHAN(b);
781 p1 = SEQ_EP1(b);
782 u.b[0] = b->arr[6];
783 u.b[1] = b->arr[7];
784 w14 = u.s;
785 DPRINTFN(2,("seq_do_chncommon: %02x\n", cmd));
786
787 error = 0;
788 switch(cmd) {
789 case MIDI_PGM_CHANGE:
790 error = midiseq_pgmchange(md, chan, p1);
791 break;
792 case MIDI_CTL_CHANGE:
793 if (chan > 15 || p1 > 127)
794 return 0; /* EINVAL */
795 error = midiseq_ctlchange(md, chan, p1, w14);
796 break;
797 case MIDI_PITCH_BEND:
798 error = midiseq_pitchbend(md, chan, w14);
799 break;
800 case MIDI_CHN_PRESSURE:
801 error = midiseq_chnpressure(md, chan, p1);
802 break;
803 default:
804 DPRINTFN(-1,("seq_do_chncommon: unimpl command %02x\n", cmd));
805 error = EINVAL;
806 break;
807 }
808 return (error);
809 }
810
811 int
812 seq_do_timing(sc, b)
813 struct sequencer_softc *sc;
814 seq_event_rec *b;
815 {
816 union {
817 int32_t i;
818 u_int8_t b[4];
819 } u;
820 u.b[0] = b->arr[4];
821 u.b[1] = b->arr[5];
822 u.b[2] = b->arr[6];
823 u.b[3] = b->arr[7];
824 return seq_timer(sc, SEQ_TCMD(b), u.i, b);
825 }
826
827 int
828 seq_do_local(sc, b)
829 struct sequencer_softc *sc;
830 seq_event_rec *b;
831 {
832 return (EINVAL);
833 }
834
835 int
836 seq_do_sysex(sc, b)
837 struct sequencer_softc *sc;
838 seq_event_rec *b;
839 {
840 int dev, i;
841 struct midi_dev *md;
842 u_int8_t c, *buf = &b->arr[2];
843
844 dev = SEQ_EDEV(b);
845 if (dev < 0 || dev >= sc->nmidi)
846 return (ENXIO);
847 DPRINTF(("seq_do_sysex: dev=%d\n", dev));
848 md = sc->devs[dev];
849
850 if (!sc->doingsysex) {
851 c = MIDI_SYSEX_START;
852 midiseq_out(md, &c, 1, 0);
853 sc->doingsysex = 1;
854 }
855
856 for (i = 0; i < 6 && buf[i] != 0xff; i++)
857 ;
858 midiseq_out(md, buf, i, 0);
859 if (i < 6 || (i > 0 && buf[i-1] == MIDI_SYSEX_END))
860 sc->doingsysex = 0;
861 return (0);
862 }
863
864 int
865 seq_timer(sc, cmd, parm, b)
866 struct sequencer_softc *sc;
867 int cmd, parm;
868 seq_event_rec *b;
869 {
870 struct syn_timer *t = &sc->timer;
871 struct timeval when;
872 int ticks;
873 int error;
874 long long usec;
875
876 DPRINTFN(2,("seq_timer: %02x %d\n", cmd, parm));
877
878 error = 0;
879 switch(cmd) {
880 case TMR_WAIT_REL:
881 parm += t->last;
882 /* fall into */
883 case TMR_WAIT_ABS:
884 t->last = parm;
885 usec = (long long)parm * (long long)t->tick; /* convert to usec */
886 when.tv_sec = usec / 1000000;
887 when.tv_usec = usec % 1000000;
888 DPRINTFN(4, ("seq_timer: parm=%d, sleep when=%ld.%06ld", parm,
889 when.tv_sec, when.tv_usec));
890 ADDTIMEVAL(&when, &t->start); /* abstime for end */
891 ticks = hzto(&when);
892 DPRINTFN(4, (" when+start=%ld.%06ld, tick=%d\n",
893 when.tv_sec, when.tv_usec, ticks));
894 if (ticks > 0) {
895 #ifdef DIAGNOSTIC
896 if (ticks > 20 * hz) {
897 /* Waiting more than 20s */
898 printf("seq_timer: funny ticks=%d, usec=%lld, parm=%d, tick=%ld\n",
899 ticks, usec, parm, t->tick);
900 }
901 #endif
902 sc->timeout = 1;
903 callout_reset(&sc->sc_callout, ticks,
904 seq_timeout, sc);
905 }
906 #ifdef SEQUENCER_DEBUG
907 else if (tick < 0)
908 DPRINTF(("seq_timer: ticks = %d\n", ticks));
909 #endif
910 break;
911 case TMR_START:
912 microtime(&t->start);
913 t->running = 1;
914 break;
915 case TMR_STOP:
916 microtime(&t->stop);
917 t->running = 0;
918 break;
919 case TMR_CONTINUE:
920 microtime(&when);
921 SUBTIMEVAL(&when, &t->stop);
922 ADDTIMEVAL(&t->start, &when);
923 t->running = 1;
924 break;
925 case TMR_TEMPO:
926 /* parm is ticks per minute / timebase */
927 if (parm < 8)
928 parm = 8;
929 if (parm > 360)
930 parm = 360;
931 t->tempo = parm;
932 RECALC_TICK(t);
933 break;
934 case TMR_ECHO:
935 error = seq_input_event(sc, b);
936 break;
937 case TMR_RESET:
938 t->last = 0;
939 microtime(&t->start);
940 break;
941 default:
942 DPRINTF(("seq_timer: unknown %02x\n", cmd));
943 error = EINVAL;
944 break;
945 }
946 return (error);
947 }
948
949 int
950 seq_do_fullsize(sc, b, uio)
951 struct sequencer_softc *sc;
952 seq_event_rec *b;
953 struct uio *uio;
954 {
955 struct sysex_info sysex;
956 u_int dev;
957
958 #ifdef DIAGNOSTIC
959 if (sizeof(seq_event_rec) != SEQ_SYSEX_HDRSIZE) {
960 printf("seq_do_fullsize: sysex size ??\n");
961 return EINVAL;
962 }
963 #endif
964 memcpy(&sysex, b, sizeof sysex);
965 dev = sysex.device_no;
966 DPRINTFN(2, ("seq_do_fullsize: fmt=%04x, dev=%d, len=%d\n",
967 sysex.key, dev, sysex.len));
968 return (midiseq_loadpatch(sc->devs[dev], &sysex, uio));
969 }
970
971 /* Convert an old sequencer event to a new one. */
972 int
973 seq_to_new(ev, uio)
974 seq_event_rec *ev;
975 struct uio *uio;
976 {
977 int cmd, chan, note, parm;
978 u_int32_t delay;
979 int error;
980
981 cmd = SEQ_CMD(ev);
982 chan = ev->arr[1];
983 note = ev->arr[2];
984 parm = ev->arr[3];
985 DPRINTFN(3, ("seq_to_new: 0x%02x %d %d %d\n", cmd, chan, note, parm));
986
987 if (cmd >= 0x80) {
988 /* Fill the event record */
989 if (uio->uio_resid >= sizeof *ev - SEQOLD_CMDSIZE) {
990 error = uiomove(&ev->arr[SEQOLD_CMDSIZE],
991 sizeof *ev - SEQOLD_CMDSIZE, uio);
992 if (error)
993 return error;
994 } else
995 return EINVAL;
996 }
997
998 switch(cmd) {
999 case SEQOLD_NOTEOFF:
1000 note = 255;
1001 SEQ_ECMD(ev) = MIDI_NOTEOFF;
1002 goto onoff;
1003 case SEQOLD_NOTEON:
1004 SEQ_ECMD(ev) = MIDI_NOTEON;
1005 onoff:
1006 SEQ_CMD(ev) = SEQ_CHN_VOICE;
1007 SEQ_EDEV(ev) = 0;
1008 SEQ_ECHAN(ev) = chan;
1009 SEQ_ENOTE(ev) = note;
1010 SEQ_EPARM(ev) = parm;
1011 break;
1012 case SEQOLD_WAIT:
1013 delay = *(u_int32_t *)ev->arr >> 8;
1014 SEQ_CMD(ev) = SEQ_TIMING;
1015 SEQ_TCMD(ev) = TMR_WAIT_REL;
1016 *(u_int32_t *)&ev->arr[4] = delay;
1017 break;
1018 case SEQOLD_SYNCTIMER:
1019 SEQ_CMD(ev) = SEQ_TIMING;
1020 SEQ_TCMD(ev) = TMR_RESET;
1021 break;
1022 case SEQOLD_PGMCHANGE:
1023 SEQ_ECMD(ev) = MIDI_PGM_CHANGE;
1024 SEQ_CMD(ev) = SEQ_CHN_COMMON;
1025 SEQ_EDEV(ev) = 0;
1026 SEQ_ECHAN(ev) = chan;
1027 SEQ_EP1(ev) = note;
1028 break;
1029 case SEQOLD_MIDIPUTC:
1030 break; /* interpret in normal mode */
1031 case SEQOLD_ECHO:
1032 case SEQOLD_PRIVATE:
1033 case SEQOLD_EXTENDED:
1034 default:
1035 DPRINTF(("seq_to_new: not impl 0x%02x\n", cmd));
1036 return EINVAL;
1037 /* In case new events show up */
1038 case SEQ_TIMING:
1039 case SEQ_CHN_VOICE:
1040 case SEQ_CHN_COMMON:
1041 case SEQ_FULLSIZE:
1042 break;
1043 }
1044 return 0;
1045 }
1046
1047 /**********************************************/
1048
1049 void
1050 midiseq_in(md, msg, len)
1051 struct midi_dev *md;
1052 u_char *msg;
1053 int len;
1054 {
1055 int unit = md->unit;
1056 seq_event_rec ev;
1057 int status, chan;
1058
1059 DPRINTFN(2, ("midiseq_in: %p %02x %02x %02x\n",
1060 md, msg[0], msg[1], msg[2]));
1061
1062 status = MIDI_GET_STATUS(msg[0]);
1063 chan = MIDI_GET_CHAN(msg[0]);
1064 switch (status) {
1065 case MIDI_NOTEON:
1066 if (msg[2] == 0) {
1067 status = MIDI_NOTEOFF;
1068 msg[2] = MIDI_HALF_VEL;
1069 }
1070 /* fall into */
1071 case MIDI_NOTEOFF:
1072 case MIDI_KEY_PRESSURE:
1073 SEQ_MK_CHN_VOICE(&ev, unit, status, chan, msg[1], msg[2]);
1074 break;
1075 case MIDI_CTL_CHANGE:
1076 SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, msg[2]);
1077 break;
1078 case MIDI_PGM_CHANGE:
1079 case MIDI_CHN_PRESSURE:
1080 SEQ_MK_CHN_COMMON(&ev, unit, status, chan, msg[1], 0, 0);
1081 break;
1082 case MIDI_PITCH_BEND:
1083 SEQ_MK_CHN_COMMON(&ev, unit, status, chan, 0, 0,
1084 (msg[1] & 0x7f) | ((msg[2] & 0x7f) << 7));
1085 break;
1086 default:
1087 return;
1088 }
1089 seq_event_intr(md->seq, &ev);
1090 }
1091
1092 /* XXXDEVVP */
1093 static int
1094 midiseq_major(void)
1095 {
1096 static int maj = -1;
1097 int i;
1098
1099 if (maj == -1) {
1100 for (i = 0; i < nchrdev; i++) {
1101 if (cdevsw[i].d_open == midiopen) {
1102 maj = i;
1103 break;
1104 }
1105 }
1106 }
1107
1108 return (maj);
1109 }
1110
1111 struct midi_dev *
1112 midiseq_open(unit, flags, p)
1113 int unit;
1114 int flags;
1115 struct proc *p;
1116 {
1117 int error, maj;
1118 struct vnode *vp;
1119 struct midi_dev *md;
1120 struct midi_softc *sc;
1121 struct midi_info mi;
1122
1123 /* XXX DEVVP */
1124
1125 maj = midiseq_major();
1126 if (maj == -1)
1127 return NULL;
1128 if ((error = cdevvp(makedev(maj, unit), &vp)) != 0)
1129 return NULL;
1130
1131 DPRINTFN(2, ("midiseq_open: %d %d\n", unit, flags));
1132 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1133 error = VOP_OPEN(vp, flags, p->p_ucred, p, NULL);
1134 if (error != 0) {
1135 vput(vp);
1136 return NULL;
1137 }
1138 VOP_UNLOCK(vp, 0);
1139 sc = vp->v_devcookie;
1140 sc->seqopen = 1;
1141 md = malloc(sizeof *md, M_DEVBUF, M_WAITOK);
1142 sc->seq_md = md;
1143 memset(md, 0, sizeof *md);
1144 md->msc = sc;
1145 midi_getinfo(makedev(0, unit), &mi);
1146 md->unit = unit;
1147 md->name = mi.name;
1148 md->subtype = 0;
1149 md->nr_voices = 128; /* XXX */
1150 md->instr_bank_size = 128; /* XXX */
1151 md->devvp = vp;
1152 if (mi.props & MIDI_PROP_CAN_INPUT)
1153 md->capabilities |= SYNTH_CAP_INPUT;
1154 return (md);
1155 }
1156
1157 void
1158 midiseq_close(md, flag, p)
1159 struct midi_dev *md;
1160 int flag;
1161 struct proc *p;
1162 {
1163 struct vnode *vp;
1164
1165 vn_lock(md->devvp, LK_EXCLUSIVE | LK_RETRY);
1166 VOP_CLOSE(md->devvp, flag, p->p_ucred, p);
1167 vput(vp);
1168 free(md, M_DEVBUF);
1169 }
1170
1171 void
1172 midiseq_reset(md)
1173 struct midi_dev *md;
1174 {
1175 /* XXX send GM reset? */
1176 DPRINTFN(3, ("midiseq_reset: %d\n", md->unit));
1177 }
1178
1179 int
1180 midiseq_out(md, buf, cc, chk)
1181 struct midi_dev *md;
1182 u_char *buf;
1183 u_int cc;
1184 int chk;
1185 {
1186 DPRINTFN(5, ("midiseq_out: m=%p, unit=%d, buf[0]=0x%02x, cc=%d\n",
1187 md->msc, md->unit, buf[0], cc));
1188
1189 /* The MIDI "status" byte does not have to be repeated. */
1190 if (chk && md->last_cmd == buf[0])
1191 buf++, cc--;
1192 else
1193 md->last_cmd = buf[0];
1194 return midi_writebytes(md->unit, buf, cc);
1195 }
1196
1197 int
1198 midiseq_noteon(md, chan, note, vel)
1199 struct midi_dev *md;
1200 int chan, note, vel;
1201 {
1202 u_char buf[3];
1203
1204 DPRINTFN(6, ("midiseq_noteon 0x%02x %d %d\n",
1205 MIDI_NOTEON | chan, note, vel));
1206 if (chan < 0 || chan > 15 ||
1207 note < 0 || note > 127)
1208 return EINVAL;
1209 if (vel < 0) vel = 0;
1210 if (vel > 127) vel = 127;
1211 buf[0] = MIDI_NOTEON | chan;
1212 buf[1] = note;
1213 buf[2] = vel;
1214 return midiseq_out(md, buf, 3, 1);
1215 }
1216
1217 int
1218 midiseq_noteoff(md, chan, note, vel)
1219 struct midi_dev *md;
1220 int chan, note, vel;
1221 {
1222 u_char buf[3];
1223
1224 if (chan < 0 || chan > 15 ||
1225 note < 0 || note > 127)
1226 return EINVAL;
1227 if (vel < 0) vel = 0;
1228 if (vel > 127) vel = 127;
1229 buf[0] = MIDI_NOTEOFF | chan;
1230 buf[1] = note;
1231 buf[2] = vel;
1232 return midiseq_out(md, buf, 3, 1);
1233 }
1234
1235 int
1236 midiseq_keypressure(md, chan, note, vel)
1237 struct midi_dev *md;
1238 int chan, note, vel;
1239 {
1240 u_char buf[3];
1241
1242 if (chan < 0 || chan > 15 ||
1243 note < 0 || note > 127)
1244 return EINVAL;
1245 if (vel < 0) vel = 0;
1246 if (vel > 127) vel = 127;
1247 buf[0] = MIDI_KEY_PRESSURE | chan;
1248 buf[1] = note;
1249 buf[2] = vel;
1250 return midiseq_out(md, buf, 3, 1);
1251 }
1252
1253 int
1254 midiseq_pgmchange(md, chan, parm)
1255 struct midi_dev *md;
1256 int chan, parm;
1257 {
1258 u_char buf[2];
1259
1260 if (chan < 0 || chan > 15 ||
1261 parm < 0 || parm > 127)
1262 return EINVAL;
1263 buf[0] = MIDI_PGM_CHANGE | chan;
1264 buf[1] = parm;
1265 return midiseq_out(md, buf, 2, 1);
1266 }
1267
1268 int
1269 midiseq_chnpressure(md, chan, parm)
1270 struct midi_dev *md;
1271 int chan, parm;
1272 {
1273 u_char buf[2];
1274
1275 if (chan < 0 || chan > 15 ||
1276 parm < 0 || parm > 127)
1277 return EINVAL;
1278 buf[0] = MIDI_CHN_PRESSURE | chan;
1279 buf[1] = parm;
1280 return midiseq_out(md, buf, 2, 1);
1281 }
1282
1283 int
1284 midiseq_ctlchange(md, chan, parm, w14)
1285 struct midi_dev *md;
1286 int chan, parm, w14;
1287 {
1288 u_char buf[3];
1289
1290 if (chan < 0 || chan > 15 ||
1291 parm < 0 || parm > 127)
1292 return EINVAL;
1293 buf[0] = MIDI_CTL_CHANGE | chan;
1294 buf[1] = parm;
1295 buf[2] = w14 & 0x7f;
1296 return midiseq_out(md, buf, 3, 1);
1297 }
1298
1299 int
1300 midiseq_pitchbend(md, chan, parm)
1301 struct midi_dev *md;
1302 int chan, parm;
1303 {
1304 u_char buf[3];
1305
1306 if (chan < 0 || chan > 15)
1307 return EINVAL;
1308 buf[0] = MIDI_PITCH_BEND | chan;
1309 buf[1] = parm & 0x7f;
1310 buf[2] = (parm >> 7) & 0x7f;
1311 return midiseq_out(md, buf, 3, 1);
1312 }
1313
1314 int
1315 midiseq_loadpatch(md, sysex, uio)
1316 struct midi_dev *md;
1317 struct sysex_info *sysex;
1318 struct uio *uio;
1319 {
1320 u_char c, buf[128];
1321 int i, cc, error;
1322
1323 if (sysex->key != SEQ_SYSEX_PATCH) {
1324 DPRINTFN(-1,("midiseq_loadpatch: bad patch key 0x%04x\n",
1325 sysex->key));
1326 return (EINVAL);
1327 }
1328 if (uio->uio_resid < sysex->len)
1329 /* adjust length, should be an error */
1330 sysex->len = uio->uio_resid;
1331
1332 DPRINTFN(2, ("midiseq_loadpatch: len=%d\n", sysex->len));
1333 if (sysex->len == 0)
1334 return EINVAL;
1335 error = uiomove(&c, 1, uio);
1336 if (error)
1337 return error;
1338 if (c != MIDI_SYSEX_START) /* must start like this */
1339 return EINVAL;
1340 error = midiseq_out(md, &c, 1, 0);
1341 if (error)
1342 return error;
1343 --sysex->len;
1344 while (sysex->len > 0) {
1345 cc = sysex->len;
1346 if (cc > sizeof buf)
1347 cc = sizeof buf;
1348 error = uiomove(buf, cc, uio);
1349 if (error)
1350 break;
1351 for(i = 0; i < cc && !MIDI_IS_STATUS(buf[i]); i++)
1352 ;
1353 error = midiseq_out(md, buf, i, 0);
1354 if (error)
1355 break;
1356 sysex->len -= i;
1357 if (i != cc)
1358 break;
1359 }
1360 /* Any leftover data in uio is rubbish;
1361 * the SYSEX should be one write ending in SYSEX_END.
1362 */
1363 uio->uio_resid = 0;
1364 c = MIDI_SYSEX_END;
1365 return midiseq_out(md, &c, 1, 0);
1366 }
1367
1368 int
1369 midiseq_putc(md, data)
1370 struct midi_dev *md;
1371 int data;
1372 {
1373 u_char c = data;
1374 DPRINTFN(4,("midiseq_putc: 0x%02x\n", data));
1375 return midiseq_out(md, &c, 1, 0);
1376 }
1377
1378 #include "midi.h"
1379 #if NMIDI == 0
1380 /*
1381 * If someone has a sequencer, but no midi devices there will
1382 * be unresolved references, so we provide little stubs.
1383 */
1384
1385 int
1386 midiopen(struct vnode *devvp, int flags, int ifmt, struct proc *p)
1387 {
1388 return (ENXIO);
1389 }
1390
1391
1392 int
1393 midi_unit_count()
1394 {
1395 return (0);
1396 }
1397
1398 void
1399 midi_getinfo(dev, mi)
1400 dev_t dev;
1401 struct midi_info *mi;
1402 {
1403 }
1404
1405 int
1406 midi_writebytes(unit, buf, cc)
1407 int unit;
1408 u_char *buf;
1409 int cc;
1410 {
1411 return (ENXIO);
1412 }
1413 #endif /* NMIDI == 0 */
1414
1415 #endif /* NSEQUENCER > 0 */
1416
1417