blob: dcc90f995294dc6c960b7a5dfd5a2f64fd329fa1 [file] [log] [blame]
Matthias Koenig68ab8012006-07-27 16:59:23 +02001/*
2 * ALSA Driver for Ego Systems Inc. (ESI) Miditerminal 4140
3 * Copyright (c) 2006 by Matthias König <mk@phasorlab.de>
4 *
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.
9 *
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.
14 *
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
18 *
19 */
20
21#include <sound/driver.h>
22#include <linux/init.h>
23#include <linux/platform_device.h>
24#include <linux/parport.h>
25#include <linux/spinlock.h>
26#include <linux/delay.h>
27#include <sound/core.h>
28#include <sound/initval.h>
29#include <sound/rawmidi.h>
30#include <sound/control.h>
31
32#define CARD_NAME "Miditerminal 4140"
33#define DRIVER_NAME "MTS64"
34#define PLATFORM_DRIVER "snd_mts64"
35
36static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
37static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
38static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
39
40static struct platform_device *platform_devices[SNDRV_CARDS];
41static int device_count;
42
43module_param_array(index, int, NULL, S_IRUGO);
44MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
45module_param_array(id, charp, NULL, S_IRUGO);
46MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
47module_param_array(enable, bool, NULL, S_IRUGO);
48MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
49
50MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
51MODULE_DESCRIPTION("ESI Miditerminal 4140");
52MODULE_LICENSE("GPL");
53MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
54
55/*********************************************************************
56 * Chip specific
57 *********************************************************************/
58#define MTS64_NUM_INPUT_PORTS 5
59#define MTS64_NUM_OUTPUT_PORTS 4
60#define MTS64_SMPTE_SUBSTREAM 4
61
62struct mts64 {
63 spinlock_t lock;
64 struct snd_card *card;
65 struct snd_rawmidi *rmidi;
66 struct pardevice *pardev;
67 int pardev_claimed;
68
69 int open_count;
70 int current_midi_output_port;
71 int current_midi_input_port;
72 u8 mode[MTS64_NUM_INPUT_PORTS];
73 struct snd_rawmidi_substream *midi_input_substream[MTS64_NUM_INPUT_PORTS];
74 int smpte_switch;
75 u8 time[4]; /* [0]=hh, [1]=mm, [2]=ss, [3]=ff */
76 u8 fps;
77};
78
79static int snd_mts64_free(struct mts64 *mts)
80{
81 kfree(mts);
82 return 0;
83}
84
85static int __devinit snd_mts64_create(struct snd_card *card,
86 struct pardevice *pardev,
87 struct mts64 **rchip)
88{
89 struct mts64 *mts;
90
91 *rchip = NULL;
92
93 mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
94 if (mts == NULL)
95 return -ENOMEM;
96
97 /* Init chip specific data */
98 spin_lock_init(&mts->lock);
99 mts->card = card;
100 mts->pardev = pardev;
101 mts->current_midi_output_port = -1;
102 mts->current_midi_input_port = -1;
103
104 *rchip = mts;
105
106 return 0;
107}
108
109/*********************************************************************
110 * HW register related constants
111 *********************************************************************/
112
113/* Status Bits */
114#define MTS64_STAT_BSY 0x80
115#define MTS64_STAT_BIT_SET 0x20 /* readout process, bit is set */
116#define MTS64_STAT_PORT 0x10 /* read byte is a port number */
117
118/* Control Bits */
119#define MTS64_CTL_READOUT 0x08 /* enable readout */
120#define MTS64_CTL_WRITE_CMD 0x06
121#define MTS64_CTL_WRITE_DATA 0x02
122#define MTS64_CTL_STROBE 0x01
123
124/* Command */
125#define MTS64_CMD_RESET 0xfe
126#define MTS64_CMD_PROBE 0x8f /* Used in probing procedure */
127#define MTS64_CMD_SMPTE_SET_TIME 0xe8
128#define MTS64_CMD_SMPTE_SET_FPS 0xee
129#define MTS64_CMD_SMPTE_STOP 0xef
130#define MTS64_CMD_SMPTE_FPS_24 0xe3
131#define MTS64_CMD_SMPTE_FPS_25 0xe2
132#define MTS64_CMD_SMPTE_FPS_2997 0xe4
133#define MTS64_CMD_SMPTE_FPS_30D 0xe1
134#define MTS64_CMD_SMPTE_FPS_30 0xe0
135#define MTS64_CMD_COM_OPEN 0xf8 /* setting the communication mode */
136#define MTS64_CMD_COM_CLOSE1 0xff /* clearing communication mode */
137#define MTS64_CMD_COM_CLOSE2 0xf5
138
139/*********************************************************************
140 * Hardware specific functions
141 *********************************************************************/
142static void mts64_enable_readout(struct parport *p);
143static void mts64_disable_readout(struct parport *p);
144static int mts64_device_ready(struct parport *p);
145static int mts64_device_init(struct parport *p);
146static int mts64_device_open(struct mts64 *mts);
147static int mts64_device_close(struct mts64 *mts);
148static u8 mts64_map_midi_input(u8 c);
149static int mts64_probe(struct parport *p);
150static u16 mts64_read(struct parport *p);
151static u8 mts64_read_char(struct parport *p);
152static void mts64_smpte_start(struct parport *p,
153 u8 hours, u8 minutes,
154 u8 seconds, u8 frames,
155 u8 idx);
156static void mts64_smpte_stop(struct parport *p);
157static void mts64_write_command(struct parport *p, u8 c);
158static void mts64_write_data(struct parport *p, u8 c);
159static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
160
161
162/* Enables the readout procedure
163 *
164 * Before we can read a midi byte from the device, we have to set
165 * bit 3 of control port.
166 */
167static void mts64_enable_readout(struct parport *p)
168{
169 u8 c;
170
171 c = parport_read_control(p);
172 c |= MTS64_CTL_READOUT;
173 parport_write_control(p, c);
174}
175
176/* Disables readout
177 *
178 * Readout is disabled by clearing bit 3 of control
179 */
180static void mts64_disable_readout(struct parport *p)
181{
182 u8 c;
183
184 c = parport_read_control(p);
185 c &= ~MTS64_CTL_READOUT;
186 parport_write_control(p, c);
187}
188
189/* waits for device ready
190 *
191 * Checks if BUSY (Bit 7 of status) is clear
192 * 1 device ready
193 * 0 failure
194 */
195static int mts64_device_ready(struct parport *p)
196{
197 int i;
198 u8 c;
199
200 for (i = 0; i < 0xffff; ++i) {
201 c = parport_read_status(p);
202 c &= MTS64_STAT_BSY;
203 if (c != 0)
204 return 1;
205 }
206
207 return 0;
208}
209
210/* Init device (LED blinking startup magic)
211 *
212 * Returns:
213 * 0 init ok
214 * -EIO failure
215 */
216static int __devinit mts64_device_init(struct parport *p)
217{
218 int i;
219
220 mts64_write_command(p, MTS64_CMD_RESET);
221
222 for (i = 0; i < 64; ++i) {
223 msleep(100);
224
225 if (mts64_probe(p) == 0) {
226 /* success */
227 mts64_disable_readout(p);
228 return 0;
229 }
230 }
231 mts64_disable_readout(p);
232
233 return -EIO;
234}
235
236/*
237 * Opens the device (set communication mode)
238 */
239static int mts64_device_open(struct mts64 *mts)
240{
241 int i;
242 struct parport *p = mts->pardev->port;
243
244 for (i = 0; i < 5; ++i)
245 mts64_write_command(p, MTS64_CMD_COM_OPEN);
246
247 return 0;
248}
249
250/*
251 * Close device (clear communication mode)
252 */
253static int mts64_device_close(struct mts64 *mts)
254{
255 int i;
256 struct parport *p = mts->pardev->port;
257
258 for (i = 0; i < 5; ++i) {
259 mts64_write_command(p, MTS64_CMD_COM_CLOSE1);
260 mts64_write_command(p, MTS64_CMD_COM_CLOSE2);
261 }
262
263 return 0;
264}
265
266/* map hardware port to substream number
267 *
268 * When reading a byte from the device, the device tells us
269 * on what port the byte is. This HW port has to be mapped to
270 * the midiport (substream number).
271 * substream 0-3 are Midiports 1-4
272 * substream 4 is SMPTE Timecode
273 * The mapping is done by the table:
274 * HW | 0 | 1 | 2 | 3 | 4
275 * SW | 0 | 1 | 4 | 2 | 3
276 */
277static u8 mts64_map_midi_input(u8 c)
278{
279 static u8 map[] = { 0, 1, 4, 2, 3 };
280
281 return map[c];
282}
283
284
285/* Probe parport for device
286 *
287 * Do we have a Miditerminal 4140 on parport?
288 * Returns:
289 * 0 device found
290 * -ENODEV no device
291 */
292static int __devinit mts64_probe(struct parport *p)
293{
294 u8 c;
295
296 mts64_smpte_stop(p);
297 mts64_write_command(p, MTS64_CMD_PROBE);
298
299 msleep(50);
300
301 c = mts64_read(p);
302
303 c &= 0x00ff;
304 if (c != MTS64_CMD_PROBE)
305 return -ENODEV;
306 else
307 return 0;
308
309}
310
311/* Read byte incl. status from device
312 *
313 * Returns:
314 * data in lower 8 bits and status in upper 8 bits
315 */
316static u16 mts64_read(struct parport *p)
317{
318 u8 data, status;
319
320 mts64_device_ready(p);
321 mts64_enable_readout(p);
322 status = parport_read_status(p);
323 data = mts64_read_char(p);
324 mts64_disable_readout(p);
325
326 return (status << 8) | data;
327}
328
329/* Read a byte from device
330 *
331 * Note, that readout mode has to be enabled.
332 * readout procedure is as follows:
333 * - Write number of the Bit to read to DATA
334 * - Read STATUS
335 * - Bit 5 of STATUS indicates if Bit is set
336 *
337 * Returns:
338 * Byte read from device
339 */
340static u8 mts64_read_char(struct parport *p)
341{
342 u8 c = 0;
343 u8 status;
344 u8 i;
345
346 for (i = 0; i < 8; ++i) {
347 parport_write_data(p, i);
348 c >>= 1;
349 status = parport_read_status(p);
350 if (status & MTS64_STAT_BIT_SET)
351 c |= 0x80;
352 }
353
354 return c;
355}
356
357/* Starts SMPTE Timecode generation
358 *
359 * The device creates SMPTE Timecode by hardware.
360 * 0 24 fps
361 * 1 25 fps
362 * 2 29.97 fps
363 * 3 30 fps (Drop-frame)
364 * 4 30 fps
365 */
366static void mts64_smpte_start(struct parport *p,
367 u8 hours, u8 minutes,
368 u8 seconds, u8 frames,
369 u8 idx)
370{
371 static u8 fps[5] = { MTS64_CMD_SMPTE_FPS_24,
372 MTS64_CMD_SMPTE_FPS_25,
373 MTS64_CMD_SMPTE_FPS_2997,
374 MTS64_CMD_SMPTE_FPS_30D,
375 MTS64_CMD_SMPTE_FPS_30 };
376
377 mts64_write_command(p, MTS64_CMD_SMPTE_SET_TIME);
378 mts64_write_command(p, frames);
379 mts64_write_command(p, seconds);
380 mts64_write_command(p, minutes);
381 mts64_write_command(p, hours);
382
383 mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
384 mts64_write_command(p, fps[idx]);
385}
386
387/* Stops SMPTE Timecode generation
388 */
389static void mts64_smpte_stop(struct parport *p)
390{
391 mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
392}
393
394/* Write a command byte to device
395 */
396static void mts64_write_command(struct parport *p, u8 c)
397{
398 mts64_device_ready(p);
399
400 parport_write_data(p, c);
401
402 parport_write_control(p, MTS64_CTL_WRITE_CMD);
403 parport_write_control(p, MTS64_CTL_WRITE_CMD | MTS64_CTL_STROBE);
404 parport_write_control(p, MTS64_CTL_WRITE_CMD);
405}
406
407/* Write a data byte to device
408 */
409static void mts64_write_data(struct parport *p, u8 c)
410{
411 mts64_device_ready(p);
412
413 parport_write_data(p, c);
414
415 parport_write_control(p, MTS64_CTL_WRITE_DATA);
416 parport_write_control(p, MTS64_CTL_WRITE_DATA | MTS64_CTL_STROBE);
417 parport_write_control(p, MTS64_CTL_WRITE_DATA);
418}
419
420/* Write a MIDI byte to midiport
421 *
422 * midiport ranges from 0-3 and maps to Ports 1-4
423 * assumptions: communication mode is on
424 */
425static void mts64_write_midi(struct mts64 *mts, u8 c,
426 int midiport)
427{
428 struct parport *p = mts->pardev->port;
429
430 /* check current midiport */
431 if (mts->current_midi_output_port != midiport)
432 mts64_write_command(p, midiport);
433
434 /* write midi byte */
435 mts64_write_data(p, c);
436}
437
438/*********************************************************************
439 * Control elements
440 *********************************************************************/
441
442/* SMPTE Switch */
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200443#define snd_mts64_ctl_smpte_switch_info snd_ctl_boolean_mono_info
Matthias Koenig68ab8012006-07-27 16:59:23 +0200444
445static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
446 struct snd_ctl_elem_value *uctl)
447{
448 struct mts64 *mts = snd_kcontrol_chip(kctl);
449
450 spin_lock_irq(&mts->lock);
451 uctl->value.integer.value[0] = mts->smpte_switch;
452 spin_unlock_irq(&mts->lock);
453
454 return 0;
455}
456
457/* smpte_switch is not accessed from IRQ handler, so we just need
458 to protect the HW access */
459static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
460 struct snd_ctl_elem_value *uctl)
461{
462 struct mts64 *mts = snd_kcontrol_chip(kctl);
463 int changed = 0;
464
465 spin_lock_irq(&mts->lock);
466 if (mts->smpte_switch == uctl->value.integer.value[0])
467 goto __out;
468
469 changed = 1;
470 mts->smpte_switch = uctl->value.integer.value[0];
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],
475 mts->fps);
476 } else {
477 mts64_smpte_stop(mts->pardev->port);
478 }
479__out:
480 spin_unlock_irq(&mts->lock);
481 return changed;
482}
483
484static struct snd_kcontrol_new mts64_ctl_smpte_switch __devinitdata = {
485 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
486 .name = "SMPTE Playback Switch",
487 .index = 0,
488 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
489 .private_value = 0,
490 .info = snd_mts64_ctl_smpte_switch_info,
491 .get = snd_mts64_ctl_smpte_switch_get,
492 .put = snd_mts64_ctl_smpte_switch_put
493};
494
495/* Time */
496static int snd_mts64_ctl_smpte_time_h_info(struct snd_kcontrol *kctl,
497 struct snd_ctl_elem_info *uinfo)
498{
499 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
500 uinfo->count = 1;
501 uinfo->value.integer.min = 0;
502 uinfo->value.integer.max = 23;
503 return 0;
504}
505
506static int snd_mts64_ctl_smpte_time_f_info(struct snd_kcontrol *kctl,
507 struct snd_ctl_elem_info *uinfo)
508{
509 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
510 uinfo->count = 1;
511 uinfo->value.integer.min = 0;
512 uinfo->value.integer.max = 99;
513 return 0;
514}
515
516static int snd_mts64_ctl_smpte_time_info(struct snd_kcontrol *kctl,
517 struct snd_ctl_elem_info *uinfo)
518{
519 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
520 uinfo->count = 1;
521 uinfo->value.integer.min = 0;
522 uinfo->value.integer.max = 59;
523 return 0;
524}
525
526static int snd_mts64_ctl_smpte_time_get(struct snd_kcontrol *kctl,
527 struct snd_ctl_elem_value *uctl)
528{
529 struct mts64 *mts = snd_kcontrol_chip(kctl);
530 int idx = kctl->private_value;
531
532 spin_lock_irq(&mts->lock);
533 uctl->value.integer.value[0] = mts->time[idx];
534 spin_unlock_irq(&mts->lock);
535
536 return 0;
537}
538
539static int snd_mts64_ctl_smpte_time_put(struct snd_kcontrol *kctl,
540 struct snd_ctl_elem_value *uctl)
541{
542 struct mts64 *mts = snd_kcontrol_chip(kctl);
543 int idx = kctl->private_value;
544 int changed = 0;
545
546 spin_lock_irq(&mts->lock);
547 if (mts->time[idx] != uctl->value.integer.value[0]) {
548 changed = 1;
549 mts->time[idx] = uctl->value.integer.value[0];
550 }
551 spin_unlock_irq(&mts->lock);
552
553 return changed;
554}
555
556static struct snd_kcontrol_new mts64_ctl_smpte_time_hours __devinitdata = {
557 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
558 .name = "SMPTE Time Hours",
559 .index = 0,
560 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
561 .private_value = 0,
562 .info = snd_mts64_ctl_smpte_time_h_info,
563 .get = snd_mts64_ctl_smpte_time_get,
564 .put = snd_mts64_ctl_smpte_time_put
565};
566
567static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes __devinitdata = {
568 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
569 .name = "SMPTE Time Minutes",
570 .index = 0,
571 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
572 .private_value = 1,
573 .info = snd_mts64_ctl_smpte_time_info,
574 .get = snd_mts64_ctl_smpte_time_get,
575 .put = snd_mts64_ctl_smpte_time_put
576};
577
578static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds __devinitdata = {
579 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
580 .name = "SMPTE Time Seconds",
581 .index = 0,
582 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
583 .private_value = 2,
584 .info = snd_mts64_ctl_smpte_time_info,
585 .get = snd_mts64_ctl_smpte_time_get,
586 .put = snd_mts64_ctl_smpte_time_put
587};
588
589static struct snd_kcontrol_new mts64_ctl_smpte_time_frames __devinitdata = {
590 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
591 .name = "SMPTE Time Frames",
592 .index = 0,
593 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
594 .private_value = 3,
595 .info = snd_mts64_ctl_smpte_time_f_info,
596 .get = snd_mts64_ctl_smpte_time_get,
597 .put = snd_mts64_ctl_smpte_time_put
598};
599
600/* FPS */
601static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
602 struct snd_ctl_elem_info *uinfo)
603{
604 static char *texts[5] = { "24",
605 "25",
606 "29.97",
607 "30D",
608 "30" };
609
610 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
611 uinfo->count = 1;
612 uinfo->value.enumerated.items = 5;
613 if (uinfo->value.enumerated.item > 4)
614 uinfo->value.enumerated.item = 4;
615 strcpy(uinfo->value.enumerated.name,
616 texts[uinfo->value.enumerated.item]);
617
618 return 0;
619}
620
621static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
622 struct snd_ctl_elem_value *uctl)
623{
624 struct mts64 *mts = snd_kcontrol_chip(kctl);
625
626 spin_lock_irq(&mts->lock);
627 uctl->value.enumerated.item[0] = mts->fps;
628 spin_unlock_irq(&mts->lock);
629
630 return 0;
631}
632
633static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
634 struct snd_ctl_elem_value *uctl)
635{
636 struct mts64 *mts = snd_kcontrol_chip(kctl);
637 int changed = 0;
638
639 spin_lock_irq(&mts->lock);
640 if (mts->fps != uctl->value.enumerated.item[0]) {
641 changed = 1;
642 mts->fps = uctl->value.enumerated.item[0];
643 }
644 spin_unlock_irq(&mts->lock);
645
646 return changed;
647}
648
649static struct snd_kcontrol_new mts64_ctl_smpte_fps __devinitdata = {
650 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
651 .name = "SMPTE Fps",
652 .index = 0,
653 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
654 .private_value = 0,
655 .info = snd_mts64_ctl_smpte_fps_info,
656 .get = snd_mts64_ctl_smpte_fps_get,
657 .put = snd_mts64_ctl_smpte_fps_put
658};
659
660
661static int __devinit snd_mts64_ctl_create(struct snd_card *card,
662 struct mts64 *mts)
663{
664 int err, i;
665 static struct snd_kcontrol_new *control[] = {
666 &mts64_ctl_smpte_switch,
667 &mts64_ctl_smpte_time_hours,
668 &mts64_ctl_smpte_time_minutes,
669 &mts64_ctl_smpte_time_seconds,
670 &mts64_ctl_smpte_time_frames,
671 &mts64_ctl_smpte_fps,
Al Viroae97dd92006-09-24 23:42:57 +0100672 NULL };
Matthias Koenig68ab8012006-07-27 16:59:23 +0200673
674 for (i = 0; control[i]; ++i) {
675 err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
676 if (err < 0) {
677 snd_printd("Cannot create control: %s\n",
678 control[i]->name);
679 return err;
680 }
681 }
682
683 return 0;
684}
685
686/*********************************************************************
687 * Rawmidi
688 *********************************************************************/
689#define MTS64_MODE_INPUT_TRIGGERED 0x01
690
691static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
692{
693 struct mts64 *mts = substream->rmidi->private_data;
694
695 if (mts->open_count == 0) {
696 /* We don't need a spinlock here, because this is just called
697 if the device has not been opened before.
698 So there aren't any IRQs from the device */
699 mts64_device_open(mts);
700
701 msleep(50);
702 }
703 ++(mts->open_count);
704
705 return 0;
706}
707
708static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
709{
710 struct mts64 *mts = substream->rmidi->private_data;
711 unsigned long flags;
712
713 --(mts->open_count);
714 if (mts->open_count == 0) {
715 /* We need the spinlock_irqsave here because we can still
716 have IRQs at this point */
717 spin_lock_irqsave(&mts->lock, flags);
718 mts64_device_close(mts);
719 spin_unlock_irqrestore(&mts->lock, flags);
720
721 msleep(500);
722
723 } else if (mts->open_count < 0)
724 mts->open_count = 0;
725
726 return 0;
727}
728
729static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
730 int up)
731{
732 struct mts64 *mts = substream->rmidi->private_data;
733 u8 data;
734 unsigned long flags;
735
736 spin_lock_irqsave(&mts->lock, flags);
737 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
738 mts64_write_midi(mts, data, substream->number+1);
739 snd_rawmidi_transmit_ack(substream, 1);
740 }
741 spin_unlock_irqrestore(&mts->lock, flags);
742}
743
744static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
745 int up)
746{
747 struct mts64 *mts = substream->rmidi->private_data;
748 unsigned long flags;
749
750 spin_lock_irqsave(&mts->lock, flags);
751 if (up)
752 mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
753 else
754 mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
755
756 spin_unlock_irqrestore(&mts->lock, flags);
757}
758
759static struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
760 .open = snd_mts64_rawmidi_open,
761 .close = snd_mts64_rawmidi_close,
762 .trigger = snd_mts64_rawmidi_output_trigger
763};
764
765static struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
766 .open = snd_mts64_rawmidi_open,
767 .close = snd_mts64_rawmidi_close,
768 .trigger = snd_mts64_rawmidi_input_trigger
769};
770
771/* Create and initialize the rawmidi component */
772static int __devinit snd_mts64_rawmidi_create(struct snd_card *card)
773{
774 struct mts64 *mts = card->private_data;
775 struct snd_rawmidi *rmidi;
776 struct snd_rawmidi_substream *substream;
777 struct list_head *list;
778 int err;
779
780 err = snd_rawmidi_new(card, CARD_NAME, 0,
781 MTS64_NUM_OUTPUT_PORTS,
782 MTS64_NUM_INPUT_PORTS,
783 &rmidi);
784 if (err < 0)
785 return err;
786
787 rmidi->private_data = mts;
788 strcpy(rmidi->name, CARD_NAME);
789 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
790 SNDRV_RAWMIDI_INFO_INPUT |
791 SNDRV_RAWMIDI_INFO_DUPLEX;
792
793 mts->rmidi = rmidi;
794
795 /* register rawmidi ops */
796 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
797 &snd_mts64_rawmidi_output_ops);
798 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
799 &snd_mts64_rawmidi_input_ops);
800
801 /* name substreams */
802 /* output */
803 list_for_each(list,
804 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
805 substream = list_entry(list, struct snd_rawmidi_substream, list);
806 sprintf(substream->name,
807 "Miditerminal %d", substream->number+1);
808 }
809 /* input */
810 list_for_each(list,
811 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
812 substream = list_entry(list, struct snd_rawmidi_substream, list);
813 mts->midi_input_substream[substream->number] = substream;
814 switch(substream->number) {
815 case MTS64_SMPTE_SUBSTREAM:
816 strcpy(substream->name, "Miditerminal SMPTE");
817 break;
818 default:
819 sprintf(substream->name,
820 "Miditerminal %d", substream->number+1);
821 }
822 }
823
824 /* controls */
825 err = snd_mts64_ctl_create(card, mts);
826
827 return err;
828}
829
830/*********************************************************************
831 * parport stuff
832 *********************************************************************/
Jeff Garzik5712cb32007-10-19 02:54:26 -0400833static void snd_mts64_interrupt(void *private)
Matthias Koenig68ab8012006-07-27 16:59:23 +0200834{
835 struct mts64 *mts = ((struct snd_card*)private)->private_data;
836 u16 ret;
837 u8 status, data;
838 struct snd_rawmidi_substream *substream;
839
840 spin_lock(&mts->lock);
841 ret = mts64_read(mts->pardev->port);
842 data = ret & 0x00ff;
843 status = ret >> 8;
844
845 if (status & MTS64_STAT_PORT) {
846 mts->current_midi_input_port = mts64_map_midi_input(data);
847 } else {
848 if (mts->current_midi_input_port == -1)
849 goto __out;
850 substream = mts->midi_input_substream[mts->current_midi_input_port];
851 if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
852 snd_rawmidi_receive(substream, &data, 1);
853 }
854__out:
855 spin_unlock(&mts->lock);
856}
857
858static int __devinit snd_mts64_probe_port(struct parport *p)
859{
860 struct pardevice *pardev;
861 int res;
862
863 pardev = parport_register_device(p, DRIVER_NAME,
864 NULL, NULL, NULL,
865 0, NULL);
866 if (!pardev)
867 return -EIO;
868
869 if (parport_claim(pardev)) {
870 parport_unregister_device(pardev);
871 return -EIO;
872 }
873
874 res = mts64_probe(p);
875
876 parport_release(pardev);
877 parport_unregister_device(pardev);
878
879 return res;
880}
881
882static void __devinit snd_mts64_attach(struct parport *p)
883{
884 struct platform_device *device;
885
886 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
Akinobu Mita479ef432007-04-23 11:54:41 +0200887 if (!device)
Matthias Koenig68ab8012006-07-27 16:59:23 +0200888 return;
889
890 /* Temporary assignment to forward the parport */
891 platform_set_drvdata(device, p);
892
Akinobu Mita479ef432007-04-23 11:54:41 +0200893 if (platform_device_add(device) < 0) {
Matthias Koenig68ab8012006-07-27 16:59:23 +0200894 platform_device_put(device);
895 return;
896 }
897
898 /* Since we dont get the return value of probe
899 * We need to check if device probing succeeded or not */
900 if (!platform_get_drvdata(device)) {
901 platform_device_unregister(device);
902 return;
903 }
904
905 /* register device in global table */
906 platform_devices[device_count] = device;
907 device_count++;
908}
909
910static void snd_mts64_detach(struct parport *p)
911{
912 /* nothing to do here */
913}
914
915static struct parport_driver mts64_parport_driver = {
916 .name = "mts64",
917 .attach = snd_mts64_attach,
918 .detach = snd_mts64_detach
919};
920
921/*********************************************************************
922 * platform stuff
923 *********************************************************************/
924static void snd_mts64_card_private_free(struct snd_card *card)
925{
926 struct mts64 *mts = card->private_data;
927 struct pardevice *pardev = mts->pardev;
928
929 if (pardev) {
930 if (mts->pardev_claimed)
931 parport_release(pardev);
932 parport_unregister_device(pardev);
933 }
934
935 snd_mts64_free(mts);
936}
937
938static int __devinit snd_mts64_probe(struct platform_device *pdev)
939{
940 struct pardevice *pardev;
941 struct parport *p;
942 int dev = pdev->id;
943 struct snd_card *card = NULL;
944 struct mts64 *mts = NULL;
945 int err;
946
947 p = platform_get_drvdata(pdev);
948 platform_set_drvdata(pdev, NULL);
949
950 if (dev >= SNDRV_CARDS)
951 return -ENODEV;
952 if (!enable[dev])
953 return -ENOENT;
954 if ((err = snd_mts64_probe_port(p)) < 0)
955 return err;
956
957 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
958 if (card == NULL) {
959 snd_printd("Cannot create card\n");
960 return -ENOMEM;
961 }
962 strcpy(card->driver, DRIVER_NAME);
963 strcpy(card->shortname, "ESI " CARD_NAME);
964 sprintf(card->longname, "%s at 0x%lx, irq %i",
965 card->shortname, p->base, p->irq);
966
967 pardev = parport_register_device(p, /* port */
968 DRIVER_NAME, /* name */
969 NULL, /* preempt */
970 NULL, /* wakeup */
971 snd_mts64_interrupt, /* ISR */
972 PARPORT_DEV_EXCL, /* flags */
973 (void *)card); /* private */
974 if (pardev == NULL) {
975 snd_printd("Cannot register pardevice\n");
976 err = -EIO;
977 goto __err;
978 }
979
980 if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
981 snd_printd("Cannot create main component\n");
982 parport_unregister_device(pardev);
983 goto __err;
984 }
985 card->private_data = mts;
986 card->private_free = snd_mts64_card_private_free;
987
988 if ((err = snd_mts64_rawmidi_create(card)) < 0) {
989 snd_printd("Creating Rawmidi component failed\n");
990 goto __err;
991 }
992
993 /* claim parport */
994 if (parport_claim(pardev)) {
995 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
996 err = -EIO;
997 goto __err;
998 }
999 mts->pardev_claimed = 1;
1000
1001 /* init device */
1002 if ((err = mts64_device_init(p)) < 0)
1003 goto __err;
1004
1005 platform_set_drvdata(pdev, card);
1006
1007 /* At this point card will be usable */
1008 if ((err = snd_card_register(card)) < 0) {
1009 snd_printd("Cannot register card\n");
1010 goto __err;
1011 }
1012
1013 snd_printk("ESI Miditerminal 4140 on 0x%lx\n", p->base);
1014 return 0;
1015
1016__err:
1017 snd_card_free(card);
1018 return err;
1019}
1020
Prarit Bhargava788c6042007-02-13 13:11:11 +01001021static int __devexit snd_mts64_remove(struct platform_device *pdev)
Matthias Koenig68ab8012006-07-27 16:59:23 +02001022{
1023 struct snd_card *card = platform_get_drvdata(pdev);
1024
1025 if (card)
1026 snd_card_free(card);
1027
1028 return 0;
1029}
1030
1031
1032static struct platform_driver snd_mts64_driver = {
1033 .probe = snd_mts64_probe,
Prarit Bhargava788c6042007-02-13 13:11:11 +01001034 .remove = __devexit_p(snd_mts64_remove),
Matthias Koenig68ab8012006-07-27 16:59:23 +02001035 .driver = {
1036 .name = PLATFORM_DRIVER
1037 }
1038};
1039
1040/*********************************************************************
1041 * module init stuff
1042 *********************************************************************/
Randy Dunlap10c86be2007-07-01 12:06:37 -07001043static void snd_mts64_unregister_all(void)
Matthias Koenig68ab8012006-07-27 16:59:23 +02001044{
1045 int i;
1046
1047 for (i = 0; i < SNDRV_CARDS; ++i) {
1048 if (platform_devices[i]) {
1049 platform_device_unregister(platform_devices[i]);
1050 platform_devices[i] = NULL;
1051 }
1052 }
1053 platform_driver_unregister(&snd_mts64_driver);
1054 parport_unregister_driver(&mts64_parport_driver);
1055}
1056
1057static int __init snd_mts64_module_init(void)
1058{
1059 int err;
1060
1061 if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1062 return err;
1063
1064 if (parport_register_driver(&mts64_parport_driver) != 0) {
1065 platform_driver_unregister(&snd_mts64_driver);
1066 return -EIO;
1067 }
1068
1069 if (device_count == 0) {
1070 snd_mts64_unregister_all();
1071 return -ENODEV;
1072 }
1073
1074 return 0;
1075}
1076
1077static void __exit snd_mts64_module_exit(void)
1078{
1079 snd_mts64_unregister_all();
1080}
1081
1082module_init(snd_mts64_module_init);
1083module_exit(snd_mts64_module_exit);