stmmac: fix a filter problem after resuming.
[linux/fpc-iii.git] / sound / drivers / mts64.c
blob2a008a9ccf85c48f743b7eb7ffd7cece8d76758c
1 /*
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/module.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/initval.h>
30 #include <sound/rawmidi.h>
31 #include <sound/control.h>
33 #define CARD_NAME "Miditerminal 4140"
34 #define DRIVER_NAME "MTS64"
35 #define PLATFORM_DRIVER "snd_mts64"
37 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
38 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
39 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
41 static struct platform_device *platform_devices[SNDRV_CARDS];
42 static int device_count;
44 module_param_array(index, int, NULL, S_IRUGO);
45 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
46 module_param_array(id, charp, NULL, S_IRUGO);
47 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
48 module_param_array(enable, bool, NULL, S_IRUGO);
49 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
51 MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
52 MODULE_DESCRIPTION("ESI Miditerminal 4140");
53 MODULE_LICENSE("GPL");
54 MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
56 /*********************************************************************
57 * Chip specific
58 *********************************************************************/
59 #define MTS64_NUM_INPUT_PORTS 5
60 #define MTS64_NUM_OUTPUT_PORTS 4
61 #define MTS64_SMPTE_SUBSTREAM 4
63 struct mts64 {
64 spinlock_t lock;
65 struct snd_card *card;
66 struct snd_rawmidi *rmidi;
67 struct pardevice *pardev;
68 int pardev_claimed;
70 int open_count;
71 int current_midi_output_port;
72 int current_midi_input_port;
73 u8 mode[MTS64_NUM_INPUT_PORTS];
74 struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
75 int smpte_switch;
76 u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
77 u8 fps;
80 static int snd_mts64_free(struct mts64 *mts)
82 kfree(mts);
83 return 0;
86 static int snd_mts64_create(struct snd_card *card,
87 struct pardevice *pardev,
88 struct mts64 **rchip)
90 struct mts64 *mts;
92 *rchip = NULL;
94 mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
95 if (mts == NULL)
96 return -ENOMEM;
98 /* Init chip specific data */
99 spin_lock_init(&mts->lock);
100 mts->card = card;
101 mts->pardev = pardev;
102 mts->current_midi_output_port = -1;
103 mts->current_midi_input_port = -1;
105 *rchip = mts;
107 return 0;
110 /*********************************************************************
111 * HW register related constants
112 *********************************************************************/
114 /* Status Bits */
115 #define MTS64_STAT_BSY 0x80
116 #define MTS64_STAT_BIT_SET 0x20 /* readout process, bit is set */
117 #define MTS64_STAT_PORT 0x10 /* read byte is a port number */
119 /* Control Bits */
120 #define MTS64_CTL_READOUT 0x08 /* enable readout */
121 #define MTS64_CTL_WRITE_CMD 0x06
122 #define MTS64_CTL_WRITE_DATA 0x02
123 #define MTS64_CTL_STROBE 0x01
125 /* Command */
126 #define MTS64_CMD_RESET 0xfe
127 #define MTS64_CMD_PROBE 0x8f /* Used in probing procedure */
128 #define MTS64_CMD_SMPTE_SET_TIME 0xe8
129 #define MTS64_CMD_SMPTE_SET_FPS 0xee
130 #define MTS64_CMD_SMPTE_STOP 0xef
131 #define MTS64_CMD_SMPTE_FPS_24 0xe3
132 #define MTS64_CMD_SMPTE_FPS_25 0xe2
133 #define MTS64_CMD_SMPTE_FPS_2997 0xe4
134 #define MTS64_CMD_SMPTE_FPS_30D 0xe1
135 #define MTS64_CMD_SMPTE_FPS_30 0xe0
136 #define MTS64_CMD_COM_OPEN 0xf8 /* setting the communication mode */
137 #define MTS64_CMD_COM_CLOSE1 0xff /* clearing communication mode */
138 #define MTS64_CMD_COM_CLOSE2 0xf5
140 /*********************************************************************
141 * Hardware specific functions
142 *********************************************************************/
143 static void mts64_enable_readout(struct parport *p);
144 static void mts64_disable_readout(struct parport *p);
145 static int mts64_device_ready(struct parport *p);
146 static int mts64_device_init(struct parport *p);
147 static int mts64_device_open(struct mts64 *mts);
148 static int mts64_device_close(struct mts64 *mts);
149 static u8 mts64_map_midi_input(u8 c);
150 static int mts64_probe(struct parport *p);
151 static u16 mts64_read(struct parport *p);
152 static u8 mts64_read_char(struct parport *p);
153 static void mts64_smpte_start(struct parport *p,
154 u8 hours, u8 minutes,
155 u8 seconds, u8 frames,
156 u8 idx);
157 static void mts64_smpte_stop(struct parport *p);
158 static void mts64_write_command(struct parport *p, u8 c);
159 static void mts64_write_data(struct parport *p, u8 c);
160 static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
163 /* Enables the readout procedure
165 * Before we can read a midi byte from the device, we have to set
166 * bit 3 of control port.
168 static void mts64_enable_readout(struct parport *p)
170 u8 c;
172 c = parport_read_control(p);
173 c |= MTS64_CTL_READOUT;
174 parport_write_control(p, c);
177 /* Disables readout
179 * Readout is disabled by clearing bit 3 of control
181 static void mts64_disable_readout(struct parport *p)
183 u8 c;
185 c = parport_read_control(p);
186 c &= ~MTS64_CTL_READOUT;
187 parport_write_control(p, c);
190 /* waits for device ready
192 * Checks if BUSY (Bit 7 of status) is clear
193 * 1 device ready
194 * 0 failure
196 static int mts64_device_ready(struct parport *p)
198 int i;
199 u8 c;
201 for (i = 0; i < 0xffff; ++i) {
202 c = parport_read_status(p);
203 c &= MTS64_STAT_BSY;
204 if (c != 0)
205 return 1;
208 return 0;
211 /* Init device (LED blinking startup magic)
213 * Returns:
214 * 0 init ok
215 * -EIO failure
217 static int mts64_device_init(struct parport *p)
219 int i;
221 mts64_write_command(p, MTS64_CMD_RESET);
223 for (i = 0; i < 64; ++i) {
224 msleep(100);
226 if (mts64_probe(p) == 0) {
227 /* success */
228 mts64_disable_readout(p);
229 return 0;
232 mts64_disable_readout(p);
234 return -EIO;
238 * Opens the device (set communication mode)
240 static int mts64_device_open(struct mts64 *mts)
242 int i;
243 struct parport *p = mts->pardev->port;
245 for (i = 0; i < 5; ++i)
246 mts64_write_command(p, MTS64_CMD_COM_OPEN);
248 return 0;
252 * Close device (clear communication mode)
254 static int mts64_device_close(struct mts64 *mts)
256 int i;
257 struct parport *p = mts->pardev->port;
259 for (i = 0; i < 5; ++i) {
260 mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
261 mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
264 return 0;
267 /* map hardware port to substream number
269 * When reading a byte from the device, the device tells us
270 * on what port the byte is. This HW port has to be mapped to
271 * the midiport (substream number).
272 * substream 0-3 are Midiports 1-4
273 * substream 4 is SMPTE Timecode
274 * The mapping is done by the table:
275 * HW | 0 | 1 | 2 | 3 | 4
276 * SW | 0 | 1 | 4 | 2 | 3
278 static u8 mts64_map_midi_input(u8 c)
280 static u8 map[] = { 0, 1, 4, 2, 3 };
282 return map[c];
286 /* Probe parport for device
288 * Do we have a Miditerminal 4140 on parport?
289 * Returns:
290 * 0 device found
291 * -ENODEV no device
293 static int mts64_probe(struct parport *p)
295 u8 c;
297 mts64_smpte_stop(p);
298 mts64_write_command(p, MTS64_CMD_PROBE);
300 msleep(50);
302 c = mts64_read(p);
304 c &= 0x00ff;
305 if (c != MTS64_CMD_PROBE)
306 return -ENODEV;
307 else
308 return 0;
312 /* Read byte incl. status from device
314 * Returns:
315 * data in lower 8 bits and status in upper 8 bits
317 static u16 mts64_read(struct parport *p)
319 u8 data, status;
321 mts64_device_ready(p);
322 mts64_enable_readout(p);
323 status = parport_read_status(p);
324 data = mts64_read_char(p);
325 mts64_disable_readout(p);
327 return (status << 8) | data;
330 /* Read a byte from device
332 * Note, that readout mode has to be enabled.
333 * readout procedure is as follows:
334 * - Write number of the Bit to read to DATA
335 * - Read STATUS
336 * - Bit 5 of STATUS indicates if Bit is set
338 * Returns:
339 * Byte read from device
341 static u8 mts64_read_char(struct parport *p)
343 u8 c = 0;
344 u8 status;
345 u8 i;
347 for (i = 0; i < 8; ++i) {
348 parport_write_data(p, i);
349 c >>= 1;
350 status = parport_read_status(p);
351 if (status & MTS64_STAT_BIT_SET)
352 c |= 0x80;
355 return c;
358 /* Starts SMPTE Timecode generation
360 * The device creates SMPTE Timecode by hardware.
361 * 0 24 fps
362 * 1 25 fps
363 * 2 29.97 fps
364 * 3 30 fps (Drop-frame)
365 * 4 30 fps
367 static void mts64_smpte_start(struct parport *p,
368 u8 hours, u8 minutes,
369 u8 seconds, u8 frames,
370 u8 idx)
372 static u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24,
373 MTS64_CMD_SMPTE_FPS_25,
374 MTS64_CMD_SMPTE_FPS_2997,
375 MTS64_CMD_SMPTE_FPS_30D,
376 MTS64_CMD_SMPTE_FPS_30 };
378 mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
379 mts64_write_command(p, frames);
380 mts64_write_command(p, seconds);
381 mts64_write_command(p, minutes);
382 mts64_write_command(p, hours);
384 mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
385 mts64_write_command(p, fps[idx]);
388 /* Stops SMPTE Timecode generation
390 static void mts64_smpte_stop(struct parport *p)
392 mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
395 /* Write a command byte to device
397 static void mts64_write_command(struct parport *p, u8 c)
399 mts64_device_ready(p);
401 parport_write_data(p, c);
403 parport_write_control(p, MTS64_CTL_WRITE_CMD);
404 parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
405 parport_write_control(p, MTS64_CTL_WRITE_CMD);
408 /* Write a data byte to device
410 static void mts64_write_data(struct parport *p, u8 c)
412 mts64_device_ready(p);
414 parport_write_data(p, c);
416 parport_write_control(p, MTS64_CTL_WRITE_DATA);
417 parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
418 parport_write_control(p, MTS64_CTL_WRITE_DATA);
421 /* Write a MIDI byte to midiport
423 * midiport ranges from 0-3 and maps to Ports 1-4
424 * assumptions: communication mode is on
426 static void mts64_write_midi(struct mts64 *mts, u8 c,
427 int midiport)
429 struct parport *p = mts->pardev->port;
431 /* check current midiport */
432 if (mts->current_midi_output_port != midiport)
433 mts64_write_command(p, midiport);
435 /* write midi byte */
436 mts64_write_data(p, c);
439 /*********************************************************************
440 * Control elements
441 *********************************************************************/
443 /* SMPTE Switch */
444 #define snd_mts64_ctl_smpte_switch_info snd_ctl_boolean_mono_info
446 static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
447 struct snd_ctl_elem_value *uctl)
449 struct mts64 *mts = snd_kcontrol_chip(kctl);
451 spin_lock_irq(&mts->lock);
452 uctl->value.integer.value[0] = mts->smpte_switch;
453 spin_unlock_irq(&mts->lock);
455 return 0;
458 /* smpte_switch is not accessed from IRQ handler, so we just need
459 to protect the HW access */
460 static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
461 struct snd_ctl_elem_value *uctl)
463 struct mts64 *mts = snd_kcontrol_chip(kctl);
464 int changed = 0;
465 int val = !!uctl->value.integer.value[0];
467 spin_lock_irq(&mts->lock);
468 if (mts->smpte_switch == val)
469 goto __out;
471 changed = 1;
472 mts->smpte_switch = val;
473 if (mts->smpte_switch) {
474 mts64_smpte_start(mts->pardev->port,
475 mts->time[0], mts->time[1],
476 mts->time[2], mts->time[3],
477 mts->fps);
478 } else {
479 mts64_smpte_stop(mts->pardev->port);
481 __out:
482 spin_unlock_irq(&mts->lock);
483 return changed;
486 static struct snd_kcontrol_new mts64_ctl_smpte_switch = {
487 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
488 .name = "SMPTE Playback Switch",
489 .index = 0,
490 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
491 .private_value = 0,
492 .info = snd_mts64_ctl_smpte_switch_info,
493 .get = snd_mts64_ctl_smpte_switch_get,
494 .put = snd_mts64_ctl_smpte_switch_put
497 /* Time */
498 static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
499 struct snd_ctl_elem_info *uinfo)
501 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
502 uinfo->count = 1;
503 uinfo->value.integer.min = 0;
504 uinfo->value.integer.max = 23;
505 return 0;
508 static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
509 struct snd_ctl_elem_info *uinfo)
511 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
512 uinfo->count = 1;
513 uinfo->value.integer.min = 0;
514 uinfo->value.integer.max = 99;
515 return 0;
518 static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
519 struct snd_ctl_elem_info *uinfo)
521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
522 uinfo->count = 1;
523 uinfo->value.integer.min = 0;
524 uinfo->value.integer.max = 59;
525 return 0;
528 static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
529 struct snd_ctl_elem_value *uctl)
531 struct mts64 *mts = snd_kcontrol_chip(kctl);
532 int idx = kctl->private_value;
534 spin_lock_irq(&mts->lock);
535 uctl->value.integer.value[0] = mts->time[idx];
536 spin_unlock_irq(&mts->lock);
538 return 0;
541 static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
542 struct snd_ctl_elem_value *uctl)
544 struct mts64 *mts = snd_kcontrol_chip(kctl);
545 int idx = kctl->private_value;
546 unsigned int time = uctl->value.integer.value[0] % 60;
547 int changed = 0;
549 spin_lock_irq(&mts->lock);
550 if (mts->time[idx] != time) {
551 changed = 1;
552 mts->time[idx] = time;
554 spin_unlock_irq(&mts->lock);
556 return changed;
559 static struct snd_kcontrol_new mts64_ctl_smpte_time_hours = {
560 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
561 .name = "SMPTE Time Hours",
562 .index = 0,
563 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
564 .private_value = 0,
565 .info = snd_mts64_ctl_smpte_time_h_info,
566 .get = snd_mts64_ctl_smpte_time_get,
567 .put = snd_mts64_ctl_smpte_time_put
570 static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes = {
571 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
572 .name = "SMPTE Time Minutes",
573 .index = 0,
574 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
575 .private_value = 1,
576 .info = snd_mts64_ctl_smpte_time_info,
577 .get = snd_mts64_ctl_smpte_time_get,
578 .put = snd_mts64_ctl_smpte_time_put
581 static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds = {
582 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
583 .name = "SMPTE Time Seconds",
584 .index = 0,
585 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
586 .private_value = 2,
587 .info = snd_mts64_ctl_smpte_time_info,
588 .get = snd_mts64_ctl_smpte_time_get,
589 .put = snd_mts64_ctl_smpte_time_put
592 static struct snd_kcontrol_new mts64_ctl_smpte_time_frames = {
593 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
594 .name = "SMPTE Time Frames",
595 .index = 0,
596 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
597 .private_value = 3,
598 .info = snd_mts64_ctl_smpte_time_f_info,
599 .get = snd_mts64_ctl_smpte_time_get,
600 .put = snd_mts64_ctl_smpte_time_put
603 /* FPS */
604 static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
605 struct snd_ctl_elem_info *uinfo)
607 static const char * const texts[5] = {
608 "24", "25", "29.97", "30D", "30"
611 return snd_ctl_enum_info(uinfo, 1, 5, texts);
614 static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
615 struct snd_ctl_elem_value *uctl)
617 struct mts64 *mts = snd_kcontrol_chip(kctl);
619 spin_lock_irq(&mts->lock);
620 uctl->value.enumerated.item[0] = mts->fps;
621 spin_unlock_irq(&mts->lock);
623 return 0;
626 static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
627 struct snd_ctl_elem_value *uctl)
629 struct mts64 *mts = snd_kcontrol_chip(kctl);
630 int changed = 0;
632 if (uctl->value.enumerated.item[0] >= 5)
633 return -EINVAL;
634 spin_lock_irq(&mts->lock);
635 if (mts->fps != uctl->value.enumerated.item[0]) {
636 changed = 1;
637 mts->fps = uctl->value.enumerated.item[0];
639 spin_unlock_irq(&mts->lock);
641 return changed;
644 static struct snd_kcontrol_new mts64_ctl_smpte_fps = {
645 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
646 .name = "SMPTE Fps",
647 .index = 0,
648 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
649 .private_value = 0,
650 .info = snd_mts64_ctl_smpte_fps_info,
651 .get = snd_mts64_ctl_smpte_fps_get,
652 .put = snd_mts64_ctl_smpte_fps_put
656 static int snd_mts64_ctl_create(struct snd_card *card,
657 struct mts64 *mts)
659 int err, i;
660 static struct snd_kcontrol_new *control[] = {
661 &mts64_ctl_smpte_switch,
662 &mts64_ctl_smpte_time_hours,
663 &mts64_ctl_smpte_time_minutes,
664 &mts64_ctl_smpte_time_seconds,
665 &mts64_ctl_smpte_time_frames,
666 &mts64_ctl_smpte_fps,
667 NULL };
669 for (i = 0; control[i]; ++i) {
670 err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
671 if (err < 0) {
672 snd_printd("Cannot create control: %s\n",
673 control[i]->name);
674 return err;
678 return 0;
681 /*********************************************************************
682 * Rawmidi
683 *********************************************************************/
684 #define MTS64_MODE_INPUT_TRIGGERED 0x01
686 static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
688 struct mts64 *mts = substream->rmidi->private_data;
690 if (mts->open_count == 0) {
691 /* We don't need a spinlock here, because this is just called
692 if the device has not been opened before.
693 So there aren't any IRQs from the device */
694 mts64_device_open(mts);
696 msleep(50);
698 ++(mts->open_count);
700 return 0;
703 static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
705 struct mts64 *mts = substream->rmidi->private_data;
706 unsigned long flags;
708 --(mts->open_count);
709 if (mts->open_count == 0) {
710 /* We need the spinlock_irqsave here because we can still
711 have IRQs at this point */
712 spin_lock_irqsave(&mts->lock, flags);
713 mts64_device_close(mts);
714 spin_unlock_irqrestore(&mts->lock, flags);
716 msleep(500);
718 } else if (mts->open_count < 0)
719 mts->open_count = 0;
721 return 0;
724 static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
725 int up)
727 struct mts64 *mts = substream->rmidi->private_data;
728 u8 data;
729 unsigned long flags;
731 spin_lock_irqsave(&mts->lock, flags);
732 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
733 mts64_write_midi(mts, data, substream->number+1);
734 snd_rawmidi_transmit_ack(substream, 1);
736 spin_unlock_irqrestore(&mts->lock, flags);
739 static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
740 int up)
742 struct mts64 *mts = substream->rmidi->private_data;
743 unsigned long flags;
745 spin_lock_irqsave(&mts->lock, flags);
746 if (up)
747 mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
748 else
749 mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
751 spin_unlock_irqrestore(&mts->lock, flags);
754 static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
755 .open = snd_mts64_rawmidi_open,
756 .close = snd_mts64_rawmidi_close,
757 .trigger = snd_mts64_rawmidi_output_trigger
760 static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
761 .open = snd_mts64_rawmidi_open,
762 .close = snd_mts64_rawmidi_close,
763 .trigger = snd_mts64_rawmidi_input_trigger
766 /* Create and initialize the rawmidi component */
767 static int snd_mts64_rawmidi_create(struct snd_card *card)
769 struct mts64 *mts = card->private_data;
770 struct snd_rawmidi *rmidi;
771 struct snd_rawmidi_substream *substream;
772 struct list_head *list;
773 int err;
775 err = snd_rawmidi_new(card, CARD_NAME, 0,
776 MTS64_NUM_OUTPUT_PORTS,
777 MTS64_NUM_INPUT_PORTS,
778 &rmidi);
779 if (err < 0)
780 return err;
782 rmidi->private_data = mts;
783 strcpy(rmidi->name, CARD_NAME);
784 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
785 SNDRV_RAWMIDI_INFO_INPUT |
786 SNDRV_RAWMIDI_INFO_DUPLEX;
788 mts->rmidi = rmidi;
790 /* register rawmidi ops */
791 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
792 &snd_mts64_rawmidi_output_ops);
793 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
794 &snd_mts64_rawmidi_input_ops);
796 /* name substreams */
797 /* output */
798 list_for_each(list,
799 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
800 substream = list_entry(list, struct snd_rawmidi_substream, list);
801 sprintf(substream->name,
802 "Miditerminal %d", substream->number+1);
804 /* input */
805 list_for_each(list,
806 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
807 substream = list_entry(list, struct snd_rawmidi_substream, list);
808 mts->midi_input_substream[substream->number] = substream;
809 switch(substream->number) {
810 case MTS64_SMPTE_SUBSTREAM:
811 strcpy(substream->name, "Miditerminal SMPTE");
812 break;
813 default:
814 sprintf(substream->name,
815 "Miditerminal %d", substream->number+1);
819 /* controls */
820 err = snd_mts64_ctl_create(card, mts);
822 return err;
825 /*********************************************************************
826 * parport stuff
827 *********************************************************************/
828 static void snd_mts64_interrupt(void *private)
830 struct mts64 *mts = ((struct snd_card*)private)->private_data;
831 u16 ret;
832 u8 status, data;
833 struct snd_rawmidi_substream *substream;
835 spin_lock(&mts->lock);
836 ret = mts64_read(mts->pardev->port);
837 data = ret & 0x00ff;
838 status = ret >> 8;
840 if (status & MTS64_STAT_PORT) {
841 mts->current_midi_input_port = mts64_map_midi_input(data);
842 } else {
843 if (mts->current_midi_input_port == -1)
844 goto __out;
845 substream = mts->midi_input_substream[mts->current_midi_input_port];
846 if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
847 snd_rawmidi_receive(substream, &data, 1);
849 __out:
850 spin_unlock(&mts->lock);
853 static int snd_mts64_probe_port(struct parport *p)
855 struct pardevice *pardev;
856 int res;
858 pardev = parport_register_device(p, DRIVER_NAME,
859 NULL, NULL, NULL,
860 0, NULL);
861 if (!pardev)
862 return -EIO;
864 if (parport_claim(pardev)) {
865 parport_unregister_device(pardev);
866 return -EIO;
869 res = mts64_probe(p);
871 parport_release(pardev);
872 parport_unregister_device(pardev);
874 return res;
877 static void snd_mts64_attach(struct parport *p)
879 struct platform_device *device;
881 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
882 if (!device)
883 return;
885 /* Temporary assignment to forward the parport */
886 platform_set_drvdata(device, p);
888 if (platform_device_add(device) < 0) {
889 platform_device_put(device);
890 return;
893 /* Since we dont get the return value of probe
894 * We need to check if device probing succeeded or not */
895 if (!platform_get_drvdata(device)) {
896 platform_device_unregister(device);
897 return;
900 /* register device in global table */
901 platform_devices[device_count] = device;
902 device_count++;
905 static void snd_mts64_detach(struct parport *p)
907 /* nothing to do here */
910 static struct parport_driver mts64_parport_driver = {
911 .name = "mts64",
912 .attach = snd_mts64_attach,
913 .detach = snd_mts64_detach
916 /*********************************************************************
917 * platform stuff
918 *********************************************************************/
919 static void snd_mts64_card_private_free(struct snd_card *card)
921 struct mts64 *mts = card->private_data;
922 struct pardevice *pardev = mts->pardev;
924 if (pardev) {
925 if (mts->pardev_claimed)
926 parport_release(pardev);
927 parport_unregister_device(pardev);
930 snd_mts64_free(mts);
933 static int snd_mts64_probe(struct platform_device *pdev)
935 struct pardevice *pardev;
936 struct parport *p;
937 int dev = pdev->id;
938 struct snd_card *card = NULL;
939 struct mts64 *mts = NULL;
940 int err;
942 p = platform_get_drvdata(pdev);
943 platform_set_drvdata(pdev, NULL);
945 if (dev >= SNDRV_CARDS)
946 return -ENODEV;
947 if (!enable[dev])
948 return -ENOENT;
949 if ((err = snd_mts64_probe_port(p)) < 0)
950 return err;
952 err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
953 0, &card);
954 if (err < 0) {
955 snd_printd("Cannot create card\n");
956 return err;
958 strcpy(card->driver, DRIVER_NAME);
959 strcpy(card->shortname, "ESI " CARD_NAME);
960 sprintf(card->longname, "%s at 0x%lx, irq %i",
961 card->shortname, p->base, p->irq);
963 pardev = parport_register_device(p, /* port */
964 DRIVER_NAME, /* name */
965 NULL, /* preempt */
966 NULL, /* wakeup */
967 snd_mts64_interrupt, /* ISR */
968 PARPORT_DEV_EXCL, /* flags */
969 (void *)card); /* private */
970 if (pardev == NULL) {
971 snd_printd("Cannot register pardevice\n");
972 err = -EIO;
973 goto __err;
976 if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
977 snd_printd("Cannot create main component\n");
978 parport_unregister_device(pardev);
979 goto __err;
981 card->private_data = mts;
982 card->private_free = snd_mts64_card_private_free;
984 if ((err = snd_mts64_rawmidi_create(card)) < 0) {
985 snd_printd("Creating Rawmidi component failed\n");
986 goto __err;
989 /* claim parport */
990 if (parport_claim(pardev)) {
991 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
992 err = -EIO;
993 goto __err;
995 mts->pardev_claimed = 1;
997 /* init device */
998 if ((err = mts64_device_init(p)) < 0)
999 goto __err;
1001 platform_set_drvdata(pdev, card);
1003 /* At this point card will be usable */
1004 if ((err = snd_card_register(card)) < 0) {
1005 snd_printd("Cannot register card\n");
1006 goto __err;
1009 snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
1010 return 0;
1012 __err:
1013 snd_card_free(card);
1014 return err;
1017 static int snd_mts64_remove(struct platform_device *pdev)
1019 struct snd_card *card = platform_get_drvdata(pdev);
1021 if (card)
1022 snd_card_free(card);
1024 return 0;
1028 static struct platform_driver snd_mts64_driver = {
1029 .probe = snd_mts64_probe,
1030 .remove = snd_mts64_remove,
1031 .driver = {
1032 .name = PLATFORM_DRIVER,
1036 /*********************************************************************
1037 * module init stuff
1038 *********************************************************************/
1039 static void snd_mts64_unregister_all(void)
1041 int i;
1043 for (i = 0; i < SNDRV_CARDS; ++i) {
1044 if (platform_devices[i]) {
1045 platform_device_unregister(platform_devices[i]);
1046 platform_devices[i] = NULL;
1049 platform_driver_unregister(&snd_mts64_driver);
1050 parport_unregister_driver(&mts64_parport_driver);
1053 static int __init snd_mts64_module_init(void)
1055 int err;
1057 if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1058 return err;
1060 if (parport_register_driver(&mts64_parport_driver) != 0) {
1061 platform_driver_unregister(&snd_mts64_driver);
1062 return -EIO;
1065 if (device_count == 0) {
1066 snd_mts64_unregister_all();
1067 return -ENODEV;
1070 return 0;
1073 static void __exit snd_mts64_module_exit(void)
1075 snd_mts64_unregister_all();
1078 module_init(snd_mts64_module_init);
1079 module_exit(snd_mts64_module_exit);