mca-legacy.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. EXPORT_SYMBOL(mca_find_device_by_slot);
  147. /**
  148. * mca_read_stored_pos - read POS register from boot data
  149. * @slot: slot number to read from
  150. * @reg: register to read from
  151. *
  152. * Fetch a POS value that was stored at boot time by the kernel
  153. * when it scanned the MCA space. The register value is returned.
  154. * Missing or invalid registers report 0.
  155. */
  156. unsigned char mca_read_stored_pos(int slot, int reg)
  157. {
  158. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  159. if(!mca_dev)
  160. return 0;
  161. return mca_device_read_stored_pos(mca_dev, reg);
  162. }
  163. EXPORT_SYMBOL(mca_read_stored_pos);
  164. /**
  165. * mca_read_pos - read POS register from card
  166. * @slot: slot number to read from
  167. * @reg: register to read from
  168. *
  169. * Fetch a POS value directly from the hardware to obtain the
  170. * current value. This is much slower than mca_read_stored_pos and
  171. * may not be invoked from interrupt context. It handles the
  172. * deep magic required for onboard devices transparently.
  173. */
  174. unsigned char mca_read_pos(int slot, int reg)
  175. {
  176. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  177. if(!mca_dev)
  178. return 0;
  179. return mca_device_read_pos(mca_dev, reg);
  180. }
  181. EXPORT_SYMBOL(mca_read_pos);
  182. /**
  183. * mca_write_pos - read POS register from card
  184. * @slot: slot number to read from
  185. * @reg: register to read from
  186. * @byte: byte to write to the POS registers
  187. *
  188. * Store a POS value directly from the hardware. You should not
  189. * normally need to use this function and should have a very good
  190. * knowledge of MCA bus before you do so. Doing this wrongly can
  191. * damage the hardware.
  192. *
  193. * This function may not be used from interrupt context.
  194. *
  195. * Note that this a technically a Bad Thing, as IBM tech stuff says
  196. * you should only set POS values through their utilities.
  197. * However, some devices such as the 3c523 recommend that you write
  198. * back some data to make sure the configuration is consistent.
  199. * I'd say that IBM is right, but I like my drivers to work.
  200. *
  201. * This function can't do checks to see if multiple devices end up
  202. * with the same resources, so you might see magic smoke if someone
  203. * screws up.
  204. */
  205. void mca_write_pos(int slot, int reg, unsigned char byte)
  206. {
  207. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  208. if(!mca_dev)
  209. return;
  210. mca_device_write_pos(mca_dev, reg, byte);
  211. }
  212. EXPORT_SYMBOL(mca_write_pos);
  213. /**
  214. * mca_set_adapter_name - Set the description of the card
  215. * @slot: slot to name
  216. * @name: text string for the namen
  217. *
  218. * This function sets the name reported via /proc for this
  219. * adapter slot. This is for user information only. Setting a
  220. * name deletes any previous name.
  221. */
  222. void mca_set_adapter_name(int slot, char* name)
  223. {
  224. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  225. if(!mca_dev)
  226. return;
  227. mca_device_set_name(mca_dev, name);
  228. }
  229. EXPORT_SYMBOL(mca_set_adapter_name);
  230. /**
  231. * mca_is_adapter_used - check if claimed by driver
  232. * @slot: slot to check
  233. *
  234. * Returns 1 if the slot has been claimed by a driver
  235. */
  236. int mca_is_adapter_used(int slot)
  237. {
  238. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  239. if(!mca_dev)
  240. return 0;
  241. return mca_device_claimed(mca_dev);
  242. }
  243. EXPORT_SYMBOL(mca_is_adapter_used);
  244. /**
  245. * mca_mark_as_used - claim an MCA device
  246. * @slot: slot to claim
  247. * FIXME: should we make this threadsafe
  248. *
  249. * Claim an MCA slot for a device driver. If the
  250. * slot is already taken the function returns 1,
  251. * if it is not taken it is claimed and 0 is
  252. * returned.
  253. */
  254. int mca_mark_as_used(int slot)
  255. {
  256. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  257. if(!mca_dev)
  258. /* FIXME: this is actually a severe error */
  259. return 1;
  260. if(mca_device_claimed(mca_dev))
  261. return 1;
  262. mca_device_set_claim(mca_dev, 1);
  263. return 0;
  264. }
  265. EXPORT_SYMBOL(mca_mark_as_used);
  266. /**
  267. * mca_mark_as_unused - release an MCA device
  268. * @slot: slot to claim
  269. *
  270. * Release the slot for other drives to use.
  271. */
  272. void mca_mark_as_unused(int slot)
  273. {
  274. struct mca_device *mca_dev = mca_find_device_by_slot(slot);
  275. if(!mca_dev)
  276. return;
  277. mca_device_set_claim(mca_dev, 0);
  278. }
  279. EXPORT_SYMBOL(mca_mark_as_unused);