4 * Device file manager for /dev/midi#
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
14 * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
16 #include <linux/stddef.h>
17 #include <linux/kmod.h>
18 #include <linux/spinlock.h>
21 #include "sound_config.h"
25 * Don't make MAX_QUEUE_SIZE larger than 4000
28 #define MAX_QUEUE_SIZE 4000
30 static wait_queue_head_t midi_sleeper
[MAX_MIDI_DEV
];
31 static wait_queue_head_t input_sleeper
[MAX_MIDI_DEV
];
36 unsigned char queue
[MAX_QUEUE_SIZE
];
41 long prech_timeout
; /*
42 * Timeout before the first ch
46 static struct midi_buf
*midi_out_buf
[MAX_MIDI_DEV
] = {NULL
};
47 static struct midi_buf
*midi_in_buf
[MAX_MIDI_DEV
] = {NULL
};
48 static struct midi_parms parms
[MAX_MIDI_DEV
];
50 static void midi_poll(unsigned long dummy
);
53 static DEFINE_TIMER(poll_timer
, midi_poll
, 0, 0);
55 static volatile int open_devs
;
56 static DEFINE_SPINLOCK(lock
);
58 #define DATA_AVAIL(q) (q->len)
59 #define SPACE_AVAIL(q) (MAX_QUEUE_SIZE - q->len)
61 #define QUEUE_BYTE(q, data) \
64 unsigned long flags; \
65 spin_lock_irqsave(&lock, flags); \
66 q->queue[q->tail] = (data); \
67 q->len++; q->tail = (q->tail+1) % MAX_QUEUE_SIZE; \
68 spin_unlock_irqrestore(&lock, flags); \
71 #define REMOVE_BYTE(q, data) \
74 unsigned long flags; \
75 spin_lock_irqsave(&lock, flags); \
76 data = q->queue[q->head]; \
77 q->len--; q->head = (q->head+1) % MAX_QUEUE_SIZE; \
78 spin_unlock_irqrestore(&lock, flags); \
81 static void drain_midi_queue(int dev
)
85 * Give the Midi driver time to drain its output queues
88 if (midi_devs
[dev
]->buffer_status
!= NULL
)
89 while (!signal_pending(current
) && midi_devs
[dev
]->buffer_status(dev
))
90 interruptible_sleep_on_timeout(&midi_sleeper
[dev
],
94 static void midi_input_intr(int dev
, unsigned char data
)
96 if (midi_in_buf
[dev
] == NULL
)
106 if (SPACE_AVAIL(midi_in_buf
[dev
])) {
107 QUEUE_BYTE(midi_in_buf
[dev
], data
);
108 wake_up(&input_sleeper
[dev
]);
112 static void midi_output_intr(int dev
)
119 static void midi_poll(unsigned long dummy
)
124 spin_lock_irqsave(&lock
, flags
);
127 for (dev
= 0; dev
< num_midis
; dev
++)
128 if (midi_devs
[dev
] != NULL
&& midi_out_buf
[dev
] != NULL
)
130 while (DATA_AVAIL(midi_out_buf
[dev
]))
133 int c
= midi_out_buf
[dev
]->queue
[midi_out_buf
[dev
]->head
];
135 spin_unlock_irqrestore(&lock
,flags
);/* Give some time to others */
136 ok
= midi_devs
[dev
]->outputc(dev
, c
);
137 spin_lock_irqsave(&lock
, flags
);
140 midi_out_buf
[dev
]->head
= (midi_out_buf
[dev
]->head
+ 1) % MAX_QUEUE_SIZE
;
141 midi_out_buf
[dev
]->len
--;
144 if (DATA_AVAIL(midi_out_buf
[dev
]) < 100)
145 wake_up(&midi_sleeper
[dev
]);
147 poll_timer
.expires
= (1) + jiffies
;
148 add_timer(&poll_timer
);
153 spin_unlock_irqrestore(&lock
, flags
);
156 int MIDIbuf_open(int dev
, struct file
*file
)
161 mode
= translate_mode(file
);
163 if (num_midis
> MAX_MIDI_DEV
)
165 printk(KERN_ERR
"midi: Too many midi interfaces\n");
166 num_midis
= MAX_MIDI_DEV
;
168 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
171 * Interrupts disabled. Be careful
174 module_put(midi_devs
[dev
]->owner
);
176 if ((err
= midi_devs
[dev
]->open(dev
, mode
,
177 midi_input_intr
, midi_output_intr
)) < 0)
180 parms
[dev
].prech_timeout
= MAX_SCHEDULE_TIMEOUT
;
181 midi_in_buf
[dev
] = vmalloc(sizeof(struct midi_buf
));
183 if (midi_in_buf
[dev
] == NULL
)
185 printk(KERN_WARNING
"midi: Can't allocate buffer\n");
186 midi_devs
[dev
]->close(dev
);
189 midi_in_buf
[dev
]->len
= midi_in_buf
[dev
]->head
= midi_in_buf
[dev
]->tail
= 0;
191 midi_out_buf
[dev
] = vmalloc(sizeof(struct midi_buf
));
193 if (midi_out_buf
[dev
] == NULL
)
195 printk(KERN_WARNING
"midi: Can't allocate buffer\n");
196 midi_devs
[dev
]->close(dev
);
197 vfree(midi_in_buf
[dev
]);
198 midi_in_buf
[dev
] = NULL
;
201 midi_out_buf
[dev
]->len
= midi_out_buf
[dev
]->head
= midi_out_buf
[dev
]->tail
= 0;
204 init_waitqueue_head(&midi_sleeper
[dev
]);
205 init_waitqueue_head(&input_sleeper
[dev
]);
207 if (open_devs
< 2) /* This was first open */
209 poll_timer
.expires
= 1 + jiffies
;
210 add_timer(&poll_timer
); /* Start polling */
215 void MIDIbuf_release(int dev
, struct file
*file
)
220 mode
= translate_mode(file
);
222 if (dev
< 0 || dev
>= num_midis
|| midi_devs
[dev
] == NULL
)
226 * Wait until the queue is empty
229 if (mode
!= OPEN_READ
)
231 midi_devs
[dev
]->outputc(dev
, 0xfe); /*
232 * Active sensing to shut the
236 while (!signal_pending(current
) && DATA_AVAIL(midi_out_buf
[dev
]))
237 interruptible_sleep_on(&midi_sleeper
[dev
]);
242 drain_midi_queue(dev
); /*
243 * Ensure the output queues are empty
247 midi_devs
[dev
]->close(dev
);
251 del_timer_sync(&poll_timer
);
252 vfree(midi_in_buf
[dev
]);
253 vfree(midi_out_buf
[dev
]);
254 midi_in_buf
[dev
] = NULL
;
255 midi_out_buf
[dev
] = NULL
;
257 module_put(midi_devs
[dev
]->owner
);
260 int MIDIbuf_write(int dev
, struct file
*file
, const char __user
*buf
, int count
)
263 unsigned char tmp_data
;
274 n
= SPACE_AVAIL(midi_out_buf
[dev
]);
280 if (file
->f_flags
& O_NONBLOCK
) {
285 interruptible_sleep_on(&midi_sleeper
[dev
]);
286 if (signal_pending(current
))
291 n
= SPACE_AVAIL(midi_out_buf
[dev
]);
296 for (i
= 0; i
< n
; i
++)
298 /* BROKE BROKE BROKE - CAN'T DO THIS WITH CLI !! */
299 /* yes, think the same, so I removed the cli() brackets
300 QUEUE_BYTE is protected against interrupts */
301 if (copy_from_user((char *) &tmp_data
, &(buf
)[c
], 1)) {
305 QUEUE_BYTE(midi_out_buf
[dev
], tmp_data
);
314 int MIDIbuf_read(int dev
, struct file
*file
, char __user
*buf
, int count
)
317 unsigned char tmp_data
;
321 if (!DATA_AVAIL(midi_in_buf
[dev
])) { /*
324 if (file
->f_flags
& O_NONBLOCK
) {
328 interruptible_sleep_on_timeout(&input_sleeper
[dev
],
329 parms
[dev
].prech_timeout
);
331 if (signal_pending(current
))
332 c
= -EINTR
; /* The user is getting restless */
334 if (c
== 0 && DATA_AVAIL(midi_in_buf
[dev
])) /*
338 n
= DATA_AVAIL(midi_in_buf
[dev
]);
346 REMOVE_BYTE(midi_in_buf
[dev
], tmp_data
);
347 fixit
= (char *) &tmp_data
;
348 /* BROKE BROKE BROKE */
349 /* yes removed the cli() brackets again
350 should q->len,tail&head be atomic_t? */
351 if (copy_to_user(&(buf
)[c
], fixit
, 1)) {
362 int MIDIbuf_ioctl(int dev
, struct file
*file
,
363 unsigned int cmd
, void __user
*arg
)
369 if (((cmd
>> 8) & 0xff) == 'C')
371 if (midi_devs
[dev
]->coproc
) /* Coprocessor ioctl */
372 return midi_devs
[dev
]->coproc
->ioctl(midi_devs
[dev
]->coproc
->devc
, cmd
, arg
, 0);
373 /* printk("/dev/midi%d: No coprocessor for this device\n", dev);*/
380 case SNDCTL_MIDI_PRETIME
:
381 if (get_user(val
, (int __user
*)arg
))
385 val
= (HZ
* val
) / 10;
386 parms
[dev
].prech_timeout
= val
;
387 return put_user(val
, (int __user
*)arg
);
390 if (!midi_devs
[dev
]->ioctl
)
392 return midi_devs
[dev
]->ioctl(dev
, cmd
, arg
);
397 /* No kernel lock - fine */
398 unsigned int MIDIbuf_poll(int dev
, struct file
*file
, poll_table
* wait
)
400 unsigned int mask
= 0;
405 poll_wait(file
, &input_sleeper
[dev
], wait
);
406 if (DATA_AVAIL(midi_in_buf
[dev
]))
407 mask
|= POLLIN
| POLLRDNORM
;
410 poll_wait(file
, &midi_sleeper
[dev
], wait
);
411 if (!SPACE_AVAIL(midi_out_buf
[dev
]))
412 mask
|= POLLOUT
| POLLWRNORM
;
418 int MIDIbuf_avail(int dev
)
420 if (midi_in_buf
[dev
])
421 return DATA_AVAIL (midi_in_buf
[dev
]);
424 EXPORT_SYMBOL(MIDIbuf_avail
);