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