Lines Matching +full:tx +full:- +full:output +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Routines for control of MPU-401 in UART mode
6 * MPU-401 supports UART mode which is not capable generate transmit
7 * interrupts thus output is done via polling. Without interrupt,
10 * 13-03-2003:
12 * are port and mmio. For other kind of I/O, set mpu->read and
13 * mpu->write to your own I/O functions.
28 MODULE_DESCRIPTION("Routines for control of MPU-401 in UART mode");
39 (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_RX_EMPTY))
41 (!(mpu->read(mpu, MPU401C(mpu)) & MPU401_TX_FULL))
72 for (; timeout > 0 && snd_mpu401_input_avail(mpu); timeout--)
73 mpu->read(mpu, MPU401D(mpu));
76 dev_err(mpu->rmidi->dev,
78 mpu->read(mpu, MPU401C(mpu)));
84 if (test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode) &&
85 test_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode)) {
86 guard(spinlock_irqsave)(&mpu->output_lock);
93 if (mpu->info_flags & MPU401_INFO_INPUT) {
94 guard(spinlock_irqsave)(&mpu->input_lock);
95 if (test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
100 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
101 /* ok. for better Tx performance try do some output
107 * snd_mpu401_uart_interrupt - generic MPU401-UART interrupt handler
111 * Processes the interrupt for MPU401-UART i/o.
128 * snd_mpu401_uart_interrupt_tx - generic MPU401-UART transmit irq handler
132 * Processes the interrupt for MPU401-UART output.
156 scoped_guard(spinlock_irqsave, &mpu->timer_lock) {
157 /*mpu->mode |= MPU401_MODE_TIMER;*/
158 mod_timer(&mpu->timer, 1 + jiffies);
160 if (mpu->rmidi)
169 guard(spinlock_irqsave)(&mpu->timer_lock);
170 if (mpu->timer_invoked == 0) {
171 timer_setup(&mpu->timer, snd_mpu401_uart_timer, 0);
172 mod_timer(&mpu->timer, 1 + jiffies);
174 mpu->timer_invoked |= input ? MPU401_MODE_INPUT_TIMER :
183 guard(spinlock_irqsave)(&mpu->timer_lock);
184 if (mpu->timer_invoked) {
185 mpu->timer_invoked &= input ? ~MPU401_MODE_INPUT_TIMER :
187 if (! mpu->timer_invoked)
188 timer_delete(&mpu->timer);
194 * return zero if successful, non-zero for some errors
202 guard(spinlock_irqsave)(&mpu->input_lock);
203 if (mpu->hardware != MPU401_HW_TRID4DWAVE) {
204 mpu->write(mpu, 0x00, MPU401D(mpu));
207 /* ok. standard MPU-401 initialization */
208 if (mpu->hardware != MPU401_HW_SB) {
210 !snd_mpu401_output_ready(mpu); timeout--)
214 dev_err(mpu->rmidi->dev,
215 "cmd: tx timeout (status = 0x%x)\n",
216 mpu->read(mpu, MPU401C(mpu)));
219 mpu->write(mpu, cmd, MPU401C(mpu));
220 if (ack && !(mpu->info_flags & MPU401_INFO_NO_ACK)) {
223 while (!ok && timeout-- > 0) {
225 if (mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
229 if (!ok && mpu->read(mpu, MPU401D(mpu)) == MPU401_ACK)
234 dev_err(mpu->rmidi->dev,
236 cmd, mpu->port,
237 mpu->read(mpu, MPU401C(mpu)),
238 mpu->read(mpu, MPU401D(mpu)));
247 return -EIO;
249 return -EIO;
254 * input/output open/close - protected by open_mutex in rawmidi.c
261 mpu = substream->rmidi->private_data;
262 if (mpu->open_input) {
263 err = mpu->open_input(mpu);
267 if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode)) {
271 mpu->substream_input = substream;
272 set_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
276 if (mpu->open_input && mpu->close_input)
277 mpu->close_input(mpu);
278 return -EIO;
286 mpu = substream->rmidi->private_data;
287 if (mpu->open_output) {
288 err = mpu->open_output(mpu);
292 if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode)) {
296 mpu->substream_output = substream;
297 set_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
301 if (mpu->open_output && mpu->close_output)
302 mpu->close_output(mpu);
303 return -EIO;
311 mpu = substream->rmidi->private_data;
312 clear_bit(MPU401_MODE_BIT_INPUT, &mpu->mode);
313 mpu->substream_input = NULL;
314 if (! test_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode))
316 if (mpu->close_input)
317 mpu->close_input(mpu);
319 return -EIO;
328 mpu = substream->rmidi->private_data;
329 clear_bit(MPU401_MODE_BIT_OUTPUT, &mpu->mode);
330 mpu->substream_output = NULL;
331 if (! test_bit(MPU401_MODE_BIT_INPUT, &mpu->mode))
333 if (mpu->close_output)
334 mpu->close_output(mpu);
336 return -EIO;
349 mpu = substream->rmidi->private_data;
352 &mpu->mode)) {
353 /* first time - flush FIFO */
354 while (max-- > 0)
355 mpu->read(mpu, MPU401D(mpu));
356 if (mpu->info_flags & MPU401_INFO_USE_TIMER)
361 guard(spinlock_irqsave)(&mpu->input_lock);
364 if (mpu->info_flags & MPU401_INFO_USE_TIMER)
366 clear_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode);
380 while (max-- > 0) {
383 byte = mpu->read(mpu, MPU401D(mpu));
384 if (test_bit(MPU401_MODE_BIT_INPUT_TRIGGER, &mpu->mode))
385 snd_rawmidi_receive(mpu->substream_input, &byte, 1);
390 * Tx FIFO sizes:
391 * CS4237B - 16 bytes
392 * AudioDrive ES1688 - 12 bytes
393 * S3 SonicVibes - 8 bytes
394 * SoundBlaster AWE 64 - 2 bytes (ugly hardware)
398 * write output pending bytes
407 if (snd_rawmidi_transmit_peek(mpu->substream_output,
411 * setting the output busy bit after each write.
415 break; /* Tx FIFO full - try again later */
416 mpu->write(mpu, byte, MPU401D(mpu));
417 snd_rawmidi_transmit_ack(mpu->substream_output, 1);
420 break; /* no other data - leave the tx loop */
422 } while (--max > 0);
426 * output trigger callback
433 mpu = substream->rmidi->private_data;
435 set_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
437 /* try to add the timer at each output trigger,
438 * since the output timer might have been removed in
441 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
444 /* output pending data */
445 guard(spinlock_irqsave)(&mpu->output_lock);
448 if (! (mpu->info_flags & MPU401_INFO_TX_IRQ))
450 clear_bit(MPU401_MODE_BIT_OUTPUT_TRIGGER, &mpu->mode);
474 struct snd_mpu401 *mpu = rmidi->private_data;
475 if (mpu->irq >= 0)
476 free_irq(mpu->irq, (void *) mpu);
477 release_and_free_resource(mpu->res);
482 * snd_mpu401_uart_new - create an MPU401-UART instance
484 * @device: the device index, zero-based
488 * @irq: the ISA irq number, -1 if not to be allocated
491 * Creates a new MPU-401 instance.
495 * cast from rawmidi->private_data (with struct snd_mpu401 magic-cast).
517 err = snd_rawmidi_new(card, "MPU-401U", device,
523 err = -ENOMEM;
526 rmidi->private_data = mpu;
527 rmidi->private_free = snd_mpu401_uart_free;
528 spin_lock_init(&mpu->input_lock);
529 spin_lock_init(&mpu->output_lock);
530 spin_lock_init(&mpu->timer_lock);
531 mpu->hardware = hardware;
532 mpu->irq = -1;
533 mpu->rmidi = rmidi;
536 mpu->res = request_region(port, res_size, "MPU401 UART");
537 if (!mpu->res) {
538 dev_err(rmidi->dev,
541 err = -EBUSY;
546 mpu->write = mpu401_write_mmio;
547 mpu->read = mpu401_read_mmio;
549 mpu->write = mpu401_write_port;
550 mpu->read = mpu401_read_port;
552 mpu->port = port;
554 mpu->cport = port + 2;
556 mpu->cport = port + 1;
560 dev_err(rmidi->dev,
562 err = -EBUSY;
568 mpu->info_flags = info_flags;
569 mpu->irq = irq;
570 if (card->shortname[0])
571 snprintf(rmidi->name, sizeof(rmidi->name), "%s MIDI",
572 card->shortname);
574 sprintf(rmidi->name, "MPU-401 MIDI %d-%d",card->number, device);
578 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
583 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
585 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;