1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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>
32
33#define CARD_NAME "Miditerminal 4140"
34#define DRIVER_NAME "MTS64"
35#define PLATFORM_DRIVER "snd_mts64"
36
37static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
38static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
39static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
40
41static struct platform_device *platform_devices[SNDRV_CARDS];
42static int device_count;
43
44module_param_array(index, int, NULL, 0444);
45MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
46module_param_array(id, charp, NULL, 0444);
47MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
48module_param_array(enable, bool, NULL, 0444);
49MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
50
51MODULE_AUTHOR("Matthias Koenig <mk@phasorlab.de>");
52MODULE_DESCRIPTION("ESI Miditerminal 4140");
53MODULE_LICENSE("GPL");
54MODULE_SUPPORTED_DEVICE("{{ESI,Miditerminal 4140}}");
55
56
57
58
59#define MTS64_NUM_INPUT_PORTS 5
60#define MTS64_NUM_OUTPUT_PORTS 4
61#define MTS64_SMPTE_SUBSTREAM 4
62
63struct mts64 {
64 spinlock_t lock;
65 struct snd_card *card;
66 struct snd_rawmidi *rmidi;
67 struct pardevice *pardev;
68 int open_count;
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];
73 int smpte_switch;
74 u8 time[4];
75 u8 fps;
76};
77
78static int snd_mts64_free(struct mts64 *mts)
79{
80 kfree(mts);
81 return 0;
82}
83
84static int snd_mts64_create(struct snd_card *card,
85 struct pardevice *pardev,
86 struct mts64 **rchip)
87{
88 struct mts64 *mts;
89
90 *rchip = NULL;
91
92 mts = kzalloc(sizeof(struct mts64), GFP_KERNEL);
93 if (mts == NULL)
94 return -ENOMEM;
95
96
97 spin_lock_init(&mts->lock);
98 mts->card = card;
99 mts->pardev = pardev;
100 mts->current_midi_output_port = -1;
101 mts->current_midi_input_port = -1;
102
103 *rchip = mts;
104
105 return 0;
106}
107
108
109
110
111
112
113#define MTS64_STAT_BSY 0x80
114#define MTS64_STAT_BIT_SET 0x20
115#define MTS64_STAT_PORT 0x10
116
117
118#define MTS64_CTL_READOUT 0x08
119#define MTS64_CTL_WRITE_CMD 0x06
120#define MTS64_CTL_WRITE_DATA 0x02
121#define MTS64_CTL_STROBE 0x01
122
123
124#define MTS64_CMD_RESET 0xfe
125#define MTS64_CMD_PROBE 0x8f
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
135#define MTS64_CMD_COM_CLOSE1 0xff
136#define MTS64_CMD_COM_CLOSE2 0xf5
137
138
139
140
141static void mts64_enable_readout(struct parport *p);
142static void mts64_disable_readout(struct parport *p);
143static int mts64_device_ready(struct parport *p);
144static int mts64_device_init(struct parport *p);
145static int mts64_device_open(struct mts64 *mts);
146static int mts64_device_close(struct mts64 *mts);
147static u8 mts64_map_midi_input(u8 c);
148static int mts64_probe(struct parport *p);
149static u16 mts64_read(struct parport *p);
150static u8 mts64_read_char(struct parport *p);
151static void mts64_smpte_start(struct parport *p,
152 u8 hours, u8 minutes,
153 u8 seconds, u8 frames,
154 u8 idx);
155static void mts64_smpte_stop(struct parport *p);
156static void mts64_write_command(struct parport *p, u8 c);
157static void mts64_write_data(struct parport *p, u8 c);
158static void mts64_write_midi(struct mts64 *mts, u8 c, int midiport);
159
160
161
162
163
164
165
166static void mts64_enable_readout(struct parport *p)
167{
168 u8 c;
169
170 c = parport_read_control(p);
171 c |= MTS64_CTL_READOUT;
172 parport_write_control(p, c);
173}
174
175
176
177
178
179static void mts64_disable_readout(struct parport *p)
180{
181 u8 c;
182
183 c = parport_read_control(p);
184 c &= ~MTS64_CTL_READOUT;
185 parport_write_control(p, c);
186}
187
188
189
190
191
192
193
194static int mts64_device_ready(struct parport *p)
195{
196 int i;
197 u8 c;
198
199 for (i = 0; i < 0xffff; ++i) {
200 c = parport_read_status(p);
201 c &= MTS64_STAT_BSY;
202 if (c != 0)
203 return 1;
204 }
205
206 return 0;
207}
208
209
210
211
212
213
214
215static int mts64_device_init(struct parport *p)
216{
217 int i;
218
219 mts64_write_command(p, MTS64_CMD_RESET);
220
221 for (i = 0; i < 64; ++i) {
222 msleep(100);
223
224 if (mts64_probe(p) == 0) {
225
226 mts64_disable_readout(p);
227 return 0;
228 }
229 }
230 mts64_disable_readout(p);
231
232 return -EIO;
233}
234
235
236
237
238static int mts64_device_open(struct mts64 *mts)
239{
240 int i;
241 struct parport *p = mts->pardev->port;
242
243 for (i = 0; i < 5; ++i)
244 mts64_write_command(p, MTS64_CMD_COM_OPEN);
245
246 return 0;
247}
248
249
250
251
252static int mts64_device_close(struct mts64 *mts)
253{
254 int i;
255 struct parport *p = mts->pardev->port;
256
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);
260 }
261
262 return 0;
263}
264
265
266
267
268
269
270
271
272
273
274
275
276static u8 mts64_map_midi_input(u8 c)
277{
278 static u8 map[] = { 0, 1, 4, 2, 3 };
279
280 return map[c];
281}
282
283
284
285
286
287
288
289
290
291static int mts64_probe(struct parport *p)
292{
293 u8 c;
294
295 mts64_smpte_stop(p);
296 mts64_write_command(p, MTS64_CMD_PROBE);
297
298 msleep(50);
299
300 c = mts64_read(p);
301
302 c &= 0x00ff;
303 if (c != MTS64_CMD_PROBE)
304 return -ENODEV;
305 else
306 return 0;
307
308}
309
310
311
312
313
314
315static u16 mts64_read(struct parport *p)
316{
317 u8 data, status;
318
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);
324
325 return (status << 8) | data;
326}
327
328
329
330
331
332
333
334
335
336
337
338
339static u8 mts64_read_char(struct parport *p)
340{
341 u8 c = 0;
342 u8 status;
343 u8 i;
344
345 for (i = 0; i < 8; ++i) {
346 parport_write_data(p, i);
347 c >>= 1;
348 status = parport_read_status(p);
349 if (status & MTS64_STAT_BIT_SET)
350 c |= 0x80;
351 }
352
353 return c;
354}
355
356
357
358
359
360
361
362
363
364
365static void mts64_smpte_start(struct parport *p,
366 u8 hours, u8 minutes,
367 u8 seconds, u8 frames,
368 u8 idx)
369{
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 };
375
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);
381
382 mts64_write_command(p, MTS64_CMD_SMPTE_SET_FPS);
383 mts64_write_command(p, fps[idx]);
384}
385
386
387
388static void mts64_smpte_stop(struct parport *p)
389{
390 mts64_write_command(p, MTS64_CMD_SMPTE_STOP);
391}
392
393
394
395static void mts64_write_command(struct parport *p, u8 c)
396{
397 mts64_device_ready(p);
398
399 parport_write_data(p, c);
400
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);
404}
405
406
407
408static void mts64_write_data(struct parport *p, u8 c)
409{
410 mts64_device_ready(p);
411
412 parport_write_data(p, c);
413
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);
417}
418
419
420
421
422
423
424static void mts64_write_midi(struct mts64 *mts, u8 c,
425 int midiport)
426{
427 struct parport *p = mts->pardev->port;
428
429
430 if (mts->current_midi_output_port != midiport)
431 mts64_write_command(p, midiport);
432
433
434 mts64_write_data(p, c);
435}
436
437
438
439
440
441
442#define snd_mts64_ctl_smpte_switch_info snd_ctl_boolean_mono_info
443
444static int snd_mts64_ctl_smpte_switch_get(struct snd_kcontrol* kctl,
445 struct snd_ctl_elem_value *uctl)
446{
447 struct mts64 *mts = snd_kcontrol_chip(kctl);
448
449 spin_lock_irq(&mts->lock);
450 uctl->value.integer.value[0] = mts->smpte_switch;
451 spin_unlock_irq(&mts->lock);
452
453 return 0;
454}
455
456
457
458static int snd_mts64_ctl_smpte_switch_put(struct snd_kcontrol* kctl,
459 struct snd_ctl_elem_value *uctl)
460{
461 struct mts64 *mts = snd_kcontrol_chip(kctl);
462 int changed = 0;
463 int val = !!uctl->value.integer.value[0];
464
465 spin_lock_irq(&mts->lock);
466 if (mts->smpte_switch == val)
467 goto __out;
468
469 changed = 1;
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],
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 = {
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
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 unsigned int time = uctl->value.integer.value[0] % 60;
545 int changed = 0;
546
547 spin_lock_irq(&mts->lock);
548 if (mts->time[idx] != time) {
549 changed = 1;
550 mts->time[idx] = time;
551 }
552 spin_unlock_irq(&mts->lock);
553
554 return changed;
555}
556
557static struct snd_kcontrol_new mts64_ctl_smpte_time_hours = {
558 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
559 .name = "SMPTE Time Hours",
560 .index = 0,
561 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
562 .private_value = 0,
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
566};
567
568static struct snd_kcontrol_new mts64_ctl_smpte_time_minutes = {
569 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
570 .name = "SMPTE Time Minutes",
571 .index = 0,
572 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
573 .private_value = 1,
574 .info = snd_mts64_ctl_smpte_time_info,
575 .get = snd_mts64_ctl_smpte_time_get,
576 .put = snd_mts64_ctl_smpte_time_put
577};
578
579static struct snd_kcontrol_new mts64_ctl_smpte_time_seconds = {
580 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
581 .name = "SMPTE Time Seconds",
582 .index = 0,
583 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
584 .private_value = 2,
585 .info = snd_mts64_ctl_smpte_time_info,
586 .get = snd_mts64_ctl_smpte_time_get,
587 .put = snd_mts64_ctl_smpte_time_put
588};
589
590static struct snd_kcontrol_new mts64_ctl_smpte_time_frames = {
591 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
592 .name = "SMPTE Time Frames",
593 .index = 0,
594 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
595 .private_value = 3,
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
599};
600
601
602static int snd_mts64_ctl_smpte_fps_info(struct snd_kcontrol *kctl,
603 struct snd_ctl_elem_info *uinfo)
604{
605 static const char * const texts[5] = {
606 "24", "25", "29.97", "30D", "30"
607 };
608
609 return snd_ctl_enum_info(uinfo, 1, 5, texts);
610}
611
612static int snd_mts64_ctl_smpte_fps_get(struct snd_kcontrol *kctl,
613 struct snd_ctl_elem_value *uctl)
614{
615 struct mts64 *mts = snd_kcontrol_chip(kctl);
616
617 spin_lock_irq(&mts->lock);
618 uctl->value.enumerated.item[0] = mts->fps;
619 spin_unlock_irq(&mts->lock);
620
621 return 0;
622}
623
624static int snd_mts64_ctl_smpte_fps_put(struct snd_kcontrol *kctl,
625 struct snd_ctl_elem_value *uctl)
626{
627 struct mts64 *mts = snd_kcontrol_chip(kctl);
628 int changed = 0;
629
630 if (uctl->value.enumerated.item[0] >= 5)
631 return -EINVAL;
632 spin_lock_irq(&mts->lock);
633 if (mts->fps != uctl->value.enumerated.item[0]) {
634 changed = 1;
635 mts->fps = uctl->value.enumerated.item[0];
636 }
637 spin_unlock_irq(&mts->lock);
638
639 return changed;
640}
641
642static struct snd_kcontrol_new mts64_ctl_smpte_fps = {
643 .iface = SNDRV_CTL_ELEM_IFACE_RAWMIDI,
644 .name = "SMPTE Fps",
645 .index = 0,
646 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
647 .private_value = 0,
648 .info = snd_mts64_ctl_smpte_fps_info,
649 .get = snd_mts64_ctl_smpte_fps_get,
650 .put = snd_mts64_ctl_smpte_fps_put
651};
652
653
654static int snd_mts64_ctl_create(struct snd_card *card,
655 struct mts64 *mts)
656{
657 int err, i;
658 static struct snd_kcontrol_new *control[] = {
659 &mts64_ctl_smpte_switch,
660 &mts64_ctl_smpte_time_hours,
661 &mts64_ctl_smpte_time_minutes,
662 &mts64_ctl_smpte_time_seconds,
663 &mts64_ctl_smpte_time_frames,
664 &mts64_ctl_smpte_fps,
665 NULL };
666
667 for (i = 0; control[i]; ++i) {
668 err = snd_ctl_add(card, snd_ctl_new1(control[i], mts));
669 if (err < 0) {
670 snd_printd("Cannot create control: %s\n",
671 control[i]->name);
672 return err;
673 }
674 }
675
676 return 0;
677}
678
679
680
681
682#define MTS64_MODE_INPUT_TRIGGERED 0x01
683
684static int snd_mts64_rawmidi_open(struct snd_rawmidi_substream *substream)
685{
686 struct mts64 *mts = substream->rmidi->private_data;
687
688 if (mts->open_count == 0) {
689
690
691
692 mts64_device_open(mts);
693
694 msleep(50);
695 }
696 ++(mts->open_count);
697
698 return 0;
699}
700
701static int snd_mts64_rawmidi_close(struct snd_rawmidi_substream *substream)
702{
703 struct mts64 *mts = substream->rmidi->private_data;
704 unsigned long flags;
705
706 --(mts->open_count);
707 if (mts->open_count == 0) {
708
709
710 spin_lock_irqsave(&mts->lock, flags);
711 mts64_device_close(mts);
712 spin_unlock_irqrestore(&mts->lock, flags);
713
714 msleep(500);
715
716 } else if (mts->open_count < 0)
717 mts->open_count = 0;
718
719 return 0;
720}
721
722static void snd_mts64_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,
723 int up)
724{
725 struct mts64 *mts = substream->rmidi->private_data;
726 u8 data;
727 unsigned long flags;
728
729 spin_lock_irqsave(&mts->lock, flags);
730 while (snd_rawmidi_transmit_peek(substream, &data, 1) == 1) {
731 mts64_write_midi(mts, data, substream->number+1);
732 snd_rawmidi_transmit_ack(substream, 1);
733 }
734 spin_unlock_irqrestore(&mts->lock, flags);
735}
736
737static void snd_mts64_rawmidi_input_trigger(struct snd_rawmidi_substream *substream,
738 int up)
739{
740 struct mts64 *mts = substream->rmidi->private_data;
741 unsigned long flags;
742
743 spin_lock_irqsave(&mts->lock, flags);
744 if (up)
745 mts->mode[substream->number] |= MTS64_MODE_INPUT_TRIGGERED;
746 else
747 mts->mode[substream->number] &= ~MTS64_MODE_INPUT_TRIGGERED;
748
749 spin_unlock_irqrestore(&mts->lock, flags);
750}
751
752static const struct snd_rawmidi_ops snd_mts64_rawmidi_output_ops = {
753 .open = snd_mts64_rawmidi_open,
754 .close = snd_mts64_rawmidi_close,
755 .trigger = snd_mts64_rawmidi_output_trigger
756};
757
758static const struct snd_rawmidi_ops snd_mts64_rawmidi_input_ops = {
759 .open = snd_mts64_rawmidi_open,
760 .close = snd_mts64_rawmidi_close,
761 .trigger = snd_mts64_rawmidi_input_trigger
762};
763
764
765static int snd_mts64_rawmidi_create(struct snd_card *card)
766{
767 struct mts64 *mts = card->private_data;
768 struct snd_rawmidi *rmidi;
769 struct snd_rawmidi_substream *substream;
770 struct list_head *list;
771 int err;
772
773 err = snd_rawmidi_new(card, CARD_NAME, 0,
774 MTS64_NUM_OUTPUT_PORTS,
775 MTS64_NUM_INPUT_PORTS,
776 &rmidi);
777 if (err < 0)
778 return err;
779
780 rmidi->private_data = mts;
781 strcpy(rmidi->name, CARD_NAME);
782 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
783 SNDRV_RAWMIDI_INFO_INPUT |
784 SNDRV_RAWMIDI_INFO_DUPLEX;
785
786 mts->rmidi = rmidi;
787
788
789 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
790 &snd_mts64_rawmidi_output_ops);
791 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
792 &snd_mts64_rawmidi_input_ops);
793
794
795
796 list_for_each(list,
797 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
798 substream = list_entry(list, struct snd_rawmidi_substream, list);
799 sprintf(substream->name,
800 "Miditerminal %d", substream->number+1);
801 }
802
803 list_for_each(list,
804 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
805 substream = list_entry(list, struct snd_rawmidi_substream, list);
806 mts->midi_input_substream[substream->number] = substream;
807 switch(substream->number) {
808 case MTS64_SMPTE_SUBSTREAM:
809 strcpy(substream->name, "Miditerminal SMPTE");
810 break;
811 default:
812 sprintf(substream->name,
813 "Miditerminal %d", substream->number+1);
814 }
815 }
816
817
818 err = snd_mts64_ctl_create(card, mts);
819
820 return err;
821}
822
823
824
825
826static void snd_mts64_interrupt(void *private)
827{
828 struct mts64 *mts = ((struct snd_card*)private)->private_data;
829 u16 ret;
830 u8 status, data;
831 struct snd_rawmidi_substream *substream;
832
833 spin_lock(&mts->lock);
834 ret = mts64_read(mts->pardev->port);
835 data = ret & 0x00ff;
836 status = ret >> 8;
837
838 if (status & MTS64_STAT_PORT) {
839 mts->current_midi_input_port = mts64_map_midi_input(data);
840 } else {
841 if (mts->current_midi_input_port == -1)
842 goto __out;
843 substream = mts->midi_input_substream[mts->current_midi_input_port];
844 if (mts->mode[substream->number] & MTS64_MODE_INPUT_TRIGGERED)
845 snd_rawmidi_receive(substream, &data, 1);
846 }
847__out:
848 spin_unlock(&mts->lock);
849}
850
851static void snd_mts64_attach(struct parport *p)
852{
853 struct platform_device *device;
854
855 device = platform_device_alloc(PLATFORM_DRIVER, device_count);
856 if (!device)
857 return;
858
859
860 platform_set_drvdata(device, p);
861
862 if (platform_device_add(device) < 0) {
863 platform_device_put(device);
864 return;
865 }
866
867
868
869 if (!platform_get_drvdata(device)) {
870 platform_device_unregister(device);
871 return;
872 }
873
874
875 platform_devices[device_count] = device;
876 device_count++;
877}
878
879static void snd_mts64_detach(struct parport *p)
880{
881
882}
883
884static int snd_mts64_dev_probe(struct pardevice *pardev)
885{
886 if (strcmp(pardev->name, DRIVER_NAME))
887 return -ENODEV;
888
889 return 0;
890}
891
892static struct parport_driver mts64_parport_driver = {
893 .name = "mts64",
894 .probe = snd_mts64_dev_probe,
895 .match_port = snd_mts64_attach,
896 .detach = snd_mts64_detach,
897 .devmodel = true,
898};
899
900
901
902
903static void snd_mts64_card_private_free(struct snd_card *card)
904{
905 struct mts64 *mts = card->private_data;
906 struct pardevice *pardev = mts->pardev;
907
908 if (pardev) {
909 parport_release(pardev);
910 parport_unregister_device(pardev);
911 }
912
913 snd_mts64_free(mts);
914}
915
916static int snd_mts64_probe(struct platform_device *pdev)
917{
918 struct pardevice *pardev;
919 struct parport *p;
920 int dev = pdev->id;
921 struct snd_card *card = NULL;
922 struct mts64 *mts = NULL;
923 int err;
924 struct pardev_cb mts64_cb = {
925 .preempt = NULL,
926 .wakeup = NULL,
927 .irq_func = snd_mts64_interrupt,
928 .flags = PARPORT_DEV_EXCL,
929 };
930
931 p = platform_get_drvdata(pdev);
932 platform_set_drvdata(pdev, NULL);
933
934 if (dev >= SNDRV_CARDS)
935 return -ENODEV;
936 if (!enable[dev])
937 return -ENOENT;
938
939 err = snd_card_new(&pdev->dev, index[dev], id[dev], THIS_MODULE,
940 0, &card);
941 if (err < 0) {
942 snd_printd("Cannot create card\n");
943 return err;
944 }
945 strcpy(card->driver, DRIVER_NAME);
946 strcpy(card->shortname, "ESI " CARD_NAME);
947 sprintf(card->longname, "%s at 0x%lx, irq %i",
948 card->shortname, p->base, p->irq);
949
950 mts64_cb.private = card;
951 pardev = parport_register_dev_model(p,
952 DRIVER_NAME,
953 &mts64_cb,
954 pdev->id);
955 if (!pardev) {
956 snd_printd("Cannot register pardevice\n");
957 err = -EIO;
958 goto __err;
959 }
960
961
962 if (parport_claim(pardev)) {
963 snd_printd("Cannot claim parport 0x%lx\n", pardev->port->base);
964 err = -EIO;
965 goto free_pardev;
966 }
967
968 if ((err = snd_mts64_create(card, pardev, &mts)) < 0) {
969 snd_printd("Cannot create main component\n");
970 goto release_pardev;
971 }
972 card->private_data = mts;
973 card->private_free = snd_mts64_card_private_free;
974
975 err = mts64_probe(p);
976 if (err) {
977 err = -EIO;
978 goto __err;
979 }
980
981 if ((err = snd_mts64_rawmidi_create(card)) < 0) {
982 snd_printd("Creating Rawmidi component failed\n");
983 goto __err;
984 }
985
986
987 if ((err = mts64_device_init(p)) < 0)
988 goto __err;
989
990 platform_set_drvdata(pdev, card);
991
992
993 if ((err = snd_card_register(card)) < 0) {
994 snd_printd("Cannot register card\n");
995 goto __err;
996 }
997
998 snd_printk(KERN_INFO "ESI Miditerminal 4140 on 0x%lx\n", p->base);
999 return 0;
1000
1001release_pardev:
1002 parport_release(pardev);
1003free_pardev:
1004 parport_unregister_device(pardev);
1005__err:
1006 snd_card_free(card);
1007 return err;
1008}
1009
1010static int snd_mts64_remove(struct platform_device *pdev)
1011{
1012 struct snd_card *card = platform_get_drvdata(pdev);
1013
1014 if (card)
1015 snd_card_free(card);
1016
1017 return 0;
1018}
1019
1020static struct platform_driver snd_mts64_driver = {
1021 .probe = snd_mts64_probe,
1022 .remove = snd_mts64_remove,
1023 .driver = {
1024 .name = PLATFORM_DRIVER,
1025 }
1026};
1027
1028
1029
1030
1031static void snd_mts64_unregister_all(void)
1032{
1033 int i;
1034
1035 for (i = 0; i < SNDRV_CARDS; ++i) {
1036 if (platform_devices[i]) {
1037 platform_device_unregister(platform_devices[i]);
1038 platform_devices[i] = NULL;
1039 }
1040 }
1041 platform_driver_unregister(&snd_mts64_driver);
1042 parport_unregister_driver(&mts64_parport_driver);
1043}
1044
1045static int __init snd_mts64_module_init(void)
1046{
1047 int err;
1048
1049 if ((err = platform_driver_register(&snd_mts64_driver)) < 0)
1050 return err;
1051
1052 if (parport_register_driver(&mts64_parport_driver) != 0) {
1053 platform_driver_unregister(&snd_mts64_driver);
1054 return -EIO;
1055 }
1056
1057 if (device_count == 0) {
1058 snd_mts64_unregister_all();
1059 return -ENODEV;
1060 }
1061
1062 return 0;
1063}
1064
1065static void __exit snd_mts64_module_exit(void)
1066{
1067 snd_mts64_unregister_all();
1068}
1069
1070module_init(snd_mts64_module_init);
1071module_exit(snd_mts64_module_exit);
1072