summaryrefslogtreecommitdiff
path: root/sound/oss/emu10k1/cardmi.c
blob: 57674f8c8a2e73f1f5a5dc95ec90d87d63393271 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/*
 **********************************************************************
 *     sblive_mi.c - MIDI UART input HAL for emu10k1 driver
 *     Copyright 1999, 2000 Creative Labs, Inc.
 *
 **********************************************************************
 *
 *     Date                 Author          Summary of changes
 *     ----                 ------          ------------------
 *     October 20, 1999     Bertrand Lee    base code release
 *     November 2, 1999     Alan Cox        clean up
 *
 **********************************************************************
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this program; if not, write to the Free
 *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *     USA.
 *
 **********************************************************************
 */

#include <linux/slab.h>
#include <linux/jiffies.h>

#include "hwaccess.h"
#include "8010.h"
#include "cardmi.h"
#include "irqmgr.h"


static int emu10k1_mpuin_callback(struct emu10k1_mpuin *card_mpuin, u32 msg, unsigned long data, u32 bytesvalid);

static int sblive_miStateInit(struct emu10k1_mpuin *);
static int sblive_miStateEntry(struct emu10k1_mpuin *, u8);
static int sblive_miStateParse(struct emu10k1_mpuin *, u8);
static int sblive_miState3Byte(struct emu10k1_mpuin *, u8);
static int sblive_miState3ByteKey(struct emu10k1_mpuin *, u8);
static int sblive_miState3ByteVel(struct emu10k1_mpuin *, u8);
static int sblive_miState2Byte(struct emu10k1_mpuin *, u8);
static int sblive_miState2ByteKey(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysCommon2(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysCommon2Key(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysCommon3(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysCommon3Key(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysCommon3Vel(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysExNorm(struct emu10k1_mpuin *, u8);
static int sblive_miStateSysReal(struct emu10k1_mpuin *, u8);


static struct {
	int (*Fn) (struct emu10k1_mpuin *, u8);
} midistatefn[] = {

	{
	sblive_miStateParse}, {
	sblive_miState3Byte},	/* 0x8n, 0x9n, 0xAn, 0xBn, 0xEn */
	{
	sblive_miState3ByteKey},	/* Byte 1                       */
	{
	sblive_miState3ByteVel},	/* Byte 2                       */
	{
	sblive_miState2Byte},	/* 0xCn, 0xDn                   */
	{
	sblive_miState2ByteKey},	/* Byte 1                       */
	{
	sblive_miStateSysCommon2},	/* 0xF1 , 0xF3                  */
	{
	sblive_miStateSysCommon2Key},	/* 0xF1 , 0xF3, Byte 1          */
	{
	sblive_miStateSysCommon3},	/* 0xF2                         */
	{
	sblive_miStateSysCommon3Key},	/* 0xF2 , Byte 1                */
	{
	sblive_miStateSysCommon3Vel},	/* 0xF2 , Byte 2                */
	{
	sblive_miStateSysExNorm},	/* 0xF0, 0xF7, Normal mode      */
	{
	sblive_miStateSysReal}	/* 0xF4 - 0xF6 ,0xF8 - 0xFF     */
};


/* Installs the IRQ handler for the MPU in port                 */

/* and initialize parameters                                    */

int emu10k1_mpuin_open(struct emu10k1_card *card, struct midi_openinfo *openinfo)
{
	struct emu10k1_mpuin *card_mpuin = card->mpuin;

	DPF(2, "emu10k1_mpuin_open\n");

	if (!(card_mpuin->status & FLAGS_AVAILABLE))
		return -1;

	/* Copy open info and mark channel as in use */
	card_mpuin->openinfo = *openinfo;
	card_mpuin->status &= ~FLAGS_AVAILABLE;	/* clear */
	card_mpuin->status |= FLAGS_READY;	/* set */
	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */
	card_mpuin->firstmidiq = NULL;
	card_mpuin->lastmidiq = NULL;
	card_mpuin->qhead = 0;
	card_mpuin->qtail = 0;

	sblive_miStateInit(card_mpuin);

	emu10k1_mpu_reset(card);
	emu10k1_mpu_acquire(card);

	return 0;
}

int emu10k1_mpuin_close(struct emu10k1_card *card)
{
	struct emu10k1_mpuin *card_mpuin = card->mpuin;

	DPF(2, "emu10k1_mpuin_close()\n");

	/* Check if there are pending input SysEx buffers */
	if (card_mpuin->firstmidiq != NULL) {
		ERROR();
		return -1;
	}

	/* Disable RX interrupt */
	emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);

	emu10k1_mpu_release(card);

	card_mpuin->status |= FLAGS_AVAILABLE;	/* set */
	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */

	return 0;
}

/* Adds MIDI buffer to local queue list                         */

int emu10k1_mpuin_add_buffer(struct emu10k1_mpuin *card_mpuin, struct midi_hdr *midihdr)
{
	struct midi_queue *midiq;
	unsigned long flags;

	DPF(2, "emu10k1_mpuin_add_buffer()\n");

	/* Update MIDI buffer flags */
	midihdr->flags |= MIDIBUF_INQUEUE;	/* set */
	midihdr->flags &= ~MIDIBUF_DONE;	/* clear */

	if ((midiq = kmalloc(sizeof(struct midi_queue), GFP_ATOMIC)) == NULL) {
		/* Message lost */
		return -1;
	}

	midiq->next = NULL;
	midiq->qtype = 1;
	midiq->length = midihdr->bufferlength;
	midiq->sizeLeft = midihdr->bufferlength;
	midiq->midibyte = midihdr->data;
	midiq->refdata = (unsigned long) midihdr;

	spin_lock_irqsave(&card_mpuin->lock, flags);

	if (card_mpuin->firstmidiq == NULL) {
		card_mpuin->firstmidiq = midiq;
		card_mpuin->lastmidiq = midiq;
	} else {
		(card_mpuin->lastmidiq)->next = midiq;
		card_mpuin->lastmidiq = midiq;
	}

	spin_unlock_irqrestore(&card_mpuin->lock, flags);

	return 0;
}

/* First set the Time Stamp if MIDI IN has not started.         */

/* Then enable RX Irq.                                          */

int emu10k1_mpuin_start(struct emu10k1_card *card)
{
	struct emu10k1_mpuin *card_mpuin = card->mpuin;
	u8 dummy;

	DPF(2, "emu10k1_mpuin_start()\n");

	/* Set timestamp if not set */
	if (card_mpuin->status & FLAGS_MIDM_STARTED) {
		DPF(2, "Time Stamp not changed\n");
	} else {
		while (!emu10k1_mpu_read_data(card, &dummy));

		card_mpuin->status |= FLAGS_MIDM_STARTED;	/* set */

		/* Set new time stamp */
		card_mpuin->timestart = (jiffies * 1000) / HZ;
		DPD(2, "New Time Stamp = %d\n", card_mpuin->timestart);

		card_mpuin->qhead = 0;
		card_mpuin->qtail = 0;

		emu10k1_irq_enable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);
	}

	return 0;
}

/* Disable the RX Irq.  If a partial recorded buffer            */

/* exist, send it up to IMIDI level.                            */

int emu10k1_mpuin_stop(struct emu10k1_card *card)
{
	struct emu10k1_mpuin *card_mpuin = card->mpuin;
	struct midi_queue *midiq;
	unsigned long flags;

	DPF(2, "emu10k1_mpuin_stop()\n");

	emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);

	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */

	if (card_mpuin->firstmidiq) {
		spin_lock_irqsave(&card_mpuin->lock, flags);

		midiq = card_mpuin->firstmidiq;
		if (midiq != NULL) {
			if (midiq->sizeLeft == midiq->length)
				midiq = NULL;
			else {
				card_mpuin->firstmidiq = midiq->next;
				if (card_mpuin->firstmidiq == NULL)
					card_mpuin->lastmidiq = NULL;
			}
		}

		spin_unlock_irqrestore(&card_mpuin->lock, flags);

		if (midiq) {
			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
			kfree(midiq);
		}
	}

	return 0;
}

/* Disable the RX Irq.  If any buffer                           */

/* exist, send it up to IMIDI level.                            */
int emu10k1_mpuin_reset(struct emu10k1_card *card)
{
	struct emu10k1_mpuin *card_mpuin = card->mpuin;
	struct midi_queue *midiq;

	DPF(2, "emu10k1_mpuin_reset()\n");

	emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);

	while (card_mpuin->firstmidiq) {
		midiq = card_mpuin->firstmidiq;
		card_mpuin->firstmidiq = midiq->next;

		if (midiq->sizeLeft == midiq->length)
			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
		else
			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);

		kfree(midiq);
	}

	card_mpuin->lastmidiq = NULL;
	card_mpuin->status &= ~FLAGS_MIDM_STARTED;

	return 0;
}

/* Passes the message with the data back to the client          */

/* via IRQ & DPC callbacks to Ring 3                            */
static int emu10k1_mpuin_callback(struct emu10k1_mpuin *card_mpuin, u32 msg, unsigned long data, u32 bytesvalid)
{
	unsigned long timein;
	struct midi_queue *midiq;
	unsigned long callback_msg[3];
	struct midi_hdr *midihdr;

	/* Called during ISR. The data & code touched are:
	 * 1. card_mpuin
	 * 2. The function to be called
	 */

	timein = card_mpuin->timein;
	if (card_mpuin->timestart <= timein)
		callback_msg[0] = timein - card_mpuin->timestart;
	else
		callback_msg[0] = (~0x0L - card_mpuin->timestart) + timein;

	if (msg == ICARDMIDI_INDATA || msg == ICARDMIDI_INDATAERROR) {
		callback_msg[1] = data;
		callback_msg[2] = bytesvalid;
		DPD(2, "emu10k1_mpuin_callback: midimsg = %#lx\n", data);
	} else {
		midiq = (struct midi_queue *) data;
		midihdr = (struct midi_hdr *) midiq->refdata;

		callback_msg[1] = midiq->length - midiq->sizeLeft;
		callback_msg[2] = midiq->refdata;
		midihdr->flags &= ~MIDIBUF_INQUEUE;
		midihdr->flags |= MIDIBUF_DONE;

		midihdr->bytesrecorded = midiq->length - midiq->sizeLeft;
	}

	/* Notify client that Sysex buffer has been sent */
	emu10k1_midi_callback(msg, card_mpuin->openinfo.refdata, callback_msg);

	return 0;
}

void emu10k1_mpuin_bh(unsigned long refdata)
{
	u8 data;
	unsigned idx;
	struct emu10k1_mpuin *card_mpuin = (struct emu10k1_mpuin *) refdata;
	unsigned long flags;

	while (card_mpuin->qhead != card_mpuin->qtail) {
		spin_lock_irqsave(&card_mpuin->lock, flags);
		idx = card_mpuin->qhead;
		data = card_mpuin->midiq[idx].data;
		card_mpuin->timein = card_mpuin->midiq[idx].timein;
		idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
		card_mpuin->qhead = idx;
		spin_unlock_irqrestore(&card_mpuin->lock, flags);

		sblive_miStateEntry(card_mpuin, data);
	}

	return;
}

/* IRQ callback handler routine for the MPU in port */

int emu10k1_mpuin_irqhandler(struct emu10k1_card *card)
{
	unsigned idx;
	unsigned count;
	u8 MPUIvalue;
	struct emu10k1_mpuin *card_mpuin = card->mpuin;

	/* IRQ service routine. The data and code touched are:
	 * 1. card_mpuin
	 */

	count = 0;
	idx = card_mpuin->qtail;

	while (1) {
		if (emu10k1_mpu_read_data(card, &MPUIvalue) < 0) {
			break;
		} else {
			++count;
			card_mpuin->midiq[idx].data = MPUIvalue;
			card_mpuin->midiq[idx].timein = (jiffies * 1000) / HZ;
			idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
		}
	}

	if (count) {
		card_mpuin->qtail = idx;

		tasklet_hi_schedule(&card_mpuin->tasklet);
	}

	return 0;
}

/*****************************************************************************/

/*   Supporting functions for Midi-In Interpretation State Machine           */

/*****************************************************************************/

/* FIXME: This should be a macro */
static int sblive_miStateInit(struct emu10k1_mpuin *card_mpuin)
{
	card_mpuin->status = 0;	/* For MIDI running status */
	card_mpuin->fstatus = 0;	/* For 0xFn status only */
	card_mpuin->curstate = STIN_PARSE;
	card_mpuin->laststate = STIN_PARSE;
	card_mpuin->data = 0;
	card_mpuin->timestart = 0;
	card_mpuin->timein = 0;

	return 0;
}

/* FIXME: This should be a macro */
static int sblive_miStateEntry(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
}

static int sblive_miStateParse(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	switch (data & 0xf0) {
	case 0x80:
	case 0x90:
	case 0xA0:
	case 0xB0:
	case 0xE0:
		card_mpuin->curstate = STIN_3BYTE;
		break;

	case 0xC0:
	case 0xD0:
		card_mpuin->curstate = STIN_2BYTE;
		break;

	case 0xF0:
		/* System messages do not affect the previous running status! */
		switch (data & 0x0f) {
		case 0x0:
			card_mpuin->laststate = card_mpuin->curstate;
			card_mpuin->curstate = STIN_SYS_EX_NORM;

			if (card_mpuin->firstmidiq) {
				struct midi_queue *midiq;

				midiq = card_mpuin->firstmidiq;
				*midiq->midibyte = data;
				--midiq->sizeLeft;
				++midiq->midibyte;
			}

			return CTSTATUS_NEXT_BYTE;

		case 0x7:
			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, 0xf7, 0);
			return -1;

		case 0x2:
			card_mpuin->laststate = card_mpuin->curstate;
			card_mpuin->curstate = STIN_SYS_COMMON_3;
			break;

		case 0x1:
		case 0x3:
			card_mpuin->laststate = card_mpuin->curstate;
			card_mpuin->curstate = STIN_SYS_COMMON_2;
			break;

		default:
			/* includes 0xF4 - 0xF6, 0xF8 - 0xFF */
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
		}

		break;

	default:
		DPF(2, "BUG: default case hit\n");
		return -1;
	}

	return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
}

static int sblive_miState3Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	u8 temp = data & 0xf0;

	if (temp < 0x80) {
		return midistatefn[STIN_3BYTE_KEY].Fn(card_mpuin, data);
	} else if (temp <= 0xe0 && temp != 0xc0 && temp != 0xd0) {
		card_mpuin->status = data;
		card_mpuin->curstate = STIN_3BYTE_KEY;

		return CTSTATUS_NEXT_BYTE;
	}

	return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
}

static int sblive_miState3ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 1 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = STIN_PARSE;
		tmp = ((unsigned long) data) << 8;
		tmp |= (unsigned long) card_mpuin->status;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->data = data;
	card_mpuin->curstate = STIN_3BYTE_VEL;

	return CTSTATUS_NEXT_BYTE;
}

static int sblive_miState3ByteVel(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 2 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = STIN_PARSE;
		tmp = ((unsigned long) data) << 8;
		tmp |= card_mpuin->data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->status;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->curstate = STIN_3BYTE;
	tmp = (unsigned long) data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->status;

	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);

	return 0;
}

static int sblive_miState2Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	u8 temp = data & 0xf0;

	if ((temp == 0xc0) || (temp == 0xd0)) {
		card_mpuin->status = data;
		card_mpuin->curstate = STIN_2BYTE_KEY;

		return CTSTATUS_NEXT_BYTE;
	}

	if (temp < 0x80)
		return midistatefn[STIN_2BYTE_KEY].Fn(card_mpuin, data);

	return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
}

static int sblive_miState2ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 1 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = STIN_PARSE;
		tmp = (unsigned long) data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->status;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->curstate = STIN_2BYTE;
	tmp = (unsigned long) data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->status;

	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);

	return 0;
}

static int sblive_miStateSysCommon2(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	card_mpuin->fstatus = data;
	card_mpuin->curstate = STIN_SYS_COMMON_2_KEY;

	return CTSTATUS_NEXT_BYTE;
}

static int sblive_miStateSysCommon2Key(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 1 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = card_mpuin->laststate;
		tmp = (unsigned long) data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->fstatus;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->curstate = card_mpuin->laststate;
	tmp = (unsigned long) data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->fstatus;

	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);

	return 0;
}

static int sblive_miStateSysCommon3(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	card_mpuin->fstatus = data;
	card_mpuin->curstate = STIN_SYS_COMMON_3_KEY;

	return CTSTATUS_NEXT_BYTE;
}

static int sblive_miStateSysCommon3Key(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 1 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = card_mpuin->laststate;
		tmp = (unsigned long) data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->fstatus;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->data = data;
	card_mpuin->curstate = STIN_SYS_COMMON_3_VEL;

	return CTSTATUS_NEXT_BYTE;
}

static int sblive_miStateSysCommon3Vel(struct emu10k1_mpuin *card_mpuin, u8 data)
/* byte 2 */
{
	unsigned long tmp;

	if (data > 0x7f) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = card_mpuin->laststate;
		tmp = (unsigned long) data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->data;
		tmp = tmp << 8;
		tmp |= (unsigned long) card_mpuin->fstatus;

		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);

		return -1;
	}

	card_mpuin->curstate = card_mpuin->laststate;
	tmp = (unsigned long) data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->data;
	tmp = tmp << 8;
	tmp |= (unsigned long) card_mpuin->fstatus;

	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);

	return 0;
}

static int sblive_miStateSysExNorm(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	unsigned long flags;

	if ((data > 0x7f) && (data != 0xf7)) {
		/* Real-time messages check */
		if (data > 0xf7)
			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);

		/* Invalid Data! */
		DPF(2, "Invalid data!\n");

		card_mpuin->curstate = card_mpuin->laststate;

		if (card_mpuin->firstmidiq) {
			struct midi_queue *midiq;

			midiq = card_mpuin->firstmidiq;
			*midiq->midibyte = data;
			--midiq->sizeLeft;
			++midiq->midibyte;

			spin_lock_irqsave(&card_mpuin->lock, flags);

			card_mpuin->firstmidiq = midiq->next;
			if (card_mpuin->firstmidiq == NULL)
				card_mpuin->lastmidiq = NULL;

			spin_unlock_irqrestore(&card_mpuin->lock, flags);

			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);

			kfree(midiq);
		}

		return -1;
	}

	if (card_mpuin->firstmidiq) {
		struct midi_queue *midiq;

		midiq = card_mpuin->firstmidiq;
		*midiq->midibyte = data;
		--midiq->sizeLeft;
		++midiq->midibyte;
	}

	if (data == 0xf7) {
		/* End of Sysex buffer */
		/* Send down the buffer */

		card_mpuin->curstate = card_mpuin->laststate;

		if (card_mpuin->firstmidiq) {
			struct midi_queue *midiq;

			midiq = card_mpuin->firstmidiq;

			spin_lock_irqsave(&card_mpuin->lock, flags);

			card_mpuin->firstmidiq = midiq->next;
			if (card_mpuin->firstmidiq == NULL)
				card_mpuin->lastmidiq = NULL;

			spin_unlock_irqrestore(&card_mpuin->lock, flags);

			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);

			kfree(midiq);
		}

		return 0;
	}

	if (card_mpuin->firstmidiq) {
		struct midi_queue *midiq;

		midiq = card_mpuin->firstmidiq;

		if (midiq->sizeLeft == 0) {
			/* Special case */

			spin_lock_irqsave(&card_mpuin->lock, flags);

			card_mpuin->firstmidiq = midiq->next;
			if (card_mpuin->firstmidiq == NULL)
				card_mpuin->lastmidiq = NULL;

			spin_unlock_irqrestore(&card_mpuin->lock, flags);

			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);

			kfree(midiq);

			return CTSTATUS_NEXT_BYTE;
		}
	}

	return CTSTATUS_NEXT_BYTE;
}

static int sblive_miStateSysReal(struct emu10k1_mpuin *card_mpuin, u8 data)
{
	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, data, 1);

	return CTSTATUS_NEXT_BYTE;
}