summaryrefslogtreecommitdiff
path: root/kernel/framework/uart401/oss_uart401.c
blob: 58945af9749801d90b178e9786499d51da253451 (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
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
/*
 * Purpose: MPU-401 (UART mode) driver
 *
 * This driver/library can be used by the drivers for devices that provide
 * MPU-401 (UART) compatible interface.
 */
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

#include "oss_config.h"
#include "midi_core.h"

#include "uart401.h"

#define	DATAPORT   (devc->base)
#define	COMDPORT   (devc->base+1)
#define	STATPORT   (devc->base+1)

static int
uart401_status (uart401_devc * devc)
{
  return INB (devc->osdev, STATPORT);
}

#define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
#define output_ready(devc)	(!(uart401_status(devc)&OUTPUT_READY))
static void
uart401_cmd (uart401_devc * devc, unsigned char cmd)
{
  OUTB (devc->osdev, cmd, COMDPORT);
}
static int
uart401_read (uart401_devc * devc)
{
  return INB (devc->osdev, DATAPORT);
}
static void
uart401_write (uart401_devc * devc, unsigned char byte)
{
  OUTB (devc->osdev, byte, DATAPORT);
}

#define	OUTPUT_READY	0x40
#define	INPUT_AVAIL	0x80
#define	MPU_ACK		0xFE
#define	MPU_RESET	0xFF
#define	UART_MODE_ON	0x3F

static int reset_uart401 (uart401_devc * devc);
static void enter_uart_mode (uart401_devc * devc);

static void
uart401_input_loop (uart401_devc * devc)
{
  unsigned char buf[128];
  int l = 0;

  while (input_avail (devc))
    {
      int dev;
      unsigned char c;

      c = uart401_read (devc);

      dev = devc->my_dev;
      if (midi_devs[dev]->input_callback != NULL)
	midi_devs[dev]->input_callback (dev, c);

      if (c == MPU_ACK)
	devc->input_byte = c;
      else if ((devc->opened & OPEN_READ))
	{
	  buf[l++] = c;

	  if (l >= sizeof (buf))	/* Buffer full */
	    {
	      if (devc->save_input_buffer != NULL)
		devc->save_input_buffer (devc->my_dev, buf, l);
	      l = 0;
	    }
	}

      if (l > 0)
	{
	  if (devc->save_input_buffer != NULL)
	    devc->save_input_buffer (devc->my_dev, buf, l);
	}
    }
}

void
uart401_irq (uart401_devc * devc)
{
  if (devc->base == 0)
    {
      /* cmn_err(CE_CONT, "uart401_irq: Bad base address\n"); */
      return;
    }

  if (input_avail (devc))
    {
      uart401_input_loop (devc);
    }
}

/*ARGSUSED*/
static int
uart401_open (int dev, int mode, oss_midi_inputbyte_t inputbyte,
	      oss_midi_inputbuf_t inputbuf, oss_midi_outputintr_t outputintr)
{
  uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;

  if (devc->opened)
    {
      return OSS_EBUSY;
    }

  while (input_avail (devc))
    uart401_read (devc);

  devc->save_input_buffer = inputbuf;
  devc->opened = mode;
  enter_uart_mode (devc);
  devc->disabled = 0;

  return 0;
}

/*ARGSUSED*/
static void
uart401_close (int dev, int mode)
{
  uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;

  reset_uart401 (devc);
  oss_udelay (10);
  enter_uart_mode (devc);
  reset_uart401 (devc);
  devc->save_input_buffer = NULL;
  devc->opened = 0;
}

static int
uart401_out (int dev, unsigned char midi_byte)
{
  int timeout;
  uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
  oss_native_word flags;

  if (devc->disabled)
    return 1;
  /*
   * Test for input since pending input seems to block the output.
   */

  MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);

  if (input_avail (devc))
    uart401_input_loop (devc);
  MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);

  /*
   * Sometimes it takes about 130000 loops before the output becomes ready
   * (After reset). Normally it takes just about 10 loops.
   */

  for (timeout = 13000; timeout > 0 && !output_ready (devc); timeout--);

  if (!output_ready (devc))
    {
      return 0;
    }

  uart401_write (devc, midi_byte);
  return 1;
}

/*ARGSUSED*/
static int
uart401_ioctl (int dev, unsigned cmd, ioctl_arg arg)
{
  return OSS_EINVAL;
}

static midi_driver_t uart401_driver = {
  uart401_open,
  uart401_close,
  uart401_ioctl,
  uart401_out
};

static void
enter_uart_mode (uart401_devc * devc)
{
  int ok, timeout;
  oss_native_word flags;

  MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
  for (timeout = 30000; timeout > 0 && !output_ready (devc); timeout--);

  devc->input_byte = 0;
  uart401_cmd (devc, UART_MODE_ON);

  ok = 0;
  for (timeout = 50000; timeout > 0 && !ok; timeout--)
    if (devc->input_byte == MPU_ACK)
      ok = 1;
    else if (input_avail (devc))
      if (uart401_read (devc) == MPU_ACK)
	ok = 1;

  MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
}

static int
reset_uart401 (uart401_devc * devc)
{
  int ok, timeout, n;
  oss_native_word flags;

  /*
   * Send the RESET command. Try again if no success at the first time.
   */
  MUTEX_ENTER_IRQDISABLE (devc->mutex, flags);
  ok = 0;

  for (n = 0; n < 2 && !ok; n++)
    {
      for (timeout = 30000; timeout > 0 && !output_ready (devc); timeout--);

      devc->input_byte = 0;
      uart401_cmd (devc, MPU_RESET);

      /*
       * Wait at least 25 msec. This method is not accurate so let's make the
       * loop bit longer. Cannot sleep since this is called during boot.
       */

      for (timeout = 50000; timeout > 0 && !ok; timeout--)
	if (devc->input_byte == MPU_ACK)	/* Interrupt */
	  ok = 1;
	else if (input_avail (devc))
	  if (uart401_read (devc) == MPU_ACK)
	    ok = 1;

    }



  if (ok)
    uart401_input_loop (devc);	/*
				 * Flush input before enabling interrupts
				 */
  MUTEX_EXIT_IRQRESTORE (devc->mutex, flags);
  return ok;
}

int
uart401_init (uart401_devc * devc, oss_device_t * osdev, int base, char *name)
{
  int ok = 0;

  DDB (cmn_err (CE_CONT, "Entered uart401_init(%x)\n", base));

  devc->base = base;
  devc->irq = 0;
  devc->osdev = osdev;
  MUTEX_INIT (devc->osdev, devc->mutex, MH_DRV + 3);
  devc->running = 1;

  devc->save_input_buffer = NULL;
  devc->opened = 0;
  devc->input_byte = 0;
  devc->my_dev = 0;
  devc->share_irq = 0;

  ok = reset_uart401 (devc);

  if (ok)
    {
      DDB (cmn_err (CE_CONT, "Reset UART401 OK\n"));
    }
  else
    {
      DDB (cmn_err
	   (CE_CONT, "Reset UART401 failed (no hardware present?).\n"));
      DDB (cmn_err (CE_CONT, "mpu401 status %02x\n", uart401_status (devc)));
    }

  if (!ok)
    {
      return 0;
    }

  DDB (cmn_err (CE_CONT, "uart401 detected OK\n"));

  enter_uart_mode (devc);

  devc->my_dev = oss_install_mididev (OSS_MIDI_DRIVER_VERSION, "UART401", name, &uart401_driver, sizeof (midi_driver_t),
				      0, devc, devc->osdev);
  devc->opened = 0;
#ifdef USE_POLLING
  if (!timer_armed)
    {
      timer_armed = 1;
      poll_interval = OSS_HZ / 10;
      if (poll_interval < 1)
	poll_interval = 1;
      INIT_TIMER (uart401_timer, uart401_poll);
      ACTIVATE_TIMER (uart401_timer, uart401_poll, poll_interval);
    }
#endif
  return 1;
}

void
uart401_disable (uart401_devc * devc)
{
#ifdef USE_POLLING
  if (timer_armed)
    {
      timer_armed = 0;
      REMOVE_TIMER (uart401_timer, uart401_poll);
    }
  else;
#endif
  if (!devc->running)
    return;
  reset_uart401 (devc);
  devc->running = 0;
}