1 /*
2  **********************************************************************
3  *     sblive_mi.c - MIDI UART input HAL for emu10k1 driver
4  *     Copyright 1999, 2000 Creative Labs, Inc.
5  *
6  **********************************************************************
7  *
8  *     Date                 Author          Summary of changes
9  *     ----                 ------          ------------------
10  *     October 20, 1999     Bertrand Lee    base code release
11  *     November 2, 1999     Alan Cox        clean up
12  *
13  **********************************************************************
14  *
15  *     This program is free software; you can redistribute it and/or
16  *     modify it under the terms of the GNU General Public License as
17  *     published by the Free Software Foundation; either version 2 of
18  *     the License, or (at your option) any later version.
19  *
20  *     This program is distributed in the hope that it will be useful,
21  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *     GNU General Public License for more details.
24  *
25  *     You should have received a copy of the GNU General Public
26  *     License along with this program; if not, write to the Free
27  *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
28  *     USA.
29  *
30  **********************************************************************
31  */
32 
33 #include <linux/slab.h>
34 #include <linux/sched.h>
35 
36 #include "hwaccess.h"
37 #include "8010.h"
38 #include "cardmi.h"
39 #include "irqmgr.h"
40 
41 static struct {
42 	int (*Fn) (struct emu10k1_mpuin *, u8);
43 } midistatefn[] = {
44 
45 	{
46 	sblive_miStateParse}, {
47 	sblive_miState3Byte},	/* 0x8n, 0x9n, 0xAn, 0xBn, 0xEn */
48 	{
49 	sblive_miState3ByteKey},	/* Byte 1                       */
50 	{
51 	sblive_miState3ByteVel},	/* Byte 2                       */
52 	{
53 	sblive_miState2Byte},	/* 0xCn, 0xDn                   */
54 	{
55 	sblive_miState2ByteKey},	/* Byte 1                       */
56 	{
57 	sblive_miStateSysCommon2},	/* 0xF1 , 0xF3                  */
58 	{
59 	sblive_miStateSysCommon2Key},	/* 0xF1 , 0xF3, Byte 1          */
60 	{
61 	sblive_miStateSysCommon3},	/* 0xF2                         */
62 	{
63 	sblive_miStateSysCommon3Key},	/* 0xF2 , Byte 1                */
64 	{
65 	sblive_miStateSysCommon3Vel},	/* 0xF2 , Byte 2                */
66 	{
67 	sblive_miStateSysExNorm},	/* 0xF0, 0xF7, Normal mode      */
68 	{
69 	sblive_miStateSysReal}	/* 0xF4 - 0xF6 ,0xF8 - 0xFF     */
70 };
71 
72 /* Installs the IRQ handler for the MPU in port                 */
73 
74 /* and initialize parameters                                    */
75 
emu10k1_mpuin_open(struct emu10k1_card * card,struct midi_openinfo * openinfo)76 int emu10k1_mpuin_open(struct emu10k1_card *card, struct midi_openinfo *openinfo)
77 {
78 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
79 
80 	DPF(2, "emu10k1_mpuin_open\n");
81 
82 	if (!(card_mpuin->status & FLAGS_AVAILABLE))
83 		return -1;
84 
85 	/* Copy open info and mark channel as in use */
86 	card_mpuin->openinfo = *openinfo;
87 	card_mpuin->status &= ~FLAGS_AVAILABLE;	/* clear */
88 	card_mpuin->status |= FLAGS_READY;	/* set */
89 	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */
90 	card_mpuin->firstmidiq = NULL;
91 	card_mpuin->lastmidiq = NULL;
92 	card_mpuin->qhead = 0;
93 	card_mpuin->qtail = 0;
94 
95 	sblive_miStateInit(card_mpuin);
96 
97 	emu10k1_mpu_reset(card);
98 	emu10k1_mpu_acquire(card);
99 
100 	return 0;
101 }
102 
emu10k1_mpuin_close(struct emu10k1_card * card)103 int emu10k1_mpuin_close(struct emu10k1_card *card)
104 {
105 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
106 
107 	DPF(2, "emu10k1_mpuin_close()\n");
108 
109 	/* Check if there are pending input SysEx buffers */
110 	if (card_mpuin->firstmidiq != NULL) {
111 		ERROR();
112 		return -1;
113 	}
114 
115 	/* Disable RX interrupt */
116 	emu10k1_irq_disable(card, INTE_MIDIRXENABLE);
117 
118 	emu10k1_mpu_release(card);
119 
120 	card_mpuin->status |= FLAGS_AVAILABLE;	/* set */
121 	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */
122 
123 	return 0;
124 }
125 
126 /* Adds MIDI buffer to local queue list                         */
127 
emu10k1_mpuin_add_buffer(struct emu10k1_mpuin * card_mpuin,struct midi_hdr * midihdr)128 int emu10k1_mpuin_add_buffer(struct emu10k1_mpuin *card_mpuin, struct midi_hdr *midihdr)
129 {
130 	struct midi_queue *midiq;
131 	unsigned long flags;
132 
133 	DPF(2, "emu10k1_mpuin_add_buffer()\n");
134 
135 	/* Update MIDI buffer flags */
136 	midihdr->flags |= MIDIBUF_INQUEUE;	/* set */
137 	midihdr->flags &= ~MIDIBUF_DONE;	/* clear */
138 
139 	if ((midiq = (struct midi_queue *) kmalloc(sizeof(struct midi_queue), GFP_ATOMIC)) == NULL) {
140 		/* Message lost */
141 		return -1;
142 	}
143 
144 	midiq->next = NULL;
145 	midiq->qtype = 1;
146 	midiq->length = midihdr->bufferlength;
147 	midiq->sizeLeft = midihdr->bufferlength;
148 	midiq->midibyte = midihdr->data;
149 	midiq->refdata = (unsigned long) midihdr;
150 
151 	spin_lock_irqsave(&card_mpuin->lock, flags);
152 
153 	if (card_mpuin->firstmidiq == NULL) {
154 		card_mpuin->firstmidiq = midiq;
155 		card_mpuin->lastmidiq = midiq;
156 	} else {
157 		(card_mpuin->lastmidiq)->next = midiq;
158 		card_mpuin->lastmidiq = midiq;
159 	}
160 
161 	spin_unlock_irqrestore(&card_mpuin->lock, flags);
162 
163 	return 0;
164 }
165 
166 /* First set the Time Stamp if MIDI IN has not started.         */
167 
168 /* Then enable RX Irq.                                          */
169 
emu10k1_mpuin_start(struct emu10k1_card * card)170 int emu10k1_mpuin_start(struct emu10k1_card *card)
171 {
172 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
173 	u8 dummy;
174 
175 	DPF(2, "emu10k1_mpuin_start()\n");
176 
177 	/* Set timestamp if not set */
178 	if (card_mpuin->status & FLAGS_MIDM_STARTED) {
179 		DPF(2, "Time Stamp not changed\n");
180 	} else {
181 		while (!emu10k1_mpu_read_data(card, &dummy));
182 
183 		card_mpuin->status |= FLAGS_MIDM_STARTED;	/* set */
184 
185 		/* Set new time stamp */
186 		card_mpuin->timestart = (jiffies * 1000) / HZ;
187 		DPD(2, "New Time Stamp = %d\n", card_mpuin->timestart);
188 
189 		card_mpuin->qhead = 0;
190 		card_mpuin->qtail = 0;
191 
192 		emu10k1_irq_enable(card, INTE_MIDIRXENABLE);
193 	}
194 
195 	return 0;
196 }
197 
198 /* Disable the RX Irq.  If a partial recorded buffer            */
199 
200 /* exist, send it up to IMIDI level.                            */
201 
emu10k1_mpuin_stop(struct emu10k1_card * card)202 int emu10k1_mpuin_stop(struct emu10k1_card *card)
203 {
204 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
205 	struct midi_queue *midiq;
206 	unsigned long flags;
207 
208 	DPF(2, "emu10k1_mpuin_stop()\n");
209 
210 	emu10k1_irq_disable(card, INTE_MIDIRXENABLE);
211 
212 	card_mpuin->status &= ~FLAGS_MIDM_STARTED;	/* clear */
213 
214 	if (card_mpuin->firstmidiq) {
215 		spin_lock_irqsave(&card_mpuin->lock, flags);
216 
217 		midiq = card_mpuin->firstmidiq;
218 		if (midiq != NULL) {
219 			if (midiq->sizeLeft == midiq->length)
220 				midiq = NULL;
221 			else {
222 				card_mpuin->firstmidiq = midiq->next;
223 				if (card_mpuin->firstmidiq == NULL)
224 					card_mpuin->lastmidiq = NULL;
225 			}
226 		}
227 
228 		spin_unlock_irqrestore(&card_mpuin->lock, flags);
229 
230 		if (midiq) {
231 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
232 			kfree(midiq);
233 		}
234 	}
235 
236 	return 0;
237 }
238 
239 /* Disable the RX Irq.  If any buffer                           */
240 
241 /* exist, send it up to IMIDI level.                            */
emu10k1_mpuin_reset(struct emu10k1_card * card)242 int emu10k1_mpuin_reset(struct emu10k1_card *card)
243 {
244 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
245 	struct midi_queue *midiq;
246 
247 	DPF(2, "emu10k1_mpuin_reset()\n");
248 
249 	emu10k1_irq_disable(card, INTE_MIDIRXENABLE);
250 
251 	while (card_mpuin->firstmidiq) {
252 		midiq = card_mpuin->firstmidiq;
253 		card_mpuin->firstmidiq = midiq->next;
254 
255 		if (midiq->sizeLeft == midiq->length)
256 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
257 		else
258 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
259 
260 		kfree(midiq);
261 	}
262 
263 	card_mpuin->lastmidiq = NULL;
264 	card_mpuin->status &= ~FLAGS_MIDM_STARTED;
265 
266 	return 0;
267 }
268 
269 /* Passes the message with the data back to the client          */
270 
271 /* via IRQ & DPC callbacks to Ring 3                            */
emu10k1_mpuin_callback(struct emu10k1_mpuin * card_mpuin,u32 msg,unsigned long data,u32 bytesvalid)272 int emu10k1_mpuin_callback(struct emu10k1_mpuin *card_mpuin, u32 msg, unsigned long data, u32 bytesvalid)
273 {
274 	unsigned long timein;
275 	struct midi_queue *midiq;
276 	unsigned long callback_msg[3];
277 	struct midi_hdr *midihdr;
278 
279 	/* Called during ISR. The data & code touched are:
280 	 * 1. card_mpuin
281 	 * 2. The function to be called
282 	 */
283 
284 	timein = card_mpuin->timein;
285 	if (card_mpuin->timestart <= timein)
286 		callback_msg[0] = timein - card_mpuin->timestart;
287 	else
288 		callback_msg[0] = (~0x0L - card_mpuin->timestart) + timein;
289 
290 	if (msg == ICARDMIDI_INDATA || msg == ICARDMIDI_INDATAERROR) {
291 		callback_msg[1] = data;
292 		callback_msg[2] = bytesvalid;
293 		DPD(2, "emu10k1_mpuin_callback: midimsg = %#lx\n", data);
294 	} else {
295 		midiq = (struct midi_queue *) data;
296 		midihdr = (struct midi_hdr *) midiq->refdata;
297 
298 		callback_msg[1] = midiq->length - midiq->sizeLeft;
299 		callback_msg[2] = midiq->refdata;
300 		midihdr->flags &= ~MIDIBUF_INQUEUE;
301 		midihdr->flags |= MIDIBUF_DONE;
302 
303 		midihdr->bytesrecorded = midiq->length - midiq->sizeLeft;
304 	}
305 
306 	/* Notify client that Sysex buffer has been sent */
307 	emu10k1_midi_callback(msg, card_mpuin->openinfo.refdata, callback_msg);
308 
309 	return 0;
310 }
311 
emu10k1_mpuin_bh(unsigned long refdata)312 void emu10k1_mpuin_bh(unsigned long refdata)
313 {
314 	u8 data;
315 	unsigned idx;
316 	struct emu10k1_mpuin *card_mpuin = (struct emu10k1_mpuin *) refdata;
317 	unsigned long flags;
318 
319 	while (card_mpuin->qhead != card_mpuin->qtail) {
320 		spin_lock_irqsave(&card_mpuin->lock, flags);
321 		idx = card_mpuin->qhead;
322 		data = card_mpuin->midiq[idx].data;
323 		card_mpuin->timein = card_mpuin->midiq[idx].timein;
324 		idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
325 		card_mpuin->qhead = idx;
326 		spin_unlock_irqrestore(&card_mpuin->lock, flags);
327 
328 		sblive_miStateEntry(card_mpuin, data);
329 	}
330 
331 	return;
332 }
333 
334 /* IRQ callback handler routine for the MPU in port */
335 
emu10k1_mpuin_irqhandler(struct emu10k1_card * card)336 int emu10k1_mpuin_irqhandler(struct emu10k1_card *card)
337 {
338 	unsigned idx;
339 	unsigned count;
340 	u8 MPUIvalue;
341 	struct emu10k1_mpuin *card_mpuin = card->mpuin;
342 
343 	/* IRQ service routine. The data and code touched are:
344 	 * 1. card_mpuin
345 	 */
346 
347 	count = 0;
348 	idx = card_mpuin->qtail;
349 
350 	while (1) {
351 		if (emu10k1_mpu_read_data(card, &MPUIvalue) < 0) {
352 			break;
353 		} else {
354 			++count;
355 			card_mpuin->midiq[idx].data = MPUIvalue;
356 			card_mpuin->midiq[idx].timein = (jiffies * 1000) / HZ;
357 			idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
358 		}
359 	}
360 
361 	if (count) {
362 		card_mpuin->qtail = idx;
363 
364 		tasklet_hi_schedule(&card_mpuin->tasklet);
365 	}
366 
367 	return 0;
368 }
369 
370 /*****************************************************************************/
371 
372 /*   Supporting functions for Midi-In Interpretation State Machine           */
373 
374 /*****************************************************************************/
375 
376 /* FIXME: This should be a macro */
sblive_miStateInit(struct emu10k1_mpuin * card_mpuin)377 int sblive_miStateInit(struct emu10k1_mpuin *card_mpuin)
378 {
379 	card_mpuin->status = 0;	/* For MIDI running status */
380 	card_mpuin->fstatus = 0;	/* For 0xFn status only */
381 	card_mpuin->curstate = STIN_PARSE;
382 	card_mpuin->laststate = STIN_PARSE;
383 	card_mpuin->data = 0;
384 	card_mpuin->timestart = 0;
385 	card_mpuin->timein = 0;
386 
387 	return 0;
388 }
389 
390 /* FIXME: This should be a macro */
sblive_miStateEntry(struct emu10k1_mpuin * card_mpuin,u8 data)391 int sblive_miStateEntry(struct emu10k1_mpuin *card_mpuin, u8 data)
392 {
393 	return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
394 }
395 
sblive_miStateParse(struct emu10k1_mpuin * card_mpuin,u8 data)396 int sblive_miStateParse(struct emu10k1_mpuin *card_mpuin, u8 data)
397 {
398 	switch (data & 0xf0) {
399 	case 0x80:
400 	case 0x90:
401 	case 0xA0:
402 	case 0xB0:
403 	case 0xE0:
404 		card_mpuin->curstate = STIN_3BYTE;
405 		break;
406 
407 	case 0xC0:
408 	case 0xD0:
409 		card_mpuin->curstate = STIN_2BYTE;
410 		break;
411 
412 	case 0xF0:
413 		/* System messages do not affect the previous running status! */
414 		switch (data & 0x0f) {
415 		case 0x0:
416 			card_mpuin->laststate = card_mpuin->curstate;
417 			card_mpuin->curstate = STIN_SYS_EX_NORM;
418 
419 			if (card_mpuin->firstmidiq) {
420 				struct midi_queue *midiq;
421 
422 				midiq = card_mpuin->firstmidiq;
423 				*midiq->midibyte = data;
424 				--midiq->sizeLeft;
425 				++midiq->midibyte;
426 			}
427 
428 			return CTSTATUS_NEXT_BYTE;
429 
430 		case 0x7:
431 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, 0xf7, 0);
432 			return -1;
433 
434 		case 0x2:
435 			card_mpuin->laststate = card_mpuin->curstate;
436 			card_mpuin->curstate = STIN_SYS_COMMON_3;
437 			break;
438 
439 		case 0x1:
440 		case 0x3:
441 			card_mpuin->laststate = card_mpuin->curstate;
442 			card_mpuin->curstate = STIN_SYS_COMMON_2;
443 			break;
444 
445 		default:
446 			/* includes 0xF4 - 0xF6, 0xF8 - 0xFF */
447 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
448 		}
449 
450 		break;
451 
452 	default:
453 		DPF(2, "BUG: default case hit\n");
454 		return -1;
455 	}
456 
457 	return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
458 }
459 
sblive_miState3Byte(struct emu10k1_mpuin * card_mpuin,u8 data)460 int sblive_miState3Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
461 {
462 	u8 temp = data & 0xf0;
463 
464 	if (temp < 0x80) {
465 		return midistatefn[STIN_3BYTE_KEY].Fn(card_mpuin, data);
466 	} else if (temp <= 0xe0 && temp != 0xc0 && temp != 0xd0) {
467 		card_mpuin->status = data;
468 		card_mpuin->curstate = STIN_3BYTE_KEY;
469 
470 		return CTSTATUS_NEXT_BYTE;
471 	}
472 
473 	return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
474 }
475 
sblive_miState3ByteKey(struct emu10k1_mpuin * card_mpuin,u8 data)476 int sblive_miState3ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
477 
478 /* byte 1 */
479 {
480 	unsigned long tmp;
481 
482 	if (data > 0x7f) {
483 		/* Real-time messages check */
484 		if (data > 0xf7)
485 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
486 
487 		/* Invalid data! */
488 		DPF(2, "Invalid data!\n");
489 
490 		card_mpuin->curstate = STIN_PARSE;
491 		tmp = ((unsigned long) data) << 8;
492 		tmp |= (unsigned long) card_mpuin->status;
493 
494 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
495 
496 		return -1;
497 	}
498 
499 	card_mpuin->data = data;
500 	card_mpuin->curstate = STIN_3BYTE_VEL;
501 
502 	return CTSTATUS_NEXT_BYTE;
503 }
504 
sblive_miState3ByteVel(struct emu10k1_mpuin * card_mpuin,u8 data)505 int sblive_miState3ByteVel(struct emu10k1_mpuin *card_mpuin, u8 data)
506 
507 /* byte 2 */
508 {
509 	unsigned long tmp;
510 
511 	if (data > 0x7f) {
512 		/* Real-time messages check */
513 		if (data > 0xf7)
514 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
515 
516 		/* Invalid data! */
517 		DPF(2, "Invalid data!\n");
518 
519 		card_mpuin->curstate = STIN_PARSE;
520 		tmp = ((unsigned long) data) << 8;
521 		tmp |= card_mpuin->data;
522 		tmp = tmp << 8;
523 		tmp |= (unsigned long) card_mpuin->status;
524 
525 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
526 
527 		return -1;
528 	}
529 
530 	card_mpuin->curstate = STIN_3BYTE;
531 	tmp = (unsigned long) data;
532 	tmp = tmp << 8;
533 	tmp |= (unsigned long) card_mpuin->data;
534 	tmp = tmp << 8;
535 	tmp |= (unsigned long) card_mpuin->status;
536 
537 	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);
538 
539 	return 0;
540 }
541 
sblive_miState2Byte(struct emu10k1_mpuin * card_mpuin,u8 data)542 int sblive_miState2Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
543 {
544 	u8 temp = data & 0xf0;
545 
546 	if ((temp == 0xc0) || (temp == 0xd0)) {
547 		card_mpuin->status = data;
548 		card_mpuin->curstate = STIN_2BYTE_KEY;
549 
550 		return CTSTATUS_NEXT_BYTE;
551 	}
552 
553 	if (temp < 0x80)
554 		return midistatefn[STIN_2BYTE_KEY].Fn(card_mpuin, data);
555 
556 	return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
557 }
558 
sblive_miState2ByteKey(struct emu10k1_mpuin * card_mpuin,u8 data)559 int sblive_miState2ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
560 
561 /* byte 1 */
562 {
563 	unsigned long tmp;
564 
565 	if (data > 0x7f) {
566 		/* Real-time messages check */
567 		if (data > 0xf7)
568 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
569 
570 		/* Invalid data! */
571 		DPF(2, "Invalid data!\n");
572 
573 		card_mpuin->curstate = STIN_PARSE;
574 		tmp = (unsigned long) data;
575 		tmp = tmp << 8;
576 		tmp |= (unsigned long) card_mpuin->status;
577 
578 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
579 
580 		return -1;
581 	}
582 
583 	card_mpuin->curstate = STIN_2BYTE;
584 	tmp = (unsigned long) data;
585 	tmp = tmp << 8;
586 	tmp |= (unsigned long) card_mpuin->status;
587 
588 	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);
589 
590 	return 0;
591 }
592 
sblive_miStateSysCommon2(struct emu10k1_mpuin * card_mpuin,u8 data)593 int sblive_miStateSysCommon2(struct emu10k1_mpuin *card_mpuin, u8 data)
594 {
595 	card_mpuin->fstatus = data;
596 	card_mpuin->curstate = STIN_SYS_COMMON_2_KEY;
597 
598 	return CTSTATUS_NEXT_BYTE;
599 }
600 
sblive_miStateSysCommon2Key(struct emu10k1_mpuin * card_mpuin,u8 data)601 int sblive_miStateSysCommon2Key(struct emu10k1_mpuin *card_mpuin, u8 data)
602 
603 /* byte 1 */
604 {
605 	unsigned long tmp;
606 
607 	if (data > 0x7f) {
608 		/* Real-time messages check */
609 		if (data > 0xf7)
610 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
611 
612 		/* Invalid data! */
613 		DPF(2, "Invalid data!\n");
614 
615 		card_mpuin->curstate = card_mpuin->laststate;
616 		tmp = (unsigned long) data;
617 		tmp = tmp << 8;
618 		tmp |= (unsigned long) card_mpuin->fstatus;
619 
620 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
621 
622 		return -1;
623 	}
624 
625 	card_mpuin->curstate = card_mpuin->laststate;
626 	tmp = (unsigned long) data;
627 	tmp = tmp << 8;
628 	tmp |= (unsigned long) card_mpuin->fstatus;
629 
630 	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);
631 
632 	return 0;
633 }
634 
sblive_miStateSysCommon3(struct emu10k1_mpuin * card_mpuin,u8 data)635 int sblive_miStateSysCommon3(struct emu10k1_mpuin *card_mpuin, u8 data)
636 {
637 	card_mpuin->fstatus = data;
638 	card_mpuin->curstate = STIN_SYS_COMMON_3_KEY;
639 
640 	return CTSTATUS_NEXT_BYTE;
641 }
642 
sblive_miStateSysCommon3Key(struct emu10k1_mpuin * card_mpuin,u8 data)643 int sblive_miStateSysCommon3Key(struct emu10k1_mpuin *card_mpuin, u8 data)
644 
645 /* byte 1 */
646 {
647 	unsigned long tmp;
648 
649 	if (data > 0x7f) {
650 		/* Real-time messages check */
651 		if (data > 0xf7)
652 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
653 
654 		/* Invalid data! */
655 		DPF(2, "Invalid data!\n");
656 
657 		card_mpuin->curstate = card_mpuin->laststate;
658 		tmp = (unsigned long) data;
659 		tmp = tmp << 8;
660 		tmp |= (unsigned long) card_mpuin->fstatus;
661 
662 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
663 
664 		return -1;
665 	}
666 
667 	card_mpuin->data = data;
668 	card_mpuin->curstate = STIN_SYS_COMMON_3_VEL;
669 
670 	return CTSTATUS_NEXT_BYTE;
671 }
672 
sblive_miStateSysCommon3Vel(struct emu10k1_mpuin * card_mpuin,u8 data)673 int sblive_miStateSysCommon3Vel(struct emu10k1_mpuin *card_mpuin, u8 data)
674 
675 /* byte 2 */
676 {
677 	unsigned long tmp;
678 
679 	if (data > 0x7f) {
680 		/* Real-time messages check */
681 		if (data > 0xf7)
682 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
683 
684 		/* Invalid data! */
685 		DPF(2, "Invalid data!\n");
686 
687 		card_mpuin->curstate = card_mpuin->laststate;
688 		tmp = (unsigned long) data;
689 		tmp = tmp << 8;
690 		tmp |= (unsigned long) card_mpuin->data;
691 		tmp = tmp << 8;
692 		tmp |= (unsigned long) card_mpuin->fstatus;
693 
694 		emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
695 
696 		return -1;
697 	}
698 
699 	card_mpuin->curstate = card_mpuin->laststate;
700 	tmp = (unsigned long) data;
701 	tmp = tmp << 8;
702 	tmp |= (unsigned long) card_mpuin->data;
703 	tmp = tmp << 8;
704 	tmp |= (unsigned long) card_mpuin->fstatus;
705 
706 	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);
707 
708 	return 0;
709 }
710 
sblive_miStateSysExNorm(struct emu10k1_mpuin * card_mpuin,u8 data)711 int sblive_miStateSysExNorm(struct emu10k1_mpuin *card_mpuin, u8 data)
712 {
713 	unsigned long flags;
714 
715 	if ((data > 0x7f) && (data != 0xf7)) {
716 		/* Real-time messages check */
717 		if (data > 0xf7)
718 			return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
719 
720 		/* Invalid Data! */
721 		DPF(2, "Invalid data!\n");
722 
723 		card_mpuin->curstate = card_mpuin->laststate;
724 
725 		if (card_mpuin->firstmidiq) {
726 			struct midi_queue *midiq;
727 
728 			midiq = card_mpuin->firstmidiq;
729 			*midiq->midibyte = data;
730 			--midiq->sizeLeft;
731 			++midiq->midibyte;
732 
733 			spin_lock_irqsave(&card_mpuin->lock, flags);
734 
735 			card_mpuin->firstmidiq = midiq->next;
736 			if (card_mpuin->firstmidiq == NULL)
737 				card_mpuin->lastmidiq = NULL;
738 
739 			spin_unlock_irqrestore(&card_mpuin->lock, flags);
740 
741 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
742 
743 			kfree(midiq);
744 		}
745 
746 		return -1;
747 	}
748 
749 	if (card_mpuin->firstmidiq) {
750 		struct midi_queue *midiq;
751 
752 		midiq = card_mpuin->firstmidiq;
753 		*midiq->midibyte = data;
754 		--midiq->sizeLeft;
755 		++midiq->midibyte;
756 	}
757 
758 	if (data == 0xf7) {
759 		/* End of Sysex buffer */
760 		/* Send down the buffer */
761 
762 		card_mpuin->curstate = card_mpuin->laststate;
763 
764 		if (card_mpuin->firstmidiq) {
765 			struct midi_queue *midiq;
766 
767 			midiq = card_mpuin->firstmidiq;
768 
769 			spin_lock_irqsave(&card_mpuin->lock, flags);
770 
771 			card_mpuin->firstmidiq = midiq->next;
772 			if (card_mpuin->firstmidiq == NULL)
773 				card_mpuin->lastmidiq = NULL;
774 
775 			spin_unlock_irqrestore(&card_mpuin->lock, flags);
776 
777 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
778 
779 			kfree(midiq);
780 		}
781 
782 		return 0;
783 	}
784 
785 	if (card_mpuin->firstmidiq) {
786 		struct midi_queue *midiq;
787 
788 		midiq = card_mpuin->firstmidiq;
789 
790 		if (midiq->sizeLeft == 0) {
791 			/* Special case */
792 
793 			spin_lock_irqsave(&card_mpuin->lock, flags);
794 
795 			card_mpuin->firstmidiq = midiq->next;
796 			if (card_mpuin->firstmidiq == NULL)
797 				card_mpuin->lastmidiq = NULL;
798 
799 			spin_unlock_irqrestore(&card_mpuin->lock, flags);
800 
801 			emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
802 
803 			kfree(midiq);
804 
805 			return CTSTATUS_NEXT_BYTE;
806 		}
807 	}
808 
809 	return CTSTATUS_NEXT_BYTE;
810 }
811 
sblive_miStateSysReal(struct emu10k1_mpuin * card_mpuin,u8 data)812 int sblive_miStateSysReal(struct emu10k1_mpuin *card_mpuin, u8 data)
813 {
814 	emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, data, 1);
815 
816 	return CTSTATUS_NEXT_BYTE;
817 }
818