18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * serial.c 48c2ecf20Sopenharmony_ci * Copyright (c) by Jaroslav Kysela <perex@perex.cz>, 58c2ecf20Sopenharmony_ci * Isaku Yamahata <yamahata@private.email.ne.jp>, 68c2ecf20Sopenharmony_ci * George Hansper <ghansper@apana.org.au>, 78c2ecf20Sopenharmony_ci * Hannu Savolainen 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * This code is based on the code from ALSA 0.5.9, but heavily rewritten. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * Sat Mar 31 17:27:57 PST 2001 tim.mann@compaq.com 128c2ecf20Sopenharmony_ci * Added support for the Midiator MS-124T and for the MS-124W in 138c2ecf20Sopenharmony_ci * Single Addressed (S/A) or Multiple Burst (M/B) mode, with 148c2ecf20Sopenharmony_ci * power derived either parasitically from the serial port or 158c2ecf20Sopenharmony_ci * from a separate power supply. 168c2ecf20Sopenharmony_ci * 178c2ecf20Sopenharmony_ci * More documentation can be found in serial-u16550.txt. 188c2ecf20Sopenharmony_ci */ 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <linux/init.h> 218c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 228c2ecf20Sopenharmony_ci#include <linux/err.h> 238c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 248c2ecf20Sopenharmony_ci#include <linux/slab.h> 258c2ecf20Sopenharmony_ci#include <linux/ioport.h> 268c2ecf20Sopenharmony_ci#include <linux/module.h> 278c2ecf20Sopenharmony_ci#include <linux/io.h> 288c2ecf20Sopenharmony_ci#include <sound/core.h> 298c2ecf20Sopenharmony_ci#include <sound/rawmidi.h> 308c2ecf20Sopenharmony_ci#include <sound/initval.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include <linux/serial_reg.h> 338c2ecf20Sopenharmony_ci#include <linux/jiffies.h> 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("MIDI serial u16550"); 368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 378c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{ALSA, MIDI serial u16550}}"); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_SOUNDCANVAS 0 /* Roland Soundcanvas; F5 NN selects part */ 408c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MS124T 1 /* Midiator MS-124T */ 418c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MS124W_SA 2 /* Midiator MS-124W in S/A mode */ 428c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MS124W_MB 3 /* Midiator MS-124W in M/B mode */ 438c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_GENERIC 4 /* Generic Interface */ 448c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MAX_ADAPTOR SNDRV_SERIAL_GENERIC 458c2ecf20Sopenharmony_cistatic const char * const adaptor_names[] = { 468c2ecf20Sopenharmony_ci "Soundcanvas", 478c2ecf20Sopenharmony_ci "MS-124T", 488c2ecf20Sopenharmony_ci "MS-124W S/A", 498c2ecf20Sopenharmony_ci "MS-124W M/B", 508c2ecf20Sopenharmony_ci "Generic" 518c2ecf20Sopenharmony_ci}; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_NORMALBUFF 0 /* Normal blocking buffer operation */ 548c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_DROPBUFF 1 /* Non-blocking discard operation */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 578c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 588c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE; /* Enable this card */ 598c2ecf20Sopenharmony_cistatic long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x3f8,0x2f8,0x3e8,0x2e8 */ 608c2ecf20Sopenharmony_cistatic int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 3,4,5,7,9,10,11,14,15 */ 618c2ecf20Sopenharmony_cistatic int speed[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 38400}; /* 9600,19200,38400,57600,115200 */ 628c2ecf20Sopenharmony_cistatic int base[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 115200}; /* baud base */ 638c2ecf20Sopenharmony_cistatic int outs[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */ 648c2ecf20Sopenharmony_cistatic int ins[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1}; /* 1 to 16 */ 658c2ecf20Sopenharmony_cistatic int adaptor[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = SNDRV_SERIAL_SOUNDCANVAS}; 668c2ecf20Sopenharmony_cistatic bool droponfull[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS -1)] = SNDRV_SERIAL_NORMALBUFF }; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444); 698c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for Serial MIDI."); 708c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444); 718c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for Serial MIDI."); 728c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444); 738c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable UART16550A chip."); 748c2ecf20Sopenharmony_cimodule_param_hw_array(port, long, ioport, NULL, 0444); 758c2ecf20Sopenharmony_ciMODULE_PARM_DESC(port, "Port # for UART16550A chip."); 768c2ecf20Sopenharmony_cimodule_param_hw_array(irq, int, irq, NULL, 0444); 778c2ecf20Sopenharmony_ciMODULE_PARM_DESC(irq, "IRQ # for UART16550A chip."); 788c2ecf20Sopenharmony_cimodule_param_array(speed, int, NULL, 0444); 798c2ecf20Sopenharmony_ciMODULE_PARM_DESC(speed, "Speed in bauds."); 808c2ecf20Sopenharmony_cimodule_param_array(base, int, NULL, 0444); 818c2ecf20Sopenharmony_ciMODULE_PARM_DESC(base, "Base for divisor in bauds."); 828c2ecf20Sopenharmony_cimodule_param_array(outs, int, NULL, 0444); 838c2ecf20Sopenharmony_ciMODULE_PARM_DESC(outs, "Number of MIDI outputs."); 848c2ecf20Sopenharmony_cimodule_param_array(ins, int, NULL, 0444); 858c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ins, "Number of MIDI inputs."); 868c2ecf20Sopenharmony_cimodule_param_array(droponfull, bool, NULL, 0444); 878c2ecf20Sopenharmony_ciMODULE_PARM_DESC(droponfull, "Flag to enable drop-on-full buffer mode"); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cimodule_param_array(adaptor, int, NULL, 0444); 908c2ecf20Sopenharmony_ciMODULE_PARM_DESC(adaptor, "Type of adaptor."); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/*#define SNDRV_SERIAL_MS124W_MB_NOCOMBO 1*/ /* Address outs as 0-3 instead of bitmap */ 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MAX_OUTS 16 /* max 64, min 16 */ 958c2ecf20Sopenharmony_ci#define SNDRV_SERIAL_MAX_INS 16 /* max 64, min 16 */ 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci#define TX_BUFF_SIZE (1<<15) /* Must be 2^n */ 988c2ecf20Sopenharmony_ci#define TX_BUFF_MASK (TX_BUFF_SIZE - 1) 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci#define SERIAL_MODE_NOT_OPENED (0) 1018c2ecf20Sopenharmony_ci#define SERIAL_MODE_INPUT_OPEN (1 << 0) 1028c2ecf20Sopenharmony_ci#define SERIAL_MODE_OUTPUT_OPEN (1 << 1) 1038c2ecf20Sopenharmony_ci#define SERIAL_MODE_INPUT_TRIGGERED (1 << 2) 1048c2ecf20Sopenharmony_ci#define SERIAL_MODE_OUTPUT_TRIGGERED (1 << 3) 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistruct snd_uart16550 { 1078c2ecf20Sopenharmony_ci struct snd_card *card; 1088c2ecf20Sopenharmony_ci struct snd_rawmidi *rmidi; 1098c2ecf20Sopenharmony_ci struct snd_rawmidi_substream *midi_output[SNDRV_SERIAL_MAX_OUTS]; 1108c2ecf20Sopenharmony_ci struct snd_rawmidi_substream *midi_input[SNDRV_SERIAL_MAX_INS]; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci int filemode; /* open status of file */ 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci spinlock_t open_lock; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci int irq; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci unsigned long base; 1198c2ecf20Sopenharmony_ci struct resource *res_base; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci unsigned int speed; 1228c2ecf20Sopenharmony_ci unsigned int speed_base; 1238c2ecf20Sopenharmony_ci unsigned char divisor; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci unsigned char old_divisor_lsb; 1268c2ecf20Sopenharmony_ci unsigned char old_divisor_msb; 1278c2ecf20Sopenharmony_ci unsigned char old_line_ctrl_reg; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* parameter for using of write loop */ 1308c2ecf20Sopenharmony_ci short int fifo_limit; /* used in uart16550 */ 1318c2ecf20Sopenharmony_ci short int fifo_count; /* used in uart16550 */ 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* type of adaptor */ 1348c2ecf20Sopenharmony_ci int adaptor; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci /* inputs */ 1378c2ecf20Sopenharmony_ci int prev_in; 1388c2ecf20Sopenharmony_ci unsigned char rstatus; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci /* outputs */ 1418c2ecf20Sopenharmony_ci int prev_out; 1428c2ecf20Sopenharmony_ci unsigned char prev_status[SNDRV_SERIAL_MAX_OUTS]; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* write buffer and its writing/reading position */ 1458c2ecf20Sopenharmony_ci unsigned char tx_buff[TX_BUFF_SIZE]; 1468c2ecf20Sopenharmony_ci int buff_in_count; 1478c2ecf20Sopenharmony_ci int buff_in; 1488c2ecf20Sopenharmony_ci int buff_out; 1498c2ecf20Sopenharmony_ci int drop_on_full; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* wait timer */ 1528c2ecf20Sopenharmony_ci unsigned int timer_running:1; 1538c2ecf20Sopenharmony_ci struct timer_list buffer_timer; 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci}; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic struct platform_device *devices[SNDRV_CARDS]; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic inline void snd_uart16550_add_timer(struct snd_uart16550 *uart) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci if (!uart->timer_running) { 1628c2ecf20Sopenharmony_ci /* timer 38600bps * 10bit * 16byte */ 1638c2ecf20Sopenharmony_ci mod_timer(&uart->buffer_timer, jiffies + (HZ + 255) / 256); 1648c2ecf20Sopenharmony_ci uart->timer_running = 1; 1658c2ecf20Sopenharmony_ci } 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_cistatic inline void snd_uart16550_del_timer(struct snd_uart16550 *uart) 1698c2ecf20Sopenharmony_ci{ 1708c2ecf20Sopenharmony_ci if (uart->timer_running) { 1718c2ecf20Sopenharmony_ci del_timer(&uart->buffer_timer); 1728c2ecf20Sopenharmony_ci uart->timer_running = 0; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci} 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci/* This macro is only used in snd_uart16550_io_loop */ 1778c2ecf20Sopenharmony_cistatic inline void snd_uart16550_buffer_output(struct snd_uart16550 *uart) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci unsigned short buff_out = uart->buff_out; 1808c2ecf20Sopenharmony_ci if (uart->buff_in_count > 0) { 1818c2ecf20Sopenharmony_ci outb(uart->tx_buff[buff_out], uart->base + UART_TX); 1828c2ecf20Sopenharmony_ci uart->fifo_count++; 1838c2ecf20Sopenharmony_ci buff_out++; 1848c2ecf20Sopenharmony_ci buff_out &= TX_BUFF_MASK; 1858c2ecf20Sopenharmony_ci uart->buff_out = buff_out; 1868c2ecf20Sopenharmony_ci uart->buff_in_count--; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* This loop should be called with interrupts disabled 1918c2ecf20Sopenharmony_ci * We don't want to interrupt this, 1928c2ecf20Sopenharmony_ci * as we're already handling an interrupt 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_cistatic void snd_uart16550_io_loop(struct snd_uart16550 * uart) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci unsigned char c, status; 1978c2ecf20Sopenharmony_ci int substream; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci /* recall previous stream */ 2008c2ecf20Sopenharmony_ci substream = uart->prev_in; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* Read Loop */ 2038c2ecf20Sopenharmony_ci while ((status = inb(uart->base + UART_LSR)) & UART_LSR_DR) { 2048c2ecf20Sopenharmony_ci /* while receive data ready */ 2058c2ecf20Sopenharmony_ci c = inb(uart->base + UART_RX); 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci /* keep track of last status byte */ 2088c2ecf20Sopenharmony_ci if (c & 0x80) 2098c2ecf20Sopenharmony_ci uart->rstatus = c; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci /* handle stream switch */ 2128c2ecf20Sopenharmony_ci if (uart->adaptor == SNDRV_SERIAL_GENERIC) { 2138c2ecf20Sopenharmony_ci if (uart->rstatus == 0xf5) { 2148c2ecf20Sopenharmony_ci if (c <= SNDRV_SERIAL_MAX_INS && c > 0) 2158c2ecf20Sopenharmony_ci substream = c - 1; 2168c2ecf20Sopenharmony_ci if (c != 0xf5) 2178c2ecf20Sopenharmony_ci /* prevent future bytes from being 2188c2ecf20Sopenharmony_ci interpreted as streams */ 2198c2ecf20Sopenharmony_ci uart->rstatus = 0; 2208c2ecf20Sopenharmony_ci } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) 2218c2ecf20Sopenharmony_ci && uart->midi_input[substream]) 2228c2ecf20Sopenharmony_ci snd_rawmidi_receive(uart->midi_input[substream], 2238c2ecf20Sopenharmony_ci &c, 1); 2248c2ecf20Sopenharmony_ci } else if ((uart->filemode & SERIAL_MODE_INPUT_OPEN) && 2258c2ecf20Sopenharmony_ci uart->midi_input[substream]) 2268c2ecf20Sopenharmony_ci snd_rawmidi_receive(uart->midi_input[substream], &c, 1); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci if (status & UART_LSR_OE) 2298c2ecf20Sopenharmony_ci snd_printk(KERN_WARNING 2308c2ecf20Sopenharmony_ci "%s: Overrun on device at 0x%lx\n", 2318c2ecf20Sopenharmony_ci uart->rmidi->name, uart->base); 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci /* remember the last stream */ 2358c2ecf20Sopenharmony_ci uart->prev_in = substream; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* no need of check SERIAL_MODE_OUTPUT_OPEN because if not, 2388c2ecf20Sopenharmony_ci buffer is never filled. */ 2398c2ecf20Sopenharmony_ci /* Check write status */ 2408c2ecf20Sopenharmony_ci if (status & UART_LSR_THRE) 2418c2ecf20Sopenharmony_ci uart->fifo_count = 0; 2428c2ecf20Sopenharmony_ci if (uart->adaptor == SNDRV_SERIAL_MS124W_SA 2438c2ecf20Sopenharmony_ci || uart->adaptor == SNDRV_SERIAL_GENERIC) { 2448c2ecf20Sopenharmony_ci /* Can't use FIFO, must send only when CTS is true */ 2458c2ecf20Sopenharmony_ci status = inb(uart->base + UART_MSR); 2468c2ecf20Sopenharmony_ci while (uart->fifo_count == 0 && (status & UART_MSR_CTS) && 2478c2ecf20Sopenharmony_ci uart->buff_in_count > 0) { 2488c2ecf20Sopenharmony_ci snd_uart16550_buffer_output(uart); 2498c2ecf20Sopenharmony_ci status = inb(uart->base + UART_MSR); 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci } else { 2528c2ecf20Sopenharmony_ci /* Write loop */ 2538c2ecf20Sopenharmony_ci while (uart->fifo_count < uart->fifo_limit /* Can we write ? */ 2548c2ecf20Sopenharmony_ci && uart->buff_in_count > 0) /* Do we want to? */ 2558c2ecf20Sopenharmony_ci snd_uart16550_buffer_output(uart); 2568c2ecf20Sopenharmony_ci } 2578c2ecf20Sopenharmony_ci if (uart->irq < 0 && uart->buff_in_count > 0) 2588c2ecf20Sopenharmony_ci snd_uart16550_add_timer(uart); 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci/* NOTES ON SERVICING INTERUPTS 2628c2ecf20Sopenharmony_ci * --------------------------- 2638c2ecf20Sopenharmony_ci * After receiving a interrupt, it is important to indicate to the UART that 2648c2ecf20Sopenharmony_ci * this has been done. 2658c2ecf20Sopenharmony_ci * For a Rx interrupt, this is done by reading the received byte. 2668c2ecf20Sopenharmony_ci * For a Tx interrupt this is done by either: 2678c2ecf20Sopenharmony_ci * a) Writing a byte 2688c2ecf20Sopenharmony_ci * b) Reading the IIR 2698c2ecf20Sopenharmony_ci * It is particularly important to read the IIR if a Tx interrupt is received 2708c2ecf20Sopenharmony_ci * when there is no data in tx_buff[], as in this case there no other 2718c2ecf20Sopenharmony_ci * indication that the interrupt has been serviced, and it remains outstanding 2728c2ecf20Sopenharmony_ci * indefinitely. This has the curious side effect that and no further interrupts 2738c2ecf20Sopenharmony_ci * will be generated from this device AT ALL!!. 2748c2ecf20Sopenharmony_ci * It is also desirable to clear outstanding interrupts when the device is 2758c2ecf20Sopenharmony_ci * opened/closed. 2768c2ecf20Sopenharmony_ci * 2778c2ecf20Sopenharmony_ci * 2788c2ecf20Sopenharmony_ci * Note that some devices need OUT2 to be set before they will generate 2798c2ecf20Sopenharmony_ci * interrupts at all. (Possibly tied to an internal pull-up on CTS?) 2808c2ecf20Sopenharmony_ci */ 2818c2ecf20Sopenharmony_cistatic irqreturn_t snd_uart16550_interrupt(int irq, void *dev_id) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci struct snd_uart16550 *uart; 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci uart = dev_id; 2868c2ecf20Sopenharmony_ci spin_lock(&uart->open_lock); 2878c2ecf20Sopenharmony_ci if (uart->filemode == SERIAL_MODE_NOT_OPENED) { 2888c2ecf20Sopenharmony_ci spin_unlock(&uart->open_lock); 2898c2ecf20Sopenharmony_ci return IRQ_NONE; 2908c2ecf20Sopenharmony_ci } 2918c2ecf20Sopenharmony_ci /* indicate to the UART that the interrupt has been serviced */ 2928c2ecf20Sopenharmony_ci inb(uart->base + UART_IIR); 2938c2ecf20Sopenharmony_ci snd_uart16550_io_loop(uart); 2948c2ecf20Sopenharmony_ci spin_unlock(&uart->open_lock); 2958c2ecf20Sopenharmony_ci return IRQ_HANDLED; 2968c2ecf20Sopenharmony_ci} 2978c2ecf20Sopenharmony_ci 2988c2ecf20Sopenharmony_ci/* When the polling mode, this function calls snd_uart16550_io_loop. */ 2998c2ecf20Sopenharmony_cistatic void snd_uart16550_buffer_timer(struct timer_list *t) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci unsigned long flags; 3028c2ecf20Sopenharmony_ci struct snd_uart16550 *uart; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci uart = from_timer(uart, t, buffer_timer); 3058c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 3068c2ecf20Sopenharmony_ci snd_uart16550_del_timer(uart); 3078c2ecf20Sopenharmony_ci snd_uart16550_io_loop(uart); 3088c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 3098c2ecf20Sopenharmony_ci} 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci/* 3128c2ecf20Sopenharmony_ci * this method probes, if an uart sits on given port 3138c2ecf20Sopenharmony_ci * return 0 if found 3148c2ecf20Sopenharmony_ci * return negative error if not found 3158c2ecf20Sopenharmony_ci */ 3168c2ecf20Sopenharmony_cistatic int snd_uart16550_detect(struct snd_uart16550 *uart) 3178c2ecf20Sopenharmony_ci{ 3188c2ecf20Sopenharmony_ci unsigned long io_base = uart->base; 3198c2ecf20Sopenharmony_ci int ok; 3208c2ecf20Sopenharmony_ci unsigned char c; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci /* Do some vague tests for the presence of the uart */ 3238c2ecf20Sopenharmony_ci if (io_base == 0 || io_base == SNDRV_AUTO_PORT) { 3248c2ecf20Sopenharmony_ci return -ENODEV; /* Not configured */ 3258c2ecf20Sopenharmony_ci } 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci uart->res_base = request_region(io_base, 8, "Serial MIDI"); 3288c2ecf20Sopenharmony_ci if (uart->res_base == NULL) { 3298c2ecf20Sopenharmony_ci snd_printk(KERN_ERR "u16550: can't grab port 0x%lx\n", io_base); 3308c2ecf20Sopenharmony_ci return -EBUSY; 3318c2ecf20Sopenharmony_ci } 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci /* uart detected unless one of the following tests should fail */ 3348c2ecf20Sopenharmony_ci ok = 1; 3358c2ecf20Sopenharmony_ci /* 8 data-bits, 1 stop-bit, parity off, DLAB = 0 */ 3368c2ecf20Sopenharmony_ci outb(UART_LCR_WLEN8, io_base + UART_LCR); /* Line Control Register */ 3378c2ecf20Sopenharmony_ci c = inb(io_base + UART_IER); 3388c2ecf20Sopenharmony_ci /* The top four bits of the IER should always == 0 */ 3398c2ecf20Sopenharmony_ci if ((c & 0xf0) != 0) 3408c2ecf20Sopenharmony_ci ok = 0; /* failed */ 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci outb(0xaa, io_base + UART_SCR); 3438c2ecf20Sopenharmony_ci /* Write arbitrary data into the scratch reg */ 3448c2ecf20Sopenharmony_ci c = inb(io_base + UART_SCR); 3458c2ecf20Sopenharmony_ci /* If it comes back, it's OK */ 3468c2ecf20Sopenharmony_ci if (c != 0xaa) 3478c2ecf20Sopenharmony_ci ok = 0; /* failed */ 3488c2ecf20Sopenharmony_ci 3498c2ecf20Sopenharmony_ci outb(0x55, io_base + UART_SCR); 3508c2ecf20Sopenharmony_ci /* Write arbitrary data into the scratch reg */ 3518c2ecf20Sopenharmony_ci c = inb(io_base + UART_SCR); 3528c2ecf20Sopenharmony_ci /* If it comes back, it's OK */ 3538c2ecf20Sopenharmony_ci if (c != 0x55) 3548c2ecf20Sopenharmony_ci ok = 0; /* failed */ 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci return ok; 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic void snd_uart16550_do_open(struct snd_uart16550 * uart) 3608c2ecf20Sopenharmony_ci{ 3618c2ecf20Sopenharmony_ci char byte; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci /* Initialize basic variables */ 3648c2ecf20Sopenharmony_ci uart->buff_in_count = 0; 3658c2ecf20Sopenharmony_ci uart->buff_in = 0; 3668c2ecf20Sopenharmony_ci uart->buff_out = 0; 3678c2ecf20Sopenharmony_ci uart->fifo_limit = 1; 3688c2ecf20Sopenharmony_ci uart->fifo_count = 0; 3698c2ecf20Sopenharmony_ci uart->timer_running = 0; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci outb(UART_FCR_ENABLE_FIFO /* Enable FIFO's (if available) */ 3728c2ecf20Sopenharmony_ci | UART_FCR_CLEAR_RCVR /* Clear receiver FIFO */ 3738c2ecf20Sopenharmony_ci | UART_FCR_CLEAR_XMIT /* Clear transmitter FIFO */ 3748c2ecf20Sopenharmony_ci | UART_FCR_TRIGGER_4 /* Set FIFO trigger at 4-bytes */ 3758c2ecf20Sopenharmony_ci /* NOTE: interrupt generated after T=(time)4-bytes 3768c2ecf20Sopenharmony_ci * if less than UART_FCR_TRIGGER bytes received 3778c2ecf20Sopenharmony_ci */ 3788c2ecf20Sopenharmony_ci ,uart->base + UART_FCR); /* FIFO Control Register */ 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci if ((inb(uart->base + UART_IIR) & 0xf0) == 0xc0) 3818c2ecf20Sopenharmony_ci uart->fifo_limit = 16; 3828c2ecf20Sopenharmony_ci if (uart->divisor != 0) { 3838c2ecf20Sopenharmony_ci uart->old_line_ctrl_reg = inb(uart->base + UART_LCR); 3848c2ecf20Sopenharmony_ci outb(UART_LCR_DLAB /* Divisor latch access bit */ 3858c2ecf20Sopenharmony_ci ,uart->base + UART_LCR); /* Line Control Register */ 3868c2ecf20Sopenharmony_ci uart->old_divisor_lsb = inb(uart->base + UART_DLL); 3878c2ecf20Sopenharmony_ci uart->old_divisor_msb = inb(uart->base + UART_DLM); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci outb(uart->divisor 3908c2ecf20Sopenharmony_ci ,uart->base + UART_DLL); /* Divisor Latch Low */ 3918c2ecf20Sopenharmony_ci outb(0 3928c2ecf20Sopenharmony_ci ,uart->base + UART_DLM); /* Divisor Latch High */ 3938c2ecf20Sopenharmony_ci /* DLAB is reset to 0 in next outb() */ 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci /* Set serial parameters (parity off, etc) */ 3968c2ecf20Sopenharmony_ci outb(UART_LCR_WLEN8 /* 8 data-bits */ 3978c2ecf20Sopenharmony_ci | 0 /* 1 stop-bit */ 3988c2ecf20Sopenharmony_ci | 0 /* parity off */ 3998c2ecf20Sopenharmony_ci | 0 /* DLAB = 0 */ 4008c2ecf20Sopenharmony_ci ,uart->base + UART_LCR); /* Line Control Register */ 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci switch (uart->adaptor) { 4038c2ecf20Sopenharmony_ci default: 4048c2ecf20Sopenharmony_ci outb(UART_MCR_RTS /* Set Request-To-Send line active */ 4058c2ecf20Sopenharmony_ci | UART_MCR_DTR /* Set Data-Terminal-Ready line active */ 4068c2ecf20Sopenharmony_ci | UART_MCR_OUT2 /* Set OUT2 - not always required, but when 4078c2ecf20Sopenharmony_ci * it is, it is ESSENTIAL for enabling interrupts 4088c2ecf20Sopenharmony_ci */ 4098c2ecf20Sopenharmony_ci ,uart->base + UART_MCR); /* Modem Control Register */ 4108c2ecf20Sopenharmony_ci break; 4118c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_SA: 4128c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_MB: 4138c2ecf20Sopenharmony_ci /* MS-124W can draw power from RTS and DTR if they 4148c2ecf20Sopenharmony_ci are in opposite states. */ 4158c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | (0&UART_MCR_DTR) | UART_MCR_OUT2, 4168c2ecf20Sopenharmony_ci uart->base + UART_MCR); 4178c2ecf20Sopenharmony_ci break; 4188c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124T: 4198c2ecf20Sopenharmony_ci /* MS-124T can draw power from RTS and/or DTR (preferably 4208c2ecf20Sopenharmony_ci both) if they are both asserted. */ 4218c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | UART_MCR_DTR | UART_MCR_OUT2, 4228c2ecf20Sopenharmony_ci uart->base + UART_MCR); 4238c2ecf20Sopenharmony_ci break; 4248c2ecf20Sopenharmony_ci } 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci if (uart->irq < 0) { 4278c2ecf20Sopenharmony_ci byte = (0 & UART_IER_RDI) /* Disable Receiver data interrupt */ 4288c2ecf20Sopenharmony_ci |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */ 4298c2ecf20Sopenharmony_ci ; 4308c2ecf20Sopenharmony_ci } else if (uart->adaptor == SNDRV_SERIAL_MS124W_SA) { 4318c2ecf20Sopenharmony_ci byte = UART_IER_RDI /* Enable Receiver data interrupt */ 4328c2ecf20Sopenharmony_ci | UART_IER_MSI /* Enable Modem status interrupt */ 4338c2ecf20Sopenharmony_ci ; 4348c2ecf20Sopenharmony_ci } else if (uart->adaptor == SNDRV_SERIAL_GENERIC) { 4358c2ecf20Sopenharmony_ci byte = UART_IER_RDI /* Enable Receiver data interrupt */ 4368c2ecf20Sopenharmony_ci | UART_IER_MSI /* Enable Modem status interrupt */ 4378c2ecf20Sopenharmony_ci | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */ 4388c2ecf20Sopenharmony_ci ; 4398c2ecf20Sopenharmony_ci } else { 4408c2ecf20Sopenharmony_ci byte = UART_IER_RDI /* Enable Receiver data interrupt */ 4418c2ecf20Sopenharmony_ci | UART_IER_THRI /* Enable Transmitter holding register empty interrupt */ 4428c2ecf20Sopenharmony_ci ; 4438c2ecf20Sopenharmony_ci } 4448c2ecf20Sopenharmony_ci outb(byte, uart->base + UART_IER); /* Interrupt enable Register */ 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci inb(uart->base + UART_LSR); /* Clear any pre-existing overrun indication */ 4478c2ecf20Sopenharmony_ci inb(uart->base + UART_IIR); /* Clear any pre-existing transmit interrupt */ 4488c2ecf20Sopenharmony_ci inb(uart->base + UART_RX); /* Clear any pre-existing receive interrupt */ 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_cistatic void snd_uart16550_do_close(struct snd_uart16550 * uart) 4528c2ecf20Sopenharmony_ci{ 4538c2ecf20Sopenharmony_ci if (uart->irq < 0) 4548c2ecf20Sopenharmony_ci snd_uart16550_del_timer(uart); 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci /* NOTE: may need to disable interrupts before de-registering out handler. 4578c2ecf20Sopenharmony_ci * For now, the consequences are harmless. 4588c2ecf20Sopenharmony_ci */ 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci outb((0 & UART_IER_RDI) /* Disable Receiver data interrupt */ 4618c2ecf20Sopenharmony_ci |(0 & UART_IER_THRI) /* Disable Transmitter holding register empty interrupt */ 4628c2ecf20Sopenharmony_ci ,uart->base + UART_IER); /* Interrupt enable Register */ 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci switch (uart->adaptor) { 4658c2ecf20Sopenharmony_ci default: 4668c2ecf20Sopenharmony_ci outb((0 & UART_MCR_RTS) /* Deactivate Request-To-Send line */ 4678c2ecf20Sopenharmony_ci |(0 & UART_MCR_DTR) /* Deactivate Data-Terminal-Ready line */ 4688c2ecf20Sopenharmony_ci |(0 & UART_MCR_OUT2) /* Deactivate OUT2 */ 4698c2ecf20Sopenharmony_ci ,uart->base + UART_MCR); /* Modem Control Register */ 4708c2ecf20Sopenharmony_ci break; 4718c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_SA: 4728c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_MB: 4738c2ecf20Sopenharmony_ci /* MS-124W can draw power from RTS and DTR if they 4748c2ecf20Sopenharmony_ci are in opposite states; leave it powered. */ 4758c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | (0&UART_MCR_DTR) | (0&UART_MCR_OUT2), 4768c2ecf20Sopenharmony_ci uart->base + UART_MCR); 4778c2ecf20Sopenharmony_ci break; 4788c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124T: 4798c2ecf20Sopenharmony_ci /* MS-124T can draw power from RTS and/or DTR (preferably 4808c2ecf20Sopenharmony_ci both) if they are both asserted; leave it powered. */ 4818c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | UART_MCR_DTR | (0&UART_MCR_OUT2), 4828c2ecf20Sopenharmony_ci uart->base + UART_MCR); 4838c2ecf20Sopenharmony_ci break; 4848c2ecf20Sopenharmony_ci } 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci inb(uart->base + UART_IIR); /* Clear any outstanding interrupts */ 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci /* Restore old divisor */ 4898c2ecf20Sopenharmony_ci if (uart->divisor != 0) { 4908c2ecf20Sopenharmony_ci outb(UART_LCR_DLAB /* Divisor latch access bit */ 4918c2ecf20Sopenharmony_ci ,uart->base + UART_LCR); /* Line Control Register */ 4928c2ecf20Sopenharmony_ci outb(uart->old_divisor_lsb 4938c2ecf20Sopenharmony_ci ,uart->base + UART_DLL); /* Divisor Latch Low */ 4948c2ecf20Sopenharmony_ci outb(uart->old_divisor_msb 4958c2ecf20Sopenharmony_ci ,uart->base + UART_DLM); /* Divisor Latch High */ 4968c2ecf20Sopenharmony_ci /* Restore old LCR (data bits, stop bits, parity, DLAB) */ 4978c2ecf20Sopenharmony_ci outb(uart->old_line_ctrl_reg 4988c2ecf20Sopenharmony_ci ,uart->base + UART_LCR); /* Line Control Register */ 4998c2ecf20Sopenharmony_ci } 5008c2ecf20Sopenharmony_ci} 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_cistatic int snd_uart16550_input_open(struct snd_rawmidi_substream *substream) 5038c2ecf20Sopenharmony_ci{ 5048c2ecf20Sopenharmony_ci unsigned long flags; 5058c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 5088c2ecf20Sopenharmony_ci if (uart->filemode == SERIAL_MODE_NOT_OPENED) 5098c2ecf20Sopenharmony_ci snd_uart16550_do_open(uart); 5108c2ecf20Sopenharmony_ci uart->filemode |= SERIAL_MODE_INPUT_OPEN; 5118c2ecf20Sopenharmony_ci uart->midi_input[substream->number] = substream; 5128c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 5138c2ecf20Sopenharmony_ci return 0; 5148c2ecf20Sopenharmony_ci} 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_cistatic int snd_uart16550_input_close(struct snd_rawmidi_substream *substream) 5178c2ecf20Sopenharmony_ci{ 5188c2ecf20Sopenharmony_ci unsigned long flags; 5198c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 5228c2ecf20Sopenharmony_ci uart->filemode &= ~SERIAL_MODE_INPUT_OPEN; 5238c2ecf20Sopenharmony_ci uart->midi_input[substream->number] = NULL; 5248c2ecf20Sopenharmony_ci if (uart->filemode == SERIAL_MODE_NOT_OPENED) 5258c2ecf20Sopenharmony_ci snd_uart16550_do_close(uart); 5268c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 5278c2ecf20Sopenharmony_ci return 0; 5288c2ecf20Sopenharmony_ci} 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_cistatic void snd_uart16550_input_trigger(struct snd_rawmidi_substream *substream, 5318c2ecf20Sopenharmony_ci int up) 5328c2ecf20Sopenharmony_ci{ 5338c2ecf20Sopenharmony_ci unsigned long flags; 5348c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 5378c2ecf20Sopenharmony_ci if (up) 5388c2ecf20Sopenharmony_ci uart->filemode |= SERIAL_MODE_INPUT_TRIGGERED; 5398c2ecf20Sopenharmony_ci else 5408c2ecf20Sopenharmony_ci uart->filemode &= ~SERIAL_MODE_INPUT_TRIGGERED; 5418c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 5428c2ecf20Sopenharmony_ci} 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_cistatic int snd_uart16550_output_open(struct snd_rawmidi_substream *substream) 5458c2ecf20Sopenharmony_ci{ 5468c2ecf20Sopenharmony_ci unsigned long flags; 5478c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 5508c2ecf20Sopenharmony_ci if (uart->filemode == SERIAL_MODE_NOT_OPENED) 5518c2ecf20Sopenharmony_ci snd_uart16550_do_open(uart); 5528c2ecf20Sopenharmony_ci uart->filemode |= SERIAL_MODE_OUTPUT_OPEN; 5538c2ecf20Sopenharmony_ci uart->midi_output[substream->number] = substream; 5548c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 5558c2ecf20Sopenharmony_ci return 0; 5568c2ecf20Sopenharmony_ci}; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_cistatic int snd_uart16550_output_close(struct snd_rawmidi_substream *substream) 5598c2ecf20Sopenharmony_ci{ 5608c2ecf20Sopenharmony_ci unsigned long flags; 5618c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 5648c2ecf20Sopenharmony_ci uart->filemode &= ~SERIAL_MODE_OUTPUT_OPEN; 5658c2ecf20Sopenharmony_ci uart->midi_output[substream->number] = NULL; 5668c2ecf20Sopenharmony_ci if (uart->filemode == SERIAL_MODE_NOT_OPENED) 5678c2ecf20Sopenharmony_ci snd_uart16550_do_close(uart); 5688c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 5698c2ecf20Sopenharmony_ci return 0; 5708c2ecf20Sopenharmony_ci}; 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_cistatic inline int snd_uart16550_buffer_can_write(struct snd_uart16550 *uart, 5738c2ecf20Sopenharmony_ci int Num) 5748c2ecf20Sopenharmony_ci{ 5758c2ecf20Sopenharmony_ci if (uart->buff_in_count + Num < TX_BUFF_SIZE) 5768c2ecf20Sopenharmony_ci return 1; 5778c2ecf20Sopenharmony_ci else 5788c2ecf20Sopenharmony_ci return 0; 5798c2ecf20Sopenharmony_ci} 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_cistatic inline int snd_uart16550_write_buffer(struct snd_uart16550 *uart, 5828c2ecf20Sopenharmony_ci unsigned char byte) 5838c2ecf20Sopenharmony_ci{ 5848c2ecf20Sopenharmony_ci unsigned short buff_in = uart->buff_in; 5858c2ecf20Sopenharmony_ci if (uart->buff_in_count < TX_BUFF_SIZE) { 5868c2ecf20Sopenharmony_ci uart->tx_buff[buff_in] = byte; 5878c2ecf20Sopenharmony_ci buff_in++; 5888c2ecf20Sopenharmony_ci buff_in &= TX_BUFF_MASK; 5898c2ecf20Sopenharmony_ci uart->buff_in = buff_in; 5908c2ecf20Sopenharmony_ci uart->buff_in_count++; 5918c2ecf20Sopenharmony_ci if (uart->irq < 0) /* polling mode */ 5928c2ecf20Sopenharmony_ci snd_uart16550_add_timer(uart); 5938c2ecf20Sopenharmony_ci return 1; 5948c2ecf20Sopenharmony_ci } else 5958c2ecf20Sopenharmony_ci return 0; 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_cistatic int snd_uart16550_output_byte(struct snd_uart16550 *uart, 5998c2ecf20Sopenharmony_ci struct snd_rawmidi_substream *substream, 6008c2ecf20Sopenharmony_ci unsigned char midi_byte) 6018c2ecf20Sopenharmony_ci{ 6028c2ecf20Sopenharmony_ci if (uart->buff_in_count == 0 /* Buffer empty? */ 6038c2ecf20Sopenharmony_ci && ((uart->adaptor != SNDRV_SERIAL_MS124W_SA && 6048c2ecf20Sopenharmony_ci uart->adaptor != SNDRV_SERIAL_GENERIC) || 6058c2ecf20Sopenharmony_ci (uart->fifo_count == 0 /* FIFO empty? */ 6068c2ecf20Sopenharmony_ci && (inb(uart->base + UART_MSR) & UART_MSR_CTS)))) { /* CTS? */ 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci /* Tx Buffer Empty - try to write immediately */ 6098c2ecf20Sopenharmony_ci if ((inb(uart->base + UART_LSR) & UART_LSR_THRE) != 0) { 6108c2ecf20Sopenharmony_ci /* Transmitter holding register (and Tx FIFO) empty */ 6118c2ecf20Sopenharmony_ci uart->fifo_count = 1; 6128c2ecf20Sopenharmony_ci outb(midi_byte, uart->base + UART_TX); 6138c2ecf20Sopenharmony_ci } else { 6148c2ecf20Sopenharmony_ci if (uart->fifo_count < uart->fifo_limit) { 6158c2ecf20Sopenharmony_ci uart->fifo_count++; 6168c2ecf20Sopenharmony_ci outb(midi_byte, uart->base + UART_TX); 6178c2ecf20Sopenharmony_ci } else { 6188c2ecf20Sopenharmony_ci /* Cannot write (buffer empty) - 6198c2ecf20Sopenharmony_ci * put char in buffer */ 6208c2ecf20Sopenharmony_ci snd_uart16550_write_buffer(uart, midi_byte); 6218c2ecf20Sopenharmony_ci } 6228c2ecf20Sopenharmony_ci } 6238c2ecf20Sopenharmony_ci } else { 6248c2ecf20Sopenharmony_ci if (!snd_uart16550_write_buffer(uart, midi_byte)) { 6258c2ecf20Sopenharmony_ci snd_printk(KERN_WARNING 6268c2ecf20Sopenharmony_ci "%s: Buffer overrun on device at 0x%lx\n", 6278c2ecf20Sopenharmony_ci uart->rmidi->name, uart->base); 6288c2ecf20Sopenharmony_ci return 0; 6298c2ecf20Sopenharmony_ci } 6308c2ecf20Sopenharmony_ci } 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci return 1; 6338c2ecf20Sopenharmony_ci} 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_cistatic void snd_uart16550_output_write(struct snd_rawmidi_substream *substream) 6368c2ecf20Sopenharmony_ci{ 6378c2ecf20Sopenharmony_ci unsigned long flags; 6388c2ecf20Sopenharmony_ci unsigned char midi_byte, addr_byte; 6398c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 6408c2ecf20Sopenharmony_ci char first; 6418c2ecf20Sopenharmony_ci static unsigned long lasttime = 0; 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci /* Interrupts are disabled during the updating of the tx_buff, 6448c2ecf20Sopenharmony_ci * since it is 'bad' to have two processes updating the same 6458c2ecf20Sopenharmony_ci * variables (ie buff_in & buff_out) 6468c2ecf20Sopenharmony_ci */ 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci if (uart->irq < 0) /* polling */ 6518c2ecf20Sopenharmony_ci snd_uart16550_io_loop(uart); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci if (uart->adaptor == SNDRV_SERIAL_MS124W_MB) { 6548c2ecf20Sopenharmony_ci while (1) { 6558c2ecf20Sopenharmony_ci /* buffer full? */ 6568c2ecf20Sopenharmony_ci /* in this mode we need two bytes of space */ 6578c2ecf20Sopenharmony_ci if (uart->buff_in_count > TX_BUFF_SIZE - 2) 6588c2ecf20Sopenharmony_ci break; 6598c2ecf20Sopenharmony_ci if (snd_rawmidi_transmit(substream, &midi_byte, 1) != 1) 6608c2ecf20Sopenharmony_ci break; 6618c2ecf20Sopenharmony_ci#ifdef SNDRV_SERIAL_MS124W_MB_NOCOMBO 6628c2ecf20Sopenharmony_ci /* select exactly one of the four ports */ 6638c2ecf20Sopenharmony_ci addr_byte = (1 << (substream->number + 4)) | 0x08; 6648c2ecf20Sopenharmony_ci#else 6658c2ecf20Sopenharmony_ci /* select any combination of the four ports */ 6668c2ecf20Sopenharmony_ci addr_byte = (substream->number << 4) | 0x08; 6678c2ecf20Sopenharmony_ci /* ...except none */ 6688c2ecf20Sopenharmony_ci if (addr_byte == 0x08) 6698c2ecf20Sopenharmony_ci addr_byte = 0xf8; 6708c2ecf20Sopenharmony_ci#endif 6718c2ecf20Sopenharmony_ci snd_uart16550_output_byte(uart, substream, addr_byte); 6728c2ecf20Sopenharmony_ci /* send midi byte */ 6738c2ecf20Sopenharmony_ci snd_uart16550_output_byte(uart, substream, midi_byte); 6748c2ecf20Sopenharmony_ci } 6758c2ecf20Sopenharmony_ci } else { 6768c2ecf20Sopenharmony_ci first = 0; 6778c2ecf20Sopenharmony_ci while (snd_rawmidi_transmit_peek(substream, &midi_byte, 1) == 1) { 6788c2ecf20Sopenharmony_ci /* Also send F5 after 3 seconds with no data 6798c2ecf20Sopenharmony_ci * to handle device disconnect */ 6808c2ecf20Sopenharmony_ci if (first == 0 && 6818c2ecf20Sopenharmony_ci (uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS || 6828c2ecf20Sopenharmony_ci uart->adaptor == SNDRV_SERIAL_GENERIC) && 6838c2ecf20Sopenharmony_ci (uart->prev_out != substream->number || 6848c2ecf20Sopenharmony_ci time_after(jiffies, lasttime + 3*HZ))) { 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci if (snd_uart16550_buffer_can_write(uart, 3)) { 6878c2ecf20Sopenharmony_ci /* Roland Soundcanvas part selection */ 6888c2ecf20Sopenharmony_ci /* If this substream of the data is 6898c2ecf20Sopenharmony_ci * different previous substream 6908c2ecf20Sopenharmony_ci * in this uart, send the change part 6918c2ecf20Sopenharmony_ci * event 6928c2ecf20Sopenharmony_ci */ 6938c2ecf20Sopenharmony_ci uart->prev_out = substream->number; 6948c2ecf20Sopenharmony_ci /* change part */ 6958c2ecf20Sopenharmony_ci snd_uart16550_output_byte(uart, substream, 6968c2ecf20Sopenharmony_ci 0xf5); 6978c2ecf20Sopenharmony_ci /* data */ 6988c2ecf20Sopenharmony_ci snd_uart16550_output_byte(uart, substream, 6998c2ecf20Sopenharmony_ci uart->prev_out + 1); 7008c2ecf20Sopenharmony_ci /* If midi_byte is a data byte, 7018c2ecf20Sopenharmony_ci * send the previous status byte */ 7028c2ecf20Sopenharmony_ci if (midi_byte < 0x80 && 7038c2ecf20Sopenharmony_ci uart->adaptor == SNDRV_SERIAL_SOUNDCANVAS) 7048c2ecf20Sopenharmony_ci snd_uart16550_output_byte(uart, substream, uart->prev_status[uart->prev_out]); 7058c2ecf20Sopenharmony_ci } else if (!uart->drop_on_full) 7068c2ecf20Sopenharmony_ci break; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci } 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci /* send midi byte */ 7118c2ecf20Sopenharmony_ci if (!snd_uart16550_output_byte(uart, substream, midi_byte) && 7128c2ecf20Sopenharmony_ci !uart->drop_on_full ) 7138c2ecf20Sopenharmony_ci break; 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci if (midi_byte >= 0x80 && midi_byte < 0xf0) 7168c2ecf20Sopenharmony_ci uart->prev_status[uart->prev_out] = midi_byte; 7178c2ecf20Sopenharmony_ci first = 1; 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci snd_rawmidi_transmit_ack( substream, 1 ); 7208c2ecf20Sopenharmony_ci } 7218c2ecf20Sopenharmony_ci lasttime = jiffies; 7228c2ecf20Sopenharmony_ci } 7238c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 7248c2ecf20Sopenharmony_ci} 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_cistatic void snd_uart16550_output_trigger(struct snd_rawmidi_substream *substream, 7278c2ecf20Sopenharmony_ci int up) 7288c2ecf20Sopenharmony_ci{ 7298c2ecf20Sopenharmony_ci unsigned long flags; 7308c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = substream->rmidi->private_data; 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci spin_lock_irqsave(&uart->open_lock, flags); 7338c2ecf20Sopenharmony_ci if (up) 7348c2ecf20Sopenharmony_ci uart->filemode |= SERIAL_MODE_OUTPUT_TRIGGERED; 7358c2ecf20Sopenharmony_ci else 7368c2ecf20Sopenharmony_ci uart->filemode &= ~SERIAL_MODE_OUTPUT_TRIGGERED; 7378c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&uart->open_lock, flags); 7388c2ecf20Sopenharmony_ci if (up) 7398c2ecf20Sopenharmony_ci snd_uart16550_output_write(substream); 7408c2ecf20Sopenharmony_ci} 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_cistatic const struct snd_rawmidi_ops snd_uart16550_output = 7438c2ecf20Sopenharmony_ci{ 7448c2ecf20Sopenharmony_ci .open = snd_uart16550_output_open, 7458c2ecf20Sopenharmony_ci .close = snd_uart16550_output_close, 7468c2ecf20Sopenharmony_ci .trigger = snd_uart16550_output_trigger, 7478c2ecf20Sopenharmony_ci}; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_cistatic const struct snd_rawmidi_ops snd_uart16550_input = 7508c2ecf20Sopenharmony_ci{ 7518c2ecf20Sopenharmony_ci .open = snd_uart16550_input_open, 7528c2ecf20Sopenharmony_ci .close = snd_uart16550_input_close, 7538c2ecf20Sopenharmony_ci .trigger = snd_uart16550_input_trigger, 7548c2ecf20Sopenharmony_ci}; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_cistatic int snd_uart16550_free(struct snd_uart16550 *uart) 7578c2ecf20Sopenharmony_ci{ 7588c2ecf20Sopenharmony_ci if (uart->irq >= 0) 7598c2ecf20Sopenharmony_ci free_irq(uart->irq, uart); 7608c2ecf20Sopenharmony_ci release_and_free_resource(uart->res_base); 7618c2ecf20Sopenharmony_ci kfree(uart); 7628c2ecf20Sopenharmony_ci return 0; 7638c2ecf20Sopenharmony_ci}; 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_cistatic int snd_uart16550_dev_free(struct snd_device *device) 7668c2ecf20Sopenharmony_ci{ 7678c2ecf20Sopenharmony_ci struct snd_uart16550 *uart = device->device_data; 7688c2ecf20Sopenharmony_ci return snd_uart16550_free(uart); 7698c2ecf20Sopenharmony_ci} 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_cistatic int snd_uart16550_create(struct snd_card *card, 7728c2ecf20Sopenharmony_ci unsigned long iobase, 7738c2ecf20Sopenharmony_ci int irq, 7748c2ecf20Sopenharmony_ci unsigned int speed, 7758c2ecf20Sopenharmony_ci unsigned int base, 7768c2ecf20Sopenharmony_ci int adaptor, 7778c2ecf20Sopenharmony_ci int droponfull, 7788c2ecf20Sopenharmony_ci struct snd_uart16550 **ruart) 7798c2ecf20Sopenharmony_ci{ 7808c2ecf20Sopenharmony_ci static const struct snd_device_ops ops = { 7818c2ecf20Sopenharmony_ci .dev_free = snd_uart16550_dev_free, 7828c2ecf20Sopenharmony_ci }; 7838c2ecf20Sopenharmony_ci struct snd_uart16550 *uart; 7848c2ecf20Sopenharmony_ci int err; 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci 7878c2ecf20Sopenharmony_ci if ((uart = kzalloc(sizeof(*uart), GFP_KERNEL)) == NULL) 7888c2ecf20Sopenharmony_ci return -ENOMEM; 7898c2ecf20Sopenharmony_ci uart->adaptor = adaptor; 7908c2ecf20Sopenharmony_ci uart->card = card; 7918c2ecf20Sopenharmony_ci spin_lock_init(&uart->open_lock); 7928c2ecf20Sopenharmony_ci uart->irq = -1; 7938c2ecf20Sopenharmony_ci uart->base = iobase; 7948c2ecf20Sopenharmony_ci uart->drop_on_full = droponfull; 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci if ((err = snd_uart16550_detect(uart)) <= 0) { 7978c2ecf20Sopenharmony_ci printk(KERN_ERR "no UART detected at 0x%lx\n", iobase); 7988c2ecf20Sopenharmony_ci snd_uart16550_free(uart); 7998c2ecf20Sopenharmony_ci return -ENODEV; 8008c2ecf20Sopenharmony_ci } 8018c2ecf20Sopenharmony_ci 8028c2ecf20Sopenharmony_ci if (irq >= 0 && irq != SNDRV_AUTO_IRQ) { 8038c2ecf20Sopenharmony_ci if (request_irq(irq, snd_uart16550_interrupt, 8048c2ecf20Sopenharmony_ci 0, "Serial MIDI", uart)) { 8058c2ecf20Sopenharmony_ci snd_printk(KERN_WARNING 8068c2ecf20Sopenharmony_ci "irq %d busy. Using Polling.\n", irq); 8078c2ecf20Sopenharmony_ci } else { 8088c2ecf20Sopenharmony_ci uart->irq = irq; 8098c2ecf20Sopenharmony_ci } 8108c2ecf20Sopenharmony_ci } 8118c2ecf20Sopenharmony_ci uart->divisor = base / speed; 8128c2ecf20Sopenharmony_ci uart->speed = base / (unsigned int)uart->divisor; 8138c2ecf20Sopenharmony_ci uart->speed_base = base; 8148c2ecf20Sopenharmony_ci uart->prev_out = -1; 8158c2ecf20Sopenharmony_ci uart->prev_in = 0; 8168c2ecf20Sopenharmony_ci uart->rstatus = 0; 8178c2ecf20Sopenharmony_ci memset(uart->prev_status, 0x80, sizeof(unsigned char) * SNDRV_SERIAL_MAX_OUTS); 8188c2ecf20Sopenharmony_ci timer_setup(&uart->buffer_timer, snd_uart16550_buffer_timer, 0); 8198c2ecf20Sopenharmony_ci uart->timer_running = 0; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci /* Register device */ 8228c2ecf20Sopenharmony_ci if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, uart, &ops)) < 0) { 8238c2ecf20Sopenharmony_ci snd_uart16550_free(uart); 8248c2ecf20Sopenharmony_ci return err; 8258c2ecf20Sopenharmony_ci } 8268c2ecf20Sopenharmony_ci 8278c2ecf20Sopenharmony_ci switch (uart->adaptor) { 8288c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_SA: 8298c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_MB: 8308c2ecf20Sopenharmony_ci /* MS-124W can draw power from RTS and DTR if they 8318c2ecf20Sopenharmony_ci are in opposite states. */ 8328c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | (0&UART_MCR_DTR), uart->base + UART_MCR); 8338c2ecf20Sopenharmony_ci break; 8348c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124T: 8358c2ecf20Sopenharmony_ci /* MS-124T can draw power from RTS and/or DTR (preferably 8368c2ecf20Sopenharmony_ci both) if they are asserted. */ 8378c2ecf20Sopenharmony_ci outb(UART_MCR_RTS | UART_MCR_DTR, uart->base + UART_MCR); 8388c2ecf20Sopenharmony_ci break; 8398c2ecf20Sopenharmony_ci default: 8408c2ecf20Sopenharmony_ci break; 8418c2ecf20Sopenharmony_ci } 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_ci if (ruart) 8448c2ecf20Sopenharmony_ci *ruart = uart; 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci return 0; 8478c2ecf20Sopenharmony_ci} 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_cistatic void snd_uart16550_substreams(struct snd_rawmidi_str *stream) 8508c2ecf20Sopenharmony_ci{ 8518c2ecf20Sopenharmony_ci struct snd_rawmidi_substream *substream; 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci list_for_each_entry(substream, &stream->substreams, list) { 8548c2ecf20Sopenharmony_ci sprintf(substream->name, "Serial MIDI %d", substream->number + 1); 8558c2ecf20Sopenharmony_ci } 8568c2ecf20Sopenharmony_ci} 8578c2ecf20Sopenharmony_ci 8588c2ecf20Sopenharmony_cistatic int snd_uart16550_rmidi(struct snd_uart16550 *uart, int device, 8598c2ecf20Sopenharmony_ci int outs, int ins, 8608c2ecf20Sopenharmony_ci struct snd_rawmidi **rmidi) 8618c2ecf20Sopenharmony_ci{ 8628c2ecf20Sopenharmony_ci struct snd_rawmidi *rrawmidi; 8638c2ecf20Sopenharmony_ci int err; 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci err = snd_rawmidi_new(uart->card, "UART Serial MIDI", device, 8668c2ecf20Sopenharmony_ci outs, ins, &rrawmidi); 8678c2ecf20Sopenharmony_ci if (err < 0) 8688c2ecf20Sopenharmony_ci return err; 8698c2ecf20Sopenharmony_ci snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_INPUT, 8708c2ecf20Sopenharmony_ci &snd_uart16550_input); 8718c2ecf20Sopenharmony_ci snd_rawmidi_set_ops(rrawmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, 8728c2ecf20Sopenharmony_ci &snd_uart16550_output); 8738c2ecf20Sopenharmony_ci strcpy(rrawmidi->name, "Serial MIDI"); 8748c2ecf20Sopenharmony_ci snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]); 8758c2ecf20Sopenharmony_ci snd_uart16550_substreams(&rrawmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]); 8768c2ecf20Sopenharmony_ci rrawmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | 8778c2ecf20Sopenharmony_ci SNDRV_RAWMIDI_INFO_INPUT | 8788c2ecf20Sopenharmony_ci SNDRV_RAWMIDI_INFO_DUPLEX; 8798c2ecf20Sopenharmony_ci rrawmidi->private_data = uart; 8808c2ecf20Sopenharmony_ci if (rmidi) 8818c2ecf20Sopenharmony_ci *rmidi = rrawmidi; 8828c2ecf20Sopenharmony_ci return 0; 8838c2ecf20Sopenharmony_ci} 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_cistatic int snd_serial_probe(struct platform_device *devptr) 8868c2ecf20Sopenharmony_ci{ 8878c2ecf20Sopenharmony_ci struct snd_card *card; 8888c2ecf20Sopenharmony_ci struct snd_uart16550 *uart; 8898c2ecf20Sopenharmony_ci int err; 8908c2ecf20Sopenharmony_ci int dev = devptr->id; 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci switch (adaptor[dev]) { 8938c2ecf20Sopenharmony_ci case SNDRV_SERIAL_SOUNDCANVAS: 8948c2ecf20Sopenharmony_ci ins[dev] = 1; 8958c2ecf20Sopenharmony_ci break; 8968c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124T: 8978c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_SA: 8988c2ecf20Sopenharmony_ci outs[dev] = 1; 8998c2ecf20Sopenharmony_ci ins[dev] = 1; 9008c2ecf20Sopenharmony_ci break; 9018c2ecf20Sopenharmony_ci case SNDRV_SERIAL_MS124W_MB: 9028c2ecf20Sopenharmony_ci outs[dev] = 16; 9038c2ecf20Sopenharmony_ci ins[dev] = 1; 9048c2ecf20Sopenharmony_ci break; 9058c2ecf20Sopenharmony_ci case SNDRV_SERIAL_GENERIC: 9068c2ecf20Sopenharmony_ci break; 9078c2ecf20Sopenharmony_ci default: 9088c2ecf20Sopenharmony_ci snd_printk(KERN_ERR 9098c2ecf20Sopenharmony_ci "Adaptor type is out of range 0-%d (%d)\n", 9108c2ecf20Sopenharmony_ci SNDRV_SERIAL_MAX_ADAPTOR, adaptor[dev]); 9118c2ecf20Sopenharmony_ci return -ENODEV; 9128c2ecf20Sopenharmony_ci } 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci if (outs[dev] < 1 || outs[dev] > SNDRV_SERIAL_MAX_OUTS) { 9158c2ecf20Sopenharmony_ci snd_printk(KERN_ERR 9168c2ecf20Sopenharmony_ci "Count of outputs is out of range 1-%d (%d)\n", 9178c2ecf20Sopenharmony_ci SNDRV_SERIAL_MAX_OUTS, outs[dev]); 9188c2ecf20Sopenharmony_ci return -ENODEV; 9198c2ecf20Sopenharmony_ci } 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci if (ins[dev] < 1 || ins[dev] > SNDRV_SERIAL_MAX_INS) { 9228c2ecf20Sopenharmony_ci snd_printk(KERN_ERR 9238c2ecf20Sopenharmony_ci "Count of inputs is out of range 1-%d (%d)\n", 9248c2ecf20Sopenharmony_ci SNDRV_SERIAL_MAX_INS, ins[dev]); 9258c2ecf20Sopenharmony_ci return -ENODEV; 9268c2ecf20Sopenharmony_ci } 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci err = snd_card_new(&devptr->dev, index[dev], id[dev], THIS_MODULE, 9298c2ecf20Sopenharmony_ci 0, &card); 9308c2ecf20Sopenharmony_ci if (err < 0) 9318c2ecf20Sopenharmony_ci return err; 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci strcpy(card->driver, "Serial"); 9348c2ecf20Sopenharmony_ci strcpy(card->shortname, "Serial MIDI (UART16550A)"); 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci if ((err = snd_uart16550_create(card, 9378c2ecf20Sopenharmony_ci port[dev], 9388c2ecf20Sopenharmony_ci irq[dev], 9398c2ecf20Sopenharmony_ci speed[dev], 9408c2ecf20Sopenharmony_ci base[dev], 9418c2ecf20Sopenharmony_ci adaptor[dev], 9428c2ecf20Sopenharmony_ci droponfull[dev], 9438c2ecf20Sopenharmony_ci &uart)) < 0) 9448c2ecf20Sopenharmony_ci goto _err; 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci err = snd_uart16550_rmidi(uart, 0, outs[dev], ins[dev], &uart->rmidi); 9478c2ecf20Sopenharmony_ci if (err < 0) 9488c2ecf20Sopenharmony_ci goto _err; 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci sprintf(card->longname, "%s [%s] at %#lx, irq %d", 9518c2ecf20Sopenharmony_ci card->shortname, 9528c2ecf20Sopenharmony_ci adaptor_names[uart->adaptor], 9538c2ecf20Sopenharmony_ci uart->base, 9548c2ecf20Sopenharmony_ci uart->irq); 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci if ((err = snd_card_register(card)) < 0) 9578c2ecf20Sopenharmony_ci goto _err; 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ci platform_set_drvdata(devptr, card); 9608c2ecf20Sopenharmony_ci return 0; 9618c2ecf20Sopenharmony_ci 9628c2ecf20Sopenharmony_ci _err: 9638c2ecf20Sopenharmony_ci snd_card_free(card); 9648c2ecf20Sopenharmony_ci return err; 9658c2ecf20Sopenharmony_ci} 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_cistatic int snd_serial_remove(struct platform_device *devptr) 9688c2ecf20Sopenharmony_ci{ 9698c2ecf20Sopenharmony_ci snd_card_free(platform_get_drvdata(devptr)); 9708c2ecf20Sopenharmony_ci return 0; 9718c2ecf20Sopenharmony_ci} 9728c2ecf20Sopenharmony_ci 9738c2ecf20Sopenharmony_ci#define SND_SERIAL_DRIVER "snd_serial_u16550" 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_cistatic struct platform_driver snd_serial_driver = { 9768c2ecf20Sopenharmony_ci .probe = snd_serial_probe, 9778c2ecf20Sopenharmony_ci .remove = snd_serial_remove, 9788c2ecf20Sopenharmony_ci .driver = { 9798c2ecf20Sopenharmony_ci .name = SND_SERIAL_DRIVER, 9808c2ecf20Sopenharmony_ci }, 9818c2ecf20Sopenharmony_ci}; 9828c2ecf20Sopenharmony_ci 9838c2ecf20Sopenharmony_cistatic void snd_serial_unregister_all(void) 9848c2ecf20Sopenharmony_ci{ 9858c2ecf20Sopenharmony_ci int i; 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(devices); ++i) 9888c2ecf20Sopenharmony_ci platform_device_unregister(devices[i]); 9898c2ecf20Sopenharmony_ci platform_driver_unregister(&snd_serial_driver); 9908c2ecf20Sopenharmony_ci} 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_cistatic int __init alsa_card_serial_init(void) 9938c2ecf20Sopenharmony_ci{ 9948c2ecf20Sopenharmony_ci int i, cards, err; 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci if ((err = platform_driver_register(&snd_serial_driver)) < 0) 9978c2ecf20Sopenharmony_ci return err; 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci cards = 0; 10008c2ecf20Sopenharmony_ci for (i = 0; i < SNDRV_CARDS; i++) { 10018c2ecf20Sopenharmony_ci struct platform_device *device; 10028c2ecf20Sopenharmony_ci if (! enable[i]) 10038c2ecf20Sopenharmony_ci continue; 10048c2ecf20Sopenharmony_ci device = platform_device_register_simple(SND_SERIAL_DRIVER, 10058c2ecf20Sopenharmony_ci i, NULL, 0); 10068c2ecf20Sopenharmony_ci if (IS_ERR(device)) 10078c2ecf20Sopenharmony_ci continue; 10088c2ecf20Sopenharmony_ci if (!platform_get_drvdata(device)) { 10098c2ecf20Sopenharmony_ci platform_device_unregister(device); 10108c2ecf20Sopenharmony_ci continue; 10118c2ecf20Sopenharmony_ci } 10128c2ecf20Sopenharmony_ci devices[i] = device; 10138c2ecf20Sopenharmony_ci cards++; 10148c2ecf20Sopenharmony_ci } 10158c2ecf20Sopenharmony_ci if (! cards) { 10168c2ecf20Sopenharmony_ci#ifdef MODULE 10178c2ecf20Sopenharmony_ci printk(KERN_ERR "serial midi soundcard not found or device busy\n"); 10188c2ecf20Sopenharmony_ci#endif 10198c2ecf20Sopenharmony_ci snd_serial_unregister_all(); 10208c2ecf20Sopenharmony_ci return -ENODEV; 10218c2ecf20Sopenharmony_ci } 10228c2ecf20Sopenharmony_ci return 0; 10238c2ecf20Sopenharmony_ci} 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_cistatic void __exit alsa_card_serial_exit(void) 10268c2ecf20Sopenharmony_ci{ 10278c2ecf20Sopenharmony_ci snd_serial_unregister_all(); 10288c2ecf20Sopenharmony_ci} 10298c2ecf20Sopenharmony_ci 10308c2ecf20Sopenharmony_cimodule_init(alsa_card_serial_init) 10318c2ecf20Sopenharmony_cimodule_exit(alsa_card_serial_exit) 1032