/* -*- mode: c; c-basic-offset: 8 -*- */12/*3* MCA bus support functions for legacy (2.4) API.4*5* Legacy API means the API that operates in terms of MCA slot number6*7* (C) 2002 James Bottomley <[email protected]>8*9**-----------------------------------------------------------------------------10**11** This program is free software; you can redistribute it and/or modify12** it under the terms of the GNU General Public License as published by13** the Free Software Foundation; either version 2 of the License, or14** (at your option) any later version.15**16** This program is distributed in the hope that it will be useful,17** but WITHOUT ANY WARRANTY; without even the implied warranty of18** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19** GNU General Public License for more details.20**21** You should have received a copy of the GNU General Public License22** along with this program; if not, write to the Free Software23** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.24**25**-----------------------------------------------------------------------------26*/2728#include <linux/module.h>29#include <linux/device.h>30#include <linux/mca-legacy.h>31#include <asm/io.h>3233/* NOTE: This structure is stack allocated */34struct mca_find_adapter_info {35int id;36int slot;37struct mca_device *mca_dev;38};3940/* The purpose of this iterator is to loop over all the devices and41* find the one with the smallest slot number that's just greater than42* or equal to the required slot with a matching id */43static int mca_find_adapter_callback(struct device *dev, void *data)44{45struct mca_find_adapter_info *info = data;46struct mca_device *mca_dev = to_mca_device(dev);4748if(mca_dev->pos_id != info->id)49return 0;5051if(mca_dev->slot < info->slot)52return 0;5354if(!info->mca_dev || info->mca_dev->slot >= mca_dev->slot)55info->mca_dev = mca_dev;5657return 0;58}5960/**61* mca_find_adapter - scan for adapters62* @id: MCA identification to search for63* @start: starting slot64*65* Search the MCA configuration for adapters matching the 16bit66* ID given. The first time it should be called with start as zero67* and then further calls made passing the return value of the68* previous call until %MCA_NOTFOUND is returned.69*70* Disabled adapters are not reported.71*/7273int mca_find_adapter(int id, int start)74{75struct mca_find_adapter_info info;7677if(id == 0xffff)78return MCA_NOTFOUND;7980info.slot = start;81info.id = id;82info.mca_dev = NULL;8384for(;;) {85bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);8687if(info.mca_dev == NULL)88return MCA_NOTFOUND;8990if(info.mca_dev->status != MCA_ADAPTER_DISABLED)91break;9293/* OK, found adapter but it was disabled. Go around94* again, excluding the slot we just found */9596info.slot = info.mca_dev->slot + 1;97info.mca_dev = NULL;98}99100return info.mca_dev->slot;101}102EXPORT_SYMBOL(mca_find_adapter);103104/*--------------------------------------------------------------------*/105106/**107* mca_find_unused_adapter - scan for unused adapters108* @id: MCA identification to search for109* @start: starting slot110*111* Search the MCA configuration for adapters matching the 16bit112* ID given. The first time it should be called with start as zero113* and then further calls made passing the return value of the114* previous call until %MCA_NOTFOUND is returned.115*116* Adapters that have been claimed by drivers and those that117* are disabled are not reported. This function thus allows a driver118* to scan for further cards when some may already be driven.119*/120121int mca_find_unused_adapter(int id, int start)122{123struct mca_find_adapter_info info = { 0 };124125if (!MCA_bus || id == 0xffff)126return MCA_NOTFOUND;127128info.slot = start;129info.id = id;130info.mca_dev = NULL;131132for(;;) {133bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);134135if(info.mca_dev == NULL)136return MCA_NOTFOUND;137138if(info.mca_dev->status != MCA_ADAPTER_DISABLED139&& !info.mca_dev->driver_loaded)140break;141142/* OK, found adapter but it was disabled or already in143* use. Go around again, excluding the slot we just144* found */145146info.slot = info.mca_dev->slot + 1;147info.mca_dev = NULL;148}149150return info.mca_dev->slot;151}152EXPORT_SYMBOL(mca_find_unused_adapter);153154/* NOTE: stack allocated structure */155struct mca_find_device_by_slot_info {156int slot;157struct mca_device *mca_dev;158};159160static int mca_find_device_by_slot_callback(struct device *dev, void *data)161{162struct mca_find_device_by_slot_info *info = data;163struct mca_device *mca_dev = to_mca_device(dev);164165if(mca_dev->slot == info->slot)166info->mca_dev = mca_dev;167168return 0;169}170171struct mca_device *mca_find_device_by_slot(int slot)172{173struct mca_find_device_by_slot_info info;174175info.slot = slot;176info.mca_dev = NULL;177178bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_device_by_slot_callback);179180return info.mca_dev;181}182183/**184* mca_read_stored_pos - read POS register from boot data185* @slot: slot number to read from186* @reg: register to read from187*188* Fetch a POS value that was stored at boot time by the kernel189* when it scanned the MCA space. The register value is returned.190* Missing or invalid registers report 0.191*/192unsigned char mca_read_stored_pos(int slot, int reg)193{194struct mca_device *mca_dev = mca_find_device_by_slot(slot);195196if(!mca_dev)197return 0;198199return mca_device_read_stored_pos(mca_dev, reg);200}201EXPORT_SYMBOL(mca_read_stored_pos);202203204/**205* mca_read_pos - read POS register from card206* @slot: slot number to read from207* @reg: register to read from208*209* Fetch a POS value directly from the hardware to obtain the210* current value. This is much slower than mca_read_stored_pos and211* may not be invoked from interrupt context. It handles the212* deep magic required for onboard devices transparently.213*/214215unsigned char mca_read_pos(int slot, int reg)216{217struct mca_device *mca_dev = mca_find_device_by_slot(slot);218219if(!mca_dev)220return 0;221222return mca_device_read_pos(mca_dev, reg);223}224EXPORT_SYMBOL(mca_read_pos);225226227/**228* mca_write_pos - read POS register from card229* @slot: slot number to read from230* @reg: register to read from231* @byte: byte to write to the POS registers232*233* Store a POS value directly from the hardware. You should not234* normally need to use this function and should have a very good235* knowledge of MCA bus before you do so. Doing this wrongly can236* damage the hardware.237*238* This function may not be used from interrupt context.239*240* Note that this a technically a Bad Thing, as IBM tech stuff says241* you should only set POS values through their utilities.242* However, some devices such as the 3c523 recommend that you write243* back some data to make sure the configuration is consistent.244* I'd say that IBM is right, but I like my drivers to work.245*246* This function can't do checks to see if multiple devices end up247* with the same resources, so you might see magic smoke if someone248* screws up.249*/250251void mca_write_pos(int slot, int reg, unsigned char byte)252{253struct mca_device *mca_dev = mca_find_device_by_slot(slot);254255if(!mca_dev)256return;257258mca_device_write_pos(mca_dev, reg, byte);259}260EXPORT_SYMBOL(mca_write_pos);261262/**263* mca_set_adapter_name - Set the description of the card264* @slot: slot to name265* @name: text string for the namen266*267* This function sets the name reported via /proc for this268* adapter slot. This is for user information only. Setting a269* name deletes any previous name.270*/271272void mca_set_adapter_name(int slot, char* name)273{274struct mca_device *mca_dev = mca_find_device_by_slot(slot);275276if(!mca_dev)277return;278279mca_device_set_name(mca_dev, name);280}281EXPORT_SYMBOL(mca_set_adapter_name);282283/**284* mca_mark_as_used - claim an MCA device285* @slot: slot to claim286* FIXME: should we make this threadsafe287*288* Claim an MCA slot for a device driver. If the289* slot is already taken the function returns 1,290* if it is not taken it is claimed and 0 is291* returned.292*/293294int mca_mark_as_used(int slot)295{296struct mca_device *mca_dev = mca_find_device_by_slot(slot);297298if(!mca_dev)299/* FIXME: this is actually a severe error */300return 1;301302if(mca_device_claimed(mca_dev))303return 1;304305mca_device_set_claim(mca_dev, 1);306307return 0;308}309EXPORT_SYMBOL(mca_mark_as_used);310311/**312* mca_mark_as_unused - release an MCA device313* @slot: slot to claim314*315* Release the slot for other drives to use.316*/317318void mca_mark_as_unused(int slot)319{320struct mca_device *mca_dev = mca_find_device_by_slot(slot);321322if(!mca_dev)323return;324325mca_device_set_claim(mca_dev, 0);326}327EXPORT_SYMBOL(mca_mark_as_unused);328329330331