summaryrefslogtreecommitdiffstats
path: root/drivers/sound/pas2_midi.c
blob: 74d04750e432a7151d845e7a8f3033e308afc4e0 (plain)
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
/*
 * sound/pas2_midi.c
 *
 * The low level driver for the PAS Midi Interface.
 */
/*
 * Copyright (C) by Hannu Savolainen 1993-1996
 *
 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 * for more info.
 */
#include <linux/config.h>


#include "sound_config.h"

#if defined(CONFIG_PAS) && defined(CONFIG_MIDI)

static int      midi_busy = 0, input_opened = 0;
static int      my_dev;

static unsigned char tmp_queue[256];
static volatile int qlen;
static volatile unsigned char qhead, qtail;

static void     (*midi_input_intr) (int dev, unsigned char data);

static int
pas_midi_open (int dev, int mode,
	       void            (*input) (int dev, unsigned char data),
	       void            (*output) (int dev)
)
{
  int             err;
  unsigned long   flags;
  unsigned char   ctrl;


  if (midi_busy)
    {
      printk ("PAS2: Midi busy\n");
      return -EBUSY;
    }

  /*
   * Reset input and output FIFO pointers
   */
  pas_write (0x20 | 0x40,
	     0x178b);

  save_flags (flags);
  cli ();

  if ((err = pas_set_intr (0x10)) < 0)
    return err;

  /*
   * Enable input available and output FIFO empty interrupts
   */

  ctrl = 0;
  input_opened = 0;
  midi_input_intr = input;

  if (mode == OPEN_READ || mode == OPEN_READWRITE)
    {
      ctrl |= 0x04;		/* Enable input */
      input_opened = 1;
    }

  if (mode == OPEN_WRITE || mode == OPEN_READWRITE)
    {
      ctrl |= 0x08 | 0x10;	/* Enable output */
    }

  pas_write (ctrl, 0x178b);

  /*
   * Acknowledge any pending interrupts
   */

  pas_write (0xff, 0x1B88);

  restore_flags (flags);

  midi_busy = 1;
  qlen = qhead = qtail = 0;
  return 0;
}

static void
pas_midi_close (int dev)
{

  /*
   * Reset FIFO pointers, disable intrs
   */
  pas_write (0x20 | 0x40, 0x178b);

  pas_remove_intr (0x10);
  midi_busy = 0;
}

static int
dump_to_midi (unsigned char midi_byte)
{
  int             fifo_space, x;

  fifo_space = ((x = pas_read (0x1B89)) >> 4) & 0x0f;

/*
 * The MIDI FIFO space register and it's documentation is nonunderstandable.
 * There seem to be no way to differentiate between buffer full and buffer
 * empty situations. For this reason we don't never write the buffer
 * completely full. In this way we can assume that 0 (or is it 15)
 * means that the buffer is empty.
 */

  if (fifo_space < 2 && fifo_space != 0)	/* Full (almost) */
    {
      return 0;			/* Ask upper layers to retry after some time */
    }

  pas_write (midi_byte, 0x178A);

  return 1;
}

static int
pas_midi_out (int dev, unsigned char midi_byte)
{

  unsigned long   flags;

  /*
   * Drain the local queue first
   */

  save_flags (flags);
  cli ();

  while (qlen && dump_to_midi (tmp_queue[qhead]))
    {
      qlen--;
      qhead++;
    }

  restore_flags (flags);

  /*
   * Output the byte if the local queue is empty.
   */

  if (!qlen)
    if (dump_to_midi (midi_byte))
      return 1;

  /*
   * Put to the local queue
   */

  if (qlen >= 256)
    return 0;			/* Local queue full */

  save_flags (flags);
  cli ();

  tmp_queue[qtail] = midi_byte;
  qlen++;
  qtail++;

  restore_flags (flags);

  return 1;
}

static int
pas_midi_start_read (int dev)
{
  return 0;
}

static int
pas_midi_end_read (int dev)
{
  return 0;
}

static int
pas_midi_ioctl (int dev, unsigned cmd, caddr_t arg)
{
  return -EINVAL;
}

static void
pas_midi_kick (int dev)
{
}

static int
pas_buffer_status (int dev)
{
  return qlen;
}

#define MIDI_SYNTH_NAME	"Pro Audio Spectrum Midi"
#define MIDI_SYNTH_CAPS	SYNTH_CAP_INPUT
#include "midi_synth.h"

static struct midi_operations pas_midi_operations =
{
  {"Pro Audio Spectrum", 0, 0, SNDCARD_PAS},
  &std_midi_synth,
  {0},
  pas_midi_open,
  pas_midi_close,
  pas_midi_ioctl,
  pas_midi_out,
  pas_midi_start_read,
  pas_midi_end_read,
  pas_midi_kick,
  NULL,
  pas_buffer_status,
  NULL
};

void
pas_midi_init (void)
{
  if (num_midis >= MAX_MIDI_DEV)
    {
      printk ("Sound: Too many midi devices detected\n");
      return;
    }

  std_midi_synth.midi_dev = my_dev = num_midis;
  midi_devs[num_midis++] = &pas_midi_operations;
}

void
pas_midi_interrupt (void)
{
  unsigned char   stat;
  int             i, incount;
  unsigned long   flags;

  stat = pas_read (0x1B88);

  if (stat & 0x04)		/* Input data available */
    {
      incount = pas_read (0x1B89) & 0x0f;	/* Input FIFO size */
      if (!incount)
	incount = 16;

      for (i = 0; i < incount; i++)
	if (input_opened)
	  {
	    midi_input_intr (my_dev, pas_read (0x178A));
	  }
	else
	  pas_read (0x178A);	/* Flush */
    }

  if (stat & (0x08 | 0x10))
    {
      save_flags (flags);
      cli ();

      while (qlen && dump_to_midi (tmp_queue[qhead]))
	{
	  qlen--;
	  qhead++;
	}

      restore_flags (flags);
    }


  if (stat & 0x40)
    {
      printk ("MIDI output overrun %x,%x\n", pas_read (0x1B89), stat);
    }

  pas_write (stat, 0x1B88);	/* Acknowledge interrupts */
}

#endif