2 * ALSA Driver for Ego Systems Inc. (ESI) Miditerminal 4140
3 * Copyright (c) 2006 by Matthias König <mk@phasorlab.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/parport.h>
24 #include <linux/spinlock.h>
25 #include <linux/delay.h>
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/rawmidi.h>
29 #include <sound/control.h>
31 #define CARD_NAME "Miditerminal 4140"
32 #define DRIVER_NAME "MTS64"
33 #define PLATFORM_DRIVER "snd_mts64"
35 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
36 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
37 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
39 static struct platform_device *platform_devices[SNDRV_CARDS];
40 static int device_count;
42 module_param_array(index, int, NULL, S_IRUGO);
43 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
44 module_param_array(id, charp, NULL, S_IRUGO);
45 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
46 module_param_array(enable, bool, NULL, S_IRUGO);
47 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
49 MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
50 MODULE_DESCRIPTION("ESI Miditerminal 4140");
51 MODULE_LICENSE("GPL");
52 MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
54 /*********************************************************************
56 *********************************************************************/
57 #define MTS64_NUM_INPUT_PORTS 5
58 #define MTS64_NUM_OUTPUT_PORTS 4
59 #define MTS64_SMPTE_SUBSTREAM 4
63 struct snd_card *card;
64 struct snd_rawmidi *rmidi;
65 struct pardevice *pardev;
69 int current_midi_output_port;
70 int current_midi_input_port;
71 u8 mode[MTS64_NUM_INPUT_PORTS];
72 struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
74 u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
78 static int snd_mts64_free(struct mts64 *mts)
84 static int __devinit snd_mts64_create(struct snd_card *card,
85 struct pardevice *pardev,
92 mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
96 /* Init chip specific data */
97 spin_lock_init(&mts->lock);
100 mts->current_midi_output_port = -1;
101 mts->current_midi_input_port = -1;
108 /*********************************************************************
109 * HW register related constants
110 *********************************************************************/
113 #define MTS64_STAT_BSY 0x80
114 #define MTS64_STAT_BIT_SET 0x20 /* readout process, bit is set */
115 #define MTS64_STAT_PORT 0x10 /* read byte is a port number */
118 #define MTS64_CTL_READOUT 0x08 /* enable readout */
119 #define MTS64_CTL_WRITE_CMD 0x06
120 #define MTS64_CTL_WRITE_DATA 0x02
121 #define MTS64_CTL_STROBE 0x01
124 #define MTS64_CMD_RESET 0xfe
125 #define MTS64_CMD_PROBE 0x8f /* Used in probing procedure */
126 #define MTS64_CMD_SMPTE_SET_TIME 0xe8
127 #define MTS64_CMD_SMPTE_SET_FPS 0xee
128 #define MTS64_CMD_SMPTE_STOP 0xef
129 #define MTS64_CMD_SMPTE_FPS_24 0xe3
130 #define MTS64_CMD_SMPTE_FPS_25 0xe2
131 #define MTS64_CMD_SMPTE_FPS_2997 0xe4
132 #define MTS64_CMD_SMPTE_FPS_30D 0xe1
133 #define MTS64_CMD_SMPTE_FPS_30 0xe0
134 #define MTS64_CMD_COM_OPEN 0xf8 /* setting the communication mode */
135 #define MTS64_CMD_COM_CLOSE1 0xff /* clearing communication mode */
136 #define MTS64_CMD_COM_CLOSE2 0xf5
138 /*********************************************************************
139 * Hardware specific functions
140 *********************************************************************/
141 static void mts64_enable_readout(struct parport *p);
142 static void mts64_disable_readout(struct parport *p);
143 static int mts64_device_ready(struct parport *p);
144 static int mts64_device_init(struct parport *p);
145 static int mts64_device_open(struct mts64 *mts);
146 static int mts64_device_close(struct mts64 *mts);
147 static u8 mts64_map_midi_input(u8 c);
148 static int mts64_probe(struct parport *p);
149 static u16 mts64_read(struct parport *p);
150 static u8 mts64_read_char(struct parport *p);
151 static void mts64_smpte_start(struct parport *p,
152 u8 hours, u8 minutes,
153 u8 seconds, u8 frames,
155 static void mts64_smpte_stop(struct parport *p);
156 static void mts64_write_command(struct parport *p, u8 c);
157 static void mts64_write_data(struct parport *p, u8 c);
158 static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
161 /* Enables the readout procedure
163 * Before we can read a midi byte from the device, we have to set
164 * bit 3 of control port.
166 static void mts64_enable_readout(struct parport *p)
170 c = parport_read_control(p);
171 c |= MTS64_CTL_READOUT;
172 parport_write_control(p, c);
177 * Readout is disabled by clearing bit 3 of control
179 static void mts64_disable_readout(struct parport *p)
183 c = parport_read_control(p);
184 c &= ~MTS64_CTL_READOUT;
185 parport_write_control(p, c);
188 /* waits for device ready
190 * Checks if BUSY (Bit 7 of status) is clear
194 static int mts64_device_ready(struct parport *p)
199 for (i = 0; i < 0xffff; ++i) {
200 c = parport_read_status(p);
209 /* Init device (LED blinking startup magic)
215 static int __devinit mts64_device_init(struct parport *p)
219 mts64_write_command(p, MTS64_CMD_RESET);
221 for (i = 0; i < 64; ++i) {
224 if (mts64_probe(p) == 0) {
226 mts64_disable_readout(p);
230 mts64_disable_readout(p);
236 * Opens the device (set communication mode)
238 static int mts64_device_open(struct mts64 *mts)
241 struct parport *p = mts->pardev->port;
243 for (i = 0; i < 5; ++i)
244 mts64_write_command(p, MTS64_CMD_COM_OPEN);
250 * Close device (clear communication mode)
252 static int mts64_device_close(struct mts64 *mts)
255 struct parport *p = mts->pardev->port;
257 for (i = 0; i < 5; ++i) {
258 mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
259 mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
265 /* map hardware port to substream number
267 * When reading a byte from the device, the device tells us
268 * on what port the byte is. This HW port has to be mapped to
269 * the midiport (substream number).
270 * substream 0-3 are Midiports 1-4
271 * substream 4 is SMPTE Timecode
272 * The mapping is done by the table:
273 * HW | 0 | 1 | 2 | 3 | 4
274 * SW | 0 | 1 | 4 | 2 | 3
276 static u8 mts64_map_midi_input(u8 c)
278 static u8 map[] = { 0, 1, 4, 2, 3 };
284 /* Probe parport for device
286 * Do we have a Miditerminal 4140 on parport?
291 static int __devinit mts64_probe(struct parport *p)
296 mts64_write_command(p, MTS64_CMD_PROBE);
303 if (c != MTS64_CMD_PROBE)
310 /* Read byte incl. status from device
313 * data in lower 8 bits and status in upper 8 bits
315 static u16 mts64_read(struct parport *p)
319 mts64_device_ready(p);
320 mts64_enable_readout(p);
321 status = parport_read_status(p);
322 data = mts64_read_char(p);
323 mts64_disable_readout(p);
325 return (status << 8) | data;
328 /* Read a byte from device
330 * Note, that readout mode has to be enabled.
331 * readout procedure is as follows:
332 * - Write number of the Bit to read to DATA
334 * - Bit 5 of STATUS indicates if Bit is set
337 * Byte read from device
339 static u8 mts64_read_char(struct parport *p)
345 for (i = 0; i < 8; ++i) {
346 parport_write_data(p, i);
348 status = parport_read_status(p);
349 if (status & MTS64_STAT_BIT_SET)
356 /* Starts SMPTE Timecode generation
358 * The device creates SMPTE Timecode by hardware.
362 * 3 30 fps (Drop-frame)
365 static void mts64_smpte_start(struct parport *p,
366 u8 hours, u8 minutes,
367 u8 seconds, u8 frames,
370 static u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24,
371 MTS64_CMD_SMPTE_FPS_25,
372 MTS64_CMD_SMPTE_FPS_2997,
373 MTS64_CMD_SMPTE_FPS_30D,
374 MTS64_CMD_SMPTE_FPS_30 };
376 mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
377 mts64_write_command(p, frames);
378 mts64_write_command(p, seconds);
379 mts64_write_command(p, minutes);
380 mts64_write_command(p, hours);
382 mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
383 mts64_write_command(p, fps[idx]);
386 /* Stops SMPTE Timecode generation
388 static void mts64_smpte_stop(struct parport *p)
390 mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
393 /* Write a command byte to device
395 static void mts64_write_command(struct parport *p, u8 c)
397 mts64_device_ready(p);
399 parport_write_data(p, c);
401 parport_write_control(p, MTS64_CTL_WRITE_CMD);
402 parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
403 parport_write_control(p, MTS64_CTL_WRITE_CMD);
406 /* Write a data byte to device
408 static void mts64_write_data(struct parport *p, u8 c)
410 mts64_device_ready(p);
412 parport_write_data(p, c);
414 parport_write_control(p, MTS64_CTL_WRITE_DATA);
415 parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
416 parport_write_control(p, MTS64_CTL_WRITE_DATA);
419 /* Write a MIDI byte to midiport
421 * midiport ranges from 0-3 and maps to Ports 1-4
422 * assumptions: communication mode is on
424 static void mts64_write_midi(struct mts64 *mts, u8 c,
427 struct parport *p = mts->pardev->port;
429 /* check current midiport */
430 if (mts->current_midi_output_port != midiport)
431 mts64_write_command(p, midiport);
433 /* write midi byte */
434 mts64_write_data(p, c);
437 /*********************************************************************
439 *********************************************************************/
442 #define snd_mts64_ctl_smpte_switch_info snd_ctl_boolean_mono_info
444 static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
445 struct snd_ctl_elem_value *uctl)
447 struct mts64 *mts = snd_kcontrol_chip(kctl);
449 spin_lock_irq(&mts->lock);
450 uctl->value.integer.value[0] = mts->smpte_switch;
451 spin_unlock_irq(&mts->lock);
456 /* smpte_switch is not accessed from IRQ handler, so we just need
457 to protect the HW access */
458 static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
459 struct snd_ctl_elem_value *uctl)
461 struct mts64 *mts = snd_kcontrol_chip(kctl);
463 int val = !!uctl->value.integer.value[0];
465 spin_lock_irq(&mts->lock);
466 if (mts->smpte_switch == val)
470 mts->smpte_switch = val;
471 if (mts->smpte_switch) {
472 mts64_smpte_start(mts->pardev->port,
473 mts->time[0], mts->time[1],
474 mts->time[2], mts->time[3],
477 mts64_smpte_stop(mts->pardev->port);
480 spin_unlock_irq(&mts->lock);
484 static struct snd_kcontrol_new mts64_ctl_smpte_switch __devinitdata = {
485 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
486 .name = "SMPTE Playback Switch",
488 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
490 .info = snd_mts64_ctl_smpte_switch_info,
491 .get = snd_mts64_ctl_smpte_switch_get,
492 .put = snd_mts64_ctl_smpte_switch_put
496 static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
497 struct snd_ctl_elem_info *uinfo)
499 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
501 uinfo->value.integer.min = 0;
502 uinfo->value.integer.max = 23;
506 static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
507 struct snd_ctl_elem_info *uinfo)
509 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
511 uinfo->value.integer.min = 0;
512 uinfo->value.integer.max = 99;
516 static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
517 struct snd_ctl_elem_info *uinfo)
519 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
521 uinfo->value.integer.min = 0;
522 uinfo->value.integer.max = 59;
526 static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
527 struct snd_ctl_elem_value *uctl)
529 struct mts64 *mts = snd_kcontrol_chip(kctl);
530 int idx = kctl->private_value;
532 spin_lock_irq(&mts->lock);
533 uctl->value.integer.value[0] = mts->time[idx];
534 spin_unlock_irq(&mts->lock);
539 static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
540 struct snd_ctl_elem_value *uctl)
542 struct mts64 *mts = snd_kcontrol_chip(kctl);
543 int idx = kctl->private_value;
544 unsigned int time = uctl->value.integer.value[0] % 60;
547 spin_lock_irq(&mts->lock);
548 if (mts->time[idx] != time) {
550 mts->time[idx] = time;
552 spin_unlock_irq(&mts->lock);
557 static struct snd_kcontrol_new mts64_ctl_smpte_time_hours __devinitdata = {
558 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
559 .name = "SMPTE Time Hours",
561 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
563 .info = snd_mts64_ctl_smpte_time_h_info,
564 .get = snd_mts64_ctl_smpte_time_get,
565 .put = snd_mts64_ctl_smpte_time_put
568 static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes __devinitdata = {
569 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
570 .name = "SMPTE Time Minutes",
572 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
574 .info = snd_mts64_ctl_smpte_time_info,
575 .get = snd_mts64_ctl_smpte_time_get,
576 .put = snd_mts64_ctl_smpte_time_put
579 static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds __devinitdata = {
580 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
581 .name = "SMPTE Time Seconds",
583 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
585 .info = snd_mts64_ctl_smpte_time_info,
586 .get = snd_mts64_ctl_smpte_time_get,
587 .put = snd_mts64_ctl_smpte_time_put
590 static struct snd_kcontrol_new mts64_ctl_smpte_time_frames __devinitdata = {
591 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
592 .name = "SMPTE Time Frames",
594 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
596 .info = snd_mts64_ctl_smpte_time_f_info,
597 .get = snd_mts64_ctl_smpte_time_get,
598 .put = snd_mts64_ctl_smpte_time_put
602 static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
603 struct snd_ctl_elem_info *uinfo)
605 static char *texts[5] = { "24",
611 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
613 uinfo->value.enumerated.items = 5;
614 if (uinfo->value.enumerated.item > 4)
615 uinfo->value.enumerated.item = 4;
616 strcpy(uinfo->value.enumerated.name,
617 texts[uinfo->value.enumerated.item]);
622 static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
623 struct snd_ctl_elem_value *uctl)
625 struct mts64 *mts = snd_kcontrol_chip(kctl);
627 spin_lock_irq(&mts->lock);
628 uctl->value.enumerated.item[0] = mts->fps;
629 spin_unlock_irq(&mts->lock);
634 static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
635 struct snd_ctl_elem_value *uctl)
637 struct mts64 *mts = snd_kcontrol_chip(kctl);
640 if (uctl->value.enumerated.item[0] >= 5)
642 spin_lock_irq(&mts->lock);
643 if (mts->fps != uctl->value.enumerated.item[0]) {
645 mts->fps = uctl->value.enumerated.item[0];
647 spin_unlock_irq(&mts->lock);
652 static struct snd_kcontrol_new mts64_ctl_smpte_fps __devinitdata = {
653 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
656 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
658 .info = snd_mts64_ctl_smpte_fps_info,
659 .get = snd_mts64_ctl_smpte_fps_get,
660 .put = snd_mts64_ctl_smpte_fps_put
664 static int __devinit snd_mts64_ctl_create(struct snd_card *card,
668 static struct snd_kcontrol_new *control[] __devinitdata = {
669 &mts64_ctl_smpte_switch,
670 &mts64_ctl_smpte_time_hours,
671 &mts64_ctl_smpte_time_minutes,
672 &mts64_ctl_smpte_time_seconds,
673 &mts64_ctl_smpte_time_frames,
674 &mts64_ctl_smpte_fps,
677 for (i = 0; control[i]; ++i) {
678 err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
680 snd_printd("Cannot create control: %s\n",
689 /*********************************************************************
691 *********************************************************************/
692 #define MTS64_MODE_INPUT_TRIGGERED 0x01
694 static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
696 struct mts64 *mts = substream->rmidi->private_data;
698 if (mts->open_count == 0) {
699 /* We don't need a spinlock here, because this is just called
700 if the device has not been opened before.
701 So there aren't any IRQs from the device */
702 mts64_device_open(mts);
711 static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
713 struct mts64 *mts = substream->rmidi->private_data;
717 if (mts->open_count == 0) {
718 /* We need the spinlock_irqsave here because we can still
719 have IRQs at this point */
720 spin_lock_irqsave(&mts->lock, flags);
721 mts64_device_close(mts);
722 spin_unlock_irqrestore(&mts->lock, flags);
726 } else if (mts->open_count < 0)
732 static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
735 struct mts64 *mts = substream->rmidi->private_data;
739 spin_lock_irqsave(&mts->lock, flags);
740 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
741 mts64_write_midi(mts, data, substream->number+1);
742 snd_rawmidi_transmit_ack(substream, 1);
744 spin_unlock_irqrestore(&mts->lock, flags);
747 static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
750 struct mts64 *mts = substream->rmidi->private_data;
753 spin_lock_irqsave(&mts->lock, flags);
755 mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
757 mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
759 spin_unlock_irqrestore(&mts->lock, flags);
762 static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
763 .open = snd_mts64_rawmidi_open,
764 .close = snd_mts64_rawmidi_close,
765 .trigger = snd_mts64_rawmidi_output_trigger
768 static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
769 .open = snd_mts64_rawmidi_open,
770 .close = snd_mts64_rawmidi_close,
771 .trigger = snd_mts64_rawmidi_input_trigger
774 /* Create and initialize the rawmidi component */
775 static int __devinit snd_mts64_rawmidi_create(struct snd_card *card)
777 struct mts64 *mts = card->private_data;
778 struct snd_rawmidi *rmidi;
779 struct snd_rawmidi_substream *substream;
780 struct list_head *list;
783 err = snd_rawmidi_new(card, CARD_NAME, 0,
784 MTS64_NUM_OUTPUT_PORTS,
785 MTS64_NUM_INPUT_PORTS,
790 rmidi->private_data = mts;
791 strcpy(rmidi->name, CARD_NAME);
792 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
793 SNDRV_RAWMIDI_INFO_INPUT |
794 SNDRV_RAWMIDI_INFO_DUPLEX;
798 /* register rawmidi ops */
799 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
800 &snd_mts64_rawmidi_output_ops);
801 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
802 &snd_mts64_rawmidi_input_ops);
804 /* name substreams */
807 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
808 substream = list_entry(list, struct snd_rawmidi_substream, list);
809 sprintf(substream->name,
810 "Miditerminal %d", substream->number+1);
814 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
815 substream = list_entry(list, struct snd_rawmidi_substream, list);
816 mts->midi_input_substream[substream->number] = substream;
817 switch(substream->number) {
818 case MTS64_SMPTE_SUBSTREAM:
819 strcpy(substream->name, "Miditerminal SMPTE");
822 sprintf(substream->name,
823 "Miditerminal %d", substream->number+1);
828 err = snd_mts64_ctl_create(card, mts);
833 /*********************************************************************
835 *********************************************************************/
836 static void snd_mts64_interrupt(void *private)
838 struct mts64 *mts = ((struct snd_card*)private)->private_data;
841 struct snd_rawmidi_substream *substream;
843 spin_lock(&mts->lock);
844 ret = mts64_read(mts->pardev->port);
848 if (status & MTS64_STAT_PORT) {
849 mts->current_midi_input_port = mts64_map_midi_input(data);
851 if (mts->current_midi_input_port == -1)
853 substream = mts->midi_input_substream[mts->current_midi_input_port];
854 if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
855 snd_rawmidi_receive(substream, &data, 1);
858 spin_unlock(&mts->lock);
861 static int __devinit snd_mts64_probe_port(struct parport *p)
863 struct pardevice *pardev;
866 pardev = parport_register_device(p, DRIVER_NAME,
872 if (parport_claim(pardev)) {
873 parport_unregister_device(pardev);
877 res = mts64_probe(p);
879 parport_release(pardev);
880 parport_unregister_device(pardev);
885 static void __devinit snd_mts64_attach(struct parport *p)
887 struct platform_device *device;
889 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
893 /* Temporary assignment to forward the parport */
894 platform_set_drvdata(device, p);
896 if (platform_device_add(device) < 0) {
897 platform_device_put(device);
901 /* Since we dont get the return value of probe
902 * We need to check if device probing succeeded or not */
903 if (!platform_get_drvdata(device)) {
904 platform_device_unregister(device);
908 /* register device in global table */
909 platform_devices[device_count] = device;
913 static void snd_mts64_detach(struct parport *p)
915 /* nothing to do here */
918 static struct parport_driver mts64_parport_driver = {
920 .attach = snd_mts64_attach,
921 .detach = snd_mts64_detach
924 /*********************************************************************
926 *********************************************************************/
927 static void snd_mts64_card_private_free(struct snd_card *card)
929 struct mts64 *mts = card->private_data;
930 struct pardevice *pardev = mts->pardev;
933 if (mts->pardev_claimed)
934 parport_release(pardev);
935 parport_unregister_device(pardev);
941 static int __devinit snd_mts64_probe(struct platform_device *pdev)
943 struct pardevice *pardev;
946 struct snd_card *card = NULL;
947 struct mts64 *mts = NULL;
950 p = platform_get_drvdata(pdev);
951 platform_set_drvdata(pdev, NULL);
953 if (dev >= SNDRV_CARDS)
957 if ((err = snd_mts64_probe_port(p)) < 0)
960 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
962 snd_printd("Cannot create card\n");
965 strcpy(card->driver, DRIVER_NAME);
966 strcpy(card->shortname, "ESI " CARD_NAME);
967 sprintf(card->longname, "%s at 0x%lx, irq %i",
968 card->shortname, p->base, p->irq);
970 pardev = parport_register_device(p, /* port */
971 DRIVER_NAME, /* name */
974 snd_mts64_interrupt, /* ISR */
975 PARPORT_DEV_EXCL, /* flags */
976 (void *)card); /* private */
977 if (pardev == NULL) {
978 snd_printd("Cannot register pardevice\n");
983 if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
984 snd_printd("Cannot create main component\n");
985 parport_unregister_device(pardev);
988 card->private_data = mts;
989 card->private_free = snd_mts64_card_private_free;
991 if ((err = snd_mts64_rawmidi_create(card)) < 0) {
992 snd_printd("Creating Rawmidi component failed\n");
997 if (parport_claim(pardev)) {
998 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
1002 mts->pardev_claimed = 1;
1005 if ((err = mts64_device_init(p)) < 0)
1008 platform_set_drvdata(pdev, card);
1010 snd_card_set_dev(card, &pdev->dev);
1012 /* At this point card will be usable */
1013 if ((err = snd_card_register(card)) < 0) {
1014 snd_printd("Cannot register card\n");
1018 snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
1022 snd_card_free(card);
1026 static int __devexit snd_mts64_remove(struct platform_device *pdev)
1028 struct snd_card *card = platform_get_drvdata(pdev);
1031 snd_card_free(card);
1037 static struct platform_driver snd_mts64_driver = {
1038 .probe = snd_mts64_probe,
1039 .remove = __devexit_p(snd_mts64_remove),
1041 .name = PLATFORM_DRIVER
1045 /*********************************************************************
1047 *********************************************************************/
1048 static void snd_mts64_unregister_all(void)
1052 for (i = 0; i < SNDRV_CARDS; ++i) {
1053 if (platform_devices[i]) {
1054 platform_device_unregister(platform_devices[i]);
1055 platform_devices[i] = NULL;
1058 platform_driver_unregister(&snd_mts64_driver);
1059 parport_unregister_driver(&mts64_parport_driver);
1062 static int __init snd_mts64_module_init(void)
1066 if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1069 if (parport_register_driver(&mts64_parport_driver) != 0) {
1070 platform_driver_unregister(&snd_mts64_driver);
1074 if (device_count == 0) {
1075 snd_mts64_unregister_all();
1082 static void __exit snd_mts64_module_exit(void)
1084 snd_mts64_unregister_all();
1087 module_init(snd_mts64_module_init);
1088 module_exit(snd_mts64_module_exit);