1 | /* $Id: midi.c 142 2000-04-23 14:55:46Z ktk $ */
|
---|
2 |
|
---|
3 |
|
---|
4 | /*
|
---|
5 | **********************************************************************
|
---|
6 | * midi.c - /dev/midi interface for emu10k1 driver
|
---|
7 | * Copyright 1999, 2000 Creative Labs, Inc.
|
---|
8 | *
|
---|
9 | **********************************************************************
|
---|
10 | *
|
---|
11 | * Date Author Summary of changes
|
---|
12 | * ---- ------ ------------------
|
---|
13 | * October 20, 1999 Bertrand Lee base code release
|
---|
14 | *
|
---|
15 | **********************************************************************
|
---|
16 | *
|
---|
17 | * This program is free software; you can redistribute it and/or
|
---|
18 | * modify it under the terms of the GNU General Public License as
|
---|
19 | * published by the Free Software Foundation; either version 2 of
|
---|
20 | * the License, or (at your option) any later version.
|
---|
21 | *
|
---|
22 | * This program is distributed in the hope that it will be useful,
|
---|
23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
25 | * GNU General Public License for more details.
|
---|
26 | *
|
---|
27 | * You should have received a copy of the GNU General Public
|
---|
28 | * License along with this program; if not, write to the Free
|
---|
29 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
|
---|
30 | * USA.
|
---|
31 | *
|
---|
32 | **********************************************************************
|
---|
33 | */
|
---|
34 |
|
---|
35 | #define __NO_VERSION__
|
---|
36 | #include <linux/module.h>
|
---|
37 | #include <asm/uaccess.h>
|
---|
38 |
|
---|
39 | #include "hwaccess.h"
|
---|
40 | #include "cardmo.h"
|
---|
41 | #include "cardmi.h"
|
---|
42 | #include "midi.h"
|
---|
43 |
|
---|
44 | static spinlock_t midi_spinlock = SPIN_LOCK_UNLOCKED;
|
---|
45 |
|
---|
46 | static void init_midi_hdr(struct midi_hdr *midihdr)
|
---|
47 | {
|
---|
48 | midihdr->bufferlength = MIDIIN_BUFLEN;
|
---|
49 | midihdr->bytesrecorded = 0;
|
---|
50 | midihdr->flags = 0;
|
---|
51 | }
|
---|
52 |
|
---|
53 | static int midiin_add_buffer(struct emu10k1_mididevice *midi_dev, struct midi_hdr **midihdrptr)
|
---|
54 | {
|
---|
55 | struct midi_hdr *midihdr;
|
---|
56 |
|
---|
57 | if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr *), GFP_KERNEL)) == NULL) {
|
---|
58 | ERROR();
|
---|
59 | return -EINVAL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | init_midi_hdr(midihdr);
|
---|
63 |
|
---|
64 | if ((midihdr->data = (u8 *) kmalloc(MIDIIN_BUFLEN, GFP_KERNEL)) == NULL) {
|
---|
65 | ERROR();
|
---|
66 | kfree(midihdr);
|
---|
67 | return CTSTATUS_ERROR;
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr) != CTSTATUS_SUCCESS) {
|
---|
71 | ERROR();
|
---|
72 | kfree(midihdr->data);
|
---|
73 | kfree(midihdr);
|
---|
74 | return CTSTATUS_ERROR;
|
---|
75 | }
|
---|
76 |
|
---|
77 | *midihdrptr = midihdr;
|
---|
78 | list_add_tail(&midihdr->list, &midi_dev->mid_hdrs);
|
---|
79 |
|
---|
80 | return CTSTATUS_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int emu10k1_midi_open(struct inode *inode, struct file *file)
|
---|
84 | {
|
---|
85 | int minor = MINOR(inode->i_rdev);
|
---|
86 | struct emu10k1_card *card;
|
---|
87 | struct emu10k1_mididevice *midi_dev;
|
---|
88 | struct list_head *entry;
|
---|
89 |
|
---|
90 | DPF(2, "emu10k1_midi_open()\n");
|
---|
91 |
|
---|
92 | /* Check for correct device to open */
|
---|
93 | list_for_each(entry, &emu10k1_devs) {
|
---|
94 | card = list_entry(entry, struct emu10k1_card, list);
|
---|
95 |
|
---|
96 | if (card->midi_num == minor)
|
---|
97 | break;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (entry == &emu10k1_devs)
|
---|
101 | return -ENODEV;
|
---|
102 |
|
---|
103 | MOD_INC_USE_COUNT;
|
---|
104 |
|
---|
105 | /* Wait for device to become free */
|
---|
106 | down(&card->open_sem);
|
---|
107 | while (card->open_mode & (file->f_mode << FMODE_MIDI_SHIFT)) {
|
---|
108 | if (file->f_flags & O_NONBLOCK) {
|
---|
109 | up(&card->open_sem);
|
---|
110 | MOD_DEC_USE_COUNT;
|
---|
111 | return -EBUSY;
|
---|
112 | }
|
---|
113 |
|
---|
114 | up(&card->open_sem);
|
---|
115 | interruptible_sleep_on(&card->open_wait);
|
---|
116 |
|
---|
117 | if (signal_pending(current)) {
|
---|
118 | MOD_DEC_USE_COUNT;
|
---|
119 | return -ERESTARTSYS;
|
---|
120 | }
|
---|
121 |
|
---|
122 | down(&card->open_sem);
|
---|
123 | }
|
---|
124 |
|
---|
125 | if ((midi_dev = (struct emu10k1_mididevice *) kmalloc(sizeof(*midi_dev), GFP_KERNEL)) == NULL) {
|
---|
126 | MOD_DEC_USE_COUNT;
|
---|
127 | return -EINVAL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | midi_dev->card = card;
|
---|
131 | midi_dev->mistate = MIDIIN_STATE_STOPPED;
|
---|
132 | init_waitqueue_head(&midi_dev->oWait);
|
---|
133 | init_waitqueue_head(&midi_dev->iWait);
|
---|
134 | midi_dev->ird = 0;
|
---|
135 | midi_dev->iwr = 0;
|
---|
136 | midi_dev->icnt = 0;
|
---|
137 | INIT_LIST_HEAD(&midi_dev->mid_hdrs);
|
---|
138 |
|
---|
139 | if (file->f_mode & FMODE_READ) {
|
---|
140 | struct midi_openinfo dsCardMidiOpenInfo;
|
---|
141 | struct midi_hdr *midihdr1;
|
---|
142 | struct midi_hdr *midihdr2;
|
---|
143 |
|
---|
144 | dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
|
---|
145 |
|
---|
146 | if (emu10k1_mpuin_open(card, &dsCardMidiOpenInfo)
|
---|
147 | != CTSTATUS_SUCCESS) {
|
---|
148 | ERROR();
|
---|
149 | kfree(midi_dev);
|
---|
150 | MOD_DEC_USE_COUNT;
|
---|
151 | return -ENODEV;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Add two buffers to receive sysex buffer */
|
---|
155 | if (midiin_add_buffer(midi_dev, &midihdr1) != CTSTATUS_SUCCESS) {
|
---|
156 | kfree(midi_dev);
|
---|
157 | MOD_DEC_USE_COUNT;
|
---|
158 | return -ENODEV;
|
---|
159 | }
|
---|
160 |
|
---|
161 | if (midiin_add_buffer(midi_dev, &midihdr2) != CTSTATUS_SUCCESS) {
|
---|
162 | list_del(&midihdr1->list);
|
---|
163 | kfree(midihdr1->data);
|
---|
164 | kfree(midihdr1);
|
---|
165 | kfree(midi_dev);
|
---|
166 | MOD_DEC_USE_COUNT;
|
---|
167 | return -ENODEV;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (file->f_mode & FMODE_WRITE) {
|
---|
172 | struct midi_openinfo dsCardMidiOpenInfo;
|
---|
173 |
|
---|
174 | dsCardMidiOpenInfo.refdata = (unsigned long) midi_dev;
|
---|
175 |
|
---|
176 | if (emu10k1_mpuout_open(card, &dsCardMidiOpenInfo)
|
---|
177 | != CTSTATUS_SUCCESS) {
|
---|
178 | ERROR();
|
---|
179 | kfree(midi_dev);
|
---|
180 | MOD_DEC_USE_COUNT;
|
---|
181 | return -ENODEV;
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | file->private_data = (void *) midi_dev;
|
---|
186 |
|
---|
187 | card->open_mode |= (file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE);
|
---|
188 |
|
---|
189 | up(&card->open_sem);
|
---|
190 |
|
---|
191 | return 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static int emu10k1_midi_release(struct inode *inode, struct file *file)
|
---|
195 | {
|
---|
196 | struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
|
---|
197 | struct emu10k1_card *card = midi_dev->card;
|
---|
198 |
|
---|
199 | DPF(2, "emu10k1_midi_release()\n");
|
---|
200 |
|
---|
201 | if (file->f_mode & FMODE_WRITE) {
|
---|
202 | if (!(file->f_flags & O_NONBLOCK)) {
|
---|
203 |
|
---|
204 | while (!signal_pending(current) && (card->mpuout->firstmidiq != NULL)) {
|
---|
205 | DPF(4, "Cannot close - buffers not empty\n");
|
---|
206 |
|
---|
207 | interruptible_sleep_on(&midi_dev->oWait);
|
---|
208 |
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | emu10k1_mpuout_close(card);
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (file->f_mode & FMODE_READ) {
|
---|
216 | struct midi_hdr *midihdr;
|
---|
217 |
|
---|
218 | if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
|
---|
219 | emu10k1_mpuin_stop(card);
|
---|
220 | midi_dev->mistate = MIDIIN_STATE_STOPPED;
|
---|
221 | }
|
---|
222 |
|
---|
223 | emu10k1_mpuin_reset(card);
|
---|
224 | emu10k1_mpuin_close(card);
|
---|
225 |
|
---|
226 | while (!list_empty(&midi_dev->mid_hdrs)) {
|
---|
227 | midihdr = list_entry(midi_dev->mid_hdrs.next, struct midi_hdr, list);
|
---|
228 |
|
---|
229 | list_del(midi_dev->mid_hdrs.next);
|
---|
230 | kfree(midihdr->data);
|
---|
231 | kfree(midihdr);
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | kfree(midi_dev);
|
---|
236 |
|
---|
237 | down(&card->open_sem);
|
---|
238 | card->open_mode &= ~((file->f_mode << FMODE_MIDI_SHIFT) & (FMODE_MIDI_READ | FMODE_MIDI_WRITE));
|
---|
239 | up(&card->open_sem);
|
---|
240 | wake_up_interruptible(&card->open_wait);
|
---|
241 |
|
---|
242 | MOD_DEC_USE_COUNT;
|
---|
243 |
|
---|
244 | return 0;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static ssize_t emu10k1_midi_read(struct file *file, char *buffer, size_t count, loff_t * pos)
|
---|
248 | {
|
---|
249 | struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
|
---|
250 | ssize_t ret = 0;
|
---|
251 | u16 cnt;
|
---|
252 | unsigned long flags;
|
---|
253 |
|
---|
254 | DPD(4, "emu10k1_midi_read(), count %x\n", (u32) count);
|
---|
255 |
|
---|
256 | if (pos != &file->f_pos)
|
---|
257 | return -ESPIPE;
|
---|
258 |
|
---|
259 | if (!access_ok(VERIFY_WRITE, buffer, count))
|
---|
260 | return -EFAULT;
|
---|
261 |
|
---|
262 | if (midi_dev->mistate == MIDIIN_STATE_STOPPED) {
|
---|
263 | if (emu10k1_mpuin_start(midi_dev->card)
|
---|
264 | != CTSTATUS_SUCCESS) {
|
---|
265 | ERROR();
|
---|
266 | return -EINVAL;
|
---|
267 | }
|
---|
268 |
|
---|
269 | midi_dev->mistate = MIDIIN_STATE_STARTED;
|
---|
270 | }
|
---|
271 |
|
---|
272 | while (count > 0) {
|
---|
273 | cnt = MIDIIN_BUFLEN - midi_dev->ird;
|
---|
274 |
|
---|
275 | spin_lock_irqsave(&midi_spinlock, flags);
|
---|
276 |
|
---|
277 | if (midi_dev->icnt < cnt)
|
---|
278 | cnt = midi_dev->icnt;
|
---|
279 |
|
---|
280 | spin_unlock_irqrestore(&midi_spinlock, flags);
|
---|
281 |
|
---|
282 | if (cnt > count)
|
---|
283 | cnt = count;
|
---|
284 |
|
---|
285 | if (cnt <= 0) {
|
---|
286 | if (file->f_flags & O_NONBLOCK)
|
---|
287 | return ret ? ret : -EAGAIN;
|
---|
288 | DPF(2, " Go to sleep...\n");
|
---|
289 |
|
---|
290 | interruptible_sleep_on(&midi_dev->iWait);
|
---|
291 |
|
---|
292 | if (signal_pending(current))
|
---|
293 | return ret ? ret : -ERESTARTSYS;
|
---|
294 |
|
---|
295 | continue;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (copy_to_user(buffer, midi_dev->iBuf + midi_dev->ird, cnt)) {
|
---|
299 | ERROR();
|
---|
300 | return ret ? ret : -EFAULT;
|
---|
301 | }
|
---|
302 |
|
---|
303 | midi_dev->ird += cnt;
|
---|
304 | midi_dev->ird %= MIDIIN_BUFLEN;
|
---|
305 |
|
---|
306 | spin_lock_irqsave(&midi_spinlock, flags);
|
---|
307 |
|
---|
308 | midi_dev->icnt -= cnt;
|
---|
309 |
|
---|
310 | spin_unlock_irqrestore(&midi_spinlock, flags);
|
---|
311 |
|
---|
312 | count -= cnt;
|
---|
313 | buffer += cnt;
|
---|
314 | ret += cnt;
|
---|
315 |
|
---|
316 | if (midi_dev->icnt == 0)
|
---|
317 | break;
|
---|
318 | }
|
---|
319 |
|
---|
320 | return ret;
|
---|
321 | }
|
---|
322 |
|
---|
323 | static ssize_t emu10k1_midi_write(struct file *file, const char *buffer, size_t count, loff_t * pos)
|
---|
324 | {
|
---|
325 | struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) file->private_data;
|
---|
326 | struct midi_hdr *midihdr;
|
---|
327 | ssize_t ret = 0;
|
---|
328 | unsigned long flags;
|
---|
329 |
|
---|
330 | DPD(4, "emu10k1_midi_write(), count=%x\n", (u32) count);
|
---|
331 |
|
---|
332 | if (pos != &file->f_pos)
|
---|
333 | return -ESPIPE;
|
---|
334 |
|
---|
335 | if (!access_ok(VERIFY_READ, buffer, count))
|
---|
336 | return -EFAULT;
|
---|
337 |
|
---|
338 | if ((midihdr = (struct midi_hdr *) kmalloc(sizeof(struct midi_hdr *), GFP_KERNEL)) == NULL)
|
---|
339 | return -EINVAL;
|
---|
340 |
|
---|
341 | midihdr->bufferlength = count;
|
---|
342 | midihdr->bytesrecorded = 0;
|
---|
343 | midihdr->flags = 0;
|
---|
344 |
|
---|
345 | if ((midihdr->data = (u8 *) kmalloc(count, GFP_KERNEL)) == NULL) {
|
---|
346 | ERROR();
|
---|
347 | kfree(midihdr);
|
---|
348 | return -EINVAL;
|
---|
349 | }
|
---|
350 |
|
---|
351 | if (copy_from_user(midihdr->data, buffer, count)) {
|
---|
352 | kfree(midihdr->data);
|
---|
353 | kfree(midihdr);
|
---|
354 | return ret ? ret : -EFAULT;
|
---|
355 | }
|
---|
356 |
|
---|
357 | spin_lock_irqsave(&midi_spinlock, flags);
|
---|
358 |
|
---|
359 | if (emu10k1_mpuout_add_buffer(midi_dev->card, midihdr) != CTSTATUS_SUCCESS) {
|
---|
360 | ERROR();
|
---|
361 | kfree(midihdr->data);
|
---|
362 | kfree(midihdr);
|
---|
363 | spin_unlock_irqrestore(&midi_spinlock, flags);
|
---|
364 | return -EINVAL;
|
---|
365 | }
|
---|
366 |
|
---|
367 | spin_unlock_irqrestore(&midi_spinlock, flags);
|
---|
368 |
|
---|
369 | return count;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static unsigned int emu10k1_midi_poll(struct file *file, struct poll_table_struct *wait)
|
---|
373 | {
|
---|
374 | DPF(4, "emu10k1_midi_poll() called\n");
|
---|
375 | return 0;
|
---|
376 | }
|
---|
377 |
|
---|
378 | int emu10k1_midi_callback(unsigned long msg, unsigned long refdata, unsigned long *pmsg)
|
---|
379 | {
|
---|
380 | struct emu10k1_mididevice *midi_dev = (struct emu10k1_mididevice *) refdata;
|
---|
381 | struct midi_hdr *midihdr = NULL;
|
---|
382 | unsigned long flags;
|
---|
383 | int i;
|
---|
384 |
|
---|
385 | DPF(4, "emu10k1_midi_callback()\n");
|
---|
386 |
|
---|
387 | spin_lock_irqsave(&midi_spinlock, flags);
|
---|
388 |
|
---|
389 | switch (msg) {
|
---|
390 | case ICARDMIDI_OUTLONGDATA:
|
---|
391 | midihdr = (struct midi_hdr *) pmsg[2];
|
---|
392 |
|
---|
393 | kfree(midihdr->data);
|
---|
394 | kfree(midihdr);
|
---|
395 | wake_up_interruptible(&midi_dev->oWait);
|
---|
396 |
|
---|
397 | break;
|
---|
398 |
|
---|
399 | case ICARDMIDI_INLONGDATA:
|
---|
400 | midihdr = (struct midi_hdr *) pmsg[2];
|
---|
401 |
|
---|
402 | for (i = 0; i < midihdr->bytesrecorded; i++) {
|
---|
403 | midi_dev->iBuf[midi_dev->iwr++] = midihdr->data[i];
|
---|
404 | midi_dev->iwr %= MIDIIN_BUFLEN;
|
---|
405 | }
|
---|
406 |
|
---|
407 | midi_dev->icnt += midihdr->bytesrecorded;
|
---|
408 |
|
---|
409 | if (midi_dev->mistate == MIDIIN_STATE_STARTED) {
|
---|
410 | init_midi_hdr(midihdr);
|
---|
411 | emu10k1_mpuin_add_buffer(midi_dev->card->mpuin, midihdr);
|
---|
412 | wake_up_interruptible(&midi_dev->iWait);
|
---|
413 | }
|
---|
414 | break;
|
---|
415 |
|
---|
416 | case ICARDMIDI_INDATA:
|
---|
417 | {
|
---|
418 | u8 *pBuf = (u8 *) & pmsg[1];
|
---|
419 | u16 bytesvalid = pmsg[2];
|
---|
420 |
|
---|
421 | for (i = 0; i < bytesvalid; i++) {
|
---|
422 | midi_dev->iBuf[midi_dev->iwr++] = pBuf[i];
|
---|
423 | midi_dev->iwr %= MIDIIN_BUFLEN;
|
---|
424 | }
|
---|
425 |
|
---|
426 | midi_dev->icnt += bytesvalid;
|
---|
427 | }
|
---|
428 |
|
---|
429 | wake_up_interruptible(&midi_dev->iWait);
|
---|
430 | break;
|
---|
431 |
|
---|
432 | default: /* Unknown message */
|
---|
433 | return CTSTATUS_ERROR;
|
---|
434 | }
|
---|
435 |
|
---|
436 | spin_unlock_irqrestore(&midi_spinlock, flags);
|
---|
437 |
|
---|
438 | return CTSTATUS_SUCCESS;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /* MIDI file operations */
|
---|
442 | #ifdef TARGET_OS2
|
---|
443 | //Hello!?! Use standard C!
|
---|
444 | struct file_operations emu10k1_midi_fops =
|
---|
445 | {
|
---|
446 | NULL,
|
---|
447 | &emu10k1_midi_read,
|
---|
448 | &emu10k1_midi_write,
|
---|
449 | NULL, /* readdir */
|
---|
450 | &emu10k1_midi_poll,
|
---|
451 | NULL, /* ioctl */
|
---|
452 | NULL, /* mmap */
|
---|
453 | &emu10k1_midi_open,
|
---|
454 | #if LINUX_VERSION_CODE >= 0x020100
|
---|
455 | NULL, /* flush */
|
---|
456 | #endif
|
---|
457 | &emu10k1_midi_release,
|
---|
458 | NULL, /* fsync */
|
---|
459 | NULL, /* fasync */
|
---|
460 | NULL, /* check_media_change */
|
---|
461 | #if LINUX_VERSION_CODE >= 0x020100
|
---|
462 | NULL, /* revalidate */
|
---|
463 | NULL /* lock */
|
---|
464 | #endif
|
---|
465 | };
|
---|
466 | #else
|
---|
467 | struct file_operations emu10k1_midi_fops = {
|
---|
468 | read:emu10k1_midi_read,
|
---|
469 | write:emu10k1_midi_write,
|
---|
470 | poll:emu10k1_midi_poll,
|
---|
471 | open:emu10k1_midi_open,
|
---|
472 | release:emu10k1_midi_release,
|
---|
473 | };
|
---|
474 | #endif
|
---|