mca-legacy.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /*
  3. * MCA bus support functions for legacy (2.4) API.
  4. *
  5. * Legacy API means the API that operates in terms of MCA slot number
  6. *
  7. * (C) 2002 James Bottomley <James.Bottomley@HansenPartnership.com>
  8. *
  9. **-----------------------------------------------------------------------------
  10. **
  11. ** This program is free software; you can redistribute it and/or modify
  12. ** it under the terms of the GNU General Public License as published by
  13. ** the Free Software Foundation; either version 2 of the License, or
  14. ** (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 of
  18. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ** GNU General Public License for more details.
  20. **
  21. ** You should have received a copy of the GNU General Public License
  22. ** along with this program; if not, write to the Free Software
  23. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. **
  25. **-----------------------------------------------------------------------------
  26. */
  27. #include <linux/module.h>
  28. #include <linux/device.h>
  29. #include <linux/mca-legacy.h>
  30. #include <asm/io.h>
  31. /* NOTE: This structure is stack allocated */
  32. struct mca_find_adapter_info {
  33. int id;
  34. int slot;
  35. struct mca_device *mca_dev;
  36. };
  37. /* The purpose of this iterator is to loop over all the devices and
  38. * find the one with the smallest slot number that's just greater than
  39. * or equal to the required slot with a matching id */
  40. static int mca_find_adapter_callback(struct device *dev, void *data)
  41. {
  42. struct mca_find_adapter_info *info = data;
  43. struct mca_device *mca_dev = to_mca_device(dev);
  44. if(mca_dev->pos_id != info->id)
  45. return 0;
  46. if(mca_dev->slot < info->slot)
  47. return 0;
  48. if(!info->mca_dev || info->mca_dev->slot >= mca_dev->slot)
  49. info->mca_dev = mca_dev;
  50. return 0;
  51. }
  52. /**
  53. * mca_find_adapter - scan for adapters
  54. * @id: MCA identification to search for
  55. * @start: starting slot
  56. *
  57. * Search the MCA configuration for adapters matching the 16bit
  58. * ID given. The first time it should be called with start as zero
  59. * and then further calls made passing the return value of the
  60. * previous call until %MCA_NOTFOUND is returned.
  61. *
  62. * Disabled adapters are not reported.
  63. */
  64. int mca_find_adapter(int id, int start)
  65. {
  66. struct mca_find_adapter_info info;
  67. if(id == 0xffff)
  68. return MCA_NOTFOUND;
  69. info.slot = start;
  70. info.id = id;
  71. info.mca_dev = NULL;
  72. for(;;) {
  73. bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);
  74. if(info.mca_dev == NULL)
  75. return MCA_NOTFOUND;
  76. if(info.mca_dev->status != MCA_ADAPTER_DISABLED)
  77. break;
  78. /* OK, found adapter but it was disabled. Go around
  79. * again, excluding the slot we just found */
  80. info.slot = info.mca_dev->slot + 1;
  81. info.mca_dev = NULL;
  82. }
  83. return info.mca_dev->slot;
  84. }
  85. EXPORT_SYMBOL(mca_find_adapter);
  86. /*--------------------------------------------------------------------*/
  87. /**
  88. * mca_find_unused_adapter - scan for unused adapters
  89. * @id: MCA identification to search for
  90. * @start: starting slot
  91. *
  92. * Search the MCA configuration for adapters matching the 16bit
  93. * ID given. The first time it should be called with start as zero
  94. * and then further calls made passing the return value of the
  95. * previous call until %MCA_NOTFOUND is returned.
  96. *
  97. * Adapters that have been claimed by drivers and those that
  98. * are disabled are not reported. This function thus allows a driver
  99. * to scan for further cards when some may already be driven.
  100. */
  101. int mca_find_unused_adapter(int id, int start)
  102. {
  103. struct mca_find_adapter_info info = { 0 };
  104. if (!MCA_bus || id == 0xffff)
  105. return MCA_NOTFOUND;
  106. info.slot = start;
  107. info.id = id;
  108. info.mca_dev = NULL;
  109. for(;;) {
  110. bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_adapter_callback);
  111. if(info.mca_dev == NULL)
  112. return MCA_NOTFOUND;
  113. if(info.mca_dev->status != MCA_ADAPTER_DISABLED
  114. && !info.mca_dev->driver_loaded)
  115. break;
  116. /* OK, found adapter but it was disabled or already in
  117. * use. Go around again, excluding the slot we just
  118. * found */
  119. info.slot = info.mca_dev->slot + 1;
  120. info.mca_dev = NULL;
  121. }
  122. return info.mca_dev->slot;
  123. }
  124. EXPORT_SYMBOL(mca_find_unused_adapter);
  125. /* NOTE: stack allocated structure */
  126. struct mca_find_device_by_slot_info {
  127. int slot;
  128. struct mca_device *mca_dev;
  129. };
  130. static int mca_find_device_by_slot_callback(struct device *dev, void *data)
  131. {
  132. struct mca_find_device_by_slot_info *info = data;
  133. struct mca_device *mca_dev = to_mca_device(dev);
  134. if(mca_dev->slot == info->slot)
  135. info->mca_dev = mca_dev;
  136. return 0;
  137. }
  138. struct mca_device *mca_find_device_by_slot(int slot)
  139. {
  140. struct mca_find_device_by_slot_info info;
  141. info.slot = slot;
  142. info.mca_dev = NULL;
  143. bus_for_each_dev(&mca_bus_type, NULL, &info, mca_find_device_by_slot_callback);
  144. return info.mca_dev;
  145. }
  146. /**
  147. * mca_read_stored_pos - read POS register from boot data
  148. * @slot: slot number to read from
  149. * @reg: register to read from
  150. *
  151. * Fetch a POS value that was stored at boot time by the kernel
  152. * when it scanned the MCA space. The register value is returned.
  153. * Missing or invalid registers report 0.
  154. */
  155. unsigned char mca_read_stored_pos(int slot, int reg)
  156. {
  157. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  158. if(!mca_dev)
  159. return 0;
  160. return mca_device_read_stored_pos(mca_dev, reg);
  161. }
  162. EXPORT_SYMBOL(mca_read_stored_pos);
  163. /**
  164. * mca_read_pos - read POS register from card
  165. * @slot: slot number to read from
  166. * @reg: register to read from
  167. *
  168. * Fetch a POS value directly from the hardware to obtain the
  169. * current value. This is much slower than mca_read_stored_pos and
  170. * may not be invoked from interrupt context. It handles the
  171. * deep magic required for onboard devices transparently.
  172. */
  173. unsigned char mca_read_pos(int slot, int reg)
  174. {
  175. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  176. if(!mca_dev)
  177. return 0;
  178. return mca_device_read_pos(mca_dev, reg);
  179. }
  180. EXPORT_SYMBOL(mca_read_pos);
  181. /**
  182. * mca_write_pos - read POS register from card
  183. * @slot: slot number to read from
  184. * @reg: register to read from
  185. * @byte: byte to write to the POS registers
  186. *
  187. * Store a POS value directly from the hardware. You should not
  188. * normally need to use this function and should have a very good
  189. * knowledge of MCA bus before you do so. Doing this wrongly can
  190. * damage the hardware.
  191. *
  192. * This function may not be used from interrupt context.
  193. *
  194. * Note that this a technically a Bad Thing, as IBM tech stuff says
  195. * you should only set POS values through their utilities.
  196. * However, some devices such as the 3c523 recommend that you write
  197. * back some data to make sure the configuration is consistent.
  198. * I'd say that IBM is right, but I like my drivers to work.
  199. *
  200. * This function can't do checks to see if multiple devices end up
  201. * with the same resources, so you might see magic smoke if someone
  202. * screws up.
  203. */
  204. void mca_write_pos(int slot, int reg, unsigned char byte)
  205. {
  206. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  207. if(!mca_dev)
  208. return;
  209. mca_device_write_pos(mca_dev, reg, byte);
  210. }
  211. EXPORT_SYMBOL(mca_write_pos);
  212. /**
  213. * mca_set_adapter_name - Set the description of the card
  214. * @slot: slot to name
  215. * @name: text string for the namen
  216. *
  217. * This function sets the name reported via /proc for this
  218. * adapter slot. This is for user information only. Setting a
  219. * name deletes any previous name.
  220. */
  221. void mca_set_adapter_name(int slot, char* name)
  222. {
  223. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  224. if(!mca_dev)
  225. return;
  226. mca_device_set_name(mca_dev, name);
  227. }
  228. EXPORT_SYMBOL(mca_set_adapter_name);
  229. /**
  230. * mca_mark_as_used - claim an MCA device
  231. * @slot: slot to claim
  232. * FIXME: should we make this threadsafe
  233. *
  234. * Claim an MCA slot for a device driver. If the
  235. * slot is already taken the function returns 1,
  236. * if it is not taken it is claimed and 0 is
  237. * returned.
  238. */
  239. int mca_mark_as_used(int slot)
  240. {
  241. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  242. if(!mca_dev)
  243. /* FIXME: this is actually a severe error */
  244. return 1;
  245. if(mca_device_claimed(mca_dev))
  246. return 1;
  247. mca_device_set_claim(mca_dev, 1);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL(mca_mark_as_used);
  251. /**
  252. * mca_mark_as_unused - release an MCA device
  253. * @slot: slot to claim
  254. *
  255. * Release the slot for other drives to use.
  256. */
  257. void mca_mark_as_unused(int slot)
  258. {
  259. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  260. if(!mca_dev)
  261. return;
  262. mca_device_set_claim(mca_dev, 0);
  263. }
  264. EXPORT_SYMBOL(mca_mark_as_unused);