1 /*
2  **********************************************************************
3  *     midi.c - /dev/midi interface for emu10k1 driver
4  *     Copyright 1999, 2000 Creative Labs, Inc.
5  *
6  **********************************************************************
7  *
8  *     Date                 Author          Summary of changes
9  *     ----                 ------          ------------------
10  *     October 20, 1999     Bertrand Lee    base code release
11  *
12  **********************************************************************
13  *
14  *     This program is free software; you can redistribute it and/or
15  *     modify it under the terms of the GNU General Public License as
16  *     published by the Free Software Foundation; either version 2 of
17  *     the License, or (at your option) any later version.
18  *
19  *     This program is distributed in the hope that it will be useful,
20  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *     GNU General Public License for more details.
23  *
24  *     You should have received a copy of the GNU General Public
25  *     License along with this program; if not, write to the Free
26  *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
27  *     USA.
28  *
29  **********************************************************************
30  */
31 
32 #define __NO_VERSION__
33 #include <linux/module.h>
34 #include <linux/poll.h>
35 #include <linux/slab.h>
36 #include <linux/version.h>
37 #include <linux/sched.h>
38 #include <linux/smp_lock.h>
39 #include <asm/uaccess.h>
40 
41 #include "hwaccess.h"
42 #include "cardmo.h"
43 #include "cardmi.h"
44 #include "midi.h"
45 
46 #ifdef EMU10K1_SEQUENCER
47 #include "../sound_config.h"
48 #endif
49 
50 static spinlock_t midi_spinlock __attribute((unused)) = SPIN_LOCK_UNLOCKED;
51 
init_midi_hdr(struct midi_hdr * midihdr)52 static void init_midi_hdr(struct midi_hdr *midihdr)
53 {
54 	midihdr->bufferlength = MIDIIN_BUFLEN;
55 	midihdr->bytesrecorded = 0;
56 	midihdr->flags = 0;
57 }
58 
midiin_add_buffer(struct emu10k1_mididevice * midi_dev,struct midi_hdr ** midihdrptr)59 static int midiin_add_buffer(struct emu10k1_mididevice *midi_dev, struct midi_hdr **midihdrptr)
60 {
61 	struct midi_hdr *midihdr;
62 
63 	if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr), GFP_KERNEL)) == NULL) {
64 		ERROR();
65 		return -EINVAL;
66 	}
67 
68 	init_midi_hdr(midihdr);
69 
70 	if ((midihdr->data = (u8 *) kmalloc(MIDIIN_BUFLEN, GFP_KERNEL)) == NULL) {
71 		ERROR();
72 		kfree(midihdr);
73 		return -1;
74 	}
75 
76 	if (emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr) < 0) {
77 		ERROR();
78 		kfree(midihdr->data);
79 		kfree(midihdr);
80 		return -1;
81 	}
82 
83 	*midihdrptr = midihdr;
84 	list_add_tail(&midihdr->list, &midi_dev->mid_hdrs);
85 
86 	return 0;
87 }
88 
emu10k1_midi_open(struct inode * inode,struct file * file)89 static int emu10k1_midi_open(struct inode *inode, struct file *file)
90 {
91 	int minor = MINOR(inode->i_rdev);
92 	struct emu10k1_card *card = NULL;
93 	struct emu10k1_mididevice *midi_dev;
94 	struct list_head *entry;
95 
96 	DPF(2, "emu10k1_midi_open()\n");
97 
98 	/* Check for correct device to open */
99 	list_for_each(entry, &emu10k1_devs) {
100 		card = list_entry(entry, struct emu10k1_card, list);
101 
102 		if (card->midi_dev == minor)
103 			goto match;
104 	}
105 
106 	return -ENODEV;
107 
108 match:
109 #ifdef EMU10K1_SEQUENCER
110 	if (card->seq_mididev)	/* card is opened by sequencer */
111 		return -EBUSY;
112 #endif
113 
114 	/* Wait for device to become free */
115 	down(&card->open_sem);
116 	while (card->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
117 		if (file->f_flags & O_NONBLOCK) {
118 			up(&card->open_sem);
119 			return -EBUSY;
120 		}
121 
122 		up(&card->open_sem);
123 		interruptible_sleep_on(&card->open_wait);
124 
125 		if (signal_pending(current)) {
126 			return -ERESTARTSYS;
127 		}
128 
129 		down(&card->open_sem);
130 	}
131 
132 	if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL)
133 		return -EINVAL;
134 
135 	midi_dev->card = card;
136 	midi_dev->mistate = MIDIIN_STATE_STOPPED;
137 	init_waitqueue_head(&midi_dev->oWait);
138 	init_waitqueue_head(&midi_dev->iWait);
139 	midi_dev->ird = 0;
140 	midi_dev->iwr = 0;
141 	midi_dev->icnt = 0;
142 	INIT_LIST_HEAD(&midi_dev->mid_hdrs);
143 
144 	if (file->f_mode & FMODE_READ) {
145 		struct midi_openinfo dsCardMidiOpenInfo;
146 		struct midi_hdr *midihdr1;
147 		struct midi_hdr *midihdr2;
148 
149 		dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
150 
151 		if (emu10k1_mpuin_open(card, &dsCardMidiOpenInfo) < 0) {
152 			ERROR();
153 			kfree(midi_dev);
154 			return -ENODEV;
155 		}
156 
157 		/* Add two buffers to receive sysex buffer */
158 		if (midiin_add_buffer(midi_dev, &midihdr1) < 0) {
159 			kfree(midi_dev);
160 			return -ENODEV;
161 		}
162 
163 		if (midiin_add_buffer(midi_dev, &midihdr2) < 0) {
164 			list_del(&midihdr1->list);
165 			kfree(midihdr1->data);
166 			kfree(midihdr1);
167 			kfree(midi_dev);
168 			return -ENODEV;
169 		}
170 	}
171 
172 	if (file->f_mode & FMODE_WRITE) {
173 		struct midi_openinfo dsCardMidiOpenInfo;
174 
175 		dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
176 
177 		if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo) < 0) {
178 			ERROR();
179 			kfree(midi_dev);
180 			return -ENODEV;
181 		}
182 	}
183 
184 	file->private_data = (void *) midi_dev;
185 
186 	card->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
187 
188 	up(&card->open_sem);
189 
190 	return 0;
191 }
192 
emu10k1_midi_release(struct inode * inode,struct file * file)193 static int emu10k1_midi_release(struct inode *inode, struct file *file)
194 {
195 	struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
196 	struct emu10k1_card *card;
197 
198 	lock_kernel();
199 
200 	card = midi_dev->card;
201 	DPF(2, "emu10k1_midi_release()\n");
202 
203 	if (file->f_mode & FMODE_WRITE) {
204 		if (!(file->f_flags & O_NONBLOCK)) {
205 
206 			while (!signal_pending(current) && (card->mpuout->firstmidiq != NULL)) {
207 				DPF(4, "Cannot close - buffers not empty\n");
208 
209 				interruptible_sleep_on(&midi_dev->oWait);
210 
211 			}
212 		}
213 
214 		emu10k1_mpuout_close(card);
215 	}
216 
217 	if (file->f_mode & FMODE_READ) {
218 		struct midi_hdr *midihdr;
219 
220 		if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
221 			emu10k1_mpuin_stop(card);
222 			midi_dev->mistate = MIDIIN_STATE_STOPPED;
223 		}
224 
225 		emu10k1_mpuin_reset(card);
226 		emu10k1_mpuin_close(card);
227 
228 		while (!list_empty(&midi_dev->mid_hdrs)) {
229 			midihdr = list_entry(midi_dev->mid_hdrs.next, struct midi_hdr, list);
230 
231 			list_del(midi_dev->mid_hdrs.next);
232 			kfree(midihdr->data);
233 			kfree(midihdr);
234 		}
235 	}
236 
237 	kfree(midi_dev);
238 
239 	down(&card->open_sem);
240 	card->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE));
241 	up(&card->open_sem);
242 	wake_up_interruptible(&card->open_wait);
243 
244 	unlock_kernel();
245 
246 	return 0;
247 }
248 
emu10k1_midi_read(struct file * file,char * buffer,size_t count,loff_t * pos)249 static ssize_t emu10k1_midi_read(struct file *file, char *buffer, size_t count, loff_t * pos)
250 {
251 	struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
252 	ssize_t ret = 0;
253 	u16 cnt;
254 	unsigned long flags;
255 
256 	DPD(4, "emu10k1_midi_read(), count %#x\n", (u32) count);
257 
258 	if (pos != &file->f_pos)
259 		return -ESPIPE;
260 
261 	if (!access_ok(VERIFY_WRITE, buffer, count))
262 		return -EFAULT;
263 
264 	if (midi_dev->mistate == MIDIIN_STATE_STOPPED) {
265 		if (emu10k1_mpuin_start(midi_dev->card) < 0) {
266 			ERROR();
267 			return -EINVAL;
268 		}
269 
270 		midi_dev->mistate = MIDIIN_STATE_STARTED;
271 	}
272 
273 	while (count > 0) {
274 		cnt = MIDIIN_BUFLEN - midi_dev->ird;
275 
276 		spin_lock_irqsave(&midi_spinlock, flags);
277 
278 		if (midi_dev->icnt < cnt)
279 			cnt = midi_dev->icnt;
280 
281 		spin_unlock_irqrestore(&midi_spinlock, flags);
282 
283 		if (cnt > count)
284 			cnt = count;
285 
286 		if (cnt <= 0) {
287 			if (file->f_flags & O_NONBLOCK)
288 				return ret ? ret : -EAGAIN;
289 			DPF(2, " Go to sleep...\n");
290 
291 			interruptible_sleep_on(&midi_dev->iWait);
292 
293 			if (signal_pending(current))
294 				return ret ? ret : -ERESTARTSYS;
295 
296 			continue;
297 		}
298 
299 		if (copy_to_user(buffer, midi_dev->iBuf + midi_dev->ird, cnt)) {
300 			ERROR();
301 			return ret ? ret : -EFAULT;
302 		}
303 
304 		midi_dev->ird += cnt;
305 		midi_dev->ird %= MIDIIN_BUFLEN;
306 
307 		spin_lock_irqsave(&midi_spinlock, flags);
308 
309 		midi_dev->icnt -= cnt;
310 
311 		spin_unlock_irqrestore(&midi_spinlock, flags);
312 
313 		count -= cnt;
314 		buffer += cnt;
315 		ret += cnt;
316 
317 		if (midi_dev->icnt == 0)
318 			break;
319 	}
320 
321 	return ret;
322 }
323 
emu10k1_midi_write(struct file * file,const char * buffer,size_t count,loff_t * pos)324 static ssize_t emu10k1_midi_write(struct file *file, const char *buffer, size_t count, loff_t * pos)
325 {
326 	struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
327 	struct midi_hdr *midihdr;
328 	ssize_t ret = 0;
329 	unsigned long flags;
330 
331 	DPD(4, "emu10k1_midi_write(), count=%#x\n", (u32) count);
332 
333 	if (pos != &file->f_pos)
334 		return -ESPIPE;
335 
336 	if (!access_ok(VERIFY_READ, buffer, count))
337 		return -EFAULT;
338 
339 	if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr), GFP_KERNEL)) == NULL)
340 		return -EINVAL;
341 
342 	midihdr->bufferlength = count;
343 	midihdr->bytesrecorded = 0;
344 	midihdr->flags = 0;
345 
346 	if ((midihdr->data = (u8 *) kmalloc(count, GFP_KERNEL)) == NULL) {
347 		ERROR();
348 		kfree(midihdr);
349 		return -EINVAL;
350 	}
351 
352 	if (copy_from_user(midihdr->data, buffer, count)) {
353 		kfree(midihdr->data);
354 		kfree(midihdr);
355 		return ret ? ret : -EFAULT;
356 	}
357 
358 	spin_lock_irqsave(&midi_spinlock, flags);
359 
360 	if (emu10k1_mpuout_add_buffer(midi_dev->card, midihdr) < 0) {
361 		ERROR();
362 		kfree(midihdr->data);
363 		kfree(midihdr);
364 		spin_unlock_irqrestore(&midi_spinlock, flags);
365 		return -EINVAL;
366 	}
367 
368 	spin_unlock_irqrestore(&midi_spinlock, flags);
369 
370 	return count;
371 }
372 
emu10k1_midi_poll(struct file * file,struct poll_table_struct * wait)373 static unsigned int emu10k1_midi_poll(struct file *file, struct poll_table_struct *wait)
374 {
375 	struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
376 	unsigned long flags;
377 	unsigned int mask = 0;
378 
379 	DPF(4, "emu10k1_midi_poll() called\n");
380 
381 	if (file->f_mode & FMODE_WRITE)
382 		poll_wait(file, &midi_dev->oWait, wait);
383 
384 	if (file->f_mode & FMODE_READ)
385 		poll_wait(file, &midi_dev->iWait, wait);
386 
387 	spin_lock_irqsave(&midi_spinlock, flags);
388 
389 	if (file->f_mode & FMODE_WRITE)
390 		mask |= POLLOUT | POLLWRNORM;
391 
392 	if (file->f_mode & FMODE_READ) {
393 		if (midi_dev->mistate == MIDIIN_STATE_STARTED)
394 			if (midi_dev->icnt > 0)
395 				mask |= POLLIN | POLLRDNORM;
396 	}
397 
398 	spin_unlock_irqrestore(&midi_spinlock, flags);
399 
400 	return mask;
401 }
402 
emu10k1_midi_callback(unsigned long msg,unsigned long refdata,unsigned long * pmsg)403 int emu10k1_midi_callback(unsigned long msg, unsigned long refdata, unsigned long *pmsg)
404 {
405 	struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) refdata;
406 	struct midi_hdr *midihdr = NULL;
407 	unsigned long flags;
408 	int i;
409 
410 	DPF(4, "emu10k1_midi_callback()\n");
411 
412 	spin_lock_irqsave(&midi_spinlock, flags);
413 
414 	switch (msg) {
415 	case ICARDMIDI_OUTLONGDATA:
416 		midihdr = (struct midi_hdr *) pmsg[2];
417 
418 		kfree(midihdr->data);
419 		kfree(midihdr);
420 		wake_up_interruptible(&midi_dev->oWait);
421 
422 		break;
423 
424 	case ICARDMIDI_INLONGDATA:
425 		midihdr = (struct midi_hdr *) pmsg[2];
426 
427 		for (i = 0; i < midihdr->bytesrecorded; i++) {
428 			midi_dev->iBuf[midi_dev->iwr++] = midihdr->data[i];
429 			midi_dev->iwr %= MIDIIN_BUFLEN;
430 		}
431 
432 		midi_dev->icnt += midihdr->bytesrecorded;
433 
434 		if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
435 			init_midi_hdr(midihdr);
436 			emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr);
437 			wake_up_interruptible(&midi_dev->iWait);
438 		}
439 		break;
440 
441 	case ICARDMIDI_INDATA:
442 		{
443 			u8 *pBuf = (u8 *) & pmsg[1];
444 			u16 bytesvalid = pmsg[2];
445 
446 			for (i = 0; i < bytesvalid; i++) {
447 				midi_dev->iBuf[midi_dev->iwr++] = pBuf[i];
448 				midi_dev->iwr %= MIDIIN_BUFLEN;
449 			}
450 
451 			midi_dev->icnt += bytesvalid;
452 		}
453 
454 		wake_up_interruptible(&midi_dev->iWait);
455 		break;
456 
457 	default:		/* Unknown message */
458 		spin_unlock_irqrestore(&midi_spinlock, flags);
459 		return -1;
460 	}
461 
462 	spin_unlock_irqrestore(&midi_spinlock, flags);
463 
464 	return 0;
465 }
466 
467 /* MIDI file operations */
468 struct file_operations emu10k1_midi_fops = {
469 	owner:		THIS_MODULE,
470 	read:		emu10k1_midi_read,
471 	write:		emu10k1_midi_write,
472 	poll:		emu10k1_midi_poll,
473 	open:		emu10k1_midi_open,
474 	release:	emu10k1_midi_release,
475 };
476 
477 
478 #ifdef EMU10K1_SEQUENCER
479 
480 /* functions used for sequencer access */
481 
emu10k1_seq_midi_open(int dev,int mode,void (* input)(int dev,unsigned char data),void (* output)(int dev))482 int emu10k1_seq_midi_open(int dev, int mode,
483 				void (*input) (int dev, unsigned char data),
484 				void (*output) (int dev))
485 {
486 	struct emu10k1_card *card;
487 	struct midi_openinfo dsCardMidiOpenInfo;
488 	struct emu10k1_mididevice *midi_dev;
489 
490 	if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
491 		return -EINVAL;
492 
493 	card = midi_devs[dev]->devc;
494 
495 	if (card->open_mode)		/* card is opened native */
496 		return -EBUSY;
497 
498 	DPF(2, "emu10k1_seq_midi_open()\n");
499 
500 	if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL)
501 		return -EINVAL;
502 
503 	midi_dev->card = card;
504 	midi_dev->mistate = MIDIIN_STATE_STOPPED;
505 	init_waitqueue_head(&midi_dev->oWait);
506 	init_waitqueue_head(&midi_dev->iWait);
507 	midi_dev->ird = 0;
508 	midi_dev->iwr = 0;
509 	midi_dev->icnt = 0;
510 	INIT_LIST_HEAD(&midi_dev->mid_hdrs);
511 
512 	dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
513 
514 	if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo) < 0) {
515 		ERROR();
516 		return -ENODEV;
517 	}
518 
519 	card->seq_mididev = midi_dev;
520 
521 	return 0;
522 }
523 
emu10k1_seq_midi_close(int dev)524 void emu10k1_seq_midi_close(int dev)
525 {
526 	struct emu10k1_card *card;
527 
528 	DPF(2, "emu10k1_seq_midi_close()\n");
529 	if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
530 		return;
531 
532 	card = midi_devs[dev]->devc;
533 	emu10k1_mpuout_close(card);
534 
535 	if (card->seq_mididev) {
536 		kfree(card->seq_mididev);
537 		card->seq_mididev = 0;
538 	}
539 }
540 
emu10k1_seq_midi_out(int dev,unsigned char midi_byte)541 int emu10k1_seq_midi_out(int dev, unsigned char midi_byte)
542 {
543 	struct emu10k1_card *card;
544 	struct midi_hdr *midihdr;
545 	unsigned long flags;
546 
547 	if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
548 		return -EINVAL;
549 
550 	card = midi_devs[dev]->devc;
551 
552 	if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr), GFP_KERNEL)) == NULL)
553 		return -EINVAL;
554 
555 	midihdr->bufferlength = 1;
556 	midihdr->bytesrecorded = 0;
557 	midihdr->flags = 0;
558 
559 	if ((midihdr->data = (u8 *) kmalloc(1, GFP_KERNEL)) == NULL) {
560 		ERROR();
561 		kfree(midihdr);
562 		return -EINVAL;
563 	}
564 
565 	*(midihdr->data) = midi_byte;
566 
567 	spin_lock_irqsave(&midi_spinlock, flags);
568 
569 	if (emu10k1_mpuout_add_buffer(card, midihdr) < 0) {
570 		ERROR();
571 		kfree(midihdr->data);
572 		kfree(midihdr);
573 		spin_unlock_irqrestore(&midi_spinlock, flags);
574 		return -EINVAL;
575 	}
576 
577 	spin_unlock_irqrestore(&midi_spinlock, flags);
578 
579 	return 1;
580 }
581 
emu10k1_seq_midi_start_read(int dev)582 int emu10k1_seq_midi_start_read(int dev)
583 {
584 	return 0;
585 }
586 
emu10k1_seq_midi_end_read(int dev)587 int emu10k1_seq_midi_end_read(int dev)
588 {
589 	return 0;
590 }
591 
emu10k1_seq_midi_kick(int dev)592 void emu10k1_seq_midi_kick(int dev)
593 {
594 }
595 
emu10k1_seq_midi_buffer_status(int dev)596 int emu10k1_seq_midi_buffer_status(int dev)
597 {
598 	int count;
599 	struct midi_queue *queue;
600 	struct emu10k1_card *card;
601 
602 	if (midi_devs[dev] == NULL || midi_devs[dev]->devc == NULL)
603 		return -EINVAL;
604 
605 	count = 0;
606 
607 	card = midi_devs[dev]->devc;
608 	queue = card->mpuout->firstmidiq;
609 
610 	while (queue != NULL) {
611 		count++;
612 		if (queue == card->mpuout->lastmidiq)
613 			break;
614 
615 		queue = queue->next;
616 	}
617 
618 	return count;
619 }
620 
621 #endif
622 
623