Blame view

sound/oss/uart401.c 10.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
f30c22695   Uwe Zeisberger   fix file specific...
2
   * sound/oss/uart401.c
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   *
   * MPU-401 UART driver (formerly uart401_midi.c)
   *
   *
   * Copyright (C) by Hannu Savolainen 1993-1997
   *
   * 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.
   *
   * Changes:
   *	Alan Cox		Reformatted, removed sound_mem usage, use normal Linux
   *				interrupt allocation. Protect against bogus unload
   *				Fixed to allow IRQ > 15
   *	Christoph Hellwig	Adapted to module_init/module_exit
   *	Arnaldo C. de Melo	got rid of check_region
   *
   * Status:
   *		Untested
   */
  
  #include <linux/init.h>
  #include <linux/interrupt.h>
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
27
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
  #include <linux/spinlock.h>
  #include "sound_config.h"
  
  #include "mpu401.h"
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
32
  struct uart401_devc
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
35
36
37
38
39
40
41
42
  {
  	int             base;
  	int             irq;
  	int            *osp;
  	void            (*midi_input_intr) (int dev, unsigned char data);
  	int             opened, disabled;
  	volatile unsigned char input_byte;
  	int             my_dev;
  	int             share_irq;
  	spinlock_t	lock;
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
43
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
  
  #define	DATAPORT   (devc->base)
  #define	COMDPORT   (devc->base+1)
  #define	STATPORT   (devc->base+1)
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
48
  static int uart401_status(struct uart401_devc *devc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
  {
  	return inb(STATPORT);
  }
  
  #define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
  #define output_ready(devc)	(!(uart401_status(devc)&OUTPUT_READY))
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
55
  static void uart401_cmd(struct uart401_devc *devc, unsigned char cmd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
  {
  	outb((cmd), COMDPORT);
  }
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
59
  static int uart401_read(struct uart401_devc *devc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
  {
  	return inb(DATAPORT);
  }
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
63
  static void uart401_write(struct uart401_devc *devc, unsigned char byte)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
70
71
72
  {
  	outb((byte), DATAPORT);
  }
  
  #define	OUTPUT_READY	0x40
  #define	INPUT_AVAIL	0x80
  #define	MPU_ACK		0xFE
  #define	MPU_RESET	0xFF
  #define	UART_MODE_ON	0x3F
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
73
74
  static int      reset_uart401(struct uart401_devc *devc);
  static void     enter_uart_mode(struct uart401_devc *devc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75

80e7bbac4   Himangi Saraogi   sound: oss: uart4...
76
  static void uart401_input_loop(struct uart401_devc *devc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  {
  	int work_limit=30000;
  	
  	while (input_avail(devc) && --work_limit)
  	{
  		unsigned char   c = uart401_read(devc);
  
  		if (c == MPU_ACK)
  			devc->input_byte = c;
  		else if (devc->opened & OPEN_READ && devc->midi_input_intr)
  			devc->midi_input_intr(devc->my_dev, c);
  	}
  	if(work_limit==0)
  		printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??
  ", devc->base);
  }
7d12e780e   David Howells   IRQ: Maintain reg...
93
  irqreturn_t uart401intr(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  {
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
95
  	struct uart401_devc *devc = dev_id;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  
  	if (devc == NULL)
  	{
  		printk(KERN_ERR "uart401: bad devc
  ");
  		return IRQ_NONE;
  	}
  
  	if (input_avail(devc))
  		uart401_input_loop(devc);
  	return IRQ_HANDLED;
  }
  
  static int
  uart401_open(int dev, int mode,
  	     void            (*input) (int dev, unsigned char data),
  	     void            (*output) (int dev)
  )
  {
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
115
116
  	struct uart401_devc *devc = (struct uart401_devc *)
  				    midi_devs[dev]->devc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  
  	if (devc->opened)
  		return -EBUSY;
  
  	/* Flush the UART */
  	
  	while (input_avail(devc))
  		uart401_read(devc);
  
  	devc->midi_input_intr = input;
  	devc->opened = mode;
  	enter_uart_mode(devc);
  	devc->disabled = 0;
  
  	return 0;
  }
  
  static void uart401_close(int dev)
  {
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
136
137
  	struct uart401_devc *devc = (struct uart401_devc *)
  				    midi_devs[dev]->devc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
139
140
141
142
143
144
145
146
  
  	reset_uart401(devc);
  	devc->opened = 0;
  }
  
  static int uart401_out(int dev, unsigned char midi_byte)
  {
  	int timeout;
  	unsigned long flags;
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
147
148
  	struct uart401_devc *devc = (struct uart401_devc *)
  				    midi_devs[dev]->devc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  
  	if (devc->disabled)
  		return 1;
  	/*
  	 * Test for input since pending input seems to block the output.
  	 */
  
  	spin_lock_irqsave(&devc->lock,flags);	
  	if (input_avail(devc))
  		uart401_input_loop(devc);
  
  	spin_unlock_irqrestore(&devc->lock,flags);
  
  	/*
  	 * Sometimes it takes about 13000 loops before the output becomes ready
  	 * (After reset). Normally it takes just about 10 loops.
  	 */
  
  	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
  
  	if (!output_ready(devc))
  	{
  		  printk(KERN_WARNING "uart401: Timeout - Device not responding
  ");
  		  devc->disabled = 1;
  		  reset_uart401(devc);
  		  enter_uart_mode(devc);
  		  return 1;
  	}
  	uart401_write(devc, midi_byte);
  	return 1;
  }
  
  static inline int uart401_start_read(int dev)
  {
  	return 0;
  }
  
  static inline int uart401_end_read(int dev)
  {
  	return 0;
  }
  
  static inline void uart401_kick(int dev)
  {
  }
  
  static inline int uart401_buffer_status(int dev)
  {
  	return 0;
  }
  
  #define MIDI_SYNTH_NAME	"MPU-401 UART"
  #define MIDI_SYNTH_CAPS	SYNTH_CAP_INPUT
  #include "midi_synth.h"
  
  static const struct midi_operations uart401_operations =
  {
  	.owner		= THIS_MODULE,
  	.info		= {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
  	.converter	= &std_midi_synth,
  	.in_info	= {0},
  	.open		= uart401_open,
  	.close		= uart401_close,
  	.outputc	= uart401_out,
  	.start_read	= uart401_start_read,
  	.end_read	= uart401_end_read,
  	.kick		= uart401_kick,
  	.buffer_status	= uart401_buffer_status,
  };
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
219
  static void enter_uart_mode(struct uart401_devc *devc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
  {
  	int ok, timeout;
  	unsigned long flags;
  
  	spin_lock_irqsave(&devc->lock,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;
  
  	spin_unlock_irqrestore(&devc->lock,flags);
  }
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
240
  static int reset_uart401(struct uart401_devc *devc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  {
  	int ok, timeout, n;
  
  	/*
  	 * Send the RESET command. Try again if no success at the first time.
  	 */
  
  	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;
  			}
  		}
  	}
3af5d0524   Joe Perches   sound/oss: Remove...
272
  	/* Flush input before enabling interrupts */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
273
  	if (ok)
3af5d0524   Joe Perches   sound/oss: Remove...
274
  		uart401_input_loop(devc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
  	else
  		DDB(printk("Reset UART401 failed - No hardware detected.
  "));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
  	return ok;
  }
  
  int probe_uart401(struct address_info *hw_config, struct module *owner)
  {
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
283
  	struct uart401_devc *devc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  	char *name = "MPU-401 (UART) MIDI";
  	int ok = 0;
  	unsigned long flags;
  
  	DDB(printk("Entered probe_uart401()
  "));
  
  	/* Default to "not found" */
  	hw_config->slots[4] = -1;
  
  	if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
  		printk(KERN_INFO "uart401: could not request_region(%d, 4)
  ", hw_config->io_base);
  		return 0;
  	}
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
299
  	devc = kmalloc(sizeof(struct uart401_devc), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	if (!devc) {
  		printk(KERN_WARNING "uart401: Can't allocate memory
  ");
  		goto cleanup_region;
  	}
  
  	devc->base = hw_config->io_base;
  	devc->irq = hw_config->irq;
  	devc->osp = hw_config->osp;
  	devc->midi_input_intr = NULL;
  	devc->opened = 0;
  	devc->input_byte = 0;
  	devc->my_dev = 0;
  	devc->share_irq = 0;
  	spin_lock_init(&devc->lock);
  
  	spin_lock_irqsave(&devc->lock,flags);	
  	ok = reset_uart401(devc);
  	spin_unlock_irqrestore(&devc->lock,flags);
  
  	if (!ok)
  		goto cleanup_devc;
  
  	if (hw_config->name)
  		name = hw_config->name;
  
  	if (devc->irq < 0) {
  		devc->share_irq = 1;
  		devc->irq *= -1;
  	} else
  		devc->share_irq = 0;
  
  	if (!devc->share_irq)
  		if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
  			printk(KERN_WARNING "uart401: Failed to allocate IRQ%d
  ", devc->irq);
  			devc->share_irq = 1;
  		}
  	devc->my_dev = sound_alloc_mididev();
  	enter_uart_mode(devc);
  
  	if (devc->my_dev == -1) {
  		printk(KERN_INFO "uart401: Too many midi devices detected
  ");
  		goto cleanup_irq;
  	}
  	conf_printf(name, hw_config);
0d9ffc979   Alexandru Gheorghiu   sound: oss: uart4...
347
348
349
  	midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
  					  sizeof(struct midi_operations),
  					  GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
350
351
352
353
354
  	if (!midi_devs[devc->my_dev]) {
  		printk(KERN_ERR "uart401: Failed to allocate memory
  ");
  		goto cleanup_unload_mididev;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
355
356
357
358
359
  
  	if (owner)
  		midi_devs[devc->my_dev]->owner = owner;
  	
  	midi_devs[devc->my_dev]->devc = devc;
0d9ffc979   Alexandru Gheorghiu   sound: oss: uart4...
360
361
362
  	midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
  						     sizeof(struct synth_operations),
  						     GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
364
365
366
367
  	if (!midi_devs[devc->my_dev]->converter) {
  		printk(KERN_WARNING "uart401: Failed to allocate memory
  ");
  		goto cleanup_midi_devs;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	strcpy(midi_devs[devc->my_dev]->info.name, name);
  	midi_devs[devc->my_dev]->converter->id = "UART401";
  	midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
  
  	if (owner)
  		midi_devs[devc->my_dev]->converter->owner = owner;
  
  	hw_config->slots[4] = devc->my_dev;
  	sequencer_init();
  	devc->opened = 0;
  	return 1;
  cleanup_midi_devs:
  	kfree(midi_devs[devc->my_dev]);
  cleanup_unload_mididev:
  	sound_unload_mididev(devc->my_dev);
  cleanup_irq:
  	if (!devc->share_irq)
  		free_irq(devc->irq, devc);
  cleanup_devc:
  	kfree(devc);
  cleanup_region:
  	release_region(hw_config->io_base, 4);
  	return 0;
  }
  
  void unload_uart401(struct address_info *hw_config)
  {
80e7bbac4   Himangi Saraogi   sound: oss: uart4...
395
  	struct uart401_devc *devc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  	int n=hw_config->slots[4];
  	
  	/* Not set up */
  	if(n==-1 || midi_devs[n]==NULL)
  		return;
  		
  	/* Not allocated (erm ??) */
  	
  	devc = midi_devs[hw_config->slots[4]]->devc;
  	if (devc == NULL)
  		return;
  
  	reset_uart401(devc);
  	release_region(hw_config->io_base, 4);
  
  	if (!devc->share_irq)
  		free_irq(devc->irq, devc);
4fc390a19   Dan Carpenter   sound: oss: uart4...
413
414
415
  	kfree(midi_devs[devc->my_dev]->converter);
  	kfree(midi_devs[devc->my_dev]);
  	kfree(devc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	/* This kills midi_devs[x] */
  	sound_unload_mididev(hw_config->slots[4]);
  }
  
  EXPORT_SYMBOL(probe_uart401);
  EXPORT_SYMBOL(unload_uart401);
  EXPORT_SYMBOL(uart401intr);
  
  static struct address_info cfg_mpu;
  
  static int io = -1;
  static int irq = -1;
  
  module_param(io, int, 0444);
  module_param(irq, int, 0444);
  
  
  static int __init init_uart401(void)
  {
  	cfg_mpu.irq = irq;
  	cfg_mpu.io_base = io;
  
  	/* Can be loaded either for module use or to provide functions
  	   to others */
  	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
  		printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
  		if (!probe_uart401(&cfg_mpu, THIS_MODULE))
  			return -ENODEV;
  	}
  
  	return 0;
  }
  
  static void __exit cleanup_uart401(void)
  {
  	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
  		unload_uart401(&cfg_mpu);
  }
  
  module_init(init_uart401);
  module_exit(cleanup_uart401);
  
  #ifndef MODULE
  static int __init setup_uart401(char *str)
  {
  	/* io, irq */
  	int ints[3];
  	
  	str = get_options(str, ARRAY_SIZE(ints), ints);
  
  	io = ints[1];
  	irq = ints[2];
  	
  	return 1;
  }
  
  __setup("uart401=", setup_uart401);
  #endif
  MODULE_LICENSE("GPL");