[XFRM]: Fix aevent structuring to be more complete.
[hh.org.git] / sound / oss / emu10k1 / cardmi.c
blob0545814cc67dc08a0da0776356df6ebfc34fff76
1 /*
2 **********************************************************************
3 * sblive_mi.c - MIDI UART input HAL for emu10k1 driver
4 * Copyright 1999, 2000 Creative Labs, Inc.
6 **********************************************************************
8 * Date Author Summary of changes
9 * ---- ------ ------------------
10 * October 20, 1999 Bertrand Lee base code release
11 * November 2, 1999 Alan Cox clean up
13 **********************************************************************
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.
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.
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.
30 **********************************************************************
33 #include <linux/slab.h>
34 #include <linux/jiffies.h>
36 #include "hwaccess.h"
37 #include "8010.h"
38 #include "cardmi.h"
39 #include "irqmgr.h"
42 static int emu10k1_mpuin_callback(struct emu10k1_mpuin *card_mpuin, u32 msg, unsigned long data, u32 bytesvalid);
44 static int sblive_miStateInit(struct emu10k1_mpuin *);
45 static int sblive_miStateEntry(struct emu10k1_mpuin *, u8);
46 static int sblive_miStateParse(struct emu10k1_mpuin *, u8);
47 static int sblive_miState3Byte(struct emu10k1_mpuin *, u8);
48 static int sblive_miState3ByteKey(struct emu10k1_mpuin *, u8);
49 static int sblive_miState3ByteVel(struct emu10k1_mpuin *, u8);
50 static int sblive_miState2Byte(struct emu10k1_mpuin *, u8);
51 static int sblive_miState2ByteKey(struct emu10k1_mpuin *, u8);
52 static int sblive_miStateSysCommon2(struct emu10k1_mpuin *, u8);
53 static int sblive_miStateSysCommon2Key(struct emu10k1_mpuin *, u8);
54 static int sblive_miStateSysCommon3(struct emu10k1_mpuin *, u8);
55 static int sblive_miStateSysCommon3Key(struct emu10k1_mpuin *, u8);
56 static int sblive_miStateSysCommon3Vel(struct emu10k1_mpuin *, u8);
57 static int sblive_miStateSysExNorm(struct emu10k1_mpuin *, u8);
58 static int sblive_miStateSysReal(struct emu10k1_mpuin *, u8);
61 static struct {
62 int (*Fn) (struct emu10k1_mpuin *, u8);
63 } midistatefn[] = {
66 sblive_miStateParse}, {
67 sblive_miState3Byte}, /* 0x8n, 0x9n, 0xAn, 0xBn, 0xEn */
69 sblive_miState3ByteKey}, /* Byte 1 */
71 sblive_miState3ByteVel}, /* Byte 2 */
73 sblive_miState2Byte}, /* 0xCn, 0xDn */
75 sblive_miState2ByteKey}, /* Byte 1 */
77 sblive_miStateSysCommon2}, /* 0xF1 , 0xF3 */
79 sblive_miStateSysCommon2Key}, /* 0xF1 , 0xF3, Byte 1 */
81 sblive_miStateSysCommon3}, /* 0xF2 */
83 sblive_miStateSysCommon3Key}, /* 0xF2 , Byte 1 */
85 sblive_miStateSysCommon3Vel}, /* 0xF2 , Byte 2 */
87 sblive_miStateSysExNorm}, /* 0xF0, 0xF7, Normal mode */
89 sblive_miStateSysReal} /* 0xF4 - 0xF6 ,0xF8 - 0xFF */
93 /* Installs the IRQ handler for the MPU in port */
95 /* and initialize parameters */
97 int emu10k1_mpuin_open(struct emu10k1_card *card, struct midi_openinfo *openinfo)
99 struct emu10k1_mpuin *card_mpuin = card->mpuin;
101 DPF(2, "emu10k1_mpuin_open\n");
103 if (!(card_mpuin->status & FLAGS_AVAILABLE))
104 return -1;
106 /* Copy open info and mark channel as in use */
107 card_mpuin->openinfo = *openinfo;
108 card_mpuin->status &= ~FLAGS_AVAILABLE; /* clear */
109 card_mpuin->status |= FLAGS_READY; /* set */
110 card_mpuin->status &= ~FLAGS_MIDM_STARTED; /* clear */
111 card_mpuin->firstmidiq = NULL;
112 card_mpuin->lastmidiq = NULL;
113 card_mpuin->qhead = 0;
114 card_mpuin->qtail = 0;
116 sblive_miStateInit(card_mpuin);
118 emu10k1_mpu_reset(card);
119 emu10k1_mpu_acquire(card);
121 return 0;
124 int emu10k1_mpuin_close(struct emu10k1_card *card)
126 struct emu10k1_mpuin *card_mpuin = card->mpuin;
128 DPF(2, "emu10k1_mpuin_close()\n");
130 /* Check if there are pending input SysEx buffers */
131 if (card_mpuin->firstmidiq != NULL) {
132 ERROR();
133 return -1;
136 /* Disable RX interrupt */
137 emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);
139 emu10k1_mpu_release(card);
141 card_mpuin->status |= FLAGS_AVAILABLE; /* set */
142 card_mpuin->status &= ~FLAGS_MIDM_STARTED; /* clear */
144 return 0;
147 /* Adds MIDI buffer to local queue list */
149 int emu10k1_mpuin_add_buffer(struct emu10k1_mpuin *card_mpuin, struct midi_hdr *midihdr)
151 struct midi_queue *midiq;
152 unsigned long flags;
154 DPF(2, "emu10k1_mpuin_add_buffer()\n");
156 /* Update MIDI buffer flags */
157 midihdr->flags |= MIDIBUF_INQUEUE; /* set */
158 midihdr->flags &= ~MIDIBUF_DONE; /* clear */
160 if ((midiq = (struct midi_queue *) kmalloc(sizeof(struct midi_queue), GFP_ATOMIC)) == NULL) {
161 /* Message lost */
162 return -1;
165 midiq->next = NULL;
166 midiq->qtype = 1;
167 midiq->length = midihdr->bufferlength;
168 midiq->sizeLeft = midihdr->bufferlength;
169 midiq->midibyte = midihdr->data;
170 midiq->refdata = (unsigned long) midihdr;
172 spin_lock_irqsave(&card_mpuin->lock, flags);
174 if (card_mpuin->firstmidiq == NULL) {
175 card_mpuin->firstmidiq = midiq;
176 card_mpuin->lastmidiq = midiq;
177 } else {
178 (card_mpuin->lastmidiq)->next = midiq;
179 card_mpuin->lastmidiq = midiq;
182 spin_unlock_irqrestore(&card_mpuin->lock, flags);
184 return 0;
187 /* First set the Time Stamp if MIDI IN has not started. */
189 /* Then enable RX Irq. */
191 int emu10k1_mpuin_start(struct emu10k1_card *card)
193 struct emu10k1_mpuin *card_mpuin = card->mpuin;
194 u8 dummy;
196 DPF(2, "emu10k1_mpuin_start()\n");
198 /* Set timestamp if not set */
199 if (card_mpuin->status & FLAGS_MIDM_STARTED) {
200 DPF(2, "Time Stamp not changed\n");
201 } else {
202 while (!emu10k1_mpu_read_data(card, &dummy));
204 card_mpuin->status |= FLAGS_MIDM_STARTED; /* set */
206 /* Set new time stamp */
207 card_mpuin->timestart = (jiffies * 1000) / HZ;
208 DPD(2, "New Time Stamp = %d\n", card_mpuin->timestart);
210 card_mpuin->qhead = 0;
211 card_mpuin->qtail = 0;
213 emu10k1_irq_enable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);
216 return 0;
219 /* Disable the RX Irq. If a partial recorded buffer */
221 /* exist, send it up to IMIDI level. */
223 int emu10k1_mpuin_stop(struct emu10k1_card *card)
225 struct emu10k1_mpuin *card_mpuin = card->mpuin;
226 struct midi_queue *midiq;
227 unsigned long flags;
229 DPF(2, "emu10k1_mpuin_stop()\n");
231 emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);
233 card_mpuin->status &= ~FLAGS_MIDM_STARTED; /* clear */
235 if (card_mpuin->firstmidiq) {
236 spin_lock_irqsave(&card_mpuin->lock, flags);
238 midiq = card_mpuin->firstmidiq;
239 if (midiq != NULL) {
240 if (midiq->sizeLeft == midiq->length)
241 midiq = NULL;
242 else {
243 card_mpuin->firstmidiq = midiq->next;
244 if (card_mpuin->firstmidiq == NULL)
245 card_mpuin->lastmidiq = NULL;
249 spin_unlock_irqrestore(&card_mpuin->lock, flags);
251 if (midiq) {
252 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
253 kfree(midiq);
257 return 0;
260 /* Disable the RX Irq. If any buffer */
262 /* exist, send it up to IMIDI level. */
263 int emu10k1_mpuin_reset(struct emu10k1_card *card)
265 struct emu10k1_mpuin *card_mpuin = card->mpuin;
266 struct midi_queue *midiq;
268 DPF(2, "emu10k1_mpuin_reset()\n");
270 emu10k1_irq_disable(card, card->is_audigy ? A_INTE_MIDIRXENABLE : INTE_MIDIRXENABLE);
272 while (card_mpuin->firstmidiq) {
273 midiq = card_mpuin->firstmidiq;
274 card_mpuin->firstmidiq = midiq->next;
276 if (midiq->sizeLeft == midiq->length)
277 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
278 else
279 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
281 kfree(midiq);
284 card_mpuin->lastmidiq = NULL;
285 card_mpuin->status &= ~FLAGS_MIDM_STARTED;
287 return 0;
290 /* Passes the message with the data back to the client */
292 /* via IRQ & DPC callbacks to Ring 3 */
293 static int emu10k1_mpuin_callback(struct emu10k1_mpuin *card_mpuin, u32 msg, unsigned long data, u32 bytesvalid)
295 unsigned long timein;
296 struct midi_queue *midiq;
297 unsigned long callback_msg[3];
298 struct midi_hdr *midihdr;
300 /* Called during ISR. The data & code touched are:
301 * 1. card_mpuin
302 * 2. The function to be called
305 timein = card_mpuin->timein;
306 if (card_mpuin->timestart <= timein)
307 callback_msg[0] = timein - card_mpuin->timestart;
308 else
309 callback_msg[0] = (~0x0L - card_mpuin->timestart) + timein;
311 if (msg == ICARDMIDI_INDATA || msg == ICARDMIDI_INDATAERROR) {
312 callback_msg[1] = data;
313 callback_msg[2] = bytesvalid;
314 DPD(2, "emu10k1_mpuin_callback: midimsg = %#lx\n", data);
315 } else {
316 midiq = (struct midi_queue *) data;
317 midihdr = (struct midi_hdr *) midiq->refdata;
319 callback_msg[1] = midiq->length - midiq->sizeLeft;
320 callback_msg[2] = midiq->refdata;
321 midihdr->flags &= ~MIDIBUF_INQUEUE;
322 midihdr->flags |= MIDIBUF_DONE;
324 midihdr->bytesrecorded = midiq->length - midiq->sizeLeft;
327 /* Notify client that Sysex buffer has been sent */
328 emu10k1_midi_callback(msg, card_mpuin->openinfo.refdata, callback_msg);
330 return 0;
333 void emu10k1_mpuin_bh(unsigned long refdata)
335 u8 data;
336 unsigned idx;
337 struct emu10k1_mpuin *card_mpuin = (struct emu10k1_mpuin *) refdata;
338 unsigned long flags;
340 while (card_mpuin->qhead != card_mpuin->qtail) {
341 spin_lock_irqsave(&card_mpuin->lock, flags);
342 idx = card_mpuin->qhead;
343 data = card_mpuin->midiq[idx].data;
344 card_mpuin->timein = card_mpuin->midiq[idx].timein;
345 idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
346 card_mpuin->qhead = idx;
347 spin_unlock_irqrestore(&card_mpuin->lock, flags);
349 sblive_miStateEntry(card_mpuin, data);
352 return;
355 /* IRQ callback handler routine for the MPU in port */
357 int emu10k1_mpuin_irqhandler(struct emu10k1_card *card)
359 unsigned idx;
360 unsigned count;
361 u8 MPUIvalue;
362 struct emu10k1_mpuin *card_mpuin = card->mpuin;
364 /* IRQ service routine. The data and code touched are:
365 * 1. card_mpuin
368 count = 0;
369 idx = card_mpuin->qtail;
371 while (1) {
372 if (emu10k1_mpu_read_data(card, &MPUIvalue) < 0) {
373 break;
374 } else {
375 ++count;
376 card_mpuin->midiq[idx].data = MPUIvalue;
377 card_mpuin->midiq[idx].timein = (jiffies * 1000) / HZ;
378 idx = (idx + 1) % MIDIIN_MAX_BUFFER_SIZE;
382 if (count) {
383 card_mpuin->qtail = idx;
385 tasklet_hi_schedule(&card_mpuin->tasklet);
388 return 0;
391 /*****************************************************************************/
393 /* Supporting functions for Midi-In Interpretation State Machine */
395 /*****************************************************************************/
397 /* FIXME: This should be a macro */
398 static int sblive_miStateInit(struct emu10k1_mpuin *card_mpuin)
400 card_mpuin->status = 0; /* For MIDI running status */
401 card_mpuin->fstatus = 0; /* For 0xFn status only */
402 card_mpuin->curstate = STIN_PARSE;
403 card_mpuin->laststate = STIN_PARSE;
404 card_mpuin->data = 0;
405 card_mpuin->timestart = 0;
406 card_mpuin->timein = 0;
408 return 0;
411 /* FIXME: This should be a macro */
412 static int sblive_miStateEntry(struct emu10k1_mpuin *card_mpuin, u8 data)
414 return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
417 static int sblive_miStateParse(struct emu10k1_mpuin *card_mpuin, u8 data)
419 switch (data & 0xf0) {
420 case 0x80:
421 case 0x90:
422 case 0xA0:
423 case 0xB0:
424 case 0xE0:
425 card_mpuin->curstate = STIN_3BYTE;
426 break;
428 case 0xC0:
429 case 0xD0:
430 card_mpuin->curstate = STIN_2BYTE;
431 break;
433 case 0xF0:
434 /* System messages do not affect the previous running status! */
435 switch (data & 0x0f) {
436 case 0x0:
437 card_mpuin->laststate = card_mpuin->curstate;
438 card_mpuin->curstate = STIN_SYS_EX_NORM;
440 if (card_mpuin->firstmidiq) {
441 struct midi_queue *midiq;
443 midiq = card_mpuin->firstmidiq;
444 *midiq->midibyte = data;
445 --midiq->sizeLeft;
446 ++midiq->midibyte;
449 return CTSTATUS_NEXT_BYTE;
451 case 0x7:
452 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, 0xf7, 0);
453 return -1;
455 case 0x2:
456 card_mpuin->laststate = card_mpuin->curstate;
457 card_mpuin->curstate = STIN_SYS_COMMON_3;
458 break;
460 case 0x1:
461 case 0x3:
462 card_mpuin->laststate = card_mpuin->curstate;
463 card_mpuin->curstate = STIN_SYS_COMMON_2;
464 break;
466 default:
467 /* includes 0xF4 - 0xF6, 0xF8 - 0xFF */
468 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
471 break;
473 default:
474 DPF(2, "BUG: default case hit\n");
475 return -1;
478 return midistatefn[card_mpuin->curstate].Fn(card_mpuin, data);
481 static int sblive_miState3Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
483 u8 temp = data & 0xf0;
485 if (temp < 0x80) {
486 return midistatefn[STIN_3BYTE_KEY].Fn(card_mpuin, data);
487 } else if (temp <= 0xe0 && temp != 0xc0 && temp != 0xd0) {
488 card_mpuin->status = data;
489 card_mpuin->curstate = STIN_3BYTE_KEY;
491 return CTSTATUS_NEXT_BYTE;
494 return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
497 static int sblive_miState3ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
498 /* byte 1 */
500 unsigned long tmp;
502 if (data > 0x7f) {
503 /* Real-time messages check */
504 if (data > 0xf7)
505 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
507 /* Invalid data! */
508 DPF(2, "Invalid data!\n");
510 card_mpuin->curstate = STIN_PARSE;
511 tmp = ((unsigned long) data) << 8;
512 tmp |= (unsigned long) card_mpuin->status;
514 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
516 return -1;
519 card_mpuin->data = data;
520 card_mpuin->curstate = STIN_3BYTE_VEL;
522 return CTSTATUS_NEXT_BYTE;
525 static int sblive_miState3ByteVel(struct emu10k1_mpuin *card_mpuin, u8 data)
526 /* byte 2 */
528 unsigned long tmp;
530 if (data > 0x7f) {
531 /* Real-time messages check */
532 if (data > 0xf7)
533 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
535 /* Invalid data! */
536 DPF(2, "Invalid data!\n");
538 card_mpuin->curstate = STIN_PARSE;
539 tmp = ((unsigned long) data) << 8;
540 tmp |= card_mpuin->data;
541 tmp = tmp << 8;
542 tmp |= (unsigned long) card_mpuin->status;
544 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
546 return -1;
549 card_mpuin->curstate = STIN_3BYTE;
550 tmp = (unsigned long) data;
551 tmp = tmp << 8;
552 tmp |= (unsigned long) card_mpuin->data;
553 tmp = tmp << 8;
554 tmp |= (unsigned long) card_mpuin->status;
556 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);
558 return 0;
561 static int sblive_miState2Byte(struct emu10k1_mpuin *card_mpuin, u8 data)
563 u8 temp = data & 0xf0;
565 if ((temp == 0xc0) || (temp == 0xd0)) {
566 card_mpuin->status = data;
567 card_mpuin->curstate = STIN_2BYTE_KEY;
569 return CTSTATUS_NEXT_BYTE;
572 if (temp < 0x80)
573 return midistatefn[STIN_2BYTE_KEY].Fn(card_mpuin, data);
575 return midistatefn[STIN_PARSE].Fn(card_mpuin, data);
578 static int sblive_miState2ByteKey(struct emu10k1_mpuin *card_mpuin, u8 data)
579 /* byte 1 */
581 unsigned long tmp;
583 if (data > 0x7f) {
584 /* Real-time messages check */
585 if (data > 0xf7)
586 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
588 /* Invalid data! */
589 DPF(2, "Invalid data!\n");
591 card_mpuin->curstate = STIN_PARSE;
592 tmp = (unsigned long) data;
593 tmp = tmp << 8;
594 tmp |= (unsigned long) card_mpuin->status;
596 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
598 return -1;
601 card_mpuin->curstate = STIN_2BYTE;
602 tmp = (unsigned long) data;
603 tmp = tmp << 8;
604 tmp |= (unsigned long) card_mpuin->status;
606 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);
608 return 0;
611 static int sblive_miStateSysCommon2(struct emu10k1_mpuin *card_mpuin, u8 data)
613 card_mpuin->fstatus = data;
614 card_mpuin->curstate = STIN_SYS_COMMON_2_KEY;
616 return CTSTATUS_NEXT_BYTE;
619 static int sblive_miStateSysCommon2Key(struct emu10k1_mpuin *card_mpuin, u8 data)
620 /* byte 1 */
622 unsigned long tmp;
624 if (data > 0x7f) {
625 /* Real-time messages check */
626 if (data > 0xf7)
627 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
629 /* Invalid data! */
630 DPF(2, "Invalid data!\n");
632 card_mpuin->curstate = card_mpuin->laststate;
633 tmp = (unsigned long) data;
634 tmp = tmp << 8;
635 tmp |= (unsigned long) card_mpuin->fstatus;
637 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
639 return -1;
642 card_mpuin->curstate = card_mpuin->laststate;
643 tmp = (unsigned long) data;
644 tmp = tmp << 8;
645 tmp |= (unsigned long) card_mpuin->fstatus;
647 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 2);
649 return 0;
652 static int sblive_miStateSysCommon3(struct emu10k1_mpuin *card_mpuin, u8 data)
654 card_mpuin->fstatus = data;
655 card_mpuin->curstate = STIN_SYS_COMMON_3_KEY;
657 return CTSTATUS_NEXT_BYTE;
660 static int sblive_miStateSysCommon3Key(struct emu10k1_mpuin *card_mpuin, u8 data)
661 /* byte 1 */
663 unsigned long tmp;
665 if (data > 0x7f) {
666 /* Real-time messages check */
667 if (data > 0xf7)
668 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
670 /* Invalid data! */
671 DPF(2, "Invalid data!\n");
673 card_mpuin->curstate = card_mpuin->laststate;
674 tmp = (unsigned long) data;
675 tmp = tmp << 8;
676 tmp |= (unsigned long) card_mpuin->fstatus;
678 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
680 return -1;
683 card_mpuin->data = data;
684 card_mpuin->curstate = STIN_SYS_COMMON_3_VEL;
686 return CTSTATUS_NEXT_BYTE;
689 static int sblive_miStateSysCommon3Vel(struct emu10k1_mpuin *card_mpuin, u8 data)
690 /* byte 2 */
692 unsigned long tmp;
694 if (data > 0x7f) {
695 /* Real-time messages check */
696 if (data > 0xf7)
697 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
699 /* Invalid data! */
700 DPF(2, "Invalid data!\n");
702 card_mpuin->curstate = card_mpuin->laststate;
703 tmp = (unsigned long) data;
704 tmp = tmp << 8;
705 tmp |= (unsigned long) card_mpuin->data;
706 tmp = tmp << 8;
707 tmp |= (unsigned long) card_mpuin->fstatus;
709 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATAERROR, tmp, 0);
711 return -1;
714 card_mpuin->curstate = card_mpuin->laststate;
715 tmp = (unsigned long) data;
716 tmp = tmp << 8;
717 tmp |= (unsigned long) card_mpuin->data;
718 tmp = tmp << 8;
719 tmp |= (unsigned long) card_mpuin->fstatus;
721 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, tmp, 3);
723 return 0;
726 static int sblive_miStateSysExNorm(struct emu10k1_mpuin *card_mpuin, u8 data)
728 unsigned long flags;
730 if ((data > 0x7f) && (data != 0xf7)) {
731 /* Real-time messages check */
732 if (data > 0xf7)
733 return midistatefn[STIN_SYS_REAL].Fn(card_mpuin, data);
735 /* Invalid Data! */
736 DPF(2, "Invalid data!\n");
738 card_mpuin->curstate = card_mpuin->laststate;
740 if (card_mpuin->firstmidiq) {
741 struct midi_queue *midiq;
743 midiq = card_mpuin->firstmidiq;
744 *midiq->midibyte = data;
745 --midiq->sizeLeft;
746 ++midiq->midibyte;
748 spin_lock_irqsave(&card_mpuin->lock, flags);
750 card_mpuin->firstmidiq = midiq->next;
751 if (card_mpuin->firstmidiq == NULL)
752 card_mpuin->lastmidiq = NULL;
754 spin_unlock_irqrestore(&card_mpuin->lock, flags);
756 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGERROR, (unsigned long) midiq, 0);
758 kfree(midiq);
761 return -1;
764 if (card_mpuin->firstmidiq) {
765 struct midi_queue *midiq;
767 midiq = card_mpuin->firstmidiq;
768 *midiq->midibyte = data;
769 --midiq->sizeLeft;
770 ++midiq->midibyte;
773 if (data == 0xf7) {
774 /* End of Sysex buffer */
775 /* Send down the buffer */
777 card_mpuin->curstate = card_mpuin->laststate;
779 if (card_mpuin->firstmidiq) {
780 struct midi_queue *midiq;
782 midiq = card_mpuin->firstmidiq;
784 spin_lock_irqsave(&card_mpuin->lock, flags);
786 card_mpuin->firstmidiq = midiq->next;
787 if (card_mpuin->firstmidiq == NULL)
788 card_mpuin->lastmidiq = NULL;
790 spin_unlock_irqrestore(&card_mpuin->lock, flags);
792 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
794 kfree(midiq);
797 return 0;
800 if (card_mpuin->firstmidiq) {
801 struct midi_queue *midiq;
803 midiq = card_mpuin->firstmidiq;
805 if (midiq->sizeLeft == 0) {
806 /* Special case */
808 spin_lock_irqsave(&card_mpuin->lock, flags);
810 card_mpuin->firstmidiq = midiq->next;
811 if (card_mpuin->firstmidiq == NULL)
812 card_mpuin->lastmidiq = NULL;
814 spin_unlock_irqrestore(&card_mpuin->lock, flags);
816 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INLONGDATA, (unsigned long) midiq, 0);
818 kfree(midiq);
820 return CTSTATUS_NEXT_BYTE;
824 return CTSTATUS_NEXT_BYTE;
827 static int sblive_miStateSysReal(struct emu10k1_mpuin *card_mpuin, u8 data)
829 emu10k1_mpuin_callback(card_mpuin, ICARDMIDI_INDATA, data, 1);
831 return CTSTATUS_NEXT_BYTE;