ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mca / mca-legacy.c
1 /* -*- mode: c; c-basic-offset: 8 -*- */
2
3 /*
4  * MCA bus support functions for legacy (2.4) API.
5  *
6  * Legacy API means the API that operates in terms of MCA slot number
7  *
8  * (C) 2002 James Bottomley <James.Bottomley@HansenPartnership.com>
9  *
10 **-----------------------------------------------------------------------------
11 **  
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License as published by
14 **  the Free Software Foundation; either version 2 of the License, or
15 **  (at your option) any later version.
16 **
17 **  This program is distributed in the hope that it will be useful,
18 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 **  GNU General Public License for more details.
21 **
22 **  You should have received a copy of the GNU General Public License
23 **  along with this program; if not, write to the Free Software
24 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 **
26 **-----------------------------------------------------------------------------
27  */
28
29 #include <linux/module.h>
30 #include <linux/device.h>
31 #include <linux/mca-legacy.h>
32 #include <asm/io.h>
33
34 /* NOTE: This structure is stack allocated */
35 struct mca_find_adapter_info {
36         int                     id;
37         int                     slot;
38         struct mca_device       *mca_dev;
39 };
40
41 /* The purpose of this iterator is to loop over all the devices and
42  * find the one with the smallest slot number that's just greater than
43  * or equal to the required slot with a matching id */
44 static int mca_find_adapter_callback(struct device *dev, void *data)
45 {
46         struct mca_find_adapter_info *info = data;
47         struct mca_device *mca_dev = to_mca_device(dev);
48
49         if(mca_dev->pos_id != info->id)
50                 return 0;
51
52         if(mca_dev->slot < info->slot)
53                 return 0;
54
55         if(!info->mca_dev || info->mca_dev->slot >= mca_dev->slot)
56                 info->mca_dev = mca_dev;
57
58         return 0;
59 }
60
61 /**
62  *      mca_find_adapter - scan for adapters
63  *      @id:    MCA identification to search for
64  *      @start: starting slot
65  *
66  *      Search the MCA configuration for adapters matching the 16bit
67  *      ID given. The first time it should be called with start as zero
68  *      and then further calls made passing the return value of the
69  *      previous call until %MCA_NOTFOUND is returned.
70  *
71  *      Disabled adapters are not reported.
72  */
73
74 int mca_find_adapter(int id, int start)
75 {
76         struct mca_find_adapter_info info;
77
78         if(id == 0xffff)
79                 return MCA_NOTFOUND;
80
81         info.slot = start;
82         info.id = id;
83         info.mca_dev = NULL;
84
85         for(;;) {
86                 bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);
87
88                 if(info.mca_dev == NULL)
89                         return MCA_NOTFOUND;
90
91                 if(info.mca_dev->status != MCA_ADAPTER_DISABLED)
92                         break;
93
94                 /* OK, found adapter but it was disabled.  Go around
95                  * again, excluding the slot we just found */
96
97                 info.slot = info.mca_dev->slot + 1;
98                 info.mca_dev = NULL;
99         }
100                 
101         return info.mca_dev->slot;
102 }
103 EXPORT_SYMBOL(mca_find_adapter);
104
105 /*--------------------------------------------------------------------*/
106
107 /**
108  *      mca_find_unused_adapter - scan for unused adapters
109  *      @id:    MCA identification to search for
110  *      @start: starting slot
111  *
112  *      Search the MCA configuration for adapters matching the 16bit
113  *      ID given. The first time it should be called with start as zero
114  *      and then further calls made passing the return value of the
115  *      previous call until %MCA_NOTFOUND is returned.
116  *
117  *      Adapters that have been claimed by drivers and those that
118  *      are disabled are not reported. This function thus allows a driver
119  *      to scan for further cards when some may already be driven.
120  */
121
122 int mca_find_unused_adapter(int id, int start)
123 {
124         struct mca_find_adapter_info info = { 0 };
125
126         if (!MCA_bus || id == 0xffff)
127                 return MCA_NOTFOUND;
128
129         info.slot = start;
130         info.id = id;
131         info.mca_dev = NULL;
132
133         for(;;) {
134                 bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);
135
136                 if(info.mca_dev == NULL)
137                         return MCA_NOTFOUND;
138
139                 if(info.mca_dev->status != MCA_ADAPTER_DISABLED
140                    && !info.mca_dev->driver_loaded)
141                         break;
142
143                 /* OK, found adapter but it was disabled or already in
144                  * use.  Go around again, excluding the slot we just
145                  * found */
146
147                 info.slot = info.mca_dev->slot + 1;
148                 info.mca_dev = NULL;
149         }
150                 
151         return info.mca_dev->slot;
152 }
153 EXPORT_SYMBOL(mca_find_unused_adapter);
154
155 /* NOTE: stack allocated structure */
156 struct mca_find_device_by_slot_info {
157         int                     slot;
158         struct mca_device       *mca_dev;
159 };
160
161 static int mca_find_device_by_slot_callback(struct device *dev, void *data)
162 {
163         struct mca_find_device_by_slot_info *info = data;
164         struct mca_device *mca_dev = to_mca_device(dev);
165
166         if(mca_dev->slot == info->slot)
167                 info->mca_dev = mca_dev;
168
169         return 0;
170 }
171
172 struct mca_device *mca_find_device_by_slot(int slot)
173 {
174         struct mca_find_device_by_slot_info info;
175
176         info.slot = slot;
177         info.mca_dev = NULL;
178
179         bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_device_by_slot_callback);
180
181         return info.mca_dev;
182 }
183 EXPORT_SYMBOL(mca_find_device_by_slot);
184
185 /**
186  *      mca_read_stored_pos - read POS register from boot data
187  *      @slot: slot number to read from
188  *      @reg:  register to read from
189  *
190  *      Fetch a POS value that was stored at boot time by the kernel
191  *      when it scanned the MCA space. The register value is returned.
192  *      Missing or invalid registers report 0.
193  */
194 unsigned char mca_read_stored_pos(int slot, int reg)
195 {
196         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
197
198         if(!mca_dev)
199                 return 0;
200
201         return mca_device_read_stored_pos(mca_dev, reg);
202 }
203 EXPORT_SYMBOL(mca_read_stored_pos);
204
205
206 /**
207  *      mca_read_pos - read POS register from card
208  *      @slot: slot number to read from
209  *      @reg:  register to read from
210  *
211  *      Fetch a POS value directly from the hardware to obtain the
212  *      current value. This is much slower than mca_read_stored_pos and
213  *      may not be invoked from interrupt context. It handles the
214  *      deep magic required for onboard devices transparently.
215  */
216
217 unsigned char mca_read_pos(int slot, int reg)
218 {
219         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
220
221         if(!mca_dev)
222                 return 0;
223
224         return mca_device_read_pos(mca_dev, reg);
225 }
226 EXPORT_SYMBOL(mca_read_pos);
227
228                 
229 /**
230  *      mca_write_pos - read POS register from card
231  *      @slot: slot number to read from
232  *      @reg:  register to read from
233  *      @byte: byte to write to the POS registers
234  *
235  *      Store a POS value directly from the hardware. You should not
236  *      normally need to use this function and should have a very good
237  *      knowledge of MCA bus before you do so. Doing this wrongly can
238  *      damage the hardware.
239  *
240  *      This function may not be used from interrupt context.
241  *
242  *      Note that this a technically a Bad Thing, as IBM tech stuff says
243  *      you should only set POS values through their utilities.
244  *      However, some devices such as the 3c523 recommend that you write
245  *      back some data to make sure the configuration is consistent.
246  *      I'd say that IBM is right, but I like my drivers to work.
247  *
248  *      This function can't do checks to see if multiple devices end up
249  *      with the same resources, so you might see magic smoke if someone
250  *      screws up.
251  */
252
253 void mca_write_pos(int slot, int reg, unsigned char byte)
254 {
255         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
256
257         if(!mca_dev)
258                 return;
259
260         mca_device_write_pos(mca_dev, reg, byte);
261 }
262 EXPORT_SYMBOL(mca_write_pos);
263
264 /**
265  *      mca_set_adapter_name - Set the description of the card
266  *      @slot: slot to name
267  *      @name: text string for the namen
268  *
269  *      This function sets the name reported via /proc for this
270  *      adapter slot. This is for user information only. Setting a
271  *      name deletes any previous name.
272  */
273
274 void mca_set_adapter_name(int slot, char* name)
275 {
276         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
277
278         if(!mca_dev)
279                 return;
280
281         mca_device_set_name(mca_dev, name);
282 }
283 EXPORT_SYMBOL(mca_set_adapter_name);
284
285 /**
286  *      mca_get_adapter_name - get the adapter description
287  *      @slot:  slot to query
288  *
289  *      Return the adapter description if set. If it has not been
290  *      set or the slot is out range then return NULL.
291  */
292
293 char *mca_get_adapter_name(int slot)
294 {
295         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
296
297         if(!mca_dev)
298                 return NULL;
299
300         return mca_device_get_name(mca_dev);
301 }
302 EXPORT_SYMBOL(mca_get_adapter_name);
303
304 /**
305  *      mca_is_adapter_used - check if claimed by driver
306  *      @slot:  slot to check
307  *
308  *      Returns 1 if the slot has been claimed by a driver
309  */
310
311 int mca_is_adapter_used(int slot)
312 {
313         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
314
315         if(!mca_dev)
316                 return 0;
317
318         return mca_device_claimed(mca_dev);
319 }
320 EXPORT_SYMBOL(mca_is_adapter_used);
321
322 /**
323  *      mca_mark_as_used - claim an MCA device
324  *      @slot:  slot to claim
325  *      FIXME:  should we make this threadsafe
326  *
327  *      Claim an MCA slot for a device driver. If the
328  *      slot is already taken the function returns 1,
329  *      if it is not taken it is claimed and 0 is
330  *      returned.
331  */
332
333 int mca_mark_as_used(int slot)
334 {
335         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
336
337         if(!mca_dev)
338                 /* FIXME: this is actually a severe error */
339                 return 1;
340
341         if(mca_device_claimed(mca_dev))
342                 return 1;
343
344         mca_device_set_claim(mca_dev, 1);
345
346         return 0;
347 }
348 EXPORT_SYMBOL(mca_mark_as_used);
349
350 /**
351  *      mca_mark_as_unused - release an MCA device
352  *      @slot:  slot to claim
353  *
354  *      Release the slot for other drives to use.
355  */
356
357 void mca_mark_as_unused(int slot)
358 {
359         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
360
361         if(!mca_dev)
362                 return;
363
364         mca_device_set_claim(mca_dev, 0);
365 }
366 EXPORT_SYMBOL(mca_mark_as_unused);
367
368 /**
369  *      mca_isadapter - check if the slot holds an adapter
370  *      @slot:  slot to query
371  *
372  *      Returns zero if the slot does not hold an adapter, non zero if
373  *      it does.
374  */
375
376 int mca_isadapter(int slot)
377 {
378         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
379         enum MCA_AdapterStatus status;
380
381         if(!mca_dev)
382                 return 0;
383
384         status = mca_device_status(mca_dev);
385
386         return status == MCA_ADAPTER_NORMAL
387                 || status == MCA_ADAPTER_DISABLED;
388 }
389 EXPORT_SYMBOL(mca_isadapter);
390
391 /**
392  *      mca_isenabled - check if the slot holds an enabled adapter
393  *      @slot:  slot to query
394  *
395  *      Returns a non zero value if the slot holds an enabled adapter
396  *      and zero for any other case.
397  */
398
399 int mca_isenabled(int slot)
400 {
401         struct mca_device *mca_dev = mca_find_device_by_slot(slot);
402
403         if(!mca_dev)
404                 return 0;
405
406         return mca_device_status(mca_dev) == MCA_ADAPTER_NORMAL;
407 }