Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *      MOTU Midi Timepiece ALSA Main routines
  4 *      Copyright by Michael T. Mayers (c) Jan 09, 2000
  5 *      mail: michael@tweakoz.com
  6 *      Thanks to John Galbraith
  7 *
  8 *      This driver is for the 'Mark Of The Unicorn' (MOTU)
  9 *      MidiTimePiece AV multiport MIDI interface 
 10 *
 11 *      IOPORTS
 12 *      -------
 13 *      8 MIDI Ins and 8 MIDI outs
 14 *      Video Sync In (BNC), Word Sync Out (BNC), 
 15 *      ADAT Sync Out (DB9)
 16 *      SMPTE in/out (1/4")
 17 *      2 programmable pedal/footswitch inputs and 4 programmable MIDI controller knobs.
 18 *      Macintosh RS422 serial port
 19 *      RS422 "network" port for ganging multiple MTP's
 20 *      PC Parallel Port ( which this driver currently uses )
 21 *
 22 *      MISC FEATURES
 23 *      -------------
 24 *      Hardware MIDI routing, merging, and filtering   
 25 *      MIDI Synchronization to Video, ADAT, SMPTE and other Clock sources
 26 *      128 'scene' memories, recallable from MIDI program change
 27 *
 28 * ChangeLog
 29 * Jun 11 2001	Takashi Iwai <tiwai@suse.de>
 30 *      - Recoded & debugged
 31 *      - Added timer interrupt for midi outputs
 32 *      - hwports is between 1 and 8, which specifies the number of hardware ports.
 33 *        The three global ports, computer, adat and broadcast ports, are created
 34 *        always after h/w and remote ports.
 35 */
 36
 37#include <linux/init.h>
 38#include <linux/interrupt.h>
 39#include <linux/module.h>
 40#include <linux/err.h>
 41#include <linux/platform_device.h>
 42#include <linux/ioport.h>
 43#include <linux/io.h>
 44#include <linux/moduleparam.h>
 45#include <sound/core.h>
 46#include <sound/initval.h>
 47#include <sound/rawmidi.h>
 48#include <linux/delay.h>
 49
 50/*
 51 *      globals
 52 */
 53MODULE_AUTHOR("Michael T. Mayers");
 54MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
 55MODULE_LICENSE("GPL");
 56
 57// io resources
 58#define MTPAV_IOBASE		0x378
 59#define MTPAV_IRQ		7
 60#define MTPAV_MAX_PORTS		8
 61
 62static int index = SNDRV_DEFAULT_IDX1;
 63static char *id = SNDRV_DEFAULT_STR1;
 64static long port = MTPAV_IOBASE;	/* 0x378, 0x278 */
 65static int irq = MTPAV_IRQ;		/* 7, 5 */
 66static int hwports = MTPAV_MAX_PORTS;	/* use hardware ports 1-8 */
 67
 68module_param(index, int, 0444);
 69MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
 70module_param(id, charp, 0444);
 71MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
 72module_param_hw(port, long, ioport, 0444);
 73MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
 74module_param_hw(irq, int, irq, 0444);
 75MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
 76module_param(hwports, int, 0444);
 77MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
 78
 79static struct platform_device *device;
 80
 81/*
 82 *      defines
 83 */
 84//#define USE_FAKE_MTP //       don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
 85
 86// parallel port usage masks
 87#define SIGS_BYTE 0x08
 88#define SIGS_RFD 0x80
 89#define SIGS_IRQ 0x40
 90#define SIGS_IN0 0x10
 91#define SIGS_IN1 0x20
 92
 93#define SIGC_WRITE 0x04
 94#define SIGC_READ 0x08
 95#define SIGC_INTEN 0x10
 96
 97#define DREG 0
 98#define SREG 1
 99#define CREG 2
100
101//
102#define MTPAV_MODE_INPUT_OPENED		0x01
103#define MTPAV_MODE_OUTPUT_OPENED	0x02
104#define MTPAV_MODE_INPUT_TRIGGERED	0x04
105#define MTPAV_MODE_OUTPUT_TRIGGERED	0x08
106
107#define NUMPORTS (0x12+1)
108
109
110/*
111 */
112
113struct mtpav_port {
114	u8 number;
115	u8 hwport;
116	u8 mode;
117	u8 running_status;
118	struct snd_rawmidi_substream *input;
119	struct snd_rawmidi_substream *output;
120};
121
122struct mtpav {
123	struct snd_card *card;
124	unsigned long port;
125	struct resource *res_port;
126	int irq;			/* interrupt (for inputs) */
127	spinlock_t spinlock;
128	int share_irq;			/* number of accesses to input interrupts */
129	int istimer;			/* number of accesses to timer interrupts */
130	struct timer_list timer;	/* timer interrupts for outputs */
131	struct snd_rawmidi *rmidi;
132	int num_ports;		/* number of hw ports (1-8) */
133	struct mtpav_port ports[NUMPORTS];	/* all ports including computer, adat and bc */
134
135	u32 inmidiport;		/* selected input midi port */
136	u32 inmidistate;	/* during midi command 0xf5 */
137
138	u32 outmidihwport;	/* selected output midi hw port */
139};
140
141
142/*
143 * possible hardware ports (selected by 0xf5 port message)
144 *      0x00		all ports
145 *      0x01 .. 0x08    this MTP's ports 1..8
146 *      0x09 .. 0x10    networked MTP's ports (9..16)
147 *      0x11            networked MTP's computer port
148 *      0x63            to ADAT
149 *
150 * mappig:
151 *  subdevice 0 - (X-1)    ports
152 *            X - (2*X-1)  networked ports
153 *            X            computer
154 *            X+1          ADAT
155 *            X+2          all ports
156 *
157 *  where X = chip->num_ports
158 */
159
160#define MTPAV_PIDX_COMPUTER	0
161#define MTPAV_PIDX_ADAT		1
162#define MTPAV_PIDX_BROADCAST	2
163
164
165static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
166{
167	if (subdev < 0)
168		return 0x01; /* invalid - use port 0 as default */
169	else if (subdev < chip->num_ports)
170		return subdev + 1; /* single mtp port */
171	else if (subdev < chip->num_ports * 2)
172		return subdev - chip->num_ports + 0x09; /* remote port */
173	else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
174		return 0x11; /* computer port */
175	else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
176		return 0x63;		/* ADAT */
177	return 0; /* all ports */
178}
179
180static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
181{
182	int p;
183	if (hwport <= 0x00) /* all ports */
184		return chip->num_ports + MTPAV_PIDX_BROADCAST;
185	else if (hwport <= 0x08) { /* single port */
186		p = hwport - 1;
187		if (p >= chip->num_ports)
188			p = 0;
189		return p;
190	} else if (hwport <= 0x10) { /* remote port */
191		p = hwport - 0x09 + chip->num_ports;
192		if (p >= chip->num_ports * 2)
193			p = chip->num_ports;
194		return p;
195	} else if (hwport == 0x11)  /* computer port */
196		return chip->num_ports + MTPAV_PIDX_COMPUTER;
197	else  /* ADAT */
198		return chip->num_ports + MTPAV_PIDX_ADAT;
199}
200
201
202/*
203 */
204
205static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
206{
207	u8 rval = 0;
208
209	if (reg == SREG) {
210		rval = inb(chip->port + SREG);
211		rval = (rval & 0xf8);
212	} else if (reg == CREG) {
213		rval = inb(chip->port + CREG);
214		rval = (rval & 0x1c);
215	}
216
217	return rval;
218}
219
220/*
221 */
222
223static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
224{
225	if (reg == DREG || reg == CREG)
226		outb(val, chip->port + reg);
227}
228
229/*
230 */
231
232static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
233{
234	int counts = 10000;
235	u8 sbyte;
236
237	sbyte = snd_mtpav_getreg(chip, SREG);
238	while (!(sbyte & SIGS_RFD) && counts--) {
239		sbyte = snd_mtpav_getreg(chip, SREG);
240		udelay(10);
241	}
242}
243
244static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
245{
246	u8 tcbyt;
247	u8 clrwrite;
248	u8 setwrite;
249
250	snd_mtpav_wait_rfdhi(chip);
251
252	/////////////////
253
254	tcbyt = snd_mtpav_getreg(chip, CREG);
255	clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
256	setwrite = tcbyt | SIGC_WRITE;
257
258	snd_mtpav_mputreg(chip, DREG, byte);
259	snd_mtpav_mputreg(chip, CREG, clrwrite);	// clear write bit
260
261	snd_mtpav_mputreg(chip, CREG, setwrite);	// set write bit
262
263}
264
265
266/*
267 */
268
269/* call this with spin lock held */
270static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
271					struct mtpav_port *portp,
272					struct snd_rawmidi_substream *substream)
273{
274	u8 outbyte;
275
276	// Get the outbyte first, so we can emulate running status if
277	// necessary
278	if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
279		return;
280
281	// send port change command if necessary
282
283	if (portp->hwport != mtp_card->outmidihwport) {
284		mtp_card->outmidihwport = portp->hwport;
285
286		snd_mtpav_send_byte(mtp_card, 0xf5);
287		snd_mtpav_send_byte(mtp_card, portp->hwport);
288		/*
289		snd_printk(KERN_DEBUG "new outport: 0x%x\n",
290			   (unsigned int) portp->hwport);
291		*/
292		if (!(outbyte & 0x80) && portp->running_status)
293			snd_mtpav_send_byte(mtp_card, portp->running_status);
294	}
295
296	// send data
297
298	do {
299		if (outbyte & 0x80)
300			portp->running_status = outbyte;
301		
302		snd_mtpav_send_byte(mtp_card, outbyte);
303	} while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
304}
305
306static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
307{
308	struct mtpav *mtp_card = substream->rmidi->private_data;
309	struct mtpav_port *portp = &mtp_card->ports[substream->number];
310	unsigned long flags;
311
312	spin_lock_irqsave(&mtp_card->spinlock, flags);
313	snd_mtpav_output_port_write(mtp_card, portp, substream);
314	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
315}
316
317
318/*
319 *      mtpav control
320 */
321
322static void snd_mtpav_portscan(struct mtpav *chip)	// put mtp into smart routing mode
323{
324	u8 p;
325
326	for (p = 0; p < 8; p++) {
327		snd_mtpav_send_byte(chip, 0xf5);
328		snd_mtpav_send_byte(chip, p);
329		snd_mtpav_send_byte(chip, 0xfe);
330	}
331}
332
333/*
334 */
335
336static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
337{
338	struct mtpav *mtp_card = substream->rmidi->private_data;
339	struct mtpav_port *portp = &mtp_card->ports[substream->number];
340	unsigned long flags;
341
342	spin_lock_irqsave(&mtp_card->spinlock, flags);
343	portp->mode |= MTPAV_MODE_INPUT_OPENED;
344	portp->input = substream;
345	if (mtp_card->share_irq++ == 0)
346		snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE));	// enable pport interrupts
347	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
348	return 0;
349}
350
351/*
352 */
353
354static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
355{
356	struct mtpav *mtp_card = substream->rmidi->private_data;
357	struct mtpav_port *portp = &mtp_card->ports[substream->number];
358	unsigned long flags;
359
360	spin_lock_irqsave(&mtp_card->spinlock, flags);
361	portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
362	portp->input = NULL;
363	if (--mtp_card->share_irq == 0)
364		snd_mtpav_mputreg(mtp_card, CREG, 0);	// disable pport interrupts
365	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
366	return 0;
367}
368
369/*
370 */
371
372static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
373{
374	struct mtpav *mtp_card = substream->rmidi->private_data;
375	struct mtpav_port *portp = &mtp_card->ports[substream->number];
376	unsigned long flags;
377
378	spin_lock_irqsave(&mtp_card->spinlock, flags);
379	if (up)
380		portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
381	else
382		portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
383	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
384
385}
386
387
388/*
389 * timer interrupt for outputs
390 */
391
392static void snd_mtpav_output_timer(struct timer_list *t)
393{
394	unsigned long flags;
395	struct mtpav *chip = from_timer(chip, t, timer);
396	int p;
397
398	spin_lock_irqsave(&chip->spinlock, flags);
399	/* reprogram timer */
400	mod_timer(&chip->timer, 1 + jiffies);
401	/* process each port */
402	for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
403		struct mtpav_port *portp = &chip->ports[p];
404		if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
405			snd_mtpav_output_port_write(chip, portp, portp->output);
406	}
407	spin_unlock_irqrestore(&chip->spinlock, flags);
408}
409
410/* spinlock held! */
411static void snd_mtpav_add_output_timer(struct mtpav *chip)
412{
413	mod_timer(&chip->timer, 1 + jiffies);
414}
415
416/* spinlock held! */
417static void snd_mtpav_remove_output_timer(struct mtpav *chip)
418{
419	del_timer(&chip->timer);
420}
421
422/*
423 */
424
425static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
426{
427	struct mtpav *mtp_card = substream->rmidi->private_data;
428	struct mtpav_port *portp = &mtp_card->ports[substream->number];
429	unsigned long flags;
430
431	spin_lock_irqsave(&mtp_card->spinlock, flags);
432	portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
433	portp->output = substream;
434	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
435	return 0;
436};
437
438/*
439 */
440
441static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
442{
443	struct mtpav *mtp_card = substream->rmidi->private_data;
444	struct mtpav_port *portp = &mtp_card->ports[substream->number];
445	unsigned long flags;
446
447	spin_lock_irqsave(&mtp_card->spinlock, flags);
448	portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
449	portp->output = NULL;
450	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
451	return 0;
452};
453
454/*
455 */
456
457static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
458{
459	struct mtpav *mtp_card = substream->rmidi->private_data;
460	struct mtpav_port *portp = &mtp_card->ports[substream->number];
461	unsigned long flags;
462
463	spin_lock_irqsave(&mtp_card->spinlock, flags);
464	if (up) {
465		if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
466			if (mtp_card->istimer++ == 0)
467				snd_mtpav_add_output_timer(mtp_card);
468			portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
469		}
470	} else {
471		portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
472		if (--mtp_card->istimer == 0)
473			snd_mtpav_remove_output_timer(mtp_card);
474	}
475	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
476
477	if (up)
478		snd_mtpav_output_write(substream);
479}
480
481/*
482 * midi interrupt for inputs
483 */
484
485static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
486{
487	struct mtpav_port *portp;
488
489	if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
490		return;
491
492	portp = &mcrd->ports[mcrd->inmidiport];
493	if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
494		snd_rawmidi_receive(portp->input, &inbyte, 1);
495}
496
497static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
498{
499	if (inbyte >= 0xf8) {
500		/* real-time midi code */
501		snd_mtpav_inmidi_process(mcrd, inbyte);
502		return;
503	}
504
505	if (mcrd->inmidistate == 0) {	// awaiting command
506		if (inbyte == 0xf5)	// MTP port #
507			mcrd->inmidistate = 1;
508		else
509			snd_mtpav_inmidi_process(mcrd, inbyte);
510	} else if (mcrd->inmidistate) {
511		mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
512		mcrd->inmidistate = 0;
513	}
514}
515
516static void snd_mtpav_read_bytes(struct mtpav *mcrd)
517{
518	u8 clrread, setread;
519	u8 mtp_read_byte;
520	u8 sr, cbyt;
521	int i;
522
523	u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
524
525	/* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
526
527	if (!(sbyt & SIGS_BYTE))
528		return;
529
530	cbyt = snd_mtpav_getreg(mcrd, CREG);
531	clrread = cbyt & (SIGC_READ ^ 0xff);
532	setread = cbyt | SIGC_READ;
533
534	do {
535
536		mtp_read_byte = 0;
537		for (i = 0; i < 4; i++) {
538			snd_mtpav_mputreg(mcrd, CREG, setread);
539			sr = snd_mtpav_getreg(mcrd, SREG);
540			snd_mtpav_mputreg(mcrd, CREG, clrread);
541
542			sr &= SIGS_IN0 | SIGS_IN1;
543			sr >>= 4;
544			mtp_read_byte |= sr << (i * 2);
545		}
546
547		snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
548
549		sbyt = snd_mtpav_getreg(mcrd, SREG);
550
551	} while (sbyt & SIGS_BYTE);
552}
553
554static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
555{
556	struct mtpav *mcard = dev_id;
557
558	spin_lock(&mcard->spinlock);
559	snd_mtpav_read_bytes(mcard);
560	spin_unlock(&mcard->spinlock);
561	return IRQ_HANDLED;
562}
563
564/*
565 * get ISA resources
566 */
567static int snd_mtpav_get_ISA(struct mtpav *mcard)
568{
569	mcard->res_port = devm_request_region(mcard->card->dev, port, 3,
570					      "MotuMTPAV MIDI");
571	if (!mcard->res_port) {
572		snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
573		return -EBUSY;
574	}
575	mcard->port = port;
576	if (devm_request_irq(mcard->card->dev, irq, snd_mtpav_irqh, 0,
577			     "MOTU MTPAV", mcard)) {
578		snd_printk(KERN_ERR "MTVAP IRQ %d busy\n", irq);
579		return -EBUSY;
580	}
581	mcard->irq = irq;
582	return 0;
583}
584
585
586/*
587 */
588
589static const struct snd_rawmidi_ops snd_mtpav_output = {
590	.open =		snd_mtpav_output_open,
591	.close =	snd_mtpav_output_close,
592	.trigger =	snd_mtpav_output_trigger,
593};
594
595static const struct snd_rawmidi_ops snd_mtpav_input = {
596	.open =		snd_mtpav_input_open,
597	.close =	snd_mtpav_input_close,
598	.trigger =	snd_mtpav_input_trigger,
599};
600
601
602/*
603 * get RAWMIDI resources
604 */
605
606static void snd_mtpav_set_name(struct mtpav *chip,
607			       struct snd_rawmidi_substream *substream)
608{
609	if (substream->number >= 0 && substream->number < chip->num_ports)
610		sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
611	else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
612		sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
613	else if (substream->number == chip->num_ports * 2)
614		strcpy(substream->name, "MTP computer");
615	else if (substream->number == chip->num_ports * 2 + 1)
616		strcpy(substream->name, "MTP ADAT");
617	else
618		strcpy(substream->name, "MTP broadcast");
619}
620
621static int snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
622{
623	int rval;
624	struct snd_rawmidi *rawmidi;
625	struct snd_rawmidi_substream *substream;
626	struct list_head *list;
627
628	if (hwports < 1)
629		hwports = 1;
630	else if (hwports > 8)
631		hwports = 8;
632	mcard->num_ports = hwports;
633
634	rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
635			       mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
636			       mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
637			       &mcard->rmidi);
638	if (rval < 0)
639		return rval;
640	rawmidi = mcard->rmidi;
641	rawmidi->private_data = mcard;
642
643	list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
644		substream = list_entry(list, struct snd_rawmidi_substream, list);
645		snd_mtpav_set_name(mcard, substream);
646		substream->ops = &snd_mtpav_input;
647	}
648	list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
649		substream = list_entry(list, struct snd_rawmidi_substream, list);
650		snd_mtpav_set_name(mcard, substream);
651		substream->ops = &snd_mtpav_output;
652		mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
653	}
654	rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
655			       SNDRV_RAWMIDI_INFO_DUPLEX;
656	sprintf(rawmidi->name, "MTP AV MIDI");
657	return 0;
658}
659
660/*
661 */
662
663static void snd_mtpav_free(struct snd_card *card)
664{
665	struct mtpav *crd = card->private_data;
666	unsigned long flags;
667
668	spin_lock_irqsave(&crd->spinlock, flags);
669	if (crd->istimer > 0)
670		snd_mtpav_remove_output_timer(crd);
671	spin_unlock_irqrestore(&crd->spinlock, flags);
672}
673
674/*
675 */
676static int snd_mtpav_probe(struct platform_device *dev)
677{
678	struct snd_card *card;
679	int err;
680	struct mtpav *mtp_card;
681
682	err = snd_devm_card_new(&dev->dev, index, id, THIS_MODULE,
683				sizeof(*mtp_card), &card);
684	if (err < 0)
685		return err;
686
687	mtp_card = card->private_data;
688	spin_lock_init(&mtp_card->spinlock);
689	mtp_card->card = card;
690	mtp_card->irq = -1;
691	mtp_card->share_irq = 0;
692	mtp_card->inmidistate = 0;
693	mtp_card->outmidihwport = 0xffffffff;
694	timer_setup(&mtp_card->timer, snd_mtpav_output_timer, 0);
695
696	err = snd_mtpav_get_RAWMIDI(mtp_card);
697	if (err < 0)
698		return err;
699
700	mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
701
702	err = snd_mtpav_get_ISA(mtp_card);
703	if (err < 0)
704		return err;
705
706	strcpy(card->driver, "MTPAV");
707	strcpy(card->shortname, "MTPAV on parallel port");
708	snprintf(card->longname, sizeof(card->longname),
709		 "MTPAV on parallel port at 0x%lx", port);
710
711	snd_mtpav_portscan(mtp_card);
712
713	err = snd_card_register(mtp_card->card);
714	if (err < 0)
715		return err;
716
717	card->private_free = snd_mtpav_free;
718
719	platform_set_drvdata(dev, card);
720	printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
721	return 0;
722}
723
724#define SND_MTPAV_DRIVER	"snd_mtpav"
725
726static struct platform_driver snd_mtpav_driver = {
727	.probe		= snd_mtpav_probe,
728	.driver		= {
729		.name	= SND_MTPAV_DRIVER,
730	},
731};
732
733static int __init alsa_card_mtpav_init(void)
734{
735	int err;
736
737	err = platform_driver_register(&snd_mtpav_driver);
738	if (err < 0)
739		return err;
740
741	device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
742	if (!IS_ERR(device)) {
743		if (platform_get_drvdata(device))
744			return 0;
745		platform_device_unregister(device);
746		err = -ENODEV;
747	} else
748		err = PTR_ERR(device);
749	platform_driver_unregister(&snd_mtpav_driver);
750	return err;
751}
752
753static void __exit alsa_card_mtpav_exit(void)
754{
755	platform_device_unregister(device);
756	platform_driver_unregister(&snd_mtpav_driver);
757}
758
759module_init(alsa_card_mtpav_init)
760module_exit(alsa_card_mtpav_exit)
  1/*
  2 *      MOTU Midi Timepiece ALSA Main routines
  3 *      Copyright by Michael T. Mayers (c) Jan 09, 2000
  4 *      mail: michael@tweakoz.com
  5 *      Thanks to John Galbraith
  6 *
  7 *      This program is free software; you can redistribute it and/or modify
  8 *      it under the terms of the GNU General Public License as published by
  9 *      the Free Software Foundation; either version 2 of the License, or
 10 *      (at your option) any later version.
 11 *
 12 *      This program is distributed in the hope that it will be useful,
 13 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *      GNU General Public License for more details.
 16 *
 17 *      You should have received a copy of the GNU General Public License
 18 *      along with this program; if not, write to the Free Software
 19 *      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 20 *
 21 *
 22 *      This driver is for the 'Mark Of The Unicorn' (MOTU)
 23 *      MidiTimePiece AV multiport MIDI interface 
 24 *
 25 *      IOPORTS
 26 *      -------
 27 *      8 MIDI Ins and 8 MIDI outs
 28 *      Video Sync In (BNC), Word Sync Out (BNC), 
 29 *      ADAT Sync Out (DB9)
 30 *      SMPTE in/out (1/4")
 31 *      2 programmable pedal/footswitch inputs and 4 programmable MIDI controller knobs.
 32 *      Macintosh RS422 serial port
 33 *      RS422 "network" port for ganging multiple MTP's
 34 *      PC Parallel Port ( which this driver currently uses )
 35 *
 36 *      MISC FEATURES
 37 *      -------------
 38 *      Hardware MIDI routing, merging, and filtering   
 39 *      MIDI Synchronization to Video, ADAT, SMPTE and other Clock sources
 40 *      128 'scene' memories, recallable from MIDI program change
 41 *
 42 *
 43 * ChangeLog
 44 * Jun 11 2001	Takashi Iwai <tiwai@suse.de>
 45 *      - Recoded & debugged
 46 *      - Added timer interrupt for midi outputs
 47 *      - hwports is between 1 and 8, which specifies the number of hardware ports.
 48 *        The three global ports, computer, adat and broadcast ports, are created
 49 *        always after h/w and remote ports.
 50 *
 51 */
 52
 53#include <linux/init.h>
 54#include <linux/interrupt.h>
 55#include <linux/module.h>
 56#include <linux/err.h>
 57#include <linux/platform_device.h>
 58#include <linux/ioport.h>
 59#include <linux/io.h>
 60#include <linux/moduleparam.h>
 61#include <sound/core.h>
 62#include <sound/initval.h>
 63#include <sound/rawmidi.h>
 64#include <linux/delay.h>
 65
 66/*
 67 *      globals
 68 */
 69MODULE_AUTHOR("Michael T. Mayers");
 70MODULE_DESCRIPTION("MOTU MidiTimePiece AV multiport MIDI");
 71MODULE_LICENSE("GPL");
 72MODULE_SUPPORTED_DEVICE("{{MOTU,MidiTimePiece AV multiport MIDI}}");
 73
 74// io resources
 75#define MTPAV_IOBASE		0x378
 76#define MTPAV_IRQ		7
 77#define MTPAV_MAX_PORTS		8
 78
 79static int index = SNDRV_DEFAULT_IDX1;
 80static char *id = SNDRV_DEFAULT_STR1;
 81static long port = MTPAV_IOBASE;	/* 0x378, 0x278 */
 82static int irq = MTPAV_IRQ;		/* 7, 5 */
 83static int hwports = MTPAV_MAX_PORTS;	/* use hardware ports 1-8 */
 84
 85module_param(index, int, 0444);
 86MODULE_PARM_DESC(index, "Index value for MotuMTPAV MIDI.");
 87module_param(id, charp, 0444);
 88MODULE_PARM_DESC(id, "ID string for MotuMTPAV MIDI.");
 89module_param(port, long, 0444);
 90MODULE_PARM_DESC(port, "Parallel port # for MotuMTPAV MIDI.");
 91module_param(irq, int, 0444);
 92MODULE_PARM_DESC(irq, "Parallel IRQ # for MotuMTPAV MIDI.");
 93module_param(hwports, int, 0444);
 94MODULE_PARM_DESC(hwports, "Hardware ports # for MotuMTPAV MIDI.");
 95
 96static struct platform_device *device;
 97
 98/*
 99 *      defines
100 */
101//#define USE_FAKE_MTP //       don't actually read/write to MTP device (for debugging without an actual unit) (does not work yet)
102
103// parallel port usage masks
104#define SIGS_BYTE 0x08
105#define SIGS_RFD 0x80
106#define SIGS_IRQ 0x40
107#define SIGS_IN0 0x10
108#define SIGS_IN1 0x20
109
110#define SIGC_WRITE 0x04
111#define SIGC_READ 0x08
112#define SIGC_INTEN 0x10
113
114#define DREG 0
115#define SREG 1
116#define CREG 2
117
118//
119#define MTPAV_MODE_INPUT_OPENED		0x01
120#define MTPAV_MODE_OUTPUT_OPENED	0x02
121#define MTPAV_MODE_INPUT_TRIGGERED	0x04
122#define MTPAV_MODE_OUTPUT_TRIGGERED	0x08
123
124#define NUMPORTS (0x12+1)
125
126
127/*
128 */
129
130struct mtpav_port {
131	u8 number;
132	u8 hwport;
133	u8 mode;
134	u8 running_status;
135	struct snd_rawmidi_substream *input;
136	struct snd_rawmidi_substream *output;
137};
138
139struct mtpav {
140	struct snd_card *card;
141	unsigned long port;
142	struct resource *res_port;
143	int irq;			/* interrupt (for inputs) */
144	spinlock_t spinlock;
145	int share_irq;			/* number of accesses to input interrupts */
146	int istimer;			/* number of accesses to timer interrupts */
147	struct timer_list timer;	/* timer interrupts for outputs */
148	struct snd_rawmidi *rmidi;
149	int num_ports;		/* number of hw ports (1-8) */
150	struct mtpav_port ports[NUMPORTS];	/* all ports including computer, adat and bc */
151
152	u32 inmidiport;		/* selected input midi port */
153	u32 inmidistate;	/* during midi command 0xf5 */
154
155	u32 outmidihwport;	/* selected output midi hw port */
156};
157
158
159/*
160 * possible hardware ports (selected by 0xf5 port message)
161 *      0x00		all ports
162 *      0x01 .. 0x08    this MTP's ports 1..8
163 *      0x09 .. 0x10    networked MTP's ports (9..16)
164 *      0x11            networked MTP's computer port
165 *      0x63            to ADAT
166 *
167 * mappig:
168 *  subdevice 0 - (X-1)    ports
169 *            X - (2*X-1)  networked ports
170 *            X            computer
171 *            X+1          ADAT
172 *            X+2          all ports
173 *
174 *  where X = chip->num_ports
175 */
176
177#define MTPAV_PIDX_COMPUTER	0
178#define MTPAV_PIDX_ADAT		1
179#define MTPAV_PIDX_BROADCAST	2
180
181
182static int translate_subdevice_to_hwport(struct mtpav *chip, int subdev)
183{
184	if (subdev < 0)
185		return 0x01; /* invalid - use port 0 as default */
186	else if (subdev < chip->num_ports)
187		return subdev + 1; /* single mtp port */
188	else if (subdev < chip->num_ports * 2)
189		return subdev - chip->num_ports + 0x09; /* remote port */
190	else if (subdev == chip->num_ports * 2 + MTPAV_PIDX_COMPUTER)
191		return 0x11; /* computer port */
192	else if (subdev == chip->num_ports + MTPAV_PIDX_ADAT)
193		return 0x63;		/* ADAT */
194	return 0; /* all ports */
195}
196
197static int translate_hwport_to_subdevice(struct mtpav *chip, int hwport)
198{
199	int p;
200	if (hwport <= 0x00) /* all ports */
201		return chip->num_ports + MTPAV_PIDX_BROADCAST;
202	else if (hwport <= 0x08) { /* single port */
203		p = hwport - 1;
204		if (p >= chip->num_ports)
205			p = 0;
206		return p;
207	} else if (hwport <= 0x10) { /* remote port */
208		p = hwport - 0x09 + chip->num_ports;
209		if (p >= chip->num_ports * 2)
210			p = chip->num_ports;
211		return p;
212	} else if (hwport == 0x11)  /* computer port */
213		return chip->num_ports + MTPAV_PIDX_COMPUTER;
214	else  /* ADAT */
215		return chip->num_ports + MTPAV_PIDX_ADAT;
216}
217
218
219/*
220 */
221
222static u8 snd_mtpav_getreg(struct mtpav *chip, u16 reg)
223{
224	u8 rval = 0;
225
226	if (reg == SREG) {
227		rval = inb(chip->port + SREG);
228		rval = (rval & 0xf8);
229	} else if (reg == CREG) {
230		rval = inb(chip->port + CREG);
231		rval = (rval & 0x1c);
232	}
233
234	return rval;
235}
236
237/*
238 */
239
240static inline void snd_mtpav_mputreg(struct mtpav *chip, u16 reg, u8 val)
241{
242	if (reg == DREG || reg == CREG)
243		outb(val, chip->port + reg);
244}
245
246/*
247 */
248
249static void snd_mtpav_wait_rfdhi(struct mtpav *chip)
250{
251	int counts = 10000;
252	u8 sbyte;
253
254	sbyte = snd_mtpav_getreg(chip, SREG);
255	while (!(sbyte & SIGS_RFD) && counts--) {
256		sbyte = snd_mtpav_getreg(chip, SREG);
257		udelay(10);
258	}
259}
260
261static void snd_mtpav_send_byte(struct mtpav *chip, u8 byte)
262{
263	u8 tcbyt;
264	u8 clrwrite;
265	u8 setwrite;
266
267	snd_mtpav_wait_rfdhi(chip);
268
269	/////////////////
270
271	tcbyt = snd_mtpav_getreg(chip, CREG);
272	clrwrite = tcbyt & (SIGC_WRITE ^ 0xff);
273	setwrite = tcbyt | SIGC_WRITE;
274
275	snd_mtpav_mputreg(chip, DREG, byte);
276	snd_mtpav_mputreg(chip, CREG, clrwrite);	// clear write bit
277
278	snd_mtpav_mputreg(chip, CREG, setwrite);	// set write bit
279
280}
281
282
283/*
284 */
285
286/* call this with spin lock held */
287static void snd_mtpav_output_port_write(struct mtpav *mtp_card,
288					struct mtpav_port *portp,
289					struct snd_rawmidi_substream *substream)
290{
291	u8 outbyte;
292
293	// Get the outbyte first, so we can emulate running status if
294	// necessary
295	if (snd_rawmidi_transmit(substream, &outbyte, 1) != 1)
296		return;
297
298	// send port change command if necessary
299
300	if (portp->hwport != mtp_card->outmidihwport) {
301		mtp_card->outmidihwport = portp->hwport;
302
303		snd_mtpav_send_byte(mtp_card, 0xf5);
304		snd_mtpav_send_byte(mtp_card, portp->hwport);
305		/*
306		snd_printk(KERN_DEBUG "new outport: 0x%x\n",
307			   (unsigned int) portp->hwport);
308		*/
309		if (!(outbyte & 0x80) && portp->running_status)
310			snd_mtpav_send_byte(mtp_card, portp->running_status);
311	}
312
313	// send data
314
315	do {
316		if (outbyte & 0x80)
317			portp->running_status = outbyte;
318		
319		snd_mtpav_send_byte(mtp_card, outbyte);
320	} while (snd_rawmidi_transmit(substream, &outbyte, 1) == 1);
321}
322
323static void snd_mtpav_output_write(struct snd_rawmidi_substream *substream)
324{
325	struct mtpav *mtp_card = substream->rmidi->private_data;
326	struct mtpav_port *portp = &mtp_card->ports[substream->number];
327	unsigned long flags;
328
329	spin_lock_irqsave(&mtp_card->spinlock, flags);
330	snd_mtpav_output_port_write(mtp_card, portp, substream);
331	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
332}
333
334
335/*
336 *      mtpav control
337 */
338
339static void snd_mtpav_portscan(struct mtpav *chip)	// put mtp into smart routing mode
340{
341	u8 p;
342
343	for (p = 0; p < 8; p++) {
344		snd_mtpav_send_byte(chip, 0xf5);
345		snd_mtpav_send_byte(chip, p);
346		snd_mtpav_send_byte(chip, 0xfe);
347	}
348}
349
350/*
351 */
352
353static int snd_mtpav_input_open(struct snd_rawmidi_substream *substream)
354{
355	struct mtpav *mtp_card = substream->rmidi->private_data;
356	struct mtpav_port *portp = &mtp_card->ports[substream->number];
357	unsigned long flags;
358
359	spin_lock_irqsave(&mtp_card->spinlock, flags);
360	portp->mode |= MTPAV_MODE_INPUT_OPENED;
361	portp->input = substream;
362	if (mtp_card->share_irq++ == 0)
363		snd_mtpav_mputreg(mtp_card, CREG, (SIGC_INTEN | SIGC_WRITE));	// enable pport interrupts
364	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
365	return 0;
366}
367
368/*
369 */
370
371static int snd_mtpav_input_close(struct snd_rawmidi_substream *substream)
372{
373	struct mtpav *mtp_card = substream->rmidi->private_data;
374	struct mtpav_port *portp = &mtp_card->ports[substream->number];
375	unsigned long flags;
376
377	spin_lock_irqsave(&mtp_card->spinlock, flags);
378	portp->mode &= ~MTPAV_MODE_INPUT_OPENED;
379	portp->input = NULL;
380	if (--mtp_card->share_irq == 0)
381		snd_mtpav_mputreg(mtp_card, CREG, 0);	// disable pport interrupts
382	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
383	return 0;
384}
385
386/*
387 */
388
389static void snd_mtpav_input_trigger(struct snd_rawmidi_substream *substream, int up)
390{
391	struct mtpav *mtp_card = substream->rmidi->private_data;
392	struct mtpav_port *portp = &mtp_card->ports[substream->number];
393	unsigned long flags;
394
395	spin_lock_irqsave(&mtp_card->spinlock, flags);
396	if (up)
397		portp->mode |= MTPAV_MODE_INPUT_TRIGGERED;
398	else
399		portp->mode &= ~MTPAV_MODE_INPUT_TRIGGERED;
400	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
401
402}
403
404
405/*
406 * timer interrupt for outputs
407 */
408
409static void snd_mtpav_output_timer(unsigned long data)
410{
411	unsigned long flags;
412	struct mtpav *chip = (struct mtpav *)data;
413	int p;
414
415	spin_lock_irqsave(&chip->spinlock, flags);
416	/* reprogram timer */
417	chip->timer.expires = 1 + jiffies;
418	add_timer(&chip->timer);
419	/* process each port */
420	for (p = 0; p <= chip->num_ports * 2 + MTPAV_PIDX_BROADCAST; p++) {
421		struct mtpav_port *portp = &chip->ports[p];
422		if ((portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED) && portp->output)
423			snd_mtpav_output_port_write(chip, portp, portp->output);
424	}
425	spin_unlock_irqrestore(&chip->spinlock, flags);
426}
427
428/* spinlock held! */
429static void snd_mtpav_add_output_timer(struct mtpav *chip)
430{
431	chip->timer.expires = 1 + jiffies;
432	add_timer(&chip->timer);
433}
434
435/* spinlock held! */
436static void snd_mtpav_remove_output_timer(struct mtpav *chip)
437{
438	del_timer(&chip->timer);
439}
440
441/*
442 */
443
444static int snd_mtpav_output_open(struct snd_rawmidi_substream *substream)
445{
446	struct mtpav *mtp_card = substream->rmidi->private_data;
447	struct mtpav_port *portp = &mtp_card->ports[substream->number];
448	unsigned long flags;
449
450	spin_lock_irqsave(&mtp_card->spinlock, flags);
451	portp->mode |= MTPAV_MODE_OUTPUT_OPENED;
452	portp->output = substream;
453	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
454	return 0;
455};
456
457/*
458 */
459
460static int snd_mtpav_output_close(struct snd_rawmidi_substream *substream)
461{
462	struct mtpav *mtp_card = substream->rmidi->private_data;
463	struct mtpav_port *portp = &mtp_card->ports[substream->number];
464	unsigned long flags;
465
466	spin_lock_irqsave(&mtp_card->spinlock, flags);
467	portp->mode &= ~MTPAV_MODE_OUTPUT_OPENED;
468	portp->output = NULL;
469	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
470	return 0;
471};
472
473/*
474 */
475
476static void snd_mtpav_output_trigger(struct snd_rawmidi_substream *substream, int up)
477{
478	struct mtpav *mtp_card = substream->rmidi->private_data;
479	struct mtpav_port *portp = &mtp_card->ports[substream->number];
480	unsigned long flags;
481
482	spin_lock_irqsave(&mtp_card->spinlock, flags);
483	if (up) {
484		if (! (portp->mode & MTPAV_MODE_OUTPUT_TRIGGERED)) {
485			if (mtp_card->istimer++ == 0)
486				snd_mtpav_add_output_timer(mtp_card);
487			portp->mode |= MTPAV_MODE_OUTPUT_TRIGGERED;
488		}
489	} else {
490		portp->mode &= ~MTPAV_MODE_OUTPUT_TRIGGERED;
491		if (--mtp_card->istimer == 0)
492			snd_mtpav_remove_output_timer(mtp_card);
493	}
494	spin_unlock_irqrestore(&mtp_card->spinlock, flags);
495
496	if (up)
497		snd_mtpav_output_write(substream);
498}
499
500/*
501 * midi interrupt for inputs
502 */
503
504static void snd_mtpav_inmidi_process(struct mtpav *mcrd, u8 inbyte)
505{
506	struct mtpav_port *portp;
507
508	if ((int)mcrd->inmidiport > mcrd->num_ports * 2 + MTPAV_PIDX_BROADCAST)
509		return;
510
511	portp = &mcrd->ports[mcrd->inmidiport];
512	if (portp->mode & MTPAV_MODE_INPUT_TRIGGERED)
513		snd_rawmidi_receive(portp->input, &inbyte, 1);
514}
515
516static void snd_mtpav_inmidi_h(struct mtpav *mcrd, u8 inbyte)
517{
518	if (inbyte >= 0xf8) {
519		/* real-time midi code */
520		snd_mtpav_inmidi_process(mcrd, inbyte);
521		return;
522	}
523
524	if (mcrd->inmidistate == 0) {	// awaiting command
525		if (inbyte == 0xf5)	// MTP port #
526			mcrd->inmidistate = 1;
527		else
528			snd_mtpav_inmidi_process(mcrd, inbyte);
529	} else if (mcrd->inmidistate) {
530		mcrd->inmidiport = translate_hwport_to_subdevice(mcrd, inbyte);
531		mcrd->inmidistate = 0;
532	}
533}
534
535static void snd_mtpav_read_bytes(struct mtpav *mcrd)
536{
537	u8 clrread, setread;
538	u8 mtp_read_byte;
539	u8 sr, cbyt;
540	int i;
541
542	u8 sbyt = snd_mtpav_getreg(mcrd, SREG);
543
544	/* printk(KERN_DEBUG "snd_mtpav_read_bytes() sbyt: 0x%x\n", sbyt); */
545
546	if (!(sbyt & SIGS_BYTE))
547		return;
548
549	cbyt = snd_mtpav_getreg(mcrd, CREG);
550	clrread = cbyt & (SIGC_READ ^ 0xff);
551	setread = cbyt | SIGC_READ;
552
553	do {
554
555		mtp_read_byte = 0;
556		for (i = 0; i < 4; i++) {
557			snd_mtpav_mputreg(mcrd, CREG, setread);
558			sr = snd_mtpav_getreg(mcrd, SREG);
559			snd_mtpav_mputreg(mcrd, CREG, clrread);
560
561			sr &= SIGS_IN0 | SIGS_IN1;
562			sr >>= 4;
563			mtp_read_byte |= sr << (i * 2);
564		}
565
566		snd_mtpav_inmidi_h(mcrd, mtp_read_byte);
567
568		sbyt = snd_mtpav_getreg(mcrd, SREG);
569
570	} while (sbyt & SIGS_BYTE);
571}
572
573static irqreturn_t snd_mtpav_irqh(int irq, void *dev_id)
574{
575	struct mtpav *mcard = dev_id;
576
577	spin_lock(&mcard->spinlock);
578	snd_mtpav_read_bytes(mcard);
579	spin_unlock(&mcard->spinlock);
580	return IRQ_HANDLED;
581}
582
583/*
584 * get ISA resources
585 */
586static int snd_mtpav_get_ISA(struct mtpav *mcard)
587{
588	if ((mcard->res_port = request_region(port, 3, "MotuMTPAV MIDI")) == NULL) {
589		snd_printk(KERN_ERR "MTVAP port 0x%lx is busy\n", port);
590		return -EBUSY;
591	}
592	mcard->port = port;
593	if (request_irq(irq, snd_mtpav_irqh, 0, "MOTU MTPAV", mcard)) {
594		snd_printk(KERN_ERR "MTVAP IRQ %d busy\n", irq);
595		return -EBUSY;
596	}
597	mcard->irq = irq;
598	return 0;
599}
600
601
602/*
603 */
604
605static struct snd_rawmidi_ops snd_mtpav_output = {
606	.open =		snd_mtpav_output_open,
607	.close =	snd_mtpav_output_close,
608	.trigger =	snd_mtpav_output_trigger,
609};
610
611static struct snd_rawmidi_ops snd_mtpav_input = {
612	.open =		snd_mtpav_input_open,
613	.close =	snd_mtpav_input_close,
614	.trigger =	snd_mtpav_input_trigger,
615};
616
617
618/*
619 * get RAWMIDI resources
620 */
621
622static void snd_mtpav_set_name(struct mtpav *chip,
623			       struct snd_rawmidi_substream *substream)
624{
625	if (substream->number >= 0 && substream->number < chip->num_ports)
626		sprintf(substream->name, "MTP direct %d", (substream->number % chip->num_ports) + 1);
627	else if (substream->number >= 8 && substream->number < chip->num_ports * 2)
628		sprintf(substream->name, "MTP remote %d", (substream->number % chip->num_ports) + 1);
629	else if (substream->number == chip->num_ports * 2)
630		strcpy(substream->name, "MTP computer");
631	else if (substream->number == chip->num_ports * 2 + 1)
632		strcpy(substream->name, "MTP ADAT");
633	else
634		strcpy(substream->name, "MTP broadcast");
635}
636
637static int snd_mtpav_get_RAWMIDI(struct mtpav *mcard)
638{
639	int rval;
640	struct snd_rawmidi *rawmidi;
641	struct snd_rawmidi_substream *substream;
642	struct list_head *list;
643
644	if (hwports < 1)
645		hwports = 1;
646	else if (hwports > 8)
647		hwports = 8;
648	mcard->num_ports = hwports;
649
650	if ((rval = snd_rawmidi_new(mcard->card, "MotuMIDI", 0,
651				    mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
652				    mcard->num_ports * 2 + MTPAV_PIDX_BROADCAST + 1,
653				    &mcard->rmidi)) < 0)
654		return rval;
655	rawmidi = mcard->rmidi;
656	rawmidi->private_data = mcard;
657
658	list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
659		substream = list_entry(list, struct snd_rawmidi_substream, list);
660		snd_mtpav_set_name(mcard, substream);
661		substream->ops = &snd_mtpav_input;
662	}
663	list_for_each(list, &rawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
664		substream = list_entry(list, struct snd_rawmidi_substream, list);
665		snd_mtpav_set_name(mcard, substream);
666		substream->ops = &snd_mtpav_output;
667		mcard->ports[substream->number].hwport = translate_subdevice_to_hwport(mcard, substream->number);
668	}
669	rawmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
670			       SNDRV_RAWMIDI_INFO_DUPLEX;
671	sprintf(rawmidi->name, "MTP AV MIDI");
672	return 0;
673}
674
675/*
676 */
677
678static void snd_mtpav_free(struct snd_card *card)
679{
680	struct mtpav *crd = card->private_data;
681	unsigned long flags;
682
683	spin_lock_irqsave(&crd->spinlock, flags);
684	if (crd->istimer > 0)
685		snd_mtpav_remove_output_timer(crd);
686	spin_unlock_irqrestore(&crd->spinlock, flags);
687	if (crd->irq >= 0)
688		free_irq(crd->irq, (void *)crd);
689	release_and_free_resource(crd->res_port);
690}
691
692/*
693 */
694static int snd_mtpav_probe(struct platform_device *dev)
695{
696	struct snd_card *card;
697	int err;
698	struct mtpav *mtp_card;
699
700	err = snd_card_new(&dev->dev, index, id, THIS_MODULE,
701			   sizeof(*mtp_card), &card);
702	if (err < 0)
703		return err;
704
705	mtp_card = card->private_data;
706	spin_lock_init(&mtp_card->spinlock);
707	init_timer(&mtp_card->timer);
708	mtp_card->card = card;
709	mtp_card->irq = -1;
710	mtp_card->share_irq = 0;
711	mtp_card->inmidistate = 0;
712	mtp_card->outmidihwport = 0xffffffff;
713	init_timer(&mtp_card->timer);
714	mtp_card->timer.function = snd_mtpav_output_timer;
715	mtp_card->timer.data = (unsigned long) mtp_card;
716
717	card->private_free = snd_mtpav_free;
718
719	err = snd_mtpav_get_RAWMIDI(mtp_card);
720	if (err < 0)
721		goto __error;
722
723	mtp_card->inmidiport = mtp_card->num_ports + MTPAV_PIDX_BROADCAST;
724
725	err = snd_mtpav_get_ISA(mtp_card);
726	if (err < 0)
727		goto __error;
728
729	strcpy(card->driver, "MTPAV");
730	strcpy(card->shortname, "MTPAV on parallel port");
731	snprintf(card->longname, sizeof(card->longname),
732		 "MTPAV on parallel port at 0x%lx", port);
733
734	snd_mtpav_portscan(mtp_card);
735
736	err = snd_card_register(mtp_card->card);
737	if (err < 0)
738		goto __error;
739
740	platform_set_drvdata(dev, card);
741	printk(KERN_INFO "Motu MidiTimePiece on parallel port irq: %d ioport: 0x%lx\n", irq, port);
742	return 0;
743
744 __error:
745	snd_card_free(card);
746	return err;
747}
748
749static int snd_mtpav_remove(struct platform_device *devptr)
750{
751	snd_card_free(platform_get_drvdata(devptr));
752	return 0;
753}
754
755#define SND_MTPAV_DRIVER	"snd_mtpav"
756
757static struct platform_driver snd_mtpav_driver = {
758	.probe		= snd_mtpav_probe,
759	.remove		= snd_mtpav_remove,
760	.driver		= {
761		.name	= SND_MTPAV_DRIVER,
762		.owner	= THIS_MODULE,
763	},
764};
765
766static int __init alsa_card_mtpav_init(void)
767{
768	int err;
769
770	if ((err = platform_driver_register(&snd_mtpav_driver)) < 0)
771		return err;
772
773	device = platform_device_register_simple(SND_MTPAV_DRIVER, -1, NULL, 0);
774	if (!IS_ERR(device)) {
775		if (platform_get_drvdata(device))
776			return 0;
777		platform_device_unregister(device);
778		err = -ENODEV;
779	} else
780		err = PTR_ERR(device);
781	platform_driver_unregister(&snd_mtpav_driver);
782	return err;
783}
784
785static void __exit alsa_card_mtpav_exit(void)
786{
787	platform_device_unregister(device);
788	platform_driver_unregister(&snd_mtpav_driver);
789}
790
791module_init(alsa_card_mtpav_init)
792module_exit(alsa_card_mtpav_exit)