/* * sound/oss/uart6850.c * * * Copyright (C) by Hannu Savolainen 1993-1997 * * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL) * Version 2 (June 1991). See the "COPYING" file distributed with this software * for more info. * Extended by Alan Cox for Red Hat Software. Now a loadable MIDI driver. * 28/4/97 - (C) Copyright Alan Cox. Released under the GPL version 2. * * Alan Cox: Updated for new modular code. Removed snd_* irq handling. Now * uses native linux resources * Christoph Hellwig: Adapted to module_init/module_exit * Jeff Garzik: Made it work again, in theory * FIXME: If the request_irq() succeeds, the probe succeeds. Ug. * * Status: Testing required (no shit -jgarzik) * * */ #include #include #include #include /* Mon Nov 22 22:38:35 MET 1993 marco@driq.home.usn.nl: * added 6850 support, used with COVOX SoundMaster II and custom cards. */ #include "sound_config.h" static int uart6850_base = 0x330; static int *uart6850_osp; #define DATAPORT (uart6850_base) #define COMDPORT (uart6850_base+1) #define STATPORT (uart6850_base+1) static int uart6850_status(void) { return inb(STATPORT); } #define input_avail() (uart6850_status()&INPUT_AVAIL) #define output_ready() (uart6850_status()&OUTPUT_READY) static void uart6850_cmd(unsigned char cmd) { outb(cmd, COMDPORT); } static int uart6850_read(void) { return inb(DATAPORT); } static void uart6850_write(unsigned char byte) { outb(byte, DATAPORT); } #define OUTPUT_READY 0x02 /* Mask for data ready Bit */ #define INPUT_AVAIL 0x01 /* Mask for Data Send Ready Bit */ #define UART_RESET 0x95 #define UART_MODE_ON 0x03 static int uart6850_opened; static int uart6850_irq; static int uart6850_detected; static int my_dev; static DEFINE_SPINLOCK(lock); static void (*midi_input_intr) (int dev, unsigned char data); static void poll_uart6850(unsigned long dummy); static DEFINE_TIMER(uart6850_timer, poll_uart6850, 0, 0); static void uart6850_input_loop(void) { int count = 10; while (count) { /* * Not timed out */ if (input_avail()) { unsigned char c = uart6850_read(); count = 100; if (uart6850_opened & OPEN_READ) midi_input_intr(my_dev, c); } else { while (!input_avail() && count) count--; } } } static irqreturn_t m6850intr(int irq, void *dev_id) { if (input_avail()) uart6850_input_loop(); return IRQ_HANDLED; } /* * It looks like there is no input interrupts in the UART mode. Let's try * polling. */ static void poll_uart6850(unsigned long dummy) { unsigned long flags; if (!(uart6850_opened & OPEN_READ)) return; /* Device has been closed */ spin_lock_irqsave(&lock,flags); if (input_avail()) uart6850_input_loop(); uart6850_timer.expires = 1 + jiffies; add_timer(&uart6850_timer); /* * Come back later */ spin_unlock_irqrestore(&lock,flags); } static int uart6850_open(int dev, int mode, void (*input) (int dev, unsigned char data), void (*output) (int dev) ) { if (uart6850_opened) { /* printk("Midi6850: Midi busy\n");*/ return -EBUSY; }; uart6850_cmd(UART_RESET); uart6850_input_loop(); midi_input_intr = input; uart6850_opened = mode; poll_uart6850(0); /* * Enable input polling */ return 0; } static void uart6850_close(int dev) { uart6850_cmd(UART_MODE_ON); del_timer(&uart6850_timer); uart6850_opened = 0; } static int uart6850_out(int dev, unsigned char midi_byte) { int timeout; unsigned long flags; /* * Test for input since pending input seems to block the output. */ spin_lock_irqsave(&lock,flags); if (input_avail()) uart6850_input_loop(); spin_unlock_irqrestore(&lock,flags); /* * Sometimes it takes about 13000 loops before the output becomes ready * (After reset). Normally it takes just about 10 loops. */ for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /* * Wait */ if (!output_ready()) { printk(KERN_WARNING "Midi6850: Timeout\n"); return 0; } uart6850_write(midi_byte); return 1; } static inline int uart6850_command(int dev, unsigned char *midi_byte) { return 1; } static inline int uart6850_start_read(int dev) { return 0; } static inline int uart6850_end_read(int dev) { return 0; } static inline void uart6850_kick(int dev) { } static inline int uart6850_buffer_status(int dev) { return 0; /* * No data in buffers */ } #define MIDI_SYNTH_NAME "6850 UART Midi" #define MIDI_SYNTH_CAPS SYNTH_CAP_INPUT #include "midi_synth.h" static struct midi_operations uart6850_operations = { .owner = THIS_MODULE, .info = {"6850 UART", 0, 0, SNDCARD_UART6850}, .converter = &std_midi_synth, .in_info = {0}, .open = uart6850_open, .close = uart6850_close, .outputc = uart6850_out, .start_read = uart6850_start_read, .end_read = uart6850_end_read, .kick = uart6850_kick, .command = uart6850_command, .buffer_status = uart6850_buffer_status }; static void __init attach_uart6850(struct address_info *hw_config) { int ok, timeout; unsigned long flags; if (!uart6850_detected) return; if ((my_dev = sound_alloc_mididev()) == -1) { printk(KERN_INFO "uart6850: Too many midi devices detected\n"); return; } uart6850_base = hw_config->io_base; uart6850_osp = hw_config->osp; uart6850_irq = hw_config->irq; spin_lock_irqsave(&lock,flags); for (timeout = 30000; timeout > 0 && !output_ready(); timeout--); /* * Wait */ uart6850_cmd(UART_MODE_ON); ok = 1; spin_unlock_irqrestore(&lock,flags); conf_printf("6850 Midi Interface", hw_config); std_midi_synth.midi_dev = my_dev; hw_config->slots[4] = my_dev; midi_devs[my_dev] = &uart6850_operations; sequencer_init(); } static inline int reset_uart6850(void) { uart6850_read(); return 1; /* * OK */ } static int __init probe_uart6850(struct address_info *hw_config) { int ok; uart6850_osp = hw_config->osp; uart6850_base = hw_config->io_base; uart6850_irq = hw_config->irq; if (request_irq(uart6850_irq, m6850intr, 0, "MIDI6850", NULL) < 0) return 0; ok = reset_uart6850(); uart6850_detected = ok; return ok; } static void __exit unload_uart6850(struct address_info *hw_config) { free_irq(hw_config->irq, NULL); sound_unload_mididev(hw_config->slots[4]); } static struct address_info cfg_mpu; static int __initdata io = -1; static int __initdata irq = -1; module_param(io, int, 0); module_param(irq, int, 0); static int __init init_uart6850(void) { cfg_mpu.io_base = io; cfg_mpu.irq = irq; if (cfg_mpu.io_base == -1 || cfg_mpu.irq == -1) { printk(KERN_INFO "uart6850: irq and io must be set.\n"); return -EINVAL; } if (probe_uart6850(&cfg_mpu)) return -ENODEV; attach_uart6850(&cfg_mpu); return 0; } static void __exit cleanup_uart6850(void) { unload_uart6850(&cfg_mpu); } module_init(init_uart6850); module_exit(cleanup_uart6850); #ifndef MODULE static int __init setup_uart6850(char *str) { /* io, irq */ int ints[3]; str = get_options(str, ARRAY_SIZE(ints), ints); io = ints[1]; irq = ints[2]; return 1; } __setup("uart6850=", setup_uart6850); #endif MODULE_LICENSE("GPL"); ' href='#n91'>91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/*
 * ipmi_smi.h
 *
 * MontaVista IPMI system management interface
 *
 * Author: MontaVista Software, Inc.
 *         Corey Minyard <minyard@mvista.com>
 *         source@mvista.com
 *
 * Copyright 2002 MontaVista Software Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  675 Mass Ave, Cambridge, MA 02139, USA.
 */

#ifndef __LINUX_IPMI_SMI_H
#define __LINUX_IPMI_SMI_H

#include <linux/ipmi_msgdefs.h>
#include <linux/proc_fs.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/ipmi_smi.h>

/* This files describes the interface for IPMI system management interface
   drivers to bind into the IPMI message handler. */

/* Structure for the low-level drivers. */
typedef struct ipmi_smi *ipmi_smi_t;

/*
 * Messages to/from the lower layer.  The smi interface will take one
 * of these to send. After the send has occurred and a response has
 * been received, it will report this same data structure back up to
 * the upper layer.  If an error occurs, it should fill in the
 * response with an error code in the completion code location. When
 * asynchronous data is received, one of these is allocated, the
 * data_size is set to zero and the response holds the data from the
 * get message or get event command that the interface initiated.
 * Note that it is the interfaces responsibility to detect
 * asynchronous data and messages and request them from the
 * interface.
 */
struct ipmi_smi_msg {
	struct list_head link;

	long    msgid;
	void    *user_data;

	int           data_size;
	unsigned char data[IPMI_MAX_MSG_LENGTH];

	int           rsp_size;
	unsigned char rsp[IPMI_MAX_MSG_LENGTH];

	/* Will be called when the system is done with the message
	   (presumably to free it). */
	void (*done)(struct ipmi_smi_msg *msg);
};

struct ipmi_smi_handlers {
	struct module *owner;

	/* The low-level interface cannot start sending messages to
	   the upper layer until this function is called.  This may
	   not be NULL, the lower layer must take the interface from
	   this call. */
	int (*start_processing)(void       *send_info,
				ipmi_smi_t new_intf);

	/* Called to enqueue an SMI message to be sent.  This
	   operation is not allowed to fail.  If an error occurs, it
	   should report back the error in a received message.  It may
	   do this in the current call context, since no write locks
	   are held when this is run.  If the priority is > 0, the
	   message will go into a high-priority queue and be sent
	   first.  Otherwise, it goes into a normal-priority queue. */
	void (*sender)(void                *send_info,
		       struct ipmi_smi_msg *msg,
		       int                 priority);

	/* Called by the upper layer to request that we try to get
	   events from the BMC we are attached to. */
	void (*request_events)(void *send_info);

	/* Called when the interface should go into "run to
	   completion" mode.  If this call sets the value to true, the
	   interface should make sure that all messages are flushed
	   out and that none are pending, and any new requests are run
	   to completion immediately. */
	void (*set_run_to_completion)(void *send_info, int run_to_completion);

	/* Called to poll for work to do.  This is so upper layers can
	   poll for operations during things like crash dumps. */
	void (*poll)(void *send_info);

	/* Enable/disable firmware maintenance mode.  Note that this
	   is *not* the modes defined, this is simply an on/off
	   setting.  The message handler does the mode handling.  Note
	   that this is called from interrupt context, so it cannot
	   block. */
	void (*set_maintenance_mode)(void *send_info, int enable);

	/* Tell the handler that we are using it/not using it.  The
	   message handler get the modules that this handler belongs
	   to; this function lets the SMI claim any modules that it
	   uses.  These may be NULL if this is not required. */
	int (*inc_usecount)(void *send_info);
	void (*dec_usecount)(void *send_info);
};

struct ipmi_device_id {
	unsigned char device_id;
	unsigned char device_revision;
	unsigned char firmware_revision_1;
	unsigned char firmware_revision_2;
	unsigned char ipmi_version;
	unsigned char additional_device_support;
	unsigned int  manufacturer_id;
	unsigned int  product_id;
	unsigned char aux_firmware_revision[4];
	unsigned int  aux_firmware_revision_set : 1;
};

#define ipmi_version_major(v) ((v)->ipmi_version & 0xf)
#define ipmi_version_minor(v) ((v)->ipmi_version >> 4)

/* Take a pointer to a raw data buffer and a length and extract device
   id information from it.  The first byte of data must point to the
   netfn << 2, the data should be of the format:
      netfn << 2, cmd, completion code, data
   as normally comes from a device interface. */
static inline int ipmi_demangle_device_id(const unsigned char *data,
					  unsigned int data_len,
					  struct ipmi_device_id *id)
{
	if (data_len < 9)
		return -EINVAL;
	if (data[0] != IPMI_NETFN_APP_RESPONSE << 2 ||
	    data[1] != IPMI_GET_DEVICE_ID_CMD)
		/* Strange, didn't get the response we expected. */
		return -EINVAL;
	if (data[2] != 0)
		/* That's odd, it shouldn't be able to fail. */
		return -EINVAL;

	data += 3;
	data_len -= 3;
	id->device_id = data[0];
	id->device_revision = data[1];
	id->firmware_revision_1 = data[2];
	id->firmware_revision_2 = data[3];
	id->ipmi_version = data[4];
	id->additional_device_support = data[5];
	if (data_len >= 11) {
		id->manufacturer_id = (data[6] | (data[7] << 8) |
				       (data[8] << 16));
		id->product_id = data[9] | (data[10] << 8);
	} else {
		id->manufacturer_id = 0;
		id->product_id = 0;
	}
	if (data_len >= 15) {
		memcpy(id->aux_firmware_revision, data+11, 4);