Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 * sound/oss/uart401.c
  3 *
  4 * MPU-401 UART driver (formerly uart401_midi.c)
  5 *
  6 *
  7 * Copyright (C) by Hannu Savolainen 1993-1997
  8 *
  9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 11 * for more info.
 12 *
 13 * Changes:
 14 *	Alan Cox		Reformatted, removed sound_mem usage, use normal Linux
 15 *				interrupt allocation. Protect against bogus unload
 16 *				Fixed to allow IRQ > 15
 17 *	Christoph Hellwig	Adapted to module_init/module_exit
 18 *	Arnaldo C. de Melo	got rid of check_region
 19 *
 20 * Status:
 21 *		Untested
 22 */
 23
 24#include <linux/init.h>
 25#include <linux/interrupt.h>
 26#include <linux/module.h>
 27#include <linux/slab.h>
 28#include <linux/spinlock.h>
 29#include "sound_config.h"
 30
 31#include "mpu401.h"
 32
 33typedef struct uart401_devc
 34{
 35	int             base;
 36	int             irq;
 37	int            *osp;
 38	void            (*midi_input_intr) (int dev, unsigned char data);
 39	int             opened, disabled;
 40	volatile unsigned char input_byte;
 41	int             my_dev;
 42	int             share_irq;
 43	spinlock_t	lock;
 44}
 45uart401_devc;
 46
 47#define	DATAPORT   (devc->base)
 48#define	COMDPORT   (devc->base+1)
 49#define	STATPORT   (devc->base+1)
 50
 51static int uart401_status(uart401_devc * devc)
 52{
 53	return inb(STATPORT);
 54}
 55
 56#define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
 57#define output_ready(devc)	(!(uart401_status(devc)&OUTPUT_READY))
 58
 59static void uart401_cmd(uart401_devc * devc, unsigned char cmd)
 60{
 61	outb((cmd), COMDPORT);
 62}
 63
 64static int uart401_read(uart401_devc * devc)
 65{
 66	return inb(DATAPORT);
 67}
 68
 69static void uart401_write(uart401_devc * devc, unsigned char byte)
 70{
 71	outb((byte), DATAPORT);
 72}
 73
 74#define	OUTPUT_READY	0x40
 75#define	INPUT_AVAIL	0x80
 76#define	MPU_ACK		0xFE
 77#define	MPU_RESET	0xFF
 78#define	UART_MODE_ON	0x3F
 79
 80static int      reset_uart401(uart401_devc * devc);
 81static void     enter_uart_mode(uart401_devc * devc);
 82
 83static void uart401_input_loop(uart401_devc * devc)
 84{
 85	int work_limit=30000;
 86	
 87	while (input_avail(devc) && --work_limit)
 88	{
 89		unsigned char   c = uart401_read(devc);
 90
 91		if (c == MPU_ACK)
 92			devc->input_byte = c;
 93		else if (devc->opened & OPEN_READ && devc->midi_input_intr)
 94			devc->midi_input_intr(devc->my_dev, c);
 95	}
 96	if(work_limit==0)
 97		printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc->base);
 98}
 99
100irqreturn_t uart401intr(int irq, void *dev_id)
101{
102	uart401_devc *devc = dev_id;
103
104	if (devc == NULL)
105	{
106		printk(KERN_ERR "uart401: bad devc\n");
107		return IRQ_NONE;
108	}
109
110	if (input_avail(devc))
111		uart401_input_loop(devc);
112	return IRQ_HANDLED;
113}
114
115static int
116uart401_open(int dev, int mode,
117	     void            (*input) (int dev, unsigned char data),
118	     void            (*output) (int dev)
119)
120{
121	uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
 
122
123	if (devc->opened)
124		return -EBUSY;
125
126	/* Flush the UART */
127	
128	while (input_avail(devc))
129		uart401_read(devc);
130
131	devc->midi_input_intr = input;
132	devc->opened = mode;
133	enter_uart_mode(devc);
134	devc->disabled = 0;
135
136	return 0;
137}
138
139static void uart401_close(int dev)
140{
141	uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
 
142
143	reset_uart401(devc);
144	devc->opened = 0;
145}
146
147static int uart401_out(int dev, unsigned char midi_byte)
148{
149	int timeout;
150	unsigned long flags;
151	uart401_devc *devc = (uart401_devc *) midi_devs[dev]->devc;
 
152
153	if (devc->disabled)
154		return 1;
155	/*
156	 * Test for input since pending input seems to block the output.
157	 */
158
159	spin_lock_irqsave(&devc->lock,flags);	
160	if (input_avail(devc))
161		uart401_input_loop(devc);
162
163	spin_unlock_irqrestore(&devc->lock,flags);
164
165	/*
166	 * Sometimes it takes about 13000 loops before the output becomes ready
167	 * (After reset). Normally it takes just about 10 loops.
168	 */
169
170	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
171
172	if (!output_ready(devc))
173	{
174		  printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
175		  devc->disabled = 1;
176		  reset_uart401(devc);
177		  enter_uart_mode(devc);
178		  return 1;
179	}
180	uart401_write(devc, midi_byte);
181	return 1;
182}
183
184static inline int uart401_start_read(int dev)
185{
186	return 0;
187}
188
189static inline int uart401_end_read(int dev)
190{
191	return 0;
192}
193
194static inline void uart401_kick(int dev)
195{
196}
197
198static inline int uart401_buffer_status(int dev)
199{
200	return 0;
201}
202
203#define MIDI_SYNTH_NAME	"MPU-401 UART"
204#define MIDI_SYNTH_CAPS	SYNTH_CAP_INPUT
205#include "midi_synth.h"
206
207static const struct midi_operations uart401_operations =
208{
209	.owner		= THIS_MODULE,
210	.info		= {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
211	.converter	= &std_midi_synth,
212	.in_info	= {0},
213	.open		= uart401_open,
214	.close		= uart401_close,
215	.outputc	= uart401_out,
216	.start_read	= uart401_start_read,
217	.end_read	= uart401_end_read,
218	.kick		= uart401_kick,
219	.buffer_status	= uart401_buffer_status,
220};
221
222static void enter_uart_mode(uart401_devc * devc)
223{
224	int ok, timeout;
225	unsigned long flags;
226
227	spin_lock_irqsave(&devc->lock,flags);	
228	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
229
230	devc->input_byte = 0;
231	uart401_cmd(devc, UART_MODE_ON);
232
233	ok = 0;
234	for (timeout = 50000; timeout > 0 && !ok; timeout--)
235		if (devc->input_byte == MPU_ACK)
236			ok = 1;
237		else if (input_avail(devc))
238			if (uart401_read(devc) == MPU_ACK)
239				ok = 1;
240
241	spin_unlock_irqrestore(&devc->lock,flags);
242}
243
244static int reset_uart401(uart401_devc * devc)
245{
246	int ok, timeout, n;
247
248	/*
249	 * Send the RESET command. Try again if no success at the first time.
250	 */
251
252	ok = 0;
253
254	for (n = 0; n < 2 && !ok; n++)
255	{
256		for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
257		devc->input_byte = 0;
258		uart401_cmd(devc, MPU_RESET);
259
260		/*
261		 * Wait at least 25 msec. This method is not accurate so let's make the
262		 * loop bit longer. Cannot sleep since this is called during boot.
263		 */
264
265		for (timeout = 50000; timeout > 0 && !ok; timeout--)
266		{
267			if (devc->input_byte == MPU_ACK)	/* Interrupt */
268				ok = 1;
269			else if (input_avail(devc))
270			{
271				if (uart401_read(devc) == MPU_ACK)
272					ok = 1;
273			}
274		}
275	}
276
277	/* Flush input before enabling interrupts */
278	if (ok)
279		uart401_input_loop(devc);
280	else
281		DDB(printk("Reset UART401 failed - No hardware detected.\n"));
282
283	return ok;
284}
285
286int probe_uart401(struct address_info *hw_config, struct module *owner)
287{
288	uart401_devc *devc;
289	char *name = "MPU-401 (UART) MIDI";
290	int ok = 0;
291	unsigned long flags;
292
293	DDB(printk("Entered probe_uart401()\n"));
294
295	/* Default to "not found" */
296	hw_config->slots[4] = -1;
297
298	if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
299		printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
300		return 0;
301	}
302
303	devc = kmalloc(sizeof(uart401_devc), GFP_KERNEL);
304	if (!devc) {
305		printk(KERN_WARNING "uart401: Can't allocate memory\n");
306		goto cleanup_region;
307	}
308
309	devc->base = hw_config->io_base;
310	devc->irq = hw_config->irq;
311	devc->osp = hw_config->osp;
312	devc->midi_input_intr = NULL;
313	devc->opened = 0;
314	devc->input_byte = 0;
315	devc->my_dev = 0;
316	devc->share_irq = 0;
317	spin_lock_init(&devc->lock);
318
319	spin_lock_irqsave(&devc->lock,flags);	
320	ok = reset_uart401(devc);
321	spin_unlock_irqrestore(&devc->lock,flags);
322
323	if (!ok)
324		goto cleanup_devc;
325
326	if (hw_config->name)
327		name = hw_config->name;
328
329	if (devc->irq < 0) {
330		devc->share_irq = 1;
331		devc->irq *= -1;
332	} else
333		devc->share_irq = 0;
334
335	if (!devc->share_irq)
336		if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
337			printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
338			devc->share_irq = 1;
339		}
340	devc->my_dev = sound_alloc_mididev();
341	enter_uart_mode(devc);
342
343	if (devc->my_dev == -1) {
344		printk(KERN_INFO "uart401: Too many midi devices detected\n");
345		goto cleanup_irq;
346	}
347	conf_printf(name, hw_config);
348	midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
349					  sizeof(struct midi_operations),
350					  GFP_KERNEL);
351	if (!midi_devs[devc->my_dev]) {
352		printk(KERN_ERR "uart401: Failed to allocate memory\n");
353		goto cleanup_unload_mididev;
354	}
355
356	if (owner)
357		midi_devs[devc->my_dev]->owner = owner;
358	
359	midi_devs[devc->my_dev]->devc = devc;
360	midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
361						     sizeof(struct synth_operations),
362						     GFP_KERNEL);
363
364	if (!midi_devs[devc->my_dev]->converter) {
365		printk(KERN_WARNING "uart401: Failed to allocate memory\n");
366		goto cleanup_midi_devs;
367	}
368	strcpy(midi_devs[devc->my_dev]->info.name, name);
369	midi_devs[devc->my_dev]->converter->id = "UART401";
370	midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
371
372	if (owner)
373		midi_devs[devc->my_dev]->converter->owner = owner;
374
375	hw_config->slots[4] = devc->my_dev;
376	sequencer_init();
377	devc->opened = 0;
378	return 1;
379cleanup_midi_devs:
380	kfree(midi_devs[devc->my_dev]);
381cleanup_unload_mididev:
382	sound_unload_mididev(devc->my_dev);
383cleanup_irq:
384	if (!devc->share_irq)
385		free_irq(devc->irq, devc);
386cleanup_devc:
387	kfree(devc);
388cleanup_region:
389	release_region(hw_config->io_base, 4);
390	return 0;
391}
392
393void unload_uart401(struct address_info *hw_config)
394{
395	uart401_devc *devc;
396	int n=hw_config->slots[4];
397	
398	/* Not set up */
399	if(n==-1 || midi_devs[n]==NULL)
400		return;
401		
402	/* Not allocated (erm ??) */
403	
404	devc = midi_devs[hw_config->slots[4]]->devc;
405	if (devc == NULL)
406		return;
407
408	reset_uart401(devc);
409	release_region(hw_config->io_base, 4);
410
411	if (!devc->share_irq)
412		free_irq(devc->irq, devc);
413	if (devc)
414	{
415		kfree(midi_devs[devc->my_dev]->converter);
416		kfree(midi_devs[devc->my_dev]);
417		kfree(devc);
418		devc = NULL;
419	}
420	/* This kills midi_devs[x] */
421	sound_unload_mididev(hw_config->slots[4]);
422}
423
424EXPORT_SYMBOL(probe_uart401);
425EXPORT_SYMBOL(unload_uart401);
426EXPORT_SYMBOL(uart401intr);
427
428static struct address_info cfg_mpu;
429
430static int io = -1;
431static int irq = -1;
432
433module_param(io, int, 0444);
434module_param(irq, int, 0444);
435
436
437static int __init init_uart401(void)
438{
439	cfg_mpu.irq = irq;
440	cfg_mpu.io_base = io;
441
442	/* Can be loaded either for module use or to provide functions
443	   to others */
444	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
445		printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
446		if (!probe_uart401(&cfg_mpu, THIS_MODULE))
447			return -ENODEV;
448	}
449
450	return 0;
451}
452
453static void __exit cleanup_uart401(void)
454{
455	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
456		unload_uart401(&cfg_mpu);
457}
458
459module_init(init_uart401);
460module_exit(cleanup_uart401);
461
462#ifndef MODULE
463static int __init setup_uart401(char *str)
464{
465	/* io, irq */
466	int ints[3];
467	
468	str = get_options(str, ARRAY_SIZE(ints), ints);
469
470	io = ints[1];
471	irq = ints[2];
472	
473	return 1;
474}
475
476__setup("uart401=", setup_uart401);
477#endif
478MODULE_LICENSE("GPL");
v4.10.11
  1/*
  2 * sound/oss/uart401.c
  3 *
  4 * MPU-401 UART driver (formerly uart401_midi.c)
  5 *
  6 *
  7 * Copyright (C) by Hannu Savolainen 1993-1997
  8 *
  9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
 10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
 11 * for more info.
 12 *
 13 * Changes:
 14 *	Alan Cox		Reformatted, removed sound_mem usage, use normal Linux
 15 *				interrupt allocation. Protect against bogus unload
 16 *				Fixed to allow IRQ > 15
 17 *	Christoph Hellwig	Adapted to module_init/module_exit
 18 *	Arnaldo C. de Melo	got rid of check_region
 19 *
 20 * Status:
 21 *		Untested
 22 */
 23
 24#include <linux/init.h>
 25#include <linux/interrupt.h>
 26#include <linux/module.h>
 27#include <linux/slab.h>
 28#include <linux/spinlock.h>
 29#include "sound_config.h"
 30
 31#include "mpu401.h"
 32
 33struct uart401_devc
 34{
 35	int             base;
 36	int             irq;
 37	int            *osp;
 38	void            (*midi_input_intr) (int dev, unsigned char data);
 39	int             opened, disabled;
 40	volatile unsigned char input_byte;
 41	int             my_dev;
 42	int             share_irq;
 43	spinlock_t	lock;
 44};
 
 45
 46#define	DATAPORT   (devc->base)
 47#define	COMDPORT   (devc->base+1)
 48#define	STATPORT   (devc->base+1)
 49
 50static int uart401_status(struct uart401_devc *devc)
 51{
 52	return inb(STATPORT);
 53}
 54
 55#define input_avail(devc) (!(uart401_status(devc)&INPUT_AVAIL))
 56#define output_ready(devc)	(!(uart401_status(devc)&OUTPUT_READY))
 57
 58static void uart401_cmd(struct uart401_devc *devc, unsigned char cmd)
 59{
 60	outb((cmd), COMDPORT);
 61}
 62
 63static int uart401_read(struct uart401_devc *devc)
 64{
 65	return inb(DATAPORT);
 66}
 67
 68static void uart401_write(struct uart401_devc *devc, unsigned char byte)
 69{
 70	outb((byte), DATAPORT);
 71}
 72
 73#define	OUTPUT_READY	0x40
 74#define	INPUT_AVAIL	0x80
 75#define	MPU_ACK		0xFE
 76#define	MPU_RESET	0xFF
 77#define	UART_MODE_ON	0x3F
 78
 79static int      reset_uart401(struct uart401_devc *devc);
 80static void     enter_uart_mode(struct uart401_devc *devc);
 81
 82static void uart401_input_loop(struct uart401_devc *devc)
 83{
 84	int work_limit=30000;
 85	
 86	while (input_avail(devc) && --work_limit)
 87	{
 88		unsigned char   c = uart401_read(devc);
 89
 90		if (c == MPU_ACK)
 91			devc->input_byte = c;
 92		else if (devc->opened & OPEN_READ && devc->midi_input_intr)
 93			devc->midi_input_intr(devc->my_dev, c);
 94	}
 95	if(work_limit==0)
 96		printk(KERN_WARNING "Too much work in interrupt on uart401 (0x%X). UART jabbering ??\n", devc->base);
 97}
 98
 99irqreturn_t uart401intr(int irq, void *dev_id)
100{
101	struct uart401_devc *devc = dev_id;
102
103	if (devc == NULL)
104	{
105		printk(KERN_ERR "uart401: bad devc\n");
106		return IRQ_NONE;
107	}
108
109	if (input_avail(devc))
110		uart401_input_loop(devc);
111	return IRQ_HANDLED;
112}
113
114static int
115uart401_open(int dev, int mode,
116	     void            (*input) (int dev, unsigned char data),
117	     void            (*output) (int dev)
118)
119{
120	struct uart401_devc *devc = (struct uart401_devc *)
121				    midi_devs[dev]->devc;
122
123	if (devc->opened)
124		return -EBUSY;
125
126	/* Flush the UART */
127	
128	while (input_avail(devc))
129		uart401_read(devc);
130
131	devc->midi_input_intr = input;
132	devc->opened = mode;
133	enter_uart_mode(devc);
134	devc->disabled = 0;
135
136	return 0;
137}
138
139static void uart401_close(int dev)
140{
141	struct uart401_devc *devc = (struct uart401_devc *)
142				    midi_devs[dev]->devc;
143
144	reset_uart401(devc);
145	devc->opened = 0;
146}
147
148static int uart401_out(int dev, unsigned char midi_byte)
149{
150	int timeout;
151	unsigned long flags;
152	struct uart401_devc *devc = (struct uart401_devc *)
153				    midi_devs[dev]->devc;
154
155	if (devc->disabled)
156		return 1;
157	/*
158	 * Test for input since pending input seems to block the output.
159	 */
160
161	spin_lock_irqsave(&devc->lock,flags);	
162	if (input_avail(devc))
163		uart401_input_loop(devc);
164
165	spin_unlock_irqrestore(&devc->lock,flags);
166
167	/*
168	 * Sometimes it takes about 13000 loops before the output becomes ready
169	 * (After reset). Normally it takes just about 10 loops.
170	 */
171
172	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
173
174	if (!output_ready(devc))
175	{
176		  printk(KERN_WARNING "uart401: Timeout - Device not responding\n");
177		  devc->disabled = 1;
178		  reset_uart401(devc);
179		  enter_uart_mode(devc);
180		  return 1;
181	}
182	uart401_write(devc, midi_byte);
183	return 1;
184}
185
186static inline int uart401_start_read(int dev)
187{
188	return 0;
189}
190
191static inline int uart401_end_read(int dev)
192{
193	return 0;
194}
195
196static inline void uart401_kick(int dev)
197{
198}
199
200static inline int uart401_buffer_status(int dev)
201{
202	return 0;
203}
204
205#define MIDI_SYNTH_NAME	"MPU-401 UART"
206#define MIDI_SYNTH_CAPS	SYNTH_CAP_INPUT
207#include "midi_synth.h"
208
209static const struct midi_operations uart401_operations =
210{
211	.owner		= THIS_MODULE,
212	.info		= {"MPU-401 (UART) MIDI", 0, 0, SNDCARD_MPU401},
213	.converter	= &std_midi_synth,
214	.in_info	= {0},
215	.open		= uart401_open,
216	.close		= uart401_close,
217	.outputc	= uart401_out,
218	.start_read	= uart401_start_read,
219	.end_read	= uart401_end_read,
220	.kick		= uart401_kick,
221	.buffer_status	= uart401_buffer_status,
222};
223
224static void enter_uart_mode(struct uart401_devc *devc)
225{
226	int ok, timeout;
227	unsigned long flags;
228
229	spin_lock_irqsave(&devc->lock,flags);	
230	for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
231
232	devc->input_byte = 0;
233	uart401_cmd(devc, UART_MODE_ON);
234
235	ok = 0;
236	for (timeout = 50000; timeout > 0 && !ok; timeout--)
237		if (devc->input_byte == MPU_ACK)
238			ok = 1;
239		else if (input_avail(devc))
240			if (uart401_read(devc) == MPU_ACK)
241				ok = 1;
242
243	spin_unlock_irqrestore(&devc->lock,flags);
244}
245
246static int reset_uart401(struct uart401_devc *devc)
247{
248	int ok, timeout, n;
249
250	/*
251	 * Send the RESET command. Try again if no success at the first time.
252	 */
253
254	ok = 0;
255
256	for (n = 0; n < 2 && !ok; n++)
257	{
258		for (timeout = 30000; timeout > 0 && !output_ready(devc); timeout--);
259		devc->input_byte = 0;
260		uart401_cmd(devc, MPU_RESET);
261
262		/*
263		 * Wait at least 25 msec. This method is not accurate so let's make the
264		 * loop bit longer. Cannot sleep since this is called during boot.
265		 */
266
267		for (timeout = 50000; timeout > 0 && !ok; timeout--)
268		{
269			if (devc->input_byte == MPU_ACK)	/* Interrupt */
270				ok = 1;
271			else if (input_avail(devc))
272			{
273				if (uart401_read(devc) == MPU_ACK)
274					ok = 1;
275			}
276		}
277	}
278
279	/* Flush input before enabling interrupts */
280	if (ok)
281		uart401_input_loop(devc);
282	else
283		DDB(printk("Reset UART401 failed - No hardware detected.\n"));
284
285	return ok;
286}
287
288int probe_uart401(struct address_info *hw_config, struct module *owner)
289{
290	struct uart401_devc *devc;
291	char *name = "MPU-401 (UART) MIDI";
292	int ok = 0;
293	unsigned long flags;
294
295	DDB(printk("Entered probe_uart401()\n"));
296
297	/* Default to "not found" */
298	hw_config->slots[4] = -1;
299
300	if (!request_region(hw_config->io_base, 4, "MPU-401 UART")) {
301		printk(KERN_INFO "uart401: could not request_region(%d, 4)\n", hw_config->io_base);
302		return 0;
303	}
304
305	devc = kmalloc(sizeof(struct uart401_devc), GFP_KERNEL);
306	if (!devc) {
307		printk(KERN_WARNING "uart401: Can't allocate memory\n");
308		goto cleanup_region;
309	}
310
311	devc->base = hw_config->io_base;
312	devc->irq = hw_config->irq;
313	devc->osp = hw_config->osp;
314	devc->midi_input_intr = NULL;
315	devc->opened = 0;
316	devc->input_byte = 0;
317	devc->my_dev = 0;
318	devc->share_irq = 0;
319	spin_lock_init(&devc->lock);
320
321	spin_lock_irqsave(&devc->lock,flags);	
322	ok = reset_uart401(devc);
323	spin_unlock_irqrestore(&devc->lock,flags);
324
325	if (!ok)
326		goto cleanup_devc;
327
328	if (hw_config->name)
329		name = hw_config->name;
330
331	if (devc->irq < 0) {
332		devc->share_irq = 1;
333		devc->irq *= -1;
334	} else
335		devc->share_irq = 0;
336
337	if (!devc->share_irq)
338		if (request_irq(devc->irq, uart401intr, 0, "MPU-401 UART", devc) < 0) {
339			printk(KERN_WARNING "uart401: Failed to allocate IRQ%d\n", devc->irq);
340			devc->share_irq = 1;
341		}
342	devc->my_dev = sound_alloc_mididev();
343	enter_uart_mode(devc);
344
345	if (devc->my_dev == -1) {
346		printk(KERN_INFO "uart401: Too many midi devices detected\n");
347		goto cleanup_irq;
348	}
349	conf_printf(name, hw_config);
350	midi_devs[devc->my_dev] = kmemdup(&uart401_operations,
351					  sizeof(struct midi_operations),
352					  GFP_KERNEL);
353	if (!midi_devs[devc->my_dev]) {
354		printk(KERN_ERR "uart401: Failed to allocate memory\n");
355		goto cleanup_unload_mididev;
356	}
357
358	if (owner)
359		midi_devs[devc->my_dev]->owner = owner;
360	
361	midi_devs[devc->my_dev]->devc = devc;
362	midi_devs[devc->my_dev]->converter = kmemdup(&std_midi_synth,
363						     sizeof(struct synth_operations),
364						     GFP_KERNEL);
365
366	if (!midi_devs[devc->my_dev]->converter) {
367		printk(KERN_WARNING "uart401: Failed to allocate memory\n");
368		goto cleanup_midi_devs;
369	}
370	strcpy(midi_devs[devc->my_dev]->info.name, name);
371	midi_devs[devc->my_dev]->converter->id = "UART401";
372	midi_devs[devc->my_dev]->converter->midi_dev = devc->my_dev;
373
374	if (owner)
375		midi_devs[devc->my_dev]->converter->owner = owner;
376
377	hw_config->slots[4] = devc->my_dev;
378	sequencer_init();
379	devc->opened = 0;
380	return 1;
381cleanup_midi_devs:
382	kfree(midi_devs[devc->my_dev]);
383cleanup_unload_mididev:
384	sound_unload_mididev(devc->my_dev);
385cleanup_irq:
386	if (!devc->share_irq)
387		free_irq(devc->irq, devc);
388cleanup_devc:
389	kfree(devc);
390cleanup_region:
391	release_region(hw_config->io_base, 4);
392	return 0;
393}
394
395void unload_uart401(struct address_info *hw_config)
396{
397	struct uart401_devc *devc;
398	int n=hw_config->slots[4];
399	
400	/* Not set up */
401	if(n==-1 || midi_devs[n]==NULL)
402		return;
403		
404	/* Not allocated (erm ??) */
405	
406	devc = midi_devs[hw_config->slots[4]]->devc;
407	if (devc == NULL)
408		return;
409
410	reset_uart401(devc);
411	release_region(hw_config->io_base, 4);
412
413	if (!devc->share_irq)
414		free_irq(devc->irq, devc);
415	kfree(midi_devs[devc->my_dev]->converter);
416	kfree(midi_devs[devc->my_dev]);
417	kfree(devc);
418
 
 
 
419	/* This kills midi_devs[x] */
420	sound_unload_mididev(hw_config->slots[4]);
421}
422
423EXPORT_SYMBOL(probe_uart401);
424EXPORT_SYMBOL(unload_uart401);
425EXPORT_SYMBOL(uart401intr);
426
427static struct address_info cfg_mpu;
428
429static int io = -1;
430static int irq = -1;
431
432module_param(io, int, 0444);
433module_param(irq, int, 0444);
434
435
436static int __init init_uart401(void)
437{
438	cfg_mpu.irq = irq;
439	cfg_mpu.io_base = io;
440
441	/* Can be loaded either for module use or to provide functions
442	   to others */
443	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1) {
444		printk(KERN_INFO "MPU-401 UART driver Copyright (C) Hannu Savolainen 1993-1997");
445		if (!probe_uart401(&cfg_mpu, THIS_MODULE))
446			return -ENODEV;
447	}
448
449	return 0;
450}
451
452static void __exit cleanup_uart401(void)
453{
454	if (cfg_mpu.io_base != -1 && cfg_mpu.irq != -1)
455		unload_uart401(&cfg_mpu);
456}
457
458module_init(init_uart401);
459module_exit(cleanup_uart401);
460
461#ifndef MODULE
462static int __init setup_uart401(char *str)
463{
464	/* io, irq */
465	int ints[3];
466	
467	str = get_options(str, ARRAY_SIZE(ints), ints);
468
469	io = ints[1];
470	irq = ints[2];
471	
472	return 1;
473}
474
475__setup("uart401=", setup_uart401);
476#endif
477MODULE_LICENSE("GPL");